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
35 #include <vm/vm_page2.h>
39 static int hammer_unload_inode(struct hammer_inode *ip);
40 static void hammer_free_inode(hammer_inode_t ip);
41 static void hammer_flush_inode_core(hammer_inode_t ip,
42 hammer_flush_group_t flg, int flags);
43 static int hammer_setup_child_callback(hammer_record_t rec, void *data);
45 static int hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
47 static int hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
48 hammer_flush_group_t flg);
49 static int hammer_setup_parent_inodes_helper(hammer_record_t record,
50 int depth, hammer_flush_group_t flg);
51 static void hammer_inode_wakereclaims(hammer_inode_t ip);
52 static struct hammer_inostats *hammer_inode_inostats(hammer_mount_t hmp,
54 static struct hammer_inode *__hammer_find_inode(hammer_transaction_t trans,
55 int64_t obj_id, hammer_tid_t asof,
56 uint32_t localization);
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 uint32_t localization = *(uint32_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, uint32_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, uint32_t localization,
420 int flags, int *errorp)
422 hammer_mount_t hmp = trans->hmp;
423 struct hammer_node_cache *cachep;
424 struct hammer_cursor cursor;
425 struct hammer_inode *ip;
429 * Determine if we already have an inode cached. If we do then
432 * If we find an inode with no vnode we have to mark the
433 * transaction such that hammer_inode_waitreclaims() is
434 * called later on to avoid building up an infinite number
435 * of inodes. Otherwise we can continue to * add new inodes
436 * faster then they can be disposed of, even with the tsleep
439 * If we find a dummy inode we return a failure so dounlink
440 * (which does another lookup) doesn't try to mess with the
441 * link count. hammer_vop_nresolve() uses hammer_get_dummy_inode()
442 * to ref dummy inodes.
446 ip = __hammer_find_inode(trans, obj_id, asof, localization);
448 if (ip->flags & HAMMER_INODE_DUMMY) {
452 hammer_ref(&ip->lock);
457 * Allocate a new inode structure and deal with races later.
459 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
460 ++hammer_count_inodes;
464 ip->obj_localization = localization;
466 ip->flags = flags & HAMMER_INODE_RO;
467 ip->cache[0].ip = ip;
468 ip->cache[1].ip = ip;
469 ip->cache[2].ip = ip;
470 ip->cache[3].ip = ip;
472 ip->flags |= HAMMER_INODE_RO;
473 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
474 0x7FFFFFFFFFFFFFFFLL;
475 RB_INIT(&ip->rec_tree);
476 TAILQ_INIT(&ip->target_list);
477 hammer_ref(&ip->lock);
480 * Locate the on-disk inode. If this is a PFS root we always
481 * access the current version of the root inode and (if it is not
482 * a master) always access information under it with a snapshot
485 * We cache recent inode lookups in this directory in dip->cache[2].
486 * If we can't find it we assume the inode we are looking for is
487 * close to the directory inode.
492 if (dip->cache[2].node)
493 cachep = &dip->cache[2];
495 cachep = &dip->cache[0];
497 hammer_init_cursor(trans, &cursor, cachep, NULL);
498 cursor.key_beg.localization = localization | HAMMER_LOCALIZE_INODE;
499 cursor.key_beg.obj_id = ip->obj_id;
500 cursor.key_beg.key = 0;
501 cursor.key_beg.create_tid = 0;
502 cursor.key_beg.delete_tid = 0;
503 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
504 cursor.key_beg.obj_type = 0;
507 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
510 *errorp = hammer_btree_lookup(&cursor);
511 if (*errorp == EDEADLK) {
512 hammer_done_cursor(&cursor);
517 * On success the B-Tree lookup will hold the appropriate
518 * buffer cache buffers and provide a pointer to the requested
519 * information. Copy the information to the in-memory inode
520 * and cache the B-Tree node to improve future operations.
523 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
524 ip->ino_data = cursor.data->inode;
527 * cache[0] tries to cache the location of the object inode.
528 * The assumption is that it is near the directory inode.
530 * cache[1] tries to cache the location of the object data.
531 * We might have something in the governing directory from
532 * scan optimizations (see the strategy code in
535 * We update dip->cache[2], if possible, with the location
536 * of the object inode for future directory shortcuts.
538 hammer_cache_node(&ip->cache[0], cursor.node);
540 if (dip->cache[3].node) {
541 hammer_cache_node(&ip->cache[1],
544 hammer_cache_node(&dip->cache[2], cursor.node);
548 * The file should not contain any data past the file size
549 * stored in the inode. Setting save_trunc_off to the
550 * file size instead of max reduces B-Tree lookup overheads
551 * on append by allowing the flusher to avoid checking for
554 ip->save_trunc_off = ip->ino_data.size;
557 * Locate and assign the pseudofs management structure to
560 if (dip && dip->obj_localization == ip->obj_localization) {
561 ip->pfsm = dip->pfsm;
562 hammer_ref(&ip->pfsm->lock);
564 ip->pfsm = hammer_load_pseudofs(trans,
565 ip->obj_localization,
567 *errorp = 0; /* ignore ENOENT */
572 * The inode is placed on the red-black tree and will be synced to
573 * the media when flushed or by the filesystem sync. If this races
574 * another instantiation/lookup the insertion will fail.
577 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
578 hammer_free_inode(ip);
579 hammer_done_cursor(&cursor);
582 ip->flags |= HAMMER_INODE_ONDISK;
584 if (ip->flags & HAMMER_INODE_RSV_INODES) {
585 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
589 hammer_free_inode(ip);
592 hammer_done_cursor(&cursor);
595 * NEWINODE is only set if the inode becomes dirty later,
596 * setting it here just leads to unnecessary stalls.
598 * trans->flags |= HAMMER_TRANSF_NEWINODE;
604 * Get a dummy inode to placemark a broken directory entry.
606 struct hammer_inode *
607 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
608 int64_t obj_id, hammer_tid_t asof, uint32_t localization,
609 int flags, int *errorp)
611 hammer_mount_t hmp = trans->hmp;
612 struct hammer_inode *ip;
615 * Determine if we already have an inode cached. If we do then
618 * If we find an inode with no vnode we have to mark the
619 * transaction such that hammer_inode_waitreclaims() is
620 * called later on to avoid building up an infinite number
621 * of inodes. Otherwise we can continue to * add new inodes
622 * faster then they can be disposed of, even with the tsleep
625 * If we find a non-fake inode we return an error. Only fake
626 * inodes can be returned by this routine.
630 ip = __hammer_find_inode(trans, obj_id, asof, localization);
632 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
636 hammer_ref(&ip->lock);
641 * Allocate a new inode structure and deal with races later.
643 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
644 ++hammer_count_inodes;
648 ip->obj_localization = localization;
650 ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
651 ip->cache[0].ip = ip;
652 ip->cache[1].ip = ip;
653 ip->cache[2].ip = ip;
654 ip->cache[3].ip = ip;
655 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
656 0x7FFFFFFFFFFFFFFFLL;
657 RB_INIT(&ip->rec_tree);
658 TAILQ_INIT(&ip->target_list);
659 hammer_ref(&ip->lock);
662 * Populate the dummy inode. Leave everything zero'd out.
664 * (ip->ino_leaf and ip->ino_data)
666 * Make the dummy inode a FIFO object which most copy programs
667 * will properly ignore.
669 ip->save_trunc_off = ip->ino_data.size;
670 ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
673 * Locate and assign the pseudofs management structure to
676 if (dip && dip->obj_localization == ip->obj_localization) {
677 ip->pfsm = dip->pfsm;
678 hammer_ref(&ip->pfsm->lock);
680 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
682 *errorp = 0; /* ignore ENOENT */
686 * The inode is placed on the red-black tree and will be synced to
687 * the media when flushed or by the filesystem sync. If this races
688 * another instantiation/lookup the insertion will fail.
690 * NOTE: Do not set HAMMER_INODE_ONDISK. The inode is a fake.
693 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
694 hammer_free_inode(ip);
698 if (ip->flags & HAMMER_INODE_RSV_INODES) {
699 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
702 hammer_free_inode(ip);
705 trans->flags |= HAMMER_TRANSF_NEWINODE;
710 * Return a referenced inode only if it is in our inode cache.
711 * Dummy inodes do not count.
713 struct hammer_inode *
714 hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
715 hammer_tid_t asof, uint32_t localization)
717 struct hammer_inode *ip;
719 ip = __hammer_find_inode(trans, obj_id, asof, localization);
721 if (ip->flags & HAMMER_INODE_DUMMY)
724 hammer_ref(&ip->lock);
730 * Return a referenced inode only if it is in our inode cache.
731 * This function does not reference inode.
733 static struct hammer_inode *
734 __hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
735 hammer_tid_t asof, uint32_t localization)
737 hammer_mount_t hmp = trans->hmp;
738 struct hammer_inode_info iinfo;
739 struct hammer_inode *ip;
741 iinfo.obj_id = obj_id;
742 iinfo.obj_asof = asof;
743 iinfo.obj_localization = localization;
745 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
751 * Create a new filesystem object, returning the inode in *ipp. The
752 * returned inode will be referenced. The inode is created in-memory.
754 * If pfsm is non-NULL the caller wishes to create the root inode for
758 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
760 hammer_inode_t dip, const char *name, int namelen,
761 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
773 * Disallow the creation of new inodes in directories which
774 * have been deleted. In HAMMER, this will cause a record
775 * syncing assertion later on in the flush code.
777 if (dip && dip->ino_data.nlinks == 0) {
785 ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
786 ++hammer_count_inodes;
788 trans->flags |= HAMMER_TRANSF_NEWINODE;
791 KKASSERT(pfsm->localization != 0);
792 ip->obj_id = HAMMER_OBJID_ROOT;
793 ip->obj_localization = pfsm->localization;
795 KKASSERT(dip != NULL);
796 namekey = hammer_directory_namekey(dip, name, namelen, &dummy);
797 ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
798 ip->obj_localization = dip->obj_localization;
801 KKASSERT(ip->obj_id != 0);
802 ip->obj_asof = hmp->asof;
804 ip->flush_state = HAMMER_FST_IDLE;
805 ip->flags = HAMMER_INODE_DDIRTY |
806 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
807 ip->cache[0].ip = ip;
808 ip->cache[1].ip = ip;
809 ip->cache[2].ip = ip;
810 ip->cache[3].ip = ip;
812 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
813 /* ip->save_trunc_off = 0; (already zero) */
814 RB_INIT(&ip->rec_tree);
815 TAILQ_INIT(&ip->target_list);
817 ip->ino_data.atime = trans->time;
818 ip->ino_data.mtime = trans->time;
819 ip->ino_data.size = 0;
820 ip->ino_data.nlinks = 0;
823 * A nohistory designator on the parent directory is inherited by
824 * the child. We will do this even for pseudo-fs creation... the
825 * sysad can turn it off.
828 ip->ino_data.uflags = dip->ino_data.uflags &
829 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
832 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
833 ip->ino_leaf.base.localization = ip->obj_localization |
834 HAMMER_LOCALIZE_INODE;
835 ip->ino_leaf.base.obj_id = ip->obj_id;
836 ip->ino_leaf.base.key = 0;
837 ip->ino_leaf.base.create_tid = 0;
838 ip->ino_leaf.base.delete_tid = 0;
839 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
840 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
842 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
843 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
844 ip->ino_data.mode = vap->va_mode;
845 ip->ino_data.ctime = trans->time;
848 * If we are running version 2 or greater directory entries are
849 * inode-localized instead of data-localized.
851 if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
852 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
853 ip->ino_data.cap_flags |=
854 HAMMER_INODE_CAP_DIR_LOCAL_INO;
857 if (trans->hmp->version >= HAMMER_VOL_VERSION_SIX) {
858 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
859 ip->ino_data.cap_flags |=
860 HAMMER_INODE_CAP_DIRHASH_ALG1;
865 * Setup the ".." pointer. This only needs to be done for directories
866 * but we do it for all objects as a recovery aid if dip exists.
867 * The inode is probably a PFS root if dip is NULL.
870 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
873 * The parent_obj_localization field only applies to pseudo-fs roots.
874 * XXX this is no longer applicable, PFSs are no longer directly
875 * tied into the parent's directory structure.
877 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
878 ip->obj_id == HAMMER_OBJID_ROOT) {
879 ip->ino_data.ext.obj.parent_obj_localization =
880 dip->obj_localization;
884 switch(ip->ino_leaf.base.obj_type) {
885 case HAMMER_OBJTYPE_CDEV:
886 case HAMMER_OBJTYPE_BDEV:
887 ip->ino_data.rmajor = vap->va_rmajor;
888 ip->ino_data.rminor = vap->va_rminor;
895 * Calculate default uid/gid and overwrite with information from
899 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
900 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
901 xuid, cred, &vap->va_mode);
905 ip->ino_data.mode = vap->va_mode;
907 if (vap->va_vaflags & VA_UID_UUID_VALID)
908 ip->ino_data.uid = vap->va_uid_uuid;
909 else if (vap->va_uid != (uid_t)VNOVAL)
910 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
912 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
914 if (vap->va_vaflags & VA_GID_UUID_VALID)
915 ip->ino_data.gid = vap->va_gid_uuid;
916 else if (vap->va_gid != (gid_t)VNOVAL)
917 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
919 ip->ino_data.gid = dip->ino_data.gid;
921 hammer_ref(&ip->lock);
925 hammer_ref(&pfsm->lock);
927 } else if (dip->obj_localization == ip->obj_localization) {
928 ip->pfsm = dip->pfsm;
929 hammer_ref(&ip->pfsm->lock);
932 ip->pfsm = hammer_load_pseudofs(trans,
933 ip->obj_localization,
935 error = 0; /* ignore ENOENT */
939 hammer_free_inode(ip);
941 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
942 hpanic("duplicate obj_id %llx", (long long)ip->obj_id);
944 hammer_free_inode(ip);
951 * Final cleanup / freeing of an inode structure
954 hammer_free_inode(hammer_inode_t ip)
956 struct hammer_mount *hmp;
959 KKASSERT(hammer_oneref(&ip->lock));
960 hammer_uncache_node(&ip->cache[0]);
961 hammer_uncache_node(&ip->cache[1]);
962 hammer_uncache_node(&ip->cache[2]);
963 hammer_uncache_node(&ip->cache[3]);
964 hammer_inode_wakereclaims(ip);
966 hammer_clear_objid(ip);
967 --hammer_count_inodes;
970 hammer_rel_pseudofs(hmp, ip->pfsm);
973 kfree(ip, hmp->m_inodes);
977 * Retrieve pseudo-fs data. NULL will never be returned.
979 * If an error occurs *errorp will be set and a default template is returned,
980 * otherwise *errorp is set to 0. Typically when an error occurs it will
983 hammer_pseudofs_inmem_t
984 hammer_load_pseudofs(hammer_transaction_t trans,
985 uint32_t localization, int *errorp)
987 hammer_mount_t hmp = trans->hmp;
989 hammer_pseudofs_inmem_t pfsm;
990 struct hammer_cursor cursor;
994 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
996 hammer_ref(&pfsm->lock);
1002 * PFS records are associated with the root inode (not the PFS root
1003 * inode, but the real root). Avoid an infinite recursion if loading
1004 * the PFS for the real root.
1007 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
1009 HAMMER_DEF_LOCALIZATION, 0, errorp);
1014 pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
1015 pfsm->localization = localization;
1016 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
1017 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
1019 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
1020 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION |
1021 HAMMER_LOCALIZE_MISC;
1022 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
1023 cursor.key_beg.create_tid = 0;
1024 cursor.key_beg.delete_tid = 0;
1025 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
1026 cursor.key_beg.obj_type = 0;
1027 cursor.key_beg.key = localization;
1028 cursor.asof = HAMMER_MAX_TID;
1029 cursor.flags |= HAMMER_CURSOR_ASOF;
1032 *errorp = hammer_ip_lookup(&cursor);
1034 *errorp = hammer_btree_lookup(&cursor);
1036 *errorp = hammer_ip_resolve_data(&cursor);
1038 if (cursor.data->pfsd.mirror_flags &
1039 HAMMER_PFSD_DELETED) {
1042 bytes = cursor.leaf->data_len;
1043 if (bytes > sizeof(pfsm->pfsd))
1044 bytes = sizeof(pfsm->pfsd);
1045 bcopy(cursor.data, &pfsm->pfsd, bytes);
1049 hammer_done_cursor(&cursor);
1051 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
1052 hammer_ref(&pfsm->lock);
1054 hammer_rel_inode(ip, 0);
1055 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
1056 kfree(pfsm, hmp->m_misc);
1063 * Store pseudo-fs data. The backend will automatically delete any prior
1064 * on-disk pseudo-fs data but we have to delete in-memory versions.
1067 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
1069 struct hammer_cursor cursor;
1070 hammer_record_t record;
1075 * PFS records are associated with the root inode (not the PFS root
1076 * inode, but the real root).
1078 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1079 HAMMER_DEF_LOCALIZATION, 0, &error);
1081 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
1082 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
1083 cursor.key_beg.localization = ip->obj_localization |
1084 HAMMER_LOCALIZE_MISC;
1085 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
1086 cursor.key_beg.create_tid = 0;
1087 cursor.key_beg.delete_tid = 0;
1088 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
1089 cursor.key_beg.obj_type = 0;
1090 cursor.key_beg.key = pfsm->localization;
1091 cursor.asof = HAMMER_MAX_TID;
1092 cursor.flags |= HAMMER_CURSOR_ASOF;
1095 * Replace any in-memory version of the record.
1097 error = hammer_ip_lookup(&cursor);
1098 if (error == 0 && hammer_cursor_inmem(&cursor)) {
1099 record = cursor.iprec;
1100 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
1101 KKASSERT(cursor.deadlk_rec == NULL);
1102 hammer_ref(&record->lock);
1103 cursor.deadlk_rec = record;
1106 record->flags |= HAMMER_RECF_DELETED_FE;
1112 * Allocate replacement general record. The backend flush will
1113 * delete any on-disk version of the record.
1115 if (error == 0 || error == ENOENT) {
1116 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
1117 record->type = HAMMER_MEM_RECORD_GENERAL;
1119 record->leaf.base.localization = ip->obj_localization |
1120 HAMMER_LOCALIZE_MISC;
1121 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
1122 record->leaf.base.key = pfsm->localization;
1123 record->leaf.data_len = sizeof(pfsm->pfsd);
1124 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
1125 error = hammer_ip_add_record(trans, record);
1127 hammer_done_cursor(&cursor);
1128 if (error == EDEADLK)
1130 hammer_rel_inode(ip, 0);
1135 * Create a root directory for a PFS if one does not alredy exist.
1137 * The PFS root stands alone so we must also bump the nlinks count
1138 * to prevent it from being destroyed on release.
1141 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1142 hammer_pseudofs_inmem_t pfsm)
1148 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1149 pfsm->localization, 0, &error);
1154 error = hammer_create_inode(trans, &vap, cred,
1158 ++ip->ino_data.nlinks;
1159 hammer_modify_inode(trans, ip, HAMMER_INODE_DDIRTY);
1163 hammer_rel_inode(ip, 0);
1168 * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1169 * if we are unable to disassociate all the inodes.
1173 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1177 hammer_ref(&ip->lock);
1178 if (ip->vp && (ip->vp->v_flag & VPFSROOT)) {
1180 * The hammer pfs-upgrade directive itself might have the
1181 * root of the pfs open. Just allow it.
1186 * Don't allow any subdirectories or files to be open.
1188 if (hammer_isactive(&ip->lock) == 2 && ip->vp)
1189 vclean_unlocked(ip->vp);
1190 if (hammer_isactive(&ip->lock) == 1 && ip->vp == NULL)
1193 res = -1; /* stop, someone is using the inode */
1195 hammer_rel_inode(ip, 0);
1200 hammer_unload_pseudofs(hammer_transaction_t trans, uint32_t localization)
1205 for (try = res = 0; try < 4; ++try) {
1206 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1207 hammer_inode_pfs_cmp,
1208 hammer_unload_pseudofs_callback,
1210 if (res == 0 && try > 1)
1212 hammer_flusher_sync(trans->hmp);
1221 * Release a reference on a PFS
1224 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1226 hammer_rel(&pfsm->lock);
1227 if (hammer_norefs(&pfsm->lock)) {
1228 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1229 kfree(pfsm, hmp->m_misc);
1234 * Called by hammer_sync_inode().
1237 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1239 hammer_transaction_t trans = cursor->trans;
1240 hammer_record_t record;
1248 * If the inode has a presence on-disk then locate it and mark
1249 * it deleted, setting DELONDISK.
1251 * The record may or may not be physically deleted, depending on
1252 * the retention policy.
1254 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1255 HAMMER_INODE_ONDISK) {
1256 hammer_normalize_cursor(cursor);
1257 cursor->key_beg.localization = ip->obj_localization |
1258 HAMMER_LOCALIZE_INODE;
1259 cursor->key_beg.obj_id = ip->obj_id;
1260 cursor->key_beg.key = 0;
1261 cursor->key_beg.create_tid = 0;
1262 cursor->key_beg.delete_tid = 0;
1263 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1264 cursor->key_beg.obj_type = 0;
1265 cursor->asof = ip->obj_asof;
1266 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1267 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1268 cursor->flags |= HAMMER_CURSOR_BACKEND;
1270 error = hammer_btree_lookup(cursor);
1271 if (hammer_debug_inode)
1272 hdkprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1275 error = hammer_ip_delete_record(cursor, ip, trans->tid);
1276 if (hammer_debug_inode)
1277 hdkprintf("error %d\n", error);
1279 ip->flags |= HAMMER_INODE_DELONDISK;
1282 hammer_cache_node(&ip->cache[0], cursor->node);
1284 if (error == EDEADLK) {
1285 hammer_done_cursor(cursor);
1286 error = hammer_init_cursor(trans, cursor,
1288 if (hammer_debug_inode)
1289 hdkprintf("IPDED %p %d\n", ip, error);
1296 * Ok, write out the initial record or a new record (after deleting
1297 * the old one), unless the DELETED flag is set. This routine will
1298 * clear DELONDISK if it writes out a record.
1300 * Update our inode statistics if this is the first application of
1301 * the inode on-disk.
1303 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1305 * Generate a record and write it to the media. We clean-up
1306 * the state before releasing so we do not have to set-up
1309 record = hammer_alloc_mem_record(ip, 0);
1310 record->type = HAMMER_MEM_RECORD_INODE;
1311 record->flush_state = HAMMER_FST_FLUSH;
1312 record->leaf = ip->sync_ino_leaf;
1313 record->leaf.base.create_tid = trans->tid;
1314 record->leaf.data_len = sizeof(ip->sync_ino_data);
1315 record->leaf.create_ts = trans->time32;
1316 record->data = (void *)&ip->sync_ino_data;
1317 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1320 * If this flag is set we cannot sync the new file size
1321 * because we haven't finished related truncations. The
1322 * inode will be flushed in another flush group to finish
1325 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1326 ip->sync_ino_data.size != ip->ino_data.size) {
1328 ip->sync_ino_data.size = ip->ino_data.size;
1334 error = hammer_ip_sync_record_cursor(cursor, record);
1335 if (hammer_debug_inode)
1336 hdkprintf("GENREC %p rec %08x %d\n",
1337 ip, record->flags, error);
1338 if (error != EDEADLK)
1340 hammer_done_cursor(cursor);
1341 error = hammer_init_cursor(trans, cursor,
1343 if (hammer_debug_inode)
1344 hdkprintf("GENREC reinit %d\n", error);
1350 * Note: The record was never on the inode's record tree
1351 * so just wave our hands importantly and destroy it.
1353 record->flags |= HAMMER_RECF_COMMITTED;
1354 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1355 record->flush_state = HAMMER_FST_IDLE;
1356 ++ip->rec_generation;
1357 hammer_rel_mem_record(record);
1363 if (hammer_debug_inode)
1364 hdkprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1365 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1366 HAMMER_INODE_SDIRTY |
1367 HAMMER_INODE_ATIME |
1368 HAMMER_INODE_MTIME);
1369 ip->flags &= ~HAMMER_INODE_DELONDISK;
1371 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1374 * Root volume count of inodes
1376 hammer_sync_lock_sh(trans);
1377 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1378 hammer_modify_volume_field(trans,
1381 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1382 hammer_modify_volume_done(trans->rootvol);
1383 ip->flags |= HAMMER_INODE_ONDISK;
1384 if (hammer_debug_inode)
1385 hdkprintf("NOWONDISK %p\n", ip);
1387 hammer_sync_unlock(trans);
1392 * If the inode has been destroyed, clean out any left-over flags
1393 * that may have been set by the frontend.
1395 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1396 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1397 HAMMER_INODE_SDIRTY |
1398 HAMMER_INODE_ATIME |
1399 HAMMER_INODE_MTIME);
1405 * Update only the itimes fields.
1407 * ATIME can be updated without generating any UNDO. MTIME is updated
1408 * with UNDO so it is guaranteed to be synchronized properly in case of
1411 * Neither field is included in the B-Tree leaf element's CRC, which is how
1412 * we can get away with updating ATIME the way we do.
1415 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1417 hammer_transaction_t trans = cursor->trans;
1421 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1422 HAMMER_INODE_ONDISK) {
1426 hammer_normalize_cursor(cursor);
1427 cursor->key_beg.localization = ip->obj_localization |
1428 HAMMER_LOCALIZE_INODE;
1429 cursor->key_beg.obj_id = ip->obj_id;
1430 cursor->key_beg.key = 0;
1431 cursor->key_beg.create_tid = 0;
1432 cursor->key_beg.delete_tid = 0;
1433 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1434 cursor->key_beg.obj_type = 0;
1435 cursor->asof = ip->obj_asof;
1436 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1437 cursor->flags |= HAMMER_CURSOR_ASOF;
1438 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1439 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1440 cursor->flags |= HAMMER_CURSOR_BACKEND;
1442 error = hammer_btree_lookup(cursor);
1444 hammer_cache_node(&ip->cache[0], cursor->node);
1445 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1447 * Updating MTIME requires an UNDO. Just cover
1448 * both atime and mtime.
1450 hammer_sync_lock_sh(trans);
1451 hammer_modify_buffer(trans, cursor->data_buffer,
1452 &cursor->data->inode.mtime,
1453 sizeof(cursor->data->inode.atime) +
1454 sizeof(cursor->data->inode.mtime));
1455 cursor->data->inode.atime = ip->sync_ino_data.atime;
1456 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1457 hammer_modify_buffer_done(cursor->data_buffer);
1458 hammer_sync_unlock(trans);
1459 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1461 * Updating atime only can be done in-place with
1464 hammer_sync_lock_sh(trans);
1465 hammer_modify_buffer_noundo(trans, cursor->data_buffer);
1466 cursor->data->inode.atime = ip->sync_ino_data.atime;
1467 hammer_modify_buffer_done(cursor->data_buffer);
1468 hammer_sync_unlock(trans);
1470 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1472 if (error == EDEADLK) {
1473 hammer_done_cursor(cursor);
1474 error = hammer_init_cursor(trans, cursor, &ip->cache[0], ip);
1482 * Release a reference on an inode, flush as requested.
1484 * On the last reference we queue the inode to the flusher for its final
1488 hammer_rel_inode(struct hammer_inode *ip, int flush)
1491 * Handle disposition when dropping the last ref.
1494 if (hammer_oneref(&ip->lock)) {
1496 * Determine whether on-disk action is needed for
1497 * the inode's final disposition.
1499 KKASSERT(ip->vp == NULL);
1500 hammer_inode_unloadable_check(ip, 0);
1501 if (ip->flags & HAMMER_INODE_MODMASK) {
1502 hammer_flush_inode(ip, 0);
1503 } else if (hammer_oneref(&ip->lock)) {
1504 hammer_unload_inode(ip);
1509 hammer_flush_inode(ip, 0);
1512 * The inode still has multiple refs, try to drop
1515 KKASSERT(hammer_isactive(&ip->lock) >= 1);
1516 if (hammer_isactive(&ip->lock) > 1) {
1517 hammer_rel(&ip->lock);
1525 * Unload and destroy the specified inode. Must be called with one remaining
1526 * reference. The reference is disposed of.
1528 * The inode must be completely clean.
1531 hammer_unload_inode(struct hammer_inode *ip)
1533 hammer_mount_t hmp = ip->hmp;
1535 KASSERT(hammer_oneref(&ip->lock),
1536 ("hammer_unload_inode: %d refs", hammer_isactive(&ip->lock)));
1537 KKASSERT(ip->vp == NULL);
1538 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1539 KKASSERT(ip->cursor_ip_refs == 0);
1540 KKASSERT(hammer_notlocked(&ip->lock));
1541 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1543 KKASSERT(RB_EMPTY(&ip->rec_tree));
1544 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1546 if (ip->flags & HAMMER_INODE_RDIRTY) {
1547 RB_REMOVE(hammer_redo_rb_tree, &hmp->rb_redo_root, ip);
1548 ip->flags &= ~HAMMER_INODE_RDIRTY;
1550 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1552 hammer_free_inode(ip);
1557 * Called during unmounting if a critical error occured. The in-memory
1558 * inode and all related structures are destroyed.
1560 * If a critical error did not occur the unmount code calls the standard
1561 * release and asserts that the inode is gone.
1564 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1566 hammer_record_t rec;
1569 * Get rid of the inodes in-memory records, regardless of their
1570 * state, and clear the mod-mask.
1572 while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1573 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1574 rec->target_ip = NULL;
1575 if (rec->flush_state == HAMMER_FST_SETUP)
1576 rec->flush_state = HAMMER_FST_IDLE;
1578 while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1579 if (rec->flush_state == HAMMER_FST_FLUSH)
1580 --rec->flush_group->refs;
1582 hammer_ref(&rec->lock);
1583 KKASSERT(hammer_oneref(&rec->lock));
1584 rec->flush_state = HAMMER_FST_IDLE;
1585 rec->flush_group = NULL;
1586 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1587 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1588 ++ip->rec_generation;
1589 hammer_rel_mem_record(rec);
1591 ip->flags &= ~HAMMER_INODE_MODMASK;
1592 ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1593 KKASSERT(ip->vp == NULL);
1596 * Remove the inode from any flush group, force it idle. FLUSH
1597 * and SETUP states have an inode ref.
1599 switch(ip->flush_state) {
1600 case HAMMER_FST_FLUSH:
1601 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
1602 --ip->flush_group->refs;
1603 ip->flush_group = NULL;
1605 case HAMMER_FST_SETUP:
1606 hammer_rel(&ip->lock);
1607 ip->flush_state = HAMMER_FST_IDLE;
1609 case HAMMER_FST_IDLE:
1614 * There shouldn't be any associated vnode. The unload needs at
1615 * least one ref, if we do have a vp steal its ip ref.
1618 hdkprintf("Unexpected vnode association ip %p vp %p\n",
1620 ip->vp->v_data = NULL;
1623 hammer_ref(&ip->lock);
1625 hammer_unload_inode(ip);
1630 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1631 * the read-only flag for cached inodes.
1633 * This routine is called from a RB_SCAN().
1636 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1638 hammer_mount_t hmp = ip->hmp;
1640 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1641 ip->flags |= HAMMER_INODE_RO;
1643 ip->flags &= ~HAMMER_INODE_RO;
1648 * A transaction has modified an inode, requiring updates as specified by
1651 * HAMMER_INODE_DDIRTY: Inode data has been updated, not incl mtime/atime,
1652 * and not including size changes due to write-append
1653 * (but other size changes are included).
1654 * HAMMER_INODE_SDIRTY: Inode data has been updated, size changes due to
1656 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1657 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1658 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1659 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1662 hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags)
1665 * ronly of 0 or 2 does not trigger assertion.
1666 * 2 is a special error state
1668 KKASSERT(ip->hmp->ronly != 1 ||
1669 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1670 HAMMER_INODE_SDIRTY |
1671 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1672 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1673 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1674 ip->flags |= HAMMER_INODE_RSV_INODES;
1675 ++ip->hmp->rsv_inodes;
1679 * Set the NEWINODE flag in the transaction if the inode
1680 * transitions to a dirty state. This is used to track
1681 * the load on the inode cache.
1684 (ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1685 (flags & HAMMER_INODE_MODMASK)) {
1686 trans->flags |= HAMMER_TRANSF_NEWINODE;
1688 if (flags & HAMMER_INODE_MODMASK)
1689 hammer_inode_dirty(ip);
1694 * Attempt to quickly update the atime for a hammer inode. Return 0 on
1695 * success, -1 on failure.
1697 * We attempt to update the atime with only the ip lock and not the
1698 * whole filesystem lock in order to improve concurrency. We can only
1699 * do this safely if the ATIME flag is already pending on the inode.
1701 * This function is called via a vnops path (ip pointer is stable) without
1705 hammer_update_atime_quick(hammer_inode_t ip)
1710 if ((ip->flags & HAMMER_INODE_RO) ||
1711 (ip->hmp->mp->mnt_flag & MNT_NOATIME)) {
1713 * Silently indicate success on read-only mount/snap
1716 } else if (ip->flags & HAMMER_INODE_ATIME) {
1718 * Double check with inode lock held against backend. This
1719 * is only safe if all we need to do is update
1723 hammer_lock_ex(&ip->lock);
1724 if (ip->flags & HAMMER_INODE_ATIME) {
1725 ip->ino_data.atime =
1726 (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
1729 hammer_unlock(&ip->lock);
1735 * Request that an inode be flushed. This whole mess cannot block and may
1736 * recurse (if not synchronous). Once requested HAMMER will attempt to
1737 * actively flush the inode until the flush can be done.
1739 * The inode may already be flushing, or may be in a setup state. We can
1740 * place the inode in a flushing state if it is currently idle and flag it
1741 * to reflush if it is currently flushing.
1743 * Upon return if the inode could not be flushed due to a setup
1744 * dependancy, then it will be automatically flushed when the dependancy
1748 hammer_flush_inode(hammer_inode_t ip, int flags)
1751 hammer_flush_group_t flg;
1755 * fill_flush_group is the first flush group we may be able to
1756 * continue filling, it may be open or closed but it will always
1757 * be past the currently flushing (running) flg.
1759 * next_flush_group is the next open flush group.
1762 while ((flg = hmp->fill_flush_group) != NULL) {
1763 KKASSERT(flg->running == 0);
1764 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit &&
1765 flg->total_count <= hammer_autoflush) {
1768 hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
1769 hammer_flusher_async(ip->hmp, flg);
1772 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1773 flg->seq = hmp->flusher.next++;
1774 if (hmp->next_flush_group == NULL)
1775 hmp->next_flush_group = flg;
1776 if (hmp->fill_flush_group == NULL)
1777 hmp->fill_flush_group = flg;
1778 RB_INIT(&flg->flush_tree);
1779 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1783 * Trivial 'nothing to flush' case. If the inode is in a SETUP
1784 * state we have to put it back into an IDLE state so we can
1785 * drop the extra ref.
1787 * If we have a parent dependancy we must still fall through
1790 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1791 if (ip->flush_state == HAMMER_FST_SETUP &&
1792 TAILQ_EMPTY(&ip->target_list)) {
1793 ip->flush_state = HAMMER_FST_IDLE;
1794 hammer_rel_inode(ip, 0);
1796 if (ip->flush_state == HAMMER_FST_IDLE)
1801 * Our flush action will depend on the current state.
1803 switch(ip->flush_state) {
1804 case HAMMER_FST_IDLE:
1806 * We have no dependancies and can flush immediately. Some
1807 * our children may not be flushable so we have to re-test
1808 * with that additional knowledge.
1810 hammer_flush_inode_core(ip, flg, flags);
1812 case HAMMER_FST_SETUP:
1814 * Recurse upwards through dependancies via target_list
1815 * and start their flusher actions going if possible.
1817 * 'good' is our connectivity. -1 means we have none and
1818 * can't flush, 0 means there weren't any dependancies, and
1819 * 1 means we have good connectivity.
1821 good = hammer_setup_parent_inodes(ip, 0, flg);
1825 * We can continue if good >= 0. Determine how
1826 * many records under our inode can be flushed (and
1829 hammer_flush_inode_core(ip, flg, flags);
1832 * Parent has no connectivity, tell it to flush
1833 * us as soon as it does.
1835 * The REFLUSH flag is also needed to trigger
1836 * dependancy wakeups.
1838 ip->flags |= HAMMER_INODE_CONN_DOWN |
1839 HAMMER_INODE_REFLUSH;
1840 if (flags & HAMMER_FLUSH_SIGNAL) {
1841 ip->flags |= HAMMER_INODE_RESIGNAL;
1842 hammer_flusher_async(ip->hmp, flg);
1846 case HAMMER_FST_FLUSH:
1848 * We are already flushing, flag the inode to reflush
1849 * if needed after it completes its current flush.
1851 * The REFLUSH flag is also needed to trigger
1852 * dependancy wakeups.
1854 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1855 ip->flags |= HAMMER_INODE_REFLUSH;
1856 if (flags & HAMMER_FLUSH_SIGNAL) {
1857 ip->flags |= HAMMER_INODE_RESIGNAL;
1858 hammer_flusher_async(ip->hmp, flg);
1865 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1866 * ip which reference our ip.
1868 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1869 * so for now do not ref/deref the structures. Note that if we use the
1870 * ref/rel code later, the rel CAN block.
1873 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1874 hammer_flush_group_t flg)
1876 hammer_record_t depend;
1881 * If we hit our recursion limit and we have parent dependencies
1882 * We cannot continue. Returning < 0 will cause us to be flagged
1883 * for reflush. Returning -2 cuts off additional dependency checks
1884 * because they are likely to also hit the depth limit.
1886 * We cannot return < 0 if there are no dependencies or there might
1887 * not be anything to wakeup (ip).
1889 if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1890 if (hammer_debug_general & 0x10000)
1891 hkrateprintf(&hammer_gen_krate,
1892 "Warning: depth limit reached on "
1893 "setup recursion, inode %p %016llx\n",
1894 ip, (long long)ip->obj_id);
1902 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1903 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1904 KKASSERT(depend->target_ip == ip);
1905 if (r < 0 && good == 0)
1911 * If we failed due to the recursion depth limit then stop
1921 * This helper function takes a record representing the dependancy between
1922 * the parent inode and child inode.
1924 * record = record in question (*rec in below)
1925 * record->ip = parent inode (*pip in below)
1926 * record->target_ip = child inode (*ip in below)
1928 * *pip--------------\
1931 * \ip /\\\\\ rbtree of recs from parent inode's view
1935 * \------*rec------target_ip------>*ip
1936 * ...target_entry<----...----->target_list<---...
1937 * list of recs from inode's view
1939 * We are asked to recurse upwards and convert the record from SETUP
1940 * to FLUSH if possible.
1942 * Return 1 if the record gives us connectivity
1944 * Return 0 if the record is not relevant
1946 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1949 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1950 hammer_flush_group_t flg)
1955 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1959 * If the record is already flushing, is it in our flush group?
1961 * If it is in our flush group but it is a general record or a
1962 * delete-on-disk, it does not improve our connectivity (return 0),
1963 * and if the target inode is not trying to destroy itself we can't
1964 * allow the operation yet anyway (the second return -1).
1966 if (record->flush_state == HAMMER_FST_FLUSH) {
1968 * If not in our flush group ask the parent to reflush
1969 * us as soon as possible.
1971 if (record->flush_group != flg) {
1972 pip->flags |= HAMMER_INODE_REFLUSH;
1973 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1978 * If in our flush group everything is already set up,
1979 * just return whether the record will improve our
1980 * visibility or not.
1982 if (record->type == HAMMER_MEM_RECORD_ADD)
1988 * It must be a setup record. Try to resolve the setup dependancies
1989 * by recursing upwards so we can place ip on the flush list.
1991 * Limit ourselves to 20 levels of recursion to avoid blowing out
1992 * the kernel stack. If we hit the recursion limit we can't flush
1993 * until the parent flushes. The parent will flush independantly
1994 * on its own and ultimately a deep recursion will be resolved.
1996 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1998 good = hammer_setup_parent_inodes(pip, depth + 1, flg);
2001 * If good < 0 the parent has no connectivity and we cannot safely
2002 * flush the directory entry, which also means we can't flush our
2003 * ip. Flag us for downward recursion once the parent's
2004 * connectivity is resolved. Flag the parent for [re]flush or it
2005 * may not check for downward recursions.
2008 pip->flags |= HAMMER_INODE_REFLUSH;
2009 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
2014 * We are go, place the parent inode in a flushing state so we can
2015 * place its record in a flushing state. Note that the parent
2016 * may already be flushing. The record must be in the same flush
2017 * group as the parent.
2019 if (pip->flush_state != HAMMER_FST_FLUSH)
2020 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
2021 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
2024 * It is possible for a rename to create a loop in the recursion
2025 * and revisit a record. This will result in the record being
2026 * placed in a flush state unexpectedly. This check deals with
2029 if (record->flush_state == HAMMER_FST_FLUSH) {
2030 if (record->type == HAMMER_MEM_RECORD_ADD)
2035 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
2038 if (record->type == HAMMER_MEM_RECORD_DEL &&
2039 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
2041 * Regardless of flushing state we cannot sync this path if the
2042 * record represents a delete-on-disk but the target inode
2043 * is not ready to sync its own deletion.
2045 * XXX need to count effective nlinks to determine whether
2046 * the flush is ok, otherwise removing a hardlink will
2047 * just leave the DEL record to rot.
2049 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
2053 if (pip->flush_group == flg) {
2055 * Because we have not calculated nlinks yet we can just
2056 * set records to the flush state if the parent is in
2057 * the same flush group as we are.
2059 record->flush_state = HAMMER_FST_FLUSH;
2060 record->flush_group = flg;
2061 ++record->flush_group->refs;
2062 hammer_ref(&record->lock);
2065 * A general directory-add contributes to our visibility.
2067 * Otherwise it is probably a directory-delete or
2068 * delete-on-disk record and does not contribute to our
2069 * visbility (but we can still flush it).
2071 if (record->type == HAMMER_MEM_RECORD_ADD)
2076 * If the parent is not in our flush group we cannot
2077 * flush this record yet, there is no visibility.
2078 * We tell the parent to reflush and mark ourselves
2079 * so the parent knows it should flush us too.
2081 pip->flags |= HAMMER_INODE_REFLUSH;
2082 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
2088 * This is the core routine placing an inode into the FST_FLUSH state.
2091 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
2093 hammer_mount_t hmp = ip->hmp;
2097 * Set flush state and prevent the flusher from cycling into
2098 * the next flush group. Do not place the ip on the list yet.
2099 * Inodes not in the idle state get an extra reference.
2101 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
2102 if (ip->flush_state == HAMMER_FST_IDLE)
2103 hammer_ref(&ip->lock);
2104 ip->flush_state = HAMMER_FST_FLUSH;
2105 ip->flush_group = flg;
2106 ++hmp->flusher.group_lock;
2107 ++hmp->count_iqueued;
2108 ++hammer_count_iqueued;
2110 hammer_redo_fifo_start_flush(ip);
2114 * We need to be able to vfsync/truncate from the backend.
2116 * XXX Any truncation from the backend will acquire the vnode
2119 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
2120 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
2121 ip->flags |= HAMMER_INODE_VHELD;
2127 * Figure out how many in-memory records we can actually flush
2128 * (not including inode meta-data, buffers, etc).
2130 KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
2131 if (flags & HAMMER_FLUSH_RECURSION) {
2133 * If this is a upwards recursion we do not want to
2134 * recurse down again!
2138 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2140 * No new records are added if we must complete a flush
2141 * from a previous cycle, but we do have to move the records
2142 * from the previous cycle to the current one.
2145 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2146 hammer_syncgrp_child_callback, NULL);
2152 * Normal flush, scan records and bring them into the flush.
2153 * Directory adds and deletes are usually skipped (they are
2154 * grouped with the related inode rather then with the
2157 * go_count can be negative, which means the scan aborted
2158 * due to the flush group being over-full and we should
2159 * flush what we have.
2161 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2162 hammer_setup_child_callback, NULL);
2166 * This is a more involved test that includes go_count. If we
2167 * can't flush, flag the inode and return. If go_count is 0 we
2168 * were are unable to flush any records in our rec_tree and
2169 * must ignore the XDIRTY flag.
2171 if (go_count == 0) {
2172 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
2173 --hmp->count_iqueued;
2174 --hammer_count_iqueued;
2177 ip->flush_state = HAMMER_FST_SETUP;
2178 ip->flush_group = NULL;
2179 if (flags & HAMMER_FLUSH_SIGNAL) {
2180 ip->flags |= HAMMER_INODE_REFLUSH |
2181 HAMMER_INODE_RESIGNAL;
2183 ip->flags |= HAMMER_INODE_REFLUSH;
2186 if (ip->flags & HAMMER_INODE_VHELD) {
2187 ip->flags &= ~HAMMER_INODE_VHELD;
2193 * REFLUSH is needed to trigger dependancy wakeups
2194 * when an inode is in SETUP.
2196 ip->flags |= HAMMER_INODE_REFLUSH;
2197 if (--hmp->flusher.group_lock == 0)
2198 wakeup(&hmp->flusher.group_lock);
2204 * Snapshot the state of the inode for the backend flusher.
2206 * We continue to retain save_trunc_off even when all truncations
2207 * have been resolved as an optimization to determine if we can
2208 * skip the B-Tree lookup for overwrite deletions.
2210 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
2211 * and stays in ip->flags. Once set, it stays set until the
2212 * inode is destroyed.
2214 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2215 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
2216 ip->sync_trunc_off = ip->trunc_off;
2217 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
2218 ip->flags &= ~HAMMER_INODE_TRUNCATED;
2219 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
2222 * The save_trunc_off used to cache whether the B-Tree
2223 * holds any records past that point is not used until
2224 * after the truncation has succeeded, so we can safely
2227 if (ip->save_trunc_off > ip->sync_trunc_off)
2228 ip->save_trunc_off = ip->sync_trunc_off;
2230 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
2231 ~HAMMER_INODE_TRUNCATED);
2232 ip->sync_ino_leaf = ip->ino_leaf;
2233 ip->sync_ino_data = ip->ino_data;
2234 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
2237 * The flusher list inherits our inode and reference.
2239 KKASSERT(flg->running == 0);
2240 RB_INSERT(hammer_fls_rb_tree, &flg->flush_tree, ip);
2241 if (--hmp->flusher.group_lock == 0)
2242 wakeup(&hmp->flusher.group_lock);
2245 * Auto-flush the group if it grows too large. Make sure the
2246 * inode reclaim wait pipeline continues to work.
2248 if (flg->total_count >= hammer_autoflush ||
2249 flg->total_count >= hammer_limit_reclaims / 4) {
2250 if (hmp->fill_flush_group == flg)
2251 hmp->fill_flush_group = TAILQ_NEXT(flg, flush_entry);
2252 hammer_flusher_async(hmp, flg);
2257 * Callback for scan of ip->rec_tree. Try to include each record in our
2258 * flush. ip->flush_group has been set but the inode has not yet been
2259 * moved into a flushing state.
2261 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
2264 * We return 1 for any record placed or found in FST_FLUSH, which prevents
2265 * the caller from shortcutting the flush.
2268 hammer_setup_child_callback(hammer_record_t rec, void *data)
2270 hammer_flush_group_t flg;
2271 hammer_inode_t target_ip;
2276 * Records deleted or committed by the backend are ignored.
2277 * Note that the flush detects deleted frontend records at
2278 * multiple points to deal with races. This is just the first
2279 * line of defense. The only time HAMMER_RECF_DELETED_FE cannot
2280 * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2281 * messes up link-count calculations.
2283 * NOTE: Don't get confused between record deletion and, say,
2284 * directory entry deletion. The deletion of a directory entry
2285 * which is on-media has nothing to do with the record deletion
2288 if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2289 HAMMER_RECF_COMMITTED)) {
2290 if (rec->flush_state == HAMMER_FST_FLUSH) {
2291 KKASSERT(rec->flush_group == rec->ip->flush_group);
2300 * If the record is in an idle state it has no dependancies and
2304 flg = ip->flush_group;
2307 switch(rec->flush_state) {
2308 case HAMMER_FST_IDLE:
2310 * The record has no setup dependancy, we can flush it.
2312 KKASSERT(rec->target_ip == NULL);
2313 rec->flush_state = HAMMER_FST_FLUSH;
2314 rec->flush_group = flg;
2316 hammer_ref(&rec->lock);
2319 case HAMMER_FST_SETUP:
2321 * The record has a setup dependancy. These are typically
2322 * directory entry adds and deletes. Such entries will be
2323 * flushed when their inodes are flushed so we do not
2324 * usually have to add them to the flush here. However,
2325 * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2326 * it is asking us to flush this record (and it).
2328 target_ip = rec->target_ip;
2329 KKASSERT(target_ip != NULL);
2330 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2333 * If the target IP is already flushing in our group
2334 * we could associate the record, but target_ip has
2335 * already synced ino_data to sync_ino_data and we
2336 * would also have to adjust nlinks. Plus there are
2337 * ordering issues for adds and deletes.
2339 * Reflush downward if this is an ADD, and upward if
2342 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2343 if (rec->type == HAMMER_MEM_RECORD_ADD)
2344 ip->flags |= HAMMER_INODE_REFLUSH;
2346 target_ip->flags |= HAMMER_INODE_REFLUSH;
2351 * Target IP is not yet flushing. This can get complex
2352 * because we have to be careful about the recursion.
2354 * Directories create an issue for us in that if a flush
2355 * of a directory is requested the expectation is to flush
2356 * any pending directory entries, but this will cause the
2357 * related inodes to recursively flush as well. We can't
2358 * really defer the operation so just get as many as we
2362 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2363 (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2365 * We aren't reclaiming and the target ip was not
2366 * previously prevented from flushing due to this
2367 * record dependancy. Do not flush this record.
2372 if (flg->total_count + flg->refs >
2373 ip->hmp->undo_rec_limit) {
2375 * Our flush group is over-full and we risk blowing
2376 * out the UNDO FIFO. Stop the scan, flush what we
2377 * have, then reflush the directory.
2379 * The directory may be forced through multiple
2380 * flush groups before it can be completely
2383 ip->flags |= HAMMER_INODE_RESIGNAL |
2384 HAMMER_INODE_REFLUSH;
2386 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2388 * If the target IP is not flushing we can force
2389 * it to flush, even if it is unable to write out
2390 * any of its own records we have at least one in
2391 * hand that we CAN deal with.
2393 rec->flush_state = HAMMER_FST_FLUSH;
2394 rec->flush_group = flg;
2396 hammer_ref(&rec->lock);
2397 hammer_flush_inode_core(target_ip, flg,
2398 HAMMER_FLUSH_RECURSION);
2402 * General or delete-on-disk record.
2404 * XXX this needs help. If a delete-on-disk we could
2405 * disconnect the target. If the target has its own
2406 * dependancies they really need to be flushed.
2410 rec->flush_state = HAMMER_FST_FLUSH;
2411 rec->flush_group = flg;
2413 hammer_ref(&rec->lock);
2414 hammer_flush_inode_core(target_ip, flg,
2415 HAMMER_FLUSH_RECURSION);
2419 case HAMMER_FST_FLUSH:
2421 * The record could be part of a previous flush group if the
2422 * inode is a directory (the record being a directory entry).
2423 * Once the flush group was closed a hammer_test_inode()
2424 * function can cause a new flush group to be setup, placing
2425 * the directory inode itself in a new flush group.
2427 * When associated with a previous flush group we count it
2428 * as if it were in our current flush group, since it will
2429 * effectively be flushed by the time we flush our current
2433 rec->ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY ||
2434 rec->flush_group == flg);
2443 * This version just moves records already in a flush state to the new
2444 * flush group and that is it.
2447 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2449 hammer_inode_t ip = rec->ip;
2451 switch(rec->flush_state) {
2452 case HAMMER_FST_FLUSH:
2453 KKASSERT(rec->flush_group == ip->flush_group);
2463 * Wait for a previously queued flush to complete.
2465 * If a critical error occured we don't try to wait.
2468 hammer_wait_inode(hammer_inode_t ip)
2471 * The inode can be in a SETUP state in which case RESIGNAL
2472 * should be set. If RESIGNAL is not set then the previous
2473 * flush completed and a later operation placed the inode
2474 * in a passive setup state again, so we're done.
2476 * The inode can be in a FLUSH state in which case we
2477 * can just wait for completion.
2479 while (ip->flush_state == HAMMER_FST_FLUSH ||
2480 (ip->flush_state == HAMMER_FST_SETUP &&
2481 (ip->flags & HAMMER_INODE_RESIGNAL))) {
2483 * Don't try to flush on a critical error
2485 if (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
2489 * If the inode was already being flushed its flg
2490 * may not have been queued to the backend. We
2491 * have to make sure it gets queued or we can wind
2492 * up blocked or deadlocked (particularly if we are
2493 * the vnlru thread).
2495 if (ip->flush_state == HAMMER_FST_FLUSH) {
2496 KKASSERT(ip->flush_group);
2497 if (ip->flush_group->closed == 0) {
2498 if (hammer_debug_inode) {
2499 hkprintf("debug: forcing "
2500 "async flush ip %016jx\n",
2501 (intmax_t)ip->obj_id);
2503 hammer_flusher_async(ip->hmp, ip->flush_group);
2504 continue; /* retest */
2509 * In a flush state with the flg queued to the backend
2510 * or in a setup state with RESIGNAL set, we can safely
2513 ip->flags |= HAMMER_INODE_FLUSHW;
2514 tsleep(&ip->flags, 0, "hmrwin", 0);
2519 * The inode may have been in a passive setup state,
2520 * call flush to make sure we get signaled.
2522 if (ip->flush_state == HAMMER_FST_SETUP)
2523 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2529 * Called by the backend code when a flush has been completed.
2530 * The inode has already been removed from the flush list.
2532 * A pipelined flush can occur, in which case we must re-enter the
2533 * inode on the list and re-copy its fields.
2536 hammer_flush_inode_done(hammer_inode_t ip, int error)
2541 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2546 * Auto-reflush if the backend could not completely flush
2547 * the inode. This fixes a case where a deferred buffer flush
2548 * could cause fsync to return early.
2550 if (ip->sync_flags & HAMMER_INODE_MODMASK)
2551 ip->flags |= HAMMER_INODE_REFLUSH;
2554 * Merge left-over flags back into the frontend and fix the state.
2555 * Incomplete truncations are retained by the backend.
2558 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2559 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2562 * The backend may have adjusted nlinks, so if the adjusted nlinks
2563 * does not match the fronttend set the frontend's DDIRTY flag again.
2565 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2566 ip->flags |= HAMMER_INODE_DDIRTY;
2569 * Fix up the dirty buffer status.
2571 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2572 ip->flags |= HAMMER_INODE_BUFS;
2574 hammer_redo_fifo_end_flush(ip);
2577 * Re-set the XDIRTY flag if some of the inode's in-memory records
2578 * could not be flushed.
2580 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2581 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2582 (!RB_EMPTY(&ip->rec_tree) &&
2583 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2586 * Do not lose track of inodes which no longer have vnode
2587 * assocations, otherwise they may never get flushed again.
2589 * The reflush flag can be set superfluously, causing extra pain
2590 * for no reason. If the inode is no longer modified it no longer
2591 * needs to be flushed.
2593 if (ip->flags & HAMMER_INODE_MODMASK) {
2595 ip->flags |= HAMMER_INODE_REFLUSH;
2597 ip->flags &= ~HAMMER_INODE_REFLUSH;
2601 * The fs token is held but the inode lock is not held. Because this
2602 * is a backend flush it is possible that the vnode has no references
2603 * and cause a reclaim race inside vsetisdirty() if/when it blocks.
2605 * Therefore, we must lock the inode around this particular dirtying
2606 * operation. We don't have to around other dirtying operations
2607 * where the vnode is implicitly or explicitly held.
2609 if (ip->flags & HAMMER_INODE_MODMASK) {
2610 hammer_lock_ex(&ip->lock);
2611 hammer_inode_dirty(ip);
2612 hammer_unlock(&ip->lock);
2616 * Adjust the flush state.
2618 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2620 * We were unable to flush out all our records, leave the
2621 * inode in a flush state and in the current flush group.
2622 * The flush group will be re-run.
2624 * This occurs if the UNDO block gets too full or there is
2625 * too much dirty meta-data and allows the flusher to
2626 * finalize the UNDO block and then re-flush.
2628 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2632 * Remove from the flush_group
2634 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
2635 ip->flush_group = NULL;
2639 * Clean up the vnode ref and tracking counts.
2641 if (ip->flags & HAMMER_INODE_VHELD) {
2642 ip->flags &= ~HAMMER_INODE_VHELD;
2646 --hmp->count_iqueued;
2647 --hammer_count_iqueued;
2650 * And adjust the state.
2652 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2653 ip->flush_state = HAMMER_FST_IDLE;
2656 ip->flush_state = HAMMER_FST_SETUP;
2661 * If the frontend is waiting for a flush to complete,
2664 if (ip->flags & HAMMER_INODE_FLUSHW) {
2665 ip->flags &= ~HAMMER_INODE_FLUSHW;
2670 * If the frontend made more changes and requested another
2671 * flush, then try to get it running.
2673 * Reflushes are aborted when the inode is errored out.
2675 if (ip->flags & HAMMER_INODE_REFLUSH) {
2676 ip->flags &= ~HAMMER_INODE_REFLUSH;
2677 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2678 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2679 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2681 hammer_flush_inode(ip, 0);
2687 * If we have no parent dependancies we can clear CONN_DOWN
2689 if (TAILQ_EMPTY(&ip->target_list))
2690 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2693 * If the inode is now clean drop the space reservation.
2695 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2696 (ip->flags & HAMMER_INODE_RSV_INODES)) {
2697 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2701 ip->flags &= ~HAMMER_INODE_SLAVEFLUSH;
2704 hammer_rel_inode(ip, 0);
2708 * Called from hammer_sync_inode() to synchronize in-memory records
2712 hammer_sync_record_callback(hammer_record_t record, void *data)
2714 hammer_cursor_t cursor = data;
2715 hammer_transaction_t trans = cursor->trans;
2716 hammer_mount_t hmp = trans->hmp;
2720 * Skip records that do not belong to the current flush.
2722 ++hammer_stats_record_iterations;
2723 if (record->flush_state != HAMMER_FST_FLUSH)
2726 if (record->flush_group != record->ip->flush_group) {
2727 hdkprintf("rec %p ip %p bad flush group %p %p\n",
2730 record->flush_group,
2731 record->ip->flush_group);
2732 if (hammer_debug_critical)
2736 KKASSERT(record->flush_group == record->ip->flush_group);
2739 * Interlock the record using the BE flag. Once BE is set the
2740 * frontend cannot change the state of FE.
2742 * NOTE: If FE is set prior to us setting BE we still sync the
2743 * record out, but the flush completion code converts it to
2744 * a delete-on-disk record instead of destroying it.
2746 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2747 record->flags |= HAMMER_RECF_INTERLOCK_BE;
2750 * The backend has already disposed of the record.
2752 if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2758 * If the whole inode is being deleted and all on-disk records will
2759 * be deleted very soon, we can't sync any new records to disk
2760 * because they will be deleted in the same transaction they were
2761 * created in (delete_tid == create_tid), which will assert.
2763 * XXX There may be a case with RECORD_ADD with DELETED_FE set
2764 * that we currently panic on.
2766 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2767 switch(record->type) {
2768 case HAMMER_MEM_RECORD_DATA:
2770 * We don't have to do anything, if the record was
2771 * committed the space will have been accounted for
2775 case HAMMER_MEM_RECORD_GENERAL:
2777 * Set deleted-by-backend flag. Do not set the
2778 * backend committed flag, because we are throwing
2781 record->flags |= HAMMER_RECF_DELETED_BE;
2782 ++record->ip->rec_generation;
2785 case HAMMER_MEM_RECORD_ADD:
2786 hpanic("illegal add during inode deletion record %p",
2788 break; /* NOT REACHED */
2789 case HAMMER_MEM_RECORD_INODE:
2790 hpanic("attempt to sync inode record %p?", record);
2791 break; /* NOT REACHED */
2792 case HAMMER_MEM_RECORD_DEL:
2794 * Follow through and issue the on-disk deletion
2801 * If DELETED_FE is set special handling is needed for directory
2802 * entries. Dependant pieces related to the directory entry may
2803 * have already been synced to disk. If this occurs we have to
2804 * sync the directory entry and then change the in-memory record
2805 * from an ADD to a DELETE to cover the fact that it's been
2806 * deleted by the frontend.
2808 * A directory delete covering record (MEM_RECORD_DEL) can never
2809 * be deleted by the frontend.
2811 * Any other record type (aka DATA) can be deleted by the frontend.
2812 * XXX At the moment the flusher must skip it because there may
2813 * be another data record in the flush group for the same block,
2814 * meaning that some frontend data changes can leak into the backend's
2815 * synchronization point.
2817 if (record->flags & HAMMER_RECF_DELETED_FE) {
2818 if (record->type == HAMMER_MEM_RECORD_ADD) {
2820 * Convert a front-end deleted directory-add to
2821 * a directory-delete entry later.
2823 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2826 * Dispose of the record (race case). Mark as
2827 * deleted by backend (and not committed).
2829 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2830 record->flags |= HAMMER_RECF_DELETED_BE;
2831 ++record->ip->rec_generation;
2838 * Assign the create_tid for new records. Deletions already
2839 * have the record's entire key properly set up.
2841 if (record->type != HAMMER_MEM_RECORD_DEL) {
2842 record->leaf.base.create_tid = trans->tid;
2843 record->leaf.create_ts = trans->time32;
2847 * This actually moves the record to the on-media B-Tree. We
2848 * must also generate REDO_TERM entries in the UNDO/REDO FIFO
2849 * indicating that the related REDO_WRITE(s) have been committed.
2851 * During recovery any REDO_TERM's within the nominal recovery span
2852 * are ignored since the related meta-data is being undone, causing
2853 * any matching REDO_WRITEs to execute. The REDO_TERMs outside
2854 * the nominal recovery span will match against REDO_WRITEs and
2855 * prevent them from being executed (because the meta-data has
2856 * already been synchronized).
2858 if (record->flags & HAMMER_RECF_REDO) {
2859 KKASSERT(record->type == HAMMER_MEM_RECORD_DATA);
2860 hammer_generate_redo(trans, record->ip,
2861 record->leaf.base.key -
2862 record->leaf.data_len,
2863 HAMMER_REDO_TERM_WRITE,
2865 record->leaf.data_len);
2869 error = hammer_ip_sync_record_cursor(cursor, record);
2870 if (error != EDEADLK)
2872 hammer_done_cursor(cursor);
2873 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2878 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2883 hammer_flush_record_done(record, error);
2886 * Do partial finalization if we have built up too many dirty
2887 * buffers. Otherwise a buffer cache deadlock can occur when
2888 * doing things like creating tens of thousands of tiny files.
2890 * We must release our cursor lock to avoid a 3-way deadlock
2891 * due to the exclusive sync lock the finalizer must get.
2893 * WARNING: See warnings in hammer_unlock_cursor() function.
2895 if (hammer_flusher_meta_limit(hmp) ||
2896 vm_page_count_severe()) {
2897 hammer_unlock_cursor(cursor);
2898 hammer_flusher_finalize(trans, 0);
2899 hammer_lock_cursor(cursor);
2905 * Backend function called by the flusher to sync an inode to media.
2908 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2910 struct hammer_cursor cursor;
2911 hammer_node_t tmp_node;
2912 hammer_record_t depend;
2913 hammer_record_t next;
2914 int error, tmp_error;
2917 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2920 error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2925 * Any directory records referencing this inode which are not in
2926 * our current flush group must adjust our nlink count for the
2927 * purposes of synchronizating to disk.
2929 * Records which are in our flush group can be unlinked from our
2930 * inode now, potentially allowing the inode to be physically
2933 * This cannot block.
2935 nlinks = ip->ino_data.nlinks;
2936 next = TAILQ_FIRST(&ip->target_list);
2937 while ((depend = next) != NULL) {
2938 next = TAILQ_NEXT(depend, target_entry);
2939 if (depend->flush_state == HAMMER_FST_FLUSH &&
2940 depend->flush_group == ip->flush_group) {
2942 * If this is an ADD that was deleted by the frontend
2943 * the frontend nlinks count will have already been
2944 * decremented, but the backend is going to sync its
2945 * directory entry and must account for it. The
2946 * record will be converted to a delete-on-disk when
2949 * If the ADD was not deleted by the frontend we
2950 * can remove the dependancy from our target_list.
2952 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2955 TAILQ_REMOVE(&ip->target_list, depend,
2957 depend->target_ip = NULL;
2959 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2961 * Not part of our flush group and not deleted by
2962 * the front-end, adjust the link count synced to
2963 * the media (undo what the frontend did when it
2964 * queued the record).
2966 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2967 switch(depend->type) {
2968 case HAMMER_MEM_RECORD_ADD:
2971 case HAMMER_MEM_RECORD_DEL:
2981 * Set dirty if we had to modify the link count.
2983 if (ip->sync_ino_data.nlinks != nlinks) {
2984 KKASSERT((int64_t)nlinks >= 0);
2985 ip->sync_ino_data.nlinks = nlinks;
2986 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2990 * If there is a trunction queued destroy any data past the (aligned)
2991 * truncation point. Userland will have dealt with the buffer
2992 * containing the truncation point for us.
2994 * We don't flush pending frontend data buffers until after we've
2995 * dealt with the truncation.
2997 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2999 * Interlock trunc_off. The VOP front-end may continue to
3000 * make adjustments to it while we are blocked.
3003 off_t aligned_trunc_off;
3006 trunc_off = ip->sync_trunc_off;
3007 blkmask = hammer_blocksize(trunc_off) - 1;
3008 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
3011 * Delete any whole blocks on-media. The front-end has
3012 * already cleaned out any partial block and made it
3013 * pending. The front-end may have updated trunc_off
3014 * while we were blocked so we only use sync_trunc_off.
3016 * This operation can blow out the buffer cache, EWOULDBLOCK
3017 * means we were unable to complete the deletion. The
3018 * deletion will update sync_trunc_off in that case.
3020 error = hammer_ip_delete_range(&cursor, ip,
3022 0x7FFFFFFFFFFFFFFFLL, 2);
3023 if (error == EWOULDBLOCK) {
3024 ip->flags |= HAMMER_INODE_WOULDBLOCK;
3026 goto defer_buffer_flush;
3033 * Generate a REDO_TERM_TRUNC entry in the UNDO/REDO FIFO.
3035 * XXX we do this even if we did not previously generate
3036 * a REDO_TRUNC record. This operation may enclosed the
3037 * range for multiple prior truncation entries in the REDO
3040 if (trans->hmp->version >= HAMMER_VOL_VERSION_FOUR &&
3041 (ip->flags & HAMMER_INODE_RDIRTY)) {
3042 hammer_generate_redo(trans, ip, aligned_trunc_off,
3043 HAMMER_REDO_TERM_TRUNC,
3048 * Clear the truncation flag on the backend after we have
3049 * completed the deletions. Backend data is now good again
3050 * (including new records we are about to sync, below).
3052 * Leave sync_trunc_off intact. As we write additional
3053 * records the backend will update sync_trunc_off. This
3054 * tells the backend whether it can skip the overwrite
3055 * test. This should work properly even when the backend
3056 * writes full blocks where the truncation point straddles
3057 * the block because the comparison is against the base
3058 * offset of the record.
3060 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
3061 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
3067 * Now sync related records. These will typically be directory
3068 * entries, records tracking direct-writes, or delete-on-disk records.
3071 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
3072 hammer_sync_record_callback, &cursor);
3078 hammer_cache_node(&ip->cache[1], cursor.node);
3081 * Re-seek for inode update, assuming our cache hasn't been ripped
3082 * out from under us.
3085 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
3087 hammer_cursor_downgrade(&cursor);
3088 hammer_lock_sh(&tmp_node->lock);
3089 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
3090 hammer_cursor_seek(&cursor, tmp_node, 0);
3091 hammer_unlock(&tmp_node->lock);
3092 hammer_rel_node(tmp_node);
3098 * If we are deleting the inode the frontend had better not have
3099 * any active references on elements making up the inode.
3101 * The call to hammer_ip_delete_clean() cleans up auxillary records
3102 * but not DB or DATA records. Those must have already been deleted
3103 * by the normal truncation mechanic.
3105 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
3106 RB_EMPTY(&ip->rec_tree) &&
3107 (ip->sync_flags & HAMMER_INODE_DELETING) &&
3108 (ip->flags & HAMMER_INODE_DELETED) == 0) {
3111 error = hammer_ip_delete_clean(&cursor, ip, &count1);
3113 ip->flags |= HAMMER_INODE_DELETED;
3114 ip->sync_flags &= ~HAMMER_INODE_DELETING;
3115 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
3116 KKASSERT(RB_EMPTY(&ip->rec_tree));
3119 * Set delete_tid in both the frontend and backend
3120 * copy of the inode record. The DELETED flag handles
3121 * this, do not set DDIRTY.
3123 ip->ino_leaf.base.delete_tid = trans->tid;
3124 ip->sync_ino_leaf.base.delete_tid = trans->tid;
3125 ip->ino_leaf.delete_ts = trans->time32;
3126 ip->sync_ino_leaf.delete_ts = trans->time32;
3130 * Adjust the inode count in the volume header
3132 hammer_sync_lock_sh(trans);
3133 if (ip->flags & HAMMER_INODE_ONDISK) {
3134 hammer_modify_volume_field(trans,
3137 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
3138 hammer_modify_volume_done(trans->rootvol);
3140 hammer_sync_unlock(trans);
3146 ip->sync_flags &= ~HAMMER_INODE_BUFS;
3150 * Now update the inode's on-disk inode-data and/or on-disk record.
3151 * DELETED and ONDISK are managed only in ip->flags.
3153 * In the case of a defered buffer flush we still update the on-disk
3154 * inode to satisfy visibility requirements if there happen to be
3155 * directory dependancies.
3157 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
3158 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
3160 * If deleted and on-disk, don't set any additional flags.
3161 * the delete flag takes care of things.
3163 * Clear flags which may have been set by the frontend.
3165 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
3166 HAMMER_INODE_SDIRTY |
3167 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
3168 HAMMER_INODE_DELETING);
3170 case HAMMER_INODE_DELETED:
3172 * Take care of the case where a deleted inode was never
3173 * flushed to the disk in the first place.
3175 * Clear flags which may have been set by the frontend.
3177 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
3178 HAMMER_INODE_SDIRTY |
3179 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
3180 HAMMER_INODE_DELETING);
3181 while (RB_ROOT(&ip->rec_tree)) {
3182 hammer_record_t record = RB_ROOT(&ip->rec_tree);
3183 hammer_ref(&record->lock);
3184 KKASSERT(hammer_oneref(&record->lock));
3185 record->flags |= HAMMER_RECF_DELETED_BE;
3186 ++record->ip->rec_generation;
3187 hammer_rel_mem_record(record);
3190 case HAMMER_INODE_ONDISK:
3192 * If already on-disk, do not set any additional flags.
3197 * If not on-disk and not deleted, set DDIRTY to force
3198 * an initial record to be written.
3200 * Also set the create_tid in both the frontend and backend
3201 * copy of the inode record.
3203 ip->ino_leaf.base.create_tid = trans->tid;
3204 ip->ino_leaf.create_ts = trans->time32;
3205 ip->sync_ino_leaf.base.create_tid = trans->tid;
3206 ip->sync_ino_leaf.create_ts = trans->time32;
3207 ip->sync_flags |= HAMMER_INODE_DDIRTY;
3212 * If DDIRTY or SDIRTY is set, write out a new record.
3213 * If the inode is already on-disk the old record is marked as
3216 * If DELETED is set hammer_update_inode() will delete the existing
3217 * record without writing out a new one.
3219 if (ip->flags & HAMMER_INODE_DELETED) {
3220 error = hammer_update_inode(&cursor, ip);
3222 if (!(ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY)) &&
3223 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
3224 error = hammer_update_itimes(&cursor, ip);
3226 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY |
3227 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
3228 error = hammer_update_inode(&cursor, ip);
3231 if (ip->flags & HAMMER_INODE_MODMASK)
3232 hammer_inode_dirty(ip);
3234 hammer_critical_error(ip->hmp, ip, error,
3235 "while syncing inode");
3237 hammer_done_cursor(&cursor);
3242 * This routine is called when the OS is no longer actively referencing
3243 * the inode (but might still be keeping it cached), or when releasing
3244 * the last reference to an inode.
3246 * At this point if the inode's nlinks count is zero we want to destroy
3247 * it, which may mean destroying it on-media too.
3250 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
3255 * Set the DELETING flag when the link count drops to 0 and the
3256 * OS no longer has any opens on the inode.
3258 * The backend will clear DELETING (a mod flag) and set DELETED
3259 * (a state flag) when it is actually able to perform the
3262 * Don't reflag the deletion if the flusher is currently syncing
3263 * one that was already flagged. A previously set DELETING flag
3264 * may bounce around flags and sync_flags until the operation is
3267 * Do not attempt to modify a snapshot inode (one set to read-only).
3269 if (ip->ino_data.nlinks == 0 &&
3270 ((ip->flags | ip->sync_flags) & (HAMMER_INODE_RO|HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
3271 ip->flags |= HAMMER_INODE_DELETING;
3272 ip->flags |= HAMMER_INODE_TRUNCATED;
3276 if (hammer_get_vnode(ip, &vp) != 0)
3284 nvtruncbuf(ip->vp, 0, HAMMER_BUFSIZE, 0, 0);
3285 if (ip->flags & HAMMER_INODE_MODMASK)
3286 hammer_inode_dirty(ip);
3293 * After potentially resolving a dependancy the inode is tested
3294 * to determine whether it needs to be reflushed.
3297 hammer_test_inode(hammer_inode_t ip)
3299 if (ip->flags & HAMMER_INODE_REFLUSH) {
3300 ip->flags &= ~HAMMER_INODE_REFLUSH;
3301 hammer_ref(&ip->lock);
3302 if (ip->flags & HAMMER_INODE_RESIGNAL) {
3303 ip->flags &= ~HAMMER_INODE_RESIGNAL;
3304 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
3306 hammer_flush_inode(ip, 0);
3308 hammer_rel_inode(ip, 0);
3313 * Clear the RECLAIM flag on an inode. This occurs when the inode is
3314 * reassociated with a vp or just before it gets freed.
3316 * Pipeline wakeups to threads blocked due to an excessive number of
3317 * detached inodes. This typically occurs when atime updates accumulate
3318 * while scanning a directory tree.
3321 hammer_inode_wakereclaims(hammer_inode_t ip)
3323 struct hammer_reclaim *reclaim;
3324 hammer_mount_t hmp = ip->hmp;
3326 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
3329 --hammer_count_reclaims;
3330 --hmp->count_reclaims;
3331 ip->flags &= ~HAMMER_INODE_RECLAIM;
3333 if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
3334 KKASSERT(reclaim->count > 0);
3335 if (--reclaim->count == 0) {
3336 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
3343 * Setup our reclaim pipeline. We only let so many detached (and dirty)
3344 * inodes build up before we start blocking. This routine is called
3345 * if a new inode is created or an inode is loaded from media.
3347 * When we block we don't care *which* inode has finished reclaiming,
3348 * as long as one does.
3350 * The reclaim pipeline is primarily governed by the auto-flush which is
3351 * 1/4 hammer_limit_reclaims. We don't want to block if the count is
3352 * less than 1/2 hammer_limit_reclaims. From 1/2 to full count is
3353 * dynamically governed.
3356 hammer_inode_waitreclaims(hammer_transaction_t trans)
3358 hammer_mount_t hmp = trans->hmp;
3359 struct hammer_reclaim reclaim;
3363 * Track inode load, delay if the number of reclaiming inodes is
3364 * between 2/4 and 4/4 hammer_limit_reclaims, depending.
3366 if (curthread->td_proc) {
3367 struct hammer_inostats *stats;
3369 stats = hammer_inode_inostats(hmp, curthread->td_proc->p_pid);
3372 if (stats->count > hammer_limit_reclaims / 2)
3373 stats->count = hammer_limit_reclaims / 2;
3374 lower_limit = hammer_limit_reclaims - stats->count;
3375 if (hammer_debug_general & 0x10000) {
3376 hdkprintf("pid %5d limit %d\n",
3377 (int)curthread->td_proc->p_pid, lower_limit);
3380 lower_limit = hammer_limit_reclaims * 3 / 4;
3382 if (hmp->count_reclaims >= lower_limit) {
3384 TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
3385 tsleep(&reclaim, 0, "hmrrcm", hz);
3386 if (reclaim.count > 0)
3387 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
3392 * Keep track of reclaim statistics on a per-pid basis using a loose
3393 * 4-way set associative hash table. Collisions inherit the count of
3394 * the previous entry.
3396 * NOTE: We want to be careful here to limit the chain size. If the chain
3397 * size is too large a pid will spread its stats out over too many
3398 * entries under certain types of heavy filesystem activity and
3399 * wind up not delaying long enough.
3402 struct hammer_inostats *
3403 hammer_inode_inostats(hammer_mount_t hmp, pid_t pid)
3405 struct hammer_inostats *stats;
3408 static volatile int iterator; /* we don't care about MP races */
3411 * Chain up to 4 times to find our entry.
3413 for (chain = 0; chain < 4; ++chain) {
3414 stats = &hmp->inostats[(pid + chain) & HAMMER_INOSTATS_HMASK];
3415 if (stats->pid == pid)
3420 * Replace one of the four chaining entries with our new entry.
3423 stats = &hmp->inostats[(pid + (iterator++ & 3)) &
3424 HAMMER_INOSTATS_HMASK];
3431 if (stats->count && stats->ltick != ticks) {
3432 delta = ticks - stats->ltick;
3433 stats->ltick = ticks;
3434 if (delta <= 0 || delta > hz * 60)
3437 stats->count = stats->count * hz / (hz + delta);
3439 if (hammer_debug_general & 0x10000)
3440 hdkprintf("pid %5d stats %d\n", (int)pid, stats->count);
3447 * XXX not used, doesn't work very well due to the large batching nature
3450 * A larger then normal backlog of inodes is sitting in the flusher,
3451 * enforce a general slowdown to let it catch up. This routine is only
3452 * called on completion of a non-flusher-related transaction which
3453 * performed B-Tree node I/O.
3455 * It is possible for the flusher to stall in a continuous load.
3456 * blogbench -i1000 -o seems to do a good job generating this sort of load.
3457 * If the flusher is unable to catch up the inode count can bloat until
3458 * we run out of kvm.
3460 * This is a bit of a hack.
3463 hammer_inode_waithard(hammer_mount_t hmp)
3468 if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
3469 if (hmp->count_reclaims < hammer_limit_reclaims / 2 &&
3470 hmp->count_iqueued < hmp->count_inodes / 20) {
3471 hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
3475 if (hmp->count_reclaims < hammer_limit_reclaims ||
3476 hmp->count_iqueued < hmp->count_inodes / 10) {
3479 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3483 * Block for one flush cycle.
3485 hammer_flusher_wait_next(hmp);