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.25 2008/04/24 21:20:33 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 release 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_deallocate(struct buf *bp);
56 static int hammer_io_checkwrite(struct buf *bp);
59 * Initialize an already-zero'd hammer_io structure
62 hammer_io_init(hammer_io_t io, enum hammer_io_type type)
68 * Helper routine to disassociate a buffer cache buffer from an I/O
69 * structure. Called with the io structure exclusively locked.
71 * The io may have 0 or 1 references depending on who called us. The
72 * caller is responsible for dealing with the refs.
74 * This call can only be made when no action is required on the buffer.
75 * HAMMER must own the buffer (released == 0) since we mess around with it.
78 hammer_io_disassociate(hammer_io_structure_t iou, int elseit)
80 struct buf *bp = iou->io.bp;
82 KKASSERT(iou->io.modified == 0);
85 bp->b_flags &= ~B_LOCKED;
87 KKASSERT(iou->io.released == 0);
91 KKASSERT(iou->io.released);
94 switch(iou->io.type) {
95 case HAMMER_STRUCTURE_VOLUME:
96 iou->volume.ondisk = NULL;
98 case HAMMER_STRUCTURE_BUFFER:
99 iou->buffer.ondisk = NULL;
105 * Wait for any physical IO to complete
108 hammer_io_wait(hammer_io_t io)
112 tsleep_interlock(io);
115 tsleep(io, 0, "hmrflw", 0);
116 if (io->running == 0)
118 tsleep_interlock(io);
120 if (io->running == 0)
128 * Load bp for a HAMMER structure. The io is exclusively locked by the
132 hammer_io_read(struct vnode *devvp, struct hammer_io *io)
137 if ((bp = io->bp) == NULL) {
138 error = bread(devvp, io->offset, HAMMER_BUFSIZE, &io->bp);
141 bp->b_ops = &hammer_bioops;
142 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
145 io->modified = 0; /* no new modifications yet */
146 io->released = 0; /* we hold an active lock on bp */
156 * Similar to hammer_io_read() but returns a zero'd out buffer instead.
157 * vfs_bio_clrbuf() is kinda nasty, enforce serialization against background
158 * I/O so we can call it.
160 * The caller is responsible for calling hammer_modify_*() on the appropriate
164 hammer_io_new(struct vnode *devvp, struct hammer_io *io)
168 if ((bp = io->bp) == NULL) {
169 io->bp = getblk(devvp, io->offset, HAMMER_BUFSIZE, 0, 0);
171 bp->b_ops = &hammer_bioops;
172 LIST_INSERT_HEAD(&bp->b_dep, &io->worklist, node);
190 * This routine is called on the last reference to a hammer structure.
191 * The io is usually locked exclusively (but may not be during unmount).
193 * If flush is 1, or B_LOCKED was set indicating that the kernel
194 * wanted to recycle the buffer, and there are no dependancies, this
195 * function will issue an asynchronous write.
197 * If flush is 2 this function waits until all I/O has completed and
198 * disassociates the bp from the IO before returning, unless there
199 * are still other references.
202 hammer_io_release(struct hammer_io *io)
206 if ((bp = io->bp) == NULL)
211 * If flush is 2 wait for dependancies
213 while (io->waitdep && TAILQ_FIRST(&io->deplist)) {
214 hammer_io_wait(TAILQ_FIRST(&io->deplist));
219 * Try to flush a dirty IO to disk if asked to by the caller
220 * or if the kernel tried to flush the buffer in the past.
222 * The flush will fail if any dependancies are present.
224 if (io->modified && (io->flush || (bp->b_flags & B_LOCKED)))
228 * If flush is 2 we wait for the IO to complete.
230 if (io->waitdep && io->running) {
235 * Actively or passively release the buffer. Modified IOs with
236 * dependancies cannot be released.
238 if (io->flush && io->modified == 0 && io->running == 0) {
244 hammer_io_disassociate((hammer_io_structure_t)io, 1);
245 } else if (io->modified) {
246 if (io->released == 0) {
250 } else if (io->released == 0) {
257 * This routine is called with a locked IO when a flush is desired and
258 * no other references to the structure exists other then ours. This
259 * routine is ONLY called when HAMMER believes it is safe to flush a
260 * potentially modified buffer out.
263 hammer_io_flush(struct hammer_io *io)
268 * Can't flush if the IO isn't modified or if it has dependancies.
270 if (io->modified == 0) {
278 * XXX - umount syncs buffers without referencing them, check for 0
281 KKASSERT(io->lock.refs == 0 || io->lock.refs == 1);
284 * Reset modified to 0 here and re-check it after the IO completes.
285 * This is only legal when lock.refs == 1 (otherwise we might clear
286 * the modified bit while there are still users of the cluster
287 * modifying the data).
289 * NOTE: We have no dependancies so we don't have to worry about
290 * cluster-open's here.
292 * Do this before potentially blocking so any attempt to modify the
293 * ondisk while we are blocked blocks waiting for us.
295 io->modified = 0; /* force interlock */
301 /* BUF_KERNPROC(io->bp); */
309 /************************************************************************
311 ************************************************************************
313 * These routines deal with dependancies created when IO buffers get
314 * modified. The caller must call hammer_modify_*() on a referenced
315 * HAMMER structure prior to modifying its on-disk data.
317 * Any intent to modify an IO buffer acquires the related bp and imposes
318 * various write ordering dependancies.
322 * Mark a HAMMER structure as undergoing modification. Return 1 when applying
323 * a non-NULL ordering dependancy for the first time, 0 otherwise.
327 hammer_io_modify(hammer_io_t io)
330 * Shortcut if nothing to do.
332 KKASSERT(io->lock.refs != 0 && io->bp != NULL);
333 if (io->modified && io->released == 0)
336 hammer_lock_ex(&io->lock);
340 BUF_KERNPROC(io->bp);
342 KKASSERT(io->modified != 0);
344 hammer_unlock(&io->lock);
348 hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
351 hammer_io_modify(&volume->io);
354 intptr_t rel_offset = (intptr_t)base - (intptr_t)volume->ondisk;
355 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
356 hammer_generate_undo(trans, &volume->io,
357 HAMMER_ENCODE_RAW_VOLUME(volume->vol_no, rel_offset),
363 * Caller intends to modify a buffer's ondisk structure. The related
364 * cluster must be marked open prior to being able to flush the modified
365 * buffer so get that I/O going now.
368 hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
371 hammer_io_modify(&buffer->io);
373 intptr_t rel_offset = (intptr_t)base - (intptr_t)buffer->ondisk;
374 KKASSERT((rel_offset & ~(intptr_t)HAMMER_BUFMASK) == 0);
375 hammer_generate_undo(trans, &buffer->io,
376 buffer->zone2_offset + rel_offset,
382 * Mark an entity as not being dirty any more -- this usually occurs when
383 * the governing a-list has freed the entire entity.
388 hammer_io_clear_modify(struct hammer_io *io)
394 if ((bp = io->bp) != NULL) {
397 /* BUF_KERNPROC(io->bp); */
401 if (io->modified == 0) {
402 kprintf("hammer_io_clear_modify: cleared %p\n", io);
412 /************************************************************************
414 ************************************************************************
419 * Pre-IO initiation kernel callback - cluster build only
422 hammer_io_start(struct buf *bp)
427 * Post-IO completion kernel callback
429 * NOTE: HAMMER may modify a buffer after initiating I/O. The modified bit
430 * may also be set if we were marking a cluster header open. Only remove
431 * our dependancy if the modified bit is clear.
434 hammer_io_complete(struct buf *bp)
436 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
438 KKASSERT(iou->io.released == 1);
443 * If no lock references remain and we can acquire the IO lock and
444 * someone at some point wanted us to flush (B_LOCKED test), then
445 * try to dispose of the IO.
448 if (iou->io.waiting) {
454 * Someone wanted us to flush, try to clean out the buffer.
456 if ((bp->b_flags & B_LOCKED) && iou->io.lock.refs == 0) {
457 KKASSERT(iou->io.modified == 0);
458 bp->b_flags &= ~B_LOCKED;
459 hammer_io_deallocate(bp);
460 /* structure may be dead now */
465 * Callback from kernel when it wishes to deallocate a passively
466 * associated structure. This case can only occur with read-only
469 * If we cannot disassociate we set B_LOCKED to prevent the buffer
470 * from getting reused.
472 * WARNING: Because this can be called directly by getnewbuf we cannot
473 * recurse into the tree. If a bp cannot be immediately disassociated
474 * our only recourse is to set B_LOCKED.
476 * WARNING: If the HAMMER structure is passively cached we have to
480 hammer_io_deallocate(struct buf *bp)
482 hammer_io_structure_t iou = (void *)LIST_FIRST(&bp->b_dep);
484 KKASSERT((bp->b_flags & B_LOCKED) == 0 && iou->io.running == 0);
485 if (iou->io.lock.refs > 0 || iou->io.modified) {
486 bp->b_flags |= B_LOCKED;
488 /* XXX interlock against ref or another disassociate */
489 /* XXX this can leave HAMMER structures lying around */
490 hammer_io_disassociate(iou, 0);
492 switch(iou->io.type) {
493 case HAMMER_STRUCTURE_VOLUME:
494 hammer_rel_volume(&iou->volume, 1);
496 case HAMMER_STRUCTURE_BUFFER:
497 hammer_rel_buffer(&iou->buffer, 1);
505 hammer_io_fsync(struct vnode *vp)
511 * NOTE: will not be called unless we tell the kernel about the
512 * bioops. Unused... we use the mount's VFS_SYNC instead.
515 hammer_io_sync(struct mount *mp)
521 hammer_io_movedeps(struct buf *bp1, struct buf *bp2)
526 * I/O pre-check for reading and writing. HAMMER only uses this for
527 * B_CACHE buffers so checkread just shouldn't happen, but if it does
530 * Writing is a different case. We don't want the kernel to try to write
531 * out a buffer that HAMMER may be modifying passively or which has a
534 * This code enforces the following write ordering: buffers, then cluster
535 * headers, then volume headers.
538 hammer_io_checkread(struct buf *bp)
544 hammer_io_checkwrite(struct buf *bp)
546 union hammer_io_structure *iou = (void *)LIST_FIRST(&bp->b_dep);
549 * We are called from the kernel on delayed-write buffers, and
550 * called from hammer_io_flush() on flush requests. There should
551 * be no dependancies in either case.
553 * In the case of delayed-writes, the introduction of a dependancy
554 * will block until the bp can be reacquired, and the bp is then
555 * simply not released until the dependancy can be satisfied.
557 * We can only clear the modified bit when entered from the kernel
558 * if io.lock.refs == 0.
560 if (iou->io.lock.refs == 0) {
561 iou->io.modified = 0;
567 * Return non-zero if the caller should flush the structure associated
568 * with this io sub-structure.
571 hammer_io_checkflush(struct hammer_io *io)
573 if (io->bp == NULL || (io->bp->b_flags & B_LOCKED)) {
580 * Return non-zero if we wish to delay the kernel's attempt to flush
581 * this buffer to disk.
584 hammer_io_countdeps(struct buf *bp, int n)
589 struct bio_ops hammer_bioops = {
590 .io_start = hammer_io_start,
591 .io_complete = hammer_io_complete,
592 .io_deallocate = hammer_io_deallocate,
593 .io_fsync = hammer_io_fsync,
594 .io_sync = hammer_io_sync,
595 .io_movedeps = hammer_io_movedeps,
596 .io_countdeps = hammer_io_countdeps,
597 .io_checkread = hammer_io_checkread,
598 .io_checkwrite = hammer_io_checkwrite,