HAMMER 60J/Many: Mirroring
[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.97 2008/07/10 04:44:33 dillon Exp $
35  */
36
37 #include "hammer.h"
38 #include <vm/vm_extern.h>
39 #include <sys/buf.h>
40 #include <sys/buf2.h>
41
42 static int      hammer_unload_inode(struct hammer_inode *ip);
43 static void     hammer_free_inode(hammer_inode_t ip);
44 static void     hammer_flush_inode_core(hammer_inode_t ip, int flags);
45 static int      hammer_setup_child_callback(hammer_record_t rec, void *data);
46 static int      hammer_syncgrp_child_callback(hammer_record_t rec, void *data);
47 static int      hammer_setup_parent_inodes(hammer_inode_t ip);
48 static int      hammer_setup_parent_inodes_helper(hammer_record_t record);
49 static void     hammer_inode_wakereclaims(hammer_inode_t ip);
50
51 #ifdef DEBUG_TRUNCATE
52 extern struct hammer_inode *HammerTruncIp;
53 #endif
54
55 /*
56  * RB-Tree support for inode structures
57  */
58 int
59 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
60 {
61         if (ip1->obj_localization < ip2->obj_localization)
62                 return(-1);
63         if (ip1->obj_localization > ip2->obj_localization)
64                 return(1);
65         if (ip1->obj_id < ip2->obj_id)
66                 return(-1);
67         if (ip1->obj_id > ip2->obj_id)
68                 return(1);
69         if (ip1->obj_asof < ip2->obj_asof)
70                 return(-1);
71         if (ip1->obj_asof > ip2->obj_asof)
72                 return(1);
73         return(0);
74 }
75
76 /*
77  * RB-Tree support for inode structures / special LOOKUP_INFO
78  */
79 static int
80 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
81 {
82         if (info->obj_localization < ip->obj_localization)
83                 return(-1);
84         if (info->obj_localization > ip->obj_localization)
85                 return(1);
86         if (info->obj_id < ip->obj_id)
87                 return(-1);
88         if (info->obj_id > ip->obj_id)
89                 return(1);
90         if (info->obj_asof < ip->obj_asof)
91                 return(-1);
92         if (info->obj_asof > ip->obj_asof)
93                 return(1);
94         return(0);
95 }
96
97 /*
98  * Used by hammer_scan_inode_snapshots() to locate all of an object's
99  * snapshots.  Note that the asof field is not tested, which we can get
100  * away with because it is the lowest-priority field.
101  */
102 static int
103 hammer_inode_info_cmp_all_history(hammer_inode_t ip, void *data)
104 {
105         hammer_inode_info_t info = data;
106
107         if (ip->obj_localization > info->obj_localization)
108                 return(1);
109         if (ip->obj_localization < info->obj_localization)
110                 return(-1);
111         if (ip->obj_id > info->obj_id)
112                 return(1);
113         if (ip->obj_id < info->obj_id)
114                 return(-1);
115         return(0);
116 }
117
118 /*
119  * RB-Tree support for pseudofs structures
120  */
121 static int
122 hammer_pfs_rb_compare(hammer_pseudofs_inmem_t p1, hammer_pseudofs_inmem_t p2)
123 {
124         if (p1->localization < p2->localization)
125                 return(-1);
126         if (p1->localization > p2->localization)
127                 return(1);
128         return(0);
129 }
130
131
132 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
133 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
134                 hammer_inode_info_cmp, hammer_inode_info_t);
135 RB_GENERATE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
136              hammer_pfs_rb_compare, u_int32_t, localization);
137
138 /*
139  * The kernel is not actively referencing this vnode but is still holding
140  * it cached.
141  *
142  * This is called from the frontend.
143  */
144 int
145 hammer_vop_inactive(struct vop_inactive_args *ap)
146 {
147         struct hammer_inode *ip = VTOI(ap->a_vp);
148
149         /*
150          * Degenerate case
151          */
152         if (ip == NULL) {
153                 vrecycle(ap->a_vp);
154                 return(0);
155         }
156
157         /*
158          * If the inode no longer has visibility in the filesystem try to
159          * recycle it immediately, even if the inode is dirty.  Recycling
160          * it quickly allows the system to reclaim buffer cache and VM
161          * resources which can matter a lot in a heavily loaded system.
162          *
163          * This can deadlock in vfsync() if we aren't careful.
164          * 
165          * Do not queue the inode to the flusher if we still have visibility,
166          * otherwise namespace calls such as chmod will unnecessarily generate
167          * multiple inode updates.
168          */
169         hammer_inode_unloadable_check(ip, 0);
170         if (ip->ino_data.nlinks == 0) {
171                 if (ip->flags & HAMMER_INODE_MODMASK)
172                         hammer_flush_inode(ip, 0);
173                 vrecycle(ap->a_vp);
174         }
175         return(0);
176 }
177
178 /*
179  * Release the vnode association.  This is typically (but not always)
180  * the last reference on the inode.
181  *
182  * Once the association is lost we are on our own with regards to
183  * flushing the inode.
184  */
185 int
186 hammer_vop_reclaim(struct vop_reclaim_args *ap)
187 {
188         struct hammer_inode *ip;
189         hammer_mount_t hmp;
190         struct vnode *vp;
191
192         vp = ap->a_vp;
193
194         if ((ip = vp->v_data) != NULL) {
195                 hmp = ip->hmp;
196                 vp->v_data = NULL;
197                 ip->vp = NULL;
198
199                 if ((ip->flags & HAMMER_INODE_RECLAIM) == 0) {
200                         ++hammer_count_reclaiming;
201                         ++hmp->inode_reclaims;
202                         ip->flags |= HAMMER_INODE_RECLAIM;
203                         if (hmp->inode_reclaims > HAMMER_RECLAIM_FLUSH &&
204                             (hmp->inode_reclaims & 255) == 0) {
205                                 hammer_flusher_async(hmp);
206                         }
207                 }
208                 hammer_rel_inode(ip, 1);
209         }
210         return(0);
211 }
212
213 /*
214  * Return a locked vnode for the specified inode.  The inode must be
215  * referenced but NOT LOCKED on entry and will remain referenced on
216  * return.
217  *
218  * Called from the frontend.
219  */
220 int
221 hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp)
222 {
223         hammer_mount_t hmp;
224         struct vnode *vp;
225         int error = 0;
226         u_int8_t obj_type;
227
228         hmp = ip->hmp;
229
230         for (;;) {
231                 if ((vp = ip->vp) == NULL) {
232                         error = getnewvnode(VT_HAMMER, hmp->mp, vpp, 0, 0);
233                         if (error)
234                                 break;
235                         hammer_lock_ex(&ip->lock);
236                         if (ip->vp != NULL) {
237                                 hammer_unlock(&ip->lock);
238                                 vp->v_type = VBAD;
239                                 vx_put(vp);
240                                 continue;
241                         }
242                         hammer_ref(&ip->lock);
243                         vp = *vpp;
244                         ip->vp = vp;
245
246                         obj_type = ip->ino_data.obj_type;
247                         vp->v_type = hammer_get_vnode_type(obj_type);
248
249                         hammer_inode_wakereclaims(ip);
250
251                         switch(ip->ino_data.obj_type) {
252                         case HAMMER_OBJTYPE_CDEV:
253                         case HAMMER_OBJTYPE_BDEV:
254                                 vp->v_ops = &hmp->mp->mnt_vn_spec_ops;
255                                 addaliasu(vp, ip->ino_data.rmajor,
256                                           ip->ino_data.rminor);
257                                 break;
258                         case HAMMER_OBJTYPE_FIFO:
259                                 vp->v_ops = &hmp->mp->mnt_vn_fifo_ops;
260                                 break;
261                         default:
262                                 break;
263                         }
264
265                         /*
266                          * Only mark as the root vnode if the ip is not
267                          * historical, otherwise the VFS cache will get
268                          * confused.  The other half of the special handling
269                          * is in hammer_vop_nlookupdotdot().
270                          *
271                          * Pseudo-filesystem roots also do not count.
272                          */
273                         if (ip->obj_id == HAMMER_OBJID_ROOT &&
274                             ip->obj_asof == hmp->asof &&
275                             ip->obj_localization == 0) {
276                                 vp->v_flag |= VROOT;
277                         }
278
279                         vp->v_data = (void *)ip;
280                         /* vnode locked by getnewvnode() */
281                         /* make related vnode dirty if inode dirty? */
282                         hammer_unlock(&ip->lock);
283                         if (vp->v_type == VREG)
284                                 vinitvmio(vp, ip->ino_data.size);
285                         break;
286                 }
287
288                 /*
289                  * loop if the vget fails (aka races), or if the vp
290                  * no longer matches ip->vp.
291                  */
292                 if (vget(vp, LK_EXCLUSIVE) == 0) {
293                         if (vp == ip->vp)
294                                 break;
295                         vput(vp);
296                 }
297         }
298         *vpp = vp;
299         return(error);
300 }
301
302 /*
303  * Locate all copies of the inode for obj_id compatible with the specified
304  * asof, reference, and issue the related call-back.  This routine is used
305  * for direct-io invalidation and does not create any new inodes.
306  */
307 void
308 hammer_scan_inode_snapshots(hammer_mount_t hmp, hammer_inode_info_t iinfo,
309                             int (*callback)(hammer_inode_t ip, void *data),
310                             void *data)
311 {
312         hammer_ino_rb_tree_RB_SCAN(&hmp->rb_inos_root,
313                                    hammer_inode_info_cmp_all_history,
314                                    callback, iinfo);
315 }
316
317 /*
318  * Acquire a HAMMER inode.  The returned inode is not locked.  These functions
319  * do not attach or detach the related vnode (use hammer_get_vnode() for
320  * that).
321  *
322  * The flags argument is only applied for newly created inodes, and only
323  * certain flags are inherited.
324  *
325  * Called from the frontend.
326  */
327 struct hammer_inode *
328 hammer_get_inode(hammer_transaction_t trans, hammer_inode_t dip,
329                  int64_t obj_id, hammer_tid_t asof, u_int32_t localization,
330                  int flags, int *errorp)
331 {
332         hammer_mount_t hmp = trans->hmp;
333         struct hammer_inode_info iinfo;
334         struct hammer_cursor cursor;
335         struct hammer_inode *ip;
336
337
338         /*
339          * Determine if we already have an inode cached.  If we do then
340          * we are golden.
341          */
342         iinfo.obj_id = obj_id;
343         iinfo.obj_asof = asof;
344         iinfo.obj_localization = localization;
345 loop:
346         ip = hammer_ino_rb_tree_RB_LOOKUP_INFO(&hmp->rb_inos_root, &iinfo);
347         if (ip) {
348                 hammer_ref(&ip->lock);
349                 *errorp = 0;
350                 return(ip);
351         }
352
353         /*
354          * Allocate a new inode structure and deal with races later.
355          */
356         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
357         ++hammer_count_inodes;
358         ++hmp->count_inodes;
359         ip->obj_id = obj_id;
360         ip->obj_asof = iinfo.obj_asof;
361         ip->obj_localization = localization;
362         ip->hmp = hmp;
363         ip->flags = flags & HAMMER_INODE_RO;
364         ip->cache[0].ip = ip;
365         ip->cache[1].ip = ip;
366         if (hmp->ronly)
367                 ip->flags |= HAMMER_INODE_RO;
368         ip->sync_trunc_off = ip->trunc_off = ip->save_trunc_off =
369                 0x7FFFFFFFFFFFFFFFLL;
370         RB_INIT(&ip->rec_tree);
371         TAILQ_INIT(&ip->target_list);
372         hammer_ref(&ip->lock);
373
374         /*
375          * Locate the on-disk inode.  If this is a PFS root we always
376          * access the current version of the root inode and (if it is not
377          * a master) always access information under it with a snapshot
378          * TID.
379          */
380 retry:
381         hammer_init_cursor(trans, &cursor, (dip ? &dip->cache[0] : NULL), NULL);
382         cursor.key_beg.localization = localization + HAMMER_LOCALIZE_INODE;
383         cursor.key_beg.obj_id = ip->obj_id;
384         cursor.key_beg.key = 0;
385         cursor.key_beg.create_tid = 0;
386         cursor.key_beg.delete_tid = 0;
387         cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
388         cursor.key_beg.obj_type = 0;
389
390         cursor.asof = iinfo.obj_asof;
391         cursor.flags = HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_GET_DATA |
392                        HAMMER_CURSOR_ASOF;
393
394         *errorp = hammer_btree_lookup(&cursor);
395         if (*errorp == EDEADLK) {
396                 hammer_done_cursor(&cursor);
397                 goto retry;
398         }
399
400         /*
401          * On success the B-Tree lookup will hold the appropriate
402          * buffer cache buffers and provide a pointer to the requested
403          * information.  Copy the information to the in-memory inode
404          * and cache the B-Tree node to improve future operations.
405          */
406         if (*errorp == 0) {
407                 ip->ino_leaf = cursor.node->ondisk->elms[cursor.index].leaf;
408                 ip->ino_data = cursor.data->inode;
409
410                 /*
411                  * cache[0] tries to cache the location of the object inode.
412                  * The assumption is that it is near the directory inode.
413                  *
414                  * cache[1] tries to cache the location of the object data.
415                  * The assumption is that it is near the directory data.
416                  */
417                 hammer_cache_node(&ip->cache[0], cursor.node);
418                 if (dip && dip->cache[1].node)
419                         hammer_cache_node(&ip->cache[1], dip->cache[1].node);
420
421                 /*
422                  * The file should not contain any data past the file size
423                  * stored in the inode.  Setting save_trunc_off to the
424                  * file size instead of max reduces B-Tree lookup overheads
425                  * on append by allowing the flusher to avoid checking for
426                  * record overwrites.
427                  */
428                 ip->save_trunc_off = ip->ino_data.size;
429
430                 /*
431                  * Locate and assign the pseudofs management structure to
432                  * the inode.
433                  */
434                 if (dip && dip->obj_localization == ip->obj_localization) {
435                         ip->pfsm = dip->pfsm;
436                         hammer_ref(&ip->pfsm->lock);
437                 } else {
438                         ip->pfsm = hammer_load_pseudofs(trans,
439                                                         ip->obj_localization,
440                                                         errorp);
441                         *errorp = 0;    /* ignore ENOENT */
442                 }
443         }
444
445         /*
446          * The inode is placed on the red-black tree and will be synced to
447          * the media when flushed or by the filesystem sync.  If this races
448          * another instantiation/lookup the insertion will fail.
449          */
450         if (*errorp == 0) {
451                 if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
452                         hammer_free_inode(ip);
453                         hammer_done_cursor(&cursor);
454                         goto loop;
455                 }
456                 ip->flags |= HAMMER_INODE_ONDISK;
457         } else {
458                 if (ip->flags & HAMMER_INODE_RSV_INODES) {
459                         ip->flags &= ~HAMMER_INODE_RSV_INODES; /* sanity */
460                         --hmp->rsv_inodes;
461                 }
462
463                 hammer_free_inode(ip);
464                 ip = NULL;
465         }
466         hammer_done_cursor(&cursor);
467         return (ip);
468 }
469
470 /*
471  * Create a new filesystem object, returning the inode in *ipp.  The
472  * returned inode will be referenced.  The inode is created in-memory.
473  *
474  * If pfsm is non-NULL the caller wishes to create the root inode for
475  * a master PFS.
476  */
477 int
478 hammer_create_inode(hammer_transaction_t trans, struct vattr *vap,
479                     struct ucred *cred, hammer_inode_t dip,
480                     hammer_pseudofs_inmem_t pfsm, struct hammer_inode **ipp)
481 {
482         hammer_mount_t hmp;
483         hammer_inode_t ip;
484         uid_t xuid;
485         int error;
486
487         hmp = trans->hmp;
488
489         ip = kmalloc(sizeof(*ip), M_HAMMER, M_WAITOK|M_ZERO);
490         ++hammer_count_inodes;
491         ++hmp->count_inodes;
492
493         if (pfsm) {
494                 KKASSERT(pfsm->localization != 0);
495                 ip->obj_id = HAMMER_OBJID_ROOT;
496                 ip->obj_localization = pfsm->localization;
497         } else {
498                 KKASSERT(dip != NULL);
499                 ip->obj_id = hammer_alloc_objid(hmp, dip);
500                 ip->obj_localization = dip->obj_localization;
501         }
502
503         KKASSERT(ip->obj_id != 0);
504         ip->obj_asof = hmp->asof;
505         ip->hmp = hmp;
506         ip->flush_state = HAMMER_FST_IDLE;
507         ip->flags = HAMMER_INODE_DDIRTY |
508                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME;
509         ip->cache[0].ip = ip;
510         ip->cache[1].ip = ip;
511
512         ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
513         /* ip->save_trunc_off = 0; (already zero) */
514         RB_INIT(&ip->rec_tree);
515         TAILQ_INIT(&ip->target_list);
516
517         ip->ino_data.atime = trans->time;
518         ip->ino_data.mtime = trans->time;
519         ip->ino_data.size = 0;
520         ip->ino_data.nlinks = 0;
521
522         /*
523          * A nohistory designator on the parent directory is inherited by
524          * the child.  We will do this even for pseudo-fs creation... the
525          * sysad can turn it off.
526          */
527         if (dip) {
528                 ip->ino_data.uflags = dip->ino_data.uflags &
529                                       (SF_NOHISTORY|UF_NOHISTORY|UF_NODUMP);
530         }
531
532         ip->ino_leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
533         ip->ino_leaf.base.localization = ip->obj_localization +
534                                          HAMMER_LOCALIZE_INODE;
535         ip->ino_leaf.base.obj_id = ip->obj_id;
536         ip->ino_leaf.base.key = 0;
537         ip->ino_leaf.base.create_tid = 0;
538         ip->ino_leaf.base.delete_tid = 0;
539         ip->ino_leaf.base.rec_type = HAMMER_RECTYPE_INODE;
540         ip->ino_leaf.base.obj_type = hammer_get_obj_type(vap->va_type);
541
542         ip->ino_data.obj_type = ip->ino_leaf.base.obj_type;
543         ip->ino_data.version = HAMMER_INODE_DATA_VERSION;
544         ip->ino_data.mode = vap->va_mode;
545         ip->ino_data.ctime = trans->time;
546
547         /*
548          * Setup the ".." pointer.  This only needs to be done for directories
549          * but we do it for all objects as a recovery aid.
550          */
551         if (dip)
552                 ip->ino_data.parent_obj_id = dip->ino_leaf.base.obj_id;
553 #if 0
554         /*
555          * The parent_obj_localization field only applies to pseudo-fs roots.
556          * XXX this is no longer applicable, PFSs are no longer directly
557          * tied into the parent's directory structure.
558          */
559         if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY &&
560             ip->obj_id == HAMMER_OBJID_ROOT) {
561                 ip->ino_data.ext.obj.parent_obj_localization = 
562                                                 dip->obj_localization;
563         }
564 #endif
565
566         switch(ip->ino_leaf.base.obj_type) {
567         case HAMMER_OBJTYPE_CDEV:
568         case HAMMER_OBJTYPE_BDEV:
569                 ip->ino_data.rmajor = vap->va_rmajor;
570                 ip->ino_data.rminor = vap->va_rminor;
571                 break;
572         default:
573                 break;
574         }
575
576         /*
577          * Calculate default uid/gid and overwrite with information from
578          * the vap.
579          */
580         if (dip) {
581                 xuid = hammer_to_unix_xid(&dip->ino_data.uid);
582                 xuid = vop_helper_create_uid(hmp->mp, dip->ino_data.mode,
583                                              xuid, cred, &vap->va_mode);
584         } else {
585                 xuid = 0;
586         }
587         ip->ino_data.mode = vap->va_mode;
588
589         if (vap->va_vaflags & VA_UID_UUID_VALID)
590                 ip->ino_data.uid = vap->va_uid_uuid;
591         else if (vap->va_uid != (uid_t)VNOVAL)
592                 hammer_guid_to_uuid(&ip->ino_data.uid, vap->va_uid);
593         else
594                 hammer_guid_to_uuid(&ip->ino_data.uid, xuid);
595
596         if (vap->va_vaflags & VA_GID_UUID_VALID)
597                 ip->ino_data.gid = vap->va_gid_uuid;
598         else if (vap->va_gid != (gid_t)VNOVAL)
599                 hammer_guid_to_uuid(&ip->ino_data.gid, vap->va_gid);
600         else if (dip)
601                 ip->ino_data.gid = dip->ino_data.gid;
602
603         hammer_ref(&ip->lock);
604
605         if (pfsm) {
606                 ip->pfsm = pfsm;
607                 hammer_ref(&pfsm->lock);
608                 error = 0;
609         } else if (dip->obj_localization == ip->obj_localization) {
610                 ip->pfsm = dip->pfsm;
611                 hammer_ref(&ip->pfsm->lock);
612                 error = 0;
613         } else {
614                 ip->pfsm = hammer_load_pseudofs(trans,
615                                                 ip->obj_localization,
616                                                 &error);
617                 error = 0;      /* ignore ENOENT */
618         }
619
620         if (error) {
621                 hammer_free_inode(ip);
622                 ip = NULL;
623         } else if (RB_INSERT(hammer_ino_rb_tree, &hmp->rb_inos_root, ip)) {
624                 panic("hammer_create_inode: duplicate obj_id %llx", ip->obj_id);
625                 /* not reached */
626                 hammer_free_inode(ip);
627         }
628         *ipp = ip;
629         return(error);
630 }
631
632 /*
633  * Final cleanup / freeing of an inode structure
634  */
635 static void
636 hammer_free_inode(hammer_inode_t ip)
637 {
638         KKASSERT(ip->lock.refs == 1);
639         hammer_uncache_node(&ip->cache[0]);
640         hammer_uncache_node(&ip->cache[1]);
641         hammer_inode_wakereclaims(ip);
642         if (ip->objid_cache)
643                 hammer_clear_objid(ip);
644         --hammer_count_inodes;
645         --ip->hmp->count_inodes;
646         if (ip->pfsm) {
647                 hammer_rel_pseudofs(ip->hmp, ip->pfsm);
648                 ip->pfsm = NULL;
649         }
650         kfree(ip, M_HAMMER);
651         ip = NULL;
652 }
653
654 /*
655  * Retrieve pseudo-fs data.  NULL will never be returned.
656  *
657  * If an error occurs *errorp will be set and a default template is returned,
658  * otherwise *errorp is set to 0.  Typically when an error occurs it will
659  * be ENOENT.
660  */
661 hammer_pseudofs_inmem_t
662 hammer_load_pseudofs(hammer_transaction_t trans,
663                      u_int32_t localization, int *errorp)
664 {
665         hammer_mount_t hmp = trans->hmp;
666         hammer_inode_t ip;
667         hammer_pseudofs_inmem_t pfsm;
668         struct hammer_cursor cursor;
669         int bytes;
670
671 retry:
672         pfsm = RB_LOOKUP(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, localization);
673         if (pfsm) {
674                 hammer_ref(&pfsm->lock);
675                 *errorp = 0;
676                 return(pfsm);
677         }
678
679         /*
680          * PFS records are stored in the root inode (not the PFS root inode,
681          * but the real root).  Avoid an infinite recursion if loading
682          * the PFS for the real root.
683          */
684         if (localization) {
685                 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT,
686                                       HAMMER_MAX_TID,
687                                       HAMMER_DEF_LOCALIZATION, 0, errorp);
688         } else {
689                 ip = NULL;
690         }
691
692         pfsm = kmalloc(sizeof(*pfsm), M_HAMMER, M_WAITOK | M_ZERO);
693         pfsm->localization = localization;
694         pfsm->pfsd.unique_uuid = trans->rootvol->ondisk->vol_fsid;
695         pfsm->pfsd.shared_uuid = pfsm->pfsd.unique_uuid;
696
697         hammer_init_cursor(trans, &cursor, (ip ? &ip->cache[1] : NULL), ip);
698         cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
699                                       HAMMER_LOCALIZE_MISC;
700         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
701         cursor.key_beg.create_tid = 0;
702         cursor.key_beg.delete_tid = 0;
703         cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
704         cursor.key_beg.obj_type = 0;
705         cursor.key_beg.key = localization;
706         cursor.asof = HAMMER_MAX_TID;
707         cursor.flags |= HAMMER_CURSOR_ASOF;
708
709         if (ip)
710                 *errorp = hammer_ip_lookup(&cursor);
711         else
712                 *errorp = hammer_btree_lookup(&cursor);
713         if (*errorp == 0) {
714                 *errorp = hammer_ip_resolve_data(&cursor);
715                 if (*errorp == 0) {
716                         bytes = cursor.leaf->data_len;
717                         if (bytes > sizeof(pfsm->pfsd))
718                                 bytes = sizeof(pfsm->pfsd);
719                         bcopy(cursor.data, &pfsm->pfsd, bytes);
720                 }
721         }
722         hammer_done_cursor(&cursor);
723
724         pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
725         hammer_ref(&pfsm->lock);
726         if (ip)
727                 hammer_rel_inode(ip, 0);
728         if (RB_INSERT(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm)) {
729                 kfree(pfsm, M_HAMMER);
730                 goto retry;
731         }
732         return(pfsm);
733 }
734
735 /*
736  * Store pseudo-fs data.  The backend will automatically delete any prior
737  * on-disk pseudo-fs data but we have to delete in-memory versions.
738  */
739 int
740 hammer_save_pseudofs(hammer_transaction_t trans, hammer_pseudofs_inmem_t pfsm)
741 {
742         struct hammer_cursor cursor;
743         hammer_record_t record;
744         hammer_inode_t ip;
745         int error;
746
747         ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
748                               HAMMER_DEF_LOCALIZATION, 0, &error);
749 retry:
750         pfsm->fsid_udev = hammer_fsid_to_udev(&pfsm->pfsd.shared_uuid);
751         hammer_init_cursor(trans, &cursor, &ip->cache[1], ip);
752         cursor.key_beg.localization = ip->obj_localization +
753                                       HAMMER_LOCALIZE_MISC;
754         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
755         cursor.key_beg.create_tid = 0;
756         cursor.key_beg.delete_tid = 0;
757         cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
758         cursor.key_beg.obj_type = 0;
759         cursor.key_beg.key = pfsm->localization;
760         cursor.asof = HAMMER_MAX_TID;
761         cursor.flags |= HAMMER_CURSOR_ASOF;
762
763         error = hammer_ip_lookup(&cursor);
764         if (error == 0 && hammer_cursor_inmem(&cursor)) {
765                 record = cursor.iprec;
766                 if (record->flags & HAMMER_RECF_INTERLOCK_BE) {
767                         KKASSERT(cursor.deadlk_rec == NULL);
768                         hammer_ref(&record->lock);
769                         cursor.deadlk_rec = record;
770                         error = EDEADLK;
771                 } else {
772                         record->flags |= HAMMER_RECF_DELETED_FE;
773                         error = 0;
774                 }
775         }
776         if (error == 0 || error == ENOENT) {
777                 record = hammer_alloc_mem_record(ip, sizeof(pfsm->pfsd));
778                 record->type = HAMMER_MEM_RECORD_GENERAL;
779
780                 record->leaf.base.localization = ip->obj_localization +
781                                                  HAMMER_LOCALIZE_MISC;
782                 record->leaf.base.rec_type = HAMMER_RECTYPE_PFS;
783                 record->leaf.base.key = pfsm->localization;
784                 record->leaf.data_len = sizeof(pfsm->pfsd);
785                 bcopy(&pfsm->pfsd, record->data, sizeof(pfsm->pfsd));
786                 error = hammer_ip_add_record(trans, record);
787         }
788         hammer_done_cursor(&cursor);
789         if (error == EDEADLK)
790                 goto retry;
791         hammer_rel_inode(ip, 0);
792         return(error);
793 }
794
795 /*
796  * Create a root directory for a PFS if one does not alredy exist.
797  *
798  * The PFS root stands alone so we must also bump the nlinks count
799  * to prevent it from being destroyed on release.
800  */
801 int
802 hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
803                        hammer_pseudofs_inmem_t pfsm)
804 {
805         hammer_inode_t ip;
806         struct vattr vap;
807         int error;
808
809         ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
810                               pfsm->localization, 0, &error);
811         if (ip == NULL) {
812                 vattr_null(&vap);
813                 vap.va_mode = 0755;
814                 vap.va_type = VDIR;
815                 error = hammer_create_inode(trans, &vap, cred, NULL, pfsm, &ip);
816                 if (error == 0) {
817                         ++ip->ino_data.nlinks;
818                         hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
819                 }
820         }
821         if (ip)
822                 hammer_rel_inode(ip, 0);
823         return(error);
824 }
825
826 /*
827  * Release a reference on a PFS
828  */
829 void
830 hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm)
831 {
832         hammer_unref(&pfsm->lock);
833         if (pfsm->lock.refs == 0) {
834                 RB_REMOVE(hammer_pfs_rb_tree, &hmp->rb_pfsm_root, pfsm);
835                 kfree(pfsm, M_HAMMER);
836         }
837 }
838
839 /*
840  * Called by hammer_sync_inode().
841  */
842 static int
843 hammer_update_inode(hammer_cursor_t cursor, hammer_inode_t ip)
844 {
845         hammer_transaction_t trans = cursor->trans;
846         hammer_record_t record;
847         int error;
848         int redirty;
849
850 retry:
851         error = 0;
852
853         /*
854          * If the inode has a presence on-disk then locate it and mark
855          * it deleted, setting DELONDISK.
856          *
857          * The record may or may not be physically deleted, depending on
858          * the retention policy.
859          */
860         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) ==
861             HAMMER_INODE_ONDISK) {
862                 hammer_normalize_cursor(cursor);
863                 cursor->key_beg.localization = ip->obj_localization + 
864                                                HAMMER_LOCALIZE_INODE;
865                 cursor->key_beg.obj_id = ip->obj_id;
866                 cursor->key_beg.key = 0;
867                 cursor->key_beg.create_tid = 0;
868                 cursor->key_beg.delete_tid = 0;
869                 cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
870                 cursor->key_beg.obj_type = 0;
871                 cursor->asof = ip->obj_asof;
872                 cursor->flags &= ~HAMMER_CURSOR_INITMASK;
873                 cursor->flags |= HAMMER_CURSOR_GET_LEAF | HAMMER_CURSOR_ASOF;
874                 cursor->flags |= HAMMER_CURSOR_BACKEND;
875
876                 error = hammer_btree_lookup(cursor);
877                 if (hammer_debug_inode)
878                         kprintf("IPDEL %p %08x %d", ip, ip->flags, error);
879                 if (error) {
880                         kprintf("error %d\n", error);
881                         Debugger("hammer_update_inode");
882                 }
883
884                 if (error == 0) {
885                         error = hammer_ip_delete_record(cursor, ip, trans->tid);
886                         if (hammer_debug_inode)
887                                 kprintf(" error %d\n", error);
888                         if (error && error != EDEADLK) {
889                                 kprintf("error %d\n", error);
890                                 Debugger("hammer_update_inode2");
891                         }
892                         if (error == 0) {
893                                 ip->flags |= HAMMER_INODE_DELONDISK;
894                         }
895                         if (cursor->node)
896                                 hammer_cache_node(&ip->cache[0], cursor->node);
897                 }
898                 if (error == EDEADLK) {
899                         hammer_done_cursor(cursor);
900                         error = hammer_init_cursor(trans, cursor,
901                                                    &ip->cache[0], ip);
902                         if (hammer_debug_inode)
903                                 kprintf("IPDED %p %d\n", ip, error);
904                         if (error == 0)
905                                 goto retry;
906                 }
907         }
908
909         /*
910          * Ok, write out the initial record or a new record (after deleting
911          * the old one), unless the DELETED flag is set.  This routine will
912          * clear DELONDISK if it writes out a record.
913          *
914          * Update our inode statistics if this is the first application of
915          * the inode on-disk.
916          */
917         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED) == 0) {
918                 /*
919                  * Generate a record and write it to the media
920                  */
921                 record = hammer_alloc_mem_record(ip, 0);
922                 record->type = HAMMER_MEM_RECORD_INODE;
923                 record->flush_state = HAMMER_FST_FLUSH;
924                 record->leaf = ip->sync_ino_leaf;
925                 record->leaf.base.create_tid = trans->tid;
926                 record->leaf.data_len = sizeof(ip->sync_ino_data);
927                 record->leaf.create_ts = trans->time32;
928                 record->data = (void *)&ip->sync_ino_data;
929                 record->flags |= HAMMER_RECF_INTERLOCK_BE;
930
931                 /*
932                  * If this flag is set we cannot sync the new file size
933                  * because we haven't finished related truncations.  The
934                  * inode will be flushed in another flush group to finish
935                  * the job.
936                  */
937                 if ((ip->flags & HAMMER_INODE_WOULDBLOCK) &&
938                     ip->sync_ino_data.size != ip->ino_data.size) {
939                         redirty = 1;
940                         ip->sync_ino_data.size = ip->ino_data.size;
941                 } else {
942                         redirty = 0;
943                 }
944
945                 for (;;) {
946                         error = hammer_ip_sync_record_cursor(cursor, record);
947                         if (hammer_debug_inode)
948                                 kprintf("GENREC %p rec %08x %d\n",      
949                                         ip, record->flags, error);
950                         if (error != EDEADLK)
951                                 break;
952                         hammer_done_cursor(cursor);
953                         error = hammer_init_cursor(trans, cursor,
954                                                    &ip->cache[0], ip);
955                         if (hammer_debug_inode)
956                                 kprintf("GENREC reinit %d\n", error);
957                         if (error)
958                                 break;
959                 }
960                 if (error) {
961                         kprintf("error %d\n", error);
962                         Debugger("hammer_update_inode3");
963                 }
964
965                 /*
966                  * The record isn't managed by the inode's record tree,
967                  * destroy it whether we succeed or fail.
968                  */
969                 record->flags &= ~HAMMER_RECF_INTERLOCK_BE;
970                 record->flags |= HAMMER_RECF_DELETED_FE;
971                 record->flush_state = HAMMER_FST_IDLE;
972                 hammer_rel_mem_record(record);
973
974                 /*
975                  * Finish up.
976                  */
977                 if (error == 0) {
978                         if (hammer_debug_inode)
979                                 kprintf("CLEANDELOND %p %08x\n", ip, ip->flags);
980                         ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
981                                             HAMMER_INODE_ATIME |
982                                             HAMMER_INODE_MTIME);
983                         ip->flags &= ~HAMMER_INODE_DELONDISK;
984                         if (redirty)
985                                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
986
987                         /*
988                          * Root volume count of inodes
989                          */
990                         if ((ip->flags & HAMMER_INODE_ONDISK) == 0) {
991                                 hammer_modify_volume_field(trans,
992                                                            trans->rootvol,
993                                                            vol0_stat_inodes);
994                                 ++ip->hmp->rootvol->ondisk->vol0_stat_inodes;
995                                 hammer_modify_volume_done(trans->rootvol);
996                                 ip->flags |= HAMMER_INODE_ONDISK;
997                                 if (hammer_debug_inode)
998                                         kprintf("NOWONDISK %p\n", ip);
999                         }
1000                 }
1001         }
1002
1003         /*
1004          * If the inode has been destroyed, clean out any left-over flags
1005          * that may have been set by the frontend.
1006          */
1007         if (error == 0 && (ip->flags & HAMMER_INODE_DELETED)) { 
1008                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY |
1009                                     HAMMER_INODE_ATIME |
1010                                     HAMMER_INODE_MTIME);
1011         }
1012         return(error);
1013 }
1014
1015 /*
1016  * Update only the itimes fields.
1017  *
1018  * ATIME can be updated without generating any UNDO.  MTIME is updated
1019  * with UNDO so it is guaranteed to be synchronized properly in case of
1020  * a crash.
1021  *
1022  * Neither field is included in the B-Tree leaf element's CRC, which is how
1023  * we can get away with updating ATIME the way we do.
1024  */
1025 static int
1026 hammer_update_itimes(hammer_cursor_t cursor, hammer_inode_t ip)
1027 {
1028         hammer_transaction_t trans = cursor->trans;
1029         int error;
1030
1031 retry:
1032         if ((ip->flags & (HAMMER_INODE_ONDISK|HAMMER_INODE_DELONDISK)) !=
1033             HAMMER_INODE_ONDISK) {
1034                 return(0);
1035         }
1036
1037         hammer_normalize_cursor(cursor);
1038         cursor->key_beg.localization = ip->obj_localization + 
1039                                        HAMMER_LOCALIZE_INODE;
1040         cursor->key_beg.obj_id = ip->obj_id;
1041         cursor->key_beg.key = 0;
1042         cursor->key_beg.create_tid = 0;
1043         cursor->key_beg.delete_tid = 0;
1044         cursor->key_beg.rec_type = HAMMER_RECTYPE_INODE;
1045         cursor->key_beg.obj_type = 0;
1046         cursor->asof = ip->obj_asof;
1047         cursor->flags &= ~HAMMER_CURSOR_INITMASK;
1048         cursor->flags |= HAMMER_CURSOR_ASOF;
1049         cursor->flags |= HAMMER_CURSOR_GET_LEAF;
1050         cursor->flags |= HAMMER_CURSOR_GET_DATA;
1051         cursor->flags |= HAMMER_CURSOR_BACKEND;
1052
1053         error = hammer_btree_lookup(cursor);
1054         if (error) {
1055                 kprintf("error %d\n", error);
1056                 Debugger("hammer_update_itimes1");
1057         }
1058         if (error == 0) {
1059                 hammer_cache_node(&ip->cache[0], cursor->node);
1060                 if (ip->sync_flags & HAMMER_INODE_MTIME) {
1061                         /*
1062                          * Updating MTIME requires an UNDO.  Just cover
1063                          * both atime and mtime.
1064                          */
1065                         hammer_modify_buffer(trans, cursor->data_buffer,
1066                                      HAMMER_ITIMES_BASE(&cursor->data->inode),
1067                                      HAMMER_ITIMES_BYTES);
1068                         cursor->data->inode.atime = ip->sync_ino_data.atime;
1069                         cursor->data->inode.mtime = ip->sync_ino_data.mtime;
1070                         hammer_modify_buffer_done(cursor->data_buffer);
1071                 } else if (ip->sync_flags & HAMMER_INODE_ATIME) {
1072                         /*
1073                          * Updating atime only can be done in-place with
1074                          * no UNDO.
1075                          */
1076                         hammer_modify_buffer(trans, cursor->data_buffer,
1077                                              NULL, 0);
1078                         cursor->data->inode.atime = ip->sync_ino_data.atime;
1079                         hammer_modify_buffer_done(cursor->data_buffer);
1080                 }
1081                 ip->sync_flags &= ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME);
1082         }
1083         if (error == EDEADLK) {
1084                 hammer_done_cursor(cursor);
1085                 error = hammer_init_cursor(trans, cursor,
1086                                            &ip->cache[0], ip);
1087                 if (error == 0)
1088                         goto retry;
1089         }
1090         return(error);
1091 }
1092
1093 /*
1094  * Release a reference on an inode, flush as requested.
1095  *
1096  * On the last reference we queue the inode to the flusher for its final
1097  * disposition.
1098  */
1099 void
1100 hammer_rel_inode(struct hammer_inode *ip, int flush)
1101 {
1102         hammer_mount_t hmp = ip->hmp;
1103
1104         /*
1105          * Handle disposition when dropping the last ref.
1106          */
1107         for (;;) {
1108                 if (ip->lock.refs == 1) {
1109                         /*
1110                          * Determine whether on-disk action is needed for
1111                          * the inode's final disposition.
1112                          */
1113                         KKASSERT(ip->vp == NULL);
1114                         hammer_inode_unloadable_check(ip, 0);
1115                         if (ip->flags & HAMMER_INODE_MODMASK) {
1116                                 if (hmp->rsv_inodes > desiredvnodes) {
1117                                         hammer_flush_inode(ip,
1118                                                            HAMMER_FLUSH_SIGNAL);
1119                                 } else {
1120                                         hammer_flush_inode(ip, 0);
1121                                 }
1122                         } else if (ip->lock.refs == 1) {
1123                                 hammer_unload_inode(ip);
1124                                 break;
1125                         }
1126                 } else {
1127                         if (flush)
1128                                 hammer_flush_inode(ip, 0);
1129
1130                         /*
1131                          * The inode still has multiple refs, try to drop
1132                          * one ref.
1133                          */
1134                         KKASSERT(ip->lock.refs >= 1);
1135                         if (ip->lock.refs > 1) {
1136                                 hammer_unref(&ip->lock);
1137                                 break;
1138                         }
1139                 }
1140         }
1141 }
1142
1143 /*
1144  * Unload and destroy the specified inode.  Must be called with one remaining
1145  * reference.  The reference is disposed of.
1146  *
1147  * This can only be called in the context of the flusher.
1148  */
1149 static int
1150 hammer_unload_inode(struct hammer_inode *ip)
1151 {
1152         hammer_mount_t hmp = ip->hmp;
1153
1154         KASSERT(ip->lock.refs == 1,
1155                 ("hammer_unload_inode: %d refs\n", ip->lock.refs));
1156         KKASSERT(ip->vp == NULL);
1157         KKASSERT(ip->flush_state == HAMMER_FST_IDLE);
1158         KKASSERT(ip->cursor_ip_refs == 0);
1159         KKASSERT(ip->lock.lockcount == 0);
1160         KKASSERT((ip->flags & HAMMER_INODE_MODMASK) == 0);
1161
1162         KKASSERT(RB_EMPTY(&ip->rec_tree));
1163         KKASSERT(TAILQ_EMPTY(&ip->target_list));
1164
1165         RB_REMOVE(hammer_ino_rb_tree, &hmp->rb_inos_root, ip);
1166
1167         hammer_free_inode(ip);
1168         return(0);
1169 }
1170
1171 /*
1172  * Called on mount -u when switching from RW to RO or vise-versa.  Adjust
1173  * the read-only flag for cached inodes.
1174  *
1175  * This routine is called from a RB_SCAN().
1176  */
1177 int
1178 hammer_reload_inode(hammer_inode_t ip, void *arg __unused)
1179 {
1180         hammer_mount_t hmp = ip->hmp;
1181
1182         if (hmp->ronly || hmp->asof != HAMMER_MAX_TID)
1183                 ip->flags |= HAMMER_INODE_RO;
1184         else
1185                 ip->flags &= ~HAMMER_INODE_RO;
1186         return(0);
1187 }
1188
1189 /*
1190  * A transaction has modified an inode, requiring updates as specified by
1191  * the passed flags.
1192  *
1193  * HAMMER_INODE_DDIRTY: Inode data has been updated
1194  * HAMMER_INODE_XDIRTY: Dirty in-memory records
1195  * HAMMER_INODE_BUFS:   Dirty buffer cache buffers
1196  * HAMMER_INODE_DELETED: Inode record/data must be deleted
1197  * HAMMER_INODE_ATIME/MTIME: mtime/atime has been updated
1198  */
1199 void
1200 hammer_modify_inode(hammer_inode_t ip, int flags)
1201 {
1202         KKASSERT(ip->hmp->ronly == 0 ||
1203                   (flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY | 
1204                             HAMMER_INODE_BUFS | HAMMER_INODE_DELETED |
1205                             HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) == 0);
1206         if ((ip->flags & HAMMER_INODE_RSV_INODES) == 0) {
1207                 ip->flags |= HAMMER_INODE_RSV_INODES;
1208                 ++ip->hmp->rsv_inodes;
1209         }
1210
1211         ip->flags |= flags;
1212 }
1213
1214 /*
1215  * Request that an inode be flushed.  This whole mess cannot block and may
1216  * recurse (if not synchronous).  Once requested HAMMER will attempt to
1217  * actively flush the inode until the flush can be done.
1218  *
1219  * The inode may already be flushing, or may be in a setup state.  We can
1220  * place the inode in a flushing state if it is currently idle and flag it
1221  * to reflush if it is currently flushing.
1222  *
1223  * If the HAMMER_FLUSH_SYNCHRONOUS flag is specified we will attempt to
1224  * flush the indoe synchronously using the caller's context.
1225  */
1226 void
1227 hammer_flush_inode(hammer_inode_t ip, int flags)
1228 {
1229         int good;
1230
1231         /*
1232          * Trivial 'nothing to flush' case.  If the inode is ina SETUP
1233          * state we have to put it back into an IDLE state so we can
1234          * drop the extra ref.
1235          */
1236         if ((ip->flags & HAMMER_INODE_MODMASK) == 0) {
1237                 if (ip->flush_state == HAMMER_FST_SETUP) {
1238                         ip->flush_state = HAMMER_FST_IDLE;
1239                         hammer_rel_inode(ip, 0);
1240                 }
1241                 return;
1242         }
1243
1244         /*
1245          * Our flush action will depend on the current state.
1246          */
1247         switch(ip->flush_state) {
1248         case HAMMER_FST_IDLE:
1249                 /*
1250                  * We have no dependancies and can flush immediately.  Some
1251                  * our children may not be flushable so we have to re-test
1252                  * with that additional knowledge.
1253                  */
1254                 hammer_flush_inode_core(ip, flags);
1255                 break;
1256         case HAMMER_FST_SETUP:
1257                 /*
1258                  * Recurse upwards through dependancies via target_list
1259                  * and start their flusher actions going if possible.
1260                  *
1261                  * 'good' is our connectivity.  -1 means we have none and
1262                  * can't flush, 0 means there weren't any dependancies, and
1263                  * 1 means we have good connectivity.
1264                  */
1265                 good = hammer_setup_parent_inodes(ip);
1266
1267                 /*
1268                  * We can continue if good >= 0.  Determine how many records
1269                  * under our inode can be flushed (and mark them).
1270                  */
1271                 if (good >= 0) {
1272                         hammer_flush_inode_core(ip, flags);
1273                 } else {
1274                         ip->flags |= HAMMER_INODE_REFLUSH;
1275                         if (flags & HAMMER_FLUSH_SIGNAL) {
1276                                 ip->flags |= HAMMER_INODE_RESIGNAL;
1277                                 hammer_flusher_async(ip->hmp);
1278                         }
1279                 }
1280                 break;
1281         default:
1282                 /*
1283                  * We are already flushing, flag the inode to reflush
1284                  * if needed after it completes its current flush.
1285                  */
1286                 if ((ip->flags & HAMMER_INODE_REFLUSH) == 0)
1287                         ip->flags |= HAMMER_INODE_REFLUSH;
1288                 if (flags & HAMMER_FLUSH_SIGNAL) {
1289                         ip->flags |= HAMMER_INODE_RESIGNAL;
1290                         hammer_flusher_async(ip->hmp);
1291                 }
1292                 break;
1293         }
1294 }
1295
1296 /*
1297  * Scan ip->target_list, which is a list of records owned by PARENTS to our
1298  * ip which reference our ip.
1299  *
1300  * XXX This is a huge mess of recursive code, but not one bit of it blocks
1301  *     so for now do not ref/deref the structures.  Note that if we use the
1302  *     ref/rel code later, the rel CAN block.
1303  */
1304 static int
1305 hammer_setup_parent_inodes(hammer_inode_t ip)
1306 {
1307         hammer_record_t depend;
1308 #if 0
1309         hammer_record_t next;
1310         hammer_inode_t  pip;
1311 #endif
1312         int good;
1313         int r;
1314
1315         good = 0;
1316         TAILQ_FOREACH(depend, &ip->target_list, target_entry) {
1317                 r = hammer_setup_parent_inodes_helper(depend);
1318                 KKASSERT(depend->target_ip == ip);
1319                 if (r < 0 && good == 0)
1320                         good = -1;
1321                 if (r > 0)
1322                         good = 1;
1323         }
1324         return(good);
1325
1326 #if 0
1327 retry:
1328         good = 0;
1329         next = TAILQ_FIRST(&ip->target_list);
1330         if (next) {
1331                 hammer_ref(&next->lock);
1332                 hammer_ref(&next->ip->lock);
1333         }
1334         while ((depend = next) != NULL) {
1335                 if (depend->target_ip == NULL) {
1336                         pip = depend->ip;
1337                         hammer_rel_mem_record(depend);
1338                         hammer_rel_inode(pip, 0);
1339                         goto retry;
1340                 }
1341                 KKASSERT(depend->target_ip == ip);
1342                 next = TAILQ_NEXT(depend, target_entry);
1343                 if (next) {
1344                         hammer_ref(&next->lock);
1345                         hammer_ref(&next->ip->lock);
1346                 }
1347                 r = hammer_setup_parent_inodes_helper(depend);
1348                 if (r < 0 && good == 0)
1349                         good = -1;
1350                 if (r > 0)
1351                         good = 1;
1352                 pip = depend->ip;
1353                 hammer_rel_mem_record(depend);
1354                 hammer_rel_inode(pip, 0);
1355         }
1356         return(good);
1357 #endif
1358 }
1359
1360 /*
1361  * This helper function takes a record representing the dependancy between
1362  * the parent inode and child inode.
1363  *
1364  * record->ip           = parent inode
1365  * record->target_ip    = child inode
1366  * 
1367  * We are asked to recurse upwards and convert the record from SETUP
1368  * to FLUSH if possible.
1369  *
1370  * Return 1 if the record gives us connectivity
1371  *
1372  * Return 0 if the record is not relevant 
1373  *
1374  * Return -1 if we can't resolve the dependancy and there is no connectivity.
1375  */
1376 static int
1377 hammer_setup_parent_inodes_helper(hammer_record_t record)
1378 {
1379         hammer_mount_t hmp;
1380         hammer_inode_t pip;
1381         int good;
1382
1383         KKASSERT(record->flush_state != HAMMER_FST_IDLE);
1384         pip = record->ip;
1385         hmp = pip->hmp;
1386
1387         /*
1388          * If the record is already flushing, is it in our flush group?
1389          *
1390          * If it is in our flush group but it is a general record or a 
1391          * delete-on-disk, it does not improve our connectivity (return 0),
1392          * and if the target inode is not trying to destroy itself we can't
1393          * allow the operation yet anyway (the second return -1).
1394          */
1395         if (record->flush_state == HAMMER_FST_FLUSH) {
1396                 if (record->flush_group != hmp->flusher.next) {
1397                         pip->flags |= HAMMER_INODE_REFLUSH;
1398                         return(-1);
1399                 }
1400                 if (record->type == HAMMER_MEM_RECORD_ADD)
1401                         return(1);
1402                 /* GENERAL or DEL */
1403                 return(0);
1404         }
1405
1406         /*
1407          * It must be a setup record.  Try to resolve the setup dependancies
1408          * by recursing upwards so we can place ip on the flush list.
1409          */
1410         KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1411
1412         good = hammer_setup_parent_inodes(pip);
1413
1414         /*
1415          * We can't flush ip because it has no connectivity (XXX also check
1416          * nlinks for pre-existing connectivity!).  Flag it so any resolution
1417          * recurses back down.
1418          */
1419         if (good < 0) {
1420                 pip->flags |= HAMMER_INODE_REFLUSH;
1421                 return(good);
1422         }
1423
1424         /*
1425          * We are go, place the parent inode in a flushing state so we can
1426          * place its record in a flushing state.  Note that the parent
1427          * may already be flushing.  The record must be in the same flush
1428          * group as the parent.
1429          */
1430         if (pip->flush_state != HAMMER_FST_FLUSH)
1431                 hammer_flush_inode_core(pip, HAMMER_FLUSH_RECURSION);
1432         KKASSERT(pip->flush_state == HAMMER_FST_FLUSH);
1433         KKASSERT(record->flush_state == HAMMER_FST_SETUP);
1434
1435 #if 0
1436         if (record->type == HAMMER_MEM_RECORD_DEL &&
1437             (record->target_ip->flags & (HAMMER_INODE_DELETED|HAMMER_INODE_DELONDISK)) == 0) {
1438                 /*
1439                  * Regardless of flushing state we cannot sync this path if the
1440                  * record represents a delete-on-disk but the target inode
1441                  * is not ready to sync its own deletion.
1442                  *
1443                  * XXX need to count effective nlinks to determine whether
1444                  * the flush is ok, otherwise removing a hardlink will
1445                  * just leave the DEL record to rot.
1446                  */
1447                 record->target_ip->flags |= HAMMER_INODE_REFLUSH;
1448                 return(-1);
1449         } else
1450 #endif
1451         if (pip->flush_group == pip->hmp->flusher.next) {
1452                 /*
1453                  * This is the record we wanted to synchronize.  If the
1454                  * record went into a flush state while we blocked it 
1455                  * had better be in the correct flush group.
1456                  */
1457                 if (record->flush_state != HAMMER_FST_FLUSH) {
1458                         record->flush_state = HAMMER_FST_FLUSH;
1459                         record->flush_group = pip->flush_group;
1460                         hammer_ref(&record->lock);
1461                 } else {
1462                         KKASSERT(record->flush_group == pip->flush_group);
1463                 }
1464                 if (record->type == HAMMER_MEM_RECORD_ADD)
1465                         return(1);
1466
1467                 /*
1468                  * A general or delete-on-disk record does not contribute
1469                  * to our visibility.  We can still flush it, however.
1470                  */
1471                 return(0);
1472         } else {
1473                 /*
1474                  * We couldn't resolve the dependancies, request that the
1475                  * inode be flushed when the dependancies can be resolved.
1476                  */
1477                 pip->flags |= HAMMER_INODE_REFLUSH;
1478                 return(-1);
1479         }
1480 }
1481
1482 /*
1483  * This is the core routine placing an inode into the FST_FLUSH state.
1484  */
1485 static void
1486 hammer_flush_inode_core(hammer_inode_t ip, int flags)
1487 {
1488         int go_count;
1489
1490         /*
1491          * Set flush state and prevent the flusher from cycling into
1492          * the next flush group.  Do not place the ip on the list yet.
1493          * Inodes not in the idle state get an extra reference.
1494          */
1495         KKASSERT(ip->flush_state != HAMMER_FST_FLUSH);
1496         if (ip->flush_state == HAMMER_FST_IDLE)
1497                 hammer_ref(&ip->lock);
1498         ip->flush_state = HAMMER_FST_FLUSH;
1499         ip->flush_group = ip->hmp->flusher.next;
1500         ++ip->hmp->flusher.group_lock;
1501         ++ip->hmp->count_iqueued;
1502         ++hammer_count_iqueued;
1503
1504         /*
1505          * We need to be able to vfsync/truncate from the backend.
1506          */
1507         KKASSERT((ip->flags & HAMMER_INODE_VHELD) == 0);
1508         if (ip->vp && (ip->vp->v_flag & VINACTIVE) == 0) {
1509                 ip->flags |= HAMMER_INODE_VHELD;
1510                 vref(ip->vp);
1511         }
1512
1513         /*
1514          * Figure out how many in-memory records we can actually flush
1515          * (not including inode meta-data, buffers, etc).
1516          *
1517          * Do not add new records to the flush if this is a recursion or
1518          * if we must still complete a flush from the previous flush cycle.
1519          */
1520         if (flags & HAMMER_FLUSH_RECURSION) {
1521                 go_count = 1;
1522         } else if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1523                 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1524                                    hammer_syncgrp_child_callback, NULL);
1525                 go_count = 1;
1526         } else {
1527                 go_count = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
1528                                    hammer_setup_child_callback, NULL);
1529         }
1530
1531         /*
1532          * This is a more involved test that includes go_count.  If we
1533          * can't flush, flag the inode and return.  If go_count is 0 we
1534          * were are unable to flush any records in our rec_tree and
1535          * must ignore the XDIRTY flag.
1536          */
1537         if (go_count == 0) {
1538                 if ((ip->flags & HAMMER_INODE_MODMASK_NOXDIRTY) == 0) {
1539                         ip->flags |= HAMMER_INODE_REFLUSH;
1540
1541                         --ip->hmp->count_iqueued;
1542                         --hammer_count_iqueued;
1543
1544                         ip->flush_state = HAMMER_FST_SETUP;
1545                         if (ip->flags & HAMMER_INODE_VHELD) {
1546                                 ip->flags &= ~HAMMER_INODE_VHELD;
1547                                 vrele(ip->vp);
1548                         }
1549                         if (flags & HAMMER_FLUSH_SIGNAL) {
1550                                 ip->flags |= HAMMER_INODE_RESIGNAL;
1551                                 hammer_flusher_async(ip->hmp);
1552                         }
1553                         if (--ip->hmp->flusher.group_lock == 0)
1554                                 wakeup(&ip->hmp->flusher.group_lock);
1555                         return;
1556                 }
1557         }
1558
1559         /*
1560          * Snapshot the state of the inode for the backend flusher.
1561          *
1562          * We continue to retain save_trunc_off even when all truncations
1563          * have been resolved as an optimization to determine if we can
1564          * skip the B-Tree lookup for overwrite deletions.
1565          *
1566          * NOTE: The DELETING flag is a mod flag, but it is also sticky,
1567          * and stays in ip->flags.  Once set, it stays set until the
1568          * inode is destroyed.
1569          *
1570          * NOTE: If a truncation from a previous flush cycle had to be
1571          * continued into this one, the TRUNCATED flag will still be
1572          * set in sync_flags as will WOULDBLOCK.  When this occurs
1573          * we CANNOT safely integrate a new truncation from the front-end
1574          * because there may be data records in-memory assigned a flush
1575          * state from the previous cycle that are supposed to be flushed
1576          * before the next frontend truncation.
1577          */
1578         if ((ip->flags & (HAMMER_INODE_TRUNCATED | HAMMER_INODE_WOULDBLOCK)) ==
1579             HAMMER_INODE_TRUNCATED) {
1580                 KKASSERT((ip->sync_flags & HAMMER_INODE_TRUNCATED) == 0);
1581                 ip->sync_trunc_off = ip->trunc_off;
1582                 ip->trunc_off = 0x7FFFFFFFFFFFFFFFLL;
1583                 ip->flags &= ~HAMMER_INODE_TRUNCATED;
1584                 ip->sync_flags |= HAMMER_INODE_TRUNCATED;
1585
1586                 /*
1587                  * The save_trunc_off used to cache whether the B-Tree
1588                  * holds any records past that point is not used until
1589                  * after the truncation has succeeded, so we can safely
1590                  * set it now.
1591                  */
1592                 if (ip->save_trunc_off > ip->sync_trunc_off)
1593                         ip->save_trunc_off = ip->sync_trunc_off;
1594         }
1595         ip->sync_flags |= (ip->flags & HAMMER_INODE_MODMASK &
1596                            ~HAMMER_INODE_TRUNCATED);
1597         ip->sync_ino_leaf = ip->ino_leaf;
1598         ip->sync_ino_data = ip->ino_data;
1599         ip->flags &= ~HAMMER_INODE_MODMASK | HAMMER_INODE_TRUNCATED;
1600 #ifdef DEBUG_TRUNCATE
1601         if ((ip->sync_flags & HAMMER_INODE_TRUNCATED) && ip == HammerTruncIp)
1602                 kprintf("truncateS %016llx\n", ip->sync_trunc_off);
1603 #endif
1604
1605         /*
1606          * The flusher list inherits our inode and reference.
1607          */
1608         TAILQ_INSERT_TAIL(&ip->hmp->flush_list, ip, flush_entry);
1609         if (--ip->hmp->flusher.group_lock == 0)
1610                 wakeup(&ip->hmp->flusher.group_lock);
1611
1612         if (flags & HAMMER_FLUSH_SIGNAL) {
1613                 hammer_flusher_async(ip->hmp);
1614         }
1615 }
1616
1617 /*
1618  * Callback for scan of ip->rec_tree.  Try to include each record in our
1619  * flush.  ip->flush_group has been set but the inode has not yet been
1620  * moved into a flushing state.
1621  *
1622  * If we get stuck on a record we have to set HAMMER_INODE_REFLUSH on
1623  * both inodes.
1624  *
1625  * We return 1 for any record placed or found in FST_FLUSH, which prevents
1626  * the caller from shortcutting the flush.
1627  */
1628 static int
1629 hammer_setup_child_callback(hammer_record_t rec, void *data)
1630 {
1631         hammer_inode_t target_ip;
1632         hammer_inode_t ip;
1633         int r;
1634
1635         /*
1636          * Deleted records are ignored.  Note that the flush detects deleted
1637          * front-end records at multiple points to deal with races.  This is
1638          * just the first line of defense.  The only time DELETED_FE cannot
1639          * be set is when HAMMER_RECF_INTERLOCK_BE is set. 
1640          *
1641          * Don't get confused between record deletion and, say, directory
1642          * entry deletion.  The deletion of a directory entry that is on
1643          * the media has nothing to do with the record deletion flags.
1644          *
1645          * The flush_group for a record already in a flush state must
1646          * be updated.  This case can only occur if the inode deleting
1647          * too many records had to be moved to the next flush group.
1648          */
1649         if (rec->flags & (HAMMER_RECF_DELETED_FE|HAMMER_RECF_DELETED_BE)) {
1650                 if (rec->flush_state == HAMMER_FST_FLUSH) {
1651                         KKASSERT(rec->ip->flags & HAMMER_INODE_WOULDBLOCK);
1652                         rec->flush_group = rec->ip->flush_group;
1653                         r = 1;
1654                 } else {
1655                         r = 0;
1656                 }
1657                 return(r);
1658         }
1659
1660         /*
1661          * If the record is in an idle state it has no dependancies and
1662          * can be flushed.
1663          */
1664         ip = rec->ip;
1665         r = 0;
1666
1667         switch(rec->flush_state) {
1668         case HAMMER_FST_IDLE:
1669                 /*
1670                  * Record has no setup dependancy, we can flush it.
1671                  */
1672                 KKASSERT(rec->target_ip == NULL);
1673                 rec->flush_state = HAMMER_FST_FLUSH;
1674                 rec->flush_group = ip->flush_group;
1675                 hammer_ref(&rec->lock);
1676                 r = 1;
1677                 break;
1678         case HAMMER_FST_SETUP:
1679                 /*
1680                  * Record has a setup dependancy.  Try to include the
1681                  * target ip in the flush. 
1682                  *
1683                  * We have to be careful here, if we do not do the right
1684                  * thing we can lose track of dirty inodes and the system
1685                  * will lockup trying to allocate buffers.
1686                  */
1687                 target_ip = rec->target_ip;
1688                 KKASSERT(target_ip != NULL);
1689                 KKASSERT(target_ip->flush_state != HAMMER_FST_IDLE);
1690                 if (target_ip->flush_state == HAMMER_FST_FLUSH) {
1691                         /*
1692                          * If the target IP is already flushing in our group
1693                          * we are golden, otherwise make sure the target
1694                          * reflushes.
1695                          */
1696                         if (target_ip->flush_group == ip->flush_group) {
1697                                 rec->flush_state = HAMMER_FST_FLUSH;
1698                                 rec->flush_group = ip->flush_group;
1699                                 hammer_ref(&rec->lock);
1700                                 r = 1;
1701                         } else {
1702                                 target_ip->flags |= HAMMER_INODE_REFLUSH;
1703                         }
1704                 } else if (rec->type == HAMMER_MEM_RECORD_ADD) {
1705                         /*
1706                          * If the target IP is not flushing we can force
1707                          * it to flush, even if it is unable to write out
1708                          * any of its own records we have at least one in
1709                          * hand that we CAN deal with.
1710                          */
1711                         rec->flush_state = HAMMER_FST_FLUSH;
1712                         rec->flush_group = ip->flush_group;
1713                         hammer_ref(&rec->lock);
1714                         hammer_flush_inode_core(target_ip,
1715                                                 HAMMER_FLUSH_RECURSION);
1716                         r = 1;
1717                 } else {
1718                         /*
1719                          * General or delete-on-disk record.
1720                          *
1721                          * XXX this needs help.  If a delete-on-disk we could
1722                          * disconnect the target.  If the target has its own
1723                          * dependancies they really need to be flushed.
1724                          *
1725                          * XXX
1726                          */
1727                         rec->flush_state = HAMMER_FST_FLUSH;
1728                         rec->flush_group = ip->flush_group;
1729                         hammer_ref(&rec->lock);
1730                         hammer_flush_inode_core(target_ip,
1731                                                 HAMMER_FLUSH_RECURSION);
1732                         r = 1;
1733                 }
1734                 break;
1735         case HAMMER_FST_FLUSH:
1736                 /* 
1737                  * If the WOULDBLOCK flag is set records may have been left
1738                  * over from a previous flush attempt and should be moved
1739                  * to the current flush group.  If it is not set then all
1740                  * such records had better have been flushed already or
1741                  * already associated with the current flush group.
1742                  */
1743                 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1744                         rec->flush_group = ip->flush_group;
1745                 } else {
1746                         KKASSERT(rec->flush_group == ip->flush_group);
1747                 }
1748                 r = 1;
1749                 break;
1750         }
1751         return(r);
1752 }
1753
1754 /*
1755  * This version just moves records already in a flush state to the new
1756  * flush group and that is it.
1757  */
1758 static int
1759 hammer_syncgrp_child_callback(hammer_record_t rec, void *data)
1760 {
1761         hammer_inode_t ip = rec->ip;
1762
1763         switch(rec->flush_state) {
1764         case HAMMER_FST_FLUSH:
1765                 if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1766                         rec->flush_group = ip->flush_group;
1767                 } else {
1768                         KKASSERT(rec->flush_group == ip->flush_group);
1769                 }
1770                 break;
1771         default:
1772                 break;
1773         }
1774         return(0);
1775 }
1776
1777 /*
1778  * Wait for a previously queued flush to complete.  Not only do we need to
1779  * wait for the inode to sync out, we also may have to run the flusher again
1780  * to get it past the UNDO position pertaining to the flush so a crash does
1781  * not 'undo' our flush.
1782  */
1783 void
1784 hammer_wait_inode(hammer_inode_t ip)
1785 {
1786         hammer_mount_t hmp = ip->hmp;
1787         int sync_group;
1788         int waitcount;
1789
1790         sync_group = ip->flush_group;
1791         waitcount = (ip->flags & HAMMER_INODE_REFLUSH) ? 2 : 1;
1792
1793         if (ip->flush_state == HAMMER_FST_SETUP) {
1794                 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1795         }
1796         /* XXX can we make this != FST_IDLE ? check SETUP depends */
1797         while (ip->flush_state == HAMMER_FST_FLUSH &&
1798                (ip->flush_group - sync_group) < waitcount) {
1799                 ip->flags |= HAMMER_INODE_FLUSHW;
1800                 tsleep(&ip->flags, 0, "hmrwin", 0);
1801         }
1802         while (hmp->flusher.done - sync_group < waitcount) {
1803                 kprintf("Y");
1804                 hammer_flusher_sync(hmp);
1805         }
1806 }
1807
1808 /*
1809  * Called by the backend code when a flush has been completed.
1810  * The inode has already been removed from the flush list.
1811  *
1812  * A pipelined flush can occur, in which case we must re-enter the
1813  * inode on the list and re-copy its fields.
1814  */
1815 void
1816 hammer_flush_inode_done(hammer_inode_t ip)
1817 {
1818         hammer_mount_t hmp;
1819         int dorel;
1820
1821         KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1822
1823         hmp = ip->hmp;
1824
1825         /*
1826          * Merge left-over flags back into the frontend and fix the state.
1827          * Incomplete truncations are retained by the backend.
1828          */
1829         ip->flags |= ip->sync_flags & ~HAMMER_INODE_TRUNCATED;
1830         ip->sync_flags &= HAMMER_INODE_TRUNCATED;
1831
1832         /*
1833          * The backend may have adjusted nlinks, so if the adjusted nlinks
1834          * does not match the fronttend set the frontend's RDIRTY flag again.
1835          */
1836         if (ip->ino_data.nlinks != ip->sync_ino_data.nlinks)
1837                 ip->flags |= HAMMER_INODE_DDIRTY;
1838
1839         /*
1840          * Fix up the dirty buffer status.
1841          */
1842         if (ip->vp && RB_ROOT(&ip->vp->v_rbdirty_tree)) {
1843                 ip->flags |= HAMMER_INODE_BUFS;
1844         }
1845
1846         /*
1847          * Re-set the XDIRTY flag if some of the inode's in-memory records
1848          * could not be flushed.
1849          */
1850         KKASSERT((RB_EMPTY(&ip->rec_tree) &&
1851                   (ip->flags & HAMMER_INODE_XDIRTY) == 0) ||
1852                  (!RB_EMPTY(&ip->rec_tree) &&
1853                   (ip->flags & HAMMER_INODE_XDIRTY) != 0));
1854
1855         /*
1856          * Do not lose track of inodes which no longer have vnode
1857          * assocations, otherwise they may never get flushed again.
1858          */
1859         if ((ip->flags & HAMMER_INODE_MODMASK) && ip->vp == NULL)
1860                 ip->flags |= HAMMER_INODE_REFLUSH;
1861
1862         /*
1863          * Clean up the vnode ref
1864          */
1865         if (ip->flags & HAMMER_INODE_VHELD) {
1866                 ip->flags &= ~HAMMER_INODE_VHELD;
1867                 vrele(ip->vp);
1868         }
1869
1870         /*
1871          * Adjust flush_state.  The target state (idle or setup) shouldn't
1872          * be terribly important since we will reflush if we really need
1873          * to do anything.
1874          *
1875          * If the WOULDBLOCK flag is set we must re-flush immediately
1876          * to continue a potentially large deletion.  The flag also causes
1877          * the hammer_setup_child_callback() to move records in the old
1878          * flush group to the new one.
1879          */
1880         if (ip->flags & HAMMER_INODE_WOULDBLOCK) {
1881                 ip->flush_state = HAMMER_FST_IDLE;
1882                 hammer_flush_inode_core(ip, HAMMER_FLUSH_SIGNAL);
1883                 ip->flags &= ~HAMMER_INODE_WOULDBLOCK;
1884                 dorel = 1;
1885         } else if (TAILQ_EMPTY(&ip->target_list) && RB_EMPTY(&ip->rec_tree)) {
1886                 ip->flush_state = HAMMER_FST_IDLE;
1887                 dorel = 1;
1888         } else {
1889                 ip->flush_state = HAMMER_FST_SETUP;
1890                 dorel = 0;
1891         }
1892
1893         --hmp->count_iqueued;
1894         --hammer_count_iqueued;
1895
1896         /*
1897          * If the frontend made more changes and requested another flush,
1898          * then try to get it running.
1899          */
1900         if (ip->flags & HAMMER_INODE_REFLUSH) {
1901                 ip->flags &= ~HAMMER_INODE_REFLUSH;
1902                 if (ip->flags & HAMMER_INODE_RESIGNAL) {
1903                         ip->flags &= ~HAMMER_INODE_RESIGNAL;
1904                         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1905                 } else {
1906                         hammer_flush_inode(ip, 0);
1907                 }
1908         }
1909
1910         /*
1911          * If the inode is now clean drop the space reservation.
1912          */
1913         if ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1914             (ip->flags & HAMMER_INODE_RSV_INODES)) {
1915                 ip->flags &= ~HAMMER_INODE_RSV_INODES;
1916                 --hmp->rsv_inodes;
1917         }
1918
1919         /*
1920          * Finally, if the frontend is waiting for a flush to complete,
1921          * wake it up.
1922          */
1923         if (ip->flush_state != HAMMER_FST_FLUSH) {
1924                 if (ip->flags & HAMMER_INODE_FLUSHW) {
1925                         ip->flags &= ~HAMMER_INODE_FLUSHW;
1926                         wakeup(&ip->flags);
1927                 }
1928         }
1929         if (dorel)
1930                 hammer_rel_inode(ip, 0);
1931 }
1932
1933 /*
1934  * Called from hammer_sync_inode() to synchronize in-memory records
1935  * to the media.
1936  */
1937 static int
1938 hammer_sync_record_callback(hammer_record_t record, void *data)
1939 {
1940         hammer_cursor_t cursor = data;
1941         hammer_transaction_t trans = cursor->trans;
1942         int error;
1943
1944         /*
1945          * Skip records that do not belong to the current flush.
1946          */
1947         ++hammer_stats_record_iterations;
1948         if (record->flush_state != HAMMER_FST_FLUSH)
1949                 return(0);
1950
1951 #if 1
1952         if (record->flush_group != record->ip->flush_group) {
1953                 kprintf("sync_record %p ip %p bad flush group %d %d\n", record, record->ip, record->flush_group ,record->ip->flush_group);
1954                 Debugger("blah2");
1955                 return(0);
1956         }
1957 #endif
1958         KKASSERT(record->flush_group == record->ip->flush_group);
1959
1960         /*
1961          * Interlock the record using the BE flag.  Once BE is set the
1962          * frontend cannot change the state of FE.
1963          *
1964          * NOTE: If FE is set prior to us setting BE we still sync the
1965          * record out, but the flush completion code converts it to 
1966          * a delete-on-disk record instead of destroying it.
1967          */
1968         KKASSERT((record->flags & HAMMER_RECF_INTERLOCK_BE) == 0);
1969         record->flags |= HAMMER_RECF_INTERLOCK_BE;
1970
1971         /*
1972          * The backend may have already disposed of the record.
1973          */
1974         if (record->flags & HAMMER_RECF_DELETED_BE) {
1975                 error = 0;
1976                 goto done;
1977         }
1978
1979         /*
1980          * If the whole inode is being deleting all on-disk records will
1981          * be deleted very soon, we can't sync any new records to disk
1982          * because they will be deleted in the same transaction they were
1983          * created in (delete_tid == create_tid), which will assert.
1984          *
1985          * XXX There may be a case with RECORD_ADD with DELETED_FE set
1986          * that we currently panic on.
1987          */
1988         if (record->ip->sync_flags & HAMMER_INODE_DELETING) {
1989                 switch(record->type) {
1990                 case HAMMER_MEM_RECORD_DATA:
1991                         /*
1992                          * We don't have to do anything, if the record was
1993                          * committed the space will have been accounted for
1994                          * in the blockmap.
1995                          */
1996                         /* fall through */
1997                 case HAMMER_MEM_RECORD_GENERAL:
1998                         record->flags |= HAMMER_RECF_DELETED_FE;
1999                         record->flags |= HAMMER_RECF_DELETED_BE;
2000                         error = 0;
2001                         goto done;
2002                 case HAMMER_MEM_RECORD_ADD:
2003                         panic("hammer_sync_record_callback: illegal add "
2004                               "during inode deletion record %p", record);
2005                         break; /* NOT REACHED */
2006                 case HAMMER_MEM_RECORD_INODE:
2007                         panic("hammer_sync_record_callback: attempt to "
2008                               "sync inode record %p?", record);
2009                         break; /* NOT REACHED */
2010                 case HAMMER_MEM_RECORD_DEL:
2011                         /* 
2012                          * Follow through and issue the on-disk deletion
2013                          */
2014                         break;
2015                 }
2016         }
2017
2018         /*
2019          * If DELETED_FE is set special handling is needed for directory
2020          * entries.  Dependant pieces related to the directory entry may
2021          * have already been synced to disk.  If this occurs we have to
2022          * sync the directory entry and then change the in-memory record
2023          * from an ADD to a DELETE to cover the fact that it's been
2024          * deleted by the frontend.
2025          *
2026          * A directory delete covering record (MEM_RECORD_DEL) can never
2027          * be deleted by the frontend.
2028          *
2029          * Any other record type (aka DATA) can be deleted by the frontend.
2030          * XXX At the moment the flusher must skip it because there may
2031          * be another data record in the flush group for the same block,
2032          * meaning that some frontend data changes can leak into the backend's
2033          * synchronization point.
2034          */
2035         if (record->flags & HAMMER_RECF_DELETED_FE) {
2036                 if (record->type == HAMMER_MEM_RECORD_ADD) {
2037                         record->flags |= HAMMER_RECF_CONVERT_DELETE;
2038                 } else {
2039                         KKASSERT(record->type != HAMMER_MEM_RECORD_DEL);
2040                         record->flags |= HAMMER_RECF_DELETED_BE;
2041                         error = 0;
2042                         goto done;
2043                 }
2044         }
2045
2046         /*
2047          * Assign the create_tid for new records.  Deletions already
2048          * have the record's entire key properly set up.
2049          */
2050         if (record->type != HAMMER_MEM_RECORD_DEL)
2051                 record->leaf.base.create_tid = trans->tid;
2052                 record->leaf.create_ts = trans->time32;
2053         for (;;) {
2054                 error = hammer_ip_sync_record_cursor(cursor, record);
2055                 if (error != EDEADLK)
2056                         break;
2057                 hammer_done_cursor(cursor);
2058                 error = hammer_init_cursor(trans, cursor, &record->ip->cache[0],
2059                                            record->ip);
2060                 if (error)
2061                         break;
2062         }
2063         record->flags &= ~HAMMER_RECF_CONVERT_DELETE;
2064
2065         if (error) {
2066                 error = -error;
2067                 if (error != -ENOSPC) {
2068                         kprintf("hammer_sync_record_callback: sync failed rec "
2069                                 "%p, error %d\n", record, error);
2070                         Debugger("sync failed rec");
2071                 }
2072         }
2073 done:
2074         hammer_flush_record_done(record, error);
2075         return(error);
2076 }
2077
2078 /*
2079  * XXX error handling
2080  */
2081 int
2082 hammer_sync_inode(hammer_inode_t ip)
2083 {
2084         struct hammer_transaction trans;
2085         struct hammer_cursor cursor;
2086         hammer_node_t tmp_node;
2087         hammer_record_t depend;
2088         hammer_record_t next;
2089         int error, tmp_error;
2090         u_int64_t nlinks;
2091
2092         if ((ip->sync_flags & HAMMER_INODE_MODMASK) == 0)
2093                 return(0);
2094
2095         hammer_start_transaction_fls(&trans, ip->hmp);
2096         error = hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2097         if (error)
2098                 goto done;
2099
2100         /*
2101          * Any directory records referencing this inode which are not in
2102          * our current flush group must adjust our nlink count for the
2103          * purposes of synchronization to disk.
2104          *
2105          * Records which are in our flush group can be unlinked from our
2106          * inode now, potentially allowing the inode to be physically
2107          * deleted.
2108          *
2109          * This cannot block.
2110          */
2111         nlinks = ip->ino_data.nlinks;
2112         next = TAILQ_FIRST(&ip->target_list);
2113         while ((depend = next) != NULL) {
2114                 next = TAILQ_NEXT(depend, target_entry);
2115                 if (depend->flush_state == HAMMER_FST_FLUSH &&
2116                     depend->flush_group == ip->hmp->flusher.act) {
2117                         /*
2118                          * If this is an ADD that was deleted by the frontend
2119                          * the frontend nlinks count will have already been
2120                          * decremented, but the backend is going to sync its
2121                          * directory entry and must account for it.  The
2122                          * record will be converted to a delete-on-disk when
2123                          * it gets synced.
2124                          *
2125                          * If the ADD was not deleted by the frontend we
2126                          * can remove the dependancy from our target_list.
2127                          */
2128                         if (depend->flags & HAMMER_RECF_DELETED_FE) {
2129                                 ++nlinks;
2130                         } else {
2131                                 TAILQ_REMOVE(&ip->target_list, depend,
2132                                              target_entry);
2133                                 depend->target_ip = NULL;
2134                         }
2135                 } else if ((depend->flags & HAMMER_RECF_DELETED_FE) == 0) {
2136                         /*
2137                          * Not part of our flush group
2138                          */
2139                         KKASSERT((depend->flags & HAMMER_RECF_DELETED_BE) == 0);
2140                         switch(depend->type) {
2141                         case HAMMER_MEM_RECORD_ADD:
2142                                 --nlinks;
2143                                 break;
2144                         case HAMMER_MEM_RECORD_DEL:
2145                                 ++nlinks;
2146                                 break;
2147                         default:
2148                                 break;
2149                         }
2150                 }
2151         }
2152
2153         /*
2154          * Set dirty if we had to modify the link count.
2155          */
2156         if (ip->sync_ino_data.nlinks != nlinks) {
2157                 KKASSERT((int64_t)nlinks >= 0);
2158                 ip->sync_ino_data.nlinks = nlinks;
2159                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2160         }
2161
2162         /*
2163          * If there is a trunction queued destroy any data past the (aligned)
2164          * truncation point.  Userland will have dealt with the buffer
2165          * containing the truncation point for us.
2166          *
2167          * We don't flush pending frontend data buffers until after we've
2168          * dealt with the truncation.
2169          */
2170         if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2171                 /*
2172                  * Interlock trunc_off.  The VOP front-end may continue to
2173                  * make adjustments to it while we are blocked.
2174                  */
2175                 off_t trunc_off;
2176                 off_t aligned_trunc_off;
2177                 int blkmask;
2178
2179                 trunc_off = ip->sync_trunc_off;
2180                 blkmask = hammer_blocksize(trunc_off) - 1;
2181                 aligned_trunc_off = (trunc_off + blkmask) & ~(int64_t)blkmask;
2182
2183                 /*
2184                  * Delete any whole blocks on-media.  The front-end has
2185                  * already cleaned out any partial block and made it
2186                  * pending.  The front-end may have updated trunc_off
2187                  * while we were blocked so we only use sync_trunc_off.
2188                  *
2189                  * This operation can blow out the buffer cache, EWOULDBLOCK
2190                  * means we were unable to complete the deletion.  The
2191                  * deletion will update sync_trunc_off in that case.
2192                  */
2193                 error = hammer_ip_delete_range(&cursor, ip,
2194                                                 aligned_trunc_off,
2195                                                 0x7FFFFFFFFFFFFFFFLL, 2);
2196                 if (error == EWOULDBLOCK) {
2197                         ip->flags |= HAMMER_INODE_WOULDBLOCK;
2198                         error = 0;
2199                         goto defer_buffer_flush;
2200                 }
2201
2202                 if (error)
2203                         Debugger("hammer_ip_delete_range errored");
2204
2205                 /*
2206                  * Clear the truncation flag on the backend after we have
2207                  * complete the deletions.  Backend data is now good again
2208                  * (including new records we are about to sync, below).
2209                  *
2210                  * Leave sync_trunc_off intact.  As we write additional
2211                  * records the backend will update sync_trunc_off.  This
2212                  * tells the backend whether it can skip the overwrite
2213                  * test.  This should work properly even when the backend
2214                  * writes full blocks where the truncation point straddles
2215                  * the block because the comparison is against the base
2216                  * offset of the record.
2217                  */
2218                 ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2219                 /* ip->sync_trunc_off = 0x7FFFFFFFFFFFFFFFLL; */
2220         } else {
2221                 error = 0;
2222         }
2223
2224         /*
2225          * Now sync related records.  These will typically be directory
2226          * entries or delete-on-disk records.
2227          *
2228          * Not all records will be flushed, but clear XDIRTY anyway.  We
2229          * will set it again in the frontend hammer_flush_inode_done() 
2230          * if records remain.
2231          */
2232         if (error == 0) {
2233                 tmp_error = RB_SCAN(hammer_rec_rb_tree, &ip->rec_tree, NULL,
2234                                     hammer_sync_record_callback, &cursor);
2235                 if (tmp_error < 0)
2236                         tmp_error = -error;
2237                 if (tmp_error)
2238                         error = tmp_error;
2239         }
2240         hammer_cache_node(&ip->cache[1], cursor.node);
2241
2242         /*
2243          * Re-seek for inode update, assuming our cache hasn't been ripped
2244          * out from under us.
2245          */
2246         if (error == 0) {
2247                 tmp_node = hammer_ref_node_safe(ip->hmp, &ip->cache[0], &error);
2248                 if (tmp_node) {
2249                         hammer_cursor_downgrade(&cursor);
2250                         hammer_lock_sh(&tmp_node->lock);
2251                         if ((tmp_node->flags & HAMMER_NODE_DELETED) == 0)
2252                                 hammer_cursor_seek(&cursor, tmp_node, 0);
2253                         hammer_unlock(&tmp_node->lock);
2254                         hammer_rel_node(tmp_node);
2255                 }
2256                 error = 0;
2257         }
2258
2259         /*
2260          * If we are deleting the inode the frontend had better not have
2261          * any active references on elements making up the inode.
2262          *
2263          * The call to hammer_ip_delete_clean() cleans up auxillary records
2264          * but not DB or DATA records.  Those must have already been deleted
2265          * by the normal truncation mechanic.
2266          */
2267         if (error == 0 && ip->sync_ino_data.nlinks == 0 &&
2268                 RB_EMPTY(&ip->rec_tree)  &&
2269             (ip->sync_flags & HAMMER_INODE_DELETING) &&
2270             (ip->flags & HAMMER_INODE_DELETED) == 0) {
2271                 int count1 = 0;
2272
2273                 error = hammer_ip_delete_clean(&cursor, ip, &count1);
2274                 if (error == 0) {
2275                         ip->flags |= HAMMER_INODE_DELETED;
2276                         ip->sync_flags &= ~HAMMER_INODE_DELETING;
2277                         ip->sync_flags &= ~HAMMER_INODE_TRUNCATED;
2278                         KKASSERT(RB_EMPTY(&ip->rec_tree));
2279
2280                         /*
2281                          * Set delete_tid in both the frontend and backend
2282                          * copy of the inode record.  The DELETED flag handles
2283                          * this, do not set RDIRTY.
2284                          */
2285                         ip->ino_leaf.base.delete_tid = trans.tid;
2286                         ip->sync_ino_leaf.base.delete_tid = trans.tid;
2287                         ip->ino_leaf.delete_ts = trans.time32;
2288                         ip->sync_ino_leaf.delete_ts = trans.time32;
2289
2290
2291                         /*
2292                          * Adjust the inode count in the volume header
2293                          */
2294                         if (ip->flags & HAMMER_INODE_ONDISK) {
2295                                 hammer_modify_volume_field(&trans,
2296                                                            trans.rootvol,
2297                                                            vol0_stat_inodes);
2298                                 --ip->hmp->rootvol->ondisk->vol0_stat_inodes;
2299                                 hammer_modify_volume_done(trans.rootvol);
2300                         }
2301                 } else {
2302                         Debugger("hammer_ip_delete_clean errored");
2303                 }
2304         }
2305
2306         ip->sync_flags &= ~HAMMER_INODE_BUFS;
2307
2308         if (error)
2309                 Debugger("RB_SCAN errored");
2310
2311 defer_buffer_flush:
2312         /*
2313          * Now update the inode's on-disk inode-data and/or on-disk record.
2314          * DELETED and ONDISK are managed only in ip->flags.
2315          *
2316          * In the case of a defered buffer flush we still update the on-disk
2317          * inode to satisfy visibility requirements if there happen to be
2318          * directory dependancies.
2319          */
2320         switch(ip->flags & (HAMMER_INODE_DELETED | HAMMER_INODE_ONDISK)) {
2321         case HAMMER_INODE_DELETED|HAMMER_INODE_ONDISK:
2322                 /*
2323                  * If deleted and on-disk, don't set any additional flags.
2324                  * the delete flag takes care of things.
2325                  *
2326                  * Clear flags which may have been set by the frontend.
2327                  */
2328                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2329                                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2330                                     HAMMER_INODE_DELETING);
2331                 break;
2332         case HAMMER_INODE_DELETED:
2333                 /*
2334                  * Take care of the case where a deleted inode was never
2335                  * flushed to the disk in the first place.
2336                  *
2337                  * Clear flags which may have been set by the frontend.
2338                  */
2339                 ip->sync_flags &= ~(HAMMER_INODE_DDIRTY | HAMMER_INODE_XDIRTY |
2340                                     HAMMER_INODE_ATIME | HAMMER_INODE_MTIME |
2341                                     HAMMER_INODE_DELETING);
2342                 while (RB_ROOT(&ip->rec_tree)) {
2343                         hammer_record_t record = RB_ROOT(&ip->rec_tree);
2344                         hammer_ref(&record->lock);
2345                         KKASSERT(record->lock.refs == 1);
2346                         record->flags |= HAMMER_RECF_DELETED_FE;
2347                         record->flags |= HAMMER_RECF_DELETED_BE;
2348                         hammer_rel_mem_record(record);
2349                 }
2350                 break;
2351         case HAMMER_INODE_ONDISK:
2352                 /*
2353                  * If already on-disk, do not set any additional flags.
2354                  */
2355                 break;
2356         default:
2357                 /*
2358                  * If not on-disk and not deleted, set DDIRTY to force
2359                  * an initial record to be written.
2360                  *
2361                  * Also set the create_tid in both the frontend and backend
2362                  * copy of the inode record.
2363                  */
2364                 ip->ino_leaf.base.create_tid = trans.tid;
2365                 ip->ino_leaf.create_ts = trans.time32;
2366                 ip->sync_ino_leaf.base.create_tid = trans.tid;
2367                 ip->sync_ino_leaf.create_ts = trans.time32;
2368                 ip->sync_flags |= HAMMER_INODE_DDIRTY;
2369                 break;
2370         }
2371
2372         /*
2373          * If RDIRTY or DDIRTY is set, write out a new record.  If the inode
2374          * is already on-disk the old record is marked as deleted.
2375          *
2376          * If DELETED is set hammer_update_inode() will delete the existing
2377          * record without writing out a new one.
2378          *
2379          * If *ONLY* the ITIMES flag is set we can update the record in-place.
2380          */
2381         if (ip->flags & HAMMER_INODE_DELETED) {
2382                 error = hammer_update_inode(&cursor, ip);
2383         } else 
2384         if ((ip->sync_flags & HAMMER_INODE_DDIRTY) == 0 &&
2385             (ip->sync_flags & (HAMMER_INODE_ATIME | HAMMER_INODE_MTIME))) {
2386                 error = hammer_update_itimes(&cursor, ip);
2387         } else
2388         if (ip->sync_flags & (HAMMER_INODE_DDIRTY | HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
2389                 error = hammer_update_inode(&cursor, ip);
2390         }
2391         if (error)
2392                 Debugger("hammer_update_itimes/inode errored");
2393 done:
2394         /*
2395          * Save the TID we used to sync the inode with to make sure we
2396          * do not improperly reuse it.
2397          */
2398         hammer_done_cursor(&cursor);
2399         hammer_done_transaction(&trans);
2400         return(error);
2401 }
2402
2403 /*
2404  * This routine is called when the OS is no longer actively referencing
2405  * the inode (but might still be keeping it cached), or when releasing
2406  * the last reference to an inode.
2407  *
2408  * At this point if the inode's nlinks count is zero we want to destroy
2409  * it, which may mean destroying it on-media too.
2410  */
2411 void
2412 hammer_inode_unloadable_check(hammer_inode_t ip, int getvp)
2413 {
2414         struct vnode *vp;
2415
2416         /*
2417          * Set the DELETING flag when the link count drops to 0 and the
2418          * OS no longer has any opens on the inode.
2419          *
2420          * The backend will clear DELETING (a mod flag) and set DELETED
2421          * (a state flag) when it is actually able to perform the
2422          * operation.
2423          */
2424         if (ip->ino_data.nlinks == 0 &&
2425             (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) == 0) {
2426                 ip->flags |= HAMMER_INODE_DELETING;
2427                 ip->flags |= HAMMER_INODE_TRUNCATED;
2428                 ip->trunc_off = 0;
2429                 vp = NULL;
2430                 if (getvp) {
2431                         if (hammer_get_vnode(ip, &vp) != 0)
2432                                 return;
2433                 }
2434
2435                 /*
2436                  * Final cleanup
2437                  */
2438                 if (ip->vp) {
2439                         vtruncbuf(ip->vp, 0, HAMMER_BUFSIZE);
2440                         vnode_pager_setsize(ip->vp, 0);
2441                 }
2442                 if (getvp) {
2443                         vput(vp);
2444                 }
2445         }
2446 }
2447
2448 /*
2449  * Re-test an inode when a dependancy had gone away to see if we
2450  * can chain flush it.
2451  */
2452 void
2453 hammer_test_inode(hammer_inode_t ip)
2454 {
2455         if (ip->flags & HAMMER_INODE_REFLUSH) {
2456                 ip->flags &= ~HAMMER_INODE_REFLUSH;
2457                 hammer_ref(&ip->lock);
2458                 if (ip->flags & HAMMER_INODE_RESIGNAL) {
2459                         ip->flags &= ~HAMMER_INODE_RESIGNAL;
2460                         hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
2461                 } else {
2462                         hammer_flush_inode(ip, 0);
2463                 }
2464                 hammer_rel_inode(ip, 0);
2465         }
2466 }
2467
2468 /*
2469  * Clear the RECLAIM flag on an inode.  This occurs when the inode is
2470  * reassociated with a vp or just before it gets freed.
2471  *
2472  * Wakeup one thread blocked waiting on reclaims to complete.  Note that
2473  * the inode the thread is waiting on behalf of is a different inode then
2474  * the inode we are called with.  This is to create a pipeline.
2475  */
2476 static void
2477 hammer_inode_wakereclaims(hammer_inode_t ip)
2478 {
2479         struct hammer_reclaim *reclaim;
2480         hammer_mount_t hmp = ip->hmp;
2481
2482         if ((ip->flags & HAMMER_INODE_RECLAIM) == 0)
2483                 return;
2484
2485         --hammer_count_reclaiming;
2486         --hmp->inode_reclaims;
2487         ip->flags &= ~HAMMER_INODE_RECLAIM;
2488
2489         if ((reclaim = TAILQ_FIRST(&hmp->reclaim_list)) != NULL) {
2490                 TAILQ_REMOVE(&hmp->reclaim_list, reclaim, entry);
2491                 reclaim->okydoky = 1;
2492                 wakeup(reclaim);
2493         }
2494 }
2495
2496 /*
2497  * Setup our reclaim pipeline.  We only let so many detached (and dirty)
2498  * inodes build up before we start blocking.
2499  *
2500  * When we block we don't care *which* inode has finished reclaiming,
2501  * as lone as one does.  This is somewhat heuristical... we also put a
2502  * cap on how long we are willing to wait.
2503  */
2504 void
2505 hammer_inode_waitreclaims(hammer_mount_t hmp)
2506 {
2507         struct hammer_reclaim reclaim;
2508         int delay;
2509
2510         if (hmp->inode_reclaims > HAMMER_RECLAIM_WAIT) {
2511                 reclaim.okydoky = 0;
2512                 TAILQ_INSERT_TAIL(&hmp->reclaim_list,
2513                                   &reclaim, entry);
2514         } else {
2515                 reclaim.okydoky = 1;
2516         }
2517
2518         if (reclaim.okydoky == 0) {
2519                 delay = (hmp->inode_reclaims - HAMMER_RECLAIM_WAIT) * hz /
2520                         HAMMER_RECLAIM_WAIT;
2521                 if (delay >= 0)
2522                         tsleep(&reclaim, 0, "hmrrcm", delay + 1);
2523                 if (reclaim.okydoky == 0)
2524                         TAILQ_REMOVE(&hmp->reclaim_list, &reclaim, entry);
2525         }
2526 }
2527