]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/bio.h
This commit was generated by cvs2svn to compensate for changes in r56545,
[FreeBSD/FreeBSD.git] / sys / sys / bio.h
1 /*
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  *      @(#)buf.h       8.9 (Berkeley) 3/30/95
39  * $FreeBSD$
40  */
41
42 #ifndef _SYS_BUF_H_
43 #define _SYS_BUF_H_
44
45 #include <sys/queue.h>
46 #include <sys/lock.h>
47
48 struct buf;
49 struct mount;
50 struct vnode;
51
52 /*
53  * To avoid including <ufs/ffs/softdep.h> 
54  */   
55 LIST_HEAD(workhead, worklist);
56 /*
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
60  * to each buffer.
61  */
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));
70 } bioops;
71
72 struct iodone_chain {
73         long    ic_prev_flags;
74         void    (*ic_prev_iodone) __P((struct buf *));
75         void    *ic_prev_iodone_chain;
76         struct {
77                 long    ia_long;
78                 void    *ia_ptr;
79         }       ic_args[5];
80 };
81
82 /*
83  * The buffer header describes an I/O operation in the kernel.
84  *
85  * NOTES:
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.
90  *
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 ).
94  *
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.
97  */
98 struct buf {
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 */
133         union   pager_info {
134                 void    *pg_spc;
135                 int     pg_reqpage;
136         } b_pager;
137         union   cluster_info {
138                 TAILQ_HEAD(cluster_list_head, buf) cluster_head;
139                 TAILQ_ENTRY(buf) cluster_entry;
140         } b_cluster;
141         struct  vm_page *b_pages[btoc(MAXPHYS)];
142         int             b_npages;
143         struct  workhead b_dep;         /* List of filesystem dependencies. */
144         struct chain_info {             /* buffer chaining */
145                 struct buf *parent;
146                 int count;
147         } b_chain;
148 };
149
150 #define b_spc   b_pager.pg_spc
151
152 /*
153  * These flags are kept in b_flags.
154  *
155  * Notes:
156  *
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
160  *                      optimize the I/O.
161  *
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.
166  *
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.
174  *
175  *                      The 'entire buffer' is defined to be the range from
176  *                      0 through b_bcount.
177  *
178  *      B_MALLOC        Request that the buffer be allocated from the malloc
179  *                      pool, DEV_BSIZE aligned instead of PAGE_SIZE aligned.
180  *
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.
187  *
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 ).
192  *      
193  */
194
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 */
228
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"
234
235 /*
236  * These flags are kept in b_xflags.
237  */
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 */
243
244 #define NOOFFSET        (-1LL)          /* No buffer offset calculated yet */
245
246 #ifdef _KERNEL
247 /*
248  * Buffer locking
249  */
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 */
254 /*
255  * Initialize a lock.
256  */
257 #define BUF_LOCKINIT(bp) \
258         lockinit(&(bp)->b_lock, PRIBIO + 4, buf_wmesg, 0, 0)
259 /*
260  *
261  * Get a lock sleeping non-interruptably until it becomes available.
262  */
263 static __inline int BUF_LOCK __P((struct buf *, int));
264 static __inline int
265 BUF_LOCK(struct buf *bp, int locktype)
266 {
267         int s, ret;
268
269         s = splbio();
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);
276         splx(s);
277         return ret;
278 }
279 /*
280  * Get a lock sleeping with specified interruptably and timeout.
281  */
282 static __inline int BUF_TIMELOCK __P((struct buf *, int, char *, int, int));
283 static __inline int
284 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int catch, int timo)
285 {
286         int s, ret;
287
288         s = splbio();
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);
295         splx(s);
296         return ret;
297 }
298 /*
299  * Release a lock. Only the acquiring process may free the lock unless
300  * it has been handed off to biodone.
301  */
302 static __inline void BUF_UNLOCK __P((struct buf *));
303 static __inline void
304 BUF_UNLOCK(struct buf *bp)
305 {
306         int s;
307
308         s = splbio();
309         lockmgr(&(bp)->b_lock, LK_RELEASE, NULL, curproc);
310         splx(s);
311 }
312
313 /*
314  * Free a buffer lock.
315  */
316 #define BUF_LOCKFREE(bp)                        \
317         if (BUF_REFCNT(bp) > 0)                 \
318                 panic("free locked buf")
319 /*
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.
324  */
325 static __inline void BUF_KERNPROC __P((struct buf *));
326 static __inline void
327 BUF_KERNPROC(struct buf *bp)
328 {
329         struct proc *p = curproc;
330
331         if (p != NULL && bp->b_lock.lk_lockholder == p->p_pid)
332                 p->p_locks--;
333         bp->b_lock.lk_lockholder = LK_KERNPROC;
334 }
335 /*
336  * Find out the number of references to a lock.
337  */
338 static __inline int BUF_REFCNT __P((struct buf *));
339 static __inline int
340 BUF_REFCNT(struct buf *bp)
341 {
342         int s, ret;
343
344         s = splbio();
345         ret = lockcount(&(bp)->b_lock);
346         splx(s);
347         return ret;
348 }
349
350 #endif /* _KERNEL */
351
352 struct buf_queue_head {
353         TAILQ_HEAD(buf_queue, buf) queue;
354         daddr_t last_pblkno;
355         struct  buf *insert_point;
356         struct  buf *switch_point;
357 };
358
359 /*
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.
364  */
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. */
371 };
372
373 #ifdef _KERNEL
374 static __inline void bufq_init __P((struct buf_queue_head *head));
375
376 static __inline void bufq_insert_tail __P((struct buf_queue_head *head,
377                                            struct buf *bp));
378
379 static __inline void bufq_remove __P((struct buf_queue_head *head,
380                                       struct buf *bp));
381
382 static __inline struct buf *bufq_first __P((struct buf_queue_head *head));
383
384 static __inline void
385 bufq_init(struct buf_queue_head *head)
386 {
387         TAILQ_INIT(&head->queue);
388         head->last_pblkno = 0;
389         head->insert_point = NULL;
390         head->switch_point = NULL;
391 }
392
393 static __inline void
394 bufq_insert_tail(struct buf_queue_head *head, struct buf *bp)
395 {
396         if ((bp->b_flags & B_ORDERED) != 0) {
397                 head->insert_point = bp;
398                 head->switch_point = NULL;
399         }
400         TAILQ_INSERT_TAIL(&head->queue, bp, b_act);
401 }
402
403 static __inline void
404 bufq_remove(struct buf_queue_head *head, struct buf *bp)
405 {
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;
417 }
418
419 static __inline struct buf *
420 bufq_first(struct buf_queue_head *head)
421 {
422         return (TAILQ_FIRST(&head->queue));
423 }
424
425 #endif /* _KERNEL */
426
427 /*
428  * Definitions for the buffer free lists.
429  */
430 #define BUFFER_QUEUES   6       /* number of free buffer queues */
431
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 */
438
439 /*
440  * Zero out the buffer's data area.
441  */
442 #define clrbuf(bp) {                                                    \
443         bzero((bp)->b_data, (u_int)(bp)->b_bcount);                     \
444         (bp)->b_resid = 0;                                              \
445 }
446
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. */
450
451 #ifdef _KERNEL
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];
461
462 struct uio;
463
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 *));
489
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 *));
514
515 #endif /* _KERNEL */
516
517 #endif /* !_SYS_BUF_H_ */