xref: /original-bsd/sys/sys/buf.h (revision 982436bd)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)buf.h	7.7 (Berkeley) 05/09/89
18  */
19 
20 /*
21  * The header for buffers in the buffer pool and otherwise used
22  * to describe a block i/o request is given here.
23  *
24  * Each buffer in the pool is usually doubly linked into 2 lists:
25  * hashed into a chain by <dev,blkno> so it can be located in the cache,
26  * and (usually) on (one of several) queues.  These lists are circular and
27  * doubly linked for easy removal.
28  *
29  * There are currently three queues for buffers:
30  *	one for buffers which must be kept permanently (super blocks)
31  * 	one for buffers containing ``useful'' information (the cache)
32  *	one for buffers containing ``non-useful'' information
33  *		(and empty buffers, pushed onto the front)
34  * The latter two queues contain the buffers which are available for
35  * reallocation, are kept in lru order.  When not on one of these queues,
36  * the buffers are ``checked out'' to drivers which use the available list
37  * pointers to keep track of them in their i/o active queues.
38  */
39 
40 /*
41  * Bufhd structures used at the head of the hashed buffer queues.
42  * We only need three words for these, so this abbreviated
43  * definition saves some space.
44  */
45 struct bufhd
46 {
47 	long	b_flags;		/* see defines below */
48 	struct	buf *b_forw, *b_back;	/* fwd/bkwd pointer in chain */
49 };
50 struct buf
51 {
52 	long	b_flags;		/* too much goes here to describe */
53 	struct	buf *b_forw, *b_back;	/* hash chain (2 way street) */
54 	struct	buf *av_forw, *av_back;	/* position on free list if not BUSY */
55 #define	b_actf	av_forw			/* alternate names for driver queue */
56 #define	b_actl	av_back			/*    head - isn't history wonderful */
57 	long	b_bcount;		/* transfer count */
58 	long	b_bufsize;		/* size of allocated buffer */
59 #define	b_active b_bcount		/* driver queue head: drive active */
60 	short	b_error;		/* returned after I/O */
61 	dev_t	b_dev;			/* major+minor device name */
62 	union {
63 	    caddr_t b_addr;		/* low order core address */
64 	    int	*b_words;		/* words for clearing */
65 	    struct fs *b_fs;		/* superblocks */
66 	    struct csum *b_cs;		/* superblock summary information */
67 	    struct cg *b_cg;		/* cylinder group block */
68 	    struct dinode *b_dino;	/* ilist */
69 	    daddr_t *b_daddr;		/* indirect block */
70 	} b_un;
71 	daddr_t	b_blkno;		/* block # on device */
72 	long	b_resid;		/* words not transferred after error */
73 #define	b_errcnt b_resid		/* while i/o in progress: # retries */
74 	struct  proc *b_proc;		/* proc doing physical or swap I/O */
75 	int	(*b_iodone)();		/* function called by iodone */
76 	struct vnode *b_vp;		/* Vnode for dev */
77 	int	b_pfcent;		/* center page when swapping cluster */
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 struct	buf *getblk();
110 struct	buf *geteblk();
111 struct	buf *getnewbuf();
112 
113 unsigned minphys();
114 #endif
115 
116 /*
117  * These flags are kept in b_flags.
118  */
119 #define	B_WRITE		0x000000	/* non-read pseudo-flag */
120 #define	B_READ		0x000001	/* read when I/O occurs */
121 #define	B_DONE		0x000002	/* transaction finished */
122 #define	B_ERROR		0x000004	/* transaction aborted */
123 #define	B_BUSY		0x000008	/* not on av_forw/back list */
124 #define	B_PHYS		0x000010	/* physical IO */
125 #define	B_XXX		0x000020	/* was B_MAP, alloc UNIBUS on pdp-11 */
126 #define	B_WANTED	0x000040	/* issue wakeup when BUSY goes off */
127 #define	B_AGE		0x000080	/* delayed write for correct aging */
128 #define	B_ASYNC		0x000100	/* don't wait for I/O completion */
129 #define	B_DELWRI	0x000200	/* write at exit of avail list */
130 #define	B_TAPE		0x000400	/* this is a magtape (no bdwrite) */
131 #define	B_UAREA		0x000800	/* add u-area to a swap operation */
132 #define	B_PAGET		0x001000	/* page in/out of page table space */
133 #define	B_DIRTY		0x002000	/* dirty page to be pushed out async */
134 #define	B_PGIN		0x004000	/* pagein op, so swap() can count it */
135 #define	B_CACHE		0x008000	/* did bread find us in the cache ? */
136 #define	B_INVAL		0x010000	/* does not contain valid info  */
137 #define	B_LOCKED	0x020000	/* locked in core (not reusable) */
138 #define	B_HEAD		0x040000	/* a buffer header, not a buffer */
139 #define	B_BAD		0x100000	/* bad block revectoring in progress */
140 #define	B_CALL		0x200000	/* call b_iodone from iodone */
141 #define	B_RAW		0x400000	/* set by physio for raw transfers */
142 #define	B_NOCACHE	0x800000	/* do not cache block after use */
143 
144 /*
145  * Insq/Remq for the buffer hash lists.
146  */
147 #define	bremhash(bp) { \
148 	(bp)->b_back->b_forw = (bp)->b_forw; \
149 	(bp)->b_forw->b_back = (bp)->b_back; \
150 }
151 #define	binshash(bp, dp) { \
152 	(bp)->b_forw = (dp)->b_forw; \
153 	(bp)->b_back = (dp); \
154 	(dp)->b_forw->b_back = (bp); \
155 	(dp)->b_forw = (bp); \
156 }
157 
158 /*
159  * Insq/Remq for the buffer free lists.
160  */
161 #define	bremfree(bp) { \
162 	(bp)->av_back->av_forw = (bp)->av_forw; \
163 	(bp)->av_forw->av_back = (bp)->av_back; \
164 }
165 #define	binsheadfree(bp, dp) { \
166 	(dp)->av_forw->av_back = (bp); \
167 	(bp)->av_forw = (dp)->av_forw; \
168 	(dp)->av_forw = (bp); \
169 	(bp)->av_back = (dp); \
170 }
171 #define	binstailfree(bp, dp) { \
172 	(dp)->av_back->av_forw = (bp); \
173 	(bp)->av_back = (dp)->av_back; \
174 	(dp)->av_back = (bp); \
175 	(bp)->av_forw = (dp); \
176 }
177 
178 /*
179  * Take a buffer off the free list it's on and
180  * mark it as being use (B_BUSY) by a device.
181  */
182 #define	notavail(bp) { \
183 	int x = splbio(); \
184 	bremfree(bp); \
185 	(bp)->b_flags |= B_BUSY; \
186 	splx(x); \
187 }
188 
189 #define	iodone	biodone
190 #define	iowait	biowait
191 
192 /*
193  * Zero out a buffer's data portion.
194  */
195 #define	clrbuf(bp) { \
196 	blkclr((bp)->b_un.b_addr, (unsigned)(bp)->b_bcount); \
197 	(bp)->b_resid = 0; \
198 }
199 #define B_CLRBUF	0x1	/* request allocated buffer be cleared */
200