HAMMER 46B/Many: Stabilization pass
[dragonfly.git] / sys / vfs / hammer / hammer_ondisk.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_ondisk.c,v 1.47 2008/05/18 21:47:06 dillon Exp $
35  */
36 /*
37  * Manage HAMMER's on-disk structures.  These routines are primarily
38  * responsible for interfacing with the kernel's I/O subsystem and for
39  * managing in-memory structures.
40  */
41
42 #include "hammer.h"
43 #include <sys/fcntl.h>
44 #include <sys/nlookup.h>
45 #include <sys/buf.h>
46 #include <sys/buf2.h>
47
48 static void hammer_free_volume(hammer_volume_t volume);
49 static int hammer_load_volume(hammer_volume_t volume);
50 static int hammer_load_buffer(hammer_buffer_t buffer, int isnew);
51 static int hammer_load_node(hammer_node_t node, int isnew);
52
53 /*
54  * Red-Black tree support for various structures
55  */
56 static int
57 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
58 {
59         if (ip1->obj_id < ip2->obj_id)
60                 return(-1);
61         if (ip1->obj_id > ip2->obj_id)
62                 return(1);
63         if (ip1->obj_asof < ip2->obj_asof)
64                 return(-1);
65         if (ip1->obj_asof > ip2->obj_asof)
66                 return(1);
67         return(0);
68 }
69
70 static int
71 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
72 {
73         if (info->obj_id < ip->obj_id)
74                 return(-1);
75         if (info->obj_id > ip->obj_id)
76                 return(1);
77         if (info->obj_asof < ip->obj_asof)
78                 return(-1);
79         if (info->obj_asof > ip->obj_asof)
80                 return(1);
81         return(0);
82 }
83
84 static int
85 hammer_vol_rb_compare(hammer_volume_t vol1, hammer_volume_t vol2)
86 {
87         if (vol1->vol_no < vol2->vol_no)
88                 return(-1);
89         if (vol1->vol_no > vol2->vol_no)
90                 return(1);
91         return(0);
92 }
93
94 static int
95 hammer_buf_rb_compare(hammer_buffer_t buf1, hammer_buffer_t buf2)
96 {
97         if (buf1->zone2_offset < buf2->zone2_offset)
98                 return(-1);
99         if (buf1->zone2_offset > buf2->zone2_offset)
100                 return(1);
101         return(0);
102 }
103
104 static int
105 hammer_nod_rb_compare(hammer_node_t node1, hammer_node_t node2)
106 {
107         if (node1->node_offset < node2->node_offset)
108                 return(-1);
109         if (node1->node_offset > node2->node_offset)
110                 return(1);
111         return(0);
112 }
113
114 /*
115  * Note: The lookup function for hammer_ino_rb_tree winds up being named
116  * hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).  The other lookup
117  * functions are normal, e.g. hammer_buf_rb_tree_RB_LOOKUP(root, zone2_offset).
118  */
119 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
120 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
121                 hammer_inode_info_cmp, hammer_inode_info_t);
122 RB_GENERATE2(hammer_vol_rb_tree, hammer_volume, rb_node,
123              hammer_vol_rb_compare, int32_t, vol_no);
124 RB_GENERATE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
125              hammer_buf_rb_compare, hammer_off_t, zone2_offset);
126 RB_GENERATE2(hammer_nod_rb_tree, hammer_node, rb_node,
127              hammer_nod_rb_compare, hammer_off_t, node_offset);
128
129 /************************************************************************
130  *                              VOLUMES                                 *
131  ************************************************************************
132  *
133  * Load a HAMMER volume by name.  Returns 0 on success or a positive error
134  * code on failure.  Volumes must be loaded at mount time, get_volume() will
135  * not load a new volume.
136  *
137  * Calls made to hammer_load_volume() or single-threaded
138  */
139 int
140 hammer_install_volume(struct hammer_mount *hmp, const char *volname)
141 {
142         struct mount *mp;
143         hammer_volume_t volume;
144         struct hammer_volume_ondisk *ondisk;
145         struct nlookupdata nd;
146         struct buf *bp = NULL;
147         int error;
148         int ronly;
149         int setmp = 0;
150
151         mp = hmp->mp;
152         ronly = ((mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
153
154         /*
155          * Allocate a volume structure
156          */
157         ++hammer_count_volumes;
158         volume = kmalloc(sizeof(*volume), M_HAMMER, M_WAITOK|M_ZERO);
159         volume->vol_name = kstrdup(volname, M_HAMMER);
160         hammer_io_init(&volume->io, hmp, HAMMER_STRUCTURE_VOLUME);
161         volume->io.offset = 0LL;
162
163         /*
164          * Get the device vnode
165          */
166         error = nlookup_init(&nd, volume->vol_name, UIO_SYSSPACE, NLC_FOLLOW);
167         if (error == 0)
168                 error = nlookup(&nd);
169         if (error == 0)
170                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &volume->devvp);
171         nlookup_done(&nd);
172         if (error == 0) {
173                 if (vn_isdisk(volume->devvp, &error)) {
174                         error = vfs_mountedon(volume->devvp);
175                 }
176         }
177         if (error == 0 &&
178             count_udev(volume->devvp->v_umajor, volume->devvp->v_uminor) > 0) {
179                 error = EBUSY;
180         }
181         if (error == 0) {
182                 vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
183                 error = vinvalbuf(volume->devvp, V_SAVE, 0, 0);
184                 if (error == 0) {
185                         error = VOP_OPEN(volume->devvp, 
186                                          (ronly ? FREAD : FREAD|FWRITE),
187                                          FSCRED, NULL);
188                 }
189                 vn_unlock(volume->devvp);
190         }
191         if (error) {
192                 hammer_free_volume(volume);
193                 return(error);
194         }
195         volume->devvp->v_rdev->si_mountpoint = mp;
196         setmp = 1;
197
198         /*
199          * Extract the volume number from the volume header and do various
200          * sanity checks.
201          */
202         error = bread(volume->devvp, 0LL, HAMMER_BUFSIZE, &bp);
203         if (error)
204                 goto late_failure;
205         ondisk = (void *)bp->b_data;
206         if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
207                 kprintf("hammer_mount: volume %s has an invalid header\n",
208                         volume->vol_name);
209                 error = EFTYPE;
210                 goto late_failure;
211         }
212         volume->vol_no = ondisk->vol_no;
213         volume->buffer_base = ondisk->vol_buf_beg;
214         volume->vol_flags = ondisk->vol_flags;
215         volume->nblocks = ondisk->vol_nblocks; 
216         volume->maxbuf_off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no,
217                                     ondisk->vol_buf_end - ondisk->vol_buf_beg);
218         volume->maxraw_off = ondisk->vol_buf_end;
219         RB_INIT(&volume->rb_bufs_root);
220
221         if (RB_EMPTY(&hmp->rb_vols_root)) {
222                 hmp->fsid = ondisk->vol_fsid;
223         } else if (bcmp(&hmp->fsid, &ondisk->vol_fsid, sizeof(uuid_t))) {
224                 kprintf("hammer_mount: volume %s's fsid does not match "
225                         "other volumes\n", volume->vol_name);
226                 error = EFTYPE;
227                 goto late_failure;
228         }
229
230         /*
231          * Insert the volume structure into the red-black tree.
232          */
233         if (RB_INSERT(hammer_vol_rb_tree, &hmp->rb_vols_root, volume)) {
234                 kprintf("hammer_mount: volume %s has a duplicate vol_no %d\n",
235                         volume->vol_name, volume->vol_no);
236                 error = EEXIST;
237         }
238
239         /*
240          * Set the root volume .  HAMMER special cases rootvol the structure.
241          * We do not hold a ref because this would prevent related I/O
242          * from being flushed.
243          */
244         if (error == 0 && ondisk->vol_rootvol == ondisk->vol_no) {
245                 hmp->rootvol = volume;
246                 if (bp) {
247                         brelse(bp);
248                         bp = NULL;
249                 }
250                 hmp->fsid_udev = dev2udev(vn_todev(volume->devvp));
251                 hmp->mp->mnt_stat.f_blocks += ondisk->vol0_stat_bigblocks *
252                         (HAMMER_LARGEBLOCK_SIZE / HAMMER_BUFSIZE);
253         }
254 late_failure:
255         if (bp)
256                 brelse(bp);
257         if (error) {
258                 /*vinvalbuf(volume->devvp, V_SAVE, 0, 0);*/
259                 if (setmp)
260                         volume->devvp->v_rdev->si_mountpoint = NULL;
261                 VOP_CLOSE(volume->devvp, ronly ? FREAD : FREAD|FWRITE);
262                 hammer_free_volume(volume);
263         }
264         return (error);
265 }
266
267 /*
268  * Unload and free a HAMMER volume.  Must return >= 0 to continue scan
269  * so returns -1 on failure.
270  */
271 int
272 hammer_unload_volume(hammer_volume_t volume, void *data __unused)
273 {
274         struct hammer_mount *hmp = volume->io.hmp;
275         int ronly = ((hmp->mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
276
277         /*
278          * Clean up the root volume pointer, which is held unlocked in hmp.
279          */
280         if (hmp->rootvol == volume)
281                 hmp->rootvol = NULL;
282
283         /*
284          * Unload buffers.
285          */
286         RB_SCAN(hammer_buf_rb_tree, &volume->rb_bufs_root, NULL,
287                         hammer_unload_buffer, NULL);
288
289         /*
290          * Release our buffer and flush anything left in the buffer cache.
291          */
292         volume->io.waitdep = 1;
293         hammer_io_release(&volume->io, 1);
294
295         /*
296          * There should be no references on the volume, no clusters, and
297          * no super-clusters.
298          */
299         KKASSERT(volume->io.lock.refs == 0);
300         KKASSERT(RB_EMPTY(&volume->rb_bufs_root));
301
302         volume->ondisk = NULL;
303         if (volume->devvp) {
304                 if (volume->devvp->v_rdev &&
305                     volume->devvp->v_rdev->si_mountpoint == hmp->mp
306                 ) {
307                         volume->devvp->v_rdev->si_mountpoint = NULL;
308                 }
309                 if (ronly) {
310                         vinvalbuf(volume->devvp, 0, 0, 0);
311                         VOP_CLOSE(volume->devvp, FREAD);
312                 } else {
313                         vinvalbuf(volume->devvp, V_SAVE, 0, 0);
314                         VOP_CLOSE(volume->devvp, FREAD|FWRITE);
315                 }
316         }
317
318         /*
319          * Destroy the structure
320          */
321         RB_REMOVE(hammer_vol_rb_tree, &hmp->rb_vols_root, volume);
322         hammer_free_volume(volume);
323         return(0);
324 }
325
326 static
327 void
328 hammer_free_volume(hammer_volume_t volume)
329 {
330         if (volume->vol_name) {
331                 kfree(volume->vol_name, M_HAMMER);
332                 volume->vol_name = NULL;
333         }
334         if (volume->devvp) {
335                 vrele(volume->devvp);
336                 volume->devvp = NULL;
337         }
338         --hammer_count_volumes;
339         kfree(volume, M_HAMMER);
340 }
341
342 /*
343  * Get a HAMMER volume.  The volume must already exist.
344  */
345 hammer_volume_t
346 hammer_get_volume(struct hammer_mount *hmp, int32_t vol_no, int *errorp)
347 {
348         struct hammer_volume *volume;
349
350         /*
351          * Locate the volume structure
352          */
353         volume = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, vol_no);
354         if (volume == NULL) {
355                 *errorp = ENOENT;
356                 return(NULL);
357         }
358         hammer_ref(&volume->io.lock);
359
360         /*
361          * Deal with on-disk info
362          */
363         if (volume->ondisk == NULL || volume->io.loading) {
364                 *errorp = hammer_load_volume(volume);
365                 if (*errorp) {
366                         hammer_rel_volume(volume, 1);
367                         volume = NULL;
368                 }
369         } else {
370                 *errorp = 0;
371         }
372         return(volume);
373 }
374
375 int
376 hammer_ref_volume(hammer_volume_t volume)
377 {
378         int error;
379
380         hammer_ref(&volume->io.lock);
381
382         /*
383          * Deal with on-disk info
384          */
385         if (volume->ondisk == NULL || volume->io.loading) {
386                 error = hammer_load_volume(volume);
387                 if (error)
388                         hammer_rel_volume(volume, 1);
389         } else {
390                 error = 0;
391         }
392         return (error);
393 }
394
395 hammer_volume_t
396 hammer_get_root_volume(struct hammer_mount *hmp, int *errorp)
397 {
398         hammer_volume_t volume;
399
400         volume = hmp->rootvol;
401         KKASSERT(volume != NULL);
402         hammer_ref(&volume->io.lock);
403
404         /*
405          * Deal with on-disk info
406          */
407         if (volume->ondisk == NULL || volume->io.loading) {
408                 *errorp = hammer_load_volume(volume);
409                 if (*errorp) {
410                         hammer_rel_volume(volume, 1);
411                         volume = NULL;
412                 }
413         } else {
414                 *errorp = 0;
415         }
416         return (volume);
417 }
418
419 /*
420  * Load a volume's on-disk information.  The volume must be referenced and
421  * not locked.  We temporarily acquire an exclusive lock to interlock
422  * against releases or multiple get's.
423  */
424 static int
425 hammer_load_volume(hammer_volume_t volume)
426 {
427         int error;
428
429         ++volume->io.loading;
430         hammer_lock_ex(&volume->io.lock);
431
432         if (volume->ondisk == NULL) {
433                 error = hammer_io_read(volume->devvp, &volume->io,
434                                        volume->maxraw_off);
435                 if (error == 0)
436                         volume->ondisk = (void *)volume->io.bp->b_data;
437         } else {
438                 error = 0;
439         }
440         --volume->io.loading;
441         hammer_unlock(&volume->io.lock);
442         return(error);
443 }
444
445 /*
446  * Release a volume.  Call hammer_io_release on the last reference.  We have
447  * to acquire an exclusive lock to interlock against volume->ondisk tests
448  * in hammer_load_volume(), and hammer_io_release() also expects an exclusive
449  * lock to be held.
450  *
451  * Volumes are not unloaded from memory during normal operation.
452  */
453 void
454 hammer_rel_volume(hammer_volume_t volume, int flush)
455 {
456         crit_enter();
457         if (volume->io.lock.refs == 1) {
458                 ++volume->io.loading;
459                 hammer_lock_ex(&volume->io.lock);
460                 if (volume->io.lock.refs == 1) {
461                         volume->ondisk = NULL;
462                         hammer_io_release(&volume->io, flush);
463                 }
464                 --volume->io.loading;
465                 hammer_unlock(&volume->io.lock);
466         }
467         hammer_unref(&volume->io.lock);
468         crit_exit();
469 }
470
471 /************************************************************************
472  *                              BUFFERS                                 *
473  ************************************************************************
474  *
475  * Manage buffers.  Currently all blockmap-backed zones are translated
476  * to zone-2 buffer offsets.
477  */
478 hammer_buffer_t
479 hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
480                   int isnew, int *errorp)
481 {
482         hammer_buffer_t buffer;
483         hammer_volume_t volume;
484         hammer_off_t    zoneX_offset;
485         hammer_io_type_t iotype;
486         int vol_no;
487         int zone;
488
489         zoneX_offset = buf_offset;
490         zone = HAMMER_ZONE_DECODE(buf_offset);
491
492         /*
493          * What is the buffer class?
494          */
495         switch(zone) {
496         case HAMMER_ZONE_LARGE_DATA_INDEX:
497         case HAMMER_ZONE_SMALL_DATA_INDEX:
498                 iotype = HAMMER_STRUCTURE_DATA_BUFFER;
499                 break;
500         case HAMMER_ZONE_UNDO_INDEX:
501                 iotype = HAMMER_STRUCTURE_UNDO_BUFFER;
502                 break;
503         default:
504                 iotype = HAMMER_STRUCTURE_META_BUFFER;
505                 break;
506         }
507
508         /*
509          * Handle blockmap offset translations
510          */
511         if (zone >= HAMMER_ZONE_BTREE_INDEX) {
512                 buf_offset = hammer_blockmap_lookup(hmp, buf_offset, errorp);
513                 KKASSERT(*errorp == 0);
514         } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
515                 buf_offset = hammer_undo_lookup(hmp, buf_offset, errorp);
516                 KKASSERT(*errorp == 0);
517         }
518
519         /*
520          * Locate the buffer given its zone-2 offset.
521          */
522         buf_offset &= ~HAMMER_BUFMASK64;
523         KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
524         vol_no = HAMMER_VOL_DECODE(buf_offset);
525         volume = hammer_get_volume(hmp, vol_no, errorp);
526         if (volume == NULL)
527                 return(NULL);
528
529         /*
530          * NOTE: buf_offset and maxbuf_off are both full zone-2 offset
531          * specifications.
532          */
533         KKASSERT(buf_offset < volume->maxbuf_off);
534
535         /*
536          * Locate and lock the buffer structure, creating one if necessary.
537          */
538 again:
539         buffer = RB_LOOKUP(hammer_buf_rb_tree, &volume->rb_bufs_root,
540                            buf_offset);
541         if (buffer == NULL) {
542                 ++hammer_count_buffers;
543                 buffer = kmalloc(sizeof(*buffer), M_HAMMER, M_WAITOK|M_ZERO);
544                 buffer->zone2_offset = buf_offset;
545                 buffer->volume = volume;
546
547                 hammer_io_init(&buffer->io, hmp, iotype);
548                 buffer->io.offset = volume->ondisk->vol_buf_beg +
549                                     (buf_offset & HAMMER_OFF_SHORT_MASK);
550                 TAILQ_INIT(&buffer->clist);
551                 hammer_ref(&buffer->io.lock);
552
553                 /*
554                  * Insert the buffer into the RB tree and handle late
555                  * collisions.
556                  */
557                 if (RB_INSERT(hammer_buf_rb_tree, &volume->rb_bufs_root, buffer)) {
558                         hammer_unref(&buffer->io.lock);
559                         --hammer_count_buffers;
560                         kfree(buffer, M_HAMMER);
561                         goto again;
562                 }
563                 hammer_ref(&volume->io.lock);
564         } else {
565                 hammer_ref(&buffer->io.lock);
566
567                 /*
568                  * The buffer is no longer loose if it has a ref.
569                  */
570                 if (buffer->io.mod_list == &hmp->lose_list) {
571                         TAILQ_REMOVE(buffer->io.mod_list, &buffer->io,
572                                      mod_entry);
573                         buffer->io.mod_list = NULL;
574                 }
575                 if (buffer->io.lock.refs == 1)
576                         hammer_io_reinit(&buffer->io, iotype);
577                 else
578                         KKASSERT(buffer->io.type == iotype);
579         }
580
581         /*
582          * Cache the blockmap translation
583          */
584         if ((zoneX_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_RAW_BUFFER)
585                 buffer->zoneX_offset = zoneX_offset;
586
587         /*
588          * Deal with on-disk info
589          */
590         if (buffer->ondisk == NULL || buffer->io.loading) {
591                 *errorp = hammer_load_buffer(buffer, isnew);
592                 if (*errorp) {
593                         hammer_rel_buffer(buffer, 1);
594                         buffer = NULL;
595                 }
596         } else {
597                 *errorp = 0;
598         }
599         hammer_rel_volume(volume, 0);
600         return(buffer);
601 }
602
603 /*
604  * Clear the cached zone-X translation for a buffer.
605  */
606 void
607 hammer_clrxlate_buffer(hammer_mount_t hmp, hammer_off_t buf_offset)
608 {
609         hammer_buffer_t buffer;
610         hammer_volume_t volume;
611         int vol_no;
612         int error;
613
614         buf_offset &= ~HAMMER_BUFMASK64;
615         KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
616         vol_no = HAMMER_VOL_DECODE(buf_offset);
617         volume = hammer_get_volume(hmp, vol_no, &error);
618         if (volume == NULL)
619                 return;
620         buffer = RB_LOOKUP(hammer_buf_rb_tree, &volume->rb_bufs_root,
621                            buf_offset);
622         if (buffer)
623                 buffer->zoneX_offset = 0;
624         hammer_rel_volume(volume, 0);
625 }
626
627 static int
628 hammer_load_buffer(hammer_buffer_t buffer, int isnew)
629 {
630         hammer_volume_t volume;
631         int error;
632
633         /*
634          * Load the buffer's on-disk info
635          */
636         volume = buffer->volume;
637         ++buffer->io.loading;
638         hammer_lock_ex(&buffer->io.lock);
639
640         if (hammer_debug_io & 0x0001) {
641                 kprintf("load_buffer %016llx %016llx\n",
642                         buffer->zoneX_offset, buffer->zone2_offset);
643         }
644
645         if (buffer->ondisk == NULL) {
646                 if (isnew) {
647                         error = hammer_io_new(volume->devvp, &buffer->io);
648                 } else {
649                         error = hammer_io_read(volume->devvp, &buffer->io,
650                                                volume->maxraw_off);
651                 }
652                 if (error == 0)
653                         buffer->ondisk = (void *)buffer->io.bp->b_data;
654         } else if (isnew) {
655                 error = hammer_io_new(volume->devvp, &buffer->io);
656         } else {
657                 error = 0;
658         }
659         --buffer->io.loading;
660         hammer_unlock(&buffer->io.lock);
661         return (error);
662 }
663
664 /*
665  * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
666  */
667 int
668 hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused)
669 {
670         hammer_ref(&buffer->io.lock);
671         hammer_flush_buffer_nodes(buffer);
672         KKASSERT(buffer->io.lock.refs == 1);
673         hammer_rel_buffer(buffer, 2);
674         return(0);
675 }
676
677 /*
678  * Reference a buffer that is either already referenced or via a specially
679  * handled pointer (aka cursor->buffer).
680  */
681 int
682 hammer_ref_buffer(hammer_buffer_t buffer)
683 {
684         int error;
685
686         hammer_ref(&buffer->io.lock);
687
688         /*
689          * No longer loose
690          */
691         if (buffer->io.mod_list == &buffer->io.hmp->lose_list) {
692                 TAILQ_REMOVE(buffer->io.mod_list, &buffer->io, mod_entry);
693                 buffer->io.mod_list = NULL;
694         }
695
696         if (buffer->ondisk == NULL || buffer->io.loading) {
697                 error = hammer_load_buffer(buffer, 0);
698                 if (error) {
699                         hammer_rel_buffer(buffer, 1);
700                         /*
701                          * NOTE: buffer pointer can become stale after
702                          * the above release.
703                          */
704                 }
705         } else {
706                 error = 0;
707         }
708         return(error);
709 }
710
711 /*
712  * Release a buffer.  We have to deal with several places where
713  * another thread can ref the buffer.
714  *
715  * Only destroy the structure itself if the related buffer cache buffer
716  * was disassociated from it.  This ties the management of the structure
717  * to the buffer cache subsystem.  buffer->ondisk determines whether the
718  * embedded io is referenced or not.
719  */
720 void
721 hammer_rel_buffer(hammer_buffer_t buffer, int flush)
722 {
723         hammer_volume_t volume;
724         int freeme = 0;
725
726         crit_enter();
727         if (buffer->io.lock.refs == 1) {
728                 ++buffer->io.loading;   /* force interlock check */
729                 hammer_lock_ex(&buffer->io.lock);
730                 if (buffer->io.lock.refs == 1) {
731                         hammer_io_release(&buffer->io, flush);
732                         hammer_flush_buffer_nodes(buffer);
733                         KKASSERT(TAILQ_EMPTY(&buffer->clist));
734
735                         if (buffer->io.bp == NULL &&
736                             buffer->io.lock.refs == 1) {
737                                 /*
738                                  * Final cleanup
739                                  */
740                                 volume = buffer->volume;
741                                 RB_REMOVE(hammer_buf_rb_tree,
742                                           &volume->rb_bufs_root, buffer);
743                                 buffer->volume = NULL; /* sanity */
744                                 hammer_rel_volume(volume, 0);
745                                 freeme = 1;
746                         }
747                 }
748                 --buffer->io.loading;
749                 hammer_unlock(&buffer->io.lock);
750         }
751         hammer_unref(&buffer->io.lock);
752         crit_exit();
753         if (freeme) {
754                 KKASSERT(buffer->io.mod_list == NULL);
755                 --hammer_count_buffers;
756                 kfree(buffer, M_HAMMER);
757         }
758 }
759
760 /*
761  * Remove the zoneX translation cache for a buffer given its zone-2 offset.
762  */
763 void
764 hammer_uncache_buffer(hammer_mount_t hmp, hammer_off_t buf_offset)
765 {
766         hammer_volume_t volume;
767         hammer_buffer_t buffer;
768         int vol_no;
769         int error;
770
771         buf_offset &= ~HAMMER_BUFMASK64;
772         KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_RAW_BUFFER);
773         vol_no = HAMMER_VOL_DECODE(buf_offset);
774         volume = hammer_get_volume(hmp, vol_no, &error);
775         KKASSERT(volume != 0);
776         KKASSERT(buf_offset < volume->maxbuf_off);
777
778         buffer = RB_LOOKUP(hammer_buf_rb_tree, &volume->rb_bufs_root,
779                            buf_offset);
780         if (buffer)
781                 buffer->zoneX_offset = 0;
782         hammer_rel_volume(volume, 0);
783 }
784
785 /*
786  * Access the filesystem buffer containing the specified hammer offset.
787  * buf_offset is a conglomeration of the volume number and vol_buf_beg
788  * relative buffer offset.  It must also have bit 55 set to be valid.
789  * (see hammer_off_t in hammer_disk.h).
790  *
791  * Any prior buffer in *bufferp will be released and replaced by the
792  * requested buffer.
793  */
794 void *
795 hammer_bread(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp, 
796              struct hammer_buffer **bufferp)
797 {
798         hammer_buffer_t buffer;
799         int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
800
801         buf_offset &= ~HAMMER_BUFMASK64;
802         KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) != 0);
803
804         buffer = *bufferp;
805         if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
806                                buffer->zoneX_offset != buf_offset)) {
807                 if (buffer)
808                         hammer_rel_buffer(buffer, 0);
809                 buffer = hammer_get_buffer(hmp, buf_offset, 0, errorp);
810                 *bufferp = buffer;
811         } else {
812                 *errorp = 0;
813         }
814
815         /*
816          * Return a pointer to the buffer data.
817          */
818         if (buffer == NULL)
819                 return(NULL);
820         else
821                 return((char *)buffer->ondisk + xoff);
822 }
823
824 /*
825  * Access the filesystem buffer containing the specified hammer offset.
826  * No disk read operation occurs.  The result buffer may contain garbage.
827  *
828  * Any prior buffer in *bufferp will be released and replaced by the
829  * requested buffer.
830  *
831  * This function marks the buffer dirty but does not increment its
832  * modify_refs count.
833  */
834 void *
835 hammer_bnew(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp, 
836              struct hammer_buffer **bufferp)
837 {
838         hammer_buffer_t buffer;
839         int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
840
841         buf_offset &= ~HAMMER_BUFMASK64;
842
843         buffer = *bufferp;
844         if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
845                                buffer->zoneX_offset != buf_offset)) {
846                 if (buffer)
847                         hammer_rel_buffer(buffer, 0);
848                 buffer = hammer_get_buffer(hmp, buf_offset, 1, errorp);
849                 *bufferp = buffer;
850         } else {
851                 *errorp = 0;
852         }
853
854         /*
855          * Return a pointer to the buffer data.
856          */
857         if (buffer == NULL)
858                 return(NULL);
859         else
860                 return((char *)buffer->ondisk + xoff);
861 }
862
863 /************************************************************************
864  *                              NODES                                   *
865  ************************************************************************
866  *
867  * Manage B-Tree nodes.  B-Tree nodes represent the primary indexing
868  * method used by the HAMMER filesystem.
869  *
870  * Unlike other HAMMER structures, a hammer_node can be PASSIVELY
871  * associated with its buffer, and will only referenced the buffer while
872  * the node itself is referenced.
873  *
874  * A hammer_node can also be passively associated with other HAMMER
875  * structures, such as inodes, while retaining 0 references.  These
876  * associations can be cleared backwards using a pointer-to-pointer in
877  * the hammer_node.
878  *
879  * This allows the HAMMER implementation to cache hammer_nodes long-term
880  * and short-cut a great deal of the infrastructure's complexity.  In
881  * most cases a cached node can be reacquired without having to dip into
882  * either the buffer or cluster management code.
883  *
884  * The caller must pass a referenced cluster on call and will retain
885  * ownership of the reference on return.  The node will acquire its own
886  * additional references, if necessary.
887  */
888 hammer_node_t
889 hammer_get_node(hammer_mount_t hmp, hammer_off_t node_offset,
890                 int isnew, int *errorp)
891 {
892         hammer_node_t node;
893
894         KKASSERT((node_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_BTREE);
895
896         /*
897          * Locate the structure, allocating one if necessary.
898          */
899 again:
900         node = RB_LOOKUP(hammer_nod_rb_tree, &hmp->rb_nods_root, node_offset);
901         if (node == NULL) {
902                 ++hammer_count_nodes;
903                 node = kmalloc(sizeof(*node), M_HAMMER, M_WAITOK|M_ZERO);
904                 node->node_offset = node_offset;
905                 node->hmp = hmp;
906                 if (RB_INSERT(hammer_nod_rb_tree, &hmp->rb_nods_root, node)) {
907                         --hammer_count_nodes;
908                         kfree(node, M_HAMMER);
909                         goto again;
910                 }
911         }
912         hammer_ref(&node->lock);
913         if (node->ondisk)
914                 *errorp = 0;
915         else
916                 *errorp = hammer_load_node(node, isnew);
917         if (*errorp) {
918                 hammer_rel_node(node);
919                 node = NULL;
920         }
921         return(node);
922 }
923
924 /*
925  * Reference an already-referenced node.
926  */
927 void
928 hammer_ref_node(hammer_node_t node)
929 {
930         KKASSERT(node->lock.refs > 0 && node->ondisk != NULL);
931         hammer_ref(&node->lock);
932 }
933
934 /*
935  * Load a node's on-disk data reference.
936  */
937 static int
938 hammer_load_node(hammer_node_t node, int isnew)
939 {
940         hammer_buffer_t buffer;
941         hammer_off_t buf_offset;
942         int error;
943
944         error = 0;
945         ++node->loading;
946         hammer_lock_ex(&node->lock);
947         if (node->ondisk == NULL) {
948                 /*
949                  * This is a little confusing but the jist is that
950                  * node->buffer determines whether the node is on
951                  * the buffer's clist and node->ondisk determines
952                  * whether the buffer is referenced.
953                  *
954                  * We could be racing a buffer release, in which case
955                  * node->buffer may become NULL while we are blocked
956                  * referencing the buffer.
957                  */
958                 if ((buffer = node->buffer) != NULL) {
959                         error = hammer_ref_buffer(buffer);
960                         if (error == 0 && node->buffer == NULL) {
961                                 TAILQ_INSERT_TAIL(&buffer->clist,
962                                                   node, entry);
963                                 node->buffer = buffer;
964                         }
965                 } else {
966                         buf_offset = node->node_offset & ~HAMMER_BUFMASK64;
967                         buffer = hammer_get_buffer(node->hmp, buf_offset,
968                                                    0, &error);
969                         if (buffer) {
970                                 KKASSERT(error == 0);
971                                 TAILQ_INSERT_TAIL(&buffer->clist,
972                                                   node, entry);
973                                 node->buffer = buffer;
974                         }
975                 }
976                 if (error == 0) {
977                         node->ondisk = (void *)((char *)buffer->ondisk +
978                                (node->node_offset & HAMMER_BUFMASK));
979                         if (isnew == 0 &&
980                             hammer_crc_test_btree(node->ondisk) == 0) {
981                                 Debugger("CRC FAILED: B-TREE NODE");
982                         }
983                 }
984         }
985         --node->loading;
986         hammer_unlock(&node->lock);
987         return (error);
988 }
989
990 /*
991  * Safely reference a node, interlock against flushes via the IO subsystem.
992  */
993 hammer_node_t
994 hammer_ref_node_safe(struct hammer_mount *hmp, struct hammer_node **cache,
995                      int *errorp)
996 {
997         hammer_node_t node;
998
999         node = *cache;
1000         if (node != NULL) {
1001                 hammer_ref(&node->lock);
1002                 if (node->ondisk)
1003                         *errorp = 0;
1004                 else
1005                         *errorp = hammer_load_node(node, 0);
1006                 if (*errorp) {
1007                         hammer_rel_node(node);
1008                         node = NULL;
1009                 }
1010         } else {
1011                 *errorp = ENOENT;
1012         }
1013         return(node);
1014 }
1015
1016 /*
1017  * Release a hammer_node.  On the last release the node dereferences
1018  * its underlying buffer and may or may not be destroyed.
1019  */
1020 void
1021 hammer_rel_node(hammer_node_t node)
1022 {
1023         hammer_buffer_t buffer;
1024
1025         /*
1026          * If this isn't the last ref just decrement the ref count and
1027          * return.
1028          */
1029         if (node->lock.refs > 1) {
1030                 hammer_unref(&node->lock);
1031                 return;
1032         }
1033
1034         /*
1035          * If there is no ondisk info or no buffer the node failed to load,
1036          * remove the last reference and destroy the node.
1037          */
1038         if (node->ondisk == NULL) {
1039                 hammer_unref(&node->lock);
1040                 hammer_flush_node(node);
1041                 /* node is stale now */
1042                 return;
1043         }
1044
1045         /*
1046          * Do final cleanups and then either destroy the node and leave it
1047          * passively cached.  The buffer reference is removed regardless.
1048          */
1049         buffer = node->buffer;
1050         node->ondisk = NULL;
1051
1052         if ((node->flags & HAMMER_NODE_FLUSH) == 0) {
1053                 hammer_unref(&node->lock);
1054                 hammer_rel_buffer(buffer, 0);
1055                 return;
1056         }
1057
1058         /*
1059          * Destroy the node.
1060          */
1061         hammer_unref(&node->lock);
1062         hammer_flush_node(node);
1063         /* node is stale */
1064         hammer_rel_buffer(buffer, 0);
1065 }
1066
1067 /*
1068  * Free space on-media associated with a B-Tree node.
1069  */
1070 void
1071 hammer_delete_node(hammer_transaction_t trans, hammer_node_t node)
1072 {
1073         KKASSERT((node->flags & HAMMER_NODE_DELETED) == 0);
1074         node->flags |= HAMMER_NODE_DELETED;
1075         hammer_blockmap_free(trans, node->node_offset, sizeof(*node->ondisk));
1076 }
1077
1078 /*
1079  * Passively cache a referenced hammer_node in *cache.  The caller may
1080  * release the node on return.
1081  */
1082 void
1083 hammer_cache_node(hammer_node_t node, struct hammer_node **cache)
1084 {
1085         hammer_node_t old;
1086
1087         /*
1088          * If the node is being deleted, don't cache it!
1089          */
1090         if (node->flags & HAMMER_NODE_DELETED)
1091                 return;
1092
1093         /*
1094          * Cache the node.  If we previously cached a different node we
1095          * have to give HAMMER a chance to destroy it.
1096          */
1097 again:
1098         if (node->cache1 != cache) {
1099                 if (node->cache2 != cache) {
1100                         if ((old = *cache) != NULL) {
1101                                 KKASSERT(node->lock.refs != 0);
1102                                 hammer_uncache_node(cache);
1103                                 goto again;
1104                         }
1105                         if (node->cache2)
1106                                 *node->cache2 = NULL;
1107                         node->cache2 = node->cache1;
1108                         node->cache1 = cache;
1109                         *cache = node;
1110                 } else {
1111                         struct hammer_node **tmp;
1112                         tmp = node->cache1;
1113                         node->cache1 = node->cache2;
1114                         node->cache2 = tmp;
1115                 }
1116         }
1117 }
1118
1119 void
1120 hammer_uncache_node(struct hammer_node **cache)
1121 {
1122         hammer_node_t node;
1123
1124         if ((node = *cache) != NULL) {
1125                 *cache = NULL;
1126                 if (node->cache1 == cache) {
1127                         node->cache1 = node->cache2;
1128                         node->cache2 = NULL;
1129                 } else if (node->cache2 == cache) {
1130                         node->cache2 = NULL;
1131                 } else {
1132                         panic("hammer_uncache_node: missing cache linkage");
1133                 }
1134                 if (node->cache1 == NULL && node->cache2 == NULL)
1135                         hammer_flush_node(node);
1136         }
1137 }
1138
1139 /*
1140  * Remove a node's cache references and destroy the node if it has no
1141  * other references or backing store.
1142  */
1143 void
1144 hammer_flush_node(hammer_node_t node)
1145 {
1146         hammer_buffer_t buffer;
1147
1148         if (node->cache1)
1149                 *node->cache1 = NULL;
1150         if (node->cache2)
1151                 *node->cache2 = NULL;
1152         if (node->lock.refs == 0 && node->ondisk == NULL) {
1153                 RB_REMOVE(hammer_nod_rb_tree, &node->hmp->rb_nods_root, node);
1154                 if ((buffer = node->buffer) != NULL) {
1155                         node->buffer = NULL;
1156                         TAILQ_REMOVE(&buffer->clist, node, entry);
1157                         /* buffer is unreferenced because ondisk is NULL */
1158                 }
1159                 --hammer_count_nodes;
1160                 kfree(node, M_HAMMER);
1161         }
1162 }
1163
1164 /*
1165  * Flush passively cached B-Tree nodes associated with this buffer.
1166  * This is only called when the buffer is about to be destroyed, so
1167  * none of the nodes should have any references.  The buffer is locked.
1168  *
1169  * We may be interlocked with the buffer.
1170  */
1171 void
1172 hammer_flush_buffer_nodes(hammer_buffer_t buffer)
1173 {
1174         hammer_node_t node;
1175
1176         while ((node = TAILQ_FIRST(&buffer->clist)) != NULL) {
1177                 KKASSERT(node->ondisk == NULL);
1178
1179                 if (node->lock.refs == 0) {
1180                         hammer_ref(&node->lock);
1181                         node->flags |= HAMMER_NODE_FLUSH;
1182                         hammer_rel_node(node);
1183                 } else {
1184                         KKASSERT(node->loading != 0);
1185                         KKASSERT(node->buffer != NULL);
1186                         buffer = node->buffer;
1187                         node->buffer = NULL;
1188                         TAILQ_REMOVE(&buffer->clist, node, entry);
1189                         /* buffer is unreferenced because ondisk is NULL */
1190                 }
1191         }
1192 }
1193
1194
1195 /************************************************************************
1196  *                              ALLOCATORS                              *
1197  ************************************************************************/
1198
1199 /*
1200  * Allocate a B-Tree node.
1201  */
1202 hammer_node_t
1203 hammer_alloc_btree(hammer_transaction_t trans, int *errorp)
1204 {
1205         hammer_buffer_t buffer = NULL;
1206         hammer_node_t node = NULL;
1207         hammer_off_t node_offset;
1208
1209         node_offset = hammer_blockmap_alloc(trans, HAMMER_ZONE_BTREE_INDEX,
1210                                             sizeof(struct hammer_node_ondisk),
1211                                             errorp);
1212         if (*errorp == 0) {
1213                 node = hammer_get_node(trans->hmp, node_offset, 1, errorp);
1214                 hammer_modify_node_noundo(trans, node);
1215                 bzero(node->ondisk, sizeof(*node->ondisk));
1216                 hammer_modify_node_done(node);
1217         }
1218         if (buffer)
1219                 hammer_rel_buffer(buffer, 0);
1220         return(node);
1221 }
1222
1223 #if 0
1224
1225 /*
1226  * The returned buffers are already appropriately marked as being modified.
1227  * If the caller marks them again unnecessary undo records may be generated.
1228  *
1229  * In-band data is indicated by data_bufferp == NULL.  Pass a data_len of 0
1230  * for zero-fill (caller modifies data_len afterwords).
1231  *
1232  * If the caller is responsible for calling hammer_modify_*() prior to making
1233  * any additional modifications to either the returned record buffer or the
1234  * returned data buffer.
1235  */
1236 void *
1237 hammer_alloc_record(hammer_transaction_t trans, 
1238                     hammer_off_t *rec_offp, u_int16_t rec_type, 
1239                     struct hammer_buffer **rec_bufferp,
1240                     int32_t data_len, void **datap,
1241                     hammer_off_t *data_offp,
1242                     struct hammer_buffer **data_bufferp, int *errorp)
1243 {
1244         hammer_record_ondisk_t rec;
1245         hammer_off_t rec_offset;
1246         hammer_off_t data_offset;
1247         int32_t reclen;
1248
1249         if (datap)
1250                 *datap = NULL;
1251
1252         /*
1253          * Allocate the record
1254          */
1255         rec_offset = hammer_blockmap_alloc(trans, HAMMER_ZONE_RECORD_INDEX,
1256                                            HAMMER_RECORD_SIZE, errorp);
1257         if (*errorp)
1258                 return(NULL);
1259         if (data_offp)
1260                 *data_offp = 0;
1261
1262         /*
1263          * Allocate data
1264          */
1265         if (data_len) {
1266                 if (data_bufferp == NULL) {
1267                         switch(rec_type) {
1268                         case HAMMER_RECTYPE_DATA:
1269                                 reclen = offsetof(struct hammer_data_record,
1270                                                   data[0]);
1271                                 break;
1272                         case HAMMER_RECTYPE_DIRENTRY:
1273                                 reclen = offsetof(struct hammer_entry_record,
1274                                                   name[0]);
1275                                 break;
1276                         default:
1277                                 panic("hammer_alloc_record: illegal "
1278                                       "in-band data");
1279                                 /* NOT REACHED */
1280                                 reclen = 0;
1281                                 break;
1282                         }
1283                         KKASSERT(reclen + data_len <= HAMMER_RECORD_SIZE);
1284                         data_offset = rec_offset + reclen;
1285                 } else if (data_len < HAMMER_BUFSIZE) {
1286                         data_offset = hammer_blockmap_alloc(trans,
1287                                                 HAMMER_ZONE_SMALL_DATA_INDEX,
1288                                                 data_len, errorp);
1289                         *data_offp = data_offset;
1290                 } else {
1291                         data_offset = hammer_blockmap_alloc(trans,
1292                                                 HAMMER_ZONE_LARGE_DATA_INDEX,
1293                                                 data_len, errorp);
1294                         *data_offp = data_offset;
1295                 }
1296         } else {
1297                 data_offset = 0;
1298         }
1299         if (*errorp) {
1300                 hammer_blockmap_free(trans, rec_offset, HAMMER_RECORD_SIZE);
1301                 return(NULL);
1302         }
1303
1304         /*
1305          * Basic return values.
1306          *
1307          * Note that because this is a 'new' buffer, there is no need to
1308          * generate UNDO records for it.
1309          */
1310         *rec_offp = rec_offset;
1311         rec = hammer_bread(trans->hmp, rec_offset, errorp, rec_bufferp);
1312         hammer_modify_buffer(trans, *rec_bufferp, NULL, 0);
1313         bzero(rec, sizeof(*rec));
1314         KKASSERT(*errorp == 0);
1315         rec->base.data_off = data_offset;
1316         rec->base.data_len = data_len;
1317         hammer_modify_buffer_done(*rec_bufferp);
1318
1319         if (data_bufferp) {
1320                 if (data_len) {
1321                         *datap = hammer_bread(trans->hmp, data_offset, errorp,
1322                                               data_bufferp);
1323                         KKASSERT(*errorp == 0);
1324                 } else {
1325                         *datap = NULL;
1326                 }
1327         } else if (data_len) {
1328                 KKASSERT(data_offset + data_len - rec_offset <=
1329                          HAMMER_RECORD_SIZE); 
1330                 if (datap) {
1331                         *datap = (void *)((char *)rec +
1332                                           (int32_t)(data_offset - rec_offset));
1333                 }
1334         } else {
1335                 KKASSERT(datap == NULL);
1336         }
1337         KKASSERT(*errorp == 0);
1338         return(rec);
1339 }
1340
1341 #endif
1342
1343 /*
1344  * Allocate data.  If the address of a data buffer is supplied then
1345  * any prior non-NULL *data_bufferp will be released and *data_bufferp
1346  * will be set to the related buffer.  The caller must release it when
1347  * finally done.  The initial *data_bufferp should be set to NULL by
1348  * the caller.
1349  *
1350  * The caller is responsible for making hammer_modify*() calls on the
1351  * *data_bufferp.
1352  */
1353 void *
1354 hammer_alloc_data(hammer_transaction_t trans, int32_t data_len, 
1355                   hammer_off_t *data_offsetp,
1356                   struct hammer_buffer **data_bufferp, int *errorp)
1357 {
1358         void *data;
1359
1360         /*
1361          * Allocate data
1362          */
1363         if (data_len) {
1364                 if (data_len < HAMMER_BUFSIZE) {
1365                         *data_offsetp = hammer_blockmap_alloc(trans,
1366                                                 HAMMER_ZONE_SMALL_DATA_INDEX,
1367                                                 data_len, errorp);
1368                 } else {
1369                         *data_offsetp = hammer_blockmap_alloc(trans,
1370                                                 HAMMER_ZONE_LARGE_DATA_INDEX,
1371                                                 data_len, errorp);
1372                 }
1373         } else {
1374                 *data_offsetp = 0;
1375         }
1376         if (*errorp == 0 && data_bufferp) {
1377                 if (data_len) {
1378                         data = hammer_bread(trans->hmp, *data_offsetp, errorp,
1379                                             data_bufferp);
1380                         KKASSERT(*errorp == 0);
1381                 } else {
1382                         data = NULL;
1383                 }
1384         } else {
1385                 data = NULL;
1386         }
1387         KKASSERT(*errorp == 0);
1388         return(data);
1389 }
1390
1391 /*
1392  * Sync dirty buffers to the media and clean-up any loose ends.
1393  */
1394 static int hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
1395 static int hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
1396
1397 int
1398 hammer_queue_inodes_flusher(hammer_mount_t hmp, int waitfor)
1399 {
1400         struct hammer_sync_info info;
1401
1402         info.error = 0;
1403         info.waitfor = waitfor;
1404         if (waitfor == MNT_WAIT) {
1405                 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS,
1406                               hammer_sync_scan1, hammer_sync_scan2, &info);
1407         } else {
1408                 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS|VMSC_NOWAIT,
1409                               hammer_sync_scan1, hammer_sync_scan2, &info);
1410         }
1411         return(info.error);
1412 }
1413
1414 int
1415 hammer_sync_hmp(hammer_mount_t hmp, int waitfor)
1416 {
1417         struct hammer_sync_info info;
1418
1419         info.error = 0;
1420         info.waitfor = waitfor;
1421
1422         vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_NOWAIT,
1423                       hammer_sync_scan1, hammer_sync_scan2, &info);
1424         if (waitfor == MNT_WAIT)
1425                 hammer_flusher_sync(hmp);
1426         else
1427                 hammer_flusher_async(hmp);
1428
1429         return(info.error);
1430 }
1431
1432 static int
1433 hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1434 {
1435         struct hammer_inode *ip;
1436
1437         ip = VTOI(vp);
1438         if (vp->v_type == VNON || ip == NULL ||
1439             ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1440              RB_EMPTY(&vp->v_rbdirty_tree))) {
1441                 return(-1);
1442         }
1443         return(0);
1444 }
1445
1446 static int
1447 hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1448 {
1449         struct hammer_sync_info *info = data;
1450         struct hammer_inode *ip;
1451         int error;
1452
1453         ip = VTOI(vp);
1454         if (vp->v_type == VNON || vp->v_type == VBAD ||
1455             ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1456              RB_EMPTY(&vp->v_rbdirty_tree))) {
1457                 return(0);
1458         }
1459         error = VOP_FSYNC(vp, info->waitfor);
1460         if (error)
1461                 info->error = error;
1462         return(0);
1463 }
1464