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.95 2008/07/07 03:49:50 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
)
230 if ((vp
= ip
->vp
) == NULL
) {
231 error
= getnewvnode(VT_HAMMER
, hmp
->mp
, vpp
, 0, 0);
234 hammer_lock_ex(&ip
->lock
);
235 if (ip
->vp
!= NULL
) {
236 hammer_unlock(&ip
->lock
);
241 hammer_ref(&ip
->lock
);
245 hammer_get_vnode_type(ip
->ino_data
.obj_type
);
247 hammer_inode_wakereclaims(ip
);
249 switch(ip
->ino_data
.obj_type
) {
250 case HAMMER_OBJTYPE_CDEV
:
251 case HAMMER_OBJTYPE_BDEV
:
252 vp
->v_ops
= &hmp
->mp
->mnt_vn_spec_ops
;
253 addaliasu(vp
, ip
->ino_data
.rmajor
,
254 ip
->ino_data
.rminor
);
256 case HAMMER_OBJTYPE_FIFO
:
257 vp
->v_ops
= &hmp
->mp
->mnt_vn_fifo_ops
;
264 * Only mark as the root vnode if the ip is not
265 * historical, otherwise the VFS cache will get
266 * confused. The other half of the special handling
267 * is in hammer_vop_nlookupdotdot().
269 * Pseudo-filesystem roots also do not count.
271 if (ip
->obj_id
== HAMMER_OBJID_ROOT
&&
272 ip
->obj_asof
== hmp
->asof
&&
273 ip
->obj_localization
== 0) {
277 vp
->v_data
= (void *)ip
;
278 /* vnode locked by getnewvnode() */
279 /* make related vnode dirty if inode dirty? */
280 hammer_unlock(&ip
->lock
);
281 if (vp
->v_type
== VREG
)
282 vinitvmio(vp
, ip
->ino_data
.size
);
287 * loop if the vget fails (aka races), or if the vp
288 * no longer matches ip->vp.
290 if (vget(vp
, LK_EXCLUSIVE
) == 0) {
301 * Locate all copies of the inode for obj_id compatible with the specified
302 * asof, reference, and issue the related call-back. This routine is used
303 * for direct-io invalidation and does not create any new inodes.
306 hammer_scan_inode_snapshots(hammer_mount_t hmp
, hammer_inode_info_t iinfo
,
307 int (*callback
)(hammer_inode_t ip
, void *data
),
310 hammer_ino_rb_tree_RB_SCAN(&hmp
->rb_inos_root
,
311 hammer_inode_info_cmp_all_history
,
316 * Acquire a HAMMER inode. The returned inode is not locked. These functions
317 * do not attach or detach the related vnode (use hammer_get_vnode() for
320 * The flags argument is only applied for newly created inodes, and only
321 * certain flags are inherited.
323 * Called from the frontend.
325 struct hammer_inode
*
326 hammer_get_inode(hammer_transaction_t trans
, hammer_inode_t dip
,
327 int64_t obj_id
, hammer_tid_t asof
, u_int32_t localization
,
328 int flags
, int *errorp
)
330 hammer_mount_t hmp
= trans
->hmp
;
331 struct hammer_inode_info iinfo
;
332 struct hammer_cursor cursor
;
333 struct hammer_inode
*ip
;
337 * Determine if we already have an inode cached. If we do then
340 iinfo
.obj_id
= obj_id
;
341 iinfo
.obj_asof
= asof
;
342 iinfo
.obj_localization
= localization
;
344 ip
= hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp
->rb_inos_root
, &iinfo
);
346 hammer_ref(&ip
->lock
);
352 * Allocate a new inode structure and deal with races later.
354 ip
= kmalloc(sizeof(*ip
), M_HAMMER
, M_WAITOK
|M_ZERO
);
355 ++hammer_count_inodes
;
358 ip
->obj_asof
= iinfo
.obj_asof
;
359 ip
->obj_localization
= localization
;
361 ip
->flags
= flags
& HAMMER_INODE_RO
;
362 ip
->cache
[0].ip
= ip
;
363 ip
->cache
[1].ip
= ip
;
365 ip
->flags
|= HAMMER_INODE_RO
;
366 ip
->sync_trunc_off
= ip
->trunc_off
= ip
->save_trunc_off
=
367 0x7FFFFFFFFFFFFFFFLL
;
368 RB_INIT(&ip
->rec_tree
);
369 TAILQ_INIT(&ip
->target_list
);
370 hammer_ref(&ip
->lock
);
373 * Locate the on-disk inode.
376 hammer_init_cursor(trans
, &cursor
, (dip
? &dip
->cache
[0] : NULL
), NULL
);
377 cursor
.key_beg
.localization
= localization
+ HAMMER_LOCALIZE_INODE
;
378 cursor
.key_beg
.obj_id
= ip
->obj_id
;
379 cursor
.key_beg
.key
= 0;
380 cursor
.key_beg
.create_tid
= 0;
381 cursor
.key_beg
.delete_tid
= 0;
382 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
;
383 cursor
.key_beg
.obj_type
= 0;
384 cursor
.asof
= iinfo
.obj_asof
;
385 cursor
.flags
= HAMMER_CURSOR_GET_LEAF
| HAMMER_CURSOR_GET_DATA
|
388 *errorp
= hammer_btree_lookup(&cursor
);
389 if (*errorp
== EDEADLK
) {
390 hammer_done_cursor(&cursor
);
395 * On success the B-Tree lookup will hold the appropriate
396 * buffer cache buffers and provide a pointer to the requested
397 * information. Copy the information to the in-memory inode
398 * and cache the B-Tree node to improve future operations.
401 ip
->ino_leaf
= cursor
.node
->ondisk
->elms
[cursor
.index
].leaf
;
402 ip
->ino_data
= cursor
.data
->inode
;
405 * cache[0] tries to cache the location of the object inode.
406 * The assumption is that it is near the directory inode.
408 * cache[1] tries to cache the location of the object data.
409 * The assumption is that it is near the directory data.
411 hammer_cache_node(&ip
->cache
[0], cursor
.node
);
412 if (dip
&& dip
->cache
[1].node
)
413 hammer_cache_node(&ip
->cache
[1], dip
->cache
[1].node
);
416 * The file should not contain any data past the file size
417 * stored in the inode. Setting save_trunc_off to the
418 * file size instead of max reduces B-Tree lookup overheads
419 * on append by allowing the flusher to avoid checking for
422 ip
->save_trunc_off
= ip
->ino_data
.size
;
425 * Locate and assign the pseudofs management structure to
428 if (dip
&& dip
->obj_localization
== ip
->obj_localization
) {
429 ip
->pfsm
= dip
->pfsm
;
430 hammer_ref(&ip
->pfsm
->lock
);
432 *errorp
= hammer_load_pseudofs(trans
, ip
);
437 * The inode is placed on the red-black tree and will be synced to
438 * the media when flushed or by the filesystem sync. If this races
439 * another instantiation/lookup the insertion will fail.
442 if (RB_INSERT(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, ip
)) {
443 hammer_free_inode(ip
);
444 hammer_done_cursor(&cursor
);
447 ip
->flags
|= HAMMER_INODE_ONDISK
;
449 if (ip
->flags
& HAMMER_INODE_RSV_INODES
) {
450 ip
->flags
&= ~HAMMER_INODE_RSV_INODES
; /* sanity */
454 hammer_free_inode(ip
);
457 hammer_done_cursor(&cursor
);
462 * Create a new filesystem object, returning the inode in *ipp. The
463 * returned inode will be referenced.
465 * The inode is created in-memory.
468 hammer_create_inode(hammer_transaction_t trans
, struct vattr
*vap
,
469 struct ucred
*cred
, hammer_inode_t dip
,
470 int pseudofs
, struct hammer_inode
**ipp
)
475 u_int32_t localization
;
481 * Assign the localization domain. If if dip is NULL we are creating
482 * a pseudo-fs and must locate an unused localization domain.
485 for (localization
= HAMMER_DEF_LOCALIZATION
;
486 localization
< HAMMER_LOCALIZE_PSEUDOFS_MASK
;
487 localization
+= HAMMER_LOCALIZE_PSEUDOFS_INC
) {
488 ip
= hammer_get_inode(trans
, NULL
, HAMMER_OBJID_ROOT
,
489 hmp
->asof
, localization
,
497 hammer_rel_inode(ip
, 0);
500 localization
= dip
->obj_localization
;
503 ip
= kmalloc(sizeof(*ip
), M_HAMMER
, M_WAITOK
|M_ZERO
);
504 ++hammer_count_inodes
;
508 * Allocate a new object id. If creating a new pseudo-fs the
512 ip
->obj_id
= HAMMER_OBJID_ROOT
;
514 ip
->obj_id
= hammer_alloc_objid(hmp
, dip
);
515 ip
->obj_localization
= localization
;
517 KKASSERT(ip
->obj_id
!= 0);
518 ip
->obj_asof
= hmp
->asof
;
520 ip
->flush_state
= HAMMER_FST_IDLE
;
521 ip
->flags
= HAMMER_INODE_DDIRTY
|
522 HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
;
523 ip
->cache
[0].ip
= ip
;
524 ip
->cache
[1].ip
= ip
;
526 ip
->trunc_off
= 0x7FFFFFFFFFFFFFFFLL
;
527 /* ip->save_trunc_off = 0; (already zero) */
528 RB_INIT(&ip
->rec_tree
);
529 TAILQ_INIT(&ip
->target_list
);
531 ip
->ino_data
.atime
= trans
->time
;
532 ip
->ino_data
.mtime
= trans
->time
;
533 ip
->ino_data
.size
= 0;
534 ip
->ino_data
.nlinks
= 0;
537 * A nohistory designator on the parent directory is inherited by
538 * the child. We will do this even for pseudo-fs creation... the
539 * sysad can turn it off.
541 ip
->ino_data
.uflags
= dip
->ino_data
.uflags
&
542 (SF_NOHISTORY
|UF_NOHISTORY
|UF_NODUMP
);
544 ip
->ino_leaf
.base
.btype
= HAMMER_BTREE_TYPE_RECORD
;
545 ip
->ino_leaf
.base
.localization
= ip
->obj_localization
+
546 HAMMER_LOCALIZE_INODE
;
547 ip
->ino_leaf
.base
.obj_id
= ip
->obj_id
;
548 ip
->ino_leaf
.base
.key
= 0;
549 ip
->ino_leaf
.base
.create_tid
= 0;
550 ip
->ino_leaf
.base
.delete_tid
= 0;
551 ip
->ino_leaf
.base
.rec_type
= HAMMER_RECTYPE_INODE
;
552 ip
->ino_leaf
.base
.obj_type
= hammer_get_obj_type(vap
->va_type
);
554 ip
->ino_data
.obj_type
= ip
->ino_leaf
.base
.obj_type
;
555 ip
->ino_data
.version
= HAMMER_INODE_DATA_VERSION
;
556 ip
->ino_data
.mode
= vap
->va_mode
;
557 ip
->ino_data
.ctime
= trans
->time
;
560 * Setup the ".." pointer. This only needs to be done for directories
561 * but we do it for all objects as a recovery aid.
563 * The parent_obj_localization field only applies to pseudo-fs roots.
565 ip
->ino_data
.parent_obj_id
= dip
->ino_leaf
.base
.obj_id
;
566 if (ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_DIRECTORY
&&
567 ip
->obj_id
== HAMMER_OBJID_ROOT
) {
568 ip
->ino_data
.ext
.obj
.parent_obj_localization
=
569 dip
->obj_localization
;
572 switch(ip
->ino_leaf
.base
.obj_type
) {
573 case HAMMER_OBJTYPE_CDEV
:
574 case HAMMER_OBJTYPE_BDEV
:
575 ip
->ino_data
.rmajor
= vap
->va_rmajor
;
576 ip
->ino_data
.rminor
= vap
->va_rminor
;
583 * Calculate default uid/gid and overwrite with information from
586 xuid
= hammer_to_unix_xid(&dip
->ino_data
.uid
);
587 xuid
= vop_helper_create_uid(hmp
->mp
, dip
->ino_data
.mode
, xuid
, cred
,
589 ip
->ino_data
.mode
= vap
->va_mode
;
591 if (vap
->va_vaflags
& VA_UID_UUID_VALID
)
592 ip
->ino_data
.uid
= vap
->va_uid_uuid
;
593 else if (vap
->va_uid
!= (uid_t
)VNOVAL
)
594 hammer_guid_to_uuid(&ip
->ino_data
.uid
, vap
->va_uid
);
596 hammer_guid_to_uuid(&ip
->ino_data
.uid
, xuid
);
598 if (vap
->va_vaflags
& VA_GID_UUID_VALID
)
599 ip
->ino_data
.gid
= vap
->va_gid_uuid
;
600 else if (vap
->va_gid
!= (gid_t
)VNOVAL
)
601 hammer_guid_to_uuid(&ip
->ino_data
.gid
, vap
->va_gid
);
603 ip
->ino_data
.gid
= dip
->ino_data
.gid
;
605 hammer_ref(&ip
->lock
);
607 if (dip
->obj_localization
== ip
->obj_localization
) {
608 ip
->pfsm
= dip
->pfsm
;
609 hammer_ref(&ip
->pfsm
->lock
);
612 error
= hammer_load_pseudofs(trans
, ip
);
616 hammer_free_inode(ip
);
618 } else if (RB_INSERT(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, ip
)) {
619 panic("hammer_create_inode: duplicate obj_id %llx", ip
->obj_id
);
621 hammer_free_inode(ip
);
628 * Final cleanup / freeing of an inode structure
631 hammer_free_inode(hammer_inode_t ip
)
633 KKASSERT(ip
->lock
.refs
== 1);
634 hammer_uncache_node(&ip
->cache
[0]);
635 hammer_uncache_node(&ip
->cache
[1]);
636 hammer_inode_wakereclaims(ip
);
638 hammer_clear_objid(ip
);
639 --hammer_count_inodes
;
640 --ip
->hmp
->count_inodes
;
642 hammer_rel_pseudofs(ip
->hmp
, ip
->pfsm
);
650 * Retrieve pseudo-fs data.
653 hammer_load_pseudofs(hammer_transaction_t trans
, hammer_inode_t ip
)
655 hammer_mount_t hmp
= trans
->hmp
;
656 hammer_pseudofs_inmem_t pfsm
;
657 struct hammer_cursor cursor
;
662 pfsm
= RB_LOOKUP(hammer_pfs_rb_tree
, &hmp
->rb_pfsm_root
,
663 ip
->obj_localization
);
665 KKASSERT(ip
->pfsm
== NULL
);
667 hammer_ref(&pfsm
->lock
);
671 pfsm
= kmalloc(sizeof(*pfsm
), M_HAMMER
, M_WAITOK
| M_ZERO
);
672 pfsm
->localization
= ip
->obj_localization
;
673 pfsm
->pfsd
.unique_uuid
= trans
->rootvol
->ondisk
->vol_fsid
;
674 pfsm
->pfsd
.shared_uuid
= pfsm
->pfsd
.unique_uuid
;
676 hammer_init_cursor(trans
, &cursor
, NULL
, NULL
);
677 cursor
.key_beg
.localization
= ip
->obj_localization
+
678 HAMMER_LOCALIZE_MISC
;
679 cursor
.key_beg
.obj_id
= HAMMER_OBJID_ROOT
;
680 cursor
.key_beg
.create_tid
= 0;
681 cursor
.key_beg
.delete_tid
= 0;
682 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_FIX
;
683 cursor
.key_beg
.obj_type
= 0;
684 cursor
.key_beg
.key
= HAMMER_FIXKEY_PSEUDOFS
;
685 cursor
.asof
= HAMMER_MAX_TID
;
686 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
688 error
= hammer_btree_lookup(&cursor
);
690 error
= hammer_btree_extract(&cursor
, HAMMER_CURSOR_GET_DATA
);
692 bytes
= cursor
.leaf
->data_len
;
693 if (bytes
> sizeof(pfsm
->pfsd
))
694 bytes
= sizeof(pfsm
->pfsd
);
695 bcopy(cursor
.data
, &pfsm
->pfsd
, bytes
);
697 } else if (error
== ENOENT
) {
701 hammer_done_cursor(&cursor
);
704 pfsm
->fsid_udev
= hammer_fsid_to_udev(&pfsm
->pfsd
.shared_uuid
);
705 hammer_ref(&pfsm
->lock
);
706 if (RB_INSERT(hammer_pfs_rb_tree
, &hmp
->rb_pfsm_root
, pfsm
)) {
707 kfree(pfsm
, M_HAMMER
);
713 * Certain aspects of the pseudofs configuration are reflected
716 if (pfsm
->pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
717 ip
->flags
|= HAMMER_INODE_RO
;
718 ip
->flags
|= HAMMER_INODE_PFSD
;
719 } else if (pfsm
->pfsd
.master_id
>= 0) {
720 ip
->flags
|= HAMMER_INODE_PFSD
;
723 kprintf("cannot load pfsm error %d\n", error
);
724 kfree(pfsm
, M_HAMMER
);
730 * Store pseudo-fs data. The backend will automatically delete any prior
731 * on-disk pseudo-fs data but we have to delete in-memory versions.
734 hammer_save_pseudofs(hammer_transaction_t trans
, hammer_inode_t ip
)
736 struct hammer_cursor cursor
;
737 hammer_pseudofs_inmem_t pfsm
;
738 hammer_record_t record
;
743 pfsm
->fsid_udev
= hammer_fsid_to_udev(&pfsm
->pfsd
.shared_uuid
);
744 hammer_init_cursor(trans
, &cursor
, &ip
->cache
[1], ip
);
745 cursor
.key_beg
.localization
= ip
->obj_localization
+
746 HAMMER_LOCALIZE_MISC
;
747 cursor
.key_beg
.obj_id
= ip
->obj_id
;
748 cursor
.key_beg
.create_tid
= 0;
749 cursor
.key_beg
.delete_tid
= 0;
750 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_FIX
;
751 cursor
.key_beg
.obj_type
= 0;
752 cursor
.key_beg
.key
= HAMMER_FIXKEY_PSEUDOFS
;
753 cursor
.asof
= HAMMER_MAX_TID
;
754 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
756 error
= hammer_ip_lookup(&cursor
);
757 if (error
== 0 && hammer_cursor_inmem(&cursor
)) {
758 record
= cursor
.iprec
;
759 if (record
->flags
& HAMMER_RECF_INTERLOCK_BE
) {
760 KKASSERT(cursor
.deadlk_rec
== NULL
);
761 hammer_ref(&record
->lock
);
762 cursor
.deadlk_rec
= record
;
765 record
->flags
|= HAMMER_RECF_DELETED_FE
;
769 if (error
== 0 || error
== ENOENT
) {
770 record
= hammer_alloc_mem_record(ip
, sizeof(pfsm
->pfsd
));
771 record
->type
= HAMMER_MEM_RECORD_GENERAL
;
773 record
->leaf
.base
.localization
= ip
->obj_localization
+
774 HAMMER_LOCALIZE_MISC
;
775 record
->leaf
.base
.rec_type
= HAMMER_RECTYPE_FIX
;
776 record
->leaf
.base
.key
= HAMMER_FIXKEY_PSEUDOFS
;
777 record
->leaf
.data_len
= sizeof(pfsm
->pfsd
);
778 bcopy(&pfsm
->pfsd
, record
->data
, sizeof(pfsm
->pfsd
));
779 error
= hammer_ip_add_record(trans
, record
);
781 hammer_done_cursor(&cursor
);
782 if (error
== EDEADLK
)
786 * Certain aspects of the pseudofs configuration are reflected
787 * in the inode. Note that we cannot mess with the as-of or
788 * clear the read-only state.
790 * If this inode represented a slave snapshot its asof will
791 * be set to a snapshot tid. When clearing slave mode any
792 * re-access of the inode via the parent directory will
793 * wind up using a different asof and thus will instantiate
796 if (pfsm
->pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) {
797 ip
->flags
|= HAMMER_INODE_RO
;
798 ip
->flags
|= HAMMER_INODE_PFSD
;
799 } else if (pfsm
->pfsd
.master_id
>= 0) {
800 ip
->flags
|= HAMMER_INODE_PFSD
;
802 ip
->flags
&= ~HAMMER_INODE_PFSD
;
809 hammer_rel_pseudofs(hammer_mount_t hmp
, hammer_pseudofs_inmem_t pfsm
)
811 hammer_unref(&pfsm
->lock
);
812 if (pfsm
->lock
.refs
== 0) {
813 RB_REMOVE(hammer_pfs_rb_tree
, &hmp
->rb_pfsm_root
, pfsm
);
814 kfree(pfsm
, M_HAMMER
);
819 * Called by hammer_sync_inode().
822 hammer_update_inode(hammer_cursor_t cursor
, hammer_inode_t ip
)
824 hammer_transaction_t trans
= cursor
->trans
;
825 hammer_record_t record
;
833 * If the inode has a presence on-disk then locate it and mark
834 * it deleted, setting DELONDISK.
836 * The record may or may not be physically deleted, depending on
837 * the retention policy.
839 if ((ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DELONDISK
)) ==
840 HAMMER_INODE_ONDISK
) {
841 hammer_normalize_cursor(cursor
);
842 cursor
->key_beg
.localization
= ip
->obj_localization
+
843 HAMMER_LOCALIZE_INODE
;
844 cursor
->key_beg
.obj_id
= ip
->obj_id
;
845 cursor
->key_beg
.key
= 0;
846 cursor
->key_beg
.create_tid
= 0;
847 cursor
->key_beg
.delete_tid
= 0;
848 cursor
->key_beg
.rec_type
= HAMMER_RECTYPE_INODE
;
849 cursor
->key_beg
.obj_type
= 0;
850 cursor
->asof
= ip
->obj_asof
;
851 cursor
->flags
&= ~HAMMER_CURSOR_INITMASK
;
852 cursor
->flags
|= HAMMER_CURSOR_GET_LEAF
| HAMMER_CURSOR_ASOF
;
853 cursor
->flags
|= HAMMER_CURSOR_BACKEND
;
855 error
= hammer_btree_lookup(cursor
);
856 if (hammer_debug_inode
)
857 kprintf("IPDEL %p %08x %d", ip
, ip
->flags
, error
);
859 kprintf("error %d\n", error
);
860 Debugger("hammer_update_inode");
864 error
= hammer_ip_delete_record(cursor
, ip
, trans
->tid
);
865 if (hammer_debug_inode
)
866 kprintf(" error %d\n", error
);
867 if (error
&& error
!= EDEADLK
) {
868 kprintf("error %d\n", error
);
869 Debugger("hammer_update_inode2");
872 ip
->flags
|= HAMMER_INODE_DELONDISK
;
875 hammer_cache_node(&ip
->cache
[0], cursor
->node
);
877 if (error
== EDEADLK
) {
878 hammer_done_cursor(cursor
);
879 error
= hammer_init_cursor(trans
, cursor
,
881 if (hammer_debug_inode
)
882 kprintf("IPDED %p %d\n", ip
, error
);
889 * Ok, write out the initial record or a new record (after deleting
890 * the old one), unless the DELETED flag is set. This routine will
891 * clear DELONDISK if it writes out a record.
893 * Update our inode statistics if this is the first application of
896 if (error
== 0 && (ip
->flags
& HAMMER_INODE_DELETED
) == 0) {
898 * Generate a record and write it to the media
900 record
= hammer_alloc_mem_record(ip
, 0);
901 record
->type
= HAMMER_MEM_RECORD_INODE
;
902 record
->flush_state
= HAMMER_FST_FLUSH
;
903 record
->leaf
= ip
->sync_ino_leaf
;
904 record
->leaf
.base
.create_tid
= trans
->tid
;
905 record
->leaf
.data_len
= sizeof(ip
->sync_ino_data
);
906 record
->leaf
.create_ts
= trans
->time32
;
907 record
->data
= (void *)&ip
->sync_ino_data
;
908 record
->flags
|= HAMMER_RECF_INTERLOCK_BE
;
911 * If this flag is set we cannot sync the new file size
912 * because we haven't finished related truncations. The
913 * inode will be flushed in another flush group to finish
916 if ((ip
->flags
& HAMMER_INODE_WOULDBLOCK
) &&
917 ip
->sync_ino_data
.size
!= ip
->ino_data
.size
) {
919 ip
->sync_ino_data
.size
= ip
->ino_data
.size
;
925 error
= hammer_ip_sync_record_cursor(cursor
, record
);
926 if (hammer_debug_inode
)
927 kprintf("GENREC %p rec %08x %d\n",
928 ip
, record
->flags
, error
);
929 if (error
!= EDEADLK
)
931 hammer_done_cursor(cursor
);
932 error
= hammer_init_cursor(trans
, cursor
,
934 if (hammer_debug_inode
)
935 kprintf("GENREC reinit %d\n", error
);
940 kprintf("error %d\n", error
);
941 Debugger("hammer_update_inode3");
945 * The record isn't managed by the inode's record tree,
946 * destroy it whether we succeed or fail.
948 record
->flags
&= ~HAMMER_RECF_INTERLOCK_BE
;
949 record
->flags
|= HAMMER_RECF_DELETED_FE
;
950 record
->flush_state
= HAMMER_FST_IDLE
;
951 hammer_rel_mem_record(record
);
957 if (hammer_debug_inode
)
958 kprintf("CLEANDELOND %p %08x\n", ip
, ip
->flags
);
959 ip
->sync_flags
&= ~(HAMMER_INODE_DDIRTY
|
962 ip
->flags
&= ~HAMMER_INODE_DELONDISK
;
964 ip
->sync_flags
|= HAMMER_INODE_DDIRTY
;
967 * Root volume count of inodes
969 if ((ip
->flags
& HAMMER_INODE_ONDISK
) == 0) {
970 hammer_modify_volume_field(trans
,
973 ++ip
->hmp
->rootvol
->ondisk
->vol0_stat_inodes
;
974 hammer_modify_volume_done(trans
->rootvol
);
975 ip
->flags
|= HAMMER_INODE_ONDISK
;
976 if (hammer_debug_inode
)
977 kprintf("NOWONDISK %p\n", ip
);
983 * If the inode has been destroyed, clean out any left-over flags
984 * that may have been set by the frontend.
986 if (error
== 0 && (ip
->flags
& HAMMER_INODE_DELETED
)) {
987 ip
->sync_flags
&= ~(HAMMER_INODE_DDIRTY
|
995 * Update only the itimes fields.
997 * ATIME can be updated without generating any UNDO. MTIME is updated
998 * with UNDO so it is guaranteed to be synchronized properly in case of
1001 * Neither field is included in the B-Tree leaf element's CRC, which is how
1002 * we can get away with updating ATIME the way we do.
1005 hammer_update_itimes(hammer_cursor_t cursor
, hammer_inode_t ip
)
1007 hammer_transaction_t trans
= cursor
->trans
;
1011 if ((ip
->flags
& (HAMMER_INODE_ONDISK
|HAMMER_INODE_DELONDISK
)) !=
1012 HAMMER_INODE_ONDISK
) {
1016 hammer_normalize_cursor(cursor
);
1017 cursor
->key_beg
.localization
= ip
->obj_localization
+
1018 HAMMER_LOCALIZE_INODE
;
1019 cursor
->key_beg
.obj_id
= ip
->obj_id
;
1020 cursor
->key_beg
.key
= 0;
1021 cursor
->key_beg
.create_tid
= 0;
1022 cursor
->key_beg
.delete_tid
= 0;
1023 cursor
->key_beg
.rec_type
= HAMMER_RECTYPE_INODE
;
1024 cursor
->key_beg
.obj_type
= 0;
1025 cursor
->asof
= ip
->obj_asof
;
1026 cursor
->flags
&= ~HAMMER_CURSOR_INITMASK
;
1027 cursor
->flags
|= HAMMER_CURSOR_ASOF
;
1028 cursor
->flags
|= HAMMER_CURSOR_GET_LEAF
;
1029 cursor
->flags
|= HAMMER_CURSOR_GET_DATA
;
1030 cursor
->flags
|= HAMMER_CURSOR_BACKEND
;
1032 error
= hammer_btree_lookup(cursor
);
1034 kprintf("error %d\n", error
);
1035 Debugger("hammer_update_itimes1");
1038 hammer_cache_node(&ip
->cache
[0], cursor
->node
);
1039 if (ip
->sync_flags
& HAMMER_INODE_MTIME
) {
1041 * Updating MTIME requires an UNDO. Just cover
1042 * both atime and mtime.
1044 hammer_modify_buffer(trans
, cursor
->data_buffer
,
1045 HAMMER_ITIMES_BASE(&cursor
->data
->inode
),
1046 HAMMER_ITIMES_BYTES
);
1047 cursor
->data
->inode
.atime
= ip
->sync_ino_data
.atime
;
1048 cursor
->data
->inode
.mtime
= ip
->sync_ino_data
.mtime
;
1049 hammer_modify_buffer_done(cursor
->data_buffer
);
1050 } else if (ip
->sync_flags
& HAMMER_INODE_ATIME
) {
1052 * Updating atime only can be done in-place with
1055 hammer_modify_buffer(trans
, cursor
->data_buffer
,
1057 cursor
->data
->inode
.atime
= ip
->sync_ino_data
.atime
;
1058 hammer_modify_buffer_done(cursor
->data_buffer
);
1060 ip
->sync_flags
&= ~(HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
);
1062 if (error
== EDEADLK
) {
1063 hammer_done_cursor(cursor
);
1064 error
= hammer_init_cursor(trans
, cursor
,
1073 * Release a reference on an inode, flush as requested.
1075 * On the last reference we queue the inode to the flusher for its final
1079 hammer_rel_inode(struct hammer_inode
*ip
, int flush
)
1081 hammer_mount_t hmp
= ip
->hmp
;
1084 * Handle disposition when dropping the last ref.
1087 if (ip
->lock
.refs
== 1) {
1089 * Determine whether on-disk action is needed for
1090 * the inode's final disposition.
1092 KKASSERT(ip
->vp
== NULL
);
1093 hammer_inode_unloadable_check(ip
, 0);
1094 if (ip
->flags
& HAMMER_INODE_MODMASK
) {
1095 if (hmp
->rsv_inodes
> desiredvnodes
) {
1096 hammer_flush_inode(ip
,
1097 HAMMER_FLUSH_SIGNAL
);
1099 hammer_flush_inode(ip
, 0);
1101 } else if (ip
->lock
.refs
== 1) {
1102 hammer_unload_inode(ip
);
1107 hammer_flush_inode(ip
, 0);
1110 * The inode still has multiple refs, try to drop
1113 KKASSERT(ip
->lock
.refs
>= 1);
1114 if (ip
->lock
.refs
> 1) {
1115 hammer_unref(&ip
->lock
);
1123 * Unload and destroy the specified inode. Must be called with one remaining
1124 * reference. The reference is disposed of.
1126 * This can only be called in the context of the flusher.
1129 hammer_unload_inode(struct hammer_inode
*ip
)
1131 hammer_mount_t hmp
= ip
->hmp
;
1133 KASSERT(ip
->lock
.refs
== 1,
1134 ("hammer_unload_inode: %d refs\n", ip
->lock
.refs
));
1135 KKASSERT(ip
->vp
== NULL
);
1136 KKASSERT(ip
->flush_state
== HAMMER_FST_IDLE
);
1137 KKASSERT(ip
->cursor_ip_refs
== 0);
1138 KKASSERT(ip
->lock
.lockcount
== 0);
1139 KKASSERT((ip
->flags
& HAMMER_INODE_MODMASK
) == 0);
1141 KKASSERT(RB_EMPTY(&ip
->rec_tree
));
1142 KKASSERT(TAILQ_EMPTY(&ip
->target_list
));
1144 RB_REMOVE(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, ip
);
1146 hammer_free_inode(ip
);
1151 * Called on mount -u when switching from RW to RO or vise-versa. Adjust
1152 * the read-only flag for cached inodes.
1154 * This routine is called from a RB_SCAN().
1157 hammer_reload_inode(hammer_inode_t ip
, void *arg __unused
)
1159 hammer_mount_t hmp
= ip
->hmp
;
1161 if (hmp
->ronly
|| hmp
->asof
!= HAMMER_MAX_TID
)
1162 ip
->flags
|= HAMMER_INODE_RO
;
1164 ip
->flags
&= ~HAMMER_INODE_RO
;
1169 * A transaction has modified an inode, requiring updates as specified by
1172 * HAMMER_INODE_DDIRTY: Inode data has been updated
1173 * HAMMER_INODE_XDIRTY: Dirty in-memory records
1174 * HAMMER_INODE_BUFS: Dirty buffer cache buffers
1175 * HAMMER_INODE_DELETED: Inode record/data must be deleted
1176 * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1179 hammer_modify_inode(hammer_inode_t ip
, int flags
)
1181 KKASSERT(ip
->hmp
->ronly
== 0 ||
1182 (flags
& (HAMMER_INODE_DDIRTY
| HAMMER_INODE_XDIRTY
|
1183 HAMMER_INODE_BUFS
| HAMMER_INODE_DELETED
|
1184 HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
)) == 0);
1185 if ((ip
->flags
& HAMMER_INODE_RSV_INODES
) == 0) {
1186 ip
->flags
|= HAMMER_INODE_RSV_INODES
;
1187 ++ip
->hmp
->rsv_inodes
;
1194 * Request that an inode be flushed. This whole mess cannot block and may
1195 * recurse (if not synchronous). Once requested HAMMER will attempt to
1196 * actively flush the inode until the flush can be done.
1198 * The inode may already be flushing, or may be in a setup state. We can
1199 * place the inode in a flushing state if it is currently idle and flag it
1200 * to reflush if it is currently flushing.
1202 * If the HAMMER_FLUSH_SYNCHRONOUS flag is specified we will attempt to
1203 * flush the indoe synchronously using the caller's context.
1206 hammer_flush_inode(hammer_inode_t ip
, int flags
)
1211 * Trivial 'nothing to flush' case. If the inode is ina SETUP
1212 * state we have to put it back into an IDLE state so we can
1213 * drop the extra ref.
1215 if ((ip
->flags
& HAMMER_INODE_MODMASK
) == 0) {
1216 if (ip
->flush_state
== HAMMER_FST_SETUP
) {
1217 ip
->flush_state
= HAMMER_FST_IDLE
;
1218 hammer_rel_inode(ip
, 0);
1224 * Our flush action will depend on the current state.
1226 switch(ip
->flush_state
) {
1227 case HAMMER_FST_IDLE
:
1229 * We have no dependancies and can flush immediately. Some
1230 * our children may not be flushable so we have to re-test
1231 * with that additional knowledge.
1233 hammer_flush_inode_core(ip
, flags
);
1235 case HAMMER_FST_SETUP
:
1237 * Recurse upwards through dependancies via target_list
1238 * and start their flusher actions going if possible.
1240 * 'good' is our connectivity. -1 means we have none and
1241 * can't flush, 0 means there weren't any dependancies, and
1242 * 1 means we have good connectivity.
1244 good
= hammer_setup_parent_inodes(ip
);
1247 * We can continue if good >= 0. Determine how many records
1248 * under our inode can be flushed (and mark them).
1251 hammer_flush_inode_core(ip
, flags
);
1253 ip
->flags
|= HAMMER_INODE_REFLUSH
;
1254 if (flags
& HAMMER_FLUSH_SIGNAL
) {
1255 ip
->flags
|= HAMMER_INODE_RESIGNAL
;
1256 hammer_flusher_async(ip
->hmp
);
1262 * We are already flushing, flag the inode to reflush
1263 * if needed after it completes its current flush.
1265 if ((ip
->flags
& HAMMER_INODE_REFLUSH
) == 0)
1266 ip
->flags
|= HAMMER_INODE_REFLUSH
;
1267 if (flags
& HAMMER_FLUSH_SIGNAL
) {
1268 ip
->flags
|= HAMMER_INODE_RESIGNAL
;
1269 hammer_flusher_async(ip
->hmp
);
1276 * Scan ip->target_list, which is a list of records owned by PARENTS to our
1277 * ip which reference our ip.
1279 * XXX This is a huge mess of recursive code, but not one bit of it blocks
1280 * so for now do not ref/deref the structures. Note that if we use the
1281 * ref/rel code later, the rel CAN block.
1284 hammer_setup_parent_inodes(hammer_inode_t ip
)
1286 hammer_record_t depend
;
1288 hammer_record_t next
;
1295 TAILQ_FOREACH(depend
, &ip
->target_list
, target_entry
) {
1296 r
= hammer_setup_parent_inodes_helper(depend
);
1297 KKASSERT(depend
->target_ip
== ip
);
1298 if (r
< 0 && good
== 0)
1308 next
= TAILQ_FIRST(&ip
->target_list
);
1310 hammer_ref(&next
->lock
);
1311 hammer_ref(&next
->ip
->lock
);
1313 while ((depend
= next
) != NULL
) {
1314 if (depend
->target_ip
== NULL
) {
1316 hammer_rel_mem_record(depend
);
1317 hammer_rel_inode(pip
, 0);
1320 KKASSERT(depend
->target_ip
== ip
);
1321 next
= TAILQ_NEXT(depend
, target_entry
);
1323 hammer_ref(&next
->lock
);
1324 hammer_ref(&next
->ip
->lock
);
1326 r
= hammer_setup_parent_inodes_helper(depend
);
1327 if (r
< 0 && good
== 0)
1332 hammer_rel_mem_record(depend
);
1333 hammer_rel_inode(pip
, 0);
1340 * This helper function takes a record representing the dependancy between
1341 * the parent inode and child inode.
1343 * record->ip = parent inode
1344 * record->target_ip = child inode
1346 * We are asked to recurse upwards and convert the record from SETUP
1347 * to FLUSH if possible.
1349 * Return 1 if the record gives us connectivity
1351 * Return 0 if the record is not relevant
1353 * Return -1 if we can't resolve the dependancy and there is no connectivity.
1356 hammer_setup_parent_inodes_helper(hammer_record_t record
)
1362 KKASSERT(record
->flush_state
!= HAMMER_FST_IDLE
);
1367 * If the record is already flushing, is it in our flush group?
1369 * If it is in our flush group but it is a general record or a
1370 * delete-on-disk, it does not improve our connectivity (return 0),
1371 * and if the target inode is not trying to destroy itself we can't
1372 * allow the operation yet anyway (the second return -1).
1374 if (record
->flush_state
== HAMMER_FST_FLUSH
) {
1375 if (record
->flush_group
!= hmp
->flusher
.next
) {
1376 pip
->flags
|= HAMMER_INODE_REFLUSH
;
1379 if (record
->type
== HAMMER_MEM_RECORD_ADD
)
1381 /* GENERAL or DEL */
1386 * It must be a setup record. Try to resolve the setup dependancies
1387 * by recursing upwards so we can place ip on the flush list.
1389 KKASSERT(record
->flush_state
== HAMMER_FST_SETUP
);
1391 good
= hammer_setup_parent_inodes(pip
);
1394 * We can't flush ip because it has no connectivity (XXX also check
1395 * nlinks for pre-existing connectivity!). Flag it so any resolution
1396 * recurses back down.
1399 pip
->flags
|= HAMMER_INODE_REFLUSH
;
1404 * We are go, place the parent inode in a flushing state so we can
1405 * place its record in a flushing state. Note that the parent
1406 * may already be flushing. The record must be in the same flush
1407 * group as the parent.
1409 if (pip
->flush_state
!= HAMMER_FST_FLUSH
)
1410 hammer_flush_inode_core(pip
, HAMMER_FLUSH_RECURSION
);
1411 KKASSERT(pip
->flush_state
== HAMMER_FST_FLUSH
);
1412 KKASSERT(record
->flush_state
== HAMMER_FST_SETUP
);
1415 if (record
->type
== HAMMER_MEM_RECORD_DEL
&&
1416 (record
->target_ip
->flags
& (HAMMER_INODE_DELETED
|HAMMER_INODE_DELONDISK
)) == 0) {
1418 * Regardless of flushing state we cannot sync this path if the
1419 * record represents a delete-on-disk but the target inode
1420 * is not ready to sync its own deletion.
1422 * XXX need to count effective nlinks to determine whether
1423 * the flush is ok, otherwise removing a hardlink will
1424 * just leave the DEL record to rot.
1426 record
->target_ip
->flags
|= HAMMER_INODE_REFLUSH
;
1430 if (pip
->flush_group
== pip
->hmp
->flusher
.next
) {
1432 * This is the record we wanted to synchronize. If the
1433 * record went into a flush state while we blocked it
1434 * had better be in the correct flush group.
1436 if (record
->flush_state
!= HAMMER_FST_FLUSH
) {
1437 record
->flush_state
= HAMMER_FST_FLUSH
;
1438 record
->flush_group
= pip
->flush_group
;
1439 hammer_ref(&record
->lock
);
1441 KKASSERT(record
->flush_group
== pip
->flush_group
);
1443 if (record
->type
== HAMMER_MEM_RECORD_ADD
)
1447 * A general or delete-on-disk record does not contribute
1448 * to our visibility. We can still flush it, however.
1453 * We couldn't resolve the dependancies, request that the
1454 * inode be flushed when the dependancies can be resolved.
1456 pip
->flags
|= HAMMER_INODE_REFLUSH
;
1462 * This is the core routine placing an inode into the FST_FLUSH state.
1465 hammer_flush_inode_core(hammer_inode_t ip
, int flags
)
1470 * Set flush state and prevent the flusher from cycling into
1471 * the next flush group. Do not place the ip on the list yet.
1472 * Inodes not in the idle state get an extra reference.
1474 KKASSERT(ip
->flush_state
!= HAMMER_FST_FLUSH
);
1475 if (ip
->flush_state
== HAMMER_FST_IDLE
)
1476 hammer_ref(&ip
->lock
);
1477 ip
->flush_state
= HAMMER_FST_FLUSH
;
1478 ip
->flush_group
= ip
->hmp
->flusher
.next
;
1479 ++ip
->hmp
->flusher
.group_lock
;
1480 ++ip
->hmp
->count_iqueued
;
1481 ++hammer_count_iqueued
;
1484 * We need to be able to vfsync/truncate from the backend.
1486 KKASSERT((ip
->flags
& HAMMER_INODE_VHELD
) == 0);
1487 if (ip
->vp
&& (ip
->vp
->v_flag
& VINACTIVE
) == 0) {
1488 ip
->flags
|= HAMMER_INODE_VHELD
;
1493 * Figure out how many in-memory records we can actually flush
1494 * (not including inode meta-data, buffers, etc).
1496 * Do not add new records to the flush if this is a recursion or
1497 * if we must still complete a flush from the previous flush cycle.
1499 if (flags
& HAMMER_FLUSH_RECURSION
) {
1501 } else if (ip
->flags
& HAMMER_INODE_WOULDBLOCK
) {
1502 go_count
= RB_SCAN(hammer_rec_rb_tree
, &ip
->rec_tree
, NULL
,
1503 hammer_syncgrp_child_callback
, NULL
);
1506 go_count
= RB_SCAN(hammer_rec_rb_tree
, &ip
->rec_tree
, NULL
,
1507 hammer_setup_child_callback
, NULL
);
1511 * This is a more involved test that includes go_count. If we
1512 * can't flush, flag the inode and return. If go_count is 0 we
1513 * were are unable to flush any records in our rec_tree and
1514 * must ignore the XDIRTY flag.
1516 if (go_count
== 0) {
1517 if ((ip
->flags
& HAMMER_INODE_MODMASK_NOXDIRTY
) == 0) {
1518 ip
->flags
|= HAMMER_INODE_REFLUSH
;
1520 --ip
->hmp
->count_iqueued
;
1521 --hammer_count_iqueued
;
1523 ip
->flush_state
= HAMMER_FST_SETUP
;
1524 if (ip
->flags
& HAMMER_INODE_VHELD
) {
1525 ip
->flags
&= ~HAMMER_INODE_VHELD
;
1528 if (flags
& HAMMER_FLUSH_SIGNAL
) {
1529 ip
->flags
|= HAMMER_INODE_RESIGNAL
;
1530 hammer_flusher_async(ip
->hmp
);
1532 if (--ip
->hmp
->flusher
.group_lock
== 0)
1533 wakeup(&ip
->hmp
->flusher
.group_lock
);
1539 * Snapshot the state of the inode for the backend flusher.
1541 * We continue to retain save_trunc_off even when all truncations
1542 * have been resolved as an optimization to determine if we can
1543 * skip the B-Tree lookup for overwrite deletions.
1545 * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1546 * and stays in ip->flags. Once set, it stays set until the
1547 * inode is destroyed.
1549 * NOTE: If a truncation from a previous flush cycle had to be
1550 * continued into this one, the TRUNCATED flag will still be
1551 * set in sync_flags as will WOULDBLOCK. When this occurs
1552 * we CANNOT safely integrate a new truncation from the front-end
1553 * because there may be data records in-memory assigned a flush
1554 * state from the previous cycle that are supposed to be flushed
1555 * before the next frontend truncation.
1557 if ((ip
->flags
& (HAMMER_INODE_TRUNCATED
| HAMMER_INODE_WOULDBLOCK
)) ==
1558 HAMMER_INODE_TRUNCATED
) {
1559 KKASSERT((ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) == 0);
1560 ip
->sync_trunc_off
= ip
->trunc_off
;
1561 ip
->trunc_off
= 0x7FFFFFFFFFFFFFFFLL
;
1562 ip
->flags
&= ~HAMMER_INODE_TRUNCATED
;
1563 ip
->sync_flags
|= HAMMER_INODE_TRUNCATED
;
1566 * The save_trunc_off used to cache whether the B-Tree
1567 * holds any records past that point is not used until
1568 * after the truncation has succeeded, so we can safely
1571 if (ip
->save_trunc_off
> ip
->sync_trunc_off
)
1572 ip
->save_trunc_off
= ip
->sync_trunc_off
;
1574 ip
->sync_flags
|= (ip
->flags
& HAMMER_INODE_MODMASK
&
1575 ~HAMMER_INODE_TRUNCATED
);
1576 ip
->sync_ino_leaf
= ip
->ino_leaf
;
1577 ip
->sync_ino_data
= ip
->ino_data
;
1578 ip
->flags
&= ~HAMMER_INODE_MODMASK
| HAMMER_INODE_TRUNCATED
;
1579 #ifdef DEBUG_TRUNCATE
1580 if ((ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) && ip
== HammerTruncIp
)
1581 kprintf("truncateS %016llx\n", ip
->sync_trunc_off
);
1585 * The flusher list inherits our inode and reference.
1587 TAILQ_INSERT_TAIL(&ip
->hmp
->flush_list
, ip
, flush_entry
);
1588 if (--ip
->hmp
->flusher
.group_lock
== 0)
1589 wakeup(&ip
->hmp
->flusher
.group_lock
);
1591 if (flags
& HAMMER_FLUSH_SIGNAL
) {
1592 hammer_flusher_async(ip
->hmp
);
1597 * Callback for scan of ip->rec_tree. Try to include each record in our
1598 * flush. ip->flush_group has been set but the inode has not yet been
1599 * moved into a flushing state.
1601 * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1604 * We return 1 for any record placed or found in FST_FLUSH, which prevents
1605 * the caller from shortcutting the flush.
1608 hammer_setup_child_callback(hammer_record_t rec
, void *data
)
1610 hammer_inode_t target_ip
;
1615 * Deleted records are ignored. Note that the flush detects deleted
1616 * front-end records at multiple points to deal with races. This is
1617 * just the first line of defense. The only time DELETED_FE cannot
1618 * be set is when HAMMER_RECF_INTERLOCK_BE is set.
1620 * Don't get confused between record deletion and, say, directory
1621 * entry deletion. The deletion of a directory entry that is on
1622 * the media has nothing to do with the record deletion flags.
1624 * The flush_group for a record already in a flush state must
1625 * be updated. This case can only occur if the inode deleting
1626 * too many records had to be moved to the next flush group.
1628 if (rec
->flags
& (HAMMER_RECF_DELETED_FE
|HAMMER_RECF_DELETED_BE
)) {
1629 if (rec
->flush_state
== HAMMER_FST_FLUSH
) {
1630 KKASSERT(rec
->ip
->flags
& HAMMER_INODE_WOULDBLOCK
);
1631 rec
->flush_group
= rec
->ip
->flush_group
;
1640 * If the record is in an idle state it has no dependancies and
1646 switch(rec
->flush_state
) {
1647 case HAMMER_FST_IDLE
:
1649 * Record has no setup dependancy, we can flush it.
1651 KKASSERT(rec
->target_ip
== NULL
);
1652 rec
->flush_state
= HAMMER_FST_FLUSH
;
1653 rec
->flush_group
= ip
->flush_group
;
1654 hammer_ref(&rec
->lock
);
1657 case HAMMER_FST_SETUP
:
1659 * Record has a setup dependancy. Try to include the
1660 * target ip in the flush.
1662 * We have to be careful here, if we do not do the right
1663 * thing we can lose track of dirty inodes and the system
1664 * will lockup trying to allocate buffers.
1666 target_ip
= rec
->target_ip
;
1667 KKASSERT(target_ip
!= NULL
);
1668 KKASSERT(target_ip
->flush_state
!= HAMMER_FST_IDLE
);
1669 if (target_ip
->flush_state
== HAMMER_FST_FLUSH
) {
1671 * If the target IP is already flushing in our group
1672 * we are golden, otherwise make sure the target
1675 if (target_ip
->flush_group
== ip
->flush_group
) {
1676 rec
->flush_state
= HAMMER_FST_FLUSH
;
1677 rec
->flush_group
= ip
->flush_group
;
1678 hammer_ref(&rec
->lock
);
1681 target_ip
->flags
|= HAMMER_INODE_REFLUSH
;
1683 } else if (rec
->type
== HAMMER_MEM_RECORD_ADD
) {
1685 * If the target IP is not flushing we can force
1686 * it to flush, even if it is unable to write out
1687 * any of its own records we have at least one in
1688 * hand that we CAN deal with.
1690 rec
->flush_state
= HAMMER_FST_FLUSH
;
1691 rec
->flush_group
= ip
->flush_group
;
1692 hammer_ref(&rec
->lock
);
1693 hammer_flush_inode_core(target_ip
,
1694 HAMMER_FLUSH_RECURSION
);
1698 * General or delete-on-disk record.
1700 * XXX this needs help. If a delete-on-disk we could
1701 * disconnect the target. If the target has its own
1702 * dependancies they really need to be flushed.
1706 rec
->flush_state
= HAMMER_FST_FLUSH
;
1707 rec
->flush_group
= ip
->flush_group
;
1708 hammer_ref(&rec
->lock
);
1709 hammer_flush_inode_core(target_ip
,
1710 HAMMER_FLUSH_RECURSION
);
1714 case HAMMER_FST_FLUSH
:
1716 * If the WOULDBLOCK flag is set records may have been left
1717 * over from a previous flush attempt and should be moved
1718 * to the current flush group. If it is not set then all
1719 * such records had better have been flushed already or
1720 * already associated with the current flush group.
1722 if (ip
->flags
& HAMMER_INODE_WOULDBLOCK
) {
1723 rec
->flush_group
= ip
->flush_group
;
1725 KKASSERT(rec
->flush_group
== ip
->flush_group
);
1734 * This version just moves records already in a flush state to the new
1735 * flush group and that is it.
1738 hammer_syncgrp_child_callback(hammer_record_t rec
, void *data
)
1740 hammer_inode_t ip
= rec
->ip
;
1742 switch(rec
->flush_state
) {
1743 case HAMMER_FST_FLUSH
:
1744 if (ip
->flags
& HAMMER_INODE_WOULDBLOCK
) {
1745 rec
->flush_group
= ip
->flush_group
;
1747 KKASSERT(rec
->flush_group
== ip
->flush_group
);
1757 * Wait for a previously queued flush to complete. Not only do we need to
1758 * wait for the inode to sync out, we also may have to run the flusher again
1759 * to get it past the UNDO position pertaining to the flush so a crash does
1760 * not 'undo' our flush.
1763 hammer_wait_inode(hammer_inode_t ip
)
1765 hammer_mount_t hmp
= ip
->hmp
;
1769 sync_group
= ip
->flush_group
;
1770 waitcount
= (ip
->flags
& HAMMER_INODE_REFLUSH
) ? 2 : 1;
1772 if (ip
->flush_state
== HAMMER_FST_SETUP
) {
1773 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
1775 /* XXX can we make this != FST_IDLE ? check SETUP depends */
1776 while (ip
->flush_state
== HAMMER_FST_FLUSH
&&
1777 (ip
->flush_group
- sync_group
) < waitcount
) {
1778 ip
->flags
|= HAMMER_INODE_FLUSHW
;
1779 tsleep(&ip
->flags
, 0, "hmrwin", 0);
1781 while (hmp
->flusher
.done
- sync_group
< waitcount
) {
1783 hammer_flusher_sync(hmp
);
1788 * Called by the backend code when a flush has been completed.
1789 * The inode has already been removed from the flush list.
1791 * A pipelined flush can occur, in which case we must re-enter the
1792 * inode on the list and re-copy its fields.
1795 hammer_flush_inode_done(hammer_inode_t ip
)
1800 KKASSERT(ip
->flush_state
== HAMMER_FST_FLUSH
);
1805 * Merge left-over flags back into the frontend and fix the state.
1806 * Incomplete truncations are retained by the backend.
1808 ip
->flags
|= ip
->sync_flags
& ~HAMMER_INODE_TRUNCATED
;
1809 ip
->sync_flags
&= HAMMER_INODE_TRUNCATED
;
1812 * The backend may have adjusted nlinks, so if the adjusted nlinks
1813 * does not match the fronttend set the frontend's RDIRTY flag again.
1815 if (ip
->ino_data
.nlinks
!= ip
->sync_ino_data
.nlinks
)
1816 ip
->flags
|= HAMMER_INODE_DDIRTY
;
1819 * Fix up the dirty buffer status.
1821 if (ip
->vp
&& RB_ROOT(&ip
->vp
->v_rbdirty_tree
)) {
1822 ip
->flags
|= HAMMER_INODE_BUFS
;
1826 * Re-set the XDIRTY flag if some of the inode's in-memory records
1827 * could not be flushed.
1829 KKASSERT((RB_EMPTY(&ip
->rec_tree
) &&
1830 (ip
->flags
& HAMMER_INODE_XDIRTY
) == 0) ||
1831 (!RB_EMPTY(&ip
->rec_tree
) &&
1832 (ip
->flags
& HAMMER_INODE_XDIRTY
) != 0));
1835 * Do not lose track of inodes which no longer have vnode
1836 * assocations, otherwise they may never get flushed again.
1838 if ((ip
->flags
& HAMMER_INODE_MODMASK
) && ip
->vp
== NULL
)
1839 ip
->flags
|= HAMMER_INODE_REFLUSH
;
1842 * Clean up the vnode ref
1844 if (ip
->flags
& HAMMER_INODE_VHELD
) {
1845 ip
->flags
&= ~HAMMER_INODE_VHELD
;
1850 * Adjust flush_state. The target state (idle or setup) shouldn't
1851 * be terribly important since we will reflush if we really need
1854 * If the WOULDBLOCK flag is set we must re-flush immediately
1855 * to continue a potentially large deletion. The flag also causes
1856 * the hammer_setup_child_callback() to move records in the old
1857 * flush group to the new one.
1859 if (ip
->flags
& HAMMER_INODE_WOULDBLOCK
) {
1860 ip
->flush_state
= HAMMER_FST_IDLE
;
1861 hammer_flush_inode_core(ip
, HAMMER_FLUSH_SIGNAL
);
1862 ip
->flags
&= ~HAMMER_INODE_WOULDBLOCK
;
1864 } else if (TAILQ_EMPTY(&ip
->target_list
) && RB_EMPTY(&ip
->rec_tree
)) {
1865 ip
->flush_state
= HAMMER_FST_IDLE
;
1868 ip
->flush_state
= HAMMER_FST_SETUP
;
1872 --hmp
->count_iqueued
;
1873 --hammer_count_iqueued
;
1876 * If the frontend made more changes and requested another flush,
1877 * then try to get it running.
1879 if (ip
->flags
& HAMMER_INODE_REFLUSH
) {
1880 ip
->flags
&= ~HAMMER_INODE_REFLUSH
;
1881 if (ip
->flags
& HAMMER_INODE_RESIGNAL
) {
1882 ip
->flags
&= ~HAMMER_INODE_RESIGNAL
;
1883 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
1885 hammer_flush_inode(ip
, 0);
1890 * If the inode is now clean drop the space reservation.
1892 if ((ip
->flags
& HAMMER_INODE_MODMASK
) == 0 &&
1893 (ip
->flags
& HAMMER_INODE_RSV_INODES
)) {
1894 ip
->flags
&= ~HAMMER_INODE_RSV_INODES
;
1899 * Finally, if the frontend is waiting for a flush to complete,
1902 if (ip
->flush_state
!= HAMMER_FST_FLUSH
) {
1903 if (ip
->flags
& HAMMER_INODE_FLUSHW
) {
1904 ip
->flags
&= ~HAMMER_INODE_FLUSHW
;
1909 hammer_rel_inode(ip
, 0);
1913 * Called from hammer_sync_inode() to synchronize in-memory records
1917 hammer_sync_record_callback(hammer_record_t record
, void *data
)
1919 hammer_cursor_t cursor
= data
;
1920 hammer_transaction_t trans
= cursor
->trans
;
1924 * Skip records that do not belong to the current flush.
1926 ++hammer_stats_record_iterations
;
1927 if (record
->flush_state
!= HAMMER_FST_FLUSH
)
1931 if (record
->flush_group
!= record
->ip
->flush_group
) {
1932 kprintf("sync_record %p ip %p bad flush group %d %d\n", record
, record
->ip
, record
->flush_group
,record
->ip
->flush_group
);
1937 KKASSERT(record
->flush_group
== record
->ip
->flush_group
);
1940 * Interlock the record using the BE flag. Once BE is set the
1941 * frontend cannot change the state of FE.
1943 * NOTE: If FE is set prior to us setting BE we still sync the
1944 * record out, but the flush completion code converts it to
1945 * a delete-on-disk record instead of destroying it.
1947 KKASSERT((record
->flags
& HAMMER_RECF_INTERLOCK_BE
) == 0);
1948 record
->flags
|= HAMMER_RECF_INTERLOCK_BE
;
1951 * The backend may have already disposed of the record.
1953 if (record
->flags
& HAMMER_RECF_DELETED_BE
) {
1959 * If the whole inode is being deleting all on-disk records will
1960 * be deleted very soon, we can't sync any new records to disk
1961 * because they will be deleted in the same transaction they were
1962 * created in (delete_tid == create_tid), which will assert.
1964 * XXX There may be a case with RECORD_ADD with DELETED_FE set
1965 * that we currently panic on.
1967 if (record
->ip
->sync_flags
& HAMMER_INODE_DELETING
) {
1968 switch(record
->type
) {
1969 case HAMMER_MEM_RECORD_DATA
:
1971 * We don't have to do anything, if the record was
1972 * committed the space will have been accounted for
1976 case HAMMER_MEM_RECORD_GENERAL
:
1977 record
->flags
|= HAMMER_RECF_DELETED_FE
;
1978 record
->flags
|= HAMMER_RECF_DELETED_BE
;
1981 case HAMMER_MEM_RECORD_ADD
:
1982 panic("hammer_sync_record_callback: illegal add "
1983 "during inode deletion record %p", record
);
1984 break; /* NOT REACHED */
1985 case HAMMER_MEM_RECORD_INODE
:
1986 panic("hammer_sync_record_callback: attempt to "
1987 "sync inode record %p?", record
);
1988 break; /* NOT REACHED */
1989 case HAMMER_MEM_RECORD_DEL
:
1991 * Follow through and issue the on-disk deletion
1998 * If DELETED_FE is set special handling is needed for directory
1999 * entries. Dependant pieces related to the directory entry may
2000 * have already been synced to disk. If this occurs we have to
2001 * sync the directory entry and then change the in-memory record
2002 * from an ADD to a DELETE to cover the fact that it's been
2003 * deleted by the frontend.
2005 * A directory delete covering record (MEM_RECORD_DEL) can never
2006 * be deleted by the frontend.
2008 * Any other record type (aka DATA) can be deleted by the frontend.
2009 * XXX At the moment the flusher must skip it because there may
2010 * be another data record in the flush group for the same block,
2011 * meaning that some frontend data changes can leak into the backend's
2012 * synchronization point.
2014 if (record
->flags
& HAMMER_RECF_DELETED_FE
) {
2015 if (record
->type
== HAMMER_MEM_RECORD_ADD
) {
2016 record
->flags
|= HAMMER_RECF_CONVERT_DELETE
;
2018 KKASSERT(record
->type
!= HAMMER_MEM_RECORD_DEL
);
2019 record
->flags
|= HAMMER_RECF_DELETED_BE
;
2026 * Assign the create_tid for new records. Deletions already
2027 * have the record's entire key properly set up.
2029 if (record
->type
!= HAMMER_MEM_RECORD_DEL
)
2030 record
->leaf
.base
.create_tid
= trans
->tid
;
2031 record
->leaf
.create_ts
= trans
->time32
;
2033 error
= hammer_ip_sync_record_cursor(cursor
, record
);
2034 if (error
!= EDEADLK
)
2036 hammer_done_cursor(cursor
);
2037 error
= hammer_init_cursor(trans
, cursor
, &record
->ip
->cache
[0],
2042 record
->flags
&= ~HAMMER_RECF_CONVERT_DELETE
;
2046 if (error
!= -ENOSPC
) {
2047 kprintf("hammer_sync_record_callback: sync failed rec "
2048 "%p, error %d\n", record
, error
);
2049 Debugger("sync failed rec");
2053 hammer_flush_record_done(record
, error
);
2058 * XXX error handling
2061 hammer_sync_inode(hammer_inode_t ip
)
2063 struct hammer_transaction trans
;
2064 struct hammer_cursor cursor
;
2065 hammer_node_t tmp_node
;
2066 hammer_record_t depend
;
2067 hammer_record_t next
;
2068 int error
, tmp_error
;
2071 if ((ip
->sync_flags
& HAMMER_INODE_MODMASK
) == 0)
2074 hammer_start_transaction_fls(&trans
, ip
->hmp
);
2075 error
= hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2080 * Any directory records referencing this inode which are not in
2081 * our current flush group must adjust our nlink count for the
2082 * purposes of synchronization to disk.
2084 * Records which are in our flush group can be unlinked from our
2085 * inode now, potentially allowing the inode to be physically
2088 * This cannot block.
2090 nlinks
= ip
->ino_data
.nlinks
;
2091 next
= TAILQ_FIRST(&ip
->target_list
);
2092 while ((depend
= next
) != NULL
) {
2093 next
= TAILQ_NEXT(depend
, target_entry
);
2094 if (depend
->flush_state
== HAMMER_FST_FLUSH
&&
2095 depend
->flush_group
== ip
->hmp
->flusher
.act
) {
2097 * If this is an ADD that was deleted by the frontend
2098 * the frontend nlinks count will have already been
2099 * decremented, but the backend is going to sync its
2100 * directory entry and must account for it. The
2101 * record will be converted to a delete-on-disk when
2104 * If the ADD was not deleted by the frontend we
2105 * can remove the dependancy from our target_list.
2107 if (depend
->flags
& HAMMER_RECF_DELETED_FE
) {
2110 TAILQ_REMOVE(&ip
->target_list
, depend
,
2112 depend
->target_ip
= NULL
;
2114 } else if ((depend
->flags
& HAMMER_RECF_DELETED_FE
) == 0) {
2116 * Not part of our flush group
2118 KKASSERT((depend
->flags
& HAMMER_RECF_DELETED_BE
) == 0);
2119 switch(depend
->type
) {
2120 case HAMMER_MEM_RECORD_ADD
:
2123 case HAMMER_MEM_RECORD_DEL
:
2133 * Set dirty if we had to modify the link count.
2135 if (ip
->sync_ino_data
.nlinks
!= nlinks
) {
2136 KKASSERT((int64_t)nlinks
>= 0);
2137 ip
->sync_ino_data
.nlinks
= nlinks
;
2138 ip
->sync_flags
|= HAMMER_INODE_DDIRTY
;
2142 * If there is a trunction queued destroy any data past the (aligned)
2143 * truncation point. Userland will have dealt with the buffer
2144 * containing the truncation point for us.
2146 * We don't flush pending frontend data buffers until after we've
2147 * dealt with the truncation.
2149 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2151 * Interlock trunc_off. The VOP front-end may continue to
2152 * make adjustments to it while we are blocked.
2155 off_t aligned_trunc_off
;
2158 trunc_off
= ip
->sync_trunc_off
;
2159 blkmask
= hammer_blocksize(trunc_off
) - 1;
2160 aligned_trunc_off
= (trunc_off
+ blkmask
) & ~(int64_t)blkmask
;
2163 * Delete any whole blocks on-media. The front-end has
2164 * already cleaned out any partial block and made it
2165 * pending. The front-end may have updated trunc_off
2166 * while we were blocked so we only use sync_trunc_off.
2168 * This operation can blow out the buffer cache, EWOULDBLOCK
2169 * means we were unable to complete the deletion. The
2170 * deletion will update sync_trunc_off in that case.
2172 error
= hammer_ip_delete_range(&cursor
, ip
,
2174 0x7FFFFFFFFFFFFFFFLL
, 2);
2175 if (error
== EWOULDBLOCK
) {
2176 ip
->flags
|= HAMMER_INODE_WOULDBLOCK
;
2178 goto defer_buffer_flush
;
2182 Debugger("hammer_ip_delete_range errored");
2185 * Clear the truncation flag on the backend after we have
2186 * complete the deletions. Backend data is now good again
2187 * (including new records we are about to sync, below).
2189 * Leave sync_trunc_off intact. As we write additional
2190 * records the backend will update sync_trunc_off. This
2191 * tells the backend whether it can skip the overwrite
2192 * test. This should work properly even when the backend
2193 * writes full blocks where the truncation point straddles
2194 * the block because the comparison is against the base
2195 * offset of the record.
2197 ip
->sync_flags
&= ~HAMMER_INODE_TRUNCATED
;
2198 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2204 * Now sync related records. These will typically be directory
2205 * entries or delete-on-disk records.
2207 * Not all records will be flushed, but clear XDIRTY anyway. We
2208 * will set it again in the frontend hammer_flush_inode_done()
2209 * if records remain.
2212 tmp_error
= RB_SCAN(hammer_rec_rb_tree
, &ip
->rec_tree
, NULL
,
2213 hammer_sync_record_callback
, &cursor
);
2219 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2222 * Re-seek for inode update, assuming our cache hasn't been ripped
2223 * out from under us.
2226 tmp_node
= hammer_ref_node_safe(ip
->hmp
, &ip
->cache
[0], &error
);
2228 hammer_cursor_downgrade(&cursor
);
2229 hammer_lock_sh(&tmp_node
->lock
);
2230 if ((tmp_node
->flags
& HAMMER_NODE_DELETED
) == 0)
2231 hammer_cursor_seek(&cursor
, tmp_node
, 0);
2232 hammer_unlock(&tmp_node
->lock
);
2233 hammer_rel_node(tmp_node
);
2239 * If we are deleting the inode the frontend had better not have
2240 * any active references on elements making up the inode.
2242 * The call to hammer_ip_delete_clean() cleans up auxillary records
2243 * but not DB or DATA records. Those must have already been deleted
2244 * by the normal truncation mechanic.
2246 if (error
== 0 && ip
->sync_ino_data
.nlinks
== 0 &&
2247 RB_EMPTY(&ip
->rec_tree
) &&
2248 (ip
->sync_flags
& HAMMER_INODE_DELETING
) &&
2249 (ip
->flags
& HAMMER_INODE_DELETED
) == 0) {
2252 error
= hammer_ip_delete_clean(&cursor
, ip
, &count1
);
2254 ip
->flags
|= HAMMER_INODE_DELETED
;
2255 ip
->sync_flags
&= ~HAMMER_INODE_DELETING
;
2256 ip
->sync_flags
&= ~HAMMER_INODE_TRUNCATED
;
2257 KKASSERT(RB_EMPTY(&ip
->rec_tree
));
2260 * Set delete_tid in both the frontend and backend
2261 * copy of the inode record. The DELETED flag handles
2262 * this, do not set RDIRTY.
2264 ip
->ino_leaf
.base
.delete_tid
= trans
.tid
;
2265 ip
->sync_ino_leaf
.base
.delete_tid
= trans
.tid
;
2266 ip
->ino_leaf
.delete_ts
= trans
.time32
;
2267 ip
->sync_ino_leaf
.delete_ts
= trans
.time32
;
2271 * Adjust the inode count in the volume header
2273 if (ip
->flags
& HAMMER_INODE_ONDISK
) {
2274 hammer_modify_volume_field(&trans
,
2277 --ip
->hmp
->rootvol
->ondisk
->vol0_stat_inodes
;
2278 hammer_modify_volume_done(trans
.rootvol
);
2281 Debugger("hammer_ip_delete_clean errored");
2285 ip
->sync_flags
&= ~HAMMER_INODE_BUFS
;
2288 Debugger("RB_SCAN errored");
2292 * Now update the inode's on-disk inode-data and/or on-disk record.
2293 * DELETED and ONDISK are managed only in ip->flags.
2295 * In the case of a defered buffer flush we still update the on-disk
2296 * inode to satisfy visibility requirements if there happen to be
2297 * directory dependancies.
2299 switch(ip
->flags
& (HAMMER_INODE_DELETED
| HAMMER_INODE_ONDISK
)) {
2300 case HAMMER_INODE_DELETED
|HAMMER_INODE_ONDISK
:
2302 * If deleted and on-disk, don't set any additional flags.
2303 * the delete flag takes care of things.
2305 * Clear flags which may have been set by the frontend.
2307 ip
->sync_flags
&= ~(HAMMER_INODE_DDIRTY
| HAMMER_INODE_XDIRTY
|
2308 HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
|
2309 HAMMER_INODE_DELETING
);
2311 case HAMMER_INODE_DELETED
:
2313 * Take care of the case where a deleted inode was never
2314 * flushed to the disk in the first place.
2316 * Clear flags which may have been set by the frontend.
2318 ip
->sync_flags
&= ~(HAMMER_INODE_DDIRTY
| HAMMER_INODE_XDIRTY
|
2319 HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
|
2320 HAMMER_INODE_DELETING
);
2321 while (RB_ROOT(&ip
->rec_tree
)) {
2322 hammer_record_t record
= RB_ROOT(&ip
->rec_tree
);
2323 hammer_ref(&record
->lock
);
2324 KKASSERT(record
->lock
.refs
== 1);
2325 record
->flags
|= HAMMER_RECF_DELETED_FE
;
2326 record
->flags
|= HAMMER_RECF_DELETED_BE
;
2327 hammer_rel_mem_record(record
);
2330 case HAMMER_INODE_ONDISK
:
2332 * If already on-disk, do not set any additional flags.
2337 * If not on-disk and not deleted, set DDIRTY to force
2338 * an initial record to be written.
2340 * Also set the create_tid in both the frontend and backend
2341 * copy of the inode record.
2343 ip
->ino_leaf
.base
.create_tid
= trans
.tid
;
2344 ip
->ino_leaf
.create_ts
= trans
.time32
;
2345 ip
->sync_ino_leaf
.base
.create_tid
= trans
.tid
;
2346 ip
->sync_ino_leaf
.create_ts
= trans
.time32
;
2347 ip
->sync_flags
|= HAMMER_INODE_DDIRTY
;
2352 * If RDIRTY or DDIRTY is set, write out a new record. If the inode
2353 * is already on-disk the old record is marked as deleted.
2355 * If DELETED is set hammer_update_inode() will delete the existing
2356 * record without writing out a new one.
2358 * If *ONLY* the ITIMES flag is set we can update the record in-place.
2360 if (ip
->flags
& HAMMER_INODE_DELETED
) {
2361 error
= hammer_update_inode(&cursor
, ip
);
2363 if ((ip
->sync_flags
& HAMMER_INODE_DDIRTY
) == 0 &&
2364 (ip
->sync_flags
& (HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
))) {
2365 error
= hammer_update_itimes(&cursor
, ip
);
2367 if (ip
->sync_flags
& (HAMMER_INODE_DDIRTY
| HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
)) {
2368 error
= hammer_update_inode(&cursor
, ip
);
2371 Debugger("hammer_update_itimes/inode errored");
2374 * Save the TID we used to sync the inode with to make sure we
2375 * do not improperly reuse it.
2377 hammer_done_cursor(&cursor
);
2378 hammer_done_transaction(&trans
);
2383 * This routine is called when the OS is no longer actively referencing
2384 * the inode (but might still be keeping it cached), or when releasing
2385 * the last reference to an inode.
2387 * At this point if the inode's nlinks count is zero we want to destroy
2388 * it, which may mean destroying it on-media too.
2391 hammer_inode_unloadable_check(hammer_inode_t ip
, int getvp
)
2396 * Set the DELETING flag when the link count drops to 0 and the
2397 * OS no longer has any opens on the inode.
2399 * The backend will clear DELETING (a mod flag) and set DELETED
2400 * (a state flag) when it is actually able to perform the
2403 if (ip
->ino_data
.nlinks
== 0 &&
2404 (ip
->flags
& (HAMMER_INODE_DELETING
|HAMMER_INODE_DELETED
)) == 0) {
2405 ip
->flags
|= HAMMER_INODE_DELETING
;
2406 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
2410 if (hammer_get_vnode(ip
, &vp
) != 0)
2418 vtruncbuf(ip
->vp
, 0, HAMMER_BUFSIZE
);
2419 vnode_pager_setsize(ip
->vp
, 0);
2428 * Re-test an inode when a dependancy had gone away to see if we
2429 * can chain flush it.
2432 hammer_test_inode(hammer_inode_t ip
)
2434 if (ip
->flags
& HAMMER_INODE_REFLUSH
) {
2435 ip
->flags
&= ~HAMMER_INODE_REFLUSH
;
2436 hammer_ref(&ip
->lock
);
2437 if (ip
->flags
& HAMMER_INODE_RESIGNAL
) {
2438 ip
->flags
&= ~HAMMER_INODE_RESIGNAL
;
2439 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
2441 hammer_flush_inode(ip
, 0);
2443 hammer_rel_inode(ip
, 0);
2448 * Clear the RECLAIM flag on an inode. This occurs when the inode is
2449 * reassociated with a vp or just before it gets freed.
2451 * Wakeup one thread blocked waiting on reclaims to complete. Note that
2452 * the inode the thread is waiting on behalf of is a different inode then
2453 * the inode we are called with. This is to create a pipeline.
2456 hammer_inode_wakereclaims(hammer_inode_t ip
)
2458 struct hammer_reclaim
*reclaim
;
2459 hammer_mount_t hmp
= ip
->hmp
;
2461 if ((ip
->flags
& HAMMER_INODE_RECLAIM
) == 0)
2464 --hammer_count_reclaiming
;
2465 --hmp
->inode_reclaims
;
2466 ip
->flags
&= ~HAMMER_INODE_RECLAIM
;
2468 if ((reclaim
= TAILQ_FIRST(&hmp
->reclaim_list
)) != NULL
) {
2469 TAILQ_REMOVE(&hmp
->reclaim_list
, reclaim
, entry
);
2470 reclaim
->okydoky
= 1;
2476 * Setup our reclaim pipeline. We only let so many detached (and dirty)
2477 * inodes build up before we start blocking.
2479 * When we block we don't care *which* inode has finished reclaiming,
2480 * as lone as one does. This is somewhat heuristical... we also put a
2481 * cap on how long we are willing to wait.
2484 hammer_inode_waitreclaims(hammer_mount_t hmp
)
2486 struct hammer_reclaim reclaim
;
2489 if (hmp
->inode_reclaims
> HAMMER_RECLAIM_WAIT
) {
2490 reclaim
.okydoky
= 0;
2491 TAILQ_INSERT_TAIL(&hmp
->reclaim_list
,
2494 reclaim
.okydoky
= 1;
2497 if (reclaim
.okydoky
== 0) {
2498 delay
= (hmp
->inode_reclaims
- HAMMER_RECLAIM_WAIT
) * hz
/
2499 HAMMER_RECLAIM_WAIT
;
2501 tsleep(&reclaim
, 0, "hmrrcm", delay
+ 1);
2502 if (reclaim
.okydoky
== 0)
2503 TAILQ_REMOVE(&hmp
->reclaim_list
, &reclaim
, entry
);