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