FreeBSD/Linux Kernel Cross Reference
sys/sys/buf.h
1 /* $NetBSD: buf.h,v 1.72 2004/02/28 06:28:47 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1989, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)buf.h 8.9 (Berkeley) 3/30/95
74 */
75
76 #ifndef _SYS_BUF_H_
77 #define _SYS_BUF_H_
78
79 #include <sys/pool.h>
80 #include <sys/queue.h>
81 #include <sys/lock.h>
82
83 struct buf;
84 struct mount;
85 struct vnode;
86
87 #define NOLIST ((struct buf *)0x87654321)
88
89 /*
90 * To avoid including <ufs/ffs/softdep.h>
91 */
92 LIST_HEAD(workhead, worklist);
93
94 /*
95 * Device driver buffer queue.
96 */
97 struct bufq_state {
98 void (*bq_put)(struct bufq_state *, struct buf *);
99 struct buf *(*bq_get)(struct bufq_state *, int);
100 void *bq_private;
101 int bq_flags; /* Flags from bufq_alloc() */
102 };
103
104 /*
105 * Flags for bufq_alloc.
106 */
107 #define BUFQ_SORT_RAWBLOCK 0x0001 /* Sort by b_rawblkno */
108 #define BUFQ_SORT_CYLINDER 0x0002 /* Sort by b_cylinder, b_rawblkno */
109
110 #define BUFQ_FCFS 0x0010 /* First-come first-serve */
111 #define BUFQ_DISKSORT 0x0020 /* Min seek sort */
112 #define BUFQ_READ_PRIO 0x0030 /* Min seek and read priority */
113 #define BUFQ_PRIOCSCAN 0x0040 /* Per-priority CSCAN */
114
115 #define BUFQ_SORT_MASK 0x000f
116 #define BUFQ_METHOD_MASK 0x00f0
117
118 #ifdef _KERNEL
119
120 extern int bufq_disk_default_strat;
121 #define BUFQ_DISK_DEFAULT_STRAT() bufq_disk_default_strat
122 void bufq_alloc(struct bufq_state *, int);
123 void bufq_free(struct bufq_state *);
124
125 #define BUFQ_PUT(bufq, bp) \
126 (*(bufq)->bq_put)((bufq), (bp)) /* Put buffer in queue */
127 #define BUFQ_GET(bufq) \
128 (*(bufq)->bq_get)((bufq), 1) /* Get and remove buffer from queue */
129 #define BUFQ_PEEK(bufq) \
130 (*(bufq)->bq_get)((bufq), 0) /* Get buffer from queue */
131
132 #endif /* _KERNEL */
133
134 /*
135 * These are currently used only by the soft dependency code, hence
136 * are stored once in a global variable. If other subsystems wanted
137 * to use these hooks, a pointer to a set of bio_ops could be added
138 * to each buffer.
139 */
140 struct bio_ops {
141 void (*io_start)(struct buf *);
142 void (*io_complete)(struct buf *);
143 void (*io_deallocate)(struct buf *);
144 int (*io_fsync)(struct vnode *);
145 int (*io_sync)(struct mount *);
146 void (*io_movedeps)(struct buf *, struct buf *);
147 int (*io_countdeps)(struct buf *, int);
148 void (*io_pageiodone)(struct buf *);
149 };
150
151 /*
152 * The buffer header describes an I/O operation in the kernel.
153 */
154 struct buf {
155 TAILQ_ENTRY(buf) b_actq; /* Device driver queue when active. */
156 struct simplelock b_interlock; /* Lock for b_flags changes */
157 volatile long b_flags; /* B_* flags. */
158 int b_error; /* Errno value. */
159 int b_prio; /* Hint for buffer queue discipline. */
160 long b_bufsize; /* Allocated buffer size. */
161 long b_bcount; /* Valid bytes in buffer. */
162 long b_resid; /* Remaining I/O. */
163 dev_t b_dev; /* Device associated with buffer. */
164 struct {
165 caddr_t b_addr; /* Memory, superblocks, indirect etc. */
166 } b_un;
167 daddr_t b_blkno; /* Underlying physical block number
168 (partition relative) */
169 daddr_t b_rawblkno; /* Raw underlying physical block
170 number (not partition relative) */
171 /* Function to call upon completion. */
172 void (*b_iodone)(struct buf *);
173 struct proc *b_proc; /* Associated proc if B_PHYS set. */
174 struct vnode *b_vp; /* File vnode. */
175 struct workhead b_dep; /* List of filesystem dependencies. */
176 void *b_saveaddr; /* Original b_addr for physio. */
177
178 /*
179 * private data for owner.
180 * - buffer cache buffers are owned by corresponding filesystem.
181 * - non-buffer cache buffers are owned by subsystem which
182 * allocated them. (filesystem, disk driver, etc)
183 */
184 union {
185 void *bf_private;
186 off_t bf_dcookie; /* NFS: Offset cookie if dir block */
187 } b_fspriv;
188 #define b_private b_fspriv.bf_private
189 #define b_dcookie b_fspriv.bf_dcookie
190
191 /*
192 * buffer cache specific data
193 */
194 LIST_ENTRY(buf) b_hash; /* Hash chain. */
195 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
196 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
197 daddr_t b_lblkno; /* Logical block number. */
198 };
199
200 #define BUF_INIT(bp) \
201 do { \
202 LIST_INIT(&(bp)->b_dep); \
203 simple_lock_init(&(bp)->b_interlock); \
204 (bp)->b_dev = NODEV; \
205 BIO_SETPRIO((bp), BPRIO_DEFAULT); \
206 } while (/*CONSTCOND*/0)
207
208 /*
209 * For portability with historic industry practice, the cylinder number has
210 * to be maintained in the `b_resid' field.
211 */
212 #define b_cylinder b_resid /* Cylinder number for disksort(). */
213
214 /* Device driver compatibility definitions. */
215 #define b_data b_un.b_addr /* b_un.b_addr is not changeable. */
216
217 /*
218 * These flags are kept in b_flags.
219 */
220 #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
221 #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
222 #define B_BAD 0x00000008 /* Bad block revectoring in progress. */
223 #define B_BUSY 0x00000010 /* I/O in progress. */
224 #define B_SCANNED 0x00000020 /* Block already pushed during sync */
225 #define B_CALL 0x00000040 /* Call b_iodone from biodone. */
226 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
227 #define B_DIRTY 0x00000100 /* Dirty page to be pushed out async. */
228 #define B_DONE 0x00000200 /* I/O completed. */
229 #define B_EINTR 0x00000400 /* I/O was interrupted */
230 #define B_ERROR 0x00000800 /* I/O error occurred. */
231 #define B_GATHERED 0x00001000 /* LFS: already in a segment. */
232 #define B_INVAL 0x00002000 /* Does not contain valid info. */
233 #define B_LOCKED 0x00004000 /* Locked in core (not reusable). */
234 #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
235 #define B_CACHE 0x00020000 /* Bread found us in the cache. */
236 #define B_PHYS 0x00040000 /* I/O to user memory. */
237 #define B_RAW 0x00080000 /* Set by physio for raw transfers. */
238 #define B_READ 0x00100000 /* Read buffer. */
239 #define B_TAPE 0x00200000 /* Magnetic tape I/O. */
240 #define B_WANTED 0x00800000 /* Process wants this buffer. */
241 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
242 #define B_XXX 0x02000000 /* Debugging flag. */
243 #define B_VFLUSH 0x04000000 /* Buffer is being synced. */
244
245 /*
246 * This structure describes a clustered I/O. It is stored in the b_saveaddr
247 * field of the buffer on which I/O is done. At I/O completion, cluster
248 * callback uses the structure to parcel I/O's to individual buffers, and
249 * then free's this structure.
250 */
251 struct cluster_save {
252 long bs_bcount; /* Saved b_bcount. */
253 long bs_bufsize; /* Saved b_bufsize. */
254 void *bs_saveaddr; /* Saved b_addr. */
255 int bs_nchildren; /* Number of associated buffers. */
256 struct buf **bs_children; /* List of associated buffers. */
257 };
258
259 /*
260 * Zero out the buffer's data area.
261 */
262 #define clrbuf(bp) \
263 do { \
264 memset((bp)->b_data, 0, (u_int)(bp)->b_bcount); \
265 (bp)->b_resid = 0; \
266 } while (/* CONSTCOND */ 0)
267
268 /* Flags to low-level allocation routines. */
269 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
270 #define B_SYNC 0x02 /* Do all allocations synchronously. */
271
272 #ifdef _KERNEL
273
274 #define BIO_GETPRIO(bp) ((bp)->b_prio)
275 #define BIO_SETPRIO(bp, prio) (bp)->b_prio = (prio)
276 #define BIO_COPYPRIO(bp1, bp2) BIO_SETPRIO(bp1, BIO_GETPRIO(bp2))
277
278 #define BPRIO_NPRIO 3
279 #define BPRIO_TIMECRITICAL 2
280 #define BPRIO_TIMELIMITED 1
281 #define BPRIO_TIMENONCRITICAL 0
282 #define BPRIO_DEFAULT BPRIO_TIMELIMITED
283
284 extern struct bio_ops bioops;
285 extern u_int nbuf; /* The number of buffer headers */
286 extern struct buf *buf; /* The buffer headers. */
287 extern char *buffers; /* The buffer contents. */
288 extern u_int bufpages; /* Number of memory pages in the buffer pool. */
289
290 /*
291 * Pool of I/O buffers. Access to this pool must be protected with
292 * splbio().
293 */
294 extern struct pool bufpool;
295
296 __BEGIN_DECLS
297 void allocbuf(struct buf *, int, int);
298 void bawrite(struct buf *);
299 void bdirty(struct buf *);
300 void bdwrite(struct buf *);
301 void biodone(struct buf *);
302 int biowait(struct buf *);
303 int bread(struct vnode *, daddr_t, int, struct ucred *, struct buf **);
304 int breada(struct vnode *, daddr_t, int, daddr_t, int, struct ucred *,
305 struct buf **);
306 int breadn(struct vnode *, daddr_t, int, daddr_t *, int *, int,
307 struct ucred *, struct buf **);
308 void brelse(struct buf *);
309 void bremfree(struct buf *);
310 void bufinit(void);
311 int bwrite(struct buf *);
312 struct buf *getblk(struct vnode *, daddr_t, int, int, int);
313 struct buf *geteblk(int);
314 struct buf *getnewbuf(int, int, int);
315 struct buf *incore(struct vnode *, daddr_t);
316
317 void minphys(struct buf *);
318 int physio(void (*)(struct buf *), struct buf *, dev_t, int,
319 void (*)(struct buf *), struct uio *);
320
321 void brelvp(struct buf *);
322 void reassignbuf(struct buf *, struct vnode *);
323 void bgetvp(struct vnode *, struct buf *);
324 int buf_syncwait(void);
325 u_long buf_memcalc(void);
326 int buf_drain(int);
327 int buf_setvalimit(vsize_t);
328 #ifdef DDB
329 void vfs_buf_print(struct buf *, int, void (*)(const char *, ...));
330 #endif
331 __END_DECLS
332 #endif
333 #endif /* !_SYS_BUF_H_ */
Cache object: 4a2a34d7fb52e66012ef99c94d52e5a3
|