xref: /f-stack/freebsd/sys/bufobj.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2004 Poul-Henning Kamp
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang  *
16a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9643ea8Slogwang  * SUCH DAMAGE.
27a9643ea8Slogwang  *
28a9643ea8Slogwang  * $FreeBSD$
29a9643ea8Slogwang  */
30a9643ea8Slogwang 
31a9643ea8Slogwang /*
32a9643ea8Slogwang  * Architectural notes:
33a9643ea8Slogwang  *
34a9643ea8Slogwang  * bufobj is a new object which is what buffers hang from in the buffer
35a9643ea8Slogwang  * cache.
36a9643ea8Slogwang  *
37a9643ea8Slogwang  * This used to be vnodes, but we need non-vnode code to be able
38a9643ea8Slogwang  * to use the buffer cache as well, specifically geom classes like gbde,
39a9643ea8Slogwang  * raid3 and raid5.
40a9643ea8Slogwang  *
41a9643ea8Slogwang  * All vnodes will contain a bufobj initially, but down the road we may
42a9643ea8Slogwang  * want to only allocate bufobjs when they are needed.  There could be a
43a9643ea8Slogwang  * large number of vnodes in the system which wouldn't need a bufobj during
44a9643ea8Slogwang  * their lifetime.
45a9643ea8Slogwang  *
46a9643ea8Slogwang  * The exact relationship to the vmobject is not determined at this point,
47a9643ea8Slogwang  * it may in fact be that we find them to be two sides of the same object
48a9643ea8Slogwang  * once things starts to crystalize.
49a9643ea8Slogwang  */
50a9643ea8Slogwang 
51a9643ea8Slogwang #ifndef _SYS_BUFOBJ_H_
52a9643ea8Slogwang #define _SYS_BUFOBJ_H_
53a9643ea8Slogwang 
54a9643ea8Slogwang #if defined(_KERNEL) || defined(_KVM_VNODE)
55a9643ea8Slogwang 
56a9643ea8Slogwang #include <sys/queue.h>
57a9643ea8Slogwang #include <sys/_lock.h>
58a9643ea8Slogwang #include <sys/_rwlock.h>
59a9643ea8Slogwang #include <sys/_pctrie.h>
60a9643ea8Slogwang 
61a9643ea8Slogwang struct bufobj;
62a9643ea8Slogwang struct buf_ops;
63a9643ea8Slogwang 
64a9643ea8Slogwang extern struct buf_ops buf_ops_bio;
65a9643ea8Slogwang 
66a9643ea8Slogwang TAILQ_HEAD(buflists, buf);
67a9643ea8Slogwang 
68a9643ea8Slogwang /* A Buffer list & trie */
69a9643ea8Slogwang struct bufv {
70a9643ea8Slogwang 	struct buflists	bv_hd;		/* Sorted blocklist */
71a9643ea8Slogwang 	struct pctrie	bv_root;	/* Buf trie */
72a9643ea8Slogwang 	int		bv_cnt;		/* Number of buffers */
73a9643ea8Slogwang };
74a9643ea8Slogwang 
75a9643ea8Slogwang typedef void b_strategy_t(struct bufobj *, struct buf *);
76a9643ea8Slogwang typedef int b_write_t(struct buf *);
77a9643ea8Slogwang typedef int b_sync_t(struct bufobj *, int waitfor);
78a9643ea8Slogwang typedef void b_bdflush_t(struct bufobj *, struct buf *);
79a9643ea8Slogwang 
80a9643ea8Slogwang struct buf_ops {
81*22ce4affSfengbojiang 	const char	*bop_name;
82a9643ea8Slogwang 	b_write_t	*bop_write;
83a9643ea8Slogwang 	b_strategy_t	*bop_strategy;
84a9643ea8Slogwang 	b_sync_t	*bop_sync;
85a9643ea8Slogwang 	b_bdflush_t	*bop_bdflush;
86a9643ea8Slogwang };
87a9643ea8Slogwang 
88a9643ea8Slogwang #define BO_STRATEGY(bo, bp)	((bo)->bo_ops->bop_strategy((bo), (bp)))
89a9643ea8Slogwang #define BO_SYNC(bo, w)		((bo)->bo_ops->bop_sync((bo), (w)))
90a9643ea8Slogwang #define BO_WRITE(bo, bp)	((bo)->bo_ops->bop_write((bp)))
91a9643ea8Slogwang #define BO_BDFLUSH(bo, bp)	((bo)->bo_ops->bop_bdflush((bo), (bp)))
92a9643ea8Slogwang 
93*22ce4affSfengbojiang /*
94*22ce4affSfengbojiang  * Locking notes:
95*22ce4affSfengbojiang  * 'S' is sync_mtx
96*22ce4affSfengbojiang  * 'v' is the vnode lock which embeds the bufobj.
97*22ce4affSfengbojiang  * '-' Constant and unchanging after initialization.
98*22ce4affSfengbojiang  */
99a9643ea8Slogwang struct bufobj {
100a9643ea8Slogwang 	struct rwlock	bo_lock;	/* Lock which protects "i" things */
101a9643ea8Slogwang 	struct buf_ops	*bo_ops;	/* - Buffer operations */
102a9643ea8Slogwang 	struct vm_object *bo_object;	/* v Place to store VM object */
103a9643ea8Slogwang 	LIST_ENTRY(bufobj) bo_synclist;	/* S dirty vnode list */
104a9643ea8Slogwang 	void		*bo_private;	/* private pointer */
105a9643ea8Slogwang 	struct bufv	bo_clean;	/* i Clean buffers */
106a9643ea8Slogwang 	struct bufv	bo_dirty;	/* i Dirty buffers */
107*22ce4affSfengbojiang 	int		bo_numoutput;	/* i Writes in progress */
108a9643ea8Slogwang 	u_int		bo_flag;	/* i Flags */
109*22ce4affSfengbojiang 	int		bo_domain;	/* - Clean queue affinity */
110a9643ea8Slogwang 	int		bo_bsize;	/* - Block size for i/o */
111a9643ea8Slogwang };
112a9643ea8Slogwang 
113a9643ea8Slogwang /*
114a9643ea8Slogwang  * XXX BO_ONWORKLST could be replaced with a check for NULL list elements
115a9643ea8Slogwang  * in v_synclist.
116a9643ea8Slogwang  */
117a9643ea8Slogwang #define	BO_ONWORKLST	(1 << 0)	/* On syncer work-list */
118a9643ea8Slogwang #define	BO_WWAIT	(1 << 1)	/* Wait for output to complete */
119a9643ea8Slogwang #define	BO_DEAD		(1 << 2)	/* Dead; only with INVARIANTS */
120*22ce4affSfengbojiang #define	BO_NOBUFS	(1 << 3)	/* No bufs allowed */
121a9643ea8Slogwang 
122a9643ea8Slogwang #define	BO_LOCKPTR(bo)		(&(bo)->bo_lock)
123a9643ea8Slogwang #define	BO_LOCK(bo)		rw_wlock(BO_LOCKPTR((bo)))
124a9643ea8Slogwang #define	BO_UNLOCK(bo)		rw_wunlock(BO_LOCKPTR((bo)))
125a9643ea8Slogwang #define	BO_RLOCK(bo)		rw_rlock(BO_LOCKPTR((bo)))
126a9643ea8Slogwang #define	BO_RUNLOCK(bo)		rw_runlock(BO_LOCKPTR((bo)))
127a9643ea8Slogwang #define	ASSERT_BO_WLOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_WLOCKED)
128a9643ea8Slogwang #define	ASSERT_BO_LOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_LOCKED)
129a9643ea8Slogwang #define	ASSERT_BO_UNLOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_UNLOCKED)
130a9643ea8Slogwang 
131*22ce4affSfengbojiang void bufobj_init(struct bufobj *bo, void *priv);
132a9643ea8Slogwang void bufobj_wdrop(struct bufobj *bo);
133a9643ea8Slogwang void bufobj_wref(struct bufobj *bo);
134a9643ea8Slogwang void bufobj_wrefl(struct bufobj *bo);
135a9643ea8Slogwang int bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo);
136a9643ea8Slogwang int bufobj_wwait(struct bufobj *bo, int slpflag, int timeo);
137a9643ea8Slogwang int bufsync(struct bufobj *bo, int waitfor);
138a9643ea8Slogwang void bufbdflush(struct bufobj *bo, struct buf *bp);
139a9643ea8Slogwang 
140a9643ea8Slogwang #endif /* defined(_KERNEL) || defined(_KVM_VNODE) */
141a9643ea8Slogwang #endif /* _SYS_BUFOBJ_H_ */
142