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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)buf.h 8.9 (Berkeley) 3/30/95
45 #include <sys/queue.h>
53 * To avoid including <ufs/ffs/softdep.h>
55 LIST_HEAD(workhead, worklist);
57 * These are currently used only by the soft dependency code, hence
58 * are stored once in a global variable. If other subsystems wanted
59 * to use these hooks, a pointer to a set of bio_ops could be added
62 extern struct bio_ops {
63 void (*io_start) __P((struct buf *));
64 void (*io_complete) __P((struct buf *));
65 void (*io_deallocate) __P((struct buf *));
66 int (*io_fsync) __P((struct vnode *));
67 int (*io_sync) __P((struct mount *));
68 void (*io_movedeps) __P((struct buf *, struct buf *));
69 int (*io_countdeps) __P((struct buf *, int));
74 void (*ic_prev_iodone) __P((struct buf *));
75 void *ic_prev_iodone_chain;
83 * The buffer header describes an I/O operation in the kernel.
86 * b_bufsize, b_bcount. b_bufsize is the allocation size of the
87 * buffer, either DEV_BSIZE or PAGE_SIZE aligned. b_bcount is the
88 * originally requested buffer size and can serve as a bounds check
89 * against EOF. For most, but not all uses, b_bcount == b_bufsize.
91 * b_dirtyoff, b_dirtyend. Buffers support piecemeal, unaligned
92 * ranges of dirty data that need to be written to backing store.
93 * The range is typically clipped at b_bcount ( not b_bufsize ).
95 * b_resid. Number of bytes remaining in I/O. After an I/O operation
96 * completes, b_resid is usually 0 indicating 100% success.
99 LIST_ENTRY(buf) b_hash; /* Hash chain. */
100 TAILQ_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
101 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
102 TAILQ_ENTRY(buf) b_act; /* Device driver queue when active. *new* */
103 long b_flags; /* B_* flags. */
104 unsigned short b_qindex; /* buffer queue index */
105 unsigned char b_xflags; /* extra flags */
106 struct lock b_lock; /* Buffer lock */
107 int b_error; /* Errno value. */
108 long b_bufsize; /* Allocated buffer size. */
109 long b_bcount; /* Valid bytes in buffer. */
110 long b_resid; /* Remaining I/O. */
111 dev_t b_dev; /* Device associated with buffer. */
112 caddr_t b_data; /* Memory, superblocks, indirect etc. */
113 caddr_t b_kvabase; /* base kva for buffer */
114 int b_kvasize; /* size of kva for buffer */
115 daddr_t b_lblkno; /* Logical block number. */
116 daddr_t b_blkno; /* Underlying physical block number. */
117 off_t b_offset; /* Offset into file */
118 /* Function to call upon completion. */
119 void (*b_iodone) __P((struct buf *));
120 /* For nested b_iodone's. */
121 struct iodone_chain *b_iodone_chain;
122 struct vnode *b_vp; /* Device vnode. */
123 int b_dirtyoff; /* Offset in buffer of dirty region. */
124 int b_dirtyend; /* Offset of end of dirty region. */
125 struct ucred *b_rcred; /* Read credentials reference. */
126 struct ucred *b_wcred; /* Write credentials reference. */
127 daddr_t b_pblkno; /* physical block number */
128 void *b_saveaddr; /* Original b_addr for physio. */
129 void *b_driver1; /* for private use by the driver */
130 void *b_driver2; /* for private use by the driver */
131 void *b_caller1; /* for private use by the caller */
132 void *b_caller2; /* for private use by the caller */
138 TAILQ_HEAD(cluster_list_head, buf) cluster_head;
139 TAILQ_ENTRY(buf) cluster_entry;
141 struct vm_page *b_pages[btoc(MAXPHYS)];
143 struct workhead b_dep; /* List of filesystem dependencies. */
144 struct chain_info { /* buffer chaining */
150 #define b_spc b_pager.pg_spc
153 * These flags are kept in b_flags.
157 * B_ASYNC VOP calls on bp's are usually async whether or not
158 * B_ASYNC is set, but some subsystems, such as NFS, like
159 * to know what is best for the caller so they can
162 * B_PAGING Indicates that bp is being used by the paging system or
163 * some paging system and that the bp is not linked into
164 * the b_vp's clean/dirty linked lists or ref counts.
165 * Buffer vp reassignments are illegal in this case.
167 * B_CACHE This may only be set if the buffer is entirely valid.
168 * The situation where B_DELWRI is set and B_CACHE is
169 * clear MUST be committed to disk by getblk() so
170 * B_DELWRI can also be cleared. See the comments for
171 * getblk() in kern/vfs_bio.c. If B_CACHE is clear,
172 * the caller is expected to clear B_ERROR|B_INVAL,
173 * set B_READ, and initiate an I/O.
175 * The 'entire buffer' is defined to be the range from
176 * 0 through b_bcount.
178 * B_MALLOC Request that the buffer be allocated from the malloc
179 * pool, DEV_BSIZE aligned instead of PAGE_SIZE aligned.
181 * B_CLUSTEROK This flag is typically set for B_DELWRI buffers
182 * by filesystems that allow clustering when the buffer
183 * is fully dirty and indicates that it may be clustered
184 * with other adjacent dirty buffers. Note the clustering
185 * may not be used with the stage 1 data write under NFS
186 * but may be used for the commit rpc portion.
188 * B_VMIO Indicates that the buffer is tied into an VM object.
189 * The buffer's data is always PAGE_SIZE aligned even
190 * if b_bufsize and b_bcount are not. ( b_bufsize is
191 * always at least DEV_BSIZE aligned, though ).
195 #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
196 #define B_NEEDCOMMIT 0x00000002 /* Append-write in progress. */
197 #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
198 #define B_UNUSED0 0x00000008 /* Old B_BAD */
199 #define B_DEFERRED 0x00000010 /* Skipped over for cleaning */
200 #define B_CACHE 0x00000020 /* Bread found us in the cache. */
201 #define B_CALL 0x00000040 /* Call b_iodone from biodone. */
202 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
203 #define B_FREEBUF 0x00000100 /* Instruct driver: free blocks */
204 #define B_DONE 0x00000200 /* I/O completed. */
205 #define B_EINTR 0x00000400 /* I/O was interrupted */
206 #define B_ERROR 0x00000800 /* I/O error occurred. */
207 #define B_SCANNED 0x00001000 /* VOP_FSYNC funcs mark written bufs */
208 #define B_INVAL 0x00002000 /* Does not contain valid info. */
209 #define B_LOCKED 0x00004000 /* Locked in core (not reusable). */
210 #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
211 #define B_MALLOC 0x00010000 /* malloced b_data */
212 #define B_CLUSTEROK 0x00020000 /* Pagein op, so swap() can count it. */
213 #define B_PHYS 0x00040000 /* I/O to user memory. */
214 #define B_RAW 0x00080000 /* Set by physio for raw transfers. */
215 #define B_READ 0x00100000 /* Read buffer. */
216 #define B_DIRTY 0x00200000 /* Needs writing later. */
217 #define B_RELBUF 0x00400000 /* Release VMIO buffer. */
218 #define B_WANT 0x00800000 /* Used by vm_pager.c */
219 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
220 #define B_WRITEINPROG 0x01000000 /* Write in progress. */
221 #define B_XXX 0x02000000 /* Debugging flag. */
222 #define B_PAGING 0x04000000 /* volatile paging I/O -- bypass VMIO */
223 #define B_ORDERED 0x08000000 /* Must guarantee I/O ordering */
224 #define B_RAM 0x10000000 /* Read ahead mark (flag) */
225 #define B_VMIO 0x20000000 /* VMIO flag */
226 #define B_CLUSTER 0x40000000 /* pagein op, so swap() can count it */
227 #define B_AUTOCHAINDONE 0x80000000 /* Available flag */
229 #define PRINT_BUF_FLAGS "\20\40autochain\37cluster\36vmio\35ram\34ordered" \
230 "\33paging\32xxx\31writeinprog\30want\27relbuf\26dirty" \
231 "\25read\24raw\23phys\22clusterok\21malloc\20nocache" \
232 "\17locked\16inval\15scanned\14error\13eintr\12done\11freebuf" \
233 "\10delwri\7call\6cache\4bad\3async\2needcommit\1age"
236 * These flags are kept in b_xflags.
238 #define BX_VNDIRTY 0x00000001 /* On vnode dirty list */
239 #define BX_VNCLEAN 0x00000002 /* On vnode clean list */
240 #define BX_BKGRDWRITE 0x00000004 /* Do writes in background */
241 #define BX_BKGRDINPROG 0x00000008 /* Background write in progress */
242 #define BX_BKGRDWAIT 0x00000010 /* Background write waiting */
244 #define NOOFFSET (-1LL) /* No buffer offset calculated yet */
250 struct simplelock buftimelock; /* Interlock on setting prio and timo */
251 extern char *buf_wmesg; /* Default buffer lock message */
252 #define BUF_WMESG "bufwait"
253 #include <sys/proc.h> /* XXX for curproc */
257 #define BUF_LOCKINIT(bp) \
258 lockinit(&(bp)->b_lock, PRIBIO + 4, buf_wmesg, 0, 0)
261 * Get a lock sleeping non-interruptably until it becomes available.
263 static __inline int BUF_LOCK __P((struct buf *, int));
265 BUF_LOCK(struct buf *bp, int locktype)
270 simple_lock(&buftimelock);
271 locktype |= LK_INTERLOCK;
272 bp->b_lock.lk_wmesg = buf_wmesg;
273 bp->b_lock.lk_prio = PRIBIO + 4;
274 bp->b_lock.lk_timo = 0;
275 ret = lockmgr(&(bp)->b_lock, locktype, &buftimelock, curproc);
280 * Get a lock sleeping with specified interruptably and timeout.
282 static __inline int BUF_TIMELOCK __P((struct buf *, int, char *, int, int));
284 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int catch, int timo)
289 simple_lock(&buftimelock);
290 locktype |= LK_INTERLOCK;
291 bp->b_lock.lk_wmesg = wmesg;
292 bp->b_lock.lk_prio = (PRIBIO + 4) | catch;
293 bp->b_lock.lk_timo = timo;
294 ret = lockmgr(&(bp)->b_lock, (locktype), &buftimelock, curproc);
299 * Release a lock. Only the acquiring process may free the lock unless
300 * it has been handed off to biodone.
302 static __inline void BUF_UNLOCK __P((struct buf *));
304 BUF_UNLOCK(struct buf *bp)
309 lockmgr(&(bp)->b_lock, LK_RELEASE, NULL, curproc);
314 * Free a buffer lock.
316 #define BUF_LOCKFREE(bp) \
317 if (BUF_REFCNT(bp) > 0) \
318 panic("free locked buf")
320 * When initiating asynchronous I/O, change ownership of the lock to the
321 * kernel. Once done, the lock may legally released by biodone. The
322 * original owning process can no longer acquire it recursively, but must
323 * wait until the I/O is completed and the lock has been freed by biodone.
325 static __inline void BUF_KERNPROC __P((struct buf *));
327 BUF_KERNPROC(struct buf *bp)
329 struct proc *p = curproc;
331 if (p != NULL && bp->b_lock.lk_lockholder == p->p_pid)
333 bp->b_lock.lk_lockholder = LK_KERNPROC;
336 * Find out the number of references to a lock.
338 static __inline int BUF_REFCNT __P((struct buf *));
340 BUF_REFCNT(struct buf *bp)
345 ret = lockcount(&(bp)->b_lock);
352 struct buf_queue_head {
353 TAILQ_HEAD(buf_queue, buf) queue;
355 struct buf *insert_point;
356 struct buf *switch_point;
360 * This structure describes a clustered I/O. It is stored in the b_saveaddr
361 * field of the buffer on which I/O is done. At I/O completion, cluster
362 * callback uses the structure to parcel I/O's to individual buffers, and
363 * then free's this structure.
365 struct cluster_save {
366 long bs_bcount; /* Saved b_bcount. */
367 long bs_bufsize; /* Saved b_bufsize. */
368 void *bs_saveaddr; /* Saved b_addr. */
369 int bs_nchildren; /* Number of associated buffers. */
370 struct buf **bs_children; /* List of associated buffers. */
374 static __inline void bufq_init __P((struct buf_queue_head *head));
376 static __inline void bufq_insert_tail __P((struct buf_queue_head *head,
379 static __inline void bufq_remove __P((struct buf_queue_head *head,
382 static __inline struct buf *bufq_first __P((struct buf_queue_head *head));
385 bufq_init(struct buf_queue_head *head)
387 TAILQ_INIT(&head->queue);
388 head->last_pblkno = 0;
389 head->insert_point = NULL;
390 head->switch_point = NULL;
394 bufq_insert_tail(struct buf_queue_head *head, struct buf *bp)
396 if ((bp->b_flags & B_ORDERED) != 0) {
397 head->insert_point = bp;
398 head->switch_point = NULL;
400 TAILQ_INSERT_TAIL(&head->queue, bp, b_act);
404 bufq_remove(struct buf_queue_head *head, struct buf *bp)
406 if (bp == head->switch_point)
407 head->switch_point = TAILQ_NEXT(bp, b_act);
408 if (bp == head->insert_point) {
409 head->insert_point = TAILQ_PREV(bp, buf_queue, b_act);
410 if (head->insert_point == NULL)
411 head->last_pblkno = 0;
412 } else if (bp == TAILQ_FIRST(&head->queue))
413 head->last_pblkno = bp->b_pblkno;
414 TAILQ_REMOVE(&head->queue, bp, b_act);
415 if (TAILQ_FIRST(&head->queue) == head->switch_point)
416 head->switch_point = NULL;
419 static __inline struct buf *
420 bufq_first(struct buf_queue_head *head)
422 return (TAILQ_FIRST(&head->queue));
428 * Definitions for the buffer free lists.
430 #define BUFFER_QUEUES 6 /* number of free buffer queues */
432 #define QUEUE_NONE 0 /* on no queue */
433 #define QUEUE_LOCKED 1 /* locked buffers */
434 #define QUEUE_CLEAN 2 /* non-B_DELWRI buffers */
435 #define QUEUE_DIRTY 3 /* B_DELWRI buffers */
436 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
437 #define QUEUE_EMPTY 5 /* empty buffer headers */
440 * Zero out the buffer's data area.
442 #define clrbuf(bp) { \
443 bzero((bp)->b_data, (u_int)(bp)->b_bcount); \
447 /* Flags to low-level allocation routines. */
448 #define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
449 #define B_SYNC 0x02 /* Do all allocations synchronously. */
452 extern int nbuf; /* The number of buffer headers */
453 extern int buf_maxio; /* nominal maximum I/O for buffer */
454 extern struct buf *buf; /* The buffer headers. */
455 extern char *buffers; /* The buffer contents. */
456 extern int bufpages; /* Number of memory pages in the buffer pool. */
457 extern struct buf *swbuf; /* Swap I/O buffer headers. */
458 extern int nswbuf; /* Number of swap I/O buffer headers. */
459 extern TAILQ_HEAD(swqueue, buf) bswlist;
460 extern TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES];
464 caddr_t bufhashinit __P((caddr_t));
465 void bufinit __P((void));
466 void bwillwrite __P((void));
467 void bremfree __P((struct buf *));
468 int bread __P((struct vnode *, daddr_t, int,
469 struct ucred *, struct buf **));
470 int breadn __P((struct vnode *, daddr_t, int, daddr_t *, int *, int,
471 struct ucred *, struct buf **));
472 int bwrite __P((struct buf *));
473 void bdwrite __P((struct buf *));
474 void bawrite __P((struct buf *));
475 void bdirty __P((struct buf *));
476 void bundirty __P((struct buf *));
477 int bowrite __P((struct buf *));
478 void brelse __P((struct buf *));
479 void bqrelse __P((struct buf *));
480 int vfs_bio_awrite __P((struct buf *));
481 struct buf * getpbuf __P((int *));
482 struct buf *incore __P((struct vnode *, daddr_t));
483 struct buf *gbincore __P((struct vnode *, daddr_t));
484 int inmem __P((struct vnode *, daddr_t));
485 struct buf *getblk __P((struct vnode *, daddr_t, int, int, int));
486 struct buf *geteblk __P((int));
487 int biowait __P((struct buf *));
488 void biodone __P((struct buf *));
490 void cluster_callback __P((struct buf *));
491 int cluster_read __P((struct vnode *, u_quad_t, daddr_t, long,
492 struct ucred *, long, int, struct buf **));
493 int cluster_wbuild __P((struct vnode *, long, daddr_t, int));
494 void cluster_write __P((struct buf *, u_quad_t));
495 int physio __P((dev_t dev, struct uio *uio, int ioflag));
496 #define physread physio
497 #define physwrite physio
498 void vfs_bio_set_validclean __P((struct buf *, int base, int size));
499 void vfs_bio_clrbuf __P((struct buf *));
500 void vfs_busy_pages __P((struct buf *, int clear_modify));
501 void vfs_unbusy_pages __P((struct buf *));
502 void vwakeup __P((struct buf *));
503 void vmapbuf __P((struct buf *));
504 void vunmapbuf __P((struct buf *));
505 void relpbuf __P((struct buf *, int *));
506 void brelvp __P((struct buf *));
507 void bgetvp __P((struct vnode *, struct buf *));
508 void pbgetvp __P((struct vnode *, struct buf *));
509 void pbrelvp __P((struct buf *));
510 int allocbuf __P((struct buf *bp, int size));
511 void reassignbuf __P((struct buf *, struct vnode *));
512 void pbreassignbuf __P((struct buf *, struct vnode *));
513 struct buf *trypbuf __P((int *));
517 #endif /* !_SYS_BUF_H_ */