2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.114 2008/09/24 00:53:51 dillon Exp $
38 #include <vm/vm_extern.h>
42 static int hammer_unload_inode(struct hammer_inode *ip);
43 static void hammer_free_inode(hammer_inode_t ip);
44 static void hammer_flush_inode_core(hammer_inode_t ip,
45 hammer_flush_group_t flg, int flags);
46 static int hammer_setup_child_callback(hammer_record_t rec, void *data);
48 static int hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
50 static int hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
51 hammer_flush_group_t flg);
52 static int hammer_setup_parent_inodes_helper(hammer_record_t record,
53 int depth, hammer_flush_group_t flg);
54 static void hammer_inode_wakereclaims(hammer_inode_t ip, int dowake);
57 extern struct hammer_inode *HammerTruncIp;
61 * RB-Tree support for inode structures
64 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
66 if (ip1->obj_localization < ip2->obj_localization)
68 if (ip1->obj_localization > ip2->obj_localization)
70 if (ip1->obj_id < ip2->obj_id)
72 if (ip1->obj_id > ip2->obj_id)
74 if (ip1->obj_asof < ip2->obj_asof)
76 if (ip1->obj_asof > ip2->obj_asof)
82 * RB-Tree support for inode structures / special LOOKUP_INFO
85 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
87 if (info->obj_localization < ip->obj_localization)
89 if (info->obj_localization > ip->obj_localization)
91 if (info->obj_id < ip->obj_id)
93 if (info->obj_id > ip->obj_id)
95 if (info->obj_asof < ip->obj_asof)
97 if (info->obj_asof > ip->obj_asof)
103 * Used by hammer_scan_inode_snapshots() to locate all of an object's
104 * snapshots. Note that the asof field is not tested, which we can get
105 * away with because it is the lowest-priority field.
108 hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
110 hammer_inode_info_t info = data;
112 if (ip->obj_localization > info->obj_localization)
114 if (ip->obj_localization < info->obj_localization)
116 if (ip->obj_id > info->obj_id)
118 if (ip->obj_id < info->obj_id)
124 * Used by hammer_unload_pseudofs() to locate all inodes associated with
128 hammer_inode_pfs_cmp(hammer_inode_t ip, void *data)
130 u_int32_t localization = *(u_int32_t *)data;
131 if (ip->obj_localization > localization)
133 if (ip->obj_localization < localization)
139 * RB-Tree support for pseudofs structures
142 hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
144 if (p1->localization < p2->localization)
146 if (p1->localization > p2->localization)
152 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
153 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
154 hammer_inode_info_cmp, hammer_inode_info_t);
155 RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
156 hammer_pfs_rb_compare, u_int32_t, localization);
159 * The kernel is not actively referencing this vnode but is still holding
162 * This is called from the frontend.
165 hammer_vop_inactive(struct vop_inactive_args *ap)
167 struct hammer_inode *ip = VTOI(ap->a_vp);
178 * If the inode no longer has visibility in the filesystem try to
179 * recycle it immediately, even if the inode is dirty. Recycling
180 * it quickly allows the system to reclaim buffer cache and VM
181 * resources which can matter a lot in a heavily loaded system.
183 * This can deadlock in vfsync() if we aren't careful.
185 * Do not queue the inode to the flusher if we still have visibility,
186 * otherwise namespace calls such as chmod will unnecessarily generate
187 * multiple inode updates.
189 hammer_inode_unloadable_check(ip, 0);
190 if (ip->ino_data.nlinks == 0) {
191 if (ip->flags & HAMMER_INODE_MODMASK)
192 hammer_flush_inode(ip, 0);
199 * Release the vnode association. This is typically (but not always)
200 * the last reference on the inode.
202 * Once the association is lost we are on our own with regards to
203 * flushing the inode.
206 hammer_vop_reclaim(struct vop_reclaim_args *ap)
208 struct hammer_inode *ip;
214 if ((ip = vp->v_data) != NULL) {
219 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
220 ++hammer_count_reclaiming;
221 ++hmp->inode_reclaims;
222 ip->flags |= HAMMER_INODE_RECLAIM;
224 hammer_rel_inode(ip, 1);
230 * Return a locked vnode for the specified inode. The inode must be
231 * referenced but NOT LOCKED on entry and will remain referenced on
234 * Called from the frontend.
237 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
247 if ((vp = ip->vp) == NULL) {
248 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
251 hammer_lock_ex(&ip->lock);
252 if (ip->vp != NULL) {
253 hammer_unlock(&ip->lock);
258 hammer_ref(&ip->lock);
262 obj_type = ip->ino_data.obj_type;
263 vp->v_type = hammer_get_vnode_type(obj_type);
265 hammer_inode_wakereclaims(ip, 0);
267 switch(ip->ino_data.obj_type) {
268 case HAMMER_OBJTYPE_CDEV:
269 case HAMMER_OBJTYPE_BDEV:
270 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
271 addaliasu(vp, ip->ino_data.rmajor,
272 ip->ino_data.rminor);
274 case HAMMER_OBJTYPE_FIFO:
275 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
282 * Only mark as the root vnode if the ip is not
283 * historical, otherwise the VFS cache will get
284 * confused. The other half of the special handling
285 * is in hammer_vop_nlookupdotdot().
287 * Pseudo-filesystem roots can be accessed via
288 * non-root filesystem paths and setting VROOT may
289 * confuse the namecache. Set VPFSROOT instead.
291 if (ip->obj_id == HAMMER_OBJID_ROOT &&
292 ip->obj_asof == hmp->asof) {
293 if (ip->obj_localization == 0)
296 vp->v_flag |= VPFSROOT;
299 vp->v_data = (void *)ip;
300 /* vnode locked by getnewvnode() */
301 /* make related vnode dirty if inode dirty? */
302 hammer_unlock(&ip->lock);
303 if (vp->v_type == VREG)
304 vinitvmio(vp, ip->ino_data.size);
309 * loop if the vget fails (aka races), or if the vp
310 * no longer matches ip->vp.
312 if (vget(vp, LK_EXCLUSIVE) == 0) {
323 * Locate all copies of the inode for obj_id compatible with the specified
324 * asof, reference, and issue the related call-back. This routine is used
325 * for direct-io invalidation and does not create any new inodes.
328 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
329 int (*callback)(hammer_inode_t ip, void *data),
332 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
333 hammer_inode_info_cmp_all_history,
338 * Acquire a HAMMER inode. The returned inode is not locked. These functions
339 * do not attach or detach the related vnode (use hammer_get_vnode() for
342 * The flags argument is only applied for newly created inodes, and only
343 * certain flags are inherited.
345 * Called from the frontend.
347 struct hammer_inode *
348 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
349 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
350 int flags, int *errorp)
352 hammer_mount_t hmp = trans->hmp;
353 struct hammer_inode_info iinfo;
354 struct hammer_cursor cursor;
355 struct hammer_inode *ip;
359 * Determine if we already have an inode cached. If we do then
362 * If we find an inode with no vnode we have to mark the
363 * transaction such that hammer_inode_waitreclaims() is
364 * called later on to avoid building up an infinite number
365 * of inodes. Otherwise we can continue to * add new inodes
366 * faster then they can be disposed of, even with the tsleep
369 * If we find a dummy inode we return a failure so dounlink
370 * (which does another lookup) doesn't try to mess with the
371 * link count. hammer_vop_nresolve() uses hammer_get_dummy_inode()
372 * to ref dummy inodes.
374 iinfo.obj_id = obj_id;
375 iinfo.obj_asof = asof;
376 iinfo.obj_localization = localization;
378 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
380 if (ip->flags & HAMMER_INODE_DUMMY) {
384 hammer_ref(&ip->lock);
390 * Allocate a new inode structure and deal with races later.
392 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
393 ++hammer_count_inodes;
396 ip->obj_asof = iinfo.obj_asof;
397 ip->obj_localization = localization;
399 ip->flags = flags & HAMMER_INODE_RO;
400 ip->cache[0].ip = ip;
401 ip->cache[1].ip = ip;
403 ip->flags |= HAMMER_INODE_RO;
404 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
405 0x7FFFFFFFFFFFFFFFLL;
406 RB_INIT(&ip->rec_tree);
407 TAILQ_INIT(&ip->target_list);
408 hammer_ref(&ip->lock);
411 * Locate the on-disk inode. If this is a PFS root we always
412 * access the current version of the root inode and (if it is not
413 * a master) always access information under it with a snapshot
417 hammer_init_cursor(trans, &cursor, (dip ? &dip->cache[0] : NULL), NULL);
418 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
419 cursor.key_beg.obj_id = ip->obj_id;
420 cursor.key_beg.key = 0;
421 cursor.key_beg.create_tid = 0;
422 cursor.key_beg.delete_tid = 0;
423 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
424 cursor.key_beg.obj_type = 0;
426 cursor.asof = iinfo.obj_asof;
427 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
430 *errorp = hammer_btree_lookup(&cursor);
431 if (*errorp == EDEADLK) {
432 hammer_done_cursor(&cursor);
437 * On success the B-Tree lookup will hold the appropriate
438 * buffer cache buffers and provide a pointer to the requested
439 * information. Copy the information to the in-memory inode
440 * and cache the B-Tree node to improve future operations.
443 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
444 ip->ino_data = cursor.data->inode;
447 * cache[0] tries to cache the location of the object inode.
448 * The assumption is that it is near the directory inode.
450 * cache[1] tries to cache the location of the object data.
451 * The assumption is that it is near the directory data.
453 hammer_cache_node(&ip->cache[0], cursor.node);
454 if (dip && dip->cache[1].node)
455 hammer_cache_node(&ip->cache[1], dip->cache[1].node);
458 * The file should not contain any data past the file size
459 * stored in the inode. Setting save_trunc_off to the
460 * file size instead of max reduces B-Tree lookup overheads
461 * on append by allowing the flusher to avoid checking for
464 ip->save_trunc_off = ip->ino_data.size;
467 * Locate and assign the pseudofs management structure to
470 if (dip && dip->obj_localization == ip->obj_localization) {
471 ip->pfsm = dip->pfsm;
472 hammer_ref(&ip->pfsm->lock);
474 ip->pfsm = hammer_load_pseudofs(trans,
475 ip->obj_localization,
477 *errorp = 0; /* ignore ENOENT */
482 * The inode is placed on the red-black tree and will be synced to
483 * the media when flushed or by the filesystem sync. If this races
484 * another instantiation/lookup the insertion will fail.
487 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
488 hammer_free_inode(ip);
489 hammer_done_cursor(&cursor);
492 ip->flags |= HAMMER_INODE_ONDISK;
494 if (ip->flags & HAMMER_INODE_RSV_INODES) {
495 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
499 hammer_free_inode(ip);
502 hammer_done_cursor(&cursor);
503 trans->flags |= HAMMER_TRANSF_NEWINODE;
508 * Get a dummy inode to placemark a broken directory entry.
510 struct hammer_inode *
511 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
512 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
513 int flags, int *errorp)
515 hammer_mount_t hmp = trans->hmp;
516 struct hammer_inode_info iinfo;
517 struct hammer_inode *ip;
520 * Determine if we already have an inode cached. If we do then
523 * If we find an inode with no vnode we have to mark the
524 * transaction such that hammer_inode_waitreclaims() is
525 * called later on to avoid building up an infinite number
526 * of inodes. Otherwise we can continue to * add new inodes
527 * faster then they can be disposed of, even with the tsleep
530 * If we find a non-fake inode we return an error. Only fake
531 * inodes can be returned by this routine.
533 iinfo.obj_id = obj_id;
534 iinfo.obj_asof = asof;
535 iinfo.obj_localization = localization;
538 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
540 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
544 hammer_ref(&ip->lock);
549 * Allocate a new inode structure and deal with races later.
551 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
552 ++hammer_count_inodes;
555 ip->obj_asof = iinfo.obj_asof;
556 ip->obj_localization = localization;
558 ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
559 ip->cache[0].ip = ip;
560 ip->cache[1].ip = ip;
561 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
562 0x7FFFFFFFFFFFFFFFLL;
563 RB_INIT(&ip->rec_tree);
564 TAILQ_INIT(&ip->target_list);
565 hammer_ref(&ip->lock);
568 * Populate the dummy inode. Leave everything zero'd out.
570 * (ip->ino_leaf and ip->ino_data)
572 * Make the dummy inode a FIFO object which most copy programs
573 * will properly ignore.
575 ip->save_trunc_off = ip->ino_data.size;
576 ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
579 * Locate and assign the pseudofs management structure to
582 if (dip && dip->obj_localization == ip->obj_localization) {
583 ip->pfsm = dip->pfsm;
584 hammer_ref(&ip->pfsm->lock);
586 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
588 *errorp = 0; /* ignore ENOENT */
592 * The inode is placed on the red-black tree and will be synced to
593 * the media when flushed or by the filesystem sync. If this races
594 * another instantiation/lookup the insertion will fail.
596 * NOTE: Do not set HAMMER_INODE_ONDISK. The inode is a fake.
599 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
600 hammer_free_inode(ip);
604 if (ip->flags & HAMMER_INODE_RSV_INODES) {
605 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
608 hammer_free_inode(ip);
611 trans->flags |= HAMMER_TRANSF_NEWINODE;
616 * Create a new filesystem object, returning the inode in *ipp. The
617 * returned inode will be referenced. The inode is created in-memory.
619 * If pfsm is non-NULL the caller wishes to create the root inode for
623 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
624 struct ucred *cred, hammer_inode_t dip,
625 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
634 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
635 ++hammer_count_inodes;
637 trans->flags |= HAMMER_TRANSF_NEWINODE;
640 KKASSERT(pfsm->localization != 0);
641 ip->obj_id = HAMMER_OBJID_ROOT;
642 ip->obj_localization = pfsm->localization;
644 KKASSERT(dip != NULL);
645 ip->obj_id = hammer_alloc_objid(hmp, dip);
646 ip->obj_localization = dip->obj_localization;
649 KKASSERT(ip->obj_id != 0);
650 ip->obj_asof = hmp->asof;
652 ip->flush_state = HAMMER_FST_IDLE;
653 ip->flags = HAMMER_INODE_DDIRTY |
654 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
655 ip->cache[0].ip = ip;
656 ip->cache[1].ip = ip;
658 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
659 /* ip->save_trunc_off = 0; (already zero) */
660 RB_INIT(&ip->rec_tree);
661 TAILQ_INIT(&ip->target_list);
663 ip->ino_data.atime = trans->time;
664 ip->ino_data.mtime = trans->time;
665 ip->ino_data.size = 0;
666 ip->ino_data.nlinks = 0;
669 * A nohistory designator on the parent directory is inherited by
670 * the child. We will do this even for pseudo-fs creation... the
671 * sysad can turn it off.
674 ip->ino_data.uflags = dip->ino_data.uflags &
675 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
678 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
679 ip->ino_leaf.base.localization = ip->obj_localization +
680 HAMMER_LOCALIZE_INODE;
681 ip->ino_leaf.base.obj_id = ip->obj_id;
682 ip->ino_leaf.base.key = 0;
683 ip->ino_leaf.base.create_tid = 0;
684 ip->ino_leaf.base.delete_tid = 0;
685 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
686 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
688 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
689 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
690 ip->ino_data.mode = vap->va_mode;
691 ip->ino_data.ctime = trans->time;
694 * If we are running version 2 or greater we use dirhash algorithm #1
695 * which is semi-sorted. Algorithm #0 was just a pure crc.
697 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
698 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
699 ip->ino_data.cap_flags |= HAMMER_INODE_CAP_DIRHASH_ALG1;
704 * Setup the ".." pointer. This only needs to be done for directories
705 * but we do it for all objects as a recovery aid.
708 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
711 * The parent_obj_localization field only applies to pseudo-fs roots.
712 * XXX this is no longer applicable, PFSs are no longer directly
713 * tied into the parent's directory structure.
715 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
716 ip->obj_id == HAMMER_OBJID_ROOT) {
717 ip->ino_data.ext.obj.parent_obj_localization =
718 dip->obj_localization;
722 switch(ip->ino_leaf.base.obj_type) {
723 case HAMMER_OBJTYPE_CDEV:
724 case HAMMER_OBJTYPE_BDEV:
725 ip->ino_data.rmajor = vap->va_rmajor;
726 ip->ino_data.rminor = vap->va_rminor;
733 * Calculate default uid/gid and overwrite with information from
737 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
738 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
739 xuid, cred, &vap->va_mode);
743 ip->ino_data.mode = vap->va_mode;
745 if (vap->va_vaflags & VA_UID_UUID_VALID)
746 ip->ino_data.uid = vap->va_uid_uuid;
747 else if (vap->va_uid != (uid_t)VNOVAL)
748 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
750 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
752 if (vap->va_vaflags & VA_GID_UUID_VALID)
753 ip->ino_data.gid = vap->va_gid_uuid;
754 else if (vap->va_gid != (gid_t)VNOVAL)
755 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
757 ip->ino_data.gid = dip->ino_data.gid;
759 hammer_ref(&ip->lock);
763 hammer_ref(&pfsm->lock);
765 } else if (dip->obj_localization == ip->obj_localization) {
766 ip->pfsm = dip->pfsm;
767 hammer_ref(&ip->pfsm->lock);
770 ip->pfsm = hammer_load_pseudofs(trans,
771 ip->obj_localization,
773 error = 0; /* ignore ENOENT */
777 hammer_free_inode(ip);
779 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
780 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
782 hammer_free_inode(ip);
789 * Final cleanup / freeing of an inode structure
792 hammer_free_inode(hammer_inode_t ip)
794 struct hammer_mount *hmp;
797 KKASSERT(ip->lock.refs == 1);
798 hammer_uncache_node(&ip->cache[0]);
799 hammer_uncache_node(&ip->cache[1]);
800 hammer_inode_wakereclaims(ip, 1);
802 hammer_clear_objid(ip);
803 --hammer_count_inodes;
806 hammer_rel_pseudofs(hmp, ip->pfsm);
809 kfree(ip, hmp->m_inodes);
814 * Retrieve pseudo-fs data. NULL will never be returned.
816 * If an error occurs *errorp will be set and a default template is returned,
817 * otherwise *errorp is set to 0. Typically when an error occurs it will
820 hammer_pseudofs_inmem_t
821 hammer_load_pseudofs(hammer_transaction_t trans,
822 u_int32_t localization, int *errorp)
824 hammer_mount_t hmp = trans->hmp;
826 hammer_pseudofs_inmem_t pfsm;
827 struct hammer_cursor cursor;
831 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
833 hammer_ref(&pfsm->lock);
839 * PFS records are stored in the root inode (not the PFS root inode,
840 * but the real root). Avoid an infinite recursion if loading
841 * the PFS for the real root.
844 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
846 HAMMER_DEF_LOCALIZATION, 0, errorp);
851 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
852 pfsm->localization = localization;
853 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
854 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
856 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
857 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
858 HAMMER_LOCALIZE_MISC;
859 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
860 cursor.key_beg.create_tid = 0;
861 cursor.key_beg.delete_tid = 0;
862 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
863 cursor.key_beg.obj_type = 0;
864 cursor.key_beg.key = localization;
865 cursor.asof = HAMMER_MAX_TID;
866 cursor.flags |= HAMMER_CURSOR_ASOF;
869 *errorp = hammer_ip_lookup(&cursor);
871 *errorp = hammer_btree_lookup(&cursor);
873 *errorp = hammer_ip_resolve_data(&cursor);
875 if (cursor.data->pfsd.mirror_flags &
876 HAMMER_PFSD_DELETED) {
879 bytes = cursor.leaf->data_len;
880 if (bytes > sizeof(pfsm->pfsd))
881 bytes = sizeof(pfsm->pfsd);
882 bcopy(cursor.data, &pfsm->pfsd, bytes);
886 hammer_done_cursor(&cursor);
888 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
889 hammer_ref(&pfsm->lock);
891 hammer_rel_inode(ip, 0);
892 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
893 kfree(pfsm, hmp->m_misc);
900 * Store pseudo-fs data. The backend will automatically delete any prior
901 * on-disk pseudo-fs data but we have to delete in-memory versions.
904 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
906 struct hammer_cursor cursor;
907 hammer_record_t record;
911 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
912 HAMMER_DEF_LOCALIZATION, 0, &error);
914 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
915 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
916 cursor.key_beg.localization = ip->obj_localization +
917 HAMMER_LOCALIZE_MISC;
918 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
919 cursor.key_beg.create_tid = 0;
920 cursor.key_beg.delete_tid = 0;
921 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
922 cursor.key_beg.obj_type = 0;
923 cursor.key_beg.key = pfsm->localization;
924 cursor.asof = HAMMER_MAX_TID;
925 cursor.flags |= HAMMER_CURSOR_ASOF;
928 * Replace any in-memory version of the record.
930 error = hammer_ip_lookup(&cursor);
931 if (error == 0 && hammer_cursor_inmem(&cursor)) {
932 record = cursor.iprec;
933 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
934 KKASSERT(cursor.deadlk_rec == NULL);
935 hammer_ref(&record->lock);
936 cursor.deadlk_rec = record;
939 record->flags |= HAMMER_RECF_DELETED_FE;
945 * Allocate replacement general record. The backend flush will
946 * delete any on-disk version of the record.
948 if (error == 0 || error == ENOENT) {
949 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
950 record->type = HAMMER_MEM_RECORD_GENERAL;
952 record->leaf.base.localization = ip->obj_localization +
953 HAMMER_LOCALIZE_MISC;
954 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
955 record->leaf.base.key = pfsm->localization;
956 record->leaf.data_len = sizeof(pfsm->pfsd);
957 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
958 error = hammer_ip_add_record(trans, record);
960 hammer_done_cursor(&cursor);
961 if (error == EDEADLK)
963 hammer_rel_inode(ip, 0);
968 * Create a root directory for a PFS if one does not alredy exist.
970 * The PFS root stands alone so we must also bump the nlinks count
971 * to prevent it from being destroyed on release.
974 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
975 hammer_pseudofs_inmem_t pfsm)
981 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
982 pfsm->localization, 0, &error);
987 error = hammer_create_inode(trans, &vap, cred, NULL, pfsm, &ip);
989 ++ip->ino_data.nlinks;
990 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
994 hammer_rel_inode(ip, 0);
999 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1000 * if we are unable to disassociate all the inodes.
1004 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1008 hammer_ref(&ip->lock);
1009 if (ip->lock.refs == 2 && ip->vp)
1010 vclean_unlocked(ip->vp);
1011 if (ip->lock.refs == 1 && ip->vp == NULL)
1014 res = -1; /* stop, someone is using the inode */
1015 hammer_rel_inode(ip, 0);
1020 hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
1025 for (try = res = 0; try < 4; ++try) {
1026 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1027 hammer_inode_pfs_cmp,
1028 hammer_unload_pseudofs_callback,
1030 if (res == 0 && try > 1)
1032 hammer_flusher_sync(trans->hmp);
1041 * Release a reference on a PFS
1044 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1046 hammer_unref(&pfsm->lock);
1047 if (pfsm->lock.refs == 0) {
1048 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1049 kfree(pfsm, hmp->m_misc);
1054 * Called by hammer_sync_inode().
1057 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1059 hammer_transaction_t trans = cursor->trans;
1060 hammer_record_t record;
1068 * If the inode has a presence on-disk then locate it and mark
1069 * it deleted, setting DELONDISK.
1071 * The record may or may not be physically deleted, depending on
1072 * the retention policy.
1074 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1075 HAMMER_INODE_ONDISK) {
1076 hammer_normalize_cursor(cursor);
1077 cursor->key_beg.localization = ip->obj_localization +
1078 HAMMER_LOCALIZE_INODE;
1079 cursor->key_beg.obj_id = ip->obj_id;
1080 cursor->key_beg.key = 0;
1081 cursor->key_beg.create_tid = 0;
1082 cursor->key_beg.delete_tid = 0;
1083 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1084 cursor->key_beg.obj_type = 0;
1085 cursor->asof = ip->obj_asof;
1086 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1087 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1088 cursor->flags |= HAMMER_CURSOR_BACKEND;
1090 error = hammer_btree_lookup(cursor);
1091 if (hammer_debug_inode)
1092 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1095 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1096 if (hammer_debug_inode)
1097 kprintf(" error %d\n", error);
1099 ip->flags |= HAMMER_INODE_DELONDISK;
1102 hammer_cache_node(&ip->cache[0], cursor->node);
1104 if (error == EDEADLK) {
1105 hammer_done_cursor(cursor);
1106 error = hammer_init_cursor(trans, cursor,
1108 if (hammer_debug_inode)
1109 kprintf("IPDED %p %d\n", ip, error);
1116 * Ok, write out the initial record or a new record (after deleting
1117 * the old one), unless the DELETED flag is set. This routine will
1118 * clear DELONDISK if it writes out a record.
1120 * Update our inode statistics if this is the first application of
1121 * the inode on-disk.
1123 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1125 * Generate a record and write it to the media. We clean-up
1126 * the state before releasing so we do not have to set-up
1129 record = hammer_alloc_mem_record(ip, 0);
1130 record->type = HAMMER_MEM_RECORD_INODE;
1131 record->flush_state = HAMMER_FST_FLUSH;
1132 record->leaf = ip->sync_ino_leaf;
1133 record->leaf.base.create_tid = trans->tid;
1134 record->leaf.data_len = sizeof(ip->sync_ino_data);
1135 record->leaf.create_ts = trans->time32;
1136 record->data = (void *)&ip->sync_ino_data;
1137 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1140 * If this flag is set we cannot sync the new file size
1141 * because we haven't finished related truncations. The
1142 * inode will be flushed in another flush group to finish
1145 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1146 ip->sync_ino_data.size != ip->ino_data.size) {
1148 ip->sync_ino_data.size = ip->ino_data.size;
1154 error = hammer_ip_sync_record_cursor(cursor, record);
1155 if (hammer_debug_inode)
1156 kprintf("GENREC %p rec %08x %d\n",
1157 ip, record->flags, error);
1158 if (error != EDEADLK)
1160 hammer_done_cursor(cursor);
1161 error = hammer_init_cursor(trans, cursor,
1163 if (hammer_debug_inode)
1164 kprintf("GENREC reinit %d\n", error);
1170 * Note: The record was never on the inode's record tree
1171 * so just wave our hands importantly and destroy it.
1173 record->flags |= HAMMER_RECF_COMMITTED;
1174 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1175 record->flush_state = HAMMER_FST_IDLE;
1176 ++ip->rec_generation;
1177 hammer_rel_mem_record(record);
1183 if (hammer_debug_inode)
1184 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1185 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1186 HAMMER_INODE_ATIME |
1187 HAMMER_INODE_MTIME);
1188 ip->flags &= ~HAMMER_INODE_DELONDISK;
1190 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1193 * Root volume count of inodes
1195 hammer_sync_lock_sh(trans);
1196 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1197 hammer_modify_volume_field(trans,
1200 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1201 hammer_modify_volume_done(trans->rootvol);
1202 ip->flags |= HAMMER_INODE_ONDISK;
1203 if (hammer_debug_inode)
1204 kprintf("NOWONDISK %p\n", ip);
1206 hammer_sync_unlock(trans);
1211 * If the inode has been destroyed, clean out any left-over flags
1212 * that may have been set by the frontend.
1214 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1215 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1216 HAMMER_INODE_ATIME |
1217 HAMMER_INODE_MTIME);
1223 * Update only the itimes fields.
1225 * ATIME can be updated without generating any UNDO. MTIME is updated
1226 * with UNDO so it is guaranteed to be synchronized properly in case of
1229 * Neither field is included in the B-Tree leaf element's CRC, which is how
1230 * we can get away with updating ATIME the way we do.
1233 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1235 hammer_transaction_t trans = cursor->trans;
1239 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1240 HAMMER_INODE_ONDISK) {
1244 hammer_normalize_cursor(cursor);
1245 cursor->key_beg.localization = ip->obj_localization +
1246 HAMMER_LOCALIZE_INODE;
1247 cursor->key_beg.obj_id = ip->obj_id;
1248 cursor->key_beg.key = 0;
1249 cursor->key_beg.create_tid = 0;
1250 cursor->key_beg.delete_tid = 0;
1251 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1252 cursor->key_beg.obj_type = 0;
1253 cursor->asof = ip->obj_asof;
1254 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1255 cursor->flags |= HAMMER_CURSOR_ASOF;
1256 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1257 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1258 cursor->flags |= HAMMER_CURSOR_BACKEND;
1260 error = hammer_btree_lookup(cursor);
1262 hammer_cache_node(&ip->cache[0], cursor->node);
1263 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1265 * Updating MTIME requires an UNDO. Just cover
1266 * both atime and mtime.
1268 hammer_sync_lock_sh(trans);
1269 hammer_modify_buffer(trans, cursor->data_buffer,
1270 HAMMER_ITIMES_BASE(&cursor->data->inode),
1271 HAMMER_ITIMES_BYTES);
1272 cursor->data->inode.atime = ip->sync_ino_data.atime;
1273 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1274 hammer_modify_buffer_done(cursor->data_buffer);
1275 hammer_sync_unlock(trans);
1276 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1278 * Updating atime only can be done in-place with
1281 hammer_sync_lock_sh(trans);
1282 hammer_modify_buffer(trans, cursor->data_buffer,
1284 cursor->data->inode.atime = ip->sync_ino_data.atime;
1285 hammer_modify_buffer_done(cursor->data_buffer);
1286 hammer_sync_unlock(trans);
1288 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1290 if (error == EDEADLK) {
1291 hammer_done_cursor(cursor);
1292 error = hammer_init_cursor(trans, cursor,
1301 * Release a reference on an inode, flush as requested.
1303 * On the last reference we queue the inode to the flusher for its final
1307 hammer_rel_inode(struct hammer_inode *ip, int flush)
1309 /*hammer_mount_t hmp = ip->hmp;*/
1312 * Handle disposition when dropping the last ref.
1315 if (ip->lock.refs == 1) {
1317 * Determine whether on-disk action is needed for
1318 * the inode's final disposition.
1320 KKASSERT(ip->vp == NULL);
1321 hammer_inode_unloadable_check(ip, 0);
1322 if (ip->flags & HAMMER_INODE_MODMASK) {
1323 hammer_flush_inode(ip, 0);
1324 } else if (ip->lock.refs == 1) {
1325 hammer_unload_inode(ip);
1330 hammer_flush_inode(ip, 0);
1333 * The inode still has multiple refs, try to drop
1336 KKASSERT(ip->lock.refs >= 1);
1337 if (ip->lock.refs > 1) {
1338 hammer_unref(&ip->lock);
1346 * Unload and destroy the specified inode. Must be called with one remaining
1347 * reference. The reference is disposed of.
1349 * The inode must be completely clean.
1352 hammer_unload_inode(struct hammer_inode *ip)
1354 hammer_mount_t hmp = ip->hmp;
1356 KASSERT(ip->lock.refs == 1,
1357 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1358 KKASSERT(ip->vp == NULL);
1359 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1360 KKASSERT(ip->cursor_ip_refs == 0);
1361 KKASSERT(ip->lock.lockcount == 0);
1362 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1364 KKASSERT(RB_EMPTY(&ip->rec_tree));
1365 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1367 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1369 hammer_free_inode(ip);
1374 * Called during unmounting if a critical error occured. The in-memory
1375 * inode and all related structures are destroyed.
1377 * If a critical error did not occur the unmount code calls the standard
1378 * release and asserts that the inode is gone.
1381 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1383 hammer_record_t rec;
1386 * Get rid of the inodes in-memory records, regardless of their
1387 * state, and clear the mod-mask.
1389 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1390 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1391 rec->target_ip = NULL;
1392 if (rec->flush_state == HAMMER_FST_SETUP)
1393 rec->flush_state = HAMMER_FST_IDLE;
1395 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1396 if (rec->flush_state == HAMMER_FST_FLUSH)
1397 --rec->flush_group->refs;
1399 hammer_ref(&rec->lock);
1400 KKASSERT(rec->lock.refs == 1);
1401 rec->flush_state = HAMMER_FST_IDLE;
1402 rec->flush_group = NULL;
1403 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1404 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1405 ++ip->rec_generation;
1406 hammer_rel_mem_record(rec);
1408 ip->flags &= ~HAMMER_INODE_MODMASK;
1409 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1410 KKASSERT(ip->vp == NULL);
1413 * Remove the inode from any flush group, force it idle. FLUSH
1414 * and SETUP states have an inode ref.
1416 switch(ip->flush_state) {
1417 case HAMMER_FST_FLUSH:
1418 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
1419 --ip->flush_group->refs;
1420 ip->flush_group = NULL;
1422 case HAMMER_FST_SETUP:
1423 hammer_unref(&ip->lock);
1424 ip->flush_state = HAMMER_FST_IDLE;
1426 case HAMMER_FST_IDLE:
1431 * There shouldn't be any associated vnode. The unload needs at
1432 * least one ref, if we do have a vp steal its ip ref.
1435 kprintf("hammer_destroy_inode_callback: Unexpected "
1436 "vnode association ip %p vp %p\n", ip, ip->vp);
1437 ip->vp->v_data = NULL;
1440 hammer_ref(&ip->lock);
1442 hammer_unload_inode(ip);
1447 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1448 * the read-only flag for cached inodes.
1450 * This routine is called from a RB_SCAN().
1453 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1455 hammer_mount_t hmp = ip->hmp;
1457 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1458 ip->flags |= HAMMER_INODE_RO;
1460 ip->flags &= ~HAMMER_INODE_RO;
1465 * A transaction has modified an inode, requiring updates as specified by
1468 * HAMMER_INODE_DDIRTY: Inode data has been updated
1469 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1470 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1471 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1472 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1475 hammer_modify_inode(hammer_inode_t ip, int flags)
1478 * ronly of 0 or 2 does not trigger assertion.
1479 * 2 is a special error state
1481 KKASSERT(ip->hmp->ronly != 1 ||
1482 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1483 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1484 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1485 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1486 ip->flags |= HAMMER_INODE_RSV_INODES;
1487 ++ip->hmp->rsv_inodes;
1494 * Request that an inode be flushed. This whole mess cannot block and may
1495 * recurse (if not synchronous). Once requested HAMMER will attempt to
1496 * actively flush the inode until the flush can be done.
1498 * The inode may already be flushing, or may be in a setup state. We can
1499 * place the inode in a flushing state if it is currently idle and flag it
1500 * to reflush if it is currently flushing.
1502 * Upon return if the inode could not be flushed due to a setup
1503 * dependancy, then it will be automatically flushed when the dependancy
1507 hammer_flush_inode(hammer_inode_t ip, int flags)
1510 hammer_flush_group_t flg;
1514 * next_flush_group is the first flush group we can place the inode
1515 * in. It may be NULL. If it becomes full we append a new flush
1516 * group and make that the next_flush_group.
1519 while ((flg = hmp->next_flush_group) != NULL) {
1520 KKASSERT(flg->running == 0);
1521 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit)
1523 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
1524 hammer_flusher_async(ip->hmp, flg);
1527 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1528 hmp->next_flush_group = flg;
1529 TAILQ_INIT(&flg->flush_list);
1530 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1534 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1535 * state we have to put it back into an IDLE state so we can
1536 * drop the extra ref.
1538 * If we have a parent dependancy we must still fall through
1541 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1542 if (ip->flush_state == HAMMER_FST_SETUP &&
1543 TAILQ_EMPTY(&ip->target_list)) {
1544 ip->flush_state = HAMMER_FST_IDLE;
1545 hammer_rel_inode(ip, 0);
1547 if (ip->flush_state == HAMMER_FST_IDLE)
1552 * Our flush action will depend on the current state.
1554 switch(ip->flush_state) {
1555 case HAMMER_FST_IDLE:
1557 * We have no dependancies and can flush immediately. Some
1558 * our children may not be flushable so we have to re-test
1559 * with that additional knowledge.
1561 hammer_flush_inode_core(ip, flg, flags);
1563 case HAMMER_FST_SETUP:
1565 * Recurse upwards through dependancies via target_list
1566 * and start their flusher actions going if possible.
1568 * 'good' is our connectivity. -1 means we have none and
1569 * can't flush, 0 means there weren't any dependancies, and
1570 * 1 means we have good connectivity.
1572 good = hammer_setup_parent_inodes(ip, 0, flg);
1576 * We can continue if good >= 0. Determine how
1577 * many records under our inode can be flushed (and
1580 hammer_flush_inode_core(ip, flg, flags);
1583 * Parent has no connectivity, tell it to flush
1584 * us as soon as it does.
1586 * The REFLUSH flag is also needed to trigger
1587 * dependancy wakeups.
1589 ip->flags |= HAMMER_INODE_CONN_DOWN |
1590 HAMMER_INODE_REFLUSH;
1591 if (flags & HAMMER_FLUSH_SIGNAL) {
1592 ip->flags |= HAMMER_INODE_RESIGNAL;
1593 hammer_flusher_async(ip->hmp, flg);
1597 case HAMMER_FST_FLUSH:
1599 * We are already flushing, flag the inode to reflush
1600 * if needed after it completes its current flush.
1602 * The REFLUSH flag is also needed to trigger
1603 * dependancy wakeups.
1605 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1606 ip->flags |= HAMMER_INODE_REFLUSH;
1607 if (flags & HAMMER_FLUSH_SIGNAL) {
1608 ip->flags |= HAMMER_INODE_RESIGNAL;
1609 hammer_flusher_async(ip->hmp, flg);
1616 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1617 * ip which reference our ip.
1619 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1620 * so for now do not ref/deref the structures. Note that if we use the
1621 * ref/rel code later, the rel CAN block.
1624 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1625 hammer_flush_group_t flg)
1627 hammer_record_t depend;
1632 * If we hit our recursion limit and we have parent dependencies
1633 * We cannot continue. Returning < 0 will cause us to be flagged
1634 * for reflush. Returning -2 cuts off additional dependency checks
1635 * because they are likely to also hit the depth limit.
1637 * We cannot return < 0 if there are no dependencies or there might
1638 * not be anything to wakeup (ip).
1640 if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1641 kprintf("HAMMER Warning: depth limit reached on "
1642 "setup recursion, inode %p %016llx\n",
1643 ip, (long long)ip->obj_id);
1651 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1652 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1653 KKASSERT(depend->target_ip == ip);
1654 if (r < 0 && good == 0)
1660 * If we failed due to the recursion depth limit then stop
1670 * This helper function takes a record representing the dependancy between
1671 * the parent inode and child inode.
1673 * record->ip = parent inode
1674 * record->target_ip = child inode
1676 * We are asked to recurse upwards and convert the record from SETUP
1677 * to FLUSH if possible.
1679 * Return 1 if the record gives us connectivity
1681 * Return 0 if the record is not relevant
1683 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1686 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1687 hammer_flush_group_t flg)
1693 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1698 * If the record is already flushing, is it in our flush group?
1700 * If it is in our flush group but it is a general record or a
1701 * delete-on-disk, it does not improve our connectivity (return 0),
1702 * and if the target inode is not trying to destroy itself we can't
1703 * allow the operation yet anyway (the second return -1).
1705 if (record->flush_state == HAMMER_FST_FLUSH) {
1707 * If not in our flush group ask the parent to reflush
1708 * us as soon as possible.
1710 if (record->flush_group != flg) {
1711 pip->flags |= HAMMER_INODE_REFLUSH;
1712 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1717 * If in our flush group everything is already set up,
1718 * just return whether the record will improve our
1719 * visibility or not.
1721 if (record->type == HAMMER_MEM_RECORD_ADD)
1727 * It must be a setup record. Try to resolve the setup dependancies
1728 * by recursing upwards so we can place ip on the flush list.
1730 * Limit ourselves to 20 levels of recursion to avoid blowing out
1731 * the kernel stack. If we hit the recursion limit we can't flush
1732 * until the parent flushes. The parent will flush independantly
1733 * on its own and ultimately a deep recursion will be resolved.
1735 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1737 good = hammer_setup_parent_inodes(pip, depth + 1, flg);
1740 * If good < 0 the parent has no connectivity and we cannot safely
1741 * flush the directory entry, which also means we can't flush our
1742 * ip. Flag us for downward recursion once the parent's
1743 * connectivity is resolved. Flag the parent for [re]flush or it
1744 * may not check for downward recursions.
1747 pip->flags |= HAMMER_INODE_REFLUSH;
1748 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1753 * We are go, place the parent inode in a flushing state so we can
1754 * place its record in a flushing state. Note that the parent
1755 * may already be flushing. The record must be in the same flush
1756 * group as the parent.
1758 if (pip->flush_state != HAMMER_FST_FLUSH)
1759 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
1760 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1761 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1764 if (record->type == HAMMER_MEM_RECORD_DEL &&
1765 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1767 * Regardless of flushing state we cannot sync this path if the
1768 * record represents a delete-on-disk but the target inode
1769 * is not ready to sync its own deletion.
1771 * XXX need to count effective nlinks to determine whether
1772 * the flush is ok, otherwise removing a hardlink will
1773 * just leave the DEL record to rot.
1775 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1779 if (pip->flush_group == flg) {
1781 * Because we have not calculated nlinks yet we can just
1782 * set records to the flush state if the parent is in
1783 * the same flush group as we are.
1785 record->flush_state = HAMMER_FST_FLUSH;
1786 record->flush_group = flg;
1787 ++record->flush_group->refs;
1788 hammer_ref(&record->lock);
1791 * A general directory-add contributes to our visibility.
1793 * Otherwise it is probably a directory-delete or
1794 * delete-on-disk record and does not contribute to our
1795 * visbility (but we can still flush it).
1797 if (record->type == HAMMER_MEM_RECORD_ADD)
1802 * If the parent is not in our flush group we cannot
1803 * flush this record yet, there is no visibility.
1804 * We tell the parent to reflush and mark ourselves
1805 * so the parent knows it should flush us too.
1807 pip->flags |= HAMMER_INODE_REFLUSH;
1808 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1814 * This is the core routine placing an inode into the FST_FLUSH state.
1817 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
1822 * Set flush state and prevent the flusher from cycling into
1823 * the next flush group. Do not place the ip on the list yet.
1824 * Inodes not in the idle state get an extra reference.
1826 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1827 if (ip->flush_state == HAMMER_FST_IDLE)
1828 hammer_ref(&ip->lock);
1829 ip->flush_state = HAMMER_FST_FLUSH;
1830 ip->flush_group = flg;
1831 ++ip->hmp->flusher.group_lock;
1832 ++ip->hmp->count_iqueued;
1833 ++hammer_count_iqueued;
1837 * If the flush group reaches the autoflush limit we want to signal
1838 * the flusher. This is particularly important for remove()s.
1840 if (flg->total_count == hammer_autoflush)
1841 flags |= HAMMER_FLUSH_SIGNAL;
1844 * We need to be able to vfsync/truncate from the backend.
1846 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1847 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1848 ip->flags |= HAMMER_INODE_VHELD;
1853 * Figure out how many in-memory records we can actually flush
1854 * (not including inode meta-data, buffers, etc).
1856 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
1857 if (flags & HAMMER_FLUSH_RECURSION) {
1859 * If this is a upwards recursion we do not want to
1860 * recurse down again!
1864 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1866 * No new records are added if we must complete a flush
1867 * from a previous cycle, but we do have to move the records
1868 * from the previous cycle to the current one.
1871 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1872 hammer_syncgrp_child_callback, NULL);
1878 * Normal flush, scan records and bring them into the flush.
1879 * Directory adds and deletes are usually skipped (they are
1880 * grouped with the related inode rather then with the
1883 * go_count can be negative, which means the scan aborted
1884 * due to the flush group being over-full and we should
1885 * flush what we have.
1887 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1888 hammer_setup_child_callback, NULL);
1892 * This is a more involved test that includes go_count. If we
1893 * can't flush, flag the inode and return. If go_count is 0 we
1894 * were are unable to flush any records in our rec_tree and
1895 * must ignore the XDIRTY flag.
1897 if (go_count == 0) {
1898 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1899 --ip->hmp->count_iqueued;
1900 --hammer_count_iqueued;
1903 ip->flush_state = HAMMER_FST_SETUP;
1904 ip->flush_group = NULL;
1905 if (ip->flags & HAMMER_INODE_VHELD) {
1906 ip->flags &= ~HAMMER_INODE_VHELD;
1911 * REFLUSH is needed to trigger dependancy wakeups
1912 * when an inode is in SETUP.
1914 ip->flags |= HAMMER_INODE_REFLUSH;
1915 if (flags & HAMMER_FLUSH_SIGNAL) {
1916 ip->flags |= HAMMER_INODE_RESIGNAL;
1917 hammer_flusher_async(ip->hmp, flg);
1919 if (--ip->hmp->flusher.group_lock == 0)
1920 wakeup(&ip->hmp->flusher.group_lock);
1926 * Snapshot the state of the inode for the backend flusher.
1928 * We continue to retain save_trunc_off even when all truncations
1929 * have been resolved as an optimization to determine if we can
1930 * skip the B-Tree lookup for overwrite deletions.
1932 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1933 * and stays in ip->flags. Once set, it stays set until the
1934 * inode is destroyed.
1936 if (ip->flags & HAMMER_INODE_TRUNCATED) {
1937 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
1938 ip->sync_trunc_off = ip->trunc_off;
1939 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1940 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1941 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
1944 * The save_trunc_off used to cache whether the B-Tree
1945 * holds any records past that point is not used until
1946 * after the truncation has succeeded, so we can safely
1949 if (ip->save_trunc_off > ip->sync_trunc_off)
1950 ip->save_trunc_off = ip->sync_trunc_off;
1952 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
1953 ~HAMMER_INODE_TRUNCATED);
1954 ip->sync_ino_leaf = ip->ino_leaf;
1955 ip->sync_ino_data = ip->ino_data;
1956 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
1957 #ifdef DEBUG_TRUNCATE
1958 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1959 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1963 * The flusher list inherits our inode and reference.
1965 KKASSERT(flg->running == 0);
1966 TAILQ_INSERT_TAIL(&flg->flush_list, ip, flush_entry);
1967 if (--ip->hmp->flusher.group_lock == 0)
1968 wakeup(&ip->hmp->flusher.group_lock);
1970 if (flags & HAMMER_FLUSH_SIGNAL) {
1971 hammer_flusher_async(ip->hmp, flg);
1976 * Callback for scan of ip->rec_tree. Try to include each record in our
1977 * flush. ip->flush_group has been set but the inode has not yet been
1978 * moved into a flushing state.
1980 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1983 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1984 * the caller from shortcutting the flush.
1987 hammer_setup_child_callback(hammer_record_t rec, void *data)
1989 hammer_flush_group_t flg;
1990 hammer_inode_t target_ip;
1995 * Records deleted or committed by the backend are ignored.
1996 * Note that the flush detects deleted frontend records at
1997 * multiple points to deal with races. This is just the first
1998 * line of defense. The only time HAMMER_RECF_DELETED_FE cannot
1999 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2000 * messes up link-count calculations.
2002 * NOTE: Don't get confused between record deletion and, say,
2003 * directory entry deletion. The deletion of a directory entry
2004 * which is on-media has nothing to do with the record deletion
2007 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2008 HAMMER_RECF_COMMITTED)) {
2009 if (rec->flush_state == HAMMER_FST_FLUSH) {
2010 KKASSERT(rec->flush_group == rec->ip->flush_group);
2019 * If the record is in an idle state it has no dependancies and
2023 flg = ip->flush_group;
2026 switch(rec->flush_state) {
2027 case HAMMER_FST_IDLE:
2029 * The record has no setup dependancy, we can flush it.
2031 KKASSERT(rec->target_ip == NULL);
2032 rec->flush_state = HAMMER_FST_FLUSH;
2033 rec->flush_group = flg;
2035 hammer_ref(&rec->lock);
2038 case HAMMER_FST_SETUP:
2040 * The record has a setup dependancy. These are typically
2041 * directory entry adds and deletes. Such entries will be
2042 * flushed when their inodes are flushed so we do not
2043 * usually have to add them to the flush here. However,
2044 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2045 * it is asking us to flush this record (and it).
2047 target_ip = rec->target_ip;
2048 KKASSERT(target_ip != NULL);
2049 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2052 * If the target IP is already flushing in our group
2053 * we could associate the record, but target_ip has
2054 * already synced ino_data to sync_ino_data and we
2055 * would also have to adjust nlinks. Plus there are
2056 * ordering issues for adds and deletes.
2058 * Reflush downward if this is an ADD, and upward if
2061 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2062 if (rec->flush_state == HAMMER_MEM_RECORD_ADD)
2063 ip->flags |= HAMMER_INODE_REFLUSH;
2065 target_ip->flags |= HAMMER_INODE_REFLUSH;
2070 * Target IP is not yet flushing. This can get complex
2071 * because we have to be careful about the recursion.
2073 * Directories create an issue for us in that if a flush
2074 * of a directory is requested the expectation is to flush
2075 * any pending directory entries, but this will cause the
2076 * related inodes to recursively flush as well. We can't
2077 * really defer the operation so just get as many as we
2081 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2082 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2084 * We aren't reclaiming and the target ip was not
2085 * previously prevented from flushing due to this
2086 * record dependancy. Do not flush this record.
2091 if (flg->total_count + flg->refs >
2092 ip->hmp->undo_rec_limit) {
2094 * Our flush group is over-full and we risk blowing
2095 * out the UNDO FIFO. Stop the scan, flush what we
2096 * have, then reflush the directory.
2098 * The directory may be forced through multiple
2099 * flush groups before it can be completely
2102 ip->flags |= HAMMER_INODE_RESIGNAL |
2103 HAMMER_INODE_REFLUSH;
2105 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2107 * If the target IP is not flushing we can force
2108 * it to flush, even if it is unable to write out
2109 * any of its own records we have at least one in
2110 * hand that we CAN deal with.
2112 rec->flush_state = HAMMER_FST_FLUSH;
2113 rec->flush_group = flg;
2115 hammer_ref(&rec->lock);
2116 hammer_flush_inode_core(target_ip, flg,
2117 HAMMER_FLUSH_RECURSION);
2121 * General or delete-on-disk record.
2123 * XXX this needs help. If a delete-on-disk we could
2124 * disconnect the target. If the target has its own
2125 * dependancies they really need to be flushed.
2129 rec->flush_state = HAMMER_FST_FLUSH;
2130 rec->flush_group = flg;
2132 hammer_ref(&rec->lock);
2133 hammer_flush_inode_core(target_ip, flg,
2134 HAMMER_FLUSH_RECURSION);
2138 case HAMMER_FST_FLUSH:
2140 * The flush_group should already match.
2142 KKASSERT(rec->flush_group == flg);
2151 * This version just moves records already in a flush state to the new
2152 * flush group and that is it.
2155 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2157 hammer_inode_t ip = rec->ip;
2159 switch(rec->flush_state) {
2160 case HAMMER_FST_FLUSH:
2161 KKASSERT(rec->flush_group == ip->flush_group);
2171 * Wait for a previously queued flush to complete.
2173 * If a critical error occured we don't try to wait.
2176 hammer_wait_inode(hammer_inode_t ip)
2178 hammer_flush_group_t flg;
2181 if ((ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2182 while (ip->flush_state != HAMMER_FST_IDLE &&
2183 (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2184 if (ip->flush_state == HAMMER_FST_SETUP)
2185 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2186 if (ip->flush_state != HAMMER_FST_IDLE) {
2187 ip->flags |= HAMMER_INODE_FLUSHW;
2188 tsleep(&ip->flags, 0, "hmrwin", 0);
2195 * Called by the backend code when a flush has been completed.
2196 * The inode has already been removed from the flush list.
2198 * A pipelined flush can occur, in which case we must re-enter the
2199 * inode on the list and re-copy its fields.
2202 hammer_flush_inode_done(hammer_inode_t ip, int error)
2207 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2212 * Auto-reflush if the backend could not completely flush
2213 * the inode. This fixes a case where a deferred buffer flush
2214 * could cause fsync to return early.
2216 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2217 ip->flags |= HAMMER_INODE_REFLUSH;
2220 * Merge left-over flags back into the frontend and fix the state.
2221 * Incomplete truncations are retained by the backend.
2224 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2225 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2228 * The backend may have adjusted nlinks, so if the adjusted nlinks
2229 * does not match the fronttend set the frontend's RDIRTY flag again.
2231 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2232 ip->flags |= HAMMER_INODE_DDIRTY;
2235 * Fix up the dirty buffer status.
2237 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2238 ip->flags |= HAMMER_INODE_BUFS;
2242 * Re-set the XDIRTY flag if some of the inode's in-memory records
2243 * could not be flushed.
2245 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2246 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2247 (!RB_EMPTY(&ip->rec_tree) &&
2248 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2251 * Do not lose track of inodes which no longer have vnode
2252 * assocations, otherwise they may never get flushed again.
2254 * The reflush flag can be set superfluously, causing extra pain
2255 * for no reason. If the inode is no longer modified it no longer
2256 * needs to be flushed.
2258 if (ip->flags & HAMMER_INODE_MODMASK) {
2260 ip->flags |= HAMMER_INODE_REFLUSH;
2262 ip->flags &= ~HAMMER_INODE_REFLUSH;
2266 * Adjust the flush state.
2268 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2270 * We were unable to flush out all our records, leave the
2271 * inode in a flush state and in the current flush group.
2272 * The flush group will be re-run.
2274 * This occurs if the UNDO block gets too full or there is
2275 * too much dirty meta-data and allows the flusher to
2276 * finalize the UNDO block and then re-flush.
2278 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2282 * Remove from the flush_group
2284 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
2285 ip->flush_group = NULL;
2288 * Clean up the vnode ref and tracking counts.
2290 if (ip->flags & HAMMER_INODE_VHELD) {
2291 ip->flags &= ~HAMMER_INODE_VHELD;
2294 --hmp->count_iqueued;
2295 --hammer_count_iqueued;
2298 * And adjust the state.
2300 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2301 ip->flush_state = HAMMER_FST_IDLE;
2304 ip->flush_state = HAMMER_FST_SETUP;
2309 * If the frontend is waiting for a flush to complete,
2312 if (ip->flags & HAMMER_INODE_FLUSHW) {
2313 ip->flags &= ~HAMMER_INODE_FLUSHW;
2318 * If the frontend made more changes and requested another
2319 * flush, then try to get it running.
2321 * Reflushes are aborted when the inode is errored out.
2323 if (ip->flags & HAMMER_INODE_REFLUSH) {
2324 ip->flags &= ~HAMMER_INODE_REFLUSH;
2325 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2326 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2327 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2329 hammer_flush_inode(ip, 0);
2335 * If we have no parent dependancies we can clear CONN_DOWN
2337 if (TAILQ_EMPTY(&ip->target_list))
2338 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2341 * If the inode is now clean drop the space reservation.
2343 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2344 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2345 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2350 hammer_rel_inode(ip, 0);
2354 * Called from hammer_sync_inode() to synchronize in-memory records
2358 hammer_sync_record_callback(hammer_record_t record, void *data)
2360 hammer_cursor_t cursor = data;
2361 hammer_transaction_t trans = cursor->trans;
2362 hammer_mount_t hmp = trans->hmp;
2366 * Skip records that do not belong to the current flush.
2368 ++hammer_stats_record_iterations;
2369 if (record->flush_state != HAMMER_FST_FLUSH)
2373 if (record->flush_group != record->ip->flush_group) {
2374 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
2379 KKASSERT(record->flush_group == record->ip->flush_group);
2382 * Interlock the record using the BE flag. Once BE is set the
2383 * frontend cannot change the state of FE.
2385 * NOTE: If FE is set prior to us setting BE we still sync the
2386 * record out, but the flush completion code converts it to
2387 * a delete-on-disk record instead of destroying it.
2389 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2390 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2393 * The backend has already disposed of the record.
2395 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2401 * If the whole inode is being deleting all on-disk records will
2402 * be deleted very soon, we can't sync any new records to disk
2403 * because they will be deleted in the same transaction they were
2404 * created in (delete_tid == create_tid), which will assert.
2406 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2407 * that we currently panic on.
2409 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2410 switch(record->type) {
2411 case HAMMER_MEM_RECORD_DATA:
2413 * We don't have to do anything, if the record was
2414 * committed the space will have been accounted for
2418 case HAMMER_MEM_RECORD_GENERAL:
2420 * Set deleted-by-backend flag. Do not set the
2421 * backend committed flag, because we are throwing
2424 record->flags |= HAMMER_RECF_DELETED_BE;
2425 ++record->ip->rec_generation;
2428 case HAMMER_MEM_RECORD_ADD:
2429 panic("hammer_sync_record_callback: illegal add "
2430 "during inode deletion record %p", record);
2431 break; /* NOT REACHED */
2432 case HAMMER_MEM_RECORD_INODE:
2433 panic("hammer_sync_record_callback: attempt to "
2434 "sync inode record %p?", record);
2435 break; /* NOT REACHED */
2436 case HAMMER_MEM_RECORD_DEL:
2438 * Follow through and issue the on-disk deletion
2445 * If DELETED_FE is set special handling is needed for directory
2446 * entries. Dependant pieces related to the directory entry may
2447 * have already been synced to disk. If this occurs we have to
2448 * sync the directory entry and then change the in-memory record
2449 * from an ADD to a DELETE to cover the fact that it's been
2450 * deleted by the frontend.
2452 * A directory delete covering record (MEM_RECORD_DEL) can never
2453 * be deleted by the frontend.
2455 * Any other record type (aka DATA) can be deleted by the frontend.
2456 * XXX At the moment the flusher must skip it because there may
2457 * be another data record in the flush group for the same block,
2458 * meaning that some frontend data changes can leak into the backend's
2459 * synchronization point.
2461 if (record->flags & HAMMER_RECF_DELETED_FE) {
2462 if (record->type == HAMMER_MEM_RECORD_ADD) {
2464 * Convert a front-end deleted directory-add to
2465 * a directory-delete entry later.
2467 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2470 * Dispose of the record (race case). Mark as
2471 * deleted by backend (and not committed).
2473 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2474 record->flags |= HAMMER_RECF_DELETED_BE;
2475 ++record->ip->rec_generation;
2482 * Assign the create_tid for new records. Deletions already
2483 * have the record's entire key properly set up.
2485 if (record->type != HAMMER_MEM_RECORD_DEL) {
2486 record->leaf.base.create_tid = trans->tid;
2487 record->leaf.create_ts = trans->time32;
2490 error = hammer_ip_sync_record_cursor(cursor, record);
2491 if (error != EDEADLK)
2493 hammer_done_cursor(cursor);
2494 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2499 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2504 hammer_flush_record_done(record, error);
2507 * Do partial finalization if we have built up too many dirty
2508 * buffers. Otherwise a buffer cache deadlock can occur when
2509 * doing things like creating tens of thousands of tiny files.
2511 * We must release our cursor lock to avoid a 3-way deadlock
2512 * due to the exclusive sync lock the finalizer must get.
2514 if (hammer_flusher_meta_limit(hmp)) {
2515 hammer_unlock_cursor(cursor);
2516 hammer_flusher_finalize(trans, 0);
2517 hammer_lock_cursor(cursor);
2524 * Backend function called by the flusher to sync an inode to media.
2527 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2529 struct hammer_cursor cursor;
2530 hammer_node_t tmp_node;
2531 hammer_record_t depend;
2532 hammer_record_t next;
2533 int error, tmp_error;
2536 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2539 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2544 * Any directory records referencing this inode which are not in
2545 * our current flush group must adjust our nlink count for the
2546 * purposes of synchronization to disk.
2548 * Records which are in our flush group can be unlinked from our
2549 * inode now, potentially allowing the inode to be physically
2552 * This cannot block.
2554 nlinks = ip->ino_data.nlinks;
2555 next = TAILQ_FIRST(&ip->target_list);
2556 while ((depend = next) != NULL) {
2557 next = TAILQ_NEXT(depend, target_entry);
2558 if (depend->flush_state == HAMMER_FST_FLUSH &&
2559 depend->flush_group == ip->flush_group) {
2561 * If this is an ADD that was deleted by the frontend
2562 * the frontend nlinks count will have already been
2563 * decremented, but the backend is going to sync its
2564 * directory entry and must account for it. The
2565 * record will be converted to a delete-on-disk when
2568 * If the ADD was not deleted by the frontend we
2569 * can remove the dependancy from our target_list.
2571 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2574 TAILQ_REMOVE(&ip->target_list, depend,
2576 depend->target_ip = NULL;
2578 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2580 * Not part of our flush group and not deleted by
2581 * the front-end, adjust the link count synced to
2582 * the media (undo what the frontend did when it
2583 * queued the record).
2585 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2586 switch(depend->type) {
2587 case HAMMER_MEM_RECORD_ADD:
2590 case HAMMER_MEM_RECORD_DEL:
2600 * Set dirty if we had to modify the link count.
2602 if (ip->sync_ino_data.nlinks != nlinks) {
2603 KKASSERT((int64_t)nlinks >= 0);
2604 ip->sync_ino_data.nlinks = nlinks;
2605 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2609 * If there is a trunction queued destroy any data past the (aligned)
2610 * truncation point. Userland will have dealt with the buffer
2611 * containing the truncation point for us.
2613 * We don't flush pending frontend data buffers until after we've
2614 * dealt with the truncation.
2616 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2618 * Interlock trunc_off. The VOP front-end may continue to
2619 * make adjustments to it while we are blocked.
2622 off_t aligned_trunc_off;
2625 trunc_off = ip->sync_trunc_off;
2626 blkmask = hammer_blocksize(trunc_off) - 1;
2627 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2630 * Delete any whole blocks on-media. The front-end has
2631 * already cleaned out any partial block and made it
2632 * pending. The front-end may have updated trunc_off
2633 * while we were blocked so we only use sync_trunc_off.
2635 * This operation can blow out the buffer cache, EWOULDBLOCK
2636 * means we were unable to complete the deletion. The
2637 * deletion will update sync_trunc_off in that case.
2639 error = hammer_ip_delete_range(&cursor, ip,
2641 0x7FFFFFFFFFFFFFFFLL, 2);
2642 if (error == EWOULDBLOCK) {
2643 ip->flags |= HAMMER_INODE_WOULDBLOCK;
2645 goto defer_buffer_flush;
2652 * Clear the truncation flag on the backend after we have
2653 * complete the deletions. Backend data is now good again
2654 * (including new records we are about to sync, below).
2656 * Leave sync_trunc_off intact. As we write additional
2657 * records the backend will update sync_trunc_off. This
2658 * tells the backend whether it can skip the overwrite
2659 * test. This should work properly even when the backend
2660 * writes full blocks where the truncation point straddles
2661 * the block because the comparison is against the base
2662 * offset of the record.
2664 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2665 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2671 * Now sync related records. These will typically be directory
2672 * entries, records tracking direct-writes, or delete-on-disk records.
2675 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2676 hammer_sync_record_callback, &cursor);
2682 hammer_cache_node(&ip->cache[1], cursor.node);
2685 * Re-seek for inode update, assuming our cache hasn't been ripped
2686 * out from under us.
2689 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
2691 hammer_cursor_downgrade(&cursor);
2692 hammer_lock_sh(&tmp_node->lock);
2693 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2694 hammer_cursor_seek(&cursor, tmp_node, 0);
2695 hammer_unlock(&tmp_node->lock);
2696 hammer_rel_node(tmp_node);
2702 * If we are deleting the inode the frontend had better not have
2703 * any active references on elements making up the inode.
2705 * The call to hammer_ip_delete_clean() cleans up auxillary records
2706 * but not DB or DATA records. Those must have already been deleted
2707 * by the normal truncation mechanic.
2709 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2710 RB_EMPTY(&ip->rec_tree) &&
2711 (ip->sync_flags & HAMMER_INODE_DELETING) &&
2712 (ip->flags & HAMMER_INODE_DELETED) == 0) {
2715 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2717 ip->flags |= HAMMER_INODE_DELETED;
2718 ip->sync_flags &= ~HAMMER_INODE_DELETING;
2719 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2720 KKASSERT(RB_EMPTY(&ip->rec_tree));
2723 * Set delete_tid in both the frontend and backend
2724 * copy of the inode record. The DELETED flag handles
2725 * this, do not set RDIRTY.
2727 ip->ino_leaf.base.delete_tid = trans->tid;
2728 ip->sync_ino_leaf.base.delete_tid = trans->tid;
2729 ip->ino_leaf.delete_ts = trans->time32;
2730 ip->sync_ino_leaf.delete_ts = trans->time32;
2734 * Adjust the inode count in the volume header
2736 hammer_sync_lock_sh(trans);
2737 if (ip->flags & HAMMER_INODE_ONDISK) {
2738 hammer_modify_volume_field(trans,
2741 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2742 hammer_modify_volume_done(trans->rootvol);
2744 hammer_sync_unlock(trans);
2750 ip->sync_flags &= ~HAMMER_INODE_BUFS;
2754 * Now update the inode's on-disk inode-data and/or on-disk record.
2755 * DELETED and ONDISK are managed only in ip->flags.
2757 * In the case of a defered buffer flush we still update the on-disk
2758 * inode to satisfy visibility requirements if there happen to be
2759 * directory dependancies.
2761 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2762 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2764 * If deleted and on-disk, don't set any additional flags.
2765 * the delete flag takes care of things.
2767 * Clear flags which may have been set by the frontend.
2769 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2770 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2771 HAMMER_INODE_DELETING);
2773 case HAMMER_INODE_DELETED:
2775 * Take care of the case where a deleted inode was never
2776 * flushed to the disk in the first place.
2778 * Clear flags which may have been set by the frontend.
2780 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2781 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2782 HAMMER_INODE_DELETING);
2783 while (RB_ROOT(&ip->rec_tree)) {
2784 hammer_record_t record = RB_ROOT(&ip->rec_tree);
2785 hammer_ref(&record->lock);
2786 KKASSERT(record->lock.refs == 1);
2787 record->flags |= HAMMER_RECF_DELETED_BE;
2788 ++record->ip->rec_generation;
2789 hammer_rel_mem_record(record);
2792 case HAMMER_INODE_ONDISK:
2794 * If already on-disk, do not set any additional flags.
2799 * If not on-disk and not deleted, set DDIRTY to force
2800 * an initial record to be written.
2802 * Also set the create_tid in both the frontend and backend
2803 * copy of the inode record.
2805 ip->ino_leaf.base.create_tid = trans->tid;
2806 ip->ino_leaf.create_ts = trans->time32;
2807 ip->sync_ino_leaf.base.create_tid = trans->tid;
2808 ip->sync_ino_leaf.create_ts = trans->time32;
2809 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2814 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2815 * is already on-disk the old record is marked as deleted.
2817 * If DELETED is set hammer_update_inode() will delete the existing
2818 * record without writing out a new one.
2820 * If *ONLY* the ITIMES flag is set we can update the record in-place.
2822 if (ip->flags & HAMMER_INODE_DELETED) {
2823 error = hammer_update_inode(&cursor, ip);
2825 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2826 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2827 error = hammer_update_itimes(&cursor, ip);
2829 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2830 error = hammer_update_inode(&cursor, ip);
2834 hammer_critical_error(ip->hmp, ip, error,
2835 "while syncing inode");
2837 hammer_done_cursor(&cursor);
2842 * This routine is called when the OS is no longer actively referencing
2843 * the inode (but might still be keeping it cached), or when releasing
2844 * the last reference to an inode.
2846 * At this point if the inode's nlinks count is zero we want to destroy
2847 * it, which may mean destroying it on-media too.
2850 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2855 * Set the DELETING flag when the link count drops to 0 and the
2856 * OS no longer has any opens on the inode.
2858 * The backend will clear DELETING (a mod flag) and set DELETED
2859 * (a state flag) when it is actually able to perform the
2862 * Don't reflag the deletion if the flusher is currently syncing
2863 * one that was already flagged. A previously set DELETING flag
2864 * may bounce around flags and sync_flags until the operation is
2867 if (ip->ino_data.nlinks == 0 &&
2868 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2869 ip->flags |= HAMMER_INODE_DELETING;
2870 ip->flags |= HAMMER_INODE_TRUNCATED;
2874 if (hammer_get_vnode(ip, &vp) != 0)
2882 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2883 vnode_pager_setsize(ip->vp, 0);
2892 * After potentially resolving a dependancy the inode is tested
2893 * to determine whether it needs to be reflushed.
2896 hammer_test_inode(hammer_inode_t ip)
2898 if (ip->flags & HAMMER_INODE_REFLUSH) {
2899 ip->flags &= ~HAMMER_INODE_REFLUSH;
2900 hammer_ref(&ip->lock);
2901 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2902 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2903 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2905 hammer_flush_inode(ip, 0);
2907 hammer_rel_inode(ip, 0);
2912 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2913 * reassociated with a vp or just before it gets freed.
2915 * Pipeline wakeups to threads blocked due to an excessive number of
2916 * detached inodes. The reclaim count generates a bit of negative
2920 hammer_inode_wakereclaims(hammer_inode_t ip, int dowake)
2922 struct hammer_reclaim *reclaim;
2923 hammer_mount_t hmp = ip->hmp;
2925 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
2928 --hammer_count_reclaiming;
2929 --hmp->inode_reclaims;
2930 ip->flags &= ~HAMMER_INODE_RECLAIM;
2932 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT || dowake) {
2933 reclaim = TAILQ_FIRST(&hmp->reclaim_list);
2934 if (reclaim && reclaim->count > 0 && --reclaim->count == 0) {
2935 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
2942 * Setup our reclaim pipeline. We only let so many detached (and dirty)
2943 * inodes build up before we start blocking.
2945 * When we block we don't care *which* inode has finished reclaiming,
2946 * as lone as one does. This is somewhat heuristical... we also put a
2947 * cap on how long we are willing to wait.
2950 hammer_inode_waitreclaims(hammer_mount_t hmp)
2952 struct hammer_reclaim reclaim;
2955 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT)
2957 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
2958 (HAMMER_RECLAIM_WAIT * 3) + 1;
2961 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
2962 tsleep(&reclaim, 0, "hmrrcm", delay);
2963 if (reclaim.count > 0)
2964 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
2969 * A larger then normal backlog of inodes is sitting in the flusher,
2970 * enforce a general slowdown to let it catch up. This routine is only
2971 * called on completion of a non-flusher-related transaction which
2972 * performed B-Tree node I/O.
2974 * It is possible for the flusher to stall in a continuous load.
2975 * blogbench -i1000 -o seems to do a good job generating this sort of load.
2976 * If the flusher is unable to catch up the inode count can bloat until
2977 * we run out of kvm.
2979 * This is a bit of a hack.
2982 hammer_inode_waithard(hammer_mount_t hmp)
2987 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
2988 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT / 2 &&
2989 hmp->count_iqueued < hmp->count_inodes / 20) {
2990 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
2994 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT ||
2995 hmp->count_iqueued < hmp->count_inodes / 10) {
2998 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3002 * Block for one flush cycle.
3004 hammer_flusher_wait_next(hmp);