2 * Copyright (c) 2013 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
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
38 * Implements an abstraction layer for synchronous and asynchronous
39 * buffered device I/O. Can be used for OS-abstraction but the main
40 * purpose is to allow larger buffers to be used against hammer2_chain's
41 * using smaller allocations, without causing deadlocks.
44 static void hammer2_io_callback(struct bio *bio);
45 static int hammer2_io_cleanup_callback(hammer2_io_t *dio, void *arg);
48 hammer2_io_cmp(hammer2_io_t *io1, hammer2_io_t *io2)
50 if (io2->pbase < io1->pbase)
52 if (io2->pbase > io1->pbase)
57 RB_PROTOTYPE2(hammer2_io_tree, hammer2_io, rbnode, hammer2_io_cmp, off_t);
58 RB_GENERATE2(hammer2_io_tree, hammer2_io, rbnode, hammer2_io_cmp,
61 #define HAMMER2_DIO_INPROG 0x80000000
62 #define HAMMER2_DIO_GOOD 0x40000000
63 #define HAMMER2_DIO_WAITING 0x20000000
64 #define HAMMER2_DIO_DIRTY 0x10000000
66 #define HAMMER2_DIO_MASK 0x0FFFFFFF
69 * Acquire the requested dio, set *ownerp based on state. If state is good
70 * *ownerp is set to 0, otherwise *ownerp is set to DIO_INPROG and the
71 * caller must resolve the buffer.
74 hammer2_io_getblk(hammer2_mount_t *hmp, off_t lbase, int lsize, int *ownerp)
80 int psize = hammer2_devblksize(lsize);
83 pmask = ~(hammer2_off_t)(psize - 1);
85 KKASSERT((1 << (int)(lbase & HAMMER2_OFF_MASK_RADIX)) == lsize);
86 lbase &= ~HAMMER2_OFF_MASK_RADIX;
87 pbase = lbase & pmask;
88 KKASSERT(pbase != 0 && ((lbase + lsize - 1) & pmask) == pbase);
91 * Access/Allocate the DIO
93 spin_lock_shared(&hmp->io_spin);
94 dio = RB_LOOKUP(hammer2_io_tree, &hmp->iotree, pbase);
96 if (atomic_fetchadd_int(&dio->refs, 1) == 0)
97 atomic_add_int(&dio->hmp->iofree_count, -1);
98 spin_unlock_shared(&hmp->io_spin);
100 spin_unlock_shared(&hmp->io_spin);
101 dio = kmalloc(sizeof(*dio), M_HAMMER2, M_INTWAIT | M_ZERO);
106 spin_lock(&hmp->io_spin);
107 xio = RB_INSERT(hammer2_io_tree, &hmp->iotree, dio);
109 spin_unlock(&hmp->io_spin);
111 if (atomic_fetchadd_int(&xio->refs, 1) == 0)
112 atomic_add_int(&xio->hmp->iofree_count, -1);
113 spin_unlock(&hmp->io_spin);
114 kfree(dio, M_HAMMER2);
120 * Obtain/Validate the buffer.
127 * Stop if the buffer is good. Once set GOOD the flag cannot
128 * be cleared until refs drops to 0.
130 if (refs & HAMMER2_DIO_GOOD) {
136 * We need to acquire the in-progress lock on the buffer
138 if (refs & HAMMER2_DIO_INPROG) {
139 tsleep_interlock(dio, 0);
140 if (atomic_cmpset_int(&dio->refs, refs,
141 refs | HAMMER2_DIO_WAITING)) {
142 tsleep(dio, PINTERLOCKED, "h2dio", 0);
146 if (atomic_cmpset_int(&dio->refs, refs,
147 refs | HAMMER2_DIO_INPROG)) {
155 * We need to do more work before the buffer is usable
157 *ownerp = HAMMER2_DIO_INPROG;
163 * If part of an asynchronous I/O the asynchronous I/O is biodone()'d.
165 * If the caller owned INPROG then the dio will be set GOOD or not
166 * depending on whether the caller disposed of dio->bp or not.
170 hammer2_io_complete(hammer2_io_t *dio, int owner)
175 while (owner & HAMMER2_DIO_INPROG) {
178 good = dio->bp ? HAMMER2_DIO_GOOD : 0;
179 if (atomic_cmpset_int(&dio->refs, refs,
180 (refs & ~(HAMMER2_DIO_WAITING |
181 HAMMER2_DIO_INPROG)) |
183 if (refs & HAMMER2_DIO_WAITING)
186 BUF_KERNPROC(dio->bp);
194 * Release our ref on *diop, dispose of the underlying buffer.
197 hammer2_io_putblk(hammer2_io_t **diop)
199 hammer2_mount_t *hmp;
213 if ((refs & HAMMER2_DIO_MASK) == 1) {
214 KKASSERT((refs & HAMMER2_DIO_INPROG) == 0);
215 if (atomic_cmpset_int(&dio->refs, refs,
218 HAMMER2_DIO_DIRTY)) |
219 HAMMER2_DIO_INPROG)) {
224 if (atomic_cmpset_int(&dio->refs, refs, refs - 1))
232 * Locked INPROG on 1->0 transition and we cleared DIO_GOOD (which is
233 * legal only on the last ref). This allows us to dispose of the
234 * buffer. refs is now 0.
236 * The instant we call io_complete dio is a free agent again and
237 * can be ripped out from under us. Acquisition of the dio after
238 * this point will require a shared or exclusive spinlock.
245 hammer2_io_complete(dio, HAMMER2_DIO_INPROG); /* clears INPROG */
246 dio = NULL; /* dio stale */
248 atomic_add_int(&hmp->iofree_count, 1);
250 if (refs & HAMMER2_DIO_GOOD) {
251 KKASSERT(bp != NULL);
252 if (refs & HAMMER2_DIO_DIRTY) {
253 if (hammer2_cluster_enable) {
254 peof = (pbase + HAMMER2_SEGMASK64) &
256 cluster_write(bp, peof, psize, 4);
258 bp->b_flags |= B_CLUSTEROK;
261 } else if (bp->b_flags & (B_ERROR | B_INVAL | B_RELBUF)) {
269 * We cache free buffers so re-use cases can use a shared lock, but
270 * if too many build up we have to clean them out.
272 if (hmp->iofree_count > 1000) {
273 struct hammer2_io_tree tmptree;
276 spin_lock(&hmp->io_spin);
277 if (hmp->iofree_count > 1000) {
278 RB_SCAN(hammer2_io_tree, &hmp->iotree, NULL,
279 hammer2_io_cleanup_callback, &tmptree);
281 spin_unlock(&hmp->io_spin);
282 hammer2_io_cleanup(hmp, &tmptree);
288 hammer2_io_cleanup_callback(hammer2_io_t *dio, void *arg)
290 struct hammer2_io_tree *tmptree = arg;
293 if ((dio->refs & (HAMMER2_DIO_MASK | HAMMER2_DIO_INPROG)) == 0) {
294 RB_REMOVE(hammer2_io_tree, &dio->hmp->iotree, dio);
295 xio = RB_INSERT(hammer2_io_tree, tmptree, dio);
296 KKASSERT(xio == NULL);
302 hammer2_io_cleanup(hammer2_mount_t *hmp, struct hammer2_io_tree *tree)
306 while ((dio = RB_ROOT(tree)) != NULL) {
307 RB_REMOVE(hammer2_io_tree, tree, dio);
308 KKASSERT(dio->bp == NULL &&
309 (dio->refs & (HAMMER2_DIO_MASK | HAMMER2_DIO_INPROG)) == 0);
310 kfree(dio, M_HAMMER2);
311 atomic_add_int(&hmp->iofree_count, -1);
316 hammer2_io_data(hammer2_io_t *dio, off_t lbase)
322 KKASSERT(bp != NULL);
323 off = (lbase & ~HAMMER2_OFF_MASK_RADIX) - bp->b_loffset;
324 KKASSERT(off >= 0 && off < bp->b_bufsize);
325 return(bp->b_data + off);
330 _hammer2_io_new(hammer2_mount_t *hmp, off_t lbase, int lsize,
331 hammer2_io_t **diop, int dozero, int quick)
337 dio = *diop = hammer2_io_getblk(hmp, lbase, lsize, &owner);
339 if (lsize == dio->psize) {
340 dio->bp = getblk(hmp->devvp,
341 dio->pbase, dio->psize,
342 (quick ? GETBLK_NOWAIT : 0),
345 vfs_bio_clrbuf(dio->bp);
347 dio->bp->b_flags |= B_CACHE;
357 error = bread(hmp->devvp, dio->pbase,
358 dio->psize, &dio->bp);
364 hammer2_io_complete(dio, owner);
370 bzero(hammer2_io_data(dio, lbase), lsize);
371 atomic_set_int(&dio->refs, HAMMER2_DIO_DIRTY);
377 hammer2_io_new(hammer2_mount_t *hmp, off_t lbase, int lsize,
380 return(_hammer2_io_new(hmp, lbase, lsize, diop, 1, 0));
384 hammer2_io_newnz(hammer2_mount_t *hmp, off_t lbase, int lsize,
387 return(_hammer2_io_new(hmp, lbase, lsize, diop, 0, 0));
391 hammer2_io_newq(hammer2_mount_t *hmp, off_t lbase, int lsize,
394 return(_hammer2_io_new(hmp, lbase, lsize, diop, 0, 1));
398 hammer2_io_bread(hammer2_mount_t *hmp, off_t lbase, int lsize,
406 dio = *diop = hammer2_io_getblk(hmp, lbase, lsize, &owner);
408 if (hammer2_cluster_enable) {
409 peof = (dio->pbase + HAMMER2_SEGMASK64) &
411 error = cluster_read(hmp->devvp, peof, dio->pbase,
413 dio->psize, HAMMER2_PBUFSIZE*4,
416 error = bread(hmp->devvp, dio->pbase,
417 dio->psize, &dio->bp);
423 hammer2_io_complete(dio, owner);
431 hammer2_io_breadcb(hammer2_mount_t *hmp, off_t lbase, int lsize,
432 void (*callback)(hammer2_io_t *dio, hammer2_chain_t *arg_c,
433 void *arg_p, off_t arg_o),
434 hammer2_chain_t *arg_c, void *arg_p, off_t arg_o)
440 dio = hammer2_io_getblk(hmp, lbase, lsize, &owner);
442 dio->callback = callback;
446 breadcb(hmp->devvp, dio->pbase, dio->psize,
447 hammer2_io_callback, dio);
450 callback(dio, arg_c, arg_p, arg_o);
451 hammer2_io_bqrelse(&dio);
456 hammer2_io_callback(struct bio *bio)
458 struct buf *dbp = bio->bio_buf;
459 hammer2_io_t *dio = bio->bio_caller_info1.ptr;
461 if ((bio->bio_flags & BIO_DONE) == 0)
463 bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
464 dio->bp = bio->bio_buf;
465 KKASSERT((dio->bp->b_flags & B_ERROR) == 0); /* XXX */
466 hammer2_io_complete(dio, HAMMER2_DIO_INPROG);
467 dio->callback(dio, dio->arg_c, dio->arg_p, dio->arg_o);
468 hammer2_io_bqrelse(&dio);
469 /* TODO: async load meta-data and assign chain->dio */
473 hammer2_io_bawrite(hammer2_io_t **diop)
475 atomic_set_int(&(*diop)->refs, HAMMER2_DIO_DIRTY);
476 hammer2_io_putblk(diop);
480 hammer2_io_bdwrite(hammer2_io_t **diop)
482 atomic_set_int(&(*diop)->refs, HAMMER2_DIO_DIRTY);
483 hammer2_io_putblk(diop);
487 hammer2_io_bwrite(hammer2_io_t **diop)
489 atomic_set_int(&(*diop)->refs, HAMMER2_DIO_DIRTY);
490 hammer2_io_putblk(diop);
491 return (0); /* XXX */
495 hammer2_io_setdirty(hammer2_io_t *dio)
497 atomic_set_int(&dio->refs, HAMMER2_DIO_DIRTY);
501 hammer2_io_setinval(hammer2_io_t *dio, u_int bytes)
503 if ((u_int)dio->psize == bytes)
504 dio->bp->b_flags |= B_INVAL | B_RELBUF;
508 hammer2_io_brelse(hammer2_io_t **diop)
510 hammer2_io_putblk(diop);
514 hammer2_io_bqrelse(hammer2_io_t **diop)
516 hammer2_io_putblk(diop);
520 hammer2_io_isdirty(hammer2_io_t *dio)
522 return((dio->refs & HAMMER2_DIO_DIRTY) != 0);