bufobj.h revision 258391
1256694Snp/*- 2256694Snp * Copyright (c) 2004 Poul-Henning Kamp 3256694Snp * All rights reserved. 4256694Snp * 5256694Snp * Redistribution and use in source and binary forms, with or without 6256694Snp * modification, are permitted provided that the following conditions 7256694Snp * are met: 8256694Snp * 1. Redistributions of source code must retain the above copyright 9256694Snp * notice, this list of conditions and the following disclaimer. 10256694Snp * 2. Redistributions in binary form must reproduce the above copyright 11256694Snp * notice, this list of conditions and the following disclaimer in the 12256694Snp * documentation and/or other materials provided with the distribution. 13256694Snp * 14256694Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15256694Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16256694Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17256694Snp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18256694Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19256694Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20256694Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21256694Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22256694Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23256694Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24256694Snp * SUCH DAMAGE. 25256694Snp * 26256694Snp * $FreeBSD: head/sys/sys/bufobj.h 258391 2013-11-20 13:22:22Z glebius $ 27256694Snp */ 28256694Snp 29256694Snp/* 30256694Snp * Architectural notes: 31256694Snp * 32256694Snp * bufobj is a new object which is what buffers hang from in the buffer 33256694Snp * cache. 34256694Snp * 35256694Snp * This used to be vnodes, but we need non-vnode code to be able 36256694Snp * to use the buffer cache as well, specifically geom classes like gbde, 37256694Snp * raid3 and raid5. 38256694Snp * 39256694Snp * All vnodes will contain a bufobj initially, but down the road we may 40256694Snp * want to only allocate bufobjs when they are needed. There could be a 41256694Snp * large number of vnodes in the system which wouldn't need a bufobj during 42256694Snp * their lifetime. 43256694Snp * 44256694Snp * The exact relationship to the vmobject is not determined at this point, 45256694Snp * it may in fact be that we find them to be two sides of the same object 46256694Snp * once things starts to crystalize. 47256694Snp */ 48256694Snp 49256694Snp#ifndef _SYS_BUFOBJ_H_ 50256694Snp#define _SYS_BUFOBJ_H_ 51256694Snp 52256694Snp#if defined(_KERNEL) || defined(_KVM_VNODE) 53256694Snp 54256694Snp#include <sys/queue.h> 55256694Snp#include <sys/_lock.h> 56256694Snp#include <sys/_rwlock.h> 57256694Snp#include <sys/_pctrie.h> 58256694Snp 59256694Snpstruct bufobj; 60256694Snpstruct buf_ops; 61256694Snp 62256694Snpextern struct buf_ops buf_ops_bio; 63256694Snp 64256694SnpTAILQ_HEAD(buflists, buf); 65256694Snp 66256694Snp/* A Buffer list & trie */ 67256694Snpstruct bufv { 68256694Snp struct buflists bv_hd; /* Sorted blocklist */ 69256694Snp struct pctrie bv_root; /* Buf trie */ 70256694Snp int bv_cnt; /* Number of buffers */ 71256694Snp}; 72256694Snp 73256694Snptypedef void b_strategy_t(struct bufobj *, struct buf *); 74256694Snptypedef int b_write_t(struct buf *); 75256694Snptypedef int b_sync_t(struct bufobj *, int waitfor); 76256694Snptypedef void b_bdflush_t(struct bufobj *, struct buf *); 77256694Snp 78256694Snpstruct buf_ops { 79256694Snp char *bop_name; 80256694Snp b_write_t *bop_write; 81256694Snp b_strategy_t *bop_strategy; 82256694Snp b_sync_t *bop_sync; 83256694Snp b_bdflush_t *bop_bdflush; 84256694Snp}; 85256694Snp 86256694Snp#define BO_STRATEGY(bo, bp) ((bo)->bo_ops->bop_strategy((bo), (bp))) 87256694Snp#define BO_SYNC(bo, w) ((bo)->bo_ops->bop_sync((bo), (w))) 88256694Snp#define BO_WRITE(bo, bp) ((bo)->bo_ops->bop_write((bp))) 89256694Snp#define BO_BDFLUSH(bo, bp) ((bo)->bo_ops->bop_bdflush((bo), (bp))) 90256694Snp 91256694Snpstruct bufobj { 92256694Snp struct rwlock bo_lock; /* Lock which protects "i" things */ 93256694Snp struct buf_ops *bo_ops; /* - Buffer operations */ 94256694Snp struct vm_object *bo_object; /* v Place to store VM object */ 95256694Snp LIST_ENTRY(bufobj) bo_synclist; /* S dirty vnode list */ 96256694Snp void *bo_private; /* private pointer */ 97256694Snp struct vnode *__bo_vnode; /* 98256694Snp * XXX: This vnode pointer is here 99256694Snp * XXX: only to keep the syncer working 100256694Snp * XXX: for now. 101256694Snp */ 102256694Snp struct bufv bo_clean; /* i Clean buffers */ 103256694Snp struct bufv bo_dirty; /* i Dirty buffers */ 104256694Snp long bo_numoutput; /* i Writes in progress */ 105256694Snp u_int bo_flag; /* i Flags */ 106256694Snp int bo_bsize; /* - Block size for i/o */ 107256694Snp}; 108256694Snp 109256694Snp/* 110256694Snp * XXX BO_ONWORKLST could be replaced with a check for NULL list elements 111256694Snp * in v_synclist. 112256694Snp */ 113256694Snp#define BO_ONWORKLST (1 << 0) /* On syncer work-list */ 114256694Snp#define BO_WWAIT (1 << 1) /* Wait for output to complete */ 115256694Snp 116256694Snp#define BO_LOCKPTR(bo) (&(bo)->bo_lock) 117256694Snp#define BO_LOCK(bo) rw_wlock(BO_LOCKPTR((bo))) 118256694Snp#define BO_UNLOCK(bo) rw_wunlock(BO_LOCKPTR((bo))) 119256694Snp#define BO_RLOCK(bo) rw_rlock(BO_LOCKPTR((bo))) 120256694Snp#define BO_RUNLOCK(bo) rw_runlock(BO_LOCKPTR((bo))) 121256694Snp#define ASSERT_BO_WLOCKED(bo) rw_assert(BO_LOCKPTR((bo)), RA_WLOCKED) 122256694Snp#define ASSERT_BO_LOCKED(bo) rw_assert(BO_LOCKPTR((bo)), RA_LOCKED) 123256694Snp#define ASSERT_BO_UNLOCKED(bo) rw_assert(BO_LOCKPTR((bo)), RA_UNLOCKED) 124256694Snp 125256694Snpvoid bufobj_wdrop(struct bufobj *bo); 126256694Snpvoid bufobj_wref(struct bufobj *bo); 127256694Snpvoid bufobj_wrefl(struct bufobj *bo); 128256694Snpint bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo); 129256694Snpint bufobj_wwait(struct bufobj *bo, int slpflag, int timeo); 130256694Snpint bufsync(struct bufobj *bo, int waitfor); 131256694Snpvoid bufbdflush(struct bufobj *bo, struct buf *bp); 132256694Snp 133256694Snp#endif /* defined(_KERNEL) || defined(_KVM_VNODE) */ 134256694Snp#endif /* _SYS_BUFOBJ_H_ */ 135256694Snp