2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.70 2008/06/10 22:30:21 dillon Exp $
38 #include <vm/vm_extern.h>
42 static int hammer_unload_inode(struct hammer_inode *ip);
43 static void hammer_flush_inode_core(hammer_inode_t ip, int flags);
44 static int hammer_setup_child_callback(hammer_record_t rec, void *data);
45 static int hammer_setup_parent_inodes(hammer_record_t record);
46 static void hammer_inode_wakereclaims(hammer_mount_t hmp);
49 extern struct hammer_inode *HammerTruncIp;
53 * The kernel is not actively referencing this vnode but is still holding
56 * This is called from the frontend.
59 hammer_vop_inactive(struct vop_inactive_args *ap)
61 struct hammer_inode *ip = VTOI(ap->a_vp);
72 * If the inode no longer has visibility in the filesystem and is
73 * fairly clean, try to recycle it immediately. This can deadlock
74 * in vfsync() if we aren't careful.
76 * Do not queue the inode to the flusher if we still have visibility,
77 * otherwise namespace calls such as chmod will unnecessarily generate
78 * multiple inode updates.
80 hammer_inode_unloadable_check(ip, 0);
81 if (ip->ino_data.nlinks == 0) {
82 if (ip->flags & HAMMER_INODE_MODMASK)
83 hammer_flush_inode(ip, 0);
91 * Release the vnode association. This is typically (but not always)
92 * the last reference on the inode.
94 * Once the association is lost we are on our own with regards to
98 hammer_vop_reclaim(struct vop_reclaim_args *ap)
101 struct hammer_inode *ip;
106 if ((ip = vp->v_data) != NULL) {
110 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
111 ++hammer_count_reclaiming;
112 ++hmp->inode_reclaims;
113 ip->flags |= HAMMER_INODE_RECLAIM;
115 hammer_inode_waitreclaims(hmp);
117 hammer_rel_inode(ip, 1);
123 * Return a locked vnode for the specified inode. The inode must be
124 * referenced but NOT LOCKED on entry and will remain referenced on
127 * Called from the frontend.
130 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
139 if ((vp = ip->vp) == NULL) {
140 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
143 hammer_lock_ex(&ip->lock);
144 if (ip->vp != NULL) {
145 hammer_unlock(&ip->lock);
150 hammer_ref(&ip->lock);
154 hammer_get_vnode_type(ip->ino_data.obj_type);
156 if (ip->flags & HAMMER_INODE_RECLAIM) {
157 --hammer_count_reclaiming;
158 --hmp->inode_reclaims;
159 ip->flags &= ~HAMMER_INODE_RECLAIM;
160 if (hmp->flags & HAMMER_MOUNT_WAITIMAX)
161 hammer_inode_wakereclaims(hmp);
164 switch(ip->ino_data.obj_type) {
165 case HAMMER_OBJTYPE_CDEV:
166 case HAMMER_OBJTYPE_BDEV:
167 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
168 addaliasu(vp, ip->ino_data.rmajor,
169 ip->ino_data.rminor);
171 case HAMMER_OBJTYPE_FIFO:
172 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
179 * Only mark as the root vnode if the ip is not
180 * historical, otherwise the VFS cache will get
181 * confused. The other half of the special handling
182 * is in hammer_vop_nlookupdotdot().
184 if (ip->obj_id == HAMMER_OBJID_ROOT &&
185 ip->obj_asof == hmp->asof) {
189 vp->v_data = (void *)ip;
190 /* vnode locked by getnewvnode() */
191 /* make related vnode dirty if inode dirty? */
192 hammer_unlock(&ip->lock);
193 if (vp->v_type == VREG)
194 vinitvmio(vp, ip->ino_data.size);
199 * loop if the vget fails (aka races), or if the vp
200 * no longer matches ip->vp.
202 if (vget(vp, LK_EXCLUSIVE) == 0) {
213 * Acquire a HAMMER inode. The returned inode is not locked. These functions
214 * do not attach or detach the related vnode (use hammer_get_vnode() for
217 * The flags argument is only applied for newly created inodes, and only
218 * certain flags are inherited.
220 * Called from the frontend.
222 struct hammer_inode *
223 hammer_get_inode(hammer_transaction_t trans, struct hammer_node **cache,
224 u_int64_t obj_id, hammer_tid_t asof, int flags, int *errorp)
226 hammer_mount_t hmp = trans->hmp;
227 struct hammer_inode_info iinfo;
228 struct hammer_cursor cursor;
229 struct hammer_inode *ip;
232 * Determine if we already have an inode cached. If we do then
235 iinfo.obj_id = obj_id;
236 iinfo.obj_asof = asof;
238 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
240 hammer_ref(&ip->lock);
246 * Allocate a new inode structure and deal with races later.
248 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
249 ++hammer_count_inodes;
252 ip->obj_asof = iinfo.obj_asof;
254 ip->flags = flags & HAMMER_INODE_RO;
256 ip->flags |= HAMMER_INODE_RO;
257 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
258 RB_INIT(&ip->rec_tree);
259 TAILQ_INIT(&ip->target_list);
262 * Locate the on-disk inode.
265 hammer_init_cursor(trans, &cursor, cache, NULL);
266 cursor.key_beg.localization = HAMMER_LOCALIZE_INODE;
267 cursor.key_beg.obj_id = ip->obj_id;
268 cursor.key_beg.key = 0;
269 cursor.key_beg.create_tid = 0;
270 cursor.key_beg.delete_tid = 0;
271 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
272 cursor.key_beg.obj_type = 0;
273 cursor.asof = iinfo.obj_asof;
274 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
277 *errorp = hammer_btree_lookup(&cursor);
278 if (*errorp == EDEADLK) {
279 hammer_done_cursor(&cursor);
284 * On success the B-Tree lookup will hold the appropriate
285 * buffer cache buffers and provide a pointer to the requested
286 * information. Copy the information to the in-memory inode
287 * and cache the B-Tree node to improve future operations.
290 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
291 ip->ino_data = cursor.data->inode;
292 hammer_cache_node(cursor.node, &ip->cache[0]);
294 hammer_cache_node(cursor.node, cache);
298 * On success load the inode's record and data and insert the
299 * inode into the B-Tree. It is possible to race another lookup
300 * insertion of the same inode so deal with that condition too.
302 * The cursor's locked node interlocks against others creating and
303 * destroying ip while we were blocked.
306 hammer_ref(&ip->lock);
307 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
308 hammer_uncache_node(&ip->cache[0]);
309 hammer_uncache_node(&ip->cache[1]);
310 KKASSERT(ip->lock.refs == 1);
311 --hammer_count_inodes;
314 hammer_done_cursor(&cursor);
317 ip->flags |= HAMMER_INODE_ONDISK;
320 * Do not panic on read-only accesses which fail, particularly
321 * historical accesses where the snapshot might not have
322 * complete connectivity.
324 if ((flags & HAMMER_INODE_RO) == 0) {
325 kprintf("hammer_get_inode: failed ip %p obj_id %016llx cursor %p error %d\n",
326 ip, ip->obj_id, &cursor, *errorp);
329 if (ip->flags & HAMMER_INODE_RSV_INODES) {
330 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
333 hmp->rsv_databufs -= ip->rsv_databufs;
334 ip->rsv_databufs = 0; /* sanity */
336 --hammer_count_inodes;
341 hammer_done_cursor(&cursor);
346 * Create a new filesystem object, returning the inode in *ipp. The
347 * returned inode will be referenced.
349 * The inode is created in-memory.
352 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
353 struct ucred *cred, hammer_inode_t dip,
354 struct hammer_inode **ipp)
361 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
362 ++hammer_count_inodes;
364 ip->obj_id = hammer_alloc_objid(trans, dip);
365 KKASSERT(ip->obj_id != 0);
366 ip->obj_asof = hmp->asof;
368 ip->flush_state = HAMMER_FST_IDLE;
369 ip->flags = HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES;
371 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
372 RB_INIT(&ip->rec_tree);
373 TAILQ_INIT(&ip->target_list);
375 ip->ino_leaf.atime = trans->time;
376 ip->ino_data.mtime = trans->time;
377 ip->ino_data.size = 0;
378 ip->ino_data.nlinks = 0;
381 * A nohistory designator on the parent directory is inherited by
384 ip->ino_data.uflags = dip->ino_data.uflags &
385 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
387 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
388 ip->ino_leaf.base.localization = HAMMER_LOCALIZE_INODE;
389 ip->ino_leaf.base.obj_id = ip->obj_id;
390 ip->ino_leaf.base.key = 0;
391 ip->ino_leaf.base.create_tid = 0;
392 ip->ino_leaf.base.delete_tid = 0;
393 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
394 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
396 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
397 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
398 ip->ino_data.mode = vap->va_mode;
399 ip->ino_data.ctime = trans->time;
400 ip->ino_data.parent_obj_id = (dip) ? dip->ino_leaf.base.obj_id : 0;
402 switch(ip->ino_leaf.base.obj_type) {
403 case HAMMER_OBJTYPE_CDEV:
404 case HAMMER_OBJTYPE_BDEV:
405 ip->ino_data.rmajor = vap->va_rmajor;
406 ip->ino_data.rminor = vap->va_rminor;
413 * Calculate default uid/gid and overwrite with information from
416 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
417 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
419 ip->ino_data.mode = vap->va_mode;
421 if (vap->va_vaflags & VA_UID_UUID_VALID)
422 ip->ino_data.uid = vap->va_uid_uuid;
423 else if (vap->va_uid != (uid_t)VNOVAL)
424 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
426 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
428 if (vap->va_vaflags & VA_GID_UUID_VALID)
429 ip->ino_data.gid = vap->va_gid_uuid;
430 else if (vap->va_gid != (gid_t)VNOVAL)
431 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
433 ip->ino_data.gid = dip->ino_data.gid;
435 hammer_ref(&ip->lock);
436 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
437 hammer_unref(&ip->lock);
438 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
445 * Called by hammer_sync_inode().
448 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
450 hammer_transaction_t trans = cursor->trans;
451 hammer_record_t record;
458 * If the inode has a presence on-disk then locate it and mark
459 * it deleted, setting DELONDISK.
461 * The record may or may not be physically deleted, depending on
462 * the retention policy.
464 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
465 HAMMER_INODE_ONDISK) {
466 hammer_normalize_cursor(cursor);
467 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
468 cursor->key_beg.obj_id = ip->obj_id;
469 cursor->key_beg.key = 0;
470 cursor->key_beg.create_tid = 0;
471 cursor->key_beg.delete_tid = 0;
472 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
473 cursor->key_beg.obj_type = 0;
474 cursor->asof = ip->obj_asof;
475 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
476 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
477 cursor->flags |= HAMMER_CURSOR_BACKEND;
479 error = hammer_btree_lookup(cursor);
480 if (hammer_debug_inode)
481 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
483 kprintf("error %d\n", error);
484 Debugger("hammer_update_inode");
488 error = hammer_ip_delete_record(cursor, ip, trans->tid);
489 if (hammer_debug_inode)
490 kprintf(" error %d\n", error);
491 if (error && error != EDEADLK) {
492 kprintf("error %d\n", error);
493 Debugger("hammer_update_inode2");
496 ip->flags |= HAMMER_INODE_DELONDISK;
499 hammer_cache_node(cursor->node, &ip->cache[0]);
501 if (error == EDEADLK) {
502 hammer_done_cursor(cursor);
503 error = hammer_init_cursor(trans, cursor,
505 if (hammer_debug_inode)
506 kprintf("IPDED %p %d\n", ip, error);
513 * Ok, write out the initial record or a new record (after deleting
514 * the old one), unless the DELETED flag is set. This routine will
515 * clear DELONDISK if it writes out a record.
517 * Update our inode statistics if this is the first application of
520 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
522 * Generate a record and write it to the media
524 record = hammer_alloc_mem_record(ip, 0);
525 record->type = HAMMER_MEM_RECORD_INODE;
526 record->flush_state = HAMMER_FST_FLUSH;
527 record->leaf = ip->sync_ino_leaf;
528 record->leaf.base.create_tid = trans->tid;
529 record->leaf.data_len = sizeof(ip->sync_ino_data);
530 record->data = (void *)&ip->sync_ino_data;
531 record->flags |= HAMMER_RECF_INTERLOCK_BE;
533 error = hammer_ip_sync_record_cursor(cursor, record);
534 if (hammer_debug_inode)
535 kprintf("GENREC %p rec %08x %d\n",
536 ip, record->flags, error);
537 if (error != EDEADLK)
539 hammer_done_cursor(cursor);
540 error = hammer_init_cursor(trans, cursor,
542 if (hammer_debug_inode)
543 kprintf("GENREC reinit %d\n", error);
548 kprintf("error %d\n", error);
549 Debugger("hammer_update_inode3");
553 * The record isn't managed by the inode's record tree,
554 * destroy it whether we succeed or fail.
556 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
557 record->flags |= HAMMER_RECF_DELETED_FE;
558 record->flush_state = HAMMER_FST_IDLE;
559 hammer_rel_mem_record(record);
565 if (hammer_debug_inode)
566 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
567 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
568 HAMMER_INODE_ITIMES);
569 ip->flags &= ~HAMMER_INODE_DELONDISK;
572 * Root volume count of inodes
574 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
575 hammer_modify_volume_field(trans,
578 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
579 hammer_modify_volume_done(trans->rootvol);
580 ip->flags |= HAMMER_INODE_ONDISK;
581 if (hammer_debug_inode)
582 kprintf("NOWONDISK %p\n", ip);
588 * If the inode has been destroyed, clean out any left-over flags
589 * that may have been set by the frontend.
591 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
592 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
593 HAMMER_INODE_ITIMES);
599 * Update only the itimes fields. This is done no-historically. The
600 * record is updated in-place on the disk.
603 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
605 hammer_transaction_t trans = cursor->trans;
606 struct hammer_btree_leaf_elm *leaf;
611 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
612 HAMMER_INODE_ONDISK) {
613 hammer_normalize_cursor(cursor);
614 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
615 cursor->key_beg.obj_id = ip->obj_id;
616 cursor->key_beg.key = 0;
617 cursor->key_beg.create_tid = 0;
618 cursor->key_beg.delete_tid = 0;
619 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
620 cursor->key_beg.obj_type = 0;
621 cursor->asof = ip->obj_asof;
622 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
623 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
624 cursor->flags |= HAMMER_CURSOR_BACKEND;
626 error = hammer_btree_lookup(cursor);
628 kprintf("error %d\n", error);
629 Debugger("hammer_update_itimes1");
633 * Do not generate UNDO records for atime updates.
636 hammer_modify_node(trans, cursor->node,
637 &leaf->atime, sizeof(leaf->atime));
638 leaf->atime = ip->sync_ino_leaf.atime;
639 hammer_modify_node_done(cursor->node);
640 /*rec->ino_mtime = ip->sync_ino_rec.ino_mtime;*/
641 ip->sync_flags &= ~HAMMER_INODE_ITIMES;
642 /* XXX recalculate crc */
643 hammer_cache_node(cursor->node, &ip->cache[0]);
645 if (error == EDEADLK) {
646 hammer_done_cursor(cursor);
647 error = hammer_init_cursor(trans, cursor,
657 * Release a reference on an inode, flush as requested.
659 * On the last reference we queue the inode to the flusher for its final
663 hammer_rel_inode(struct hammer_inode *ip, int flush)
665 hammer_mount_t hmp = ip->hmp;
668 * Handle disposition when dropping the last ref.
671 if (ip->lock.refs == 1) {
673 * Determine whether on-disk action is needed for
674 * the inode's final disposition.
676 KKASSERT(ip->vp == NULL);
677 hammer_inode_unloadable_check(ip, 0);
678 if (ip->flags & HAMMER_INODE_MODMASK) {
679 if (hmp->rsv_inodes > desiredvnodes) {
680 hammer_flush_inode(ip,
681 HAMMER_FLUSH_SIGNAL);
683 hammer_flush_inode(ip, 0);
685 } else if (ip->lock.refs == 1) {
686 hammer_unload_inode(ip);
691 hammer_flush_inode(ip, 0);
694 * The inode still has multiple refs, try to drop
697 KKASSERT(ip->lock.refs >= 1);
698 if (ip->lock.refs > 1) {
699 hammer_unref(&ip->lock);
707 * Unload and destroy the specified inode. Must be called with one remaining
708 * reference. The reference is disposed of.
710 * This can only be called in the context of the flusher.
713 hammer_unload_inode(struct hammer_inode *ip)
715 hammer_mount_t hmp = ip->hmp;
717 KASSERT(ip->lock.refs == 1,
718 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
719 KKASSERT(ip->vp == NULL);
720 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
721 KKASSERT(ip->cursor_ip_refs == 0);
722 KKASSERT(ip->lock.lockcount == 0);
723 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
725 KKASSERT(RB_EMPTY(&ip->rec_tree));
726 KKASSERT(TAILQ_EMPTY(&ip->target_list));
728 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
730 hammer_uncache_node(&ip->cache[0]);
731 hammer_uncache_node(&ip->cache[1]);
733 hammer_clear_objid(ip);
734 --hammer_count_inodes;
737 if (ip->flags & HAMMER_INODE_RECLAIM) {
738 --hammer_count_reclaiming;
739 --hmp->inode_reclaims;
740 ip->flags &= ~HAMMER_INODE_RECLAIM;
741 if (hmp->flags & HAMMER_MOUNT_WAITIMAX)
742 hammer_inode_wakereclaims(hmp);
750 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
751 * the read-only flag for cached inodes.
753 * This routine is called from a RB_SCAN().
756 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
758 hammer_mount_t hmp = ip->hmp;
760 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
761 ip->flags |= HAMMER_INODE_RO;
763 ip->flags &= ~HAMMER_INODE_RO;
768 * A transaction has modified an inode, requiring updates as specified by
771 * HAMMER_INODE_DDIRTY: Inode data has been updated
772 * HAMMER_INODE_XDIRTY: Dirty in-memory records
773 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
774 * HAMMER_INODE_DELETED: Inode record/data must be deleted
775 * HAMMER_INODE_ITIMES: mtime/atime has been updated
778 hammer_modify_inode(hammer_inode_t ip, int flags)
780 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
781 (flags & (HAMMER_INODE_DDIRTY |
782 HAMMER_INODE_XDIRTY | HAMMER_INODE_BUFS |
783 HAMMER_INODE_DELETED | HAMMER_INODE_ITIMES)) == 0);
784 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
785 ip->flags |= HAMMER_INODE_RSV_INODES;
786 ++ip->hmp->rsv_inodes;
793 * Request that an inode be flushed. This whole mess cannot block and may
794 * recurse. Once requested HAMMER will attempt to actively flush it until
795 * the flush can be done.
797 * The inode may already be flushing, or may be in a setup state. We can
798 * place the inode in a flushing state if it is currently idle and flag it
799 * to reflush if it is currently flushing.
802 hammer_flush_inode(hammer_inode_t ip, int flags)
804 hammer_record_t depend;
808 * Trivial 'nothing to flush' case. If the inode is ina SETUP
809 * state we have to put it back into an IDLE state so we can
810 * drop the extra ref.
812 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
813 if (ip->flush_state == HAMMER_FST_SETUP) {
814 ip->flush_state = HAMMER_FST_IDLE;
815 hammer_rel_inode(ip, 0);
821 * Our flush action will depend on the current state.
823 switch(ip->flush_state) {
824 case HAMMER_FST_IDLE:
826 * We have no dependancies and can flush immediately. Some
827 * our children may not be flushable so we have to re-test
828 * with that additional knowledge.
830 hammer_flush_inode_core(ip, flags);
832 case HAMMER_FST_SETUP:
834 * Recurse upwards through dependancies via target_list
835 * and start their flusher actions going if possible.
837 * 'good' is our connectivity. -1 means we have none and
838 * can't flush, 0 means there weren't any dependancies, and
839 * 1 means we have good connectivity.
842 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
843 r = hammer_setup_parent_inodes(depend);
844 if (r < 0 && good == 0)
851 * We can continue if good >= 0. Determine how many records
852 * under our inode can be flushed (and mark them).
855 hammer_flush_inode_core(ip, flags);
857 ip->flags |= HAMMER_INODE_REFLUSH;
858 if (flags & HAMMER_FLUSH_SIGNAL) {
859 ip->flags |= HAMMER_INODE_RESIGNAL;
860 hammer_flusher_async(ip->hmp);
866 * We are already flushing, flag the inode to reflush
867 * if needed after it completes its current flush.
869 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
870 ip->flags |= HAMMER_INODE_REFLUSH;
871 if (flags & HAMMER_FLUSH_SIGNAL) {
872 ip->flags |= HAMMER_INODE_RESIGNAL;
873 hammer_flusher_async(ip->hmp);
880 * We are asked to recurse upwards and convert the record from SETUP
881 * to FLUSH if possible. record->ip is a parent of the caller's inode,
882 * and record->target_ip is the caller's inode.
884 * Return 1 if the record gives us connectivity
886 * Return 0 if the record is not relevant
888 * Return -1 if we can't resolve the dependancy and there is no connectivity.
891 hammer_setup_parent_inodes(hammer_record_t record)
893 hammer_mount_t hmp = record->ip->hmp;
894 hammer_record_t depend;
898 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
902 * If the record is already flushing, is it in our flush group?
904 * If it is in our flush group but it is a general record or a
905 * delete-on-disk, it does not improve our connectivity (return 0),
906 * and if the target inode is not trying to destroy itself we can't
907 * allow the operation yet anyway (the second return -1).
909 if (record->flush_state == HAMMER_FST_FLUSH) {
910 if (record->flush_group != hmp->flusher.next) {
911 ip->flags |= HAMMER_INODE_REFLUSH;
914 if (record->type == HAMMER_MEM_RECORD_ADD)
921 * It must be a setup record. Try to resolve the setup dependancies
922 * by recursing upwards so we can place ip on the flush list.
924 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
927 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
928 r = hammer_setup_parent_inodes(depend);
929 if (r < 0 && good == 0)
936 * We can't flush ip because it has no connectivity (XXX also check
937 * nlinks for pre-existing connectivity!). Flag it so any resolution
938 * recurses back down.
941 ip->flags |= HAMMER_INODE_REFLUSH;
946 * We are go, place the parent inode in a flushing state so we can
947 * place its record in a flushing state. Note that the parent
948 * may already be flushing. The record must be in the same flush
949 * group as the parent.
951 if (ip->flush_state != HAMMER_FST_FLUSH)
952 hammer_flush_inode_core(ip, HAMMER_FLUSH_RECURSION);
953 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
954 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
957 if (record->type == HAMMER_MEM_RECORD_DEL &&
958 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
960 * Regardless of flushing state we cannot sync this path if the
961 * record represents a delete-on-disk but the target inode
962 * is not ready to sync its own deletion.
964 * XXX need to count effective nlinks to determine whether
965 * the flush is ok, otherwise removing a hardlink will
966 * just leave the DEL record to rot.
968 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
972 if (ip->flush_group == ip->hmp->flusher.next) {
974 * This is the record we wanted to synchronize.
976 record->flush_state = HAMMER_FST_FLUSH;
977 record->flush_group = ip->flush_group;
978 hammer_ref(&record->lock);
979 if (record->type == HAMMER_MEM_RECORD_ADD)
983 * A general or delete-on-disk record does not contribute
984 * to our visibility. We can still flush it, however.
989 * We couldn't resolve the dependancies, request that the
990 * inode be flushed when the dependancies can be resolved.
992 ip->flags |= HAMMER_INODE_REFLUSH;
998 * This is the core routine placing an inode into the FST_FLUSH state.
1001 hammer_flush_inode_core(hammer_inode_t ip, int flags)
1006 * Set flush state and prevent the flusher from cycling into
1007 * the next flush group. Do not place the ip on the list yet.
1008 * Inodes not in the idle state get an extra reference.
1010 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1011 if (ip->flush_state == HAMMER_FST_IDLE)
1012 hammer_ref(&ip->lock);
1013 ip->flush_state = HAMMER_FST_FLUSH;
1014 ip->flush_group = ip->hmp->flusher.next;
1015 ++ip->hmp->flusher.group_lock;
1016 ++ip->hmp->count_iqueued;
1017 ++hammer_count_iqueued;
1020 * We need to be able to vfsync/truncate from the backend.
1022 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1023 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1024 ip->flags |= HAMMER_INODE_VHELD;
1029 * Figure out how many in-memory records we can actually flush
1030 * (not including inode meta-data, buffers, etc).
1032 if (flags & HAMMER_FLUSH_RECURSION) {
1035 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1036 hammer_setup_child_callback, NULL);
1040 * This is a more involved test that includes go_count. If we
1041 * can't flush, flag the inode and return. If go_count is 0 we
1042 * were are unable to flush any records in our rec_tree and
1043 * must ignore the XDIRTY flag.
1045 if (go_count == 0) {
1046 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1047 ip->flags |= HAMMER_INODE_REFLUSH;
1049 --ip->hmp->count_iqueued;
1050 --hammer_count_iqueued;
1052 ip->flush_state = HAMMER_FST_SETUP;
1053 if (ip->flags & HAMMER_INODE_VHELD) {
1054 ip->flags &= ~HAMMER_INODE_VHELD;
1057 if (flags & HAMMER_FLUSH_SIGNAL) {
1058 ip->flags |= HAMMER_INODE_RESIGNAL;
1059 hammer_flusher_async(ip->hmp);
1061 if (--ip->hmp->flusher.group_lock == 0)
1062 wakeup(&ip->hmp->flusher.group_lock);
1068 * Snapshot the state of the inode for the backend flusher.
1070 * The truncation must be retained in the frontend until after
1071 * we've actually performed the record deletion.
1073 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1074 * and stays in ip->flags. Once set, it stays set until the
1075 * inode is destroyed.
1077 ip->sync_flags = (ip->flags & HAMMER_INODE_MODMASK);
1078 ip->sync_trunc_off = ip->trunc_off;
1079 ip->sync_ino_leaf = ip->ino_leaf;
1080 ip->sync_ino_data = ip->ino_data;
1081 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1082 ip->flags &= ~HAMMER_INODE_MODMASK;
1083 #ifdef DEBUG_TRUNCATE
1084 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1085 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1089 * The flusher list inherits our inode and reference.
1091 TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
1092 if (--ip->hmp->flusher.group_lock == 0)
1093 wakeup(&ip->hmp->flusher.group_lock);
1095 if (flags & HAMMER_FLUSH_SIGNAL) {
1096 hammer_flusher_async(ip->hmp);
1101 * Callback for scan of ip->rec_tree. Try to include each record in our
1102 * flush. ip->flush_group has been set but the inode has not yet been
1103 * moved into a flushing state.
1105 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1108 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1109 * the caller from shortcutting the flush.
1112 hammer_setup_child_callback(hammer_record_t rec, void *data)
1114 hammer_inode_t target_ip;
1119 * If the record has been deleted by the backend (it's being held
1120 * by the frontend in a race), just ignore it.
1122 if (rec->flags & HAMMER_RECF_DELETED_BE)
1126 * If the record is in an idle state it has no dependancies and
1132 switch(rec->flush_state) {
1133 case HAMMER_FST_IDLE:
1135 * Record has no setup dependancy, we can flush it.
1137 KKASSERT(rec->target_ip == NULL);
1138 rec->flush_state = HAMMER_FST_FLUSH;
1139 rec->flush_group = ip->flush_group;
1140 hammer_ref(&rec->lock);
1143 case HAMMER_FST_SETUP:
1145 * Record has a setup dependancy. Try to include the
1146 * target ip in the flush.
1148 * We have to be careful here, if we do not do the right
1149 * thing we can lose track of dirty inodes and the system
1150 * will lockup trying to allocate buffers.
1152 target_ip = rec->target_ip;
1153 KKASSERT(target_ip != NULL);
1154 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1155 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1157 * If the target IP is already flushing in our group
1158 * we are golden, otherwise make sure the target
1161 if (target_ip->flush_group == ip->flush_group) {
1162 rec->flush_state = HAMMER_FST_FLUSH;
1163 rec->flush_group = ip->flush_group;
1164 hammer_ref(&rec->lock);
1167 target_ip->flags |= HAMMER_INODE_REFLUSH;
1169 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1171 * If the target IP is not flushing we can force
1172 * it to flush, even if it is unable to write out
1173 * any of its own records we have at least one in
1174 * hand that we CAN deal with.
1176 rec->flush_state = HAMMER_FST_FLUSH;
1177 rec->flush_group = ip->flush_group;
1178 hammer_ref(&rec->lock);
1179 hammer_flush_inode_core(target_ip,
1180 HAMMER_FLUSH_RECURSION);
1184 * General or delete-on-disk record.
1186 * XXX this needs help. If a delete-on-disk we could
1187 * disconnect the target. If the target has its own
1188 * dependancies they really need to be flushed.
1192 rec->flush_state = HAMMER_FST_FLUSH;
1193 rec->flush_group = ip->flush_group;
1194 hammer_ref(&rec->lock);
1195 hammer_flush_inode_core(target_ip,
1196 HAMMER_FLUSH_RECURSION);
1200 case HAMMER_FST_FLUSH:
1202 * Record already associated with a flush group. It had
1205 KKASSERT(rec->flush_group == ip->flush_group);
1213 * Wait for a previously queued flush to complete
1216 hammer_wait_inode(hammer_inode_t ip)
1218 while (ip->flush_state != HAMMER_FST_IDLE) {
1219 if (ip->flush_state == HAMMER_FST_SETUP) {
1220 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1222 ip->flags |= HAMMER_INODE_FLUSHW;
1223 tsleep(&ip->flags, 0, "hmrwin", 0);
1229 * Called by the backend code when a flush has been completed.
1230 * The inode has already been removed from the flush list.
1232 * A pipelined flush can occur, in which case we must re-enter the
1233 * inode on the list and re-copy its fields.
1236 hammer_flush_inode_done(hammer_inode_t ip)
1241 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1246 * Merge left-over flags back into the frontend and fix the state.
1248 ip->flags |= ip->sync_flags;
1251 * The backend may have adjusted nlinks, so if the adjusted nlinks
1252 * does not match the fronttend set the frontend's RDIRTY flag again.
1254 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
1255 ip->flags |= HAMMER_INODE_DDIRTY;
1258 * Fix up the dirty buffer status. IO completions will also
1259 * try to clean up rsv_databufs.
1261 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
1262 ip->flags |= HAMMER_INODE_BUFS;
1264 hmp->rsv_databufs -= ip->rsv_databufs;
1265 ip->rsv_databufs = 0;
1269 * Re-set the XDIRTY flag if some of the inode's in-memory records
1270 * could not be flushed.
1272 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
1273 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
1274 (!RB_EMPTY(&ip->rec_tree) &&
1275 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
1278 * Do not lose track of inodes which no longer have vnode
1279 * assocations, otherwise they may never get flushed again.
1281 if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
1282 ip->flags |= HAMMER_INODE_REFLUSH;
1285 * Adjust flush_state. The target state (idle or setup) shouldn't
1286 * be terribly important since we will reflush if we really need
1287 * to do anything. XXX
1289 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1290 ip->flush_state = HAMMER_FST_IDLE;
1293 ip->flush_state = HAMMER_FST_SETUP;
1297 --hmp->count_iqueued;
1298 --hammer_count_iqueued;
1301 * Clean up the vnode ref
1303 if (ip->flags & HAMMER_INODE_VHELD) {
1304 ip->flags &= ~HAMMER_INODE_VHELD;
1309 * If the frontend made more changes and requested another flush,
1310 * then try to get it running.
1312 if (ip->flags & HAMMER_INODE_REFLUSH) {
1313 ip->flags &= ~HAMMER_INODE_REFLUSH;
1314 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1315 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1316 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1318 hammer_flush_inode(ip, 0);
1323 * If the inode is now clean drop the space reservation.
1325 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1326 (ip->flags & HAMMER_INODE_RSV_INODES)) {
1327 ip->flags &= ~HAMMER_INODE_RSV_INODES;
1332 * Finally, if the frontend is waiting for a flush to complete,
1335 if (ip->flush_state != HAMMER_FST_FLUSH) {
1336 if (ip->flags & HAMMER_INODE_FLUSHW) {
1337 ip->flags &= ~HAMMER_INODE_FLUSHW;
1342 hammer_rel_inode(ip, 0);
1346 * Called from hammer_sync_inode() to synchronize in-memory records
1350 hammer_sync_record_callback(hammer_record_t record, void *data)
1352 hammer_cursor_t cursor = data;
1353 hammer_transaction_t trans = cursor->trans;
1357 * Skip records that do not belong to the current flush.
1359 ++hammer_stats_record_iterations;
1360 if (record->flush_state != HAMMER_FST_FLUSH)
1364 if (record->flush_group != record->ip->flush_group) {
1365 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1370 KKASSERT(record->flush_group == record->ip->flush_group);
1373 * Interlock the record using the BE flag. Once BE is set the
1374 * frontend cannot change the state of FE.
1376 * NOTE: If FE is set prior to us setting BE we still sync the
1377 * record out, but the flush completion code converts it to
1378 * a delete-on-disk record instead of destroying it.
1380 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
1381 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1384 * The backend may have already disposed of the record.
1386 if (record->flags & HAMMER_RECF_DELETED_BE) {
1392 * If the whole inode is being deleting all on-disk records will
1393 * be deleted very soon, we can't sync any new records to disk
1394 * because they will be deleted in the same transaction they were
1395 * created in (delete_tid == create_tid), which will assert.
1397 * XXX There may be a case with RECORD_ADD with DELETED_FE set
1398 * that we currently panic on.
1400 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
1401 switch(record->type) {
1402 case HAMMER_MEM_RECORD_DATA:
1404 * We don't have to do anything, if the record was
1405 * committed the space will have been accounted for
1409 case HAMMER_MEM_RECORD_GENERAL:
1410 record->flags |= HAMMER_RECF_DELETED_FE;
1411 record->flags |= HAMMER_RECF_DELETED_BE;
1414 case HAMMER_MEM_RECORD_ADD:
1415 panic("hammer_sync_record_callback: illegal add "
1416 "during inode deletion record %p", record);
1417 break; /* NOT REACHED */
1418 case HAMMER_MEM_RECORD_INODE:
1419 panic("hammer_sync_record_callback: attempt to "
1420 "sync inode record %p?", record);
1421 break; /* NOT REACHED */
1422 case HAMMER_MEM_RECORD_DEL:
1424 * Follow through and issue the on-disk deletion
1431 * If DELETED_FE is set we may have already sent dependant pieces
1432 * to the disk and we must flush the record as if it hadn't been
1433 * deleted. This creates a bit of a mess because we have to
1434 * have ip_sync_record convert the record to MEM_RECORD_DEL before
1435 * it inserts the B-Tree record. Otherwise the media sync might
1436 * be visible to the frontend.
1438 if (record->flags & HAMMER_RECF_DELETED_FE) {
1439 if (record->type == HAMMER_MEM_RECORD_ADD) {
1440 record->flags |= HAMMER_RECF_CONVERT_DELETE;
1442 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
1448 * Assign the create_tid for new records. Deletions already
1449 * have the record's entire key properly set up.
1451 if (record->type != HAMMER_MEM_RECORD_DEL)
1452 record->leaf.base.create_tid = trans->tid;
1454 error = hammer_ip_sync_record_cursor(cursor, record);
1455 if (error != EDEADLK)
1457 hammer_done_cursor(cursor);
1458 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
1463 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1467 if (error != -ENOSPC) {
1468 kprintf("hammer_sync_record_callback: sync failed rec "
1469 "%p, error %d\n", record, error);
1470 Debugger("sync failed rec");
1474 hammer_flush_record_done(record, error);
1479 * XXX error handling
1482 hammer_sync_inode(hammer_inode_t ip)
1484 struct hammer_transaction trans;
1485 struct hammer_cursor cursor;
1486 hammer_record_t depend;
1487 hammer_record_t next;
1488 int error, tmp_error;
1491 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
1494 hammer_start_transaction_fls(&trans, ip->hmp);
1495 error = hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
1500 * Any directory records referencing this inode which are not in
1501 * our current flush group must adjust our nlink count for the
1502 * purposes of synchronization to disk.
1504 * Records which are in our flush group can be unlinked from our
1505 * inode now, potentially allowing the inode to be physically
1508 nlinks = ip->ino_data.nlinks;
1509 next = TAILQ_FIRST(&ip->target_list);
1510 while ((depend = next) != NULL) {
1511 next = TAILQ_NEXT(depend, target_entry);
1512 if (depend->flush_state == HAMMER_FST_FLUSH &&
1513 depend->flush_group == ip->hmp->flusher.act) {
1515 * If this is an ADD that was deleted by the frontend
1516 * the frontend nlinks count will have already been
1517 * decremented, but the backend is going to sync its
1518 * directory entry and must account for it. The
1519 * record will be converted to a delete-on-disk when
1522 * If the ADD was not deleted by the frontend we
1523 * can remove the dependancy from our target_list.
1525 if (depend->flags & HAMMER_RECF_DELETED_FE) {
1528 TAILQ_REMOVE(&ip->target_list, depend,
1530 depend->target_ip = NULL;
1532 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
1534 * Not part of our flush group
1536 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
1537 switch(depend->type) {
1538 case HAMMER_MEM_RECORD_ADD:
1541 case HAMMER_MEM_RECORD_DEL:
1551 * Set dirty if we had to modify the link count.
1553 if (ip->sync_ino_data.nlinks != nlinks) {
1554 KKASSERT((int64_t)nlinks >= 0);
1555 ip->sync_ino_data.nlinks = nlinks;
1556 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1560 * If there is a trunction queued destroy any data past the (aligned)
1561 * truncation point. Userland will have dealt with the buffer
1562 * containing the truncation point for us.
1564 * We don't flush pending frontend data buffers until after we've
1565 * dealth with the truncation.
1567 * Don't bother if the inode is or has been deleted.
1569 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
1571 * Interlock trunc_off. The VOP front-end may continue to
1572 * make adjustments to it while we are blocked.
1575 off_t aligned_trunc_off;
1577 trunc_off = ip->sync_trunc_off;
1578 aligned_trunc_off = (trunc_off + HAMMER_BUFMASK) &
1582 * Delete any whole blocks on-media. The front-end has
1583 * already cleaned out any partial block and made it
1584 * pending. The front-end may have updated trunc_off
1585 * while we were blocked so we only use sync_trunc_off.
1587 error = hammer_ip_delete_range(&cursor, ip,
1589 0x7FFFFFFFFFFFFFFFLL, 1);
1591 Debugger("hammer_ip_delete_range errored");
1594 * Clear the truncation flag on the backend after we have
1595 * complete the deletions. Backend data is now good again
1596 * (including new records we are about to sync, below).
1598 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1599 ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1605 * Now sync related records. These will typically be directory
1606 * entries or delete-on-disk records.
1608 * Not all records will be flushed, but clear XDIRTY anyway. We
1609 * will set it again in the frontend hammer_flush_inode_done()
1610 * if records remain.
1613 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1614 hammer_sync_record_callback, &cursor);
1622 * If we are deleting the inode the frontend had better not have
1623 * any active references on elements making up the inode.
1625 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
1626 RB_EMPTY(&ip->rec_tree) &&
1627 (ip->sync_flags & HAMMER_INODE_DELETING) &&
1628 (ip->flags & HAMMER_INODE_DELETED) == 0) {
1631 ip->flags |= HAMMER_INODE_DELETED;
1632 error = hammer_ip_delete_range_all(&cursor, ip, &count1);
1634 ip->sync_flags &= ~HAMMER_INODE_DELETING;
1635 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1636 KKASSERT(RB_EMPTY(&ip->rec_tree));
1639 * Set delete_tid in both the frontend and backend
1640 * copy of the inode record. The DELETED flag handles
1641 * this, do not set RDIRTY.
1643 ip->ino_leaf.base.delete_tid = trans.tid;
1644 ip->sync_ino_leaf.base.delete_tid = trans.tid;
1647 * Adjust the inode count in the volume header
1649 if (ip->flags & HAMMER_INODE_ONDISK) {
1650 hammer_modify_volume_field(&trans,
1653 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1654 hammer_modify_volume_done(trans.rootvol);
1657 ip->flags &= ~HAMMER_INODE_DELETED;
1658 Debugger("hammer_ip_delete_range_all errored");
1662 ip->sync_flags &= ~HAMMER_INODE_BUFS;
1665 Debugger("RB_SCAN errored");
1668 * Now update the inode's on-disk inode-data and/or on-disk record.
1669 * DELETED and ONDISK are managed only in ip->flags.
1671 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
1672 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
1674 * If deleted and on-disk, don't set any additional flags.
1675 * the delete flag takes care of things.
1677 * Clear flags which may have been set by the frontend.
1679 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY|
1680 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1681 HAMMER_INODE_DELETING);
1683 case HAMMER_INODE_DELETED:
1685 * Take care of the case where a deleted inode was never
1686 * flushed to the disk in the first place.
1688 * Clear flags which may have been set by the frontend.
1690 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY|
1691 HAMMER_INODE_XDIRTY|HAMMER_INODE_ITIMES|
1692 HAMMER_INODE_DELETING);
1693 while (RB_ROOT(&ip->rec_tree)) {
1694 hammer_record_t record = RB_ROOT(&ip->rec_tree);
1695 hammer_ref(&record->lock);
1696 KKASSERT(record->lock.refs == 1);
1697 record->flags |= HAMMER_RECF_DELETED_FE;
1698 record->flags |= HAMMER_RECF_DELETED_BE;
1699 hammer_rel_mem_record(record);
1702 case HAMMER_INODE_ONDISK:
1704 * If already on-disk, do not set any additional flags.
1709 * If not on-disk and not deleted, set both dirty flags
1710 * to force an initial record to be written. Also set
1711 * the create_tid for the inode.
1713 * Set create_tid in both the frontend and backend
1714 * copy of the inode record.
1716 ip->ino_leaf.base.create_tid = trans.tid;
1717 ip->sync_ino_leaf.base.create_tid = trans.tid;
1718 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1723 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
1724 * is already on-disk the old record is marked as deleted.
1726 * If DELETED is set hammer_update_inode() will delete the existing
1727 * record without writing out a new one.
1729 * If *ONLY* the ITIMES flag is set we can update the record in-place.
1731 if (ip->flags & HAMMER_INODE_DELETED) {
1732 error = hammer_update_inode(&cursor, ip);
1734 if ((ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES)) ==
1735 HAMMER_INODE_ITIMES) {
1736 error = hammer_update_itimes(&cursor, ip);
1738 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ITIMES)) {
1739 error = hammer_update_inode(&cursor, ip);
1742 Debugger("hammer_update_itimes/inode errored");
1745 * Save the TID we used to sync the inode with to make sure we
1746 * do not improperly reuse it.
1748 hammer_done_cursor(&cursor);
1749 hammer_done_transaction(&trans);
1754 * This routine is called when the OS is no longer actively referencing
1755 * the inode (but might still be keeping it cached), or when releasing
1756 * the last reference to an inode.
1758 * At this point if the inode's nlinks count is zero we want to destroy
1759 * it, which may mean destroying it on-media too.
1762 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
1767 * Set the DELETING flag when the link count drops to 0 and the
1768 * OS no longer has any opens on the inode.
1770 * The backend will clear DELETING (a mod flag) and set DELETED
1771 * (a state flag) when it is actually able to perform the
1774 if (ip->ino_data.nlinks == 0 &&
1775 (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
1776 ip->flags |= HAMMER_INODE_DELETING;
1777 ip->flags |= HAMMER_INODE_TRUNCATED;
1781 if (hammer_get_vnode(ip, &vp) != 0)
1789 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
1790 vnode_pager_setsize(ip->vp, 0);
1799 * Re-test an inode when a dependancy had gone away to see if we
1800 * can chain flush it.
1803 hammer_test_inode(hammer_inode_t ip)
1805 if (ip->flags & HAMMER_INODE_REFLUSH) {
1806 ip->flags &= ~HAMMER_INODE_REFLUSH;
1807 hammer_ref(&ip->lock);
1808 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1809 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1810 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1812 hammer_flush_inode(ip, 0);
1814 hammer_rel_inode(ip, 0);
1819 * We need to slow down user processes if we get too large a backlog of
1820 * inodes in the flusher. Even though the frontend can theoretically
1821 * get way, way ahead of the flusher, if we let it do that the flusher
1822 * will have no buffer cache locality of reference and will have to re-read
1823 * everything a second time, causing performance to drop precipitously.
1825 * Reclaims are especially senssitive to this effect because the kernel has
1826 * already abandoned the related vnode.
1830 hammer_inode_waitreclaims(hammer_mount_t hmp)
1834 while (hmp->inode_reclaims > HAMMER_RECLAIM_MIN) {
1835 if (hmp->inode_reclaims < HAMMER_RECLAIM_MID) {
1836 hammer_flusher_async(hmp);
1839 if (hmp->inode_reclaims < HAMMER_RECLAIM_MAX) {
1840 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_MID) *
1841 hz / (HAMMER_RECLAIM_MAX - HAMMER_RECLAIM_MID);
1844 hammer_flusher_async(hmp);
1845 tsleep(&delay, 0, "hmitik", delay);
1848 hmp->flags |= HAMMER_MOUNT_WAITIMAX;
1849 hammer_flusher_async(hmp);
1850 tsleep(&hmp->inode_reclaims, 0, "hmimax", hz / 10);
1855 hammer_inode_wakereclaims(hammer_mount_t hmp)
1857 if ((hmp->flags & HAMMER_MOUNT_WAITIMAX) &&
1858 hmp->inode_reclaims < HAMMER_RECLAIM_MAX) {
1859 hmp->flags &= ~HAMMER_MOUNT_WAITIMAX;
1860 wakeup(&hmp->inode_reclaims);