AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
CommitLineData
66325755 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
66325755
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 *
e83ca595 34 * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.55 2008/09/15 17:02:49 dillon Exp $
66325755
MD
35 */
36/*
37 * IO Primitives and buffer cache management
38 *
39 * All major data-tracking structures in HAMMER contain a struct hammer_io
40 * which is used to manage their backing store. We use filesystem buffers
41 * for backing store and we leave them passively associated with their
42 * HAMMER structures.
43 *
9f5097dc 44 * If the kernel tries to destroy a passively associated buf which we cannot
66325755
MD
45 * yet let go we set B_LOCKED in the buffer and then actively released it
46 * later when we can.
47 */
48
49#include "hammer.h"
50#include <sys/fcntl.h>
51#include <sys/nlookup.h>
52#include <sys/buf.h>
53#include <sys/buf2.h>
54
10a5d1ba 55static void hammer_io_modify(hammer_io_t io, int count);
055f5ff8 56static void hammer_io_deallocate(struct buf *bp);
1b0ab2c3
MD
57#if 0
58static void hammer_io_direct_read_complete(struct bio *nbio);
59#endif
60static void hammer_io_direct_write_complete(struct bio *nbio);
43c665ae 61static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
cdb6e4e6 62static void hammer_io_set_modlist(struct hammer_io *io);
748efb59
MD
63static void hammer_io_flush_mark(hammer_volume_t volume);
64static void hammer_io_flush_sync_done(struct bio *bio);
65
055f5ff8
MD
66
67/*
10a5d1ba
MD
68 * Initialize a new, already-zero'd hammer_io structure, or reinitialize
69 * an existing hammer_io structure which may have switched to another type.
055f5ff8
MD
70 */
71void
748efb59 72hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
055f5ff8 73{
748efb59
MD
74 io->volume = volume;
75 io->hmp = volume->io.hmp;
055f5ff8 76 io->type = type;
055f5ff8
MD
77}
78
66325755 79/*
fbc6e32a 80 * Helper routine to disassociate a buffer cache buffer from an I/O
ecca949a 81 * structure. The buffer is unlocked and marked appropriate for reclamation.
055f5ff8
MD
82 *
83 * The io may have 0 or 1 references depending on who called us. The
84 * caller is responsible for dealing with the refs.
85 *
86 * This call can only be made when no action is required on the buffer.
ecca949a
MD
87 *
88 * The caller must own the buffer and the IO must indicate that the
89 * structure no longer owns it (io.released != 0).
66325755
MD
90 */
91static void
ecca949a 92hammer_io_disassociate(hammer_io_structure_t iou)
66325755 93{
055f5ff8 94 struct buf *bp = iou->io.bp;
66325755 95
ecca949a 96 KKASSERT(iou->io.released);
b58c6388 97 KKASSERT(iou->io.modified == 0);
af209b0f 98 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
4d75d829 99 buf_dep_init(bp);
055f5ff8 100 iou->io.bp = NULL;
9f5097dc
MD
101
102 /*
103 * If the buffer was locked someone wanted to get rid of it.
104 */
a99b9ea2
MD
105 if (bp->b_flags & B_LOCKED) {
106 --hammer_count_io_locked;
9f5097dc 107 bp->b_flags &= ~B_LOCKED;
a99b9ea2 108 }
ecca949a
MD
109 if (iou->io.reclaim) {
110 bp->b_flags |= B_NOCACHE|B_RELBUF;
111 iou->io.reclaim = 0;
055f5ff8 112 }
66325755 113
055f5ff8 114 switch(iou->io.type) {
66325755 115 case HAMMER_STRUCTURE_VOLUME:
055f5ff8 116 iou->volume.ondisk = NULL;
66325755 117 break;
10a5d1ba
MD
118 case HAMMER_STRUCTURE_DATA_BUFFER:
119 case HAMMER_STRUCTURE_META_BUFFER:
120 case HAMMER_STRUCTURE_UNDO_BUFFER:
055f5ff8 121 iou->buffer.ondisk = NULL;
66325755
MD
122 break;
123 }
fbc6e32a
MD
124}
125
126/*
055f5ff8 127 * Wait for any physical IO to complete
fbc6e32a 128 */
1b0ab2c3 129void
055f5ff8 130hammer_io_wait(hammer_io_t io)
fbc6e32a 131{
055f5ff8
MD
132 if (io->running) {
133 crit_enter();
134 tsleep_interlock(io);
135 io->waiting = 1;
136 for (;;) {
137 tsleep(io, 0, "hmrflw", 0);
138 if (io->running == 0)
139 break;
140 tsleep_interlock(io);
141 io->waiting = 1;
142 if (io->running == 0)
143 break;
144 }
145 crit_exit();
66325755
MD
146 }
147}
148
af209b0f
MD
149/*
150 * Wait for all hammer_io-initated write I/O's to complete. This is not
151 * supposed to count direct I/O's but some can leak through (for
152 * non-full-sized direct I/Os).
153 */
154void
155hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
156{
748efb59 157 hammer_io_flush_sync(hmp);
af209b0f 158 crit_enter();
f5a07a7a
MD
159 while (hmp->io_running_space)
160 tsleep(&hmp->io_running_space, 0, ident, 0);
af209b0f
MD
161 crit_exit();
162}
163
2f85fa4d
MD
164#define HAMMER_MAXRA 4
165
66325755 166/*
10a5d1ba
MD
167 * Load bp for a HAMMER structure. The io must be exclusively locked by
168 * the caller.
2f85fa4d 169 *
a99b9ea2
MD
170 * This routine is mostly used on meta-data and small-data blocks. Generally
171 * speaking HAMMER assumes some locality of reference and will cluster
172 * a 64K read.
af209b0f
MD
173 *
174 * Note that clustering occurs at the device layer, not the logical layer.
175 * If the buffers do not apply to the current operation they may apply to
176 * some other.
66325755
MD
177 */
178int
2f85fa4d 179hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
66325755
MD
180{
181 struct buf *bp;
2f85fa4d 182 int error;
66325755
MD
183
184 if ((bp = io->bp) == NULL) {
f5a07a7a 185 hammer_count_io_running_read += io->bytes;
ce0138a6
MD
186 if (hammer_cluster_enable) {
187 error = cluster_read(devvp, limit,
188 io->offset, io->bytes,
189 HAMMER_CLUSTER_SIZE,
190 HAMMER_CLUSTER_BUFS, &io->bp);
191 } else {
192 error = bread(devvp, io->offset, io->bytes, &io->bp);
193 }
194 hammer_stats_disk_read += io->bytes;
f5a07a7a 195 hammer_count_io_running_read -= io->bytes;
cdb6e4e6
MD
196
197 /*
198 * The code generally assumes b_ops/b_dep has been set-up,
199 * even if we error out here.
200 */
201 bp = io->bp;
202 bp->b_ops = &hammer_bioops;
203 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
204 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
205 BUF_KERNPROC(bp);
10a5d1ba
MD
206 KKASSERT(io->modified == 0);
207 KKASSERT(io->running == 0);
208 KKASSERT(io->waiting == 0);
66325755
MD
209 io->released = 0; /* we hold an active lock on bp */
210 } else {
211 error = 0;
212 }
213 return(error);
214}
215
216/*
217 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
10a5d1ba
MD
218 * Must be called with the IO exclusively locked.
219 *
66325755 220 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
10a5d1ba
MD
221 * I/O by forcing the buffer to not be in a released state before calling
222 * it.
055f5ff8 223 *
10a5d1ba
MD
224 * This function will also mark the IO as modified but it will not
225 * increment the modify_refs count.
66325755
MD
226 */
227int
228hammer_io_new(struct vnode *devvp, struct hammer_io *io)
229{
230 struct buf *bp;
231
232 if ((bp = io->bp) == NULL) {
4a2796f3 233 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
66325755
MD
234 bp = io->bp;
235 bp->b_ops = &hammer_bioops;
af209b0f 236 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
66325755 237 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
055f5ff8 238 io->released = 0;
10a5d1ba 239 KKASSERT(io->running == 0);
055f5ff8 240 io->waiting = 0;
66325755
MD
241 BUF_KERNPROC(bp);
242 } else {
243 if (io->released) {
244 regetblk(bp);
66325755 245 BUF_KERNPROC(bp);
d113fda1 246 io->released = 0;
66325755
MD
247 }
248 }
10a5d1ba 249 hammer_io_modify(io, 0);
66325755
MD
250 vfs_bio_clrbuf(bp);
251 return(0);
252}
253
254/*
47637bff 255 * Remove potential device level aliases against buffers managed by high level
362ec2dc
MD
256 * vnodes. Aliases can also be created due to mixed buffer sizes or via
257 * direct access to the backing store device.
e469566b
MD
258 *
259 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
260 * does not exist its backing VM pages might, and we have to invalidate
261 * those as well or a getblk() will reinstate them.
362ec2dc
MD
262 *
263 * Buffer cache buffers associated with hammer_buffers cannot be
264 * invalidated.
47637bff 265 */
362ec2dc 266int
47637bff
MD
267hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
268{
cebe9493 269 hammer_io_structure_t iou;
47637bff
MD
270 hammer_off_t phys_offset;
271 struct buf *bp;
362ec2dc 272 int error;
47637bff
MD
273
274 phys_offset = volume->ondisk->vol_buf_beg +
275 (zone2_offset & HAMMER_OFF_SHORT_MASK);
4a2796f3 276 crit_enter();
e469566b 277 if ((bp = findblk(volume->devvp, phys_offset)) != NULL)
4a2796f3 278 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
e469566b
MD
279 else
280 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
281 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
362ec2dc 282#if 0
5c8d05e2 283 hammer_ref(&iou->io.lock);
e469566b
MD
284 hammer_io_clear_modify(&iou->io, 1);
285 bundirty(bp);
e83ca595
MD
286 iou->io.released = 0;
287 BUF_KERNPROC(bp);
e469566b 288 iou->io.reclaim = 1;
5c8d05e2 289 iou->io.waitdep = 1;
e83ca595 290 KKASSERT(iou->io.lock.refs == 1);
5c8d05e2
MD
291 hammer_rel_buffer(&iou->buffer, 0);
292 /*hammer_io_deallocate(bp);*/
362ec2dc 293#endif
04b04ca6 294 bqrelse(bp);
362ec2dc 295 error = EAGAIN;
e469566b
MD
296 } else {
297 KKASSERT((bp->b_flags & B_LOCKED) == 0);
298 bundirty(bp);
299 bp->b_flags |= B_NOCACHE|B_RELBUF;
e83ca595 300 brelse(bp);
362ec2dc 301 error = 0;
47637bff 302 }
4a2796f3 303 crit_exit();
362ec2dc 304 return(error);
47637bff
MD
305}
306
307/*
b3deaf57 308 * This routine is called on the last reference to a hammer structure.
ecca949a 309 * The io is usually interlocked with io.loading and io.refs must be 1.
b3deaf57 310 *
ecca949a
MD
311 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
312 * simply means the caller finishes decrementing the ref-count on the
313 * IO structure then brelse()'s the bp. The bp may or may not still be
314 * passively associated with the IO.
315 *
316 * The only requirement here is that modified meta-data and volume-header
317 * buffer may NOT be disassociated from the IO structure, and consequently
318 * we also leave such buffers actively associated with the IO if they already
319 * are (since the kernel can't do anything with them anyway). Only the
320 * flusher is allowed to write such buffers out. Modified pure-data and
321 * undo buffers are returned to the kernel but left passively associated
322 * so we can track when the kernel writes the bp out.
66325755 323 */
ecca949a 324struct buf *
09ac686b 325hammer_io_release(struct hammer_io *io, int flush)
66325755 326{
9f5097dc 327 union hammer_io_structure *iou = (void *)io;
66325755
MD
328 struct buf *bp;
329
055f5ff8 330 if ((bp = io->bp) == NULL)
ecca949a 331 return(NULL);
fbc6e32a 332
055f5ff8 333 /*
10a5d1ba
MD
334 * Try to flush a dirty IO to disk if asked to by the
335 * caller or if the kernel tried to flush the buffer in the past.
055f5ff8 336 *
10a5d1ba
MD
337 * Kernel-initiated flushes are only allowed for pure-data buffers.
338 * meta-data and volume buffers can only be flushed explicitly
339 * by HAMMER.
055f5ff8 340 */
10a5d1ba 341 if (io->modified) {
09ac686b 342 if (flush) {
10a5d1ba
MD
343 hammer_io_flush(io);
344 } else if (bp->b_flags & B_LOCKED) {
345 switch(io->type) {
346 case HAMMER_STRUCTURE_DATA_BUFFER:
347 case HAMMER_STRUCTURE_UNDO_BUFFER:
348 hammer_io_flush(io);
349 break;
350 default:
351 break;
352 }
353 } /* else no explicit request to flush the buffer */
354 }
fbc6e32a 355
055f5ff8 356 /*
5c8d05e2
MD
357 * Wait for the IO to complete if asked to. This occurs when
358 * the buffer must be disposed of definitively during an umount
359 * or buffer invalidation.
055f5ff8 360 */
b58c6388 361 if (io->waitdep && io->running) {
055f5ff8
MD
362 hammer_io_wait(io);
363 }
364
365 /*
10a5d1ba
MD
366 * Return control of the buffer to the kernel (with the provisio
367 * that our bioops can override kernel decisions with regards to
368 * the buffer).
055f5ff8 369 */
cebe9493 370 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
10a5d1ba
MD
371 /*
372 * Always disassociate the bp if an explicit flush
373 * was requested and the IO completed with no error
374 * (so unmount can really clean up the structure).
375 */
055f5ff8 376 if (io->released) {
b3deaf57 377 regetblk(bp);
46fe7ae1 378 BUF_KERNPROC(bp);
ecca949a
MD
379 } else {
380 io->released = 1;
055f5ff8 381 }
ecca949a
MD
382 hammer_io_disassociate((hammer_io_structure_t)io);
383 /* return the bp */
055f5ff8 384 } else if (io->modified) {
10a5d1ba 385 /*
ecca949a
MD
386 * Only certain IO types can be released to the kernel if
387 * the buffer has been modified.
388 *
389 * volume and meta-data IO types may only be explicitly
390 * flushed by HAMMER.
10a5d1ba
MD
391 */
392 switch(io->type) {
393 case HAMMER_STRUCTURE_DATA_BUFFER:
394 case HAMMER_STRUCTURE_UNDO_BUFFER:
395 if (io->released == 0) {
396 io->released = 1;
397 bdwrite(bp);
398 }
399 break;
400 default:
401 break;
055f5ff8 402 }
ecca949a 403 bp = NULL; /* bp left associated */
055f5ff8 404 } else if (io->released == 0) {
10a5d1ba
MD
405 /*
406 * Clean buffers can be generally released to the kernel.
407 * We leave the bp passively associated with the HAMMER
408 * structure and use bioops to disconnect it later on
409 * if the kernel wants to discard the buffer.
ecca949a
MD
410 *
411 * We can steal the structure's ownership of the bp.
10a5d1ba 412 */
ecca949a 413 io->released = 1;
9f5097dc 414 if (bp->b_flags & B_LOCKED) {
ecca949a
MD
415 hammer_io_disassociate(iou);
416 /* return the bp */
9f5097dc 417 } else {
cebe9493 418 if (io->reclaim) {
ecca949a
MD
419 hammer_io_disassociate(iou);
420 /* return the bp */
cebe9493 421 } else {
ecca949a 422 /* return the bp (bp passively associated) */
cebe9493 423 }
9f5097dc 424 }
19b97e01
MD
425 } else {
426 /*
af209b0f
MD
427 * A released buffer is passively associate with our
428 * hammer_io structure. The kernel cannot destroy it
429 * without making a bioops call. If the kernel (B_LOCKED)
430 * or we (reclaim) requested that the buffer be destroyed
431 * we destroy it, otherwise we do a quick get/release to
432 * reset its position in the kernel's LRU list.
433 *
434 * Leaving the buffer passively associated allows us to
435 * use the kernel's LRU buffer flushing mechanisms rather
436 * then rolling our own.
cb51be26
MD
437 *
438 * XXX there are two ways of doing this. We can re-acquire
439 * and passively release to reset the LRU, or not.
19b97e01 440 */
af209b0f 441 if (io->running == 0) {
19b97e01 442 regetblk(bp);
cebe9493 443 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
ecca949a
MD
444 hammer_io_disassociate(iou);
445 /* return the bp */
9f5097dc 446 } else {
ecca949a 447 /* return the bp (bp passively associated) */
9f5097dc 448 }
ecca949a
MD
449 } else {
450 /*
451 * bp is left passively associated but we do not
452 * try to reacquire it. Interactions with the io
453 * structure will occur on completion of the bp's
454 * I/O.
455 */
456 bp = NULL;
19b97e01 457 }
66325755 458 }
ecca949a 459 return(bp);
66325755
MD
460}
461
462/*
b33e2cc0
MD
463 * This routine is called with a locked IO when a flush is desired and
464 * no other references to the structure exists other then ours. This
465 * routine is ONLY called when HAMMER believes it is safe to flush a
466 * potentially modified buffer out.
fbc6e32a
MD
467 */
468void
055f5ff8 469hammer_io_flush(struct hammer_io *io)
fbc6e32a
MD
470{
471 struct buf *bp;
fbc6e32a 472
055f5ff8 473 /*
10a5d1ba 474 * Degenerate case - nothing to flush if nothing is dirty.
055f5ff8 475 */
b58c6388 476 if (io->modified == 0) {
055f5ff8 477 return;
b58c6388 478 }
055f5ff8
MD
479
480 KKASSERT(io->bp);
9f5097dc 481 KKASSERT(io->modify_refs <= 0);
055f5ff8 482
b33e2cc0 483 /*
77062c8a
MD
484 * Acquire ownership of the bp, particularly before we clear our
485 * modified flag.
486 *
487 * We are going to bawrite() this bp. Don't leave a window where
488 * io->released is set, we actually own the bp rather then our
489 * buffer.
490 */
491 bp = io->bp;
492 if (io->released) {
493 regetblk(bp);
494 /* BUF_KERNPROC(io->bp); */
495 /* io->released = 0; */
496 KKASSERT(io->released);
497 KKASSERT(io->bp == bp);
498 }
499 io->released = 1;
500
501 /*
10a5d1ba
MD
502 * Acquire exclusive access to the bp and then clear the modified
503 * state of the buffer prior to issuing I/O to interlock any
504 * modifications made while the I/O is in progress. This shouldn't
505 * happen anyway but losing data would be worse. The modified bit
506 * will be rechecked after the IO completes.
507 *
4a2796f3
MD
508 * NOTE: This call also finalizes the buffer's content (inval == 0).
509 *
b33e2cc0
MD
510 * This is only legal when lock.refs == 1 (otherwise we might clear
511 * the modified bit while there are still users of the cluster
512 * modifying the data).
513 *
b33e2cc0
MD
514 * Do this before potentially blocking so any attempt to modify the
515 * ondisk while we are blocked blocks waiting for us.
516 */
5c8d05e2 517 hammer_ref(&io->lock);
4a2796f3 518 hammer_io_clear_modify(io, 0);
5c8d05e2 519 hammer_unref(&io->lock);
bcac4bbb
MD
520
521 /*
10a5d1ba
MD
522 * Transfer ownership to the kernel and initiate I/O.
523 */
055f5ff8 524 io->running = 1;
f5a07a7a
MD
525 io->hmp->io_running_space += io->bytes;
526 hammer_count_io_running_write += io->bytes;
055f5ff8 527 bawrite(bp);
748efb59 528 hammer_io_flush_mark(io->volume);
fbc6e32a
MD
529}
530
055f5ff8
MD
531/************************************************************************
532 * BUFFER DIRTYING *
533 ************************************************************************
534 *
535 * These routines deal with dependancies created when IO buffers get
536 * modified. The caller must call hammer_modify_*() on a referenced
537 * HAMMER structure prior to modifying its on-disk data.
0b075555 538 *
055f5ff8
MD
539 * Any intent to modify an IO buffer acquires the related bp and imposes
540 * various write ordering dependancies.
0b075555 541 */
055f5ff8
MD
542
543/*
10a5d1ba
MD
544 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
545 * are locked until the flusher can deal with them, pure data buffers
546 * can be written out.
055f5ff8 547 */
10a5d1ba 548static
b58c6388 549void
10a5d1ba 550hammer_io_modify(hammer_io_t io, int count)
0b075555 551{
46fe7ae1 552 /*
9f5097dc
MD
553 * io->modify_refs must be >= 0
554 */
555 while (io->modify_refs < 0) {
556 io->waitmod = 1;
557 tsleep(io, 0, "hmrmod", 0);
558 }
559
560 /*
46fe7ae1
MD
561 * Shortcut if nothing to do.
562 */
0b075555 563 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
10a5d1ba 564 io->modify_refs += count;
b58c6388
MD
565 if (io->modified && io->released == 0)
566 return;
46fe7ae1
MD
567
568 hammer_lock_ex(&io->lock);
10a5d1ba 569 if (io->modified == 0) {
cdb6e4e6 570 hammer_io_set_modlist(io);
10a5d1ba
MD
571 io->modified = 1;
572 }
46fe7ae1
MD
573 if (io->released) {
574 regetblk(io->bp);
575 BUF_KERNPROC(io->bp);
576 io->released = 0;
577 KKASSERT(io->modified != 0);
578 }
46fe7ae1 579 hammer_unlock(&io->lock);
055f5ff8
MD
580}
581
10a5d1ba
MD
582static __inline
583void
584hammer_io_modify_done(hammer_io_t io)
585{
586 KKASSERT(io->modify_refs > 0);
587 --io->modify_refs;
9f5097dc
MD
588 if (io->modify_refs == 0 && io->waitmod) {
589 io->waitmod = 0;
590 wakeup(io);
591 }
592}
593
594void
595hammer_io_write_interlock(hammer_io_t io)
596{
597 while (io->modify_refs != 0) {
598 io->waitmod = 1;
599 tsleep(io, 0, "hmrmod", 0);
600 }
601 io->modify_refs = -1;
602}
603
604void
605hammer_io_done_interlock(hammer_io_t io)
606{
607 KKASSERT(io->modify_refs == -1);
608 io->modify_refs = 0;
609 if (io->waitmod) {
610 io->waitmod = 0;
611 wakeup(io);
612 }
10a5d1ba
MD
613}
614
2f85fa4d
MD
615/*
616 * Caller intends to modify a volume's ondisk structure.
617 *
618 * This is only allowed if we are the flusher or we have a ref on the
619 * sync_lock.
620 */
055f5ff8 621void
36f82b23
MD
622hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
623 void *base, int len)
055f5ff8 624{
2f85fa4d 625 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
055f5ff8 626
2f85fa4d 627 hammer_io_modify(&volume->io, 1);
47197d71
MD
628 if (len) {
629 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
630 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
059819e3 631 hammer_generate_undo(trans, &volume->io,
47197d71
MD
632 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
633 base, len);
634 }
0b075555
MD
635}
636
055f5ff8 637/*
2f85fa4d
MD
638 * Caller intends to modify a buffer's ondisk structure.
639 *
640 * This is only allowed if we are the flusher or we have a ref on the
641 * sync_lock.
055f5ff8 642 */
0b075555 643void
36f82b23
MD
644hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
645 void *base, int len)
46fe7ae1 646{
2f85fa4d
MD
647 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
648
10a5d1ba 649 hammer_io_modify(&buffer->io, 1);
47197d71
MD
650 if (len) {
651 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
652 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
059819e3 653 hammer_generate_undo(trans, &buffer->io,
34d829f7 654 buffer->zone2_offset + rel_offset,
47197d71
MD
655 base, len);
656 }
46fe7ae1
MD
657}
658
10a5d1ba
MD
659void
660hammer_modify_volume_done(hammer_volume_t volume)
661{
662 hammer_io_modify_done(&volume->io);
663}
664
665void
666hammer_modify_buffer_done(hammer_buffer_t buffer)
667{
668 hammer_io_modify_done(&buffer->io);
669}
670
46fe7ae1 671/*
4a2796f3
MD
672 * Mark an entity as not being dirty any more and finalize any
673 * delayed adjustments to the buffer.
674 *
675 * Delayed adjustments are an important performance enhancement, allowing
676 * us to avoid recalculating B-Tree node CRCs over and over again when
677 * making bulk-modifications to the B-Tree.
678 *
679 * If inval is non-zero delayed adjustments are ignored.
5c8d05e2
MD
680 *
681 * This routine may dereference related btree nodes and cause the
682 * buffer to be dereferenced. The caller must own a reference on io.
61aeeb33
MD
683 */
684void
4a2796f3 685hammer_io_clear_modify(struct hammer_io *io, int inval)
61aeeb33 686{
4a2796f3
MD
687 if (io->modified == 0)
688 return;
689
690 /*
691 * Take us off the mod-list and clear the modified bit.
692 */
693 KKASSERT(io->mod_list != NULL);
694 if (io->mod_list == &io->hmp->volu_list ||
695 io->mod_list == &io->hmp->meta_list) {
f5a07a7a
MD
696 io->hmp->locked_dirty_space -= io->bytes;
697 hammer_count_dirtybufspace -= io->bytes;
4a2796f3
MD
698 }
699 TAILQ_REMOVE(io->mod_list, io, mod_entry);
700 io->mod_list = NULL;
701 io->modified = 0;
702
703 /*
704 * If this bit is not set there are no delayed adjustments.
705 */
706 if (io->gencrc == 0)
707 return;
708 io->gencrc = 0;
709
710 /*
711 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
712 * on the node (& underlying buffer). Release the node after clearing
713 * the flag.
714 */
715 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
716 hammer_buffer_t buffer = (void *)io;
717 hammer_node_t node;
718
719restart:
720 TAILQ_FOREACH(node, &buffer->clist, entry) {
721 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
722 continue;
723 node->flags &= ~HAMMER_NODE_NEEDSCRC;
724 KKASSERT(node->ondisk);
725 if (inval == 0)
726 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
727 hammer_rel_node(node);
728 goto restart;
61aeeb33 729 }
cebe9493 730 }
5c8d05e2
MD
731 /* caller must still have ref on io */
732 KKASSERT(io->lock.refs > 0);
cebe9493
MD
733}
734
735/*
736 * Clear the IO's modify list. Even though the IO is no longer modified
737 * it may still be on the lose_list. This routine is called just before
738 * the governing hammer_buffer is destroyed.
739 */
740void
741hammer_io_clear_modlist(struct hammer_io *io)
742{
4a2796f3 743 KKASSERT(io->modified == 0);
cebe9493 744 if (io->mod_list) {
a99b9ea2 745 crit_enter(); /* biodone race against list */
cebe9493
MD
746 KKASSERT(io->mod_list == &io->hmp->lose_list);
747 TAILQ_REMOVE(io->mod_list, io, mod_entry);
748 io->mod_list = NULL;
a99b9ea2 749 crit_exit();
61aeeb33
MD
750 }
751}
752
cdb6e4e6
MD
753static void
754hammer_io_set_modlist(struct hammer_io *io)
755{
756 struct hammer_mount *hmp = io->hmp;
757
758 KKASSERT(io->mod_list == NULL);
759
760 switch(io->type) {
761 case HAMMER_STRUCTURE_VOLUME:
762 io->mod_list = &hmp->volu_list;
763 hmp->locked_dirty_space += io->bytes;
764 hammer_count_dirtybufspace += io->bytes;
765 break;
766 case HAMMER_STRUCTURE_META_BUFFER:
767 io->mod_list = &hmp->meta_list;
768 hmp->locked_dirty_space += io->bytes;
769 hammer_count_dirtybufspace += io->bytes;
770 break;
771 case HAMMER_STRUCTURE_UNDO_BUFFER:
772 io->mod_list = &hmp->undo_list;
773 break;
774 case HAMMER_STRUCTURE_DATA_BUFFER:
775 io->mod_list = &hmp->data_list;
776 break;
777 }
778 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
779}
780
055f5ff8
MD
781/************************************************************************
782 * HAMMER_BIOOPS *
783 ************************************************************************
784 *
66325755
MD
785 */
786
787/*
055f5ff8 788 * Pre-IO initiation kernel callback - cluster build only
66325755 789 */
66325755
MD
790static void
791hammer_io_start(struct buf *bp)
792{
793}
794
055f5ff8 795/*
7bc5b8c2 796 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
b33e2cc0
MD
797 *
798 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
799 * may also be set if we were marking a cluster header open. Only remove
800 * our dependancy if the modified bit is clear.
055f5ff8 801 */
66325755
MD
802static void
803hammer_io_complete(struct buf *bp)
804{
055f5ff8
MD
805 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
806
807 KKASSERT(iou->io.released == 1);
fbc6e32a 808
bf3b416b
MD
809 /*
810 * Deal with people waiting for I/O to drain
811 */
f90dde4c 812 if (iou->io.running) {
cdb6e4e6
MD
813 /*
814 * Deal with critical write errors. Once a critical error
815 * has been flagged in hmp the UNDO FIFO will not be updated.
816 * That way crash recover will give us a consistent
817 * filesystem.
818 *
819 * Because of this we can throw away failed UNDO buffers. If
820 * we throw away META or DATA buffers we risk corrupting
821 * the now read-only version of the filesystem visible to
822 * the user. Clear B_ERROR so the buffer is not re-dirtied
823 * by the kernel and ref the io so it doesn't get thrown
824 * away.
825 */
826 if (bp->b_flags & B_ERROR) {
827 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
828 "while flushing meta-data");
829 switch(iou->io.type) {
830 case HAMMER_STRUCTURE_UNDO_BUFFER:
831 break;
832 default:
833 if (iou->io.ioerror == 0) {
834 iou->io.ioerror = 1;
835 if (iou->io.lock.refs == 0)
836 ++hammer_count_refedbufs;
837 hammer_ref(&iou->io.lock);
838 }
839 break;
840 }
841 bp->b_flags &= ~B_ERROR;
842 bundirty(bp);
843#if 0
844 hammer_io_set_modlist(&iou->io);
845 iou->io.modified = 1;
846#endif
847 }
ce0138a6 848 hammer_stats_disk_write += iou->io.bytes;
f5a07a7a
MD
849 hammer_count_io_running_write -= iou->io.bytes;
850 iou->io.hmp->io_running_space -= iou->io.bytes;
851 if (iou->io.hmp->io_running_space == 0)
852 wakeup(&iou->io.hmp->io_running_space);
853 KKASSERT(iou->io.hmp->io_running_space >= 0);
f90dde4c 854 iou->io.running = 0;
ce0138a6
MD
855 } else {
856 hammer_stats_disk_read += iou->io.bytes;
f90dde4c
MD
857 }
858
055f5ff8
MD
859 if (iou->io.waiting) {
860 iou->io.waiting = 0;
861 wakeup(iou);
862 }
863
864 /*
bf3b416b
MD
865 * If B_LOCKED is set someone wanted to deallocate the bp at some
866 * point, do it now if refs has become zero.
055f5ff8
MD
867 */
868 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
b33e2cc0 869 KKASSERT(iou->io.modified == 0);
a99b9ea2 870 --hammer_count_io_locked;
d5ef456e 871 bp->b_flags &= ~B_LOCKED;
055f5ff8
MD
872 hammer_io_deallocate(bp);
873 /* structure may be dead now */
874 }
66325755
MD
875}
876
877/*
878 * Callback from kernel when it wishes to deallocate a passively
10a5d1ba
MD
879 * associated structure. This mostly occurs with clean buffers
880 * but it may be possible for a holding structure to be marked dirty
7bc5b8c2 881 * while its buffer is passively associated. The caller owns the bp.
66325755
MD
882 *
883 * If we cannot disassociate we set B_LOCKED to prevent the buffer
884 * from getting reused.
46fe7ae1
MD
885 *
886 * WARNING: Because this can be called directly by getnewbuf we cannot
887 * recurse into the tree. If a bp cannot be immediately disassociated
888 * our only recourse is to set B_LOCKED.
7bc5b8c2
MD
889 *
890 * WARNING: This may be called from an interrupt via hammer_io_complete()
66325755
MD
891 */
892static void
893hammer_io_deallocate(struct buf *bp)
894{
055f5ff8 895 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
66325755 896
055f5ff8 897 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
46fe7ae1 898 if (iou->io.lock.refs > 0 || iou->io.modified) {
10a5d1ba
MD
899 /*
900 * It is not legal to disassociate a modified buffer. This
901 * case really shouldn't ever occur.
902 */
055f5ff8 903 bp->b_flags |= B_LOCKED;
a99b9ea2 904 ++hammer_count_io_locked;
055f5ff8 905 } else {
10a5d1ba
MD
906 /*
907 * Disassociate the BP. If the io has no refs left we
908 * have to add it to the loose list.
909 */
ecca949a
MD
910 hammer_io_disassociate(iou);
911 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
912 KKASSERT(iou->io.bp == NULL);
10a5d1ba 913 KKASSERT(iou->io.mod_list == NULL);
a99b9ea2 914 crit_enter(); /* biodone race against list */
10a5d1ba
MD
915 iou->io.mod_list = &iou->io.hmp->lose_list;
916 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
a99b9ea2 917 crit_exit();
66325755 918 }
66325755 919 }
66325755
MD
920}
921
922static int
923hammer_io_fsync(struct vnode *vp)
924{
925 return(0);
926}
927
928/*
929 * NOTE: will not be called unless we tell the kernel about the
930 * bioops. Unused... we use the mount's VFS_SYNC instead.
931 */
932static int
933hammer_io_sync(struct mount *mp)
934{
935 return(0);
936}
937
938static void
939hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
940{
941}
942
943/*
944 * I/O pre-check for reading and writing. HAMMER only uses this for
945 * B_CACHE buffers so checkread just shouldn't happen, but if it does
946 * allow it.
947 *
fbc6e32a
MD
948 * Writing is a different case. We don't want the kernel to try to write
949 * out a buffer that HAMMER may be modifying passively or which has a
10a5d1ba
MD
950 * dependancy. In addition, kernel-demanded writes can only proceed for
951 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
952 * buffer types can only be explicitly written by the flusher.
fbc6e32a 953 *
10a5d1ba
MD
954 * checkwrite will only be called for bdwrite()n buffers. If we return
955 * success the kernel is guaranteed to initiate the buffer write.
66325755
MD
956 */
957static int
958hammer_io_checkread(struct buf *bp)
959{
960 return(0);
961}
962
963static int
964hammer_io_checkwrite(struct buf *bp)
965{
10a5d1ba 966 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
66325755 967
77062c8a
MD
968 /*
969 * This shouldn't happen under normal operation.
970 */
971 if (io->type == HAMMER_STRUCTURE_VOLUME ||
972 io->type == HAMMER_STRUCTURE_META_BUFFER) {
973 if (!panicstr)
974 panic("hammer_io_checkwrite: illegal buffer");
a99b9ea2
MD
975 if ((bp->b_flags & B_LOCKED) == 0) {
976 bp->b_flags |= B_LOCKED;
977 ++hammer_count_io_locked;
978 }
77062c8a
MD
979 return(1);
980 }
c9b9e29d 981
055f5ff8 982 /*
10a5d1ba
MD
983 * We can only clear the modified bit if the IO is not currently
984 * undergoing modification. Otherwise we may miss changes.
5c8d05e2
MD
985 *
986 * Only data and undo buffers can reach here. These buffers do
987 * not have terminal crc functions but we temporarily reference
988 * the IO anyway, just in case.
b33e2cc0 989 */
5c8d05e2
MD
990 if (io->modify_refs == 0 && io->modified) {
991 hammer_ref(&io->lock);
4a2796f3 992 hammer_io_clear_modify(io, 0);
5c8d05e2
MD
993 hammer_unref(&io->lock);
994 } else if (io->modified) {
995 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
996 }
f90dde4c
MD
997
998 /*
999 * The kernel is going to start the IO, set io->running.
1000 */
1001 KKASSERT(io->running == 0);
1002 io->running = 1;
f5a07a7a
MD
1003 io->hmp->io_running_space += io->bytes;
1004 hammer_count_io_running_write += io->bytes;
055f5ff8 1005 return(0);
66325755
MD
1006}
1007
8cd0a023 1008/*
66325755
MD
1009 * Return non-zero if we wish to delay the kernel's attempt to flush
1010 * this buffer to disk.
1011 */
1012static int
1013hammer_io_countdeps(struct buf *bp, int n)
1014{
1015 return(0);
1016}
1017
1018struct bio_ops hammer_bioops = {
1019 .io_start = hammer_io_start,
1020 .io_complete = hammer_io_complete,
1021 .io_deallocate = hammer_io_deallocate,
1022 .io_fsync = hammer_io_fsync,
1023 .io_sync = hammer_io_sync,
1024 .io_movedeps = hammer_io_movedeps,
1025 .io_countdeps = hammer_io_countdeps,
1026 .io_checkread = hammer_io_checkread,
1027 .io_checkwrite = hammer_io_checkwrite,
1028};
1029
47637bff
MD
1030/************************************************************************
1031 * DIRECT IO OPS *
1032 ************************************************************************
1033 *
1034 * These functions operate directly on the buffer cache buffer associated
1035 * with a front-end vnode rather then a back-end device vnode.
1036 */
1037
1038/*
1039 * Read a buffer associated with a front-end vnode directly from the
1b0ab2c3
MD
1040 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1041 * we validate the CRC.
a99b9ea2 1042 *
1b0ab2c3
MD
1043 * We must check for the presence of a HAMMER buffer to handle the case
1044 * where the reblocker has rewritten the data (which it does via the HAMMER
1045 * buffer system, not via the high-level vnode buffer cache), but not yet
1046 * committed the buffer to the media.
47637bff
MD
1047 */
1048int
1b0ab2c3
MD
1049hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1050 hammer_btree_leaf_elm_t leaf)
47637bff 1051{
1b0ab2c3 1052 hammer_off_t buf_offset;
47637bff
MD
1053 hammer_off_t zone2_offset;
1054 hammer_volume_t volume;
1055 struct buf *bp;
1056 struct bio *nbio;
1057 int vol_no;
1058 int error;
1059
1b0ab2c3
MD
1060 buf_offset = bio->bio_offset;
1061 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1062 HAMMER_ZONE_LARGE_DATA);
1063
1064 /*
1065 * The buffer cache may have an aliased buffer (the reblocker can
1066 * write them). If it does we have to sync any dirty data before
1067 * we can build our direct-read. This is a non-critical code path.
1068 */
1069 bp = bio->bio_buf;
1070 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
4a2796f3 1071
1b0ab2c3
MD
1072 /*
1073 * Resolve to a zone-2 offset. The conversion just requires
1074 * munging the top 4 bits but we want to abstract it anyway
1075 * so the blockmap code can verify the zone assignment.
1076 */
1077 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1078 if (error)
1079 goto done;
43c665ae
MD
1080 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1081 HAMMER_ZONE_RAW_BUFFER);
1082
1b0ab2c3
MD
1083 /*
1084 * Resolve volume and raw-offset for 3rd level bio. The
1085 * offset will be specific to the volume.
1086 */
43c665ae
MD
1087 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1088 volume = hammer_get_volume(hmp, vol_no, &error);
1089 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1090 error = EIO;
1091
47637bff 1092 if (error == 0) {
e469566b
MD
1093 /*
1094 * 3rd level bio
1095 */
43c665ae
MD
1096 nbio = push_bio(bio);
1097 nbio->bio_offset = volume->ondisk->vol_buf_beg +
e469566b 1098 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1b0ab2c3
MD
1099#if 0
1100 /*
1101 * XXX disabled - our CRC check doesn't work if the OS
1102 * does bogus_page replacement on the direct-read.
1103 */
1104 if (leaf && hammer_verify_data) {
1105 nbio->bio_done = hammer_io_direct_read_complete;
1106 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1107 }
1108#endif
ce0138a6 1109 hammer_stats_disk_read += bp->b_bufsize;
43c665ae 1110 vn_strategy(volume->devvp, nbio);
47637bff 1111 }
43c665ae 1112 hammer_rel_volume(volume, 0);
1b0ab2c3 1113done:
47637bff 1114 if (error) {
cebe9493 1115 kprintf("hammer_direct_read: failed @ %016llx\n",
973c11b9 1116 (long long)zone2_offset);
47637bff
MD
1117 bp->b_error = error;
1118 bp->b_flags |= B_ERROR;
1119 biodone(bio);
1120 }
1121 return(error);
1122}
1123
1b0ab2c3
MD
1124#if 0
1125/*
1126 * On completion of the BIO this callback must check the data CRC
1127 * and chain to the previous bio.
1128 */
1129static
1130void
1131hammer_io_direct_read_complete(struct bio *nbio)
1132{
1133 struct bio *obio;
1134 struct buf *bp;
1135 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1136
1137 bp = nbio->bio_buf;
1138 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1139 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1140 nbio->bio_offset, bp->b_bufsize);
1141 if (hammer_debug_debug)
1142 Debugger("");
1143 bp->b_flags |= B_ERROR;
1144 bp->b_error = EIO;
1145 }
1146 obio = pop_bio(nbio);
1147 biodone(obio);
1148}
1149#endif
1150
47637bff
MD
1151/*
1152 * Write a buffer associated with a front-end vnode directly to the
1153 * disk media. The bio may be issued asynchronously.
1b0ab2c3
MD
1154 *
1155 * The BIO is associated with the specified record and RECF_DIRECT_IO
e469566b 1156 * is set. The recorded is added to its object.
47637bff
MD
1157 */
1158int
1b0ab2c3 1159hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
47637bff
MD
1160 struct bio *bio)
1161{
1b0ab2c3 1162 hammer_btree_leaf_elm_t leaf = &record->leaf;
0832c9bb 1163 hammer_off_t buf_offset;
47637bff
MD
1164 hammer_off_t zone2_offset;
1165 hammer_volume_t volume;
0832c9bb 1166 hammer_buffer_t buffer;
47637bff
MD
1167 struct buf *bp;
1168 struct bio *nbio;
0832c9bb 1169 char *ptr;
47637bff
MD
1170 int vol_no;
1171 int error;
1172
0832c9bb
MD
1173 buf_offset = leaf->data_offset;
1174
1175 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
47637bff
MD
1176 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1177
0832c9bb 1178 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
4a2796f3 1179 leaf->data_len >= HAMMER_BUFSIZE) {
0832c9bb
MD
1180 /*
1181 * We are using the vnode's bio to write directly to the
1182 * media, any hammer_buffer at the same zone-X offset will
1183 * now have stale data.
1184 */
1185 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
47637bff
MD
1186 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1187 volume = hammer_get_volume(hmp, vol_no, &error);
1188
1189 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1190 error = EIO;
1191 if (error == 0) {
0832c9bb 1192 bp = bio->bio_buf;
4a2796f3 1193 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
e469566b 1194 /*
4a2796f3
MD
1195 hammer_del_buffers(hmp, buf_offset,
1196 zone2_offset, bp->b_bufsize);
e469566b 1197 */
1b0ab2c3 1198
43c665ae
MD
1199 /*
1200 * Second level bio - cached zone2 offset.
1b0ab2c3
MD
1201 *
1202 * (We can put our bio_done function in either the
1203 * 2nd or 3rd level).
43c665ae 1204 */
47637bff 1205 nbio = push_bio(bio);
43c665ae 1206 nbio->bio_offset = zone2_offset;
1b0ab2c3
MD
1207 nbio->bio_done = hammer_io_direct_write_complete;
1208 nbio->bio_caller_info1.ptr = record;
e469566b
MD
1209 record->zone2_offset = zone2_offset;
1210 record->flags |= HAMMER_RECF_DIRECT_IO |
1211 HAMMER_RECF_DIRECT_INVAL;
43c665ae
MD
1212
1213 /*
1214 * Third level bio - raw offset specific to the
1215 * correct volume.
1216 */
1217 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1218 nbio = push_bio(nbio);
47637bff 1219 nbio->bio_offset = volume->ondisk->vol_buf_beg +
0832c9bb 1220 zone2_offset;
ce0138a6 1221 hammer_stats_disk_write += bp->b_bufsize;
47637bff 1222 vn_strategy(volume->devvp, nbio);
748efb59 1223 hammer_io_flush_mark(volume);
47637bff
MD
1224 }
1225 hammer_rel_volume(volume, 0);
0832c9bb 1226 } else {
1b0ab2c3
MD
1227 /*
1228 * Must fit in a standard HAMMER buffer. In this case all
1229 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1230 * does not need to be set-up.
1231 */
0832c9bb
MD
1232 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1233 buffer = NULL;
1234 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1235 if (error == 0) {
0832c9bb 1236 bp = bio->bio_buf;
7bc5b8c2 1237 bp->b_flags |= B_AGE;
0832c9bb
MD
1238 hammer_io_modify(&buffer->io, 1);
1239 bcopy(bp->b_data, ptr, leaf->data_len);
1240 hammer_io_modify_done(&buffer->io);
7bc5b8c2 1241 hammer_rel_buffer(buffer, 0);
0832c9bb
MD
1242 bp->b_resid = 0;
1243 biodone(bio);
1244 }
47637bff 1245 }
e469566b
MD
1246 if (error == 0) {
1247 /*
1248 * The record is all setup now, add it. Potential conflics
1249 * have already been dealt with.
1250 */
1251 error = hammer_mem_add(record);
1252 KKASSERT(error == 0);
1253 } else {
1254 /*
3214ade6
MD
1255 * Major suckage occured. Also note: The record was never added
1256 * to the tree so we do not have to worry about the backend.
e469566b 1257 */
cebe9493 1258 kprintf("hammer_direct_write: failed @ %016llx\n",
973c11b9 1259 (long long)leaf->data_offset);
47637bff
MD
1260 bp = bio->bio_buf;
1261 bp->b_resid = 0;
1262 bp->b_error = EIO;
1263 bp->b_flags |= B_ERROR;
1264 biodone(bio);
e469566b
MD
1265 record->flags |= HAMMER_RECF_DELETED_FE;
1266 hammer_rel_mem_record(record);
47637bff
MD
1267 }
1268 return(error);
1269}
1270
43c665ae 1271/*
1b0ab2c3
MD
1272 * On completion of the BIO this callback must disconnect
1273 * it from the hammer_record and chain to the previous bio.
cdb6e4e6
MD
1274 *
1275 * An I/O error forces the mount to read-only. Data buffers
1276 * are not B_LOCKED like meta-data buffers are, so we have to
1277 * throw the buffer away to prevent the kernel from retrying.
1b0ab2c3
MD
1278 */
1279static
1280void
1281hammer_io_direct_write_complete(struct bio *nbio)
1282{
1283 struct bio *obio;
e469566b 1284 struct buf *bp;
1b0ab2c3
MD
1285 hammer_record_t record = nbio->bio_caller_info1.ptr;
1286
e469566b 1287 bp = nbio->bio_buf;
1b0ab2c3 1288 obio = pop_bio(nbio);
e469566b 1289 if (bp->b_flags & B_ERROR) {
cdb6e4e6 1290 hammer_critical_error(record->ip->hmp, record->ip,
e469566b 1291 bp->b_error,
cdb6e4e6 1292 "while writing bulk data");
e469566b 1293 bp->b_flags |= B_INVAL;
cdb6e4e6 1294 }
1b0ab2c3 1295 biodone(obio);
e469566b
MD
1296
1297 KKASSERT(record != NULL);
1298 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1b0ab2c3
MD
1299 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1300 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1301 record->flags &= ~HAMMER_RECF_DIRECT_WAIT;
1302 wakeup(&record->flags);
1303 }
1304}
1305
1306
1307/*
1308 * This is called before a record is either committed to the B-Tree
e469566b 1309 * or destroyed, to resolve any associated direct-IO.
1b0ab2c3 1310 *
e469566b
MD
1311 * (1) We must wait for any direct-IO related to the record to complete.
1312 *
1313 * (2) We must remove any buffer cache aliases for data accessed via
1314 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1315 * (the mirroring and reblocking code) do not see stale data.
1b0ab2c3
MD
1316 */
1317void
1318hammer_io_direct_wait(hammer_record_t record)
1319{
e469566b
MD
1320 /*
1321 * Wait for I/O to complete
1322 */
1323 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1324 crit_enter();
1325 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1326 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1327 tsleep(&record->flags, 0, "hmdiow", 0);
1328 }
1329 crit_exit();
1330 }
1331
1332 /*
362ec2dc
MD
1333 * Invalidate any related buffer cache aliases associated with the
1334 * backing device. This is needed because the buffer cache buffer
1335 * for file data is associated with the file vnode, not the backing
1336 * device vnode.
1337 *
1338 * XXX I do not think this case can occur any more now that
1339 * reservations ensure that all such buffers are removed before
1340 * an area can be reused.
e469566b
MD
1341 */
1342 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1343 KKASSERT(record->leaf.data_offset);
362ec2dc
MD
1344 hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1345 record->zone2_offset, record->leaf.data_len,
1346 1);
e469566b 1347 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1b0ab2c3 1348 }
1b0ab2c3
MD
1349}
1350
1351/*
43c665ae
MD
1352 * This is called to remove the second-level cached zone-2 offset from
1353 * frontend buffer cache buffers, now stale due to a data relocation.
1354 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1355 * by hammer_vop_strategy_read().
1356 *
1357 * This is rather nasty because here we have something like the reblocker
1358 * scanning the raw B-Tree with no held references on anything, really,
1359 * other then a shared lock on the B-Tree node, and we have to access the
1360 * frontend's buffer cache to check for and clean out the association.
1361 * Specifically, if the reblocker is moving data on the disk, these cached
1362 * offsets will become invalid.
1363 *
1364 * Only data record types associated with the large-data zone are subject
1365 * to direct-io and need to be checked.
1366 *
1367 */
1368void
1369hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1370{
1371 struct hammer_inode_info iinfo;
1372 int zone;
1373
1374 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1375 return;
1376 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1377 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1378 return;
1379 iinfo.obj_id = leaf->base.obj_id;
1380 iinfo.obj_asof = 0; /* unused */
1381 iinfo.obj_localization = leaf->base.localization &
5a930e66 1382 HAMMER_LOCALIZE_PSEUDOFS_MASK;
43c665ae
MD
1383 iinfo.u.leaf = leaf;
1384 hammer_scan_inode_snapshots(hmp, &iinfo,
1385 hammer_io_direct_uncache_callback,
1386 leaf);
1387}
1388
1389static int
1390hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1391{
1392 hammer_inode_info_t iinfo = data;
1393 hammer_off_t data_offset;
1394 hammer_off_t file_offset;
1395 struct vnode *vp;
1396 struct buf *bp;
1397 int blksize;
1398
1399 if (ip->vp == NULL)
1400 return(0);
1401 data_offset = iinfo->u.leaf->data_offset;
1402 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1403 blksize = iinfo->u.leaf->data_len;
1404 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1405
1406 hammer_ref(&ip->lock);
1407 if (hammer_get_vnode(ip, &vp) == 0) {
1408 if ((bp = findblk(ip->vp, file_offset)) != NULL &&
1409 bp->b_bio2.bio_offset != NOOFFSET) {
1410 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1411 bp->b_bio2.bio_offset = NOOFFSET;
1412 brelse(bp);
1413 }
1414 vput(vp);
1415 }
1416 hammer_rel_inode(ip, 0);
1417 return(0);
1418}
47637bff 1419
748efb59
MD
1420
1421/*
1422 * This function is called when writes may have occured on the volume,
1423 * indicating that the device may be holding cached writes.
1424 */
1425static void
1426hammer_io_flush_mark(hammer_volume_t volume)
1427{
1428 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1429}
1430
1431/*
1432 * This function ensures that the device has flushed any cached writes out.
1433 */
1434void
1435hammer_io_flush_sync(hammer_mount_t hmp)
1436{
1437 hammer_volume_t volume;
1438 struct buf *bp_base = NULL;
1439 struct buf *bp;
1440
1441 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1442 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1443 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1444 bp = getpbuf(NULL);
1445 bp->b_bio1.bio_offset = 0;
1446 bp->b_bufsize = 0;
1447 bp->b_bcount = 0;
1448 bp->b_cmd = BUF_CMD_FLUSH;
1449 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1450 bp->b_bio1.bio_done = hammer_io_flush_sync_done;
1451 bp->b_flags |= B_ASYNC;
1452 bp_base = bp;
1453 vn_strategy(volume->devvp, &bp->b_bio1);
1454 }
1455 }
1456 while ((bp = bp_base) != NULL) {
1457 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1458 while (bp->b_cmd != BUF_CMD_DONE) {
1459 crit_enter();
1460 tsleep_interlock(&bp->b_cmd);
1461 if (bp->b_cmd != BUF_CMD_DONE)
1462 tsleep(&bp->b_cmd, 0, "hmrFLS", 0);
1463 crit_exit();
1464 }
1465 bp->b_flags &= ~B_ASYNC;
1466 relpbuf(bp, NULL);
1467 }
1468}
1469
1470/*
1471 * Callback to deal with completed flush commands to the device.
1472 */
1473static void
1474hammer_io_flush_sync_done(struct bio *bio)
1475{
1476 struct buf *bp;
1477
1478 bp = bio->bio_buf;
1479 bp->b_cmd = BUF_CMD_DONE;
1480 wakeup(&bp->b_cmd);
1481}
1482