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