xref: /titanic_52/usr/src/uts/common/sys/buf.h (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #ifndef _SYS_BUF_H
41 #define	_SYS_BUF_H
42 
43 #pragma ident	"%Z%%M%	%I%	%E% SMI"
44 
45 #include <sys/types32.h>
46 #include <sys/t_lock.h>
47 #include <sys/kstat.h>
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  *	Each buffer in the pool is usually doubly linked into 2 lists:
55  *	the device with which it is currently associated (always)
56  *	and also on a list of blocks available for allocation
57  *	for other use (usually).
58  *	The latter list is kept in last-used order, and the two
59  *	lists are doubly linked to make it easy to remove
60  *	a buffer from one list when it was found by
61  *	looking through the other.
62  *	A buffer is on the available list, and is liable
63  *	to be reassigned to another disk block, if and only
64  *	if it is not marked BUSY.  When a buffer is busy, the
65  *	available-list pointers can be used for other purposes.
66  *	Most drivers use the forward ptr as a link in their I/O active queue.
67  *	A buffer header contains all the information required to perform I/O.
68  *	Most of the routines which manipulate these things are in bio.c.
69  *
70  *	There are a number of locks associated with the buffer management
71  *	system.
72  *	hbuf.b_lock:	protects hash chains, buffer hdr freelists
73  *			and delayed write freelist
74  *	bfree_lock;	protects the bfreelist structure
75  *	bhdr_lock:	protects the free header list
76  *	blist_lock:	protects b_list fields
77  *	buf.b_sem:	protects all remaining members in the buf struct
78  *	buf.b_io:	I/O synchronization variable
79  *
80  *	A buffer header is never "locked" (b_sem) when it is on
81  *	a "freelist" (bhdrlist or bfreelist avail lists).
82  */
83 typedef struct	buf {
84 	int	b_flags;		/* see defines below */
85 	struct buf *b_forw;		/* headed by d_tab of conf.c */
86 	struct buf *b_back;		/*  "  */
87 	struct buf *av_forw;		/* position on free list, */
88 	struct buf *av_back;		/* if not BUSY */
89 	o_dev_t	b_dev;			/* OLD major+minor device name */
90 	size_t b_bcount;		/* transfer count */
91 	union {
92 		caddr_t b_addr;		/* low order core address */
93 		struct fs *b_fs;	/* superblocks */
94 		struct cg *b_cg;	/* UFS cylinder group block */
95 		struct dinode *b_dino;	/* UFS ilist */
96 		daddr32_t *b_daddr;	/* disk blocks */
97 	} b_un;
98 
99 	lldaddr_t	_b_blkno;	/* block # on device (union) */
100 #define	b_lblkno	_b_blkno._f
101 #ifdef _LP64
102 #define	b_blkno		_b_blkno._f
103 #else
104 #define	b_blkno		_b_blkno._p._l
105 #endif /* _LP64 */
106 
107 	char	b_obs1;			/* obsolete */
108 	size_t	b_resid;		/* words not transferred after error */
109 	clock_t	b_start;		/* request start time */
110 	struct  proc  *b_proc;		/* process doing physical or swap I/O */
111 	struct	page  *b_pages;		/* page list for PAGEIO */
112 	clock_t b_obs2;			/* obsolete */
113 	/* Begin new stuff */
114 #define	b_actf	av_forw
115 #define	b_actl	av_back
116 #define	b_active b_bcount
117 #define	b_errcnt b_resid
118 	size_t	b_bufsize;		/* size of allocated buffer */
119 	int	(*b_iodone)(struct buf *);	/* function called by iodone */
120 	struct	vnode *b_vp;		/* vnode associated with block */
121 	struct 	buf *b_chain;		/* chain together all buffers here */
122 	int	b_obs3;			/* obsolete */
123 	int	b_error;		/* expanded error field */
124 	void	*b_private;		/* "opaque" driver private area */
125 	dev_t	b_edev;			/* expanded dev field */
126 	ksema_t	b_sem;			/* Exclusive access to buf */
127 	ksema_t	b_io;			/* I/O Synchronization */
128 	struct buf *b_list;		/* List of potential B_DELWRI bufs */
129 	struct page **b_shadow;		/* shadow page list */
130 	void	*b_dip;			/* device info pointer */
131 	struct vnode *b_file;		/* file associated with this buffer */
132 	offset_t b_offset;		/* offset in file assoc. with buffer */
133 } buf_t;
134 
135 /*
136  * Bufhd structures used at the head of the hashed buffer queues.
137  * We only need seven words for this, so this abbreviated
138  * definition saves some space.
139  */
140 struct diskhd {
141 	int	b_flags;		/* not used, needed for consistency */
142 	struct buf *b_forw, *b_back;	/* queue of unit queues */
143 	struct buf *av_forw, *av_back;	/* queue of bufs for this unit */
144 	o_dev_t	b_dev;			/* OLD major+minor device name */
145 	size_t b_bcount;		/* transfer count */
146 };
147 
148 
149 /*
150  * Statistics on the buffer cache
151  */
152 struct biostats {
153 	kstat_named_t	bio_lookup;	/* requests to assign buffer */
154 	kstat_named_t	bio_hit;	/* buffer already associated with blk */
155 	kstat_named_t	bio_bufwant;	/* kmem_allocs NOSLEEP failed new buf */
156 	kstat_named_t	bio_bufwait;	/* kmem_allocs with KM_SLEEP for buf */
157 	kstat_named_t	bio_bufbusy;	/* buffer locked by someone else */
158 	kstat_named_t	bio_bufdup;	/* duplicate buffer found for block */
159 };
160 
161 /*
162  * These flags are kept in b_flags.
163  * The first group is part of the DDI
164  */
165 #define	B_BUSY		0x0001	/* not on av_forw/back list */
166 #define	B_DONE		0x0002	/* transaction finished */
167 #define	B_ERROR		0x0004	/* transaction aborted */
168 #define	B_PAGEIO	0x0010	/* do I/O to pages on bp->p_pages */
169 #define	B_PHYS		0x0020	/* Physical IO potentially using UNIBUS map */
170 #define	B_READ		0x0040	/* read when I/O occurs */
171 #define	B_WRITE		0x0100	/* non-read pseudo-flag */
172 
173 /* Not part of the DDI */
174 #define	B_WANTED	0x0080		/* issue wakeup when BUSY goes off */
175 #define	B_AGE		0x000200	/* delayed write for correct aging */
176 #define	B_ASYNC		0x000400	/* don't wait for I/O completion */
177 #define	B_DELWRI	0x000800	/* delayed write-wait til buf needed */
178 #define	B_STALE		0x001000	/* on av_* list; invalid contents */
179 #define	B_DONTNEED	0x002000	/* after write, need not be cached */
180 #define	B_REMAPPED	0x004000	/* buffer is kernel addressable */
181 #define	B_FREE		0x008000	/* free page when done */
182 #define	B_INVAL		0x010000	/* does not contain valid info  */
183 #define	B_FORCE		0x020000	/* semi-permanent removal from cache */
184 #define	B_NOCACHE	0x080000 	/* don't cache block when released */
185 #define	B_TRUNC		0x100000	/* truncate page without I/O */
186 #define	B_SHADOW	0x200000	/* is b_shadow field valid? */
187 #define	B_RETRYWRI	0x400000	/* retry write til works or bfinval */
188 #define	B_FAILFAST	0x1000000	/* Fail promptly if device goes away */
189 #define	B_STARTED	0x2000000	/* io:::start probe called for buf */
190 #define	B_ABRWRITE	0x4000000	/* Application based recovery active */
191 
192 /*
193  * Insq/Remq for the buffer hash lists.
194  */
195 #define	bremhash(bp) { \
196 	ASSERT((bp)->b_forw != NULL); \
197 	ASSERT((bp)->b_back != NULL); \
198 	(bp)->b_back->b_forw = (bp)->b_forw; \
199 	(bp)->b_forw->b_back = (bp)->b_back; \
200 	(bp)->b_forw = (bp)->b_back = NULL; \
201 }
202 #define	binshash(bp, dp) { \
203 	ASSERT((bp)->b_forw == NULL); \
204 	ASSERT((bp)->b_back == NULL); \
205 	ASSERT((dp)->b_forw != NULL); \
206 	ASSERT((dp)->b_back != NULL); \
207 	(bp)->b_forw = (dp)->b_forw; \
208 	(bp)->b_back = (dp); \
209 	(dp)->b_forw->b_back = (bp); \
210 	(dp)->b_forw = (bp); \
211 }
212 
213 
214 /*
215  * The hash structure maintains two lists:
216  *
217  * 	1) The hash list of buffers (b_forw & b_back)
218  *	2) The LRU free list of buffers on this hash bucket (av_forw & av_back)
219  *
220  * The dwbuf structure keeps a list of delayed write buffers per hash bucket
221  * hence there are exactly the same number of dwbuf structures as there are
222  * the hash buckets (hbuf structures) in the system.
223  *
224  * The number of buffers on the freelist may not be equal to the number of
225  * buffers on the hash list. That is because when buffers are busy they are
226  * taken off the freelist but not off the hash list. "b_length" field keeps
227  * track of the number of free buffers (including delayed writes ones) on
228  * the hash bucket. The "b_lock" mutex protects the free list as well as
229  * the hash list. It also protects the counter "b_length".
230  *
231  * Enties b_forw, b_back, av_forw & av_back must be at the same offset
232  * as the ones in buf structure.
233  */
234 struct	hbuf {
235 	int	b_flags;
236 
237 	struct	buf	*b_forw;	/* hash list forw pointer */
238 	struct	buf	*b_back;	/* hash list back pointer */
239 
240 	struct	buf	*av_forw;	/* free list forw pointer */
241 	struct	buf	*av_back;	/* free list back pointer */
242 
243 	int		b_length;	/* # of entries on free list */
244 	kmutex_t	b_lock;		/* lock to protect this structure */
245 };
246 
247 
248 /*
249  * The delayed list pointer entries should match with the buf strcuture.
250  */
251 struct	dwbuf {
252 	int	b_flags;		/* not used */
253 
254 	struct	buf	*b_forw;	/* not used */
255 	struct	buf	*b_back;	/* not used */
256 
257 	struct	buf	*av_forw;	/* delayed write forw pointer */
258 	struct	buf	*av_back;	/* delayed write back pointer */
259 };
260 
261 
262 /*
263  * Unlink a buffer from the available (free or delayed write) list and mark
264  * it busy (internal interface).
265  */
266 #define	notavail(bp) \
267 {\
268 	ASSERT(SEMA_HELD(&bp->b_sem)); \
269 	ASSERT((bp)->av_forw != NULL); \
270 	ASSERT((bp)->av_back != NULL); \
271 	ASSERT((bp)->av_forw != (bp)); \
272 	ASSERT((bp)->av_back != (bp)); \
273 	(bp)->av_back->av_forw = (bp)->av_forw; \
274 	(bp)->av_forw->av_back = (bp)->av_back; \
275 	(bp)->b_flags |= B_BUSY; \
276 	(bp)->av_forw = (bp)->av_back = NULL; \
277 }
278 
279 #if defined(_KERNEL)
280 /*
281  * Macros to avoid the extra function call needed for binary compat.
282  *
283  * B_RETRYWRI is not included in clear_flags for BWRITE(), BWRITE2(),
284  * or brwrite() so that the retry operation is persistent until the
285  * write either succeeds or the buffer is bfinval()'d.
286  *
287  */
288 #define	BREAD(dev, blkno, bsize) \
289 	bread_common(/* ufsvfsp */ NULL, dev, blkno, bsize)
290 
291 #define	BWRITE(bp) \
292 	bwrite_common(/* ufsvfsp */ NULL, bp, /* force_wait */ 0, \
293 		/* do_relse */ 1, \
294 		/* clear_flags */ (B_READ | B_DONE | B_ERROR | B_DELWRI))
295 
296 #define	BWRITE2(bp) \
297 	bwrite_common(/* ufsvfsp */ NULL, bp, /* force_wait */ 1, \
298 		/* do_relse */ 0, \
299 		/* clear_flags */ (B_READ | B_DONE | B_ERROR | B_DELWRI))
300 
301 #define	GETBLK(dev, blkno, bsize) \
302 	getblk_common(/* ufsvfsp */ NULL, dev, blkno, bsize, /* errflg */ 0)
303 
304 
305 /*
306  * Macros for new retry write interfaces.
307  */
308 
309 /*
310  * Same as bdwrite() except write failures are retried.
311  */
312 #define	bdrwrite(bp) { \
313 	(bp)->b_flags |= B_RETRYWRI; \
314 	bdwrite((bp)); \
315 }
316 
317 /*
318  * Same as bwrite() except write failures are retried.
319  */
320 #define	brwrite(bp) { \
321 	(bp)->b_flags |= B_RETRYWRI; \
322 	bwrite_common((bp), /* force_wait */ 0, /* do_relse */ 1, \
323 		/* clear_flags */ (B_READ | B_DONE | B_ERROR | B_DELWRI)); \
324 }
325 
326 extern struct hbuf	*hbuf;		/* Hash table */
327 extern struct dwbuf	*dwbuf;		/* delayed write hash table */
328 extern struct buf	*buf;		/* The buffer pool itself */
329 extern struct buf	bfreelist;	/* head of available list */
330 
331 extern void (*bio_lufs_strategy)(void *, buf_t *);	/* UFS Logging */
332 extern void (*bio_snapshot_strategy)(void *, buf_t *);	/* UFS snapshots */
333 
334 int	bcheck(dev_t, struct buf *);
335 int	iowait(struct buf *);
336 int	hash2ints(int x, int y);
337 int	bio_busy(int);
338 int	biowait(struct buf *);
339 int	biomodified(struct buf *);
340 int	geterror(struct buf *);
341 void	minphys(struct buf *);
342 /*
343  * ufsvfsp is declared as a void * to avoid having everyone that uses
344  * this header file include sys/fs/ufs_inode.h.
345  */
346 void	bwrite_common(void *ufsvfsp, struct buf *, int force_wait,
347 	int do_relse, int clear_flags);
348 void	bwrite(struct buf *);
349 void	bwrite2(struct buf *);
350 void	bdwrite(struct buf *);
351 void	bawrite(struct buf *);
352 void	brelse(struct buf *);
353 void	iodone(struct buf *);
354 void	clrbuf(struct buf *);
355 void	bflush(dev_t);
356 void	blkflush(dev_t, daddr_t);
357 void	binval(dev_t);
358 int	bfinval(dev_t, int);
359 void	binit(void);
360 void	biodone(struct buf *);
361 void	bioinit(struct buf *);
362 void	biofini(struct buf *);
363 void	bp_mapin(struct buf *);
364 void	*bp_mapin_common(struct buf *, int);
365 void	bp_mapout(struct buf *);
366 void	bp_init(size_t, uint_t);
367 int	bp_color(struct buf *);
368 void	pageio_done(struct buf *);
369 struct buf *bread(dev_t, daddr_t, long);
370 struct buf *bread_common(void *, dev_t, daddr_t, long);
371 struct buf *breada(dev_t, daddr_t, daddr_t, long);
372 struct buf *getblk(dev_t, daddr_t, long);
373 struct buf *getblk_common(void *, dev_t, daddr_t, long, int);
374 struct buf *ngeteblk(long);
375 struct buf *geteblk(void);
376 struct buf *pageio_setup(struct page *, size_t, struct vnode *, int);
377 void bioerror(struct buf *bp, int error);
378 void bioreset(struct buf *bp);
379 struct buf *bioclone(struct buf *, off_t, size_t, dev_t, daddr_t,
380 	int (*)(struct buf *), struct buf *, int);
381 size_t	biosize(void);
382 #endif	/* defined(_KERNEL) */
383 
384 #ifdef	__cplusplus
385 }
386 #endif
387 
388 #endif	/* _SYS_BUF_H */
389