bioqdisksort - fixes to avoid starvation
[dragonfly.git] / sys / sys / buf2.h
1 /*
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)buf.h       8.9 (Berkeley) 3/30/95
39  * $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
40  * $DragonFly: src/sys/sys/buf2.h,v 1.21 2008/01/28 07:19:06 nth Exp $
41  */
42
43 #ifndef _SYS_BUF2_H_
44 #define _SYS_BUF2_H_
45
46 #ifdef _KERNEL
47
48 #ifndef _SYS_BUF_H_
49 #include <sys/buf.h>            /* crit_*() functions */
50 #endif
51 #ifndef _SYS_GLOBALDATA_H_
52 #include <sys/globaldata.h>     /* curthread */
53 #endif
54 #ifndef _SYS_THREAD2_H_
55 #include <sys/thread2.h>        /* crit_*() functions */
56 #endif
57 #ifndef _SYS_SPINLOCK2_H_
58 #include <sys/spinlock2.h>      /* crit_*() functions */
59 #endif
60 #ifndef _SYS_MOUNT_H_
61 #include <sys/mount.h>
62 #endif
63 #ifndef _SYS_VNODE_H_
64 #include <sys/vnode.h>
65 #endif
66
67 /*
68  * Initialize a lock.
69  */
70 #define BUF_LOCKINIT(bp) \
71         lockinit(&(bp)->b_lock, buf_wmesg, 0, 0)
72
73 /*
74  *
75  * Get a lock sleeping non-interruptably until it becomes available.
76  *
77  * XXX lk_wmesg can race, but should not result in any operational issues.
78  */
79 static __inline int
80 BUF_LOCK(struct buf *bp, int locktype)
81 {
82         bp->b_lock.lk_wmesg = buf_wmesg;
83         return (lockmgr(&(bp)->b_lock, locktype));
84 }
85 /*
86  * Get a lock sleeping with specified interruptably and timeout.
87  *
88  * XXX lk_timo can race against other entities calling BUF_TIMELOCK,
89  * but will not interfere with entities calling BUF_LOCK since LK_TIMELOCK
90  * will not be set in that case.
91  *
92  * XXX lk_wmesg can race, but should not result in any operational issues.
93  */
94 static __inline int
95 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int timo)
96 {
97         bp->b_lock.lk_wmesg = wmesg;
98         bp->b_lock.lk_timo = timo;
99         return (lockmgr(&(bp)->b_lock, locktype | LK_TIMELOCK));
100 }
101 /*
102  * Release a lock. Only the acquiring process may free the lock unless
103  * it has been handed off to biodone.
104  */
105 static __inline void
106 BUF_UNLOCK(struct buf *bp)
107 {
108         lockmgr(&(bp)->b_lock, LK_RELEASE);
109 }
110
111 /*
112  * When initiating asynchronous I/O, change ownership of the lock to the
113  * kernel. Once done, the lock may legally released by biodone. The
114  * original owning process can no longer acquire it recursively, but must
115  * wait until the I/O is completed and the lock has been freed by biodone.
116  */
117 static __inline void
118 BUF_KERNPROC(struct buf *bp)
119 {
120         lockmgr_kernproc(&(bp)->b_lock);
121 }
122 /*
123  * Find out the number of references to a lock.
124  *
125  * The non-blocking version should only be used for assertions in cases
126  * where the buffer is expected to be owned or otherwise data stable.
127  */
128 static __inline int
129 BUF_REFCNT(struct buf *bp)
130 {
131         return (lockcount(&(bp)->b_lock));
132 }
133
134 static __inline int
135 BUF_REFCNTNB(struct buf *bp)
136 {
137         return (lockcountnb(&(bp)->b_lock));
138 }
139
140 /*
141  * Free a buffer lock.
142  */
143 #define BUF_LOCKFREE(bp)                        \
144         if (BUF_REFCNTNB(bp) > 0)               \
145                 panic("free locked buf")
146
147 static __inline void
148 bioq_init(struct bio_queue_head *head)
149 {
150         TAILQ_INIT(&head->queue);
151         head->last_offset = 0;
152         head->order_count = 0;
153         head->insert_point = NULL;
154         head->switch_point = NULL;
155 }
156
157 static __inline void
158 bioq_insert_tail_order(struct bio_queue_head *head, struct bio *bio, int order)
159 {
160         if (order) {
161                 head->insert_point = bio;
162                 head->switch_point = NULL;
163                 head->order_count = 0;
164         }
165         TAILQ_INSERT_TAIL(&head->queue, bio, bio_act);
166 }
167
168 static __inline void
169 bioq_insert_tail(struct bio_queue_head *head, struct bio *bio)
170 {
171         bioq_insert_tail_order(head, bio, bio->bio_buf->b_flags & B_ORDERED);
172 }
173
174
175 static __inline void
176 bioq_remove(struct bio_queue_head *head, struct bio *bio)
177 {
178         if (bio == head->switch_point)
179                 head->switch_point = TAILQ_NEXT(bio, bio_act);
180         if (bio == head->insert_point) {
181                 head->insert_point = TAILQ_PREV(bio, bio_queue, bio_act);
182                 if (head->insert_point == NULL)
183                         head->last_offset = 0;
184         } else if (bio == TAILQ_FIRST(&head->queue))
185                 head->last_offset = bio->bio_offset;
186         TAILQ_REMOVE(&head->queue, bio, bio_act);
187         if (TAILQ_FIRST(&head->queue) == head->switch_point)
188                 head->switch_point = NULL;
189 }
190
191 static __inline struct bio *
192 bioq_first(struct bio_queue_head *head)
193 {
194         return (TAILQ_FIRST(&head->queue));
195 }
196
197 /*
198  * biodeps inlines - used by softupdates and HAMMER.
199  */
200 static __inline void
201 buf_dep_init(struct buf *bp)
202 {
203         bp->b_ops = NULL;
204         LIST_INIT(&bp->b_dep);
205 }
206
207 /*
208  * Precondition: the buffer has some dependencies.
209  */
210 static __inline void
211 buf_deallocate(struct buf *bp)
212 {
213         struct bio_ops *ops = bp->b_ops;
214
215         KKASSERT(! LIST_EMPTY(&bp->b_dep));
216         if (ops)
217                 ops->io_deallocate(bp);
218 }
219
220 static __inline int
221 buf_countdeps(struct buf *bp, int n)
222 {
223         struct bio_ops *ops = bp->b_ops;
224         int r;
225
226         if (ops)
227                 r = ops->io_countdeps(bp, n);
228         else
229                 r = 0;
230         return(r);
231 }
232
233 static __inline void
234 buf_start(struct buf *bp)
235 {
236         struct bio_ops *ops = bp->b_ops;
237
238         if (ops)
239                 ops->io_start(bp);
240 }
241
242 static __inline void
243 buf_complete(struct buf *bp)
244 {
245         struct bio_ops *ops = bp->b_ops;
246
247         if (ops)
248                 ops->io_complete(bp);
249 }
250
251 static __inline int
252 buf_fsync(struct vnode *vp)
253 {
254         struct bio_ops *ops = vp->v_mount->mnt_bioops;
255         int r;
256
257         if (ops)
258                 r = ops->io_fsync(vp);
259         else
260                 r = 0;
261         return(r);
262 }
263
264 static __inline void
265 buf_movedeps(struct buf *bp1, struct buf *bp2)
266 {
267         struct bio_ops *ops = bp1->b_ops;
268
269         if (ops)
270                 ops->io_movedeps(bp1, bp2);
271 }
272
273 static __inline int
274 buf_checkread(struct buf *bp)
275 {
276         struct bio_ops *ops = bp->b_ops;
277
278         if (ops)
279                 return(ops->io_checkread(bp));
280         return(0);
281 }
282
283 static __inline int
284 buf_checkwrite(struct buf *bp)
285 {
286         struct bio_ops *ops = bp->b_ops;
287
288         if (ops)
289                 return(ops->io_checkwrite(bp));
290         return(0);
291 }
292
293 /*
294  * Chained biodone.  The bio callback was made and the callback function
295  * wishes to chain the biodone.  If no BIO's are left we call bpdone()
296  * with elseit=TRUE (asynchronous completion).
297  */
298 static __inline void
299 biodone_chain(struct bio *bio)
300 {
301         if (bio->bio_prev)
302                 biodone(bio->bio_prev);
303         else
304                 bpdone(bio->bio_buf, 1);
305 }
306
307 #endif /* _KERNEL */
308
309 #endif /* !_SYS_BUF2_H_ */