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