9796a64d386744c063b4595d27ded8b78b066814
[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.17 2006/05/05 20:15:01 dillon Exp $
41  */
42
43 #ifndef _SYS_BUF2_H_
44 #define _SYS_BUF2_H_
45
46 #ifdef _KERNEL
47
48 #ifndef _SYS_GLOBALDATA_H_
49 #include <sys/globaldata.h>     /* curthread */
50 #endif
51 #ifndef _SYS_THREAD2_H_
52 #include <sys/thread2.h>        /* crit_*() functions */
53 #endif
54 #ifndef _SYS_SPINLOCK2_H_
55 #include <sys/spinlock2.h>      /* crit_*() functions */
56 #endif
57
58 /*
59  * Initialize a lock.
60  */
61 #define BUF_LOCKINIT(bp) \
62         lockinit(&(bp)->b_lock, buf_wmesg, 0, 0)
63
64 /*
65  *
66  * Get a lock sleeping non-interruptably until it becomes available.
67  *
68  * XXX lk_wmesg can race, but should not result in any operational issues.
69  */
70 static __inline int
71 BUF_LOCK(struct buf *bp, int locktype)
72 {
73         bp->b_lock.lk_wmesg = buf_wmesg;
74         return (lockmgr(&(bp)->b_lock, locktype));
75 }
76 /*
77  * Get a lock sleeping with specified interruptably and timeout.
78  *
79  * XXX lk_timo can race against other entities calling BUF_TIMELOCK,
80  * but will not interfere with entities calling BUF_LOCK since LK_TIMELOCK
81  * will not be set in that case.
82  *
83  * XXX lk_wmesg can race, but should not result in any operational issues.
84  */
85 static __inline int
86 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int timo)
87 {
88         bp->b_lock.lk_wmesg = wmesg;
89         bp->b_lock.lk_timo = timo;
90         return (lockmgr(&(bp)->b_lock, locktype | LK_TIMELOCK));
91 }
92 /*
93  * Release a lock. Only the acquiring process may free the lock unless
94  * it has been handed off to biodone.
95  */
96 static __inline void
97 BUF_UNLOCK(struct buf *bp)
98 {
99         lockmgr(&(bp)->b_lock, LK_RELEASE);
100 }
101
102 /*
103  * When initiating asynchronous I/O, change ownership of the lock to the
104  * kernel. Once done, the lock may legally released by biodone. The
105  * original owning process can no longer acquire it recursively, but must
106  * wait until the I/O is completed and the lock has been freed by biodone.
107  */
108 static __inline void
109 BUF_KERNPROC(struct buf *bp)
110 {
111         lockmgr_kernproc(&(bp)->b_lock);
112 }
113 /*
114  * Find out the number of references to a lock.
115  *
116  * The non-blocking version should only be used for assertions in cases
117  * where the buffer is expected to be owned or otherwise data stable.
118  */
119 static __inline int
120 BUF_REFCNT(struct buf *bp)
121 {
122         return (lockcount(&(bp)->b_lock));
123 }
124
125 static __inline int
126 BUF_REFCNTNB(struct buf *bp)
127 {
128         return (lockcountnb(&(bp)->b_lock));
129 }
130
131 /*
132  * Free a buffer lock.
133  */
134 #define BUF_LOCKFREE(bp)                        \
135         if (BUF_REFCNTNB(bp) > 0)               \
136                 panic("free locked buf")
137
138 static __inline void
139 bioq_init(struct bio_queue_head *head)
140 {
141         TAILQ_INIT(&head->queue);
142         head->last_offset = 0;
143         head->insert_point = NULL;
144         head->switch_point = NULL;
145 }
146
147 static __inline void
148 bioq_insert_tail(struct bio_queue_head *head, struct bio *bio)
149 {
150         if ((bio->bio_buf->b_flags & B_ORDERED) != 0) {
151                 head->insert_point = bio;
152                 head->switch_point = NULL;
153         }
154         TAILQ_INSERT_TAIL(&head->queue, bio, bio_act);
155 }
156
157 static __inline void
158 bioq_remove(struct bio_queue_head *head, struct bio *bio)
159 {
160         if (bio == head->switch_point)
161                 head->switch_point = TAILQ_NEXT(bio, bio_act);
162         if (bio == head->insert_point) {
163                 head->insert_point = TAILQ_PREV(bio, bio_queue, bio_act);
164                 if (head->insert_point == NULL)
165                         head->last_offset = 0;
166         } else if (bio == TAILQ_FIRST(&head->queue))
167                 head->last_offset = bio->bio_offset;
168         TAILQ_REMOVE(&head->queue, bio, bio_act);
169         if (TAILQ_FIRST(&head->queue) == head->switch_point)
170                 head->switch_point = NULL;
171 }
172
173 static __inline struct bio *
174 bioq_first(struct bio_queue_head *head)
175 {
176         return (TAILQ_FIRST(&head->queue));
177 }
178
179 #endif /* _KERNEL */
180
181 #endif /* !_SYS_BUF2_H_ */