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