HAMMER VFS - REDO implementation base code part 2/many
[dragonfly.git] / sys / vfs / hammer / hammer_inode.c
1 /*
2  * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_inode.c,v 1.114 2008/09/24 00:53:51 dillon Exp $
35  */
36
37 #include "hammer.h"
38 #include <vm/vm_extern.h>
39
40 static int      hammer_unload_inode(struct hammer_inode *ip);
41 static void     hammer_free_inode(hammer_inode_t ip);
42 static void     hammer_flush_inode_core(hammer_inode_t ip,
43                                         hammer_flush_group_t flg, int flags);
44 static int      hammer_setup_child_callback(hammer_record_t rec, void *data);
45 #if 0
46 static int      hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
47 #endif
48 static int      hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
49                                         hammer_flush_group_t flg);
50 static int      hammer_setup_parent_inodes_helper(hammer_record_t record,
51                                         int depth, hammer_flush_group_t flg);
52 static void     hammer_inode_wakereclaims(hammer_inode_t ip);
53
54 #ifdef DEBUG_TRUNCATE
55 extern struct hammer_inode *HammerTruncIp;
56 #endif
57
58 /*
59  * RB-Tree support for inode structures
60  */
61 int
62 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
63 {
64         if (ip1->obj_localization < ip2->obj_localization)
65                 return(-1);
66         if (ip1->obj_localization > ip2->obj_localization)
67                 return(1);
68         if (ip1->obj_id < ip2->obj_id)
69                 return(-1);
70         if (ip1->obj_id > ip2->obj_id)
71                 return(1);
72         if (ip1->obj_asof < ip2->obj_asof)
73                 return(-1);
74         if (ip1->obj_asof > ip2->obj_asof)
75                 return(1);
76         return(0);
77 }
78
79 /*
80  * RB-Tree support for inode structures / special LOOKUP_INFO
81  */
82 static int
83 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
84 {
85         if (info->obj_localization < ip->obj_localization)
86                 return(-1);
87         if (info->obj_localization > ip->obj_localization)
88                 return(1);
89         if (info->obj_id < ip->obj_id)
90                 return(-1);
91         if (info->obj_id > ip->obj_id)
92                 return(1);
93         if (info->obj_asof < ip->obj_asof)
94                 return(-1);
95         if (info->obj_asof > ip->obj_asof)
96                 return(1);
97         return(0);
98 }
99
100 /*
101  * Used by hammer_scan_inode_snapshots() to locate all of an object's
102  * snapshots.  Note that the asof field is not tested, which we can get
103  * away with because it is the lowest-priority field.
104  */
105 static int
106 hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
107 {
108         hammer_inode_info_t info = data;
109
110         if (ip->obj_localization > info->obj_localization)
111                 return(1);
112         if (ip->obj_localization < info->obj_localization)
113                 return(-1);
114         if (ip->obj_id > info->obj_id)
115                 return(1);
116         if (ip->obj_id < info->obj_id)
117                 return(-1);
118         return(0);
119 }
120
121 /*
122  * Used by hammer_unload_pseudofs() to locate all inodes associated with
123  * a particular PFS.
124  */
125 static int
126 hammer_inode_pfs_cmp(hammer_inode_t ip, void *data)
127 {
128         u_int32_t localization = *(u_int32_t *)data;
129         if (ip->obj_localization > localization)
130                 return(1);
131         if (ip->obj_localization < localization)
132                 return(-1);
133         return(0);
134 }
135
136 /*
137  * RB-Tree support for pseudofs structures
138  */
139 static int
140 hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
141 {
142         if (p1->localization < p2->localization)
143                 return(-1);
144         if (p1->localization > p2->localization)
145                 return(1);
146         return(0);
147 }
148
149
150 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
151 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
152                 hammer_inode_info_cmp, hammer_inode_info_t);
153 RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
154              hammer_pfs_rb_compare, u_int32_t, localization);
155
156 /*
157  * The kernel is not actively referencing this vnode but is still holding
158  * it cached.
159  *
160  * This is called from the frontend.
161  *
162  * MPALMOSTSAFE
163  */
164 int
165 hammer_vop_inactive(struct vop_inactive_args *ap)
166 {
167         struct hammer_inode *ip = VTOI(ap->a_vp);
168
169         /*
170          * Degenerate case
171          */
172         if (ip == NULL) {
173                 vrecycle(ap->a_vp);
174                 return(0);
175         }
176
177         /*
178          * If the inode no longer has visibility in the filesystem try to
179          * recycle it immediately, even if the inode is dirty.  Recycling
180          * it quickly allows the system to reclaim buffer cache and VM
181          * resources which can matter a lot in a heavily loaded system.
182          *
183          * This can deadlock in vfsync() if we aren't careful.
184          * 
185          * Do not queue the inode to the flusher if we still have visibility,
186          * otherwise namespace calls such as chmod will unnecessarily generate
187          * multiple inode updates.
188          */
189         if (ip->ino_data.nlinks == 0) {
190                 get_mplock();
191                 hammer_inode_unloadable_check(ip, 0);
192                 if (ip->flags & HAMMER_INODE_MODMASK)
193                         hammer_flush_inode(ip, 0);
194                 vrecycle(ap->a_vp);
195                 rel_mplock();
196         }
197         return(0);
198 }
199
200 /*
201  * Release the vnode association.  This is typically (but not always)
202  * the last reference on the inode.
203  *
204  * Once the association is lost we are on our own with regards to
205  * flushing the inode.
206  */
207 int
208 hammer_vop_reclaim(struct vop_reclaim_args *ap)
209 {
210         struct hammer_inode *ip;
211         hammer_mount_t hmp;
212         struct vnode *vp;
213
214         vp = ap->a_vp;
215
216         if ((ip = vp->v_data) != NULL) {
217                 hmp = ip->hmp;
218                 vp->v_data = NULL;
219                 ip->vp = NULL;
220
221                 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
222                         ++hammer_count_reclaiming;
223                         ++hmp->inode_reclaims;
224                         ip->flags |= HAMMER_INODE_RECLAIM;
225                 }
226                 hammer_rel_inode(ip, 1);
227         }
228         return(0);
229 }
230
231 /*
232  * Return a locked vnode for the specified inode.  The inode must be
233  * referenced but NOT LOCKED on entry and will remain referenced on
234  * return.
235  *
236  * Called from the frontend.
237  */
238 int
239 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
240 {
241         hammer_mount_t hmp;
242         struct vnode *vp;
243         int error = 0;
244         u_int8_t obj_type;
245
246         hmp = ip->hmp;
247
248         for (;;) {
249                 if ((vp = ip->vp) == NULL) {
250                         error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
251                         if (error)
252                                 break;
253                         hammer_lock_ex(&ip->lock);
254                         if (ip->vp != NULL) {
255                                 hammer_unlock(&ip->lock);
256                                 vp = *vpp;
257                                 vp->v_type = VBAD;
258                                 vx_put(vp);
259                                 continue;
260                         }
261                         hammer_ref(&ip->lock);
262                         vp = *vpp;
263                         ip->vp = vp;
264
265                         obj_type = ip->ino_data.obj_type;
266                         vp->v_type = hammer_get_vnode_type(obj_type);
267
268                         hammer_inode_wakereclaims(ip);
269
270                         switch(ip->ino_data.obj_type) {
271                         case HAMMER_OBJTYPE_CDEV:
272                         case HAMMER_OBJTYPE_BDEV:
273                                 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
274                                 addaliasu(vp, ip->ino_data.rmajor,
275                                           ip->ino_data.rminor);
276                                 break;
277                         case HAMMER_OBJTYPE_FIFO:
278                                 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
279                                 break;
280                         case HAMMER_OBJTYPE_REGFILE:
281                                 break;
282                         default:
283                                 break;
284                         }
285
286                         /*
287                          * Only mark as the root vnode if the ip is not
288                          * historical, otherwise the VFS cache will get
289                          * confused.  The other half of the special handling
290                          * is in hammer_vop_nlookupdotdot().
291                          *
292                          * Pseudo-filesystem roots can be accessed via
293                          * non-root filesystem paths and setting VROOT may
294                          * confuse the namecache.  Set VPFSROOT instead.
295                          */
296                         if (ip->obj_id == HAMMER_OBJID_ROOT &&
297                             ip->obj_asof == hmp->asof) {
298                                 if (ip->obj_localization == 0)
299                                         vsetflags(vp, VROOT);
300                                 else
301                                         vsetflags(vp, VPFSROOT);
302                         }
303
304                         vp->v_data = (void *)ip;
305                         /* vnode locked by getnewvnode() */
306                         /* make related vnode dirty if inode dirty? */
307                         hammer_unlock(&ip->lock);
308                         if (vp->v_type == VREG)
309                                 vinitvmio(vp, ip->ino_data.size);
310                         break;
311                 }
312
313                 /*
314                  * loop if the vget fails (aka races), or if the vp
315                  * no longer matches ip->vp.
316                  */
317                 if (vget(vp, LK_EXCLUSIVE) == 0) {
318                         if (vp == ip->vp)
319                                 break;
320                         vput(vp);
321                 }
322         }
323         *vpp = vp;
324         return(error);
325 }
326
327 /*
328  * Locate all copies of the inode for obj_id compatible with the specified
329  * asof, reference, and issue the related call-back.  This routine is used
330  * for direct-io invalidation and does not create any new inodes.
331  */
332 void
333 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
334                             int (*callback)(hammer_inode_t ip, void *data),
335                             void *data)
336 {
337         hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
338                                    hammer_inode_info_cmp_all_history,
339                                    callback, iinfo);
340 }
341
342 /*
343  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
344  * do not attach or detach the related vnode (use hammer_get_vnode() for
345  * that).
346  *
347  * The flags argument is only applied for newly created inodes, and only
348  * certain flags are inherited.
349  *
350  * Called from the frontend.
351  */
352 struct hammer_inode *
353 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
354                  int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
355                  int flags, int *errorp)
356 {
357         hammer_mount_t hmp = trans->hmp;
358         struct hammer_node_cache *cachep;
359         struct hammer_inode_info iinfo;
360         struct hammer_cursor cursor;
361         struct hammer_inode *ip;
362
363
364         /*
365          * Determine if we already have an inode cached.  If we do then
366          * we are golden.
367          *
368          * If we find an inode with no vnode we have to mark the
369          * transaction such that hammer_inode_waitreclaims() is
370          * called later on to avoid building up an infinite number
371          * of inodes.  Otherwise we can continue to * add new inodes
372          * faster then they can be disposed of, even with the tsleep
373          * delay.
374          *
375          * If we find a dummy inode we return a failure so dounlink
376          * (which does another lookup) doesn't try to mess with the
377          * link count.  hammer_vop_nresolve() uses hammer_get_dummy_inode()
378          * to ref dummy inodes.
379          */
380         iinfo.obj_id = obj_id;
381         iinfo.obj_asof = asof;
382         iinfo.obj_localization = localization;
383 loop:
384         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
385         if (ip) {
386                 if (ip->flags & HAMMER_INODE_DUMMY) {
387                         *errorp = ENOENT;
388                         return(NULL);
389                 }
390                 hammer_ref(&ip->lock);
391                 *errorp = 0;
392                 return(ip);
393         }
394
395         /*
396          * Allocate a new inode structure and deal with races later.
397          */
398         ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
399         ++hammer_count_inodes;
400         ++hmp->count_inodes;
401         ip->obj_id = obj_id;
402         ip->obj_asof = iinfo.obj_asof;
403         ip->obj_localization = localization;
404         ip->hmp = hmp;
405         ip->flags = flags & HAMMER_INODE_RO;
406         ip->cache[0].ip = ip;
407         ip->cache[1].ip = ip;
408         ip->cache[2].ip = ip;
409         ip->cache[3].ip = ip;
410         if (hmp->ronly)
411                 ip->flags |= HAMMER_INODE_RO;
412         ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
413                 0x7FFFFFFFFFFFFFFFLL;
414         RB_INIT(&ip->rec_tree);
415         TAILQ_INIT(&ip->target_list);
416         hammer_ref(&ip->lock);
417
418         /*
419          * Locate the on-disk inode.  If this is a PFS root we always
420          * access the current version of the root inode and (if it is not
421          * a master) always access information under it with a snapshot
422          * TID.
423          *
424          * We cache recent inode lookups in this directory in dip->cache[2].
425          * If we can't find it we assume the inode we are looking for is
426          * close to the directory inode.
427          */
428 retry:
429         cachep = NULL;
430         if (dip) {
431                 if (dip->cache[2].node)
432                         cachep = &dip->cache[2];
433                 else
434                         cachep = &dip->cache[0];
435         }
436         hammer_init_cursor(trans, &cursor, cachep, NULL);
437         cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
438         cursor.key_beg.obj_id = ip->obj_id;
439         cursor.key_beg.key = 0;
440         cursor.key_beg.create_tid = 0;
441         cursor.key_beg.delete_tid = 0;
442         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
443         cursor.key_beg.obj_type = 0;
444
445         cursor.asof = iinfo.obj_asof;
446         cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
447                        HAMMER_CURSOR_ASOF;
448
449         *errorp = hammer_btree_lookup(&cursor);
450         if (*errorp == EDEADLK) {
451                 hammer_done_cursor(&cursor);
452                 goto retry;
453         }
454
455         /*
456          * On success the B-Tree lookup will hold the appropriate
457          * buffer cache buffers and provide a pointer to the requested
458          * information.  Copy the information to the in-memory inode
459          * and cache the B-Tree node to improve future operations.
460          */
461         if (*errorp == 0) {
462                 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
463                 ip->ino_data = cursor.data->inode;
464
465                 /*
466                  * cache[0] tries to cache the location of the object inode.
467                  * The assumption is that it is near the directory inode.
468                  *
469                  * cache[1] tries to cache the location of the object data.
470                  * We might have something in the governing directory from
471                  * scan optimizations (see the strategy code in
472                  * hammer_vnops.c).
473                  *
474                  * We update dip->cache[2], if possible, with the location
475                  * of the object inode for future directory shortcuts.
476                  */
477                 hammer_cache_node(&ip->cache[0], cursor.node);
478                 if (dip) {
479                         if (dip->cache[3].node) {
480                                 hammer_cache_node(&ip->cache[1],
481                                                   dip->cache[3].node);
482                         }
483                         hammer_cache_node(&dip->cache[2], cursor.node);
484                 }
485
486                 /*
487                  * The file should not contain any data past the file size
488                  * stored in the inode.  Setting save_trunc_off to the
489                  * file size instead of max reduces B-Tree lookup overheads
490                  * on append by allowing the flusher to avoid checking for
491                  * record overwrites.
492                  */
493                 ip->save_trunc_off = ip->ino_data.size;
494
495                 /*
496                  * Locate and assign the pseudofs management structure to
497                  * the inode.
498                  */
499                 if (dip && dip->obj_localization == ip->obj_localization) {
500                         ip->pfsm = dip->pfsm;
501                         hammer_ref(&ip->pfsm->lock);
502                 } else {
503                         ip->pfsm = hammer_load_pseudofs(trans,
504                                                         ip->obj_localization,
505                                                         errorp);
506                         *errorp = 0;    /* ignore ENOENT */
507                 }
508         }
509
510         /*
511          * The inode is placed on the red-black tree and will be synced to
512          * the media when flushed or by the filesystem sync.  If this races
513          * another instantiation/lookup the insertion will fail.
514          */
515         if (*errorp == 0) {
516                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
517                         hammer_free_inode(ip);
518                         hammer_done_cursor(&cursor);
519                         goto loop;
520                 }
521                 ip->flags |= HAMMER_INODE_ONDISK;
522         } else {
523                 if (ip->flags & HAMMER_INODE_RSV_INODES) {
524                         ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
525                         --hmp->rsv_inodes;
526                 }
527
528                 hammer_free_inode(ip);
529                 ip = NULL;
530         }
531         hammer_done_cursor(&cursor);
532         trans->flags |= HAMMER_TRANSF_NEWINODE;
533         return (ip);
534 }
535
536 /*
537  * Get a dummy inode to placemark a broken directory entry.
538  */
539 struct hammer_inode *
540 hammer_get_dummy_inode(hammer_transaction_t trans, hammer_inode_t dip,
541                  int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
542                  int flags, int *errorp)
543 {
544         hammer_mount_t hmp = trans->hmp;
545         struct hammer_inode_info iinfo;
546         struct hammer_inode *ip;
547
548         /*
549          * Determine if we already have an inode cached.  If we do then
550          * we are golden.
551          *
552          * If we find an inode with no vnode we have to mark the
553          * transaction such that hammer_inode_waitreclaims() is
554          * called later on to avoid building up an infinite number
555          * of inodes.  Otherwise we can continue to * add new inodes
556          * faster then they can be disposed of, even with the tsleep
557          * delay.
558          *
559          * If we find a non-fake inode we return an error.  Only fake
560          * inodes can be returned by this routine.
561          */
562         iinfo.obj_id = obj_id;
563         iinfo.obj_asof = asof;
564         iinfo.obj_localization = localization;
565 loop:
566         *errorp = 0;
567         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
568         if (ip) {
569                 if ((ip->flags & HAMMER_INODE_DUMMY) == 0) {
570                         *errorp = ENOENT;
571                         return(NULL);
572                 }
573                 hammer_ref(&ip->lock);
574                 return(ip);
575         }
576
577         /*
578          * Allocate a new inode structure and deal with races later.
579          */
580         ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
581         ++hammer_count_inodes;
582         ++hmp->count_inodes;
583         ip->obj_id = obj_id;
584         ip->obj_asof = iinfo.obj_asof;
585         ip->obj_localization = localization;
586         ip->hmp = hmp;
587         ip->flags = flags | HAMMER_INODE_RO | HAMMER_INODE_DUMMY;
588         ip->cache[0].ip = ip;
589         ip->cache[1].ip = ip;
590         ip->cache[2].ip = ip;
591         ip->cache[3].ip = ip;
592         ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
593                 0x7FFFFFFFFFFFFFFFLL;
594         RB_INIT(&ip->rec_tree);
595         TAILQ_INIT(&ip->target_list);
596         hammer_ref(&ip->lock);
597
598         /*
599          * Populate the dummy inode.  Leave everything zero'd out.
600          *
601          * (ip->ino_leaf and ip->ino_data)
602          *
603          * Make the dummy inode a FIFO object which most copy programs
604          * will properly ignore.
605          */
606         ip->save_trunc_off = ip->ino_data.size;
607         ip->ino_data.obj_type = HAMMER_OBJTYPE_FIFO;
608
609         /*
610          * Locate and assign the pseudofs management structure to
611          * the inode.
612          */
613         if (dip && dip->obj_localization == ip->obj_localization) {
614                 ip->pfsm = dip->pfsm;
615                 hammer_ref(&ip->pfsm->lock);
616         } else {
617                 ip->pfsm = hammer_load_pseudofs(trans, ip->obj_localization,
618                                                 errorp);
619                 *errorp = 0;    /* ignore ENOENT */
620         }
621
622         /*
623          * The inode is placed on the red-black tree and will be synced to
624          * the media when flushed or by the filesystem sync.  If this races
625          * another instantiation/lookup the insertion will fail.
626          *
627          * NOTE: Do not set HAMMER_INODE_ONDISK.  The inode is a fake.
628          */
629         if (*errorp == 0) {
630                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
631                         hammer_free_inode(ip);
632                         goto loop;
633                 }
634         } else {
635                 if (ip->flags & HAMMER_INODE_RSV_INODES) {
636                         ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
637                         --hmp->rsv_inodes;
638                 }
639                 hammer_free_inode(ip);
640                 ip = NULL;
641         }
642         trans->flags |= HAMMER_TRANSF_NEWINODE;
643         return (ip);
644 }
645
646 /*
647  * Return a referenced inode only if it is in our inode cache.
648  *
649  * Dummy inodes do not count.
650  */
651 struct hammer_inode *
652 hammer_find_inode(hammer_transaction_t trans, int64_t obj_id,
653                   hammer_tid_t asof, u_int32_t localization)
654 {
655         hammer_mount_t hmp = trans->hmp;
656         struct hammer_inode_info iinfo;
657         struct hammer_inode *ip;
658
659         iinfo.obj_id = obj_id;
660         iinfo.obj_asof = asof;
661         iinfo.obj_localization = localization;
662
663         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
664         if (ip) {
665                 if (ip->flags & HAMMER_INODE_DUMMY)
666                         ip = NULL;
667                 else
668                         hammer_ref(&ip->lock);
669         }
670         return(ip);
671 }
672
673 /*
674  * Create a new filesystem object, returning the inode in *ipp.  The
675  * returned inode will be referenced.  The inode is created in-memory.
676  *
677  * If pfsm is non-NULL the caller wishes to create the root inode for
678  * a master PFS.
679  */
680 int
681 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
682                     struct ucred *cred,
683                     hammer_inode_t dip, const char *name, int namelen,
684                     hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
685 {
686         hammer_mount_t hmp;
687         hammer_inode_t ip;
688         uid_t xuid;
689         int error;
690         int64_t namekey;
691         u_int32_t dummy;
692
693         hmp = trans->hmp;
694
695         ip = kmalloc(sizeof(*ip), hmp->m_inodes, M_WAITOK|M_ZERO);
696         ++hammer_count_inodes;
697         ++hmp->count_inodes;
698         trans->flags |= HAMMER_TRANSF_NEWINODE;
699
700         if (pfsm) {
701                 KKASSERT(pfsm->localization != 0);
702                 ip->obj_id = HAMMER_OBJID_ROOT;
703                 ip->obj_localization = pfsm->localization;
704         } else {
705                 KKASSERT(dip != NULL);
706                 namekey = hammer_directory_namekey(dip, name, namelen, &dummy);
707                 ip->obj_id = hammer_alloc_objid(hmp, dip, namekey);
708                 ip->obj_localization = dip->obj_localization;
709         }
710
711         KKASSERT(ip->obj_id != 0);
712         ip->obj_asof = hmp->asof;
713         ip->hmp = hmp;
714         ip->flush_state = HAMMER_FST_IDLE;
715         ip->flags = HAMMER_INODE_DDIRTY |
716                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
717         ip->cache[0].ip = ip;
718         ip->cache[1].ip = ip;
719         ip->cache[2].ip = ip;
720         ip->cache[3].ip = ip;
721
722         ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
723         /* ip->save_trunc_off = 0; (already zero) */
724         RB_INIT(&ip->rec_tree);
725         TAILQ_INIT(&ip->target_list);
726
727         ip->ino_data.atime = trans->time;
728         ip->ino_data.mtime = trans->time;
729         ip->ino_data.size = 0;
730         ip->ino_data.nlinks = 0;
731
732         /*
733          * A nohistory designator on the parent directory is inherited by
734          * the child.  We will do this even for pseudo-fs creation... the
735          * sysad can turn it off.
736          */
737         if (dip) {
738                 ip->ino_data.uflags = dip->ino_data.uflags &
739                                       (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
740         }
741
742         ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
743         ip->ino_leaf.base.localization = ip->obj_localization +
744                                          HAMMER_LOCALIZE_INODE;
745         ip->ino_leaf.base.obj_id = ip->obj_id;
746         ip->ino_leaf.base.key = 0;
747         ip->ino_leaf.base.create_tid = 0;
748         ip->ino_leaf.base.delete_tid = 0;
749         ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
750         ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
751
752         ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
753         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
754         ip->ino_data.mode = vap->va_mode;
755         ip->ino_data.ctime = trans->time;
756
757         /*
758          * If we are running version 2 or greater directory entries are
759          * inode-localized instead of data-localized.
760          */
761         if (trans->hmp->version >= HAMMER_VOL_VERSION_TWO) {
762                 if (ip->ino_leaf.base.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
763                         ip->ino_data.cap_flags |=
764                                 HAMMER_INODE_CAP_DIR_LOCAL_INO;
765                 }
766         }
767
768         /*
769          * Setup the ".." pointer.  This only needs to be done for directories
770          * but we do it for all objects as a recovery aid.
771          */
772         if (dip)
773                 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
774 #if 0
775         /*
776          * The parent_obj_localization field only applies to pseudo-fs roots.
777          * XXX this is no longer applicable, PFSs are no longer directly
778          * tied into the parent's directory structure.
779          */
780         if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
781             ip->obj_id == HAMMER_OBJID_ROOT) {
782                 ip->ino_data.ext.obj.parent_obj_localization = 
783                                                 dip->obj_localization;
784         }
785 #endif
786
787         switch(ip->ino_leaf.base.obj_type) {
788         case HAMMER_OBJTYPE_CDEV:
789         case HAMMER_OBJTYPE_BDEV:
790                 ip->ino_data.rmajor = vap->va_rmajor;
791                 ip->ino_data.rminor = vap->va_rminor;
792                 break;
793         default:
794                 break;
795         }
796
797         /*
798          * Calculate default uid/gid and overwrite with information from
799          * the vap.
800          */
801         if (dip) {
802                 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
803                 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
804                                              xuid, cred, &vap->va_mode);
805         } else {
806                 xuid = 0;
807         }
808         ip->ino_data.mode = vap->va_mode;
809
810         if (vap->va_vaflags & VA_UID_UUID_VALID)
811                 ip->ino_data.uid = vap->va_uid_uuid;
812         else if (vap->va_uid != (uid_t)VNOVAL)
813                 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
814         else
815                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
816
817         if (vap->va_vaflags & VA_GID_UUID_VALID)
818                 ip->ino_data.gid = vap->va_gid_uuid;
819         else if (vap->va_gid != (gid_t)VNOVAL)
820                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
821         else if (dip)
822                 ip->ino_data.gid = dip->ino_data.gid;
823
824         hammer_ref(&ip->lock);
825
826         if (pfsm) {
827                 ip->pfsm = pfsm;
828                 hammer_ref(&pfsm->lock);
829                 error = 0;
830         } else if (dip->obj_localization == ip->obj_localization) {
831                 ip->pfsm = dip->pfsm;
832                 hammer_ref(&ip->pfsm->lock);
833                 error = 0;
834         } else {
835                 ip->pfsm = hammer_load_pseudofs(trans,
836                                                 ip->obj_localization,
837                                                 &error);
838                 error = 0;      /* ignore ENOENT */
839         }
840
841         if (error) {
842                 hammer_free_inode(ip);
843                 ip = NULL;
844         } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
845                 panic("hammer_create_inode: duplicate obj_id %llx",
846                       (long long)ip->obj_id);
847                 /* not reached */
848                 hammer_free_inode(ip);
849         }
850         *ipp = ip;
851         return(error);
852 }
853
854 /*
855  * Final cleanup / freeing of an inode structure
856  */
857 static void
858 hammer_free_inode(hammer_inode_t ip)
859 {
860         struct hammer_mount *hmp;
861
862         hmp = ip->hmp;
863         KKASSERT(ip->lock.refs == 1);
864         hammer_uncache_node(&ip->cache[0]);
865         hammer_uncache_node(&ip->cache[1]);
866         hammer_uncache_node(&ip->cache[2]);
867         hammer_uncache_node(&ip->cache[3]);
868         hammer_inode_wakereclaims(ip);
869         if (ip->objid_cache)
870                 hammer_clear_objid(ip);
871         --hammer_count_inodes;
872         --hmp->count_inodes;
873         if (ip->pfsm) {
874                 hammer_rel_pseudofs(hmp, ip->pfsm);
875                 ip->pfsm = NULL;
876         }
877         kfree(ip, hmp->m_inodes);
878         ip = NULL;
879 }
880
881 /*
882  * Retrieve pseudo-fs data.  NULL will never be returned.
883  *
884  * If an error occurs *errorp will be set and a default template is returned,
885  * otherwise *errorp is set to 0.  Typically when an error occurs it will
886  * be ENOENT.
887  */
888 hammer_pseudofs_inmem_t
889 hammer_load_pseudofs(hammer_transaction_t trans,
890                      u_int32_t localization, int *errorp)
891 {
892         hammer_mount_t hmp = trans->hmp;
893         hammer_inode_t ip;
894         hammer_pseudofs_inmem_t pfsm;
895         struct hammer_cursor cursor;
896         int bytes;
897
898 retry:
899         pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
900         if (pfsm) {
901                 hammer_ref(&pfsm->lock);
902                 *errorp = 0;
903                 return(pfsm);
904         }
905
906         /*
907          * PFS records are stored in the root inode (not the PFS root inode,
908          * but the real root).  Avoid an infinite recursion if loading
909          * the PFS for the real root.
910          */
911         if (localization) {
912                 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
913                                       HAMMER_MAX_TID,
914                                       HAMMER_DEF_LOCALIZATION, 0, errorp);
915         } else {
916                 ip = NULL;
917         }
918
919         pfsm = kmalloc(sizeof(*pfsm), hmp->m_misc, M_WAITOK | M_ZERO);
920         pfsm->localization = localization;
921         pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
922         pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
923
924         hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
925         cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
926                                       HAMMER_LOCALIZE_MISC;
927         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
928         cursor.key_beg.create_tid = 0;
929         cursor.key_beg.delete_tid = 0;
930         cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
931         cursor.key_beg.obj_type = 0;
932         cursor.key_beg.key = localization;
933         cursor.asof = HAMMER_MAX_TID;
934         cursor.flags |= HAMMER_CURSOR_ASOF;
935
936         if (ip)
937                 *errorp = hammer_ip_lookup(&cursor);
938         else
939                 *errorp = hammer_btree_lookup(&cursor);
940         if (*errorp == 0) {
941                 *errorp = hammer_ip_resolve_data(&cursor);
942                 if (*errorp == 0) {
943                         if (cursor.data->pfsd.mirror_flags &
944                             HAMMER_PFSD_DELETED) {
945                                 *errorp = ENOENT;
946                         } else {
947                                 bytes = cursor.leaf->data_len;
948                                 if (bytes > sizeof(pfsm->pfsd))
949                                         bytes = sizeof(pfsm->pfsd);
950                                 bcopy(cursor.data, &pfsm->pfsd, bytes);
951                         }
952                 }
953         }
954         hammer_done_cursor(&cursor);
955
956         pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
957         hammer_ref(&pfsm->lock);
958         if (ip)
959                 hammer_rel_inode(ip, 0);
960         if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
961                 kfree(pfsm, hmp->m_misc);
962                 goto retry;
963         }
964         return(pfsm);
965 }
966
967 /*
968  * Store pseudo-fs data.  The backend will automatically delete any prior
969  * on-disk pseudo-fs data but we have to delete in-memory versions.
970  */
971 int
972 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
973 {
974         struct hammer_cursor cursor;
975         hammer_record_t record;
976         hammer_inode_t ip;
977         int error;
978
979         ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
980                               HAMMER_DEF_LOCALIZATION, 0, &error);
981 retry:
982         pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
983         hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
984         cursor.key_beg.localization = ip->obj_localization +
985                                       HAMMER_LOCALIZE_MISC;
986         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
987         cursor.key_beg.create_tid = 0;
988         cursor.key_beg.delete_tid = 0;
989         cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
990         cursor.key_beg.obj_type = 0;
991         cursor.key_beg.key = pfsm->localization;
992         cursor.asof = HAMMER_MAX_TID;
993         cursor.flags |= HAMMER_CURSOR_ASOF;
994
995         /*
996          * Replace any in-memory version of the record.
997          */
998         error = hammer_ip_lookup(&cursor);
999         if (error == 0 && hammer_cursor_inmem(&cursor)) {
1000                 record = cursor.iprec;
1001                 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
1002                         KKASSERT(cursor.deadlk_rec == NULL);
1003                         hammer_ref(&record->lock);
1004                         cursor.deadlk_rec = record;
1005                         error = EDEADLK;
1006                 } else {
1007                         record->flags |= HAMMER_RECF_DELETED_FE;
1008                         error = 0;
1009                 }
1010         }
1011
1012         /*
1013          * Allocate replacement general record.  The backend flush will
1014          * delete any on-disk version of the record.
1015          */
1016         if (error == 0 || error == ENOENT) {
1017                 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
1018                 record->type = HAMMER_MEM_RECORD_GENERAL;
1019
1020                 record->leaf.base.localization = ip->obj_localization +
1021                                                  HAMMER_LOCALIZE_MISC;
1022                 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
1023                 record->leaf.base.key = pfsm->localization;
1024                 record->leaf.data_len = sizeof(pfsm->pfsd);
1025                 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
1026                 error = hammer_ip_add_record(trans, record);
1027         }
1028         hammer_done_cursor(&cursor);
1029         if (error == EDEADLK)
1030                 goto retry;
1031         hammer_rel_inode(ip, 0);
1032         return(error);
1033 }
1034
1035 /*
1036  * Create a root directory for a PFS if one does not alredy exist.
1037  *
1038  * The PFS root stands alone so we must also bump the nlinks count
1039  * to prevent it from being destroyed on release.
1040  */
1041 int
1042 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1043                        hammer_pseudofs_inmem_t pfsm)
1044 {
1045         hammer_inode_t ip;
1046         struct vattr vap;
1047         int error;
1048
1049         ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1050                               pfsm->localization, 0, &error);
1051         if (ip == NULL) {
1052                 vattr_null(&vap);
1053                 vap.va_mode = 0755;
1054                 vap.va_type = VDIR;
1055                 error = hammer_create_inode(trans, &vap, cred,
1056                                             NULL, NULL, 0,
1057                                             pfsm, &ip);
1058                 if (error == 0) {
1059                         ++ip->ino_data.nlinks;
1060                         hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1061                 }
1062         }
1063         if (ip)
1064                 hammer_rel_inode(ip, 0);
1065         return(error);
1066 }
1067
1068 /*
1069  * Unload any vnodes & inodes associated with a PFS, return ENOTEMPTY
1070  * if we are unable to disassociate all the inodes.
1071  */
1072 static
1073 int
1074 hammer_unload_pseudofs_callback(hammer_inode_t ip, void *data)
1075 {
1076         int res;
1077
1078         hammer_ref(&ip->lock);
1079         if (ip->lock.refs == 2 && ip->vp)
1080                 vclean_unlocked(ip->vp);
1081         if (ip->lock.refs == 1 && ip->vp == NULL)
1082                 res = 0;
1083         else
1084                 res = -1;       /* stop, someone is using the inode */
1085         hammer_rel_inode(ip, 0);
1086         return(res);
1087 }
1088
1089 int
1090 hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization)
1091 {
1092         int res;
1093         int try;
1094
1095         for (try = res = 0; try < 4; ++try) {
1096                 res = hammer_ino_rb_tree_RB_SCAN(&trans->hmp->rb_inos_root,
1097                                            hammer_inode_pfs_cmp,
1098                                            hammer_unload_pseudofs_callback,
1099                                            &localization);
1100                 if (res == 0 && try > 1)
1101                         break;
1102                 hammer_flusher_sync(trans->hmp);
1103         }
1104         if (res != 0)
1105                 res = ENOTEMPTY;
1106         return(res);
1107 }
1108
1109
1110 /*
1111  * Release a reference on a PFS
1112  */
1113 void
1114 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
1115 {
1116         hammer_unref(&pfsm->lock);
1117         if (pfsm->lock.refs == 0) {
1118                 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
1119                 kfree(pfsm, hmp->m_misc);
1120         }
1121 }
1122
1123 /*
1124  * Called by hammer_sync_inode().
1125  */
1126 static int
1127 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
1128 {
1129         hammer_transaction_t trans = cursor->trans;
1130         hammer_record_t record;
1131         int error;
1132         int redirty;
1133
1134 retry:
1135         error = 0;
1136
1137         /*
1138          * If the inode has a presence on-disk then locate it and mark
1139          * it deleted, setting DELONDISK.
1140          *
1141          * The record may or may not be physically deleted, depending on
1142          * the retention policy.
1143          */
1144         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
1145             HAMMER_INODE_ONDISK) {
1146                 hammer_normalize_cursor(cursor);
1147                 cursor->key_beg.localization = ip->obj_localization + 
1148                                                HAMMER_LOCALIZE_INODE;
1149                 cursor->key_beg.obj_id = ip->obj_id;
1150                 cursor->key_beg.key = 0;
1151                 cursor->key_beg.create_tid = 0;
1152                 cursor->key_beg.delete_tid = 0;
1153                 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1154                 cursor->key_beg.obj_type = 0;
1155                 cursor->asof = ip->obj_asof;
1156                 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1157                 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
1158                 cursor->flags |= HAMMER_CURSOR_BACKEND;
1159
1160                 error = hammer_btree_lookup(cursor);
1161                 if (hammer_debug_inode)
1162                         kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
1163
1164                 if (error == 0) {
1165                         error = hammer_ip_delete_record(cursor, ip, trans->tid);
1166                         if (hammer_debug_inode)
1167                                 kprintf(" error %d\n", error);
1168                         if (error == 0) {
1169                                 ip->flags |= HAMMER_INODE_DELONDISK;
1170                         }
1171                         if (cursor->node)
1172                                 hammer_cache_node(&ip->cache[0], cursor->node);
1173                 }
1174                 if (error == EDEADLK) {
1175                         hammer_done_cursor(cursor);
1176                         error = hammer_init_cursor(trans, cursor,
1177                                                    &ip->cache[0], ip);
1178                         if (hammer_debug_inode)
1179                                 kprintf("IPDED %p %d\n", ip, error);
1180                         if (error == 0)
1181                                 goto retry;
1182                 }
1183         }
1184
1185         /*
1186          * Ok, write out the initial record or a new record (after deleting
1187          * the old one), unless the DELETED flag is set.  This routine will
1188          * clear DELONDISK if it writes out a record.
1189          *
1190          * Update our inode statistics if this is the first application of
1191          * the inode on-disk.
1192          */
1193         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
1194                 /*
1195                  * Generate a record and write it to the media.  We clean-up
1196                  * the state before releasing so we do not have to set-up
1197                  * a flush_group.
1198                  */
1199                 record = hammer_alloc_mem_record(ip, 0);
1200                 record->type = HAMMER_MEM_RECORD_INODE;
1201                 record->flush_state = HAMMER_FST_FLUSH;
1202                 record->leaf = ip->sync_ino_leaf;
1203                 record->leaf.base.create_tid = trans->tid;
1204                 record->leaf.data_len = sizeof(ip->sync_ino_data);
1205                 record->leaf.create_ts = trans->time32;
1206                 record->data = (void *)&ip->sync_ino_data;
1207                 record->flags |= HAMMER_RECF_INTERLOCK_BE;
1208
1209                 /*
1210                  * If this flag is set we cannot sync the new file size
1211                  * because we haven't finished related truncations.  The
1212                  * inode will be flushed in another flush group to finish
1213                  * the job.
1214                  */
1215                 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
1216                     ip->sync_ino_data.size != ip->ino_data.size) {
1217                         redirty = 1;
1218                         ip->sync_ino_data.size = ip->ino_data.size;
1219                 } else {
1220                         redirty = 0;
1221                 }
1222
1223                 for (;;) {
1224                         error = hammer_ip_sync_record_cursor(cursor, record);
1225                         if (hammer_debug_inode)
1226                                 kprintf("GENREC %p rec %08x %d\n",      
1227                                         ip, record->flags, error);
1228                         if (error != EDEADLK)
1229                                 break;
1230                         hammer_done_cursor(cursor);
1231                         error = hammer_init_cursor(trans, cursor,
1232                                                    &ip->cache[0], ip);
1233                         if (hammer_debug_inode)
1234                                 kprintf("GENREC reinit %d\n", error);
1235                         if (error)
1236                                 break;
1237                 }
1238
1239                 /*
1240                  * Note:  The record was never on the inode's record tree
1241                  * so just wave our hands importantly and destroy it.
1242                  */
1243                 record->flags |= HAMMER_RECF_COMMITTED;
1244                 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
1245                 record->flush_state = HAMMER_FST_IDLE;
1246                 ++ip->rec_generation;
1247                 hammer_rel_mem_record(record);
1248
1249                 /*
1250                  * Finish up.
1251                  */
1252                 if (error == 0) {
1253                         if (hammer_debug_inode)
1254                                 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
1255                         ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1256                                             HAMMER_INODE_SDIRTY |
1257                                             HAMMER_INODE_ATIME |
1258                                             HAMMER_INODE_MTIME);
1259                         ip->flags &= ~HAMMER_INODE_DELONDISK;
1260                         if (redirty)
1261                                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
1262
1263                         /*
1264                          * Root volume count of inodes
1265                          */
1266                         hammer_sync_lock_sh(trans);
1267                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
1268                                 hammer_modify_volume_field(trans,
1269                                                            trans->rootvol,
1270                                                            vol0_stat_inodes);
1271                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
1272                                 hammer_modify_volume_done(trans->rootvol);
1273                                 ip->flags |= HAMMER_INODE_ONDISK;
1274                                 if (hammer_debug_inode)
1275                                         kprintf("NOWONDISK %p\n", ip);
1276                         }
1277                         hammer_sync_unlock(trans);
1278                 }
1279         }
1280
1281         /*
1282          * If the inode has been destroyed, clean out any left-over flags
1283          * that may have been set by the frontend.
1284          */
1285         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) { 
1286                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1287                                     HAMMER_INODE_SDIRTY |
1288                                     HAMMER_INODE_ATIME |
1289                                     HAMMER_INODE_MTIME);
1290         }
1291         return(error);
1292 }
1293
1294 /*
1295  * Update only the itimes fields.
1296  *
1297  * ATIME can be updated without generating any UNDO.  MTIME is updated
1298  * with UNDO so it is guaranteed to be synchronized properly in case of
1299  * a crash.
1300  *
1301  * Neither field is included in the B-Tree leaf element's CRC, which is how
1302  * we can get away with updating ATIME the way we do.
1303  */
1304 static int
1305 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1306 {
1307         hammer_transaction_t trans = cursor->trans;
1308         int error;
1309
1310 retry:
1311         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1312             HAMMER_INODE_ONDISK) {
1313                 return(0);
1314         }
1315
1316         hammer_normalize_cursor(cursor);
1317         cursor->key_beg.localization = ip->obj_localization + 
1318                                        HAMMER_LOCALIZE_INODE;
1319         cursor->key_beg.obj_id = ip->obj_id;
1320         cursor->key_beg.key = 0;
1321         cursor->key_beg.create_tid = 0;
1322         cursor->key_beg.delete_tid = 0;
1323         cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1324         cursor->key_beg.obj_type = 0;
1325         cursor->asof = ip->obj_asof;
1326         cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1327         cursor->flags |= HAMMER_CURSOR_ASOF;
1328         cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1329         cursor->flags |= HAMMER_CURSOR_GET_DATA;
1330         cursor->flags |= HAMMER_CURSOR_BACKEND;
1331
1332         error = hammer_btree_lookup(cursor);
1333         if (error == 0) {
1334                 hammer_cache_node(&ip->cache[0], cursor->node);
1335                 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1336                         /*
1337                          * Updating MTIME requires an UNDO.  Just cover
1338                          * both atime and mtime.
1339                          */
1340                         hammer_sync_lock_sh(trans);
1341                         hammer_modify_buffer(trans, cursor->data_buffer,
1342                                      HAMMER_ITIMES_BASE(&cursor->data->inode),
1343                                      HAMMER_ITIMES_BYTES);
1344                         cursor->data->inode.atime = ip->sync_ino_data.atime;
1345                         cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1346                         hammer_modify_buffer_done(cursor->data_buffer);
1347                         hammer_sync_unlock(trans);
1348                 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1349                         /*
1350                          * Updating atime only can be done in-place with
1351                          * no UNDO.
1352                          */
1353                         hammer_sync_lock_sh(trans);
1354                         hammer_modify_buffer(trans, cursor->data_buffer,
1355                                              NULL, 0);
1356                         cursor->data->inode.atime = ip->sync_ino_data.atime;
1357                         hammer_modify_buffer_done(cursor->data_buffer);
1358                         hammer_sync_unlock(trans);
1359                 }
1360                 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1361         }
1362         if (error == EDEADLK) {
1363                 hammer_done_cursor(cursor);
1364                 error = hammer_init_cursor(trans, cursor,
1365                                            &ip->cache[0], ip);
1366                 if (error == 0)
1367                         goto retry;
1368         }
1369         return(error);
1370 }
1371
1372 /*
1373  * Release a reference on an inode, flush as requested.
1374  *
1375  * On the last reference we queue the inode to the flusher for its final
1376  * disposition.
1377  */
1378 void
1379 hammer_rel_inode(struct hammer_inode *ip, int flush)
1380 {
1381         /*hammer_mount_t hmp = ip->hmp;*/
1382
1383         /*
1384          * Handle disposition when dropping the last ref.
1385          */
1386         for (;;) {
1387                 if (ip->lock.refs == 1) {
1388                         /*
1389                          * Determine whether on-disk action is needed for
1390                          * the inode's final disposition.
1391                          */
1392                         KKASSERT(ip->vp == NULL);
1393                         hammer_inode_unloadable_check(ip, 0);
1394                         if (ip->flags & HAMMER_INODE_MODMASK) {
1395                                 hammer_flush_inode(ip, 0);
1396                         } else if (ip->lock.refs == 1) {
1397                                 hammer_unload_inode(ip);
1398                                 break;
1399                         }
1400                 } else {
1401                         if (flush)
1402                                 hammer_flush_inode(ip, 0);
1403
1404                         /*
1405                          * The inode still has multiple refs, try to drop
1406                          * one ref.
1407                          */
1408                         KKASSERT(ip->lock.refs >= 1);
1409                         if (ip->lock.refs > 1) {
1410                                 hammer_unref(&ip->lock);
1411                                 break;
1412                         }
1413                 }
1414         }
1415 }
1416
1417 /*
1418  * Unload and destroy the specified inode.  Must be called with one remaining
1419  * reference.  The reference is disposed of.
1420  *
1421  * The inode must be completely clean.
1422  */
1423 static int
1424 hammer_unload_inode(struct hammer_inode *ip)
1425 {
1426         hammer_mount_t hmp = ip->hmp;
1427
1428         KASSERT(ip->lock.refs == 1,
1429                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1430         KKASSERT(ip->vp == NULL);
1431         KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1432         KKASSERT(ip->cursor_ip_refs == 0);
1433         KKASSERT(hammer_notlocked(&ip->lock));
1434         KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1435
1436         KKASSERT(RB_EMPTY(&ip->rec_tree));
1437         KKASSERT(TAILQ_EMPTY(&ip->target_list));
1438
1439         RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1440
1441         hammer_free_inode(ip);
1442         return(0);
1443 }
1444
1445 /*
1446  * Called during unmounting if a critical error occured.  The in-memory
1447  * inode and all related structures are destroyed.
1448  *
1449  * If a critical error did not occur the unmount code calls the standard
1450  * release and asserts that the inode is gone.
1451  */
1452 int
1453 hammer_destroy_inode_callback(struct hammer_inode *ip, void *data __unused)
1454 {
1455         hammer_record_t rec;
1456
1457         /*
1458          * Get rid of the inodes in-memory records, regardless of their
1459          * state, and clear the mod-mask.
1460          */
1461         while ((rec = TAILQ_FIRST(&ip->target_list)) != NULL) {
1462                 TAILQ_REMOVE(&ip->target_list, rec, target_entry);
1463                 rec->target_ip = NULL;
1464                 if (rec->flush_state == HAMMER_FST_SETUP)
1465                         rec->flush_state = HAMMER_FST_IDLE;
1466         }
1467         while ((rec = RB_ROOT(&ip->rec_tree)) != NULL) {
1468                 if (rec->flush_state == HAMMER_FST_FLUSH)
1469                         --rec->flush_group->refs;
1470                 else
1471                         hammer_ref(&rec->lock);
1472                 KKASSERT(rec->lock.refs == 1);
1473                 rec->flush_state = HAMMER_FST_IDLE;
1474                 rec->flush_group = NULL;
1475                 rec->flags |= HAMMER_RECF_DELETED_FE; /* wave hands */
1476                 rec->flags |= HAMMER_RECF_DELETED_BE; /* wave hands */
1477                 ++ip->rec_generation;
1478                 hammer_rel_mem_record(rec);
1479         }
1480         ip->flags &= ~HAMMER_INODE_MODMASK;
1481         ip->sync_flags &= ~HAMMER_INODE_MODMASK;
1482         KKASSERT(ip->vp == NULL);
1483
1484         /*
1485          * Remove the inode from any flush group, force it idle.  FLUSH
1486          * and SETUP states have an inode ref.
1487          */
1488         switch(ip->flush_state) {
1489         case HAMMER_FST_FLUSH:
1490                 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
1491                 --ip->flush_group->refs;
1492                 ip->flush_group = NULL;
1493                 /* fall through */
1494         case HAMMER_FST_SETUP:
1495                 hammer_unref(&ip->lock);
1496                 ip->flush_state = HAMMER_FST_IDLE;
1497                 /* fall through */
1498         case HAMMER_FST_IDLE:
1499                 break;
1500         }
1501
1502         /*
1503          * There shouldn't be any associated vnode.  The unload needs at
1504          * least one ref, if we do have a vp steal its ip ref.
1505          */
1506         if (ip->vp) {
1507                 kprintf("hammer_destroy_inode_callback: Unexpected "
1508                         "vnode association ip %p vp %p\n", ip, ip->vp);
1509                 ip->vp->v_data = NULL;
1510                 ip->vp = NULL;
1511         } else {
1512                 hammer_ref(&ip->lock);
1513         }
1514         hammer_unload_inode(ip);
1515         return(0);
1516 }
1517
1518 /*
1519  * Called on mount -u when switching from RW to RO or vise-versa.  Adjust
1520  * the read-only flag for cached inodes.
1521  *
1522  * This routine is called from a RB_SCAN().
1523  */
1524 int
1525 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1526 {
1527         hammer_mount_t hmp = ip->hmp;
1528
1529         if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1530                 ip->flags |= HAMMER_INODE_RO;
1531         else
1532                 ip->flags &= ~HAMMER_INODE_RO;
1533         return(0);
1534 }
1535
1536 /*
1537  * A transaction has modified an inode, requiring updates as specified by
1538  * the passed flags.
1539  *
1540  * HAMMER_INODE_DDIRTY: Inode data has been updated, not incl mtime/atime,
1541  *                      and not including size changes due to write-append
1542  *                      (but other size changes are included).
1543  * HAMMER_INODE_SDIRTY: Inode data has been updated, size changes due to
1544  *                      write-append.
1545  * HAMMER_INODE_XDIRTY: Dirty in-memory records
1546  * HAMMER_INODE_BUFS:   Dirty buffer cache buffers
1547  * HAMMER_INODE_DELETED: Inode record/data must be deleted
1548  * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1549  */
1550 void
1551 hammer_modify_inode(hammer_inode_t ip, int flags)
1552 {
1553         /* 
1554          * ronly of 0 or 2 does not trigger assertion.
1555          * 2 is a special error state 
1556          */
1557         KKASSERT(ip->hmp->ronly != 1 ||
1558                   (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY | 
1559                             HAMMER_INODE_SDIRTY |
1560                             HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1561                             HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1562         if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1563                 ip->flags |= HAMMER_INODE_RSV_INODES;
1564                 ++ip->hmp->rsv_inodes;
1565         }
1566
1567         ip->flags |= flags;
1568 }
1569
1570 /*
1571  * Request that an inode be flushed.  This whole mess cannot block and may
1572  * recurse (if not synchronous).  Once requested HAMMER will attempt to
1573  * actively flush the inode until the flush can be done.
1574  *
1575  * The inode may already be flushing, or may be in a setup state.  We can
1576  * place the inode in a flushing state if it is currently idle and flag it
1577  * to reflush if it is currently flushing.
1578  *
1579  * Upon return if the inode could not be flushed due to a setup
1580  * dependancy, then it will be automatically flushed when the dependancy
1581  * is satisfied.
1582  */
1583 void
1584 hammer_flush_inode(hammer_inode_t ip, int flags)
1585 {
1586         hammer_mount_t hmp;
1587         hammer_flush_group_t flg;
1588         int good;
1589
1590         /*
1591          * next_flush_group is the first flush group we can place the inode
1592          * in.  It may be NULL.  If it becomes full we append a new flush
1593          * group and make that the next_flush_group.
1594          */
1595         hmp = ip->hmp;
1596         while ((flg = hmp->next_flush_group) != NULL) {
1597                 KKASSERT(flg->running == 0);
1598                 if (flg->total_count + flg->refs <= ip->hmp->undo_rec_limit)
1599                         break;
1600                 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
1601                 hammer_flusher_async(ip->hmp, flg);
1602         }
1603         if (flg == NULL) {
1604                 flg = kmalloc(sizeof(*flg), hmp->m_misc, M_WAITOK|M_ZERO);
1605                 hmp->next_flush_group = flg;
1606                 RB_INIT(&flg->flush_tree);
1607                 TAILQ_INSERT_TAIL(&hmp->flush_group_list, flg, flush_entry);
1608         }
1609
1610         /*
1611          * Trivial 'nothing to flush' case.  If the inode is in a SETUP
1612          * state we have to put it back into an IDLE state so we can
1613          * drop the extra ref.
1614          *
1615          * If we have a parent dependancy we must still fall through
1616          * so we can run it.
1617          */
1618         if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1619                 if (ip->flush_state == HAMMER_FST_SETUP &&
1620                     TAILQ_EMPTY(&ip->target_list)) {
1621                         ip->flush_state = HAMMER_FST_IDLE;
1622                         hammer_rel_inode(ip, 0);
1623                 }
1624                 if (ip->flush_state == HAMMER_FST_IDLE)
1625                         return;
1626         }
1627
1628         /*
1629          * Our flush action will depend on the current state.
1630          */
1631         switch(ip->flush_state) {
1632         case HAMMER_FST_IDLE:
1633                 /*
1634                  * We have no dependancies and can flush immediately.  Some
1635                  * our children may not be flushable so we have to re-test
1636                  * with that additional knowledge.
1637                  */
1638                 hammer_flush_inode_core(ip, flg, flags);
1639                 break;
1640         case HAMMER_FST_SETUP:
1641                 /*
1642                  * Recurse upwards through dependancies via target_list
1643                  * and start their flusher actions going if possible.
1644                  *
1645                  * 'good' is our connectivity.  -1 means we have none and
1646                  * can't flush, 0 means there weren't any dependancies, and
1647                  * 1 means we have good connectivity.
1648                  */
1649                 good = hammer_setup_parent_inodes(ip, 0, flg);
1650
1651                 if (good >= 0) {
1652                         /*
1653                          * We can continue if good >= 0.  Determine how 
1654                          * many records under our inode can be flushed (and
1655                          * mark them).
1656                          */
1657                         hammer_flush_inode_core(ip, flg, flags);
1658                 } else {
1659                         /*
1660                          * Parent has no connectivity, tell it to flush
1661                          * us as soon as it does.
1662                          *
1663                          * The REFLUSH flag is also needed to trigger
1664                          * dependancy wakeups.
1665                          */
1666                         ip->flags |= HAMMER_INODE_CONN_DOWN |
1667                                      HAMMER_INODE_REFLUSH;
1668                         if (flags & HAMMER_FLUSH_SIGNAL) {
1669                                 ip->flags |= HAMMER_INODE_RESIGNAL;
1670                                 hammer_flusher_async(ip->hmp, flg);
1671                         }
1672                 }
1673                 break;
1674         case HAMMER_FST_FLUSH:
1675                 /*
1676                  * We are already flushing, flag the inode to reflush
1677                  * if needed after it completes its current flush.
1678                  *
1679                  * The REFLUSH flag is also needed to trigger
1680                  * dependancy wakeups.
1681                  */
1682                 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1683                         ip->flags |= HAMMER_INODE_REFLUSH;
1684                 if (flags & HAMMER_FLUSH_SIGNAL) {
1685                         ip->flags |= HAMMER_INODE_RESIGNAL;
1686                         hammer_flusher_async(ip->hmp, flg);
1687                 }
1688                 break;
1689         }
1690 }
1691
1692 /*
1693  * Scan ip->target_list, which is a list of records owned by PARENTS to our
1694  * ip which reference our ip.
1695  *
1696  * XXX This is a huge mess of recursive code, but not one bit of it blocks
1697  *     so for now do not ref/deref the structures.  Note that if we use the
1698  *     ref/rel code later, the rel CAN block.
1699  */
1700 static int
1701 hammer_setup_parent_inodes(hammer_inode_t ip, int depth,
1702                            hammer_flush_group_t flg)
1703 {
1704         hammer_record_t depend;
1705         int good;
1706         int r;
1707
1708         /*
1709          * If we hit our recursion limit and we have parent dependencies
1710          * We cannot continue.  Returning < 0 will cause us to be flagged
1711          * for reflush.  Returning -2 cuts off additional dependency checks
1712          * because they are likely to also hit the depth limit.
1713          *
1714          * We cannot return < 0 if there are no dependencies or there might
1715          * not be anything to wakeup (ip).
1716          */
1717         if (depth == 20 && TAILQ_FIRST(&ip->target_list)) {
1718                 kprintf("HAMMER Warning: depth limit reached on "
1719                         "setup recursion, inode %p %016llx\n",
1720                         ip, (long long)ip->obj_id);
1721                 return(-2);
1722         }
1723
1724         /*
1725          * Scan dependencies
1726          */
1727         good = 0;
1728         TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1729                 r = hammer_setup_parent_inodes_helper(depend, depth, flg);
1730                 KKASSERT(depend->target_ip == ip);
1731                 if (r < 0 && good == 0)
1732                         good = -1;
1733                 if (r > 0)
1734                         good = 1;
1735
1736                 /*
1737                  * If we failed due to the recursion depth limit then stop
1738                  * now.
1739                  */
1740                 if (r == -2)
1741                         break;
1742         }
1743         return(good);
1744 }
1745
1746 /*
1747  * This helper function takes a record representing the dependancy between
1748  * the parent inode and child inode.
1749  *
1750  * record->ip           = parent inode
1751  * record->target_ip    = child inode
1752  * 
1753  * We are asked to recurse upwards and convert the record from SETUP
1754  * to FLUSH if possible.
1755  *
1756  * Return 1 if the record gives us connectivity
1757  *
1758  * Return 0 if the record is not relevant 
1759  *
1760  * Return -1 if we can't resolve the dependancy and there is no connectivity.
1761  */
1762 static int
1763 hammer_setup_parent_inodes_helper(hammer_record_t record, int depth,
1764                                   hammer_flush_group_t flg)
1765 {
1766         hammer_mount_t hmp;
1767         hammer_inode_t pip;
1768         int good;
1769
1770         KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1771         pip = record->ip;
1772         hmp = pip->hmp;
1773
1774         /*
1775          * If the record is already flushing, is it in our flush group?
1776          *
1777          * If it is in our flush group but it is a general record or a 
1778          * delete-on-disk, it does not improve our connectivity (return 0),
1779          * and if the target inode is not trying to destroy itself we can't
1780          * allow the operation yet anyway (the second return -1).
1781          */
1782         if (record->flush_state == HAMMER_FST_FLUSH) {
1783                 /*
1784                  * If not in our flush group ask the parent to reflush
1785                  * us as soon as possible.
1786                  */
1787                 if (record->flush_group != flg) {
1788                         pip->flags |= HAMMER_INODE_REFLUSH;
1789                         record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1790                         return(-1);
1791                 }
1792
1793                 /*
1794                  * If in our flush group everything is already set up,
1795                  * just return whether the record will improve our
1796                  * visibility or not.
1797                  */
1798                 if (record->type == HAMMER_MEM_RECORD_ADD)
1799                         return(1);
1800                 return(0);
1801         }
1802
1803         /*
1804          * It must be a setup record.  Try to resolve the setup dependancies
1805          * by recursing upwards so we can place ip on the flush list.
1806          *
1807          * Limit ourselves to 20 levels of recursion to avoid blowing out
1808          * the kernel stack.  If we hit the recursion limit we can't flush
1809          * until the parent flushes.  The parent will flush independantly
1810          * on its own and ultimately a deep recursion will be resolved.
1811          */
1812         KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1813
1814         good = hammer_setup_parent_inodes(pip, depth + 1, flg);
1815
1816         /*
1817          * If good < 0 the parent has no connectivity and we cannot safely
1818          * flush the directory entry, which also means we can't flush our
1819          * ip.  Flag us for downward recursion once the parent's
1820          * connectivity is resolved.  Flag the parent for [re]flush or it
1821          * may not check for downward recursions.
1822          */
1823         if (good < 0) {
1824                 pip->flags |= HAMMER_INODE_REFLUSH;
1825                 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1826                 return(good);
1827         }
1828
1829         /*
1830          * We are go, place the parent inode in a flushing state so we can
1831          * place its record in a flushing state.  Note that the parent
1832          * may already be flushing.  The record must be in the same flush
1833          * group as the parent.
1834          */
1835         if (pip->flush_state != HAMMER_FST_FLUSH)
1836                 hammer_flush_inode_core(pip, flg, HAMMER_FLUSH_RECURSION);
1837         KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1838         KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1839
1840 #if 0
1841         if (record->type == HAMMER_MEM_RECORD_DEL &&
1842             (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1843                 /*
1844                  * Regardless of flushing state we cannot sync this path if the
1845                  * record represents a delete-on-disk but the target inode
1846                  * is not ready to sync its own deletion.
1847                  *
1848                  * XXX need to count effective nlinks to determine whether
1849                  * the flush is ok, otherwise removing a hardlink will
1850                  * just leave the DEL record to rot.
1851                  */
1852                 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1853                 return(-1);
1854         } else
1855 #endif
1856         if (pip->flush_group == flg) {
1857                 /*
1858                  * Because we have not calculated nlinks yet we can just
1859                  * set records to the flush state if the parent is in
1860                  * the same flush group as we are.
1861                  */
1862                 record->flush_state = HAMMER_FST_FLUSH;
1863                 record->flush_group = flg;
1864                 ++record->flush_group->refs;
1865                 hammer_ref(&record->lock);
1866
1867                 /*
1868                  * A general directory-add contributes to our visibility.
1869                  *
1870                  * Otherwise it is probably a directory-delete or 
1871                  * delete-on-disk record and does not contribute to our
1872                  * visbility (but we can still flush it).
1873                  */
1874                 if (record->type == HAMMER_MEM_RECORD_ADD)
1875                         return(1);
1876                 return(0);
1877         } else {
1878                 /*
1879                  * If the parent is not in our flush group we cannot
1880                  * flush this record yet, there is no visibility.
1881                  * We tell the parent to reflush and mark ourselves
1882                  * so the parent knows it should flush us too.
1883                  */
1884                 pip->flags |= HAMMER_INODE_REFLUSH;
1885                 record->target_ip->flags |= HAMMER_INODE_CONN_DOWN;
1886                 return(-1);
1887         }
1888 }
1889
1890 /*
1891  * This is the core routine placing an inode into the FST_FLUSH state.
1892  */
1893 static void
1894 hammer_flush_inode_core(hammer_inode_t ip, hammer_flush_group_t flg, int flags)
1895 {
1896         int go_count;
1897
1898         /*
1899          * Set flush state and prevent the flusher from cycling into
1900          * the next flush group.  Do not place the ip on the list yet.
1901          * Inodes not in the idle state get an extra reference.
1902          */
1903         KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1904         if (ip->flush_state == HAMMER_FST_IDLE)
1905                 hammer_ref(&ip->lock);
1906         ip->flush_state = HAMMER_FST_FLUSH;
1907         ip->flush_group = flg;
1908         ++ip->hmp->flusher.group_lock;
1909         ++ip->hmp->count_iqueued;
1910         ++hammer_count_iqueued;
1911         ++flg->total_count;
1912
1913         /*
1914          * If the flush group reaches the autoflush limit we want to signal
1915          * the flusher.  This is particularly important for remove()s.
1916          */
1917         if (flg->total_count == hammer_autoflush)
1918                 flags |= HAMMER_FLUSH_SIGNAL;
1919
1920         /*
1921          * We need to be able to vfsync/truncate from the backend.
1922          */
1923         KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1924         if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1925                 ip->flags |= HAMMER_INODE_VHELD;
1926                 vref(ip->vp);
1927         }
1928
1929         /*
1930          * Figure out how many in-memory records we can actually flush
1931          * (not including inode meta-data, buffers, etc).
1932          */
1933         KKASSERT((ip->flags & HAMMER_INODE_WOULDBLOCK) == 0);
1934         if (flags & HAMMER_FLUSH_RECURSION) {
1935                 /*
1936                  * If this is a upwards recursion we do not want to
1937                  * recurse down again!
1938                  */
1939                 go_count = 1;
1940 #if 0
1941         } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1942                 /*
1943                  * No new records are added if we must complete a flush
1944                  * from a previous cycle, but we do have to move the records
1945                  * from the previous cycle to the current one.
1946                  */
1947 #if 0
1948                 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1949                                    hammer_syncgrp_child_callback, NULL);
1950 #endif
1951                 go_count = 1;
1952 #endif
1953         } else {
1954                 /*
1955                  * Normal flush, scan records and bring them into the flush.
1956                  * Directory adds and deletes are usually skipped (they are
1957                  * grouped with the related inode rather then with the
1958                  * directory).
1959                  *
1960                  * go_count can be negative, which means the scan aborted
1961                  * due to the flush group being over-full and we should
1962                  * flush what we have.
1963                  */
1964                 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1965                                    hammer_setup_child_callback, NULL);
1966         }
1967
1968         /*
1969          * This is a more involved test that includes go_count.  If we
1970          * can't flush, flag the inode and return.  If go_count is 0 we
1971          * were are unable to flush any records in our rec_tree and
1972          * must ignore the XDIRTY flag.
1973          */
1974         if (go_count == 0) {
1975                 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1976                         --ip->hmp->count_iqueued;
1977                         --hammer_count_iqueued;
1978
1979                         --flg->total_count;
1980                         ip->flush_state = HAMMER_FST_SETUP;
1981                         ip->flush_group = NULL;
1982                         if (ip->flags & HAMMER_INODE_VHELD) {
1983                                 ip->flags &= ~HAMMER_INODE_VHELD;
1984                                 vrele(ip->vp);
1985                         }
1986
1987                         /*
1988                          * REFLUSH is needed to trigger dependancy wakeups
1989                          * when an inode is in SETUP.
1990                          */
1991                         ip->flags |= HAMMER_INODE_REFLUSH;
1992                         if (flags & HAMMER_FLUSH_SIGNAL) {
1993                                 ip->flags |= HAMMER_INODE_RESIGNAL;
1994                                 hammer_flusher_async(ip->hmp, flg);
1995                         }
1996                         if (--ip->hmp->flusher.group_lock == 0)
1997                                 wakeup(&ip->hmp->flusher.group_lock);
1998                         return;
1999                 }
2000         }
2001
2002         /*
2003          * Snapshot the state of the inode for the backend flusher.
2004          *
2005          * We continue to retain save_trunc_off even when all truncations
2006          * have been resolved as an optimization to determine if we can
2007          * skip the B-Tree lookup for overwrite deletions.
2008          *
2009          * NOTE: The DELETING flag is a mod flag, but it is also sticky,
2010          * and stays in ip->flags.  Once set, it stays set until the
2011          * inode is destroyed.
2012          */
2013         if (ip->flags & HAMMER_INODE_TRUNCATED) {
2014                 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
2015                 ip->sync_trunc_off = ip->trunc_off;
2016                 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
2017                 ip->flags &= ~HAMMER_INODE_TRUNCATED;
2018                 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
2019
2020                 /*
2021                  * The save_trunc_off used to cache whether the B-Tree
2022                  * holds any records past that point is not used until
2023                  * after the truncation has succeeded, so we can safely
2024                  * set it now.
2025                  */
2026                 if (ip->save_trunc_off > ip->sync_trunc_off)
2027                         ip->save_trunc_off = ip->sync_trunc_off;
2028         }
2029         ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
2030                            ~HAMMER_INODE_TRUNCATED);
2031         ip->sync_ino_leaf = ip->ino_leaf;
2032         ip->sync_ino_data = ip->ino_data;
2033         ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
2034 #ifdef DEBUG_TRUNCATE
2035         if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
2036                 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
2037 #endif
2038
2039         /*
2040          * The flusher list inherits our inode and reference.
2041          */
2042         KKASSERT(flg->running == 0);
2043         RB_INSERT(hammer_fls_rb_tree, &flg->flush_tree, ip);
2044         if (--ip->hmp->flusher.group_lock == 0)
2045                 wakeup(&ip->hmp->flusher.group_lock);
2046
2047         if (flags & HAMMER_FLUSH_SIGNAL) {
2048                 hammer_flusher_async(ip->hmp, flg);
2049         }
2050 }
2051
2052 /*
2053  * Callback for scan of ip->rec_tree.  Try to include each record in our
2054  * flush.  ip->flush_group has been set but the inode has not yet been
2055  * moved into a flushing state.
2056  *
2057  * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
2058  * both inodes.
2059  *
2060  * We return 1 for any record placed or found in FST_FLUSH, which prevents
2061  * the caller from shortcutting the flush.
2062  */
2063 static int
2064 hammer_setup_child_callback(hammer_record_t rec, void *data)
2065 {
2066         hammer_flush_group_t flg;
2067         hammer_inode_t target_ip;
2068         hammer_inode_t ip;
2069         int r;
2070
2071         /*
2072          * Records deleted or committed by the backend are ignored.
2073          * Note that the flush detects deleted frontend records at
2074          * multiple points to deal with races.  This is just the first
2075          * line of defense.  The only time HAMMER_RECF_DELETED_FE cannot
2076          * be set is when HAMMER_RECF_INTERLOCK_BE is set, because it
2077          * messes up link-count calculations.
2078          *
2079          * NOTE: Don't get confused between record deletion and, say,
2080          * directory entry deletion.  The deletion of a directory entry
2081          * which is on-media has nothing to do with the record deletion
2082          * flags.
2083          */
2084         if (rec->flags & (HAMMER_RECF_DELETED_FE | HAMMER_RECF_DELETED_BE |
2085                           HAMMER_RECF_COMMITTED)) {
2086                 if (rec->flush_state == HAMMER_FST_FLUSH) {
2087                         KKASSERT(rec->flush_group == rec->ip->flush_group);
2088                         r = 1;
2089                 } else {
2090                         r = 0;
2091                 }
2092                 return(r);
2093         }
2094
2095         /*
2096          * If the record is in an idle state it has no dependancies and
2097          * can be flushed.
2098          */
2099         ip = rec->ip;
2100         flg = ip->flush_group;
2101         r = 0;
2102
2103         switch(rec->flush_state) {
2104         case HAMMER_FST_IDLE:
2105                 /*
2106                  * The record has no setup dependancy, we can flush it.
2107                  */
2108                 KKASSERT(rec->target_ip == NULL);
2109                 rec->flush_state = HAMMER_FST_FLUSH;
2110                 rec->flush_group = flg;
2111                 ++flg->refs;
2112                 hammer_ref(&rec->lock);
2113                 r = 1;
2114                 break;
2115         case HAMMER_FST_SETUP:
2116                 /*
2117                  * The record has a setup dependancy.  These are typically
2118                  * directory entry adds and deletes.  Such entries will be
2119                  * flushed when their inodes are flushed so we do not
2120                  * usually have to add them to the flush here.  However,
2121                  * if the target_ip has set HAMMER_INODE_CONN_DOWN then
2122                  * it is asking us to flush this record (and it).
2123                  */
2124                 target_ip = rec->target_ip;
2125                 KKASSERT(target_ip != NULL);
2126                 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
2127
2128                 /*
2129                  * If the target IP is already flushing in our group
2130                  * we could associate the record, but target_ip has
2131                  * already synced ino_data to sync_ino_data and we
2132                  * would also have to adjust nlinks.   Plus there are
2133                  * ordering issues for adds and deletes.
2134                  *
2135                  * Reflush downward if this is an ADD, and upward if
2136                  * this is a DEL.
2137                  */
2138                 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
2139                         if (rec->flush_state == HAMMER_MEM_RECORD_ADD)
2140                                 ip->flags |= HAMMER_INODE_REFLUSH;
2141                         else
2142                                 target_ip->flags |= HAMMER_INODE_REFLUSH;
2143                         break;
2144                 } 
2145
2146                 /*
2147                  * Target IP is not yet flushing.  This can get complex
2148                  * because we have to be careful about the recursion.
2149                  *
2150                  * Directories create an issue for us in that if a flush
2151                  * of a directory is requested the expectation is to flush
2152                  * any pending directory entries, but this will cause the
2153                  * related inodes to recursively flush as well.  We can't
2154                  * really defer the operation so just get as many as we
2155                  * can and
2156                  */
2157 #if 0
2158                 if ((target_ip->flags & HAMMER_INODE_RECLAIM) == 0 &&
2159                     (target_ip->flags & HAMMER_INODE_CONN_DOWN) == 0) {
2160                         /*
2161                          * We aren't reclaiming and the target ip was not
2162                          * previously prevented from flushing due to this
2163                          * record dependancy.  Do not flush this record.
2164                          */
2165                         /*r = 0;*/
2166                 } else
2167 #endif
2168                 if (flg->total_count + flg->refs >
2169                            ip->hmp->undo_rec_limit) {
2170                         /*
2171                          * Our flush group is over-full and we risk blowing
2172                          * out the UNDO FIFO.  Stop the scan, flush what we
2173                          * have, then reflush the directory.
2174                          *
2175                          * The directory may be forced through multiple
2176                          * flush groups before it can be completely
2177                          * flushed.
2178                          */
2179                         ip->flags |= HAMMER_INODE_RESIGNAL |
2180                                      HAMMER_INODE_REFLUSH;
2181                         r = -1;
2182                 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
2183                         /*
2184                          * If the target IP is not flushing we can force
2185                          * it to flush, even if it is unable to write out
2186                          * any of its own records we have at least one in
2187                          * hand that we CAN deal with.
2188                          */
2189                         rec->flush_state = HAMMER_FST_FLUSH;
2190                         rec->flush_group = flg;
2191                         ++flg->refs;
2192                         hammer_ref(&rec->lock);
2193                         hammer_flush_inode_core(target_ip, flg,
2194                                                 HAMMER_FLUSH_RECURSION);
2195                         r = 1;
2196                 } else {
2197                         /*
2198                          * General or delete-on-disk record.
2199                          *
2200                          * XXX this needs help.  If a delete-on-disk we could
2201                          * disconnect the target.  If the target has its own
2202                          * dependancies they really need to be flushed.
2203                          *
2204                          * XXX
2205                          */
2206                         rec->flush_state = HAMMER_FST_FLUSH;
2207                         rec->flush_group = flg;
2208                         ++flg->refs;
2209                         hammer_ref(&rec->lock);
2210                         hammer_flush_inode_core(target_ip, flg,
2211                                                 HAMMER_FLUSH_RECURSION);
2212                         r = 1;
2213                 }
2214                 break;
2215         case HAMMER_FST_FLUSH:
2216                 /* 
2217                  * The flush_group should already match.
2218                  */
2219                 KKASSERT(rec->flush_group == flg);
2220                 r = 1;
2221                 break;
2222         }
2223         return(r);
2224 }
2225
2226 #if 0
2227 /*
2228  * This version just moves records already in a flush state to the new
2229  * flush group and that is it.
2230  */
2231 static int
2232 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
2233 {
2234         hammer_inode_t ip = rec->ip;
2235
2236         switch(rec->flush_state) {
2237         case HAMMER_FST_FLUSH:
2238                 KKASSERT(rec->flush_group == ip->flush_group);
2239                 break;
2240         default:
2241                 break;
2242         }
2243         return(0);
2244 }
2245 #endif
2246
2247 /*
2248  * Wait for a previously queued flush to complete.
2249  *
2250  * If a critical error occured we don't try to wait.
2251  */
2252 void
2253 hammer_wait_inode(hammer_inode_t ip)
2254 {
2255         hammer_flush_group_t flg;
2256
2257         flg = NULL;
2258         if ((ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2259                 while (ip->flush_state != HAMMER_FST_IDLE &&
2260                        (ip->hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) == 0) {
2261                         if (ip->flush_state == HAMMER_FST_SETUP)
2262                                 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2263                         if (ip->flush_state != HAMMER_FST_IDLE) {
2264                                 ip->flags |= HAMMER_INODE_FLUSHW;
2265                                 tsleep(&ip->flags, 0, "hmrwin", 0);
2266                         }
2267                 }
2268         }
2269 }
2270
2271 /*
2272  * Called by the backend code when a flush has been completed.
2273  * The inode has already been removed from the flush list.
2274  *
2275  * A pipelined flush can occur, in which case we must re-enter the
2276  * inode on the list and re-copy its fields.
2277  */
2278 void
2279 hammer_flush_inode_done(hammer_inode_t ip, int error)
2280 {
2281         hammer_mount_t hmp;
2282         int dorel;
2283
2284         KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
2285
2286         hmp = ip->hmp;
2287
2288         /*
2289          * Auto-reflush if the backend could not completely flush
2290          * the inode.  This fixes a case where a deferred buffer flush
2291          * could cause fsync to return early.
2292          */
2293         if (ip->sync_flags & HAMMER_INODE_MODMASK)
2294                 ip->flags |= HAMMER_INODE_REFLUSH;
2295
2296         /*
2297          * Merge left-over flags back into the frontend and fix the state.
2298          * Incomplete truncations are retained by the backend.
2299          */
2300         ip->error = error;
2301         ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
2302         ip->sync_flags &= HAMMER_INODE_TRUNCATED;
2303
2304         /*
2305          * The backend may have adjusted nlinks, so if the adjusted nlinks
2306          * does not match the fronttend set the frontend's DDIRTY flag again.
2307          */
2308         if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
2309                 ip->flags |= HAMMER_INODE_DDIRTY;
2310
2311         /*
2312          * Fix up the dirty buffer status.
2313          */
2314         if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
2315                 ip->flags |= HAMMER_INODE_BUFS;
2316         }
2317
2318         /*
2319          * Re-set the XDIRTY flag if some of the inode's in-memory records
2320          * could not be flushed.
2321          */
2322         KKASSERT((RB_EMPTY(&ip->rec_tree) &&
2323                   (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
2324                  (!RB_EMPTY(&ip->rec_tree) &&
2325                   (ip->flags & HAMMER_INODE_XDIRTY) != 0));
2326
2327         /*
2328          * Do not lose track of inodes which no longer have vnode
2329          * assocations, otherwise they may never get flushed again.
2330          *
2331          * The reflush flag can be set superfluously, causing extra pain
2332          * for no reason.  If the inode is no longer modified it no longer
2333          * needs to be flushed.
2334          */
2335         if (ip->flags & HAMMER_INODE_MODMASK) {
2336                 if (ip->vp == NULL)
2337                         ip->flags |= HAMMER_INODE_REFLUSH;
2338         } else {
2339                 ip->flags &= ~HAMMER_INODE_REFLUSH;
2340         }
2341
2342         /*
2343          * Adjust the flush state.
2344          */
2345         if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
2346                 /*
2347                  * We were unable to flush out all our records, leave the
2348                  * inode in a flush state and in the current flush group.
2349                  * The flush group will be re-run.
2350                  *
2351                  * This occurs if the UNDO block gets too full or there is
2352                  * too much dirty meta-data and allows the flusher to
2353                  * finalize the UNDO block and then re-flush.
2354                  */
2355                 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
2356                 dorel = 0;
2357         } else {
2358                 /*
2359                  * Remove from the flush_group
2360                  */
2361                 RB_REMOVE(hammer_fls_rb_tree, &ip->flush_group->flush_tree, ip);
2362                 ip->flush_group = NULL;
2363
2364                 /*
2365                  * Clean up the vnode ref and tracking counts.
2366                  */
2367                 if (ip->flags & HAMMER_INODE_VHELD) {
2368                         ip->flags &= ~HAMMER_INODE_VHELD;
2369                         vrele(ip->vp);
2370                 }
2371                 --hmp->count_iqueued;
2372                 --hammer_count_iqueued;
2373
2374                 /*
2375                  * And adjust the state.
2376                  */
2377                 if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
2378                         ip->flush_state = HAMMER_FST_IDLE;
2379                         dorel = 1;
2380                 } else {
2381                         ip->flush_state = HAMMER_FST_SETUP;
2382                         dorel = 0;
2383                 }
2384
2385                 /*
2386                  * If the frontend is waiting for a flush to complete,
2387                  * wake it up.
2388                  */
2389                 if (ip->flags & HAMMER_INODE_FLUSHW) {
2390                         ip->flags &= ~HAMMER_INODE_FLUSHW;
2391                         wakeup(&ip->flags);
2392                 }
2393
2394                 /*
2395                  * If the frontend made more changes and requested another
2396                  * flush, then try to get it running.
2397                  *
2398                  * Reflushes are aborted when the inode is errored out.
2399                  */
2400                 if (ip->flags & HAMMER_INODE_REFLUSH) {
2401                         ip->flags &= ~HAMMER_INODE_REFLUSH;
2402                         if (ip->flags & HAMMER_INODE_RESIGNAL) {
2403                                 ip->flags &= ~HAMMER_INODE_RESIGNAL;
2404                                 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2405                         } else {
2406                                 hammer_flush_inode(ip, 0);
2407                         }
2408                 }
2409         }
2410
2411         /*
2412          * If we have no parent dependancies we can clear CONN_DOWN
2413          */
2414         if (TAILQ_EMPTY(&ip->target_list))
2415                 ip->flags &= ~HAMMER_INODE_CONN_DOWN;
2416
2417         /*
2418          * If the inode is now clean drop the space reservation.
2419          */
2420         if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2421             (ip->flags & HAMMER_INODE_RSV_INODES)) {
2422                 ip->flags &= ~HAMMER_INODE_RSV_INODES;
2423                 --hmp->rsv_inodes;
2424         }
2425
2426         if (dorel)
2427                 hammer_rel_inode(ip, 0);
2428 }
2429
2430 /*
2431  * Called from hammer_sync_inode() to synchronize in-memory records
2432  * to the media.
2433  */
2434 static int
2435 hammer_sync_record_callback(hammer_record_t record, void *data)
2436 {
2437         hammer_cursor_t cursor = data;
2438         hammer_transaction_t trans = cursor->trans;
2439         hammer_mount_t hmp = trans->hmp;
2440         int error;
2441
2442         /*
2443          * Skip records that do not belong to the current flush.
2444          */
2445         ++hammer_stats_record_iterations;
2446         if (record->flush_state != HAMMER_FST_FLUSH)
2447                 return(0);
2448
2449 #if 1
2450         if (record->flush_group != record->ip->flush_group) {
2451                 kprintf("sync_record %p ip %p bad flush group %p %p\n", record, record->ip, record->flush_group ,record->ip->flush_group);
2452                 if (hammer_debug_critical)
2453                         Debugger("blah2");
2454                 return(0);
2455         }
2456 #endif
2457         KKASSERT(record->flush_group == record->ip->flush_group);
2458
2459         /*
2460          * Interlock the record using the BE flag.  Once BE is set the
2461          * frontend cannot change the state of FE.
2462          *
2463          * NOTE: If FE is set prior to us setting BE we still sync the
2464          * record out, but the flush completion code converts it to 
2465          * a delete-on-disk record instead of destroying it.
2466          */
2467         KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
2468         record->flags |= HAMMER_RECF_INTERLOCK_BE;
2469
2470         /*
2471          * The backend has already disposed of the record.
2472          */
2473         if (record->flags & (HAMMER_RECF_DELETED_BE | HAMMER_RECF_COMMITTED)) {
2474                 error = 0;
2475                 goto done;
2476         }
2477
2478         /*
2479          * If the whole inode is being deleting all on-disk records will
2480          * be deleted very soon, we can't sync any new records to disk
2481          * because they will be deleted in the same transaction they were
2482          * created in (delete_tid == create_tid), which will assert.
2483          *
2484          * XXX There may be a case with RECORD_ADD with DELETED_FE set
2485          * that we currently panic on.
2486          */
2487         if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
2488                 switch(record->type) {
2489                 case HAMMER_MEM_RECORD_DATA:
2490                         /*
2491                          * We don't have to do anything, if the record was
2492                          * committed the space will have been accounted for
2493                          * in the blockmap.
2494                          */
2495                         /* fall through */
2496                 case HAMMER_MEM_RECORD_GENERAL:
2497                         /*
2498                          * Set deleted-by-backend flag.  Do not set the
2499                          * backend committed flag, because we are throwing
2500                          * the record away.
2501                          */
2502                         record->flags |= HAMMER_RECF_DELETED_BE;
2503                         ++record->ip->rec_generation;
2504                         error = 0;
2505                         goto done;
2506                 case HAMMER_MEM_RECORD_ADD:
2507                         panic("hammer_sync_record_callback: illegal add "
2508                               "during inode deletion record %p", record);
2509                         break; /* NOT REACHED */
2510                 case HAMMER_MEM_RECORD_INODE:
2511                         panic("hammer_sync_record_callback: attempt to "
2512                               "sync inode record %p?", record);
2513                         break; /* NOT REACHED */
2514                 case HAMMER_MEM_RECORD_DEL:
2515                         /* 
2516                          * Follow through and issue the on-disk deletion
2517                          */
2518                         break;
2519                 }
2520         }
2521
2522         /*
2523          * If DELETED_FE is set special handling is needed for directory
2524          * entries.  Dependant pieces related to the directory entry may
2525          * have already been synced to disk.  If this occurs we have to
2526          * sync the directory entry and then change the in-memory record
2527          * from an ADD to a DELETE to cover the fact that it's been
2528          * deleted by the frontend.
2529          *
2530          * A directory delete covering record (MEM_RECORD_DEL) can never
2531          * be deleted by the frontend.
2532          *
2533          * Any other record type (aka DATA) can be deleted by the frontend.
2534          * XXX At the moment the flusher must skip it because there may
2535          * be another data record in the flush group for the same block,
2536          * meaning that some frontend data changes can leak into the backend's
2537          * synchronization point.
2538          */
2539         if (record->flags & HAMMER_RECF_DELETED_FE) {
2540                 if (record->type == HAMMER_MEM_RECORD_ADD) {
2541                         /*
2542                          * Convert a front-end deleted directory-add to
2543                          * a directory-delete entry later.
2544                          */
2545                         record->flags |= HAMMER_RECF_CONVERT_DELETE;
2546                 } else {
2547                         /*
2548                          * Dispose of the record (race case).  Mark as
2549                          * deleted by backend (and not committed).
2550                          */
2551                         KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2552                         record->flags |= HAMMER_RECF_DELETED_BE;
2553                         ++record->ip->rec_generation;
2554                         error = 0;
2555                         goto done;
2556                 }
2557         }
2558
2559         /*
2560          * Assign the create_tid for new records.  Deletions already
2561          * have the record's entire key properly set up.
2562          */
2563         if (record->type != HAMMER_MEM_RECORD_DEL) {
2564                 record->leaf.base.create_tid = trans->tid;
2565                 record->leaf.create_ts = trans->time32;
2566         }
2567
2568         /*
2569          * This actually moves the record to the on-media B-Tree.  We
2570          * must also generate REDO_TERM entries in the UNDO/REDO FIFO
2571          * indicating that the related REDO_WRITE(s) have been committed.
2572          *
2573          * During recovery any REDO_TERM's within the nominal recovery span
2574          * are ignored since the related meta-data is being undone, causing
2575          * any matching REDO_WRITEs to execute.  The REDO_TERMs outside
2576          * the nominal recovery span will match against REDO_WRITEs and
2577          * prevent them from being executed (because the meta-data has
2578          * already been synchronized).
2579          */
2580         if (record->flags & HAMMER_RECF_REDO) {
2581                 KKASSERT(record->type == HAMMER_MEM_RECORD_DATA);
2582                 hammer_generate_redo(trans, record->ip,
2583                                      record->leaf.base.key -
2584                                          record->leaf.data_len,
2585                                      HAMMER_REDO_TERM_WRITE,
2586                                      NULL,
2587                                      record->leaf.data_len);
2588         }
2589         for (;;) {
2590                 error = hammer_ip_sync_record_cursor(cursor, record);
2591                 if (error != EDEADLK)
2592                         break;
2593                 hammer_done_cursor(cursor);
2594                 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2595                                            record->ip);
2596                 if (error)
2597                         break;
2598         }
2599         record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2600
2601         if (error)
2602                 error = -error;
2603 done:
2604         hammer_flush_record_done(record, error);
2605
2606         /*
2607          * Do partial finalization if we have built up too many dirty
2608          * buffers.  Otherwise a buffer cache deadlock can occur when
2609          * doing things like creating tens of thousands of tiny files.
2610          *
2611          * We must release our cursor lock to avoid a 3-way deadlock
2612          * due to the exclusive sync lock the finalizer must get.
2613          *
2614          * WARNING: See warnings in hammer_unlock_cursor() function.
2615          */
2616         if (hammer_flusher_meta_limit(hmp)) {
2617                 hammer_unlock_cursor(cursor);
2618                 hammer_flusher_finalize(trans, 0);
2619                 hammer_lock_cursor(cursor);
2620         }
2621
2622         return(error);
2623 }
2624
2625 /*
2626  * Backend function called by the flusher to sync an inode to media.
2627  */
2628 int
2629 hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip)
2630 {
2631         struct hammer_cursor cursor;
2632         hammer_node_t tmp_node;
2633         hammer_record_t depend;
2634         hammer_record_t next;
2635         int error, tmp_error;
2636         u_int64_t nlinks;
2637
2638         if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2639                 return(0);
2640
2641         error = hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
2642         if (error)
2643                 goto done;
2644
2645         /*
2646          * Any directory records referencing this inode which are not in
2647          * our current flush group must adjust our nlink count for the
2648          * purposes of synchronizating to disk.
2649          *
2650          * Records which are in our flush group can be unlinked from our
2651          * inode now, potentially allowing the inode to be physically
2652          * deleted.
2653          *
2654          * This cannot block.
2655          */
2656         nlinks = ip->ino_data.nlinks;
2657         next = TAILQ_FIRST(&ip->target_list);
2658         while ((depend = next) != NULL) {
2659                 next = TAILQ_NEXT(depend, target_entry);
2660                 if (depend->flush_state == HAMMER_FST_FLUSH &&
2661                     depend->flush_group == ip->flush_group) {
2662                         /*
2663                          * If this is an ADD that was deleted by the frontend
2664                          * the frontend nlinks count will have already been
2665                          * decremented, but the backend is going to sync its
2666                          * directory entry and must account for it.  The
2667                          * record will be converted to a delete-on-disk when
2668                          * it gets synced.
2669                          *
2670                          * If the ADD was not deleted by the frontend we
2671                          * can remove the dependancy from our target_list.
2672                          */
2673                         if (depend->flags & HAMMER_RECF_DELETED_FE) {
2674                                 ++nlinks;
2675                         } else {
2676                                 TAILQ_REMOVE(&ip->target_list, depend,
2677                                              target_entry);
2678                                 depend->target_ip = NULL;
2679                         }
2680                 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2681                         /*
2682                          * Not part of our flush group and not deleted by
2683                          * the front-end, adjust the link count synced to
2684                          * the media (undo what the frontend did when it
2685                          * queued the record).
2686                          */
2687                         KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2688                         switch(depend->type) {
2689                         case HAMMER_MEM_RECORD_ADD:
2690                                 --nlinks;
2691                                 break;
2692                         case HAMMER_MEM_RECORD_DEL:
2693                                 ++nlinks;
2694                                 break;
2695                         default:
2696                                 break;
2697                         }
2698                 }
2699         }
2700
2701         /*
2702          * Set dirty if we had to modify the link count.
2703          */
2704         if (ip->sync_ino_data.nlinks != nlinks) {
2705                 KKASSERT((int64_t)nlinks >= 0);
2706                 ip->sync_ino_data.nlinks = nlinks;
2707                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2708         }
2709
2710         /*
2711          * If there is a trunction queued destroy any data past the (aligned)
2712          * truncation point.  Userland will have dealt with the buffer
2713          * containing the truncation point for us.
2714          *
2715          * We don't flush pending frontend data buffers until after we've
2716          * dealt with the truncation.
2717          */
2718         if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2719                 /*
2720                  * Interlock trunc_off.  The VOP front-end may continue to
2721                  * make adjustments to it while we are blocked.
2722                  */
2723                 off_t trunc_off;
2724                 off_t aligned_trunc_off;
2725                 int blkmask;
2726
2727                 trunc_off = ip->sync_trunc_off;
2728                 blkmask = hammer_blocksize(trunc_off) - 1;
2729                 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2730
2731                 /*
2732                  * Delete any whole blocks on-media.  The front-end has
2733                  * already cleaned out any partial block and made it
2734                  * pending.  The front-end may have updated trunc_off
2735                  * while we were blocked so we only use sync_trunc_off.
2736                  *
2737                  * This operation can blow out the buffer cache, EWOULDBLOCK
2738                  * means we were unable to complete the deletion.  The
2739                  * deletion will update sync_trunc_off in that case.
2740                  */
2741                 error = hammer_ip_delete_range(&cursor, ip,
2742                                                 aligned_trunc_off,
2743                                                 0x7FFFFFFFFFFFFFFFLL, 2);
2744                 if (error == EWOULDBLOCK) {
2745                         ip->flags |= HAMMER_INODE_WOULDBLOCK;
2746                         error = 0;
2747                         goto defer_buffer_flush;
2748                 }
2749
2750                 if (error)
2751                         goto done;
2752
2753                 /*
2754                  * Generate a REDO_TERM_TRUNC entry in the UNDO/REDO FIFO.
2755                  *
2756                  * XXX we do this even if we did not previously generate
2757                  * a REDO_TRUNC record.  This operation may enclosed the
2758                  * range for multiple prior truncation entries in the REDO
2759                  * log.
2760                  */
2761                 if (trans->hmp->version >= HAMMER_VOL_VERSION_FOUR) {
2762                         hammer_generate_redo(trans, ip, aligned_trunc_off,
2763                                              HAMMER_REDO_TERM_TRUNC,
2764                                              NULL, 0);
2765                 }
2766
2767                 /*
2768                  * Clear the truncation flag on the backend after we have
2769                  * completed the deletions.  Backend data is now good again
2770                  * (including new records we are about to sync, below).
2771                  *
2772                  * Leave sync_trunc_off intact.  As we write additional
2773                  * records the backend will update sync_trunc_off.  This
2774                  * tells the backend whether it can skip the overwrite
2775                  * test.  This should work properly even when the backend
2776                  * writes full blocks where the truncation point straddles
2777                  * the block because the comparison is against the base
2778                  * offset of the record.
2779                  */
2780                 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2781                 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2782         } else {
2783                 error = 0;
2784         }
2785
2786         /*
2787          * Now sync related records.  These will typically be directory
2788          * entries, records tracking direct-writes, or delete-on-disk records.
2789          */
2790         if (error == 0) {
2791                 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2792                                     hammer_sync_record_callback, &cursor);
2793                 if (tmp_error < 0)
2794                         tmp_error = -error;
2795                 if (tmp_error)
2796                         error = tmp_error;
2797         }
2798         hammer_cache_node(&ip->cache[1], cursor.node);
2799
2800         /*
2801          * Re-seek for inode update, assuming our cache hasn't been ripped
2802          * out from under us.
2803          */
2804         if (error == 0) {
2805                 tmp_node = hammer_ref_node_safe(trans, &ip->cache[0], &error);
2806                 if (tmp_node) {
2807                         hammer_cursor_downgrade(&cursor);
2808                         hammer_lock_sh(&tmp_node->lock);
2809                         if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2810                                 hammer_cursor_seek(&cursor, tmp_node, 0);
2811                         hammer_unlock(&tmp_node->lock);
2812                         hammer_rel_node(tmp_node);
2813                 }
2814                 error = 0;
2815         }
2816
2817         /*
2818          * If we are deleting the inode the frontend had better not have
2819          * any active references on elements making up the inode.
2820          *
2821          * The call to hammer_ip_delete_clean() cleans up auxillary records
2822          * but not DB or DATA records.  Those must have already been deleted
2823          * by the normal truncation mechanic.
2824          */
2825         if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2826                 RB_EMPTY(&ip->rec_tree)  &&
2827             (ip->sync_flags & HAMMER_INODE_DELETING) &&
2828             (ip->flags & HAMMER_INODE_DELETED) == 0) {
2829                 int count1 = 0;
2830
2831                 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2832                 if (error == 0) {
2833                         ip->flags |= HAMMER_INODE_DELETED;
2834                         ip->sync_flags &= ~HAMMER_INODE_DELETING;
2835                         ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2836                         KKASSERT(RB_EMPTY(&ip->rec_tree));
2837
2838                         /*
2839                          * Set delete_tid in both the frontend and backend
2840                          * copy of the inode record.  The DELETED flag handles
2841                          * this, do not set DDIRTY.
2842                          */
2843                         ip->ino_leaf.base.delete_tid = trans->tid;
2844                         ip->sync_ino_leaf.base.delete_tid = trans->tid;
2845                         ip->ino_leaf.delete_ts = trans->time32;
2846                         ip->sync_ino_leaf.delete_ts = trans->time32;
2847
2848
2849                         /*
2850                          * Adjust the inode count in the volume header
2851                          */
2852                         hammer_sync_lock_sh(trans);
2853                         if (ip->flags & HAMMER_INODE_ONDISK) {
2854                                 hammer_modify_volume_field(trans,
2855                                                            trans->rootvol,
2856                                                            vol0_stat_inodes);
2857                                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2858                                 hammer_modify_volume_done(trans->rootvol);
2859                         }
2860                         hammer_sync_unlock(trans);
2861                 }
2862         }
2863
2864         if (error)
2865                 goto done;
2866         ip->sync_flags &= ~HAMMER_INODE_BUFS;
2867
2868 defer_buffer_flush:
2869         /*
2870          * Now update the inode's on-disk inode-data and/or on-disk record.
2871          * DELETED and ONDISK are managed only in ip->flags.
2872          *
2873          * In the case of a defered buffer flush we still update the on-disk
2874          * inode to satisfy visibility requirements if there happen to be
2875          * directory dependancies.
2876          */
2877         switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2878         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2879                 /*
2880                  * If deleted and on-disk, don't set any additional flags.
2881                  * the delete flag takes care of things.
2882                  *
2883                  * Clear flags which may have been set by the frontend.
2884                  */
2885                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2886                                     HAMMER_INODE_SDIRTY |
2887                                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2888                                     HAMMER_INODE_DELETING);
2889                 break;
2890         case HAMMER_INODE_DELETED:
2891                 /*
2892                  * Take care of the case where a deleted inode was never
2893                  * flushed to the disk in the first place.
2894                  *
2895                  * Clear flags which may have been set by the frontend.
2896                  */
2897                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2898                                     HAMMER_INODE_SDIRTY |
2899                                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2900                                     HAMMER_INODE_DELETING);
2901                 while (RB_ROOT(&ip->rec_tree)) {
2902                         hammer_record_t record = RB_ROOT(&ip->rec_tree);
2903                         hammer_ref(&record->lock);
2904                         KKASSERT(record->lock.refs == 1);
2905                         record->flags |= HAMMER_RECF_DELETED_BE;
2906                         ++record->ip->rec_generation;
2907                         hammer_rel_mem_record(record);
2908                 }
2909                 break;
2910         case HAMMER_INODE_ONDISK:
2911                 /*
2912                  * If already on-disk, do not set any additional flags.
2913                  */
2914                 break;
2915         default:
2916                 /*
2917                  * If not on-disk and not deleted, set DDIRTY to force
2918                  * an initial record to be written.
2919                  *
2920                  * Also set the create_tid in both the frontend and backend
2921                  * copy of the inode record.
2922                  */
2923                 ip->ino_leaf.base.create_tid = trans->tid;
2924                 ip->ino_leaf.create_ts = trans->time32;
2925                 ip->sync_ino_leaf.base.create_tid = trans->tid;
2926                 ip->sync_ino_leaf.create_ts = trans->time32;
2927                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2928                 break;
2929         }
2930
2931         /*
2932          * If DDIRTY or SDIRTY is set, write out a new record.
2933          * If the inode is already on-disk the old record is marked as
2934          * deleted.
2935          *
2936          * If DELETED is set hammer_update_inode() will delete the existing
2937          * record without writing out a new one.
2938          *
2939          * If *ONLY* the ITIMES flag is set we can update the record in-place.
2940          */
2941         if (ip->flags & HAMMER_INODE_DELETED) {
2942                 error = hammer_update_inode(&cursor, ip);
2943         } else 
2944         if (!(ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY)) &&
2945             (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2946                 error = hammer_update_itimes(&cursor, ip);
2947         } else
2948         if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_SDIRTY |
2949                               HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2950                 error = hammer_update_inode(&cursor, ip);
2951         }
2952 done:
2953         if (error) {
2954                 hammer_critical_error(ip->hmp, ip, error,
2955                                       "while syncing inode");
2956         }
2957         hammer_done_cursor(&cursor);
2958         return(error);
2959 }
2960
2961 /*
2962  * This routine is called when the OS is no longer actively referencing
2963  * the inode (but might still be keeping it cached), or when releasing
2964  * the last reference to an inode.
2965  *
2966  * At this point if the inode's nlinks count is zero we want to destroy
2967  * it, which may mean destroying it on-media too.
2968  */
2969 void
2970 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2971 {
2972         struct vnode *vp;
2973
2974         /*
2975          * Set the DELETING flag when the link count drops to 0 and the
2976          * OS no longer has any opens on the inode.
2977          *
2978          * The backend will clear DELETING (a mod flag) and set DELETED
2979          * (a state flag) when it is actually able to perform the
2980          * operation.
2981          *
2982          * Don't reflag the deletion if the flusher is currently syncing
2983          * one that was already flagged.  A previously set DELETING flag
2984          * may bounce around flags and sync_flags until the operation is
2985          * completely done.
2986          */
2987         if (ip->ino_data.nlinks == 0 &&
2988             ((ip->flags | ip->sync_flags) & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2989                 ip->flags |= HAMMER_INODE_DELETING;
2990                 ip->flags |= HAMMER_INODE_TRUNCATED;
2991                 ip->trunc_off = 0;
2992                 vp = NULL;
2993                 if (getvp) {
2994                         if (hammer_get_vnode(ip, &vp) != 0)
2995                                 return;
2996                 }
2997
2998                 /*
2999                  * Final cleanup
3000                  */
3001                 if (ip->vp) {
3002                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
3003                         vnode_pager_setsize(ip->vp, 0);
3004                 }
3005                 if (getvp) {
3006                         vput(vp);
3007                 }
3008         }
3009 }
3010
3011 /*
3012  * After potentially resolving a dependancy the inode is tested
3013  * to determine whether it needs to be reflushed.
3014  */
3015 void
3016 hammer_test_inode(hammer_inode_t ip)
3017 {
3018         if (ip->flags & HAMMER_INODE_REFLUSH) {
3019                 ip->flags &= ~HAMMER_INODE_REFLUSH;
3020                 hammer_ref(&ip->lock);
3021                 if (ip->flags & HAMMER_INODE_RESIGNAL) {
3022                         ip->flags &= ~HAMMER_INODE_RESIGNAL;
3023                         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
3024                 } else {
3025                         hammer_flush_inode(ip, 0);
3026                 }
3027                 hammer_rel_inode(ip, 0);
3028         }
3029 }
3030
3031 /*
3032  * Clear the RECLAIM flag on an inode.  This occurs when the inode is
3033  * reassociated with a vp or just before it gets freed.
3034  *
3035  * Pipeline wakeups to threads blocked due to an excessive number of
3036  * detached inodes.  This typically occurs when atime updates accumulate
3037  * while scanning a directory tree.
3038  */
3039 static void
3040 hammer_inode_wakereclaims(hammer_inode_t ip)
3041 {
3042         struct hammer_reclaim *reclaim;
3043         hammer_mount_t hmp = ip->hmp;
3044
3045         if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
3046                 return;
3047
3048         --hammer_count_reclaiming;
3049         --hmp->inode_reclaims;
3050         ip->flags &= ~HAMMER_INODE_RECLAIM;
3051
3052         while ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
3053                 if (reclaim->count > 0 && --reclaim->count == 0) {
3054                         TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
3055                         wakeup(reclaim);
3056                 }
3057                 if (hmp->inode_reclaims > hammer_limit_reclaim / 2)
3058                         break;
3059         }
3060 }
3061
3062 /*
3063  * Setup our reclaim pipeline.  We only let so many detached (and dirty)
3064  * inodes build up before we start blocking.  This routine is called
3065  * if a new inode is created or an inode is loaded from media.
3066  *
3067  * When we block we don't care *which* inode has finished reclaiming,
3068  * as lone as one does.
3069  */
3070 void
3071 hammer_inode_waitreclaims(hammer_mount_t hmp)
3072 {
3073         struct hammer_reclaim reclaim;
3074
3075         if (hmp->inode_reclaims < hammer_limit_reclaim)
3076                 return;
3077         reclaim.count = 1;
3078         TAILQ_INSERT_TAIL(&hmp->reclaim_list, &reclaim, entry);
3079         tsleep(&reclaim, 0, "hmrrcm", hz);
3080         if (reclaim.count > 0)
3081                 TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
3082 }
3083
3084 #if 0
3085
3086 /*
3087  * XXX not used, doesn't work very well due to the large batching nature
3088  * of flushes.
3089  *
3090  * A larger then normal backlog of inodes is sitting in the flusher,
3091  * enforce a general slowdown to let it catch up.  This routine is only
3092  * called on completion of a non-flusher-related transaction which
3093  * performed B-Tree node I/O.
3094  *
3095  * It is possible for the flusher to stall in a continuous load.
3096  * blogbench -i1000 -o seems to do a good job generating this sort of load.
3097  * If the flusher is unable to catch up the inode count can bloat until
3098  * we run out of kvm.
3099  *
3100  * This is a bit of a hack.
3101  */
3102 void
3103 hammer_inode_waithard(hammer_mount_t hmp)
3104 {
3105         /*
3106          * Hysteresis.
3107          */
3108         if (hmp->flags & HAMMER_MOUNT_FLUSH_RECOVERY) {
3109                 if (hmp->inode_reclaims < hammer_limit_reclaim / 2 &&
3110                     hmp->count_iqueued < hmp->count_inodes / 20) {
3111                         hmp->flags &= ~HAMMER_MOUNT_FLUSH_RECOVERY;
3112                         return;
3113                 }
3114         } else {
3115                 if (hmp->inode_reclaims < hammer_limit_reclaim ||
3116                     hmp->count_iqueued < hmp->count_inodes / 10) {
3117                         return;
3118                 }
3119                 hmp->flags |= HAMMER_MOUNT_FLUSH_RECOVERY;
3120         }
3121
3122         /*
3123          * Block for one flush cycle.
3124          */
3125         hammer_flusher_wait_next(hmp);
3126 }
3127
3128 #endif