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;
278 case HAMMER_OBJTYPE_REGFILE:
280 * MPSAFE read supported.
282 vp->v_flag |= VMP_READ;
289 * Only mark as the root vnode if the ip is not
290 * historical, otherwise the VFS cache will get
291 * confused. The other half of the special handling
292 * is in hammer_vop_nlookupdotdot().
294 * Pseudo-filesystem roots can be accessed via
295 * non-root filesystem paths and setting VROOT may
296 * confuse the namecache. Set VPFSROOT instead.
298 if (ip->obj_id == HAMMER_OBJID_ROOT &&
299 ip->obj_asof == hmp->asof) {
300 if (ip->obj_localization == 0)
303 vp->v_flag |= VPFSROOT;
306 vp->v_data = (void *)ip;
307 /* vnode locked by getnewvnode() */
308 /* make related vnode dirty if inode dirty? */
309 hammer_unlock(&ip->lock);
310 if (vp->v_type == VREG)
311 vinitvmio(vp, ip->ino_data.size);
316 * loop if the vget fails (aka races), or if the vp
317 * no longer matches ip->vp.
319 if (vget(vp, LK_EXCLUSIVE) == 0) {
330 * Locate all copies of the inode for obj_id compatible with the specified
331 * asof, reference, and issue the related call-back. This routine is used
332 * for direct-io invalidation and does not create any new inodes.
335 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
336 int (*callback)(hammer_inode_t ip, void *data),
339 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
340 hammer_inode_info_cmp_all_history,
345 * Acquire a HAMMER inode. The returned inode is not locked. These functions
346 * do not attach or detach the related vnode (use hammer_get_vnode() for
349 * The flags argument is only applied for newly created inodes, and only
350 * certain flags are inherited.
352 * Called from the frontend.
354 struct hammer_inode *
355 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
356 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
357 int flags, int *errorp)
359 hammer_mount_t hmp = trans->hmp;
360 struct hammer_node_cache *cachep;
361 struct hammer_inode_info iinfo;
362 struct hammer_cursor cursor;
363 struct hammer_inode *ip;
367 * Determine if we already have an inode cached. If we do then
370 * If we find an inode with no vnode we have to mark the
371 * transaction such that hammer_inode_waitreclaims() is
372 * called later on to avoid building up an infinite number
373 * of inodes. Otherwise we can continue to * add new inodes
374 * faster then they can be disposed of, even with the tsleep
377 * If we find a dummy inode we return a failure so dounlink
378 * (which does another lookup) doesn't try to mess with the
379 * link count. hammer_vop_nresolve() uses hammer_get_dummy_inode()
380 * to ref dummy inodes.
382 iinfo.obj_id = obj_id;
383 iinfo.obj_asof = asof;
384 iinfo.obj_localization = localization;
386 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
388 if (ip->flags & HAMMER_INODE_DUMMY) {
392 hammer_ref(&ip->lock);
398 * Allocate a new inode structure and deal with races later.
400 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
401 ++hammer_count_inodes;
404 ip->obj_asof = iinfo.obj_asof;
405 ip->obj_localization = localization;
407 ip->flags = flags & HAMMER_INODE_RO;
408 ip->cache[0].ip = ip;
409 ip->cache[1].ip = ip;
410 ip->cache[2].ip = ip;
411 ip->cache[3].ip = ip;
413 ip->flags |= HAMMER_INODE_RO;
414 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
415 0x7FFFFFFFFFFFFFFFLL;
416 RB_INIT(&ip->rec_tree);
417 TAILQ_INIT(&ip->target_list);
418 hammer_ref(&ip->lock);
421 * Locate the on-disk inode. If this is a PFS root we always
422 * access the current version of the root inode and (if it is not
423 * a master) always access information under it with a snapshot
426 * We cache recent inode lookups in this directory in dip->cache[2].
427 * If we can't find it we assume the inode we are looking for is
428 * close to the directory inode.
433 if (dip->cache[2].node)
434 cachep = &dip->cache[2];
436 cachep = &dip->cache[0];
438 hammer_init_cursor(trans, &cursor, cachep, NULL);
439 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
440 cursor.key_beg.obj_id = ip->obj_id;
441 cursor.key_beg.key = 0;
442 cursor.key_beg.create_tid = 0;
443 cursor.key_beg.delete_tid = 0;
444 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
445 cursor.key_beg.obj_type = 0;
447 cursor.asof = iinfo.obj_asof;
448 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
451 *errorp = hammer_btree_lookup(&cursor);
452 if (*errorp == EDEADLK) {
453 hammer_done_cursor(&cursor);
458 * On success the B-Tree lookup will hold the appropriate
459 * buffer cache buffers and provide a pointer to the requested
460 * information. Copy the information to the in-memory inode
461 * and cache the B-Tree node to improve future operations.
464 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
465 ip->ino_data = cursor.data->inode;
468 * cache[0] tries to cache the location of the object inode.
469 * The assumption is that it is near the directory inode.
471 * cache[1] tries to cache the location of the object data.
472 * We might have something in the governing directory from
473 * scan optimizations (see the strategy code in
476 * We update dip->cache[2], if possible, with the location
477 * of the object inode for future directory shortcuts.
479 hammer_cache_node(&ip->cache[0], cursor.node);
481 if (dip->cache[3].node) {
482 hammer_cache_node(&ip->cache[1],
485 hammer_cache_node(&dip->cache[2], cursor.node);
489 * The file should not contain any data past the file size
490 * stored in the inode. Setting save_trunc_off to the
491 * file size instead of max reduces B-Tree lookup overheads
492 * on append by allowing the flusher to avoid checking for
495 ip->save_trunc_off = ip->ino_data.size;
498 * Locate and assign the pseudofs management structure to
501 if (dip && dip->obj_localization == ip->obj_localization) {
502 ip->pfsm = dip->pfsm;
503 hammer_ref(&ip->pfsm->lock);
505 ip->pfsm = hammer_load_pseudofs(trans,
506 ip->obj_localization,
508 *errorp = 0; /* ignore ENOENT */
513 * The inode is placed on the red-black tree and will be synced to
514 * the media when flushed or by the filesystem sync. If this races
515 * another instantiation/lookup the insertion will fail.
518 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
519 hammer_free_inode(ip);
520 hammer_done_cursor(&cursor);
523 ip->flags |= HAMMER_INODE_ONDISK;
525 if (ip->flags & HAMMER_INODE_RSV_INODES) {
526 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
530 hammer_free_inode(ip);
533 hammer_done_cursor(&cursor);
534 trans->flags |= HAMMER_TRANSF_NEWINODE;
539 * Get a dummy inode to placemark a broken directory entry.
541 struct hammer_inode *
542 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
543 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
544 int flags, int *errorp)
546 hammer_mount_t hmp = trans->hmp;
547 struct hammer_inode_info iinfo;
548 struct hammer_inode *ip;
551 * Determine if we already have an inode cached. If we do then
554 * If we find an inode with no vnode we have to mark the
555 * transaction such that hammer_inode_waitreclaims() is
556 * called later on to avoid building up an infinite number
557 * of inodes. Otherwise we can continue to * add new inodes
558 * faster then they can be disposed of, even with the tsleep
561 * If we find a non-fake inode we return an error. Only fake
562 * inodes can be returned by this routine.
564 iinfo.obj_id = obj_id;
565 iinfo.obj_asof = asof;
566 iinfo.obj_localization = localization;
569 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
571 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
575 hammer_ref(&ip->lock);
580 * Allocate a new inode structure and deal with races later.
582 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
583 ++hammer_count_inodes;
586 ip->obj_asof = iinfo.obj_asof;
587 ip->obj_localization = localization;
589 ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
590 ip->cache[0].ip = ip;
591 ip->cache[1].ip = ip;
592 ip->cache[2].ip = ip;
593 ip->cache[3].ip = ip;
594 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
595 0x7FFFFFFFFFFFFFFFLL;
596 RB_INIT(&ip->rec_tree);
597 TAILQ_INIT(&ip->target_list);
598 hammer_ref(&ip->lock);
601 * Populate the dummy inode. Leave everything zero'd out.
603 * (ip->ino_leaf and ip->ino_data)
605 * Make the dummy inode a FIFO object which most copy programs
606 * will properly ignore.
608 ip->save_trunc_off = ip->ino_data.size;
609 ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
612 * Locate and assign the pseudofs management structure to
615 if (dip && dip->obj_localization == ip->obj_localization) {
616 ip->pfsm = dip->pfsm;
617 hammer_ref(&ip->pfsm->lock);
619 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
621 *errorp = 0; /* ignore ENOENT */
625 * The inode is placed on the red-black tree and will be synced to
626 * the media when flushed or by the filesystem sync. If this races
627 * another instantiation/lookup the insertion will fail.
629 * NOTE: Do not set HAMMER_INODE_ONDISK. The inode is a fake.
632 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
633 hammer_free_inode(ip);
637 if (ip->flags & HAMMER_INODE_RSV_INODES) {
638 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
641 hammer_free_inode(ip);
644 trans->flags |= HAMMER_TRANSF_NEWINODE;
649 * Return a referenced inode only if it is in our inode cache.
651 * Dummy inodes do not count.
653 struct hammer_inode *
654 hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
655 hammer_tid_t asof, u_int32_t localization)
657 hammer_mount_t hmp = trans->hmp;
658 struct hammer_inode_info iinfo;
659 struct hammer_inode *ip;
661 iinfo.obj_id = obj_id;
662 iinfo.obj_asof = asof;
663 iinfo.obj_localization = localization;
665 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
667 if (ip->flags & HAMMER_INODE_DUMMY)
670 hammer_ref(&ip->lock);
676 * Create a new filesystem object, returning the inode in *ipp. The
677 * returned inode will be referenced. The inode is created in-memory.
679 * If pfsm is non-NULL the caller wishes to create the root inode for
683 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
685 hammer_inode_t dip, const char *name, int namelen,
686 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
697 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
698 ++hammer_count_inodes;
700 trans->flags |= HAMMER_TRANSF_NEWINODE;
703 KKASSERT(pfsm->localization != 0);
704 ip->obj_id = HAMMER_OBJID_ROOT;
705 ip->obj_localization = pfsm->localization;
707 KKASSERT(dip != NULL);
708 namekey = hammer_directory_namekey(dip, name, namelen, &dummy);
709 ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
710 ip->obj_localization = dip->obj_localization;
713 KKASSERT(ip->obj_id != 0);
714 ip->obj_asof = hmp->asof;
716 ip->flush_state = HAMMER_FST_IDLE;
717 ip->flags = HAMMER_INODE_DDIRTY |
718 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
719 ip->cache[0].ip = ip;
720 ip->cache[1].ip = ip;
721 ip->cache[2].ip = ip;
722 ip->cache[3].ip = ip;
724 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
725 /* ip->save_trunc_off = 0; (already zero) */
726 RB_INIT(&ip->rec_tree);
727 TAILQ_INIT(&ip->target_list);
729 ip->ino_data.atime = trans->time;
730 ip->ino_data.mtime = trans->time;
731 ip->ino_data.size = 0;
732 ip->ino_data.nlinks = 0;
735 * A nohistory designator on the parent directory is inherited by
736 * the child. We will do this even for pseudo-fs creation... the
737 * sysad can turn it off.
740 ip->ino_data.uflags = dip->ino_data.uflags &
741 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
744 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
745 ip->ino_leaf.base.localization = ip->obj_localization +
746 HAMMER_LOCALIZE_INODE;
747 ip->ino_leaf.base.obj_id = ip->obj_id;
748 ip->ino_leaf.base.key = 0;
749 ip->ino_leaf.base.create_tid = 0;
750 ip->ino_leaf.base.delete_tid = 0;
751 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
752 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
754 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
755 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
756 ip->ino_data.mode = vap->va_mode;
757 ip->ino_data.ctime = trans->time;
760 * If we are running version 2 or greater directory entries are
761 * inode-localized instead of data-localized.
763 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
764 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
765 ip->ino_data.cap_flags |=
766 HAMMER_INODE_CAP_DIR_LOCAL_INO;
771 * Setup the ".." pointer. This only needs to be done for directories
772 * but we do it for all objects as a recovery aid.
775 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
778 * The parent_obj_localization field only applies to pseudo-fs roots.
779 * XXX this is no longer applicable, PFSs are no longer directly
780 * tied into the parent's directory structure.
782 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
783 ip->obj_id == HAMMER_OBJID_ROOT) {
784 ip->ino_data.ext.obj.parent_obj_localization =
785 dip->obj_localization;
789 switch(ip->ino_leaf.base.obj_type) {
790 case HAMMER_OBJTYPE_CDEV:
791 case HAMMER_OBJTYPE_BDEV:
792 ip->ino_data.rmajor = vap->va_rmajor;
793 ip->ino_data.rminor = vap->va_rminor;
800 * Calculate default uid/gid and overwrite with information from
804 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
805 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
806 xuid, cred, &vap->va_mode);
810 ip->ino_data.mode = vap->va_mode;
812 if (vap->va_vaflags & VA_UID_UUID_VALID)
813 ip->ino_data.uid = vap->va_uid_uuid;
814 else if (vap->va_uid != (uid_t)VNOVAL)
815 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
817 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
819 if (vap->va_vaflags & VA_GID_UUID_VALID)
820 ip->ino_data.gid = vap->va_gid_uuid;
821 else if (vap->va_gid != (gid_t)VNOVAL)
822 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
824 ip->ino_data.gid = dip->ino_data.gid;
826 hammer_ref(&ip->lock);
830 hammer_ref(&pfsm->lock);
832 } else if (dip->obj_localization == ip->obj_localization) {
833 ip->pfsm = dip->pfsm;
834 hammer_ref(&ip->pfsm->lock);
837 ip->pfsm = hammer_load_pseudofs(trans,
838 ip->obj_localization,
840 error = 0; /* ignore ENOENT */
844 hammer_free_inode(ip);
846 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
847 panic("hammer_create_inode: duplicate obj_id %llx",
848 (long long)ip->obj_id);
850 hammer_free_inode(ip);
857 * Final cleanup / freeing of an inode structure
860 hammer_free_inode(hammer_inode_t ip)
862 struct hammer_mount *hmp;
865 KKASSERT(ip->lock.refs == 1);
866 hammer_uncache_node(&ip->cache[0]);
867 hammer_uncache_node(&ip->cache[1]);
868 hammer_uncache_node(&ip->cache[2]);
869 hammer_uncache_node(&ip->cache[3]);
870 hammer_inode_wakereclaims(ip, 1);
872 hammer_clear_objid(ip);
873 --hammer_count_inodes;
876 hammer_rel_pseudofs(hmp, ip->pfsm);
879 kfree(ip, hmp->m_inodes);
884 * Retrieve pseudo-fs data. NULL will never be returned.
886 * If an error occurs *errorp will be set and a default template is returned,
887 * otherwise *errorp is set to 0. Typically when an error occurs it will
890 hammer_pseudofs_inmem_t
891 hammer_load_pseudofs(hammer_transaction_t trans,
892 u_int32_t localization, int *errorp)
894 hammer_mount_t hmp = trans->hmp;
896 hammer_pseudofs_inmem_t pfsm;
897 struct hammer_cursor cursor;
901 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
903 hammer_ref(&pfsm->lock);
909 * PFS records are stored in the root inode (not the PFS root inode,
910 * but the real root). Avoid an infinite recursion if loading
911 * the PFS for the real root.
914 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
916 HAMMER_DEF_LOCALIZATION, 0, errorp);
921 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
922 pfsm->localization = localization;
923 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
924 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
926 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
927 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
928 HAMMER_LOCALIZE_MISC;
929 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
930 cursor.key_beg.create_tid = 0;
931 cursor.key_beg.delete_tid = 0;
932 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
933 cursor.key_beg.obj_type = 0;
934 cursor.key_beg.key = localization;
935 cursor.asof = HAMMER_MAX_TID;
936 cursor.flags |= HAMMER_CURSOR_ASOF;
939 *errorp = hammer_ip_lookup(&cursor);
941 *errorp = hammer_btree_lookup(&cursor);
943 *errorp = hammer_ip_resolve_data(&cursor);
945 if (cursor.data->pfsd.mirror_flags &
946 HAMMER_PFSD_DELETED) {
949 bytes = cursor.leaf->data_len;
950 if (bytes > sizeof(pfsm->pfsd))
951 bytes = sizeof(pfsm->pfsd);
952 bcopy(cursor.data, &pfsm->pfsd, bytes);
956 hammer_done_cursor(&cursor);
958 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
959 hammer_ref(&pfsm->lock);
961 hammer_rel_inode(ip, 0);
962 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
963 kfree(pfsm, hmp->m_misc);
970 * Store pseudo-fs data. The backend will automatically delete any prior
971 * on-disk pseudo-fs data but we have to delete in-memory versions.
974 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
976 struct hammer_cursor cursor;
977 hammer_record_t record;
981 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
982 HAMMER_DEF_LOCALIZATION, 0, &error);
984 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
985 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
986 cursor.key_beg.localization = ip->obj_localization +
987 HAMMER_LOCALIZE_MISC;
988 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
989 cursor.key_beg.create_tid = 0;
990 cursor.key_beg.delete_tid = 0;
991 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
992 cursor.key_beg.obj_type = 0;
993 cursor.key_beg.key = pfsm->localization;
994 cursor.asof = HAMMER_MAX_TID;
995 cursor.flags |= HAMMER_CURSOR_ASOF;
998 * Replace any in-memory version of the record.
1000 error = hammer_ip_lookup(&cursor);
1001 if (error == 0 && hammer_cursor_inmem(&cursor)) {
1002 record = cursor.iprec;
1003 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
1004 KKASSERT(cursor.deadlk_rec == NULL);
1005 hammer_ref(&record->lock);
1006 cursor.deadlk_rec = record;
1009 record->flags |= HAMMER_RECF_DELETED_FE;
1015 * Allocate replacement general record. The backend flush will
1016 * delete any on-disk version of the record.
1018 if (error == 0 || error == ENOENT) {
1019 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
1020 record->type = HAMMER_MEM_RECORD_GENERAL;
1022 record->leaf.base.localization = ip->obj_localization +
1023 HAMMER_LOCALIZE_MISC;
1024 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
1025 record->leaf.base.key = pfsm->localization;
1026 record->leaf.data_len = sizeof(pfsm->pfsd);
1027 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
1028 error = hammer_ip_add_record(trans, record);
1030 hammer_done_cursor(&cursor);
1031 if (error == EDEADLK)
1033 hammer_rel_inode(ip, 0);
1038 * Create a root directory for a PFS if one does not alredy exist.
1040 * The PFS root stands alone so we must also bump the nlinks count
1041 * to prevent it from being destroyed on release.
1044 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1045 hammer_pseudofs_inmem_t pfsm)
1051 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1052 pfsm->localization, 0, &error);
1057 error = hammer_create_inode(trans, &vap, cred,
1061 ++ip->ino_data.nlinks;
1062 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1066 hammer_rel_inode(ip, 0);
1071 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1072 * if we are unable to disassociate all the inodes.
1076 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1080 hammer_ref(&ip->lock);
1081 if (ip->lock.refs == 2 && ip->vp)
1082 vclean_unlocked(ip->vp);
1083 if (ip->lock.refs == 1 && ip->vp == NULL)
1086 res = -1; /* stop, someone is using the inode */
1087 hammer_rel_inode(ip, 0);
1092 hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
1097 for (try = res = 0; try < 4; ++try) {
1098 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1099 hammer_inode_pfs_cmp,
1100 hammer_unload_pseudofs_callback,
1102 if (res == 0 && try > 1)
1104 hammer_flusher_sync(trans->hmp);
1113 * Release a reference on a PFS
1116 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1118 hammer_unref(&pfsm->lock);
1119 if (pfsm->lock.refs == 0) {
1120 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1121 kfree(pfsm, hmp->m_misc);
1126 * Called by hammer_sync_inode().
1129 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1131 hammer_transaction_t trans = cursor->trans;
1132 hammer_record_t record;
1140 * If the inode has a presence on-disk then locate it and mark
1141 * it deleted, setting DELONDISK.
1143 * The record may or may not be physically deleted, depending on
1144 * the retention policy.
1146 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1147 HAMMER_INODE_ONDISK) {
1148 hammer_normalize_cursor(cursor);
1149 cursor->key_beg.localization = ip->obj_localization +
1150 HAMMER_LOCALIZE_INODE;
1151 cursor->key_beg.obj_id = ip->obj_id;
1152 cursor->key_beg.key = 0;
1153 cursor->key_beg.create_tid = 0;
1154 cursor->key_beg.delete_tid = 0;
1155 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1156 cursor->key_beg.obj_type = 0;
1157 cursor->asof = ip->obj_asof;
1158 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1159 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1160 cursor->flags |= HAMMER_CURSOR_BACKEND;
1162 error = hammer_btree_lookup(cursor);
1163 if (hammer_debug_inode)
1164 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1167 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1168 if (hammer_debug_inode)
1169 kprintf(" error %d\n", error);
1171 ip->flags |= HAMMER_INODE_DELONDISK;
1174 hammer_cache_node(&ip->cache[0], cursor->node);
1176 if (error == EDEADLK) {
1177 hammer_done_cursor(cursor);
1178 error = hammer_init_cursor(trans, cursor,
1180 if (hammer_debug_inode)
1181 kprintf("IPDED %p %d\n", ip, error);
1188 * Ok, write out the initial record or a new record (after deleting
1189 * the old one), unless the DELETED flag is set. This routine will
1190 * clear DELONDISK if it writes out a record.
1192 * Update our inode statistics if this is the first application of
1193 * the inode on-disk.
1195 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1197 * Generate a record and write it to the media. We clean-up
1198 * the state before releasing so we do not have to set-up
1201 record = hammer_alloc_mem_record(ip, 0);
1202 record->type = HAMMER_MEM_RECORD_INODE;
1203 record->flush_state = HAMMER_FST_FLUSH;
1204 record->leaf = ip->sync_ino_leaf;
1205 record->leaf.base.create_tid = trans->tid;
1206 record->leaf.data_len = sizeof(ip->sync_ino_data);
1207 record->leaf.create_ts = trans->time32;
1208 record->data = (void *)&ip->sync_ino_data;
1209 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1212 * If this flag is set we cannot sync the new file size
1213 * because we haven't finished related truncations. The
1214 * inode will be flushed in another flush group to finish
1217 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1218 ip->sync_ino_data.size != ip->ino_data.size) {
1220 ip->sync_ino_data.size = ip->ino_data.size;
1226 error = hammer_ip_sync_record_cursor(cursor, record);
1227 if (hammer_debug_inode)
1228 kprintf("GENREC %p rec %08x %d\n",
1229 ip, record->flags, error);
1230 if (error != EDEADLK)
1232 hammer_done_cursor(cursor);
1233 error = hammer_init_cursor(trans, cursor,
1235 if (hammer_debug_inode)
1236 kprintf("GENREC reinit %d\n", error);
1242 * Note: The record was never on the inode's record tree
1243 * so just wave our hands importantly and destroy it.
1245 record->flags |= HAMMER_RECF_COMMITTED;
1246 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1247 record->flush_state = HAMMER_FST_IDLE;
1248 ++ip->rec_generation;
1249 hammer_rel_mem_record(record);
1255 if (hammer_debug_inode)
1256 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1257 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1258 HAMMER_INODE_ATIME |
1259 HAMMER_INODE_MTIME);
1260 ip->flags &= ~HAMMER_INODE_DELONDISK;
1262 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1265 * Root volume count of inodes
1267 hammer_sync_lock_sh(trans);
1268 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1269 hammer_modify_volume_field(trans,
1272 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1273 hammer_modify_volume_done(trans->rootvol);
1274 ip->flags |= HAMMER_INODE_ONDISK;
1275 if (hammer_debug_inode)
1276 kprintf("NOWONDISK %p\n", ip);
1278 hammer_sync_unlock(trans);
1283 * If the inode has been destroyed, clean out any left-over flags
1284 * that may have been set by the frontend.
1286 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1287 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1288 HAMMER_INODE_ATIME |
1289 HAMMER_INODE_MTIME);
1295 * Update only the itimes fields.
1297 * ATIME can be updated without generating any UNDO. MTIME is updated
1298 * with UNDO so it is guaranteed to be synchronized properly in case of
1301 * Neither field is included in the B-Tree leaf element's CRC, which is how
1302 * we can get away with updating ATIME the way we do.
1305 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1307 hammer_transaction_t trans = cursor->trans;
1311 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1312 HAMMER_INODE_ONDISK) {
1316 hammer_normalize_cursor(cursor);
1317 cursor->key_beg.localization = ip->obj_localization +
1318 HAMMER_LOCALIZE_INODE;
1319 cursor->key_beg.obj_id = ip->obj_id;
1320 cursor->key_beg.key = 0;
1321 cursor->key_beg.create_tid = 0;
1322 cursor->key_beg.delete_tid = 0;
1323 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1324 cursor->key_beg.obj_type = 0;
1325 cursor->asof = ip->obj_asof;
1326 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1327 cursor->flags |= HAMMER_CURSOR_ASOF;
1328 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1329 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1330 cursor->flags |= HAMMER_CURSOR_BACKEND;
1332 error = hammer_btree_lookup(cursor);
1334 hammer_cache_node(&ip->cache[0], cursor->node);
1335 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1337 * Updating MTIME requires an UNDO. Just cover
1338 * both atime and mtime.
1340 hammer_sync_lock_sh(trans);
1341 hammer_modify_buffer(trans, cursor->data_buffer,
1342 HAMMER_ITIMES_BASE(&cursor->data->inode),
1343 HAMMER_ITIMES_BYTES);
1344 cursor->data->inode.atime = ip->sync_ino_data.atime;
1345 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1346 hammer_modify_buffer_done(cursor->data_buffer);
1347 hammer_sync_unlock(trans);
1348 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1350 * Updating atime only can be done in-place with
1353 hammer_sync_lock_sh(trans);
1354 hammer_modify_buffer(trans, cursor->data_buffer,
1356 cursor->data->inode.atime = ip->sync_ino_data.atime;
1357 hammer_modify_buffer_done(cursor->data_buffer);
1358 hammer_sync_unlock(trans);
1360 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1362 if (error == EDEADLK) {
1363 hammer_done_cursor(cursor);
1364 error = hammer_init_cursor(trans, cursor,
1373 * Release a reference on an inode, flush as requested.
1375 * On the last reference we queue the inode to the flusher for its final
1379 hammer_rel_inode(struct hammer_inode *ip, int flush)
1381 /*hammer_mount_t hmp = ip->hmp;*/
1384 * Handle disposition when dropping the last ref.
1387 if (ip->lock.refs == 1) {
1389 * Determine whether on-disk action is needed for
1390 * the inode's final disposition.
1392 KKASSERT(ip->vp == NULL);
1393 hammer_inode_unloadable_check(ip, 0);
1394 if (ip->flags & HAMMER_INODE_MODMASK) {
1395 hammer_flush_inode(ip, 0);
1396 } else if (ip->lock.refs == 1) {
1397 hammer_unload_inode(ip);
1402 hammer_flush_inode(ip, 0);
1405 * The inode still has multiple refs, try to drop
1408 KKASSERT(ip->lock.refs >= 1);
1409 if (ip->lock.refs > 1) {
1410 hammer_unref(&ip->lock);
1418 * Unload and destroy the specified inode. Must be called with one remaining
1419 * reference. The reference is disposed of.
1421 * The inode must be completely clean.
1424 hammer_unload_inode(struct hammer_inode *ip)
1426 hammer_mount_t hmp = ip->hmp;
1428 KASSERT(ip->lock.refs == 1,
1429 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1430 KKASSERT(ip->vp == NULL);
1431 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1432 KKASSERT(ip->cursor_ip_refs == 0);
1433 KKASSERT(ip->lock.lockcount == 0);
1434 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1436 KKASSERT(RB_EMPTY(&ip->rec_tree));
1437 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1439 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1441 hammer_free_inode(ip);
1446 * Called during unmounting if a critical error occured. The in-memory
1447 * inode and all related structures are destroyed.
1449 * If a critical error did not occur the unmount code calls the standard
1450 * release and asserts that the inode is gone.
1453 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1455 hammer_record_t rec;
1458 * Get rid of the inodes in-memory records, regardless of their
1459 * state, and clear the mod-mask.
1461 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1462 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1463 rec->target_ip = NULL;
1464 if (rec->flush_state == HAMMER_FST_SETUP)
1465 rec->flush_state = HAMMER_FST_IDLE;
1467 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1468 if (rec->flush_state == HAMMER_FST_FLUSH)
1469 --rec->flush_group->refs;
1471 hammer_ref(&rec->lock);
1472 KKASSERT(rec->lock.refs == 1);
1473 rec->flush_state = HAMMER_FST_IDLE;
1474 rec->flush_group = NULL;
1475 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1476 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1477 ++ip->rec_generation;
1478 hammer_rel_mem_record(rec);
1480 ip->flags &= ~HAMMER_INODE_MODMASK;
1481 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1482 KKASSERT(ip->vp == NULL);
1485 * Remove the inode from any flush group, force it idle. FLUSH
1486 * and SETUP states have an inode ref.
1488 switch(ip->flush_state) {
1489 case HAMMER_FST_FLUSH:
1490 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
1491 --ip->flush_group->refs;
1492 ip->flush_group = NULL;
1494 case HAMMER_FST_SETUP:
1495 hammer_unref(&ip->lock);
1496 ip->flush_state = HAMMER_FST_IDLE;
1498 case HAMMER_FST_IDLE:
1503 * There shouldn't be any associated vnode. The unload needs at
1504 * least one ref, if we do have a vp steal its ip ref.
1507 kprintf("hammer_destroy_inode_callback: Unexpected "
1508 "vnode association ip %p vp %p\n", ip, ip->vp);
1509 ip->vp->v_data = NULL;
1512 hammer_ref(&ip->lock);
1514 hammer_unload_inode(ip);
1519 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1520 * the read-only flag for cached inodes.
1522 * This routine is called from a RB_SCAN().
1525 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1527 hammer_mount_t hmp = ip->hmp;
1529 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1530 ip->flags |= HAMMER_INODE_RO;
1532 ip->flags &= ~HAMMER_INODE_RO;
1537 * A transaction has modified an inode, requiring updates as specified by
1540 * HAMMER_INODE_DDIRTY: Inode data has been updated
1541 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1542 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1543 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1544 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1547 hammer_modify_inode(hammer_inode_t ip, int flags)
1550 * ronly of 0 or 2 does not trigger assertion.
1551 * 2 is a special error state
1553 KKASSERT(ip->hmp->ronly != 1 ||
1554 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1555 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1556 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1557 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1558 ip->flags |= HAMMER_INODE_RSV_INODES;
1559 ++ip->hmp->rsv_inodes;
1566 * Request that an inode be flushed. This whole mess cannot block and may
1567 * recurse (if not synchronous). Once requested HAMMER will attempt to
1568 * actively flush the inode until the flush can be done.
1570 * The inode may already be flushing, or may be in a setup state. We can
1571 * place the inode in a flushing state if it is currently idle and flag it
1572 * to reflush if it is currently flushing.
1574 * Upon return if the inode could not be flushed due to a setup
1575 * dependancy, then it will be automatically flushed when the dependancy
1579 hammer_flush_inode(hammer_inode_t ip, int flags)
1582 hammer_flush_group_t flg;
1586 * next_flush_group is the first flush group we can place the inode
1587 * in. It may be NULL. If it becomes full we append a new flush
1588 * group and make that the next_flush_group.
1591 while ((flg = hmp->next_flush_group) != NULL) {
1592 KKASSERT(flg->running == 0);
1593 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit)
1595 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
1596 hammer_flusher_async(ip->hmp, flg);
1599 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1600 hmp->next_flush_group = flg;
1601 TAILQ_INIT(&flg->flush_list);
1602 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1606 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1607 * state we have to put it back into an IDLE state so we can
1608 * drop the extra ref.
1610 * If we have a parent dependancy we must still fall through
1613 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1614 if (ip->flush_state == HAMMER_FST_SETUP &&
1615 TAILQ_EMPTY(&ip->target_list)) {
1616 ip->flush_state = HAMMER_FST_IDLE;
1617 hammer_rel_inode(ip, 0);
1619 if (ip->flush_state == HAMMER_FST_IDLE)
1624 * Our flush action will depend on the current state.
1626 switch(ip->flush_state) {
1627 case HAMMER_FST_IDLE:
1629 * We have no dependancies and can flush immediately. Some
1630 * our children may not be flushable so we have to re-test
1631 * with that additional knowledge.
1633 hammer_flush_inode_core(ip, flg, flags);
1635 case HAMMER_FST_SETUP:
1637 * Recurse upwards through dependancies via target_list
1638 * and start their flusher actions going if possible.
1640 * 'good' is our connectivity. -1 means we have none and
1641 * can't flush, 0 means there weren't any dependancies, and
1642 * 1 means we have good connectivity.
1644 good = hammer_setup_parent_inodes(ip, 0, flg);
1648 * We can continue if good >= 0. Determine how
1649 * many records under our inode can be flushed (and
1652 hammer_flush_inode_core(ip, flg, flags);
1655 * Parent has no connectivity, tell it to flush
1656 * us as soon as it does.
1658 * The REFLUSH flag is also needed to trigger
1659 * dependancy wakeups.
1661 ip->flags |= HAMMER_INODE_CONN_DOWN |
1662 HAMMER_INODE_REFLUSH;
1663 if (flags & HAMMER_FLUSH_SIGNAL) {
1664 ip->flags |= HAMMER_INODE_RESIGNAL;
1665 hammer_flusher_async(ip->hmp, flg);
1669 case HAMMER_FST_FLUSH:
1671 * We are already flushing, flag the inode to reflush
1672 * if needed after it completes its current flush.
1674 * The REFLUSH flag is also needed to trigger
1675 * dependancy wakeups.
1677 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1678 ip->flags |= HAMMER_INODE_REFLUSH;
1679 if (flags & HAMMER_FLUSH_SIGNAL) {
1680 ip->flags |= HAMMER_INODE_RESIGNAL;
1681 hammer_flusher_async(ip->hmp, flg);
1688 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1689 * ip which reference our ip.
1691 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1692 * so for now do not ref/deref the structures. Note that if we use the
1693 * ref/rel code later, the rel CAN block.
1696 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1697 hammer_flush_group_t flg)
1699 hammer_record_t depend;
1704 * If we hit our recursion limit and we have parent dependencies
1705 * We cannot continue. Returning < 0 will cause us to be flagged
1706 * for reflush. Returning -2 cuts off additional dependency checks
1707 * because they are likely to also hit the depth limit.
1709 * We cannot return < 0 if there are no dependencies or there might
1710 * not be anything to wakeup (ip).
1712 if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1713 kprintf("HAMMER Warning: depth limit reached on "
1714 "setup recursion, inode %p %016llx\n",
1715 ip, (long long)ip->obj_id);
1723 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1724 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1725 KKASSERT(depend->target_ip == ip);
1726 if (r < 0 && good == 0)
1732 * If we failed due to the recursion depth limit then stop
1742 * This helper function takes a record representing the dependancy between
1743 * the parent inode and child inode.
1745 * record->ip = parent inode
1746 * record->target_ip = child inode
1748 * We are asked to recurse upwards and convert the record from SETUP
1749 * to FLUSH if possible.
1751 * Return 1 if the record gives us connectivity
1753 * Return 0 if the record is not relevant
1755 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1758 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1759 hammer_flush_group_t flg)
1765 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1770 * If the record is already flushing, is it in our flush group?
1772 * If it is in our flush group but it is a general record or a
1773 * delete-on-disk, it does not improve our connectivity (return 0),
1774 * and if the target inode is not trying to destroy itself we can't
1775 * allow the operation yet anyway (the second return -1).
1777 if (record->flush_state == HAMMER_FST_FLUSH) {
1779 * If not in our flush group ask the parent to reflush
1780 * us as soon as possible.
1782 if (record->flush_group != flg) {
1783 pip->flags |= HAMMER_INODE_REFLUSH;
1784 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1789 * If in our flush group everything is already set up,
1790 * just return whether the record will improve our
1791 * visibility or not.
1793 if (record->type == HAMMER_MEM_RECORD_ADD)
1799 * It must be a setup record. Try to resolve the setup dependancies
1800 * by recursing upwards so we can place ip on the flush list.
1802 * Limit ourselves to 20 levels of recursion to avoid blowing out
1803 * the kernel stack. If we hit the recursion limit we can't flush
1804 * until the parent flushes. The parent will flush independantly
1805 * on its own and ultimately a deep recursion will be resolved.
1807 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1809 good = hammer_setup_parent_inodes(pip, depth + 1, flg);
1812 * If good < 0 the parent has no connectivity and we cannot safely
1813 * flush the directory entry, which also means we can't flush our
1814 * ip. Flag us for downward recursion once the parent's
1815 * connectivity is resolved. Flag the parent for [re]flush or it
1816 * may not check for downward recursions.
1819 pip->flags |= HAMMER_INODE_REFLUSH;
1820 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1825 * We are go, place the parent inode in a flushing state so we can
1826 * place its record in a flushing state. Note that the parent
1827 * may already be flushing. The record must be in the same flush
1828 * group as the parent.
1830 if (pip->flush_state != HAMMER_FST_FLUSH)
1831 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
1832 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1833 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1836 if (record->type == HAMMER_MEM_RECORD_DEL &&
1837 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1839 * Regardless of flushing state we cannot sync this path if the
1840 * record represents a delete-on-disk but the target inode
1841 * is not ready to sync its own deletion.
1843 * XXX need to count effective nlinks to determine whether
1844 * the flush is ok, otherwise removing a hardlink will
1845 * just leave the DEL record to rot.
1847 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1851 if (pip->flush_group == flg) {
1853 * Because we have not calculated nlinks yet we can just
1854 * set records to the flush state if the parent is in
1855 * the same flush group as we are.
1857 record->flush_state = HAMMER_FST_FLUSH;
1858 record->flush_group = flg;
1859 ++record->flush_group->refs;
1860 hammer_ref(&record->lock);
1863 * A general directory-add contributes to our visibility.
1865 * Otherwise it is probably a directory-delete or
1866 * delete-on-disk record and does not contribute to our
1867 * visbility (but we can still flush it).
1869 if (record->type == HAMMER_MEM_RECORD_ADD)
1874 * If the parent is not in our flush group we cannot
1875 * flush this record yet, there is no visibility.
1876 * We tell the parent to reflush and mark ourselves
1877 * so the parent knows it should flush us too.
1879 pip->flags |= HAMMER_INODE_REFLUSH;
1880 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1886 * This is the core routine placing an inode into the FST_FLUSH state.
1889 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
1894 * Set flush state and prevent the flusher from cycling into
1895 * the next flush group. Do not place the ip on the list yet.
1896 * Inodes not in the idle state get an extra reference.
1898 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1899 if (ip->flush_state == HAMMER_FST_IDLE)
1900 hammer_ref(&ip->lock);
1901 ip->flush_state = HAMMER_FST_FLUSH;
1902 ip->flush_group = flg;
1903 ++ip->hmp->flusher.group_lock;
1904 ++ip->hmp->count_iqueued;
1905 ++hammer_count_iqueued;
1909 * If the flush group reaches the autoflush limit we want to signal
1910 * the flusher. This is particularly important for remove()s.
1912 if (flg->total_count == hammer_autoflush)
1913 flags |= HAMMER_FLUSH_SIGNAL;
1916 * We need to be able to vfsync/truncate from the backend.
1918 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1919 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1920 ip->flags |= HAMMER_INODE_VHELD;
1925 * Figure out how many in-memory records we can actually flush
1926 * (not including inode meta-data, buffers, etc).
1928 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
1929 if (flags & HAMMER_FLUSH_RECURSION) {
1931 * If this is a upwards recursion we do not want to
1932 * recurse down again!
1936 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1938 * No new records are added if we must complete a flush
1939 * from a previous cycle, but we do have to move the records
1940 * from the previous cycle to the current one.
1943 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1944 hammer_syncgrp_child_callback, NULL);
1950 * Normal flush, scan records and bring them into the flush.
1951 * Directory adds and deletes are usually skipped (they are
1952 * grouped with the related inode rather then with the
1955 * go_count can be negative, which means the scan aborted
1956 * due to the flush group being over-full and we should
1957 * flush what we have.
1959 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1960 hammer_setup_child_callback, NULL);
1964 * This is a more involved test that includes go_count. If we
1965 * can't flush, flag the inode and return. If go_count is 0 we
1966 * were are unable to flush any records in our rec_tree and
1967 * must ignore the XDIRTY flag.
1969 if (go_count == 0) {
1970 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1971 --ip->hmp->count_iqueued;
1972 --hammer_count_iqueued;
1975 ip->flush_state = HAMMER_FST_SETUP;
1976 ip->flush_group = NULL;
1977 if (ip->flags & HAMMER_INODE_VHELD) {
1978 ip->flags &= ~HAMMER_INODE_VHELD;
1983 * REFLUSH is needed to trigger dependancy wakeups
1984 * when an inode is in SETUP.
1986 ip->flags |= HAMMER_INODE_REFLUSH;
1987 if (flags & HAMMER_FLUSH_SIGNAL) {
1988 ip->flags |= HAMMER_INODE_RESIGNAL;
1989 hammer_flusher_async(ip->hmp, flg);
1991 if (--ip->hmp->flusher.group_lock == 0)
1992 wakeup(&ip->hmp->flusher.group_lock);
1998 * Snapshot the state of the inode for the backend flusher.
2000 * We continue to retain save_trunc_off even when all truncations
2001 * have been resolved as an optimization to determine if we can
2002 * skip the B-Tree lookup for overwrite deletions.
2004 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
2005 * and stays in ip->flags. Once set, it stays set until the
2006 * inode is destroyed.
2008 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2009 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
2010 ip->sync_trunc_off = ip->trunc_off;
2011 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
2012 ip->flags &= ~HAMMER_INODE_TRUNCATED;
2013 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
2016 * The save_trunc_off used to cache whether the B-Tree
2017 * holds any records past that point is not used until
2018 * after the truncation has succeeded, so we can safely
2021 if (ip->save_trunc_off > ip->sync_trunc_off)
2022 ip->save_trunc_off = ip->sync_trunc_off;
2024 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
2025 ~HAMMER_INODE_TRUNCATED);
2026 ip->sync_ino_leaf = ip->ino_leaf;
2027 ip->sync_ino_data = ip->ino_data;
2028 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
2029 #ifdef DEBUG_TRUNCATE
2030 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
2031 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
2035 * The flusher list inherits our inode and reference.
2037 KKASSERT(flg->running == 0);
2038 TAILQ_INSERT_TAIL(&flg->flush_list, ip, flush_entry);
2039 if (--ip->hmp->flusher.group_lock == 0)
2040 wakeup(&ip->hmp->flusher.group_lock);
2042 if (flags & HAMMER_FLUSH_SIGNAL) {
2043 hammer_flusher_async(ip->hmp, flg);
2048 * Callback for scan of ip->rec_tree. Try to include each record in our
2049 * flush. ip->flush_group has been set but the inode has not yet been
2050 * moved into a flushing state.
2052 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
2055 * We return 1 for any record placed or found in FST_FLUSH, which prevents
2056 * the caller from shortcutting the flush.
2059 hammer_setup_child_callback(hammer_record_t rec, void *data)
2061 hammer_flush_group_t flg;
2062 hammer_inode_t target_ip;
2067 * Records deleted or committed by the backend are ignored.
2068 * Note that the flush detects deleted frontend records at
2069 * multiple points to deal with races. This is just the first
2070 * line of defense. The only time HAMMER_RECF_DELETED_FE cannot
2071 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2072 * messes up link-count calculations.
2074 * NOTE: Don't get confused between record deletion and, say,
2075 * directory entry deletion. The deletion of a directory entry
2076 * which is on-media has nothing to do with the record deletion
2079 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2080 HAMMER_RECF_COMMITTED)) {
2081 if (rec->flush_state == HAMMER_FST_FLUSH) {
2082 KKASSERT(rec->flush_group == rec->ip->flush_group);
2091 * If the record is in an idle state it has no dependancies and
2095 flg = ip->flush_group;
2098 switch(rec->flush_state) {
2099 case HAMMER_FST_IDLE:
2101 * The record has no setup dependancy, we can flush it.
2103 KKASSERT(rec->target_ip == NULL);
2104 rec->flush_state = HAMMER_FST_FLUSH;
2105 rec->flush_group = flg;
2107 hammer_ref(&rec->lock);
2110 case HAMMER_FST_SETUP:
2112 * The record has a setup dependancy. These are typically
2113 * directory entry adds and deletes. Such entries will be
2114 * flushed when their inodes are flushed so we do not
2115 * usually have to add them to the flush here. However,
2116 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2117 * it is asking us to flush this record (and it).
2119 target_ip = rec->target_ip;
2120 KKASSERT(target_ip != NULL);
2121 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2124 * If the target IP is already flushing in our group
2125 * we could associate the record, but target_ip has
2126 * already synced ino_data to sync_ino_data and we
2127 * would also have to adjust nlinks. Plus there are
2128 * ordering issues for adds and deletes.
2130 * Reflush downward if this is an ADD, and upward if
2133 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2134 if (rec->flush_state == HAMMER_MEM_RECORD_ADD)
2135 ip->flags |= HAMMER_INODE_REFLUSH;
2137 target_ip->flags |= HAMMER_INODE_REFLUSH;
2142 * Target IP is not yet flushing. This can get complex
2143 * because we have to be careful about the recursion.
2145 * Directories create an issue for us in that if a flush
2146 * of a directory is requested the expectation is to flush
2147 * any pending directory entries, but this will cause the
2148 * related inodes to recursively flush as well. We can't
2149 * really defer the operation so just get as many as we
2153 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2154 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2156 * We aren't reclaiming and the target ip was not
2157 * previously prevented from flushing due to this
2158 * record dependancy. Do not flush this record.
2163 if (flg->total_count + flg->refs >
2164 ip->hmp->undo_rec_limit) {
2166 * Our flush group is over-full and we risk blowing
2167 * out the UNDO FIFO. Stop the scan, flush what we
2168 * have, then reflush the directory.
2170 * The directory may be forced through multiple
2171 * flush groups before it can be completely
2174 ip->flags |= HAMMER_INODE_RESIGNAL |
2175 HAMMER_INODE_REFLUSH;
2177 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2179 * If the target IP is not flushing we can force
2180 * it to flush, even if it is unable to write out
2181 * any of its own records we have at least one in
2182 * hand that we CAN deal with.
2184 rec->flush_state = HAMMER_FST_FLUSH;
2185 rec->flush_group = flg;
2187 hammer_ref(&rec->lock);
2188 hammer_flush_inode_core(target_ip, flg,
2189 HAMMER_FLUSH_RECURSION);
2193 * General or delete-on-disk record.
2195 * XXX this needs help. If a delete-on-disk we could
2196 * disconnect the target. If the target has its own
2197 * dependancies they really need to be flushed.
2201 rec->flush_state = HAMMER_FST_FLUSH;
2202 rec->flush_group = flg;
2204 hammer_ref(&rec->lock);
2205 hammer_flush_inode_core(target_ip, flg,
2206 HAMMER_FLUSH_RECURSION);
2210 case HAMMER_FST_FLUSH:
2212 * The flush_group should already match.
2214 KKASSERT(rec->flush_group == flg);
2223 * This version just moves records already in a flush state to the new
2224 * flush group and that is it.
2227 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2229 hammer_inode_t ip = rec->ip;
2231 switch(rec->flush_state) {
2232 case HAMMER_FST_FLUSH:
2233 KKASSERT(rec->flush_group == ip->flush_group);
2243 * Wait for a previously queued flush to complete.
2245 * If a critical error occured we don't try to wait.
2248 hammer_wait_inode(hammer_inode_t ip)
2250 hammer_flush_group_t flg;
2253 if ((ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2254 while (ip->flush_state != HAMMER_FST_IDLE &&
2255 (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2256 if (ip->flush_state == HAMMER_FST_SETUP)
2257 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2258 if (ip->flush_state != HAMMER_FST_IDLE) {
2259 ip->flags |= HAMMER_INODE_FLUSHW;
2260 tsleep(&ip->flags, 0, "hmrwin", 0);
2267 * Called by the backend code when a flush has been completed.
2268 * The inode has already been removed from the flush list.
2270 * A pipelined flush can occur, in which case we must re-enter the
2271 * inode on the list and re-copy its fields.
2274 hammer_flush_inode_done(hammer_inode_t ip, int error)
2279 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2284 * Auto-reflush if the backend could not completely flush
2285 * the inode. This fixes a case where a deferred buffer flush
2286 * could cause fsync to return early.
2288 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2289 ip->flags |= HAMMER_INODE_REFLUSH;
2292 * Merge left-over flags back into the frontend and fix the state.
2293 * Incomplete truncations are retained by the backend.
2296 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2297 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2300 * The backend may have adjusted nlinks, so if the adjusted nlinks
2301 * does not match the fronttend set the frontend's RDIRTY flag again.
2303 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2304 ip->flags |= HAMMER_INODE_DDIRTY;
2307 * Fix up the dirty buffer status.
2309 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2310 ip->flags |= HAMMER_INODE_BUFS;
2314 * Re-set the XDIRTY flag if some of the inode's in-memory records
2315 * could not be flushed.
2317 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2318 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2319 (!RB_EMPTY(&ip->rec_tree) &&
2320 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2323 * Do not lose track of inodes which no longer have vnode
2324 * assocations, otherwise they may never get flushed again.
2326 * The reflush flag can be set superfluously, causing extra pain
2327 * for no reason. If the inode is no longer modified it no longer
2328 * needs to be flushed.
2330 if (ip->flags & HAMMER_INODE_MODMASK) {
2332 ip->flags |= HAMMER_INODE_REFLUSH;
2334 ip->flags &= ~HAMMER_INODE_REFLUSH;
2338 * Adjust the flush state.
2340 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2342 * We were unable to flush out all our records, leave the
2343 * inode in a flush state and in the current flush group.
2344 * The flush group will be re-run.
2346 * This occurs if the UNDO block gets too full or there is
2347 * too much dirty meta-data and allows the flusher to
2348 * finalize the UNDO block and then re-flush.
2350 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2354 * Remove from the flush_group
2356 TAILQ_REMOVE(&ip->flush_group->flush_list, ip, flush_entry);
2357 ip->flush_group = NULL;
2360 * Clean up the vnode ref and tracking counts.
2362 if (ip->flags & HAMMER_INODE_VHELD) {
2363 ip->flags &= ~HAMMER_INODE_VHELD;
2366 --hmp->count_iqueued;
2367 --hammer_count_iqueued;
2370 * And adjust the state.
2372 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2373 ip->flush_state = HAMMER_FST_IDLE;
2376 ip->flush_state = HAMMER_FST_SETUP;
2381 * If the frontend is waiting for a flush to complete,
2384 if (ip->flags & HAMMER_INODE_FLUSHW) {
2385 ip->flags &= ~HAMMER_INODE_FLUSHW;
2390 * If the frontend made more changes and requested another
2391 * flush, then try to get it running.
2393 * Reflushes are aborted when the inode is errored out.
2395 if (ip->flags & HAMMER_INODE_REFLUSH) {
2396 ip->flags &= ~HAMMER_INODE_REFLUSH;
2397 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2398 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2399 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2401 hammer_flush_inode(ip, 0);
2407 * If we have no parent dependancies we can clear CONN_DOWN
2409 if (TAILQ_EMPTY(&ip->target_list))
2410 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2413 * If the inode is now clean drop the space reservation.
2415 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2416 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2417 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2422 hammer_rel_inode(ip, 0);
2426 * Called from hammer_sync_inode() to synchronize in-memory records
2430 hammer_sync_record_callback(hammer_record_t record, void *data)
2432 hammer_cursor_t cursor = data;
2433 hammer_transaction_t trans = cursor->trans;
2434 hammer_mount_t hmp = trans->hmp;
2438 * Skip records that do not belong to the current flush.
2440 ++hammer_stats_record_iterations;
2441 if (record->flush_state != HAMMER_FST_FLUSH)
2445 if (record->flush_group != record->ip->flush_group) {
2446 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
2451 KKASSERT(record->flush_group == record->ip->flush_group);
2454 * Interlock the record using the BE flag. Once BE is set the
2455 * frontend cannot change the state of FE.
2457 * NOTE: If FE is set prior to us setting BE we still sync the
2458 * record out, but the flush completion code converts it to
2459 * a delete-on-disk record instead of destroying it.
2461 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2462 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2465 * The backend has already disposed of the record.
2467 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2473 * If the whole inode is being deleting all on-disk records will
2474 * be deleted very soon, we can't sync any new records to disk
2475 * because they will be deleted in the same transaction they were
2476 * created in (delete_tid == create_tid), which will assert.
2478 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2479 * that we currently panic on.
2481 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2482 switch(record->type) {
2483 case HAMMER_MEM_RECORD_DATA:
2485 * We don't have to do anything, if the record was
2486 * committed the space will have been accounted for
2490 case HAMMER_MEM_RECORD_GENERAL:
2492 * Set deleted-by-backend flag. Do not set the
2493 * backend committed flag, because we are throwing
2496 record->flags |= HAMMER_RECF_DELETED_BE;
2497 ++record->ip->rec_generation;
2500 case HAMMER_MEM_RECORD_ADD:
2501 panic("hammer_sync_record_callback: illegal add "
2502 "during inode deletion record %p", record);
2503 break; /* NOT REACHED */
2504 case HAMMER_MEM_RECORD_INODE:
2505 panic("hammer_sync_record_callback: attempt to "
2506 "sync inode record %p?", record);
2507 break; /* NOT REACHED */
2508 case HAMMER_MEM_RECORD_DEL:
2510 * Follow through and issue the on-disk deletion
2517 * If DELETED_FE is set special handling is needed for directory
2518 * entries. Dependant pieces related to the directory entry may
2519 * have already been synced to disk. If this occurs we have to
2520 * sync the directory entry and then change the in-memory record
2521 * from an ADD to a DELETE to cover the fact that it's been
2522 * deleted by the frontend.
2524 * A directory delete covering record (MEM_RECORD_DEL) can never
2525 * be deleted by the frontend.
2527 * Any other record type (aka DATA) can be deleted by the frontend.
2528 * XXX At the moment the flusher must skip it because there may
2529 * be another data record in the flush group for the same block,
2530 * meaning that some frontend data changes can leak into the backend's
2531 * synchronization point.
2533 if (record->flags & HAMMER_RECF_DELETED_FE) {
2534 if (record->type == HAMMER_MEM_RECORD_ADD) {
2536 * Convert a front-end deleted directory-add to
2537 * a directory-delete entry later.
2539 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2542 * Dispose of the record (race case). Mark as
2543 * deleted by backend (and not committed).
2545 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2546 record->flags |= HAMMER_RECF_DELETED_BE;
2547 ++record->ip->rec_generation;
2554 * Assign the create_tid for new records. Deletions already
2555 * have the record's entire key properly set up.
2557 if (record->type != HAMMER_MEM_RECORD_DEL) {
2558 record->leaf.base.create_tid = trans->tid;
2559 record->leaf.create_ts = trans->time32;
2562 error = hammer_ip_sync_record_cursor(cursor, record);
2563 if (error != EDEADLK)
2565 hammer_done_cursor(cursor);
2566 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2571 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2576 hammer_flush_record_done(record, error);
2579 * Do partial finalization if we have built up too many dirty
2580 * buffers. Otherwise a buffer cache deadlock can occur when
2581 * doing things like creating tens of thousands of tiny files.
2583 * We must release our cursor lock to avoid a 3-way deadlock
2584 * due to the exclusive sync lock the finalizer must get.
2586 if (hammer_flusher_meta_limit(hmp)) {
2587 hammer_unlock_cursor(cursor);
2588 hammer_flusher_finalize(trans, 0);
2589 hammer_lock_cursor(cursor);
2596 * Backend function called by the flusher to sync an inode to media.
2599 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2601 struct hammer_cursor cursor;
2602 hammer_node_t tmp_node;
2603 hammer_record_t depend;
2604 hammer_record_t next;
2605 int error, tmp_error;
2608 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2611 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2616 * Any directory records referencing this inode which are not in
2617 * our current flush group must adjust our nlink count for the
2618 * purposes of synchronization to disk.
2620 * Records which are in our flush group can be unlinked from our
2621 * inode now, potentially allowing the inode to be physically
2624 * This cannot block.
2626 nlinks = ip->ino_data.nlinks;
2627 next = TAILQ_FIRST(&ip->target_list);
2628 while ((depend = next) != NULL) {
2629 next = TAILQ_NEXT(depend, target_entry);
2630 if (depend->flush_state == HAMMER_FST_FLUSH &&
2631 depend->flush_group == ip->flush_group) {
2633 * If this is an ADD that was deleted by the frontend
2634 * the frontend nlinks count will have already been
2635 * decremented, but the backend is going to sync its
2636 * directory entry and must account for it. The
2637 * record will be converted to a delete-on-disk when
2640 * If the ADD was not deleted by the frontend we
2641 * can remove the dependancy from our target_list.
2643 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2646 TAILQ_REMOVE(&ip->target_list, depend,
2648 depend->target_ip = NULL;
2650 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2652 * Not part of our flush group and not deleted by
2653 * the front-end, adjust the link count synced to
2654 * the media (undo what the frontend did when it
2655 * queued the record).
2657 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2658 switch(depend->type) {
2659 case HAMMER_MEM_RECORD_ADD:
2662 case HAMMER_MEM_RECORD_DEL:
2672 * Set dirty if we had to modify the link count.
2674 if (ip->sync_ino_data.nlinks != nlinks) {
2675 KKASSERT((int64_t)nlinks >= 0);
2676 ip->sync_ino_data.nlinks = nlinks;
2677 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2681 * If there is a trunction queued destroy any data past the (aligned)
2682 * truncation point. Userland will have dealt with the buffer
2683 * containing the truncation point for us.
2685 * We don't flush pending frontend data buffers until after we've
2686 * dealt with the truncation.
2688 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2690 * Interlock trunc_off. The VOP front-end may continue to
2691 * make adjustments to it while we are blocked.
2694 off_t aligned_trunc_off;
2697 trunc_off = ip->sync_trunc_off;
2698 blkmask = hammer_blocksize(trunc_off) - 1;
2699 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2702 * Delete any whole blocks on-media. The front-end has
2703 * already cleaned out any partial block and made it
2704 * pending. The front-end may have updated trunc_off
2705 * while we were blocked so we only use sync_trunc_off.
2707 * This operation can blow out the buffer cache, EWOULDBLOCK
2708 * means we were unable to complete the deletion. The
2709 * deletion will update sync_trunc_off in that case.
2711 error = hammer_ip_delete_range(&cursor, ip,
2713 0x7FFFFFFFFFFFFFFFLL, 2);
2714 if (error == EWOULDBLOCK) {
2715 ip->flags |= HAMMER_INODE_WOULDBLOCK;
2717 goto defer_buffer_flush;
2724 * Clear the truncation flag on the backend after we have
2725 * complete the deletions. Backend data is now good again
2726 * (including new records we are about to sync, below).
2728 * Leave sync_trunc_off intact. As we write additional
2729 * records the backend will update sync_trunc_off. This
2730 * tells the backend whether it can skip the overwrite
2731 * test. This should work properly even when the backend
2732 * writes full blocks where the truncation point straddles
2733 * the block because the comparison is against the base
2734 * offset of the record.
2736 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2737 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2743 * Now sync related records. These will typically be directory
2744 * entries, records tracking direct-writes, or delete-on-disk records.
2747 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2748 hammer_sync_record_callback, &cursor);
2754 hammer_cache_node(&ip->cache[1], cursor.node);
2757 * Re-seek for inode update, assuming our cache hasn't been ripped
2758 * out from under us.
2761 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
2763 hammer_cursor_downgrade(&cursor);
2764 hammer_lock_sh(&tmp_node->lock);
2765 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2766 hammer_cursor_seek(&cursor, tmp_node, 0);
2767 hammer_unlock(&tmp_node->lock);
2768 hammer_rel_node(tmp_node);
2774 * If we are deleting the inode the frontend had better not have
2775 * any active references on elements making up the inode.
2777 * The call to hammer_ip_delete_clean() cleans up auxillary records
2778 * but not DB or DATA records. Those must have already been deleted
2779 * by the normal truncation mechanic.
2781 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2782 RB_EMPTY(&ip->rec_tree) &&
2783 (ip->sync_flags & HAMMER_INODE_DELETING) &&
2784 (ip->flags & HAMMER_INODE_DELETED) == 0) {
2787 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2789 ip->flags |= HAMMER_INODE_DELETED;
2790 ip->sync_flags &= ~HAMMER_INODE_DELETING;
2791 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2792 KKASSERT(RB_EMPTY(&ip->rec_tree));
2795 * Set delete_tid in both the frontend and backend
2796 * copy of the inode record. The DELETED flag handles
2797 * this, do not set RDIRTY.
2799 ip->ino_leaf.base.delete_tid = trans->tid;
2800 ip->sync_ino_leaf.base.delete_tid = trans->tid;
2801 ip->ino_leaf.delete_ts = trans->time32;
2802 ip->sync_ino_leaf.delete_ts = trans->time32;
2806 * Adjust the inode count in the volume header
2808 hammer_sync_lock_sh(trans);
2809 if (ip->flags & HAMMER_INODE_ONDISK) {
2810 hammer_modify_volume_field(trans,
2813 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2814 hammer_modify_volume_done(trans->rootvol);
2816 hammer_sync_unlock(trans);
2822 ip->sync_flags &= ~HAMMER_INODE_BUFS;
2826 * Now update the inode's on-disk inode-data and/or on-disk record.
2827 * DELETED and ONDISK are managed only in ip->flags.
2829 * In the case of a defered buffer flush we still update the on-disk
2830 * inode to satisfy visibility requirements if there happen to be
2831 * directory dependancies.
2833 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2834 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2836 * If deleted and on-disk, don't set any additional flags.
2837 * the delete flag takes care of things.
2839 * Clear flags which may have been set by the frontend.
2841 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2842 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2843 HAMMER_INODE_DELETING);
2845 case HAMMER_INODE_DELETED:
2847 * Take care of the case where a deleted inode was never
2848 * flushed to the disk in the first place.
2850 * Clear flags which may have been set by the frontend.
2852 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2853 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2854 HAMMER_INODE_DELETING);
2855 while (RB_ROOT(&ip->rec_tree)) {
2856 hammer_record_t record = RB_ROOT(&ip->rec_tree);
2857 hammer_ref(&record->lock);
2858 KKASSERT(record->lock.refs == 1);
2859 record->flags |= HAMMER_RECF_DELETED_BE;
2860 ++record->ip->rec_generation;
2861 hammer_rel_mem_record(record);
2864 case HAMMER_INODE_ONDISK:
2866 * If already on-disk, do not set any additional flags.
2871 * If not on-disk and not deleted, set DDIRTY to force
2872 * an initial record to be written.
2874 * Also set the create_tid in both the frontend and backend
2875 * copy of the inode record.
2877 ip->ino_leaf.base.create_tid = trans->tid;
2878 ip->ino_leaf.create_ts = trans->time32;
2879 ip->sync_ino_leaf.base.create_tid = trans->tid;
2880 ip->sync_ino_leaf.create_ts = trans->time32;
2881 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2886 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2887 * is already on-disk the old record is marked as deleted.
2889 * If DELETED is set hammer_update_inode() will delete the existing
2890 * record without writing out a new one.
2892 * If *ONLY* the ITIMES flag is set we can update the record in-place.
2894 if (ip->flags & HAMMER_INODE_DELETED) {
2895 error = hammer_update_inode(&cursor, ip);
2897 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2898 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2899 error = hammer_update_itimes(&cursor, ip);
2901 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2902 error = hammer_update_inode(&cursor, ip);
2906 hammer_critical_error(ip->hmp, ip, error,
2907 "while syncing inode");
2909 hammer_done_cursor(&cursor);
2914 * This routine is called when the OS is no longer actively referencing
2915 * the inode (but might still be keeping it cached), or when releasing
2916 * the last reference to an inode.
2918 * At this point if the inode's nlinks count is zero we want to destroy
2919 * it, which may mean destroying it on-media too.
2922 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2927 * Set the DELETING flag when the link count drops to 0 and the
2928 * OS no longer has any opens on the inode.
2930 * The backend will clear DELETING (a mod flag) and set DELETED
2931 * (a state flag) when it is actually able to perform the
2934 * Don't reflag the deletion if the flusher is currently syncing
2935 * one that was already flagged. A previously set DELETING flag
2936 * may bounce around flags and sync_flags until the operation is
2939 if (ip->ino_data.nlinks == 0 &&
2940 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2941 ip->flags |= HAMMER_INODE_DELETING;
2942 ip->flags |= HAMMER_INODE_TRUNCATED;
2946 if (hammer_get_vnode(ip, &vp) != 0)
2954 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2955 vnode_pager_setsize(ip->vp, 0);
2964 * After potentially resolving a dependancy the inode is tested
2965 * to determine whether it needs to be reflushed.
2968 hammer_test_inode(hammer_inode_t ip)
2970 if (ip->flags & HAMMER_INODE_REFLUSH) {
2971 ip->flags &= ~HAMMER_INODE_REFLUSH;
2972 hammer_ref(&ip->lock);
2973 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2974 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2975 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2977 hammer_flush_inode(ip, 0);
2979 hammer_rel_inode(ip, 0);
2984 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2985 * reassociated with a vp or just before it gets freed.
2987 * Pipeline wakeups to threads blocked due to an excessive number of
2988 * detached inodes. The reclaim count generates a bit of negative
2992 hammer_inode_wakereclaims(hammer_inode_t ip, int dowake)
2994 struct hammer_reclaim *reclaim;
2995 hammer_mount_t hmp = ip->hmp;
2997 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
3000 --hammer_count_reclaiming;
3001 --hmp->inode_reclaims;
3002 ip->flags &= ~HAMMER_INODE_RECLAIM;
3004 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT || dowake) {
3005 reclaim = TAILQ_FIRST(&hmp->reclaim_list);
3006 if (reclaim && reclaim->count > 0 && --reclaim->count == 0) {
3007 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
3014 * Setup our reclaim pipeline. We only let so many detached (and dirty)
3015 * inodes build up before we start blocking.
3017 * When we block we don't care *which* inode has finished reclaiming,
3018 * as lone as one does. This is somewhat heuristical... we also put a
3019 * cap on how long we are willing to wait.
3022 hammer_inode_waitreclaims(hammer_mount_t hmp)
3024 struct hammer_reclaim reclaim;
3027 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT)
3029 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
3030 (HAMMER_RECLAIM_WAIT * 3) + 1;
3033 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
3034 tsleep(&reclaim, 0, "hmrrcm", delay);
3035 if (reclaim.count > 0)
3036 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
3041 * A larger then normal backlog of inodes is sitting in the flusher,
3042 * enforce a general slowdown to let it catch up. This routine is only
3043 * called on completion of a non-flusher-related transaction which
3044 * performed B-Tree node I/O.
3046 * It is possible for the flusher to stall in a continuous load.
3047 * blogbench -i1000 -o seems to do a good job generating this sort of load.
3048 * If the flusher is unable to catch up the inode count can bloat until
3049 * we run out of kvm.
3051 * This is a bit of a hack.
3054 hammer_inode_waithard(hammer_mount_t hmp)
3059 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
3060 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT / 2 &&
3061 hmp->count_iqueued < hmp->count_inodes / 20) {
3062 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
3066 if (hmp->inode_reclaims < HAMMER_RECLAIM_WAIT ||
3067 hmp->count_iqueued < hmp->count_inodes / 10) {
3070 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3074 * Block for one flush cycle.
3076 hammer_flusher_wait_next(hmp);