1136745Sphk/*-
2136745Sphk * Copyright (c) 2004 Poul-Henning Kamp
3136745Sphk * All rights reserved.
4136745Sphk *
5136745Sphk * Redistribution and use in source and binary forms, with or without
6136745Sphk * modification, are permitted provided that the following conditions
7136745Sphk * are met:
8136745Sphk * 1. Redistributions of source code must retain the above copyright
9136745Sphk *    notice, this list of conditions and the following disclaimer.
10136745Sphk * 2. Redistributions in binary form must reproduce the above copyright
11136745Sphk *    notice, this list of conditions and the following disclaimer in the
12136745Sphk *    documentation and/or other materials provided with the distribution.
13136745Sphk *
14136745Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15136745Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16136745Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17136745Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18136745Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19136745Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20136745Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21136745Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22136745Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23136745Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24136745Sphk * SUCH DAMAGE.
25136745Sphk *
26136745Sphk * $FreeBSD$
27136745Sphk */
28136745Sphk
29136745Sphk/*
30136745Sphk * Architectural notes:
31136745Sphk *
32136745Sphk * bufobj is a new object which is what buffers hang from in the buffer
33136745Sphk * cache.
34136745Sphk *
35136745Sphk * This used to be vnodes, but we need non-vnode code to be able
36136745Sphk * to use the buffer cache as well, specifically geom classes like gbde,
37136745Sphk * raid3 and raid5.
38136745Sphk *
39136745Sphk * All vnodes will contain a bufobj initially, but down the road we may
40136745Sphk * want to only allocate bufobjs when they are needed.  There could be a
41136745Sphk * large number of vnodes in the system which wouldn't need a bufobj during
42136745Sphk * their lifetime.
43136745Sphk *
44136745Sphk * The exact relationship to the vmobject is not determined at this point,
45160603Srwatson * it may in fact be that we find them to be two sides of the same object
46136745Sphk * once things starts to crystalize.
47136745Sphk */
48136745Sphk
49136745Sphk#ifndef _SYS_BUFOBJ_H_
50136745Sphk#define _SYS_BUFOBJ_H_
51136745Sphk
52136745Sphk#if defined(_KERNEL) || defined(_KVM_VNODE)
53136745Sphk
54136745Sphk#include <sys/queue.h>
55177493Sjeff#include <sys/_lock.h>
56251171Sjeff#include <sys/_rwlock.h>
57250551Sjeff#include <sys/_pctrie.h>
58136745Sphk
59136927Sphkstruct bufobj;
60136927Sphkstruct buf_ops;
61136927Sphk
62136927Sphkextern struct buf_ops buf_ops_bio;
63136927Sphk
64136745SphkTAILQ_HEAD(buflists, buf);
65136745Sphk
66136745Sphk/* A Buffer splay list */
67136745Sphkstruct bufv {
68136745Sphk	struct buflists	bv_hd;		/* Sorted blocklist */
69250551Sjeff	struct pctrie	bv_root;	/* Buf trie */
70136745Sphk	int		bv_cnt;		/* Number of buffers */
71136745Sphk};
72136745Sphk
73136927Sphktypedef void b_strategy_t(struct bufobj *, struct buf *);
74136927Sphktypedef int b_write_t(struct buf *);
75183754Sattiliotypedef int b_sync_t(struct bufobj *, int waitfor);
76166193Skibtypedef void b_bdflush_t(struct bufobj *, struct buf *);
77136927Sphk
78136927Sphkstruct buf_ops {
79136927Sphk	char		*bop_name;
80136927Sphk	b_write_t	*bop_write;
81136927Sphk	b_strategy_t	*bop_strategy;
82140056Sphk	b_sync_t	*bop_sync;
83166193Skib	b_bdflush_t	*bop_bdflush;
84136927Sphk};
85136927Sphk
86140051Sphk#define BO_STRATEGY(bo, bp)	((bo)->bo_ops->bop_strategy((bo), (bp)))
87183754Sattilio#define BO_SYNC(bo, w)		((bo)->bo_ops->bop_sync((bo), (w)))
88140051Sphk#define BO_WRITE(bo, bp)	((bo)->bo_ops->bop_write((bp)))
89166193Skib#define BO_BDFLUSH(bo, bp)	((bo)->bo_ops->bop_bdflush((bo), (bp)))
90140051Sphk
91136745Sphkstruct bufobj {
92251171Sjeff	struct rwlock	bo_lock;	/* Lock which protects "i" things */
93136966Sphk	struct buf_ops	*bo_ops;	/* - Buffer operations */
94136962Sphk	struct vm_object *bo_object;	/* v Place to store VM object */
95136992Sphk	LIST_ENTRY(bufobj) bo_synclist;	/* S dirty vnode list */
96136992Sphk	void		*bo_private;	/* private pointer */
97137033Sphk	struct vnode	*__bo_vnode;	/*
98137033Sphk					 * XXX: This vnode pointer is here
99137033Sphk					 * XXX: only to keep the syncer working
100137033Sphk					 * XXX: for now.
101137033Sphk					 */
102245410Skib	struct bufv	bo_clean;	/* i Clean buffers */
103245410Skib	struct bufv	bo_dirty;	/* i Dirty buffers */
104245410Skib	long		bo_numoutput;	/* i Writes in progress */
105245410Skib	u_int		bo_flag;	/* i Flags */
106245410Skib	int		bo_bsize;	/* - Block size for i/o */
107136745Sphk};
108136745Sphk
109136992Sphk/*
110136992Sphk * XXX BO_ONWORKLST could be replaced with a check for NULL list elements
111136992Sphk * in v_synclist.
112136992Sphk */
113136992Sphk#define	BO_ONWORKLST	(1 << 0)	/* On syncer work-list */
114136751Sphk#define	BO_WWAIT	(1 << 1)	/* Wait for output to complete */
115136751Sphk
116251171Sjeff#define	BO_LOCKPTR(bo)		(&(bo)->bo_lock)
117251171Sjeff#define	BO_LOCK(bo)		rw_wlock(BO_LOCKPTR((bo)))
118251171Sjeff#define	BO_UNLOCK(bo)		rw_wunlock(BO_LOCKPTR((bo)))
119251171Sjeff#define	BO_RLOCK(bo)		rw_rlock(BO_LOCKPTR((bo)))
120251171Sjeff#define	BO_RUNLOCK(bo)		rw_runlock(BO_LOCKPTR((bo)))
121251171Sjeff#define	ASSERT_BO_WLOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_WLOCKED)
122251171Sjeff#define	ASSERT_BO_LOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_LOCKED)
123251171Sjeff#define	ASSERT_BO_UNLOCKED(bo)	rw_assert(BO_LOCKPTR((bo)), RA_UNLOCKED)
124136750Sphk
125136751Sphkvoid bufobj_wdrop(struct bufobj *bo);
126136751Sphkvoid bufobj_wref(struct bufobj *bo);
127146801Sjeffvoid bufobj_wrefl(struct bufobj *bo);
128183754Sattilioint bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo);
129136751Sphkint bufobj_wwait(struct bufobj *bo, int slpflag, int timeo);
130183754Sattilioint bufsync(struct bufobj *bo, int waitfor);
131166193Skibvoid bufbdflush(struct bufobj *bo, struct buf *bp);
132136751Sphk
133136745Sphk#endif /* defined(_KERNEL) || defined(_KVM_VNODE) */
134136745Sphk#endif /* _SYS_BUFOBJ_H_ */
135