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.97 2008/07/10 04:44:33 dillon Exp $
38 #include <vm/vm_extern.h>
42 static int hammer_unload_inode(struct hammer_inode *ip);
43 static void hammer_free_inode(hammer_inode_t ip);
44 static void hammer_flush_inode_core(hammer_inode_t ip, int flags);
45 static int hammer_setup_child_callback(hammer_record_t rec, void *data);
46 static int hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
47 static int hammer_setup_parent_inodes(hammer_inode_t ip);
48 static int hammer_setup_parent_inodes_helper(hammer_record_t record);
49 static void hammer_inode_wakereclaims(hammer_inode_t ip);
52 extern struct hammer_inode *HammerTruncIp;
56 * RB-Tree support for inode structures
59 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
61 if (ip1->obj_localization < ip2->obj_localization)
63 if (ip1->obj_localization > ip2->obj_localization)
65 if (ip1->obj_id < ip2->obj_id)
67 if (ip1->obj_id > ip2->obj_id)
69 if (ip1->obj_asof < ip2->obj_asof)
71 if (ip1->obj_asof > ip2->obj_asof)
77 * RB-Tree support for inode structures / special LOOKUP_INFO
80 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
82 if (info->obj_localization < ip->obj_localization)
84 if (info->obj_localization > ip->obj_localization)
86 if (info->obj_id < ip->obj_id)
88 if (info->obj_id > ip->obj_id)
90 if (info->obj_asof < ip->obj_asof)
92 if (info->obj_asof > ip->obj_asof)
98 * Used by hammer_scan_inode_snapshots() to locate all of an object's
99 * snapshots. Note that the asof field is not tested, which we can get
100 * away with because it is the lowest-priority field.
103 hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
105 hammer_inode_info_t info = data;
107 if (ip->obj_localization > info->obj_localization)
109 if (ip->obj_localization < info->obj_localization)
111 if (ip->obj_id > info->obj_id)
113 if (ip->obj_id < info->obj_id)
119 * RB-Tree support for pseudofs structures
122 hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
124 if (p1->localization < p2->localization)
126 if (p1->localization > p2->localization)
132 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
133 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
134 hammer_inode_info_cmp, hammer_inode_info_t);
135 RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
136 hammer_pfs_rb_compare, u_int32_t, localization);
139 * The kernel is not actively referencing this vnode but is still holding
142 * This is called from the frontend.
145 hammer_vop_inactive(struct vop_inactive_args *ap)
147 struct hammer_inode *ip = VTOI(ap->a_vp);
158 * If the inode no longer has visibility in the filesystem try to
159 * recycle it immediately, even if the inode is dirty. Recycling
160 * it quickly allows the system to reclaim buffer cache and VM
161 * resources which can matter a lot in a heavily loaded system.
163 * This can deadlock in vfsync() if we aren't careful.
165 * Do not queue the inode to the flusher if we still have visibility,
166 * otherwise namespace calls such as chmod will unnecessarily generate
167 * multiple inode updates.
169 hammer_inode_unloadable_check(ip, 0);
170 if (ip->ino_data.nlinks == 0) {
171 if (ip->flags & HAMMER_INODE_MODMASK)
172 hammer_flush_inode(ip, 0);
179 * Release the vnode association. This is typically (but not always)
180 * the last reference on the inode.
182 * Once the association is lost we are on our own with regards to
183 * flushing the inode.
186 hammer_vop_reclaim(struct vop_reclaim_args *ap)
188 struct hammer_inode *ip;
194 if ((ip = vp->v_data) != NULL) {
199 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
200 ++hammer_count_reclaiming;
201 ++hmp->inode_reclaims;
202 ip->flags |= HAMMER_INODE_RECLAIM;
203 if (hmp->inode_reclaims > HAMMER_RECLAIM_FLUSH &&
204 (hmp->inode_reclaims & 255) == 0) {
205 hammer_flusher_async(hmp);
208 hammer_rel_inode(ip, 1);
214 * Return a locked vnode for the specified inode. The inode must be
215 * referenced but NOT LOCKED on entry and will remain referenced on
218 * Called from the frontend.
221 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
231 if ((vp = ip->vp) == NULL) {
232 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
235 hammer_lock_ex(&ip->lock);
236 if (ip->vp != NULL) {
237 hammer_unlock(&ip->lock);
242 hammer_ref(&ip->lock);
246 obj_type = ip->ino_data.obj_type;
247 vp->v_type = hammer_get_vnode_type(obj_type);
249 hammer_inode_wakereclaims(ip);
251 switch(ip->ino_data.obj_type) {
252 case HAMMER_OBJTYPE_CDEV:
253 case HAMMER_OBJTYPE_BDEV:
254 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
255 addaliasu(vp, ip->ino_data.rmajor,
256 ip->ino_data.rminor);
258 case HAMMER_OBJTYPE_FIFO:
259 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
266 * Only mark as the root vnode if the ip is not
267 * historical, otherwise the VFS cache will get
268 * confused. The other half of the special handling
269 * is in hammer_vop_nlookupdotdot().
271 * Pseudo-filesystem roots also do not count.
273 if (ip->obj_id == HAMMER_OBJID_ROOT &&
274 ip->obj_asof == hmp->asof &&
275 ip->obj_localization == 0) {
279 vp->v_data = (void *)ip;
280 /* vnode locked by getnewvnode() */
281 /* make related vnode dirty if inode dirty? */
282 hammer_unlock(&ip->lock);
283 if (vp->v_type == VREG)
284 vinitvmio(vp, ip->ino_data.size);
289 * loop if the vget fails (aka races), or if the vp
290 * no longer matches ip->vp.
292 if (vget(vp, LK_EXCLUSIVE) == 0) {
303 * Locate all copies of the inode for obj_id compatible with the specified
304 * asof, reference, and issue the related call-back. This routine is used
305 * for direct-io invalidation and does not create any new inodes.
308 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
309 int (*callback)(hammer_inode_t ip, void *data),
312 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
313 hammer_inode_info_cmp_all_history,
318 * Acquire a HAMMER inode. The returned inode is not locked. These functions
319 * do not attach or detach the related vnode (use hammer_get_vnode() for
322 * The flags argument is only applied for newly created inodes, and only
323 * certain flags are inherited.
325 * Called from the frontend.
327 struct hammer_inode *
328 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
329 int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
330 int flags, int *errorp)
332 hammer_mount_t hmp = trans->hmp;
333 struct hammer_inode_info iinfo;
334 struct hammer_cursor cursor;
335 struct hammer_inode *ip;
339 * Determine if we already have an inode cached. If we do then
342 iinfo.obj_id = obj_id;
343 iinfo.obj_asof = asof;
344 iinfo.obj_localization = localization;
346 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
348 hammer_ref(&ip->lock);
354 * Allocate a new inode structure and deal with races later.
356 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
357 ++hammer_count_inodes;
360 ip->obj_asof = iinfo.obj_asof;
361 ip->obj_localization = localization;
363 ip->flags = flags & HAMMER_INODE_RO;
364 ip->cache[0].ip = ip;
365 ip->cache[1].ip = ip;
367 ip->flags |= HAMMER_INODE_RO;
368 ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
369 0x7FFFFFFFFFFFFFFFLL;
370 RB_INIT(&ip->rec_tree);
371 TAILQ_INIT(&ip->target_list);
372 hammer_ref(&ip->lock);
375 * Locate the on-disk inode. If this is a PFS root we always
376 * access the current version of the root inode and (if it is not
377 * a master) always access information under it with a snapshot
381 hammer_init_cursor(trans, &cursor, (dip ? &dip->cache[0] : NULL), NULL);
382 cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
383 cursor.key_beg.obj_id = ip->obj_id;
384 cursor.key_beg.key = 0;
385 cursor.key_beg.create_tid = 0;
386 cursor.key_beg.delete_tid = 0;
387 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
388 cursor.key_beg.obj_type = 0;
390 cursor.asof = iinfo.obj_asof;
391 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
394 *errorp = hammer_btree_lookup(&cursor);
395 if (*errorp == EDEADLK) {
396 hammer_done_cursor(&cursor);
401 * On success the B-Tree lookup will hold the appropriate
402 * buffer cache buffers and provide a pointer to the requested
403 * information. Copy the information to the in-memory inode
404 * and cache the B-Tree node to improve future operations.
407 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
408 ip->ino_data = cursor.data->inode;
411 * cache[0] tries to cache the location of the object inode.
412 * The assumption is that it is near the directory inode.
414 * cache[1] tries to cache the location of the object data.
415 * The assumption is that it is near the directory data.
417 hammer_cache_node(&ip->cache[0], cursor.node);
418 if (dip && dip->cache[1].node)
419 hammer_cache_node(&ip->cache[1], dip->cache[1].node);
422 * The file should not contain any data past the file size
423 * stored in the inode. Setting save_trunc_off to the
424 * file size instead of max reduces B-Tree lookup overheads
425 * on append by allowing the flusher to avoid checking for
428 ip->save_trunc_off = ip->ino_data.size;
431 * Locate and assign the pseudofs management structure to
434 if (dip && dip->obj_localization == ip->obj_localization) {
435 ip->pfsm = dip->pfsm;
436 hammer_ref(&ip->pfsm->lock);
438 ip->pfsm = hammer_load_pseudofs(trans,
439 ip->obj_localization,
441 *errorp = 0; /* ignore ENOENT */
446 * The inode is placed on the red-black tree and will be synced to
447 * the media when flushed or by the filesystem sync. If this races
448 * another instantiation/lookup the insertion will fail.
451 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
452 hammer_free_inode(ip);
453 hammer_done_cursor(&cursor);
456 ip->flags |= HAMMER_INODE_ONDISK;
458 if (ip->flags & HAMMER_INODE_RSV_INODES) {
459 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
463 hammer_free_inode(ip);
466 hammer_done_cursor(&cursor);
471 * Create a new filesystem object, returning the inode in *ipp. The
472 * returned inode will be referenced. The inode is created in-memory.
474 * If pfsm is non-NULL the caller wishes to create the root inode for
478 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
479 struct ucred *cred, hammer_inode_t dip,
480 hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
489 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
490 ++hammer_count_inodes;
494 KKASSERT(pfsm->localization != 0);
495 ip->obj_id = HAMMER_OBJID_ROOT;
496 ip->obj_localization = pfsm->localization;
498 KKASSERT(dip != NULL);
499 ip->obj_id = hammer_alloc_objid(hmp, dip);
500 ip->obj_localization = dip->obj_localization;
503 KKASSERT(ip->obj_id != 0);
504 ip->obj_asof = hmp->asof;
506 ip->flush_state = HAMMER_FST_IDLE;
507 ip->flags = HAMMER_INODE_DDIRTY |
508 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
509 ip->cache[0].ip = ip;
510 ip->cache[1].ip = ip;
512 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
513 /* ip->save_trunc_off = 0; (already zero) */
514 RB_INIT(&ip->rec_tree);
515 TAILQ_INIT(&ip->target_list);
517 ip->ino_data.atime = trans->time;
518 ip->ino_data.mtime = trans->time;
519 ip->ino_data.size = 0;
520 ip->ino_data.nlinks = 0;
523 * A nohistory designator on the parent directory is inherited by
524 * the child. We will do this even for pseudo-fs creation... the
525 * sysad can turn it off.
528 ip->ino_data.uflags = dip->ino_data.uflags &
529 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
532 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
533 ip->ino_leaf.base.localization = ip->obj_localization +
534 HAMMER_LOCALIZE_INODE;
535 ip->ino_leaf.base.obj_id = ip->obj_id;
536 ip->ino_leaf.base.key = 0;
537 ip->ino_leaf.base.create_tid = 0;
538 ip->ino_leaf.base.delete_tid = 0;
539 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
540 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
542 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
543 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
544 ip->ino_data.mode = vap->va_mode;
545 ip->ino_data.ctime = trans->time;
548 * Setup the ".." pointer. This only needs to be done for directories
549 * but we do it for all objects as a recovery aid.
552 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
555 * The parent_obj_localization field only applies to pseudo-fs roots.
556 * XXX this is no longer applicable, PFSs are no longer directly
557 * tied into the parent's directory structure.
559 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
560 ip->obj_id == HAMMER_OBJID_ROOT) {
561 ip->ino_data.ext.obj.parent_obj_localization =
562 dip->obj_localization;
566 switch(ip->ino_leaf.base.obj_type) {
567 case HAMMER_OBJTYPE_CDEV:
568 case HAMMER_OBJTYPE_BDEV:
569 ip->ino_data.rmajor = vap->va_rmajor;
570 ip->ino_data.rminor = vap->va_rminor;
577 * Calculate default uid/gid and overwrite with information from
581 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
582 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
583 xuid, cred, &vap->va_mode);
587 ip->ino_data.mode = vap->va_mode;
589 if (vap->va_vaflags & VA_UID_UUID_VALID)
590 ip->ino_data.uid = vap->va_uid_uuid;
591 else if (vap->va_uid != (uid_t)VNOVAL)
592 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
594 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
596 if (vap->va_vaflags & VA_GID_UUID_VALID)
597 ip->ino_data.gid = vap->va_gid_uuid;
598 else if (vap->va_gid != (gid_t)VNOVAL)
599 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
601 ip->ino_data.gid = dip->ino_data.gid;
603 hammer_ref(&ip->lock);
607 hammer_ref(&pfsm->lock);
609 } else if (dip->obj_localization == ip->obj_localization) {
610 ip->pfsm = dip->pfsm;
611 hammer_ref(&ip->pfsm->lock);
614 ip->pfsm = hammer_load_pseudofs(trans,
615 ip->obj_localization,
617 error = 0; /* ignore ENOENT */
621 hammer_free_inode(ip);
623 } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
624 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
626 hammer_free_inode(ip);
633 * Final cleanup / freeing of an inode structure
636 hammer_free_inode(hammer_inode_t ip)
638 KKASSERT(ip->lock.refs == 1);
639 hammer_uncache_node(&ip->cache[0]);
640 hammer_uncache_node(&ip->cache[1]);
641 hammer_inode_wakereclaims(ip);
643 hammer_clear_objid(ip);
644 --hammer_count_inodes;
645 --ip->hmp->count_inodes;
647 hammer_rel_pseudofs(ip->hmp, ip->pfsm);
655 * Retrieve pseudo-fs data. NULL will never be returned.
657 * If an error occurs *errorp will be set and a default template is returned,
658 * otherwise *errorp is set to 0. Typically when an error occurs it will
661 hammer_pseudofs_inmem_t
662 hammer_load_pseudofs(hammer_transaction_t trans,
663 u_int32_t localization, int *errorp)
665 hammer_mount_t hmp = trans->hmp;
667 hammer_pseudofs_inmem_t pfsm;
668 struct hammer_cursor cursor;
672 pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
674 hammer_ref(&pfsm->lock);
680 * PFS records are stored in the root inode (not the PFS root inode,
681 * but the real root). Avoid an infinite recursion if loading
682 * the PFS for the real root.
685 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
687 HAMMER_DEF_LOCALIZATION, 0, errorp);
692 pfsm = kmalloc(sizeof(*pfsm), M_HAMMER, M_WAITOK | M_ZERO);
693 pfsm->localization = localization;
694 pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
695 pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
697 hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
698 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
699 HAMMER_LOCALIZE_MISC;
700 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
701 cursor.key_beg.create_tid = 0;
702 cursor.key_beg.delete_tid = 0;
703 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
704 cursor.key_beg.obj_type = 0;
705 cursor.key_beg.key = localization;
706 cursor.asof = HAMMER_MAX_TID;
707 cursor.flags |= HAMMER_CURSOR_ASOF;
710 *errorp = hammer_ip_lookup(&cursor);
712 *errorp = hammer_btree_lookup(&cursor);
714 *errorp = hammer_ip_resolve_data(&cursor);
716 bytes = cursor.leaf->data_len;
717 if (bytes > sizeof(pfsm->pfsd))
718 bytes = sizeof(pfsm->pfsd);
719 bcopy(cursor.data, &pfsm->pfsd, bytes);
722 hammer_done_cursor(&cursor);
724 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
725 hammer_ref(&pfsm->lock);
727 hammer_rel_inode(ip, 0);
728 if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
729 kfree(pfsm, M_HAMMER);
736 * Store pseudo-fs data. The backend will automatically delete any prior
737 * on-disk pseudo-fs data but we have to delete in-memory versions.
740 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
742 struct hammer_cursor cursor;
743 hammer_record_t record;
747 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
748 HAMMER_DEF_LOCALIZATION, 0, &error);
750 pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
751 hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
752 cursor.key_beg.localization = ip->obj_localization +
753 HAMMER_LOCALIZE_MISC;
754 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
755 cursor.key_beg.create_tid = 0;
756 cursor.key_beg.delete_tid = 0;
757 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
758 cursor.key_beg.obj_type = 0;
759 cursor.key_beg.key = pfsm->localization;
760 cursor.asof = HAMMER_MAX_TID;
761 cursor.flags |= HAMMER_CURSOR_ASOF;
763 error = hammer_ip_lookup(&cursor);
764 if (error == 0 && hammer_cursor_inmem(&cursor)) {
765 record = cursor.iprec;
766 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
767 KKASSERT(cursor.deadlk_rec == NULL);
768 hammer_ref(&record->lock);
769 cursor.deadlk_rec = record;
772 record->flags |= HAMMER_RECF_DELETED_FE;
776 if (error == 0 || error == ENOENT) {
777 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
778 record->type = HAMMER_MEM_RECORD_GENERAL;
780 record->leaf.base.localization = ip->obj_localization +
781 HAMMER_LOCALIZE_MISC;
782 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
783 record->leaf.base.key = pfsm->localization;
784 record->leaf.data_len = sizeof(pfsm->pfsd);
785 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
786 error = hammer_ip_add_record(trans, record);
788 hammer_done_cursor(&cursor);
789 if (error == EDEADLK)
791 hammer_rel_inode(ip, 0);
796 * Create a root directory for a PFS if one does not alredy exist.
798 * The PFS root stands alone so we must also bump the nlinks count
799 * to prevent it from being destroyed on release.
802 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
803 hammer_pseudofs_inmem_t pfsm)
809 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
810 pfsm->localization, 0, &error);
815 error = hammer_create_inode(trans, &vap, cred, NULL, pfsm, &ip);
817 ++ip->ino_data.nlinks;
818 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
822 hammer_rel_inode(ip, 0);
827 * Release a reference on a PFS
830 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
832 hammer_unref(&pfsm->lock);
833 if (pfsm->lock.refs == 0) {
834 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
835 kfree(pfsm, M_HAMMER);
840 * Called by hammer_sync_inode().
843 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
845 hammer_transaction_t trans = cursor->trans;
846 hammer_record_t record;
854 * If the inode has a presence on-disk then locate it and mark
855 * it deleted, setting DELONDISK.
857 * The record may or may not be physically deleted, depending on
858 * the retention policy.
860 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
861 HAMMER_INODE_ONDISK) {
862 hammer_normalize_cursor(cursor);
863 cursor->key_beg.localization = ip->obj_localization +
864 HAMMER_LOCALIZE_INODE;
865 cursor->key_beg.obj_id = ip->obj_id;
866 cursor->key_beg.key = 0;
867 cursor->key_beg.create_tid = 0;
868 cursor->key_beg.delete_tid = 0;
869 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
870 cursor->key_beg.obj_type = 0;
871 cursor->asof = ip->obj_asof;
872 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
873 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
874 cursor->flags |= HAMMER_CURSOR_BACKEND;
876 error = hammer_btree_lookup(cursor);
877 if (hammer_debug_inode)
878 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
880 kprintf("error %d\n", error);
881 Debugger("hammer_update_inode");
885 error = hammer_ip_delete_record(cursor, ip, trans->tid);
886 if (hammer_debug_inode)
887 kprintf(" error %d\n", error);
888 if (error && error != EDEADLK) {
889 kprintf("error %d\n", error);
890 Debugger("hammer_update_inode2");
893 ip->flags |= HAMMER_INODE_DELONDISK;
896 hammer_cache_node(&ip->cache[0], cursor->node);
898 if (error == EDEADLK) {
899 hammer_done_cursor(cursor);
900 error = hammer_init_cursor(trans, cursor,
902 if (hammer_debug_inode)
903 kprintf("IPDED %p %d\n", ip, error);
910 * Ok, write out the initial record or a new record (after deleting
911 * the old one), unless the DELETED flag is set. This routine will
912 * clear DELONDISK if it writes out a record.
914 * Update our inode statistics if this is the first application of
917 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
919 * Generate a record and write it to the media
921 record = hammer_alloc_mem_record(ip, 0);
922 record->type = HAMMER_MEM_RECORD_INODE;
923 record->flush_state = HAMMER_FST_FLUSH;
924 record->leaf = ip->sync_ino_leaf;
925 record->leaf.base.create_tid = trans->tid;
926 record->leaf.data_len = sizeof(ip->sync_ino_data);
927 record->leaf.create_ts = trans->time32;
928 record->data = (void *)&ip->sync_ino_data;
929 record->flags |= HAMMER_RECF_INTERLOCK_BE;
932 * If this flag is set we cannot sync the new file size
933 * because we haven't finished related truncations. The
934 * inode will be flushed in another flush group to finish
937 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
938 ip->sync_ino_data.size != ip->ino_data.size) {
940 ip->sync_ino_data.size = ip->ino_data.size;
946 error = hammer_ip_sync_record_cursor(cursor, record);
947 if (hammer_debug_inode)
948 kprintf("GENREC %p rec %08x %d\n",
949 ip, record->flags, error);
950 if (error != EDEADLK)
952 hammer_done_cursor(cursor);
953 error = hammer_init_cursor(trans, cursor,
955 if (hammer_debug_inode)
956 kprintf("GENREC reinit %d\n", error);
961 kprintf("error %d\n", error);
962 Debugger("hammer_update_inode3");
966 * The record isn't managed by the inode's record tree,
967 * destroy it whether we succeed or fail.
969 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
970 record->flags |= HAMMER_RECF_DELETED_FE;
971 record->flush_state = HAMMER_FST_IDLE;
972 hammer_rel_mem_record(record);
978 if (hammer_debug_inode)
979 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
980 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
983 ip->flags &= ~HAMMER_INODE_DELONDISK;
985 ip->sync_flags |= HAMMER_INODE_DDIRTY;
988 * Root volume count of inodes
990 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
991 hammer_modify_volume_field(trans,
994 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
995 hammer_modify_volume_done(trans->rootvol);
996 ip->flags |= HAMMER_INODE_ONDISK;
997 if (hammer_debug_inode)
998 kprintf("NOWONDISK %p\n", ip);
1004 * If the inode has been destroyed, clean out any left-over flags
1005 * that may have been set by the frontend.
1007 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
1008 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1009 HAMMER_INODE_ATIME |
1010 HAMMER_INODE_MTIME);
1016 * Update only the itimes fields.
1018 * ATIME can be updated without generating any UNDO. MTIME is updated
1019 * with UNDO so it is guaranteed to be synchronized properly in case of
1022 * Neither field is included in the B-Tree leaf element's CRC, which is how
1023 * we can get away with updating ATIME the way we do.
1026 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1028 hammer_transaction_t trans = cursor->trans;
1032 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1033 HAMMER_INODE_ONDISK) {
1037 hammer_normalize_cursor(cursor);
1038 cursor->key_beg.localization = ip->obj_localization +
1039 HAMMER_LOCALIZE_INODE;
1040 cursor->key_beg.obj_id = ip->obj_id;
1041 cursor->key_beg.key = 0;
1042 cursor->key_beg.create_tid = 0;
1043 cursor->key_beg.delete_tid = 0;
1044 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1045 cursor->key_beg.obj_type = 0;
1046 cursor->asof = ip->obj_asof;
1047 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1048 cursor->flags |= HAMMER_CURSOR_ASOF;
1049 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1050 cursor->flags |= HAMMER_CURSOR_GET_DATA;
1051 cursor->flags |= HAMMER_CURSOR_BACKEND;
1053 error = hammer_btree_lookup(cursor);
1055 kprintf("error %d\n", error);
1056 Debugger("hammer_update_itimes1");
1059 hammer_cache_node(&ip->cache[0], cursor->node);
1060 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1062 * Updating MTIME requires an UNDO. Just cover
1063 * both atime and mtime.
1065 hammer_modify_buffer(trans, cursor->data_buffer,
1066 HAMMER_ITIMES_BASE(&cursor->data->inode),
1067 HAMMER_ITIMES_BYTES);
1068 cursor->data->inode.atime = ip->sync_ino_data.atime;
1069 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1070 hammer_modify_buffer_done(cursor->data_buffer);
1071 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1073 * Updating atime only can be done in-place with
1076 hammer_modify_buffer(trans, cursor->data_buffer,
1078 cursor->data->inode.atime = ip->sync_ino_data.atime;
1079 hammer_modify_buffer_done(cursor->data_buffer);
1081 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1083 if (error == EDEADLK) {
1084 hammer_done_cursor(cursor);
1085 error = hammer_init_cursor(trans, cursor,
1094 * Release a reference on an inode, flush as requested.
1096 * On the last reference we queue the inode to the flusher for its final
1100 hammer_rel_inode(struct hammer_inode *ip, int flush)
1102 hammer_mount_t hmp = ip->hmp;
1105 * Handle disposition when dropping the last ref.
1108 if (ip->lock.refs == 1) {
1110 * Determine whether on-disk action is needed for
1111 * the inode's final disposition.
1113 KKASSERT(ip->vp == NULL);
1114 hammer_inode_unloadable_check(ip, 0);
1115 if (ip->flags & HAMMER_INODE_MODMASK) {
1116 if (hmp->rsv_inodes > desiredvnodes) {
1117 hammer_flush_inode(ip,
1118 HAMMER_FLUSH_SIGNAL);
1120 hammer_flush_inode(ip, 0);
1122 } else if (ip->lock.refs == 1) {
1123 hammer_unload_inode(ip);
1128 hammer_flush_inode(ip, 0);
1131 * The inode still has multiple refs, try to drop
1134 KKASSERT(ip->lock.refs >= 1);
1135 if (ip->lock.refs > 1) {
1136 hammer_unref(&ip->lock);
1144 * Unload and destroy the specified inode. Must be called with one remaining
1145 * reference. The reference is disposed of.
1147 * This can only be called in the context of the flusher.
1150 hammer_unload_inode(struct hammer_inode *ip)
1152 hammer_mount_t hmp = ip->hmp;
1154 KASSERT(ip->lock.refs == 1,
1155 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1156 KKASSERT(ip->vp == NULL);
1157 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1158 KKASSERT(ip->cursor_ip_refs == 0);
1159 KKASSERT(ip->lock.lockcount == 0);
1160 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1162 KKASSERT(RB_EMPTY(&ip->rec_tree));
1163 KKASSERT(TAILQ_EMPTY(&ip->target_list));
1165 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1167 hammer_free_inode(ip);
1172 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1173 * the read-only flag for cached inodes.
1175 * This routine is called from a RB_SCAN().
1178 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1180 hammer_mount_t hmp = ip->hmp;
1182 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1183 ip->flags |= HAMMER_INODE_RO;
1185 ip->flags &= ~HAMMER_INODE_RO;
1190 * A transaction has modified an inode, requiring updates as specified by
1193 * HAMMER_INODE_DDIRTY: Inode data has been updated
1194 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1195 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1196 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1197 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1200 hammer_modify_inode(hammer_inode_t ip, int flags)
1202 KKASSERT(ip->hmp->ronly == 0 ||
1203 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1204 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1205 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1206 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1207 ip->flags |= HAMMER_INODE_RSV_INODES;
1208 ++ip->hmp->rsv_inodes;
1215 * Request that an inode be flushed. This whole mess cannot block and may
1216 * recurse (if not synchronous). Once requested HAMMER will attempt to
1217 * actively flush the inode until the flush can be done.
1219 * The inode may already be flushing, or may be in a setup state. We can
1220 * place the inode in a flushing state if it is currently idle and flag it
1221 * to reflush if it is currently flushing.
1223 * If the HAMMER_FLUSH_SYNCHRONOUS flag is specified we will attempt to
1224 * flush the indoe synchronously using the caller's context.
1227 hammer_flush_inode(hammer_inode_t ip, int flags)
1232 * Trivial 'nothing to flush' case. If the inode is ina SETUP
1233 * state we have to put it back into an IDLE state so we can
1234 * drop the extra ref.
1236 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1237 if (ip->flush_state == HAMMER_FST_SETUP) {
1238 ip->flush_state = HAMMER_FST_IDLE;
1239 hammer_rel_inode(ip, 0);
1245 * Our flush action will depend on the current state.
1247 switch(ip->flush_state) {
1248 case HAMMER_FST_IDLE:
1250 * We have no dependancies and can flush immediately. Some
1251 * our children may not be flushable so we have to re-test
1252 * with that additional knowledge.
1254 hammer_flush_inode_core(ip, flags);
1256 case HAMMER_FST_SETUP:
1258 * Recurse upwards through dependancies via target_list
1259 * and start their flusher actions going if possible.
1261 * 'good' is our connectivity. -1 means we have none and
1262 * can't flush, 0 means there weren't any dependancies, and
1263 * 1 means we have good connectivity.
1265 good = hammer_setup_parent_inodes(ip);
1268 * We can continue if good >= 0. Determine how many records
1269 * under our inode can be flushed (and mark them).
1272 hammer_flush_inode_core(ip, flags);
1274 ip->flags |= HAMMER_INODE_REFLUSH;
1275 if (flags & HAMMER_FLUSH_SIGNAL) {
1276 ip->flags |= HAMMER_INODE_RESIGNAL;
1277 hammer_flusher_async(ip->hmp);
1283 * We are already flushing, flag the inode to reflush
1284 * if needed after it completes its current flush.
1286 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1287 ip->flags |= HAMMER_INODE_REFLUSH;
1288 if (flags & HAMMER_FLUSH_SIGNAL) {
1289 ip->flags |= HAMMER_INODE_RESIGNAL;
1290 hammer_flusher_async(ip->hmp);
1297 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1298 * ip which reference our ip.
1300 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1301 * so for now do not ref/deref the structures. Note that if we use the
1302 * ref/rel code later, the rel CAN block.
1305 hammer_setup_parent_inodes(hammer_inode_t ip)
1307 hammer_record_t depend;
1309 hammer_record_t next;
1316 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1317 r = hammer_setup_parent_inodes_helper(depend);
1318 KKASSERT(depend->target_ip == ip);
1319 if (r < 0 && good == 0)
1329 next = TAILQ_FIRST(&ip->target_list);
1331 hammer_ref(&next->lock);
1332 hammer_ref(&next->ip->lock);
1334 while ((depend = next) != NULL) {
1335 if (depend->target_ip == NULL) {
1337 hammer_rel_mem_record(depend);
1338 hammer_rel_inode(pip, 0);
1341 KKASSERT(depend->target_ip == ip);
1342 next = TAILQ_NEXT(depend, target_entry);
1344 hammer_ref(&next->lock);
1345 hammer_ref(&next->ip->lock);
1347 r = hammer_setup_parent_inodes_helper(depend);
1348 if (r < 0 && good == 0)
1353 hammer_rel_mem_record(depend);
1354 hammer_rel_inode(pip, 0);
1361 * This helper function takes a record representing the dependancy between
1362 * the parent inode and child inode.
1364 * record->ip = parent inode
1365 * record->target_ip = child inode
1367 * We are asked to recurse upwards and convert the record from SETUP
1368 * to FLUSH if possible.
1370 * Return 1 if the record gives us connectivity
1372 * Return 0 if the record is not relevant
1374 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1377 hammer_setup_parent_inodes_helper(hammer_record_t record)
1383 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1388 * If the record is already flushing, is it in our flush group?
1390 * If it is in our flush group but it is a general record or a
1391 * delete-on-disk, it does not improve our connectivity (return 0),
1392 * and if the target inode is not trying to destroy itself we can't
1393 * allow the operation yet anyway (the second return -1).
1395 if (record->flush_state == HAMMER_FST_FLUSH) {
1396 if (record->flush_group != hmp->flusher.next) {
1397 pip->flags |= HAMMER_INODE_REFLUSH;
1400 if (record->type == HAMMER_MEM_RECORD_ADD)
1402 /* GENERAL or DEL */
1407 * It must be a setup record. Try to resolve the setup dependancies
1408 * by recursing upwards so we can place ip on the flush list.
1410 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1412 good = hammer_setup_parent_inodes(pip);
1415 * We can't flush ip because it has no connectivity (XXX also check
1416 * nlinks for pre-existing connectivity!). Flag it so any resolution
1417 * recurses back down.
1420 pip->flags |= HAMMER_INODE_REFLUSH;
1425 * We are go, place the parent inode in a flushing state so we can
1426 * place its record in a flushing state. Note that the parent
1427 * may already be flushing. The record must be in the same flush
1428 * group as the parent.
1430 if (pip->flush_state != HAMMER_FST_FLUSH)
1431 hammer_flush_inode_core(pip, HAMMER_FLUSH_RECURSION);
1432 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1433 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1436 if (record->type == HAMMER_MEM_RECORD_DEL &&
1437 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1439 * Regardless of flushing state we cannot sync this path if the
1440 * record represents a delete-on-disk but the target inode
1441 * is not ready to sync its own deletion.
1443 * XXX need to count effective nlinks to determine whether
1444 * the flush is ok, otherwise removing a hardlink will
1445 * just leave the DEL record to rot.
1447 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1451 if (pip->flush_group == pip->hmp->flusher.next) {
1453 * This is the record we wanted to synchronize. If the
1454 * record went into a flush state while we blocked it
1455 * had better be in the correct flush group.
1457 if (record->flush_state != HAMMER_FST_FLUSH) {
1458 record->flush_state = HAMMER_FST_FLUSH;
1459 record->flush_group = pip->flush_group;
1460 hammer_ref(&record->lock);
1462 KKASSERT(record->flush_group == pip->flush_group);
1464 if (record->type == HAMMER_MEM_RECORD_ADD)
1468 * A general or delete-on-disk record does not contribute
1469 * to our visibility. We can still flush it, however.
1474 * We couldn't resolve the dependancies, request that the
1475 * inode be flushed when the dependancies can be resolved.
1477 pip->flags |= HAMMER_INODE_REFLUSH;
1483 * This is the core routine placing an inode into the FST_FLUSH state.
1486 hammer_flush_inode_core(hammer_inode_t ip, int flags)
1491 * Set flush state and prevent the flusher from cycling into
1492 * the next flush group. Do not place the ip on the list yet.
1493 * Inodes not in the idle state get an extra reference.
1495 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1496 if (ip->flush_state == HAMMER_FST_IDLE)
1497 hammer_ref(&ip->lock);
1498 ip->flush_state = HAMMER_FST_FLUSH;
1499 ip->flush_group = ip->hmp->flusher.next;
1500 ++ip->hmp->flusher.group_lock;
1501 ++ip->hmp->count_iqueued;
1502 ++hammer_count_iqueued;
1505 * We need to be able to vfsync/truncate from the backend.
1507 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1508 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1509 ip->flags |= HAMMER_INODE_VHELD;
1514 * Figure out how many in-memory records we can actually flush
1515 * (not including inode meta-data, buffers, etc).
1517 * Do not add new records to the flush if this is a recursion or
1518 * if we must still complete a flush from the previous flush cycle.
1520 if (flags & HAMMER_FLUSH_RECURSION) {
1522 } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1523 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1524 hammer_syncgrp_child_callback, NULL);
1527 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1528 hammer_setup_child_callback, NULL);
1532 * This is a more involved test that includes go_count. If we
1533 * can't flush, flag the inode and return. If go_count is 0 we
1534 * were are unable to flush any records in our rec_tree and
1535 * must ignore the XDIRTY flag.
1537 if (go_count == 0) {
1538 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1539 ip->flags |= HAMMER_INODE_REFLUSH;
1541 --ip->hmp->count_iqueued;
1542 --hammer_count_iqueued;
1544 ip->flush_state = HAMMER_FST_SETUP;
1545 if (ip->flags & HAMMER_INODE_VHELD) {
1546 ip->flags &= ~HAMMER_INODE_VHELD;
1549 if (flags & HAMMER_FLUSH_SIGNAL) {
1550 ip->flags |= HAMMER_INODE_RESIGNAL;
1551 hammer_flusher_async(ip->hmp);
1553 if (--ip->hmp->flusher.group_lock == 0)
1554 wakeup(&ip->hmp->flusher.group_lock);
1560 * Snapshot the state of the inode for the backend flusher.
1562 * We continue to retain save_trunc_off even when all truncations
1563 * have been resolved as an optimization to determine if we can
1564 * skip the B-Tree lookup for overwrite deletions.
1566 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1567 * and stays in ip->flags. Once set, it stays set until the
1568 * inode is destroyed.
1570 * NOTE: If a truncation from a previous flush cycle had to be
1571 * continued into this one, the TRUNCATED flag will still be
1572 * set in sync_flags as will WOULDBLOCK. When this occurs
1573 * we CANNOT safely integrate a new truncation from the front-end
1574 * because there may be data records in-memory assigned a flush
1575 * state from the previous cycle that are supposed to be flushed
1576 * before the next frontend truncation.
1578 if ((ip->flags & (HAMMER_INODE_TRUNCATED | HAMMER_INODE_WOULDBLOCK)) ==
1579 HAMMER_INODE_TRUNCATED) {
1580 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
1581 ip->sync_trunc_off = ip->trunc_off;
1582 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1583 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1584 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
1587 * The save_trunc_off used to cache whether the B-Tree
1588 * holds any records past that point is not used until
1589 * after the truncation has succeeded, so we can safely
1592 if (ip->save_trunc_off > ip->sync_trunc_off)
1593 ip->save_trunc_off = ip->sync_trunc_off;
1595 ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
1596 ~HAMMER_INODE_TRUNCATED);
1597 ip->sync_ino_leaf = ip->ino_leaf;
1598 ip->sync_ino_data = ip->ino_data;
1599 ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
1600 #ifdef DEBUG_TRUNCATE
1601 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1602 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1606 * The flusher list inherits our inode and reference.
1608 TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
1609 if (--ip->hmp->flusher.group_lock == 0)
1610 wakeup(&ip->hmp->flusher.group_lock);
1612 if (flags & HAMMER_FLUSH_SIGNAL) {
1613 hammer_flusher_async(ip->hmp);
1618 * Callback for scan of ip->rec_tree. Try to include each record in our
1619 * flush. ip->flush_group has been set but the inode has not yet been
1620 * moved into a flushing state.
1622 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1625 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1626 * the caller from shortcutting the flush.
1629 hammer_setup_child_callback(hammer_record_t rec, void *data)
1631 hammer_inode_t target_ip;
1636 * Deleted records are ignored. Note that the flush detects deleted
1637 * front-end records at multiple points to deal with races. This is
1638 * just the first line of defense. The only time DELETED_FE cannot
1639 * be set is when HAMMER_RECF_INTERLOCK_BE is set.
1641 * Don't get confused between record deletion and, say, directory
1642 * entry deletion. The deletion of a directory entry that is on
1643 * the media has nothing to do with the record deletion flags.
1645 * The flush_group for a record already in a flush state must
1646 * be updated. This case can only occur if the inode deleting
1647 * too many records had to be moved to the next flush group.
1649 if (rec->flags & (HAMMER_RECF_DELETED_FE|HAMMER_RECF_DELETED_BE)) {
1650 if (rec->flush_state == HAMMER_FST_FLUSH) {
1651 KKASSERT(rec->ip->flags & HAMMER_INODE_WOULDBLOCK);
1652 rec->flush_group = rec->ip->flush_group;
1661 * If the record is in an idle state it has no dependancies and
1667 switch(rec->flush_state) {
1668 case HAMMER_FST_IDLE:
1670 * Record has no setup dependancy, we can flush it.
1672 KKASSERT(rec->target_ip == NULL);
1673 rec->flush_state = HAMMER_FST_FLUSH;
1674 rec->flush_group = ip->flush_group;
1675 hammer_ref(&rec->lock);
1678 case HAMMER_FST_SETUP:
1680 * Record has a setup dependancy. Try to include the
1681 * target ip in the flush.
1683 * We have to be careful here, if we do not do the right
1684 * thing we can lose track of dirty inodes and the system
1685 * will lockup trying to allocate buffers.
1687 target_ip = rec->target_ip;
1688 KKASSERT(target_ip != NULL);
1689 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1690 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1692 * If the target IP is already flushing in our group
1693 * we are golden, otherwise make sure the target
1696 if (target_ip->flush_group == ip->flush_group) {
1697 rec->flush_state = HAMMER_FST_FLUSH;
1698 rec->flush_group = ip->flush_group;
1699 hammer_ref(&rec->lock);
1702 target_ip->flags |= HAMMER_INODE_REFLUSH;
1704 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1706 * If the target IP is not flushing we can force
1707 * it to flush, even if it is unable to write out
1708 * any of its own records we have at least one in
1709 * hand that we CAN deal with.
1711 rec->flush_state = HAMMER_FST_FLUSH;
1712 rec->flush_group = ip->flush_group;
1713 hammer_ref(&rec->lock);
1714 hammer_flush_inode_core(target_ip,
1715 HAMMER_FLUSH_RECURSION);
1719 * General or delete-on-disk record.
1721 * XXX this needs help. If a delete-on-disk we could
1722 * disconnect the target. If the target has its own
1723 * dependancies they really need to be flushed.
1727 rec->flush_state = HAMMER_FST_FLUSH;
1728 rec->flush_group = ip->flush_group;
1729 hammer_ref(&rec->lock);
1730 hammer_flush_inode_core(target_ip,
1731 HAMMER_FLUSH_RECURSION);
1735 case HAMMER_FST_FLUSH:
1737 * If the WOULDBLOCK flag is set records may have been left
1738 * over from a previous flush attempt and should be moved
1739 * to the current flush group. If it is not set then all
1740 * such records had better have been flushed already or
1741 * already associated with the current flush group.
1743 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1744 rec->flush_group = ip->flush_group;
1746 KKASSERT(rec->flush_group == ip->flush_group);
1755 * This version just moves records already in a flush state to the new
1756 * flush group and that is it.
1759 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
1761 hammer_inode_t ip = rec->ip;
1763 switch(rec->flush_state) {
1764 case HAMMER_FST_FLUSH:
1765 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1766 rec->flush_group = ip->flush_group;
1768 KKASSERT(rec->flush_group == ip->flush_group);
1778 * Wait for a previously queued flush to complete. Not only do we need to
1779 * wait for the inode to sync out, we also may have to run the flusher again
1780 * to get it past the UNDO position pertaining to the flush so a crash does
1781 * not 'undo' our flush.
1784 hammer_wait_inode(hammer_inode_t ip)
1786 hammer_mount_t hmp = ip->hmp;
1790 sync_group = ip->flush_group;
1791 waitcount = (ip->flags & HAMMER_INODE_REFLUSH) ? 2 : 1;
1793 if (ip->flush_state == HAMMER_FST_SETUP) {
1794 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1796 /* XXX can we make this != FST_IDLE ? check SETUP depends */
1797 while (ip->flush_state == HAMMER_FST_FLUSH &&
1798 (ip->flush_group - sync_group) < waitcount) {
1799 ip->flags |= HAMMER_INODE_FLUSHW;
1800 tsleep(&ip->flags, 0, "hmrwin", 0);
1802 while (hmp->flusher.done - sync_group < waitcount) {
1804 hammer_flusher_sync(hmp);
1809 * Called by the backend code when a flush has been completed.
1810 * The inode has already been removed from the flush list.
1812 * A pipelined flush can occur, in which case we must re-enter the
1813 * inode on the list and re-copy its fields.
1816 hammer_flush_inode_done(hammer_inode_t ip)
1821 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1826 * Merge left-over flags back into the frontend and fix the state.
1827 * Incomplete truncations are retained by the backend.
1829 ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
1830 ip->sync_flags &= HAMMER_INODE_TRUNCATED;
1833 * The backend may have adjusted nlinks, so if the adjusted nlinks
1834 * does not match the fronttend set the frontend's RDIRTY flag again.
1836 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
1837 ip->flags |= HAMMER_INODE_DDIRTY;
1840 * Fix up the dirty buffer status.
1842 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
1843 ip->flags |= HAMMER_INODE_BUFS;
1847 * Re-set the XDIRTY flag if some of the inode's in-memory records
1848 * could not be flushed.
1850 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
1851 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
1852 (!RB_EMPTY(&ip->rec_tree) &&
1853 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
1856 * Do not lose track of inodes which no longer have vnode
1857 * assocations, otherwise they may never get flushed again.
1859 if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
1860 ip->flags |= HAMMER_INODE_REFLUSH;
1863 * Clean up the vnode ref
1865 if (ip->flags & HAMMER_INODE_VHELD) {
1866 ip->flags &= ~HAMMER_INODE_VHELD;
1871 * Adjust flush_state. The target state (idle or setup) shouldn't
1872 * be terribly important since we will reflush if we really need
1875 * If the WOULDBLOCK flag is set we must re-flush immediately
1876 * to continue a potentially large deletion. The flag also causes
1877 * the hammer_setup_child_callback() to move records in the old
1878 * flush group to the new one.
1880 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1881 ip->flush_state = HAMMER_FST_IDLE;
1882 hammer_flush_inode_core(ip, HAMMER_FLUSH_SIGNAL);
1883 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
1885 } else if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1886 ip->flush_state = HAMMER_FST_IDLE;
1889 ip->flush_state = HAMMER_FST_SETUP;
1893 --hmp->count_iqueued;
1894 --hammer_count_iqueued;
1897 * If the frontend made more changes and requested another flush,
1898 * then try to get it running.
1900 if (ip->flags & HAMMER_INODE_REFLUSH) {
1901 ip->flags &= ~HAMMER_INODE_REFLUSH;
1902 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1903 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1904 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1906 hammer_flush_inode(ip, 0);
1911 * If the inode is now clean drop the space reservation.
1913 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1914 (ip->flags & HAMMER_INODE_RSV_INODES)) {
1915 ip->flags &= ~HAMMER_INODE_RSV_INODES;
1920 * Finally, if the frontend is waiting for a flush to complete,
1923 if (ip->flush_state != HAMMER_FST_FLUSH) {
1924 if (ip->flags & HAMMER_INODE_FLUSHW) {
1925 ip->flags &= ~HAMMER_INODE_FLUSHW;
1930 hammer_rel_inode(ip, 0);
1934 * Called from hammer_sync_inode() to synchronize in-memory records
1938 hammer_sync_record_callback(hammer_record_t record, void *data)
1940 hammer_cursor_t cursor = data;
1941 hammer_transaction_t trans = cursor->trans;
1945 * Skip records that do not belong to the current flush.
1947 ++hammer_stats_record_iterations;
1948 if (record->flush_state != HAMMER_FST_FLUSH)
1952 if (record->flush_group != record->ip->flush_group) {
1953 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1958 KKASSERT(record->flush_group == record->ip->flush_group);
1961 * Interlock the record using the BE flag. Once BE is set the
1962 * frontend cannot change the state of FE.
1964 * NOTE: If FE is set prior to us setting BE we still sync the
1965 * record out, but the flush completion code converts it to
1966 * a delete-on-disk record instead of destroying it.
1968 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
1969 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1972 * The backend may have already disposed of the record.
1974 if (record->flags & HAMMER_RECF_DELETED_BE) {
1980 * If the whole inode is being deleting all on-disk records will
1981 * be deleted very soon, we can't sync any new records to disk
1982 * because they will be deleted in the same transaction they were
1983 * created in (delete_tid == create_tid), which will assert.
1985 * XXX There may be a case with RECORD_ADD with DELETED_FE set
1986 * that we currently panic on.
1988 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
1989 switch(record->type) {
1990 case HAMMER_MEM_RECORD_DATA:
1992 * We don't have to do anything, if the record was
1993 * committed the space will have been accounted for
1997 case HAMMER_MEM_RECORD_GENERAL:
1998 record->flags |= HAMMER_RECF_DELETED_FE;
1999 record->flags |= HAMMER_RECF_DELETED_BE;
2002 case HAMMER_MEM_RECORD_ADD:
2003 panic("hammer_sync_record_callback: illegal add "
2004 "during inode deletion record %p", record);
2005 break; /* NOT REACHED */
2006 case HAMMER_MEM_RECORD_INODE:
2007 panic("hammer_sync_record_callback: attempt to "
2008 "sync inode record %p?", record);
2009 break; /* NOT REACHED */
2010 case HAMMER_MEM_RECORD_DEL:
2012 * Follow through and issue the on-disk deletion
2019 * If DELETED_FE is set special handling is needed for directory
2020 * entries. Dependant pieces related to the directory entry may
2021 * have already been synced to disk. If this occurs we have to
2022 * sync the directory entry and then change the in-memory record
2023 * from an ADD to a DELETE to cover the fact that it's been
2024 * deleted by the frontend.
2026 * A directory delete covering record (MEM_RECORD_DEL) can never
2027 * be deleted by the frontend.
2029 * Any other record type (aka DATA) can be deleted by the frontend.
2030 * XXX At the moment the flusher must skip it because there may
2031 * be another data record in the flush group for the same block,
2032 * meaning that some frontend data changes can leak into the backend's
2033 * synchronization point.
2035 if (record->flags & HAMMER_RECF_DELETED_FE) {
2036 if (record->type == HAMMER_MEM_RECORD_ADD) {
2037 record->flags |= HAMMER_RECF_CONVERT_DELETE;
2039 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2040 record->flags |= HAMMER_RECF_DELETED_BE;
2047 * Assign the create_tid for new records. Deletions already
2048 * have the record's entire key properly set up.
2050 if (record->type != HAMMER_MEM_RECORD_DEL)
2051 record->leaf.base.create_tid = trans->tid;
2052 record->leaf.create_ts = trans->time32;
2054 error = hammer_ip_sync_record_cursor(cursor, record);
2055 if (error != EDEADLK)
2057 hammer_done_cursor(cursor);
2058 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2063 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2067 if (error != -ENOSPC) {
2068 kprintf("hammer_sync_record_callback: sync failed rec "
2069 "%p, error %d\n", record, error);
2070 Debugger("sync failed rec");
2074 hammer_flush_record_done(record, error);
2079 * XXX error handling
2082 hammer_sync_inode(hammer_inode_t ip)
2084 struct hammer_transaction trans;
2085 struct hammer_cursor cursor;
2086 hammer_node_t tmp_node;
2087 hammer_record_t depend;
2088 hammer_record_t next;
2089 int error, tmp_error;
2092 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2095 hammer_start_transaction_fls(&trans, ip->hmp);
2096 error = hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2101 * Any directory records referencing this inode which are not in
2102 * our current flush group must adjust our nlink count for the
2103 * purposes of synchronization to disk.
2105 * Records which are in our flush group can be unlinked from our
2106 * inode now, potentially allowing the inode to be physically
2109 * This cannot block.
2111 nlinks = ip->ino_data.nlinks;
2112 next = TAILQ_FIRST(&ip->target_list);
2113 while ((depend = next) != NULL) {
2114 next = TAILQ_NEXT(depend, target_entry);
2115 if (depend->flush_state == HAMMER_FST_FLUSH &&
2116 depend->flush_group == ip->hmp->flusher.act) {
2118 * If this is an ADD that was deleted by the frontend
2119 * the frontend nlinks count will have already been
2120 * decremented, but the backend is going to sync its
2121 * directory entry and must account for it. The
2122 * record will be converted to a delete-on-disk when
2125 * If the ADD was not deleted by the frontend we
2126 * can remove the dependancy from our target_list.
2128 if (depend->flags & HAMMER_RECF_DELETED_FE) {
2131 TAILQ_REMOVE(&ip->target_list, depend,
2133 depend->target_ip = NULL;
2135 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2137 * Not part of our flush group
2139 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2140 switch(depend->type) {
2141 case HAMMER_MEM_RECORD_ADD:
2144 case HAMMER_MEM_RECORD_DEL:
2154 * Set dirty if we had to modify the link count.
2156 if (ip->sync_ino_data.nlinks != nlinks) {
2157 KKASSERT((int64_t)nlinks >= 0);
2158 ip->sync_ino_data.nlinks = nlinks;
2159 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2163 * If there is a trunction queued destroy any data past the (aligned)
2164 * truncation point. Userland will have dealt with the buffer
2165 * containing the truncation point for us.
2167 * We don't flush pending frontend data buffers until after we've
2168 * dealt with the truncation.
2170 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2172 * Interlock trunc_off. The VOP front-end may continue to
2173 * make adjustments to it while we are blocked.
2176 off_t aligned_trunc_off;
2179 trunc_off = ip->sync_trunc_off;
2180 blkmask = hammer_blocksize(trunc_off) - 1;
2181 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2184 * Delete any whole blocks on-media. The front-end has
2185 * already cleaned out any partial block and made it
2186 * pending. The front-end may have updated trunc_off
2187 * while we were blocked so we only use sync_trunc_off.
2189 * This operation can blow out the buffer cache, EWOULDBLOCK
2190 * means we were unable to complete the deletion. The
2191 * deletion will update sync_trunc_off in that case.
2193 error = hammer_ip_delete_range(&cursor, ip,
2195 0x7FFFFFFFFFFFFFFFLL, 2);
2196 if (error == EWOULDBLOCK) {
2197 ip->flags |= HAMMER_INODE_WOULDBLOCK;
2199 goto defer_buffer_flush;
2203 Debugger("hammer_ip_delete_range errored");
2206 * Clear the truncation flag on the backend after we have
2207 * complete the deletions. Backend data is now good again
2208 * (including new records we are about to sync, below).
2210 * Leave sync_trunc_off intact. As we write additional
2211 * records the backend will update sync_trunc_off. This
2212 * tells the backend whether it can skip the overwrite
2213 * test. This should work properly even when the backend
2214 * writes full blocks where the truncation point straddles
2215 * the block because the comparison is against the base
2216 * offset of the record.
2218 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2219 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2225 * Now sync related records. These will typically be directory
2226 * entries or delete-on-disk records.
2228 * Not all records will be flushed, but clear XDIRTY anyway. We
2229 * will set it again in the frontend hammer_flush_inode_done()
2230 * if records remain.
2233 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2234 hammer_sync_record_callback, &cursor);
2240 hammer_cache_node(&ip->cache[1], cursor.node);
2243 * Re-seek for inode update, assuming our cache hasn't been ripped
2244 * out from under us.
2247 tmp_node = hammer_ref_node_safe(ip->hmp, &ip->cache[0], &error);
2249 hammer_cursor_downgrade(&cursor);
2250 hammer_lock_sh(&tmp_node->lock);
2251 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2252 hammer_cursor_seek(&cursor, tmp_node, 0);
2253 hammer_unlock(&tmp_node->lock);
2254 hammer_rel_node(tmp_node);
2260 * If we are deleting the inode the frontend had better not have
2261 * any active references on elements making up the inode.
2263 * The call to hammer_ip_delete_clean() cleans up auxillary records
2264 * but not DB or DATA records. Those must have already been deleted
2265 * by the normal truncation mechanic.
2267 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2268 RB_EMPTY(&ip->rec_tree) &&
2269 (ip->sync_flags & HAMMER_INODE_DELETING) &&
2270 (ip->flags & HAMMER_INODE_DELETED) == 0) {
2273 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2275 ip->flags |= HAMMER_INODE_DELETED;
2276 ip->sync_flags &= ~HAMMER_INODE_DELETING;
2277 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2278 KKASSERT(RB_EMPTY(&ip->rec_tree));
2281 * Set delete_tid in both the frontend and backend
2282 * copy of the inode record. The DELETED flag handles
2283 * this, do not set RDIRTY.
2285 ip->ino_leaf.base.delete_tid = trans.tid;
2286 ip->sync_ino_leaf.base.delete_tid = trans.tid;
2287 ip->ino_leaf.delete_ts = trans.time32;
2288 ip->sync_ino_leaf.delete_ts = trans.time32;
2292 * Adjust the inode count in the volume header
2294 if (ip->flags & HAMMER_INODE_ONDISK) {
2295 hammer_modify_volume_field(&trans,
2298 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2299 hammer_modify_volume_done(trans.rootvol);
2302 Debugger("hammer_ip_delete_clean errored");
2306 ip->sync_flags &= ~HAMMER_INODE_BUFS;
2309 Debugger("RB_SCAN errored");
2313 * Now update the inode's on-disk inode-data and/or on-disk record.
2314 * DELETED and ONDISK are managed only in ip->flags.
2316 * In the case of a defered buffer flush we still update the on-disk
2317 * inode to satisfy visibility requirements if there happen to be
2318 * directory dependancies.
2320 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2321 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2323 * If deleted and on-disk, don't set any additional flags.
2324 * the delete flag takes care of things.
2326 * Clear flags which may have been set by the frontend.
2328 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2329 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2330 HAMMER_INODE_DELETING);
2332 case HAMMER_INODE_DELETED:
2334 * Take care of the case where a deleted inode was never
2335 * flushed to the disk in the first place.
2337 * Clear flags which may have been set by the frontend.
2339 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2340 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2341 HAMMER_INODE_DELETING);
2342 while (RB_ROOT(&ip->rec_tree)) {
2343 hammer_record_t record = RB_ROOT(&ip->rec_tree);
2344 hammer_ref(&record->lock);
2345 KKASSERT(record->lock.refs == 1);
2346 record->flags |= HAMMER_RECF_DELETED_FE;
2347 record->flags |= HAMMER_RECF_DELETED_BE;
2348 hammer_rel_mem_record(record);
2351 case HAMMER_INODE_ONDISK:
2353 * If already on-disk, do not set any additional flags.
2358 * If not on-disk and not deleted, set DDIRTY to force
2359 * an initial record to be written.
2361 * Also set the create_tid in both the frontend and backend
2362 * copy of the inode record.
2364 ip->ino_leaf.base.create_tid = trans.tid;
2365 ip->ino_leaf.create_ts = trans.time32;
2366 ip->sync_ino_leaf.base.create_tid = trans.tid;
2367 ip->sync_ino_leaf.create_ts = trans.time32;
2368 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2373 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2374 * is already on-disk the old record is marked as deleted.
2376 * If DELETED is set hammer_update_inode() will delete the existing
2377 * record without writing out a new one.
2379 * If *ONLY* the ITIMES flag is set we can update the record in-place.
2381 if (ip->flags & HAMMER_INODE_DELETED) {
2382 error = hammer_update_inode(&cursor, ip);
2384 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2385 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2386 error = hammer_update_itimes(&cursor, ip);
2388 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2389 error = hammer_update_inode(&cursor, ip);
2392 Debugger("hammer_update_itimes/inode errored");
2395 * Save the TID we used to sync the inode with to make sure we
2396 * do not improperly reuse it.
2398 hammer_done_cursor(&cursor);
2399 hammer_done_transaction(&trans);
2404 * This routine is called when the OS is no longer actively referencing
2405 * the inode (but might still be keeping it cached), or when releasing
2406 * the last reference to an inode.
2408 * At this point if the inode's nlinks count is zero we want to destroy
2409 * it, which may mean destroying it on-media too.
2412 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2417 * Set the DELETING flag when the link count drops to 0 and the
2418 * OS no longer has any opens on the inode.
2420 * The backend will clear DELETING (a mod flag) and set DELETED
2421 * (a state flag) when it is actually able to perform the
2424 if (ip->ino_data.nlinks == 0 &&
2425 (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2426 ip->flags |= HAMMER_INODE_DELETING;
2427 ip->flags |= HAMMER_INODE_TRUNCATED;
2431 if (hammer_get_vnode(ip, &vp) != 0)
2439 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2440 vnode_pager_setsize(ip->vp, 0);
2449 * Re-test an inode when a dependancy had gone away to see if we
2450 * can chain flush it.
2453 hammer_test_inode(hammer_inode_t ip)
2455 if (ip->flags & HAMMER_INODE_REFLUSH) {
2456 ip->flags &= ~HAMMER_INODE_REFLUSH;
2457 hammer_ref(&ip->lock);
2458 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2459 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2460 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2462 hammer_flush_inode(ip, 0);
2464 hammer_rel_inode(ip, 0);
2469 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2470 * reassociated with a vp or just before it gets freed.
2472 * Wakeup one thread blocked waiting on reclaims to complete. Note that
2473 * the inode the thread is waiting on behalf of is a different inode then
2474 * the inode we are called with. This is to create a pipeline.
2477 hammer_inode_wakereclaims(hammer_inode_t ip)
2479 struct hammer_reclaim *reclaim;
2480 hammer_mount_t hmp = ip->hmp;
2482 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
2485 --hammer_count_reclaiming;
2486 --hmp->inode_reclaims;
2487 ip->flags &= ~HAMMER_INODE_RECLAIM;
2489 if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
2490 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
2491 reclaim->okydoky = 1;
2497 * Setup our reclaim pipeline. We only let so many detached (and dirty)
2498 * inodes build up before we start blocking.
2500 * When we block we don't care *which* inode has finished reclaiming,
2501 * as lone as one does. This is somewhat heuristical... we also put a
2502 * cap on how long we are willing to wait.
2505 hammer_inode_waitreclaims(hammer_mount_t hmp)
2507 struct hammer_reclaim reclaim;
2510 if (hmp->inode_reclaims > HAMMER_RECLAIM_WAIT) {
2511 reclaim.okydoky = 0;
2512 TAILQ_INSERT_TAIL(&hmp->reclaim_list,
2515 reclaim.okydoky = 1;
2518 if (reclaim.okydoky == 0) {
2519 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
2520 HAMMER_RECLAIM_WAIT;
2522 tsleep(&reclaim, 0, "hmrrcm", delay + 1);
2523 if (reclaim.okydoky == 0)
2524 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);