HAMMER - Rework write pipelining
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
bloba4728790158bcce6df58240517a27216a1538080
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
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:
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.
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.
34 * $DragonFly: src/sys/vfs/hammer/hammer_io.c,v 1.55 2008/09/15 17:02:49 dillon Exp $
37 * IO Primitives and buffer cache management
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.
44 * If the kernel tries to destroy a passively associated buf which we cannot
45 * yet let go we set B_LOCKED in the buffer and then actively released it
46 * later when we can.
49 #include "hammer.h"
50 #include <sys/fcntl.h>
51 #include <sys/nlookup.h>
52 #include <sys/buf.h>
53 #include <sys/buf2.h>
55 static void hammer_io_modify(hammer_io_t io, int count);
56 static void hammer_io_deallocate(struct buf *bp);
57 #if 0
58 static void hammer_io_direct_read_complete(struct bio *nbio);
59 #endif
60 static void hammer_io_direct_write_complete(struct bio *nbio);
61 static int hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data);
62 static void hammer_io_set_modlist(struct hammer_io *io);
63 static void hammer_io_flush_mark(hammer_volume_t volume);
67 * Initialize a new, already-zero'd hammer_io structure, or reinitialize
68 * an existing hammer_io structure which may have switched to another type.
70 void
71 hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
73 io->volume = volume;
74 io->hmp = volume->io.hmp;
75 io->type = type;
79 * Helper routine to disassociate a buffer cache buffer from an I/O
80 * structure. The buffer is unlocked and marked appropriate for reclamation.
82 * The io may have 0 or 1 references depending on who called us. The
83 * caller is responsible for dealing with the refs.
85 * This call can only be made when no action is required on the buffer.
87 * The caller must own the buffer and the IO must indicate that the
88 * structure no longer owns it (io.released != 0).
90 static void
91 hammer_io_disassociate(hammer_io_structure_t iou)
93 struct buf *bp = iou->io.bp;
95 KKASSERT(iou->io.released);
96 KKASSERT(iou->io.modified == 0);
97 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
98 buf_dep_init(bp);
99 iou->io.bp = NULL;
102 * If the buffer was locked someone wanted to get rid of it.
104 if (bp->b_flags & B_LOCKED) {
105 --hammer_count_io_locked;
106 bp->b_flags &= ~B_LOCKED;
108 if (iou->io.reclaim) {
109 bp->b_flags |= B_NOCACHE|B_RELBUF;
110 iou->io.reclaim = 0;
113 switch(iou->io.type) {
114 case HAMMER_STRUCTURE_VOLUME:
115 iou->volume.ondisk = NULL;
116 break;
117 case HAMMER_STRUCTURE_DATA_BUFFER:
118 case HAMMER_STRUCTURE_META_BUFFER:
119 case HAMMER_STRUCTURE_UNDO_BUFFER:
120 iou->buffer.ondisk = NULL;
121 break;
126 * Wait for any physical IO to complete
128 * XXX we aren't interlocked against a spinlock or anything so there
129 * is a small window in the interlock / io->running == 0 test.
131 void
132 hammer_io_wait(hammer_io_t io)
134 if (io->running) {
135 for (;;) {
136 io->waiting = 1;
137 tsleep_interlock(io, 0);
138 if (io->running == 0)
139 break;
140 tsleep(io, PINTERLOCKED, "hmrflw", hz);
141 if (io->running == 0)
142 break;
148 * Wait for all hammer_io-initated write I/O's to complete. This is not
149 * supposed to count direct I/O's but some can leak through (for
150 * non-full-sized direct I/Os).
152 void
153 hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
155 hammer_io_flush_sync(hmp);
156 crit_enter();
157 while (hmp->io_running_space)
158 tsleep(&hmp->io_running_space, 0, ident, 0);
159 crit_exit();
162 #define HAMMER_MAXRA 4
165 * Load bp for a HAMMER structure. The io must be exclusively locked by
166 * the caller.
168 * This routine is mostly used on meta-data and small-data blocks. Generally
169 * speaking HAMMER assumes some locality of reference and will cluster
170 * a 64K read.
172 * Note that clustering occurs at the device layer, not the logical layer.
173 * If the buffers do not apply to the current operation they may apply to
174 * some other.
177 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
179 struct buf *bp;
180 int error;
182 if ((bp = io->bp) == NULL) {
183 hammer_count_io_running_read += io->bytes;
184 if (hammer_cluster_enable) {
185 error = cluster_read(devvp, limit,
186 io->offset, io->bytes,
187 HAMMER_CLUSTER_SIZE,
188 HAMMER_CLUSTER_BUFS, &io->bp);
189 } else {
190 error = bread(devvp, io->offset, io->bytes, &io->bp);
192 hammer_stats_disk_read += io->bytes;
193 hammer_count_io_running_read -= io->bytes;
196 * The code generally assumes b_ops/b_dep has been set-up,
197 * even if we error out here.
199 bp = io->bp;
200 bp->b_ops = &hammer_bioops;
201 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
202 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
203 BUF_KERNPROC(bp);
204 KKASSERT(io->modified == 0);
205 KKASSERT(io->running == 0);
206 KKASSERT(io->waiting == 0);
207 io->released = 0; /* we hold an active lock on bp */
208 } else {
209 error = 0;
211 return(error);
215 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
216 * Must be called with the IO exclusively locked.
218 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
219 * I/O by forcing the buffer to not be in a released state before calling
220 * it.
222 * This function will also mark the IO as modified but it will not
223 * increment the modify_refs count.
226 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
228 struct buf *bp;
230 if ((bp = io->bp) == NULL) {
231 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
232 bp = io->bp;
233 bp->b_ops = &hammer_bioops;
234 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
235 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
236 io->released = 0;
237 KKASSERT(io->running == 0);
238 io->waiting = 0;
239 BUF_KERNPROC(bp);
240 } else {
241 if (io->released) {
242 regetblk(bp);
243 BUF_KERNPROC(bp);
244 io->released = 0;
247 hammer_io_modify(io, 0);
248 vfs_bio_clrbuf(bp);
249 return(0);
253 * Remove potential device level aliases against buffers managed by high level
254 * vnodes. Aliases can also be created due to mixed buffer sizes or via
255 * direct access to the backing store device.
257 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
258 * does not exist its backing VM pages might, and we have to invalidate
259 * those as well or a getblk() will reinstate them.
261 * Buffer cache buffers associated with hammer_buffers cannot be
262 * invalidated.
265 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
267 hammer_io_structure_t iou;
268 hammer_off_t phys_offset;
269 struct buf *bp;
270 int error;
272 phys_offset = volume->ondisk->vol_buf_beg +
273 (zone2_offset & HAMMER_OFF_SHORT_MASK);
274 crit_enter();
275 if ((bp = findblk(volume->devvp, phys_offset, FINDBLK_TEST)) != NULL)
276 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
277 else
278 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
279 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
280 #if 0
281 hammer_ref(&iou->io.lock);
282 hammer_io_clear_modify(&iou->io, 1);
283 bundirty(bp);
284 iou->io.released = 0;
285 BUF_KERNPROC(bp);
286 iou->io.reclaim = 1;
287 iou->io.waitdep = 1;
288 KKASSERT(iou->io.lock.refs == 1);
289 hammer_rel_buffer(&iou->buffer, 0);
290 /*hammer_io_deallocate(bp);*/
291 #endif
292 bqrelse(bp);
293 error = EAGAIN;
294 } else {
295 KKASSERT((bp->b_flags & B_LOCKED) == 0);
296 bundirty(bp);
297 bp->b_flags |= B_NOCACHE|B_RELBUF;
298 brelse(bp);
299 error = 0;
301 crit_exit();
302 return(error);
306 * This routine is called on the last reference to a hammer structure.
307 * The io is usually interlocked with io.loading and io.refs must be 1.
309 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
310 * simply means the caller finishes decrementing the ref-count on the
311 * IO structure then brelse()'s the bp. The bp may or may not still be
312 * passively associated with the IO.
314 * The only requirement here is that modified meta-data and volume-header
315 * buffer may NOT be disassociated from the IO structure, and consequently
316 * we also leave such buffers actively associated with the IO if they already
317 * are (since the kernel can't do anything with them anyway). Only the
318 * flusher is allowed to write such buffers out. Modified pure-data and
319 * undo buffers are returned to the kernel but left passively associated
320 * so we can track when the kernel writes the bp out.
322 struct buf *
323 hammer_io_release(struct hammer_io *io, int flush)
325 union hammer_io_structure *iou = (void *)io;
326 struct buf *bp;
328 if ((bp = io->bp) == NULL)
329 return(NULL);
332 * Try to flush a dirty IO to disk if asked to by the
333 * caller or if the kernel tried to flush the buffer in the past.
335 * Kernel-initiated flushes are only allowed for pure-data buffers.
336 * meta-data and volume buffers can only be flushed explicitly
337 * by HAMMER.
339 if (io->modified) {
340 if (flush) {
341 hammer_io_flush(io);
342 } else if (bp->b_flags & B_LOCKED) {
343 switch(io->type) {
344 case HAMMER_STRUCTURE_DATA_BUFFER:
345 case HAMMER_STRUCTURE_UNDO_BUFFER:
346 hammer_io_flush(io);
347 break;
348 default:
349 break;
351 } /* else no explicit request to flush the buffer */
355 * Wait for the IO to complete if asked to. This occurs when
356 * the buffer must be disposed of definitively during an umount
357 * or buffer invalidation.
359 if (io->waitdep && io->running) {
360 hammer_io_wait(io);
364 * Return control of the buffer to the kernel (with the provisio
365 * that our bioops can override kernel decisions with regards to
366 * the buffer).
368 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
370 * Always disassociate the bp if an explicit flush
371 * was requested and the IO completed with no error
372 * (so unmount can really clean up the structure).
374 if (io->released) {
375 regetblk(bp);
376 BUF_KERNPROC(bp);
377 } else {
378 io->released = 1;
380 hammer_io_disassociate((hammer_io_structure_t)io);
381 /* return the bp */
382 } else if (io->modified) {
384 * Only certain IO types can be released to the kernel if
385 * the buffer has been modified.
387 * volume and meta-data IO types may only be explicitly
388 * flushed by HAMMER.
390 switch(io->type) {
391 case HAMMER_STRUCTURE_DATA_BUFFER:
392 case HAMMER_STRUCTURE_UNDO_BUFFER:
393 if (io->released == 0) {
394 io->released = 1;
395 bdwrite(bp);
397 break;
398 default:
399 break;
401 bp = NULL; /* bp left associated */
402 } else if (io->released == 0) {
404 * Clean buffers can be generally released to the kernel.
405 * We leave the bp passively associated with the HAMMER
406 * structure and use bioops to disconnect it later on
407 * if the kernel wants to discard the buffer.
409 * We can steal the structure's ownership of the bp.
411 io->released = 1;
412 if (bp->b_flags & B_LOCKED) {
413 hammer_io_disassociate(iou);
414 /* return the bp */
415 } else {
416 if (io->reclaim) {
417 hammer_io_disassociate(iou);
418 /* return the bp */
419 } else {
420 /* return the bp (bp passively associated) */
423 } else {
425 * A released buffer is passively associate with our
426 * hammer_io structure. The kernel cannot destroy it
427 * without making a bioops call. If the kernel (B_LOCKED)
428 * or we (reclaim) requested that the buffer be destroyed
429 * we destroy it, otherwise we do a quick get/release to
430 * reset its position in the kernel's LRU list.
432 * Leaving the buffer passively associated allows us to
433 * use the kernel's LRU buffer flushing mechanisms rather
434 * then rolling our own.
436 * XXX there are two ways of doing this. We can re-acquire
437 * and passively release to reset the LRU, or not.
439 if (io->running == 0) {
440 regetblk(bp);
441 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
442 hammer_io_disassociate(iou);
443 /* return the bp */
444 } else {
445 /* return the bp (bp passively associated) */
447 } else {
449 * bp is left passively associated but we do not
450 * try to reacquire it. Interactions with the io
451 * structure will occur on completion of the bp's
452 * I/O.
454 bp = NULL;
457 return(bp);
461 * This routine is called with a locked IO when a flush is desired and
462 * no other references to the structure exists other then ours. This
463 * routine is ONLY called when HAMMER believes it is safe to flush a
464 * potentially modified buffer out.
466 void
467 hammer_io_flush(struct hammer_io *io)
469 struct buf *bp;
472 * Degenerate case - nothing to flush if nothing is dirty.
474 if (io->modified == 0) {
475 return;
478 KKASSERT(io->bp);
479 KKASSERT(io->modify_refs <= 0);
482 * Acquire ownership of the bp, particularly before we clear our
483 * modified flag.
485 * We are going to bawrite() this bp. Don't leave a window where
486 * io->released is set, we actually own the bp rather then our
487 * buffer.
489 bp = io->bp;
490 if (io->released) {
491 regetblk(bp);
492 /* BUF_KERNPROC(io->bp); */
493 /* io->released = 0; */
494 KKASSERT(io->released);
495 KKASSERT(io->bp == bp);
497 io->released = 1;
500 * Acquire exclusive access to the bp and then clear the modified
501 * state of the buffer prior to issuing I/O to interlock any
502 * modifications made while the I/O is in progress. This shouldn't
503 * happen anyway but losing data would be worse. The modified bit
504 * will be rechecked after the IO completes.
506 * NOTE: This call also finalizes the buffer's content (inval == 0).
508 * This is only legal when lock.refs == 1 (otherwise we might clear
509 * the modified bit while there are still users of the cluster
510 * modifying the data).
512 * Do this before potentially blocking so any attempt to modify the
513 * ondisk while we are blocked blocks waiting for us.
515 hammer_ref(&io->lock);
516 hammer_io_clear_modify(io, 0);
517 hammer_unref(&io->lock);
520 * Transfer ownership to the kernel and initiate I/O.
522 io->running = 1;
523 io->hmp->io_running_space += io->bytes;
524 hammer_count_io_running_write += io->bytes;
525 bawrite(bp);
526 hammer_io_flush_mark(io->volume);
529 /************************************************************************
530 * BUFFER DIRTYING *
531 ************************************************************************
533 * These routines deal with dependancies created when IO buffers get
534 * modified. The caller must call hammer_modify_*() on a referenced
535 * HAMMER structure prior to modifying its on-disk data.
537 * Any intent to modify an IO buffer acquires the related bp and imposes
538 * various write ordering dependancies.
542 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
543 * are locked until the flusher can deal with them, pure data buffers
544 * can be written out.
546 static
547 void
548 hammer_io_modify(hammer_io_t io, int count)
551 * io->modify_refs must be >= 0
553 while (io->modify_refs < 0) {
554 io->waitmod = 1;
555 tsleep(io, 0, "hmrmod", 0);
559 * Shortcut if nothing to do.
561 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
562 io->modify_refs += count;
563 if (io->modified && io->released == 0)
564 return;
566 hammer_lock_ex(&io->lock);
567 if (io->modified == 0) {
568 hammer_io_set_modlist(io);
569 io->modified = 1;
571 if (io->released) {
572 regetblk(io->bp);
573 BUF_KERNPROC(io->bp);
574 io->released = 0;
575 KKASSERT(io->modified != 0);
577 hammer_unlock(&io->lock);
580 static __inline
581 void
582 hammer_io_modify_done(hammer_io_t io)
584 KKASSERT(io->modify_refs > 0);
585 --io->modify_refs;
586 if (io->modify_refs == 0 && io->waitmod) {
587 io->waitmod = 0;
588 wakeup(io);
592 void
593 hammer_io_write_interlock(hammer_io_t io)
595 while (io->modify_refs != 0) {
596 io->waitmod = 1;
597 tsleep(io, 0, "hmrmod", 0);
599 io->modify_refs = -1;
602 void
603 hammer_io_done_interlock(hammer_io_t io)
605 KKASSERT(io->modify_refs == -1);
606 io->modify_refs = 0;
607 if (io->waitmod) {
608 io->waitmod = 0;
609 wakeup(io);
614 * Caller intends to modify a volume's ondisk structure.
616 * This is only allowed if we are the flusher or we have a ref on the
617 * sync_lock.
619 void
620 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
621 void *base, int len)
623 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
625 hammer_io_modify(&volume->io, 1);
626 if (len) {
627 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
628 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
629 hammer_generate_undo(trans, &volume->io,
630 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
631 base, len);
636 * Caller intends to modify a buffer's ondisk structure.
638 * This is only allowed if we are the flusher or we have a ref on the
639 * sync_lock.
641 void
642 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
643 void *base, int len)
645 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
647 hammer_io_modify(&buffer->io, 1);
648 if (len) {
649 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
650 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
651 hammer_generate_undo(trans, &buffer->io,
652 buffer->zone2_offset + rel_offset,
653 base, len);
657 void
658 hammer_modify_volume_done(hammer_volume_t volume)
660 hammer_io_modify_done(&volume->io);
663 void
664 hammer_modify_buffer_done(hammer_buffer_t buffer)
666 hammer_io_modify_done(&buffer->io);
670 * Mark an entity as not being dirty any more and finalize any
671 * delayed adjustments to the buffer.
673 * Delayed adjustments are an important performance enhancement, allowing
674 * us to avoid recalculating B-Tree node CRCs over and over again when
675 * making bulk-modifications to the B-Tree.
677 * If inval is non-zero delayed adjustments are ignored.
679 * This routine may dereference related btree nodes and cause the
680 * buffer to be dereferenced. The caller must own a reference on io.
682 void
683 hammer_io_clear_modify(struct hammer_io *io, int inval)
685 if (io->modified == 0)
686 return;
689 * Take us off the mod-list and clear the modified bit.
691 KKASSERT(io->mod_list != NULL);
692 if (io->mod_list == &io->hmp->volu_list ||
693 io->mod_list == &io->hmp->meta_list) {
694 io->hmp->locked_dirty_space -= io->bytes;
695 hammer_count_dirtybufspace -= io->bytes;
697 TAILQ_REMOVE(io->mod_list, io, mod_entry);
698 io->mod_list = NULL;
699 io->modified = 0;
702 * If this bit is not set there are no delayed adjustments.
704 if (io->gencrc == 0)
705 return;
706 io->gencrc = 0;
709 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
710 * on the node (& underlying buffer). Release the node after clearing
711 * the flag.
713 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
714 hammer_buffer_t buffer = (void *)io;
715 hammer_node_t node;
717 restart:
718 TAILQ_FOREACH(node, &buffer->clist, entry) {
719 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
720 continue;
721 node->flags &= ~HAMMER_NODE_NEEDSCRC;
722 KKASSERT(node->ondisk);
723 if (inval == 0)
724 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
725 hammer_rel_node(node);
726 goto restart;
729 /* caller must still have ref on io */
730 KKASSERT(io->lock.refs > 0);
734 * Clear the IO's modify list. Even though the IO is no longer modified
735 * it may still be on the lose_list. This routine is called just before
736 * the governing hammer_buffer is destroyed.
738 void
739 hammer_io_clear_modlist(struct hammer_io *io)
741 KKASSERT(io->modified == 0);
742 if (io->mod_list) {
743 crit_enter(); /* biodone race against list */
744 KKASSERT(io->mod_list == &io->hmp->lose_list);
745 TAILQ_REMOVE(io->mod_list, io, mod_entry);
746 io->mod_list = NULL;
747 crit_exit();
751 static void
752 hammer_io_set_modlist(struct hammer_io *io)
754 struct hammer_mount *hmp = io->hmp;
756 KKASSERT(io->mod_list == NULL);
758 switch(io->type) {
759 case HAMMER_STRUCTURE_VOLUME:
760 io->mod_list = &hmp->volu_list;
761 hmp->locked_dirty_space += io->bytes;
762 hammer_count_dirtybufspace += io->bytes;
763 break;
764 case HAMMER_STRUCTURE_META_BUFFER:
765 io->mod_list = &hmp->meta_list;
766 hmp->locked_dirty_space += io->bytes;
767 hammer_count_dirtybufspace += io->bytes;
768 break;
769 case HAMMER_STRUCTURE_UNDO_BUFFER:
770 io->mod_list = &hmp->undo_list;
771 break;
772 case HAMMER_STRUCTURE_DATA_BUFFER:
773 io->mod_list = &hmp->data_list;
774 break;
776 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
779 /************************************************************************
780 * HAMMER_BIOOPS *
781 ************************************************************************
786 * Pre-IO initiation kernel callback - cluster build only
788 static void
789 hammer_io_start(struct buf *bp)
794 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
796 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
797 * may also be set if we were marking a cluster header open. Only remove
798 * our dependancy if the modified bit is clear.
800 static void
801 hammer_io_complete(struct buf *bp)
803 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
805 KKASSERT(iou->io.released == 1);
808 * Deal with people waiting for I/O to drain
810 if (iou->io.running) {
812 * Deal with critical write errors. Once a critical error
813 * has been flagged in hmp the UNDO FIFO will not be updated.
814 * That way crash recover will give us a consistent
815 * filesystem.
817 * Because of this we can throw away failed UNDO buffers. If
818 * we throw away META or DATA buffers we risk corrupting
819 * the now read-only version of the filesystem visible to
820 * the user. Clear B_ERROR so the buffer is not re-dirtied
821 * by the kernel and ref the io so it doesn't get thrown
822 * away.
824 if (bp->b_flags & B_ERROR) {
825 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
826 "while flushing meta-data");
827 switch(iou->io.type) {
828 case HAMMER_STRUCTURE_UNDO_BUFFER:
829 break;
830 default:
831 if (iou->io.ioerror == 0) {
832 iou->io.ioerror = 1;
833 if (iou->io.lock.refs == 0)
834 ++hammer_count_refedbufs;
835 hammer_ref(&iou->io.lock);
837 break;
839 bp->b_flags &= ~B_ERROR;
840 bundirty(bp);
841 #if 0
842 hammer_io_set_modlist(&iou->io);
843 iou->io.modified = 1;
844 #endif
846 hammer_stats_disk_write += iou->io.bytes;
847 hammer_count_io_running_write -= iou->io.bytes;
848 iou->io.hmp->io_running_space -= iou->io.bytes;
849 if (iou->io.hmp->io_running_space == 0)
850 wakeup(&iou->io.hmp->io_running_space);
851 KKASSERT(iou->io.hmp->io_running_space >= 0);
852 iou->io.running = 0;
853 } else {
854 hammer_stats_disk_read += iou->io.bytes;
857 if (iou->io.waiting) {
858 iou->io.waiting = 0;
859 wakeup(iou);
863 * If B_LOCKED is set someone wanted to deallocate the bp at some
864 * point, do it now if refs has become zero.
866 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
867 KKASSERT(iou->io.modified == 0);
868 --hammer_count_io_locked;
869 bp->b_flags &= ~B_LOCKED;
870 hammer_io_deallocate(bp);
871 /* structure may be dead now */
876 * Callback from kernel when it wishes to deallocate a passively
877 * associated structure. This mostly occurs with clean buffers
878 * but it may be possible for a holding structure to be marked dirty
879 * while its buffer is passively associated. The caller owns the bp.
881 * If we cannot disassociate we set B_LOCKED to prevent the buffer
882 * from getting reused.
884 * WARNING: Because this can be called directly by getnewbuf we cannot
885 * recurse into the tree. If a bp cannot be immediately disassociated
886 * our only recourse is to set B_LOCKED.
888 * WARNING: This may be called from an interrupt via hammer_io_complete()
890 static void
891 hammer_io_deallocate(struct buf *bp)
893 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
895 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
896 if (iou->io.lock.refs > 0 || iou->io.modified) {
898 * It is not legal to disassociate a modified buffer. This
899 * case really shouldn't ever occur.
901 bp->b_flags |= B_LOCKED;
902 ++hammer_count_io_locked;
903 } else {
905 * Disassociate the BP. If the io has no refs left we
906 * have to add it to the loose list.
908 hammer_io_disassociate(iou);
909 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
910 KKASSERT(iou->io.bp == NULL);
911 KKASSERT(iou->io.mod_list == NULL);
912 crit_enter(); /* biodone race against list */
913 iou->io.mod_list = &iou->io.hmp->lose_list;
914 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
915 crit_exit();
920 static int
921 hammer_io_fsync(struct vnode *vp)
923 return(0);
927 * NOTE: will not be called unless we tell the kernel about the
928 * bioops. Unused... we use the mount's VFS_SYNC instead.
930 static int
931 hammer_io_sync(struct mount *mp)
933 return(0);
936 static void
937 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
942 * I/O pre-check for reading and writing. HAMMER only uses this for
943 * B_CACHE buffers so checkread just shouldn't happen, but if it does
944 * allow it.
946 * Writing is a different case. We don't want the kernel to try to write
947 * out a buffer that HAMMER may be modifying passively or which has a
948 * dependancy. In addition, kernel-demanded writes can only proceed for
949 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
950 * buffer types can only be explicitly written by the flusher.
952 * checkwrite will only be called for bdwrite()n buffers. If we return
953 * success the kernel is guaranteed to initiate the buffer write.
955 static int
956 hammer_io_checkread(struct buf *bp)
958 return(0);
961 static int
962 hammer_io_checkwrite(struct buf *bp)
964 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
967 * This shouldn't happen under normal operation.
969 if (io->type == HAMMER_STRUCTURE_VOLUME ||
970 io->type == HAMMER_STRUCTURE_META_BUFFER) {
971 if (!panicstr)
972 panic("hammer_io_checkwrite: illegal buffer");
973 if ((bp->b_flags & B_LOCKED) == 0) {
974 bp->b_flags |= B_LOCKED;
975 ++hammer_count_io_locked;
977 return(1);
981 * We can only clear the modified bit if the IO is not currently
982 * undergoing modification. Otherwise we may miss changes.
984 * Only data and undo buffers can reach here. These buffers do
985 * not have terminal crc functions but we temporarily reference
986 * the IO anyway, just in case.
988 if (io->modify_refs == 0 && io->modified) {
989 hammer_ref(&io->lock);
990 hammer_io_clear_modify(io, 0);
991 hammer_unref(&io->lock);
992 } else if (io->modified) {
993 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
997 * The kernel is going to start the IO, set io->running.
999 KKASSERT(io->running == 0);
1000 io->running = 1;
1001 io->hmp->io_running_space += io->bytes;
1002 hammer_count_io_running_write += io->bytes;
1003 return(0);
1007 * Return non-zero if we wish to delay the kernel's attempt to flush
1008 * this buffer to disk.
1010 static int
1011 hammer_io_countdeps(struct buf *bp, int n)
1013 return(0);
1016 struct bio_ops hammer_bioops = {
1017 .io_start = hammer_io_start,
1018 .io_complete = hammer_io_complete,
1019 .io_deallocate = hammer_io_deallocate,
1020 .io_fsync = hammer_io_fsync,
1021 .io_sync = hammer_io_sync,
1022 .io_movedeps = hammer_io_movedeps,
1023 .io_countdeps = hammer_io_countdeps,
1024 .io_checkread = hammer_io_checkread,
1025 .io_checkwrite = hammer_io_checkwrite,
1028 /************************************************************************
1029 * DIRECT IO OPS *
1030 ************************************************************************
1032 * These functions operate directly on the buffer cache buffer associated
1033 * with a front-end vnode rather then a back-end device vnode.
1037 * Read a buffer associated with a front-end vnode directly from the
1038 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1039 * we validate the CRC.
1041 * We must check for the presence of a HAMMER buffer to handle the case
1042 * where the reblocker has rewritten the data (which it does via the HAMMER
1043 * buffer system, not via the high-level vnode buffer cache), but not yet
1044 * committed the buffer to the media.
1047 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1048 hammer_btree_leaf_elm_t leaf)
1050 hammer_off_t buf_offset;
1051 hammer_off_t zone2_offset;
1052 hammer_volume_t volume;
1053 struct buf *bp;
1054 struct bio *nbio;
1055 int vol_no;
1056 int error;
1058 buf_offset = bio->bio_offset;
1059 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1060 HAMMER_ZONE_LARGE_DATA);
1063 * The buffer cache may have an aliased buffer (the reblocker can
1064 * write them). If it does we have to sync any dirty data before
1065 * we can build our direct-read. This is a non-critical code path.
1067 bp = bio->bio_buf;
1068 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1071 * Resolve to a zone-2 offset. The conversion just requires
1072 * munging the top 4 bits but we want to abstract it anyway
1073 * so the blockmap code can verify the zone assignment.
1075 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1076 if (error)
1077 goto done;
1078 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1079 HAMMER_ZONE_RAW_BUFFER);
1082 * Resolve volume and raw-offset for 3rd level bio. The
1083 * offset will be specific to the volume.
1085 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1086 volume = hammer_get_volume(hmp, vol_no, &error);
1087 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1088 error = EIO;
1090 if (error == 0) {
1092 * 3rd level bio
1094 nbio = push_bio(bio);
1095 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1096 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1097 #if 0
1099 * XXX disabled - our CRC check doesn't work if the OS
1100 * does bogus_page replacement on the direct-read.
1102 if (leaf && hammer_verify_data) {
1103 nbio->bio_done = hammer_io_direct_read_complete;
1104 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1106 #endif
1107 hammer_stats_disk_read += bp->b_bufsize;
1108 vn_strategy(volume->devvp, nbio);
1110 hammer_rel_volume(volume, 0);
1111 done:
1112 if (error) {
1113 kprintf("hammer_direct_read: failed @ %016llx\n",
1114 (long long)zone2_offset);
1115 bp->b_error = error;
1116 bp->b_flags |= B_ERROR;
1117 biodone(bio);
1119 return(error);
1122 #if 0
1124 * On completion of the BIO this callback must check the data CRC
1125 * and chain to the previous bio.
1127 static
1128 void
1129 hammer_io_direct_read_complete(struct bio *nbio)
1131 struct bio *obio;
1132 struct buf *bp;
1133 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1135 bp = nbio->bio_buf;
1136 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1137 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1138 nbio->bio_offset, bp->b_bufsize);
1139 if (hammer_debug_debug)
1140 Debugger("");
1141 bp->b_flags |= B_ERROR;
1142 bp->b_error = EIO;
1144 obio = pop_bio(nbio);
1145 biodone(obio);
1147 #endif
1150 * Write a buffer associated with a front-end vnode directly to the
1151 * disk media. The bio may be issued asynchronously.
1153 * The BIO is associated with the specified record and RECF_DIRECT_IO
1154 * is set. The recorded is added to its object.
1157 hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
1158 struct bio *bio)
1160 hammer_btree_leaf_elm_t leaf = &record->leaf;
1161 hammer_off_t buf_offset;
1162 hammer_off_t zone2_offset;
1163 hammer_volume_t volume;
1164 hammer_buffer_t buffer;
1165 struct buf *bp;
1166 struct bio *nbio;
1167 char *ptr;
1168 int vol_no;
1169 int error;
1171 buf_offset = leaf->data_offset;
1173 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1174 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1176 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1177 leaf->data_len >= HAMMER_BUFSIZE) {
1179 * We are using the vnode's bio to write directly to the
1180 * media, any hammer_buffer at the same zone-X offset will
1181 * now have stale data.
1183 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1184 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1185 volume = hammer_get_volume(hmp, vol_no, &error);
1187 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1188 error = EIO;
1189 if (error == 0) {
1190 bp = bio->bio_buf;
1191 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1193 hammer_del_buffers(hmp, buf_offset,
1194 zone2_offset, bp->b_bufsize);
1198 * Second level bio - cached zone2 offset.
1200 * (We can put our bio_done function in either the
1201 * 2nd or 3rd level).
1203 nbio = push_bio(bio);
1204 nbio->bio_offset = zone2_offset;
1205 nbio->bio_done = hammer_io_direct_write_complete;
1206 nbio->bio_caller_info1.ptr = record;
1207 record->zone2_offset = zone2_offset;
1208 record->flags |= HAMMER_RECF_DIRECT_IO |
1209 HAMMER_RECF_DIRECT_INVAL;
1212 * Third level bio - raw offset specific to the
1213 * correct volume.
1215 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1216 nbio = push_bio(nbio);
1217 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1218 zone2_offset;
1219 hammer_stats_disk_write += bp->b_bufsize;
1220 vn_strategy(volume->devvp, nbio);
1221 hammer_io_flush_mark(volume);
1223 hammer_rel_volume(volume, 0);
1224 } else {
1226 * Must fit in a standard HAMMER buffer. In this case all
1227 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1228 * does not need to be set-up.
1230 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1231 buffer = NULL;
1232 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1233 if (error == 0) {
1234 bp = bio->bio_buf;
1235 bp->b_flags |= B_AGE;
1236 hammer_io_modify(&buffer->io, 1);
1237 bcopy(bp->b_data, ptr, leaf->data_len);
1238 hammer_io_modify_done(&buffer->io);
1239 hammer_rel_buffer(buffer, 0);
1240 bp->b_resid = 0;
1241 biodone(bio);
1244 if (error == 0) {
1246 * The record is all setup now, add it. Potential conflics
1247 * have already been dealt with.
1249 error = hammer_mem_add(record);
1250 KKASSERT(error == 0);
1251 } else {
1253 * Major suckage occured. Also note: The record was never added
1254 * to the tree so we do not have to worry about the backend.
1256 kprintf("hammer_direct_write: failed @ %016llx\n",
1257 (long long)leaf->data_offset);
1258 bp = bio->bio_buf;
1259 bp->b_resid = 0;
1260 bp->b_error = EIO;
1261 bp->b_flags |= B_ERROR;
1262 biodone(bio);
1263 record->flags |= HAMMER_RECF_DELETED_FE;
1264 hammer_rel_mem_record(record);
1266 return(error);
1270 * On completion of the BIO this callback must disconnect
1271 * it from the hammer_record and chain to the previous bio.
1273 * An I/O error forces the mount to read-only. Data buffers
1274 * are not B_LOCKED like meta-data buffers are, so we have to
1275 * throw the buffer away to prevent the kernel from retrying.
1277 static
1278 void
1279 hammer_io_direct_write_complete(struct bio *nbio)
1281 struct bio *obio;
1282 struct buf *bp;
1283 hammer_record_t record = nbio->bio_caller_info1.ptr;
1285 bp = nbio->bio_buf;
1286 obio = pop_bio(nbio);
1287 if (bp->b_flags & B_ERROR) {
1288 hammer_critical_error(record->ip->hmp, record->ip,
1289 bp->b_error,
1290 "while writing bulk data");
1291 bp->b_flags |= B_INVAL;
1293 biodone(obio);
1295 KKASSERT(record != NULL);
1296 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1297 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1298 record->flags &= ~(HAMMER_RECF_DIRECT_IO |
1299 HAMMER_RECF_DIRECT_WAIT);
1300 /* record can disappear once DIRECT_IO flag is cleared */
1301 wakeup(&record->flags);
1302 } else {
1303 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1304 /* record can disappear once DIRECT_IO flag is cleared */
1310 * This is called before a record is either committed to the B-Tree
1311 * or destroyed, to resolve any associated direct-IO.
1313 * (1) We must wait for any direct-IO related to the record to complete.
1315 * (2) We must remove any buffer cache aliases for data accessed via
1316 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1317 * (the mirroring and reblocking code) do not see stale data.
1319 void
1320 hammer_io_direct_wait(hammer_record_t record)
1323 * Wait for I/O to complete
1325 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1326 crit_enter();
1327 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1328 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1329 tsleep(&record->flags, 0, "hmdiow", 0);
1331 crit_exit();
1335 * Invalidate any related buffer cache aliases associated with the
1336 * backing device. This is needed because the buffer cache buffer
1337 * for file data is associated with the file vnode, not the backing
1338 * device vnode.
1340 * XXX I do not think this case can occur any more now that
1341 * reservations ensure that all such buffers are removed before
1342 * an area can be reused.
1344 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1345 KKASSERT(record->leaf.data_offset);
1346 hammer_del_buffers(record->ip->hmp, record->leaf.data_offset,
1347 record->zone2_offset, record->leaf.data_len,
1349 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1354 * This is called to remove the second-level cached zone-2 offset from
1355 * frontend buffer cache buffers, now stale due to a data relocation.
1356 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1357 * by hammer_vop_strategy_read().
1359 * This is rather nasty because here we have something like the reblocker
1360 * scanning the raw B-Tree with no held references on anything, really,
1361 * other then a shared lock on the B-Tree node, and we have to access the
1362 * frontend's buffer cache to check for and clean out the association.
1363 * Specifically, if the reblocker is moving data on the disk, these cached
1364 * offsets will become invalid.
1366 * Only data record types associated with the large-data zone are subject
1367 * to direct-io and need to be checked.
1370 void
1371 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1373 struct hammer_inode_info iinfo;
1374 int zone;
1376 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1377 return;
1378 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1379 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1380 return;
1381 iinfo.obj_id = leaf->base.obj_id;
1382 iinfo.obj_asof = 0; /* unused */
1383 iinfo.obj_localization = leaf->base.localization &
1384 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1385 iinfo.u.leaf = leaf;
1386 hammer_scan_inode_snapshots(hmp, &iinfo,
1387 hammer_io_direct_uncache_callback,
1388 leaf);
1391 static int
1392 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1394 hammer_inode_info_t iinfo = data;
1395 hammer_off_t data_offset;
1396 hammer_off_t file_offset;
1397 struct vnode *vp;
1398 struct buf *bp;
1399 int blksize;
1401 if (ip->vp == NULL)
1402 return(0);
1403 data_offset = iinfo->u.leaf->data_offset;
1404 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1405 blksize = iinfo->u.leaf->data_len;
1406 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1408 hammer_ref(&ip->lock);
1409 if (hammer_get_vnode(ip, &vp) == 0) {
1410 if ((bp = findblk(ip->vp, file_offset, FINDBLK_TEST)) != NULL &&
1411 bp->b_bio2.bio_offset != NOOFFSET) {
1412 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1413 bp->b_bio2.bio_offset = NOOFFSET;
1414 brelse(bp);
1416 vput(vp);
1418 hammer_rel_inode(ip, 0);
1419 return(0);
1424 * This function is called when writes may have occured on the volume,
1425 * indicating that the device may be holding cached writes.
1427 static void
1428 hammer_io_flush_mark(hammer_volume_t volume)
1430 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1434 * This function ensures that the device has flushed any cached writes out.
1436 void
1437 hammer_io_flush_sync(hammer_mount_t hmp)
1439 hammer_volume_t volume;
1440 struct buf *bp_base = NULL;
1441 struct buf *bp;
1443 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1444 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1445 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1446 bp = getpbuf(NULL);
1447 bp->b_bio1.bio_offset = 0;
1448 bp->b_bufsize = 0;
1449 bp->b_bcount = 0;
1450 bp->b_cmd = BUF_CMD_FLUSH;
1451 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1452 bp->b_bio1.bio_done = biodone_sync;
1453 bp->b_bio1.bio_flags |= BIO_SYNC;
1454 bp_base = bp;
1455 vn_strategy(volume->devvp, &bp->b_bio1);
1458 while ((bp = bp_base) != NULL) {
1459 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1460 biowait(&bp->b_bio1, "hmrFLS");
1461 relpbuf(bp, NULL);