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