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);
259 hammer_ref(&ip->lock);
263 obj_type = ip->ino_data.obj_type;
264 vp->v_type = hammer_get_vnode_type(obj_type);
266 hammer_inode_wakereclaims(ip, 0);
268 switch(ip->ino_data.obj_type) {
269 case HAMMER_OBJTYPE_CDEV:
270 case HAMMER_OBJTYPE_BDEV:
271 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
272 addaliasu(vp, ip->ino_data.rmajor,
273 ip->ino_data.rminor);
275 case HAMMER_OBJTYPE_FIFO:
276 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
283 * Only mark as the root vnode if the ip is not
284 * historical, otherwise the VFS cache will get
285 * confused. The other half of the special handling
286 * is in hammer_vop_nlookupdotdot().
288 * Pseudo-filesystem roots can be accessed via
289 * non-root filesystem paths and setting VROOT may
290 * confuse the namecache. Set VPFSROOT instead.
292 if (ip->obj_id == HAMMER_OBJID_ROOT &&
293 ip->obj_asof == hmp->asof) {
294 if (ip->obj_localization == 0)
297 vp->v_flag |= VPFSROOT;
300 vp->v_data = (void *)ip;
301 /* vnode locked by getnewvnode() */
302 /* make related vnode dirty if inode dirty? */
303 hammer_unlock(&ip->lock);
304 if (vp->v_type == VREG)
305 vinitvmio(vp, ip->ino_data.size);
310 * loop if the vget fails (aka races), or if the vp
311 * no longer matches ip->vp.
313 if (vget(vp, LK_EXCLUSIVE) == 0) {
324 * Locate all copies of the inode for obj_id compatible with the specified
325 * asof, reference, and issue the related call-back. This routine is used
326 * for direct-io invalidation and does not create any new inodes.
329 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
330 int (*callback)(hammer_inode_t ip, void *data),
333 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
334 hammer_inode_info_cmp_all_history,
339 * Acquire a HAMMER inode. The returned inode is not locked. These functions
340 * do not attach or detach the related vnode (use hammer_get_vnode() for
343 * The flags argument is only applied for newly created inodes, and only
344 * certain flags are inherited.
346 * Called from the frontend.
348 struct hammer_inode *
349 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
350 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
351 int flags, int *errorp)
353 hammer_mount_t hmp = trans->hmp;
354 struct hammer_node_cache *cachep;
355 struct hammer_inode_info iinfo;
356 struct hammer_cursor cursor;
357 struct hammer_inode *ip;
361 * Determine if we already have an inode cached. If we do then
364 * If we find an inode with no vnode we have to mark the
365 * transaction such that hammer_inode_waitreclaims() is
366 * called later on to avoid building up an infinite number
367 * of inodes. Otherwise we can continue to * add new inodes
368 * faster then they can be disposed of, even with the tsleep
371 * If we find a dummy inode we return a failure so dounlink
372 * (which does another lookup) doesn't try to mess with the
373 * link count. hammer_vop_nresolve() uses hammer_get_dummy_inode()
374 * to ref dummy inodes.
376 iinfo.obj_id = obj_id;
377 iinfo.obj_asof = asof;
378 iinfo.obj_localization = localization;
380 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
382 if (ip->flags & HAMMER_INODE_DUMMY) {
386 hammer_ref(&ip->lock);
392 * Allocate a new inode structure and deal with races later.
394 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
395 ++hammer_count_inodes;
398 ip->obj_asof = iinfo.obj_asof;
399 ip->obj_localization = localization;
401 ip->flags = flags & HAMMER_INODE_RO;
402 ip->cache[0].ip = ip;
403 ip->cache[1].ip = ip;
404 ip->cache[2].ip = ip;
405 ip->cache[3].ip = ip;
407 ip->flags |= HAMMER_INODE_RO;
408 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
409 0x7FFFFFFFFFFFFFFFLL;
410 RB_INIT(&ip->rec_tree);
411 TAILQ_INIT(&ip->target_list);
412 hammer_ref(&ip->lock);
415 * Locate the on-disk inode. If this is a PFS root we always
416 * access the current version of the root inode and (if it is not
417 * a master) always access information under it with a snapshot
420 * We cache recent inode lookups in this directory in dip->cache[2].
421 * If we can't find it we assume the inode we are looking for is
422 * close to the directory inode.
427 if (dip->cache[2].node)
428 cachep = &dip->cache[2];
430 cachep = &dip->cache[0];
432 hammer_init_cursor(trans, &cursor, cachep, NULL);
433 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
434 cursor.key_beg.obj_id = ip->obj_id;
435 cursor.key_beg.key = 0;
436 cursor.key_beg.create_tid = 0;
437 cursor.key_beg.delete_tid = 0;
438 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
439 cursor.key_beg.obj_type = 0;
441 cursor.asof = iinfo.obj_asof;
442 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
445 *errorp = hammer_btree_lookup(&cursor);
446 if (*errorp == EDEADLK) {
447 hammer_done_cursor(&cursor);
452 * On success the B-Tree lookup will hold the appropriate
453 * buffer cache buffers and provide a pointer to the requested
454 * information. Copy the information to the in-memory inode
455 * and cache the B-Tree node to improve future operations.
458 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
459 ip->ino_data = cursor.data->inode;
462 * cache[0] tries to cache the location of the object inode.
463 * The assumption is that it is near the directory inode.
465 * cache[1] tries to cache the location of the object data.
466 * We might have something in the governing directory from
467 * scan optimizations (see the strategy code in
470 * We update dip->cache[2], if possible, with the location
471 * of the object inode for future directory shortcuts.
473 hammer_cache_node(&ip->cache[0], cursor.node);
475 if (dip->cache[3].node) {
476 hammer_cache_node(&ip->cache[1],
479 hammer_cache_node(&dip->cache[2], cursor.node);
483 * The file should not contain any data past the file size
484 * stored in the inode. Setting save_trunc_off to the
485 * file size instead of max reduces B-Tree lookup overheads
486 * on append by allowing the flusher to avoid checking for
489 ip->save_trunc_off = ip->ino_data.size;
492 * Locate and assign the pseudofs management structure to
495 if (dip && dip->obj_localization == ip->obj_localization) {
496 ip->pfsm = dip->pfsm;
497 hammer_ref(&ip->pfsm->lock);
499 ip->pfsm = hammer_load_pseudofs(trans,
500 ip->obj_localization,
502 *errorp = 0; /* ignore ENOENT */
507 * The inode is placed on the red-black tree and will be synced to
508 * the media when flushed or by the filesystem sync. If this races
509 * another instantiation/lookup the insertion will fail.
512 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
513 hammer_free_inode(ip);
514 hammer_done_cursor(&cursor);
517 ip->flags |= HAMMER_INODE_ONDISK;
519 if (ip->flags & HAMMER_INODE_RSV_INODES) {
520 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
524 hammer_free_inode(ip);
527 hammer_done_cursor(&cursor);
528 trans->flags |= HAMMER_TRANSF_NEWINODE;
533 * Get a dummy inode to placemark a broken directory entry.
535 struct hammer_inode *
536 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
537 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
538 int flags, int *errorp)
540 hammer_mount_t hmp = trans->hmp;
541 struct hammer_inode_info iinfo;
542 struct hammer_inode *ip;
545 * Determine if we already have an inode cached. If we do then
548 * If we find an inode with no vnode we have to mark the
549 * transaction such that hammer_inode_waitreclaims() is
550 * called later on to avoid building up an infinite number
551 * of inodes. Otherwise we can continue to * add new inodes
552 * faster then they can be disposed of, even with the tsleep
555 * If we find a non-fake inode we return an error. Only fake
556 * inodes can be returned by this routine.
558 iinfo.obj_id = obj_id;
559 iinfo.obj_asof = asof;
560 iinfo.obj_localization = localization;
563 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
565 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
569 hammer_ref(&ip->lock);
574 * Allocate a new inode structure and deal with races later.
576 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
577 ++hammer_count_inodes;
580 ip->obj_asof = iinfo.obj_asof;
581 ip->obj_localization = localization;
583 ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
584 ip->cache[0].ip = ip;
585 ip->cache[1].ip = ip;
586 ip->cache[2].ip = ip;
587 ip->cache[3].ip = ip;
588 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
589 0x7FFFFFFFFFFFFFFFLL;
590 RB_INIT(&ip->rec_tree);
591 TAILQ_INIT(&ip->target_list);
592 hammer_ref(&ip->lock);
595 * Populate the dummy inode. Leave everything zero'd out.
597 * (ip->ino_leaf and ip->ino_data)
599 * Make the dummy inode a FIFO object which most copy programs
600 * will properly ignore.
602 ip->save_trunc_off = ip->ino_data.size;
603 ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
606 * Locate and assign the pseudofs management structure to
609 if (dip && dip->obj_localization == ip->obj_localization) {
610 ip->pfsm = dip->pfsm;
611 hammer_ref(&ip->pfsm->lock);
613 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
615 *errorp = 0; /* ignore ENOENT */
619 * The inode is placed on the red-black tree and will be synced to
620 * the media when flushed or by the filesystem sync. If this races
621 * another instantiation/lookup the insertion will fail.
623 * NOTE: Do not set HAMMER_INODE_ONDISK. The inode is a fake.
626 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
627 hammer_free_inode(ip);
631 if (ip->flags & HAMMER_INODE_RSV_INODES) {
632 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
635 hammer_free_inode(ip);
638 trans->flags |= HAMMER_TRANSF_NEWINODE;
643 * Return a referenced inode only if it is in our inode cache.
645 * Dummy inodes do not count.
647 struct hammer_inode *
648 hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
649 hammer_tid_t asof, u_int32_t localization)
651 hammer_mount_t hmp = trans->hmp;
652 struct hammer_inode_info iinfo;
653 struct hammer_inode *ip;
655 iinfo.obj_id = obj_id;
656 iinfo.obj_asof = asof;
657 iinfo.obj_localization = localization;
659 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
661 if (ip->flags & HAMMER_INODE_DUMMY)
664 hammer_ref(&ip->lock);
670 * Create a new filesystem object, returning the inode in *ipp. The
671 * returned inode will be referenced. The inode is created in-memory.
673 * If pfsm is non-NULL the caller wishes to create the root inode for
677 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
679 hammer_inode_t dip, const char *name, int namelen,
680 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
691 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
692 ++hammer_count_inodes;
694 trans->flags |= HAMMER_TRANSF_NEWINODE;
697 KKASSERT(pfsm->localization != 0);
698 ip->obj_id = HAMMER_OBJID_ROOT;
699 ip->obj_localization = pfsm->localization;
701 KKASSERT(dip != NULL);
702 namekey = hammer_directory_namekey(dip, name, namelen, &dummy);
703 ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
704 ip->obj_localization = dip->obj_localization;
707 KKASSERT(ip->obj_id != 0);
708 ip->obj_asof = hmp->asof;
710 ip->flush_state = HAMMER_FST_IDLE;
711 ip->flags = HAMMER_INODE_DDIRTY |
712 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
713 ip->cache[0].ip = ip;
714 ip->cache[1].ip = ip;
715 ip->cache[2].ip = ip;
716 ip->cache[3].ip = ip;
718 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
719 /* ip->save_trunc_off = 0; (already zero) */
720 RB_INIT(&ip->rec_tree);
721 TAILQ_INIT(&ip->target_list);
723 ip->ino_data.atime = trans->time;
724 ip->ino_data.mtime = trans->time;
725 ip->ino_data.size = 0;
726 ip->ino_data.nlinks = 0;
729 * A nohistory designator on the parent directory is inherited by
730 * the child. We will do this even for pseudo-fs creation... the
731 * sysad can turn it off.
734 ip->ino_data.uflags = dip->ino_data.uflags &
735 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
738 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
739 ip->ino_leaf.base.localization = ip->obj_localization +
740 HAMMER_LOCALIZE_INODE;
741 ip->ino_leaf.base.obj_id = ip->obj_id;
742 ip->ino_leaf.base.key = 0;
743 ip->ino_leaf.base.create_tid = 0;
744 ip->ino_leaf.base.delete_tid = 0;
745 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
746 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
748 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
749 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
750 ip->ino_data.mode = vap->va_mode;
751 ip->ino_data.ctime = trans->time;
754 * If we are running version 2 or greater we use dirhash algorithm #1
755 * which is semi-sorted. Algorithm #0 was just a pure crc.
757 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
758 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
759 ip->ino_data.cap_flags |= HAMMER_INODE_CAP_DIRHASH_ALG1;
764 * Setup the ".." pointer. This only needs to be done for directories
765 * but we do it for all objects as a recovery aid.
768 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
771 * The parent_obj_localization field only applies to pseudo-fs roots.
772 * XXX this is no longer applicable, PFSs are no longer directly
773 * tied into the parent's directory structure.
775 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
776 ip->obj_id == HAMMER_OBJID_ROOT) {
777 ip->ino_data.ext.obj.parent_obj_localization =
778 dip->obj_localization;
782 switch(ip->ino_leaf.base.obj_type) {
783 case HAMMER_OBJTYPE_CDEV:
784 case HAMMER_OBJTYPE_BDEV:
785 ip->ino_data.rmajor = vap->va_rmajor;
786 ip->ino_data.rminor = vap->va_rminor;
793 * Calculate default uid/gid and overwrite with information from
797 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
798 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
799 xuid, cred, &vap->va_mode);
803 ip->ino_data.mode = vap->va_mode;
805 if (vap->va_vaflags & VA_UID_UUID_VALID)
806 ip->ino_data.uid = vap->va_uid_uuid;
807 else if (vap->va_uid != (uid_t)VNOVAL)
808 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
810 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
812 if (vap->va_vaflags & VA_GID_UUID_VALID)
813 ip->ino_data.gid = vap->va_gid_uuid;
814 else if (vap->va_gid != (gid_t)VNOVAL)
815 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
817 ip->ino_data.gid = dip->ino_data.gid;
819 hammer_ref(&ip->lock);
823 hammer_ref(&pfsm->lock);
825 } else if (dip->obj_localization == ip->obj_localization) {
826 ip->pfsm = dip->pfsm;
827 hammer_ref(&ip->pfsm->lock);
830 ip->pfsm = hammer_load_pseudofs(trans,
831 ip->obj_localization,
833 error = 0; /* ignore ENOENT */
837 hammer_free_inode(ip);
839 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
840 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
842 hammer_free_inode(ip);
849 * Final cleanup / freeing of an inode structure
852 hammer_free_inode(hammer_inode_t ip)
854 struct hammer_mount *hmp;
857 KKASSERT(ip->lock.refs == 1);
858 hammer_uncache_node(&ip->cache[0]);
859 hammer_uncache_node(&ip->cache[1]);
860 hammer_uncache_node(&ip->cache[2]);
861 hammer_uncache_node(&ip->cache[3]);
862 hammer_inode_wakereclaims(ip, 1);
864 hammer_clear_objid(ip);
865 --hammer_count_inodes;
868 hammer_rel_pseudofs(hmp, ip->pfsm);
871 kfree(ip, hmp->m_inodes);
876 * Retrieve pseudo-fs data. NULL will never be returned.
878 * If an error occurs *errorp will be set and a default template is returned,
879 * otherwise *errorp is set to 0. Typically when an error occurs it will
882 hammer_pseudofs_inmem_t
883 hammer_load_pseudofs(hammer_transaction_t trans,
884 u_int32_t localization, int *errorp)
886 hammer_mount_t hmp = trans->hmp;
888 hammer_pseudofs_inmem_t pfsm;
889 struct hammer_cursor cursor;
893 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
895 hammer_ref(&pfsm->lock);
901 * PFS records are stored in the root inode (not the PFS root inode,
902 * but the real root). Avoid an infinite recursion if loading
903 * the PFS for the real root.
906 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
908 HAMMER_DEF_LOCALIZATION, 0, errorp);
913 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
914 pfsm->localization = localization;
915 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
916 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
918 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
919 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
920 HAMMER_LOCALIZE_MISC;
921 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
922 cursor.key_beg.create_tid = 0;
923 cursor.key_beg.delete_tid = 0;
924 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
925 cursor.key_beg.obj_type = 0;
926 cursor.key_beg.key = localization;
927 cursor.asof = HAMMER_MAX_TID;
928 cursor.flags |= HAMMER_CURSOR_ASOF;
931 *errorp = hammer_ip_lookup(&cursor);
933 *errorp = hammer_btree_lookup(&cursor);
935 *errorp = hammer_ip_resolve_data(&cursor);
937 if (cursor.data->pfsd.mirror_flags &
938 HAMMER_PFSD_DELETED) {
941 bytes = cursor.leaf->data_len;
942 if (bytes > sizeof(pfsm->pfsd))
943 bytes = sizeof(pfsm->pfsd);
944 bcopy(cursor.data, &pfsm->pfsd, bytes);
948 hammer_done_cursor(&cursor);
950 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
951 hammer_ref(&pfsm->lock);
953 hammer_rel_inode(ip, 0);
954 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
955 kfree(pfsm, hmp->m_misc);
962 * Store pseudo-fs data. The backend will automatically delete any prior
963 * on-disk pseudo-fs data but we have to delete in-memory versions.
966 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
968 struct hammer_cursor cursor;
969 hammer_record_t record;
973 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
974 HAMMER_DEF_LOCALIZATION, 0, &error);
976 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
977 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
978 cursor.key_beg.localization = ip->obj_localization +
979 HAMMER_LOCALIZE_MISC;
980 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
981 cursor.key_beg.create_tid = 0;
982 cursor.key_beg.delete_tid = 0;
983 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
984 cursor.key_beg.obj_type = 0;
985 cursor.key_beg.key = pfsm->localization;
986 cursor.asof = HAMMER_MAX_TID;
987 cursor.flags |= HAMMER_CURSOR_ASOF;
990 * Replace any in-memory version of the record.
992 error = hammer_ip_lookup(&cursor);
993 if (error == 0 && hammer_cursor_inmem(&cursor)) {
994 record = cursor.iprec;
995 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
996 KKASSERT(cursor.deadlk_rec == NULL);
997 hammer_ref(&record->lock);
998 cursor.deadlk_rec = record;
1001 record->flags |= HAMMER_RECF_DELETED_FE;
1007 * Allocate replacement general record. The backend flush will
1008 * delete any on-disk version of the record.
1010 if (error == 0 || error == ENOENT) {
1011 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
1012 record->type = HAMMER_MEM_RECORD_GENERAL;
1014 record->leaf.base.localization = ip->obj_localization +
1015 HAMMER_LOCALIZE_MISC;
1016 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
1017 record->leaf.base.key = pfsm->localization;
1018 record->leaf.data_len = sizeof(pfsm->pfsd);
1019 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
1020 error = hammer_ip_add_record(trans, record);
1022 hammer_done_cursor(&cursor);
1023 if (error == EDEADLK)
1025 hammer_rel_inode(ip, 0);
1030 * Create a root directory for a PFS if one does not alredy exist.
1032 * The PFS root stands alone so we must also bump the nlinks count
1033 * to prevent it from being destroyed on release.
1036 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1037 hammer_pseudofs_inmem_t pfsm)
1043 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1044 pfsm->localization, 0, &error);
1049 error = hammer_create_inode(trans, &vap, cred,
1053 ++ip->ino_data.nlinks;
1054 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1058 hammer_rel_inode(ip, 0);
1063 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1064 * if we are unable to disassociate all the inodes.
1068 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1072 hammer_ref(&ip->lock);
1073 if (ip->lock.refs == 2 && ip->vp)
1074 vclean_unlocked(ip->vp);
1075 if (ip->lock.refs == 1 && ip->vp == NULL)
1078 res = -1; /* stop, someone is using the inode */
1079 hammer_rel_inode(ip, 0);
1084 hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
1089 for (try = res = 0; try < 4; ++try) {
1090 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1091 hammer_inode_pfs_cmp,
1092 hammer_unload_pseudofs_callback,
1094 if (res == 0 && try > 1)
1096 hammer_flusher_sync(trans->hmp);
1105 * Release a reference on a PFS
1108 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1110 hammer_unref(&pfsm->lock);
1111 if (pfsm->lock.refs == 0) {
1112 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1113 kfree(pfsm, hmp->m_misc);
1118 * Called by hammer_sync_inode().
1121 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1123 hammer_transaction_t trans = cursor->trans;
1124 hammer_record_t record;
1132 * If the inode has a presence on-disk then locate it and mark
1133 * it deleted, setting DELONDISK.
1135 * The record may or may not be physically deleted, depending on
1136 * the retention policy.
1138 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1139 HAMMER_INODE_ONDISK) {
1140 hammer_normalize_cursor(cursor);
1141 cursor->key_beg.localization = ip->obj_localization +
1142 HAMMER_LOCALIZE_INODE;
1143 cursor->key_beg.obj_id = ip->obj_id;
1144 cursor->key_beg.key = 0;
1145 cursor->key_beg.create_tid = 0;
1146 cursor->key_beg.delete_tid = 0;
1147 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1148 cursor->key_beg.obj_type = 0;
1149 cursor->asof = ip->obj_asof;
1150 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1151 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1152 cursor->flags |= HAMMER_CURSOR_BACKEND;
1154 error = hammer_btree_lookup(cursor);
1155 if (hammer_debug_inode)
1156 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1159 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1160 if (hammer_debug_inode)
1161 kprintf(" error %d\n", error);
1163 ip->flags |= HAMMER_INODE_DELONDISK;
1166 hammer_cache_node(&ip->cache[0], cursor->node);
1168 if (error == EDEADLK) {
1169 hammer_done_cursor(cursor);
1170 error = hammer_init_cursor(trans, cursor,
1172 if (hammer_debug_inode)
1173 kprintf("IPDED %p %d\n", ip, error);
1180 * Ok, write out the initial record or a new record (after deleting
1181 * the old one), unless the DELETED flag is set. This routine will
1182 * clear DELONDISK if it writes out a record.
1184 * Update our inode statistics if this is the first application of
1185 * the inode on-disk.
1187 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1189 * Generate a record and write it to the media. We clean-up
1190 * the state before releasing so we do not have to set-up
1193 record = hammer_alloc_mem_record(ip, 0);
1194 record->type = HAMMER_MEM_RECORD_INODE;
1195 record->flush_state = HAMMER_FST_FLUSH;
1196 record->leaf = ip->sync_ino_leaf;
1197 record->leaf.base.create_tid = trans->tid;
1198 record->leaf.data_len = sizeof(ip->sync_ino_data);
1199 record->leaf.create_ts = trans->time32;
1200 record->data = (void *)&ip->sync_ino_data;
1201 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1204 * If this flag is set we cannot sync the new file size
1205 * because we haven't finished related truncations. The
1206 * inode will be flushed in another flush group to finish
1209 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1210 ip->sync_ino_data.size != ip->ino_data.size) {
1212 ip->sync_ino_data.size = ip->ino_data.size;
1218 error = hammer_ip_sync_record_cursor(cursor, record);
1219 if (hammer_debug_inode)
1220 kprintf("GENREC %p rec %08x %d\n",
1221 ip, record->flags, error);
1222 if (error != EDEADLK)
1224 hammer_done_cursor(cursor);
1225 error = hammer_init_cursor(trans, cursor,
1227 if (hammer_debug_inode)
1228 kprintf("GENREC reinit %d\n", error);
1234 * Note: The record was never on the inode's record tree
1235 * so just wave our hands importantly and destroy it.
1237 record->flags |= HAMMER_RECF_COMMITTED;
1238 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1239 record->flush_state = HAMMER_FST_IDLE;
1240 ++ip->rec_generation;
1241 hammer_rel_mem_record(record);
1247 if (hammer_debug_inode)
1248 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1249 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1250 HAMMER_INODE_ATIME |
1251 HAMMER_INODE_MTIME);
1252 ip->flags &= ~HAMMER_INODE_DELONDISK;
1254 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1257 * Root volume count of inodes
1259 hammer_sync_lock_sh(trans);
1260 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1261 hammer_modify_volume_field(trans,
1264 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1265 hammer_modify_volume_done(trans->rootvol);
1266 ip->flags |= HAMMER_INODE_ONDISK;
1267 if (hammer_debug_inode)
1268 kprintf("NOWONDISK %p\n", ip);
1270 hammer_sync_unlock(trans);
1275 * If the inode has been destroyed, clean out any left-over flags
1276 * that may have been set by the frontend.
1278 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1279 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1280 HAMMER_INODE_ATIME |
1281 HAMMER_INODE_MTIME);
1287 * Update only the itimes fields.
1289 * ATIME can be updated without generating any UNDO. MTIME is updated
1290 * with UNDO so it is guaranteed to be synchronized properly in case of
1293 * Neither field is included in the B-Tree leaf element's CRC, which is how
1294 * we can get away with updating ATIME the way we do.
1297 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1299 hammer_transaction_t trans = cursor->trans;
1303 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1304 HAMMER_INODE_ONDISK) {
1308 hammer_normalize_cursor(cursor);
1309 cursor->key_beg.localization = ip->obj_localization +
1310 HAMMER_LOCALIZE_INODE;
1311 cursor->key_beg.obj_id = ip->obj_id;
1312 cursor->key_beg.key = 0;
1313 cursor->key_beg.create_tid = 0;
1314 cursor->key_beg.delete_tid = 0;
1315 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1316 cursor->key_beg.obj_type = 0;
1317 cursor->asof = ip->obj_asof;
1318 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1319 cursor->flags |= HAMMER_CURSOR_ASOF;
1320 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1321 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1322 cursor->flags |= HAMMER_CURSOR_BACKEND;
1324 error = hammer_btree_lookup(cursor);
1326 hammer_cache_node(&ip->cache[0], cursor->node);
1327 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1329 * Updating MTIME requires an UNDO. Just cover
1330 * both atime and mtime.
1332 hammer_sync_lock_sh(trans);
1333 hammer_modify_buffer(trans, cursor->data_buffer,
1334 HAMMER_ITIMES_BASE(&cursor->data->inode),
1335 HAMMER_ITIMES_BYTES);
1336 cursor->data->inode.atime = ip->sync_ino_data.atime;
1337 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1338 hammer_modify_buffer_done(cursor->data_buffer);
1339 hammer_sync_unlock(trans);
1340 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1342 * Updating atime only can be done in-place with
1345 hammer_sync_lock_sh(trans);
1346 hammer_modify_buffer(trans, cursor->data_buffer,
1348 cursor->data->inode.atime = ip->sync_ino_data.atime;
1349 hammer_modify_buffer_done(cursor->data_buffer);
1350 hammer_sync_unlock(trans);
1352 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1354 if (error == EDEADLK) {
1355 hammer_done_cursor(cursor);
1356 error = hammer_init_cursor(trans, cursor,
1365 * Release a reference on an inode, flush as requested.
1367 * On the last reference we queue the inode to the flusher for its final
1371 hammer_rel_inode(struct hammer_inode *ip, int flush)
1373 /*hammer_mount_t hmp = ip->hmp;*/
1376 * Handle disposition when dropping the last ref.
1379 if (ip->lock.refs == 1) {
1381 * Determine whether on-disk action is needed for
1382 * the inode's final disposition.
1384 KKASSERT(ip->vp == NULL);
1385 hammer_inode_unloadable_check(ip, 0);
1386 if (ip->flags & HAMMER_INODE_MODMASK) {
1387 hammer_flush_inode(ip, 0);
1388 } else if (ip->lock.refs == 1) {
1389 hammer_unload_inode(ip);
1394 hammer_flush_inode(ip, 0);
1397 * The inode still has multiple refs, try to drop
1400 KKASSERT(ip->lock.refs >= 1);
1401 if (ip->lock.refs > 1) {
1402 hammer_unref(&ip->lock);
1410 * Unload and destroy the specified inode. Must be called with one remaining
1411 * reference. The reference is disposed of.
1413 * The inode must be completely clean.
1416 hammer_unload_inode(struct hammer_inode *ip)
1418 hammer_mount_t hmp = ip->hmp;
1420 KASSERT(ip->lock.refs == 1,
1421 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1422 KKASSERT(ip->vp == NULL);
1423 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1424 KKASSERT(ip->cursor_ip_refs == 0);
1425 KKASSERT(ip->lock.lockcount == 0);
1426 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1428 KKASSERT(RB_EMPTY(&ip->rec_tree));
1429 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1431 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1433 hammer_free_inode(ip);
1438 * Called during unmounting if a critical error occured. The in-memory
1439 * inode and all related structures are destroyed.
1441 * If a critical error did not occur the unmount code calls the standard
1442 * release and asserts that the inode is gone.
1445 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1447 hammer_record_t rec;
1450 * Get rid of the inodes in-memory records, regardless of their
1451 * state, and clear the mod-mask.
1453 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1454 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1455 rec->target_ip = NULL;
1456 if (rec->flush_state == HAMMER_FST_SETUP)
1457 rec->flush_state = HAMMER_FST_IDLE;
1459 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1460 if (rec->flush_state == HAMMER_FST_FLUSH)
1461 --rec->flush_group->refs;
1463 hammer_ref(&rec->lock);
1464 KKASSERT(rec->lock.refs == 1);
1465 rec->flush_state = HAMMER_FST_IDLE;
1466 rec->flush_group = NULL;
1467 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1468 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1469 ++ip->rec_generation;
1470 hammer_rel_mem_record(rec);
1472 ip->flags &= ~HAMMER_INODE_MODMASK;
1473 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1474 KKASSERT(ip->vp == NULL);
1477 * Remove the inode from any flush group, force it idle. FLUSH
1478 * and SETUP states have an inode ref.
1480 switch(ip->flush_state) {
1481 case HAMMER_FST_FLUSH:
1482 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
1483 --ip->flush_group->refs;
1484 ip->flush_group = NULL;
1486 case HAMMER_FST_SETUP:
1487 hammer_unref(&ip->lock);
1488 ip->flush_state = HAMMER_FST_IDLE;
1490 case HAMMER_FST_IDLE:
1495 * There shouldn't be any associated vnode. The unload needs at
1496 * least one ref, if we do have a vp steal its ip ref.
1499 kprintf("hammer_destroy_inode_callback: Unexpected "
1500 "vnode association ip %p vp %p\n", ip, ip->vp);
1501 ip->vp->v_data = NULL;
1504 hammer_ref(&ip->lock);
1506 hammer_unload_inode(ip);
1511 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1512 * the read-only flag for cached inodes.
1514 * This routine is called from a RB_SCAN().
1517 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1519 hammer_mount_t hmp = ip->hmp;
1521 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1522 ip->flags |= HAMMER_INODE_RO;
1524 ip->flags &= ~HAMMER_INODE_RO;
1529 * A transaction has modified an inode, requiring updates as specified by
1532 * HAMMER_INODE_DDIRTY: Inode data has been updated
1533 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1534 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1535 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1536 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1539 hammer_modify_inode(hammer_inode_t ip, int flags)
1542 * ronly of 0 or 2 does not trigger assertion.
1543 * 2 is a special error state
1545 KKASSERT(ip->hmp->ronly != 1 ||
1546 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1547 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1548 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1549 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1550 ip->flags |= HAMMER_INODE_RSV_INODES;
1551 ++ip->hmp->rsv_inodes;
1558 * Request that an inode be flushed. This whole mess cannot block and may
1559 * recurse (if not synchronous). Once requested HAMMER will attempt to
1560 * actively flush the inode until the flush can be done.
1562 * The inode may already be flushing, or may be in a setup state. We can
1563 * place the inode in a flushing state if it is currently idle and flag it
1564 * to reflush if it is currently flushing.
1566 * Upon return if the inode could not be flushed due to a setup
1567 * dependancy, then it will be automatically flushed when the dependancy
1571 hammer_flush_inode(hammer_inode_t ip, int flags)
1574 hammer_flush_group_t flg;
1578 * next_flush_group is the first flush group we can place the inode
1579 * in. It may be NULL. If it becomes full we append a new flush
1580 * group and make that the next_flush_group.
1583 while ((flg = hmp->next_flush_group) != NULL) {
1584 KKASSERT(flg->running == 0);
1585 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit)
1587 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
1588 hammer_flusher_async(ip->hmp, flg);
1591 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1592 hmp->next_flush_group = flg;
1593 TAILQ_INIT(&flg->flush_list);
1594 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1598 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1599 * state we have to put it back into an IDLE state so we can
1600 * drop the extra ref.
1602 * If we have a parent dependancy we must still fall through
1605 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1606 if (ip->flush_state == HAMMER_FST_SETUP &&
1607 TAILQ_EMPTY(&ip->target_list)) {
1608 ip->flush_state = HAMMER_FST_IDLE;
1609 hammer_rel_inode(ip, 0);
1611 if (ip->flush_state == HAMMER_FST_IDLE)
1616 * Our flush action will depend on the current state.
1618 switch(ip->flush_state) {
1619 case HAMMER_FST_IDLE:
1621 * We have no dependancies and can flush immediately. Some
1622 * our children may not be flushable so we have to re-test
1623 * with that additional knowledge.
1625 hammer_flush_inode_core(ip, flg, flags);
1627 case HAMMER_FST_SETUP:
1629 * Recurse upwards through dependancies via target_list
1630 * and start their flusher actions going if possible.
1632 * 'good' is our connectivity. -1 means we have none and
1633 * can't flush, 0 means there weren't any dependancies, and
1634 * 1 means we have good connectivity.
1636 good = hammer_setup_parent_inodes(ip, 0, flg);
1640 * We can continue if good >= 0. Determine how
1641 * many records under our inode can be flushed (and
1644 hammer_flush_inode_core(ip, flg, flags);
1647 * Parent has no connectivity, tell it to flush
1648 * us as soon as it does.
1650 * The REFLUSH flag is also needed to trigger
1651 * dependancy wakeups.
1653 ip->flags |= HAMMER_INODE_CONN_DOWN |
1654 HAMMER_INODE_REFLUSH;
1655 if (flags & HAMMER_FLUSH_SIGNAL) {
1656 ip->flags |= HAMMER_INODE_RESIGNAL;
1657 hammer_flusher_async(ip->hmp, flg);
1661 case HAMMER_FST_FLUSH:
1663 * We are already flushing, flag the inode to reflush
1664 * if needed after it completes its current flush.
1666 * The REFLUSH flag is also needed to trigger
1667 * dependancy wakeups.
1669 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1670 ip->flags |= HAMMER_INODE_REFLUSH;
1671 if (flags & HAMMER_FLUSH_SIGNAL) {
1672 ip->flags |= HAMMER_INODE_RESIGNAL;
1673 hammer_flusher_async(ip->hmp, flg);
1680 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1681 * ip which reference our ip.
1683 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1684 * so for now do not ref/deref the structures. Note that if we use the
1685 * ref/rel code later, the rel CAN block.
1688 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1689 hammer_flush_group_t flg)
1691 hammer_record_t depend;
1696 * If we hit our recursion limit and we have parent dependencies
1697 * We cannot continue. Returning < 0 will cause us to be flagged
1698 * for reflush. Returning -2 cuts off additional dependency checks
1699 * because they are likely to also hit the depth limit.
1701 * We cannot return < 0 if there are no dependencies or there might
1702 * not be anything to wakeup (ip).
1704 if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1705 kprintf("HAMMER Warning: depth limit reached on "
1706 "setup recursion, inode %p %016llx\n",
1707 ip, (long long)ip->obj_id);
1715 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1716 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1717 KKASSERT(depend->target_ip == ip);
1718 if (r < 0 && good == 0)
1724 * If we failed due to the recursion depth limit then stop
1734 * This helper function takes a record representing the dependancy between
1735 * the parent inode and child inode.
1737 * record->ip = parent inode
1738 * record->target_ip = child inode
1740 * We are asked to recurse upwards and convert the record from SETUP
1741 * to FLUSH if possible.
1743 * Return 1 if the record gives us connectivity
1745 * Return 0 if the record is not relevant
1747 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1750 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1751 hammer_flush_group_t flg)
1757 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1762 * If the record is already flushing, is it in our flush group?
1764 * If it is in our flush group but it is a general record or a
1765 * delete-on-disk, it does not improve our connectivity (return 0),
1766 * and if the target inode is not trying to destroy itself we can't
1767 * allow the operation yet anyway (the second return -1).
1769 if (record->flush_state == HAMMER_FST_FLUSH) {
1771 * If not in our flush group ask the parent to reflush
1772 * us as soon as possible.
1774 if (record->flush_group != flg) {
1775 pip->flags |= HAMMER_INODE_REFLUSH;
1776 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1781 * If in our flush group everything is already set up,
1782 * just return whether the record will improve our
1783 * visibility or not.
1785 if (record->type == HAMMER_MEM_RECORD_ADD)
1791 * It must be a setup record. Try to resolve the setup dependancies
1792 * by recursing upwards so we can place ip on the flush list.
1794 * Limit ourselves to 20 levels of recursion to avoid blowing out
1795 * the kernel stack. If we hit the recursion limit we can't flush
1796 * until the parent flushes. The parent will flush independantly
1797 * on its own and ultimately a deep recursion will be resolved.
1799 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1801 good = hammer_setup_parent_inodes(pip, depth + 1, flg);
1804 * If good < 0 the parent has no connectivity and we cannot safely
1805 * flush the directory entry, which also means we can't flush our
1806 * ip. Flag us for downward recursion once the parent's
1807 * connectivity is resolved. Flag the parent for [re]flush or it
1808 * may not check for downward recursions.
1811 pip->flags |= HAMMER_INODE_REFLUSH;
1812 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1817 * We are go, place the parent inode in a flushing state so we can
1818 * place its record in a flushing state. Note that the parent
1819 * may already be flushing. The record must be in the same flush
1820 * group as the parent.
1822 if (pip->flush_state != HAMMER_FST_FLUSH)
1823 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
1824 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1825 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1828 if (record->type == HAMMER_MEM_RECORD_DEL &&
1829 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1831 * Regardless of flushing state we cannot sync this path if the
1832 * record represents a delete-on-disk but the target inode
1833 * is not ready to sync its own deletion.
1835 * XXX need to count effective nlinks to determine whether
1836 * the flush is ok, otherwise removing a hardlink will
1837 * just leave the DEL record to rot.
1839 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1843 if (pip->flush_group == flg) {
1845 * Because we have not calculated nlinks yet we can just
1846 * set records to the flush state if the parent is in
1847 * the same flush group as we are.
1849 record->flush_state = HAMMER_FST_FLUSH;
1850 record->flush_group = flg;
1851 ++record->flush_group->refs;
1852 hammer_ref(&record->lock);
1855 * A general directory-add contributes to our visibility.
1857 * Otherwise it is probably a directory-delete or
1858 * delete-on-disk record and does not contribute to our
1859 * visbility (but we can still flush it).
1861 if (record->type == HAMMER_MEM_RECORD_ADD)
1866 * If the parent is not in our flush group we cannot
1867 * flush this record yet, there is no visibility.
1868 * We tell the parent to reflush and mark ourselves
1869 * so the parent knows it should flush us too.
1871 pip->flags |= HAMMER_INODE_REFLUSH;
1872 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1878 * This is the core routine placing an inode into the FST_FLUSH state.
1881 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
1886 * Set flush state and prevent the flusher from cycling into
1887 * the next flush group. Do not place the ip on the list yet.
1888 * Inodes not in the idle state get an extra reference.
1890 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1891 if (ip->flush_state == HAMMER_FST_IDLE)
1892 hammer_ref(&ip->lock);
1893 ip->flush_state = HAMMER_FST_FLUSH;
1894 ip->flush_group = flg;
1895 ++ip->hmp->flusher.group_lock;
1896 ++ip->hmp->count_iqueued;
1897 ++hammer_count_iqueued;
1901 * If the flush group reaches the autoflush limit we want to signal
1902 * the flusher. This is particularly important for remove()s.
1904 if (flg->total_count == hammer_autoflush)
1905 flags |= HAMMER_FLUSH_SIGNAL;
1908 * We need to be able to vfsync/truncate from the backend.
1910 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1911 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1912 ip->flags |= HAMMER_INODE_VHELD;
1917 * Figure out how many in-memory records we can actually flush
1918 * (not including inode meta-data, buffers, etc).
1920 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
1921 if (flags & HAMMER_FLUSH_RECURSION) {
1923 * If this is a upwards recursion we do not want to
1924 * recurse down again!
1928 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1930 * No new records are added if we must complete a flush
1931 * from a previous cycle, but we do have to move the records
1932 * from the previous cycle to the current one.
1935 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1936 hammer_syncgrp_child_callback, NULL);
1942 * Normal flush, scan records and bring them into the flush.
1943 * Directory adds and deletes are usually skipped (they are
1944 * grouped with the related inode rather then with the
1947 * go_count can be negative, which means the scan aborted
1948 * due to the flush group being over-full and we should
1949 * flush what we have.
1951 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1952 hammer_setup_child_callback, NULL);
1956 * This is a more involved test that includes go_count. If we
1957 * can't flush, flag the inode and return. If go_count is 0 we
1958 * were are unable to flush any records in our rec_tree and
1959 * must ignore the XDIRTY flag.
1961 if (go_count == 0) {
1962 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1963 --ip->hmp->count_iqueued;
1964 --hammer_count_iqueued;
1967 ip->flush_state = HAMMER_FST_SETUP;
1968 ip->flush_group = NULL;
1969 if (ip->flags & HAMMER_INODE_VHELD) {
1970 ip->flags &= ~HAMMER_INODE_VHELD;
1975 * REFLUSH is needed to trigger dependancy wakeups
1976 * when an inode is in SETUP.
1978 ip->flags |= HAMMER_INODE_REFLUSH;
1979 if (flags & HAMMER_FLUSH_SIGNAL) {
1980 ip->flags |= HAMMER_INODE_RESIGNAL;
1981 hammer_flusher_async(ip->hmp, flg);
1983 if (--ip->hmp->flusher.group_lock == 0)
1984 wakeup(&ip->hmp->flusher.group_lock);
1990 * Snapshot the state of the inode for the backend flusher.
1992 * We continue to retain save_trunc_off even when all truncations
1993 * have been resolved as an optimization to determine if we can
1994 * skip the B-Tree lookup for overwrite deletions.
1996 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1997 * and stays in ip->flags. Once set, it stays set until the
1998 * inode is destroyed.
2000 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2001 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
2002 ip->sync_trunc_off = ip->trunc_off;
2003 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
2004 ip->flags &= ~HAMMER_INODE_TRUNCATED;
2005 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
2008 * The save_trunc_off used to cache whether the B-Tree
2009 * holds any records past that point is not used until
2010 * after the truncation has succeeded, so we can safely
2013 if (ip->save_trunc_off > ip->sync_trunc_off)
2014 ip->save_trunc_off = ip->sync_trunc_off;
2016 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
2017 ~HAMMER_INODE_TRUNCATED);
2018 ip->sync_ino_leaf = ip->ino_leaf;
2019 ip->sync_ino_data = ip->ino_data;
2020 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
2021 #ifdef DEBUG_TRUNCATE
2022 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
2023 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
2027 * The flusher list inherits our inode and reference.
2029 KKASSERT(flg->running == 0);
2030 TAILQ_INSERT_TAIL(&flg->flush_list, ip, flush_entry);
2031 if (--ip->hmp->flusher.group_lock == 0)
2032 wakeup(&ip->hmp->flusher.group_lock);
2034 if (flags & HAMMER_FLUSH_SIGNAL) {
2035 hammer_flusher_async(ip->hmp, flg);
2040 * Callback for scan of ip->rec_tree. Try to include each record in our
2041 * flush. ip->flush_group has been set but the inode has not yet been
2042 * moved into a flushing state.
2044 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
2047 * We return 1 for any record placed or found in FST_FLUSH, which prevents
2048 * the caller from shortcutting the flush.
2051 hammer_setup_child_callback(hammer_record_t rec, void *data)
2053 hammer_flush_group_t flg;
2054 hammer_inode_t target_ip;
2059 * Records deleted or committed by the backend are ignored.
2060 * Note that the flush detects deleted frontend records at
2061 * multiple points to deal with races. This is just the first
2062 * line of defense. The only time HAMMER_RECF_DELETED_FE cannot
2063 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2064 * messes up link-count calculations.
2066 * NOTE: Don't get confused between record deletion and, say,
2067 * directory entry deletion. The deletion of a directory entry
2068 * which is on-media has nothing to do with the record deletion
2071 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2072 HAMMER_RECF_COMMITTED)) {
2073 if (rec->flush_state == HAMMER_FST_FLUSH) {
2074 KKASSERT(rec->flush_group == rec->ip->flush_group);
2083 * If the record is in an idle state it has no dependancies and
2087 flg = ip->flush_group;
2090 switch(rec->flush_state) {
2091 case HAMMER_FST_IDLE:
2093 * The record has no setup dependancy, we can flush it.
2095 KKASSERT(rec->target_ip == NULL);
2096 rec->flush_state = HAMMER_FST_FLUSH;
2097 rec->flush_group = flg;
2099 hammer_ref(&rec->lock);
2102 case HAMMER_FST_SETUP:
2104 * The record has a setup dependancy. These are typically
2105 * directory entry adds and deletes. Such entries will be
2106 * flushed when their inodes are flushed so we do not
2107 * usually have to add them to the flush here. However,
2108 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2109 * it is asking us to flush this record (and it).
2111 target_ip = rec->target_ip;
2112 KKASSERT(target_ip != NULL);
2113 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2116 * If the target IP is already flushing in our group
2117 * we could associate the record, but target_ip has
2118 * already synced ino_data to sync_ino_data and we
2119 * would also have to adjust nlinks. Plus there are
2120 * ordering issues for adds and deletes.
2122 * Reflush downward if this is an ADD, and upward if
2125 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2126 if (rec->flush_state == HAMMER_MEM_RECORD_ADD)
2127 ip->flags |= HAMMER_INODE_REFLUSH;
2129 target_ip->flags |= HAMMER_INODE_REFLUSH;
2134 * Target IP is not yet flushing. This can get complex
2135 * because we have to be careful about the recursion.
2137 * Directories create an issue for us in that if a flush
2138 * of a directory is requested the expectation is to flush
2139 * any pending directory entries, but this will cause the
2140 * related inodes to recursively flush as well. We can't
2141 * really defer the operation so just get as many as we
2145 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2146 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2148 * We aren't reclaiming and the target ip was not
2149 * previously prevented from flushing due to this
2150 * record dependancy. Do not flush this record.
2155 if (flg->total_count + flg->refs >
2156 ip->hmp->undo_rec_limit) {
2158 * Our flush group is over-full and we risk blowing
2159 * out the UNDO FIFO. Stop the scan, flush what we
2160 * have, then reflush the directory.
2162 * The directory may be forced through multiple
2163 * flush groups before it can be completely
2166 ip->flags |= HAMMER_INODE_RESIGNAL |
2167 HAMMER_INODE_REFLUSH;
2169 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2171 * If the target IP is not flushing we can force
2172 * it to flush, even if it is unable to write out
2173 * any of its own records we have at least one in
2174 * hand that we CAN deal with.
2176 rec->flush_state = HAMMER_FST_FLUSH;
2177 rec->flush_group = flg;
2179 hammer_ref(&rec->lock);
2180 hammer_flush_inode_core(target_ip, flg,
2181 HAMMER_FLUSH_RECURSION);
2185 * General or delete-on-disk record.
2187 * XXX this needs help. If a delete-on-disk we could
2188 * disconnect the target. If the target has its own
2189 * dependancies they really need to be flushed.
2193 rec->flush_state = HAMMER_FST_FLUSH;
2194 rec->flush_group = flg;
2196 hammer_ref(&rec->lock);
2197 hammer_flush_inode_core(target_ip, flg,
2198 HAMMER_FLUSH_RECURSION);
2202 case HAMMER_FST_FLUSH:
2204 * The flush_group should already match.
2206 KKASSERT(rec->flush_group == flg);
2215 * This version just moves records already in a flush state to the new
2216 * flush group and that is it.
2219 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2221 hammer_inode_t ip = rec->ip;
2223 switch(rec->flush_state) {
2224 case HAMMER_FST_FLUSH:
2225 KKASSERT(rec->flush_group == ip->flush_group);
2235 * Wait for a previously queued flush to complete.
2237 * If a critical error occured we don't try to wait.
2240 hammer_wait_inode(hammer_inode_t ip)
2242 hammer_flush_group_t flg;
2245 if ((ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2246 while (ip->flush_state != HAMMER_FST_IDLE &&
2247 (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2248 if (ip->flush_state == HAMMER_FST_SETUP)
2249 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2250 if (ip->flush_state != HAMMER_FST_IDLE) {
2251 ip->flags |= HAMMER_INODE_FLUSHW;
2252 tsleep(&ip->flags, 0, "hmrwin", 0);
2259 * Called by the backend code when a flush has been completed.
2260 * The inode has already been removed from the flush list.
2262 * A pipelined flush can occur, in which case we must re-enter the
2263 * inode on the list and re-copy its fields.
2266 hammer_flush_inode_done(hammer_inode_t ip, int error)
2271 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2276 * Auto-reflush if the backend could not completely flush
2277 * the inode. This fixes a case where a deferred buffer flush
2278 * could cause fsync to return early.
2280 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2281 ip->flags |= HAMMER_INODE_REFLUSH;
2284 * Merge left-over flags back into the frontend and fix the state.
2285 * Incomplete truncations are retained by the backend.
2288 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2289 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2292 * The backend may have adjusted nlinks, so if the adjusted nlinks
2293 * does not match the fronttend set the frontend's RDIRTY flag again.
2295 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2296 ip->flags |= HAMMER_INODE_DDIRTY;
2299 * Fix up the dirty buffer status.
2301 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2302 ip->flags |= HAMMER_INODE_BUFS;
2306 * Re-set the XDIRTY flag if some of the inode's in-memory records
2307 * could not be flushed.
2309 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2310 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2311 (!RB_EMPTY(&ip->rec_tree) &&
2312 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2315 * Do not lose track of inodes which no longer have vnode
2316 * assocations, otherwise they may never get flushed again.
2318 * The reflush flag can be set superfluously, causing extra pain
2319 * for no reason. If the inode is no longer modified it no longer
2320 * needs to be flushed.
2322 if (ip->flags & HAMMER_INODE_MODMASK) {
2324 ip->flags |= HAMMER_INODE_REFLUSH;
2326 ip->flags &= ~HAMMER_INODE_REFLUSH;
2330 * Adjust the flush state.
2332 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2334 * We were unable to flush out all our records, leave the
2335 * inode in a flush state and in the current flush group.
2336 * The flush group will be re-run.
2338 * This occurs if the UNDO block gets too full or there is
2339 * too much dirty meta-data and allows the flusher to
2340 * finalize the UNDO block and then re-flush.
2342 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2346 * Remove from the flush_group
2348 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
2349 ip->flush_group = NULL;
2352 * Clean up the vnode ref and tracking counts.
2354 if (ip->flags & HAMMER_INODE_VHELD) {
2355 ip->flags &= ~HAMMER_INODE_VHELD;
2358 --hmp->count_iqueued;
2359 --hammer_count_iqueued;
2362 * And adjust the state.
2364 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2365 ip->flush_state = HAMMER_FST_IDLE;
2368 ip->flush_state = HAMMER_FST_SETUP;
2373 * If the frontend is waiting for a flush to complete,
2376 if (ip->flags & HAMMER_INODE_FLUSHW) {
2377 ip->flags &= ~HAMMER_INODE_FLUSHW;
2382 * If the frontend made more changes and requested another
2383 * flush, then try to get it running.
2385 * Reflushes are aborted when the inode is errored out.
2387 if (ip->flags & HAMMER_INODE_REFLUSH) {
2388 ip->flags &= ~HAMMER_INODE_REFLUSH;
2389 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2390 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2391 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2393 hammer_flush_inode(ip, 0);
2399 * If we have no parent dependancies we can clear CONN_DOWN
2401 if (TAILQ_EMPTY(&ip->target_list))
2402 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2405 * If the inode is now clean drop the space reservation.
2407 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2408 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2409 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2414 hammer_rel_inode(ip, 0);
2418 * Called from hammer_sync_inode() to synchronize in-memory records
2422 hammer_sync_record_callback(hammer_record_t record, void *data)
2424 hammer_cursor_t cursor = data;
2425 hammer_transaction_t trans = cursor->trans;
2426 hammer_mount_t hmp = trans->hmp;
2430 * Skip records that do not belong to the current flush.
2432 ++hammer_stats_record_iterations;
2433 if (record->flush_state != HAMMER_FST_FLUSH)
2437 if (record->flush_group != record->ip->flush_group) {
2438 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
2443 KKASSERT(record->flush_group == record->ip->flush_group);
2446 * Interlock the record using the BE flag. Once BE is set the
2447 * frontend cannot change the state of FE.
2449 * NOTE: If FE is set prior to us setting BE we still sync the
2450 * record out, but the flush completion code converts it to
2451 * a delete-on-disk record instead of destroying it.
2453 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2454 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2457 * The backend has already disposed of the record.
2459 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2465 * If the whole inode is being deleting all on-disk records will
2466 * be deleted very soon, we can't sync any new records to disk
2467 * because they will be deleted in the same transaction they were
2468 * created in (delete_tid == create_tid), which will assert.
2470 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2471 * that we currently panic on.
2473 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2474 switch(record->type) {
2475 case HAMMER_MEM_RECORD_DATA:
2477 * We don't have to do anything, if the record was
2478 * committed the space will have been accounted for
2482 case HAMMER_MEM_RECORD_GENERAL:
2484 * Set deleted-by-backend flag. Do not set the
2485 * backend committed flag, because we are throwing
2488 record->flags |= HAMMER_RECF_DELETED_BE;
2489 ++record->ip->rec_generation;
2492 case HAMMER_MEM_RECORD_ADD:
2493 panic("hammer_sync_record_callback: illegal add "
2494 "during inode deletion record %p", record);
2495 break; /* NOT REACHED */
2496 case HAMMER_MEM_RECORD_INODE:
2497 panic("hammer_sync_record_callback: attempt to "
2498 "sync inode record %p?", record);
2499 break; /* NOT REACHED */
2500 case HAMMER_MEM_RECORD_DEL:
2502 * Follow through and issue the on-disk deletion
2509 * If DELETED_FE is set special handling is needed for directory
2510 * entries. Dependant pieces related to the directory entry may
2511 * have already been synced to disk. If this occurs we have to
2512 * sync the directory entry and then change the in-memory record
2513 * from an ADD to a DELETE to cover the fact that it's been
2514 * deleted by the frontend.
2516 * A directory delete covering record (MEM_RECORD_DEL) can never
2517 * be deleted by the frontend.
2519 * Any other record type (aka DATA) can be deleted by the frontend.
2520 * XXX At the moment the flusher must skip it because there may
2521 * be another data record in the flush group for the same block,
2522 * meaning that some frontend data changes can leak into the backend's
2523 * synchronization point.
2525 if (record->flags & HAMMER_RECF_DELETED_FE) {
2526 if (record->type == HAMMER_MEM_RECORD_ADD) {
2528 * Convert a front-end deleted directory-add to
2529 * a directory-delete entry later.
2531 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2534 * Dispose of the record (race case). Mark as
2535 * deleted by backend (and not committed).
2537 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2538 record->flags |= HAMMER_RECF_DELETED_BE;
2539 ++record->ip->rec_generation;
2546 * Assign the create_tid for new records. Deletions already
2547 * have the record's entire key properly set up.
2549 if (record->type != HAMMER_MEM_RECORD_DEL) {
2550 record->leaf.base.create_tid = trans->tid;
2551 record->leaf.create_ts = trans->time32;
2554 error = hammer_ip_sync_record_cursor(cursor, record);
2555 if (error != EDEADLK)
2557 hammer_done_cursor(cursor);
2558 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2563 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2568 hammer_flush_record_done(record, error);
2571 * Do partial finalization if we have built up too many dirty
2572 * buffers. Otherwise a buffer cache deadlock can occur when
2573 * doing things like creating tens of thousands of tiny files.
2575 * We must release our cursor lock to avoid a 3-way deadlock
2576 * due to the exclusive sync lock the finalizer must get.
2578 if (hammer_flusher_meta_limit(hmp)) {
2579 hammer_unlock_cursor(cursor);
2580 hammer_flusher_finalize(trans, 0);
2581 hammer_lock_cursor(cursor);
2588 * Backend function called by the flusher to sync an inode to media.
2591 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2593 struct hammer_cursor cursor;
2594 hammer_node_t tmp_node;
2595 hammer_record_t depend;
2596 hammer_record_t next;
2597 int error, tmp_error;
2600 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2603 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2608 * Any directory records referencing this inode which are not in
2609 * our current flush group must adjust our nlink count for the
2610 * purposes of synchronization to disk.
2612 * Records which are in our flush group can be unlinked from our
2613 * inode now, potentially allowing the inode to be physically
2616 * This cannot block.
2618 nlinks = ip->ino_data.nlinks;
2619 next = TAILQ_FIRST(&ip->target_list);
2620 while ((depend = next) != NULL) {
2621 next = TAILQ_NEXT(depend, target_entry);
2622 if (depend->flush_state == HAMMER_FST_FLUSH &&
2623 depend->flush_group == ip->flush_group) {
2625 * If this is an ADD that was deleted by the frontend
2626 * the frontend nlinks count will have already been
2627 * decremented, but the backend is going to sync its
2628 * directory entry and must account for it. The
2629 * record will be converted to a delete-on-disk when
2632 * If the ADD was not deleted by the frontend we
2633 * can remove the dependancy from our target_list.
2635 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2638 TAILQ_REMOVE(&ip->target_list, depend,
2640 depend->target_ip = NULL;
2642 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2644 * Not part of our flush group and not deleted by
2645 * the front-end, adjust the link count synced to
2646 * the media (undo what the frontend did when it
2647 * queued the record).
2649 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2650 switch(depend->type) {
2651 case HAMMER_MEM_RECORD_ADD:
2654 case HAMMER_MEM_RECORD_DEL:
2664 * Set dirty if we had to modify the link count.
2666 if (ip->sync_ino_data.nlinks != nlinks) {
2667 KKASSERT((int64_t)nlinks >= 0);
2668 ip->sync_ino_data.nlinks = nlinks;
2669 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2673 * If there is a trunction queued destroy any data past the (aligned)
2674 * truncation point. Userland will have dealt with the buffer
2675 * containing the truncation point for us.
2677 * We don't flush pending frontend data buffers until after we've
2678 * dealt with the truncation.
2680 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2682 * Interlock trunc_off. The VOP front-end may continue to
2683 * make adjustments to it while we are blocked.
2686 off_t aligned_trunc_off;
2689 trunc_off = ip->sync_trunc_off;
2690 blkmask = hammer_blocksize(trunc_off) - 1;
2691 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2694 * Delete any whole blocks on-media. The front-end has
2695 * already cleaned out any partial block and made it
2696 * pending. The front-end may have updated trunc_off
2697 * while we were blocked so we only use sync_trunc_off.
2699 * This operation can blow out the buffer cache, EWOULDBLOCK
2700 * means we were unable to complete the deletion. The
2701 * deletion will update sync_trunc_off in that case.
2703 error = hammer_ip_delete_range(&cursor, ip,
2705 0x7FFFFFFFFFFFFFFFLL, 2);
2706 if (error == EWOULDBLOCK) {
2707 ip->flags |= HAMMER_INODE_WOULDBLOCK;
2709 goto defer_buffer_flush;
2716 * Clear the truncation flag on the backend after we have
2717 * complete the deletions. Backend data is now good again
2718 * (including new records we are about to sync, below).
2720 * Leave sync_trunc_off intact. As we write additional
2721 * records the backend will update sync_trunc_off. This
2722 * tells the backend whether it can skip the overwrite
2723 * test. This should work properly even when the backend
2724 * writes full blocks where the truncation point straddles
2725 * the block because the comparison is against the base
2726 * offset of the record.
2728 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2729 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2735 * Now sync related records. These will typically be directory
2736 * entries, records tracking direct-writes, or delete-on-disk records.
2739 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2740 hammer_sync_record_callback, &cursor);
2746 hammer_cache_node(&ip->cache[1], cursor.node);
2749 * Re-seek for inode update, assuming our cache hasn't been ripped
2750 * out from under us.
2753 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
2755 hammer_cursor_downgrade(&cursor);
2756 hammer_lock_sh(&tmp_node->lock);
2757 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2758 hammer_cursor_seek(&cursor, tmp_node, 0);
2759 hammer_unlock(&tmp_node->lock);
2760 hammer_rel_node(tmp_node);
2766 * If we are deleting the inode the frontend had better not have
2767 * any active references on elements making up the inode.
2769 * The call to hammer_ip_delete_clean() cleans up auxillary records
2770 * but not DB or DATA records. Those must have already been deleted
2771 * by the normal truncation mechanic.
2773 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2774 RB_EMPTY(&ip->rec_tree) &&
2775 (ip->sync_flags & HAMMER_INODE_DELETING) &&
2776 (ip->flags & HAMMER_INODE_DELETED) == 0) {
2779 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2781 ip->flags |= HAMMER_INODE_DELETED;
2782 ip->sync_flags &= ~HAMMER_INODE_DELETING;
2783 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2784 KKASSERT(RB_EMPTY(&ip->rec_tree));
2787 * Set delete_tid in both the frontend and backend
2788 * copy of the inode record. The DELETED flag handles
2789 * this, do not set RDIRTY.
2791 ip->ino_leaf.base.delete_tid = trans->tid;
2792 ip->sync_ino_leaf.base.delete_tid = trans->tid;
2793 ip->ino_leaf.delete_ts = trans->time32;
2794 ip->sync_ino_leaf.delete_ts = trans->time32;
2798 * Adjust the inode count in the volume header
2800 hammer_sync_lock_sh(trans);
2801 if (ip->flags & HAMMER_INODE_ONDISK) {
2802 hammer_modify_volume_field(trans,
2805 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2806 hammer_modify_volume_done(trans->rootvol);
2808 hammer_sync_unlock(trans);
2814 ip->sync_flags &= ~HAMMER_INODE_BUFS;
2818 * Now update the inode's on-disk inode-data and/or on-disk record.
2819 * DELETED and ONDISK are managed only in ip->flags.
2821 * In the case of a defered buffer flush we still update the on-disk
2822 * inode to satisfy visibility requirements if there happen to be
2823 * directory dependancies.
2825 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2826 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2828 * If deleted and on-disk, don't set any additional flags.
2829 * the delete flag takes care of things.
2831 * Clear flags which may have been set by the frontend.
2833 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2834 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2835 HAMMER_INODE_DELETING);
2837 case HAMMER_INODE_DELETED:
2839 * Take care of the case where a deleted inode was never
2840 * flushed to the disk in the first place.
2842 * Clear flags which may have been set by the frontend.
2844 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2845 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2846 HAMMER_INODE_DELETING);
2847 while (RB_ROOT(&ip->rec_tree)) {
2848 hammer_record_t record = RB_ROOT(&ip->rec_tree);
2849 hammer_ref(&record->lock);
2850 KKASSERT(record->lock.refs == 1);
2851 record->flags |= HAMMER_RECF_DELETED_BE;
2852 ++record->ip->rec_generation;
2853 hammer_rel_mem_record(record);
2856 case HAMMER_INODE_ONDISK:
2858 * If already on-disk, do not set any additional flags.
2863 * If not on-disk and not deleted, set DDIRTY to force
2864 * an initial record to be written.
2866 * Also set the create_tid in both the frontend and backend
2867 * copy of the inode record.
2869 ip->ino_leaf.base.create_tid = trans->tid;
2870 ip->ino_leaf.create_ts = trans->time32;
2871 ip->sync_ino_leaf.base.create_tid = trans->tid;
2872 ip->sync_ino_leaf.create_ts = trans->time32;
2873 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2878 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2879 * is already on-disk the old record is marked as deleted.
2881 * If DELETED is set hammer_update_inode() will delete the existing
2882 * record without writing out a new one.
2884 * If *ONLY* the ITIMES flag is set we can update the record in-place.
2886 if (ip->flags & HAMMER_INODE_DELETED) {
2887 error = hammer_update_inode(&cursor, ip);
2889 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2890 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2891 error = hammer_update_itimes(&cursor, ip);
2893 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2894 error = hammer_update_inode(&cursor, ip);
2898 hammer_critical_error(ip->hmp, ip, error,
2899 "while syncing inode");
2901 hammer_done_cursor(&cursor);
2906 * This routine is called when the OS is no longer actively referencing
2907 * the inode (but might still be keeping it cached), or when releasing
2908 * the last reference to an inode.
2910 * At this point if the inode's nlinks count is zero we want to destroy
2911 * it, which may mean destroying it on-media too.
2914 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2919 * Set the DELETING flag when the link count drops to 0 and the
2920 * OS no longer has any opens on the inode.
2922 * The backend will clear DELETING (a mod flag) and set DELETED
2923 * (a state flag) when it is actually able to perform the
2926 * Don't reflag the deletion if the flusher is currently syncing
2927 * one that was already flagged. A previously set DELETING flag
2928 * may bounce around flags and sync_flags until the operation is
2931 if (ip->ino_data.nlinks == 0 &&
2932 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2933 ip->flags |= HAMMER_INODE_DELETING;
2934 ip->flags |= HAMMER_INODE_TRUNCATED;
2938 if (hammer_get_vnode(ip, &vp) != 0)
2946 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2947 vnode_pager_setsize(ip->vp, 0);
2956 * After potentially resolving a dependancy the inode is tested
2957 * to determine whether it needs to be reflushed.
2960 hammer_test_inode(hammer_inode_t ip)
2962 if (ip->flags & HAMMER_INODE_REFLUSH) {
2963 ip->flags &= ~HAMMER_INODE_REFLUSH;
2964 hammer_ref(&ip->lock);
2965 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2966 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2967 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2969 hammer_flush_inode(ip, 0);
2971 hammer_rel_inode(ip, 0);
2976 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2977 * reassociated with a vp or just before it gets freed.
2979 * Pipeline wakeups to threads blocked due to an excessive number of
2980 * detached inodes. The reclaim count generates a bit of negative
2984 hammer_inode_wakereclaims(hammer_inode_t ip, int dowake)
2986 struct hammer_reclaim *reclaim;
2987 hammer_mount_t hmp = ip->hmp;
2989 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
2992 --hammer_count_reclaiming;
2993 --hmp->inode_reclaims;
2994 ip->flags &= ~HAMMER_INODE_RECLAIM;
2996 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT || dowake) {
2997 reclaim = TAILQ_FIRST(&hmp->reclaim_list);
2998 if (reclaim && reclaim->count > 0 && --reclaim->count == 0) {
2999 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
3006 * Setup our reclaim pipeline. We only let so many detached (and dirty)
3007 * inodes build up before we start blocking.
3009 * When we block we don't care *which* inode has finished reclaiming,
3010 * as lone as one does. This is somewhat heuristical... we also put a
3011 * cap on how long we are willing to wait.
3014 hammer_inode_waitreclaims(hammer_mount_t hmp)
3016 struct hammer_reclaim reclaim;
3019 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT)
3021 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
3022 (HAMMER_RECLAIM_WAIT * 3) + 1;
3025 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
3026 tsleep(&reclaim, 0, "hmrrcm", delay);
3027 if (reclaim.count > 0)
3028 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
3033 * A larger then normal backlog of inodes is sitting in the flusher,
3034 * enforce a general slowdown to let it catch up. This routine is only
3035 * called on completion of a non-flusher-related transaction which
3036 * performed B-Tree node I/O.
3038 * It is possible for the flusher to stall in a continuous load.
3039 * blogbench -i1000 -o seems to do a good job generating this sort of load.
3040 * If the flusher is unable to catch up the inode count can bloat until
3041 * we run out of kvm.
3043 * This is a bit of a hack.
3046 hammer_inode_waithard(hammer_mount_t hmp)
3051 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
3052 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT / 2 &&
3053 hmp->count_iqueued < hmp->count_inodes / 20) {
3054 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
3058 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT ||
3059 hmp->count_iqueued < hmp->count_inodes / 10) {
3062 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3066 * Block for one flush cycle.
3068 hammer_flusher_wait_next(hmp);