2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)buf.h 8.9 (Berkeley) 3/30/95
35 * $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
36 * $DragonFly: src/sys/sys/buf2.h,v 1.21 2008/01/28 07:19:06 nth Exp $
45 #include <sys/buf.h> /* crit_*() functions */
47 #ifndef _SYS_GLOBALDATA_H_
48 #include <sys/globaldata.h> /* curthread */
50 #ifndef _SYS_THREAD2_H_
51 #include <sys/thread2.h> /* crit_*() functions */
53 #ifndef _SYS_SPINLOCK2_H_
54 #include <sys/spinlock2.h> /* crit_*() functions */
57 #include <sys/mount.h>
60 #include <sys/vnode.h>
62 #ifndef _VM_VM_PAGE_H_
63 #include <vm/vm_page.h>
69 #define BUF_LOCKINIT(bp) \
70 lockinit(&(bp)->b_lock, buf_wmesg, 0, LK_NOCOLLSTATS)
74 * Get a lock sleeping non-interruptably until it becomes available.
76 * XXX lk_wmesg can race, but should not result in any operational issues.
79 BUF_LOCK(struct buf *bp, int locktype)
81 bp->b_lock.lk_wmesg = buf_wmesg;
82 return (lockmgr(&(bp)->b_lock, locktype));
85 * Get a lock sleeping with specified interruptably and timeout.
87 * XXX lk_timo can race against other entities calling BUF_TIMELOCK,
88 * but will not interfere with entities calling BUF_LOCK since LK_TIMELOCK
89 * will not be set in that case.
91 * XXX lk_wmesg can race, but should not result in any operational issues.
94 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int timo)
96 bp->b_lock.lk_wmesg = wmesg;
97 bp->b_lock.lk_timo = timo;
98 return (lockmgr(&(bp)->b_lock, locktype | LK_TIMELOCK));
101 * Release a lock. Only the acquiring process may free the lock unless
102 * it has been handed off to biodone.
105 BUF_UNLOCK(struct buf *bp)
107 lockmgr(&(bp)->b_lock, LK_RELEASE);
111 * When initiating asynchronous I/O, change ownership of the lock to the
112 * kernel. Once done, the lock may legally released by biodone. The
113 * original owning process can no longer acquire it recursively, but must
114 * wait until the I/O is completed and the lock has been freed by biodone.
117 BUF_KERNPROC(struct buf *bp)
119 lockmgr_kernproc(&(bp)->b_lock);
122 * Find out the number of references to a lock.
124 * The non-blocking version should only be used for assertions in cases
125 * where the buffer is expected to be owned or otherwise data stable.
128 BUF_LOCKINUSE(struct buf *bp)
130 return (lockinuse(&(bp)->b_lock));
134 * Free a buffer lock.
136 #define BUF_LOCKFREE(bp) \
137 if (BUF_LOCKINUSE(bp)) \
138 panic("free locked buf")
141 bioq_init(struct bio_queue_head *bioq)
143 TAILQ_INIT(&bioq->queue);
144 bioq->off_unused = 0;
146 bioq->transition = NULL;
147 bioq->bio_unused = NULL;
151 bioq_insert_tail(struct bio_queue_head *bioq, struct bio *bio)
153 bioq->transition = NULL;
154 TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
158 bioq_remove(struct bio_queue_head *bioq, struct bio *bio)
161 * Adjust read insertion point when removing the bioq. The
162 * bio after the insert point is a write so move backwards
163 * one (NULL will indicate all the reads have cleared).
165 if (bio == bioq->transition)
166 bioq->transition = TAILQ_NEXT(bio, bio_act);
167 TAILQ_REMOVE(&bioq->queue, bio, bio_act);
170 static __inline struct bio *
171 bioq_first(struct bio_queue_head *bioq)
173 return (TAILQ_FIRST(&bioq->queue));
176 static __inline struct bio *
177 bioq_takefirst(struct bio_queue_head *bioq)
181 bp = TAILQ_FIRST(&bioq->queue);
183 bioq_remove(bioq, bp);
188 * Adjust buffer cache buffer's activity count. This
189 * works similarly to vm_page->act_count.
192 buf_act_advance(struct buf *bp)
194 if (bp->b_act_count > ACT_MAX - ACT_ADVANCE)
195 bp->b_act_count = ACT_MAX;
197 bp->b_act_count += ACT_ADVANCE;
201 buf_act_decline(struct buf *bp)
203 if (bp->b_act_count < ACT_DECLINE)
206 bp->b_act_count -= ACT_DECLINE;
210 * biodeps inlines - used by softupdates and HAMMER.
212 * All bioops are MPSAFE
215 buf_dep_init(struct buf *bp)
218 LIST_INIT(&bp->b_dep);
222 * Precondition: the buffer has some dependencies.
227 buf_deallocate(struct buf *bp)
229 struct bio_ops *ops = bp->b_ops;
231 KKASSERT(! LIST_EMPTY(&bp->b_dep));
233 ops->io_deallocate(bp);
237 * This callback is made from flushbufqueues() which uses BUF_LOCK().
238 * Since it isn't going through a normal buffer aquisition mechanic
239 * and calling the filesystem back enforce the vnode's KVABIO support.
242 buf_countdeps(struct buf *bp, int n)
244 struct bio_ops *ops = bp->b_ops;
248 if (bp->b_vp == NULL || (bp->b_vp->v_flag & VKVABIO) == 0)
250 r = ops->io_countdeps(bp, n);
261 buf_start(struct buf *bp)
263 struct bio_ops *ops = bp->b_ops;
273 buf_complete(struct buf *bp)
275 struct bio_ops *ops = bp->b_ops;
278 ops->io_complete(bp);
285 buf_fsync(struct vnode *vp)
287 struct bio_ops *ops = vp->v_mount->mnt_bioops;
291 r = ops->io_fsync(vp);
301 buf_movedeps(struct buf *bp1, struct buf *bp2)
303 struct bio_ops *ops = bp1->b_ops;
306 ops->io_movedeps(bp1, bp2);
313 buf_checkread(struct buf *bp)
315 struct bio_ops *ops = bp->b_ops;
318 return(ops->io_checkread(bp));
323 * This callback is made from flushbufqueues() which uses BUF_LOCK().
324 * Since it isn't going through a normal buffer aquisition mechanic
325 * and calling the filesystem back enforce the vnode's KVABIO support.
328 buf_checkwrite(struct buf *bp)
330 struct bio_ops *ops = bp->b_ops;
333 if (bp->b_vp == NULL || (bp->b_vp->v_flag & VKVABIO) == 0)
335 return(ops->io_checkwrite(bp));
341 * Chained biodone. The bio callback was made and the callback function
342 * wishes to chain the biodone. If no BIO's are left we call bpdone()
343 * with elseit=TRUE (asynchronous completion).
348 biodone_chain(struct bio *bio)
351 biodone(bio->bio_prev);
353 bpdone(bio->bio_buf, 1);
357 bread(struct vnode *vp, off_t loffset, int size, struct buf **bpp)
360 return(breadnx(vp, loffset, size, B_NOTMETA,
361 NULL, NULL, 0, bpp));
365 bread_kvabio(struct vnode *vp, off_t loffset, int size, struct buf **bpp)
368 return(breadnx(vp, loffset, size, B_NOTMETA | B_KVABIO,
369 NULL, NULL, 0, bpp));
373 breadn(struct vnode *vp, off_t loffset, int size, off_t *raoffset,
374 int *rabsize, int cnt, struct buf **bpp)
377 return(breadnx(vp, loffset, size, B_NOTMETA, raoffset,
382 cluster_read(struct vnode *vp, off_t filesize, off_t loffset,
383 int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
386 return(cluster_readx(vp, filesize, loffset, blksize, B_NOTMETA,
387 minreq, maxreq, bpp));
391 cluster_read_kvabio(struct vnode *vp, off_t filesize, off_t loffset,
392 int blksize, size_t minreq, size_t maxreq, struct buf **bpp)
395 return(cluster_readx(vp, filesize, loffset, blksize,
396 B_NOTMETA | B_KVABIO,
397 minreq, maxreq, bpp));
402 #endif /* !_SYS_BUF2_H_ */