1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang *
4a9643ea8Slogwang * Copyright (c) 1982, 1986, 1989, 1993
5a9643ea8Slogwang * The Regents of the University of California. All rights reserved.
6a9643ea8Slogwang * (c) UNIX System Laboratories, Inc.
7a9643ea8Slogwang * All or some portions of this file are derived from material licensed
8a9643ea8Slogwang * to the University of California by American Telephone and Telegraph
9a9643ea8Slogwang * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10a9643ea8Slogwang * the permission of UNIX System Laboratories, Inc.
11a9643ea8Slogwang *
12a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without
13a9643ea8Slogwang * modification, are permitted provided that the following conditions
14a9643ea8Slogwang * are met:
15a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright
16a9643ea8Slogwang * notice, this list of conditions and the following disclaimer.
17a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright
18a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the
19a9643ea8Slogwang * documentation and/or other materials provided with the distribution.
20*22ce4affSfengbojiang * 3. Neither the name of the University nor the names of its contributors
21a9643ea8Slogwang * may be used to endorse or promote products derived from this software
22a9643ea8Slogwang * without specific prior written permission.
23a9643ea8Slogwang *
24a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25a9643ea8Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26a9643ea8Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27a9643ea8Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28a9643ea8Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29a9643ea8Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30a9643ea8Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31a9643ea8Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32a9643ea8Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33a9643ea8Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34a9643ea8Slogwang * SUCH DAMAGE.
35a9643ea8Slogwang *
36a9643ea8Slogwang * @(#)buf.h 8.9 (Berkeley) 3/30/95
37a9643ea8Slogwang * $FreeBSD$
38a9643ea8Slogwang */
39a9643ea8Slogwang
40a9643ea8Slogwang #ifndef _SYS_BUF_H_
41a9643ea8Slogwang #define _SYS_BUF_H_
42a9643ea8Slogwang
43a9643ea8Slogwang #include <sys/bufobj.h>
44a9643ea8Slogwang #include <sys/queue.h>
45a9643ea8Slogwang #include <sys/lock.h>
46a9643ea8Slogwang #include <sys/lockmgr.h>
47*22ce4affSfengbojiang #include <vm/uma.h>
48a9643ea8Slogwang
49a9643ea8Slogwang struct bio;
50a9643ea8Slogwang struct buf;
51a9643ea8Slogwang struct bufobj;
52a9643ea8Slogwang struct mount;
53a9643ea8Slogwang struct vnode;
54a9643ea8Slogwang struct uio;
55a9643ea8Slogwang
56a9643ea8Slogwang /*
57a9643ea8Slogwang * To avoid including <ufs/ffs/softdep.h>
58a9643ea8Slogwang */
59a9643ea8Slogwang LIST_HEAD(workhead, worklist);
60a9643ea8Slogwang /*
61a9643ea8Slogwang * These are currently used only by the soft dependency code, hence
62a9643ea8Slogwang * are stored once in a global variable. If other subsystems wanted
63a9643ea8Slogwang * to use these hooks, a pointer to a set of bio_ops could be added
64a9643ea8Slogwang * to each buffer.
65a9643ea8Slogwang */
66a9643ea8Slogwang extern struct bio_ops {
67a9643ea8Slogwang void (*io_start)(struct buf *);
68a9643ea8Slogwang void (*io_complete)(struct buf *);
69a9643ea8Slogwang void (*io_deallocate)(struct buf *);
70a9643ea8Slogwang int (*io_countdeps)(struct buf *, int);
71a9643ea8Slogwang } bioops;
72a9643ea8Slogwang
73a9643ea8Slogwang struct vm_object;
74*22ce4affSfengbojiang struct vm_page;
75a9643ea8Slogwang
76*22ce4affSfengbojiang typedef uint32_t b_xflags_t;
77a9643ea8Slogwang
78a9643ea8Slogwang /*
79a9643ea8Slogwang * The buffer header describes an I/O operation in the kernel.
80a9643ea8Slogwang *
81a9643ea8Slogwang * NOTES:
82a9643ea8Slogwang * b_bufsize, b_bcount. b_bufsize is the allocation size of the
83a9643ea8Slogwang * buffer, either DEV_BSIZE or PAGE_SIZE aligned. b_bcount is the
84a9643ea8Slogwang * originally requested buffer size and can serve as a bounds check
85a9643ea8Slogwang * against EOF. For most, but not all uses, b_bcount == b_bufsize.
86a9643ea8Slogwang *
87a9643ea8Slogwang * b_dirtyoff, b_dirtyend. Buffers support piecemeal, unaligned
88a9643ea8Slogwang * ranges of dirty data that need to be written to backing store.
89a9643ea8Slogwang * The range is typically clipped at b_bcount ( not b_bufsize ).
90a9643ea8Slogwang *
91a9643ea8Slogwang * b_resid. Number of bytes remaining in I/O. After an I/O operation
92a9643ea8Slogwang * completes, b_resid is usually 0 indicating 100% success.
93a9643ea8Slogwang *
94a9643ea8Slogwang * All fields are protected by the buffer lock except those marked:
95a9643ea8Slogwang * V - Protected by owning bufobj lock
96a9643ea8Slogwang * Q - Protected by the buf queue lock
97a9643ea8Slogwang * D - Protected by an dependency implementation specific lock
98a9643ea8Slogwang */
99a9643ea8Slogwang struct buf {
100a9643ea8Slogwang struct bufobj *b_bufobj;
101a9643ea8Slogwang long b_bcount;
102a9643ea8Slogwang void *b_caller1;
103a9643ea8Slogwang caddr_t b_data;
104a9643ea8Slogwang int b_error;
105a9643ea8Slogwang uint16_t b_iocmd; /* BIO_* bio_cmd from bio.h */
106a9643ea8Slogwang uint16_t b_ioflags; /* BIO_* bio_flags from bio.h */
107a9643ea8Slogwang off_t b_iooffset;
108a9643ea8Slogwang long b_resid;
109a9643ea8Slogwang void (*b_iodone)(struct buf *);
110*22ce4affSfengbojiang void (*b_ckhashcalc)(struct buf *);
111*22ce4affSfengbojiang uint64_t b_ckhash; /* B_CKHASH requested check-hash */
112a9643ea8Slogwang daddr_t b_blkno; /* Underlying physical block number. */
113a9643ea8Slogwang off_t b_offset; /* Offset into file. */
114a9643ea8Slogwang TAILQ_ENTRY(buf) b_bobufs; /* (V) Buffer's associated vnode. */
115a9643ea8Slogwang uint32_t b_vflags; /* (V) BV_* flags */
116*22ce4affSfengbojiang uint8_t b_qindex; /* (Q) buffer queue index */
117*22ce4affSfengbojiang uint8_t b_domain; /* (Q) buf domain this resides in */
118*22ce4affSfengbojiang uint16_t b_subqueue; /* (Q) per-cpu q if any */
119a9643ea8Slogwang uint32_t b_flags; /* B_* flags. */
120a9643ea8Slogwang b_xflags_t b_xflags; /* extra flags */
121a9643ea8Slogwang struct lock b_lock; /* Buffer lock */
122a9643ea8Slogwang long b_bufsize; /* Allocated buffer size. */
123a9643ea8Slogwang int b_runningbufspace; /* when I/O is running, pipelining */
124a9643ea8Slogwang int b_kvasize; /* size of kva for buffer */
125a9643ea8Slogwang int b_dirtyoff; /* Offset in buffer of dirty region. */
126a9643ea8Slogwang int b_dirtyend; /* Offset of end of dirty region. */
127a9643ea8Slogwang caddr_t b_kvabase; /* base kva for buffer */
128a9643ea8Slogwang daddr_t b_lblkno; /* Logical block number. */
129a9643ea8Slogwang struct vnode *b_vp; /* Device vnode. */
130a9643ea8Slogwang struct ucred *b_rcred; /* Read credentials reference. */
131a9643ea8Slogwang struct ucred *b_wcred; /* Write credentials reference. */
132a9643ea8Slogwang union {
133a9643ea8Slogwang TAILQ_ENTRY(buf) b_freelist; /* (Q) */
134a9643ea8Slogwang struct {
135a9643ea8Slogwang void (*b_pgiodone)(void *, vm_page_t *, int, int);
136a9643ea8Slogwang int b_pgbefore;
137a9643ea8Slogwang int b_pgafter;
138a9643ea8Slogwang };
139a9643ea8Slogwang };
140a9643ea8Slogwang union cluster_info {
141a9643ea8Slogwang TAILQ_HEAD(cluster_list_head, buf) cluster_head;
142a9643ea8Slogwang TAILQ_ENTRY(buf) cluster_entry;
143a9643ea8Slogwang } b_cluster;
144a9643ea8Slogwang int b_npages;
145a9643ea8Slogwang struct workhead b_dep; /* (D) List of filesystem dependencies. */
146a9643ea8Slogwang void *b_fsprivate1;
147a9643ea8Slogwang void *b_fsprivate2;
148a9643ea8Slogwang void *b_fsprivate3;
149*22ce4affSfengbojiang
150*22ce4affSfengbojiang #if defined(FULL_BUF_TRACKING)
151*22ce4affSfengbojiang #define BUF_TRACKING_SIZE 32
152*22ce4affSfengbojiang #define BUF_TRACKING_ENTRY(x) ((x) & (BUF_TRACKING_SIZE - 1))
153*22ce4affSfengbojiang const char *b_io_tracking[BUF_TRACKING_SIZE];
154*22ce4affSfengbojiang uint32_t b_io_tcnt;
155*22ce4affSfengbojiang #elif defined(BUF_TRACKING)
156*22ce4affSfengbojiang const char *b_io_tracking;
157*22ce4affSfengbojiang #endif
158*22ce4affSfengbojiang struct vm_page *b_pages[];
159a9643ea8Slogwang };
160a9643ea8Slogwang
161a9643ea8Slogwang #define b_object b_bufobj->bo_object
162a9643ea8Slogwang
163a9643ea8Slogwang /*
164a9643ea8Slogwang * These flags are kept in b_flags.
165a9643ea8Slogwang *
166a9643ea8Slogwang * Notes:
167a9643ea8Slogwang *
168a9643ea8Slogwang * B_ASYNC VOP calls on bp's are usually async whether or not
169a9643ea8Slogwang * B_ASYNC is set, but some subsystems, such as NFS, like
170a9643ea8Slogwang * to know what is best for the caller so they can
171a9643ea8Slogwang * optimize the I/O.
172a9643ea8Slogwang *
173a9643ea8Slogwang * B_PAGING Indicates that bp is being used by the paging system or
174a9643ea8Slogwang * some paging system and that the bp is not linked into
175a9643ea8Slogwang * the b_vp's clean/dirty linked lists or ref counts.
176a9643ea8Slogwang * Buffer vp reassignments are illegal in this case.
177a9643ea8Slogwang *
178a9643ea8Slogwang * B_CACHE This may only be set if the buffer is entirely valid.
179a9643ea8Slogwang * The situation where B_DELWRI is set and B_CACHE is
180a9643ea8Slogwang * clear MUST be committed to disk by getblk() so
181a9643ea8Slogwang * B_DELWRI can also be cleared. See the comments for
182a9643ea8Slogwang * getblk() in kern/vfs_bio.c. If B_CACHE is clear,
183a9643ea8Slogwang * the caller is expected to clear BIO_ERROR and B_INVAL,
184a9643ea8Slogwang * set BIO_READ, and initiate an I/O.
185a9643ea8Slogwang *
186a9643ea8Slogwang * The 'entire buffer' is defined to be the range from
187a9643ea8Slogwang * 0 through b_bcount.
188a9643ea8Slogwang *
189a9643ea8Slogwang * B_MALLOC Request that the buffer be allocated from the malloc
190a9643ea8Slogwang * pool, DEV_BSIZE aligned instead of PAGE_SIZE aligned.
191a9643ea8Slogwang *
192a9643ea8Slogwang * B_CLUSTEROK This flag is typically set for B_DELWRI buffers
193a9643ea8Slogwang * by filesystems that allow clustering when the buffer
194a9643ea8Slogwang * is fully dirty and indicates that it may be clustered
195a9643ea8Slogwang * with other adjacent dirty buffers. Note the clustering
196a9643ea8Slogwang * may not be used with the stage 1 data write under NFS
197a9643ea8Slogwang * but may be used for the commit rpc portion.
198a9643ea8Slogwang *
199*22ce4affSfengbojiang * B_INVALONERR This flag is set on dirty buffers. It specifies that a
200*22ce4affSfengbojiang * write error should forcibly invalidate the buffer
201*22ce4affSfengbojiang * contents. This flag should be used with caution, as it
202*22ce4affSfengbojiang * discards data. It is incompatible with B_ASYNC.
203*22ce4affSfengbojiang *
204a9643ea8Slogwang * B_VMIO Indicates that the buffer is tied into an VM object.
205a9643ea8Slogwang * The buffer's data is always PAGE_SIZE aligned even
206a9643ea8Slogwang * if b_bufsize and b_bcount are not. ( b_bufsize is
207a9643ea8Slogwang * always at least DEV_BSIZE aligned, though ).
208a9643ea8Slogwang *
209a9643ea8Slogwang * B_DIRECT Hint that we should attempt to completely free
210a9643ea8Slogwang * the pages underlying the buffer. B_DIRECT is
211a9643ea8Slogwang * sticky until the buffer is released and typically
212a9643ea8Slogwang * only has an effect when B_RELBUF is also set.
213a9643ea8Slogwang *
214a9643ea8Slogwang */
215a9643ea8Slogwang
216a9643ea8Slogwang #define B_AGE 0x00000001 /* Move to age queue when I/O done. */
217a9643ea8Slogwang #define B_NEEDCOMMIT 0x00000002 /* Append-write in progress. */
218a9643ea8Slogwang #define B_ASYNC 0x00000004 /* Start I/O, do not wait. */
219a9643ea8Slogwang #define B_DIRECT 0x00000008 /* direct I/O flag (pls free vmio) */
220a9643ea8Slogwang #define B_DEFERRED 0x00000010 /* Skipped over for cleaning */
221a9643ea8Slogwang #define B_CACHE 0x00000020 /* Bread found us in the cache. */
222a9643ea8Slogwang #define B_VALIDSUSPWRT 0x00000040 /* Valid write during suspension. */
223a9643ea8Slogwang #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
224*22ce4affSfengbojiang #define B_CKHASH 0x00000100 /* checksum hash calculated on read */
225a9643ea8Slogwang #define B_DONE 0x00000200 /* I/O completed. */
226a9643ea8Slogwang #define B_EINTR 0x00000400 /* I/O was interrupted */
227a9643ea8Slogwang #define B_NOREUSE 0x00000800 /* Contents not reused once released. */
228*22ce4affSfengbojiang #define B_REUSE 0x00001000 /* Contents reused, second chance. */
229a9643ea8Slogwang #define B_INVAL 0x00002000 /* Does not contain valid info. */
230a9643ea8Slogwang #define B_BARRIER 0x00004000 /* Write this and all preceding first. */
231a9643ea8Slogwang #define B_NOCACHE 0x00008000 /* Do not cache block after use. */
232a9643ea8Slogwang #define B_MALLOC 0x00010000 /* malloced b_data */
233a9643ea8Slogwang #define B_CLUSTEROK 0x00020000 /* Pagein op, so swap() can count it. */
234*22ce4affSfengbojiang #define B_INVALONERR 0x00040000 /* Invalidate on write error. */
235*22ce4affSfengbojiang #define B_IOSTARTED 0x00080000 /* buf_start() called */
236a9643ea8Slogwang #define B_00100000 0x00100000 /* Available flag. */
237*22ce4affSfengbojiang #define B_MAXPHYS 0x00200000 /* nitems(b_pages[]) = atop(MAXPHYS). */
238a9643ea8Slogwang #define B_RELBUF 0x00400000 /* Release VMIO buffer. */
239a9643ea8Slogwang #define B_FS_FLAG1 0x00800000 /* Available flag for FS use. */
240a9643ea8Slogwang #define B_NOCOPY 0x01000000 /* Don't copy-on-write this buf. */
241a9643ea8Slogwang #define B_INFREECNT 0x02000000 /* buf is counted in numfreebufs */
242a9643ea8Slogwang #define B_PAGING 0x04000000 /* volatile paging I/O -- bypass VMIO */
243a9643ea8Slogwang #define B_MANAGED 0x08000000 /* Managed by FS. */
244a9643ea8Slogwang #define B_RAM 0x10000000 /* Read ahead mark (flag) */
245a9643ea8Slogwang #define B_VMIO 0x20000000 /* VMIO flag */
246a9643ea8Slogwang #define B_CLUSTER 0x40000000 /* pagein op, so swap() can count it */
247a9643ea8Slogwang #define B_REMFREE 0x80000000 /* Delayed bremfree */
248a9643ea8Slogwang
249a9643ea8Slogwang #define PRINT_BUF_FLAGS "\20\40remfree\37cluster\36vmio\35ram\34managed" \
250*22ce4affSfengbojiang "\33paging\32infreecnt\31nocopy\30b23\27relbuf\26maxphys\25b20" \
251*22ce4affSfengbojiang "\24iostarted\23invalonerr\22clusterok\21malloc\20nocache\17b14" \
252*22ce4affSfengbojiang "\16inval\15reuse\14noreuse\13eintr\12done\11b8\10delwri" \
253a9643ea8Slogwang "\7validsuspwrt\6cache\5deferred\4direct\3async\2needcommit\1age"
254a9643ea8Slogwang
255a9643ea8Slogwang /*
256a9643ea8Slogwang * These flags are kept in b_xflags.
257*22ce4affSfengbojiang *
258*22ce4affSfengbojiang * BX_FSPRIV reserves a set of eight flags that may be used by individual
259*22ce4affSfengbojiang * filesystems for their own purpose. Their specific definitions are
260*22ce4affSfengbojiang * found in the header files for each filesystem that uses them.
261a9643ea8Slogwang */
262a9643ea8Slogwang #define BX_VNDIRTY 0x00000001 /* On vnode dirty list */
263a9643ea8Slogwang #define BX_VNCLEAN 0x00000002 /* On vnode clean list */
264*22ce4affSfengbojiang #define BX_CVTENXIO 0x00000004 /* Convert errors to ENXIO */
265a9643ea8Slogwang #define BX_BKGRDWRITE 0x00000010 /* Do writes in background */
266a9643ea8Slogwang #define BX_BKGRDMARKER 0x00000020 /* Mark buffer for splay tree */
267a9643ea8Slogwang #define BX_ALTDATA 0x00000040 /* Holds extended data */
268*22ce4affSfengbojiang #define BX_FSPRIV 0x00FF0000 /* Filesystem-specific flags mask */
269a9643ea8Slogwang
270*22ce4affSfengbojiang #define PRINT_BUF_XFLAGS "\20\7altdata\6bkgrdmarker\5bkgrdwrite\3cvtenxio" \
271*22ce4affSfengbojiang "\2clean\1dirty"
272a9643ea8Slogwang
273a9643ea8Slogwang #define NOOFFSET (-1LL) /* No buffer offset calculated yet */
274a9643ea8Slogwang
275a9643ea8Slogwang /*
276a9643ea8Slogwang * These flags are kept in b_vflags.
277a9643ea8Slogwang */
278a9643ea8Slogwang #define BV_SCANNED 0x00000001 /* VOP_FSYNC funcs mark written bufs */
279a9643ea8Slogwang #define BV_BKGRDINPROG 0x00000002 /* Background write in progress */
280a9643ea8Slogwang #define BV_BKGRDWAIT 0x00000004 /* Background write waiting */
281a9643ea8Slogwang #define BV_BKGRDERR 0x00000008 /* Error from background write */
282a9643ea8Slogwang
283a9643ea8Slogwang #define PRINT_BUF_VFLAGS "\20\4bkgrderr\3bkgrdwait\2bkgrdinprog\1scanned"
284a9643ea8Slogwang
285a9643ea8Slogwang #ifdef _KERNEL
286*22ce4affSfengbojiang
287*22ce4affSfengbojiang #ifndef NSWBUF_MIN
288*22ce4affSfengbojiang #define NSWBUF_MIN 16
289*22ce4affSfengbojiang #endif
290*22ce4affSfengbojiang
291a9643ea8Slogwang /*
292a9643ea8Slogwang * Buffer locking
293a9643ea8Slogwang */
294a9643ea8Slogwang extern const char *buf_wmesg; /* Default buffer lock message */
295a9643ea8Slogwang #define BUF_WMESG "bufwait"
296a9643ea8Slogwang #include <sys/proc.h> /* XXX for curthread */
297a9643ea8Slogwang #include <sys/mutex.h>
298a9643ea8Slogwang
299a9643ea8Slogwang /*
300a9643ea8Slogwang * Initialize a lock.
301a9643ea8Slogwang */
302a9643ea8Slogwang #define BUF_LOCKINIT(bp) \
303*22ce4affSfengbojiang lockinit(&(bp)->b_lock, PRIBIO + 4, buf_wmesg, 0, LK_NEW)
304a9643ea8Slogwang /*
305a9643ea8Slogwang *
306a9643ea8Slogwang * Get a lock sleeping non-interruptably until it becomes available.
307a9643ea8Slogwang */
308a9643ea8Slogwang #define BUF_LOCK(bp, locktype, interlock) \
309a9643ea8Slogwang _lockmgr_args_rw(&(bp)->b_lock, (locktype), (interlock), \
310a9643ea8Slogwang LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, \
311a9643ea8Slogwang LOCK_FILE, LOCK_LINE)
312a9643ea8Slogwang
313a9643ea8Slogwang /*
314a9643ea8Slogwang * Get a lock sleeping with specified interruptably and timeout.
315a9643ea8Slogwang */
316a9643ea8Slogwang #define BUF_TIMELOCK(bp, locktype, interlock, wmesg, catch, timo) \
317a9643ea8Slogwang _lockmgr_args_rw(&(bp)->b_lock, (locktype) | LK_TIMELOCK, \
318a9643ea8Slogwang (interlock), (wmesg), (PRIBIO + 4) | (catch), (timo), \
319a9643ea8Slogwang LOCK_FILE, LOCK_LINE)
320a9643ea8Slogwang
321a9643ea8Slogwang /*
322a9643ea8Slogwang * Release a lock. Only the acquiring process may free the lock unless
323a9643ea8Slogwang * it has been handed off to biodone.
324a9643ea8Slogwang */
325a9643ea8Slogwang #define BUF_UNLOCK(bp) do { \
326a9643ea8Slogwang KASSERT(((bp)->b_flags & B_REMFREE) == 0, \
327a9643ea8Slogwang ("BUF_UNLOCK %p while B_REMFREE is still set.", (bp))); \
328a9643ea8Slogwang \
329*22ce4affSfengbojiang BUF_UNLOCK_RAW((bp)); \
330*22ce4affSfengbojiang } while (0)
331*22ce4affSfengbojiang #define BUF_UNLOCK_RAW(bp) do { \
332a9643ea8Slogwang (void)_lockmgr_args(&(bp)->b_lock, LK_RELEASE, NULL, \
333a9643ea8Slogwang LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, \
334a9643ea8Slogwang LOCK_FILE, LOCK_LINE); \
335a9643ea8Slogwang } while (0)
336a9643ea8Slogwang
337a9643ea8Slogwang /*
338a9643ea8Slogwang * Check if a buffer lock is recursed.
339a9643ea8Slogwang */
340a9643ea8Slogwang #define BUF_LOCKRECURSED(bp) \
341a9643ea8Slogwang lockmgr_recursed(&(bp)->b_lock)
342a9643ea8Slogwang
343a9643ea8Slogwang /*
344a9643ea8Slogwang * Check if a buffer lock is currently held.
345a9643ea8Slogwang */
346a9643ea8Slogwang #define BUF_ISLOCKED(bp) \
347a9643ea8Slogwang lockstatus(&(bp)->b_lock)
348a9643ea8Slogwang /*
349a9643ea8Slogwang * Free a buffer lock.
350a9643ea8Slogwang */
351a9643ea8Slogwang #define BUF_LOCKFREE(bp) \
352a9643ea8Slogwang lockdestroy(&(bp)->b_lock)
353a9643ea8Slogwang
354a9643ea8Slogwang /*
355a9643ea8Slogwang * Print informations on a buffer lock.
356a9643ea8Slogwang */
357a9643ea8Slogwang #define BUF_LOCKPRINTINFO(bp) \
358a9643ea8Slogwang lockmgr_printinfo(&(bp)->b_lock)
359a9643ea8Slogwang
360a9643ea8Slogwang /*
361a9643ea8Slogwang * Buffer lock assertions.
362a9643ea8Slogwang */
363a9643ea8Slogwang #if defined(INVARIANTS) && defined(INVARIANT_SUPPORT)
364a9643ea8Slogwang #define BUF_ASSERT_LOCKED(bp) \
365a9643ea8Slogwang _lockmgr_assert(&(bp)->b_lock, KA_LOCKED, LOCK_FILE, LOCK_LINE)
366a9643ea8Slogwang #define BUF_ASSERT_SLOCKED(bp) \
367a9643ea8Slogwang _lockmgr_assert(&(bp)->b_lock, KA_SLOCKED, LOCK_FILE, LOCK_LINE)
368a9643ea8Slogwang #define BUF_ASSERT_XLOCKED(bp) \
369a9643ea8Slogwang _lockmgr_assert(&(bp)->b_lock, KA_XLOCKED, LOCK_FILE, LOCK_LINE)
370a9643ea8Slogwang #define BUF_ASSERT_UNLOCKED(bp) \
371a9643ea8Slogwang _lockmgr_assert(&(bp)->b_lock, KA_UNLOCKED, LOCK_FILE, LOCK_LINE)
372a9643ea8Slogwang #else
373a9643ea8Slogwang #define BUF_ASSERT_LOCKED(bp)
374a9643ea8Slogwang #define BUF_ASSERT_SLOCKED(bp)
375a9643ea8Slogwang #define BUF_ASSERT_XLOCKED(bp)
376a9643ea8Slogwang #define BUF_ASSERT_UNLOCKED(bp)
377a9643ea8Slogwang #endif
378a9643ea8Slogwang
379a9643ea8Slogwang #ifdef _SYS_PROC_H_ /* Avoid #include <sys/proc.h> pollution */
380a9643ea8Slogwang /*
381a9643ea8Slogwang * When initiating asynchronous I/O, change ownership of the lock to the
382a9643ea8Slogwang * kernel. Once done, the lock may legally released by biodone. The
383a9643ea8Slogwang * original owning process can no longer acquire it recursively, but must
384a9643ea8Slogwang * wait until the I/O is completed and the lock has been freed by biodone.
385a9643ea8Slogwang */
386a9643ea8Slogwang #define BUF_KERNPROC(bp) \
387a9643ea8Slogwang _lockmgr_disown(&(bp)->b_lock, LOCK_FILE, LOCK_LINE)
388a9643ea8Slogwang #endif
389a9643ea8Slogwang
390a9643ea8Slogwang #endif /* _KERNEL */
391a9643ea8Slogwang
392a9643ea8Slogwang struct buf_queue_head {
393a9643ea8Slogwang TAILQ_HEAD(buf_queue, buf) queue;
394a9643ea8Slogwang daddr_t last_pblkno;
395a9643ea8Slogwang struct buf *insert_point;
396a9643ea8Slogwang struct buf *switch_point;
397a9643ea8Slogwang };
398a9643ea8Slogwang
399a9643ea8Slogwang /*
400a9643ea8Slogwang * This structure describes a clustered I/O.
401a9643ea8Slogwang */
402a9643ea8Slogwang struct cluster_save {
403a9643ea8Slogwang long bs_bcount; /* Saved b_bcount. */
404a9643ea8Slogwang long bs_bufsize; /* Saved b_bufsize. */
405a9643ea8Slogwang int bs_nchildren; /* Number of associated buffers. */
406a9643ea8Slogwang struct buf **bs_children; /* List of associated buffers. */
407a9643ea8Slogwang };
408a9643ea8Slogwang
409a9643ea8Slogwang #ifdef _KERNEL
410a9643ea8Slogwang
411a9643ea8Slogwang static __inline int
bwrite(struct buf * bp)412a9643ea8Slogwang bwrite(struct buf *bp)
413a9643ea8Slogwang {
414a9643ea8Slogwang
415a9643ea8Slogwang KASSERT(bp->b_bufobj != NULL, ("bwrite: no bufobj bp=%p", bp));
416a9643ea8Slogwang KASSERT(bp->b_bufobj->bo_ops != NULL, ("bwrite: no bo_ops bp=%p", bp));
417a9643ea8Slogwang KASSERT(bp->b_bufobj->bo_ops->bop_write != NULL,
418a9643ea8Slogwang ("bwrite: no bop_write bp=%p", bp));
419a9643ea8Slogwang return (BO_WRITE(bp->b_bufobj, bp));
420a9643ea8Slogwang }
421a9643ea8Slogwang
422a9643ea8Slogwang static __inline void
bstrategy(struct buf * bp)423a9643ea8Slogwang bstrategy(struct buf *bp)
424a9643ea8Slogwang {
425a9643ea8Slogwang
426a9643ea8Slogwang KASSERT(bp->b_bufobj != NULL, ("bstrategy: no bufobj bp=%p", bp));
427a9643ea8Slogwang KASSERT(bp->b_bufobj->bo_ops != NULL,
428a9643ea8Slogwang ("bstrategy: no bo_ops bp=%p", bp));
429a9643ea8Slogwang KASSERT(bp->b_bufobj->bo_ops->bop_strategy != NULL,
430a9643ea8Slogwang ("bstrategy: no bop_strategy bp=%p", bp));
431a9643ea8Slogwang BO_STRATEGY(bp->b_bufobj, bp);
432a9643ea8Slogwang }
433a9643ea8Slogwang
434a9643ea8Slogwang static __inline void
buf_start(struct buf * bp)435a9643ea8Slogwang buf_start(struct buf *bp)
436a9643ea8Slogwang {
437*22ce4affSfengbojiang KASSERT((bp->b_flags & B_IOSTARTED) == 0,
438*22ce4affSfengbojiang ("recursed buf_start %p", bp));
439*22ce4affSfengbojiang bp->b_flags |= B_IOSTARTED;
440a9643ea8Slogwang if (bioops.io_start)
441a9643ea8Slogwang (*bioops.io_start)(bp);
442a9643ea8Slogwang }
443a9643ea8Slogwang
444a9643ea8Slogwang static __inline void
buf_complete(struct buf * bp)445a9643ea8Slogwang buf_complete(struct buf *bp)
446a9643ea8Slogwang {
447*22ce4affSfengbojiang if ((bp->b_flags & B_IOSTARTED) != 0) {
448*22ce4affSfengbojiang bp->b_flags &= ~B_IOSTARTED;
449a9643ea8Slogwang if (bioops.io_complete)
450a9643ea8Slogwang (*bioops.io_complete)(bp);
451a9643ea8Slogwang }
452*22ce4affSfengbojiang }
453a9643ea8Slogwang
454a9643ea8Slogwang static __inline void
buf_deallocate(struct buf * bp)455a9643ea8Slogwang buf_deallocate(struct buf *bp)
456a9643ea8Slogwang {
457a9643ea8Slogwang if (bioops.io_deallocate)
458a9643ea8Slogwang (*bioops.io_deallocate)(bp);
459a9643ea8Slogwang }
460a9643ea8Slogwang
461a9643ea8Slogwang static __inline int
buf_countdeps(struct buf * bp,int i)462a9643ea8Slogwang buf_countdeps(struct buf *bp, int i)
463a9643ea8Slogwang {
464a9643ea8Slogwang if (bioops.io_countdeps)
465a9643ea8Slogwang return ((*bioops.io_countdeps)(bp, i));
466a9643ea8Slogwang else
467a9643ea8Slogwang return (0);
468a9643ea8Slogwang }
469a9643ea8Slogwang
470*22ce4affSfengbojiang static __inline void
buf_track(struct buf * bp __unused,const char * location __unused)471*22ce4affSfengbojiang buf_track(struct buf *bp __unused, const char *location __unused)
472*22ce4affSfengbojiang {
473*22ce4affSfengbojiang
474*22ce4affSfengbojiang #if defined(FULL_BUF_TRACKING)
475*22ce4affSfengbojiang bp->b_io_tracking[BUF_TRACKING_ENTRY(bp->b_io_tcnt++)] = location;
476*22ce4affSfengbojiang #elif defined(BUF_TRACKING)
477*22ce4affSfengbojiang bp->b_io_tracking = location;
478*22ce4affSfengbojiang #endif
479*22ce4affSfengbojiang }
480*22ce4affSfengbojiang
481a9643ea8Slogwang #endif /* _KERNEL */
482a9643ea8Slogwang
483a9643ea8Slogwang /*
484a9643ea8Slogwang * Zero out the buffer's data area.
485a9643ea8Slogwang */
486a9643ea8Slogwang #define clrbuf(bp) { \
487a9643ea8Slogwang bzero((bp)->b_data, (u_int)(bp)->b_bcount); \
488a9643ea8Slogwang (bp)->b_resid = 0; \
489a9643ea8Slogwang }
490a9643ea8Slogwang
491a9643ea8Slogwang /*
492a9643ea8Slogwang * Flags for getblk's last parameter.
493a9643ea8Slogwang */
494a9643ea8Slogwang #define GB_LOCK_NOWAIT 0x0001 /* Fail if we block on a buf lock. */
495a9643ea8Slogwang #define GB_NOCREAT 0x0002 /* Don't create a buf if not found. */
496a9643ea8Slogwang #define GB_NOWAIT_BD 0x0004 /* Do not wait for bufdaemon. */
497a9643ea8Slogwang #define GB_UNMAPPED 0x0008 /* Do not mmap buffer pages. */
498a9643ea8Slogwang #define GB_KVAALLOC 0x0010 /* But allocate KVA. */
499*22ce4affSfengbojiang #define GB_CKHASH 0x0020 /* If reading, calc checksum hash */
500*22ce4affSfengbojiang #define GB_NOSPARSE 0x0040 /* Do not instantiate holes */
501*22ce4affSfengbojiang #define GB_CVTENXIO 0x0080 /* Convert errors to ENXIO */
502a9643ea8Slogwang
503a9643ea8Slogwang #ifdef _KERNEL
504a9643ea8Slogwang extern int nbuf; /* The number of buffer headers */
505*22ce4affSfengbojiang extern u_long maxswzone; /* Max KVA for swap structures */
506*22ce4affSfengbojiang extern u_long maxbcache; /* Max KVA for buffer cache */
507*22ce4affSfengbojiang extern int maxbcachebuf; /* Max buffer cache block size */
508a9643ea8Slogwang extern long runningbufspace;
509a9643ea8Slogwang extern long hibufspace;
510a9643ea8Slogwang extern int dirtybufthresh;
511a9643ea8Slogwang extern int bdwriteskip;
512a9643ea8Slogwang extern int dirtybufferflushes;
513a9643ea8Slogwang extern int altbufferflushes;
514a9643ea8Slogwang extern int nswbuf; /* Number of swap I/O buffer headers. */
515a9643ea8Slogwang extern caddr_t unmapped_buf; /* Data address for unmapped buffers. */
516a9643ea8Slogwang
517a9643ea8Slogwang static inline int
buf_mapped(struct buf * bp)518a9643ea8Slogwang buf_mapped(struct buf *bp)
519a9643ea8Slogwang {
520a9643ea8Slogwang
521a9643ea8Slogwang return (bp->b_data != unmapped_buf);
522a9643ea8Slogwang }
523a9643ea8Slogwang
524a9643ea8Slogwang void runningbufwakeup(struct buf *);
525a9643ea8Slogwang void waitrunningbufspace(void);
526a9643ea8Slogwang caddr_t kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est);
527a9643ea8Slogwang void bufinit(void);
528a9643ea8Slogwang void bufshutdown(int);
529a9643ea8Slogwang void bdata2bio(struct buf *bp, struct bio *bip);
530a9643ea8Slogwang void bwillwrite(void);
531a9643ea8Slogwang int buf_dirty_count_severe(void);
532a9643ea8Slogwang void bremfree(struct buf *);
533a9643ea8Slogwang void bremfreef(struct buf *); /* XXX Force bremfree, only for nfs. */
534a9643ea8Slogwang #define bread(vp, blkno, size, cred, bpp) \
535*22ce4affSfengbojiang breadn_flags(vp, blkno, blkno, size, NULL, NULL, 0, cred, 0, \
536*22ce4affSfengbojiang NULL, bpp)
537a9643ea8Slogwang #define bread_gb(vp, blkno, size, cred, gbflags, bpp) \
538*22ce4affSfengbojiang breadn_flags(vp, blkno, blkno, size, NULL, NULL, 0, cred, \
539*22ce4affSfengbojiang gbflags, NULL, bpp)
540a9643ea8Slogwang #define breadn(vp, blkno, size, rablkno, rabsize, cnt, cred, bpp) \
541*22ce4affSfengbojiang breadn_flags(vp, blkno, blkno, size, rablkno, rabsize, cnt, cred, \
542*22ce4affSfengbojiang 0, NULL, bpp)
543*22ce4affSfengbojiang int breadn_flags(struct vnode *, daddr_t, daddr_t, int, daddr_t *, int *,
544*22ce4affSfengbojiang int, struct ucred *, int, void (*)(struct buf *), struct buf **);
545a9643ea8Slogwang void bdwrite(struct buf *);
546a9643ea8Slogwang void bawrite(struct buf *);
547a9643ea8Slogwang void babarrierwrite(struct buf *);
548a9643ea8Slogwang int bbarrierwrite(struct buf *);
549a9643ea8Slogwang void bdirty(struct buf *);
550a9643ea8Slogwang void bundirty(struct buf *);
551a9643ea8Slogwang void bufstrategy(struct bufobj *, struct buf *);
552a9643ea8Slogwang void brelse(struct buf *);
553a9643ea8Slogwang void bqrelse(struct buf *);
554a9643ea8Slogwang int vfs_bio_awrite(struct buf *);
555*22ce4affSfengbojiang void vfs_busy_pages_acquire(struct buf *bp);
556*22ce4affSfengbojiang void vfs_busy_pages_release(struct buf *bp);
557a9643ea8Slogwang struct buf *incore(struct bufobj *, daddr_t);
558*22ce4affSfengbojiang bool inmem(struct vnode *, daddr_t);
559a9643ea8Slogwang struct buf *gbincore(struct bufobj *, daddr_t);
560*22ce4affSfengbojiang struct buf *gbincore_unlocked(struct bufobj *, daddr_t);
561a9643ea8Slogwang struct buf *getblk(struct vnode *, daddr_t, int, int, int, int);
562*22ce4affSfengbojiang int getblkx(struct vnode *vp, daddr_t blkno, daddr_t dblkno, int size,
563*22ce4affSfengbojiang int slpflag, int slptimeo, int flags, struct buf **bpp);
564a9643ea8Slogwang struct buf *geteblk(int, int);
565a9643ea8Slogwang int bufwait(struct buf *);
566a9643ea8Slogwang int bufwrite(struct buf *);
567a9643ea8Slogwang void bufdone(struct buf *);
568a9643ea8Slogwang void bd_speedup(void);
569a9643ea8Slogwang
570*22ce4affSfengbojiang extern uma_zone_t pbuf_zone;
571*22ce4affSfengbojiang uma_zone_t pbuf_zsecond_create(const char *name, int max);
572*22ce4affSfengbojiang
573a9643ea8Slogwang int cluster_read(struct vnode *, u_quad_t, daddr_t, long,
574a9643ea8Slogwang struct ucred *, long, int, int, struct buf **);
575a9643ea8Slogwang int cluster_wbuild(struct vnode *, long, daddr_t, int, int);
576a9643ea8Slogwang void cluster_write(struct vnode *, struct buf *, u_quad_t, int, int);
577*22ce4affSfengbojiang void vfs_bio_brelse(struct buf *bp, int ioflags);
578a9643ea8Slogwang void vfs_bio_bzero_buf(struct buf *bp, int base, int size);
579a9643ea8Slogwang void vfs_bio_clrbuf(struct buf *);
580*22ce4affSfengbojiang void vfs_bio_set_flags(struct buf *bp, int ioflags);
581*22ce4affSfengbojiang void vfs_bio_set_valid(struct buf *, int base, int size);
582a9643ea8Slogwang void vfs_busy_pages(struct buf *, int clear_modify);
583a9643ea8Slogwang void vfs_unbusy_pages(struct buf *);
584*22ce4affSfengbojiang int vmapbuf(struct buf *, void *, size_t, int);
585a9643ea8Slogwang void vunmapbuf(struct buf *);
586a9643ea8Slogwang void brelvp(struct buf *);
587a9643ea8Slogwang void bgetvp(struct vnode *, struct buf *);
588a9643ea8Slogwang void pbgetbo(struct bufobj *bo, struct buf *bp);
589a9643ea8Slogwang void pbgetvp(struct vnode *, struct buf *);
590a9643ea8Slogwang void pbrelbo(struct buf *);
591a9643ea8Slogwang void pbrelvp(struct buf *);
592a9643ea8Slogwang int allocbuf(struct buf *bp, int size);
593a9643ea8Slogwang void reassignbuf(struct buf *);
594a9643ea8Slogwang void bwait(struct buf *, u_char, const char *);
595a9643ea8Slogwang void bdone(struct buf *);
596*22ce4affSfengbojiang
597*22ce4affSfengbojiang typedef daddr_t (vbg_get_lblkno_t)(struct vnode *, vm_ooffset_t);
598*22ce4affSfengbojiang typedef int (vbg_get_blksize_t)(struct vnode *, daddr_t);
599*22ce4affSfengbojiang int vfs_bio_getpages(struct vnode *vp, struct vm_page **ma, int count,
600*22ce4affSfengbojiang int *rbehind, int *rahead, vbg_get_lblkno_t get_lblkno,
601*22ce4affSfengbojiang vbg_get_blksize_t get_blksize);
602a9643ea8Slogwang
603a9643ea8Slogwang #endif /* _KERNEL */
604a9643ea8Slogwang
605a9643ea8Slogwang #endif /* !_SYS_BUF_H_ */
606