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
36 #include <vm/vm_extern.h>
38 static int hammer_unload_inode(struct hammer_inode *ip);
39 static void hammer_free_inode(hammer_inode_t ip);
40 static void hammer_flush_inode_core(hammer_inode_t ip,
41 hammer_flush_group_t flg, int flags);
42 static int hammer_setup_child_callback(hammer_record_t rec, void *data);
44 static int hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
46 static int hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
47 hammer_flush_group_t flg);
48 static int hammer_setup_parent_inodes_helper(hammer_record_t record,
49 int depth, hammer_flush_group_t flg);
50 static void hammer_inode_wakereclaims(hammer_inode_t ip);
51 static struct hammer_inostats *hammer_inode_inostats(hammer_mount_t hmp,
55 extern struct hammer_inode *HammerTruncIp;
58 struct krate hammer_gen_krate = { 1 };
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 hammer_redo_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
84 if (ip1->redo_fifo_start < ip2->redo_fifo_start)
86 if (ip1->redo_fifo_start > ip2->redo_fifo_start)
92 * RB-Tree support for inode structures / special LOOKUP_INFO
95 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
97 if (info->obj_localization < ip->obj_localization)
99 if (info->obj_localization > ip->obj_localization)
101 if (info->obj_id < ip->obj_id)
103 if (info->obj_id > ip->obj_id)
105 if (info->obj_asof < ip->obj_asof)
107 if (info->obj_asof > ip->obj_asof)
113 * Used by hammer_scan_inode_snapshots() to locate all of an object's
114 * snapshots. Note that the asof field is not tested, which we can get
115 * away with because it is the lowest-priority field.
118 hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
120 hammer_inode_info_t info = data;
122 if (ip->obj_localization > info->obj_localization)
124 if (ip->obj_localization < info->obj_localization)
126 if (ip->obj_id > info->obj_id)
128 if (ip->obj_id < info->obj_id)
134 * Used by hammer_unload_pseudofs() to locate all inodes associated with
138 hammer_inode_pfs_cmp(hammer_inode_t ip, void *data)
140 u_int32_t localization = *(u_int32_t *)data;
141 if (ip->obj_localization > localization)
143 if (ip->obj_localization < localization)
149 * RB-Tree support for pseudofs structures
152 hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
154 if (p1->localization < p2->localization)
156 if (p1->localization > p2->localization)
162 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
163 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
164 hammer_inode_info_cmp, hammer_inode_info_t);
165 RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
166 hammer_pfs_rb_compare, u_int32_t, localization);
169 * The kernel is not actively referencing this vnode but is still holding
172 * This is called from the frontend.
177 hammer_vop_inactive(struct vop_inactive_args *ap)
179 struct hammer_inode *ip = VTOI(ap->a_vp);
191 * If the inode no longer has visibility in the filesystem try to
192 * recycle it immediately, even if the inode is dirty. Recycling
193 * it quickly allows the system to reclaim buffer cache and VM
194 * resources which can matter a lot in a heavily loaded system.
196 * This can deadlock in vfsync() if we aren't careful.
198 * Do not queue the inode to the flusher if we still have visibility,
199 * otherwise namespace calls such as chmod will unnecessarily generate
200 * multiple inode updates.
202 if (ip->ino_data.nlinks == 0) {
204 lwkt_gettoken(&hmp->fs_token);
205 hammer_inode_unloadable_check(ip, 0);
206 if (ip->flags & HAMMER_INODE_MODMASK)
207 hammer_flush_inode(ip, 0);
208 lwkt_reltoken(&hmp->fs_token);
215 * Release the vnode association. This is typically (but not always)
216 * the last reference on the inode.
218 * Once the association is lost we are on our own with regards to
219 * flushing the inode.
221 * We must interlock ip->vp so hammer_get_vnode() can avoid races.
224 hammer_vop_reclaim(struct vop_reclaim_args *ap)
226 struct hammer_inode *ip;
232 if ((ip = vp->v_data) != NULL) {
234 lwkt_gettoken(&hmp->fs_token);
235 hammer_lock_ex(&ip->lock);
239 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
240 ++hammer_count_reclaims;
241 ++hmp->count_reclaims;
242 ip->flags |= HAMMER_INODE_RECLAIM;
244 hammer_unlock(&ip->lock);
246 hammer_rel_inode(ip, 1);
247 lwkt_reltoken(&hmp->fs_token);
253 * Inform the kernel that the inode is dirty. This will be checked
256 * Theoretically in order to reclaim a vnode the hammer_vop_reclaim()
257 * must be called which will interlock against our inode lock, so
258 * if VRECLAIMED is not set vp->v_mount (as used by vsetisdirty())
259 * should be stable without having to acquire any new locks.
262 hammer_inode_dirty(struct hammer_inode *ip)
266 if ((ip->flags & HAMMER_INODE_MODMASK) &&
267 (vp = ip->vp) != NULL &&
268 (vp->v_flag & (VRECLAIMED | VISDIRTY)) == 0) {
274 * Return a locked vnode for the specified inode. The inode must be
275 * referenced but NOT LOCKED on entry and will remain referenced on
278 * Called from the frontend.
281 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
291 if ((vp = ip->vp) == NULL) {
292 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
295 hammer_lock_ex(&ip->lock);
296 if (ip->vp != NULL) {
297 hammer_unlock(&ip->lock);
303 hammer_ref(&ip->lock);
307 obj_type = ip->ino_data.obj_type;
308 vp->v_type = hammer_get_vnode_type(obj_type);
310 hammer_inode_wakereclaims(ip);
312 switch(ip->ino_data.obj_type) {
313 case HAMMER_OBJTYPE_CDEV:
314 case HAMMER_OBJTYPE_BDEV:
315 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
316 addaliasu(vp, ip->ino_data.rmajor,
317 ip->ino_data.rminor);
319 case HAMMER_OBJTYPE_FIFO:
320 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
322 case HAMMER_OBJTYPE_REGFILE:
329 * Only mark as the root vnode if the ip is not
330 * historical, otherwise the VFS cache will get
331 * confused. The other half of the special handling
332 * is in hammer_vop_nlookupdotdot().
334 * Pseudo-filesystem roots can be accessed via
335 * non-root filesystem paths and setting VROOT may
336 * confuse the namecache. Set VPFSROOT instead.
338 if (ip->obj_id == HAMMER_OBJID_ROOT) {
339 if (ip->obj_asof == hmp->asof) {
340 if (ip->obj_localization == 0)
341 vsetflags(vp, VROOT);
343 vsetflags(vp, VPFSROOT);
345 vsetflags(vp, VPFSROOT);
349 vp->v_data = (void *)ip;
350 /* vnode locked by getnewvnode() */
351 /* make related vnode dirty if inode dirty? */
352 hammer_unlock(&ip->lock);
353 if (vp->v_type == VREG) {
354 vinitvmio(vp, ip->ino_data.size,
355 hammer_blocksize(ip->ino_data.size),
356 hammer_blockoff(ip->ino_data.size));
362 * Interlock vnode clearing. This does not prevent the
363 * vnode from going into a reclaimed state but it does
364 * prevent it from being destroyed or reused so the vget()
365 * will properly fail.
367 hammer_lock_ex(&ip->lock);
368 if ((vp = ip->vp) == NULL) {
369 hammer_unlock(&ip->lock);
373 hammer_unlock(&ip->lock);
376 * loop if the vget fails (aka races), or if the vp
377 * no longer matches ip->vp.
379 if (vget(vp, LK_EXCLUSIVE) == 0) {
393 * Locate all copies of the inode for obj_id compatible with the specified
394 * asof, reference, and issue the related call-back. This routine is used
395 * for direct-io invalidation and does not create any new inodes.
398 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
399 int (*callback)(hammer_inode_t ip, void *data),
402 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
403 hammer_inode_info_cmp_all_history,
408 * Acquire a HAMMER inode. The returned inode is not locked. These functions
409 * do not attach or detach the related vnode (use hammer_get_vnode() for
412 * The flags argument is only applied for newly created inodes, and only
413 * certain flags are inherited.
415 * Called from the frontend.
417 struct hammer_inode *
418 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
419 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
420 int flags, int *errorp)
422 hammer_mount_t hmp = trans->hmp;
423 struct hammer_node_cache *cachep;
424 struct hammer_inode_info iinfo;
425 struct hammer_cursor cursor;
426 struct hammer_inode *ip;
430 * Determine if we already have an inode cached. If we do then
433 * If we find an inode with no vnode we have to mark the
434 * transaction such that hammer_inode_waitreclaims() is
435 * called later on to avoid building up an infinite number
436 * of inodes. Otherwise we can continue to * add new inodes
437 * faster then they can be disposed of, even with the tsleep
440 * If we find a dummy inode we return a failure so dounlink
441 * (which does another lookup) doesn't try to mess with the
442 * link count. hammer_vop_nresolve() uses hammer_get_dummy_inode()
443 * to ref dummy inodes.
445 iinfo.obj_id = obj_id;
446 iinfo.obj_asof = asof;
447 iinfo.obj_localization = localization;
449 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
451 if (ip->flags & HAMMER_INODE_DUMMY) {
455 hammer_ref(&ip->lock);
461 * Allocate a new inode structure and deal with races later.
463 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
464 ++hammer_count_inodes;
467 ip->obj_asof = iinfo.obj_asof;
468 ip->obj_localization = localization;
470 ip->flags = flags & HAMMER_INODE_RO;
471 ip->cache[0].ip = ip;
472 ip->cache[1].ip = ip;
473 ip->cache[2].ip = ip;
474 ip->cache[3].ip = ip;
476 ip->flags |= HAMMER_INODE_RO;
477 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
478 0x7FFFFFFFFFFFFFFFLL;
479 RB_INIT(&ip->rec_tree);
480 TAILQ_INIT(&ip->target_list);
481 hammer_ref(&ip->lock);
484 * Locate the on-disk inode. If this is a PFS root we always
485 * access the current version of the root inode and (if it is not
486 * a master) always access information under it with a snapshot
489 * We cache recent inode lookups in this directory in dip->cache[2].
490 * If we can't find it we assume the inode we are looking for is
491 * close to the directory inode.
496 if (dip->cache[2].node)
497 cachep = &dip->cache[2];
499 cachep = &dip->cache[0];
501 hammer_init_cursor(trans, &cursor, cachep, NULL);
502 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
503 cursor.key_beg.obj_id = ip->obj_id;
504 cursor.key_beg.key = 0;
505 cursor.key_beg.create_tid = 0;
506 cursor.key_beg.delete_tid = 0;
507 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
508 cursor.key_beg.obj_type = 0;
510 cursor.asof = iinfo.obj_asof;
511 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
514 *errorp = hammer_btree_lookup(&cursor);
515 if (*errorp == EDEADLK) {
516 hammer_done_cursor(&cursor);
521 * On success the B-Tree lookup will hold the appropriate
522 * buffer cache buffers and provide a pointer to the requested
523 * information. Copy the information to the in-memory inode
524 * and cache the B-Tree node to improve future operations.
527 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
528 ip->ino_data = cursor.data->inode;
531 * cache[0] tries to cache the location of the object inode.
532 * The assumption is that it is near the directory inode.
534 * cache[1] tries to cache the location of the object data.
535 * We might have something in the governing directory from
536 * scan optimizations (see the strategy code in
539 * We update dip->cache[2], if possible, with the location
540 * of the object inode for future directory shortcuts.
542 hammer_cache_node(&ip->cache[0], cursor.node);
544 if (dip->cache[3].node) {
545 hammer_cache_node(&ip->cache[1],
548 hammer_cache_node(&dip->cache[2], cursor.node);
552 * The file should not contain any data past the file size
553 * stored in the inode. Setting save_trunc_off to the
554 * file size instead of max reduces B-Tree lookup overheads
555 * on append by allowing the flusher to avoid checking for
558 ip->save_trunc_off = ip->ino_data.size;
561 * Locate and assign the pseudofs management structure to
564 if (dip && dip->obj_localization == ip->obj_localization) {
565 ip->pfsm = dip->pfsm;
566 hammer_ref(&ip->pfsm->lock);
568 ip->pfsm = hammer_load_pseudofs(trans,
569 ip->obj_localization,
571 *errorp = 0; /* ignore ENOENT */
576 * The inode is placed on the red-black tree and will be synced to
577 * the media when flushed or by the filesystem sync. If this races
578 * another instantiation/lookup the insertion will fail.
581 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
582 hammer_free_inode(ip);
583 hammer_done_cursor(&cursor);
586 ip->flags |= HAMMER_INODE_ONDISK;
588 if (ip->flags & HAMMER_INODE_RSV_INODES) {
589 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
593 hammer_free_inode(ip);
596 hammer_done_cursor(&cursor);
599 * NEWINODE is only set if the inode becomes dirty later,
600 * setting it here just leads to unnecessary stalls.
602 * trans->flags |= HAMMER_TRANSF_NEWINODE;
608 * Get a dummy inode to placemark a broken directory entry.
610 struct hammer_inode *
611 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
612 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
613 int flags, int *errorp)
615 hammer_mount_t hmp = trans->hmp;
616 struct hammer_inode_info iinfo;
617 struct hammer_inode *ip;
620 * Determine if we already have an inode cached. If we do then
623 * If we find an inode with no vnode we have to mark the
624 * transaction such that hammer_inode_waitreclaims() is
625 * called later on to avoid building up an infinite number
626 * of inodes. Otherwise we can continue to * add new inodes
627 * faster then they can be disposed of, even with the tsleep
630 * If we find a non-fake inode we return an error. Only fake
631 * inodes can be returned by this routine.
633 iinfo.obj_id = obj_id;
634 iinfo.obj_asof = asof;
635 iinfo.obj_localization = localization;
638 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
640 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
644 hammer_ref(&ip->lock);
649 * Allocate a new inode structure and deal with races later.
651 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
652 ++hammer_count_inodes;
655 ip->obj_asof = iinfo.obj_asof;
656 ip->obj_localization = localization;
658 ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
659 ip->cache[0].ip = ip;
660 ip->cache[1].ip = ip;
661 ip->cache[2].ip = ip;
662 ip->cache[3].ip = ip;
663 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
664 0x7FFFFFFFFFFFFFFFLL;
665 RB_INIT(&ip->rec_tree);
666 TAILQ_INIT(&ip->target_list);
667 hammer_ref(&ip->lock);
670 * Populate the dummy inode. Leave everything zero'd out.
672 * (ip->ino_leaf and ip->ino_data)
674 * Make the dummy inode a FIFO object which most copy programs
675 * will properly ignore.
677 ip->save_trunc_off = ip->ino_data.size;
678 ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
681 * Locate and assign the pseudofs management structure to
684 if (dip && dip->obj_localization == ip->obj_localization) {
685 ip->pfsm = dip->pfsm;
686 hammer_ref(&ip->pfsm->lock);
688 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
690 *errorp = 0; /* ignore ENOENT */
694 * The inode is placed on the red-black tree and will be synced to
695 * the media when flushed or by the filesystem sync. If this races
696 * another instantiation/lookup the insertion will fail.
698 * NOTE: Do not set HAMMER_INODE_ONDISK. The inode is a fake.
701 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
702 hammer_free_inode(ip);
706 if (ip->flags & HAMMER_INODE_RSV_INODES) {
707 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
710 hammer_free_inode(ip);
713 trans->flags |= HAMMER_TRANSF_NEWINODE;
718 * Return a referenced inode only if it is in our inode cache.
720 * Dummy inodes do not count.
722 struct hammer_inode *
723 hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
724 hammer_tid_t asof, u_int32_t localization)
726 hammer_mount_t hmp = trans->hmp;
727 struct hammer_inode_info iinfo;
728 struct hammer_inode *ip;
730 iinfo.obj_id = obj_id;
731 iinfo.obj_asof = asof;
732 iinfo.obj_localization = localization;
734 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
736 if (ip->flags & HAMMER_INODE_DUMMY)
739 hammer_ref(&ip->lock);
745 * Create a new filesystem object, returning the inode in *ipp. The
746 * returned inode will be referenced. The inode is created in-memory.
748 * If pfsm is non-NULL the caller wishes to create the root inode for
752 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
754 hammer_inode_t dip, const char *name, int namelen,
755 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
767 * Disallow the creation of new inodes in directories which
768 * have been deleted. In HAMMER, this will cause a record
769 * syncing assertion later on in the flush code.
771 if (dip && dip->ino_data.nlinks == 0) {
779 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
780 ++hammer_count_inodes;
782 trans->flags |= HAMMER_TRANSF_NEWINODE;
785 KKASSERT(pfsm->localization != 0);
786 ip->obj_id = HAMMER_OBJID_ROOT;
787 ip->obj_localization = pfsm->localization;
789 KKASSERT(dip != NULL);
790 namekey = hammer_directory_namekey(dip, name, namelen, &dummy);
791 ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
792 ip->obj_localization = dip->obj_localization;
795 KKASSERT(ip->obj_id != 0);
796 ip->obj_asof = hmp->asof;
798 ip->flush_state = HAMMER_FST_IDLE;
799 ip->flags = HAMMER_INODE_DDIRTY |
800 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
801 ip->cache[0].ip = ip;
802 ip->cache[1].ip = ip;
803 ip->cache[2].ip = ip;
804 ip->cache[3].ip = ip;
806 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
807 /* ip->save_trunc_off = 0; (already zero) */
808 RB_INIT(&ip->rec_tree);
809 TAILQ_INIT(&ip->target_list);
811 ip->ino_data.atime = trans->time;
812 ip->ino_data.mtime = trans->time;
813 ip->ino_data.size = 0;
814 ip->ino_data.nlinks = 0;
817 * A nohistory designator on the parent directory is inherited by
818 * the child. We will do this even for pseudo-fs creation... the
819 * sysad can turn it off.
822 ip->ino_data.uflags = dip->ino_data.uflags &
823 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
826 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
827 ip->ino_leaf.base.localization = ip->obj_localization +
828 HAMMER_LOCALIZE_INODE;
829 ip->ino_leaf.base.obj_id = ip->obj_id;
830 ip->ino_leaf.base.key = 0;
831 ip->ino_leaf.base.create_tid = 0;
832 ip->ino_leaf.base.delete_tid = 0;
833 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
834 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
836 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
837 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
838 ip->ino_data.mode = vap->va_mode;
839 ip->ino_data.ctime = trans->time;
842 * If we are running version 2 or greater directory entries are
843 * inode-localized instead of data-localized.
845 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
846 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
847 ip->ino_data.cap_flags |=
848 HAMMER_INODE_CAP_DIR_LOCAL_INO;
851 if (trans->hmp->version >= HAMMER_VOL_VERSION_SIX) {
852 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
853 ip->ino_data.cap_flags |=
854 HAMMER_INODE_CAP_DIRHASH_ALG1;
859 * Setup the ".." pointer. This only needs to be done for directories
860 * but we do it for all objects as a recovery aid if dip exists.
861 * The inode is probably a PFS root if dip is NULL.
864 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
867 * The parent_obj_localization field only applies to pseudo-fs roots.
868 * XXX this is no longer applicable, PFSs are no longer directly
869 * tied into the parent's directory structure.
871 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
872 ip->obj_id == HAMMER_OBJID_ROOT) {
873 ip->ino_data.ext.obj.parent_obj_localization =
874 dip->obj_localization;
878 switch(ip->ino_leaf.base.obj_type) {
879 case HAMMER_OBJTYPE_CDEV:
880 case HAMMER_OBJTYPE_BDEV:
881 ip->ino_data.rmajor = vap->va_rmajor;
882 ip->ino_data.rminor = vap->va_rminor;
889 * Calculate default uid/gid and overwrite with information from
893 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
894 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
895 xuid, cred, &vap->va_mode);
899 ip->ino_data.mode = vap->va_mode;
901 if (vap->va_vaflags & VA_UID_UUID_VALID)
902 ip->ino_data.uid = vap->va_uid_uuid;
903 else if (vap->va_uid != (uid_t)VNOVAL)
904 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
906 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
908 if (vap->va_vaflags & VA_GID_UUID_VALID)
909 ip->ino_data.gid = vap->va_gid_uuid;
910 else if (vap->va_gid != (gid_t)VNOVAL)
911 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
913 ip->ino_data.gid = dip->ino_data.gid;
915 hammer_ref(&ip->lock);
919 hammer_ref(&pfsm->lock);
921 } else if (dip->obj_localization == ip->obj_localization) {
922 ip->pfsm = dip->pfsm;
923 hammer_ref(&ip->pfsm->lock);
926 ip->pfsm = hammer_load_pseudofs(trans,
927 ip->obj_localization,
929 error = 0; /* ignore ENOENT */
933 hammer_free_inode(ip);
935 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
936 panic("hammer_create_inode: duplicate obj_id %llx",
937 (long long)ip->obj_id);
939 hammer_free_inode(ip);
946 * Final cleanup / freeing of an inode structure
949 hammer_free_inode(hammer_inode_t ip)
951 struct hammer_mount *hmp;
954 KKASSERT(hammer_oneref(&ip->lock));
955 hammer_uncache_node(&ip->cache[0]);
956 hammer_uncache_node(&ip->cache[1]);
957 hammer_uncache_node(&ip->cache[2]);
958 hammer_uncache_node(&ip->cache[3]);
959 hammer_inode_wakereclaims(ip);
961 hammer_clear_objid(ip);
962 --hammer_count_inodes;
965 hammer_rel_pseudofs(hmp, ip->pfsm);
968 kfree(ip, hmp->m_inodes);
973 * Retrieve pseudo-fs data. NULL will never be returned.
975 * If an error occurs *errorp will be set and a default template is returned,
976 * otherwise *errorp is set to 0. Typically when an error occurs it will
979 hammer_pseudofs_inmem_t
980 hammer_load_pseudofs(hammer_transaction_t trans,
981 u_int32_t localization, int *errorp)
983 hammer_mount_t hmp = trans->hmp;
985 hammer_pseudofs_inmem_t pfsm;
986 struct hammer_cursor cursor;
990 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
992 hammer_ref(&pfsm->lock);
998 * PFS records are associated with the root inode (not the PFS root
999 * inode, but the real root). Avoid an infinite recursion if loading
1000 * the PFS for the real root.
1003 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
1005 HAMMER_DEF_LOCALIZATION, 0, errorp);
1010 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
1011 pfsm->localization = localization;
1012 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
1013 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
1015 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
1016 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
1017 HAMMER_LOCALIZE_MISC;
1018 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
1019 cursor.key_beg.create_tid = 0;
1020 cursor.key_beg.delete_tid = 0;
1021 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
1022 cursor.key_beg.obj_type = 0;
1023 cursor.key_beg.key = localization;
1024 cursor.asof = HAMMER_MAX_TID;
1025 cursor.flags |= HAMMER_CURSOR_ASOF;
1028 *errorp = hammer_ip_lookup(&cursor);
1030 *errorp = hammer_btree_lookup(&cursor);
1032 *errorp = hammer_ip_resolve_data(&cursor);
1034 if (cursor.data->pfsd.mirror_flags &
1035 HAMMER_PFSD_DELETED) {
1038 bytes = cursor.leaf->data_len;
1039 if (bytes > sizeof(pfsm->pfsd))
1040 bytes = sizeof(pfsm->pfsd);
1041 bcopy(cursor.data, &pfsm->pfsd, bytes);
1045 hammer_done_cursor(&cursor);
1047 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
1048 hammer_ref(&pfsm->lock);
1050 hammer_rel_inode(ip, 0);
1051 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
1052 kfree(pfsm, hmp->m_misc);
1059 * Store pseudo-fs data. The backend will automatically delete any prior
1060 * on-disk pseudo-fs data but we have to delete in-memory versions.
1063 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
1065 struct hammer_cursor cursor;
1066 hammer_record_t record;
1071 * PFS records are associated with the root inode (not the PFS root
1072 * inode, but the real root).
1074 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1075 HAMMER_DEF_LOCALIZATION, 0, &error);
1077 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
1078 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
1079 cursor.key_beg.localization = ip->obj_localization +
1080 HAMMER_LOCALIZE_MISC;
1081 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
1082 cursor.key_beg.create_tid = 0;
1083 cursor.key_beg.delete_tid = 0;
1084 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
1085 cursor.key_beg.obj_type = 0;
1086 cursor.key_beg.key = pfsm->localization;
1087 cursor.asof = HAMMER_MAX_TID;
1088 cursor.flags |= HAMMER_CURSOR_ASOF;
1091 * Replace any in-memory version of the record.
1093 error = hammer_ip_lookup(&cursor);
1094 if (error == 0 && hammer_cursor_inmem(&cursor)) {
1095 record = cursor.iprec;
1096 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
1097 KKASSERT(cursor.deadlk_rec == NULL);
1098 hammer_ref(&record->lock);
1099 cursor.deadlk_rec = record;
1102 record->flags |= HAMMER_RECF_DELETED_FE;
1108 * Allocate replacement general record. The backend flush will
1109 * delete any on-disk version of the record.
1111 if (error == 0 || error == ENOENT) {
1112 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
1113 record->type = HAMMER_MEM_RECORD_GENERAL;
1115 record->leaf.base.localization = ip->obj_localization +
1116 HAMMER_LOCALIZE_MISC;
1117 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
1118 record->leaf.base.key = pfsm->localization;
1119 record->leaf.data_len = sizeof(pfsm->pfsd);
1120 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
1121 error = hammer_ip_add_record(trans, record);
1123 hammer_done_cursor(&cursor);
1124 if (error == EDEADLK)
1126 hammer_rel_inode(ip, 0);
1131 * Create a root directory for a PFS if one does not alredy exist.
1133 * The PFS root stands alone so we must also bump the nlinks count
1134 * to prevent it from being destroyed on release.
1137 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1138 hammer_pseudofs_inmem_t pfsm)
1144 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1145 pfsm->localization, 0, &error);
1150 error = hammer_create_inode(trans, &vap, cred,
1154 ++ip->ino_data.nlinks;
1155 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
1159 hammer_rel_inode(ip, 0);
1164 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1165 * if we are unable to disassociate all the inodes.
1169 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1173 hammer_ref(&ip->lock);
1174 if (hammer_isactive(&ip->lock) == 2 && ip->vp)
1175 vclean_unlocked(ip->vp);
1176 if (hammer_isactive(&ip->lock) == 1 && ip->vp == NULL)
1179 res = -1; /* stop, someone is using the inode */
1180 hammer_rel_inode(ip, 0);
1185 hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
1190 for (try = res = 0; try < 4; ++try) {
1191 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1192 hammer_inode_pfs_cmp,
1193 hammer_unload_pseudofs_callback,
1195 if (res == 0 && try > 1)
1197 hammer_flusher_sync(trans->hmp);
1206 * Release a reference on a PFS
1209 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1211 hammer_rel(&pfsm->lock);
1212 if (hammer_norefs(&pfsm->lock)) {
1213 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1214 kfree(pfsm, hmp->m_misc);
1219 * Called by hammer_sync_inode().
1222 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1224 hammer_transaction_t trans = cursor->trans;
1225 hammer_record_t record;
1233 * If the inode has a presence on-disk then locate it and mark
1234 * it deleted, setting DELONDISK.
1236 * The record may or may not be physically deleted, depending on
1237 * the retention policy.
1239 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1240 HAMMER_INODE_ONDISK) {
1241 hammer_normalize_cursor(cursor);
1242 cursor->key_beg.localization = ip->obj_localization +
1243 HAMMER_LOCALIZE_INODE;
1244 cursor->key_beg.obj_id = ip->obj_id;
1245 cursor->key_beg.key = 0;
1246 cursor->key_beg.create_tid = 0;
1247 cursor->key_beg.delete_tid = 0;
1248 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1249 cursor->key_beg.obj_type = 0;
1250 cursor->asof = ip->obj_asof;
1251 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1252 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1253 cursor->flags |= HAMMER_CURSOR_BACKEND;
1255 error = hammer_btree_lookup(cursor);
1256 if (hammer_debug_inode)
1257 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1260 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1261 if (hammer_debug_inode)
1262 kprintf(" error %d\n", error);
1264 ip->flags |= HAMMER_INODE_DELONDISK;
1267 hammer_cache_node(&ip->cache[0], cursor->node);
1269 if (error == EDEADLK) {
1270 hammer_done_cursor(cursor);
1271 error = hammer_init_cursor(trans, cursor,
1273 if (hammer_debug_inode)
1274 kprintf("IPDED %p %d\n", ip, error);
1281 * Ok, write out the initial record or a new record (after deleting
1282 * the old one), unless the DELETED flag is set. This routine will
1283 * clear DELONDISK if it writes out a record.
1285 * Update our inode statistics if this is the first application of
1286 * the inode on-disk.
1288 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1290 * Generate a record and write it to the media. We clean-up
1291 * the state before releasing so we do not have to set-up
1294 record = hammer_alloc_mem_record(ip, 0);
1295 record->type = HAMMER_MEM_RECORD_INODE;
1296 record->flush_state = HAMMER_FST_FLUSH;
1297 record->leaf = ip->sync_ino_leaf;
1298 record->leaf.base.create_tid = trans->tid;
1299 record->leaf.data_len = sizeof(ip->sync_ino_data);
1300 record->leaf.create_ts = trans->time32;
1301 record->data = (void *)&ip->sync_ino_data;
1302 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1305 * If this flag is set we cannot sync the new file size
1306 * because we haven't finished related truncations. The
1307 * inode will be flushed in another flush group to finish
1310 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1311 ip->sync_ino_data.size != ip->ino_data.size) {
1313 ip->sync_ino_data.size = ip->ino_data.size;
1319 error = hammer_ip_sync_record_cursor(cursor, record);
1320 if (hammer_debug_inode)
1321 kprintf("GENREC %p rec %08x %d\n",
1322 ip, record->flags, error);
1323 if (error != EDEADLK)
1325 hammer_done_cursor(cursor);
1326 error = hammer_init_cursor(trans, cursor,
1328 if (hammer_debug_inode)
1329 kprintf("GENREC reinit %d\n", error);
1335 * Note: The record was never on the inode's record tree
1336 * so just wave our hands importantly and destroy it.
1338 record->flags |= HAMMER_RECF_COMMITTED;
1339 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1340 record->flush_state = HAMMER_FST_IDLE;
1341 ++ip->rec_generation;
1342 hammer_rel_mem_record(record);
1348 if (hammer_debug_inode)
1349 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1350 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1351 HAMMER_INODE_SDIRTY |
1352 HAMMER_INODE_ATIME |
1353 HAMMER_INODE_MTIME);
1354 ip->flags &= ~HAMMER_INODE_DELONDISK;
1356 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1359 * Root volume count of inodes
1361 hammer_sync_lock_sh(trans);
1362 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1363 hammer_modify_volume_field(trans,
1366 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1367 hammer_modify_volume_done(trans->rootvol);
1368 ip->flags |= HAMMER_INODE_ONDISK;
1369 if (hammer_debug_inode)
1370 kprintf("NOWONDISK %p\n", ip);
1372 hammer_sync_unlock(trans);
1377 * If the inode has been destroyed, clean out any left-over flags
1378 * that may have been set by the frontend.
1380 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1381 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1382 HAMMER_INODE_SDIRTY |
1383 HAMMER_INODE_ATIME |
1384 HAMMER_INODE_MTIME);
1390 * Update only the itimes fields.
1392 * ATIME can be updated without generating any UNDO. MTIME is updated
1393 * with UNDO so it is guaranteed to be synchronized properly in case of
1396 * Neither field is included in the B-Tree leaf element's CRC, which is how
1397 * we can get away with updating ATIME the way we do.
1400 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1402 hammer_transaction_t trans = cursor->trans;
1406 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1407 HAMMER_INODE_ONDISK) {
1411 hammer_normalize_cursor(cursor);
1412 cursor->key_beg.localization = ip->obj_localization +
1413 HAMMER_LOCALIZE_INODE;
1414 cursor->key_beg.obj_id = ip->obj_id;
1415 cursor->key_beg.key = 0;
1416 cursor->key_beg.create_tid = 0;
1417 cursor->key_beg.delete_tid = 0;
1418 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1419 cursor->key_beg.obj_type = 0;
1420 cursor->asof = ip->obj_asof;
1421 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1422 cursor->flags |= HAMMER_CURSOR_ASOF;
1423 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1424 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1425 cursor->flags |= HAMMER_CURSOR_BACKEND;
1427 error = hammer_btree_lookup(cursor);
1429 hammer_cache_node(&ip->cache[0], cursor->node);
1430 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1432 * Updating MTIME requires an UNDO. Just cover
1433 * both atime and mtime.
1435 hammer_sync_lock_sh(trans);
1436 hammer_modify_buffer(trans, cursor->data_buffer,
1437 HAMMER_ITIMES_BASE(&cursor->data->inode),
1438 HAMMER_ITIMES_BYTES);
1439 cursor->data->inode.atime = ip->sync_ino_data.atime;
1440 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1441 hammer_modify_buffer_done(cursor->data_buffer);
1442 hammer_sync_unlock(trans);
1443 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1445 * Updating atime only can be done in-place with
1448 hammer_sync_lock_sh(trans);
1449 hammer_modify_buffer(trans, cursor->data_buffer,
1451 cursor->data->inode.atime = ip->sync_ino_data.atime;
1452 hammer_modify_buffer_done(cursor->data_buffer);
1453 hammer_sync_unlock(trans);
1455 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1457 if (error == EDEADLK) {
1458 hammer_done_cursor(cursor);
1459 error = hammer_init_cursor(trans, cursor,
1468 * Release a reference on an inode, flush as requested.
1470 * On the last reference we queue the inode to the flusher for its final
1474 hammer_rel_inode(struct hammer_inode *ip, int flush)
1476 /*hammer_mount_t hmp = ip->hmp;*/
1479 * Handle disposition when dropping the last ref.
1482 if (hammer_oneref(&ip->lock)) {
1484 * Determine whether on-disk action is needed for
1485 * the inode's final disposition.
1487 KKASSERT(ip->vp == NULL);
1488 hammer_inode_unloadable_check(ip, 0);
1489 if (ip->flags & HAMMER_INODE_MODMASK) {
1490 hammer_flush_inode(ip, 0);
1491 } else if (hammer_oneref(&ip->lock)) {
1492 hammer_unload_inode(ip);
1497 hammer_flush_inode(ip, 0);
1500 * The inode still has multiple refs, try to drop
1503 KKASSERT(hammer_isactive(&ip->lock) >= 1);
1504 if (hammer_isactive(&ip->lock) > 1) {
1505 hammer_rel(&ip->lock);
1513 * Unload and destroy the specified inode. Must be called with one remaining
1514 * reference. The reference is disposed of.
1516 * The inode must be completely clean.
1519 hammer_unload_inode(struct hammer_inode *ip)
1521 hammer_mount_t hmp = ip->hmp;
1523 KASSERT(hammer_oneref(&ip->lock),
1524 ("hammer_unload_inode: %d refs", hammer_isactive(&ip->lock)));
1525 KKASSERT(ip->vp == NULL);
1526 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1527 KKASSERT(ip->cursor_ip_refs == 0);
1528 KKASSERT(hammer_notlocked(&ip->lock));
1529 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1531 KKASSERT(RB_EMPTY(&ip->rec_tree));
1532 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1534 if (ip->flags & HAMMER_INODE_RDIRTY) {
1535 RB_REMOVE(hammer_redo_rb_tree, &hmp->rb_redo_root, ip);
1536 ip->flags &= ~HAMMER_INODE_RDIRTY;
1538 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1540 hammer_free_inode(ip);
1545 * Called during unmounting if a critical error occured. The in-memory
1546 * inode and all related structures are destroyed.
1548 * If a critical error did not occur the unmount code calls the standard
1549 * release and asserts that the inode is gone.
1552 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1554 hammer_record_t rec;
1557 * Get rid of the inodes in-memory records, regardless of their
1558 * state, and clear the mod-mask.
1560 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1561 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1562 rec->target_ip = NULL;
1563 if (rec->flush_state == HAMMER_FST_SETUP)
1564 rec->flush_state = HAMMER_FST_IDLE;
1566 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1567 if (rec->flush_state == HAMMER_FST_FLUSH)
1568 --rec->flush_group->refs;
1570 hammer_ref(&rec->lock);
1571 KKASSERT(hammer_oneref(&rec->lock));
1572 rec->flush_state = HAMMER_FST_IDLE;
1573 rec->flush_group = NULL;
1574 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1575 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1576 ++ip->rec_generation;
1577 hammer_rel_mem_record(rec);
1579 ip->flags &= ~HAMMER_INODE_MODMASK;
1580 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1581 KKASSERT(ip->vp == NULL);
1584 * Remove the inode from any flush group, force it idle. FLUSH
1585 * and SETUP states have an inode ref.
1587 switch(ip->flush_state) {
1588 case HAMMER_FST_FLUSH:
1589 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
1590 --ip->flush_group->refs;
1591 ip->flush_group = NULL;
1593 case HAMMER_FST_SETUP:
1594 hammer_rel(&ip->lock);
1595 ip->flush_state = HAMMER_FST_IDLE;
1597 case HAMMER_FST_IDLE:
1602 * There shouldn't be any associated vnode. The unload needs at
1603 * least one ref, if we do have a vp steal its ip ref.
1606 kprintf("hammer_destroy_inode_callback: Unexpected "
1607 "vnode association ip %p vp %p\n", ip, ip->vp);
1608 ip->vp->v_data = NULL;
1611 hammer_ref(&ip->lock);
1613 hammer_unload_inode(ip);
1618 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1619 * the read-only flag for cached inodes.
1621 * This routine is called from a RB_SCAN().
1624 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1626 hammer_mount_t hmp = ip->hmp;
1628 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1629 ip->flags |= HAMMER_INODE_RO;
1631 ip->flags &= ~HAMMER_INODE_RO;
1636 * A transaction has modified an inode, requiring updates as specified by
1639 * HAMMER_INODE_DDIRTY: Inode data has been updated, not incl mtime/atime,
1640 * and not including size changes due to write-append
1641 * (but other size changes are included).
1642 * HAMMER_INODE_SDIRTY: Inode data has been updated, size changes due to
1644 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1645 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1646 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1647 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1650 hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags)
1653 * ronly of 0 or 2 does not trigger assertion.
1654 * 2 is a special error state
1656 KKASSERT(ip->hmp->ronly != 1 ||
1657 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1658 HAMMER_INODE_SDIRTY |
1659 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1660 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1661 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1662 ip->flags |= HAMMER_INODE_RSV_INODES;
1663 ++ip->hmp->rsv_inodes;
1667 * Set the NEWINODE flag in the transaction if the inode
1668 * transitions to a dirty state. This is used to track
1669 * the load on the inode cache.
1672 (ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1673 (flags & HAMMER_INODE_MODMASK)) {
1674 trans->flags |= HAMMER_TRANSF_NEWINODE;
1676 if (flags & HAMMER_INODE_MODMASK)
1677 hammer_inode_dirty(ip);
1682 * Attempt to quickly update the atime for a hammer inode. Return 0 on
1683 * success, -1 on failure.
1685 * We attempt to update the atime with only the ip lock and not the
1686 * whole filesystem lock in order to improve concurrency. We can only
1687 * do this safely if the ATIME flag is already pending on the inode.
1689 * This function is called via a vnops path (ip pointer is stable) without
1693 hammer_update_atime_quick(hammer_inode_t ip)
1698 if ((ip->flags & HAMMER_INODE_RO) ||
1699 (ip->hmp->mp->mnt_flag & MNT_NOATIME)) {
1701 * Silently indicate success on read-only mount/snap
1704 } else if (ip->flags & HAMMER_INODE_ATIME) {
1706 * Double check with inode lock held against backend. This
1707 * is only safe if all we need to do is update
1711 hammer_lock_ex(&ip->lock);
1712 if (ip->flags & HAMMER_INODE_ATIME) {
1713 ip->ino_data.atime =
1714 (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
1717 hammer_unlock(&ip->lock);
1723 * Request that an inode be flushed. This whole mess cannot block and may
1724 * recurse (if not synchronous). Once requested HAMMER will attempt to
1725 * actively flush the inode until the flush can be done.
1727 * The inode may already be flushing, or may be in a setup state. We can
1728 * place the inode in a flushing state if it is currently idle and flag it
1729 * to reflush if it is currently flushing.
1731 * Upon return if the inode could not be flushed due to a setup
1732 * dependancy, then it will be automatically flushed when the dependancy
1736 hammer_flush_inode(hammer_inode_t ip, int flags)
1739 hammer_flush_group_t flg;
1743 * fill_flush_group is the first flush group we may be able to
1744 * continue filling, it may be open or closed but it will always
1745 * be past the currently flushing (running) flg.
1747 * next_flush_group is the next open flush group.
1750 while ((flg = hmp->fill_flush_group) != NULL) {
1751 KKASSERT(flg->running == 0);
1752 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit &&
1753 flg->total_count <= hammer_autoflush) {
1756 hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
1757 hammer_flusher_async(ip->hmp, flg);
1760 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1761 flg->seq = hmp->flusher.next++;
1762 if (hmp->next_flush_group == NULL)
1763 hmp->next_flush_group = flg;
1764 if (hmp->fill_flush_group == NULL)
1765 hmp->fill_flush_group = flg;
1766 RB_INIT(&flg->flush_tree);
1767 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1771 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1772 * state we have to put it back into an IDLE state so we can
1773 * drop the extra ref.
1775 * If we have a parent dependancy we must still fall through
1778 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1779 if (ip->flush_state == HAMMER_FST_SETUP &&
1780 TAILQ_EMPTY(&ip->target_list)) {
1781 ip->flush_state = HAMMER_FST_IDLE;
1782 hammer_rel_inode(ip, 0);
1784 if (ip->flush_state == HAMMER_FST_IDLE)
1789 * Our flush action will depend on the current state.
1791 switch(ip->flush_state) {
1792 case HAMMER_FST_IDLE:
1794 * We have no dependancies and can flush immediately. Some
1795 * our children may not be flushable so we have to re-test
1796 * with that additional knowledge.
1798 hammer_flush_inode_core(ip, flg, flags);
1800 case HAMMER_FST_SETUP:
1802 * Recurse upwards through dependancies via target_list
1803 * and start their flusher actions going if possible.
1805 * 'good' is our connectivity. -1 means we have none and
1806 * can't flush, 0 means there weren't any dependancies, and
1807 * 1 means we have good connectivity.
1809 good = hammer_setup_parent_inodes(ip, 0, flg);
1813 * We can continue if good >= 0. Determine how
1814 * many records under our inode can be flushed (and
1817 hammer_flush_inode_core(ip, flg, flags);
1820 * Parent has no connectivity, tell it to flush
1821 * us as soon as it does.
1823 * The REFLUSH flag is also needed to trigger
1824 * dependancy wakeups.
1826 ip->flags |= HAMMER_INODE_CONN_DOWN |
1827 HAMMER_INODE_REFLUSH;
1828 if (flags & HAMMER_FLUSH_SIGNAL) {
1829 ip->flags |= HAMMER_INODE_RESIGNAL;
1830 hammer_flusher_async(ip->hmp, flg);
1834 case HAMMER_FST_FLUSH:
1836 * We are already flushing, flag the inode to reflush
1837 * if needed after it completes its current flush.
1839 * The REFLUSH flag is also needed to trigger
1840 * dependancy wakeups.
1842 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1843 ip->flags |= HAMMER_INODE_REFLUSH;
1844 if (flags & HAMMER_FLUSH_SIGNAL) {
1845 ip->flags |= HAMMER_INODE_RESIGNAL;
1846 hammer_flusher_async(ip->hmp, flg);
1853 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1854 * ip which reference our ip.
1856 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1857 * so for now do not ref/deref the structures. Note that if we use the
1858 * ref/rel code later, the rel CAN block.
1861 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1862 hammer_flush_group_t flg)
1864 hammer_record_t depend;
1869 * If we hit our recursion limit and we have parent dependencies
1870 * We cannot continue. Returning < 0 will cause us to be flagged
1871 * for reflush. Returning -2 cuts off additional dependency checks
1872 * because they are likely to also hit the depth limit.
1874 * We cannot return < 0 if there are no dependencies or there might
1875 * not be anything to wakeup (ip).
1877 if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1878 if (hammer_debug_general & 0x10000)
1879 krateprintf(&hammer_gen_krate,
1880 "HAMMER Warning: depth limit reached on "
1881 "setup recursion, inode %p %016llx\n",
1882 ip, (long long)ip->obj_id);
1890 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1891 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1892 KKASSERT(depend->target_ip == ip);
1893 if (r < 0 && good == 0)
1899 * If we failed due to the recursion depth limit then stop
1909 * This helper function takes a record representing the dependancy between
1910 * the parent inode and child inode.
1912 * record->ip = parent inode
1913 * record->target_ip = child inode
1915 * We are asked to recurse upwards and convert the record from SETUP
1916 * to FLUSH if possible.
1918 * Return 1 if the record gives us connectivity
1920 * Return 0 if the record is not relevant
1922 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1925 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1926 hammer_flush_group_t flg)
1931 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1935 * If the record is already flushing, is it in our flush group?
1937 * If it is in our flush group but it is a general record or a
1938 * delete-on-disk, it does not improve our connectivity (return 0),
1939 * and if the target inode is not trying to destroy itself we can't
1940 * allow the operation yet anyway (the second return -1).
1942 if (record->flush_state == HAMMER_FST_FLUSH) {
1944 * If not in our flush group ask the parent to reflush
1945 * us as soon as possible.
1947 if (record->flush_group != flg) {
1948 pip->flags |= HAMMER_INODE_REFLUSH;
1949 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1954 * If in our flush group everything is already set up,
1955 * just return whether the record will improve our
1956 * visibility or not.
1958 if (record->type == HAMMER_MEM_RECORD_ADD)
1964 * It must be a setup record. Try to resolve the setup dependancies
1965 * by recursing upwards so we can place ip on the flush list.
1967 * Limit ourselves to 20 levels of recursion to avoid blowing out
1968 * the kernel stack. If we hit the recursion limit we can't flush
1969 * until the parent flushes. The parent will flush independantly
1970 * on its own and ultimately a deep recursion will be resolved.
1972 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1974 good = hammer_setup_parent_inodes(pip, depth + 1, flg);
1977 * If good < 0 the parent has no connectivity and we cannot safely
1978 * flush the directory entry, which also means we can't flush our
1979 * ip. Flag us for downward recursion once the parent's
1980 * connectivity is resolved. Flag the parent for [re]flush or it
1981 * may not check for downward recursions.
1984 pip->flags |= HAMMER_INODE_REFLUSH;
1985 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1990 * We are go, place the parent inode in a flushing state so we can
1991 * place its record in a flushing state. Note that the parent
1992 * may already be flushing. The record must be in the same flush
1993 * group as the parent.
1995 if (pip->flush_state != HAMMER_FST_FLUSH)
1996 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
1997 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
2000 * It is possible for a rename to create a loop in the recursion
2001 * and revisit a record. This will result in the record being
2002 * placed in a flush state unexpectedly. This check deals with
2005 if (record->flush_state == HAMMER_FST_FLUSH) {
2006 if (record->type == HAMMER_MEM_RECORD_ADD)
2011 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
2014 if (record->type == HAMMER_MEM_RECORD_DEL &&
2015 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
2017 * Regardless of flushing state we cannot sync this path if the
2018 * record represents a delete-on-disk but the target inode
2019 * is not ready to sync its own deletion.
2021 * XXX need to count effective nlinks to determine whether
2022 * the flush is ok, otherwise removing a hardlink will
2023 * just leave the DEL record to rot.
2025 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
2029 if (pip->flush_group == flg) {
2031 * Because we have not calculated nlinks yet we can just
2032 * set records to the flush state if the parent is in
2033 * the same flush group as we are.
2035 record->flush_state = HAMMER_FST_FLUSH;
2036 record->flush_group = flg;
2037 ++record->flush_group->refs;
2038 hammer_ref(&record->lock);
2041 * A general directory-add contributes to our visibility.
2043 * Otherwise it is probably a directory-delete or
2044 * delete-on-disk record and does not contribute to our
2045 * visbility (but we can still flush it).
2047 if (record->type == HAMMER_MEM_RECORD_ADD)
2052 * If the parent is not in our flush group we cannot
2053 * flush this record yet, there is no visibility.
2054 * We tell the parent to reflush and mark ourselves
2055 * so the parent knows it should flush us too.
2057 pip->flags |= HAMMER_INODE_REFLUSH;
2058 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
2064 * This is the core routine placing an inode into the FST_FLUSH state.
2067 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
2069 hammer_mount_t hmp = ip->hmp;
2073 * Set flush state and prevent the flusher from cycling into
2074 * the next flush group. Do not place the ip on the list yet.
2075 * Inodes not in the idle state get an extra reference.
2077 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
2078 if (ip->flush_state == HAMMER_FST_IDLE)
2079 hammer_ref(&ip->lock);
2080 ip->flush_state = HAMMER_FST_FLUSH;
2081 ip->flush_group = flg;
2082 ++hmp->flusher.group_lock;
2083 ++hmp->count_iqueued;
2084 ++hammer_count_iqueued;
2086 hammer_redo_fifo_start_flush(ip);
2090 * We need to be able to vfsync/truncate from the backend.
2092 * XXX Any truncation from the backend will acquire the vnode
2095 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
2096 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
2097 ip->flags |= HAMMER_INODE_VHELD;
2103 * Figure out how many in-memory records we can actually flush
2104 * (not including inode meta-data, buffers, etc).
2106 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
2107 if (flags & HAMMER_FLUSH_RECURSION) {
2109 * If this is a upwards recursion we do not want to
2110 * recurse down again!
2114 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2116 * No new records are added if we must complete a flush
2117 * from a previous cycle, but we do have to move the records
2118 * from the previous cycle to the current one.
2121 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2122 hammer_syncgrp_child_callback, NULL);
2128 * Normal flush, scan records and bring them into the flush.
2129 * Directory adds and deletes are usually skipped (they are
2130 * grouped with the related inode rather then with the
2133 * go_count can be negative, which means the scan aborted
2134 * due to the flush group being over-full and we should
2135 * flush what we have.
2137 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2138 hammer_setup_child_callback, NULL);
2142 * This is a more involved test that includes go_count. If we
2143 * can't flush, flag the inode and return. If go_count is 0 we
2144 * were are unable to flush any records in our rec_tree and
2145 * must ignore the XDIRTY flag.
2147 if (go_count == 0) {
2148 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
2149 --hmp->count_iqueued;
2150 --hammer_count_iqueued;
2153 ip->flush_state = HAMMER_FST_SETUP;
2154 ip->flush_group = NULL;
2155 if (flags & HAMMER_FLUSH_SIGNAL) {
2156 ip->flags |= HAMMER_INODE_REFLUSH |
2157 HAMMER_INODE_RESIGNAL;
2159 ip->flags |= HAMMER_INODE_REFLUSH;
2162 if (ip->flags & HAMMER_INODE_VHELD) {
2163 ip->flags &= ~HAMMER_INODE_VHELD;
2169 * REFLUSH is needed to trigger dependancy wakeups
2170 * when an inode is in SETUP.
2172 ip->flags |= HAMMER_INODE_REFLUSH;
2173 if (--hmp->flusher.group_lock == 0)
2174 wakeup(&hmp->flusher.group_lock);
2180 * Snapshot the state of the inode for the backend flusher.
2182 * We continue to retain save_trunc_off even when all truncations
2183 * have been resolved as an optimization to determine if we can
2184 * skip the B-Tree lookup for overwrite deletions.
2186 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
2187 * and stays in ip->flags. Once set, it stays set until the
2188 * inode is destroyed.
2190 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2191 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
2192 ip->sync_trunc_off = ip->trunc_off;
2193 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
2194 ip->flags &= ~HAMMER_INODE_TRUNCATED;
2195 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
2198 * The save_trunc_off used to cache whether the B-Tree
2199 * holds any records past that point is not used until
2200 * after the truncation has succeeded, so we can safely
2203 if (ip->save_trunc_off > ip->sync_trunc_off)
2204 ip->save_trunc_off = ip->sync_trunc_off;
2206 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
2207 ~HAMMER_INODE_TRUNCATED);
2208 ip->sync_ino_leaf = ip->ino_leaf;
2209 ip->sync_ino_data = ip->ino_data;
2210 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
2211 #ifdef DEBUG_TRUNCATE
2212 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
2213 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
2217 * The flusher list inherits our inode and reference.
2219 KKASSERT(flg->running == 0);
2220 RB_INSERT(hammer_fls_rb_tree, &flg->flush_tree, ip);
2221 if (--hmp->flusher.group_lock == 0)
2222 wakeup(&hmp->flusher.group_lock);
2225 * Auto-flush the group if it grows too large. Make sure the
2226 * inode reclaim wait pipeline continues to work.
2228 if (flg->total_count >= hammer_autoflush ||
2229 flg->total_count >= hammer_limit_reclaims / 4) {
2230 if (hmp->fill_flush_group == flg)
2231 hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
2232 hammer_flusher_async(hmp, flg);
2237 * Callback for scan of ip->rec_tree. Try to include each record in our
2238 * flush. ip->flush_group has been set but the inode has not yet been
2239 * moved into a flushing state.
2241 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
2244 * We return 1 for any record placed or found in FST_FLUSH, which prevents
2245 * the caller from shortcutting the flush.
2248 hammer_setup_child_callback(hammer_record_t rec, void *data)
2250 hammer_flush_group_t flg;
2251 hammer_inode_t target_ip;
2256 * Records deleted or committed by the backend are ignored.
2257 * Note that the flush detects deleted frontend records at
2258 * multiple points to deal with races. This is just the first
2259 * line of defense. The only time HAMMER_RECF_DELETED_FE cannot
2260 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2261 * messes up link-count calculations.
2263 * NOTE: Don't get confused between record deletion and, say,
2264 * directory entry deletion. The deletion of a directory entry
2265 * which is on-media has nothing to do with the record deletion
2268 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2269 HAMMER_RECF_COMMITTED)) {
2270 if (rec->flush_state == HAMMER_FST_FLUSH) {
2271 KKASSERT(rec->flush_group == rec->ip->flush_group);
2280 * If the record is in an idle state it has no dependancies and
2284 flg = ip->flush_group;
2287 switch(rec->flush_state) {
2288 case HAMMER_FST_IDLE:
2290 * The record has no setup dependancy, we can flush it.
2292 KKASSERT(rec->target_ip == NULL);
2293 rec->flush_state = HAMMER_FST_FLUSH;
2294 rec->flush_group = flg;
2296 hammer_ref(&rec->lock);
2299 case HAMMER_FST_SETUP:
2301 * The record has a setup dependancy. These are typically
2302 * directory entry adds and deletes. Such entries will be
2303 * flushed when their inodes are flushed so we do not
2304 * usually have to add them to the flush here. However,
2305 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2306 * it is asking us to flush this record (and it).
2308 target_ip = rec->target_ip;
2309 KKASSERT(target_ip != NULL);
2310 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2313 * If the target IP is already flushing in our group
2314 * we could associate the record, but target_ip has
2315 * already synced ino_data to sync_ino_data and we
2316 * would also have to adjust nlinks. Plus there are
2317 * ordering issues for adds and deletes.
2319 * Reflush downward if this is an ADD, and upward if
2322 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2323 if (rec->type == HAMMER_MEM_RECORD_ADD)
2324 ip->flags |= HAMMER_INODE_REFLUSH;
2326 target_ip->flags |= HAMMER_INODE_REFLUSH;
2331 * Target IP is not yet flushing. This can get complex
2332 * because we have to be careful about the recursion.
2334 * Directories create an issue for us in that if a flush
2335 * of a directory is requested the expectation is to flush
2336 * any pending directory entries, but this will cause the
2337 * related inodes to recursively flush as well. We can't
2338 * really defer the operation so just get as many as we
2342 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2343 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2345 * We aren't reclaiming and the target ip was not
2346 * previously prevented from flushing due to this
2347 * record dependancy. Do not flush this record.
2352 if (flg->total_count + flg->refs >
2353 ip->hmp->undo_rec_limit) {
2355 * Our flush group is over-full and we risk blowing
2356 * out the UNDO FIFO. Stop the scan, flush what we
2357 * have, then reflush the directory.
2359 * The directory may be forced through multiple
2360 * flush groups before it can be completely
2363 ip->flags |= HAMMER_INODE_RESIGNAL |
2364 HAMMER_INODE_REFLUSH;
2366 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2368 * If the target IP is not flushing we can force
2369 * it to flush, even if it is unable to write out
2370 * any of its own records we have at least one in
2371 * hand that we CAN deal with.
2373 rec->flush_state = HAMMER_FST_FLUSH;
2374 rec->flush_group = flg;
2376 hammer_ref(&rec->lock);
2377 hammer_flush_inode_core(target_ip, flg,
2378 HAMMER_FLUSH_RECURSION);
2382 * General or delete-on-disk record.
2384 * XXX this needs help. If a delete-on-disk we could
2385 * disconnect the target. If the target has its own
2386 * dependancies they really need to be flushed.
2390 rec->flush_state = HAMMER_FST_FLUSH;
2391 rec->flush_group = flg;
2393 hammer_ref(&rec->lock);
2394 hammer_flush_inode_core(target_ip, flg,
2395 HAMMER_FLUSH_RECURSION);
2399 case HAMMER_FST_FLUSH:
2401 * The record could be part of a previous flush group if the
2402 * inode is a directory (the record being a directory entry).
2403 * Once the flush group was closed a hammer_test_inode()
2404 * function can cause a new flush group to be setup, placing
2405 * the directory inode itself in a new flush group.
2407 * When associated with a previous flush group we count it
2408 * as if it were in our current flush group, since it will
2409 * effectively be flushed by the time we flush our current
2413 rec->ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY ||
2414 rec->flush_group == flg);
2423 * This version just moves records already in a flush state to the new
2424 * flush group and that is it.
2427 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2429 hammer_inode_t ip = rec->ip;
2431 switch(rec->flush_state) {
2432 case HAMMER_FST_FLUSH:
2433 KKASSERT(rec->flush_group == ip->flush_group);
2443 * Wait for a previously queued flush to complete.
2445 * If a critical error occured we don't try to wait.
2448 hammer_wait_inode(hammer_inode_t ip)
2451 * The inode can be in a SETUP state in which case RESIGNAL
2452 * should be set. If RESIGNAL is not set then the previous
2453 * flush completed and a later operation placed the inode
2454 * in a passive setup state again, so we're done.
2456 * The inode can be in a FLUSH state in which case we
2457 * can just wait for completion.
2459 while (ip->flush_state == HAMMER_FST_FLUSH ||
2460 (ip->flush_state == HAMMER_FST_SETUP &&
2461 (ip->flags & HAMMER_INODE_RESIGNAL))) {
2463 * Don't try to flush on a critical error
2465 if (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
2469 * If the inode was already being flushed its flg
2470 * may not have been queued to the backend. We
2471 * have to make sure it gets queued or we can wind
2472 * up blocked or deadlocked (particularly if we are
2473 * the vnlru thread).
2475 if (ip->flush_state == HAMMER_FST_FLUSH) {
2476 KKASSERT(ip->flush_group);
2477 if (ip->flush_group->closed == 0) {
2478 if (hammer_debug_inode) {
2479 kprintf("hammer: debug: forcing "
2480 "async flush ip %016jx\n",
2481 (intmax_t)ip->obj_id);
2483 hammer_flusher_async(ip->hmp,
2485 continue; /* retest */
2490 * In a flush state with the flg queued to the backend
2491 * or in a setup state with RESIGNAL set, we can safely
2494 ip->flags |= HAMMER_INODE_FLUSHW;
2495 tsleep(&ip->flags, 0, "hmrwin", 0);
2500 * The inode may have been in a passive setup state,
2501 * call flush to make sure we get signaled.
2503 if (ip->flush_state == HAMMER_FST_SETUP)
2504 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2510 * Called by the backend code when a flush has been completed.
2511 * The inode has already been removed from the flush list.
2513 * A pipelined flush can occur, in which case we must re-enter the
2514 * inode on the list and re-copy its fields.
2517 hammer_flush_inode_done(hammer_inode_t ip, int error)
2522 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2527 * Auto-reflush if the backend could not completely flush
2528 * the inode. This fixes a case where a deferred buffer flush
2529 * could cause fsync to return early.
2531 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2532 ip->flags |= HAMMER_INODE_REFLUSH;
2535 * Merge left-over flags back into the frontend and fix the state.
2536 * Incomplete truncations are retained by the backend.
2539 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2540 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2543 * The backend may have adjusted nlinks, so if the adjusted nlinks
2544 * does not match the fronttend set the frontend's DDIRTY flag again.
2546 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2547 ip->flags |= HAMMER_INODE_DDIRTY;
2550 * Fix up the dirty buffer status.
2552 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2553 ip->flags |= HAMMER_INODE_BUFS;
2555 hammer_redo_fifo_end_flush(ip);
2558 * Re-set the XDIRTY flag if some of the inode's in-memory records
2559 * could not be flushed.
2561 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2562 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2563 (!RB_EMPTY(&ip->rec_tree) &&
2564 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2567 * Do not lose track of inodes which no longer have vnode
2568 * assocations, otherwise they may never get flushed again.
2570 * The reflush flag can be set superfluously, causing extra pain
2571 * for no reason. If the inode is no longer modified it no longer
2572 * needs to be flushed.
2574 if (ip->flags & HAMMER_INODE_MODMASK) {
2576 ip->flags |= HAMMER_INODE_REFLUSH;
2578 ip->flags &= ~HAMMER_INODE_REFLUSH;
2582 * The fs token is held but the inode lock is not held. Because this
2583 * is a backend flush it is possible that the vnode has no references
2584 * and cause a reclaim race inside vsetisdirty() if/when it blocks.
2586 * Therefore, we must lock the inode around this particular dirtying
2587 * operation. We don't have to around other dirtying operations
2588 * where the vnode is implicitly or explicitly held.
2590 if (ip->flags & HAMMER_INODE_MODMASK) {
2591 hammer_lock_ex(&ip->lock);
2592 hammer_inode_dirty(ip);
2593 hammer_unlock(&ip->lock);
2597 * Adjust the flush state.
2599 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2601 * We were unable to flush out all our records, leave the
2602 * inode in a flush state and in the current flush group.
2603 * The flush group will be re-run.
2605 * This occurs if the UNDO block gets too full or there is
2606 * too much dirty meta-data and allows the flusher to
2607 * finalize the UNDO block and then re-flush.
2609 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2613 * Remove from the flush_group
2615 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
2616 ip->flush_group = NULL;
2620 * Clean up the vnode ref and tracking counts.
2622 if (ip->flags & HAMMER_INODE_VHELD) {
2623 ip->flags &= ~HAMMER_INODE_VHELD;
2627 --hmp->count_iqueued;
2628 --hammer_count_iqueued;
2631 * And adjust the state.
2633 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2634 ip->flush_state = HAMMER_FST_IDLE;
2637 ip->flush_state = HAMMER_FST_SETUP;
2642 * If the frontend is waiting for a flush to complete,
2645 if (ip->flags & HAMMER_INODE_FLUSHW) {
2646 ip->flags &= ~HAMMER_INODE_FLUSHW;
2651 * If the frontend made more changes and requested another
2652 * flush, then try to get it running.
2654 * Reflushes are aborted when the inode is errored out.
2656 if (ip->flags & HAMMER_INODE_REFLUSH) {
2657 ip->flags &= ~HAMMER_INODE_REFLUSH;
2658 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2659 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2660 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2662 hammer_flush_inode(ip, 0);
2668 * If we have no parent dependancies we can clear CONN_DOWN
2670 if (TAILQ_EMPTY(&ip->target_list))
2671 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2674 * If the inode is now clean drop the space reservation.
2676 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2677 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2678 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2682 ip->flags &= ~HAMMER_INODE_SLAVEFLUSH;
2685 hammer_rel_inode(ip, 0);
2689 * Called from hammer_sync_inode() to synchronize in-memory records
2693 hammer_sync_record_callback(hammer_record_t record, void *data)
2695 hammer_cursor_t cursor = data;
2696 hammer_transaction_t trans = cursor->trans;
2697 hammer_mount_t hmp = trans->hmp;
2701 * Skip records that do not belong to the current flush.
2703 ++hammer_stats_record_iterations;
2704 if (record->flush_state != HAMMER_FST_FLUSH)
2708 if (record->flush_group != record->ip->flush_group) {
2709 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
2710 if (hammer_debug_critical)
2715 KKASSERT(record->flush_group == record->ip->flush_group);
2718 * Interlock the record using the BE flag. Once BE is set the
2719 * frontend cannot change the state of FE.
2721 * NOTE: If FE is set prior to us setting BE we still sync the
2722 * record out, but the flush completion code converts it to
2723 * a delete-on-disk record instead of destroying it.
2725 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2726 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2729 * The backend has already disposed of the record.
2731 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2737 * If the whole inode is being deleted and all on-disk records will
2738 * be deleted very soon, we can't sync any new records to disk
2739 * because they will be deleted in the same transaction they were
2740 * created in (delete_tid == create_tid), which will assert.
2742 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2743 * that we currently panic on.
2745 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2746 switch(record->type) {
2747 case HAMMER_MEM_RECORD_DATA:
2749 * We don't have to do anything, if the record was
2750 * committed the space will have been accounted for
2754 case HAMMER_MEM_RECORD_GENERAL:
2756 * Set deleted-by-backend flag. Do not set the
2757 * backend committed flag, because we are throwing
2760 record->flags |= HAMMER_RECF_DELETED_BE;
2761 ++record->ip->rec_generation;
2764 case HAMMER_MEM_RECORD_ADD:
2765 panic("hammer_sync_record_callback: illegal add "
2766 "during inode deletion record %p", record);
2767 break; /* NOT REACHED */
2768 case HAMMER_MEM_RECORD_INODE:
2769 panic("hammer_sync_record_callback: attempt to "
2770 "sync inode record %p?", record);
2771 break; /* NOT REACHED */
2772 case HAMMER_MEM_RECORD_DEL:
2774 * Follow through and issue the on-disk deletion
2781 * If DELETED_FE is set special handling is needed for directory
2782 * entries. Dependant pieces related to the directory entry may
2783 * have already been synced to disk. If this occurs we have to
2784 * sync the directory entry and then change the in-memory record
2785 * from an ADD to a DELETE to cover the fact that it's been
2786 * deleted by the frontend.
2788 * A directory delete covering record (MEM_RECORD_DEL) can never
2789 * be deleted by the frontend.
2791 * Any other record type (aka DATA) can be deleted by the frontend.
2792 * XXX At the moment the flusher must skip it because there may
2793 * be another data record in the flush group for the same block,
2794 * meaning that some frontend data changes can leak into the backend's
2795 * synchronization point.
2797 if (record->flags & HAMMER_RECF_DELETED_FE) {
2798 if (record->type == HAMMER_MEM_RECORD_ADD) {
2800 * Convert a front-end deleted directory-add to
2801 * a directory-delete entry later.
2803 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2806 * Dispose of the record (race case). Mark as
2807 * deleted by backend (and not committed).
2809 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2810 record->flags |= HAMMER_RECF_DELETED_BE;
2811 ++record->ip->rec_generation;
2818 * Assign the create_tid for new records. Deletions already
2819 * have the record's entire key properly set up.
2821 if (record->type != HAMMER_MEM_RECORD_DEL) {
2822 record->leaf.base.create_tid = trans->tid;
2823 record->leaf.create_ts = trans->time32;
2827 * This actually moves the record to the on-media B-Tree. We
2828 * must also generate REDO_TERM entries in the UNDO/REDO FIFO
2829 * indicating that the related REDO_WRITE(s) have been committed.
2831 * During recovery any REDO_TERM's within the nominal recovery span
2832 * are ignored since the related meta-data is being undone, causing
2833 * any matching REDO_WRITEs to execute. The REDO_TERMs outside
2834 * the nominal recovery span will match against REDO_WRITEs and
2835 * prevent them from being executed (because the meta-data has
2836 * already been synchronized).
2838 if (record->flags & HAMMER_RECF_REDO) {
2839 KKASSERT(record->type == HAMMER_MEM_RECORD_DATA);
2840 hammer_generate_redo(trans, record->ip,
2841 record->leaf.base.key -
2842 record->leaf.data_len,
2843 HAMMER_REDO_TERM_WRITE,
2845 record->leaf.data_len);
2849 error = hammer_ip_sync_record_cursor(cursor, record);
2850 if (error != EDEADLK)
2852 hammer_done_cursor(cursor);
2853 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2858 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2863 hammer_flush_record_done(record, error);
2866 * Do partial finalization if we have built up too many dirty
2867 * buffers. Otherwise a buffer cache deadlock can occur when
2868 * doing things like creating tens of thousands of tiny files.
2870 * We must release our cursor lock to avoid a 3-way deadlock
2871 * due to the exclusive sync lock the finalizer must get.
2873 * WARNING: See warnings in hammer_unlock_cursor() function.
2875 if (hammer_flusher_meta_limit(hmp) ||
2876 vm_page_count_severe()) {
2877 hammer_unlock_cursor(cursor);
2878 hammer_flusher_finalize(trans, 0);
2879 hammer_lock_cursor(cursor);
2885 * Backend function called by the flusher to sync an inode to media.
2888 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2890 struct hammer_cursor cursor;
2891 hammer_node_t tmp_node;
2892 hammer_record_t depend;
2893 hammer_record_t next;
2894 int error, tmp_error;
2897 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2900 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2905 * Any directory records referencing this inode which are not in
2906 * our current flush group must adjust our nlink count for the
2907 * purposes of synchronizating to disk.
2909 * Records which are in our flush group can be unlinked from our
2910 * inode now, potentially allowing the inode to be physically
2913 * This cannot block.
2915 nlinks = ip->ino_data.nlinks;
2916 next = TAILQ_FIRST(&ip->target_list);
2917 while ((depend = next) != NULL) {
2918 next = TAILQ_NEXT(depend, target_entry);
2919 if (depend->flush_state == HAMMER_FST_FLUSH &&
2920 depend->flush_group == ip->flush_group) {
2922 * If this is an ADD that was deleted by the frontend
2923 * the frontend nlinks count will have already been
2924 * decremented, but the backend is going to sync its
2925 * directory entry and must account for it. The
2926 * record will be converted to a delete-on-disk when
2929 * If the ADD was not deleted by the frontend we
2930 * can remove the dependancy from our target_list.
2932 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2935 TAILQ_REMOVE(&ip->target_list, depend,
2937 depend->target_ip = NULL;
2939 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2941 * Not part of our flush group and not deleted by
2942 * the front-end, adjust the link count synced to
2943 * the media (undo what the frontend did when it
2944 * queued the record).
2946 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2947 switch(depend->type) {
2948 case HAMMER_MEM_RECORD_ADD:
2951 case HAMMER_MEM_RECORD_DEL:
2961 * Set dirty if we had to modify the link count.
2963 if (ip->sync_ino_data.nlinks != nlinks) {
2964 KKASSERT((int64_t)nlinks >= 0);
2965 ip->sync_ino_data.nlinks = nlinks;
2966 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2970 * If there is a trunction queued destroy any data past the (aligned)
2971 * truncation point. Userland will have dealt with the buffer
2972 * containing the truncation point for us.
2974 * We don't flush pending frontend data buffers until after we've
2975 * dealt with the truncation.
2977 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2979 * Interlock trunc_off. The VOP front-end may continue to
2980 * make adjustments to it while we are blocked.
2983 off_t aligned_trunc_off;
2986 trunc_off = ip->sync_trunc_off;
2987 blkmask = hammer_blocksize(trunc_off) - 1;
2988 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2991 * Delete any whole blocks on-media. The front-end has
2992 * already cleaned out any partial block and made it
2993 * pending. The front-end may have updated trunc_off
2994 * while we were blocked so we only use sync_trunc_off.
2996 * This operation can blow out the buffer cache, EWOULDBLOCK
2997 * means we were unable to complete the deletion. The
2998 * deletion will update sync_trunc_off in that case.
3000 error = hammer_ip_delete_range(&cursor, ip,
3002 0x7FFFFFFFFFFFFFFFLL, 2);
3003 if (error == EWOULDBLOCK) {
3004 ip->flags |= HAMMER_INODE_WOULDBLOCK;
3006 goto defer_buffer_flush;
3013 * Generate a REDO_TERM_TRUNC entry in the UNDO/REDO FIFO.
3015 * XXX we do this even if we did not previously generate
3016 * a REDO_TRUNC record. This operation may enclosed the
3017 * range for multiple prior truncation entries in the REDO
3020 if (trans->hmp->version >= HAMMER_VOL_VERSION_FOUR &&
3021 (ip->flags & HAMMER_INODE_RDIRTY)) {
3022 hammer_generate_redo(trans, ip, aligned_trunc_off,
3023 HAMMER_REDO_TERM_TRUNC,
3028 * Clear the truncation flag on the backend after we have
3029 * completed the deletions. Backend data is now good again
3030 * (including new records we are about to sync, below).
3032 * Leave sync_trunc_off intact. As we write additional
3033 * records the backend will update sync_trunc_off. This
3034 * tells the backend whether it can skip the overwrite
3035 * test. This should work properly even when the backend
3036 * writes full blocks where the truncation point straddles
3037 * the block because the comparison is against the base
3038 * offset of the record.
3040 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
3041 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
3047 * Now sync related records. These will typically be directory
3048 * entries, records tracking direct-writes, or delete-on-disk records.
3051 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
3052 hammer_sync_record_callback, &cursor);
3058 hammer_cache_node(&ip->cache[1], cursor.node);
3061 * Re-seek for inode update, assuming our cache hasn't been ripped
3062 * out from under us.
3065 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
3067 hammer_cursor_downgrade(&cursor);
3068 hammer_lock_sh(&tmp_node->lock);
3069 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
3070 hammer_cursor_seek(&cursor, tmp_node, 0);
3071 hammer_unlock(&tmp_node->lock);
3072 hammer_rel_node(tmp_node);
3078 * If we are deleting the inode the frontend had better not have
3079 * any active references on elements making up the inode.
3081 * The call to hammer_ip_delete_clean() cleans up auxillary records
3082 * but not DB or DATA records. Those must have already been deleted
3083 * by the normal truncation mechanic.
3085 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
3086 RB_EMPTY(&ip->rec_tree) &&
3087 (ip->sync_flags & HAMMER_INODE_DELETING) &&
3088 (ip->flags & HAMMER_INODE_DELETED) == 0) {
3091 error = hammer_ip_delete_clean(&cursor, ip, &count1);
3093 ip->flags |= HAMMER_INODE_DELETED;
3094 ip->sync_flags &= ~HAMMER_INODE_DELETING;
3095 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
3096 KKASSERT(RB_EMPTY(&ip->rec_tree));
3099 * Set delete_tid in both the frontend and backend
3100 * copy of the inode record. The DELETED flag handles
3101 * this, do not set DDIRTY.
3103 ip->ino_leaf.base.delete_tid = trans->tid;
3104 ip->sync_ino_leaf.base.delete_tid = trans->tid;
3105 ip->ino_leaf.delete_ts = trans->time32;
3106 ip->sync_ino_leaf.delete_ts = trans->time32;
3110 * Adjust the inode count in the volume header
3112 hammer_sync_lock_sh(trans);
3113 if (ip->flags & HAMMER_INODE_ONDISK) {
3114 hammer_modify_volume_field(trans,
3117 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
3118 hammer_modify_volume_done(trans->rootvol);
3120 hammer_sync_unlock(trans);
3126 ip->sync_flags &= ~HAMMER_INODE_BUFS;
3130 * Now update the inode's on-disk inode-data and/or on-disk record.
3131 * DELETED and ONDISK are managed only in ip->flags.
3133 * In the case of a defered buffer flush we still update the on-disk
3134 * inode to satisfy visibility requirements if there happen to be
3135 * directory dependancies.
3137 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
3138 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
3140 * If deleted and on-disk, don't set any additional flags.
3141 * the delete flag takes care of things.
3143 * Clear flags which may have been set by the frontend.
3145 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
3146 HAMMER_INODE_SDIRTY |
3147 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
3148 HAMMER_INODE_DELETING);
3150 case HAMMER_INODE_DELETED:
3152 * Take care of the case where a deleted inode was never
3153 * flushed to the disk in the first place.
3155 * Clear flags which may have been set by the frontend.
3157 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
3158 HAMMER_INODE_SDIRTY |
3159 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
3160 HAMMER_INODE_DELETING);
3161 while (RB_ROOT(&ip->rec_tree)) {
3162 hammer_record_t record = RB_ROOT(&ip->rec_tree);
3163 hammer_ref(&record->lock);
3164 KKASSERT(hammer_oneref(&record->lock));
3165 record->flags |= HAMMER_RECF_DELETED_BE;
3166 ++record->ip->rec_generation;
3167 hammer_rel_mem_record(record);
3170 case HAMMER_INODE_ONDISK:
3172 * If already on-disk, do not set any additional flags.
3177 * If not on-disk and not deleted, set DDIRTY to force
3178 * an initial record to be written.
3180 * Also set the create_tid in both the frontend and backend
3181 * copy of the inode record.
3183 ip->ino_leaf.base.create_tid = trans->tid;
3184 ip->ino_leaf.create_ts = trans->time32;
3185 ip->sync_ino_leaf.base.create_tid = trans->tid;
3186 ip->sync_ino_leaf.create_ts = trans->time32;
3187 ip->sync_flags |= HAMMER_INODE_DDIRTY;
3192 * If DDIRTY or SDIRTY is set, write out a new record.
3193 * If the inode is already on-disk the old record is marked as
3196 * If DELETED is set hammer_update_inode() will delete the existing
3197 * record without writing out a new one.
3199 * If *ONLY* the ITIMES flag is set we can update the record in-place.
3201 if (ip->flags & HAMMER_INODE_DELETED) {
3202 error = hammer_update_inode(&cursor, ip);
3204 if (!(ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY)) &&
3205 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
3206 error = hammer_update_itimes(&cursor, ip);
3208 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY |
3209 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
3210 error = hammer_update_inode(&cursor, ip);
3213 if (ip->flags & HAMMER_INODE_MODMASK)
3214 hammer_inode_dirty(ip);
3216 hammer_critical_error(ip->hmp, ip, error,
3217 "while syncing inode");
3219 hammer_done_cursor(&cursor);
3224 * This routine is called when the OS is no longer actively referencing
3225 * the inode (but might still be keeping it cached), or when releasing
3226 * the last reference to an inode.
3228 * At this point if the inode's nlinks count is zero we want to destroy
3229 * it, which may mean destroying it on-media too.
3232 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
3237 * Set the DELETING flag when the link count drops to 0 and the
3238 * OS no longer has any opens on the inode.
3240 * The backend will clear DELETING (a mod flag) and set DELETED
3241 * (a state flag) when it is actually able to perform the
3244 * Don't reflag the deletion if the flusher is currently syncing
3245 * one that was already flagged. A previously set DELETING flag
3246 * may bounce around flags and sync_flags until the operation is
3249 * Do not attempt to modify a snapshot inode (one set to read-only).
3251 if (ip->ino_data.nlinks == 0 &&
3252 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_RO|HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
3253 ip->flags |= HAMMER_INODE_DELETING;
3254 ip->flags |= HAMMER_INODE_TRUNCATED;
3258 if (hammer_get_vnode(ip, &vp) != 0)
3266 nvtruncbuf(ip->vp, 0, HAMMER_BUFSIZE, 0, 0);
3267 if (ip->flags & HAMMER_INODE_MODMASK)
3268 hammer_inode_dirty(ip);
3275 * After potentially resolving a dependancy the inode is tested
3276 * to determine whether it needs to be reflushed.
3279 hammer_test_inode(hammer_inode_t ip)
3281 if (ip->flags & HAMMER_INODE_REFLUSH) {
3282 ip->flags &= ~HAMMER_INODE_REFLUSH;
3283 hammer_ref(&ip->lock);
3284 if (ip->flags & HAMMER_INODE_RESIGNAL) {
3285 ip->flags &= ~HAMMER_INODE_RESIGNAL;
3286 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
3288 hammer_flush_inode(ip, 0);
3290 hammer_rel_inode(ip, 0);
3295 * Clear the RECLAIM flag on an inode. This occurs when the inode is
3296 * reassociated with a vp or just before it gets freed.
3298 * Pipeline wakeups to threads blocked due to an excessive number of
3299 * detached inodes. This typically occurs when atime updates accumulate
3300 * while scanning a directory tree.
3303 hammer_inode_wakereclaims(hammer_inode_t ip)
3305 struct hammer_reclaim *reclaim;
3306 hammer_mount_t hmp = ip->hmp;
3308 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
3311 --hammer_count_reclaims;
3312 --hmp->count_reclaims;
3313 ip->flags &= ~HAMMER_INODE_RECLAIM;
3315 if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
3316 KKASSERT(reclaim->count > 0);
3317 if (--reclaim->count == 0) {
3318 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
3325 * Setup our reclaim pipeline. We only let so many detached (and dirty)
3326 * inodes build up before we start blocking. This routine is called
3327 * if a new inode is created or an inode is loaded from media.
3329 * When we block we don't care *which* inode has finished reclaiming,
3330 * as long as one does.
3332 * The reclaim pipeline is primarily governed by the auto-flush which is
3333 * 1/4 hammer_limit_reclaims. We don't want to block if the count is
3334 * less than 1/2 hammer_limit_reclaims. From 1/2 to full count is
3335 * dynamically governed.
3338 hammer_inode_waitreclaims(hammer_transaction_t trans)
3340 hammer_mount_t hmp = trans->hmp;
3341 struct hammer_reclaim reclaim;
3345 * Track inode load, delay if the number of reclaiming inodes is
3346 * between 2/4 and 4/4 hammer_limit_reclaims, depending.
3348 if (curthread->td_proc) {
3349 struct hammer_inostats *stats;
3351 stats = hammer_inode_inostats(hmp, curthread->td_proc->p_pid);
3354 if (stats->count > hammer_limit_reclaims / 2)
3355 stats->count = hammer_limit_reclaims / 2;
3356 lower_limit = hammer_limit_reclaims - stats->count;
3357 if (hammer_debug_general & 0x10000) {
3358 kprintf("pid %5d limit %d\n",
3359 (int)curthread->td_proc->p_pid, lower_limit);
3362 lower_limit = hammer_limit_reclaims * 3 / 4;
3364 if (hmp->count_reclaims >= lower_limit) {
3366 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
3367 tsleep(&reclaim, 0, "hmrrcm", hz);
3368 if (reclaim.count > 0)
3369 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
3374 * Keep track of reclaim statistics on a per-pid basis using a loose
3375 * 4-way set associative hash table. Collisions inherit the count of
3376 * the previous entry.
3378 * NOTE: We want to be careful here to limit the chain size. If the chain
3379 * size is too large a pid will spread its stats out over too many
3380 * entries under certain types of heavy filesystem activity and
3381 * wind up not delaying long enough.
3384 struct hammer_inostats *
3385 hammer_inode_inostats(hammer_mount_t hmp, pid_t pid)
3387 struct hammer_inostats *stats;
3390 static volatile int iterator; /* we don't care about MP races */
3393 * Chain up to 4 times to find our entry.
3395 for (chain = 0; chain < 4; ++chain) {
3396 stats = &hmp->inostats[(pid + chain) & HAMMER_INOSTATS_HMASK];
3397 if (stats->pid == pid)
3402 * Replace one of the four chaining entries with our new entry.
3405 stats = &hmp->inostats[(pid + (iterator++ & 3)) &
3406 HAMMER_INOSTATS_HMASK];
3413 if (stats->count && stats->ltick != ticks) {
3414 delta = ticks - stats->ltick;
3415 stats->ltick = ticks;
3416 if (delta <= 0 || delta > hz * 60)
3419 stats->count = stats->count * hz / (hz + delta);
3421 if (hammer_debug_general & 0x10000)
3422 kprintf("pid %5d stats %d\n", (int)pid, stats->count);
3429 * XXX not used, doesn't work very well due to the large batching nature
3432 * A larger then normal backlog of inodes is sitting in the flusher,
3433 * enforce a general slowdown to let it catch up. This routine is only
3434 * called on completion of a non-flusher-related transaction which
3435 * performed B-Tree node I/O.
3437 * It is possible for the flusher to stall in a continuous load.
3438 * blogbench -i1000 -o seems to do a good job generating this sort of load.
3439 * If the flusher is unable to catch up the inode count can bloat until
3440 * we run out of kvm.
3442 * This is a bit of a hack.
3445 hammer_inode_waithard(hammer_mount_t hmp)
3450 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
3451 if (hmp->count_reclaims < hammer_limit_reclaims / 2 &&
3452 hmp->count_iqueued < hmp->count_inodes / 20) {
3453 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
3457 if (hmp->count_reclaims < hammer_limit_reclaims ||
3458 hmp->count_iqueued < hmp->count_inodes / 10) {
3461 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3465 * Block for one flush cycle.
3467 hammer_flusher_wait_next(hmp);