1/* 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * The contents of this file constitute Original Code as defined in and 7 * are subject to the Apple Public Source License Version 1.1 (the 8 * "License"). You may not use this file except in compliance with the 9 * License. Please obtain a copy of the License at 10 * http://www.apple.com/publicsource and read it before using this file. 11 * 12 * This Original Code and all software distributed under the License are 13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 17 * License for the specific language governing rights and limitations 18 * under the License. 19 * 20 * @APPLE_LICENSE_HEADER_END@ 21 */ 22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ 23/* 24 * Copyright (c) 1982, 1986, 1989, 1993 25 * The Regents of the University of California. All rights reserved. 26 * (c) UNIX System Laboratories, Inc. 27 * All or some portions of this file are derived from material licensed 28 * to the University of California by American Telephone and Telegraph 29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 30 * the permission of UNIX System Laboratories, Inc. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by the University of 43 * California, Berkeley and its contributors. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)buf.h 8.9 (Berkeley) 3/30/95 61 */ 62 63#ifndef _SYS_BUF_INTERNAL_H_ 64#define _SYS_BUF_INTERNAL_H_ 65 66#include <sys/appleapiopts.h> 67 68#ifdef KERNEL 69#include <sys/queue.h> 70#include <sys/errno.h> 71#include <sys/vm.h> 72#include <sys/cdefs.h> 73#include <sys/buf.h> 74#include <sys/lock.h> 75 76 77extern lck_mtx_t *buf_mtxp; 78#define NOLIST ((struct buf *)0x87654321) 79 80/* 81 * The buffer header describes an I/O operation in the kernel. 82 */ 83struct buf { 84 LIST_ENTRY(buf) b_hash; /* Hash chain. */ 85 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */ 86 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */ 87 int b_timestamp; /* timestamp for queuing operation */ 88 long b_whichq; /* the free list the buffer belongs to */ 89 volatile long b_flags; /* B_* flags. */ 90 volatile long b_lflags; /* BL_BUSY | BL_WANTED flags... protected by buf_mtx */ 91 int b_error; /* errno value. */ 92 long b_bufsize; /* Allocated buffer size. */ 93 long b_bcount; /* Valid bytes in buffer. */ 94 long b_resid; /* Remaining I/O. */ 95 dev_t b_dev; /* Device associated with buffer. */ 96 uintptr_t b_datap; /* Memory, superblocks, indirect etc.*/ 97 daddr64_t b_lblkno; /* Logical block number. */ 98 daddr64_t b_blkno; /* Underlying physical block number. */ 99 void (*b_iodone)(buf_t, void *); /* Function to call upon completion. */ 100 vnode_t b_vp; /* Device vnode. */ 101 struct ucred *b_rcred; /* Read credentials reference. */ 102 struct ucred *b_wcred; /* Write credentials reference. */ 103 void * b_upl; /* Pointer to UPL */ 104 buf_t b_real_bp; /* used to track bp generated through cluster_bp */ 105 TAILQ_ENTRY(buf) b_act; /* Device driver queue when active */ 106 void * b_drvdata; /* Device driver private use */ 107 void * b_fsprivate; /* filesystem private use */ 108 void * b_transaction; /* journal private use */ 109 int b_dirtyoff; /* Offset in buffer of dirty region. */ 110 int b_dirtyend; /* Offset of end of dirty region. */ 111 int b_validoff; /* Offset in buffer of valid region. */ 112 int b_validend; /* Offset of end of valid region. */ 113 proc_t b_proc; /* Associated proc; NULL if kernel. */ 114#ifdef JOE_DEBUG 115 void * b_owner; 116 int b_tag; 117 void * b_lastbrelse; 118 int b_stackbrelse[6]; 119 int b_stackgetblk[6]; 120#endif 121}; 122 123 124/* cluster_io definitions for use with io bufs */ 125#define b_uploffset b_bufsize 126#define b_trans_head b_freelist.tqe_prev 127#define b_trans_next b_freelist.tqe_next 128#define b_iostate b_rcred 129 130/* 131 * These flags are kept in b_lflags... 132 * buf_mtxp must be held before examining/updating 133 */ 134#define BL_BUSY 0x00000001 /* I/O in progress. */ 135#define BL_WANTED 0x00000002 /* Process wants this buffer. */ 136#define BL_IOBUF 0x00000004 /* buffer allocated via 'buf_alloc' */ 137 138 139/* 140 * mask used by buf_flags... these are the readable external flags 141 */ 142#define BUF_X_RDFLAGS (B_CLUSTER | B_PHYS | B_LOCKED | B_DELWRI | B_ASYNC |\ 143 B_READ | B_WRITE | B_META | B_PAGEIO) 144/* 145 * mask used by buf_clearflags/buf_setflags... these are the writable external flags 146 */ 147#define BUF_X_WRFLAGS (B_LOCKED | B_NOCACHE | B_ASYNC | B_READ | B_WRITE | B_PAGEIO) 148 149/* 150 * These flags are kept in b_flags... access is lockless 151 * External flags are defined in buf.h and cannot overlap 152 * the internal flags 153 * 154 * these flags are internal... there definition may change 155 */ 156#define B_CACHE 0x00010000 /* getblk found us in the cache. */ 157#define B_DONE 0x00020000 /* I/O completed. */ 158#define B_INVAL 0x00040000 /* Does not contain valid info. */ 159#define B_ERROR 0x00080000 /* I/O error occurred. */ 160#define B_EINTR 0x00100000 /* I/O was interrupted */ 161#define B_AGE 0x00200000 /* Move to age queue when I/O done. */ 162#define B_FILTER 0x00400000 /* call b_iodone from biodone as an in-line filter */ 163#define B_CALL 0x00800000 /* Call b_iodone from biodone, assumes b_iodone consumes bp */ 164#define B_RAW 0x01000000 /* Set by physio for raw transfers. */ 165#define B_WASDIRTY 0x02000000 /* page was found dirty in the VM cache */ 166#define B_HDRALLOC 0x04000000 /* zone allocated buffer header */ 167#define B_ZALLOC 0x08000000 /* b_datap is zalloc()ed */ 168/* 169 * private flags used by the journal layer 170 */ 171#define B_NORELSE 0x10000000 /* don't brelse() in bwrite() */ 172/* 173 * private flags used by by the cluster layer 174 */ 175#define B_NEED_IODONE 0x20000000 /* need biodone on the real_bp associated with a cluster_io */ 176#define B_COMMIT_UPL 0x40000000 /* commit/abort the UPL on I/O success/failure */ 177/* 178 * can we deprecate? 179 */ 180#define B_TAPE 0x80000000 /* Magnetic tape I/O. */ 181 182 183/* Flags to low-level allocation routines. */ 184#define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */ 185#define B_SYNC 0x02 /* Do all allocations synchronously. */ 186#define B_NOBUFF 0x04 /* Do not allocate struct buf */ 187 188 189extern int niobuf; /* The number of IO buffer headers for cluster IO */ 190extern int nbuf; /* The number of buffer headers */ 191extern struct buf *buf; /* The buffer headers. */ 192 193 194/* 195 * Definitions for the buffer free lists. 196 */ 197#define BQUEUES 6 /* number of free buffer queues */ 198 199#define BQ_LOCKED 0 /* super-blocks &c */ 200#define BQ_LRU 1 /* lru, useful buffers */ 201#define BQ_AGE 2 /* rubbish */ 202#define BQ_EMPTY 3 /* buffer headers with no memory */ 203#define BQ_META 4 /* buffer containing metadata */ 204#define BQ_LAUNDRY 5 /* buffers that need cleaning */ 205 206 207__BEGIN_DECLS 208 209buf_t alloc_io_buf(vnode_t, int); 210void free_io_buf(buf_t); 211 212int allocbuf(struct buf *, int); 213void bufinit(void); 214 215void buf_setfilter(buf_t, void (*)(buf_t, void *), void *, void **, void **); 216 217/* 218 * Flags for buf_acquire 219 */ 220#define BAC_NOWAIT 0x01 /* Don't wait if buffer is busy */ 221#define BAC_REMOVE 0x02 /* Remove from free list once buffer is acquired */ 222#define BAC_SKIP_NONLOCKED 0x04 /* Don't return LOCKED buffers */ 223#define BAC_SKIP_LOCKED 0x08 /* Only return LOCKED buffers */ 224 225void cluster_init(void); 226void buf_drop(buf_t); 227errno_t buf_acquire(buf_t, int, int, int); 228 229int count_busy_buffers(void); 230int count_lock_queue(void); 231 232 233__END_DECLS 234 235 236/* 237 * Stats on usefulness of the buffer cache 238 */ 239struct bufstats { 240 long bufs_incore; /* found incore */ 241 long bufs_busyincore; /* found incore. was busy */ 242 long bufs_vmhits; /* not incore. found in VM */ 243 long bufs_miss; /* not incore. not in VM */ 244 long bufs_sleeps; /* buffer starvation */ 245 long bufs_eblk; /* Calls to geteblk */ 246 long bufs_iobufmax; /* Max. number of IO buffers used */ 247 long bufs_iobufinuse; /* number of IO buffers in use */ 248 long bufs_iobufsleeps; /* IO buffer starvation */ 249}; 250 251#endif /* KERNEL */ 252#endif /* !_SYS_BUF_H_ */ 253

