2f71c5bf54cebcdabf6245ffbd3be072c971f5a6
[dragonfly.git] / sys / vfs / hammer / hammer_io.c
blob2f71c5bf54cebcdabf6245ffbd3be072c971f5a6
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.49.2.5 2008/09/15 17:10:03 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);
65 * Initialize a new, already-zero'd hammer_io structure, or reinitialize
66 * an existing hammer_io structure which may have switched to another type.
68 void
69 hammer_io_init(hammer_io_t io, hammer_mount_t hmp, enum hammer_io_type type)
71 io->hmp = hmp;
72 io->type = type;
76 * Helper routine to disassociate a buffer cache buffer from an I/O
77 * structure. The buffer is unlocked and marked appropriate for reclamation.
79 * The io may have 0 or 1 references depending on who called us. The
80 * caller is responsible for dealing with the refs.
82 * This call can only be made when no action is required on the buffer.
84 * The caller must own the buffer and the IO must indicate that the
85 * structure no longer owns it (io.released != 0).
87 static void
88 hammer_io_disassociate(hammer_io_structure_t iou)
90 struct buf *bp = iou->io.bp;
92 KKASSERT(iou->io.released);
93 KKASSERT(iou->io.modified == 0);
94 KKASSERT(LIST_FIRST(&bp->b_dep) == (void *)iou);
95 buf_dep_init(bp);
96 iou->io.bp = NULL;
99 * If the buffer was locked someone wanted to get rid of it.
101 if (bp->b_flags & B_LOCKED) {
102 --hammer_count_io_locked;
103 bp->b_flags &= ~B_LOCKED;
105 if (iou->io.reclaim) {
106 bp->b_flags |= B_NOCACHE|B_RELBUF;
107 iou->io.reclaim = 0;
110 switch(iou->io.type) {
111 case HAMMER_STRUCTURE_VOLUME:
112 iou->volume.ondisk = NULL;
113 break;
114 case HAMMER_STRUCTURE_DATA_BUFFER:
115 case HAMMER_STRUCTURE_META_BUFFER:
116 case HAMMER_STRUCTURE_UNDO_BUFFER:
117 iou->buffer.ondisk = NULL;
118 break;
123 * Wait for any physical IO to complete
125 void
126 hammer_io_wait(hammer_io_t io)
128 if (io->running) {
129 crit_enter();
130 tsleep_interlock(io);
131 io->waiting = 1;
132 for (;;) {
133 tsleep(io, 0, "hmrflw", 0);
134 if (io->running == 0)
135 break;
136 tsleep_interlock(io);
137 io->waiting = 1;
138 if (io->running == 0)
139 break;
141 crit_exit();
146 * Wait for all hammer_io-initated write I/O's to complete. This is not
147 * supposed to count direct I/O's but some can leak through (for
148 * non-full-sized direct I/Os).
150 void
151 hammer_io_wait_all(hammer_mount_t hmp, const char *ident)
153 crit_enter();
154 while (hmp->io_running_space)
155 tsleep(&hmp->io_running_space, 0, ident, 0);
156 crit_exit();
159 #define HAMMER_MAXRA 4
162 * Load bp for a HAMMER structure. The io must be exclusively locked by
163 * the caller.
165 * This routine is mostly used on meta-data and small-data blocks. Generally
166 * speaking HAMMER assumes some locality of reference and will cluster
167 * a 64K read.
169 * Note that clustering occurs at the device layer, not the logical layer.
170 * If the buffers do not apply to the current operation they may apply to
171 * some other.
174 hammer_io_read(struct vnode *devvp, struct hammer_io *io, hammer_off_t limit)
176 struct buf *bp;
177 int error;
179 if ((bp = io->bp) == NULL) {
180 hammer_count_io_running_read += io->bytes;
181 if (hammer_cluster_enable) {
182 error = cluster_read(devvp, limit,
183 io->offset, io->bytes,
184 HAMMER_CLUSTER_SIZE,
185 HAMMER_CLUSTER_BUFS, &io->bp);
186 } else {
187 error = bread(devvp, io->offset, io->bytes, &io->bp);
189 hammer_stats_disk_read += io->bytes;
190 hammer_count_io_running_read -= io->bytes;
193 * The code generally assumes b_ops/b_dep has been set-up,
194 * even if we error out here.
196 bp = io->bp;
197 bp->b_ops = &hammer_bioops;
198 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
199 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
200 BUF_KERNPROC(bp);
201 KKASSERT(io->modified == 0);
202 KKASSERT(io->running == 0);
203 KKASSERT(io->waiting == 0);
204 io->released = 0; /* we hold an active lock on bp */
205 } else {
206 error = 0;
208 return(error);
212 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
213 * Must be called with the IO exclusively locked.
215 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
216 * I/O by forcing the buffer to not be in a released state before calling
217 * it.
219 * This function will also mark the IO as modified but it will not
220 * increment the modify_refs count.
223 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
225 struct buf *bp;
227 if ((bp = io->bp) == NULL) {
228 io->bp = getblk(devvp, io->offset, io->bytes, 0, 0);
229 bp = io->bp;
230 bp->b_ops = &hammer_bioops;
231 KKASSERT(LIST_FIRST(&bp->b_dep) == NULL);
232 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
233 io->released = 0;
234 KKASSERT(io->running == 0);
235 io->waiting = 0;
236 BUF_KERNPROC(bp);
237 } else {
238 if (io->released) {
239 regetblk(bp);
240 BUF_KERNPROC(bp);
241 io->released = 0;
244 hammer_io_modify(io, 0);
245 vfs_bio_clrbuf(bp);
246 return(0);
250 * Remove potential device level aliases against buffers managed by high level
251 * vnodes. Aliases can also be created due to mixed buffer sizes.
253 * This is nasty because the buffers are also VMIO-backed. Even if a buffer
254 * does not exist its backing VM pages might, and we have to invalidate
255 * those as well or a getblk() will reinstate them.
257 void
258 hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset)
260 hammer_io_structure_t iou;
261 hammer_off_t phys_offset;
262 struct buf *bp;
264 phys_offset = volume->ondisk->vol_buf_beg +
265 (zone2_offset & HAMMER_OFF_SHORT_MASK);
266 crit_enter();
267 if ((bp = findblk(volume->devvp, phys_offset)) != NULL)
268 bp = getblk(volume->devvp, phys_offset, bp->b_bufsize, 0, 0);
269 else
270 bp = getblk(volume->devvp, phys_offset, HAMMER_BUFSIZE, 0, 0);
271 if ((iou = (void *)LIST_FIRST(&bp->b_dep)) != NULL) {
272 hammer_ref(&iou->io.lock);
273 hammer_io_clear_modify(&iou->io, 1);
274 bundirty(bp);
275 iou->io.released = 0;
276 BUF_KERNPROC(bp);
277 iou->io.reclaim = 1;
278 iou->io.waitdep = 1;
279 KKASSERT(iou->io.lock.refs == 1);
280 hammer_rel_buffer(&iou->buffer, 0);
281 /*hammer_io_deallocate(bp);*/
282 } else {
283 KKASSERT((bp->b_flags & B_LOCKED) == 0);
284 bundirty(bp);
285 bp->b_flags |= B_NOCACHE|B_RELBUF;
286 brelse(bp);
288 crit_exit();
292 * This routine is called on the last reference to a hammer structure.
293 * The io is usually interlocked with io.loading and io.refs must be 1.
295 * This routine may return a non-NULL bp to the caller for dispoal. Disposal
296 * simply means the caller finishes decrementing the ref-count on the
297 * IO structure then brelse()'s the bp. The bp may or may not still be
298 * passively associated with the IO.
300 * The only requirement here is that modified meta-data and volume-header
301 * buffer may NOT be disassociated from the IO structure, and consequently
302 * we also leave such buffers actively associated with the IO if they already
303 * are (since the kernel can't do anything with them anyway). Only the
304 * flusher is allowed to write such buffers out. Modified pure-data and
305 * undo buffers are returned to the kernel but left passively associated
306 * so we can track when the kernel writes the bp out.
308 struct buf *
309 hammer_io_release(struct hammer_io *io, int flush)
311 union hammer_io_structure *iou = (void *)io;
312 struct buf *bp;
314 if ((bp = io->bp) == NULL)
315 return(NULL);
318 * Try to flush a dirty IO to disk if asked to by the
319 * caller or if the kernel tried to flush the buffer in the past.
321 * Kernel-initiated flushes are only allowed for pure-data buffers.
322 * meta-data and volume buffers can only be flushed explicitly
323 * by HAMMER.
325 if (io->modified) {
326 if (flush) {
327 hammer_io_flush(io);
328 } else if (bp->b_flags & B_LOCKED) {
329 switch(io->type) {
330 case HAMMER_STRUCTURE_DATA_BUFFER:
331 case HAMMER_STRUCTURE_UNDO_BUFFER:
332 hammer_io_flush(io);
333 break;
334 default:
335 break;
337 } /* else no explicit request to flush the buffer */
341 * Wait for the IO to complete if asked to. This occurs when
342 * the buffer must be disposed of definitively during an umount
343 * or buffer invalidation.
345 if (io->waitdep && io->running) {
346 hammer_io_wait(io);
350 * Return control of the buffer to the kernel (with the provisio
351 * that our bioops can override kernel decisions with regards to
352 * the buffer).
354 if ((flush || io->reclaim) && io->modified == 0 && io->running == 0) {
356 * Always disassociate the bp if an explicit flush
357 * was requested and the IO completed with no error
358 * (so unmount can really clean up the structure).
360 if (io->released) {
361 regetblk(bp);
362 BUF_KERNPROC(bp);
363 } else {
364 io->released = 1;
366 hammer_io_disassociate((hammer_io_structure_t)io);
367 /* return the bp */
368 } else if (io->modified) {
370 * Only certain IO types can be released to the kernel if
371 * the buffer has been modified.
373 * volume and meta-data IO types may only be explicitly
374 * flushed by HAMMER.
376 switch(io->type) {
377 case HAMMER_STRUCTURE_DATA_BUFFER:
378 case HAMMER_STRUCTURE_UNDO_BUFFER:
379 if (io->released == 0) {
380 io->released = 1;
381 bdwrite(bp);
383 break;
384 default:
385 break;
387 bp = NULL; /* bp left associated */
388 } else if (io->released == 0) {
390 * Clean buffers can be generally released to the kernel.
391 * We leave the bp passively associated with the HAMMER
392 * structure and use bioops to disconnect it later on
393 * if the kernel wants to discard the buffer.
395 * We can steal the structure's ownership of the bp.
397 io->released = 1;
398 if (bp->b_flags & B_LOCKED) {
399 hammer_io_disassociate(iou);
400 /* return the bp */
401 } else {
402 if (io->reclaim) {
403 hammer_io_disassociate(iou);
404 /* return the bp */
405 } else {
406 /* return the bp (bp passively associated) */
409 } else {
411 * A released buffer is passively associate with our
412 * hammer_io structure. The kernel cannot destroy it
413 * without making a bioops call. If the kernel (B_LOCKED)
414 * or we (reclaim) requested that the buffer be destroyed
415 * we destroy it, otherwise we do a quick get/release to
416 * reset its position in the kernel's LRU list.
418 * Leaving the buffer passively associated allows us to
419 * use the kernel's LRU buffer flushing mechanisms rather
420 * then rolling our own.
422 * XXX there are two ways of doing this. We can re-acquire
423 * and passively release to reset the LRU, or not.
425 if (io->running == 0) {
426 regetblk(bp);
427 if ((bp->b_flags & B_LOCKED) || io->reclaim) {
428 hammer_io_disassociate(iou);
429 /* return the bp */
430 } else {
431 /* return the bp (bp passively associated) */
433 } else {
435 * bp is left passively associated but we do not
436 * try to reacquire it. Interactions with the io
437 * structure will occur on completion of the bp's
438 * I/O.
440 bp = NULL;
443 return(bp);
447 * This routine is called with a locked IO when a flush is desired and
448 * no other references to the structure exists other then ours. This
449 * routine is ONLY called when HAMMER believes it is safe to flush a
450 * potentially modified buffer out.
452 void
453 hammer_io_flush(struct hammer_io *io)
455 struct buf *bp;
458 * Degenerate case - nothing to flush if nothing is dirty.
460 if (io->modified == 0) {
461 return;
464 KKASSERT(io->bp);
465 KKASSERT(io->modify_refs <= 0);
468 * Acquire ownership of the bp, particularly before we clear our
469 * modified flag.
471 * We are going to bawrite() this bp. Don't leave a window where
472 * io->released is set, we actually own the bp rather then our
473 * buffer.
475 bp = io->bp;
476 if (io->released) {
477 regetblk(bp);
478 /* BUF_KERNPROC(io->bp); */
479 /* io->released = 0; */
480 KKASSERT(io->released);
481 KKASSERT(io->bp == bp);
483 io->released = 1;
486 * Acquire exclusive access to the bp and then clear the modified
487 * state of the buffer prior to issuing I/O to interlock any
488 * modifications made while the I/O is in progress. This shouldn't
489 * happen anyway but losing data would be worse. The modified bit
490 * will be rechecked after the IO completes.
492 * NOTE: This call also finalizes the buffer's content (inval == 0).
494 * This is only legal when lock.refs == 1 (otherwise we might clear
495 * the modified bit while there are still users of the cluster
496 * modifying the data).
498 * Do this before potentially blocking so any attempt to modify the
499 * ondisk while we are blocked blocks waiting for us.
501 hammer_ref(&io->lock);
502 hammer_io_clear_modify(io, 0);
503 hammer_unref(&io->lock);
506 * Transfer ownership to the kernel and initiate I/O.
508 io->running = 1;
509 io->hmp->io_running_space += io->bytes;
510 hammer_count_io_running_write += io->bytes;
511 bawrite(bp);
514 /************************************************************************
515 * BUFFER DIRTYING *
516 ************************************************************************
518 * These routines deal with dependancies created when IO buffers get
519 * modified. The caller must call hammer_modify_*() on a referenced
520 * HAMMER structure prior to modifying its on-disk data.
522 * Any intent to modify an IO buffer acquires the related bp and imposes
523 * various write ordering dependancies.
527 * Mark a HAMMER structure as undergoing modification. Meta-data buffers
528 * are locked until the flusher can deal with them, pure data buffers
529 * can be written out.
531 static
532 void
533 hammer_io_modify(hammer_io_t io, int count)
536 * io->modify_refs must be >= 0
538 while (io->modify_refs < 0) {
539 io->waitmod = 1;
540 tsleep(io, 0, "hmrmod", 0);
544 * Shortcut if nothing to do.
546 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
547 io->modify_refs += count;
548 if (io->modified && io->released == 0)
549 return;
551 hammer_lock_ex(&io->lock);
552 if (io->modified == 0) {
553 hammer_io_set_modlist(io);
554 io->modified = 1;
556 if (io->released) {
557 regetblk(io->bp);
558 BUF_KERNPROC(io->bp);
559 io->released = 0;
560 KKASSERT(io->modified != 0);
562 hammer_unlock(&io->lock);
565 static __inline
566 void
567 hammer_io_modify_done(hammer_io_t io)
569 KKASSERT(io->modify_refs > 0);
570 --io->modify_refs;
571 if (io->modify_refs == 0 && io->waitmod) {
572 io->waitmod = 0;
573 wakeup(io);
577 void
578 hammer_io_write_interlock(hammer_io_t io)
580 while (io->modify_refs != 0) {
581 io->waitmod = 1;
582 tsleep(io, 0, "hmrmod", 0);
584 io->modify_refs = -1;
587 void
588 hammer_io_done_interlock(hammer_io_t io)
590 KKASSERT(io->modify_refs == -1);
591 io->modify_refs = 0;
592 if (io->waitmod) {
593 io->waitmod = 0;
594 wakeup(io);
599 * Caller intends to modify a volume's ondisk structure.
601 * This is only allowed if we are the flusher or we have a ref on the
602 * sync_lock.
604 void
605 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
606 void *base, int len)
608 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
610 hammer_io_modify(&volume->io, 1);
611 if (len) {
612 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
613 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
614 hammer_generate_undo(trans, &volume->io,
615 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
616 base, len);
621 * Caller intends to modify a buffer's ondisk structure.
623 * This is only allowed if we are the flusher or we have a ref on the
624 * sync_lock.
626 void
627 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
628 void *base, int len)
630 KKASSERT (trans == NULL || trans->sync_lock_refs > 0);
632 hammer_io_modify(&buffer->io, 1);
633 if (len) {
634 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
635 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
636 hammer_generate_undo(trans, &buffer->io,
637 buffer->zone2_offset + rel_offset,
638 base, len);
642 void
643 hammer_modify_volume_done(hammer_volume_t volume)
645 hammer_io_modify_done(&volume->io);
648 void
649 hammer_modify_buffer_done(hammer_buffer_t buffer)
651 hammer_io_modify_done(&buffer->io);
655 * Mark an entity as not being dirty any more and finalize any
656 * delayed adjustments to the buffer.
658 * Delayed adjustments are an important performance enhancement, allowing
659 * us to avoid recalculating B-Tree node CRCs over and over again when
660 * making bulk-modifications to the B-Tree.
662 * If inval is non-zero delayed adjustments are ignored.
664 * This routine may dereference related btree nodes and cause the
665 * buffer to be dereferenced. The caller must own a reference on io.
667 void
668 hammer_io_clear_modify(struct hammer_io *io, int inval)
670 if (io->modified == 0)
671 return;
674 * Take us off the mod-list and clear the modified bit.
676 KKASSERT(io->mod_list != NULL);
677 if (io->mod_list == &io->hmp->volu_list ||
678 io->mod_list == &io->hmp->meta_list) {
679 io->hmp->locked_dirty_space -= io->bytes;
680 hammer_count_dirtybufspace -= io->bytes;
682 TAILQ_REMOVE(io->mod_list, io, mod_entry);
683 io->mod_list = NULL;
684 io->modified = 0;
687 * If this bit is not set there are no delayed adjustments.
689 if (io->gencrc == 0)
690 return;
691 io->gencrc = 0;
694 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
695 * on the node (& underlying buffer). Release the node after clearing
696 * the flag.
698 if (io->type == HAMMER_STRUCTURE_META_BUFFER) {
699 hammer_buffer_t buffer = (void *)io;
700 hammer_node_t node;
702 restart:
703 TAILQ_FOREACH(node, &buffer->clist, entry) {
704 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0)
705 continue;
706 node->flags &= ~HAMMER_NODE_NEEDSCRC;
707 KKASSERT(node->ondisk);
708 if (inval == 0)
709 node->ondisk->crc = crc32(&node->ondisk->crc + 1, HAMMER_BTREE_CRCSIZE);
710 hammer_rel_node(node);
711 goto restart;
714 /* caller must still have ref on io */
715 KKASSERT(io->lock.refs > 0);
719 * Clear the IO's modify list. Even though the IO is no longer modified
720 * it may still be on the lose_list. This routine is called just before
721 * the governing hammer_buffer is destroyed.
723 void
724 hammer_io_clear_modlist(struct hammer_io *io)
726 KKASSERT(io->modified == 0);
727 if (io->mod_list) {
728 crit_enter(); /* biodone race against list */
729 KKASSERT(io->mod_list == &io->hmp->lose_list);
730 TAILQ_REMOVE(io->mod_list, io, mod_entry);
731 io->mod_list = NULL;
732 crit_exit();
736 static void
737 hammer_io_set_modlist(struct hammer_io *io)
739 struct hammer_mount *hmp = io->hmp;
741 KKASSERT(io->mod_list == NULL);
743 switch(io->type) {
744 case HAMMER_STRUCTURE_VOLUME:
745 io->mod_list = &hmp->volu_list;
746 hmp->locked_dirty_space += io->bytes;
747 hammer_count_dirtybufspace += io->bytes;
748 break;
749 case HAMMER_STRUCTURE_META_BUFFER:
750 io->mod_list = &hmp->meta_list;
751 hmp->locked_dirty_space += io->bytes;
752 hammer_count_dirtybufspace += io->bytes;
753 break;
754 case HAMMER_STRUCTURE_UNDO_BUFFER:
755 io->mod_list = &hmp->undo_list;
756 break;
757 case HAMMER_STRUCTURE_DATA_BUFFER:
758 io->mod_list = &hmp->data_list;
759 break;
761 TAILQ_INSERT_TAIL(io->mod_list, io, mod_entry);
764 /************************************************************************
765 * HAMMER_BIOOPS *
766 ************************************************************************
771 * Pre-IO initiation kernel callback - cluster build only
773 static void
774 hammer_io_start(struct buf *bp)
779 * Post-IO completion kernel callback - MAY BE CALLED FROM INTERRUPT!
781 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
782 * may also be set if we were marking a cluster header open. Only remove
783 * our dependancy if the modified bit is clear.
785 static void
786 hammer_io_complete(struct buf *bp)
788 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
790 KKASSERT(iou->io.released == 1);
793 * Deal with people waiting for I/O to drain
795 if (iou->io.running) {
797 * Deal with critical write errors. Once a critical error
798 * has been flagged in hmp the UNDO FIFO will not be updated.
799 * That way crash recover will give us a consistent
800 * filesystem.
802 * Because of this we can throw away failed UNDO buffers. If
803 * we throw away META or DATA buffers we risk corrupting
804 * the now read-only version of the filesystem visible to
805 * the user. Clear B_ERROR so the buffer is not re-dirtied
806 * by the kernel and ref the io so it doesn't get thrown
807 * away.
809 if (bp->b_flags & B_ERROR) {
810 hammer_critical_error(iou->io.hmp, NULL, bp->b_error,
811 "while flushing meta-data");
812 switch(iou->io.type) {
813 case HAMMER_STRUCTURE_UNDO_BUFFER:
814 break;
815 default:
816 if (iou->io.ioerror == 0) {
817 iou->io.ioerror = 1;
818 if (iou->io.lock.refs == 0)
819 ++hammer_count_refedbufs;
820 hammer_ref(&iou->io.lock);
822 break;
824 bp->b_flags &= ~B_ERROR;
825 bundirty(bp);
826 #if 0
827 hammer_io_set_modlist(&iou->io);
828 iou->io.modified = 1;
829 #endif
831 hammer_stats_disk_write += iou->io.bytes;
832 hammer_count_io_running_write -= iou->io.bytes;
833 iou->io.hmp->io_running_space -= iou->io.bytes;
834 if (iou->io.hmp->io_running_space == 0)
835 wakeup(&iou->io.hmp->io_running_space);
836 KKASSERT(iou->io.hmp->io_running_space >= 0);
837 iou->io.running = 0;
838 } else {
839 hammer_stats_disk_read += iou->io.bytes;
842 if (iou->io.waiting) {
843 iou->io.waiting = 0;
844 wakeup(iou);
848 * If B_LOCKED is set someone wanted to deallocate the bp at some
849 * point, do it now if refs has become zero.
851 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
852 KKASSERT(iou->io.modified == 0);
853 --hammer_count_io_locked;
854 bp->b_flags &= ~B_LOCKED;
855 hammer_io_deallocate(bp);
856 /* structure may be dead now */
861 * Callback from kernel when it wishes to deallocate a passively
862 * associated structure. This mostly occurs with clean buffers
863 * but it may be possible for a holding structure to be marked dirty
864 * while its buffer is passively associated. The caller owns the bp.
866 * If we cannot disassociate we set B_LOCKED to prevent the buffer
867 * from getting reused.
869 * WARNING: Because this can be called directly by getnewbuf we cannot
870 * recurse into the tree. If a bp cannot be immediately disassociated
871 * our only recourse is to set B_LOCKED.
873 * WARNING: This may be called from an interrupt via hammer_io_complete()
875 static void
876 hammer_io_deallocate(struct buf *bp)
878 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
880 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
881 if (iou->io.lock.refs > 0 || iou->io.modified) {
883 * It is not legal to disassociate a modified buffer. This
884 * case really shouldn't ever occur.
886 bp->b_flags |= B_LOCKED;
887 ++hammer_count_io_locked;
888 } else {
890 * Disassociate the BP. If the io has no refs left we
891 * have to add it to the loose list.
893 hammer_io_disassociate(iou);
894 if (iou->io.type != HAMMER_STRUCTURE_VOLUME) {
895 KKASSERT(iou->io.bp == NULL);
896 KKASSERT(iou->io.mod_list == NULL);
897 crit_enter(); /* biodone race against list */
898 iou->io.mod_list = &iou->io.hmp->lose_list;
899 TAILQ_INSERT_TAIL(iou->io.mod_list, &iou->io, mod_entry);
900 crit_exit();
905 static int
906 hammer_io_fsync(struct vnode *vp)
908 return(0);
912 * NOTE: will not be called unless we tell the kernel about the
913 * bioops. Unused... we use the mount's VFS_SYNC instead.
915 static int
916 hammer_io_sync(struct mount *mp)
918 return(0);
921 static void
922 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
927 * I/O pre-check for reading and writing. HAMMER only uses this for
928 * B_CACHE buffers so checkread just shouldn't happen, but if it does
929 * allow it.
931 * Writing is a different case. We don't want the kernel to try to write
932 * out a buffer that HAMMER may be modifying passively or which has a
933 * dependancy. In addition, kernel-demanded writes can only proceed for
934 * certain types of buffers (i.e. UNDO and DATA types). Other dirty
935 * buffer types can only be explicitly written by the flusher.
937 * checkwrite will only be called for bdwrite()n buffers. If we return
938 * success the kernel is guaranteed to initiate the buffer write.
940 static int
941 hammer_io_checkread(struct buf *bp)
943 return(0);
946 static int
947 hammer_io_checkwrite(struct buf *bp)
949 hammer_io_t io = (void *)LIST_FIRST(&bp->b_dep);
952 * This shouldn't happen under normal operation.
954 if (io->type == HAMMER_STRUCTURE_VOLUME ||
955 io->type == HAMMER_STRUCTURE_META_BUFFER) {
956 if (!panicstr)
957 panic("hammer_io_checkwrite: illegal buffer");
958 if ((bp->b_flags & B_LOCKED) == 0) {
959 bp->b_flags |= B_LOCKED;
960 ++hammer_count_io_locked;
962 return(1);
966 * We can only clear the modified bit if the IO is not currently
967 * undergoing modification. Otherwise we may miss changes.
969 * Only data and undo buffers can reach here. These buffers do
970 * not have terminal crc functions but we temporarily reference
971 * the IO anyway, just in case.
973 if (io->modify_refs == 0 && io->modified) {
974 hammer_ref(&io->lock);
975 hammer_io_clear_modify(io, 0);
976 hammer_unref(&io->lock);
977 } else if (io->modified) {
978 KKASSERT(io->type == HAMMER_STRUCTURE_DATA_BUFFER);
982 * The kernel is going to start the IO, set io->running.
984 KKASSERT(io->running == 0);
985 io->running = 1;
986 io->hmp->io_running_space += io->bytes;
987 hammer_count_io_running_write += io->bytes;
988 return(0);
992 * Return non-zero if we wish to delay the kernel's attempt to flush
993 * this buffer to disk.
995 static int
996 hammer_io_countdeps(struct buf *bp, int n)
998 return(0);
1001 struct bio_ops hammer_bioops = {
1002 .io_start = hammer_io_start,
1003 .io_complete = hammer_io_complete,
1004 .io_deallocate = hammer_io_deallocate,
1005 .io_fsync = hammer_io_fsync,
1006 .io_sync = hammer_io_sync,
1007 .io_movedeps = hammer_io_movedeps,
1008 .io_countdeps = hammer_io_countdeps,
1009 .io_checkread = hammer_io_checkread,
1010 .io_checkwrite = hammer_io_checkwrite,
1013 /************************************************************************
1014 * DIRECT IO OPS *
1015 ************************************************************************
1017 * These functions operate directly on the buffer cache buffer associated
1018 * with a front-end vnode rather then a back-end device vnode.
1022 * Read a buffer associated with a front-end vnode directly from the
1023 * disk media. The bio may be issued asynchronously. If leaf is non-NULL
1024 * we validate the CRC.
1026 * We must check for the presence of a HAMMER buffer to handle the case
1027 * where the reblocker has rewritten the data (which it does via the HAMMER
1028 * buffer system, not via the high-level vnode buffer cache), but not yet
1029 * committed the buffer to the media.
1032 hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1033 hammer_btree_leaf_elm_t leaf)
1035 hammer_off_t buf_offset;
1036 hammer_off_t zone2_offset;
1037 hammer_volume_t volume;
1038 struct buf *bp;
1039 struct bio *nbio;
1040 int vol_no;
1041 int error;
1043 buf_offset = bio->bio_offset;
1044 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) ==
1045 HAMMER_ZONE_LARGE_DATA);
1048 * The buffer cache may have an aliased buffer (the reblocker can
1049 * write them). If it does we have to sync any dirty data before
1050 * we can build our direct-read. This is a non-critical code path.
1052 bp = bio->bio_buf;
1053 hammer_sync_buffers(hmp, buf_offset, bp->b_bufsize);
1056 * Resolve to a zone-2 offset. The conversion just requires
1057 * munging the top 4 bits but we want to abstract it anyway
1058 * so the blockmap code can verify the zone assignment.
1060 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1061 if (error)
1062 goto done;
1063 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
1064 HAMMER_ZONE_RAW_BUFFER);
1067 * Resolve volume and raw-offset for 3rd level bio. The
1068 * offset will be specific to the volume.
1070 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1071 volume = hammer_get_volume(hmp, vol_no, &error);
1072 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1073 error = EIO;
1075 if (error == 0) {
1077 * 3rd level bio
1079 nbio = push_bio(bio);
1080 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1081 (zone2_offset & HAMMER_OFF_SHORT_MASK);
1082 #if 0
1084 * XXX disabled - our CRC check doesn't work if the OS
1085 * does bogus_page replacement on the direct-read.
1087 if (leaf && hammer_verify_data) {
1088 nbio->bio_done = hammer_io_direct_read_complete;
1089 nbio->bio_caller_info1.uvalue32 = leaf->data_crc;
1091 #endif
1092 hammer_stats_disk_read += bp->b_bufsize;
1093 vn_strategy(volume->devvp, nbio);
1095 hammer_rel_volume(volume, 0);
1096 done:
1097 if (error) {
1098 kprintf("hammer_direct_read: failed @ %016llx\n",
1099 zone2_offset);
1100 bp->b_error = error;
1101 bp->b_flags |= B_ERROR;
1102 biodone(bio);
1104 return(error);
1107 #if 0
1109 * On completion of the BIO this callback must check the data CRC
1110 * and chain to the previous bio.
1112 static
1113 void
1114 hammer_io_direct_read_complete(struct bio *nbio)
1116 struct bio *obio;
1117 struct buf *bp;
1118 u_int32_t rec_crc = nbio->bio_caller_info1.uvalue32;
1120 bp = nbio->bio_buf;
1121 if (crc32(bp->b_data, bp->b_bufsize) != rec_crc) {
1122 kprintf("HAMMER: data_crc error @%016llx/%d\n",
1123 nbio->bio_offset, bp->b_bufsize);
1124 if (hammer_debug_debug)
1125 Debugger("");
1126 bp->b_flags |= B_ERROR;
1127 bp->b_error = EIO;
1129 obio = pop_bio(nbio);
1130 biodone(obio);
1132 #endif
1135 * Write a buffer associated with a front-end vnode directly to the
1136 * disk media. The bio may be issued asynchronously.
1138 * The BIO is associated with the specified record and RECF_DIRECT_IO
1139 * is set. The recorded is added to its object.
1142 hammer_io_direct_write(hammer_mount_t hmp, hammer_record_t record,
1143 struct bio *bio)
1145 hammer_btree_leaf_elm_t leaf = &record->leaf;
1146 hammer_off_t buf_offset;
1147 hammer_off_t zone2_offset;
1148 hammer_volume_t volume;
1149 hammer_buffer_t buffer;
1150 struct buf *bp;
1151 struct bio *nbio;
1152 char *ptr;
1153 int vol_no;
1154 int error;
1156 buf_offset = leaf->data_offset;
1158 KKASSERT(buf_offset > HAMMER_ZONE_BTREE);
1159 KKASSERT(bio->bio_buf->b_cmd == BUF_CMD_WRITE);
1161 if ((buf_offset & HAMMER_BUFMASK) == 0 &&
1162 leaf->data_len >= HAMMER_BUFSIZE) {
1164 * We are using the vnode's bio to write directly to the
1165 * media, any hammer_buffer at the same zone-X offset will
1166 * now have stale data.
1168 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, &error);
1169 vol_no = HAMMER_VOL_DECODE(zone2_offset);
1170 volume = hammer_get_volume(hmp, vol_no, &error);
1172 if (error == 0 && zone2_offset >= volume->maxbuf_off)
1173 error = EIO;
1174 if (error == 0) {
1175 bp = bio->bio_buf;
1176 KKASSERT((bp->b_bufsize & HAMMER_BUFMASK) == 0);
1178 hammer_del_buffers(hmp, buf_offset,
1179 zone2_offset, bp->b_bufsize);
1183 * Second level bio - cached zone2 offset.
1185 * (We can put our bio_done function in either the
1186 * 2nd or 3rd level).
1188 nbio = push_bio(bio);
1189 nbio->bio_offset = zone2_offset;
1190 nbio->bio_done = hammer_io_direct_write_complete;
1191 nbio->bio_caller_info1.ptr = record;
1192 record->zone2_offset = zone2_offset;
1193 record->flags |= HAMMER_RECF_DIRECT_IO |
1194 HAMMER_RECF_DIRECT_INVAL;
1197 * Third level bio - raw offset specific to the
1198 * correct volume.
1200 zone2_offset &= HAMMER_OFF_SHORT_MASK;
1201 nbio = push_bio(nbio);
1202 nbio->bio_offset = volume->ondisk->vol_buf_beg +
1203 zone2_offset;
1204 hammer_stats_disk_write += bp->b_bufsize;
1205 vn_strategy(volume->devvp, nbio);
1207 hammer_rel_volume(volume, 0);
1208 } else {
1210 * Must fit in a standard HAMMER buffer. In this case all
1211 * consumers use the HAMMER buffer system and RECF_DIRECT_IO
1212 * does not need to be set-up.
1214 KKASSERT(((buf_offset ^ (buf_offset + leaf->data_len - 1)) & ~HAMMER_BUFMASK64) == 0);
1215 buffer = NULL;
1216 ptr = hammer_bread(hmp, buf_offset, &error, &buffer);
1217 if (error == 0) {
1218 bp = bio->bio_buf;
1219 bp->b_flags |= B_AGE;
1220 hammer_io_modify(&buffer->io, 1);
1221 bcopy(bp->b_data, ptr, leaf->data_len);
1222 hammer_io_modify_done(&buffer->io);
1223 hammer_rel_buffer(buffer, 0);
1224 bp->b_resid = 0;
1225 biodone(bio);
1228 if (error == 0) {
1230 * The record is all setup now, add it. Potential conflics
1231 * have already been dealt with.
1233 error = hammer_mem_add(record);
1234 KKASSERT(error == 0);
1235 } else {
1237 * Major suckage occured.
1239 kprintf("hammer_direct_write: failed @ %016llx\n",
1240 leaf->data_offset);
1241 bp = bio->bio_buf;
1242 bp->b_resid = 0;
1243 bp->b_error = EIO;
1244 bp->b_flags |= B_ERROR;
1245 biodone(bio);
1246 record->flags |= HAMMER_RECF_DELETED_FE;
1247 hammer_rel_mem_record(record);
1249 return(error);
1253 * On completion of the BIO this callback must disconnect
1254 * it from the hammer_record and chain to the previous bio.
1256 * An I/O error forces the mount to read-only. Data buffers
1257 * are not B_LOCKED like meta-data buffers are, so we have to
1258 * throw the buffer away to prevent the kernel from retrying.
1260 static
1261 void
1262 hammer_io_direct_write_complete(struct bio *nbio)
1264 struct bio *obio;
1265 struct buf *bp;
1266 hammer_record_t record = nbio->bio_caller_info1.ptr;
1268 bp = nbio->bio_buf;
1269 obio = pop_bio(nbio);
1270 if (bp->b_flags & B_ERROR) {
1271 hammer_critical_error(record->ip->hmp, record->ip,
1272 bp->b_error,
1273 "while writing bulk data");
1274 bp->b_flags |= B_INVAL;
1276 biodone(obio);
1278 KKASSERT(record != NULL);
1279 KKASSERT(record->flags & HAMMER_RECF_DIRECT_IO);
1280 record->flags &= ~HAMMER_RECF_DIRECT_IO;
1281 if (record->flags & HAMMER_RECF_DIRECT_WAIT) {
1282 record->flags &= ~HAMMER_RECF_DIRECT_WAIT;
1283 wakeup(&record->flags);
1289 * This is called before a record is either committed to the B-Tree
1290 * or destroyed, to resolve any associated direct-IO.
1292 * (1) We must wait for any direct-IO related to the record to complete.
1294 * (2) We must remove any buffer cache aliases for data accessed via
1295 * leaf->data_offset or zone2_offset so non-direct-IO consumers
1296 * (the mirroring and reblocking code) do not see stale data.
1298 void
1299 hammer_io_direct_wait(hammer_record_t record)
1302 * Wait for I/O to complete
1304 if (record->flags & HAMMER_RECF_DIRECT_IO) {
1305 crit_enter();
1306 while (record->flags & HAMMER_RECF_DIRECT_IO) {
1307 record->flags |= HAMMER_RECF_DIRECT_WAIT;
1308 tsleep(&record->flags, 0, "hmdiow", 0);
1310 crit_exit();
1314 * Invalidate any related buffer cache aliases.
1316 if (record->flags & HAMMER_RECF_DIRECT_INVAL) {
1317 KKASSERT(record->leaf.data_offset);
1318 hammer_del_buffers(record->ip->hmp,
1319 record->leaf.data_offset,
1320 record->zone2_offset,
1321 record->leaf.data_len);
1322 record->flags &= ~HAMMER_RECF_DIRECT_INVAL;
1327 * This is called to remove the second-level cached zone-2 offset from
1328 * frontend buffer cache buffers, now stale due to a data relocation.
1329 * These offsets are generated by cluster_read() via VOP_BMAP, or directly
1330 * by hammer_vop_strategy_read().
1332 * This is rather nasty because here we have something like the reblocker
1333 * scanning the raw B-Tree with no held references on anything, really,
1334 * other then a shared lock on the B-Tree node, and we have to access the
1335 * frontend's buffer cache to check for and clean out the association.
1336 * Specifically, if the reblocker is moving data on the disk, these cached
1337 * offsets will become invalid.
1339 * Only data record types associated with the large-data zone are subject
1340 * to direct-io and need to be checked.
1343 void
1344 hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf)
1346 struct hammer_inode_info iinfo;
1347 int zone;
1349 if (leaf->base.rec_type != HAMMER_RECTYPE_DATA)
1350 return;
1351 zone = HAMMER_ZONE_DECODE(leaf->data_offset);
1352 if (zone != HAMMER_ZONE_LARGE_DATA_INDEX)
1353 return;
1354 iinfo.obj_id = leaf->base.obj_id;
1355 iinfo.obj_asof = 0; /* unused */
1356 iinfo.obj_localization = leaf->base.localization &
1357 HAMMER_LOCALIZE_PSEUDOFS_MASK;
1358 iinfo.u.leaf = leaf;
1359 hammer_scan_inode_snapshots(hmp, &iinfo,
1360 hammer_io_direct_uncache_callback,
1361 leaf);
1364 static int
1365 hammer_io_direct_uncache_callback(hammer_inode_t ip, void *data)
1367 hammer_inode_info_t iinfo = data;
1368 hammer_off_t data_offset;
1369 hammer_off_t file_offset;
1370 struct vnode *vp;
1371 struct buf *bp;
1372 int blksize;
1374 if (ip->vp == NULL)
1375 return(0);
1376 data_offset = iinfo->u.leaf->data_offset;
1377 file_offset = iinfo->u.leaf->base.key - iinfo->u.leaf->data_len;
1378 blksize = iinfo->u.leaf->data_len;
1379 KKASSERT((blksize & HAMMER_BUFMASK) == 0);
1381 hammer_ref(&ip->lock);
1382 if (hammer_get_vnode(ip, &vp) == 0) {
1383 if ((bp = findblk(ip->vp, file_offset)) != NULL &&
1384 bp->b_bio2.bio_offset != NOOFFSET) {
1385 bp = getblk(ip->vp, file_offset, blksize, 0, 0);
1386 bp->b_bio2.bio_offset = NOOFFSET;
1387 brelse(bp);
1389 vput(vp);
1391 hammer_rel_inode(ip, 0);
1392 return(0);