FreeBSD/Linux Kernel Cross Reference
sys/sys/buf.h
1 /* $NetBSD: buf.h,v 1.80 2005/02/26 22:25:34 perry 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 /*
96 * These are currently used only by the soft dependency code, hence
97 * are stored once in a global variable. If other subsystems wanted
98 * to use these hooks, a pointer to a set of bio_ops could be added
99 * to each buffer.
100 */
101 struct bio_ops {
102 void (*io_start)(struct buf *);
103 void (*io_complete)(struct buf *);
104 void (*io_deallocate)(struct buf *);
105 int (*io_fsync)(struct vnode *, int);
106 int (*io_sync)(struct mount *);
107 void (*io_movedeps)(struct buf *, struct buf *);
108 int (*io_countdeps)(struct buf *, int);
109 void (*io_pageiodone)(struct buf *);
110 };
111
112 /*
113 * The buffer header describes an I/O operation in the kernel.
114 */
115 struct buf {
116 TAILQ_ENTRY(buf) b_actq; /* Device driver queue when active. */
117 struct simplelock b_interlock; /* Lock for b_flags changes */
118 volatile int b_flags; /* B_* flags. */
119 int b_error; /* Errno value. */
120 int b_prio; /* Hint for buffer queue discipline. */
121 int b_bufsize; /* Allocated buffer size. */
122 int b_bcount; /* Valid bytes in buffer. */
123 int b_resid; /* Remaining I/O. */
124 dev_t b_dev; /* Device associated with buffer. */
125 struct {
126 caddr_t b_addr; /* Memory, superblocks, indirect etc. */
127 } b_un;
128 daddr_t b_blkno; /* Underlying physical block number
129 (partition relative) */
130 daddr_t b_rawblkno; /* Raw underlying physical block
131 number (not partition relative) */
132 /* Function to call upon completion. */
133 void (*b_iodone)(struct buf *);
134 struct proc *b_proc; /* Associated proc if B_PHYS set. */
135 struct vnode *b_vp; /* File vnode. */
136 struct workhead b_dep; /* List of filesystem dependencies. */
137 void *b_saveaddr; /* Original b_addr for physio. */
138
139 /*
140 * private data for owner.
141 * - buffer cache buffers are owned by corresponding filesystem.
142 * - non-buffer cache buffers are owned by subsystem which
143 * allocated them. (filesystem, disk driver, etc)
144 */
145 union {
146 void *bf_private;
147 off_t bf_dcookie; /* NFS: Offset cookie if dir block */
148 } b_fspriv;
149 #define b_private b_fspriv.bf_private
150 #define b_dcookie b_fspriv.bf_dcookie
151
152 /*
153 * buffer cache specific data
154 */
155 LIST_ENTRY(buf) b_hash; /* Hash chain. */
156 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
157 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
158 daddr_t b_lblkno; /* Logical block number. */
159 int b_freelistindex; /* Free list index. (BQ_) */
160 };
161
162 #define BUF_INIT(bp) \
163 do { \
164 LIST_INIT(&(bp)->b_dep); \
165 simple_lock_init(&(bp)->b_interlock); \
166 (bp)->b_dev = NODEV; \
167 BIO_SETPRIO((bp), BPRIO_DEFAULT); \
168 } while (/*CONSTCOND*/0)
169
170 /*
171 * For portability with historic industry practice, the cylinder number has
172 * to be maintained in the `b_resid' field.
173 */
174 #define b_cylinder b_resid /* Cylinder number for disksort(). */
175
176 /* Device driver compatibility definitions. */
177 #define b_data b_un.b_addr /* b_un.b_addr is not changeable. */
178
179 /*
180 * These flags are kept in b_flags.
181 */
182 #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
183 #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
184 #define B_BAD 0x00000008 /* Bad block revectoring in progress. */
185 #define B_BUSY 0x00000010 /* I/O in progress. */
186 #define B_SCANNED 0x00000020 /* Block already pushed during sync */
187 #define B_CALL 0x00000040 /* Call b_iodone from biodone. */
188 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
189 #define B_DIRTY 0x00000100 /* Dirty page to be pushed out async. */
190 #define B_DONE 0x00000200 /* I/O completed. */
191 #define B_EINTR 0x00000400 /* I/O was interrupted */
192 #define B_ERROR 0x00000800 /* I/O error occurred. */
193 #define B_GATHERED 0x00001000 /* LFS: already in a segment. */
194 #define B_INVAL 0x00002000 /* Does not contain valid info. */
195 #define B_LOCKED 0x00004000 /* Locked in core (not reusable). */
196 #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
197 #define B_CACHE 0x00020000 /* Bread found us in the cache. */
198 #define B_PHYS 0x00040000 /* I/O to user memory. */
199 #define B_RAW 0x00080000 /* Set by physio for raw transfers. */
200 #define B_READ 0x00100000 /* Read buffer. */
201 #define B_TAPE 0x00200000 /* Magnetic tape I/O. */
202 #define B_WANTED 0x00800000 /* Process wants this buffer. */
203 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
204 #define B_XXX 0x02000000 /* Debugging flag. */
205 #define B_VFLUSH 0x04000000 /* Buffer is being synced. */
206
207 #define BUF_FLAGBITS \
208 "\2\1AGE\3ASYNC\4BAD\5BUSY\6SCANNED\7CALL\10DELWRI" \
209 "\11DIRTY\12DONE\13EINTR\14ERROR\15GATHERED\16INVAL\17LOCKED\20NOCACHE" \
210 "\22CACHE\23PHYS\24RAW\25READ\26TAPE\30WANTED\32XXX\33VFLUSH"
211
212
213 /*
214 * This structure describes a clustered I/O. It is stored in the b_saveaddr
215 * field of the buffer on which I/O is done. At I/O completion, cluster
216 * callback uses the structure to parcel I/O's to individual buffers, and
217 * then free's this structure.
218 */
219 struct cluster_save {
220 long bs_bcount; /* Saved b_bcount. */
221 long bs_bufsize; /* Saved b_bufsize. */
222 void *bs_saveaddr; /* Saved b_addr. */
223 int bs_nchildren; /* Number of associated buffers. */
224 struct buf **bs_children; /* List of associated buffers. */
225 };
226
227 /*
228 * Zero out the buffer's data area.
229 */
230 #define clrbuf(bp) \
231 do { \
232 memset((bp)->b_data, 0, (u_int)(bp)->b_bcount); \
233 (bp)->b_resid = 0; \
234 } while (/* CONSTCOND */ 0)
235
236 /* Flags to low-level allocation routines. */
237 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
238 #define B_SYNC 0x02 /* Do all allocations synchronously. */
239 #define B_METAONLY 0x04 /* Return indirect block buffer. */
240
241 #ifdef _KERNEL
242
243 #define BIO_GETPRIO(bp) ((bp)->b_prio)
244 #define BIO_SETPRIO(bp, prio) (bp)->b_prio = (prio)
245 #define BIO_COPYPRIO(bp1, bp2) BIO_SETPRIO(bp1, BIO_GETPRIO(bp2))
246
247 #define BPRIO_NPRIO 3
248 #define BPRIO_TIMECRITICAL 2
249 #define BPRIO_TIMELIMITED 1
250 #define BPRIO_TIMENONCRITICAL 0
251 #define BPRIO_DEFAULT BPRIO_TIMELIMITED
252
253 extern struct bio_ops bioops;
254 extern u_int nbuf; /* The number of buffer headers */
255 extern struct buf *buf; /* The buffer headers. */
256 extern char *buffers; /* The buffer contents. */
257 extern u_int bufpages; /* Number of memory pages in the buffer pool. */
258
259 /*
260 * Pool of I/O buffers. Access to this pool must be protected with
261 * splbio().
262 */
263 extern struct pool bufpool;
264
265 __BEGIN_DECLS
266 void allocbuf(struct buf *, int, int);
267 void bawrite(struct buf *);
268 void bdirty(struct buf *);
269 void bdwrite(struct buf *);
270 void biodone(struct buf *);
271 int biowait(struct buf *);
272 int bread(struct vnode *, daddr_t, int, struct ucred *, struct buf **);
273 int breada(struct vnode *, daddr_t, int, daddr_t, int, struct ucred *,
274 struct buf **);
275 int breadn(struct vnode *, daddr_t, int, daddr_t *, int *, int,
276 struct ucred *, struct buf **);
277 void brelse(struct buf *);
278 void bremfree(struct buf *);
279 void bufinit(void);
280 int bwrite(struct buf *);
281 struct buf *getblk(struct vnode *, daddr_t, int, int, int);
282 struct buf *geteblk(int);
283 struct buf *getnewbuf(int, int, int);
284 struct buf *incore(struct vnode *, daddr_t);
285
286 void minphys(struct buf *);
287 int physio(void (*)(struct buf *), struct buf *, dev_t, int,
288 void (*)(struct buf *), struct uio *);
289
290 void brelvp(struct buf *);
291 void reassignbuf(struct buf *, struct vnode *);
292 void bgetvp(struct vnode *, struct buf *);
293 int buf_syncwait(void);
294 u_long buf_memcalc(void);
295 int buf_drain(int);
296 int buf_setvalimit(vsize_t);
297 #ifdef DDB
298 void vfs_buf_print(struct buf *, int, void (*)(const char *, ...));
299 #endif
300
301 __END_DECLS
302 #endif
303 #endif /* !_SYS_BUF_H_ */
Cache object: f92fd55e59cde006ee837837b23f53f7
|