proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / sys / buf2.h
CommitLineData
3020e3be
MD
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 $
dadab5e9 40 * $DragonFly: src/sys/sys/buf2.h,v 1.2 2003/06/25 03:56:10 dillon Exp $
3020e3be
MD
41 */
42
43#ifndef _SYS_BUF2_H_
44#define _SYS_BUF2_H_
45
46#ifdef _KERNEL
47
48/*
49 * Initialize a lock.
50 */
51#define BUF_LOCKINIT(bp) \
52 lockinit(&(bp)->b_lock, PRIBIO + 4, buf_wmesg, 0, 0)
53
54/*
55 *
56 * Get a lock sleeping non-interruptably until it becomes available.
57 */
58static __inline int BUF_LOCK __P((struct buf *, int));
59static __inline int
60BUF_LOCK(struct buf *bp, int locktype)
61{
62 int s, ret;
63
64 s = splbio();
65 simple_lock(&buftimelock);
66 locktype |= LK_INTERLOCK;
67 bp->b_lock.lk_wmesg = buf_wmesg;
68 bp->b_lock.lk_prio = PRIBIO + 4;
69 /* bp->b_lock.lk_timo = 0; not necessary */
dadab5e9 70 ret = lockmgr(&(bp)->b_lock, locktype, &buftimelock, curthread);
3020e3be
MD
71 splx(s);
72 return ret;
73}
74/*
75 * Get a lock sleeping with specified interruptably and timeout.
76 */
77static __inline int BUF_TIMELOCK __P((struct buf *, int, char *, int, int));
78static __inline int
79BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int catch, int timo)
80{
81 int s, ret;
82
83 s = splbio();
84 simple_lock(&buftimelock);
85 locktype |= LK_INTERLOCK | LK_TIMELOCK;
86 bp->b_lock.lk_wmesg = wmesg;
87 bp->b_lock.lk_prio = (PRIBIO + 4) | catch;
88 bp->b_lock.lk_timo = timo;
dadab5e9 89 ret = lockmgr(&(bp)->b_lock, (locktype), &buftimelock, curthread);
3020e3be
MD
90 splx(s);
91 return ret;
92}
93/*
94 * Release a lock. Only the acquiring process may free the lock unless
95 * it has been handed off to biodone.
96 */
97static __inline void BUF_UNLOCK __P((struct buf *));
98static __inline void
99BUF_UNLOCK(struct buf *bp)
100{
101 int s;
102
103 s = splbio();
dadab5e9 104 lockmgr(&(bp)->b_lock, LK_RELEASE, NULL, curthread);
3020e3be
MD
105 splx(s);
106}
107
108/*
109 * Free a buffer lock.
110 */
111#define BUF_LOCKFREE(bp) \
112 if (BUF_REFCNT(bp) > 0) \
113 panic("free locked buf")
114/*
115 * When initiating asynchronous I/O, change ownership of the lock to the
116 * kernel. Once done, the lock may legally released by biodone. The
117 * original owning process can no longer acquire it recursively, but must
118 * wait until the I/O is completed and the lock has been freed by biodone.
119 */
120static __inline void BUF_KERNPROC __P((struct buf *));
121static __inline void
122BUF_KERNPROC(struct buf *bp)
123{
dadab5e9 124 struct thread *td = curthread;
3020e3be 125
dadab5e9
MD
126 if (bp->b_lock.lk_lockholder == td)
127 td->td_locks--;
128 bp->b_lock.lk_lockholder = LK_KERNTHREAD;
3020e3be
MD
129}
130/*
131 * Find out the number of references to a lock.
132 */
133static __inline int BUF_REFCNT __P((struct buf *));
134static __inline int
135BUF_REFCNT(struct buf *bp)
136{
137 int s, ret;
138
139 s = splbio();
140 ret = lockcount(&(bp)->b_lock);
141 splx(s);
142 return ret;
143}
144
145static __inline void
146bufq_init(struct buf_queue_head *head)
147{
148 TAILQ_INIT(&head->queue);
149 head->last_pblkno = 0;
150 head->insert_point = NULL;
151 head->switch_point = NULL;
152}
153
154static __inline void
155bufq_insert_tail(struct buf_queue_head *head, struct buf *bp)
156{
157 if ((bp->b_flags & B_ORDERED) != 0) {
158 head->insert_point = bp;
159 head->switch_point = NULL;
160 }
161 TAILQ_INSERT_TAIL(&head->queue, bp, b_act);
162}
163
164static __inline void
165bufq_remove(struct buf_queue_head *head, struct buf *bp)
166{
167 if (bp == head->switch_point)
168 head->switch_point = TAILQ_NEXT(bp, b_act);
169 if (bp == head->insert_point) {
170 head->insert_point = TAILQ_PREV(bp, buf_queue, b_act);
171 if (head->insert_point == NULL)
172 head->last_pblkno = 0;
173 } else if (bp == TAILQ_FIRST(&head->queue))
174 head->last_pblkno = bp->b_pblkno;
175 TAILQ_REMOVE(&head->queue, bp, b_act);
176 if (TAILQ_FIRST(&head->queue) == head->switch_point)
177 head->switch_point = NULL;
178}
179
180static __inline struct buf *
181bufq_first(struct buf_queue_head *head)
182{
183 return (TAILQ_FIRST(&head->queue));
184}
185
186#endif /* _KERNEL */
187
188#endif /* !_SYS_BUF2_H_ */