HAMMER: Mass storage flush command support
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
blob2bc55b4ed358b72f4eb0354ef67f5b32fb05e0fc
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.54 2008/08/29 20:19:08 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);
64 static void hammer_io_flush_sync_done(struct bio *bio);
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.
71 void
72 hammer_io_init(hammer_io_t io, hammer_volume_t volume, enum hammer_io_type type)
74 io->volume = volume;
75 io->hmp = volume->io.hmp;
76 io->type = type;
80 * Helper routine to disassociate a buffer cache buffer from an I/O
81 * structure. The buffer is unlocked and marked appropriate for reclamation.
83 * The io may have 0 or 1 references depending on who called us. The
84 * caller is responsible for dealing with the refs.
86 * This call can only be made when no action is required on the buffer.
88 * The caller must own the buffer and the IO must indicate that the
89 * structure no longer owns it (io.released != 0).
91 static void
92 hammer_io_disassociate(hammer_io_structure_t iou)
94 struct buf *bp = iou->io.bp;
96 KKASSERT(iou->io.released);
97 KKASSERT(iou->io.modified == 0);
98 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
99 buf_dep_init(bp);
100 iou->io.bp = NULL;
103 * If the buffer was locked someone wanted to get rid of it.
105 if (bp->b_flags & B_LOCKED) {
106 --hammer_count_io_locked;
107 bp->b_flags &= ~B_LOCKED;
109 if (iou->io.reclaim) {
110 bp->b_flags |= B_NOCACHE|B_RELBUF;
111 iou->io.reclaim = 0;
114 switch(iou->io.type) {
115 case HAMMER_STRUCTURE_VOLUME:
116 iou->volume.ondisk = NULL;
117 break;
118 case HAMMER_STRUCTURE_DATA_BUFFER:
119 case HAMMER_STRUCTURE_META_BUFFER:
120 case HAMMER_STRUCTURE_UNDO_BUFFER:
121 iou->buffer.ondisk = NULL;
122 break;
127 * Wait for any physical IO to complete
129 void
130 hammer_io_wait(hammer_io_t io)
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;
145 crit_exit();
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).
154 void
155 hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
157 hammer_io_flush_sync(hmp);
158 crit_enter();
159 while (hmp->io_running_space)
160 tsleep(&hmp->io_running_space, 0, ident, 0);
161 crit_exit();
164 #define HAMMER_MAXRA 4
167 * Load bp for a HAMMER structure. The io must be exclusively locked by
168 * the caller.
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.
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.
179 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
181 struct buf *bp;
182 int error;
184 if ((bp = io->bp) == NULL) {
185 hammer_count_io_running_read += io->bytes;
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);
194 hammer_stats_disk_read += io->bytes;
195 hammer_count_io_running_read -= io->bytes;
198 * The code generally assumes b_ops/b_dep has been set-up,
199 * even if we error out here.
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);
206 KKASSERT(io->modified == 0);
207 KKASSERT(io->running == 0);
208 KKASSERT(io->waiting == 0);
209 io->released = 0; /* we hold an active lock on bp */
210 } else {
211 error = 0;
213 return(error);
217 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
218 * Must be called with the IO exclusively locked.
220 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
221 * I/O by forcing the buffer to not be in a released state before calling
222 * it.
224 * This function will also mark the IO as modified but it will not
225 * increment the modify_refs count.
228 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
230 struct buf *bp;
232 if ((bp = io->bp) == NULL) {
233 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
234 bp = io->bp;
235 bp->b_ops = &hammer_bioops;
236 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
237 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
238 io->released = 0;
239 KKASSERT(io->running == 0);
240 io->waiting = 0;
241 BUF_KERNPROC(bp);
242 } else {
243 if (io->released) {
244 regetblk(bp);
245 BUF_KERNPROC(bp);
246 io->released = 0;
249 hammer_io_modify(io, 0);
250 vfs_bio_clrbuf(bp);
251 return(0);
255 * Remove potential device level aliases against buffers managed by high level
256 * vnodes. Aliases can also be created due to mixed buffer sizes.
258 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
259 * does not exist its backing VM pages might, and we have to invalidate
260 * those as well or a getblk() will reinstate them.
262 void
263 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
265 hammer_io_structure_t iou;
266 hammer_off_t phys_offset;
267 struct buf *bp;
269 phys_offset = volume->ondisk->vol_buf_beg +
270 (zone2_offset & HAMMER_OFF_SHORT_MASK);
271 crit_enter();
272 if ((bp = findblk(volume->devvp, phys_offset)) != NULL)
273 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
274 else
275 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
276 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
277 hammer_ref(&iou->io.lock);
278 hammer_io_clear_modify(&iou->io, 1);
279 bundirty(bp);
280 iou->io.reclaim = 1;
281 iou->io.waitdep = 1;
282 KKASSERT(iou->io.lock.refs == 0);
283 hammer_rel_buffer(&iou->buffer, 0);
284 /*hammer_io_deallocate(bp);*/
285 } else {
286 KKASSERT((bp->b_flags & B_LOCKED) == 0);
287 bundirty(bp);
288 bp->b_flags |= B_NOCACHE|B_RELBUF;
290 brelse(bp);
291 crit_exit();
295 * This routine is called on the last reference to a hammer structure.
296 * The io is usually interlocked with io.loading and io.refs must be 1.
298 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
299 * simply means the caller finishes decrementing the ref-count on the
300 * IO structure then brelse()'s the bp. The bp may or may not still be
301 * passively associated with the IO.
303 * The only requirement here is that modified meta-data and volume-header
304 * buffer may NOT be disassociated from the IO structure, and consequently
305 * we also leave such buffers actively associated with the IO if they already
306 * are (since the kernel can't do anything with them anyway). Only the
307 * flusher is allowed to write such buffers out. Modified pure-data and
308 * undo buffers are returned to the kernel but left passively associated
309 * so we can track when the kernel writes the bp out.
311 struct buf *
312 hammer_io_release(struct hammer_io *io, int flush)
314 union hammer_io_structure *iou = (void *)io;
315 struct buf *bp;
317 if ((bp = io->bp) == NULL)
318 return(NULL);
321 * Try to flush a dirty IO to disk if asked to by the
322 * caller or if the kernel tried to flush the buffer in the past.
324 * Kernel-initiated flushes are only allowed for pure-data buffers.
325 * meta-data and volume buffers can only be flushed explicitly
326 * by HAMMER.
328 if (io->modified) {
329 if (flush) {
330 hammer_io_flush(io);
331 } else if (bp->b_flags & B_LOCKED) {
332 switch(io->type) {
333 case HAMMER_STRUCTURE_DATA_BUFFER:
334 case HAMMER_STRUCTURE_UNDO_BUFFER:
335 hammer_io_flush(io);
336 break;
337 default:
338 break;
340 } /* else no explicit request to flush the buffer */
344 * Wait for the IO to complete if asked to. This occurs when
345 * the buffer must be disposed of definitively during an umount
346 * or buffer invalidation.
348 if (io->waitdep && io->running) {
349 hammer_io_wait(io);
353 * Return control of the buffer to the kernel (with the provisio
354 * that our bioops can override kernel decisions with regards to
355 * the buffer).
357 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
359 * Always disassociate the bp if an explicit flush
360 * was requested and the IO completed with no error
361 * (so unmount can really clean up the structure).
363 if (io->released) {
364 regetblk(bp);
365 BUF_KERNPROC(bp);
366 } else {
367 io->released = 1;
369 hammer_io_disassociate((hammer_io_structure_t)io);
370 /* return the bp */
371 } else if (io->modified) {
373 * Only certain IO types can be released to the kernel if
374 * the buffer has been modified.
376 * volume and meta-data IO types may only be explicitly
377 * flushed by HAMMER.
379 switch(io->type) {
380 case HAMMER_STRUCTURE_DATA_BUFFER:
381 case HAMMER_STRUCTURE_UNDO_BUFFER:
382 if (io->released == 0) {
383 io->released = 1;
384 bdwrite(bp);
386 break;
387 default:
388 break;
390 bp = NULL; /* bp left associated */
391 } else if (io->released == 0) {
393 * Clean buffers can be generally released to the kernel.
394 * We leave the bp passively associated with the HAMMER
395 * structure and use bioops to disconnect it later on
396 * if the kernel wants to discard the buffer.
398 * We can steal the structure's ownership of the bp.
400 io->released = 1;
401 if (bp->b_flags & B_LOCKED) {
402 hammer_io_disassociate(iou);
403 /* return the bp */
404 } else {
405 if (io->reclaim) {
406 hammer_io_disassociate(iou);
407 /* return the bp */
408 } else {
409 /* return the bp (bp passively associated) */
412 } else {
414 * A released buffer is passively associate with our
415 * hammer_io structure. The kernel cannot destroy it
416 * without making a bioops call. If the kernel (B_LOCKED)
417 * or we (reclaim) requested that the buffer be destroyed
418 * we destroy it, otherwise we do a quick get/release to
419 * reset its position in the kernel's LRU list.
421 * Leaving the buffer passively associated allows us to
422 * use the kernel's LRU buffer flushing mechanisms rather
423 * then rolling our own.
425 * XXX there are two ways of doing this. We can re-acquire
426 * and passively release to reset the LRU, or not.
428 if (io->running == 0) {
429 regetblk(bp);
430 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
431 hammer_io_disassociate(iou);
432 /* return the bp */
433 } else {
434 /* return the bp (bp passively associated) */
436 } else {
438 * bp is left passively associated but we do not
439 * try to reacquire it. Interactions with the io
440 * structure will occur on completion of the bp's
441 * I/O.
443 bp = NULL;
446 return(bp);
450 * This routine is called with a locked IO when a flush is desired and
451 * no other references to the structure exists other then ours. This
452 * routine is ONLY called when HAMMER believes it is safe to flush a
453 * potentially modified buffer out.
455 void
456 hammer_io_flush(struct hammer_io *io)
458 struct buf *bp;
461 * Degenerate case - nothing to flush if nothing is dirty.
463 if (io->modified == 0) {
464 return;
467 KKASSERT(io->bp);
468 KKASSERT(io->modify_refs <= 0);
471 * Acquire ownership of the bp, particularly before we clear our
472 * modified flag.
474 * We are going to bawrite() this bp. Don't leave a window where
475 * io->released is set, we actually own the bp rather then our
476 * buffer.
478 bp = io->bp;
479 if (io->released) {
480 regetblk(bp);
481 /* BUF_KERNPROC(io->bp); */
482 /* io->released = 0; */
483 KKASSERT(io->released);
484 KKASSERT(io->bp == bp);
486 io->released = 1;
489 * Acquire exclusive access to the bp and then clear the modified
490 * state of the buffer prior to issuing I/O to interlock any
491 * modifications made while the I/O is in progress. This shouldn't
492 * happen anyway but losing data would be worse. The modified bit
493 * will be rechecked after the IO completes.
495 * NOTE: This call also finalizes the buffer's content (inval == 0).
497 * This is only legal when lock.refs == 1 (otherwise we might clear
498 * the modified bit while there are still users of the cluster
499 * modifying the data).
501 * Do this before potentially blocking so any attempt to modify the
502 * ondisk while we are blocked blocks waiting for us.
504 hammer_ref(&io->lock);
505 hammer_io_clear_modify(io, 0);
506 hammer_unref(&io->lock);
509 * Transfer ownership to the kernel and initiate I/O.
511 io->running = 1;
512 io->hmp->io_running_space += io->bytes;
513 hammer_count_io_running_write += io->bytes;
514 bawrite(bp);
515 hammer_io_flush_mark(io->volume);
518 /************************************************************************
519 * BUFFER DIRTYING *
520 ************************************************************************
522 * These routines deal with dependancies created when IO buffers get
523 * modified. The caller must call hammer_modify_*() on a referenced
524 * HAMMER structure prior to modifying its on-disk data.
526 * Any intent to modify an IO buffer acquires the related bp and imposes
527 * various write ordering dependancies.
531 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
532 * are locked until the flusher can deal with them, pure data buffers
533 * can be written out.
535 static
536 void
537 hammer_io_modify(hammer_io_t io, int count)
540 * io->modify_refs must be >= 0
542 while (io->modify_refs < 0) {
543 io->waitmod = 1;
544 tsleep(io, 0, "hmrmod", 0);
548 * Shortcut if nothing to do.
550 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
551 io->modify_refs += count;
552 if (io->modified && io->released == 0)
553 return;
555 hammer_lock_ex(&io->lock);
556 if (io->modified == 0) {
557 hammer_io_set_modlist(io);
558 io->modified = 1;
560 if (io->released) {
561 regetblk(io->bp);
562 BUF_KERNPROC(io->bp);
563 io->released = 0;
564 KKASSERT(io->modified != 0);
566 hammer_unlock(&io->lock);
569 static __inline
570 void
571 hammer_io_modify_done(hammer_io_t io)
573 KKASSERT(io->modify_refs > 0);
574 --io->modify_refs;
575 if (io->modify_refs == 0 && io->waitmod) {
576 io->waitmod = 0;
577 wakeup(io);
581 void
582 hammer_io_write_interlock(hammer_io_t io)
584 while (io->modify_refs != 0) {
585 io->waitmod = 1;
586 tsleep(io, 0, "hmrmod", 0);
588 io->modify_refs = -1;
591 void
592 hammer_io_done_interlock(hammer_io_t io)
594 KKASSERT(io->modify_refs == -1);
595 io->modify_refs = 0;
596 if (io->waitmod) {
597 io->waitmod = 0;
598 wakeup(io);
603 * Caller intends to modify a volume's ondisk structure.
605 * This is only allowed if we are the flusher or we have a ref on the
606 * sync_lock.
608 void
609 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
610 void *base, int len)
612 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
614 hammer_io_modify(&volume->io, 1);
615 if (len) {
616 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
617 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
618 hammer_generate_undo(trans, &volume->io,
619 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
620 base, len);
625 * Caller intends to modify a buffer's ondisk structure.
627 * This is only allowed if we are the flusher or we have a ref on the
628 * sync_lock.
630 void
631 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
632 void *base, int len)
634 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
636 hammer_io_modify(&buffer->io, 1);
637 if (len) {
638 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
639 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
640 hammer_generate_undo(trans, &buffer->io,
641 buffer->zone2_offset + rel_offset,
642 base, len);
646 void
647 hammer_modify_volume_done(hammer_volume_t volume)
649 hammer_io_modify_done(&volume->io);
652 void
653 hammer_modify_buffer_done(hammer_buffer_t buffer)
655 hammer_io_modify_done(&buffer->io);
659 * Mark an entity as not being dirty any more and finalize any
660 * delayed adjustments to the buffer.
662 * Delayed adjustments are an important performance enhancement, allowing
663 * us to avoid recalculating B-Tree node CRCs over and over again when
664 * making bulk-modifications to the B-Tree.
666 * If inval is non-zero delayed adjustments are ignored.
668 * This routine may dereference related btree nodes and cause the
669 * buffer to be dereferenced. The caller must own a reference on io.
671 void
672 hammer_io_clear_modify(struct hammer_io *io, int inval)
674 if (io->modified == 0)
675 return;
678 * Take us off the mod-list and clear the modified bit.
680 KKASSERT(io->mod_list != NULL);
681 if (io->mod_list == &io->hmp->volu_list ||
682 io->mod_list == &io->hmp->meta_list) {
683 io->hmp->locked_dirty_space -= io->bytes;
684 hammer_count_dirtybufspace -= io->bytes;
686 TAILQ_REMOVE(io->mod_list, io, mod_entry);
687 io->mod_list = NULL;
688 io->modified = 0;
691 * If this bit is not set there are no delayed adjustments.
693 if (io->gencrc == 0)
694 return;
695 io->gencrc = 0;
698 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
699 * on the node (& underlying buffer). Release the node after clearing
700 * the flag.
702 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
703 hammer_buffer_t buffer = (void *)io;
704 hammer_node_t node;
706 restart:
707 TAILQ_FOREACH(node, &buffer->clist, entry) {
708 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
709 continue;
710 node->flags &= ~HAMMER_NODE_NEEDSCRC;
711 KKASSERT(node->ondisk);
712 if (inval == 0)
713 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
714 hammer_rel_node(node);
715 goto restart;
718 /* caller must still have ref on io */
719 KKASSERT(io->lock.refs > 0);
723 * Clear the IO's modify list. Even though the IO is no longer modified
724 * it may still be on the lose_list. This routine is called just before
725 * the governing hammer_buffer is destroyed.
727 void
728 hammer_io_clear_modlist(struct hammer_io *io)
730 KKASSERT(io->modified == 0);
731 if (io->mod_list) {
732 crit_enter(); /* biodone race against list */
733 KKASSERT(io->mod_list == &io->hmp->lose_list);
734 TAILQ_REMOVE(io->mod_list, io, mod_entry);
735 io->mod_list = NULL;
736 crit_exit();
740 static void
741 hammer_io_set_modlist(struct hammer_io *io)
743 struct hammer_mount *hmp = io->hmp;
745 KKASSERT(io->mod_list == NULL);
747 switch(io->type) {
748 case HAMMER_STRUCTURE_VOLUME:
749 io->mod_list = &hmp->volu_list;
750 hmp->locked_dirty_space += io->bytes;
751 hammer_count_dirtybufspace += io->bytes;
752 break;
753 case HAMMER_STRUCTURE_META_BUFFER:
754 io->mod_list = &hmp->meta_list;
755 hmp->locked_dirty_space += io->bytes;
756 hammer_count_dirtybufspace += io->bytes;
757 break;
758 case HAMMER_STRUCTURE_UNDO_BUFFER:
759 io->mod_list = &hmp->undo_list;
760 break;
761 case HAMMER_STRUCTURE_DATA_BUFFER:
762 io->mod_list = &hmp->data_list;
763 break;
765 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
768 /************************************************************************
769 * HAMMER_BIOOPS *
770 ************************************************************************
775 * Pre-IO initiation kernel callback - cluster build only
777 static void
778 hammer_io_start(struct buf *bp)
783 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
785 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
786 * may also be set if we were marking a cluster header open. Only remove
787 * our dependancy if the modified bit is clear.
789 static void
790 hammer_io_complete(struct buf *bp)
792 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
794 KKASSERT(iou->io.released == 1);
797 * Deal with people waiting for I/O to drain
799 if (iou->io.running) {
801 * Deal with critical write errors. Once a critical error
802 * has been flagged in hmp the UNDO FIFO will not be updated.
803 * That way crash recover will give us a consistent
804 * filesystem.
806 * Because of this we can throw away failed UNDO buffers. If
807 * we throw away META or DATA buffers we risk corrupting
808 * the now read-only version of the filesystem visible to
809 * the user. Clear B_ERROR so the buffer is not re-dirtied
810 * by the kernel and ref the io so it doesn't get thrown
811 * away.
813 if (bp->b_flags & B_ERROR) {
814 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
815 "while flushing meta-data");
816 switch(iou->io.type) {
817 case HAMMER_STRUCTURE_UNDO_BUFFER:
818 break;
819 default:
820 if (iou->io.ioerror == 0) {
821 iou->io.ioerror = 1;
822 if (iou->io.lock.refs == 0)
823 ++hammer_count_refedbufs;
824 hammer_ref(&iou->io.lock);
826 break;
828 bp->b_flags &= ~B_ERROR;
829 bundirty(bp);
830 #if 0
831 hammer_io_set_modlist(&iou->io);
832 iou->io.modified = 1;
833 #endif
835 hammer_stats_disk_write += iou->io.bytes;
836 hammer_count_io_running_write -= iou->io.bytes;
837 iou->io.hmp->io_running_space -= iou->io.bytes;
838 if (iou->io.hmp->io_running_space == 0)
839 wakeup(&iou->io.hmp->io_running_space);
840 KKASSERT(iou->io.hmp->io_running_space >= 0);
841 iou->io.running = 0;
842 } else {
843 hammer_stats_disk_read += iou->io.bytes;
846 if (iou->io.waiting) {
847 iou->io.waiting = 0;
848 wakeup(iou);
852 * If B_LOCKED is set someone wanted to deallocate the bp at some
853 * point, do it now if refs has become zero.
855 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
856 KKASSERT(iou->io.modified == 0);
857 --hammer_count_io_locked;
858 bp->b_flags &= ~B_LOCKED;
859 hammer_io_deallocate(bp);
860 /* structure may be dead now */
865 * Callback from kernel when it wishes to deallocate a passively
866 * associated structure. This mostly occurs with clean buffers
867 * but it may be possible for a holding structure to be marked dirty
868 * while its buffer is passively associated. The caller owns the bp.
870 * If we cannot disassociate we set B_LOCKED to prevent the buffer
871 * from getting reused.
873 * WARNING: Because this can be called directly by getnewbuf we cannot
874 * recurse into the tree. If a bp cannot be immediately disassociated
875 * our only recourse is to set B_LOCKED.
877 * WARNING: This may be called from an interrupt via hammer_io_complete()
879 static void
880 hammer_io_deallocate(struct buf *bp)
882 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
884 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
885 if (iou->io.lock.refs > 0 || iou->io.modified) {
887 * It is not legal to disassociate a modified buffer. This
888 * case really shouldn't ever occur.
890 bp->b_flags |= B_LOCKED;
891 ++hammer_count_io_locked;
892 } else {
894 * Disassociate the BP. If the io has no refs left we
895 * have to add it to the loose list.
897 hammer_io_disassociate(iou);
898 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
899 KKASSERT(iou->io.bp == NULL);
900 KKASSERT(iou->io.mod_list == NULL);
901 crit_enter(); /* biodone race against list */
902 iou->io.mod_list = &iou->io.hmp->lose_list;
903 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
904 crit_exit();
909 static int
910 hammer_io_fsync(struct vnode *vp)
912 return(0);
916 * NOTE: will not be called unless we tell the kernel about the
917 * bioops. Unused... we use the mount's VFS_SYNC instead.
919 static int
920 hammer_io_sync(struct mount *mp)
922 return(0);
925 static void
926 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
931 * I/O pre-check for reading and writing. HAMMER only uses this for
932 * B_CACHE buffers so checkread just shouldn't happen, but if it does
933 * allow it.
935 * Writing is a different case. We don't want the kernel to try to write
936 * out a buffer that HAMMER may be modifying passively or which has a
937 * dependancy. In addition, kernel-demanded writes can only proceed for
938 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
939 * buffer types can only be explicitly written by the flusher.
941 * checkwrite will only be called for bdwrite()n buffers. If we return
942 * success the kernel is guaranteed to initiate the buffer write.
944 static int
945 hammer_io_checkread(struct buf *bp)
947 return(0);
950 static int
951 hammer_io_checkwrite(struct buf *bp)
953 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
956 * This shouldn't happen under normal operation.
958 if (io->type == HAMMER_STRUCTURE_VOLUME ||
959 io->type == HAMMER_STRUCTURE_META_BUFFER) {
960 if (!panicstr)
961 panic("hammer_io_checkwrite: illegal buffer");
962 if ((bp->b_flags & B_LOCKED) == 0) {
963 bp->b_flags |= B_LOCKED;
964 ++hammer_count_io_locked;
966 return(1);
970 * We can only clear the modified bit if the IO is not currently
971 * undergoing modification. Otherwise we may miss changes.
973 * Only data and undo buffers can reach here. These buffers do
974 * not have terminal crc functions but we temporarily reference
975 * the IO anyway, just in case.
977 if (io->modify_refs == 0 && io->modified) {
978 hammer_ref(&io->lock);
979 hammer_io_clear_modify(io, 0);
980 hammer_unref(&io->lock);
981 } else if (io->modified) {
982 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
986 * The kernel is going to start the IO, set io->running.
988 KKASSERT(io->running == 0);
989 io->running = 1;
990 io->hmp->io_running_space += io->bytes;
991 hammer_count_io_running_write += io->bytes;
992 return(0);
996 * Return non-zero if we wish to delay the kernel's attempt to flush
997 * this buffer to disk.
999 static int
1000 hammer_io_countdeps(struct buf *bp, int n)
1002 return(0);
1005 struct bio_ops hammer_bioops = {
1006 .io_start = hammer_io_start,
1007 .io_complete = hammer_io_complete,
1008 .io_deallocate = hammer_io_deallocate,
1009 .io_fsync = hammer_io_fsync,
1010 .io_sync = hammer_io_sync,
1011 .io_movedeps = hammer_io_movedeps,
1012 .io_countdeps = hammer_io_countdeps,
1013 .io_checkread = hammer_io_checkread,
1014 .io_checkwrite = hammer_io_checkwrite,
1017 /************************************************************************
1018 * DIRECT IO OPS *
1019 ************************************************************************
1021 * These functions operate directly on the buffer cache buffer associated
1022 * with a front-end vnode rather then a back-end device vnode.
1026 * Read a buffer associated with a front-end vnode directly from the
1027 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1028 * we validate the CRC.
1030 * We must check for the presence of a HAMMER buffer to handle the case
1031 * where the reblocker has rewritten the data (which it does via the HAMMER
1032 * buffer system, not via the high-level vnode buffer cache), but not yet
1033 * committed the buffer to the media.
1036 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1037 hammer_btree_leaf_elm_t leaf)
1039 hammer_off_t buf_offset;
1040 hammer_off_t zone2_offset;
1041 hammer_volume_t volume;
1042 struct buf *bp;
1043 struct bio *nbio;
1044 int vol_no;
1045 int error;
1047 buf_offset = bio->bio_offset;
1048 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1049 HAMMER_ZONE_LARGE_DATA);
1052 * The buffer cache may have an aliased buffer (the reblocker can
1053 * write them). If it does we have to sync any dirty data before
1054 * we can build our direct-read. This is a non-critical code path.
1056 bp = bio->bio_buf;
1057 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1060 * Resolve to a zone-2 offset. The conversion just requires
1061 * munging the top 4 bits but we want to abstract it anyway
1062 * so the blockmap code can verify the zone assignment.
1064 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1065 if (error)
1066 goto done;
1067 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1068 HAMMER_ZONE_RAW_BUFFER);
1071 * Resolve volume and raw-offset for 3rd level bio. The
1072 * offset will be specific to the volume.
1074 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1075 volume = hammer_get_volume(hmp, vol_no, &error);
1076 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1077 error = EIO;
1079 if (error == 0) {
1081 * 3rd level bio
1083 nbio = push_bio(bio);
1084 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1085 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1086 #if 0
1088 * XXX disabled - our CRC check doesn't work if the OS
1089 * does bogus_page replacement on the direct-read.
1091 if (leaf && hammer_verify_data) {
1092 nbio->bio_done = hammer_io_direct_read_complete;
1093 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1095 #endif
1096 hammer_stats_disk_read += bp->b_bufsize;
1097 vn_strategy(volume->devvp, nbio);
1099 hammer_rel_volume(volume, 0);
1100 done:
1101 if (error) {
1102 kprintf("hammer_direct_read: failed @ %016llx\n",
1103 zone2_offset);
1104 bp->b_error = error;
1105 bp->b_flags |= B_ERROR;
1106 biodone(bio);
1108 return(error);
1111 #if 0
1113 * On completion of the BIO this callback must check the data CRC
1114 * and chain to the previous bio.
1116 static
1117 void
1118 hammer_io_direct_read_complete(struct bio *nbio)
1120 struct bio *obio;
1121 struct buf *bp;
1122 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1124 bp = nbio->bio_buf;
1125 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1126 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1127 nbio->bio_offset, bp->b_bufsize);
1128 if (hammer_debug_debug)
1129 Debugger("");
1130 bp->b_flags |= B_ERROR;
1131 bp->b_error = EIO;
1133 obio = pop_bio(nbio);
1134 biodone(obio);
1136 #endif
1139 * Write a buffer associated with a front-end vnode directly to the
1140 * disk media. The bio may be issued asynchronously.
1142 * The BIO is associated with the specified record and RECF_DIRECT_IO
1143 * is set. The recorded is added to its object.
1146 hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
1147 struct bio *bio)
1149 hammer_btree_leaf_elm_t leaf = &record->leaf;
1150 hammer_off_t buf_offset;
1151 hammer_off_t zone2_offset;
1152 hammer_volume_t volume;
1153 hammer_buffer_t buffer;
1154 struct buf *bp;
1155 struct bio *nbio;
1156 char *ptr;
1157 int vol_no;
1158 int error;
1160 buf_offset = leaf->data_offset;
1162 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1163 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1165 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1166 leaf->data_len >= HAMMER_BUFSIZE) {
1168 * We are using the vnode's bio to write directly to the
1169 * media, any hammer_buffer at the same zone-X offset will
1170 * now have stale data.
1172 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1173 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1174 volume = hammer_get_volume(hmp, vol_no, &error);
1176 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1177 error = EIO;
1178 if (error == 0) {
1179 bp = bio->bio_buf;
1180 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1182 hammer_del_buffers(hmp, buf_offset,
1183 zone2_offset, bp->b_bufsize);
1187 * Second level bio - cached zone2 offset.
1189 * (We can put our bio_done function in either the
1190 * 2nd or 3rd level).
1192 nbio = push_bio(bio);
1193 nbio->bio_offset = zone2_offset;
1194 nbio->bio_done = hammer_io_direct_write_complete;
1195 nbio->bio_caller_info1.ptr = record;
1196 record->zone2_offset = zone2_offset;
1197 record->flags |= HAMMER_RECF_DIRECT_IO |
1198 HAMMER_RECF_DIRECT_INVAL;
1201 * Third level bio - raw offset specific to the
1202 * correct volume.
1204 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1205 nbio = push_bio(nbio);
1206 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1207 zone2_offset;
1208 hammer_stats_disk_write += bp->b_bufsize;
1209 vn_strategy(volume->devvp, nbio);
1210 hammer_io_flush_mark(volume);
1212 hammer_rel_volume(volume, 0);
1213 } else {
1215 * Must fit in a standard HAMMER buffer. In this case all
1216 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1217 * does not need to be set-up.
1219 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1220 buffer = NULL;
1221 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1222 if (error == 0) {
1223 bp = bio->bio_buf;
1224 bp->b_flags |= B_AGE;
1225 hammer_io_modify(&buffer->io, 1);
1226 bcopy(bp->b_data, ptr, leaf->data_len);
1227 hammer_io_modify_done(&buffer->io);
1228 hammer_rel_buffer(buffer, 0);
1229 bp->b_resid = 0;
1230 biodone(bio);
1233 if (error == 0) {
1235 * The record is all setup now, add it. Potential conflics
1236 * have already been dealt with.
1238 error = hammer_mem_add(record);
1239 KKASSERT(error == 0);
1240 } else {
1242 * Major suckage occured.
1244 kprintf("hammer_direct_write: failed @ %016llx\n",
1245 leaf->data_offset);
1246 bp = bio->bio_buf;
1247 bp->b_resid = 0;
1248 bp->b_error = EIO;
1249 bp->b_flags |= B_ERROR;
1250 biodone(bio);
1251 record->flags |= HAMMER_RECF_DELETED_FE;
1252 hammer_rel_mem_record(record);
1254 return(error);
1258 * On completion of the BIO this callback must disconnect
1259 * it from the hammer_record and chain to the previous bio.
1261 * An I/O error forces the mount to read-only. Data buffers
1262 * are not B_LOCKED like meta-data buffers are, so we have to
1263 * throw the buffer away to prevent the kernel from retrying.
1265 static
1266 void
1267 hammer_io_direct_write_complete(struct bio *nbio)
1269 struct bio *obio;
1270 struct buf *bp;
1271 hammer_record_t record = nbio->bio_caller_info1.ptr;
1273 bp = nbio->bio_buf;
1274 obio = pop_bio(nbio);
1275 if (bp->b_flags & B_ERROR) {
1276 hammer_critical_error(record->ip->hmp, record->ip,
1277 bp->b_error,
1278 "while writing bulk data");
1279 bp->b_flags |= B_INVAL;
1281 biodone(obio);
1283 KKASSERT(record != NULL);
1284 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1285 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1286 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1287 record->flags &= ~HAMMER_RECF_DIRECT_WAIT;
1288 wakeup(&record->flags);
1294 * This is called before a record is either committed to the B-Tree
1295 * or destroyed, to resolve any associated direct-IO.
1297 * (1) We must wait for any direct-IO related to the record to complete.
1299 * (2) We must remove any buffer cache aliases for data accessed via
1300 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1301 * (the mirroring and reblocking code) do not see stale data.
1303 void
1304 hammer_io_direct_wait(hammer_record_t record)
1307 * Wait for I/O to complete
1309 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1310 crit_enter();
1311 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1312 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1313 tsleep(&record->flags, 0, "hmdiow", 0);
1315 crit_exit();
1319 * Invalidate any related buffer cache aliases.
1321 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1322 KKASSERT(record->leaf.data_offset);
1323 hammer_del_buffers(record->ip->hmp,
1324 record->leaf.data_offset,
1325 record->zone2_offset,
1326 record->leaf.data_len);
1327 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1332 * This is called to remove the second-level cached zone-2 offset from
1333 * frontend buffer cache buffers, now stale due to a data relocation.
1334 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1335 * by hammer_vop_strategy_read().
1337 * This is rather nasty because here we have something like the reblocker
1338 * scanning the raw B-Tree with no held references on anything, really,
1339 * other then a shared lock on the B-Tree node, and we have to access the
1340 * frontend's buffer cache to check for and clean out the association.
1341 * Specifically, if the reblocker is moving data on the disk, these cached
1342 * offsets will become invalid.
1344 * Only data record types associated with the large-data zone are subject
1345 * to direct-io and need to be checked.
1348 void
1349 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1351 struct hammer_inode_info iinfo;
1352 int zone;
1354 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1355 return;
1356 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1357 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1358 return;
1359 iinfo.obj_id = leaf->base.obj_id;
1360 iinfo.obj_asof = 0; /* unused */
1361 iinfo.obj_localization = leaf->base.localization &
1362 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1363 iinfo.u.leaf = leaf;
1364 hammer_scan_inode_snapshots(hmp, &iinfo,
1365 hammer_io_direct_uncache_callback,
1366 leaf);
1369 static int
1370 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1372 hammer_inode_info_t iinfo = data;
1373 hammer_off_t data_offset;
1374 hammer_off_t file_offset;
1375 struct vnode *vp;
1376 struct buf *bp;
1377 int blksize;
1379 if (ip->vp == NULL)
1380 return(0);
1381 data_offset = iinfo->u.leaf->data_offset;
1382 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1383 blksize = iinfo->u.leaf->data_len;
1384 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1386 hammer_ref(&ip->lock);
1387 if (hammer_get_vnode(ip, &vp) == 0) {
1388 if ((bp = findblk(ip->vp, file_offset)) != NULL &&
1389 bp->b_bio2.bio_offset != NOOFFSET) {
1390 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1391 bp->b_bio2.bio_offset = NOOFFSET;
1392 brelse(bp);
1394 vput(vp);
1396 hammer_rel_inode(ip, 0);
1397 return(0);
1402 * This function is called when writes may have occured on the volume,
1403 * indicating that the device may be holding cached writes.
1405 static void
1406 hammer_io_flush_mark(hammer_volume_t volume)
1408 volume->vol_flags |= HAMMER_VOLF_NEEDFLUSH;
1412 * This function ensures that the device has flushed any cached writes out.
1414 void
1415 hammer_io_flush_sync(hammer_mount_t hmp)
1417 hammer_volume_t volume;
1418 struct buf *bp_base = NULL;
1419 struct buf *bp;
1421 RB_FOREACH(volume, hammer_vol_rb_tree, &hmp->rb_vols_root) {
1422 if (volume->vol_flags & HAMMER_VOLF_NEEDFLUSH) {
1423 volume->vol_flags &= ~HAMMER_VOLF_NEEDFLUSH;
1424 bp = getpbuf(NULL);
1425 bp->b_bio1.bio_offset = 0;
1426 bp->b_bufsize = 0;
1427 bp->b_bcount = 0;
1428 bp->b_cmd = BUF_CMD_FLUSH;
1429 bp->b_bio1.bio_caller_info1.cluster_head = bp_base;
1430 bp->b_bio1.bio_done = hammer_io_flush_sync_done;
1431 bp->b_flags |= B_ASYNC;
1432 bp_base = bp;
1433 vn_strategy(volume->devvp, &bp->b_bio1);
1436 while ((bp = bp_base) != NULL) {
1437 bp_base = bp->b_bio1.bio_caller_info1.cluster_head;
1438 while (bp->b_cmd != BUF_CMD_DONE) {
1439 crit_enter();
1440 tsleep_interlock(&bp->b_cmd);
1441 if (bp->b_cmd != BUF_CMD_DONE)
1442 tsleep(&bp->b_cmd, 0, "hmrFLS", 0);
1443 crit_exit();
1445 bp->b_flags &= ~B_ASYNC;
1446 relpbuf(bp, NULL);
1451 * Callback to deal with completed flush commands to the device.
1453 static void
1454 hammer_io_flush_sync_done(struct bio *bio)
1456 struct buf *bp;
1458 bp = bio->bio_buf;
1459 bp->b_cmd = BUF_CMD_DONE;
1460 wakeup(&bp->b_cmd);