]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/buf_internal.h
xnu-1699.26.8.tar.gz
[apple/xnu.git] / bsd / sys / buf_internal.h
CommitLineData
91447636 1/*
b0d623f7 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)buf.h 8.9 (Berkeley) 3/30/95
67 */
68
69#ifndef _SYS_BUF_INTERNAL_H_
70#define _SYS_BUF_INTERNAL_H_
71
72#include <sys/appleapiopts.h>
73
74#ifdef KERNEL
75#include <sys/queue.h>
76#include <sys/errno.h>
77#include <sys/vm.h>
78#include <sys/cdefs.h>
79#include <sys/buf.h>
80#include <sys/lock.h>
81
82
91447636
A
83#define NOLIST ((struct buf *)0x87654321)
84
7ddcb079
A
85/*
86 * Attributes of an I/O to be used by lower layers
87 */
88struct bufattr {
89 uint64_t ba_flags; /* flags. Some are only in-use on embedded devices */
90};
91
91447636
A
92/*
93 * The buffer header describes an I/O operation in the kernel.
94 */
95struct buf {
96 LIST_ENTRY(buf) b_hash; /* Hash chain. */
97 LIST_ENTRY(buf) b_vnbufs; /* Buffer's associated vnode. */
98 TAILQ_ENTRY(buf) b_freelist; /* Free list position if not active. */
99 int b_timestamp; /* timestamp for queuing operation */
b0d623f7
A
100 int b_whichq; /* the free list the buffer belongs to */
101 volatile uint32_t b_flags; /* B_* flags. */
102 volatile uint32_t b_lflags; /* BL_BUSY | BL_WANTED flags... protected by buf_mtx */
91447636 103 int b_error; /* errno value. */
b0d623f7
A
104 int b_bufsize; /* Allocated buffer size. */
105 int b_bcount; /* Valid bytes in buffer. */
106 int b_resid; /* Remaining I/O. */
91447636 107 dev_t b_dev; /* Device associated with buffer. */
b0d623f7 108 uintptr_t b_datap; /* Memory, superblocks, indirect etc.*/
91447636
A
109 daddr64_t b_lblkno; /* Logical block number. */
110 daddr64_t b_blkno; /* Underlying physical block number. */
111 void (*b_iodone)(buf_t, void *); /* Function to call upon completion. */
2d21ac55
A
112 vnode_t b_vp; /* File vnode for data, device vnode for metadata. */
113 kauth_cred_t b_rcred; /* Read credentials reference. */
114 kauth_cred_t b_wcred; /* Write credentials reference. */
91447636
A
115 void * b_upl; /* Pointer to UPL */
116 buf_t b_real_bp; /* used to track bp generated through cluster_bp */
117 TAILQ_ENTRY(buf) b_act; /* Device driver queue when active */
118 void * b_drvdata; /* Device driver private use */
b0d623f7 119 void * b_fsprivate; /* filesystem private use */
91447636
A
120 void * b_transaction; /* journal private use */
121 int b_dirtyoff; /* Offset in buffer of dirty region. */
122 int b_dirtyend; /* Offset of end of dirty region. */
123 int b_validoff; /* Offset in buffer of valid region. */
124 int b_validend; /* Offset of end of valid region. */
6d2010ae
A
125
126 /* store extra information related to redundancy of data, such as
127 * which redundancy copy to use, etc
128 */
129 uint32_t b_redundancy_flags;
130
91447636 131 proc_t b_proc; /* Associated proc; NULL if kernel. */
6d2010ae
A
132#ifdef BUF_MAKE_PRIVATE
133 buf_t b_data_store;
134#endif
d1ecb069
A
135#if CONFIG_PROTECT
136 struct cprotect *b_cpentry; /* address of cp_entry, to be passed further down */
137#endif /* CONFIG_PROTECT */
7ddcb079 138 struct bufattr b_attr;
91447636
A
139#ifdef JOE_DEBUG
140 void * b_owner;
141 int b_tag;
142 void * b_lastbrelse;
b0d623f7
A
143 void * b_stackbrelse[6];
144 void * b_stackgetblk[6];
91447636
A
145#endif
146};
147
148
149/* cluster_io definitions for use with io bufs */
150#define b_uploffset b_bufsize
6d2010ae
A
151#define b_orig b_freelist.tqe_prev
152#define b_shadow b_freelist.tqe_next
153#define b_shadow_ref b_validoff
154#ifdef BUF_MAKE_PRIVATE
155#define b_data_ref b_validend
156#endif
91447636
A
157#define b_trans_head b_freelist.tqe_prev
158#define b_trans_next b_freelist.tqe_next
159#define b_iostate b_rcred
2d21ac55 160#define b_cliodone b_wcred
91447636
A
161
162/*
163 * These flags are kept in b_lflags...
164 * buf_mtxp must be held before examining/updating
165 */
166#define BL_BUSY 0x00000001 /* I/O in progress. */
167#define BL_WANTED 0x00000002 /* Process wants this buffer. */
168#define BL_IOBUF 0x00000004 /* buffer allocated via 'buf_alloc' */
b0d623f7 169#define BL_WANTDEALLOC 0x00000010 /* buffer should be put on empty list when clean */
6d2010ae
A
170#define BL_SHADOW 0x00000020
171#define BL_EXTERNAL 0x00000040
172#define BL_WAITSHADOW 0x00000080
173#define BL_IOBUF_ALLOC 0x00000100
b0d623f7
A
174
175/*
176 * Parameters for buffer cache garbage collection
177 */
178#define BUF_STALE_THRESHHOLD 30 /* Collect if untouched in the last 30 seconds */
6d2010ae
A
179#define BUF_MAX_GC_COUNT 1024 /* Generally 6-8 MB */
180#define BUF_MAX_GC_BATCH_SIZE 128 /* Under a single grab of the lock */
91447636
A
181
182/*
183 * mask used by buf_flags... these are the readable external flags
184 */
2d21ac55 185#define BUF_X_RDFLAGS (B_PHYS | B_RAW | B_LOCKED | B_ASYNC | B_READ | B_WRITE | B_PAGEIO |\
6d2010ae
A
186 B_META | B_CLUSTER | B_DELWRI | B_FUA | B_PASSIVE | B_IOSTREAMING | B_THROTTLED_IO |\
187 B_ENCRYPTED_IO)
91447636
A
188/*
189 * mask used by buf_clearflags/buf_setflags... these are the writable external flags
190 */
2d21ac55 191#define BUF_X_WRFLAGS (B_PHYS | B_RAW | B_LOCKED | B_ASYNC | B_READ | B_WRITE | B_PAGEIO |\
b0d623f7 192 B_NOCACHE | B_FUA | B_PASSIVE | B_IOSTREAMING)
91447636
A
193
194/*
195 * These flags are kept in b_flags... access is lockless
196 * External flags are defined in buf.h and cannot overlap
197 * the internal flags
198 *
199 * these flags are internal... there definition may change
200 */
201#define B_CACHE 0x00010000 /* getblk found us in the cache. */
202#define B_DONE 0x00020000 /* I/O completed. */
203#define B_INVAL 0x00040000 /* Does not contain valid info. */
204#define B_ERROR 0x00080000 /* I/O error occurred. */
205#define B_EINTR 0x00100000 /* I/O was interrupted */
206#define B_AGE 0x00200000 /* Move to age queue when I/O done. */
207#define B_FILTER 0x00400000 /* call b_iodone from biodone as an in-line filter */
208#define B_CALL 0x00800000 /* Call b_iodone from biodone, assumes b_iodone consumes bp */
2d21ac55 209#define B_EOT 0x01000000 /* last buffer in a transaction list created by cluster_io */
91447636
A
210#define B_WASDIRTY 0x02000000 /* page was found dirty in the VM cache */
211#define B_HDRALLOC 0x04000000 /* zone allocated buffer header */
212#define B_ZALLOC 0x08000000 /* b_datap is zalloc()ed */
213/*
214 * private flags used by the journal layer
215 */
216#define B_NORELSE 0x10000000 /* don't brelse() in bwrite() */
217/*
218 * private flags used by by the cluster layer
219 */
6d2010ae 220#define B_TWANTED 0x20000000 /* but_t that is part of a cluster level transaction is wanted */
91447636 221#define B_COMMIT_UPL 0x40000000 /* commit/abort the UPL on I/O success/failure */
060df5ea 222#define B_TDONE 0x80000000 /* buf_t that is part of a cluster level transaction has completed */
91447636 223
91447636
A
224/* Flags to low-level allocation routines. */
225#define B_CLRBUF 0x01 /* Request allocated buffer be cleared. */
226#define B_SYNC 0x02 /* Do all allocations synchronously. */
227#define B_NOBUFF 0x04 /* Do not allocate struct buf */
228
7ddcb079
A
229/*
230 * ba_flags (Buffer Attribute flags)
231 * Some of these may be in-use only on embedded devices.
232 */
233#define BA_THROTTLED_IO 0x000000002
234
91447636 235
2d21ac55
A
236extern int niobuf_headers; /* The number of IO buffer headers for cluster IO */
237extern int nbuf_headers; /* The number of buffer headers */
0c530ab8
A
238extern int max_nbuf_headers; /* The max number of buffer headers */
239extern int nbuf_hashelements; /* The number of elements in bufhash */
2d21ac55 240extern struct buf *buf_headers; /* The buffer headers. */
91447636
A
241
242
243/*
244 * Definitions for the buffer free lists.
245 */
246#define BQUEUES 6 /* number of free buffer queues */
247
248#define BQ_LOCKED 0 /* super-blocks &c */
249#define BQ_LRU 1 /* lru, useful buffers */
250#define BQ_AGE 2 /* rubbish */
251#define BQ_EMPTY 3 /* buffer headers with no memory */
252#define BQ_META 4 /* buffer containing metadata */
253#define BQ_LAUNDRY 5 /* buffers that need cleaning */
254
255
256__BEGIN_DECLS
257
6d2010ae
A
258buf_t buf_create_shadow_priv(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg);
259
91447636
A
260buf_t alloc_io_buf(vnode_t, int);
261void free_io_buf(buf_t);
262
263int allocbuf(struct buf *, int);
2d21ac55 264void bufinit(void) __attribute__((section("__TEXT, initcode")));
91447636 265
91447636
A
266/*
267 * Flags for buf_acquire
268 */
269#define BAC_NOWAIT 0x01 /* Don't wait if buffer is busy */
270#define BAC_REMOVE 0x02 /* Remove from free list once buffer is acquired */
271#define BAC_SKIP_NONLOCKED 0x04 /* Don't return LOCKED buffers */
272#define BAC_SKIP_LOCKED 0x08 /* Only return LOCKED buffers */
273
2d21ac55
A
274void buf_list_lock(void);
275void buf_list_unlock(void);
276
2d21ac55 277void cluster_init(void) __attribute__((section("__TEXT, initcode")));
91447636
A
278void buf_drop(buf_t);
279errno_t buf_acquire(buf_t, int, int, int);
280
281int count_busy_buffers(void);
282int count_lock_queue(void);
283
6d2010ae
A
284#ifdef BUF_MAKE_PRIVATE
285errno_t buf_make_private(buf_t bp);
286#endif
91447636
A
287
288__END_DECLS
289
290
291/*
292 * Stats on usefulness of the buffer cache
293 */
294struct bufstats {
295 long bufs_incore; /* found incore */
296 long bufs_busyincore; /* found incore. was busy */
297 long bufs_vmhits; /* not incore. found in VM */
298 long bufs_miss; /* not incore. not in VM */
299 long bufs_sleeps; /* buffer starvation */
300 long bufs_eblk; /* Calls to geteblk */
301 long bufs_iobufmax; /* Max. number of IO buffers used */
302 long bufs_iobufinuse; /* number of IO buffers in use */
303 long bufs_iobufsleeps; /* IO buffer starvation */
304};
305
306#endif /* KERNEL */
307#endif /* !_SYS_BUF_H_ */