xref: /original-bsd/sys/sys/buf.h (revision 58b1b499)
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  * %sccs.include.redist.c%
11  *
12  *	@(#)buf.h	8.9 (Berkeley) 03/30/95
13  */
14 
15 #ifndef _SYS_BUF_H_
16 #define	_SYS_BUF_H_
17 #include <sys/queue.h>
18 
19 #define NOLIST ((struct buf *)0x87654321)
20 
21 /*
22  * The buffer header describes an I/O operation in the kernel.
23  */
24 struct buf {
25 	LIST_ENTRY(buf) b_hash;		/* Hash chain. */
26 	LIST_ENTRY(buf) b_vnbufs;	/* Buffer's associated vnode. */
27 	TAILQ_ENTRY(buf) b_freelist;	/* Free list position if not active. */
28 	struct	buf *b_actf, **b_actb;	/* Device driver queue when active. */
29 	struct  proc *b_proc;		/* Associated proc; NULL if kernel. */
30 	volatile long	b_flags;	/* B_* flags. */
31 	int	b_error;		/* Errno value. */
32 	long	b_bufsize;		/* Allocated buffer size. */
33 	long	b_bcount;		/* Valid bytes in buffer. */
34 	long	b_resid;		/* Remaining I/O. */
35 	dev_t	b_dev;			/* Device associated with buffer. */
36 	struct {
37 		caddr_t	b_addr;		/* Memory, superblocks, indirect etc. */
38 	} b_un;
39 	void	*b_saveaddr;		/* Original b_addr for physio. */
40 	daddr_t	b_lblkno;		/* Logical block number. */
41 	daddr_t	b_blkno;		/* Underlying physical block number. */
42 					/* Function to call upon completion. */
43 	void	(*b_iodone) __P((struct buf *));
44 	struct	vnode *b_vp;		/* Device vnode. */
45 	long	b_pfcent;		/* Center page when swapping cluster. */
46 					/* XXX pfcent should be int; overld. */
47 	int	b_dirtyoff;		/* Offset in buffer of dirty region. */
48 	int	b_dirtyend;		/* Offset of end of dirty region. */
49 	struct	ucred *b_rcred;		/* Read credentials reference. */
50 	struct	ucred *b_wcred;		/* Write credentials reference. */
51 	int	b_validoff;		/* Offset in buffer of valid region. */
52 	int	b_validend;		/* Offset of end of valid region. */
53 };
54 
55 /* Device driver compatibility definitions. */
56 #define	b_active b_bcount		/* Driver queue head: drive active. */
57 #define	b_data	 b_un.b_addr		/* b_un.b_addr is not changeable. */
58 #define	b_errcnt b_resid		/* Retry count while I/O in progress. */
59 #define	iodone	 biodone		/* Old name for biodone. */
60 #define	iowait	 biowait		/* Old name for biowait. */
61 
62 /*
63  * These flags are kept in b_flags.
64  */
65 #define	B_AGE		0x00000001	/* Move to age queue when I/O done. */
66 #define	B_NEEDCOMMIT	0x00000002	/* Append-write in progress. */
67 #define	B_ASYNC		0x00000004	/* Start I/O, do not wait. */
68 #define	B_BAD		0x00000008	/* Bad block revectoring in progress. */
69 #define	B_BUSY		0x00000010	/* I/O in progress. */
70 #define	B_CACHE		0x00000020	/* Bread found us in the cache. */
71 #define	B_CALL		0x00000040	/* Call b_iodone from biodone. */
72 #define	B_DELWRI	0x00000080	/* Delay I/O until buffer reused. */
73 #define	B_DIRTY		0x00000100	/* Dirty page to be pushed out async. */
74 #define	B_DONE		0x00000200	/* I/O completed. */
75 #define	B_EINTR		0x00000400	/* I/O was interrupted */
76 #define	B_ERROR		0x00000800	/* I/O error occurred. */
77 #define	B_GATHERED	0x00001000	/* LFS: already in a segment. */
78 #define	B_INVAL		0x00002000	/* Does not contain valid info. */
79 #define	B_LOCKED	0x00004000	/* Locked in core (not reusable). */
80 #define	B_NOCACHE	0x00008000	/* Do not cache block after use. */
81 #define	B_PAGET		0x00010000	/* Page in/out of page table space. */
82 #define	B_PGIN		0x00020000	/* Pagein op, so swap() can count it. */
83 #define	B_PHYS		0x00040000	/* I/O to user memory. */
84 #define	B_RAW		0x00080000	/* Set by physio for raw transfers. */
85 #define	B_READ		0x00100000	/* Read buffer. */
86 #define	B_TAPE		0x00200000	/* Magnetic tape I/O. */
87 #define	B_UAREA		0x00400000	/* Buffer describes Uarea I/O. */
88 #define	B_WANTED	0x00800000	/* Process wants this buffer. */
89 #define	B_WRITE		0x00000000	/* Write buffer (pseudo flag). */
90 #define	B_WRITEINPROG	0x01000000	/* Write in progress. */
91 #define	B_XXX		0x02000000	/* Debugging flag. */
92 
93 /*
94  * This structure describes a clustered I/O.  It is stored in the b_saveaddr
95  * field of the buffer on which I/O is done.  At I/O completion, cluster
96  * callback uses the structure to parcel I/O's to individual buffers, and
97  * then free's this structure.
98  */
99 struct cluster_save {
100 	long	bs_bcount;		/* Saved b_bcount. */
101 	long	bs_bufsize;		/* Saved b_bufsize. */
102 	void	*bs_saveaddr;		/* Saved b_addr. */
103 	int	bs_nchildren;		/* Number of associated buffers. */
104 	struct buf **bs_children;	/* List of associated buffers. */
105 };
106 
107 /*
108  * Zero out the buffer's data area.
109  */
110 #define	clrbuf(bp) {							\
111 	bzero((bp)->b_data, (u_int)(bp)->b_bcount);			\
112 	(bp)->b_resid = 0;						\
113 }
114 
115 /* Flags to low-level allocation routines. */
116 #define B_CLRBUF	0x01	/* Request allocated buffer be cleared. */
117 #define B_SYNC		0x02	/* Do all allocations synchronously. */
118 
119 #ifdef KERNEL
120 int	nbuf;			/* The number of buffer headers */
121 struct	buf *buf;		/* The buffer headers. */
122 char	*buffers;		/* The buffer contents. */
123 int	bufpages;		/* Number of memory pages in the buffer pool. */
124 struct	buf *swbuf;		/* Swap I/O buffer headers. */
125 int	nswbuf;			/* Number of swap I/O buffer headers. */
126 struct	buf bswlist;		/* Head of swap I/O buffer headers free list. */
127 struct	buf *bclnlist;		/* Head of cleaned page list. */
128 
129 __BEGIN_DECLS
130 int	allocbuf __P((struct buf *, int));
131 void	bawrite __P((struct buf *));
132 void	bdwrite __P((struct buf *));
133 void	biodone __P((struct buf *));
134 int	biowait __P((struct buf *));
135 int	bread __P((struct vnode *, daddr_t, int,
136 	    struct ucred *, struct buf **));
137 int	breadn __P((struct vnode *, daddr_t, int, daddr_t *, int *, int,
138 	    struct ucred *, struct buf **));
139 void	brelse __P((struct buf *));
140 void	bufinit __P((void));
141 int	bwrite __P((struct buf *));
142 void	cluster_callback __P((struct buf *));
143 int	cluster_read __P((struct vnode *, u_quad_t, daddr_t, long,
144 	    struct ucred *, struct buf **));
145 void	cluster_write __P((struct buf *, u_quad_t));
146 struct buf *getblk __P((struct vnode *, daddr_t, int, int, int));
147 struct buf *geteblk __P((int));
148 struct buf *getnewbuf __P((int slpflag, int slptimeo));
149 struct buf *incore __P((struct vnode *, daddr_t));
150 u_int	minphys __P((struct buf *bp));
151 __END_DECLS
152 #endif
153 #endif /* !_SYS_BUF_H_ */
154