]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/buf.h
912d07a49a1bfbb9d90670988bd6b5bb92a5bfca
[apple/xnu.git] / bsd / sys / buf.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)buf.h 8.9 (Berkeley) 3/30/95
69 */
70
71 #ifndef _SYS_BUF_H_
72 #define _SYS_BUF_H_
73
74 #include <sys/cdefs.h>
75 #include <sys/kernel_types.h>
76 #include <mach/memory_object_types.h>
77
78
79 #define B_WRITE 0x00000000 /* Write buffer (pseudo flag). */
80 #define B_READ 0x00000001 /* Read buffer. */
81 #define B_ASYNC 0x00000002 /* Start I/O, do not wait. */
82 #define B_NOCACHE 0x00000004 /* Do not cache block after use. */
83 #define B_DELWRI 0x00000008 /* Delay I/O until buffer reused. */
84 #define B_LOCKED 0x00000010 /* Locked in core (not reusable). */
85 #define B_PHYS 0x00000020 /* I/O to user memory. */
86 #define B_CLUSTER 0x00000040 /* UPL based I/O generated by cluster layer */
87 #define B_PAGEIO 0x00000080 /* Page in/out */
88 #define B_META 0x00000100 /* buffer contains meta-data. */
89 /*
90 * make sure to check when adding flags that
91 * that the new flags don't overlap the definitions
92 * in buf_internal.h
93 */
94
95 __BEGIN_DECLS
96
97 /*
98 * mark the buffer associated with buf_t
99 * as AGED with respect to the LRU cache
100 */
101 void buf_markaged(buf_t);
102
103 /*
104 * mark the buffer associated with buf_t
105 * as invalid... on release, it will go
106 * directly to the free list
107 */
108 void buf_markinvalid(buf_t);
109
110 /*
111 * mark the buffer assoicated with buf_t
112 * as a delayed write...
113 */
114 void buf_markdelayed(buf_t);
115
116 /*
117 * mark the buffer associated with buf_t
118 * as having been interrupted... EINTR
119 */
120 void buf_markeintr(buf_t);
121
122 /*
123 * returns 1 if the buffer associated with buf_t
124 * contains valid data... 0 if it does not
125 */
126 int buf_valid(buf_t);
127
128 /*
129 * returns 1 if the buffer was already valid
130 * in the cache... i.e. no I/O was performed
131 * returns 0 otherwise
132 */
133 int buf_fromcache(buf_t);
134
135 /*
136 * returns the UPL associated with buf_t
137 */
138 void * buf_upl(buf_t);
139
140 /*
141 * returns the offset into the UPL
142 * associated with buf_t which is to be
143 * used as the base offset for this I/O
144 */
145 uint32_t buf_uploffset(buf_t);
146
147 /*
148 * returns read credential associated with buf_t
149 * a reference is taken which must be explicilty dropped
150 */
151 ucred_t buf_rcred(buf_t);
152
153 /*
154 * returns write credential associated with buf_t
155 * a reference is taken which must be explicilty dropped
156 */
157 ucred_t buf_wcred(buf_t);
158
159 /*
160 * returns process handle associated with buf_t
161 * i.e identity of task that issued the I/O
162 */
163 proc_t buf_proc(buf_t);
164
165 uint32_t buf_dirtyoff(buf_t);
166 uint32_t buf_dirtyend(buf_t);
167 void buf_setdirtyoff(buf_t, uint32_t);
168 void buf_setdirtyend(buf_t, uint32_t);
169
170 /*
171 * return the errno value associated with buf_t
172 */
173 errno_t buf_error(buf_t);
174
175 /*
176 * set errno on buf_t
177 */
178 void buf_seterror(buf_t, errno_t);
179
180 /*
181 * set specified flags on buf_t
182 * B_LOCKED/B_NOCACHE/B_ASYNC/B_READ/B_WRITE/B_PAGEIO
183 */
184 void buf_setflags(buf_t, int32_t);
185
186 /*
187 * clear specified flags on buf_t
188 * B_LOCKED/B_NOCACHE/B_ASYNC/B_READ/B_WRITE/B_PAGEIO
189 */
190 void buf_clearflags(buf_t, int32_t);
191
192 /*
193 * return external flags associated with buf_t
194 * B_CLUSTER/B_PHYS/B_LOCKED/B_DELWRI/B_ASYNC/B_READ/B_WRITE/B_META/B_PAGEIO
195 */
196 int32_t buf_flags(buf_t);
197
198 /*
199 * clears I/O related flags (both internal and
200 * external) associated with buf_t and allows
201 * the following to be set...
202 * B_READ/B_WRITE/B_ASYNC/B_NOCACHE
203 */
204 void buf_reset(buf_t, int32_t);
205
206 /*
207 * insure that the data storage associated with buf_t
208 * is addressable
209 */
210 errno_t buf_map(buf_t, caddr_t *);
211
212 /*
213 * release our need to have the storage associated
214 * with buf_t in an addressable state
215 */
216 errno_t buf_unmap(buf_t);
217
218 /*
219 * set driver specific data for buf_t
220 */
221 void buf_setdrvdata(buf_t, void *);
222
223 /*
224 * retrieve driver specific data associated with buf_t
225 */
226 void * buf_drvdata(buf_t);
227
228 /*
229 * set fs specific data for buf_t
230 */
231 void buf_setfsprivate(buf_t, void *);
232
233 /*
234 * retrieve driver specific data associated with buf_t
235 */
236 void * buf_fsprivate(buf_t);
237
238 /*
239 * retrieve the phsyical block number associated with buf_t
240 */
241 daddr64_t buf_blkno(buf_t);
242
243 /*
244 * retrieve the logical block number associated with buf_t
245 * i.e. the block number derived from the file offset
246 */
247 daddr64_t buf_lblkno(buf_t);
248
249 /*
250 * set the phsyical block number associated with buf_t
251 */
252 void buf_setblkno(buf_t, daddr64_t);
253
254 /*
255 * set the logical block number associated with buf_t
256 * i.e. the block number derived from the file offset
257 */
258 void buf_setlblkno(buf_t, daddr64_t);
259
260 /*
261 * retrieve the count of valid bytes associated with buf_t
262 */
263 uint32_t buf_count(buf_t);
264
265 /*
266 * retrieve the size of the data store assoicated with buf_t
267 */
268 uint32_t buf_size(buf_t);
269
270 /*
271 * retrieve the residual I/O count assoicated with buf_t
272 * i.e. number of bytes that have not yet been completed
273 */
274 uint32_t buf_resid(buf_t);
275
276 /*
277 * set the count of bytes associated with buf_t
278 * typically used to set the size of the I/O to be performed
279 */
280 void buf_setcount(buf_t, uint32_t);
281
282 /*
283 * set the size of the buffer store associated with buf_t
284 * typically used when providing private storage to buf_t
285 */
286 void buf_setsize(buf_t, uint32_t);
287
288 /*
289 * set the size in bytes of the unfinished I/O associated with buf_t
290 */
291 void buf_setresid(buf_t, uint32_t);
292
293 /*
294 * associate kernel addressable storage with buf_t
295 */
296 void buf_setdataptr(buf_t, uintptr_t);
297
298 /*
299 * retrieve pointer to buffer associated with buf_t
300 * if non-null, than guaranteed to be kernel addressable
301 * size of buffer can be retrieved via buf_size
302 * size of valid data can be retrieved via buf_count
303 * if NULL, than use buf_map/buf_unmap to manage access to the underlying storage
304 */
305 uintptr_t buf_dataptr(buf_t);
306
307 /*
308 * return the vnode_t associated with buf_t
309 */
310 vnode_t buf_vnode(buf_t);
311
312 /*
313 * assign vnode_t to buf_t... the
314 * device currently associated with
315 * but_t is not changed.
316 */
317 void buf_setvnode(buf_t, vnode_t);
318
319 /*
320 * return the dev_t associated with buf_t
321 */
322 dev_t buf_device(buf_t);
323
324 /*
325 * assign the dev_t associated with vnode_t
326 * to buf_t
327 */
328 errno_t buf_setdevice(buf_t, vnode_t);
329
330 errno_t buf_strategy(vnode_t, void *);
331
332 /*
333 * flags for buf_invalblkno
334 */
335 #define BUF_WAIT 0x01
336
337 errno_t buf_invalblkno(vnode_t, daddr64_t, int);
338
339
340 /*
341 * return the callback function pointer
342 * if the callback is still valid
343 * returns NULL if a buffer that was not
344 * allocated via buf_alloc is specified
345 * or if a callback has not been set or
346 * it has already fired...
347 */
348 void * buf_callback(buf_t);
349
350 /*
351 * assign a one-shot callback function (driven from biodone)
352 * to a buf_t allocated via buf_alloc... a caller specified
353 * arg is passed to the callback function
354 */
355 errno_t buf_setcallback(buf_t, void (*)(buf_t, void *), void *);
356
357 /*
358 * add a upl_t to a buffer allocated via buf_alloc
359 * and set the offset into the upl_t (must be page
360 * aligned).
361 */
362 errno_t buf_setupl(buf_t, upl_t, uint32_t);
363
364 /*
365 * allocate a buf_t that is a clone of the buf_t
366 * passed in, but whose I/O range is a subset...
367 * if a callback routine is specified, it will
368 * be called from buf_biodone with the bp and
369 * arg specified.
370 * it must be freed via buf_free
371 */
372 buf_t buf_clone(buf_t, int, int, void (*)(buf_t, void *), void *);
373
374 /*
375 * allocate a buf_t associated with vnode_t
376 * that has NO storage associated with it
377 * but is suitable for use in issuing I/Os
378 * after storage has been assigned via buf_setdataptr
379 * or buf_addupl
380 */
381 buf_t buf_alloc(vnode_t);
382
383 /*
384 * free a buf_t that was allocated via buf_alloc
385 * any private storage associated with buf_t is the
386 * responsiblity of the caller to release
387 */
388 void buf_free(buf_t);
389
390 /*
391 * flags for buf_invalidateblks
392 */
393 #define BUF_WRITE_DATA 0x0001 /* write data blocks first */
394 #define BUF_SKIP_META 0x0002 /* skip over metadata blocks */
395
396 int buf_invalidateblks(vnode_t, int, int, int);
397 /*
398 * flags for buf_flushdirtyblks and buf_iterate
399 */
400 #define BUF_SKIP_NONLOCKED 0x01
401 #define BUF_SKIP_LOCKED 0x02
402
403 void buf_flushdirtyblks(vnode_t, int, int, char *);
404 void buf_iterate(vnode_t, int (*)(buf_t, void *), int, void *);
405
406 #define BUF_RETURNED 0
407 #define BUF_RETURNED_DONE 1
408 #define BUF_CLAIMED 2
409 #define BUF_CLAIMED_DONE 3
410
411 /*
412 * zero the storage associated with buf_t
413 */
414 void buf_clear(buf_t);
415
416 errno_t buf_bawrite(buf_t);
417 errno_t buf_bdwrite(buf_t);
418 errno_t buf_bwrite(buf_t);
419
420 void buf_biodone(buf_t);
421 errno_t buf_biowait(buf_t);
422 void buf_brelse(buf_t);
423
424 errno_t buf_bread(vnode_t, daddr64_t, int, ucred_t, buf_t *);
425 errno_t buf_breadn(vnode_t, daddr64_t, int, daddr64_t *, int *, int, ucred_t, buf_t *);
426 errno_t buf_meta_bread(vnode_t, daddr64_t, int, ucred_t, buf_t *);
427 errno_t buf_meta_breadn(vnode_t, daddr64_t, int, daddr64_t *, int *, int, ucred_t, buf_t *);
428
429 u_int minphys(buf_t bp);
430 int physio(void (*)(buf_t), buf_t, dev_t, int , u_int (*)(buf_t), struct uio *, int );
431
432
433 /*
434 * Flags for operation type in getblk()
435 */
436 #define BLK_READ 0x01 /* buffer for read */
437 #define BLK_WRITE 0x02 /* buffer for write */
438 #define BLK_META 0x10 /* buffer for metadata */
439 /*
440 * modifier for above flags... if set, getblk will only return
441 * a bp that is already valid... i.e. found in the cache
442 */
443 #define BLK_ONLYVALID 0x80000000
444
445 /* timeout is in msecs */
446 buf_t buf_getblk(vnode_t, daddr64_t, int, int, int, int);
447 buf_t buf_geteblk(int);
448
449 __END_DECLS
450
451
452 /* Macros to clear/set/test flags. */
453 #define SET(t, f) (t) |= (f)
454 #define CLR(t, f) (t) &= ~(f)
455 #define ISSET(t, f) ((t) & (f))
456
457
458 #endif /* !_SYS_BUF_H_ */