xref: /original-bsd/sys/sys/buf.h (revision 68549010)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)buf.h	7.14 (Berkeley) 01/14/92
8  */
9 
10 /*
11  * The header for buffers in the buffer pool and otherwise used
12  * to describe a block i/o request is given here.
13  *
14  * Each buffer in the pool is usually doubly linked into 2 lists:
15  * hashed into a chain by <dev,blkno> so it can be located in the cache,
16  * and (usually) on (one of several) queues.  These lists are circular and
17  * doubly linked for easy removal.
18  *
19  * There are currently three queues for buffers:
20  *	one for buffers which must be kept permanently (super blocks)
21  * 	one for buffers containing ``useful'' information (the cache)
22  *	one for buffers containing ``non-useful'' information
23  *		(and empty buffers, pushed onto the front)
24  * The latter two queues contain the buffers which are available for
25  * reallocation, are kept in lru order.  When not on one of these queues,
26  * the buffers are ``checked out'' to drivers which use the available list
27  * pointers to keep track of them in their i/o active queues.
28  */
29 
30 /*
31  * Bufhd structures used at the head of the hashed buffer queues.
32  * We only need three words for these, so this abbreviated
33  * definition saves some space.
34  */
35 struct bufhd
36 {
37 	long	b_flags;		/* see defines below */
38 	struct	buf *b_forw, *b_back;	/* fwd/bkwd pointer in chain */
39 };
40 struct buf
41 {
42 	long	b_flags;		/* too much goes here to describe */
43 	struct	buf *b_forw, *b_back;	/* hash chain (2 way street) */
44 	struct	buf *av_forw, *av_back;	/* position on free list if not BUSY */
45 	struct	buf *b_blockf, **b_blockb;/* associated vnode */
46 #define	b_actf	av_forw			/* alternate names for driver queue */
47 #define	b_actl	av_back			/*    head - isn't history wonderful */
48 	long	b_bcount;		/* transfer count */
49 	long	b_bufsize;		/* size of allocated buffer */
50 #define	b_active b_bcount		/* driver queue head: drive active */
51 	short	b_error;		/* returned after I/O */
52 	dev_t	b_dev;			/* major+minor device name */
53 	union {
54 	    caddr_t b_addr;		/* low order core address */
55 	    int	*b_words;		/* words for clearing */
56 	    struct fs *b_fs;		/* UFS superblocks */
57 	    struct lfs *b_lfs;		/* LFS superblocks */
58 	    struct csum *b_cs;		/* superblock summary information */
59 	    struct cg *b_cg;		/* cylinder group block */
60 	    struct dinode *b_dino;	/* ilist */
61 	    daddr_t *b_daddr;		/* indirect block */
62 	} b_un;
63 	daddr_t	b_lblkno;		/* logical block number */
64 	daddr_t	b_blkno;		/* block # on device */
65 	long	b_resid;		/* words not transferred after error */
66 #define	b_errcnt b_resid		/* while i/o in progress: # retries */
67 	struct  proc *b_proc;		/* proc doing physical or swap I/O */
68 	int	(*b_iodone)();		/* function called by iodone */
69 	struct	vnode *b_vp;		/* vnode for dev */
70 	int	b_pfcent;		/* center page when swapping cluster */
71 	struct	ucred *b_rcred;		/* ref to read credentials */
72 	struct	ucred *b_wcred;		/* ref to write credendtials */
73 	int	b_dirtyoff;		/* offset in buffer of dirty region */
74 	int	b_dirtyend;		/* offset of end of dirty region */
75 	caddr_t	b_saveaddr;		/* original b_addr for PHYSIO */
76 	int	b_validoff;		/* offset in buffer of valid region */
77 	int	b_validend;		/* offset of end of valid region */
78 };
79 
80 #define	BQUEUES		4		/* number of free buffer queues */
81 
82 #define	BQ_LOCKED	0		/* super-blocks &c */
83 #define	BQ_LRU		1		/* lru, useful buffers */
84 #define	BQ_AGE		2		/* rubbish */
85 #define	BQ_EMPTY	3		/* buffer headers with no memory */
86 
87 #ifdef	KERNEL
88 #define	BUFHSZ	512
89 #define RND	(MAXBSIZE/DEV_BSIZE)
90 #if	((BUFHSZ&(BUFHSZ-1)) == 0)
91 #define	BUFHASH(dvp, dblkno)	\
92 	((struct buf *)&bufhash[((int)(dvp)+(((int)(dblkno))/RND))&(BUFHSZ-1)])
93 #else
94 #define	BUFHASH(dvp, dblkno)	\
95 	((struct buf *)&bufhash[((int)(dvp)+(((int)(dblkno))/RND)) % BUFHSZ])
96 #endif
97 
98 struct	buf *buf;		/* the buffer pool itself */
99 char	*buffers;
100 int	nbuf;			/* number of buffer headers */
101 int	bufpages;		/* number of memory pages in the buffer pool */
102 struct	buf *swbuf;		/* swap I/O headers */
103 int	nswbuf;
104 struct	bufhd bufhash[BUFHSZ];	/* heads of hash lists */
105 struct	buf bfreelist[BQUEUES];	/* heads of available lists */
106 struct	buf bswlist;		/* head of free swap header list */
107 struct	buf *bclnlist;		/* head of cleaned page list */
108 
109 __BEGIN_DECLS
110 int	allocbuf __P((struct buf *, int));
111 int	bawrite __P((struct buf *));
112 int	bdwrite __P((struct buf *));
113 void	biodone __P((struct buf *));
114 int	biowait __P((struct buf *));
115 int	bread __P((struct vnode *, daddr_t, int,
116 	    struct ucred *, struct buf **));
117 int	breadn __P((struct vnode *, daddr_t, int, daddr_t *, int *, int,
118 	    struct ucred *, struct buf **));
119 int	brelse __P((struct buf *));
120 void	bufinit __P((void));
121 int	bwrite __P((struct buf *));
122 struct buf *getblk __P((struct vnode *, daddr_t, int));
123 struct buf *geteblk __P((int));
124 struct buf *getnewbuf __P((void));
125 int	incore __P((struct vnode *, daddr_t));
126 u_int	minphys __P((struct buf *bp));
127 __END_DECLS
128 #endif
129 
130 /*
131  * These flags are kept in b_flags.
132  */
133 #define	B_WRITE		0x000000	/* non-read pseudo-flag */
134 #define	B_READ		0x000001	/* read when I/O occurs */
135 #define	B_DONE		0x000002	/* transaction finished */
136 #define	B_ERROR		0x000004	/* transaction aborted */
137 #define	B_BUSY		0x000008	/* not on av_forw/back list */
138 #define	B_PHYS		0x000010	/* physical IO */
139 #define	B_XXX		0x000020	/* was B_MAP, alloc UNIBUS on pdp-11 */
140 #define	B_WANTED	0x000040	/* issue wakeup when BUSY goes off */
141 #define	B_AGE		0x000080	/* delayed write for correct aging */
142 #define	B_ASYNC		0x000100	/* don't wait for I/O completion */
143 #define	B_DELWRI	0x000200	/* write at exit of avail list */
144 #define	B_TAPE		0x000400	/* this is a magtape (no bdwrite) */
145 #define	B_UAREA		0x000800	/* add u-area to a swap operation */
146 #define	B_PAGET		0x001000	/* page in/out of page table space */
147 #define	B_DIRTY		0x002000	/* dirty page to be pushed out async */
148 #define	B_PGIN		0x004000	/* pagein op, so swap() can count it */
149 #define	B_CACHE		0x008000	/* did bread find us in the cache ? */
150 #define	B_INVAL		0x010000	/* does not contain valid info  */
151 #define	B_LOCKED	0x020000	/* locked in core (not reusable) */
152 #define	B_HEAD		0x040000	/* a buffer header, not a buffer */
153 #define	B_BAD		0x100000	/* bad block revectoring in progress */
154 #define	B_CALL		0x200000	/* call b_iodone from iodone */
155 #define	B_RAW		0x400000	/* set by physio for raw transfers */
156 #define	B_NOCACHE	0x800000	/* do not cache block after use */
157 
158 /*
159  * Insq/Remq for the buffer hash lists.
160  */
161 #define	bremhash(bp) { \
162 	(bp)->b_back->b_forw = (bp)->b_forw; \
163 	(bp)->b_forw->b_back = (bp)->b_back; \
164 }
165 #define	binshash(bp, dp) { \
166 	(bp)->b_forw = (dp)->b_forw; \
167 	(bp)->b_back = (dp); \
168 	(dp)->b_forw->b_back = (bp); \
169 	(dp)->b_forw = (bp); \
170 }
171 
172 /*
173  * Insq/Remq for the buffer free lists.
174  */
175 #define	bremfree(bp) { \
176 	(bp)->av_back->av_forw = (bp)->av_forw; \
177 	(bp)->av_forw->av_back = (bp)->av_back; \
178 }
179 #define	binsheadfree(bp, dp) { \
180 	(dp)->av_forw->av_back = (bp); \
181 	(bp)->av_forw = (dp)->av_forw; \
182 	(dp)->av_forw = (bp); \
183 	(bp)->av_back = (dp); \
184 }
185 #define	binstailfree(bp, dp) { \
186 	(dp)->av_back->av_forw = (bp); \
187 	(bp)->av_back = (dp)->av_back; \
188 	(dp)->av_back = (bp); \
189 	(bp)->av_forw = (dp); \
190 }
191 
192 #define	iodone	biodone
193 #define	iowait	biowait
194 
195 /*
196  * Zero out a buffer's data portion.
197  */
198 #define	clrbuf(bp) { \
199 	blkclr((bp)->b_un.b_addr, (unsigned)(bp)->b_bcount); \
200 	(bp)->b_resid = 0; \
201 }
202 #define B_CLRBUF	0x1	/* request allocated buffer be cleared */
203 #define B_SYNC		0x2	/* do all allocations synchronously */
204