xref: /original-bsd/sys/sys/buf.h (revision 08eb28af)
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.11 (Berkeley) 05/09/90
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;		/* superblocks */
57 	    struct csum *b_cs;		/* superblock summary information */
58 	    struct cg *b_cg;		/* cylinder group block */
59 	    struct dinode *b_dino;	/* ilist */
60 	    daddr_t *b_daddr;		/* indirect block */
61 	} b_un;
62 	daddr_t	b_lblkno;		/* logical block number */
63 	daddr_t	b_blkno;		/* block # on device */
64 	long	b_resid;		/* words not transferred after error */
65 #define	b_errcnt b_resid		/* while i/o in progress: # retries */
66 	struct  proc *b_proc;		/* proc doing physical or swap I/O */
67 	int	(*b_iodone)();		/* function called by iodone */
68 	struct	vnode *b_vp;		/* vnode for dev */
69 	int	b_pfcent;		/* center page when swapping cluster */
70 	struct	ucred *b_rcred;		/* ref to read credentials */
71 	struct	ucred *b_wcred;		/* ref to write credendtials */
72 	int	b_dirtyoff;		/* offset in buffer of dirty region */
73 	int	b_dirtyend;		/* offset of end of dirty region */
74 	caddr_t	b_saveaddr;		/* original b_addr for PHYSIO */
75 };
76 
77 #define	BQUEUES		4		/* number of free buffer queues */
78 
79 #define	BQ_LOCKED	0		/* super-blocks &c */
80 #define	BQ_LRU		1		/* lru, useful buffers */
81 #define	BQ_AGE		2		/* rubbish */
82 #define	BQ_EMPTY	3		/* buffer headers with no memory */
83 
84 #ifdef	KERNEL
85 #define	BUFHSZ	512
86 #define RND	(MAXBSIZE/DEV_BSIZE)
87 #if	((BUFHSZ&(BUFHSZ-1)) == 0)
88 #define	BUFHASH(dvp, dblkno)	\
89 	((struct buf *)&bufhash[((int)(dvp)+(((int)(dblkno))/RND))&(BUFHSZ-1)])
90 #else
91 #define	BUFHASH(dvp, dblkno)	\
92 	((struct buf *)&bufhash[((int)(dvp)+(((int)(dblkno))/RND)) % BUFHSZ])
93 #endif
94 
95 struct	buf *buf;		/* the buffer pool itself */
96 char	*buffers;
97 int	nbuf;			/* number of buffer headers */
98 int	bufpages;		/* number of memory pages in the buffer pool */
99 struct	buf *swbuf;		/* swap I/O headers */
100 int	nswbuf;
101 struct	bufhd bufhash[BUFHSZ];	/* heads of hash lists */
102 struct	buf bfreelist[BQUEUES];	/* heads of available lists */
103 struct	buf bswlist;		/* head of free swap header list */
104 struct	buf *bclnlist;		/* head of cleaned page list */
105 
106 struct	buf *getblk();
107 struct	buf *geteblk();
108 struct	buf *getnewbuf();
109 
110 unsigned minphys();
111 #endif
112 
113 /*
114  * These flags are kept in b_flags.
115  */
116 #define	B_WRITE		0x000000	/* non-read pseudo-flag */
117 #define	B_READ		0x000001	/* read when I/O occurs */
118 #define	B_DONE		0x000002	/* transaction finished */
119 #define	B_ERROR		0x000004	/* transaction aborted */
120 #define	B_BUSY		0x000008	/* not on av_forw/back list */
121 #define	B_PHYS		0x000010	/* physical IO */
122 #define	B_XXX		0x000020	/* was B_MAP, alloc UNIBUS on pdp-11 */
123 #define	B_WANTED	0x000040	/* issue wakeup when BUSY goes off */
124 #define	B_AGE		0x000080	/* delayed write for correct aging */
125 #define	B_ASYNC		0x000100	/* don't wait for I/O completion */
126 #define	B_DELWRI	0x000200	/* write at exit of avail list */
127 #define	B_TAPE		0x000400	/* this is a magtape (no bdwrite) */
128 #define	B_UAREA		0x000800	/* add u-area to a swap operation */
129 #define	B_PAGET		0x001000	/* page in/out of page table space */
130 #define	B_DIRTY		0x002000	/* dirty page to be pushed out async */
131 #define	B_PGIN		0x004000	/* pagein op, so swap() can count it */
132 #define	B_CACHE		0x008000	/* did bread find us in the cache ? */
133 #define	B_INVAL		0x010000	/* does not contain valid info  */
134 #define	B_LOCKED	0x020000	/* locked in core (not reusable) */
135 #define	B_HEAD		0x040000	/* a buffer header, not a buffer */
136 #define	B_BAD		0x100000	/* bad block revectoring in progress */
137 #define	B_CALL		0x200000	/* call b_iodone from iodone */
138 #define	B_RAW		0x400000	/* set by physio for raw transfers */
139 #define	B_NOCACHE	0x800000	/* do not cache block after use */
140 
141 /*
142  * Insq/Remq for the buffer hash lists.
143  */
144 #define	bremhash(bp) { \
145 	(bp)->b_back->b_forw = (bp)->b_forw; \
146 	(bp)->b_forw->b_back = (bp)->b_back; \
147 }
148 #define	binshash(bp, dp) { \
149 	(bp)->b_forw = (dp)->b_forw; \
150 	(bp)->b_back = (dp); \
151 	(dp)->b_forw->b_back = (bp); \
152 	(dp)->b_forw = (bp); \
153 }
154 
155 /*
156  * Insq/Remq for the buffer free lists.
157  */
158 #define	bremfree(bp) { \
159 	(bp)->av_back->av_forw = (bp)->av_forw; \
160 	(bp)->av_forw->av_back = (bp)->av_back; \
161 }
162 #define	binsheadfree(bp, dp) { \
163 	(dp)->av_forw->av_back = (bp); \
164 	(bp)->av_forw = (dp)->av_forw; \
165 	(dp)->av_forw = (bp); \
166 	(bp)->av_back = (dp); \
167 }
168 #define	binstailfree(bp, dp) { \
169 	(dp)->av_back->av_forw = (bp); \
170 	(bp)->av_back = (dp)->av_back; \
171 	(dp)->av_back = (bp); \
172 	(bp)->av_forw = (dp); \
173 }
174 
175 #define	iodone	biodone
176 #define	iowait	biowait
177 
178 /*
179  * Zero out a buffer's data portion.
180  */
181 #define	clrbuf(bp) { \
182 	blkclr((bp)->b_un.b_addr, (unsigned)(bp)->b_bcount); \
183 	(bp)->b_resid = 0; \
184 }
185 #define B_CLRBUF	0x1	/* request allocated buffer be cleared */
186 #define B_SYNC		0x2	/* do all allocations synchronously */
187