2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
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
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
50 #include <sys/fcntl.h>
51 #include <sys/nlookup.h>
55 static void hammer_io_modify(hammer_io_t io
, int count
);
56 static void hammer_io_deallocate(struct buf
*bp
);
58 static void hammer_io_direct_read_complete(struct bio
*nbio
);
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.
72 hammer_io_init(hammer_io_t io
, hammer_volume_t volume
, enum hammer_io_type type
)
75 io
->hmp
= volume
->io
.hmp
;
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).
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
);
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
;
114 switch(iou
->io
.type
) {
115 case HAMMER_STRUCTURE_VOLUME
:
116 iou
->volume
.ondisk
= NULL
;
118 case HAMMER_STRUCTURE_DATA_BUFFER
:
119 case HAMMER_STRUCTURE_META_BUFFER
:
120 case HAMMER_STRUCTURE_UNDO_BUFFER
:
121 iou
->buffer
.ondisk
= NULL
;
127 * Wait for any physical IO to complete
130 hammer_io_wait(hammer_io_t io
)
134 tsleep_interlock(io
);
137 tsleep(io
, 0, "hmrflw", 0);
138 if (io
->running
== 0)
140 tsleep_interlock(io
);
142 if (io
->running
== 0)
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).
155 hammer_io_wait_all(hammer_mount_t hmp
, const char *ident
)
157 hammer_io_flush_sync(hmp
);
159 while (hmp
->io_running_space
)
160 tsleep(&hmp
->io_running_space
, 0, ident
, 0);
164 #define HAMMER_MAXRA 4
167 * Load bp for a HAMMER structure. The io must be exclusively locked by
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
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
179 hammer_io_read(struct vnode
*devvp
, struct hammer_io
*io
, hammer_off_t limit
)
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
,
190 HAMMER_CLUSTER_BUFS
, &io
->bp
);
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.
202 bp
->b_ops
= &hammer_bioops
;
203 KKASSERT(LIST_FIRST(&bp
->b_dep
) == NULL
);
204 LIST_INSERT_HEAD(&bp
->b_dep
, &io
->worklist
, node
);
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 */
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
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
)
232 if ((bp
= io
->bp
) == NULL
) {
233 io
->bp
= getblk(devvp
, io
->offset
, io
->bytes
, 0, 0);
235 bp
->b_ops
= &hammer_bioops
;
236 KKASSERT(LIST_FIRST(&bp
->b_dep
) == NULL
);
237 LIST_INSERT_HEAD(&bp
->b_dep
, &io
->worklist
, node
);
239 KKASSERT(io
->running
== 0);
249 hammer_io_modify(io
, 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.
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
;
269 phys_offset
= volume
->ondisk
->vol_buf_beg
+
270 (zone2_offset
& HAMMER_OFF_SHORT_MASK
);
272 if ((bp
= findblk(volume
->devvp
, phys_offset
)) != NULL
)
273 bp
= getblk(volume
->devvp
, phys_offset
, bp
->b_bufsize
, 0, 0);
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);
282 KKASSERT(iou
->io
.lock
.refs
== 0);
283 hammer_rel_buffer(&iou
->buffer
, 0);
284 /*hammer_io_deallocate(bp);*/
286 KKASSERT((bp
->b_flags
& B_LOCKED
) == 0);
288 bp
->b_flags
|= B_NOCACHE
|B_RELBUF
;
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.
312 hammer_io_release(struct hammer_io
*io
, int flush
)
314 union hammer_io_structure
*iou
= (void *)io
;
317 if ((bp
= io
->bp
) == 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
331 } else if (bp
->b_flags
& B_LOCKED
) {
333 case HAMMER_STRUCTURE_DATA_BUFFER
:
334 case HAMMER_STRUCTURE_UNDO_BUFFER
:
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
) {
353 * Return control of the buffer to the kernel (with the provisio
354 * that our bioops can override kernel decisions with regards to
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).
369 hammer_io_disassociate((hammer_io_structure_t
)io
);
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
380 case HAMMER_STRUCTURE_DATA_BUFFER
:
381 case HAMMER_STRUCTURE_UNDO_BUFFER
:
382 if (io
->released
== 0) {
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.
401 if (bp
->b_flags
& B_LOCKED
) {
402 hammer_io_disassociate(iou
);
406 hammer_io_disassociate(iou
);
409 /* return the bp (bp passively associated) */
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) {
430 if ((bp
->b_flags
& B_LOCKED
) || io
->reclaim
) {
431 hammer_io_disassociate(iou
);
434 /* return the bp (bp passively associated) */
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
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.
456 hammer_io_flush(struct hammer_io
*io
)
461 * Degenerate case - nothing to flush if nothing is dirty.
463 if (io
->modified
== 0) {
468 KKASSERT(io
->modify_refs
<= 0);
471 * Acquire ownership of the bp, particularly before we clear our
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
481 /* BUF_KERNPROC(io->bp); */
482 /* io->released = 0; */
483 KKASSERT(io
->released
);
484 KKASSERT(io
->bp
== bp
);
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.
512 io
->hmp
->io_running_space
+= io
->bytes
;
513 hammer_count_io_running_write
+= io
->bytes
;
515 hammer_io_flush_mark(io
->volume
);
518 /************************************************************************
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.
537 hammer_io_modify(hammer_io_t io
, int count
)
540 * io->modify_refs must be >= 0
542 while (io
->modify_refs
< 0) {
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)
555 hammer_lock_ex(&io
->lock
);
556 if (io
->modified
== 0) {
557 hammer_io_set_modlist(io
);
562 BUF_KERNPROC(io
->bp
);
564 KKASSERT(io
->modified
!= 0);
566 hammer_unlock(&io
->lock
);
571 hammer_io_modify_done(hammer_io_t io
)
573 KKASSERT(io
->modify_refs
> 0);
575 if (io
->modify_refs
== 0 && io
->waitmod
) {
582 hammer_io_write_interlock(hammer_io_t io
)
584 while (io
->modify_refs
!= 0) {
586 tsleep(io
, 0, "hmrmod", 0);
588 io
->modify_refs
= -1;
592 hammer_io_done_interlock(hammer_io_t io
)
594 KKASSERT(io
->modify_refs
== -1);
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
609 hammer_modify_volume(hammer_transaction_t trans
, hammer_volume_t volume
,
612 KKASSERT (trans
== NULL
|| trans
->sync_lock_refs
> 0);
614 hammer_io_modify(&volume
->io
, 1);
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
),
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
631 hammer_modify_buffer(hammer_transaction_t trans
, hammer_buffer_t buffer
,
634 KKASSERT (trans
== NULL
|| trans
->sync_lock_refs
> 0);
636 hammer_io_modify(&buffer
->io
, 1);
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
,
647 hammer_modify_volume_done(hammer_volume_t volume
)
649 hammer_io_modify_done(&volume
->io
);
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.
672 hammer_io_clear_modify(struct hammer_io
*io
, int inval
)
674 if (io
->modified
== 0)
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
);
691 * If this bit is not set there are no delayed adjustments.
698 * Finalize requested CRCs. The NEEDSCRC flag also holds a reference
699 * on the node (& underlying buffer). Release the node after clearing
702 if (io
->type
== HAMMER_STRUCTURE_META_BUFFER
) {
703 hammer_buffer_t buffer
= (void *)io
;
707 TAILQ_FOREACH(node
, &buffer
->clist
, entry
) {
708 if ((node
->flags
& HAMMER_NODE_NEEDSCRC
) == 0)
710 node
->flags
&= ~HAMMER_NODE_NEEDSCRC
;
711 KKASSERT(node
->ondisk
);
713 node
->ondisk
->crc
= crc32(&node
->ondisk
->crc
+ 1, HAMMER_BTREE_CRCSIZE
);
714 hammer_rel_node(node
);
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.
728 hammer_io_clear_modlist(struct hammer_io
*io
)
730 KKASSERT(io
->modified
== 0);
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
);
741 hammer_io_set_modlist(struct hammer_io
*io
)
743 struct hammer_mount
*hmp
= io
->hmp
;
745 KKASSERT(io
->mod_list
== NULL
);
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
;
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
;
758 case HAMMER_STRUCTURE_UNDO_BUFFER
:
759 io
->mod_list
= &hmp
->undo_list
;
761 case HAMMER_STRUCTURE_DATA_BUFFER
:
762 io
->mod_list
= &hmp
->data_list
;
765 TAILQ_INSERT_TAIL(io
->mod_list
, io
, mod_entry
);
768 /************************************************************************
770 ************************************************************************
775 * Pre-IO initiation kernel callback - cluster build only
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.
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
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
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
:
820 if (iou
->io
.ioerror
== 0) {
822 if (iou
->io
.lock
.refs
== 0)
823 ++hammer_count_refedbufs
;
824 hammer_ref(&iou
->io
.lock
);
828 bp
->b_flags
&= ~B_ERROR
;
831 hammer_io_set_modlist(&iou
->io
);
832 iou
->io
.modified
= 1;
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);
843 hammer_stats_disk_read
+= iou
->io
.bytes
;
846 if (iou
->io
.waiting
) {
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()
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
;
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
);
910 hammer_io_fsync(struct vnode
*vp
)
916 * NOTE: will not be called unless we tell the kernel about the
917 * bioops. Unused... we use the mount's VFS_SYNC instead.
920 hammer_io_sync(struct mount
*mp
)
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
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.
945 hammer_io_checkread(struct buf
*bp
)
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
) {
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
;
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);
990 io
->hmp
->io_running_space
+= io
->bytes
;
991 hammer_count_io_running_write
+= io
->bytes
;
996 * Return non-zero if we wish to delay the kernel's attempt to flush
997 * this buffer to disk.
1000 hammer_io_countdeps(struct buf
*bp
, int n
)
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 /************************************************************************
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
;
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.
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
);
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
)
1083 nbio
= push_bio(bio
);
1084 nbio
->bio_offset
= volume
->ondisk
->vol_buf_beg
+
1085 (zone2_offset
& HAMMER_OFF_SHORT_MASK
);
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
;
1096 hammer_stats_disk_read
+= bp
->b_bufsize
;
1097 vn_strategy(volume
->devvp
, nbio
);
1099 hammer_rel_volume(volume
, 0);
1102 kprintf("hammer_direct_read: failed @ %016llx\n",
1104 bp
->b_error
= error
;
1105 bp
->b_flags
|= B_ERROR
;
1113 * On completion of the BIO this callback must check the data CRC
1114 * and chain to the previous bio.
1118 hammer_io_direct_read_complete(struct bio
*nbio
)
1122 u_int32_t rec_crc
= nbio
->bio_caller_info1
.uvalue32
;
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
)
1130 bp
->b_flags
|= B_ERROR
;
1133 obio
= pop_bio(nbio
);
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
,
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
;
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
)
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
1204 zone2_offset
&= HAMMER_OFF_SHORT_MASK
;
1205 nbio
= push_bio(nbio
);
1206 nbio
->bio_offset
= volume
->ondisk
->vol_buf_beg
+
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);
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);
1221 ptr
= hammer_bread(hmp
, buf_offset
, &error
, &buffer
);
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);
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);
1242 * Major suckage occured.
1244 kprintf("hammer_direct_write: failed @ %016llx\n",
1249 bp
->b_flags
|= B_ERROR
;
1251 record
->flags
|= HAMMER_RECF_DELETED_FE
;
1252 hammer_rel_mem_record(record
);
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.
1267 hammer_io_direct_write_complete(struct bio
*nbio
)
1271 hammer_record_t record
= nbio
->bio_caller_info1
.ptr
;
1274 obio
= pop_bio(nbio
);
1275 if (bp
->b_flags
& B_ERROR
) {
1276 hammer_critical_error(record
->ip
->hmp
, record
->ip
,
1278 "while writing bulk data");
1279 bp
->b_flags
|= B_INVAL
;
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.
1304 hammer_io_direct_wait(hammer_record_t record
)
1307 * Wait for I/O to complete
1309 if (record
->flags
& HAMMER_RECF_DIRECT_IO
) {
1311 while (record
->flags
& HAMMER_RECF_DIRECT_IO
) {
1312 record
->flags
|= HAMMER_RECF_DIRECT_WAIT
;
1313 tsleep(&record
->flags
, 0, "hmdiow", 0);
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.
1349 hammer_io_direct_uncache(hammer_mount_t hmp
, hammer_btree_leaf_elm_t leaf
)
1351 struct hammer_inode_info iinfo
;
1354 if (leaf
->base
.rec_type
!= HAMMER_RECTYPE_DATA
)
1356 zone
= HAMMER_ZONE_DECODE(leaf
->data_offset
);
1357 if (zone
!= HAMMER_ZONE_LARGE_DATA_INDEX
)
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
,
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
;
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
;
1396 hammer_rel_inode(ip
, 0);
1402 * This function is called when writes may have occured on the volume,
1403 * indicating that the device may be holding cached writes.
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.
1415 hammer_io_flush_sync(hammer_mount_t hmp
)
1417 hammer_volume_t volume
;
1418 struct buf
*bp_base
= NULL
;
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
;
1425 bp
->b_bio1
.bio_offset
= 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
;
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
) {
1440 tsleep_interlock(&bp
->b_cmd
);
1441 if (bp
->b_cmd
!= BUF_CMD_DONE
)
1442 tsleep(&bp
->b_cmd
, 0, "hmrFLS", 0);
1445 bp
->b_flags
&= ~B_ASYNC
;
1451 * Callback to deal with completed flush commands to the device.
1454 hammer_io_flush_sync_done(struct bio
*bio
)
1459 bp
->b_cmd
= BUF_CMD_DONE
;