1 /* $OpenBSD: buf.h,v 1.45 2004/01/21 21:00:14 tedu Exp $ */
2 /* $NetBSD: buf.h,v 1.25 1997/04/09 21:12:17 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)buf.h 8.7 (Berkeley) 1/21/94
38 */
39
40 #ifndef _SYS_BUF_H_
41 #define _SYS_BUF_H_
42 #include <sys/queue.h>
43
44 #define NOLIST ((struct buf *)0x87654321)
45
46 struct buf;
47 struct vnode;
48
49 /*
50 * To avoid including <ufs/ffs/softdep.h>
51 */
52
53 LIST_HEAD(workhead, worklist);
54
55 /*
56 * These are currently used only by the soft dependency code, hence
57 * are stored once in a global variable. If other subsystems wanted
58 * to use these hooks, a pointer to a set of bio_ops could be added
59 * to each buffer.
60 */
61 extern struct bio_ops {
62 void (*io_start)(struct buf *);
63 void (*io_complete)(struct buf *);
64 void (*io_deallocate)(struct buf *);
65 void (*io_movedeps)(struct buf *, struct buf *);
66 int (*io_countdeps)(struct buf *, int, int);
67 } bioops;
68
69 /*
70 * The buffer header describes an I/O operation in the kernel.
71 */
72 struct buf {
73 LIST_ENTRY(buf) b_hash; /* Hash chain. */
74 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
75 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
76 TAILQ_ENTRY(buf) b_synclist; /* List of dirty buffers to be written out */
77 long b_synctime; /* Time this buffer should be flushed */
78 struct buf *b_actf, **b_actb; /* Device driver queue when active. */
79 struct proc *b_proc; /* Associated proc; NULL if kernel. */
80 volatile long b_flags; /* B_* flags. */
81 int b_error; /* Errno value. */
82 long b_bufsize; /* Allocated buffer size. */
83 long b_bcount; /* Valid bytes in buffer. */
84 size_t b_resid; /* Remaining I/O. */
85 dev_t b_dev; /* Device associated with buffer. */
86 struct {
87 caddr_t b_addr; /* Memory, superblocks, indirect etc. */
88 } b_un;
89 void *b_saveaddr; /* Original b_addr for physio. */
90 daddr_t b_lblkno; /* Logical block number. */
91 daddr_t b_blkno; /* Underlying physical block number. */
92 /* Function to call upon completion.
93 * Will be called at splbio(). */
94 void (*b_iodone)(struct buf *);
95 struct vnode *b_vp; /* Device vnode. */
96 int b_dirtyoff; /* Offset in buffer of dirty region. */
97 int b_dirtyend; /* Offset of end of dirty region. */
98 int b_validoff; /* Offset in buffer of valid region. */
99 int b_validend; /* Offset of end of valid region. */
100 struct workhead b_dep; /* List of filesystem dependencies. */
101 };
102
103 /*
104 * bufq
105 * flexible buffer queue routines
106 */
107 struct bufq {
108 void (*bufq_free)(struct bufq *);
109 void (*bufq_add)(struct bufq *, struct buf *);
110 struct buf *(*bufq_get)(struct bufq *);
111 };
112
113 struct bufq_default {
114 struct bufq bufq;
115 struct buf bufq_head[3];
116 };
117
118 #define BUFQ_ALLOC(_type) bufq_default_alloc() /* XXX */
119 #define BUFQ_FREE(_bufq) (_bufq)->bufq_free(_bufq)
120 #define BUFQ_ADD(_bufq, _bp) (_bufq)->bufq_add(_bufq, _bp)
121 #define BUFQ_GET(_bufq) (_bufq)->bufq_get(_bufq)
122
123 struct bufq *bufq_default_alloc(void);
124 void bufq_default_free(struct bufq *);
125 void bufq_default_add(struct bufq *, struct buf *);
126 struct buf *bufq_default_get(struct bufq *);
127
128 /*
129 * For portability with historic industry practice, the cylinder number has
130 * to be maintained in the `b_resid' field.
131 */
132 #define b_cylinder b_resid /* Cylinder number for disksort(). */
133
134 /* Device driver compatibility definitions. */
135 #define b_active b_bcount /* Driver queue head: drive active. */
136 #define b_data b_un.b_addr /* b_un.b_addr is not changeable. */
137 #define b_errcnt b_resid /* Retry count while I/O in progress. */
138
139 /*
140 * These flags are kept in b_flags.
141 */
142 #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
143 #define B_NEEDCOMMIT 0x00000002 /* Needs committing to stable storage */
144 #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
145 #define B_BAD 0x00000008 /* Bad block revectoring in progress. */
146 #define B_BUSY 0x00000010 /* I/O in progress. */
147 #define B_CACHE 0x00000020 /* Bread found us in the cache. */
148 #define B_CALL 0x00000040 /* Call b_iodone from biodone. */
149 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
150 #define B_DIRTY 0x00000100 /* Dirty page to be pushed out async. */
151 #define B_DONE 0x00000200 /* I/O completed. */
152 #define B_EINTR 0x00000400 /* I/O was interrupted */
153 #define B_ERROR 0x00000800 /* I/O error occurred. */
154 #define B_GATHERED 0x00001000 /* LFS: already in a segment. */
155 #define B_INVAL 0x00002000 /* Does not contain valid info. */
156 #define B_LOCKED 0x00004000 /* Locked in core (not reusable). */
157 #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
158 #define B_PAGET 0x00010000 /* Page in/out of page table space. */
159 #define B_PGIN 0x00020000 /* Pagein op, so swap() can count it. */
160 #define B_PHYS 0x00040000 /* I/O to user memory. */
161 #define B_RAW 0x00080000 /* Set by physio for raw transfers. */
162 #define B_READ 0x00100000 /* Read buffer. */
163 #define B_TAPE 0x00200000 /* Magnetic tape I/O. */
164 #define B_UAREA 0x00400000 /* Buffer describes Uarea I/O. */
165 #define B_WANTED 0x00800000 /* Process wants this buffer. */
166 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
167 #define B_WRITEINPROG 0x01000000 /* Write in progress. */
168 #define B_XXX 0x02000000 /* Debugging flag. */
169 #define B_DEFERRED 0x04000000 /* Skipped over for cleaning */
170 #define B_SCANNED 0x08000000 /* Block already pushed during sync */
171 #define B_PDAEMON 0x10000000 /* I/O started by pagedaemon */
172
173 /*
174 * This structure describes a clustered I/O. It is stored in the b_saveaddr
175 * field of the buffer on which I/O is done. At I/O completion, cluster
176 * callback uses the structure to parcel I/O's to individual buffers, and
177 * then free's this structure.
178 */
179 struct cluster_save {
180 long bs_bcount; /* Saved b_bcount. */
181 long bs_bufsize; /* Saved b_bufsize. */
182 void *bs_saveaddr; /* Saved b_addr. */
183 int bs_nchildren; /* Number of associated buffers. */
184 struct buf **bs_children; /* List of associated buffers. */
185 };
186
187 /*
188 * Zero out the buffer's data area.
189 */
190 #define clrbuf(bp) { \
191 bzero((bp)->b_data, (u_int)(bp)->b_bcount); \
192 (bp)->b_resid = 0; \
193 }
194
195
196 /* Flags to low-level allocation routines. */
197 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
198 #define B_SYNC 0x02 /* Do all allocations synchronously. */
199
200 struct cluster_info {
201 daddr_t ci_lastr; /* last read (read-ahead) */
202 daddr_t ci_lastw; /* last write (write cluster) */
203 daddr_t ci_cstart; /* start block of cluster */
204 daddr_t ci_lasta; /* last allocation */
205 int ci_clen; /* length of current cluster */
206 int ci_ralen; /* Read-ahead length */
207 daddr_t ci_maxra; /* last readahead block */
208 };
209
210 #ifdef _KERNEL
211 __BEGIN_DECLS
212 extern int nbuf; /* The number of buffer headers */
213 extern struct buf *buf; /* The buffer headers. */
214 extern char *buffers; /* The buffer contents. */
215 extern int bufpages; /* Number of memory pages in the buffer pool. */
216
217 extern struct pool bufpool;
218
219 void allocbuf(struct buf *, int);
220 void bawrite(struct buf *);
221 void bdwrite(struct buf *);
222 void biodone(struct buf *);
223 int biowait(struct buf *);
224 int bread(struct vnode *, daddr_t, int,
225 struct ucred *, struct buf **);
226 int breada(struct vnode *, daddr_t, int, daddr_t, int,
227 struct ucred *, struct buf **);
228 int breadn(struct vnode *, daddr_t, int, daddr_t *, int *, int,
229 struct ucred *, struct buf **);
230 void brelse(struct buf *);
231 void bremfree(struct buf *);
232 void bufinit(void);
233 void buf_dirty(struct buf *);
234 void buf_undirty(struct buf *);
235 int bwrite(struct buf *);
236 struct buf *getblk(struct vnode *, daddr_t, int, int, int);
237 struct buf *geteblk(int);
238 struct buf *incore(struct vnode *, daddr_t);
239
240 void minphys(struct buf *bp);
241 int physio(void (*strategy)(struct buf *), struct buf *bp, dev_t dev,
242 int flags, void (*minphys)(struct buf *), struct uio *uio);
243 void brelvp(struct buf *);
244 void reassignbuf(struct buf *);
245 void bgetvp(struct vnode *, struct buf *);
246
247 void buf_replacevnode(struct buf *, struct vnode *);
248 void buf_daemon(struct proc *);
249
250 #ifdef DEBUG
251 void buf_print(struct buf *);
252 #endif
253
254 static __inline void
buf_start(struct buf * bp)255 buf_start(struct buf *bp)
256 {
257 if (bioops.io_start)
258 (*bioops.io_start)(bp);
259 }
260
261 static __inline void
buf_complete(struct buf * bp)262 buf_complete(struct buf *bp)
263 {
264 if (bioops.io_complete)
265 (*bioops.io_complete)(bp);
266 }
267
268 static __inline void
buf_deallocate(struct buf * bp)269 buf_deallocate(struct buf *bp)
270 {
271 if (bioops.io_deallocate)
272 (*bioops.io_deallocate)(bp);
273 }
274
275 static __inline void
buf_movedeps(struct buf * bp,struct buf * bp2)276 buf_movedeps(struct buf *bp, struct buf *bp2)
277 {
278 if (bioops.io_movedeps)
279 (*bioops.io_movedeps)(bp, bp2);
280 }
281
282 static __inline int
buf_countdeps(struct buf * bp,int i,int islocked)283 buf_countdeps(struct buf *bp, int i, int islocked)
284 {
285 if (bioops.io_countdeps)
286 return ((*bioops.io_countdeps)(bp, i, islocked));
287 else
288 return (0);
289 }
290
291 int cluster_read(struct vnode *, struct cluster_info *,
292 u_quad_t, daddr_t, long, struct ucred *, struct buf **);
293 void cluster_write(struct buf *, struct cluster_info *, u_quad_t);
294
295 __END_DECLS
296 #endif
297 #endif /* !_SYS_BUF_H_ */
298