bufobj.h revision 140056
1139823Simp/*-
21541Srgrimes * Copyright (c) 2004 Poul-Henning Kamp
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes *
261541Srgrimes * $FreeBSD: head/sys/sys/bufobj.h 140056 2005-01-11 10:43:08Z phk $
271541Srgrimes */
281541Srgrimes
291541Srgrimes/*
3050477Speter * Architectural notes:
311541Srgrimes *
321541Srgrimes * bufobj is a new object which is what buffers hang from in the buffer
332169Spaul * cache.
34121498Sume *
352169Spaul * This used to be vnodes, but we need non-vnode code to be able
3695336Smike * to use the buffer cache as well, specifically geom classes like gbde,
3793514Smike * raid3 and raid5.
3895336Smike *
3993514Smike * All vnodes will contain a bufobj initially, but down the road we may
4095336Smike * want to only allocate bufobjs when they are needed.  There could be a
4195336Smike * large number of vnodes in the system which wouldn't need a bufobj during
4295336Smike * their lifetime.
4395336Smike *
4495336Smike * The exact relationship to the vmobject is not determined at this point,
4595336Smike * it may in fact bee that we find them to be two sides of the same object
4695336Smike * once things starts to crystalize.
4795336Smike */
4895336Smike
4995336Smike#ifndef _SYS_BUFOBJ_H_
5095336Smike#define _SYS_BUFOBJ_H_
5195336Smike
5295336Smike#if defined(_KERNEL) || defined(_KVM_VNODE)
5395336Smike
5495336Smike#include <sys/queue.h>
5595336Smike
5695336Smikestruct bufobj;
5795336Smikestruct buf_ops;
5895336Smike
5995336Smikeextern struct buf_ops buf_ops_bio;
6095336Smike
6195336SmikeTAILQ_HEAD(buflists, buf);
6295336Smike
6395336Smike/* A Buffer splay list */
6495336Smikestruct bufv {
6595336Smike	struct buflists	bv_hd;		/* Sorted blocklist */
6695336Smike	struct buf	*bv_root;	/* Buf splay tree */
6795336Smike	int		bv_cnt;		/* Number of buffers */
6895336Smike};
6995336Smike
7095336Smiketypedef void b_strategy_t(struct bufobj *, struct buf *);
7195336Smiketypedef int b_write_t(struct buf *);
7295336Smiketypedef int b_sync_t(struct bufobj *, int waitfor, struct thread *td);
7395336Smike
74102227Smikestruct buf_ops {
75102227Smike	char		*bop_name;
76102227Smike	b_write_t	*bop_write;
7795336Smike	b_strategy_t	*bop_strategy;
7895336Smike	b_sync_t	*bop_sync;
7995336Smike};
8095336Smike
8195336Smike#define BO_STRATEGY(bo, bp)	((bo)->bo_ops->bop_strategy((bo), (bp)))
8295336Smike#define BO_SYNC(bo, w, td)	((bo)->bo_ops->bop_sync((bo), (w), (td)))
8395336Smike#define BO_WRITE(bo, bp)	((bo)->bo_ops->bop_write((bp)))
8495336Smike
8595336Smikestruct bufobj {
8695336Smike	struct mtx	*bo_mtx;	/* Mutex which protects "i" things */
8795336Smike	struct bufv	bo_clean;	/* i Clean buffers */
8895336Smike	struct bufv	bo_dirty;	/* i Dirty buffers */
8995336Smike	long		bo_numoutput;	/* i Writes in progress */
9095336Smike	u_int		bo_flag;	/* i Flags */
9195336Smike	struct buf_ops	*bo_ops;	/* - Buffer operations */
9295336Smike	int		bo_bsize;	/* - Block size for i/o */
9395336Smike	struct vm_object *bo_object;	/* v Place to store VM object */
9495336Smike	LIST_ENTRY(bufobj) bo_synclist;	/* S dirty vnode list */
9595336Smike	void		*bo_private;	/* private pointer */
9695336Smike	struct vnode	*__bo_vnode;	/*
9795336Smike					 * XXX: This vnode pointer is here
9895336Smike					 * XXX: only to keep the syncer working
9995336Smike					 * XXX: for now.
10095336Smike					 */
10195336Smike};
10295336Smike
10395336Smike/*
10495336Smike * XXX BO_ONWORKLST could be replaced with a check for NULL list elements
10595336Smike * in v_synclist.
10695336Smike */
10795336Smike#define	BO_ONWORKLST	(1 << 0)	/* On syncer work-list */
10895336Smike#define	BO_WWAIT	(1 << 1)	/* Wait for output to complete */
10995336Smike
11095336Smike#define	BO_LOCK(bo) \
11195336Smike	do { \
11295336Smike		KASSERT (bo->bo_mtx != NULL, ("No lock in bufobj")); \
11395336Smike		mtx_lock((bo)->bo_mtx); \
11495336Smike	} while (0)
11595336Smike
11695336Smike#define BO_UNLOCK(bo) \
11795336Smike	do { \
11895336Smike		KASSERT (bo->bo_mtx != NULL, ("No lock in bufobj")); \
11995336Smike		mtx_unlock((bo)->bo_mtx); \
12095336Smike	} while (0)
12195336Smike
12295336Smike#define	BO_MTX(bo)		((bo)->bo_mtx)
12395336Smike#define	ASSERT_BO_LOCKED(bo)	mtx_assert(bo->bo_mtx, MA_OWNED)
1241541Srgrimes#define	ASSERT_BO_UNLOCKED(bo)	mtx_assert(bo->bo_mtx, MA_NOTOWNED)
1251541Srgrimes
1261541Srgrimesvoid bufobj_wdrop(struct bufobj *bo);
1271541Srgrimesvoid bufobj_wref(struct bufobj *bo);
1281541Srgrimesint bufobj_wwait(struct bufobj *bo, int slpflag, int timeo);
1291541Srgrimesint bufsync(struct bufobj *bo, int waitfor, struct thread *td);
13033804Sjulian
1311541Srgrimes#endif /* defined(_KERNEL) || defined(_KVM_VNODE) */
13252904Sshin#endif /* _SYS_BUFOBJ_H_ */
1331541Srgrimes