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.81 2008/06/21 20:21:58 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_inode_t ip);
46 static int hammer_setup_parent_inodes_helper(hammer_record_t record);
47 static void hammer_inode_wakereclaims(hammer_inode_t ip);
50 extern struct hammer_inode *HammerTruncIp;
54 * Red-Black 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)
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)
118 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
119 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
120 hammer_inode_info_cmp, hammer_inode_info_t);
123 * The kernel is not actively referencing this vnode but is still holding
126 * This is called from the frontend.
129 hammer_vop_inactive(struct vop_inactive_args *ap)
131 struct hammer_inode *ip = VTOI(ap->a_vp);
142 * If the inode no longer has visibility in the filesystem try to
143 * recycle it immediately, even if the inode is dirty. Recycling
144 * it quickly allows the system to reclaim buffer cache and VM
145 * resources which can matter a lot in a heavily loaded system.
147 * This can deadlock in vfsync() if we aren't careful.
149 * Do not queue the inode to the flusher if we still have visibility,
150 * otherwise namespace calls such as chmod will unnecessarily generate
151 * multiple inode updates.
153 hammer_inode_unloadable_check(ip, 0);
154 if (ip->ino_data.nlinks == 0) {
155 if (ip->flags & HAMMER_INODE_MODMASK)
156 hammer_flush_inode(ip, 0);
163 * Release the vnode association. This is typically (but not always)
164 * the last reference on the inode.
166 * Once the association is lost we are on our own with regards to
167 * flushing the inode.
170 hammer_vop_reclaim(struct vop_reclaim_args *ap)
172 struct hammer_inode *ip;
178 if ((ip = vp->v_data) != NULL) {
183 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
184 ++hammer_count_reclaiming;
185 ++hmp->inode_reclaims;
186 ip->flags |= HAMMER_INODE_RECLAIM;
187 if (hmp->inode_reclaims > HAMMER_RECLAIM_FLUSH &&
188 (hmp->inode_reclaims & 255) == 0) {
189 hammer_flusher_async(hmp);
192 hammer_rel_inode(ip, 1);
198 * Return a locked vnode for the specified inode. The inode must be
199 * referenced but NOT LOCKED on entry and will remain referenced on
202 * Called from the frontend.
205 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
214 if ((vp = ip->vp) == NULL) {
215 error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
218 hammer_lock_ex(&ip->lock);
219 if (ip->vp != NULL) {
220 hammer_unlock(&ip->lock);
225 hammer_ref(&ip->lock);
229 hammer_get_vnode_type(ip->ino_data.obj_type);
231 hammer_inode_wakereclaims(ip);
233 switch(ip->ino_data.obj_type) {
234 case HAMMER_OBJTYPE_CDEV:
235 case HAMMER_OBJTYPE_BDEV:
236 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
237 addaliasu(vp, ip->ino_data.rmajor,
238 ip->ino_data.rminor);
240 case HAMMER_OBJTYPE_FIFO:
241 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
248 * Only mark as the root vnode if the ip is not
249 * historical, otherwise the VFS cache will get
250 * confused. The other half of the special handling
251 * is in hammer_vop_nlookupdotdot().
253 * Pseudo-filesystem roots also do not count.
255 if (ip->obj_id == HAMMER_OBJID_ROOT &&
256 ip->obj_asof == hmp->asof &&
257 ip->obj_localization == 0) {
261 vp->v_data = (void *)ip;
262 /* vnode locked by getnewvnode() */
263 /* make related vnode dirty if inode dirty? */
264 hammer_unlock(&ip->lock);
265 if (vp->v_type == VREG)
266 vinitvmio(vp, ip->ino_data.size);
271 * loop if the vget fails (aka races), or if the vp
272 * no longer matches ip->vp.
274 if (vget(vp, LK_EXCLUSIVE) == 0) {
285 * Locate all copies of the inode for obj_id compatible with the specified
286 * asof, reference, and issue the related call-back. This routine is used
287 * for direct-io invalidation and does not create any new inodes.
290 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
291 int (*callback)(hammer_inode_t ip, void *data),
294 hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
295 hammer_inode_info_cmp_all_history,
300 * Acquire a HAMMER inode. The returned inode is not locked. These functions
301 * do not attach or detach the related vnode (use hammer_get_vnode() for
304 * The flags argument is only applied for newly created inodes, and only
305 * certain flags are inherited.
307 * Called from the frontend.
309 struct hammer_inode *
310 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
311 u_int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
312 int flags, int *errorp)
314 hammer_mount_t hmp = trans->hmp;
315 struct hammer_inode_info iinfo;
316 struct hammer_cursor cursor;
317 struct hammer_inode *ip;
320 * Determine if we already have an inode cached. If we do then
323 iinfo.obj_id = obj_id;
324 iinfo.obj_asof = asof;
325 iinfo.obj_localization = localization;
327 ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
329 hammer_ref(&ip->lock);
335 * Allocate a new inode structure and deal with races later.
337 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
338 ++hammer_count_inodes;
341 ip->obj_asof = iinfo.obj_asof;
342 ip->obj_localization = localization;
344 ip->flags = flags & HAMMER_INODE_RO;
345 ip->cache[0].ip = ip;
346 ip->cache[1].ip = ip;
348 ip->flags |= HAMMER_INODE_RO;
349 ip->sync_trunc_off = ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
350 RB_INIT(&ip->rec_tree);
351 TAILQ_INIT(&ip->target_list);
354 * Locate the on-disk inode.
357 hammer_init_cursor(trans, &cursor, (dip ? &dip->cache[0] : NULL), NULL);
358 cursor.key_beg.localization = HAMMER_LOCALIZE_INODE;
359 cursor.key_beg.obj_id = ip->obj_id;
360 cursor.key_beg.key = 0;
361 cursor.key_beg.create_tid = 0;
362 cursor.key_beg.delete_tid = 0;
363 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
364 cursor.key_beg.obj_type = 0;
365 cursor.asof = iinfo.obj_asof;
366 cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
369 *errorp = hammer_btree_lookup(&cursor);
370 if (*errorp == EDEADLK) {
371 hammer_done_cursor(&cursor);
376 * On success the B-Tree lookup will hold the appropriate
377 * buffer cache buffers and provide a pointer to the requested
378 * information. Copy the information to the in-memory inode
379 * and cache the B-Tree node to improve future operations.
382 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
383 ip->ino_data = cursor.data->inode;
386 * cache[0] tries to cache the location of the object inode.
387 * The assumption is that it is near the directory inode.
389 * cache[1] tries to cache the location of the object data.
390 * The assumption is that it is near the directory data.
392 hammer_cache_node(&ip->cache[0], cursor.node);
393 if (dip && dip->cache[1].node)
394 hammer_cache_node(&ip->cache[1], dip->cache[1].node);
397 * The file should not contain any data past the file size
398 * stored in the inode. Setting sync_trunc_off to the
399 * file size instead of max reduces B-Tree lookup overheads
400 * on append by allowing the flusher to avoid checking for
403 ip->sync_trunc_off = ip->ino_data.size;
407 * The inode is placed on the red-black tree and will be synced to
408 * the media when flushed or by the filesystem sync. If this races
409 * another instantiation/lookup the insertion will fail.
412 hammer_ref(&ip->lock);
413 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
414 hammer_uncache_node(&ip->cache[0]);
415 hammer_uncache_node(&ip->cache[1]);
416 KKASSERT(ip->lock.refs == 1);
417 --hammer_count_inodes;
420 hammer_done_cursor(&cursor);
423 ip->flags |= HAMMER_INODE_ONDISK;
426 * Do not panic on read-only accesses which fail, particularly
427 * historical accesses where the snapshot might not have
428 * complete connectivity.
430 if ((flags & HAMMER_INODE_RO) == 0) {
431 kprintf("hammer_get_inode: failed ip %p obj_id %016llx cursor %p error %d\n",
432 ip, ip->obj_id, &cursor, *errorp);
435 if (ip->flags & HAMMER_INODE_RSV_INODES) {
436 ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
439 hmp->rsv_databufs -= ip->rsv_databufs;
440 ip->rsv_databufs = 0; /* sanity */
442 --hammer_count_inodes;
447 hammer_done_cursor(&cursor);
452 * Create a new filesystem object, returning the inode in *ipp. The
453 * returned inode will be referenced.
455 * The inode is created in-memory.
458 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
459 struct ucred *cred, hammer_inode_t dip,
460 struct hammer_inode **ipp)
467 ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
468 ++hammer_count_inodes;
470 ip->obj_id = hammer_alloc_objid(trans, dip);
471 KKASSERT(ip->obj_id != 0);
472 ip->obj_asof = hmp->asof;
473 ip->obj_localization = dip->obj_localization;
475 ip->flush_state = HAMMER_FST_IDLE;
476 ip->flags = HAMMER_INODE_DDIRTY |
477 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
478 ip->cache[0].ip = ip;
479 ip->cache[1].ip = ip;
481 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
482 RB_INIT(&ip->rec_tree);
483 TAILQ_INIT(&ip->target_list);
485 ip->ino_data.atime = trans->time;
486 ip->ino_data.mtime = trans->time;
487 ip->ino_data.size = 0;
488 ip->ino_data.nlinks = 0;
491 * A nohistory designator on the parent directory is inherited by
494 ip->ino_data.uflags = dip->ino_data.uflags &
495 (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
497 ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
498 ip->ino_leaf.base.localization = HAMMER_LOCALIZE_INODE;
499 ip->ino_leaf.base.obj_id = ip->obj_id;
500 ip->ino_leaf.base.key = 0;
501 ip->ino_leaf.base.create_tid = 0;
502 ip->ino_leaf.base.delete_tid = 0;
503 ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
504 ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
506 ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
507 ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
508 ip->ino_data.mode = vap->va_mode;
509 ip->ino_data.ctime = trans->time;
510 ip->ino_data.parent_obj_id = (dip) ? dip->ino_leaf.base.obj_id : 0;
512 switch(ip->ino_leaf.base.obj_type) {
513 case HAMMER_OBJTYPE_CDEV:
514 case HAMMER_OBJTYPE_BDEV:
515 ip->ino_data.rmajor = vap->va_rmajor;
516 ip->ino_data.rminor = vap->va_rminor;
523 * Calculate default uid/gid and overwrite with information from
526 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
527 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode, xuid, cred,
529 ip->ino_data.mode = vap->va_mode;
531 if (vap->va_vaflags & VA_UID_UUID_VALID)
532 ip->ino_data.uid = vap->va_uid_uuid;
533 else if (vap->va_uid != (uid_t)VNOVAL)
534 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
536 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
538 if (vap->va_vaflags & VA_GID_UUID_VALID)
539 ip->ino_data.gid = vap->va_gid_uuid;
540 else if (vap->va_gid != (gid_t)VNOVAL)
541 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
543 ip->ino_data.gid = dip->ino_data.gid;
545 hammer_ref(&ip->lock);
546 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
547 hammer_unref(&ip->lock);
548 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
555 * Called by hammer_sync_inode().
558 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
560 hammer_transaction_t trans = cursor->trans;
561 hammer_record_t record;
568 * If the inode has a presence on-disk then locate it and mark
569 * it deleted, setting DELONDISK.
571 * The record may or may not be physically deleted, depending on
572 * the retention policy.
574 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
575 HAMMER_INODE_ONDISK) {
576 hammer_normalize_cursor(cursor);
577 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
578 cursor->key_beg.obj_id = ip->obj_id;
579 cursor->key_beg.key = 0;
580 cursor->key_beg.create_tid = 0;
581 cursor->key_beg.delete_tid = 0;
582 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
583 cursor->key_beg.obj_type = 0;
584 cursor->asof = ip->obj_asof;
585 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
586 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
587 cursor->flags |= HAMMER_CURSOR_BACKEND;
589 error = hammer_btree_lookup(cursor);
590 if (hammer_debug_inode)
591 kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
593 kprintf("error %d\n", error);
594 Debugger("hammer_update_inode");
598 error = hammer_ip_delete_record(cursor, ip, trans->tid);
599 if (hammer_debug_inode)
600 kprintf(" error %d\n", error);
601 if (error && error != EDEADLK) {
602 kprintf("error %d\n", error);
603 Debugger("hammer_update_inode2");
606 ip->flags |= HAMMER_INODE_DELONDISK;
609 hammer_cache_node(&ip->cache[0], cursor->node);
611 if (error == EDEADLK) {
612 hammer_done_cursor(cursor);
613 error = hammer_init_cursor(trans, cursor,
615 if (hammer_debug_inode)
616 kprintf("IPDED %p %d\n", ip, error);
623 * Ok, write out the initial record or a new record (after deleting
624 * the old one), unless the DELETED flag is set. This routine will
625 * clear DELONDISK if it writes out a record.
627 * Update our inode statistics if this is the first application of
630 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
632 * Generate a record and write it to the media
634 record = hammer_alloc_mem_record(ip, 0);
635 record->type = HAMMER_MEM_RECORD_INODE;
636 record->flush_state = HAMMER_FST_FLUSH;
637 record->leaf = ip->sync_ino_leaf;
638 record->leaf.base.create_tid = trans->tid;
639 record->leaf.data_len = sizeof(ip->sync_ino_data);
640 record->data = (void *)&ip->sync_ino_data;
641 record->flags |= HAMMER_RECF_INTERLOCK_BE;
643 error = hammer_ip_sync_record_cursor(cursor, record);
644 if (hammer_debug_inode)
645 kprintf("GENREC %p rec %08x %d\n",
646 ip, record->flags, error);
647 if (error != EDEADLK)
649 hammer_done_cursor(cursor);
650 error = hammer_init_cursor(trans, cursor,
652 if (hammer_debug_inode)
653 kprintf("GENREC reinit %d\n", error);
658 kprintf("error %d\n", error);
659 Debugger("hammer_update_inode3");
663 * The record isn't managed by the inode's record tree,
664 * destroy it whether we succeed or fail.
666 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
667 record->flags |= HAMMER_RECF_DELETED_FE;
668 record->flush_state = HAMMER_FST_IDLE;
669 hammer_rel_mem_record(record);
675 if (hammer_debug_inode)
676 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
677 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
680 ip->flags &= ~HAMMER_INODE_DELONDISK;
683 * Root volume count of inodes
685 if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
686 hammer_modify_volume_field(trans,
689 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
690 hammer_modify_volume_done(trans->rootvol);
691 ip->flags |= HAMMER_INODE_ONDISK;
692 if (hammer_debug_inode)
693 kprintf("NOWONDISK %p\n", ip);
699 * If the inode has been destroyed, clean out any left-over flags
700 * that may have been set by the frontend.
702 if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) {
703 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
711 * Update only the itimes fields.
713 * ATIME can be updated without generating any UNDO. MTIME is updated
714 * with UNDO so it is guaranteed to be synchronized properly in case of
717 * Neither field is included in the B-Tree leaf element's CRC, which is how
718 * we can get away with updating ATIME the way we do.
721 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
723 hammer_transaction_t trans = cursor->trans;
727 if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
728 HAMMER_INODE_ONDISK) {
732 hammer_normalize_cursor(cursor);
733 cursor->key_beg.localization = HAMMER_LOCALIZE_INODE;
734 cursor->key_beg.obj_id = ip->obj_id;
735 cursor->key_beg.key = 0;
736 cursor->key_beg.create_tid = 0;
737 cursor->key_beg.delete_tid = 0;
738 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
739 cursor->key_beg.obj_type = 0;
740 cursor->asof = ip->obj_asof;
741 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
742 cursor->flags |= HAMMER_CURSOR_ASOF;
743 cursor->flags |= HAMMER_CURSOR_GET_LEAF;
744 cursor->flags |= HAMMER_CURSOR_GET_DATA;
745 cursor->flags |= HAMMER_CURSOR_BACKEND;
747 error = hammer_btree_lookup(cursor);
749 kprintf("error %d\n", error);
750 Debugger("hammer_update_itimes1");
753 hammer_cache_node(&ip->cache[0], cursor->node);
754 if (ip->sync_flags & HAMMER_INODE_MTIME) {
756 * Updating MTIME requires an UNDO. Just cover
757 * both atime and mtime.
759 hammer_modify_buffer(trans, cursor->data_buffer,
760 HAMMER_ITIMES_BASE(&cursor->data->inode),
761 HAMMER_ITIMES_BYTES);
762 cursor->data->inode.atime = ip->sync_ino_data.atime;
763 cursor->data->inode.mtime = ip->sync_ino_data.mtime;
764 hammer_modify_buffer_done(cursor->data_buffer);
765 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
767 * Updating atime only can be done in-place with
770 hammer_modify_buffer(trans, cursor->data_buffer,
772 cursor->data->inode.atime = ip->sync_ino_data.atime;
773 hammer_modify_buffer_done(cursor->data_buffer);
775 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
777 if (error == EDEADLK) {
778 hammer_done_cursor(cursor);
779 error = hammer_init_cursor(trans, cursor,
788 * Release a reference on an inode, flush as requested.
790 * On the last reference we queue the inode to the flusher for its final
794 hammer_rel_inode(struct hammer_inode *ip, int flush)
796 hammer_mount_t hmp = ip->hmp;
799 * Handle disposition when dropping the last ref.
802 if (ip->lock.refs == 1) {
804 * Determine whether on-disk action is needed for
805 * the inode's final disposition.
807 KKASSERT(ip->vp == NULL);
808 hammer_inode_unloadable_check(ip, 0);
809 if (ip->flags & HAMMER_INODE_MODMASK) {
810 if (hmp->rsv_inodes > desiredvnodes) {
811 hammer_flush_inode(ip,
812 HAMMER_FLUSH_SIGNAL);
814 hammer_flush_inode(ip, 0);
816 } else if (ip->lock.refs == 1) {
817 hammer_unload_inode(ip);
822 hammer_flush_inode(ip, 0);
825 * The inode still has multiple refs, try to drop
828 KKASSERT(ip->lock.refs >= 1);
829 if (ip->lock.refs > 1) {
830 hammer_unref(&ip->lock);
838 * Unload and destroy the specified inode. Must be called with one remaining
839 * reference. The reference is disposed of.
841 * This can only be called in the context of the flusher.
844 hammer_unload_inode(struct hammer_inode *ip)
846 hammer_mount_t hmp = ip->hmp;
848 KASSERT(ip->lock.refs == 1,
849 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
850 KKASSERT(ip->vp == NULL);
851 KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
852 KKASSERT(ip->cursor_ip_refs == 0);
853 KKASSERT(ip->lock.lockcount == 0);
854 KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
856 KKASSERT(RB_EMPTY(&ip->rec_tree));
857 KKASSERT(TAILQ_EMPTY(&ip->target_list));
859 RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
861 hammer_uncache_node(&ip->cache[0]);
862 hammer_uncache_node(&ip->cache[1]);
864 hammer_clear_objid(ip);
865 --hammer_count_inodes;
868 hammer_inode_wakereclaims(ip);
875 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
876 * the read-only flag for cached inodes.
878 * This routine is called from a RB_SCAN().
881 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
883 hammer_mount_t hmp = ip->hmp;
885 if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
886 ip->flags |= HAMMER_INODE_RO;
888 ip->flags &= ~HAMMER_INODE_RO;
893 * A transaction has modified an inode, requiring updates as specified by
896 * HAMMER_INODE_DDIRTY: Inode data has been updated
897 * HAMMER_INODE_XDIRTY: Dirty in-memory records
898 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
899 * HAMMER_INODE_DELETED: Inode record/data must be deleted
900 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
903 hammer_modify_inode(hammer_inode_t ip, int flags)
905 KKASSERT ((ip->flags & HAMMER_INODE_RO) == 0 ||
906 (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
907 HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
908 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
909 if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
910 ip->flags |= HAMMER_INODE_RSV_INODES;
911 ++ip->hmp->rsv_inodes;
918 * Request that an inode be flushed. This whole mess cannot block and may
919 * recurse (if not synchronous). Once requested HAMMER will attempt to
920 * actively flush the inode until the flush can be done.
922 * The inode may already be flushing, or may be in a setup state. We can
923 * place the inode in a flushing state if it is currently idle and flag it
924 * to reflush if it is currently flushing.
926 * If the HAMMER_FLUSH_SYNCHRONOUS flag is specified we will attempt to
927 * flush the indoe synchronously using the caller's context.
930 hammer_flush_inode(hammer_inode_t ip, int flags)
935 * Trivial 'nothing to flush' case. If the inode is ina SETUP
936 * state we have to put it back into an IDLE state so we can
937 * drop the extra ref.
939 if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
940 if (ip->flush_state == HAMMER_FST_SETUP) {
941 ip->flush_state = HAMMER_FST_IDLE;
942 hammer_rel_inode(ip, 0);
948 * Our flush action will depend on the current state.
950 switch(ip->flush_state) {
951 case HAMMER_FST_IDLE:
953 * We have no dependancies and can flush immediately. Some
954 * our children may not be flushable so we have to re-test
955 * with that additional knowledge.
957 hammer_flush_inode_core(ip, flags);
959 case HAMMER_FST_SETUP:
961 * Recurse upwards through dependancies via target_list
962 * and start their flusher actions going if possible.
964 * 'good' is our connectivity. -1 means we have none and
965 * can't flush, 0 means there weren't any dependancies, and
966 * 1 means we have good connectivity.
968 good = hammer_setup_parent_inodes(ip);
971 * We can continue if good >= 0. Determine how many records
972 * under our inode can be flushed (and mark them).
975 hammer_flush_inode_core(ip, flags);
977 ip->flags |= HAMMER_INODE_REFLUSH;
978 if (flags & HAMMER_FLUSH_SIGNAL) {
979 ip->flags |= HAMMER_INODE_RESIGNAL;
980 hammer_flusher_async(ip->hmp);
986 * We are already flushing, flag the inode to reflush
987 * if needed after it completes its current flush.
989 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
990 ip->flags |= HAMMER_INODE_REFLUSH;
991 if (flags & HAMMER_FLUSH_SIGNAL) {
992 ip->flags |= HAMMER_INODE_RESIGNAL;
993 hammer_flusher_async(ip->hmp);
1000 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1001 * ip which reference our ip.
1003 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1004 * so for now do not ref/deref the structures. Note that if we use the
1005 * ref/rel code later, the rel CAN block.
1008 hammer_setup_parent_inodes(hammer_inode_t ip)
1010 hammer_record_t depend;
1012 hammer_record_t next;
1019 TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1020 r = hammer_setup_parent_inodes_helper(depend);
1021 KKASSERT(depend->target_ip == ip);
1022 if (r < 0 && good == 0)
1032 next = TAILQ_FIRST(&ip->target_list);
1034 hammer_ref(&next->lock);
1035 hammer_ref(&next->ip->lock);
1037 while ((depend = next) != NULL) {
1038 if (depend->target_ip == NULL) {
1040 hammer_rel_mem_record(depend);
1041 hammer_rel_inode(pip, 0);
1044 KKASSERT(depend->target_ip == ip);
1045 next = TAILQ_NEXT(depend, target_entry);
1047 hammer_ref(&next->lock);
1048 hammer_ref(&next->ip->lock);
1050 r = hammer_setup_parent_inodes_helper(depend);
1051 if (r < 0 && good == 0)
1056 hammer_rel_mem_record(depend);
1057 hammer_rel_inode(pip, 0);
1064 * This helper function takes a record representing the dependancy between
1065 * the parent inode and child inode.
1067 * record->ip = parent inode
1068 * record->target_ip = child inode
1070 * We are asked to recurse upwards and convert the record from SETUP
1071 * to FLUSH if possible.
1073 * Return 1 if the record gives us connectivity
1075 * Return 0 if the record is not relevant
1077 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1080 hammer_setup_parent_inodes_helper(hammer_record_t record)
1086 KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1091 * If the record is already flushing, is it in our flush group?
1093 * If it is in our flush group but it is a general record or a
1094 * delete-on-disk, it does not improve our connectivity (return 0),
1095 * and if the target inode is not trying to destroy itself we can't
1096 * allow the operation yet anyway (the second return -1).
1098 if (record->flush_state == HAMMER_FST_FLUSH) {
1099 if (record->flush_group != hmp->flusher.next) {
1100 pip->flags |= HAMMER_INODE_REFLUSH;
1103 if (record->type == HAMMER_MEM_RECORD_ADD)
1105 /* GENERAL or DEL */
1110 * It must be a setup record. Try to resolve the setup dependancies
1111 * by recursing upwards so we can place ip on the flush list.
1113 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1115 good = hammer_setup_parent_inodes(pip);
1118 * We can't flush ip because it has no connectivity (XXX also check
1119 * nlinks for pre-existing connectivity!). Flag it so any resolution
1120 * recurses back down.
1123 pip->flags |= HAMMER_INODE_REFLUSH;
1128 * We are go, place the parent inode in a flushing state so we can
1129 * place its record in a flushing state. Note that the parent
1130 * may already be flushing. The record must be in the same flush
1131 * group as the parent.
1133 if (pip->flush_state != HAMMER_FST_FLUSH)
1134 hammer_flush_inode_core(pip, HAMMER_FLUSH_RECURSION);
1135 KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1136 KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1139 if (record->type == HAMMER_MEM_RECORD_DEL &&
1140 (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1142 * Regardless of flushing state we cannot sync this path if the
1143 * record represents a delete-on-disk but the target inode
1144 * is not ready to sync its own deletion.
1146 * XXX need to count effective nlinks to determine whether
1147 * the flush is ok, otherwise removing a hardlink will
1148 * just leave the DEL record to rot.
1150 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1154 if (pip->flush_group == pip->hmp->flusher.next) {
1156 * This is the record we wanted to synchronize. If the
1157 * record went into a flush state while we blocked it
1158 * had better be in the correct flush group.
1160 if (record->flush_state != HAMMER_FST_FLUSH) {
1161 record->flush_state = HAMMER_FST_FLUSH;
1162 record->flush_group = pip->flush_group;
1163 hammer_ref(&record->lock);
1165 KKASSERT(record->flush_group == pip->flush_group);
1167 if (record->type == HAMMER_MEM_RECORD_ADD)
1171 * A general or delete-on-disk record does not contribute
1172 * to our visibility. We can still flush it, however.
1177 * We couldn't resolve the dependancies, request that the
1178 * inode be flushed when the dependancies can be resolved.
1180 pip->flags |= HAMMER_INODE_REFLUSH;
1186 * This is the core routine placing an inode into the FST_FLUSH state.
1189 hammer_flush_inode_core(hammer_inode_t ip, int flags)
1194 * Set flush state and prevent the flusher from cycling into
1195 * the next flush group. Do not place the ip on the list yet.
1196 * Inodes not in the idle state get an extra reference.
1198 KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1199 if (ip->flush_state == HAMMER_FST_IDLE)
1200 hammer_ref(&ip->lock);
1201 ip->flush_state = HAMMER_FST_FLUSH;
1202 ip->flush_group = ip->hmp->flusher.next;
1203 ++ip->hmp->flusher.group_lock;
1204 ++ip->hmp->count_iqueued;
1205 ++hammer_count_iqueued;
1208 * We need to be able to vfsync/truncate from the backend.
1210 KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1211 if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1212 ip->flags |= HAMMER_INODE_VHELD;
1217 * Figure out how many in-memory records we can actually flush
1218 * (not including inode meta-data, buffers, etc).
1220 if (flags & HAMMER_FLUSH_RECURSION) {
1223 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1224 hammer_setup_child_callback, NULL);
1228 * This is a more involved test that includes go_count. If we
1229 * can't flush, flag the inode and return. If go_count is 0 we
1230 * were are unable to flush any records in our rec_tree and
1231 * must ignore the XDIRTY flag.
1233 if (go_count == 0) {
1234 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1235 ip->flags |= HAMMER_INODE_REFLUSH;
1237 --ip->hmp->count_iqueued;
1238 --hammer_count_iqueued;
1240 ip->flush_state = HAMMER_FST_SETUP;
1241 if (ip->flags & HAMMER_INODE_VHELD) {
1242 ip->flags &= ~HAMMER_INODE_VHELD;
1245 if (flags & HAMMER_FLUSH_SIGNAL) {
1246 ip->flags |= HAMMER_INODE_RESIGNAL;
1247 hammer_flusher_async(ip->hmp);
1249 if (--ip->hmp->flusher.group_lock == 0)
1250 wakeup(&ip->hmp->flusher.group_lock);
1256 * Snapshot the state of the inode for the backend flusher.
1258 * The truncation must be retained in the frontend until after
1259 * we've actually performed the record deletion.
1261 * We continue to retain sync_trunc_off even when all truncations
1262 * have been resolved as an optimization to determine if we can
1263 * skip the B-Tree lookup for overwrite deletions.
1265 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1266 * and stays in ip->flags. Once set, it stays set until the
1267 * inode is destroyed.
1269 ip->sync_flags = (ip->flags & HAMMER_INODE_MODMASK);
1270 if (ip->sync_flags & HAMMER_INODE_TRUNCATED)
1271 ip->sync_trunc_off = ip->trunc_off;
1272 ip->sync_ino_leaf = ip->ino_leaf;
1273 ip->sync_ino_data = ip->ino_data;
1274 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1275 ip->flags &= ~HAMMER_INODE_MODMASK;
1276 #ifdef DEBUG_TRUNCATE
1277 if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1278 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1282 * The flusher list inherits our inode and reference.
1284 TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
1285 if (--ip->hmp->flusher.group_lock == 0)
1286 wakeup(&ip->hmp->flusher.group_lock);
1288 if (flags & HAMMER_FLUSH_SIGNAL) {
1289 hammer_flusher_async(ip->hmp);
1294 * Callback for scan of ip->rec_tree. Try to include each record in our
1295 * flush. ip->flush_group has been set but the inode has not yet been
1296 * moved into a flushing state.
1298 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1301 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1302 * the caller from shortcutting the flush.
1305 hammer_setup_child_callback(hammer_record_t rec, void *data)
1307 hammer_inode_t target_ip;
1312 * Deleted records are ignored. Note that the flush detects deleted
1313 * front-end records at multiple points to deal with races. This is
1314 * just the first line of defense. The only time DELETED_FE cannot
1315 * be set is when HAMMER_RECF_INTERLOCK_BE is set.
1317 * Don't get confused between record deletion and, say, directory
1318 * entry deletion. The deletion of a directory entry that is on
1319 * the media has nothing to do with the record deletion flags.
1321 if (rec->flags & (HAMMER_RECF_DELETED_FE|HAMMER_RECF_DELETED_BE))
1325 * If the record is in an idle state it has no dependancies and
1331 switch(rec->flush_state) {
1332 case HAMMER_FST_IDLE:
1334 * Record has no setup dependancy, we can flush it.
1336 KKASSERT(rec->target_ip == NULL);
1337 rec->flush_state = HAMMER_FST_FLUSH;
1338 rec->flush_group = ip->flush_group;
1339 hammer_ref(&rec->lock);
1342 case HAMMER_FST_SETUP:
1344 * Record has a setup dependancy. Try to include the
1345 * target ip in the flush.
1347 * We have to be careful here, if we do not do the right
1348 * thing we can lose track of dirty inodes and the system
1349 * will lockup trying to allocate buffers.
1351 target_ip = rec->target_ip;
1352 KKASSERT(target_ip != NULL);
1353 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1354 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1356 * If the target IP is already flushing in our group
1357 * we are golden, otherwise make sure the target
1360 if (target_ip->flush_group == ip->flush_group) {
1361 rec->flush_state = HAMMER_FST_FLUSH;
1362 rec->flush_group = ip->flush_group;
1363 hammer_ref(&rec->lock);
1366 target_ip->flags |= HAMMER_INODE_REFLUSH;
1368 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1370 * If the target IP is not flushing we can force
1371 * it to flush, even if it is unable to write out
1372 * any of its own records we have at least one in
1373 * hand that we CAN deal with.
1375 rec->flush_state = HAMMER_FST_FLUSH;
1376 rec->flush_group = ip->flush_group;
1377 hammer_ref(&rec->lock);
1378 hammer_flush_inode_core(target_ip,
1379 HAMMER_FLUSH_RECURSION);
1383 * General or delete-on-disk record.
1385 * XXX this needs help. If a delete-on-disk we could
1386 * disconnect the target. If the target has its own
1387 * dependancies they really need to be flushed.
1391 rec->flush_state = HAMMER_FST_FLUSH;
1392 rec->flush_group = ip->flush_group;
1393 hammer_ref(&rec->lock);
1394 hammer_flush_inode_core(target_ip,
1395 HAMMER_FLUSH_RECURSION);
1399 case HAMMER_FST_FLUSH:
1401 * Record already associated with a flush group. It had
1404 KKASSERT(rec->flush_group == ip->flush_group);
1412 * Wait for a previously queued flush to complete. Not only do we need to
1413 * wait for the inode to sync out, we also may have to run the flusher again
1414 * to get it past the UNDO position pertaining to the flush so a crash does
1415 * not 'undo' our flush.
1418 hammer_wait_inode(hammer_inode_t ip)
1420 hammer_mount_t hmp = ip->hmp;
1424 sync_group = ip->flush_group;
1425 waitcount = (ip->flags & HAMMER_INODE_REFLUSH) ? 2 : 1;
1427 if (ip->flush_state == HAMMER_FST_SETUP) {
1429 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1431 /* XXX can we make this != FST_IDLE ? check SETUP depends */
1432 while (ip->flush_state == HAMMER_FST_FLUSH &&
1433 (ip->flush_group - sync_group) < waitcount) {
1434 ip->flags |= HAMMER_INODE_FLUSHW;
1435 tsleep(&ip->flags, 0, "hmrwin", 0);
1437 while (hmp->flusher.done - sync_group < waitcount) {
1439 hammer_flusher_sync(hmp);
1444 * Called by the backend code when a flush has been completed.
1445 * The inode has already been removed from the flush list.
1447 * A pipelined flush can occur, in which case we must re-enter the
1448 * inode on the list and re-copy its fields.
1451 hammer_flush_inode_done(hammer_inode_t ip)
1456 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1461 * Merge left-over flags back into the frontend and fix the state.
1463 ip->flags |= ip->sync_flags;
1466 * The backend may have adjusted nlinks, so if the adjusted nlinks
1467 * does not match the fronttend set the frontend's RDIRTY flag again.
1469 if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
1470 ip->flags |= HAMMER_INODE_DDIRTY;
1473 * Fix up the dirty buffer status. IO completions will also
1474 * try to clean up rsv_databufs.
1476 if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
1477 ip->flags |= HAMMER_INODE_BUFS;
1479 hmp->rsv_databufs -= ip->rsv_databufs;
1480 ip->rsv_databufs = 0;
1484 * Re-set the XDIRTY flag if some of the inode's in-memory records
1485 * could not be flushed.
1487 KKASSERT((RB_EMPTY(&ip->rec_tree) &&
1488 (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
1489 (!RB_EMPTY(&ip->rec_tree) &&
1490 (ip->flags & HAMMER_INODE_XDIRTY) != 0));
1493 * Do not lose track of inodes which no longer have vnode
1494 * assocations, otherwise they may never get flushed again.
1496 if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
1497 ip->flags |= HAMMER_INODE_REFLUSH;
1500 * Adjust flush_state. The target state (idle or setup) shouldn't
1501 * be terribly important since we will reflush if we really need
1502 * to do anything. XXX
1504 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1505 ip->flush_state = HAMMER_FST_IDLE;
1508 ip->flush_state = HAMMER_FST_SETUP;
1512 --hmp->count_iqueued;
1513 --hammer_count_iqueued;
1516 * Clean up the vnode ref
1518 if (ip->flags & HAMMER_INODE_VHELD) {
1519 ip->flags &= ~HAMMER_INODE_VHELD;
1524 * If the frontend made more changes and requested another flush,
1525 * then try to get it running.
1527 if (ip->flags & HAMMER_INODE_REFLUSH) {
1528 ip->flags &= ~HAMMER_INODE_REFLUSH;
1529 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1530 ip->flags &= ~HAMMER_INODE_RESIGNAL;
1531 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1533 hammer_flush_inode(ip, 0);
1538 * If the inode is now clean drop the space reservation.
1540 if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1541 (ip->flags & HAMMER_INODE_RSV_INODES)) {
1542 ip->flags &= ~HAMMER_INODE_RSV_INODES;
1547 * Finally, if the frontend is waiting for a flush to complete,
1550 if (ip->flush_state != HAMMER_FST_FLUSH) {
1551 if (ip->flags & HAMMER_INODE_FLUSHW) {
1552 ip->flags &= ~HAMMER_INODE_FLUSHW;
1557 hammer_rel_inode(ip, 0);
1561 * Called from hammer_sync_inode() to synchronize in-memory records
1565 hammer_sync_record_callback(hammer_record_t record, void *data)
1567 hammer_cursor_t cursor = data;
1568 hammer_transaction_t trans = cursor->trans;
1572 * Skip records that do not belong to the current flush.
1574 ++hammer_stats_record_iterations;
1575 if (record->flush_state != HAMMER_FST_FLUSH)
1579 if (record->flush_group != record->ip->flush_group) {
1580 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1585 KKASSERT(record->flush_group == record->ip->flush_group);
1588 * Interlock the record using the BE flag. Once BE is set the
1589 * frontend cannot change the state of FE.
1591 * NOTE: If FE is set prior to us setting BE we still sync the
1592 * record out, but the flush completion code converts it to
1593 * a delete-on-disk record instead of destroying it.
1595 KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
1596 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1599 * The backend may have already disposed of the record.
1601 if (record->flags & HAMMER_RECF_DELETED_BE) {
1607 * If the whole inode is being deleting all on-disk records will
1608 * be deleted very soon, we can't sync any new records to disk
1609 * because they will be deleted in the same transaction they were
1610 * created in (delete_tid == create_tid), which will assert.
1612 * XXX There may be a case with RECORD_ADD with DELETED_FE set
1613 * that we currently panic on.
1615 if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
1616 switch(record->type) {
1617 case HAMMER_MEM_RECORD_DATA:
1619 * We don't have to do anything, if the record was
1620 * committed the space will have been accounted for
1624 case HAMMER_MEM_RECORD_GENERAL:
1625 record->flags |= HAMMER_RECF_DELETED_FE;
1626 record->flags |= HAMMER_RECF_DELETED_BE;
1629 case HAMMER_MEM_RECORD_ADD:
1630 panic("hammer_sync_record_callback: illegal add "
1631 "during inode deletion record %p", record);
1632 break; /* NOT REACHED */
1633 case HAMMER_MEM_RECORD_INODE:
1634 panic("hammer_sync_record_callback: attempt to "
1635 "sync inode record %p?", record);
1636 break; /* NOT REACHED */
1637 case HAMMER_MEM_RECORD_DEL:
1639 * Follow through and issue the on-disk deletion
1646 * If DELETED_FE is set special handling is needed for directory
1647 * entries. Dependant pieces related to the directory entry may
1648 * have already been synced to disk. If this occurs we have to
1649 * sync the directory entry and then change the in-memory record
1650 * from an ADD to a DELETE to cover the fact that it's been
1651 * deleted by the frontend.
1653 * A directory delete covering record (MEM_RECORD_DEL) can never
1654 * be deleted by the frontend.
1656 * Any other record type (aka DATA) can be deleted by the frontend.
1657 * XXX At the moment the flusher must skip it because there may
1658 * be another data record in the flush group for the same block,
1659 * meaning that some frontend data changes can leak into the backend's
1660 * synchronization point.
1662 if (record->flags & HAMMER_RECF_DELETED_FE) {
1663 if (record->type == HAMMER_MEM_RECORD_ADD) {
1664 record->flags |= HAMMER_RECF_CONVERT_DELETE;
1666 KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
1667 record->flags |= HAMMER_RECF_DELETED_BE;
1674 * Assign the create_tid for new records. Deletions already
1675 * have the record's entire key properly set up.
1677 if (record->type != HAMMER_MEM_RECORD_DEL)
1678 record->leaf.base.create_tid = trans->tid;
1680 error = hammer_ip_sync_record_cursor(cursor, record);
1681 if (error != EDEADLK)
1683 hammer_done_cursor(cursor);
1684 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
1689 record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
1693 if (error != -ENOSPC) {
1694 kprintf("hammer_sync_record_callback: sync failed rec "
1695 "%p, error %d\n", record, error);
1696 Debugger("sync failed rec");
1700 hammer_flush_record_done(record, error);
1705 * XXX error handling
1708 hammer_sync_inode(hammer_inode_t ip)
1710 struct hammer_transaction trans;
1711 struct hammer_cursor cursor;
1712 hammer_node_t tmp_node;
1713 hammer_record_t depend;
1714 hammer_record_t next;
1715 int error, tmp_error;
1718 if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
1721 hammer_start_transaction_fls(&trans, ip->hmp);
1722 error = hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1727 * Any directory records referencing this inode which are not in
1728 * our current flush group must adjust our nlink count for the
1729 * purposes of synchronization to disk.
1731 * Records which are in our flush group can be unlinked from our
1732 * inode now, potentially allowing the inode to be physically
1735 * This cannot block.
1737 nlinks = ip->ino_data.nlinks;
1738 next = TAILQ_FIRST(&ip->target_list);
1739 while ((depend = next) != NULL) {
1740 next = TAILQ_NEXT(depend, target_entry);
1741 if (depend->flush_state == HAMMER_FST_FLUSH &&
1742 depend->flush_group == ip->hmp->flusher.act) {
1744 * If this is an ADD that was deleted by the frontend
1745 * the frontend nlinks count will have already been
1746 * decremented, but the backend is going to sync its
1747 * directory entry and must account for it. The
1748 * record will be converted to a delete-on-disk when
1751 * If the ADD was not deleted by the frontend we
1752 * can remove the dependancy from our target_list.
1754 if (depend->flags & HAMMER_RECF_DELETED_FE) {
1757 TAILQ_REMOVE(&ip->target_list, depend,
1759 depend->target_ip = NULL;
1761 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
1763 * Not part of our flush group
1765 KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
1766 switch(depend->type) {
1767 case HAMMER_MEM_RECORD_ADD:
1770 case HAMMER_MEM_RECORD_DEL:
1780 * Set dirty if we had to modify the link count.
1782 if (ip->sync_ino_data.nlinks != nlinks) {
1783 KKASSERT((int64_t)nlinks >= 0);
1784 ip->sync_ino_data.nlinks = nlinks;
1785 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1789 * If there is a trunction queued destroy any data past the (aligned)
1790 * truncation point. Userland will have dealt with the buffer
1791 * containing the truncation point for us.
1793 * We don't flush pending frontend data buffers until after we've
1794 * dealt with the truncation.
1796 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
1798 * Interlock trunc_off. The VOP front-end may continue to
1799 * make adjustments to it while we are blocked.
1802 off_t aligned_trunc_off;
1805 trunc_off = ip->sync_trunc_off;
1806 blkmask = hammer_blocksize(trunc_off) - 1;
1807 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
1810 * Delete any whole blocks on-media. The front-end has
1811 * already cleaned out any partial block and made it
1812 * pending. The front-end may have updated trunc_off
1813 * while we were blocked so we only use sync_trunc_off.
1815 error = hammer_ip_delete_range(&cursor, ip,
1817 0x7FFFFFFFFFFFFFFFLL, 1);
1819 Debugger("hammer_ip_delete_range errored");
1822 * Clear the truncation flag on the backend after we have
1823 * complete the deletions. Backend data is now good again
1824 * (including new records we are about to sync, below).
1826 * Leave sync_trunc_off intact. As we write additional
1827 * records the backend will update sync_trunc_off. This
1828 * tells the backend whether it can skip the overwrite
1829 * test. This should work properly even when the backend
1830 * writes full blocks where the truncation point straddles
1831 * the block because the comparison is against the base
1832 * offset of the record.
1834 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1835 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
1841 * Now sync related records. These will typically be directory
1842 * entries or delete-on-disk records.
1844 * Not all records will be flushed, but clear XDIRTY anyway. We
1845 * will set it again in the frontend hammer_flush_inode_done()
1846 * if records remain.
1849 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1850 hammer_sync_record_callback, &cursor);
1856 hammer_cache_node(&ip->cache[1], cursor.node);
1859 * Re-seek for inode update, assuming our cache hasn't been ripped
1860 * out from under us.
1863 tmp_node = hammer_ref_node_safe(ip->hmp, &ip->cache[0], &error);
1865 if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
1866 hammer_cursor_seek(&cursor, tmp_node, 0);
1867 hammer_rel_node(tmp_node);
1873 * If we are deleting the inode the frontend had better not have
1874 * any active references on elements making up the inode.
1876 if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
1877 RB_EMPTY(&ip->rec_tree) &&
1878 (ip->sync_flags & HAMMER_INODE_DELETING) &&
1879 (ip->flags & HAMMER_INODE_DELETED) == 0) {
1882 ip->flags |= HAMMER_INODE_DELETED;
1883 error = hammer_ip_delete_range_all(&cursor, ip, &count1);
1885 ip->sync_flags &= ~HAMMER_INODE_DELETING;
1886 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
1887 KKASSERT(RB_EMPTY(&ip->rec_tree));
1890 * Set delete_tid in both the frontend and backend
1891 * copy of the inode record. The DELETED flag handles
1892 * this, do not set RDIRTY.
1894 ip->ino_leaf.base.delete_tid = trans.tid;
1895 ip->sync_ino_leaf.base.delete_tid = trans.tid;
1898 * Adjust the inode count in the volume header
1900 if (ip->flags & HAMMER_INODE_ONDISK) {
1901 hammer_modify_volume_field(&trans,
1904 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1905 hammer_modify_volume_done(trans.rootvol);
1908 ip->flags &= ~HAMMER_INODE_DELETED;
1909 Debugger("hammer_ip_delete_range_all errored");
1913 ip->sync_flags &= ~HAMMER_INODE_BUFS;
1916 Debugger("RB_SCAN errored");
1919 * Now update the inode's on-disk inode-data and/or on-disk record.
1920 * DELETED and ONDISK are managed only in ip->flags.
1922 switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
1923 case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
1925 * If deleted and on-disk, don't set any additional flags.
1926 * the delete flag takes care of things.
1928 * Clear flags which may have been set by the frontend.
1930 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1931 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
1932 HAMMER_INODE_DELETING);
1934 case HAMMER_INODE_DELETED:
1936 * Take care of the case where a deleted inode was never
1937 * flushed to the disk in the first place.
1939 * Clear flags which may have been set by the frontend.
1941 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
1942 HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
1943 HAMMER_INODE_DELETING);
1944 while (RB_ROOT(&ip->rec_tree)) {
1945 hammer_record_t record = RB_ROOT(&ip->rec_tree);
1946 hammer_ref(&record->lock);
1947 KKASSERT(record->lock.refs == 1);
1948 record->flags |= HAMMER_RECF_DELETED_FE;
1949 record->flags |= HAMMER_RECF_DELETED_BE;
1950 hammer_rel_mem_record(record);
1953 case HAMMER_INODE_ONDISK:
1955 * If already on-disk, do not set any additional flags.
1960 * If not on-disk and not deleted, set DDIRTY to force
1961 * an initial record to be written.
1963 * Also set the create_tid in both the frontend and backend
1964 * copy of the inode record.
1966 ip->ino_leaf.base.create_tid = trans.tid;
1967 ip->sync_ino_leaf.base.create_tid = trans.tid;
1968 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1973 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
1974 * is already on-disk the old record is marked as deleted.
1976 * If DELETED is set hammer_update_inode() will delete the existing
1977 * record without writing out a new one.
1979 * If *ONLY* the ITIMES flag is set we can update the record in-place.
1981 if (ip->flags & HAMMER_INODE_DELETED) {
1982 error = hammer_update_inode(&cursor, ip);
1984 if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
1985 (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
1986 error = hammer_update_itimes(&cursor, ip);
1988 if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
1989 error = hammer_update_inode(&cursor, ip);
1992 Debugger("hammer_update_itimes/inode errored");
1995 * Save the TID we used to sync the inode with to make sure we
1996 * do not improperly reuse it.
1998 hammer_done_cursor(&cursor);
1999 hammer_done_transaction(&trans);
2004 * This routine is called when the OS is no longer actively referencing
2005 * the inode (but might still be keeping it cached), or when releasing
2006 * the last reference to an inode.
2008 * At this point if the inode's nlinks count is zero we want to destroy
2009 * it, which may mean destroying it on-media too.
2012 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2017 * Set the DELETING flag when the link count drops to 0 and the
2018 * OS no longer has any opens on the inode.
2020 * The backend will clear DELETING (a mod flag) and set DELETED
2021 * (a state flag) when it is actually able to perform the
2024 if (ip->ino_data.nlinks == 0 &&
2025 (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2026 ip->flags |= HAMMER_INODE_DELETING;
2027 ip->flags |= HAMMER_INODE_TRUNCATED;
2031 if (hammer_get_vnode(ip, &vp) != 0)
2039 vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2040 vnode_pager_setsize(ip->vp, 0);
2049 * Re-test an inode when a dependancy had gone away to see if we
2050 * can chain flush it.
2053 hammer_test_inode(hammer_inode_t ip)
2055 if (ip->flags & HAMMER_INODE_REFLUSH) {
2056 ip->flags &= ~HAMMER_INODE_REFLUSH;
2057 hammer_ref(&ip->lock);
2058 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2059 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2060 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2062 hammer_flush_inode(ip, 0);
2064 hammer_rel_inode(ip, 0);
2069 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2070 * reassociated with a vp or just before it gets freed.
2072 * Wakeup one thread blocked waiting on reclaims to complete. Note that
2073 * the inode the thread is waiting on behalf of is a different inode then
2074 * the inode we are called with. This is to create a pipeline.
2077 hammer_inode_wakereclaims(hammer_inode_t ip)
2079 struct hammer_reclaim *reclaim;
2080 hammer_mount_t hmp = ip->hmp;
2082 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
2085 --hammer_count_reclaiming;
2086 --hmp->inode_reclaims;
2087 ip->flags &= ~HAMMER_INODE_RECLAIM;
2089 if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
2090 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
2091 reclaim->okydoky = 1;
2097 * Setup our reclaim pipeline. We only let so many detached (and dirty)
2098 * inodes build up before we start blocking.
2100 * When we block we don't care *which* inode has finished reclaiming,
2101 * as lone as one does. This is somewhat heuristical... we also put a
2102 * cap on how long we are willing to wait.
2105 hammer_inode_waitreclaims(hammer_mount_t hmp)
2107 struct hammer_reclaim reclaim;
2110 if (hmp->inode_reclaims > HAMMER_RECLAIM_WAIT) {
2111 reclaim.okydoky = 0;
2112 TAILQ_INSERT_TAIL(&hmp->reclaim_list,
2115 reclaim.okydoky = 1;
2118 if (reclaim.okydoky == 0) {
2119 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
2120 HAMMER_RECLAIM_WAIT;
2122 tsleep(&reclaim, 0, "hmrrcm", delay + 1);
2123 if (reclaim.okydoky == 0)
2124 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);