Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/i386/include/sys/buf.h

1/*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.h8.9 (Berkeley) 3/30/95
67 */
68
69#ifndef _SYS_BUF_H_
70#define_SYS_BUF_H_
71
72#include <sys/cdefs.h>
73#include <sys/kernel_types.h>
74#include <mach/memory_object_types.h>
75
76
77#defineB_WRITE0x00000000/* Write buffer (pseudo flag). */
78#defineB_READ0x00000001/* Read buffer. */
79#defineB_ASYNC0x00000002/* Start I/O, do not wait. */
80#defineB_NOCACHE0x00000004/* Do not cache block after use. */
81#defineB_DELWRI0x00000008/* Delay I/O until buffer reused. */
82#defineB_LOCKED0x00000010/* Locked in core (not reusable). */
83#defineB_PHYS0x00000020/* I/O to user memory. */
84#define B_CLUSTER0x00000040/* UPL based I/O generated by cluster layer */
85#defineB_PAGEIO0x00000080/* Page in/out */
86#defineB_META0x00000100/* buffer contains meta-data. */
87#defineB_RAW0x00000200/* Set by physio for raw transfers. */
88#defineB_FUA0x00000400/* Write-through disk cache(if supported) */
89#define B_PASSIVE0x00000800/* PASSIVE I/Os are ignored by THROTTLE I/O */
90#defineB_IOSTREAMING0x00001000/* sequential access pattern detected */
91#define B_THROTTLED_IO0x00002000/* low priority I/O */
92/*
93 * make sure to check when adding flags that
94 * that the new flags don't overlap the definitions
95 * in buf_internal.h
96 */
97
98__BEGIN_DECLS
99
100/*!
101 @function buf_markaged
102 @abstract Mark a buffer as "aged," i.e. as a good candidate to be discarded and reused after buf_brelse().
103 @param bp Buffer to mark.
104 */
105voidbuf_markaged(buf_t);
106
107/*!
108 @function buf_markinvalid
109 @abstract Mark a buffer as not having valid data and being ready for immediate reuse after buf_brelse().
110 @param bp Buffer to mark.
111 */
112voidbuf_markinvalid(buf_t);
113
114/*!
115 @function buf_markdelayed
116 @abstract Mark a buffer as a delayed write: mark it dirty without actually scheduling I/O.
117 @discussion Data will be flushed to disk at some later time, not with brelse(). A sync()/fsync()
118 or pressure necessitating reuse of the buffer will cause it to be written back to disk.
119 @param bp Buffer to mark.
120 */
121voidbuf_markdelayed(buf_t);
122
123/*!
124 @function buf_markeintr
125 @abstract Mark a buffer as having been interrupted during I/O.
126 @discussion Waiters for I/O to complete (buf_biowait()) will return with EINTR when woken up.
127 buf_markeintr does not itself do a wakeup.
128 @param bp Buffer to mark.
129 */
130voidbuf_markeintr(buf_t);
131
132/*!
133 @function buf_markfua
134 @abstract Mark a buffer for write through disk cache, if disk supports it.
135 @param bp Buffer to mark.
136 */
137voidbuf_markfua(buf_t);
138
139/*!
140 @function buf_fua
141 @abstract Check if a buffer is marked for write through disk caches.
142 @param bp Buffer to test.
143 @return Nonzero if buffer is marked for write-through, 0 if not.
144 */
145intbuf_fua(buf_t);
146
147/*!
148 @function buf_valid
149 @abstract Check if a buffer contains valid data.
150 @param bp Buffer to test.
151 @return Nonzero if buffer has valid data, 0 if not.
152 */
153intbuf_valid(buf_t);
154
155/*!
156 @function buf_fromcache
157 @abstract Check if a buffer's data was found in core.
158 @discussion Will return truth after a buf_getblk that finds a valid buffer in the cache or the relevant
159 data in core (but not in a buffer).
160 @param bp Buffer to test.
161 @return Nonzero if we got this buffer's data without doing I/O, 0 if not.
162 */
163intbuf_fromcache(buf_t);
164
165/*!
166 @function buf_upl
167 @abstract Get the upl (Universal Page List) associated with a buffer.
168 @discussion Buffers allocated with buf_alloc() are not returned with a upl, and
169 traditional buffers only have a upl while an I/O is in progress.
170 @param bp Buffer whose upl to grab.
171 @return Buffer's upl if it has one, else NULL.
172 */
173void *buf_upl(buf_t);
174
175/*!
176 @function buf_uploffset
177 @abstract Get the offset into a UPL at which this buffer begins.
178 @discussion This function should only be called on iobufs, i.e. buffers allocated with buf_alloc().
179 @param bp Buffer whose uploffset to grab.
180 @return Buffer's uploffset--does not check whether that value makes sense for this buffer.
181 */
182uint32_t buf_uploffset(buf_t);
183
184
185
186/*!
187 @function buf_proc
188 @abstract Get the process associated with this buffer.
189 @discussion buf_proc() will generally return NULL; a process is currently only associated with
190 a buffer in the event of a physio() call.
191 @param bp Buffer whose associated process to find.
192 @return Associated process, possibly NULL.
193 */
194proc_tbuf_proc(buf_t);
195
196/*!
197 @function buf_dirtyoff
198 @abstract Get the starting offset of the dirty region associated with a buffer.
199 @discussion The dirty offset is zero unless someone explicitly calls buf_setdirtyoff() (which the kernel does not).
200 @param bp Buffer whose dirty offset to get.
201 @return Dirty offset (0 if not explicitly changed).
202 */
203uint32_t buf_dirtyoff(buf_t);
204
205/*!
206 @function buf_dirtyend
207 @abstract Get the ending offset of the dirty region associated with a buffer.
208 @discussion If the buffer's data was found incore and dirty, the dirty end is the size of the block; otherwise, unless
209 someone outside of xnu explicitly changes it by calling buf_setdirtyend(), it will be zero.
210 @param bp Buffer whose dirty end to get.
211 @return 0 if buffer is found clean; size of buffer if found dirty. Can be set to any value by callers of buf_setdirtyend().
212 */
213uint32_t buf_dirtyend(buf_t);
214
215/*!
216 @function buf_setdirtyoff
217 @abstract Set the starting offset of the dirty region associated with a buffer.
218 @discussion This value is zero unless someone set it explicitly.
219 @param bp Buffer whose dirty end to set.
220 @return void.
221 */
222voidbuf_setdirtyoff(buf_t, uint32_t);
223
224/*!
225 @function buf_setdirtyend
226 @abstract Set the ending offset of the dirty region associated with a buffer.
227 @discussion If the buffer's data was found incore and dirty, the dirty end is the size of the block; otherwise, unless
228 someone outside of xnu explicitly changes it by calling buf_setdirtyend(), it will be zero.
229 @param bp Buffer whose dirty end to set.
230 @return void.
231 */
232voidbuf_setdirtyend(buf_t, uint32_t);
233
234/*!
235 @function buf_error
236 @abstract Get the error value associated with a buffer.
237 @discussion Errors are set with buf_seterror().
238 @param bp Buffer whose error value to retrieve.
239 @return Error value, directly.
240 */
241errno_tbuf_error(buf_t);
242
243/*!
244 @function buf_seterror
245 @abstract Set an error value on a buffer.
246 @param bp Buffer whose error value to set.
247 @return void.
248 */
249voidbuf_seterror(buf_t, errno_t);
250
251/*!
252 @function buf_setflags
253 @abstract Set flags on a buffer.
254 @discussion: buffer_flags |= flags
255 @param bp Buffer whose flags to set.
256 @param flags Flags to add to buffer's mask. B_LOCKED/B_NOCACHE/B_ASYNC/B_READ/B_WRITE/B_PAGEIO/B_FUA
257 @return void.
258 */
259voidbuf_setflags(buf_t, int32_t);
260
261/*!
262 @function buf_clearflags
263 @abstract Clear flags on a buffer.
264 @discussion: buffer_flags &= ~flags
265 @param bp Buffer whose flags to clear.
266 @param flags Flags to remove from buffer's mask. B_LOCKED/B_NOCACHE/B_ASYNC/B_READ/B_WRITE/B_PAGEIO/B_FUA
267 @return void.
268 */
269voidbuf_clearflags(buf_t, int32_t);
270
271/*!
272 @function buf_flags
273 @abstract Get flags set on a buffer.
274 @discussion Valid flags are B_LOCKED/B_NOCACHE/B_ASYNC/B_READ/B_WRITE/B_PAGEIO/B_FUA.
275 @param bp Buffer whose flags to grab.
276 @return flags.
277 */
278int32_tbuf_flags(buf_t);
279
280/*!
281 @function buf_reset
282 @abstract Reset I/O flag state on a buffer.
283 @discussion Clears current flags on a buffer (internal and external) and allows some new flags to be set.
284 Used perhaps to prepare an iobuf for reuse.
285 @param bp Buffer whose flags to grab.
286 @param flags Flags to set on buffer: B_READ, B_WRITE, B_ASYNC, B_NOCACHE.
287 @return void.
288 */
289voidbuf_reset(buf_t, int32_t);
290
291/*!
292 @function buf_map
293 @abstract Get virtual mappings for buffer data.
294 @discussion For buffers created through buf_getblk() (i.e. traditional buffer cache usage),
295 buf_map() just returns the address at which data was mapped by but_getblk(). For a B_CLUSTER buffer, i.e. an iobuf
296 whose upl state is managed manually, there are two possibilities. If the buffer was created
297 with an underlying "real" buffer through cluster_bp(), the mapping of the "real" buffer is returned.
298 Otherwise, the buffer was created with buf_alloc() and buf_setupl() was subsequently called; buf_map()
299 will call ubc_upl_map() to get a mapping for the buffer's upl and return the start of that mapping
300 plus the buffer's upl offset (set in buf_setupl()). In the last case, buf_unmap() must later be called
301 to tear down the mapping. NOTE: buf_map() does not set the buffer data pointer; this must be done with buf_setdataptr().
302 @param bp Buffer whose mapping to find or create.
303 @param io_addr Destination for mapping address.
304 @return 0 for success, ENOMEM if unable to map the buffer.
305 */
306errno_tbuf_map(buf_t, caddr_t *);
307
308/*!
309 @function buf_unmap
310 @abstract Release mappings for buffer data.
311 @discussion For buffers created through buf_getblk() (i.e. traditional buffer cache usage),
312 buf_unmap() does nothing; buf_brelse() will take care of unmapping. For a B_CLUSTER buffer, i.e. an iobuf
313 whose upl state is managed manually, there are two possibilities. If the buffer was created
314 with an underlying "real" buffer through cluster_bp(), buf_unmap() does nothing; buf_brelse() on the
315 underlying buffer will tear down the mapping. Otherwise, the buffer was created with buf_alloc() and
316 buf_setupl() was subsequently called; buf_map() created the mapping. In this case, buf_unmap() will
317 unmap the buffer.
318 @param bp Buffer whose mapping to find or create.
319 @param io_addr Destination for mapping address.
320 @return 0 for success, EINVAL if unable to unmap buffer.
321 */
322errno_tbuf_unmap(buf_t);
323
324/*!
325 @function buf_setdrvdata
326 @abstract Set driver-specific data on a buffer.
327 @param bp Buffer whose driver-data to set.
328 @param drvdata Opaque driver data.
329 @return void.
330 */
331void buf_setdrvdata(buf_t, void *);
332
333/*!
334 @function buf_setdrvdata
335 @abstract Get driver-specific data from a buffer.
336 @param bp Buffer whose driver data to get.
337 @return Opaque driver data.
338 */
339void *buf_drvdata(buf_t);
340
341/*!
342 @function buf_setfsprivate
343 @abstract Set filesystem-specific data on a buffer.
344 @param bp Buffer whose filesystem data to set.
345 @param fsprivate Opaque filesystem data.
346 @return void.
347 */
348void buf_setfsprivate(buf_t, void *);
349
350/*!
351 @function buf_fsprivate
352 @abstract Get filesystem-specific data from a buffer.
353 @param bp Buffer whose filesystem data to get.
354 @return Opaque filesystem data.
355 */
356void *buf_fsprivate(buf_t);
357
358/*!
359 @function buf_blkno
360 @abstract Get physical block number associated with a buffer, in the sense of VNOP_BLOCKMAP.
361 @discussion When a buffer's physical block number is the same is its logical block number, then the physical
362 block number is considered uninitialized. A physical block number of -1 indicates that there is no valid
363 physical mapping (e.g. the logical block is invalid or corresponds to a sparse region in a file). Physical
364 block number is normally set by the cluster layer or by buf_getblk().
365 @param bp Buffer whose physical block number to get.
366 @return Block number.
367 */
368daddr64_t buf_blkno(buf_t);
369
370/*!
371 @function buf_lblkno
372 @abstract Get logical block number associated with a buffer.
373 @discussion Logical block number is set on traditionally-used buffers by an argument passed to buf_getblk(),
374 for example by buf_bread().
375 @param bp Buffer whose logical block number to get.
376 @return Block number.
377 */
378daddr64_t buf_lblkno(buf_t);
379
380/*!
381 @function buf_setblkno
382 @abstract Set physical block number associated with a buffer.
383 @discussion Physical block number is generally set by the cluster layer or by buf_getblk().
384 @param bp Buffer whose physical block number to set.
385 @param blkno Block number to set.
386 @return void.
387 */
388voidbuf_setblkno(buf_t, daddr64_t);
389
390/*!
391 @function buf_setlblkno
392 @abstract Set logical block number associated with a buffer.
393 @discussion Logical block number is set on traditionally-used buffers by an argument passed to buf_getblk(),
394 for example by buf_bread().
395 @param bp Buffer whose logical block number to set.
396 @param lblkno Block number to set.
397 @return void.
398 */
399voidbuf_setlblkno(buf_t, daddr64_t);
400
401/*!
402 @function buf_count
403 @abstract Get count of valid bytes in a buffer. This may be less than the space allocated to the buffer.
404 @param bp Buffer whose byte count to get.
405 @return Byte count.
406 */
407uint32_t buf_count(buf_t);
408
409/*!
410 @function buf_size
411 @abstract Get size of data region allocated to a buffer.
412 @discussion May be larger than amount of valid data in buffer.
413 @param bp Buffer whose size to get.
414 @return Size.
415 */
416uint32_t buf_size(buf_t);
417
418/*!
419 @function buf_resid
420 @abstract Get a count of bytes which were not consumed by an I/O on a buffer.
421 @discussion Set when an I/O operations completes.
422 @param bp Buffer whose outstanding count to get.
423 @return Count of unwritten/unread bytes.
424 */
425uint32_t buf_resid(buf_t);
426
427/*!
428 @function buf_setcount
429 @abstract Set count of valid bytes in a buffer. This may be less than the space allocated to the buffer.
430 @param bp Buffer whose byte count to set.
431 @param bcount Count to set.
432 @return void.
433 */
434voidbuf_setcount(buf_t, uint32_t);
435
436/*!
437 @function buf_setsize
438 @abstract Set size of data region allocated to a buffer.
439 @discussion May be larger than amount of valid data in buffer. Should be used by
440 code which is manually providing storage for an iobuf, one allocated with buf_alloc().
441 @param bp Buffer whose size to set.
442 @return void.
443 */
444voidbuf_setsize(buf_t, uint32_t);
445
446/*!
447 @function buf_setresid
448 @abstract Set a count of bytes outstanding for I/O in a buffer.
449 @discussion Set when an I/O operations completes. Examples: called by IOStorageFamily when I/O
450 completes, often called on an "original" buffer when using a manipulated buffer to perform I/O
451 on behalf of the first.
452 @param bp Buffer whose outstanding count to set.
453 @return Count of unwritten/unread bytes.
454 */
455voidbuf_setresid(buf_t, uint32_t);
456
457/*!
458 @function buf_setdataptr
459 @abstract Set the address at which a buffer's data will be stored.
460 @discussion In traditional buffer use, the data pointer will be set automatically. This routine is
461 useful with iobufs (allocated with buf_alloc()).
462 @param bp Buffer whose data pointer to set.
463 @param data Pointer to data region.
464 @return void.
465 */
466voidbuf_setdataptr(buf_t, uintptr_t);
467
468/*!
469 @function buf_dataptr
470 @abstract Get the address at which a buffer's data is stored; for iobufs, this must
471 be set with buf_setdataptr(). See buf_map().
472 @param bp Buffer whose data pointer to retrieve.
473 @return Data pointer; NULL if unset.
474 */
475uintptr_t buf_dataptr(buf_t);
476
477/*!
478 @function buf_vnode
479 @abstract Get the vnode associated with a buffer.
480 @discussion Every buffer is associated with a file. Because there is an I/O in flight,
481 there is an iocount on this vnode; it is returned WITHOUT an extra iocount, and vnode_put()
482 need NOT be called.
483 @param bp Buffer whose vnode to retrieve.
484 @return Buffer's vnode.
485 */
486vnode_tbuf_vnode(buf_t);
487
488/*!
489 @function buf_setvnode
490 @abstract Set the vnode associated with a buffer.
491 @discussion This call need not be used on traditional buffers; it is for use with iobufs.
492 @param bp Buffer whose vnode to set.
493 @param vp The vnode to attach to the buffer.
494 @return void.
495 */
496voidbuf_setvnode(buf_t, vnode_t);
497
498/*!
499 @function buf_setdevice
500 @abstract Set the device associated with a buffer.
501 @discussion A buffer's device is set in buf_strategy() (or in buf_getblk() if the file is a device).
502 It is also set on an iobuf if buf_alloc() is passed a device vnode.
503 @param bp Buffer whose device ID to set.
504 @param vp Device to set on the buffer.
505 @return 0 for success, EINVAL if vp is not a device file.
506 */
507errno_tbuf_setdevice(buf_t, vnode_t);
508
509/*!
510 @function buf_strategy
511 @abstract Pass an I/O request for a buffer down to the device layer.
512 @discussion This is one of the most important routines in the buffer cache layer. For buffers obtained
513 through buf_getblk, it handles finding physical block numbers for the I/O (with VNOP_BLKTOOFF and
514 VNOP_BLOCKMAP), packaging the I/O into page-sized chunks, and initiating I/O on the disk by calling
515 the device's strategy routine. If a buffer's UPL has been set manually with buf_setupl(), it assumes
516 that the request is already correctly configured with a block number and a size divisible by page size
517 and will just call directly to the device.
518 @param devvp Device on which to perform I/O
519 @param ap vnop_strategy_args structure (most importantly, a buffer).
520 @return 0 for success, or errors from filesystem or device layers.
521 */
522errno_tbuf_strategy(vnode_t, void *);
523
524/*
525 * Flags for buf_invalblkno()
526 */
527#defineBUF_WAIT0x01
528
529/*!
530 @function buf_invalblkno
531 @abstract Invalidate a filesystem logical block in a file.
532 @discussion buf_invalblkno() tries to make the data for a given block in a file
533 invalid; if the buffer for that block is found in core and is not busy, we mark it
534 invalid and call buf_brelse() (see "flags" param for what happens if the buffer is busy).
535 buf_brelse(), noticing that it is invalid, will
536 will return the buffer to the empty-buffer list and tell the VM subsystem to abandon
537 the relevant pages. Data will not be written to backing store--it will be cast aside.
538 Note that this function will only work if the block in question has been
539 obtained with a buf_getblk(). If data has been read into core without using
540 traditional buffer cache routines, buf_invalblkno() will not be able to invalidate it--this
541 includes the use of iobufs.
542 @param bp Buffer whose block to invalidate.
543 @param lblkno Logical block number.
544 @param flags BUF_WAIT: wait for busy buffers to become unbusy and invalidate them then. Otherwise,
545 just return EBUSY for busy blocks.
546 @return 0 for success, EINVAL if vp is not a device file.
547 */
548errno_tbuf_invalblkno(vnode_t, daddr64_t, int);
549
550/*!
551 @function buf_callback
552 @abstract Get the function set to be called when I/O on a buffer completes.
553 @discussion A function returned by buf_callback was originally set with buf_setcallback().
554 @param bp Buffer whose callback to get.
555 @return 0 for success, or errors from filesystem or device layers.
556 */
557void * buf_callback(buf_t);
558
559/*!
560 @function buf_setcallback
561 @abstract Set a function to be called once when I/O on a buffer completes.
562 @discussion A one-shot callout set with buf_setcallback() will be called from buf_biodone()
563 when I/O completes. It will be passed the "transaction" argument as well as the buffer.
564 buf_setcallback() also marks the buffer as B_ASYNC.
565 @param bp Buffer whose callback to set.
566 @param callback function to use as callback.
567 @param transaction Additional argument to callback function.
568 @return 0; always succeeds.
569 */
570errno_tbuf_setcallback(buf_t, void (*)(buf_t, void *), void *);
571
572/*!
573 @function buf_setupl
574 @abstract Set the UPL (Universal Page List), and offset therein, on a buffer.
575 @discussion buf_setupl() should only be called on buffers allocated with buf_alloc().
576 A subsequent call to buf_map() will map the UPL and give back the address at which data
577 begins. After buf_setupl() is called, a buffer is marked B_CLUSTER; when this is the case,
578 buf_strategy() assumes that a buffer is correctly configured to be passed to the device
579 layer without modification. Passing a NULL upl will clear the upl and the B_CLUSTER flag on the
580 buffer.
581 @param bp Buffer whose upl to set.
582 @param upl UPL to set in the buffer.
583 @parma offset Offset within upl at which relevant data begin.
584 @return 0 for success, EINVAL if the buffer was not allocated with buf_alloc().
585 */
586errno_tbuf_setupl(buf_t, upl_t, uint32_t);
587
588/*!
589 @function buf_clone
590 @abstract Clone a buffer with a restricted range and an optional callback.
591 @discussion Generates a buffer which is identical to its "bp" argument except that
592 it spans a subset of the data of the original. The buffer to be cloned should
593 have been allocated with buf_alloc(). Checks its arguments to make sure
594 that the data subset is coherent. Optionally, adds a callback function and argument to it
595 to be called when I/O completes (as with buf_setcallback(), but B_ASYNC is not set). If the original buffer had
596 a upl set through buf_setupl(), this upl is copied to the new buffer; otherwise, the original's
597 data pointer is used raw. The buffer must be released with buf_free().
598 @param bp Buffer to clone.
599 @param io_offset Offset, relative to start of data in original buffer, at which new buffer's data will begin.
600 @param io_size Size of buffer region in new buffer, in the sense of buf_count().
601 @param iodone Callback to be called from buf_biodone() when I/O completes, in the sense of buf_setcallback().
602 @param arg Argument to pass to iodone() callback.
603 @return NULL if io_offset/io_size combination is invalid for the buffer to be cloned; otherwise, the new buffer.
604 */
605buf_tbuf_clone(buf_t, int, int, void (*)(buf_t, void *), void *);
606
607/*!
608 @function buf_alloc
609 @abstract Allocate an uninitialized buffer.
610 @discussion A buffer returned by buf_alloc() is marked as busy and as an iobuf; it has no storage set up and must be
611 set up using buf_setdataptr() or buf_setupl()/buf_map().
612 @param vp vnode to associate with the buffer: optionally NULL. If vp is a device file, then
613 the buffer's associated device will be set. If vp is NULL, it can be set later with buf_setvnode().
614 @return New buffer.
615 */
616buf_t buf_alloc(vnode_t);
617
618/*!
619 @function buf_free
620 @abstract Free a buffer that was allocated with buf_alloc().
621 @discussion The storage (UPL, data pointer) associated with an iobuf must be freed manually.
622 @param bp The buffer to free.
623 @return void.
624 */
625voidbuf_free(buf_t);
626
627/*
628 * flags for buf_invalidateblks
629 */
630#defineBUF_WRITE_DATA0x0001/* write data blocks first */
631#defineBUF_SKIP_META0x0002/* skip over metadata blocks */
632
633/*!
634 @function buf_invalidateblks
635 @abstract Invalidate all the blocks associated with a vnode.
636 @discussion This function does for all blocks associated with a vnode what buf_invalblkno does for one block.
637 Again, it will only be able to invalidate data which were populated with traditional buffer cache routines,
638 i.e. by buf_getblk() and callers thereof. Unlike buf_invalblkno(), it can be made to write dirty data to disk
639 rather than casting it aside.
640 @param bp The buffer whose data to invalidate.
641 @param flags BUF_WRITE_DATA: write dirty data to disk with VNOP_BWRITE() before kicking buffer cache entries out.
642 BUF_SKIP_META: do not invalidate metadata blocks.
643 @param slpflag Flags to pass to "msleep" while waiting to acquire busy buffers.
644 @param slptimeo Timeout in "hz" (1/100 second) to wait for a buffer to become unbusy before waking from sleep
645 and re-starting the scan.
646 @return 0 for success, error values from msleep().
647 */
648intbuf_invalidateblks(vnode_t, int, int, int);
649
650/*
651 * flags for buf_flushdirtyblks and buf_iterate
652 */
653#define BUF_SKIP_NONLOCKED0x01
654#define BUF_SKIP_LOCKED0x02
655#define BUF_SCAN_CLEAN0x04/* scan the clean buffers */
656#define BUF_SCAN_DIRTY0x08/* scan the dirty buffers */
657#define BUF_NOTIFY_BUSY0x10/* notify the caller about the busy pages during the scan */
658
659
660#defineBUF_RETURNED0
661#define BUF_RETURNED_DONE1
662#define BUF_CLAIMED2
663#defineBUF_CLAIMED_DONE3
664/*!
665 @function buf_flushdirtyblks
666 @abstract Write dirty file blocks to disk.
667 @param vp The vnode whose blocks to flush.
668 @param wait Wait for writes to complete before returning.
669 @param flags Can pass zero, meaning "flush all dirty buffers."
670 BUF_SKIP_NONLOCKED: Skip buffers which are not busy when we encounter them.
671 BUF_SKIP_LOCKED: Skip buffers which are busy when we encounter them.
672 @param msg String to pass to msleep().
673 @return void.
674 */
675voidbuf_flushdirtyblks(vnode_t, int, int, const char *);
676
677/*!
678 @function buf_iterate
679 @abstract Perform some operation on all buffers associated with a vnode.
680 @param vp The vnode whose buffers to scan.
681 @param callout Function to call on each buffer. Should return one of:
682 BUF_RETURNED: buf_iterate() should call buf_brelse() on the buffer.
683 BUF_RETURNED_DONE: buf_iterate() should call buf_brelse() on the buffer and then stop iterating.
684 BUF_CLAIMED: buf_iterate() should continue iterating (and not call buf_brelse()).
685 BUF_CLAIMED_DONE: buf_iterate() should stop iterating (and not call buf_brelse()).
686 @param flag
687 BUF_SKIP_NONLOCKED: Skip buffers which are not busy when we encounter them. BUF_SKIP_LOCKED: Skip buffers which are busy when we encounter them.
688 BUF_SCAN_CLEAN: Call out on clean buffers.
689 BUF_SCAN_DIRTY: Call out on dirty buffers.
690 BUF_NOTIFY_BUSY: If a buffer cannot be acquired, pass a NULL buffer to callout; otherwise,
691 that buffer will be silently skipped.
692 @param arg Argument to pass to callout in addition to buffer.
693 @return void.
694 */
695voidbuf_iterate(vnode_t, int (*)(buf_t, void *), int, void *);
696
697/*!
698 @function buf_clear
699 @abstract Zero out the storage associated with a buffer.
700 @discussion Calls buf_map() to get the buffer's data address; for a B_CLUSTER
701 buffer (one which has had buf_setupl() called on it), it tries to map the buffer's
702 UPL into memory; should only be called once during the life cycle of an iobuf (one allocated
703 with buf_alloc()).
704 @param bp The buffer to zero out.
705 @return void.
706 */
707voidbuf_clear(buf_t);
708
709/*!
710 @function buf_bawrite
711 @abstract Start an asychronous write on a buffer.
712 @discussion Calls VNOP_BWRITE to start the process of propagating an asynchronous write down to the device layer.
713 Callers can wait for writes to complete at their discretion using buf_biowait(). When this function is called,
714 data should already have been written to the buffer's data region.
715 @param bp The buffer on which to initiate I/O.
716 @param throttle If "throttle" is nonzero and more than VNODE_ASYNC_THROTTLE writes are in progress on this file,
717 buf_bawrite() will block until the write count drops below VNODE_ASYNC_THROTTLE. If "throttle" is zero and the write
718 count is high, it will fail with EWOULDBLOCK; the caller can decide whether to make a blocking call or pursue
719 other opportunities.
720 @return EWOULDBLOCK if write count is high and "throttle" is zero; otherwise, errors from VNOP_BWRITE.
721 */
722errno_tbuf_bawrite(buf_t);
723
724/*!
725 @function buf_bdwrite
726 @abstract Mark a buffer for delayed write.
727 @discussion Marks a buffer as waiting for delayed write and the current I/O as complete; data will be written to backing store
728 before the buffer is reused, but it will not be queued for I/O immediately. Note that for buffers allocated
729 with buf_alloc(), there are no such guarantees; you must take care of your own flushing to disk. If
730 the number of delayed writes pending on the system is greater than an internal limit and the caller has not
731 requested otherwise [see return_error] , buf_bdwrite() will unilaterally launch an asynchronous I/O with buf_bawrite() to keep the pile of
732 delayed writes from getting too large.
733 @param bp The buffer to mark for delayed write.
734 @param return_error If the number of pending delayed writes systemwide is larger than an internal limit,
735 return EAGAIN rather than doing an asynchronous write.
736 @return EAGAIN for return_error != 0 case, 0 for succeess, errors from buf_bawrite.
737 */
738errno_tbuf_bdwrite(buf_t);
739
740/*!
741 @function buf_bwrite
742 @abstract Write a buffer's data to backing store.
743 @discussion Once the data in a buffer has been modified, buf_bwrite() starts sending it to disk by calling
744 VNOP_STRATEGY. Unless B_ASYNC has been set on the buffer (by buf_setflags() or otherwise), data will have
745 been written to disk when buf_bwrite() returns. See Bach (p 56).
746 @param bp The buffer to write to disk.
747 @return 0 for success; errors from buf_biowait().
748 */
749errno_tbuf_bwrite(buf_t);
750
751/*!
752 @function buf_biodone
753 @abstract Mark an I/O as completed.
754 @discussion buf_biodone() should be called by whosoever decides that an I/O on a buffer is complete; for example,
755 IOStorageFamily. It clears the dirty flag on a buffer and signals on the vnode that a write has completed
756 with vnode_writedone(). If a callout or filter has been set on the buffer, that function is called. In the case
757 of a callout, that function is expected to take care of cleaning up and freeing the buffer.
758 Otherwise, if the buffer is marked B_ASYNC (e.g. it was passed to buf_bawrite()), then buf_biodone()
759 considers itself justified in calling buf_brelse() to return it to free lists--no one is waiting for it. Finally,
760 waiters on the bp (e.g. in buf_biowait()) are woken up.
761 @param bp The buffer to mark as done with I/O.
762 @return void.
763 */
764voidbuf_biodone(buf_t);
765
766/*!
767 @function buf_biowait
768 @abstract Wait for I/O on a buffer to complete.
769 @discussion Waits for I/O on a buffer to finish, as marked by a buf_biodone() call.
770 @param bp The buffer to wait on.
771 @return 0 for a successful wait; nonzero the buffer has been marked as EINTR or had an error set on it.
772 */
773errno_tbuf_biowait(buf_t);
774
775/*!
776 @function buf_brelse
777 @abstract Release any claim to a buffer, sending it back to free lists.
778 @discussion buf_brelse() cleans up buffer state and releases a buffer to the free lists. If the buffer
779 is not marked invalid and its pages are dirty (e.g. a delayed write was made), its data will be commited
780 to backing store. If it is marked invalid, its data will be discarded completely.
781 A valid, cacheable buffer will be put on a list and kept in the buffer hash so it
782 can be found again; otherwise, it will be dissociated from its vnode and treated as empty. Which list a valid
783 buffer is placed on depends on the use of buf_markaged(), whether it is metadata, and the B_LOCKED flag. A
784 B_LOCKED buffer will not be available for reuse by other files, though its data may be paged out.
785 Note that buf_brelse() is intended for use with traditionally allocated buffers.
786 @param bp The buffer to release.
787 @retrn void.
788 */
789voidbuf_brelse(buf_t);
790
791/*!
792 @function minphys
793 @abstract Adjust a buffer's count to be no more than maximum physical I/O transfer size for the host architecture.
794 @discussion physio() takes as a parameter a function to bound transfer sizes for each VNOP_STRATEGY() call. minphys()
795 is a default implementation. It calls buf_setcount() to make the buffer's count the min() of its current count
796 and the max I/O size for the host architecture.
797 @param bp The buffer whose byte count to modify.
798 @return New byte count.
799 */
800u_intminphys(buf_t bp);
801
802/*
803 * Flags for operation type in getblk()
804 */
805#defineBLK_READ0x01/* buffer for read */
806#defineBLK_WRITE0x02/* buffer for write */
807#defineBLK_META0x10/* buffer for metadata */
808/*
809 * modifier for above flags... if set, getblk will only return
810 * a bp that is already valid... i.e. found in the cache
811 */
812#define BLK_ONLYVALID0x80000000
813
814/*!
815 @function buf_getblk
816 @abstract Traditional buffer cache routine to get a buffer corresponding to a logical block in a file.
817 @discussion buf_getblk() gets a buffer, not necessarily containing valid data, representing a block in a file.
818 A metadata buffer will be returned with its own zone-allocated storage, managed by the traditional buffer-cache
819 layer, whereas data buffers will be returned hooked into backing by the UBC (which in fact controls the caching of data).
820 buf_getblk() first looks for the buffer header in cache; if the buffer is in-core but busy, buf_getblk() will wait for it to become
821 unbusy, depending on the slpflag and slptimeo parameters. If the buffer is found unbusy and is a metadata buffer,
822 it must already contain valid data and will be returned directly; data buffers will have a UPL configured to
823 prepare for interaction with the underlying UBC. If the buffer is found in core, it will be marked as such
824 and buf_fromcache() will return truth. A buffer is allocated and initialized (but not filled with data)
825 if none is found in core. buf_bread(), buf_breadn(), buf_meta_bread(), and buf_meta_breadn() all
826 return buffers obtained with buf_getblk().
827 @param vp File for which to get block.
828 @param blkno Logical block number.
829 @param size Size of block.
830 @param slpflag Flag to pass to msleep() while waiting for buffer to become unbusy.
831 @param slptimeo Time, in milliseconds, to wait for buffer to become unbusy. 0 means to wait indefinitely.
832 @param operation BLK_READ: want a read buffer. BLK_WRITE: want a write buffer. BLK_META: want a metadata buffer. BLK_ONLYVALID:
833 only return buffers which are found in core (do not allocate anew), and do not change buffer size. The last remark means
834 that if a given logical block is found in core with a different size than what is requested, the buffer size will not be modified.
835 @return Buffer found in core or newly allocated, either containing valid data or ready for I/O.
836 */
837buf_tbuf_getblk(vnode_t, daddr64_t, int, int, int, int);
838
839/*!
840 @function buf_geteblk
841 @abstract Get a metadata buffer which is marked invalid and not associated with any vnode.
842 @discussion A buffer is returned with zone-allocated storage of the specified size, marked B_META and invalid.
843 It has no vnode and is not visible in the buffer hash.
844 @param size Size of buffer.
845 @return Always returns a new buffer.
846 */
847buf_tbuf_geteblk(int);
848
849
850__END_DECLS
851
852
853/* Macros to clear/set/test flags. */
854#defineSET(t, f)(t) |= (f)
855#defineCLR(t, f)(t) &= ~(f)
856#defineISSET(t, f)((t) & (f))
857
858
859#endif /* !_SYS_BUF_H_ */
860

Archive Download this file

Revision: 2225