]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /*- | |
30 | * Copyright (c) 1994 Christopher G. Demetriou | |
31 | * Copyright (c) 1982, 1986, 1989, 1993 | |
32 | * The Regents of the University of California. All rights reserved. | |
33 | * (c) UNIX System Laboratories, Inc. | |
34 | * All or some portions of this file are derived from material licensed | |
35 | * to the University of California by American Telephone and Telegraph | |
36 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
37 | * the permission of UNIX System Laboratories, Inc. | |
38 | * | |
39 | * Redistribution and use in source and binary forms, with or without | |
40 | * modification, are permitted provided that the following conditions | |
41 | * are met: | |
42 | * 1. Redistributions of source code must retain the above copyright | |
43 | * notice, this list of conditions and the following disclaimer. | |
44 | * 2. Redistributions in binary form must reproduce the above copyright | |
45 | * notice, this list of conditions and the following disclaimer in the | |
46 | * documentation and/or other materials provided with the distribution. | |
47 | * 3. All advertising materials mentioning features or use of this software | |
48 | * must display the following acknowledgement: | |
49 | * This product includes software developed by the University of | |
50 | * California, Berkeley and its contributors. | |
51 | * 4. Neither the name of the University nor the names of its contributors | |
52 | * may be used to endorse or promote products derived from this software | |
53 | * without specific prior written permission. | |
54 | * | |
55 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
56 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
57 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
58 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
59 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
60 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
61 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
62 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
63 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
64 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
65 | * SUCH DAMAGE. | |
66 | * | |
1c79356b A |
67 | * @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94 |
68 | */ | |
69 | ||
70 | /* | |
71 | * Some references: | |
72 | * Bach: The Design of the UNIX Operating System (Prentice Hall, 1986) | |
73 | * Leffler, et al.: The Design and Implementation of the 4.3BSD | |
74 | * UNIX Operating System (Addison Welley, 1989) | |
75 | */ | |
1c79356b A |
76 | |
77 | #include <sys/param.h> | |
78 | #include <sys/systm.h> | |
91447636 A |
79 | #include <sys/proc_internal.h> |
80 | #include <sys/buf_internal.h> | |
81 | #include <sys/vnode_internal.h> | |
82 | #include <sys/mount_internal.h> | |
1c79356b A |
83 | #include <sys/trace.h> |
84 | #include <sys/malloc.h> | |
85 | #include <sys/resourcevar.h> | |
86 | #include <miscfs/specfs/specdev.h> | |
87 | #include <sys/ubc.h> | |
91447636 | 88 | #include <sys/kauth.h> |
1c79356b A |
89 | #if DIAGNOSTIC |
90 | #include <kern/assert.h> | |
91 | #endif /* DIAGNOSTIC */ | |
92 | #include <kern/task.h> | |
93 | #include <kern/zalloc.h> | |
fe8ab488 A |
94 | #include <kern/locks.h> |
95 | #include <kern/thread.h> | |
91447636 | 96 | |
2d21ac55 | 97 | #include <sys/fslog.h> /* fslog_io_error() */ |
d190cdc3 | 98 | #include <sys/disk.h> /* dk_error_description_t */ |
2d21ac55 A |
99 | |
100 | #include <mach/mach_types.h> | |
101 | #include <mach/memory_object_types.h> | |
102 | #include <kern/sched_prim.h> /* thread_block() */ | |
103 | ||
91447636 | 104 | #include <vm/vm_kern.h> |
b0d623f7 | 105 | #include <vm/vm_pageout.h> |
1c79356b A |
106 | |
107 | #include <sys/kdebug.h> | |
2d21ac55 A |
108 | |
109 | #include <libkern/OSAtomic.h> | |
b0d623f7 | 110 | #include <libkern/OSDebug.h> |
2d21ac55 A |
111 | #include <sys/ubc_internal.h> |
112 | ||
113 | #include <sys/sdt.h> | |
1c79356b | 114 | |
6d2010ae | 115 | int bcleanbuf(buf_t bp, boolean_t discard); |
91447636 A |
116 | static int brecover_data(buf_t bp); |
117 | static boolean_t incore(vnode_t vp, daddr64_t blkno); | |
91447636 A |
118 | /* timeout is in msecs */ |
119 | static buf_t getnewbuf(int slpflag, int slptimeo, int *queue); | |
120 | static void bremfree_locked(buf_t bp); | |
121 | static void buf_reassign(buf_t bp, vnode_t newvp); | |
122 | static errno_t buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo); | |
123 | static int buf_iterprepare(vnode_t vp, struct buflists *, int flags); | |
124 | static void buf_itercomplete(vnode_t vp, struct buflists *, int flags); | |
6d2010ae A |
125 | static boolean_t buffer_cache_gc(int); |
126 | static buf_t buf_brelse_shadow(buf_t bp); | |
127 | static void buf_free_meta_store(buf_t bp); | |
128 | ||
129 | static buf_t buf_create_shadow_internal(buf_t bp, boolean_t force_copy, | |
130 | uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg, int priv); | |
131 | ||
1c79356b | 132 | |
39037602 | 133 | int bdwrite_internal(buf_t, int); |
1c79356b | 134 | |
5ba3f43e A |
135 | extern void disk_conditioner_delay(buf_t, int, int, uint64_t); |
136 | ||
d52fe63f | 137 | /* zone allocated buffer headers */ |
39236c6e A |
138 | static void bufzoneinit(void); |
139 | static void bcleanbuf_thread_init(void); | |
91447636 A |
140 | static void bcleanbuf_thread(void); |
141 | ||
142 | static zone_t buf_hdr_zone; | |
143 | static int buf_hdr_count; | |
d52fe63f | 144 | |
1c79356b A |
145 | |
146 | /* | |
147 | * Definitions for the buffer hash lists. | |
148 | */ | |
149 | #define BUFHASH(dvp, lbn) \ | |
150 | (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash]) | |
151 | LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash; | |
152 | u_long bufhash; | |
153 | ||
2d21ac55 A |
154 | static buf_t incore_locked(vnode_t vp, daddr64_t blkno, struct bufhashhdr *dp); |
155 | ||
1c79356b A |
156 | /* Definitions for the buffer stats. */ |
157 | struct bufstats bufstats; | |
158 | ||
d52fe63f | 159 | /* Number of delayed write buffers */ |
2d21ac55 | 160 | long nbdwrite = 0; |
91447636 | 161 | int blaundrycnt = 0; |
2d21ac55 | 162 | static int boot_nbuf_headers = 0; |
d52fe63f | 163 | |
6d2010ae | 164 | static TAILQ_HEAD(delayqueue, buf) delaybufqueue; |
1c79356b | 165 | |
91447636 A |
166 | static TAILQ_HEAD(ioqueue, buf) iobufqueue; |
167 | static TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES]; | |
d52fe63f A |
168 | static int needbuffer; |
169 | static int need_iobuffer; | |
1c79356b | 170 | |
91447636 A |
171 | static lck_grp_t *buf_mtx_grp; |
172 | static lck_attr_t *buf_mtx_attr; | |
173 | static lck_grp_attr_t *buf_mtx_grp_attr; | |
174 | static lck_mtx_t *iobuffer_mtxp; | |
175 | static lck_mtx_t *buf_mtxp; | |
813fb2f6 | 176 | static lck_mtx_t *buf_gc_callout; |
91447636 | 177 | |
b0d623f7 A |
178 | static int buf_busycount; |
179 | ||
813fb2f6 A |
180 | #define FS_BUFFER_CACHE_GC_CALLOUTS_MAX_SIZE 16 |
181 | typedef struct { | |
182 | void (* callout)(int, void *); | |
183 | void *context; | |
184 | } fs_buffer_cache_gc_callout_t; | |
185 | ||
186 | fs_buffer_cache_gc_callout_t fs_callouts[FS_BUFFER_CACHE_GC_CALLOUTS_MAX_SIZE] = { {NULL, NULL} }; | |
187 | ||
91447636 A |
188 | static __inline__ int |
189 | buf_timestamp(void) | |
190 | { | |
191 | struct timeval t; | |
192 | microuptime(&t); | |
193 | return (t.tv_sec); | |
194 | } | |
195 | ||
1c79356b A |
196 | /* |
197 | * Insq/Remq for the buffer free lists. | |
198 | */ | |
91447636 A |
199 | #define binsheadfree(bp, dp, whichq) do { \ |
200 | TAILQ_INSERT_HEAD(dp, bp, b_freelist); \ | |
1c79356b A |
201 | } while (0) |
202 | ||
91447636 A |
203 | #define binstailfree(bp, dp, whichq) do { \ |
204 | TAILQ_INSERT_TAIL(dp, bp, b_freelist); \ | |
91447636 | 205 | } while (0) |
91447636 | 206 | |
1c79356b A |
207 | #define BHASHENTCHECK(bp) \ |
208 | if ((bp)->b_hash.le_prev != (struct buf **)0xdeadbeef) \ | |
2d21ac55 | 209 | panic("%p: b_hash.le_prev is not deadbeef", (bp)); |
1c79356b A |
210 | |
211 | #define BLISTNONE(bp) \ | |
212 | (bp)->b_hash.le_next = (struct buf *)0; \ | |
213 | (bp)->b_hash.le_prev = (struct buf **)0xdeadbeef; | |
214 | ||
9bccf70c A |
215 | /* |
216 | * Insq/Remq for the vnode usage lists. | |
217 | */ | |
218 | #define bufinsvn(bp, dp) LIST_INSERT_HEAD(dp, bp, b_vnbufs) | |
219 | #define bufremvn(bp) { \ | |
220 | LIST_REMOVE(bp, b_vnbufs); \ | |
221 | (bp)->b_vnbufs.le_next = NOLIST; \ | |
222 | } | |
223 | ||
1c79356b A |
224 | /* |
225 | * Time in seconds before a buffer on a list is | |
226 | * considered as a stale buffer | |
227 | */ | |
228 | #define LRU_IS_STALE 120 /* default value for the LRU */ | |
229 | #define AGE_IS_STALE 60 /* default value for the AGE */ | |
230 | #define META_IS_STALE 180 /* default value for the BQ_META */ | |
231 | ||
232 | int lru_is_stale = LRU_IS_STALE; | |
233 | int age_is_stale = AGE_IS_STALE; | |
234 | int meta_is_stale = META_IS_STALE; | |
2d21ac55 | 235 | |
6d2010ae | 236 | #define MAXLAUNDRY 10 |
91447636 | 237 | |
9bccf70c A |
238 | /* LIST_INSERT_HEAD() with assertions */ |
239 | static __inline__ void | |
91447636 | 240 | blistenterhead(struct bufhashhdr * head, buf_t bp) |
1c79356b A |
241 | { |
242 | if ((bp->b_hash.le_next = (head)->lh_first) != NULL) | |
243 | (head)->lh_first->b_hash.le_prev = &(bp)->b_hash.le_next; | |
244 | (head)->lh_first = bp; | |
245 | bp->b_hash.le_prev = &(head)->lh_first; | |
246 | if (bp->b_hash.le_prev == (struct buf **)0xdeadbeef) | |
247 | panic("blistenterhead: le_prev is deadbeef"); | |
1c79356b | 248 | } |
1c79356b | 249 | |
9bccf70c | 250 | static __inline__ void |
91447636 | 251 | binshash(buf_t bp, struct bufhashhdr *dp) |
1c79356b | 252 | { |
0c530ab8 | 253 | #if DIAGNOSTIC |
91447636 | 254 | buf_t nbp; |
0c530ab8 | 255 | #endif /* DIAGNOSTIC */ |
9bccf70c | 256 | |
1c79356b | 257 | BHASHENTCHECK(bp); |
9bccf70c | 258 | |
0c530ab8 | 259 | #if DIAGNOSTIC |
1c79356b A |
260 | nbp = dp->lh_first; |
261 | for(; nbp != NULL; nbp = nbp->b_hash.le_next) { | |
262 | if(nbp == bp) | |
263 | panic("buf already in hashlist"); | |
264 | } | |
0c530ab8 | 265 | #endif /* DIAGNOSTIC */ |
1c79356b | 266 | |
1c79356b | 267 | blistenterhead(dp, bp); |
1c79356b A |
268 | } |
269 | ||
9bccf70c | 270 | static __inline__ void |
91447636 | 271 | bremhash(buf_t bp) |
1c79356b | 272 | { |
1c79356b A |
273 | if (bp->b_hash.le_prev == (struct buf **)0xdeadbeef) |
274 | panic("bremhash le_prev is deadbeef"); | |
275 | if (bp->b_hash.le_next == bp) | |
276 | panic("bremhash: next points to self"); | |
277 | ||
278 | if (bp->b_hash.le_next != NULL) | |
279 | bp->b_hash.le_next->b_hash.le_prev = bp->b_hash.le_prev; | |
280 | *bp->b_hash.le_prev = (bp)->b_hash.le_next; | |
1c79356b A |
281 | } |
282 | ||
6d2010ae A |
283 | /* |
284 | * buf_mtxp held. | |
285 | */ | |
286 | static __inline__ void | |
287 | bmovelaundry(buf_t bp) | |
288 | { | |
289 | bp->b_whichq = BQ_LAUNDRY; | |
290 | bp->b_timestamp = buf_timestamp(); | |
291 | binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY); | |
292 | blaundrycnt++; | |
293 | } | |
1c79356b | 294 | |
6d2010ae A |
295 | static __inline__ void |
296 | buf_release_credentials(buf_t bp) | |
297 | { | |
298 | if (IS_VALID_CRED(bp->b_rcred)) { | |
299 | kauth_cred_unref(&bp->b_rcred); | |
300 | } | |
301 | if (IS_VALID_CRED(bp->b_wcred)) { | |
302 | kauth_cred_unref(&bp->b_wcred); | |
303 | } | |
304 | } | |
1c79356b | 305 | |
9bccf70c | 306 | |
91447636 A |
307 | int |
308 | buf_valid(buf_t bp) { | |
309 | ||
310 | if ( (bp->b_flags & (B_DONE | B_DELWRI)) ) | |
311 | return 1; | |
312 | return 0; | |
9bccf70c A |
313 | } |
314 | ||
91447636 A |
315 | int |
316 | buf_fromcache(buf_t bp) { | |
9bccf70c | 317 | |
91447636 A |
318 | if ( (bp->b_flags & B_CACHE) ) |
319 | return 1; | |
320 | return 0; | |
9bccf70c A |
321 | } |
322 | ||
9bccf70c | 323 | void |
91447636 A |
324 | buf_markinvalid(buf_t bp) { |
325 | ||
326 | SET(bp->b_flags, B_INVAL); | |
327 | } | |
9bccf70c | 328 | |
91447636 A |
329 | void |
330 | buf_markdelayed(buf_t bp) { | |
331 | ||
2d21ac55 A |
332 | if (!ISSET(bp->b_flags, B_DELWRI)) { |
333 | SET(bp->b_flags, B_DELWRI); | |
334 | ||
b0d623f7 | 335 | OSAddAtomicLong(1, &nbdwrite); |
2d21ac55 A |
336 | buf_reassign(bp, bp->b_vp); |
337 | } | |
338 | SET(bp->b_flags, B_DONE); | |
9bccf70c A |
339 | } |
340 | ||
6d2010ae A |
341 | void |
342 | buf_markclean(buf_t bp) { | |
343 | ||
344 | if (ISSET(bp->b_flags, B_DELWRI)) { | |
345 | CLR(bp->b_flags, B_DELWRI); | |
346 | ||
347 | OSAddAtomicLong(-1, &nbdwrite); | |
348 | buf_reassign(bp, bp->b_vp); | |
349 | } | |
350 | } | |
351 | ||
91447636 A |
352 | void |
353 | buf_markeintr(buf_t bp) { | |
354 | ||
355 | SET(bp->b_flags, B_EINTR); | |
356 | } | |
765c9de3 | 357 | |
2d21ac55 | 358 | |
91447636 A |
359 | void |
360 | buf_markaged(buf_t bp) { | |
361 | ||
362 | SET(bp->b_flags, B_AGE); | |
765c9de3 A |
363 | } |
364 | ||
2d21ac55 A |
365 | int |
366 | buf_fua(buf_t bp) { | |
367 | ||
368 | if ((bp->b_flags & B_FUA) == B_FUA) | |
369 | return 1; | |
370 | return 0; | |
371 | } | |
372 | ||
373 | void | |
374 | buf_markfua(buf_t bp) { | |
375 | ||
376 | SET(bp->b_flags, B_FUA); | |
377 | } | |
378 | ||
316670eb | 379 | #if CONFIG_PROTECT |
3e170ce0 A |
380 | cpx_t bufattr_cpx(bufattr_t bap) |
381 | { | |
382 | return bap->ba_cpx; | |
383 | } | |
384 | ||
385 | void bufattr_setcpx(bufattr_t bap, cpx_t cpx) | |
386 | { | |
387 | bap->ba_cpx = cpx; | |
316670eb A |
388 | } |
389 | ||
390 | void | |
391 | buf_setcpoff (buf_t bp, uint64_t foffset) { | |
392 | bp->b_attr.ba_cp_file_off = foffset; | |
393 | } | |
394 | ||
316670eb A |
395 | uint64_t |
396 | bufattr_cpoff(bufattr_t bap) { | |
3e170ce0 | 397 | return bap->ba_cp_file_off; |
316670eb A |
398 | } |
399 | ||
400 | void | |
401 | bufattr_setcpoff(bufattr_t bap, uint64_t foffset) { | |
3e170ce0 | 402 | bap->ba_cp_file_off = foffset; |
d1ecb069 A |
403 | } |
404 | ||
3e170ce0 | 405 | #else // !CONTECT_PROTECT |
d1ecb069 | 406 | |
316670eb A |
407 | uint64_t |
408 | bufattr_cpoff(bufattr_t bap __unused) { | |
409 | return 0; | |
410 | } | |
411 | ||
316670eb A |
412 | void |
413 | bufattr_setcpoff(__unused bufattr_t bap, __unused uint64_t foffset) { | |
d1ecb069 A |
414 | return; |
415 | } | |
3e170ce0 A |
416 | |
417 | struct cpx *bufattr_cpx(__unused bufattr_t bap) | |
418 | { | |
419 | return NULL; | |
420 | } | |
421 | ||
422 | void bufattr_setcpx(__unused bufattr_t bap, __unused struct cpx *cpx) | |
423 | { | |
424 | } | |
425 | ||
426 | #endif /* !CONFIG_PROTECT */ | |
d1ecb069 | 427 | |
316670eb A |
428 | bufattr_t |
429 | bufattr_alloc() { | |
430 | bufattr_t bap; | |
431 | MALLOC(bap, bufattr_t, sizeof(struct bufattr), M_TEMP, M_WAITOK); | |
432 | if (bap == NULL) | |
433 | return NULL; | |
434 | ||
435 | bzero(bap, sizeof(struct bufattr)); | |
436 | return bap; | |
437 | } | |
438 | ||
439 | void | |
440 | bufattr_free(bufattr_t bap) { | |
441 | if (bap) | |
442 | FREE(bap, M_TEMP); | |
443 | } | |
444 | ||
fe8ab488 A |
445 | bufattr_t |
446 | bufattr_dup(bufattr_t bap) { | |
447 | bufattr_t new_bufattr; | |
448 | MALLOC(new_bufattr, bufattr_t, sizeof(struct bufattr), M_TEMP, M_WAITOK); | |
449 | if (new_bufattr == NULL) | |
450 | return NULL; | |
451 | ||
452 | /* Copy the provided one into the new copy */ | |
453 | memcpy (new_bufattr, bap, sizeof(struct bufattr)); | |
454 | return new_bufattr; | |
455 | } | |
456 | ||
316670eb A |
457 | int |
458 | bufattr_rawencrypted(bufattr_t bap) { | |
459 | if ( (bap->ba_flags & BA_RAW_ENCRYPTED_IO) ) | |
460 | return 1; | |
461 | return 0; | |
462 | } | |
463 | ||
7ddcb079 A |
464 | int |
465 | bufattr_throttled(bufattr_t bap) { | |
39236c6e | 466 | return (GET_BUFATTR_IO_TIER(bap)); |
7ddcb079 A |
467 | } |
468 | ||
fe8ab488 A |
469 | int |
470 | bufattr_passive(bufattr_t bap) { | |
471 | if ( (bap->ba_flags & BA_PASSIVE) ) | |
472 | return 1; | |
473 | return 0; | |
474 | } | |
475 | ||
316670eb A |
476 | int |
477 | bufattr_nocache(bufattr_t bap) { | |
478 | if ( (bap->ba_flags & BA_NOCACHE) ) | |
479 | return 1; | |
480 | return 0; | |
481 | } | |
482 | ||
483 | int | |
484 | bufattr_meta(bufattr_t bap) { | |
485 | if ( (bap->ba_flags & BA_META) ) | |
486 | return 1; | |
487 | return 0; | |
488 | } | |
489 | ||
fe8ab488 A |
490 | void |
491 | bufattr_markmeta(bufattr_t bap) { | |
492 | SET(bap->ba_flags, BA_META); | |
493 | } | |
494 | ||
316670eb | 495 | int |
5ba3f43e | 496 | #if !CONFIG_EMBEDDED |
316670eb | 497 | bufattr_delayidlesleep(bufattr_t bap) |
5ba3f43e A |
498 | #else /* !CONFIG_EMBEDDED */ |
499 | bufattr_delayidlesleep(__unused bufattr_t bap) | |
500 | #endif /* !CONFIG_EMBEDDED */ | |
316670eb | 501 | { |
5ba3f43e | 502 | #if !CONFIG_EMBEDDED |
316670eb A |
503 | if ( (bap->ba_flags & BA_DELAYIDLESLEEP) ) |
504 | return 1; | |
5ba3f43e | 505 | #endif /* !CONFIG_EMBEDDED */ |
316670eb A |
506 | return 0; |
507 | } | |
508 | ||
7ddcb079 A |
509 | bufattr_t |
510 | buf_attr(buf_t bp) { | |
511 | return &bp->b_attr; | |
512 | } | |
513 | ||
316670eb A |
514 | void |
515 | buf_markstatic(buf_t bp __unused) { | |
516 | SET(bp->b_flags, B_STATICCONTENT); | |
517 | } | |
518 | ||
519 | int | |
520 | buf_static(buf_t bp) { | |
521 | if ( (bp->b_flags & B_STATICCONTENT) ) | |
522 | return 1; | |
523 | return 0; | |
524 | } | |
525 | ||
39236c6e A |
526 | void |
527 | bufattr_markgreedymode(bufattr_t bap) { | |
528 | SET(bap->ba_flags, BA_GREEDY_MODE); | |
529 | } | |
530 | ||
531 | int | |
532 | bufattr_greedymode(bufattr_t bap) { | |
533 | if ( (bap->ba_flags & BA_GREEDY_MODE) ) | |
534 | return 1; | |
535 | return 0; | |
536 | } | |
537 | ||
fe8ab488 A |
538 | void |
539 | bufattr_markisochronous(bufattr_t bap) { | |
540 | SET(bap->ba_flags, BA_ISOCHRONOUS); | |
541 | } | |
542 | ||
543 | int | |
544 | bufattr_isochronous(bufattr_t bap) { | |
545 | if ( (bap->ba_flags & BA_ISOCHRONOUS) ) | |
546 | return 1; | |
547 | return 0; | |
548 | } | |
549 | ||
39236c6e A |
550 | void |
551 | bufattr_markquickcomplete(bufattr_t bap) { | |
552 | SET(bap->ba_flags, BA_QUICK_COMPLETE); | |
553 | } | |
554 | ||
555 | int | |
556 | bufattr_quickcomplete(bufattr_t bap) { | |
557 | if ( (bap->ba_flags & BA_QUICK_COMPLETE) ) | |
558 | return 1; | |
559 | return 0; | |
560 | } | |
561 | ||
91447636 A |
562 | errno_t |
563 | buf_error(buf_t bp) { | |
564 | ||
565 | return (bp->b_error); | |
566 | } | |
1c79356b | 567 | |
91447636 A |
568 | void |
569 | buf_seterror(buf_t bp, errno_t error) { | |
1c79356b | 570 | |
91447636 A |
571 | if ((bp->b_error = error)) |
572 | SET(bp->b_flags, B_ERROR); | |
573 | else | |
574 | CLR(bp->b_flags, B_ERROR); | |
575 | } | |
1c79356b | 576 | |
91447636 A |
577 | void |
578 | buf_setflags(buf_t bp, int32_t flags) { | |
1c79356b | 579 | |
91447636 A |
580 | SET(bp->b_flags, (flags & BUF_X_WRFLAGS)); |
581 | } | |
765c9de3 | 582 | |
91447636 A |
583 | void |
584 | buf_clearflags(buf_t bp, int32_t flags) { | |
1c79356b | 585 | |
91447636 A |
586 | CLR(bp->b_flags, (flags & BUF_X_WRFLAGS)); |
587 | } | |
1c79356b | 588 | |
91447636 A |
589 | int32_t |
590 | buf_flags(buf_t bp) { | |
591 | ||
592 | return ((bp->b_flags & BUF_X_RDFLAGS)); | |
593 | } | |
1c79356b | 594 | |
91447636 A |
595 | void |
596 | buf_reset(buf_t bp, int32_t io_flags) { | |
597 | ||
2d21ac55 | 598 | CLR(bp->b_flags, (B_READ | B_WRITE | B_ERROR | B_DONE | B_INVAL | B_ASYNC | B_NOCACHE | B_FUA)); |
91447636 | 599 | SET(bp->b_flags, (io_flags & (B_ASYNC | B_READ | B_WRITE | B_NOCACHE))); |
1c79356b | 600 | |
91447636 A |
601 | bp->b_error = 0; |
602 | } | |
1c79356b | 603 | |
91447636 A |
604 | uint32_t |
605 | buf_count(buf_t bp) { | |
606 | ||
607 | return (bp->b_bcount); | |
608 | } | |
765c9de3 | 609 | |
91447636 A |
610 | void |
611 | buf_setcount(buf_t bp, uint32_t bcount) { | |
612 | ||
613 | bp->b_bcount = bcount; | |
1c79356b A |
614 | } |
615 | ||
91447636 A |
616 | uint32_t |
617 | buf_size(buf_t bp) { | |
618 | ||
619 | return (bp->b_bufsize); | |
620 | } | |
1c79356b | 621 | |
91447636 A |
622 | void |
623 | buf_setsize(buf_t bp, uint32_t bufsize) { | |
624 | ||
625 | bp->b_bufsize = bufsize; | |
626 | } | |
1c79356b | 627 | |
91447636 A |
628 | uint32_t |
629 | buf_resid(buf_t bp) { | |
630 | ||
631 | return (bp->b_resid); | |
632 | } | |
b4c24cb9 | 633 | |
91447636 A |
634 | void |
635 | buf_setresid(buf_t bp, uint32_t resid) { | |
636 | ||
637 | bp->b_resid = resid; | |
638 | } | |
1c79356b | 639 | |
91447636 A |
640 | uint32_t |
641 | buf_dirtyoff(buf_t bp) { | |
1c79356b | 642 | |
91447636 A |
643 | return (bp->b_dirtyoff); |
644 | } | |
1c79356b | 645 | |
91447636 A |
646 | uint32_t |
647 | buf_dirtyend(buf_t bp) { | |
1c79356b | 648 | |
91447636 | 649 | return (bp->b_dirtyend); |
1c79356b | 650 | } |
1c79356b | 651 | |
91447636 A |
652 | void |
653 | buf_setdirtyoff(buf_t bp, uint32_t dirtyoff) { | |
654 | ||
655 | bp->b_dirtyoff = dirtyoff; | |
656 | } | |
1c79356b | 657 | |
91447636 A |
658 | void |
659 | buf_setdirtyend(buf_t bp, uint32_t dirtyend) { | |
660 | ||
661 | bp->b_dirtyend = dirtyend; | |
1c79356b A |
662 | } |
663 | ||
91447636 A |
664 | uintptr_t |
665 | buf_dataptr(buf_t bp) { | |
666 | ||
667 | return (bp->b_datap); | |
668 | } | |
1c79356b | 669 | |
91447636 A |
670 | void |
671 | buf_setdataptr(buf_t bp, uintptr_t data) { | |
672 | ||
673 | bp->b_datap = data; | |
674 | } | |
675 | ||
676 | vnode_t | |
677 | buf_vnode(buf_t bp) { | |
678 | ||
679 | return (bp->b_vp); | |
680 | } | |
681 | ||
682 | void | |
683 | buf_setvnode(buf_t bp, vnode_t vp) { | |
684 | ||
685 | bp->b_vp = vp; | |
686 | } | |
687 | ||
688 | ||
689 | void * | |
690 | buf_callback(buf_t bp) | |
691 | { | |
91447636 A |
692 | if ( !(bp->b_flags & B_CALL) ) |
693 | return ((void *) NULL); | |
694 | ||
695 | return ((void *)bp->b_iodone); | |
696 | } | |
697 | ||
698 | ||
699 | errno_t | |
700 | buf_setcallback(buf_t bp, void (*callback)(buf_t, void *), void *transaction) | |
701 | { | |
3e170ce0 A |
702 | assert(!ISSET(bp->b_flags, B_FILTER) && ISSET(bp->b_lflags, BL_BUSY)); |
703 | ||
91447636 A |
704 | if (callback) |
705 | bp->b_flags |= (B_CALL | B_ASYNC); | |
706 | else | |
707 | bp->b_flags &= ~B_CALL; | |
708 | bp->b_transaction = transaction; | |
709 | bp->b_iodone = callback; | |
710 | ||
711 | return (0); | |
712 | } | |
713 | ||
714 | errno_t | |
715 | buf_setupl(buf_t bp, upl_t upl, uint32_t offset) | |
716 | { | |
717 | ||
718 | if ( !(bp->b_lflags & BL_IOBUF) ) | |
719 | return (EINVAL); | |
720 | ||
721 | if (upl) | |
722 | bp->b_flags |= B_CLUSTER; | |
723 | else | |
724 | bp->b_flags &= ~B_CLUSTER; | |
725 | bp->b_upl = upl; | |
726 | bp->b_uploffset = offset; | |
727 | ||
728 | return (0); | |
729 | } | |
730 | ||
731 | buf_t | |
732 | buf_clone(buf_t bp, int io_offset, int io_size, void (*iodone)(buf_t, void *), void *arg) | |
733 | { | |
734 | buf_t io_bp; | |
735 | ||
736 | if (io_offset < 0 || io_size < 0) | |
737 | return (NULL); | |
738 | ||
739 | if ((unsigned)(io_offset + io_size) > (unsigned)bp->b_bcount) | |
740 | return (NULL); | |
741 | ||
742 | if (bp->b_flags & B_CLUSTER) { | |
743 | if (io_offset && ((bp->b_uploffset + io_offset) & PAGE_MASK)) | |
744 | return (NULL); | |
745 | ||
746 | if (((bp->b_uploffset + io_offset + io_size) & PAGE_MASK) && ((io_offset + io_size) < bp->b_bcount)) | |
747 | return (NULL); | |
748 | } | |
749 | io_bp = alloc_io_buf(bp->b_vp, 0); | |
750 | ||
2d21ac55 | 751 | io_bp->b_flags = bp->b_flags & (B_COMMIT_UPL | B_META | B_PAGEIO | B_CLUSTER | B_PHYS | B_RAW | B_ASYNC | B_READ | B_FUA); |
91447636 A |
752 | |
753 | if (iodone) { | |
754 | io_bp->b_transaction = arg; | |
755 | io_bp->b_iodone = iodone; | |
756 | io_bp->b_flags |= B_CALL; | |
757 | } | |
758 | if (bp->b_flags & B_CLUSTER) { | |
759 | io_bp->b_upl = bp->b_upl; | |
760 | io_bp->b_uploffset = bp->b_uploffset + io_offset; | |
761 | } else { | |
762 | io_bp->b_datap = (uintptr_t)(((char *)bp->b_datap) + io_offset); | |
763 | } | |
764 | io_bp->b_bcount = io_size; | |
765 | ||
766 | return (io_bp); | |
767 | } | |
768 | ||
769 | ||
6d2010ae A |
770 | int |
771 | buf_shadow(buf_t bp) | |
772 | { | |
773 | if (bp->b_lflags & BL_SHADOW) | |
774 | return 1; | |
775 | return 0; | |
776 | } | |
777 | ||
778 | ||
779 | buf_t | |
780 | buf_create_shadow_priv(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg) | |
781 | { | |
782 | return (buf_create_shadow_internal(bp, force_copy, external_storage, iodone, arg, 1)); | |
783 | } | |
784 | ||
785 | buf_t | |
786 | buf_create_shadow(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg) | |
787 | { | |
788 | return (buf_create_shadow_internal(bp, force_copy, external_storage, iodone, arg, 0)); | |
789 | } | |
790 | ||
791 | ||
792 | static buf_t | |
793 | buf_create_shadow_internal(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg, int priv) | |
794 | { | |
795 | buf_t io_bp; | |
796 | ||
797 | KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_START, bp, 0, 0, 0, 0); | |
798 | ||
799 | if ( !(bp->b_flags & B_META) || (bp->b_lflags & BL_IOBUF)) { | |
800 | ||
801 | KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_END, bp, 0, 0, 0, 0); | |
802 | return (NULL); | |
803 | } | |
804 | #ifdef BUF_MAKE_PRIVATE | |
805 | if (bp->b_shadow_ref && bp->b_data_ref == 0 && external_storage == 0) | |
806 | panic("buf_create_shadow: %p is in the private state (%d, %d)", bp, bp->b_shadow_ref, bp->b_data_ref); | |
807 | #endif | |
808 | io_bp = alloc_io_buf(bp->b_vp, priv); | |
809 | ||
810 | io_bp->b_flags = bp->b_flags & (B_META | B_ZALLOC | B_ASYNC | B_READ | B_FUA); | |
811 | io_bp->b_blkno = bp->b_blkno; | |
812 | io_bp->b_lblkno = bp->b_lblkno; | |
813 | ||
814 | if (iodone) { | |
815 | io_bp->b_transaction = arg; | |
816 | io_bp->b_iodone = iodone; | |
817 | io_bp->b_flags |= B_CALL; | |
818 | } | |
819 | if (force_copy == FALSE) { | |
820 | io_bp->b_bcount = bp->b_bcount; | |
821 | io_bp->b_bufsize = bp->b_bufsize; | |
822 | ||
823 | if (external_storage) { | |
824 | io_bp->b_datap = external_storage; | |
825 | #ifdef BUF_MAKE_PRIVATE | |
826 | io_bp->b_data_store = NULL; | |
827 | #endif | |
828 | } else { | |
829 | io_bp->b_datap = bp->b_datap; | |
830 | #ifdef BUF_MAKE_PRIVATE | |
831 | io_bp->b_data_store = bp; | |
832 | #endif | |
833 | } | |
834 | *(buf_t *)(&io_bp->b_orig) = bp; | |
835 | ||
836 | lck_mtx_lock_spin(buf_mtxp); | |
837 | ||
838 | io_bp->b_lflags |= BL_SHADOW; | |
839 | io_bp->b_shadow = bp->b_shadow; | |
840 | bp->b_shadow = io_bp; | |
841 | bp->b_shadow_ref++; | |
842 | ||
843 | #ifdef BUF_MAKE_PRIVATE | |
844 | if (external_storage) | |
845 | io_bp->b_lflags |= BL_EXTERNAL; | |
846 | else | |
847 | bp->b_data_ref++; | |
848 | #endif | |
849 | lck_mtx_unlock(buf_mtxp); | |
850 | } else { | |
851 | if (external_storage) { | |
852 | #ifdef BUF_MAKE_PRIVATE | |
853 | io_bp->b_lflags |= BL_EXTERNAL; | |
854 | #endif | |
855 | io_bp->b_bcount = bp->b_bcount; | |
856 | io_bp->b_bufsize = bp->b_bufsize; | |
857 | io_bp->b_datap = external_storage; | |
858 | } else { | |
859 | allocbuf(io_bp, bp->b_bcount); | |
860 | ||
861 | io_bp->b_lflags |= BL_IOBUF_ALLOC; | |
862 | } | |
863 | bcopy((caddr_t)bp->b_datap, (caddr_t)io_bp->b_datap, bp->b_bcount); | |
864 | ||
865 | #ifdef BUF_MAKE_PRIVATE | |
866 | io_bp->b_data_store = NULL; | |
867 | #endif | |
868 | } | |
869 | KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, io_bp, 0); | |
870 | ||
871 | return (io_bp); | |
872 | } | |
873 | ||
874 | ||
875 | #ifdef BUF_MAKE_PRIVATE | |
876 | errno_t | |
877 | buf_make_private(buf_t bp) | |
878 | { | |
879 | buf_t ds_bp; | |
880 | buf_t t_bp; | |
881 | struct buf my_buf; | |
882 | ||
883 | KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_START, bp, bp->b_shadow_ref, 0, 0, 0); | |
884 | ||
885 | if (bp->b_shadow_ref == 0 || bp->b_data_ref == 0 || ISSET(bp->b_lflags, BL_SHADOW)) { | |
886 | ||
887 | KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, EINVAL, 0); | |
888 | return (EINVAL); | |
889 | } | |
890 | my_buf.b_flags = B_META; | |
891 | my_buf.b_datap = (uintptr_t)NULL; | |
892 | allocbuf(&my_buf, bp->b_bcount); | |
893 | ||
894 | bcopy((caddr_t)bp->b_datap, (caddr_t)my_buf.b_datap, bp->b_bcount); | |
895 | ||
896 | lck_mtx_lock_spin(buf_mtxp); | |
897 | ||
898 | for (t_bp = bp->b_shadow; t_bp; t_bp = t_bp->b_shadow) { | |
899 | if ( !ISSET(bp->b_lflags, BL_EXTERNAL)) | |
900 | break; | |
901 | } | |
902 | ds_bp = t_bp; | |
903 | ||
904 | if (ds_bp == NULL && bp->b_data_ref) | |
905 | panic("buf_make_private: b_data_ref != 0 && ds_bp == NULL"); | |
906 | ||
907 | if (ds_bp && (bp->b_data_ref == 0 || bp->b_shadow_ref == 0)) | |
908 | panic("buf_make_private: ref_count == 0 && ds_bp != NULL"); | |
909 | ||
910 | if (ds_bp == NULL) { | |
911 | lck_mtx_unlock(buf_mtxp); | |
912 | ||
913 | buf_free_meta_store(&my_buf); | |
914 | ||
915 | KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, EINVAL, 0); | |
916 | return (EINVAL); | |
917 | } | |
918 | for (t_bp = bp->b_shadow; t_bp; t_bp = t_bp->b_shadow) { | |
919 | if ( !ISSET(t_bp->b_lflags, BL_EXTERNAL)) | |
920 | t_bp->b_data_store = ds_bp; | |
921 | } | |
922 | ds_bp->b_data_ref = bp->b_data_ref; | |
923 | ||
924 | bp->b_data_ref = 0; | |
925 | bp->b_datap = my_buf.b_datap; | |
926 | ||
927 | lck_mtx_unlock(buf_mtxp); | |
928 | ||
929 | KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, 0, 0); | |
930 | return (0); | |
931 | } | |
932 | #endif | |
933 | ||
91447636 A |
934 | |
935 | void | |
936 | buf_setfilter(buf_t bp, void (*filter)(buf_t, void *), void *transaction, | |
6d2010ae | 937 | void (**old_iodone)(buf_t, void *), void **old_transaction) |
91447636 | 938 | { |
3e170ce0 A |
939 | assert(ISSET(bp->b_lflags, BL_BUSY)); |
940 | ||
6d2010ae A |
941 | if (old_iodone) |
942 | *old_iodone = bp->b_iodone; | |
91447636 | 943 | if (old_transaction) |
6d2010ae | 944 | *old_transaction = bp->b_transaction; |
91447636 A |
945 | |
946 | bp->b_transaction = transaction; | |
947 | bp->b_iodone = filter; | |
2d21ac55 A |
948 | if (filter) |
949 | bp->b_flags |= B_FILTER; | |
950 | else | |
951 | bp->b_flags &= ~B_FILTER; | |
91447636 A |
952 | } |
953 | ||
954 | ||
955 | daddr64_t | |
956 | buf_blkno(buf_t bp) { | |
957 | ||
958 | return (bp->b_blkno); | |
959 | } | |
960 | ||
961 | daddr64_t | |
962 | buf_lblkno(buf_t bp) { | |
963 | ||
964 | return (bp->b_lblkno); | |
965 | } | |
966 | ||
967 | void | |
968 | buf_setblkno(buf_t bp, daddr64_t blkno) { | |
969 | ||
970 | bp->b_blkno = blkno; | |
971 | } | |
972 | ||
973 | void | |
974 | buf_setlblkno(buf_t bp, daddr64_t lblkno) { | |
975 | ||
976 | bp->b_lblkno = lblkno; | |
977 | } | |
978 | ||
979 | dev_t | |
980 | buf_device(buf_t bp) { | |
981 | ||
982 | return (bp->b_dev); | |
983 | } | |
984 | ||
985 | errno_t | |
986 | buf_setdevice(buf_t bp, vnode_t vp) { | |
987 | ||
988 | if ((vp->v_type != VBLK) && (vp->v_type != VCHR)) | |
989 | return EINVAL; | |
990 | bp->b_dev = vp->v_rdev; | |
991 | ||
992 | return 0; | |
993 | } | |
994 | ||
995 | ||
996 | void * | |
997 | buf_drvdata(buf_t bp) { | |
998 | ||
999 | return (bp->b_drvdata); | |
1000 | } | |
1001 | ||
1002 | void | |
1003 | buf_setdrvdata(buf_t bp, void *drvdata) { | |
1004 | ||
1005 | bp->b_drvdata = drvdata; | |
1006 | } | |
1007 | ||
1008 | void * | |
1009 | buf_fsprivate(buf_t bp) { | |
1010 | ||
1011 | return (bp->b_fsprivate); | |
1012 | } | |
1013 | ||
1014 | void | |
1015 | buf_setfsprivate(buf_t bp, void *fsprivate) { | |
1016 | ||
1017 | bp->b_fsprivate = fsprivate; | |
1018 | } | |
1019 | ||
b0d623f7 | 1020 | kauth_cred_t |
91447636 A |
1021 | buf_rcred(buf_t bp) { |
1022 | ||
1023 | return (bp->b_rcred); | |
1024 | } | |
1025 | ||
b0d623f7 | 1026 | kauth_cred_t |
91447636 A |
1027 | buf_wcred(buf_t bp) { |
1028 | ||
1029 | return (bp->b_wcred); | |
1030 | } | |
1031 | ||
1032 | void * | |
1033 | buf_upl(buf_t bp) { | |
1034 | ||
1035 | return (bp->b_upl); | |
1036 | } | |
1037 | ||
1038 | uint32_t | |
1039 | buf_uploffset(buf_t bp) { | |
1040 | ||
1041 | return ((uint32_t)(bp->b_uploffset)); | |
1042 | } | |
1043 | ||
1044 | proc_t | |
1045 | buf_proc(buf_t bp) { | |
1046 | ||
1047 | return (bp->b_proc); | |
1048 | } | |
1049 | ||
1050 | ||
1051 | errno_t | |
1052 | buf_map(buf_t bp, caddr_t *io_addr) | |
1053 | { | |
1054 | buf_t real_bp; | |
b0d623f7 | 1055 | vm_offset_t vaddr; |
91447636 A |
1056 | kern_return_t kret; |
1057 | ||
1058 | if ( !(bp->b_flags & B_CLUSTER)) { | |
1059 | *io_addr = (caddr_t)bp->b_datap; | |
1060 | return (0); | |
1061 | } | |
1062 | real_bp = (buf_t)(bp->b_real_bp); | |
1063 | ||
1064 | if (real_bp && real_bp->b_datap) { | |
1065 | /* | |
1066 | * b_real_bp is only valid if B_CLUSTER is SET | |
1067 | * if it's non-zero, than someone did a cluster_bp call | |
1068 | * if the backing physical pages were already mapped | |
1069 | * in before the call to cluster_bp (non-zero b_datap), | |
1070 | * than we just use that mapping | |
1071 | */ | |
1072 | *io_addr = (caddr_t)real_bp->b_datap; | |
1073 | return (0); | |
1074 | } | |
1075 | kret = ubc_upl_map(bp->b_upl, &vaddr); /* Map it in */ | |
1076 | ||
1077 | if (kret != KERN_SUCCESS) { | |
2d21ac55 | 1078 | *io_addr = NULL; |
91447636 A |
1079 | |
1080 | return(ENOMEM); | |
1081 | } | |
1082 | vaddr += bp->b_uploffset; | |
1083 | ||
1084 | *io_addr = (caddr_t)vaddr; | |
1085 | ||
1086 | return (0); | |
1087 | } | |
1088 | ||
1089 | errno_t | |
1090 | buf_unmap(buf_t bp) | |
1091 | { | |
1092 | buf_t real_bp; | |
1093 | kern_return_t kret; | |
1094 | ||
1095 | if ( !(bp->b_flags & B_CLUSTER)) | |
1096 | return (0); | |
1097 | /* | |
1098 | * see buf_map for the explanation | |
1099 | */ | |
1100 | real_bp = (buf_t)(bp->b_real_bp); | |
1101 | ||
1102 | if (real_bp && real_bp->b_datap) | |
1103 | return (0); | |
1104 | ||
2d21ac55 A |
1105 | if ((bp->b_lflags & BL_IOBUF) && |
1106 | ((bp->b_flags & (B_PAGEIO | B_READ)) != (B_PAGEIO | B_READ))) { | |
91447636 | 1107 | /* |
2d21ac55 A |
1108 | * ignore pageins... the 'right' thing will |
1109 | * happen due to the way we handle speculative | |
1110 | * clusters... | |
1111 | * | |
91447636 A |
1112 | * when we commit these pages, we'll hit |
1113 | * it with UPL_COMMIT_INACTIVE which | |
1114 | * will clear the reference bit that got | |
1115 | * turned on when we touched the mapping | |
1116 | */ | |
1117 | bp->b_flags |= B_AGE; | |
1118 | } | |
1119 | kret = ubc_upl_unmap(bp->b_upl); | |
1120 | ||
1121 | if (kret != KERN_SUCCESS) | |
1122 | return (EINVAL); | |
1123 | return (0); | |
1124 | } | |
1125 | ||
1126 | ||
1127 | void | |
1128 | buf_clear(buf_t bp) { | |
1129 | caddr_t baddr; | |
1130 | ||
1131 | if (buf_map(bp, &baddr) == 0) { | |
1132 | bzero(baddr, bp->b_bcount); | |
1133 | buf_unmap(bp); | |
1134 | } | |
1135 | bp->b_resid = 0; | |
1136 | } | |
1137 | ||
91447636 A |
1138 | /* |
1139 | * Read or write a buffer that is not contiguous on disk. | |
1140 | * buffer is marked done/error at the conclusion | |
1141 | */ | |
1142 | static int | |
1143 | buf_strategy_fragmented(vnode_t devvp, buf_t bp, off_t f_offset, size_t contig_bytes) | |
1144 | { | |
1145 | vnode_t vp = buf_vnode(bp); | |
1146 | buf_t io_bp; /* For reading or writing a single block */ | |
1147 | int io_direction; | |
1148 | int io_resid; | |
1149 | size_t io_contig_bytes; | |
1150 | daddr64_t io_blkno; | |
1151 | int error = 0; | |
1152 | int bmap_flags; | |
1153 | ||
1154 | /* | |
1155 | * save our starting point... the bp was already mapped | |
1156 | * in buf_strategy before we got called | |
1157 | * no sense doing it again. | |
1158 | */ | |
1159 | io_blkno = bp->b_blkno; | |
1160 | /* | |
1161 | * Make sure we redo this mapping for the next I/O | |
1162 | * i.e. this can never be a 'permanent' mapping | |
1163 | */ | |
1164 | bp->b_blkno = bp->b_lblkno; | |
1165 | ||
1166 | /* | |
1167 | * Get an io buffer to do the deblocking | |
1168 | */ | |
1169 | io_bp = alloc_io_buf(devvp, 0); | |
1170 | ||
1171 | io_bp->b_lblkno = bp->b_lblkno; | |
1172 | io_bp->b_datap = bp->b_datap; | |
1173 | io_resid = bp->b_bcount; | |
1174 | io_direction = bp->b_flags & B_READ; | |
1175 | io_contig_bytes = contig_bytes; | |
1176 | ||
1177 | if (bp->b_flags & B_READ) | |
1178 | bmap_flags = VNODE_READ; | |
1179 | else | |
1180 | bmap_flags = VNODE_WRITE; | |
1181 | ||
1182 | for (;;) { | |
1183 | if (io_blkno == -1) | |
1184 | /* | |
1185 | * this is unexepected, but we'll allow for it | |
1186 | */ | |
1187 | bzero((caddr_t)io_bp->b_datap, (int)io_contig_bytes); | |
1188 | else { | |
1189 | io_bp->b_bcount = io_contig_bytes; | |
1190 | io_bp->b_bufsize = io_contig_bytes; | |
1191 | io_bp->b_resid = io_contig_bytes; | |
1192 | io_bp->b_blkno = io_blkno; | |
1193 | ||
1194 | buf_reset(io_bp, io_direction); | |
2d21ac55 | 1195 | |
91447636 | 1196 | /* |
2d21ac55 | 1197 | * Call the device to do the I/O and wait for it. Make sure the appropriate party is charged for write |
91447636 | 1198 | */ |
2d21ac55 A |
1199 | |
1200 | if (!ISSET(bp->b_flags, B_READ)) | |
1201 | OSAddAtomic(1, &devvp->v_numoutput); | |
1202 | ||
91447636 A |
1203 | if ((error = VNOP_STRATEGY(io_bp))) |
1204 | break; | |
1205 | if ((error = (int)buf_biowait(io_bp))) | |
1206 | break; | |
1207 | if (io_bp->b_resid) { | |
1208 | io_resid -= (io_contig_bytes - io_bp->b_resid); | |
1209 | break; | |
1210 | } | |
1211 | } | |
1212 | if ((io_resid -= io_contig_bytes) == 0) | |
1213 | break; | |
1214 | f_offset += io_contig_bytes; | |
1215 | io_bp->b_datap += io_contig_bytes; | |
1216 | ||
1217 | /* | |
1218 | * Map the current position to a physical block number | |
1219 | */ | |
1220 | if ((error = VNOP_BLOCKMAP(vp, f_offset, io_resid, &io_blkno, &io_contig_bytes, NULL, bmap_flags, NULL))) | |
1221 | break; | |
1222 | } | |
1223 | buf_free(io_bp); | |
1224 | ||
1225 | if (error) | |
1226 | buf_seterror(bp, error); | |
1227 | bp->b_resid = io_resid; | |
1228 | /* | |
1229 | * This I/O is now complete | |
1230 | */ | |
1231 | buf_biodone(bp); | |
1232 | ||
1233 | return error; | |
1234 | } | |
1235 | ||
1236 | ||
1237 | /* | |
1238 | * struct vnop_strategy_args { | |
1239 | * struct buf *a_bp; | |
1240 | * } *ap; | |
1241 | */ | |
1242 | errno_t | |
1243 | buf_strategy(vnode_t devvp, void *ap) | |
1244 | { | |
1245 | buf_t bp = ((struct vnop_strategy_args *)ap)->a_bp; | |
1246 | vnode_t vp = bp->b_vp; | |
1247 | int bmap_flags; | |
1248 | errno_t error; | |
6d2010ae A |
1249 | #if CONFIG_DTRACE |
1250 | int dtrace_io_start_flag = 0; /* We only want to trip the io:::start | |
39236c6e | 1251 | * probe once, with the true physical |
6d2010ae A |
1252 | * block in place (b_blkno) |
1253 | */ | |
1254 | ||
1255 | #endif | |
91447636 A |
1256 | |
1257 | if (vp == NULL || vp->v_type == VCHR || vp->v_type == VBLK) | |
1258 | panic("buf_strategy: b_vp == NULL || vtype == VCHR | VBLK\n"); | |
1259 | /* | |
1260 | * associate the physical device with | |
1261 | * with this buf_t even if we don't | |
1262 | * end up issuing the I/O... | |
1263 | */ | |
1264 | bp->b_dev = devvp->v_rdev; | |
1265 | ||
1266 | if (bp->b_flags & B_READ) | |
1267 | bmap_flags = VNODE_READ; | |
1268 | else | |
1269 | bmap_flags = VNODE_WRITE; | |
1270 | ||
1271 | if ( !(bp->b_flags & B_CLUSTER)) { | |
1272 | ||
1273 | if ( (bp->b_upl) ) { | |
1274 | /* | |
1275 | * we have a UPL associated with this bp | |
1276 | * go through cluster_bp which knows how | |
1277 | * to deal with filesystem block sizes | |
1278 | * that aren't equal to the page size | |
1279 | */ | |
6d2010ae | 1280 | DTRACE_IO1(start, buf_t, bp); |
91447636 A |
1281 | return (cluster_bp(bp)); |
1282 | } | |
1283 | if (bp->b_blkno == bp->b_lblkno) { | |
316670eb | 1284 | off_t f_offset; |
91447636 A |
1285 | size_t contig_bytes; |
1286 | ||
1287 | if ((error = VNOP_BLKTOOFF(vp, bp->b_lblkno, &f_offset))) { | |
6d2010ae | 1288 | DTRACE_IO1(start, buf_t, bp); |
91447636 A |
1289 | buf_seterror(bp, error); |
1290 | buf_biodone(bp); | |
1291 | ||
316670eb | 1292 | return (error); |
91447636 | 1293 | } |
316670eb A |
1294 | |
1295 | if ((error = VNOP_BLOCKMAP(vp, f_offset, bp->b_bcount, &bp->b_blkno, &contig_bytes, NULL, bmap_flags, NULL))) { | |
6d2010ae | 1296 | DTRACE_IO1(start, buf_t, bp); |
91447636 A |
1297 | buf_seterror(bp, error); |
1298 | buf_biodone(bp); | |
1299 | ||
1300 | return (error); | |
1301 | } | |
316670eb | 1302 | |
6d2010ae A |
1303 | DTRACE_IO1(start, buf_t, bp); |
1304 | #if CONFIG_DTRACE | |
1305 | dtrace_io_start_flag = 1; | |
1306 | #endif /* CONFIG_DTRACE */ | |
316670eb | 1307 | |
b0d623f7 A |
1308 | if ((bp->b_blkno == -1) || (contig_bytes == 0)) { |
1309 | /* Set block number to force biodone later */ | |
1310 | bp->b_blkno = -1; | |
91447636 | 1311 | buf_clear(bp); |
b0d623f7 | 1312 | } |
6d2010ae | 1313 | else if ((long)contig_bytes < bp->b_bcount) { |
91447636 | 1314 | return (buf_strategy_fragmented(devvp, bp, f_offset, contig_bytes)); |
6d2010ae | 1315 | } |
91447636 | 1316 | } |
6d2010ae A |
1317 | |
1318 | #if CONFIG_DTRACE | |
1319 | if (dtrace_io_start_flag == 0) { | |
1320 | DTRACE_IO1(start, buf_t, bp); | |
1321 | dtrace_io_start_flag = 1; | |
1322 | } | |
1323 | #endif /* CONFIG_DTRACE */ | |
1324 | ||
91447636 A |
1325 | if (bp->b_blkno == -1) { |
1326 | buf_biodone(bp); | |
1327 | return (0); | |
1328 | } | |
1329 | } | |
6d2010ae A |
1330 | |
1331 | #if CONFIG_DTRACE | |
1332 | if (dtrace_io_start_flag == 0) | |
1333 | DTRACE_IO1(start, buf_t, bp); | |
1334 | #endif /* CONFIG_DTRACE */ | |
1335 | ||
316670eb A |
1336 | #if CONFIG_PROTECT |
1337 | /* Capture f_offset in the bufattr*/ | |
3e170ce0 A |
1338 | cpx_t cpx = bufattr_cpx(buf_attr(bp)); |
1339 | if (cpx) { | |
316670eb | 1340 | /* No need to go here for older EAs */ |
39037602 | 1341 | if(cpx_use_offset_for_iv(cpx) && !cpx_synthetic_offset_for_iv(cpx)) { |
316670eb A |
1342 | off_t f_offset; |
1343 | if ((error = VNOP_BLKTOOFF(bp->b_vp, bp->b_lblkno, &f_offset))) | |
1344 | return error; | |
1345 | ||
1346 | /* | |
1347 | * Attach the file offset to this buffer. The | |
1348 | * bufattr attributes will be passed down the stack | |
813fb2f6 A |
1349 | * until they reach the storage driver (whether |
1350 | * IOFlashStorage, ASP, or IONVMe). The driver | |
316670eb A |
1351 | * will retain the offset in a local variable when it |
1352 | * issues its I/Os to the NAND controller. | |
1353 | * | |
1354 | * Note that LwVM may end up splitting this I/O | |
1355 | * into sub-I/Os if it crosses a chunk boundary. In this | |
1356 | * case, LwVM will update this field when it dispatches | |
1357 | * each I/O to IOFlashStorage. But from our perspective | |
1358 | * we have only issued a single I/O. | |
813fb2f6 A |
1359 | * |
1360 | * In the case of APFS we do not bounce through another | |
1361 | * intermediate layer (such as CoreStorage). APFS will | |
1362 | * issue the I/Os directly to the block device / IOMedia | |
1363 | * via buf_strategy on the specfs node. | |
316670eb | 1364 | */ |
3e170ce0 | 1365 | buf_setcpoff(bp, f_offset); |
fe8ab488 | 1366 | CP_DEBUG((CPDBG_OFFSET_IO | DBG_FUNC_NONE), (uint32_t) f_offset, (uint32_t) bp->b_lblkno, (uint32_t) bp->b_blkno, (uint32_t) bp->b_bcount, 0); |
316670eb A |
1367 | } |
1368 | } | |
1369 | #endif | |
1370 | ||
91447636 A |
1371 | /* |
1372 | * we can issue the I/O because... | |
1373 | * either B_CLUSTER is set which | |
1374 | * means that the I/O is properly set | |
1375 | * up to be a multiple of the page size, or | |
1376 | * we were able to successfully set up the | |
39236c6e | 1377 | * physical block mapping |
91447636 | 1378 | */ |
39236c6e A |
1379 | error = VOCALL(devvp->v_op, VOFFSET(vnop_strategy), ap); |
1380 | DTRACE_FSINFO(strategy, vnode_t, vp); | |
1381 | return (error); | |
91447636 A |
1382 | } |
1383 | ||
1384 | ||
1385 | ||
1386 | buf_t | |
1387 | buf_alloc(vnode_t vp) | |
1388 | { | |
39037602 | 1389 | return(alloc_io_buf(vp, is_vm_privileged())); |
91447636 A |
1390 | } |
1391 | ||
1392 | void | |
1393 | buf_free(buf_t bp) { | |
1394 | ||
1395 | free_io_buf(bp); | |
1396 | } | |
1397 | ||
1398 | ||
2d21ac55 A |
1399 | /* |
1400 | * iterate buffers for the specified vp. | |
1401 | * if BUF_SCAN_DIRTY is set, do the dirty list | |
1402 | * if BUF_SCAN_CLEAN is set, do the clean list | |
1403 | * if neither flag is set, default to BUF_SCAN_DIRTY | |
1404 | * if BUF_NOTIFY_BUSY is set, call the callout function using a NULL bp for busy pages | |
1405 | */ | |
1406 | ||
1407 | struct buf_iterate_info_t { | |
1408 | int flag; | |
1409 | struct buflists *listhead; | |
1410 | }; | |
91447636 A |
1411 | |
1412 | void | |
2d21ac55 A |
1413 | buf_iterate(vnode_t vp, int (*callout)(buf_t, void *), int flags, void *arg) |
1414 | { | |
91447636 A |
1415 | buf_t bp; |
1416 | int retval; | |
1417 | struct buflists local_iterblkhd; | |
1418 | int lock_flags = BAC_NOWAIT | BAC_REMOVE; | |
2d21ac55 A |
1419 | int notify_busy = flags & BUF_NOTIFY_BUSY; |
1420 | struct buf_iterate_info_t list[2]; | |
1421 | int num_lists, i; | |
91447636 A |
1422 | |
1423 | if (flags & BUF_SKIP_LOCKED) | |
1424 | lock_flags |= BAC_SKIP_LOCKED; | |
1425 | if (flags & BUF_SKIP_NONLOCKED) | |
1426 | lock_flags |= BAC_SKIP_NONLOCKED; | |
1427 | ||
2d21ac55 A |
1428 | if ( !(flags & (BUF_SCAN_DIRTY | BUF_SCAN_CLEAN))) |
1429 | flags |= BUF_SCAN_DIRTY; | |
1430 | ||
1431 | num_lists = 0; | |
1432 | ||
1433 | if (flags & BUF_SCAN_DIRTY) { | |
1434 | list[num_lists].flag = VBI_DIRTY; | |
1435 | list[num_lists].listhead = &vp->v_dirtyblkhd; | |
1436 | num_lists++; | |
1437 | } | |
1438 | if (flags & BUF_SCAN_CLEAN) { | |
1439 | list[num_lists].flag = VBI_CLEAN; | |
1440 | list[num_lists].listhead = &vp->v_cleanblkhd; | |
1441 | num_lists++; | |
91447636 | 1442 | } |
91447636 | 1443 | |
2d21ac55 A |
1444 | for (i = 0; i < num_lists; i++) { |
1445 | lck_mtx_lock(buf_mtxp); | |
1446 | ||
1447 | if (buf_iterprepare(vp, &local_iterblkhd, list[i].flag)) { | |
1448 | lck_mtx_unlock(buf_mtxp); | |
1449 | continue; | |
1450 | } | |
1451 | while (!LIST_EMPTY(&local_iterblkhd)) { | |
1452 | bp = LIST_FIRST(&local_iterblkhd); | |
1453 | LIST_REMOVE(bp, b_vnbufs); | |
1454 | LIST_INSERT_HEAD(list[i].listhead, bp, b_vnbufs); | |
91447636 | 1455 | |
2d21ac55 A |
1456 | if (buf_acquire_locked(bp, lock_flags, 0, 0)) { |
1457 | if (notify_busy) { | |
1458 | bp = NULL; | |
1459 | } else { | |
1460 | continue; | |
1461 | } | |
1462 | } | |
91447636 | 1463 | |
2d21ac55 | 1464 | lck_mtx_unlock(buf_mtxp); |
91447636 | 1465 | |
2d21ac55 | 1466 | retval = callout(bp, arg); |
91447636 | 1467 | |
2d21ac55 A |
1468 | switch (retval) { |
1469 | case BUF_RETURNED: | |
1470 | if (bp) | |
1471 | buf_brelse(bp); | |
1472 | break; | |
1473 | case BUF_CLAIMED: | |
1474 | break; | |
1475 | case BUF_RETURNED_DONE: | |
1476 | if (bp) | |
1477 | buf_brelse(bp); | |
1478 | lck_mtx_lock(buf_mtxp); | |
1479 | goto out; | |
1480 | case BUF_CLAIMED_DONE: | |
1481 | lck_mtx_lock(buf_mtxp); | |
1482 | goto out; | |
1483 | } | |
1484 | lck_mtx_lock(buf_mtxp); | |
1485 | } /* while list has more nodes */ | |
1486 | out: | |
1487 | buf_itercomplete(vp, &local_iterblkhd, list[i].flag); | |
1488 | lck_mtx_unlock(buf_mtxp); | |
1489 | } /* for each list */ | |
1490 | } /* buf_iterate */ | |
91447636 A |
1491 | |
1492 | ||
1493 | /* | |
1494 | * Flush out and invalidate all buffers associated with a vnode. | |
1495 | */ | |
1496 | int | |
1497 | buf_invalidateblks(vnode_t vp, int flags, int slpflag, int slptimeo) | |
1498 | { | |
1499 | buf_t bp; | |
6d2010ae | 1500 | int aflags; |
91447636 A |
1501 | int error = 0; |
1502 | int must_rescan = 1; | |
1503 | struct buflists local_iterblkhd; | |
1504 | ||
b0d623f7 A |
1505 | |
1506 | if (LIST_EMPTY(&vp->v_cleanblkhd) && LIST_EMPTY(&vp->v_dirtyblkhd)) | |
1507 | return (0); | |
1508 | ||
91447636 A |
1509 | lck_mtx_lock(buf_mtxp); |
1510 | ||
1511 | for (;;) { | |
1512 | if (must_rescan == 0) | |
1513 | /* | |
1514 | * the lists may not be empty, but all that's left at this | |
1515 | * point are metadata or B_LOCKED buffers which are being | |
1516 | * skipped... we know this because we made it through both | |
1517 | * the clean and dirty lists without dropping buf_mtxp... | |
1518 | * each time we drop buf_mtxp we bump "must_rescan" | |
1519 | */ | |
1520 | break; | |
1521 | if (LIST_EMPTY(&vp->v_cleanblkhd) && LIST_EMPTY(&vp->v_dirtyblkhd)) | |
1522 | break; | |
1523 | must_rescan = 0; | |
1524 | /* | |
1525 | * iterate the clean list | |
1526 | */ | |
1527 | if (buf_iterprepare(vp, &local_iterblkhd, VBI_CLEAN)) { | |
1528 | goto try_dirty_list; | |
1529 | } | |
1530 | while (!LIST_EMPTY(&local_iterblkhd)) { | |
6d2010ae | 1531 | |
91447636 A |
1532 | bp = LIST_FIRST(&local_iterblkhd); |
1533 | ||
1534 | LIST_REMOVE(bp, b_vnbufs); | |
1535 | LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs); | |
1536 | ||
1537 | /* | |
1538 | * some filesystems distinguish meta data blocks with a negative logical block # | |
1539 | */ | |
1540 | if ((flags & BUF_SKIP_META) && (bp->b_lblkno < 0 || ISSET(bp->b_flags, B_META))) | |
1541 | continue; | |
1542 | ||
6d2010ae A |
1543 | aflags = BAC_REMOVE; |
1544 | ||
1545 | if ( !(flags & BUF_INVALIDATE_LOCKED) ) | |
1546 | aflags |= BAC_SKIP_LOCKED; | |
1547 | ||
1548 | if ( (error = (int)buf_acquire_locked(bp, aflags, slpflag, slptimeo)) ) { | |
91447636 A |
1549 | if (error == EDEADLK) |
1550 | /* | |
1551 | * this buffer was marked B_LOCKED... | |
1552 | * we didn't drop buf_mtxp, so we | |
1553 | * we don't need to rescan | |
1554 | */ | |
1555 | continue; | |
1556 | if (error == EAGAIN) { | |
1557 | /* | |
1558 | * found a busy buffer... we blocked and | |
1559 | * dropped buf_mtxp, so we're going to | |
1560 | * need to rescan after this pass is completed | |
1561 | */ | |
1562 | must_rescan++; | |
1563 | continue; | |
1564 | } | |
1565 | /* | |
1566 | * got some kind of 'real' error out of the msleep | |
1567 | * in buf_acquire_locked, terminate the scan and return the error | |
1568 | */ | |
1569 | buf_itercomplete(vp, &local_iterblkhd, VBI_CLEAN); | |
1570 | ||
1571 | lck_mtx_unlock(buf_mtxp); | |
1572 | return (error); | |
1573 | } | |
1574 | lck_mtx_unlock(buf_mtxp); | |
1575 | ||
6d2010ae A |
1576 | if (bp->b_flags & B_LOCKED) |
1577 | KERNEL_DEBUG(0xbbbbc038, bp, 0, 0, 0, 0); | |
1578 | ||
1579 | CLR(bp->b_flags, B_LOCKED); | |
91447636 A |
1580 | SET(bp->b_flags, B_INVAL); |
1581 | buf_brelse(bp); | |
1582 | ||
1583 | lck_mtx_lock(buf_mtxp); | |
1584 | ||
1585 | /* | |
1586 | * by dropping buf_mtxp, we allow new | |
1587 | * buffers to be added to the vnode list(s) | |
1588 | * we'll have to rescan at least once more | |
1589 | * if the queues aren't empty | |
1590 | */ | |
1591 | must_rescan++; | |
1592 | } | |
1593 | buf_itercomplete(vp, &local_iterblkhd, VBI_CLEAN); | |
1594 | ||
1595 | try_dirty_list: | |
1596 | /* | |
1597 | * Now iterate on dirty blks | |
1598 | */ | |
1599 | if (buf_iterprepare(vp, &local_iterblkhd, VBI_DIRTY)) { | |
1600 | continue; | |
1601 | } | |
1602 | while (!LIST_EMPTY(&local_iterblkhd)) { | |
1603 | bp = LIST_FIRST(&local_iterblkhd); | |
1604 | ||
1605 | LIST_REMOVE(bp, b_vnbufs); | |
1606 | LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs); | |
1607 | ||
1608 | /* | |
1609 | * some filesystems distinguish meta data blocks with a negative logical block # | |
1610 | */ | |
1611 | if ((flags & BUF_SKIP_META) && (bp->b_lblkno < 0 || ISSET(bp->b_flags, B_META))) | |
1612 | continue; | |
1613 | ||
6d2010ae A |
1614 | aflags = BAC_REMOVE; |
1615 | ||
1616 | if ( !(flags & BUF_INVALIDATE_LOCKED) ) | |
1617 | aflags |= BAC_SKIP_LOCKED; | |
1618 | ||
1619 | if ( (error = (int)buf_acquire_locked(bp, aflags, slpflag, slptimeo)) ) { | |
91447636 A |
1620 | if (error == EDEADLK) |
1621 | /* | |
1622 | * this buffer was marked B_LOCKED... | |
1623 | * we didn't drop buf_mtxp, so we | |
1624 | * we don't need to rescan | |
1625 | */ | |
1626 | continue; | |
1627 | if (error == EAGAIN) { | |
1628 | /* | |
1629 | * found a busy buffer... we blocked and | |
1630 | * dropped buf_mtxp, so we're going to | |
1631 | * need to rescan after this pass is completed | |
1632 | */ | |
1633 | must_rescan++; | |
1634 | continue; | |
1635 | } | |
1636 | /* | |
1637 | * got some kind of 'real' error out of the msleep | |
1638 | * in buf_acquire_locked, terminate the scan and return the error | |
1639 | */ | |
1640 | buf_itercomplete(vp, &local_iterblkhd, VBI_DIRTY); | |
1641 | ||
1642 | lck_mtx_unlock(buf_mtxp); | |
1643 | return (error); | |
1644 | } | |
1645 | lck_mtx_unlock(buf_mtxp); | |
1646 | ||
6d2010ae A |
1647 | if (bp->b_flags & B_LOCKED) |
1648 | KERNEL_DEBUG(0xbbbbc038, bp, 0, 0, 1, 0); | |
1649 | ||
1650 | CLR(bp->b_flags, B_LOCKED); | |
91447636 A |
1651 | SET(bp->b_flags, B_INVAL); |
1652 | ||
1653 | if (ISSET(bp->b_flags, B_DELWRI) && (flags & BUF_WRITE_DATA)) | |
1654 | (void) VNOP_BWRITE(bp); | |
1655 | else | |
1656 | buf_brelse(bp); | |
1657 | ||
1658 | lck_mtx_lock(buf_mtxp); | |
1659 | /* | |
1660 | * by dropping buf_mtxp, we allow new | |
1661 | * buffers to be added to the vnode list(s) | |
1662 | * we'll have to rescan at least once more | |
1663 | * if the queues aren't empty | |
1664 | */ | |
1665 | must_rescan++; | |
1666 | } | |
1667 | buf_itercomplete(vp, &local_iterblkhd, VBI_DIRTY); | |
1668 | } | |
1669 | lck_mtx_unlock(buf_mtxp); | |
1670 | ||
1671 | return (0); | |
1672 | } | |
1673 | ||
1674 | void | |
2d21ac55 | 1675 | buf_flushdirtyblks(vnode_t vp, int wait, int flags, const char *msg) { |
316670eb A |
1676 | |
1677 | (void) buf_flushdirtyblks_skipinfo(vp, wait, flags, msg); | |
1678 | return; | |
1679 | } | |
1680 | ||
1681 | int | |
1682 | buf_flushdirtyblks_skipinfo(vnode_t vp, int wait, int flags, const char *msg) { | |
91447636 A |
1683 | buf_t bp; |
1684 | int writes_issued = 0; | |
1685 | errno_t error; | |
1686 | int busy = 0; | |
1687 | struct buflists local_iterblkhd; | |
1688 | int lock_flags = BAC_NOWAIT | BAC_REMOVE; | |
316670eb | 1689 | int any_locked = 0; |
91447636 A |
1690 | |
1691 | if (flags & BUF_SKIP_LOCKED) | |
1692 | lock_flags |= BAC_SKIP_LOCKED; | |
1693 | if (flags & BUF_SKIP_NONLOCKED) | |
1694 | lock_flags |= BAC_SKIP_NONLOCKED; | |
1695 | loop: | |
1696 | lck_mtx_lock(buf_mtxp); | |
1697 | ||
1698 | if (buf_iterprepare(vp, &local_iterblkhd, VBI_DIRTY) == 0) { | |
1699 | while (!LIST_EMPTY(&local_iterblkhd)) { | |
1700 | bp = LIST_FIRST(&local_iterblkhd); | |
1701 | LIST_REMOVE(bp, b_vnbufs); | |
1702 | LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs); | |
316670eb A |
1703 | |
1704 | if ((error = buf_acquire_locked(bp, lock_flags, 0, 0)) == EBUSY) { | |
1705 | busy++; | |
1706 | } | |
1707 | if (error) { | |
1708 | /* | |
1709 | * If we passed in BUF_SKIP_LOCKED or BUF_SKIP_NONLOCKED, | |
1710 | * we may want to do somethign differently if a locked or unlocked | |
1711 | * buffer was encountered (depending on the arg specified). | |
1712 | * In this case, we know that one of those two was set, and the | |
1713 | * buf acquisition failed above. | |
1714 | * | |
1715 | * If it failed with EDEADLK, then save state which can be emitted | |
1716 | * later on to the caller. Most callers should not care. | |
1717 | */ | |
1718 | if (error == EDEADLK) { | |
1719 | any_locked++; | |
1720 | } | |
1721 | continue; | |
1722 | } | |
91447636 A |
1723 | lck_mtx_unlock(buf_mtxp); |
1724 | ||
1725 | bp->b_flags &= ~B_LOCKED; | |
1726 | ||
1727 | /* | |
1728 | * Wait for I/O associated with indirect blocks to complete, | |
1729 | * since there is no way to quickly wait for them below. | |
1730 | */ | |
1731 | if ((bp->b_vp == vp) || (wait == 0)) | |
1732 | (void) buf_bawrite(bp); | |
1733 | else | |
1734 | (void) VNOP_BWRITE(bp); | |
1735 | writes_issued++; | |
1736 | ||
1737 | lck_mtx_lock(buf_mtxp); | |
1738 | } | |
1739 | buf_itercomplete(vp, &local_iterblkhd, VBI_DIRTY); | |
1740 | } | |
1741 | lck_mtx_unlock(buf_mtxp); | |
1742 | ||
1743 | if (wait) { | |
1744 | (void)vnode_waitforwrites(vp, 0, 0, 0, msg); | |
1745 | ||
1746 | if (vp->v_dirtyblkhd.lh_first && busy) { | |
1747 | /* | |
1748 | * we had one or more BUSY buffers on | |
1749 | * the dirtyblock list... most likely | |
1750 | * these are due to delayed writes that | |
1751 | * were moved to the bclean queue but | |
1752 | * have not yet been 'written'. | |
1753 | * if we issued some writes on the | |
1754 | * previous pass, we try again immediately | |
1755 | * if we didn't, we'll sleep for some time | |
1756 | * to allow the state to change... | |
1757 | */ | |
1758 | if (writes_issued == 0) { | |
1759 | (void)tsleep((caddr_t)&vp->v_numoutput, | |
1760 | PRIBIO + 1, "vnode_flushdirtyblks", hz/20); | |
1761 | } | |
1762 | writes_issued = 0; | |
1763 | busy = 0; | |
1764 | ||
1765 | goto loop; | |
1766 | } | |
1767 | } | |
316670eb A |
1768 | |
1769 | return any_locked; | |
91447636 A |
1770 | } |
1771 | ||
1772 | ||
1773 | /* | |
1774 | * called with buf_mtxp held... | |
1775 | * this lock protects the queue manipulation | |
1776 | */ | |
1777 | static int | |
1778 | buf_iterprepare(vnode_t vp, struct buflists *iterheadp, int flags) | |
1779 | { | |
1780 | struct buflists * listheadp; | |
1781 | ||
1782 | if (flags & VBI_DIRTY) | |
1783 | listheadp = &vp->v_dirtyblkhd; | |
1784 | else | |
1785 | listheadp = &vp->v_cleanblkhd; | |
1786 | ||
1787 | while (vp->v_iterblkflags & VBI_ITER) { | |
1788 | vp->v_iterblkflags |= VBI_ITERWANT; | |
2d21ac55 | 1789 | msleep(&vp->v_iterblkflags, buf_mtxp, 0, "buf_iterprepare", NULL); |
91447636 A |
1790 | } |
1791 | if (LIST_EMPTY(listheadp)) { | |
1792 | LIST_INIT(iterheadp); | |
1793 | return(EINVAL); | |
1794 | } | |
1795 | vp->v_iterblkflags |= VBI_ITER; | |
1796 | ||
1797 | iterheadp->lh_first = listheadp->lh_first; | |
1798 | listheadp->lh_first->b_vnbufs.le_prev = &iterheadp->lh_first; | |
1799 | LIST_INIT(listheadp); | |
1800 | ||
1801 | return(0); | |
1802 | } | |
1803 | ||
1804 | /* | |
1805 | * called with buf_mtxp held... | |
1806 | * this lock protects the queue manipulation | |
1807 | */ | |
1808 | static void | |
1809 | buf_itercomplete(vnode_t vp, struct buflists *iterheadp, int flags) | |
1810 | { | |
1811 | struct buflists * listheadp; | |
1812 | buf_t bp; | |
1813 | ||
1814 | if (flags & VBI_DIRTY) | |
1815 | listheadp = &vp->v_dirtyblkhd; | |
1816 | else | |
1817 | listheadp = &vp->v_cleanblkhd; | |
1818 | ||
1819 | while (!LIST_EMPTY(iterheadp)) { | |
1820 | bp = LIST_FIRST(iterheadp); | |
1821 | LIST_REMOVE(bp, b_vnbufs); | |
1822 | LIST_INSERT_HEAD(listheadp, bp, b_vnbufs); | |
1823 | } | |
1824 | vp->v_iterblkflags &= ~VBI_ITER; | |
1825 | ||
1826 | if (vp->v_iterblkflags & VBI_ITERWANT) { | |
1827 | vp->v_iterblkflags &= ~VBI_ITERWANT; | |
1828 | wakeup(&vp->v_iterblkflags); | |
1829 | } | |
1830 | } | |
1831 | ||
1832 | ||
1833 | static void | |
1834 | bremfree_locked(buf_t bp) | |
1835 | { | |
1836 | struct bqueues *dp = NULL; | |
2d21ac55 | 1837 | int whichq; |
6d2010ae A |
1838 | |
1839 | whichq = bp->b_whichq; | |
1840 | ||
1841 | if (whichq == -1) { | |
1842 | if (bp->b_shadow_ref == 0) | |
1843 | panic("bremfree_locked: %p not on freelist", bp); | |
1844 | /* | |
1845 | * there are clones pointing to 'bp'... | |
1846 | * therefore, it was not put on a freelist | |
1847 | * when buf_brelse was last called on 'bp' | |
1848 | */ | |
1849 | return; | |
1850 | } | |
91447636 A |
1851 | /* |
1852 | * We only calculate the head of the freelist when removing | |
1853 | * the last element of the list as that is the only time that | |
1854 | * it is needed (e.g. to reset the tail pointer). | |
1855 | * | |
1856 | * NB: This makes an assumption about how tailq's are implemented. | |
1857 | */ | |
1858 | if (bp->b_freelist.tqe_next == NULL) { | |
2d21ac55 A |
1859 | dp = &bufqueues[whichq]; |
1860 | ||
1861 | if (dp->tqh_last != &bp->b_freelist.tqe_next) | |
91447636 A |
1862 | panic("bremfree: lost tail"); |
1863 | } | |
1864 | TAILQ_REMOVE(dp, bp, b_freelist); | |
2d21ac55 | 1865 | |
2d21ac55 A |
1866 | if (whichq == BQ_LAUNDRY) |
1867 | blaundrycnt--; | |
1868 | ||
91447636 A |
1869 | bp->b_whichq = -1; |
1870 | bp->b_timestamp = 0; | |
6d2010ae | 1871 | bp->b_shadow = 0; |
91447636 A |
1872 | } |
1873 | ||
1874 | /* | |
1875 | * Associate a buffer with a vnode. | |
2d21ac55 | 1876 | * buf_mtxp must be locked on entry |
91447636 A |
1877 | */ |
1878 | static void | |
2d21ac55 | 1879 | bgetvp_locked(vnode_t vp, buf_t bp) |
91447636 A |
1880 | { |
1881 | ||
1882 | if (bp->b_vp != vp) | |
2d21ac55 | 1883 | panic("bgetvp_locked: not free"); |
91447636 A |
1884 | |
1885 | if (vp->v_type == VBLK || vp->v_type == VCHR) | |
1886 | bp->b_dev = vp->v_rdev; | |
1887 | else | |
1888 | bp->b_dev = NODEV; | |
1889 | /* | |
1890 | * Insert onto list for new vnode. | |
1891 | */ | |
91447636 | 1892 | bufinsvn(bp, &vp->v_cleanblkhd); |
91447636 A |
1893 | } |
1894 | ||
1895 | /* | |
1896 | * Disassociate a buffer from a vnode. | |
2d21ac55 | 1897 | * buf_mtxp must be locked on entry |
91447636 A |
1898 | */ |
1899 | static void | |
2d21ac55 | 1900 | brelvp_locked(buf_t bp) |
91447636 | 1901 | { |
91447636 A |
1902 | /* |
1903 | * Delete from old vnode list, if on one. | |
1904 | */ | |
91447636 A |
1905 | if (bp->b_vnbufs.le_next != NOLIST) |
1906 | bufremvn(bp); | |
91447636 A |
1907 | |
1908 | bp->b_vp = (vnode_t)NULL; | |
1909 | } | |
1910 | ||
1911 | /* | |
1912 | * Reassign a buffer from one vnode to another. | |
1913 | * Used to assign file specific control information | |
1914 | * (indirect blocks) to the vnode to which they belong. | |
1915 | */ | |
1916 | static void | |
1917 | buf_reassign(buf_t bp, vnode_t newvp) | |
1918 | { | |
6d2010ae | 1919 | struct buflists *listheadp; |
1c79356b | 1920 | |
91447636 A |
1921 | if (newvp == NULL) { |
1922 | printf("buf_reassign: NULL"); | |
1923 | return; | |
1924 | } | |
2d21ac55 | 1925 | lck_mtx_lock_spin(buf_mtxp); |
91447636 A |
1926 | |
1927 | /* | |
1928 | * Delete from old vnode list, if on one. | |
1929 | */ | |
1930 | if (bp->b_vnbufs.le_next != NOLIST) | |
1931 | bufremvn(bp); | |
1932 | /* | |
1933 | * If dirty, put on list of dirty buffers; | |
1934 | * otherwise insert onto list of clean buffers. | |
1935 | */ | |
1936 | if (ISSET(bp->b_flags, B_DELWRI)) | |
1937 | listheadp = &newvp->v_dirtyblkhd; | |
1938 | else | |
1939 | listheadp = &newvp->v_cleanblkhd; | |
1940 | bufinsvn(bp, listheadp); | |
1941 | ||
1942 | lck_mtx_unlock(buf_mtxp); | |
1c79356b A |
1943 | } |
1944 | ||
91447636 A |
1945 | static __inline__ void |
1946 | bufhdrinit(buf_t bp) | |
55e303ae | 1947 | { |
91447636 A |
1948 | bzero((char *)bp, sizeof *bp); |
1949 | bp->b_dev = NODEV; | |
1950 | bp->b_rcred = NOCRED; | |
1951 | bp->b_wcred = NOCRED; | |
1952 | bp->b_vnbufs.le_next = NOLIST; | |
1953 | bp->b_flags = B_INVAL; | |
1954 | ||
1955 | return; | |
55e303ae A |
1956 | } |
1957 | ||
1958 | /* | |
91447636 | 1959 | * Initialize buffers and hash links for buffers. |
55e303ae | 1960 | */ |
91447636 | 1961 | __private_extern__ void |
2d21ac55 | 1962 | bufinit(void) |
55e303ae | 1963 | { |
91447636 A |
1964 | buf_t bp; |
1965 | struct bqueues *dp; | |
1966 | int i; | |
91447636 | 1967 | |
2d21ac55 | 1968 | nbuf_headers = 0; |
91447636 A |
1969 | /* Initialize the buffer queues ('freelists') and the hash table */ |
1970 | for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) | |
1971 | TAILQ_INIT(dp); | |
0c530ab8 | 1972 | bufhashtbl = hashinit(nbuf_hashelements, M_CACHE, &bufhash); |
91447636 | 1973 | |
b0d623f7 A |
1974 | buf_busycount = 0; |
1975 | ||
91447636 | 1976 | /* Initialize the buffer headers */ |
0c530ab8 | 1977 | for (i = 0; i < max_nbuf_headers; i++) { |
2d21ac55 A |
1978 | nbuf_headers++; |
1979 | bp = &buf_headers[i]; | |
91447636 A |
1980 | bufhdrinit(bp); |
1981 | ||
91447636 | 1982 | BLISTNONE(bp); |
2d21ac55 A |
1983 | dp = &bufqueues[BQ_EMPTY]; |
1984 | bp->b_whichq = BQ_EMPTY; | |
1985 | bp->b_timestamp = buf_timestamp(); | |
1986 | binsheadfree(bp, dp, BQ_EMPTY); | |
91447636 A |
1987 | binshash(bp, &invalhash); |
1988 | } | |
2d21ac55 | 1989 | boot_nbuf_headers = nbuf_headers; |
6d2010ae A |
1990 | |
1991 | TAILQ_INIT(&iobufqueue); | |
1992 | TAILQ_INIT(&delaybufqueue); | |
1993 | ||
2d21ac55 A |
1994 | for (; i < nbuf_headers + niobuf_headers; i++) { |
1995 | bp = &buf_headers[i]; | |
91447636 | 1996 | bufhdrinit(bp); |
2d21ac55 | 1997 | bp->b_whichq = -1; |
91447636 A |
1998 | binsheadfree(bp, &iobufqueue, -1); |
1999 | } | |
2000 | ||
2d21ac55 | 2001 | /* |
91447636 A |
2002 | * allocate lock group attribute and group |
2003 | */ | |
2d21ac55 | 2004 | buf_mtx_grp_attr = lck_grp_attr_alloc_init(); |
91447636 A |
2005 | buf_mtx_grp = lck_grp_alloc_init("buffer cache", buf_mtx_grp_attr); |
2006 | ||
2007 | /* | |
2008 | * allocate the lock attribute | |
2009 | */ | |
2010 | buf_mtx_attr = lck_attr_alloc_init(); | |
91447636 A |
2011 | |
2012 | /* | |
2013 | * allocate and initialize mutex's for the buffer and iobuffer pools | |
2014 | */ | |
2015 | buf_mtxp = lck_mtx_alloc_init(buf_mtx_grp, buf_mtx_attr); | |
2016 | iobuffer_mtxp = lck_mtx_alloc_init(buf_mtx_grp, buf_mtx_attr); | |
813fb2f6 | 2017 | buf_gc_callout = lck_mtx_alloc_init(buf_mtx_grp, buf_mtx_attr); |
91447636 A |
2018 | |
2019 | if (iobuffer_mtxp == NULL) | |
2020 | panic("couldn't create iobuffer mutex"); | |
2021 | ||
2022 | if (buf_mtxp == NULL) | |
2023 | panic("couldn't create buf mutex"); | |
2024 | ||
813fb2f6 A |
2025 | if (buf_gc_callout == NULL) |
2026 | panic("couldn't create buf_gc_callout mutex"); | |
2027 | ||
91447636 A |
2028 | /* |
2029 | * allocate and initialize cluster specific global locks... | |
2030 | */ | |
2031 | cluster_init(); | |
2032 | ||
2033 | printf("using %d buffer headers and %d cluster IO buffer headers\n", | |
2d21ac55 | 2034 | nbuf_headers, niobuf_headers); |
91447636 A |
2035 | |
2036 | /* Set up zones used by the buffer cache */ | |
2037 | bufzoneinit(); | |
2038 | ||
2039 | /* start the bcleanbuf() thread */ | |
2040 | bcleanbuf_thread_init(); | |
2041 | ||
b0d623f7 A |
2042 | /* Register a callout for relieving vm pressure */ |
2043 | if (vm_set_buffer_cleanup_callout(buffer_cache_gc) != KERN_SUCCESS) { | |
2044 | panic("Couldn't register buffer cache callout for vm pressure!\n"); | |
2045 | } | |
2046 | ||
91447636 A |
2047 | } |
2048 | ||
2d21ac55 A |
2049 | /* |
2050 | * Zones for the meta data buffers | |
2051 | */ | |
2052 | ||
2053 | #define MINMETA 512 | |
813fb2f6 | 2054 | #define MAXMETA 16384 |
2d21ac55 A |
2055 | |
2056 | struct meta_zone_entry { | |
2057 | zone_t mz_zone; | |
2058 | vm_size_t mz_size; | |
2059 | vm_size_t mz_max; | |
2060 | const char *mz_name; | |
2061 | }; | |
2062 | ||
2063 | struct meta_zone_entry meta_zones[] = { | |
2064 | {NULL, (MINMETA * 1), 128 * (MINMETA * 1), "buf.512" }, | |
2065 | {NULL, (MINMETA * 2), 64 * (MINMETA * 2), "buf.1024" }, | |
2066 | {NULL, (MINMETA * 4), 16 * (MINMETA * 4), "buf.2048" }, | |
2067 | {NULL, (MINMETA * 8), 512 * (MINMETA * 8), "buf.4096" }, | |
2068 | {NULL, (MINMETA * 16), 512 * (MINMETA * 16), "buf.8192" }, | |
813fb2f6 | 2069 | {NULL, (MINMETA * 32), 512 * (MINMETA * 32), "buf.16384" }, |
2d21ac55 A |
2070 | {NULL, 0, 0, "" } /* End */ |
2071 | }; | |
2072 | ||
2073 | /* | |
2074 | * Initialize the meta data zones | |
2075 | */ | |
2076 | static void | |
2077 | bufzoneinit(void) | |
2078 | { | |
2079 | int i; | |
2080 | ||
2081 | for (i = 0; meta_zones[i].mz_size != 0; i++) { | |
2082 | meta_zones[i].mz_zone = | |
2083 | zinit(meta_zones[i].mz_size, | |
2084 | meta_zones[i].mz_max, | |
2085 | PAGE_SIZE, | |
2086 | meta_zones[i].mz_name); | |
6d2010ae | 2087 | zone_change(meta_zones[i].mz_zone, Z_CALLERACCT, FALSE); |
2d21ac55 A |
2088 | } |
2089 | buf_hdr_zone = zinit(sizeof(struct buf), 32, PAGE_SIZE, "buf headers"); | |
6d2010ae | 2090 | zone_change(buf_hdr_zone, Z_CALLERACCT, FALSE); |
2d21ac55 A |
2091 | } |
2092 | ||
2093 | static __inline__ zone_t | |
2094 | getbufzone(size_t size) | |
2095 | { | |
2096 | int i; | |
2097 | ||
2098 | if ((size % 512) || (size < MINMETA) || (size > MAXMETA)) | |
2099 | panic("getbufzone: incorect size = %lu", size); | |
2100 | ||
2101 | for (i = 0; meta_zones[i].mz_size != 0; i++) { | |
2102 | if (meta_zones[i].mz_size >= size) | |
2103 | break; | |
2104 | } | |
2105 | ||
2106 | return (meta_zones[i].mz_zone); | |
2107 | } | |
2108 | ||
2109 | ||
2110 | ||
91447636 | 2111 | static struct buf * |
b0d623f7 | 2112 | bio_doread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, int async, int queuetype) |
91447636 A |
2113 | { |
2114 | buf_t bp; | |
2115 | ||
2116 | bp = buf_getblk(vp, blkno, size, 0, 0, queuetype); | |
2117 | ||
2118 | /* | |
2119 | * If buffer does not have data valid, start a read. | |
2120 | * Note that if buffer is B_INVAL, buf_getblk() won't return it. | |
2121 | * Therefore, it's valid if it's I/O has completed or been delayed. | |
2122 | */ | |
2123 | if (!ISSET(bp->b_flags, (B_DONE | B_DELWRI))) { | |
2124 | struct proc *p; | |
2125 | ||
2126 | p = current_proc(); | |
2127 | ||
2128 | /* Start I/O for the buffer (keeping credentials). */ | |
2129 | SET(bp->b_flags, B_READ | async); | |
0c530ab8 | 2130 | if (IS_VALID_CRED(cred) && !IS_VALID_CRED(bp->b_rcred)) { |
91447636 A |
2131 | kauth_cred_ref(cred); |
2132 | bp->b_rcred = cred; | |
2133 | } | |
2134 | ||
2135 | VNOP_STRATEGY(bp); | |
2136 | ||
2137 | trace(TR_BREADMISS, pack(vp, size), blkno); | |
2138 | ||
2139 | /* Pay for the read. */ | |
39236c6e | 2140 | if (p && p->p_stats) { |
b0d623f7 | 2141 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_inblock); /* XXX */ |
39236c6e | 2142 | } |
91447636 A |
2143 | |
2144 | if (async) { | |
2145 | /* | |
2146 | * since we asked for an ASYNC I/O | |
2147 | * the biodone will do the brelse | |
2148 | * we don't want to pass back a bp | |
2149 | * that we don't 'own' | |
2150 | */ | |
2151 | bp = NULL; | |
2152 | } | |
2153 | } else if (async) { | |
2154 | buf_brelse(bp); | |
2155 | bp = NULL; | |
2156 | } | |
2157 | ||
2158 | trace(TR_BREADHIT, pack(vp, size), blkno); | |
2159 | ||
2160 | return (bp); | |
55e303ae A |
2161 | } |
2162 | ||
2163 | /* | |
91447636 | 2164 | * Perform the reads for buf_breadn() and buf_meta_breadn(). |
55e303ae A |
2165 | * Trivial modification to the breada algorithm presented in Bach (p.55). |
2166 | */ | |
91447636 A |
2167 | static errno_t |
2168 | do_breadn_for_type(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, | |
b0d623f7 | 2169 | int nrablks, kauth_cred_t cred, buf_t *bpp, int queuetype) |
1c79356b | 2170 | { |
91447636 A |
2171 | buf_t bp; |
2172 | int i; | |
1c79356b | 2173 | |
55e303ae | 2174 | bp = *bpp = bio_doread(vp, blkno, size, cred, 0, queuetype); |
1c79356b A |
2175 | |
2176 | /* | |
2177 | * For each of the read-ahead blocks, start a read, if necessary. | |
2178 | */ | |
2179 | for (i = 0; i < nrablks; i++) { | |
2180 | /* If it's in the cache, just go on to next one. */ | |
2181 | if (incore(vp, rablks[i])) | |
2182 | continue; | |
2183 | ||
2184 | /* Get a buffer for the read-ahead block */ | |
55e303ae | 2185 | (void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC, queuetype); |
1c79356b A |
2186 | } |
2187 | ||
2188 | /* Otherwise, we had to start a read for it; wait until it's valid. */ | |
91447636 | 2189 | return (buf_biowait(bp)); |
1c79356b A |
2190 | } |
2191 | ||
91447636 | 2192 | |
1c79356b | 2193 | /* |
91447636 A |
2194 | * Read a disk block. |
2195 | * This algorithm described in Bach (p.54). | |
1c79356b | 2196 | */ |
91447636 | 2197 | errno_t |
b0d623f7 | 2198 | buf_bread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, buf_t *bpp) |
91447636 A |
2199 | { |
2200 | buf_t bp; | |
2201 | ||
2202 | /* Get buffer for block. */ | |
2203 | bp = *bpp = bio_doread(vp, blkno, size, cred, 0, BLK_READ); | |
2204 | ||
2205 | /* Wait for the read to complete, and return result. */ | |
2206 | return (buf_biowait(bp)); | |
2207 | } | |
2208 | ||
2209 | /* | |
2210 | * Read a disk block. [bread() for meta-data] | |
2211 | * This algorithm described in Bach (p.54). | |
2212 | */ | |
2213 | errno_t | |
b0d623f7 | 2214 | buf_meta_bread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, buf_t *bpp) |
91447636 A |
2215 | { |
2216 | buf_t bp; | |
2217 | ||
2218 | /* Get buffer for block. */ | |
2219 | bp = *bpp = bio_doread(vp, blkno, size, cred, 0, BLK_META); | |
2220 | ||
2221 | /* Wait for the read to complete, and return result. */ | |
2222 | return (buf_biowait(bp)); | |
2223 | } | |
2224 | ||
2225 | /* | |
2226 | * Read-ahead multiple disk blocks. The first is sync, the rest async. | |
2227 | */ | |
2228 | errno_t | |
b0d623f7 | 2229 | buf_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, kauth_cred_t cred, buf_t *bpp) |
1c79356b | 2230 | { |
91447636 A |
2231 | return (do_breadn_for_type(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp, BLK_READ)); |
2232 | } | |
1c79356b | 2233 | |
91447636 A |
2234 | /* |
2235 | * Read-ahead multiple disk blocks. The first is sync, the rest async. | |
2236 | * [buf_breadn() for meta-data] | |
2237 | */ | |
2238 | errno_t | |
b0d623f7 | 2239 | buf_meta_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, kauth_cred_t cred, buf_t *bpp) |
91447636 A |
2240 | { |
2241 | return (do_breadn_for_type(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp, BLK_META)); | |
1c79356b A |
2242 | } |
2243 | ||
2244 | /* | |
2245 | * Block write. Described in Bach (p.56) | |
2246 | */ | |
91447636 A |
2247 | errno_t |
2248 | buf_bwrite(buf_t bp) | |
1c79356b | 2249 | { |
91447636 A |
2250 | int sync, wasdelayed; |
2251 | errno_t rv; | |
2252 | proc_t p = current_proc(); | |
2253 | vnode_t vp = bp->b_vp; | |
1c79356b | 2254 | |
91447636 | 2255 | if (bp->b_datap == 0) { |
55e303ae A |
2256 | if (brecover_data(bp) == 0) |
2257 | return (0); | |
2258 | } | |
1c79356b A |
2259 | /* Remember buffer type, to switch on it later. */ |
2260 | sync = !ISSET(bp->b_flags, B_ASYNC); | |
2261 | wasdelayed = ISSET(bp->b_flags, B_DELWRI); | |
2262 | CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI)); | |
91447636 A |
2263 | |
2264 | if (wasdelayed) | |
b0d623f7 | 2265 | OSAddAtomicLong(-1, &nbdwrite); |
1c79356b A |
2266 | |
2267 | if (!sync) { | |
2268 | /* | |
2269 | * If not synchronous, pay for the I/O operation and make | |
2270 | * sure the buf is on the correct vnode queue. We have | |
2271 | * to do this now, because if we don't, the vnode may not | |
2272 | * be properly notified that its I/O has completed. | |
2273 | */ | |
2274 | if (wasdelayed) | |
91447636 | 2275 | buf_reassign(bp, vp); |
39236c6e A |
2276 | else |
2277 | if (p && p->p_stats) { | |
2278 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); /* XXX */ | |
39236c6e | 2279 | } |
1c79356b | 2280 | } |
d52fe63f | 2281 | trace(TR_BUFWRITE, pack(vp, bp->b_bcount), bp->b_lblkno); |
1c79356b A |
2282 | |
2283 | /* Initiate disk write. Make sure the appropriate party is charged. */ | |
91447636 A |
2284 | |
2285 | OSAddAtomic(1, &vp->v_numoutput); | |
1c79356b | 2286 | |
91447636 | 2287 | VNOP_STRATEGY(bp); |
1c79356b A |
2288 | |
2289 | if (sync) { | |
2290 | /* | |
2291 | * If I/O was synchronous, wait for it to complete. | |
2292 | */ | |
91447636 | 2293 | rv = buf_biowait(bp); |
1c79356b A |
2294 | |
2295 | /* | |
2296 | * Pay for the I/O operation, if it's not been paid for, and | |
2297 | * make sure it's on the correct vnode queue. (async operatings | |
2298 | * were payed for above.) | |
2299 | */ | |
2300 | if (wasdelayed) | |
91447636 | 2301 | buf_reassign(bp, vp); |
1c79356b | 2302 | else |
39236c6e A |
2303 | if (p && p->p_stats) { |
2304 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); /* XXX */ | |
39236c6e | 2305 | } |
1c79356b A |
2306 | |
2307 | /* Release the buffer. */ | |
39037602 | 2308 | buf_brelse(bp); |
1c79356b A |
2309 | |
2310 | return (rv); | |
2311 | } else { | |
2312 | return (0); | |
2313 | } | |
2314 | } | |
2315 | ||
2316 | int | |
2d21ac55 | 2317 | vn_bwrite(struct vnop_bwrite_args *ap) |
1c79356b | 2318 | { |
91447636 | 2319 | return (buf_bwrite(ap->a_bp)); |
1c79356b A |
2320 | } |
2321 | ||
2322 | /* | |
2323 | * Delayed write. | |
2324 | * | |
2325 | * The buffer is marked dirty, but is not queued for I/O. | |
2326 | * This routine should be used when the buffer is expected | |
2327 | * to be modified again soon, typically a small write that | |
2328 | * partially fills a buffer. | |
2329 | * | |
2330 | * NB: magnetic tapes cannot be delayed; they must be | |
2331 | * written in the order that the writes are requested. | |
2332 | * | |
2333 | * Described in Leffler, et al. (pp. 208-213). | |
d52fe63f | 2334 | * |
b0d623f7 | 2335 | * Note: With the ability to allocate additional buffer |
d52fe63f | 2336 | * headers, we can get in to the situation where "too" many |
91447636 A |
2337 | * buf_bdwrite()s can create situation where the kernel can create |
2338 | * buffers faster than the disks can service. Doing a buf_bawrite() in | |
6d2010ae | 2339 | * cases where we have "too many" outstanding buf_bdwrite()s avoids that. |
1c79356b | 2340 | */ |
39037602 | 2341 | int |
91447636 | 2342 | bdwrite_internal(buf_t bp, int return_error) |
1c79356b | 2343 | { |
91447636 A |
2344 | proc_t p = current_proc(); |
2345 | vnode_t vp = bp->b_vp; | |
1c79356b A |
2346 | |
2347 | /* | |
2348 | * If the block hasn't been seen before: | |
2349 | * (1) Mark it as having been seen, | |
2350 | * (2) Charge for the write. | |
2351 | * (3) Make sure it's on its vnode's correct block list, | |
2352 | */ | |
2353 | if (!ISSET(bp->b_flags, B_DELWRI)) { | |
2354 | SET(bp->b_flags, B_DELWRI); | |
39236c6e | 2355 | if (p && p->p_stats) { |
b0d623f7 | 2356 | OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); /* XXX */ |
39236c6e | 2357 | } |
b0d623f7 | 2358 | OSAddAtomicLong(1, &nbdwrite); |
91447636 | 2359 | buf_reassign(bp, vp); |
1c79356b A |
2360 | } |
2361 | ||
d52fe63f | 2362 | /* |
91447636 A |
2363 | * if we're not LOCKED, but the total number of delayed writes |
2364 | * has climbed above 75% of the total buffers in the system | |
2365 | * return an error if the caller has indicated that it can | |
2366 | * handle one in this case, otherwise schedule the I/O now | |
2367 | * this is done to prevent us from allocating tons of extra | |
2368 | * buffers when dealing with virtual disks (i.e. DiskImages), | |
2369 | * because additional buffers are dynamically allocated to prevent | |
2370 | * deadlocks from occurring | |
2371 | * | |
2372 | * however, can't do a buf_bawrite() if the LOCKED bit is set because the | |
2373 | * buffer is part of a transaction and can't go to disk until | |
2374 | * the LOCKED bit is cleared. | |
d52fe63f | 2375 | */ |
2d21ac55 | 2376 | if (!ISSET(bp->b_flags, B_LOCKED) && nbdwrite > ((nbuf_headers/4)*3)) { |
9bccf70c A |
2377 | if (return_error) |
2378 | return (EAGAIN); | |
91447636 A |
2379 | /* |
2380 | * If the vnode has "too many" write operations in progress | |
2381 | * wait for them to finish the IO | |
2382 | */ | |
2d21ac55 | 2383 | (void)vnode_waitforwrites(vp, VNODE_ASYNC_THROTTLE, 0, 0, "buf_bdwrite"); |
91447636 A |
2384 | |
2385 | return (buf_bawrite(bp)); | |
d52fe63f A |
2386 | } |
2387 | ||
1c79356b A |
2388 | /* Otherwise, the "write" is done, so mark and release the buffer. */ |
2389 | SET(bp->b_flags, B_DONE); | |
91447636 | 2390 | buf_brelse(bp); |
9bccf70c | 2391 | return (0); |
1c79356b A |
2392 | } |
2393 | ||
91447636 A |
2394 | errno_t |
2395 | buf_bdwrite(buf_t bp) | |
9bccf70c | 2396 | { |
91447636 | 2397 | return (bdwrite_internal(bp, 0)); |
9bccf70c A |
2398 | } |
2399 | ||
2400 | ||
1c79356b | 2401 | /* |
91447636 | 2402 | * Asynchronous block write; just an asynchronous buf_bwrite(). |
d52fe63f A |
2403 | * |
2404 | * Note: With the abilitty to allocate additional buffer | |
2405 | * headers, we can get in to the situation where "too" many | |
91447636 | 2406 | * buf_bawrite()s can create situation where the kernel can create |
d52fe63f A |
2407 | * buffers faster than the disks can service. |
2408 | * We limit the number of "in flight" writes a vnode can have to | |
2409 | * avoid this. | |
1c79356b | 2410 | */ |
9bccf70c | 2411 | static int |
91447636 | 2412 | bawrite_internal(buf_t bp, int throttle) |
1c79356b | 2413 | { |
91447636 | 2414 | vnode_t vp = bp->b_vp; |
d52fe63f A |
2415 | |
2416 | if (vp) { | |
91447636 A |
2417 | if (throttle) |
2418 | /* | |
2419 | * If the vnode has "too many" write operations in progress | |
2420 | * wait for them to finish the IO | |
2421 | */ | |
2422 | (void)vnode_waitforwrites(vp, VNODE_ASYNC_THROTTLE, 0, 0, (const char *)"buf_bawrite"); | |
2423 | else if (vp->v_numoutput >= VNODE_ASYNC_THROTTLE) | |
2424 | /* | |
2425 | * return to the caller and | |
2426 | * let him decide what to do | |
2427 | */ | |
2428 | return (EWOULDBLOCK); | |
d52fe63f | 2429 | } |
1c79356b | 2430 | SET(bp->b_flags, B_ASYNC); |
9bccf70c | 2431 | |
91447636 | 2432 | return (VNOP_BWRITE(bp)); |
9bccf70c A |
2433 | } |
2434 | ||
91447636 A |
2435 | errno_t |
2436 | buf_bawrite(buf_t bp) | |
9bccf70c | 2437 | { |
91447636 | 2438 | return (bawrite_internal(bp, 1)); |
1c79356b A |
2439 | } |
2440 | ||
91447636 | 2441 | |
6d2010ae A |
2442 | |
2443 | static void | |
2444 | buf_free_meta_store(buf_t bp) | |
2445 | { | |
2446 | if (bp->b_bufsize) { | |
2447 | if (ISSET(bp->b_flags, B_ZALLOC)) { | |
2448 | zone_t z; | |
2449 | ||
2450 | z = getbufzone(bp->b_bufsize); | |
2451 | zfree(z, (void *)bp->b_datap); | |
2452 | } else | |
2453 | kmem_free(kernel_map, bp->b_datap, bp->b_bufsize); | |
2454 | ||
2455 | bp->b_datap = (uintptr_t)NULL; | |
2456 | bp->b_bufsize = 0; | |
2457 | } | |
2458 | } | |
2459 | ||
2460 | ||
2461 | static buf_t | |
2462 | buf_brelse_shadow(buf_t bp) | |
2463 | { | |
2464 | buf_t bp_head; | |
2465 | buf_t bp_temp; | |
2466 | buf_t bp_return = NULL; | |
2467 | #ifdef BUF_MAKE_PRIVATE | |
2468 | buf_t bp_data; | |
2469 | int data_ref = 0; | |
2470 | #endif | |
316670eb A |
2471 | int need_wakeup = 0; |
2472 | ||
6d2010ae A |
2473 | lck_mtx_lock_spin(buf_mtxp); |
2474 | ||
3e170ce0 | 2475 | __IGNORE_WCASTALIGN(bp_head = (buf_t)bp->b_orig); |
6d2010ae A |
2476 | |
2477 | if (bp_head->b_whichq != -1) | |
2478 | panic("buf_brelse_shadow: bp_head on freelist %d\n", bp_head->b_whichq); | |
2479 | ||
2480 | #ifdef BUF_MAKE_PRIVATE | |
2481 | if (bp_data = bp->b_data_store) { | |
2482 | bp_data->b_data_ref--; | |
2483 | /* | |
2484 | * snapshot the ref count so that we can check it | |
2485 | * outside of the lock... we only want the guy going | |
2486 | * from 1 -> 0 to try and release the storage | |
2487 | */ | |
2488 | data_ref = bp_data->b_data_ref; | |
2489 | } | |
2490 | #endif | |
2491 | KERNEL_DEBUG(0xbbbbc008 | DBG_FUNC_START, bp, bp_head, bp_head->b_shadow_ref, 0, 0); | |
2492 | ||
2493 | bp_head->b_shadow_ref--; | |
2494 | ||
2495 | for (bp_temp = bp_head; bp_temp && bp != bp_temp->b_shadow; bp_temp = bp_temp->b_shadow); | |
2496 | ||
2497 | if (bp_temp == NULL) | |
2498 | panic("buf_brelse_shadow: bp not on list %p", bp_head); | |
2499 | ||
2500 | bp_temp->b_shadow = bp_temp->b_shadow->b_shadow; | |
2501 | ||
2502 | #ifdef BUF_MAKE_PRIVATE | |
2503 | /* | |
2504 | * we're about to free the current 'owner' of the data buffer and | |
2505 | * there is at least one other shadow buf_t still pointing at it | |
2506 | * so transfer it to the first shadow buf left in the chain | |
2507 | */ | |
2508 | if (bp == bp_data && data_ref) { | |
2509 | if ((bp_data = bp_head->b_shadow) == NULL) | |
2510 | panic("buf_brelse_shadow: data_ref mismatch bp(%p)", bp); | |
2511 | ||
2512 | for (bp_temp = bp_data; bp_temp; bp_temp = bp_temp->b_shadow) | |
2513 | bp_temp->b_data_store = bp_data; | |
2514 | bp_data->b_data_ref = data_ref; | |
2515 | } | |
2516 | #endif | |
2517 | if (bp_head->b_shadow_ref == 0 && bp_head->b_shadow) | |
2518 | panic("buf_relse_shadow: b_shadow != NULL && b_shadow_ref == 0 bp(%p)", bp); | |
2519 | if (bp_head->b_shadow_ref && bp_head->b_shadow == 0) | |
2520 | panic("buf_relse_shadow: b_shadow == NULL && b_shadow_ref != 0 bp(%p)", bp); | |
2521 | ||
2522 | if (bp_head->b_shadow_ref == 0) { | |
2523 | if (!ISSET(bp_head->b_lflags, BL_BUSY)) { | |
2524 | ||
2525 | CLR(bp_head->b_flags, B_AGE); | |
2526 | bp_head->b_timestamp = buf_timestamp(); | |
2527 | ||
2528 | if (ISSET(bp_head->b_flags, B_LOCKED)) { | |
2529 | bp_head->b_whichq = BQ_LOCKED; | |
2530 | binstailfree(bp_head, &bufqueues[BQ_LOCKED], BQ_LOCKED); | |
2531 | } else { | |
2532 | bp_head->b_whichq = BQ_META; | |
2533 | binstailfree(bp_head, &bufqueues[BQ_META], BQ_META); | |
2534 | } | |
2535 | } else if (ISSET(bp_head->b_lflags, BL_WAITSHADOW)) { | |
2536 | CLR(bp_head->b_lflags, BL_WAITSHADOW); | |
2537 | ||
2538 | bp_return = bp_head; | |
2539 | } | |
316670eb A |
2540 | if (ISSET(bp_head->b_lflags, BL_WANTED_REF)) { |
2541 | CLR(bp_head->b_lflags, BL_WANTED_REF); | |
2542 | need_wakeup = 1; | |
2543 | } | |
6d2010ae A |
2544 | } |
2545 | lck_mtx_unlock(buf_mtxp); | |
39236c6e A |
2546 | |
2547 | if (need_wakeup) | |
316670eb | 2548 | wakeup(bp_head); |
316670eb | 2549 | |
6d2010ae A |
2550 | #ifdef BUF_MAKE_PRIVATE |
2551 | if (bp == bp_data && data_ref == 0) | |
2552 | buf_free_meta_store(bp); | |
2553 | ||
2554 | bp->b_data_store = NULL; | |
2555 | #endif | |
2556 | KERNEL_DEBUG(0xbbbbc008 | DBG_FUNC_END, bp, 0, 0, 0, 0); | |
2557 | ||
2558 | return (bp_return); | |
2559 | } | |
2560 | ||
2561 | ||
1c79356b A |
2562 | /* |
2563 | * Release a buffer on to the free lists. | |
2564 | * Described in Bach (p. 46). | |
2565 | */ | |
2566 | void | |
91447636 | 2567 | buf_brelse(buf_t bp) |
1c79356b A |
2568 | { |
2569 | struct bqueues *bufq; | |
91447636 A |
2570 | long whichq; |
2571 | upl_t upl; | |
2572 | int need_wakeup = 0; | |
2573 | int need_bp_wakeup = 0; | |
2574 | ||
2575 | ||
2576 | if (bp->b_whichq != -1 || !(bp->b_lflags & BL_BUSY)) | |
2d21ac55 | 2577 | panic("buf_brelse: bad buffer = %p\n", bp); |
91447636 A |
2578 | |
2579 | #ifdef JOE_DEBUG | |
b0d623f7 | 2580 | (void) OSBacktrace(&bp->b_stackbrelse[0], 6); |
91447636 A |
2581 | |
2582 | bp->b_lastbrelse = current_thread(); | |
2583 | bp->b_tag = 0; | |
2584 | #endif | |
2585 | if (bp->b_lflags & BL_IOBUF) { | |
6d2010ae A |
2586 | buf_t shadow_master_bp = NULL; |
2587 | ||
2588 | if (ISSET(bp->b_lflags, BL_SHADOW)) | |
2589 | shadow_master_bp = buf_brelse_shadow(bp); | |
2590 | else if (ISSET(bp->b_lflags, BL_IOBUF_ALLOC)) | |
2591 | buf_free_meta_store(bp); | |
91447636 | 2592 | free_io_buf(bp); |
6d2010ae A |
2593 | |
2594 | if (shadow_master_bp) { | |
2595 | bp = shadow_master_bp; | |
2596 | goto finish_shadow_master; | |
2597 | } | |
91447636 A |
2598 | return; |
2599 | } | |
1c79356b A |
2600 | |
2601 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 388)) | DBG_FUNC_START, | |
b0d623f7 | 2602 | bp->b_lblkno * PAGE_SIZE, bp, bp->b_datap, |
fa4905b1 | 2603 | bp->b_flags, 0); |
1c79356b A |
2604 | |
2605 | trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno); | |
2606 | ||
91447636 A |
2607 | /* |
2608 | * if we're invalidating a buffer that has the B_FILTER bit | |
2609 | * set then call the b_iodone function so it gets cleaned | |
2610 | * up properly. | |
2611 | * | |
2612 | * the HFS journal code depends on this | |
2613 | */ | |
b4c24cb9 | 2614 | if (ISSET(bp->b_flags, B_META) && ISSET(bp->b_flags, B_INVAL)) { |
91447636 A |
2615 | if (ISSET(bp->b_flags, B_FILTER)) { /* if necessary, call out */ |
2616 | void (*iodone_func)(struct buf *, void *) = bp->b_iodone; | |
6d2010ae | 2617 | void *arg = bp->b_transaction; |
b4c24cb9 | 2618 | |
91447636 | 2619 | CLR(bp->b_flags, B_FILTER); /* but note callout done */ |
b4c24cb9 | 2620 | bp->b_iodone = NULL; |
91447636 | 2621 | bp->b_transaction = NULL; |
b4c24cb9 A |
2622 | |
2623 | if (iodone_func == NULL) { | |
2d21ac55 | 2624 | panic("brelse: bp @ %p has NULL b_iodone!\n", bp); |
b4c24cb9 | 2625 | } |
91447636 | 2626 | (*iodone_func)(bp, arg); |
b4c24cb9 A |
2627 | } |
2628 | } | |
91447636 A |
2629 | /* |
2630 | * I/O is done. Cleanup the UPL state | |
2631 | */ | |
2632 | upl = bp->b_upl; | |
2633 | ||
2634 | if ( !ISSET(bp->b_flags, B_META) && UBCINFOEXISTS(bp->b_vp) && bp->b_bufsize) { | |
1c79356b | 2635 | kern_return_t kret; |
1c79356b A |
2636 | int upl_flags; |
2637 | ||
6d2010ae | 2638 | if (upl == NULL) { |
1c79356b | 2639 | if ( !ISSET(bp->b_flags, B_INVAL)) { |
5ba3f43e | 2640 | kret = ubc_create_upl_kernel(bp->b_vp, |
91447636 A |
2641 | ubc_blktooff(bp->b_vp, bp->b_lblkno), |
2642 | bp->b_bufsize, | |
2643 | &upl, | |
2644 | NULL, | |
5ba3f43e A |
2645 | UPL_PRECIOUS, |
2646 | VM_KERN_MEMORY_FILE); | |
91447636 | 2647 | |
1c79356b | 2648 | if (kret != KERN_SUCCESS) |
91447636 | 2649 | panic("brelse: Failed to create UPL"); |
b0d623f7 A |
2650 | #if UPL_DEBUG |
2651 | upl_ubc_alias_set(upl, (uintptr_t) bp, (uintptr_t) 5); | |
91447636 A |
2652 | #endif /* UPL_DEBUG */ |
2653 | } | |
1c79356b | 2654 | } else { |
91447636 | 2655 | if (bp->b_datap) { |
55e303ae A |
2656 | kret = ubc_upl_unmap(upl); |
2657 | ||
2658 | if (kret != KERN_SUCCESS) | |
91447636 A |
2659 | panic("ubc_upl_unmap failed"); |
2660 | bp->b_datap = (uintptr_t)NULL; | |
55e303ae | 2661 | } |
1c79356b A |
2662 | } |
2663 | if (upl) { | |
1c79356b | 2664 | if (bp->b_flags & (B_ERROR | B_INVAL)) { |
91447636 | 2665 | if (bp->b_flags & (B_READ | B_INVAL)) |
1c79356b A |
2666 | upl_flags = UPL_ABORT_DUMP_PAGES; |
2667 | else | |
2668 | upl_flags = 0; | |
91447636 | 2669 | |
0b4e3aa0 | 2670 | ubc_upl_abort(upl, upl_flags); |
1c79356b | 2671 | } else { |
91447636 A |
2672 | if (ISSET(bp->b_flags, B_DELWRI | B_WASDIRTY)) |
2673 | upl_flags = UPL_COMMIT_SET_DIRTY ; | |
2674 | else | |
2675 | upl_flags = UPL_COMMIT_CLEAR_DIRTY ; | |
2676 | ||
0b4e3aa0 | 2677 | ubc_upl_commit_range(upl, 0, bp->b_bufsize, upl_flags | |
91447636 | 2678 | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY); |
1c79356b | 2679 | } |
91447636 | 2680 | bp->b_upl = NULL; |
1c79356b A |
2681 | } |
2682 | } else { | |
91447636 | 2683 | if ( (upl) ) |
2d21ac55 | 2684 | panic("brelse: UPL set for non VREG; vp=%p", bp->b_vp); |
1c79356b A |
2685 | } |
2686 | ||
1c79356b | 2687 | /* |
91447636 | 2688 | * If it's locked, don't report an error; try again later. |
1c79356b | 2689 | */ |
1c79356b A |
2690 | if (ISSET(bp->b_flags, (B_LOCKED|B_ERROR)) == (B_LOCKED|B_ERROR)) |
2691 | CLR(bp->b_flags, B_ERROR); | |
91447636 A |
2692 | /* |
2693 | * If it's not cacheable, or an error, mark it invalid. | |
2694 | */ | |
1c79356b A |
2695 | if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR))) |
2696 | SET(bp->b_flags, B_INVAL); | |
91447636 | 2697 | |
b0d623f7 A |
2698 | if ((bp->b_bufsize <= 0) || |
2699 | ISSET(bp->b_flags, B_INVAL) || | |
2700 | (ISSET(bp->b_lflags, BL_WANTDEALLOC) && !ISSET(bp->b_flags, B_DELWRI))) { | |
6d2010ae A |
2701 | |
2702 | boolean_t delayed_buf_free_meta_store = FALSE; | |
2703 | ||
1c79356b | 2704 | /* |
2d21ac55 A |
2705 | * If it's invalid or empty, dissociate it from its vnode, |
2706 | * release its storage if B_META, and | |
2707 | * clean it up a bit and put it on the EMPTY queue | |
1c79356b | 2708 | */ |
91447636 | 2709 | if (ISSET(bp->b_flags, B_DELWRI)) |
b0d623f7 | 2710 | OSAddAtomicLong(-1, &nbdwrite); |
91447636 | 2711 | |
2d21ac55 | 2712 | if (ISSET(bp->b_flags, B_META)) { |
6d2010ae A |
2713 | if (bp->b_shadow_ref) |
2714 | delayed_buf_free_meta_store = TRUE; | |
2715 | else | |
2716 | buf_free_meta_store(bp); | |
2d21ac55 | 2717 | } |
91447636 | 2718 | /* |
2d21ac55 | 2719 | * nuke any credentials we were holding |
91447636 | 2720 | */ |
6d2010ae A |
2721 | buf_release_credentials(bp); |
2722 | ||
2723 | lck_mtx_lock_spin(buf_mtxp); | |
2724 | ||
2725 | if (bp->b_shadow_ref) { | |
2726 | SET(bp->b_lflags, BL_WAITSHADOW); | |
2727 | ||
2728 | lck_mtx_unlock(buf_mtxp); | |
2729 | ||
2730 | return; | |
2d21ac55 | 2731 | } |
6d2010ae | 2732 | if (delayed_buf_free_meta_store == TRUE) { |
91447636 | 2733 | |
6d2010ae A |
2734 | lck_mtx_unlock(buf_mtxp); |
2735 | finish_shadow_master: | |
2736 | buf_free_meta_store(bp); | |
91447636 | 2737 | |
6d2010ae A |
2738 | lck_mtx_lock_spin(buf_mtxp); |
2739 | } | |
2740 | CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE | B_FUA)); | |
2d21ac55 A |
2741 | |
2742 | if (bp->b_vp) | |
2743 | brelvp_locked(bp); | |
2744 | ||
2745 | bremhash(bp); | |
2746 | BLISTNONE(bp); | |
2747 | binshash(bp, &invalhash); | |
2748 | ||
6d2010ae A |
2749 | bp->b_whichq = BQ_EMPTY; |
2750 | binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY); | |
1c79356b | 2751 | } else { |
6d2010ae | 2752 | |
1c79356b A |
2753 | /* |
2754 | * It has valid data. Put it on the end of the appropriate | |
2755 | * queue, so that it'll stick around for as long as possible. | |
2756 | */ | |
2757 | if (ISSET(bp->b_flags, B_LOCKED)) | |
2758 | whichq = BQ_LOCKED; /* locked in core */ | |
2759 | else if (ISSET(bp->b_flags, B_META)) | |
2760 | whichq = BQ_META; /* meta-data */ | |
2761 | else if (ISSET(bp->b_flags, B_AGE)) | |
2762 | whichq = BQ_AGE; /* stale but valid data */ | |
2763 | else | |
2764 | whichq = BQ_LRU; /* valid data */ | |
1c79356b | 2765 | bufq = &bufqueues[whichq]; |
91447636 | 2766 | |
2d21ac55 | 2767 | bp->b_timestamp = buf_timestamp(); |
91447636 | 2768 | |
6d2010ae A |
2769 | lck_mtx_lock_spin(buf_mtxp); |
2770 | ||
2771 | /* | |
2772 | * the buf_brelse_shadow routine doesn't take 'ownership' | |
2773 | * of the parent buf_t... it updates state that is protected by | |
2774 | * the buf_mtxp, and checks for BL_BUSY to determine whether to | |
2775 | * put the buf_t back on a free list. b_shadow_ref is protected | |
2776 | * by the lock, and since we have not yet cleared B_BUSY, we need | |
2777 | * to check it while holding the lock to insure that one of us | |
2778 | * puts this buf_t back on a free list when it is safe to do so | |
2779 | */ | |
2780 | if (bp->b_shadow_ref == 0) { | |
2781 | CLR(bp->b_flags, (B_AGE | B_ASYNC | B_NOCACHE)); | |
2782 | bp->b_whichq = whichq; | |
2783 | binstailfree(bp, bufq, whichq); | |
2784 | } else { | |
2785 | /* | |
2786 | * there are still cloned buf_t's pointing | |
2787 | * at this guy... need to keep it off the | |
2788 | * freelists until a buf_brelse is done on | |
2789 | * the last clone | |
2790 | */ | |
2791 | CLR(bp->b_flags, (B_ASYNC | B_NOCACHE)); | |
2792 | } | |
1c79356b | 2793 | } |
91447636 A |
2794 | if (needbuffer) { |
2795 | /* | |
2796 | * needbuffer is a global | |
2797 | * we're currently using buf_mtxp to protect it | |
2798 | * delay doing the actual wakeup until after | |
2799 | * we drop buf_mtxp | |
2800 | */ | |
2801 | needbuffer = 0; | |
2802 | need_wakeup = 1; | |
2803 | } | |
2804 | if (ISSET(bp->b_lflags, BL_WANTED)) { | |
2805 | /* | |
2806 | * delay the actual wakeup until after we | |
2807 | * clear BL_BUSY and we've dropped buf_mtxp | |
2808 | */ | |
2809 | need_bp_wakeup = 1; | |
2810 | } | |
2811 | /* | |
2812 | * Unlock the buffer. | |
2813 | */ | |
2814 | CLR(bp->b_lflags, (BL_BUSY | BL_WANTED)); | |
b0d623f7 | 2815 | buf_busycount--; |
1c79356b | 2816 | |
91447636 | 2817 | lck_mtx_unlock(buf_mtxp); |
1c79356b | 2818 | |
91447636 A |
2819 | if (need_wakeup) { |
2820 | /* | |
2821 | * Wake up any processes waiting for any buffer to become free. | |
2822 | */ | |
2823 | wakeup(&needbuffer); | |
2824 | } | |
2825 | if (need_bp_wakeup) { | |
2826 | /* | |
2827 | * Wake up any proceeses waiting for _this_ buffer to become free. | |
2828 | */ | |
2829 | wakeup(bp); | |
2830 | } | |
1c79356b | 2831 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 388)) | DBG_FUNC_END, |
b0d623f7 | 2832 | bp, bp->b_datap, bp->b_flags, 0, 0); |
1c79356b A |
2833 | } |
2834 | ||
2835 | /* | |
2836 | * Determine if a block is in the cache. | |
2837 | * Just look on what would be its hash chain. If it's there, return | |
2838 | * a pointer to it, unless it's marked invalid. If it's marked invalid, | |
2839 | * we normally don't return the buffer, unless the caller explicitly | |
2840 | * wants us to. | |
2841 | */ | |
91447636 A |
2842 | static boolean_t |
2843 | incore(vnode_t vp, daddr64_t blkno) | |
2844 | { | |
2845 | boolean_t retval; | |
2d21ac55 | 2846 | struct bufhashhdr *dp; |
91447636 | 2847 | |
2d21ac55 | 2848 | dp = BUFHASH(vp, blkno); |
91447636 | 2849 | |
2d21ac55 A |
2850 | lck_mtx_lock_spin(buf_mtxp); |
2851 | ||
2852 | if (incore_locked(vp, blkno, dp)) | |
91447636 A |
2853 | retval = TRUE; |
2854 | else | |
2855 | retval = FALSE; | |
2856 | lck_mtx_unlock(buf_mtxp); | |
2857 | ||
2858 | return (retval); | |
2859 | } | |
2860 | ||
2861 | ||
2862 | static buf_t | |
2d21ac55 | 2863 | incore_locked(vnode_t vp, daddr64_t blkno, struct bufhashhdr *dp) |
1c79356b A |
2864 | { |
2865 | struct buf *bp; | |
1c79356b | 2866 | |
1c79356b | 2867 | /* Search hash chain */ |
2d21ac55 | 2868 | for (bp = dp->lh_first; bp != NULL; bp = bp->b_hash.le_next) { |
1c79356b | 2869 | if (bp->b_lblkno == blkno && bp->b_vp == vp && |
91447636 | 2870 | !ISSET(bp->b_flags, B_INVAL)) { |
1c79356b | 2871 | return (bp); |
91447636 | 2872 | } |
1c79356b | 2873 | } |
2d21ac55 | 2874 | return (NULL); |
1c79356b A |
2875 | } |
2876 | ||
39236c6e | 2877 | |
316670eb A |
2878 | void |
2879 | buf_wait_for_shadow_io(vnode_t vp, daddr64_t blkno) | |
2880 | { | |
2881 | buf_t bp; | |
2882 | struct bufhashhdr *dp; | |
2883 | ||
2884 | dp = BUFHASH(vp, blkno); | |
2885 | ||
2886 | lck_mtx_lock_spin(buf_mtxp); | |
2887 | ||
2888 | for (;;) { | |
2889 | if ((bp = incore_locked(vp, blkno, dp)) == NULL) | |
2890 | break; | |
2891 | ||
2892 | if (bp->b_shadow_ref == 0) | |
2893 | break; | |
2894 | ||
2895 | SET(bp->b_lflags, BL_WANTED_REF); | |
fa4905b1 | 2896 | |
316670eb A |
2897 | (void) msleep(bp, buf_mtxp, PSPIN | (PRIBIO+1), "buf_wait_for_shadow", NULL); |
2898 | } | |
2899 | lck_mtx_unlock(buf_mtxp); | |
2900 | } | |
2901 | ||
fa4905b1 | 2902 | /* XXX FIXME -- Update the comment to reflect the UBC changes (please) -- */ |
1c79356b A |
2903 | /* |
2904 | * Get a block of requested size that is associated with | |
2905 | * a given vnode and block offset. If it is found in the | |
2906 | * block cache, mark it as having been found, make it busy | |
2907 | * and return it. Otherwise, return an empty block of the | |
2908 | * correct size. It is up to the caller to insure that the | |
2909 | * cached blocks be of the correct size. | |
2910 | */ | |
91447636 A |
2911 | buf_t |
2912 | buf_getblk(vnode_t vp, daddr64_t blkno, int size, int slpflag, int slptimeo, int operation) | |
1c79356b | 2913 | { |
91447636 A |
2914 | buf_t bp; |
2915 | int err; | |
1c79356b A |
2916 | upl_t upl; |
2917 | upl_page_info_t *pl; | |
1c79356b | 2918 | kern_return_t kret; |
91447636 A |
2919 | int ret_only_valid; |
2920 | struct timespec ts; | |
2921 | int upl_flags; | |
2d21ac55 | 2922 | struct bufhashhdr *dp; |
1c79356b | 2923 | |
1c79356b | 2924 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 386)) | DBG_FUNC_START, |
b0d623f7 | 2925 | (uintptr_t)(blkno * PAGE_SIZE), size, operation, 0, 0); |
1c79356b | 2926 | |
91447636 A |
2927 | ret_only_valid = operation & BLK_ONLYVALID; |
2928 | operation &= ~BLK_ONLYVALID; | |
2d21ac55 | 2929 | dp = BUFHASH(vp, blkno); |
91447636 | 2930 | start: |
2d21ac55 | 2931 | lck_mtx_lock_spin(buf_mtxp); |
b0d623f7 | 2932 | |
2d21ac55 | 2933 | if ((bp = incore_locked(vp, blkno, dp))) { |
91447636 A |
2934 | /* |
2935 | * Found in the Buffer Cache | |
2936 | */ | |
2937 | if (ISSET(bp->b_lflags, BL_BUSY)) { | |
2938 | /* | |
2939 | * but is busy | |
2940 | */ | |
1c79356b A |
2941 | switch (operation) { |
2942 | case BLK_READ: | |
2943 | case BLK_WRITE: | |
2944 | case BLK_META: | |
91447636 | 2945 | SET(bp->b_lflags, BL_WANTED); |
1c79356b | 2946 | bufstats.bufs_busyincore++; |
91447636 A |
2947 | |
2948 | /* | |
2949 | * don't retake the mutex after being awakened... | |
2950 | * the time out is in msecs | |
2951 | */ | |
2952 | ts.tv_sec = (slptimeo/1000); | |
2953 | ts.tv_nsec = (slptimeo % 1000) * 10 * NSEC_PER_USEC * 1000; | |
2954 | ||
b0d623f7 A |
2955 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 396)) | DBG_FUNC_NONE, |
2956 | (uintptr_t)blkno, size, operation, 0, 0); | |
2957 | ||
91447636 A |
2958 | err = msleep(bp, buf_mtxp, slpflag | PDROP | (PRIBIO + 1), "buf_getblk", &ts); |
2959 | ||
1c79356b A |
2960 | /* |
2961 | * Callers who call with PCATCH or timeout are | |
2962 | * willing to deal with the NULL pointer | |
2963 | */ | |
91447636 | 2964 | if (err && ((slpflag & PCATCH) || ((err == EWOULDBLOCK) && slptimeo))) |
1c79356b A |
2965 | return (NULL); |
2966 | goto start; | |
2967 | /*NOTREACHED*/ | |
1c79356b | 2968 | |
1c79356b | 2969 | default: |
91447636 A |
2970 | /* |
2971 | * unknown operation requested | |
2972 | */ | |
2973 | panic("getblk: paging or unknown operation for incore busy buffer - %x\n", operation); | |
1c79356b A |
2974 | /*NOTREACHED*/ |
2975 | break; | |
2976 | } | |
2977 | } else { | |
d190cdc3 A |
2978 | int clear_bdone; |
2979 | ||
91447636 A |
2980 | /* |
2981 | * buffer in core and not busy | |
2982 | */ | |
91447636 A |
2983 | SET(bp->b_lflags, BL_BUSY); |
2984 | SET(bp->b_flags, B_CACHE); | |
b0d623f7 | 2985 | buf_busycount++; |
2d21ac55 | 2986 | |
91447636 | 2987 | bremfree_locked(bp); |
1c79356b | 2988 | bufstats.bufs_incore++; |
91447636 A |
2989 | |
2990 | lck_mtx_unlock(buf_mtxp); | |
2d21ac55 A |
2991 | #ifdef JOE_DEBUG |
2992 | bp->b_owner = current_thread(); | |
2993 | bp->b_tag = 1; | |
2994 | #endif | |
2995 | if ( (bp->b_upl) ) | |
2996 | panic("buffer has UPL, but not marked BUSY: %p", bp); | |
1c79356b | 2997 | |
d190cdc3 A |
2998 | clear_bdone = FALSE; |
2999 | if (!ret_only_valid) { | |
3000 | /* | |
3001 | * If the number bytes that are valid is going | |
3002 | * to increase (even if we end up not doing a | |
3003 | * reallocation through allocbuf) we have to read | |
3004 | * the new size first. | |
3005 | * | |
3006 | * This is required in cases where we doing a read | |
3007 | * modify write of a already valid data on disk but | |
3008 | * in cases where the data on disk beyond (blkno + b_bcount) | |
3009 | * is invalid, we may end up doing extra I/O. | |
3010 | */ | |
3011 | if (operation == BLK_META && bp->b_bcount < size) { | |
3012 | /* | |
3013 | * Since we are going to read in the whole size first | |
3014 | * we first have to ensure that any pending delayed write | |
3015 | * is flushed to disk first. | |
3016 | */ | |
3017 | if (ISSET(bp->b_flags, B_DELWRI)) { | |
3018 | CLR(bp->b_flags, B_CACHE); | |
3019 | buf_bwrite(bp); | |
3020 | goto start; | |
3021 | } | |
3022 | /* | |
3023 | * clear B_DONE before returning from | |
3024 | * this function so that the caller can | |
3025 | * can issue a read for the new size. | |
3026 | */ | |
3027 | clear_bdone = TRUE; | |
3028 | } | |
3029 | ||
3030 | if (bp->b_bufsize != size) | |
3031 | allocbuf(bp, size); | |
3032 | } | |
1c79356b | 3033 | |
91447636 | 3034 | upl_flags = 0; |
1c79356b | 3035 | switch (operation) { |
1c79356b | 3036 | case BLK_WRITE: |
91447636 A |
3037 | /* |
3038 | * "write" operation: let the UPL subsystem | |
3039 | * know that we intend to modify the buffer | |
3040 | * cache pages we're gathering. | |
3041 | */ | |
3042 | upl_flags |= UPL_WILL_MODIFY; | |
3043 | case BLK_READ: | |
3044 | upl_flags |= UPL_PRECIOUS; | |
3045 | if (UBCINFOEXISTS(bp->b_vp) && bp->b_bufsize) { | |
5ba3f43e | 3046 | kret = ubc_create_upl_kernel(vp, |
91447636 A |
3047 | ubc_blktooff(vp, bp->b_lblkno), |
3048 | bp->b_bufsize, | |
3049 | &upl, | |
3050 | &pl, | |
5ba3f43e A |
3051 | upl_flags, |
3052 | VM_KERN_MEMORY_FILE); | |
1c79356b | 3053 | if (kret != KERN_SUCCESS) |
91447636 | 3054 | panic("Failed to create UPL"); |
1c79356b | 3055 | |
91447636 | 3056 | bp->b_upl = upl; |
1c79356b | 3057 | |
91447636 A |
3058 | if (upl_valid_page(pl, 0)) { |
3059 | if (upl_dirty_page(pl, 0)) | |
3060 | SET(bp->b_flags, B_WASDIRTY); | |
3061 | else | |
3062 | CLR(bp->b_flags, B_WASDIRTY); | |
3063 | } else | |
3064 | CLR(bp->b_flags, (B_DONE | B_CACHE | B_WASDIRTY | B_DELWRI)); | |
1c79356b | 3065 | |
b0d623f7 | 3066 | kret = ubc_upl_map(upl, (vm_offset_t*)&(bp->b_datap)); |
1c79356b | 3067 | |
9bccf70c | 3068 | if (kret != KERN_SUCCESS) |
91447636 | 3069 | panic("getblk: ubc_upl_map() failed with (%d)", kret); |
1c79356b A |
3070 | } |
3071 | break; | |
3072 | ||
3073 | case BLK_META: | |
3074 | /* | |
3075 | * VM is not involved in IO for the meta data | |
3076 | * buffer already has valid data | |
3077 | */ | |
1c79356b A |
3078 | break; |
3079 | ||
3080 | default: | |
91447636 | 3081 | panic("getblk: paging or unknown operation for incore buffer- %d\n", operation); |
1c79356b A |
3082 | /*NOTREACHED*/ |
3083 | break; | |
3084 | } | |
d190cdc3 A |
3085 | |
3086 | if (clear_bdone) | |
3087 | CLR(bp->b_flags, B_DONE); | |
1c79356b A |
3088 | } |
3089 | } else { /* not incore() */ | |
3090 | int queue = BQ_EMPTY; /* Start with no preference */ | |
1c79356b | 3091 | |
91447636 A |
3092 | if (ret_only_valid) { |
3093 | lck_mtx_unlock(buf_mtxp); | |
3094 | return (NULL); | |
1c79356b | 3095 | } |
2d21ac55 | 3096 | if ((vnode_isreg(vp) == 0) || (UBCINFOEXISTS(vp) == 0) /*|| (vnode_issystem(vp) == 1)*/) |
91447636 A |
3097 | operation = BLK_META; |
3098 | ||
1c79356b | 3099 | if ((bp = getnewbuf(slpflag, slptimeo, &queue)) == NULL) |
b0d623f7 | 3100 | goto start; |
91447636 A |
3101 | |
3102 | /* | |
3103 | * getnewbuf may block for a number of different reasons... | |
3104 | * if it does, it's then possible for someone else to | |
3105 | * create a buffer for the same block and insert it into | |
3106 | * the hash... if we see it incore at this point we dump | |
3107 | * the buffer we were working on and start over | |
3108 | */ | |
2d21ac55 | 3109 | if (incore_locked(vp, blkno, dp)) { |
0b4e3aa0 A |
3110 | SET(bp->b_flags, B_INVAL); |
3111 | binshash(bp, &invalhash); | |
91447636 A |
3112 | |
3113 | lck_mtx_unlock(buf_mtxp); | |
3114 | ||
3115 | buf_brelse(bp); | |
0b4e3aa0 A |
3116 | goto start; |
3117 | } | |
b4c24cb9 A |
3118 | /* |
3119 | * NOTE: YOU CAN NOT BLOCK UNTIL binshash() HAS BEEN | |
3120 | * CALLED! BE CAREFUL. | |
3121 | */ | |
0b4e3aa0 | 3122 | |
1c79356b | 3123 | /* |
91447636 | 3124 | * mark the buffer as B_META if indicated |
1c79356b | 3125 | * so that when buffer is released it will goto META queue |
1c79356b | 3126 | */ |
91447636 A |
3127 | if (operation == BLK_META) |
3128 | SET(bp->b_flags, B_META); | |
9bccf70c A |
3129 | |
3130 | bp->b_blkno = bp->b_lblkno = blkno; | |
3131 | bp->b_vp = vp; | |
3132 | ||
0b4e3aa0 A |
3133 | /* |
3134 | * Insert in the hash so that incore() can find it | |
3135 | */ | |
3136 | binshash(bp, BUFHASH(vp, blkno)); | |
3137 | ||
2d21ac55 | 3138 | bgetvp_locked(vp, bp); |
91447636 | 3139 | |
2d21ac55 | 3140 | lck_mtx_unlock(buf_mtxp); |
9bccf70c | 3141 | |
1c79356b A |
3142 | allocbuf(bp, size); |
3143 | ||
91447636 | 3144 | upl_flags = 0; |
1c79356b A |
3145 | switch (operation) { |
3146 | case BLK_META: | |
91447636 A |
3147 | /* |
3148 | * buffer data is invalid... | |
3149 | * | |
3150 | * I don't want to have to retake buf_mtxp, | |
3151 | * so the miss and vmhits counters are done | |
3152 | * with Atomic updates... all other counters | |
3153 | * in bufstats are protected with either | |
3154 | * buf_mtxp or iobuffer_mtxp | |
3155 | */ | |
b0d623f7 | 3156 | OSAddAtomicLong(1, &bufstats.bufs_miss); |
1c79356b A |
3157 | break; |
3158 | ||
1c79356b | 3159 | case BLK_WRITE: |
91447636 A |
3160 | /* |
3161 | * "write" operation: let the UPL subsystem know | |
3162 | * that we intend to modify the buffer cache pages | |
3163 | * we're gathering. | |
3164 | */ | |
3165 | upl_flags |= UPL_WILL_MODIFY; | |
3166 | case BLK_READ: | |
3167 | { off_t f_offset; | |
3168 | size_t contig_bytes; | |
3169 | int bmap_flags; | |
1c79356b | 3170 | |
3e170ce0 A |
3171 | #if DEVELOPMENT || DEBUG |
3172 | /* | |
3173 | * Apple implemented file systems use UBC excludively; they should | |
3174 | * not call in here." | |
3175 | */ | |
3176 | const char* excldfs[] = {"hfs", "afpfs", "smbfs", "acfs", | |
3177 | "exfat", "msdos", "webdav", NULL}; | |
3178 | ||
3179 | for (int i = 0; excldfs[i] != NULL; i++) { | |
3180 | if (vp->v_mount && | |
3181 | !strcmp(vp->v_mount->mnt_vfsstat.f_fstypename, | |
3182 | excldfs[i])) { | |
3183 | panic("%s %s calls buf_getblk", | |
3184 | excldfs[i], | |
3185 | operation == BLK_READ ? "BLK_READ" : "BLK_WRITE"); | |
3186 | } | |
3187 | } | |
3188 | #endif | |
3189 | ||
91447636 | 3190 | if ( (bp->b_upl) ) |
2d21ac55 | 3191 | panic("bp already has UPL: %p",bp); |
1c79356b | 3192 | |
91447636 A |
3193 | f_offset = ubc_blktooff(vp, blkno); |
3194 | ||
3195 | upl_flags |= UPL_PRECIOUS; | |
5ba3f43e | 3196 | kret = ubc_create_upl_kernel(vp, |
91447636 A |
3197 | f_offset, |
3198 | bp->b_bufsize, | |
3199 | &upl, | |
3200 | &pl, | |
5ba3f43e A |
3201 | upl_flags, |
3202 | VM_KERN_MEMORY_FILE); | |
1c79356b | 3203 | |
91447636 A |
3204 | if (kret != KERN_SUCCESS) |
3205 | panic("Failed to create UPL"); | |
b0d623f7 A |
3206 | #if UPL_DEBUG |
3207 | upl_ubc_alias_set(upl, (uintptr_t) bp, (uintptr_t) 4); | |
91447636 A |
3208 | #endif /* UPL_DEBUG */ |
3209 | bp->b_upl = upl; | |
1c79356b A |
3210 | |
3211 | if (upl_valid_page(pl, 0)) { | |
1c79356b | 3212 | |
91447636 A |
3213 | if (operation == BLK_READ) |
3214 | bmap_flags = VNODE_READ; | |
3215 | else | |
3216 | bmap_flags = VNODE_WRITE; | |
1c79356b | 3217 | |
91447636 | 3218 | SET(bp->b_flags, B_CACHE | B_DONE); |
1c79356b | 3219 | |
b0d623f7 | 3220 | OSAddAtomicLong(1, &bufstats.bufs_vmhits); |
1c79356b | 3221 | |
91447636 A |
3222 | bp->b_validoff = 0; |
3223 | bp->b_dirtyoff = 0; | |
1c79356b | 3224 | |
91447636 A |
3225 | if (upl_dirty_page(pl, 0)) { |
3226 | /* page is dirty */ | |
3227 | SET(bp->b_flags, B_WASDIRTY); | |
1c79356b | 3228 | |
91447636 A |
3229 | bp->b_validend = bp->b_bcount; |
3230 | bp->b_dirtyend = bp->b_bcount; | |
1c79356b | 3231 | } else { |
91447636 A |
3232 | /* page is clean */ |
3233 | bp->b_validend = bp->b_bcount; | |
3234 | bp->b_dirtyend = 0; | |
1c79356b | 3235 | } |
91447636 A |
3236 | /* |
3237 | * try to recreate the physical block number associated with | |
3238 | * this buffer... | |
3239 | */ | |
3240 | if (VNOP_BLOCKMAP(vp, f_offset, bp->b_bcount, &bp->b_blkno, &contig_bytes, NULL, bmap_flags, NULL)) | |
3241 | panic("getblk: VNOP_BLOCKMAP failed"); | |
3242 | /* | |
3243 | * if the extent represented by this buffer | |
3244 | * is not completely physically contiguous on | |
3245 | * disk, than we can't cache the physical mapping | |
3246 | * in the buffer header | |
3247 | */ | |
3248 | if ((long)contig_bytes < bp->b_bcount) | |
3249 | bp->b_blkno = bp->b_lblkno; | |
1c79356b | 3250 | } else { |
b0d623f7 | 3251 | OSAddAtomicLong(1, &bufstats.bufs_miss); |
1c79356b | 3252 | } |
b0d623f7 | 3253 | kret = ubc_upl_map(upl, (vm_offset_t *)&(bp->b_datap)); |
1c79356b | 3254 | |
91447636 A |
3255 | if (kret != KERN_SUCCESS) |
3256 | panic("getblk: ubc_upl_map() failed with (%d)", kret); | |
1c79356b | 3257 | break; |
91447636 | 3258 | } |
1c79356b | 3259 | default: |
91447636 | 3260 | panic("getblk: paging or unknown operation - %x", operation); |
1c79356b A |
3261 | /*NOTREACHED*/ |
3262 | break; | |
3263 | } | |
3264 | } | |
1c79356b | 3265 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 386)) | DBG_FUNC_END, |
b0d623f7 | 3266 | bp, bp->b_datap, bp->b_flags, 3, 0); |
91447636 A |
3267 | |
3268 | #ifdef JOE_DEBUG | |
b0d623f7 | 3269 | (void) OSBacktrace(&bp->b_stackgetblk[0], 6); |
91447636 | 3270 | #endif |
1c79356b A |
3271 | return (bp); |
3272 | } | |
3273 | ||
3274 | /* | |
3275 | * Get an empty, disassociated buffer of given size. | |
3276 | */ | |
91447636 | 3277 | buf_t |
2d21ac55 | 3278 | buf_geteblk(int size) |
1c79356b | 3279 | { |
b0d623f7 | 3280 | buf_t bp = NULL; |
91447636 A |
3281 | int queue = BQ_EMPTY; |
3282 | ||
b0d623f7 A |
3283 | do { |
3284 | lck_mtx_lock_spin(buf_mtxp); | |
3285 | ||
3286 | bp = getnewbuf(0, 0, &queue); | |
3287 | } while (bp == NULL); | |
1c79356b | 3288 | |
1c79356b | 3289 | SET(bp->b_flags, (B_META|B_INVAL)); |
1c79356b A |
3290 | |
3291 | #if DIAGNOSTIC | |
3292 | assert(queue == BQ_EMPTY); | |
3293 | #endif /* DIAGNOSTIC */ | |
3294 | /* XXX need to implement logic to deal with other queues */ | |
3295 | ||
1c79356b | 3296 | binshash(bp, &invalhash); |
1c79356b A |
3297 | bufstats.bufs_eblk++; |
3298 | ||
91447636 A |
3299 | lck_mtx_unlock(buf_mtxp); |
3300 | ||
3301 | allocbuf(bp, size); | |
3302 | ||
1c79356b A |
3303 | return (bp); |
3304 | } | |
3305 | ||
6d2010ae A |
3306 | uint32_t |
3307 | buf_redundancy_flags(buf_t bp) | |
3308 | { | |
3309 | return bp->b_redundancy_flags; | |
3310 | } | |
3311 | ||
3312 | void | |
3313 | buf_set_redundancy_flags(buf_t bp, uint32_t flags) | |
3314 | { | |
3315 | SET(bp->b_redundancy_flags, flags); | |
3316 | } | |
3317 | ||
3318 | void | |
3319 | buf_clear_redundancy_flags(buf_t bp, uint32_t flags) | |
3320 | { | |
3321 | CLR(bp->b_redundancy_flags, flags); | |
3322 | } | |
1c79356b | 3323 | |
fe8ab488 A |
3324 | |
3325 | ||
3326 | static void * | |
3327 | recycle_buf_from_pool(int nsize) | |
3328 | { | |
3329 | buf_t bp; | |
3330 | void *ptr = NULL; | |
3331 | ||
3332 | lck_mtx_lock_spin(buf_mtxp); | |
3333 | ||
3334 | TAILQ_FOREACH(bp, &bufqueues[BQ_META], b_freelist) { | |
3335 | if (ISSET(bp->b_flags, B_DELWRI) || bp->b_bufsize != nsize) | |
3336 | continue; | |
3337 | ptr = (void *)bp->b_datap; | |
3338 | bp->b_bufsize = 0; | |
3339 | ||
3340 | bcleanbuf(bp, TRUE); | |
3341 | break; | |
3342 | } | |
3343 | lck_mtx_unlock(buf_mtxp); | |
3344 | ||
3345 | return (ptr); | |
3346 | } | |
3347 | ||
3348 | ||
3349 | ||
3350 | int zalloc_nopagewait_failed = 0; | |
3351 | int recycle_buf_failed = 0; | |
3352 | ||
3353 | static void * | |
3354 | grab_memory_for_meta_buf(int nsize) | |
3355 | { | |
3356 | zone_t z; | |
3357 | void *ptr; | |
3358 | boolean_t was_vmpriv; | |
3359 | ||
3360 | z = getbufzone(nsize); | |
3361 | ||
3362 | /* | |
3363 | * make sure we're NOT priviliged so that | |
3364 | * if a vm_page_grab is needed, it won't | |
3365 | * block if we're out of free pages... if | |
3366 | * it blocks, then we can't honor the | |
3367 | * nopagewait request | |
3368 | */ | |
3369 | was_vmpriv = set_vm_privilege(FALSE); | |
3370 | ||
3371 | ptr = zalloc_nopagewait(z); | |
3372 | ||
3373 | if (was_vmpriv == TRUE) | |
3374 | set_vm_privilege(TRUE); | |
3375 | ||
3376 | if (ptr == NULL) { | |
3377 | ||
3378 | zalloc_nopagewait_failed++; | |
3379 | ||
3380 | ptr = recycle_buf_from_pool(nsize); | |
3381 | ||
3382 | if (ptr == NULL) { | |
3383 | ||
3384 | recycle_buf_failed++; | |
3385 | ||
3386 | if (was_vmpriv == FALSE) | |
3387 | set_vm_privilege(TRUE); | |
3388 | ||
3389 | ptr = zalloc(z); | |
3390 | ||
3391 | if (was_vmpriv == FALSE) | |
3392 | set_vm_privilege(FALSE); | |
3393 | } | |
3394 | } | |
3395 | return (ptr); | |
3396 | } | |
3397 | ||
1c79356b A |
3398 | /* |
3399 | * With UBC, there is no need to expand / shrink the file data | |
3400 | * buffer. The VM uses the same pages, hence no waste. | |
3401 | * All the file data buffers can have one size. | |
3402 | * In fact expand / shrink would be an expensive operation. | |
3403 | * | |
3404 | * Only exception to this is meta-data buffers. Most of the | |
3405 | * meta data operations are smaller than PAGE_SIZE. Having the | |
3406 | * meta-data buffers grow and shrink as needed, optimizes use | |
3407 | * of the kernel wired memory. | |
3408 | */ | |
3409 | ||
3410 | int | |
91447636 | 3411 | allocbuf(buf_t bp, int size) |
1c79356b A |
3412 | { |
3413 | vm_size_t desired_size; | |
3414 | ||
3415 | desired_size = roundup(size, CLBYTES); | |
3416 | ||
91447636 | 3417 | if (desired_size < PAGE_SIZE) |
1c79356b A |
3418 | desired_size = PAGE_SIZE; |
3419 | if (desired_size > MAXBSIZE) | |
3420 | panic("allocbuf: buffer larger than MAXBSIZE requested"); | |
3421 | ||
1c79356b | 3422 | if (ISSET(bp->b_flags, B_META)) { |
91447636 A |
3423 | int nsize = roundup(size, MINMETA); |
3424 | ||
3425 | if (bp->b_datap) { | |
3426 | vm_offset_t elem = (vm_offset_t)bp->b_datap; | |
3427 | ||
3428 | if (ISSET(bp->b_flags, B_ZALLOC)) { | |
3429 | if (bp->b_bufsize < nsize) { | |
fe8ab488 A |
3430 | zone_t zprev; |
3431 | ||
91447636 A |
3432 | /* reallocate to a bigger size */ |
3433 | ||
3434 | zprev = getbufzone(bp->b_bufsize); | |
3435 | if (nsize <= MAXMETA) { | |
3436 | desired_size = nsize; | |
fe8ab488 | 3437 | |
2d21ac55 | 3438 | /* b_datap not really a ptr */ |
fe8ab488 | 3439 | *(void **)(&bp->b_datap) = grab_memory_for_meta_buf(nsize); |
1c79356b | 3440 | } else { |
91447636 | 3441 | bp->b_datap = (uintptr_t)NULL; |
3e170ce0 | 3442 | kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size, VM_KERN_MEMORY_FILE); |
91447636 | 3443 | CLR(bp->b_flags, B_ZALLOC); |
1c79356b | 3444 | } |
91447636 A |
3445 | bcopy((void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize); |
3446 | zfree(zprev, (void *)elem); | |
3447 | } else { | |
3448 | desired_size = bp->b_bufsize; | |
3449 | } | |
3450 | ||
3451 | } else { | |
3452 | if ((vm_size_t)bp->b_bufsize < desired_size) { | |
1c79356b | 3453 | /* reallocate to a bigger size */ |
91447636 | 3454 | bp->b_datap = (uintptr_t)NULL; |
3e170ce0 | 3455 | kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size, VM_KERN_MEMORY_FILE); |
91447636 | 3456 | bcopy((const void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize); |
1c79356b A |
3457 | kmem_free(kernel_map, elem, bp->b_bufsize); |
3458 | } else { | |
3459 | desired_size = bp->b_bufsize; | |
3460 | } | |
91447636 | 3461 | } |
1c79356b A |
3462 | } else { |
3463 | /* new allocation */ | |
3464 | if (nsize <= MAXMETA) { | |
3465 | desired_size = nsize; | |
fe8ab488 | 3466 | |
2d21ac55 | 3467 | /* b_datap not really a ptr */ |
fe8ab488 | 3468 | *(void **)(&bp->b_datap) = grab_memory_for_meta_buf(nsize); |
1c79356b | 3469 | SET(bp->b_flags, B_ZALLOC); |
91447636 | 3470 | } else |
3e170ce0 | 3471 | kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size, VM_KERN_MEMORY_FILE); |
1c79356b | 3472 | } |
2d21ac55 A |
3473 | |
3474 | if (bp->b_datap == 0) | |
3475 | panic("allocbuf: NULL b_datap"); | |
1c79356b | 3476 | } |
9bccf70c A |
3477 | bp->b_bufsize = desired_size; |
3478 | bp->b_bcount = size; | |
91447636 | 3479 | |
9bccf70c | 3480 | return (0); |
1c79356b A |
3481 | } |
3482 | ||
3483 | /* | |
3484 | * Get a new buffer from one of the free lists. | |
3485 | * | |
3486 | * Request for a queue is passes in. The queue from which the buffer was taken | |
3487 | * from is returned. Out of range queue requests get BQ_EMPTY. Request for | |
3488 | * BQUEUE means no preference. Use heuristics in that case. | |
3489 | * Heuristics is as follows: | |
3490 | * Try BQ_AGE, BQ_LRU, BQ_EMPTY, BQ_META in that order. | |
3491 | * If none available block till one is made available. | |
3492 | * If buffers available on both BQ_AGE and BQ_LRU, check the timestamps. | |
3493 | * Pick the most stale buffer. | |
3494 | * If found buffer was marked delayed write, start the async. write | |
3495 | * and restart the search. | |
3496 | * Initialize the fields and disassociate the buffer from the vnode. | |
3497 | * Remove the buffer from the hash. Return the buffer and the queue | |
3498 | * on which it was found. | |
91447636 A |
3499 | * |
3500 | * buf_mtxp is held upon entry | |
b0d623f7 A |
3501 | * returns with buf_mtxp locked if new buf available |
3502 | * returns with buf_mtxp UNlocked if new buf NOT available | |
1c79356b A |
3503 | */ |
3504 | ||
91447636 A |
3505 | static buf_t |
3506 | getnewbuf(int slpflag, int slptimeo, int * queue) | |
1c79356b | 3507 | { |
91447636 A |
3508 | buf_t bp; |
3509 | buf_t lru_bp; | |
3510 | buf_t age_bp; | |
3511 | buf_t meta_bp; | |
3512 | int age_time, lru_time, bp_time, meta_time; | |
3513 | int req = *queue; /* save it for restarts */ | |
3514 | struct timespec ts; | |
1c79356b A |
3515 | |
3516 | start: | |
91447636 A |
3517 | /* |
3518 | * invalid request gets empty queue | |
3519 | */ | |
2d21ac55 | 3520 | if ((*queue >= BQUEUES) || (*queue < 0) |
765c9de3 | 3521 | || (*queue == BQ_LAUNDRY) || (*queue == BQ_LOCKED)) |
1c79356b | 3522 | *queue = BQ_EMPTY; |
2d21ac55 A |
3523 | |
3524 | ||
3525 | if (*queue == BQ_EMPTY && (bp = bufqueues[*queue].tqh_first)) | |
3526 | goto found; | |
3527 | ||
3528 | /* | |
3529 | * need to grow number of bufs, add another one rather than recycling | |
3530 | */ | |
3531 | if (nbuf_headers < max_nbuf_headers) { | |
0c530ab8 A |
3532 | /* |
3533 | * Increment count now as lock | |
3534 | * is dropped for allocation. | |
3535 | * That avoids over commits | |
3536 | */ | |
2d21ac55 | 3537 | nbuf_headers++; |
0c530ab8 A |
3538 | goto add_newbufs; |
3539 | } | |
2d21ac55 A |
3540 | /* Try for the requested queue first */ |
3541 | bp = bufqueues[*queue].tqh_first; | |
3542 | if (bp) | |
3543 | goto found; | |
1c79356b A |
3544 | |
3545 | /* Unable to use requested queue */ | |
3546 | age_bp = bufqueues[BQ_AGE].tqh_first; | |
3547 | lru_bp = bufqueues[BQ_LRU].tqh_first; | |
3548 | meta_bp = bufqueues[BQ_META].tqh_first; | |
3549 | ||
9bccf70c A |
3550 | if (!age_bp && !lru_bp && !meta_bp) { |
3551 | /* | |
3552 | * Unavailble on AGE or LRU or META queues | |
3553 | * Try the empty list first | |
3554 | */ | |
1c79356b A |
3555 | bp = bufqueues[BQ_EMPTY].tqh_first; |
3556 | if (bp) { | |
3557 | *queue = BQ_EMPTY; | |
3558 | goto found; | |
3559 | } | |
0c530ab8 A |
3560 | /* |
3561 | * We have seen is this is hard to trigger. | |
3562 | * This is an overcommit of nbufs but needed | |
3563 | * in some scenarios with diskiamges | |
3564 | */ | |
3565 | ||
3566 | add_newbufs: | |
91447636 | 3567 | lck_mtx_unlock(buf_mtxp); |
765c9de3 | 3568 | |
91447636 | 3569 | /* Create a new temporary buffer header */ |
765c9de3 | 3570 | bp = (struct buf *)zalloc(buf_hdr_zone); |
2d21ac55 | 3571 | |
765c9de3 A |
3572 | if (bp) { |
3573 | bufhdrinit(bp); | |
2d21ac55 A |
3574 | bp->b_whichq = BQ_EMPTY; |
3575 | bp->b_timestamp = buf_timestamp(); | |
765c9de3 | 3576 | BLISTNONE(bp); |
765c9de3 A |
3577 | SET(bp->b_flags, B_HDRALLOC); |
3578 | *queue = BQ_EMPTY; | |
2d21ac55 | 3579 | } |
b0d623f7 | 3580 | lck_mtx_lock_spin(buf_mtxp); |
2d21ac55 A |
3581 | |
3582 | if (bp) { | |
3583 | binshash(bp, &invalhash); | |
765c9de3 A |
3584 | binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY); |
3585 | buf_hdr_count++; | |
3586 | goto found; | |
3587 | } | |
0c530ab8 | 3588 | /* subtract already accounted bufcount */ |
2d21ac55 | 3589 | nbuf_headers--; |
0c530ab8 | 3590 | |
91447636 | 3591 | bufstats.bufs_sleeps++; |
765c9de3 | 3592 | |
1c79356b A |
3593 | /* wait for a free buffer of any kind */ |
3594 | needbuffer = 1; | |
91447636 A |
3595 | /* hz value is 100 */ |
3596 | ts.tv_sec = (slptimeo/1000); | |
3597 | /* the hz value is 100; which leads to 10ms */ | |
3598 | ts.tv_nsec = (slptimeo % 1000) * NSEC_PER_USEC * 1000 * 10; | |
b0d623f7 A |
3599 | |
3600 | msleep(&needbuffer, buf_mtxp, slpflag | PDROP | (PRIBIO+1), "getnewbuf", &ts); | |
2d21ac55 | 3601 | return (NULL); |
1c79356b A |
3602 | } |
3603 | ||
3604 | /* Buffer available either on AGE or LRU or META */ | |
3605 | bp = NULL; | |
3606 | *queue = -1; | |
3607 | ||
3608 | /* Buffer available either on AGE or LRU */ | |
3609 | if (!age_bp) { | |
3610 | bp = lru_bp; | |
3611 | *queue = BQ_LRU; | |
3612 | } else if (!lru_bp) { | |
3613 | bp = age_bp; | |
3614 | *queue = BQ_AGE; | |
3615 | } else { /* buffer available on both AGE and LRU */ | |
91447636 A |
3616 | int t = buf_timestamp(); |
3617 | ||
3618 | age_time = t - age_bp->b_timestamp; | |
3619 | lru_time = t - lru_bp->b_timestamp; | |
1c79356b A |
3620 | if ((age_time < 0) || (lru_time < 0)) { /* time set backwards */ |
3621 | bp = age_bp; | |
3622 | *queue = BQ_AGE; | |
3623 | /* | |
3624 | * we should probably re-timestamp eveything in the | |
3625 | * queues at this point with the current time | |
3626 | */ | |
3627 | } else { | |
3628 | if ((lru_time >= lru_is_stale) && (age_time < age_is_stale)) { | |
3629 | bp = lru_bp; | |
3630 | *queue = BQ_LRU; | |
3631 | } else { | |
3632 | bp = age_bp; | |
3633 | *queue = BQ_AGE; | |
3634 | } | |
3635 | } | |
3636 | } | |
3637 | ||
3638 | if (!bp) { /* Neither on AGE nor on LRU */ | |
3639 | bp = meta_bp; | |
3640 | *queue = BQ_META; | |
3641 | } else if (meta_bp) { | |
91447636 A |
3642 | int t = buf_timestamp(); |
3643 | ||
3644 | bp_time = t - bp->b_timestamp; | |
3645 | meta_time = t - meta_bp->b_timestamp; | |
1c79356b A |
3646 | |
3647 | if (!(bp_time < 0) && !(meta_time < 0)) { | |
3648 | /* time not set backwards */ | |
3649 | int bp_is_stale; | |
3650 | bp_is_stale = (*queue == BQ_LRU) ? | |
3651 | lru_is_stale : age_is_stale; | |
3652 | ||
3653 | if ((meta_time >= meta_is_stale) && | |
3654 | (bp_time < bp_is_stale)) { | |
3655 | bp = meta_bp; | |
3656 | *queue = BQ_META; | |
3657 | } | |
3658 | } | |
3659 | } | |
1c79356b | 3660 | found: |
91447636 | 3661 | if (ISSET(bp->b_flags, B_LOCKED) || ISSET(bp->b_lflags, BL_BUSY)) |
b0d623f7 | 3662 | panic("getnewbuf: bp @ %p is LOCKED or BUSY! (flags 0x%x)\n", bp, bp->b_flags); |
1c79356b A |
3663 | |
3664 | /* Clean it */ | |
b0d623f7 | 3665 | if (bcleanbuf(bp, FALSE)) { |
91447636 A |
3666 | /* |
3667 | * moved to the laundry thread, buffer not ready | |
3668 | */ | |
1c79356b A |
3669 | *queue = req; |
3670 | goto start; | |
3671 | } | |
1c79356b A |
3672 | return (bp); |
3673 | } | |
9bccf70c | 3674 | |
1c79356b A |
3675 | |
3676 | /* | |
3677 | * Clean a buffer. | |
6d2010ae | 3678 | * Returns 0 if buffer is ready to use, |
91447636 | 3679 | * Returns 1 if issued a buf_bawrite() to indicate |
1c79356b | 3680 | * that the buffer is not ready. |
91447636 A |
3681 | * |
3682 | * buf_mtxp is held upon entry | |
3683 | * returns with buf_mtxp locked | |
1c79356b | 3684 | */ |
6d2010ae | 3685 | int |
b0d623f7 | 3686 | bcleanbuf(buf_t bp, boolean_t discard) |
1c79356b | 3687 | { |
1c79356b | 3688 | /* Remove from the queue */ |
91447636 | 3689 | bremfree_locked(bp); |
1c79356b | 3690 | |
91447636 A |
3691 | #ifdef JOE_DEBUG |
3692 | bp->b_owner = current_thread(); | |
3693 | bp->b_tag = 2; | |
3694 | #endif | |
765c9de3 A |
3695 | /* |
3696 | * If buffer was a delayed write, start the IO by queuing | |
3697 | * it on the LAUNDRY queue, and return 1 | |
3698 | */ | |
1c79356b | 3699 | if (ISSET(bp->b_flags, B_DELWRI)) { |
b0d623f7 A |
3700 | if (discard) { |
3701 | SET(bp->b_lflags, BL_WANTDEALLOC); | |
3702 | } | |
3703 | ||
6d2010ae | 3704 | bmovelaundry(bp); |
91447636 A |
3705 | |
3706 | lck_mtx_unlock(buf_mtxp); | |
3707 | ||
2d21ac55 A |
3708 | wakeup(&bufqueues[BQ_LAUNDRY]); |
3709 | /* | |
3710 | * and give it a chance to run | |
3711 | */ | |
9bccf70c | 3712 | (void)thread_block(THREAD_CONTINUE_NULL); |
91447636 | 3713 | |
b0d623f7 | 3714 | lck_mtx_lock_spin(buf_mtxp); |
2d21ac55 | 3715 | |
1c79356b A |
3716 | return (1); |
3717 | } | |
2d21ac55 A |
3718 | #ifdef JOE_DEBUG |
3719 | bp->b_owner = current_thread(); | |
3720 | bp->b_tag = 8; | |
3721 | #endif | |
3722 | /* | |
3723 | * Buffer is no longer on any free list... we own it | |
3724 | */ | |
3725 | SET(bp->b_lflags, BL_BUSY); | |
b0d623f7 A |
3726 | buf_busycount++; |
3727 | ||
2d21ac55 | 3728 | bremhash(bp); |
91447636 | 3729 | |
91447636 A |
3730 | /* |
3731 | * disassociate us from our vnode, if we had one... | |
3732 | */ | |
3733 | if (bp->b_vp) | |
2d21ac55 A |
3734 | brelvp_locked(bp); |
3735 | ||
3736 | lck_mtx_unlock(buf_mtxp); | |
3737 | ||
3738 | BLISTNONE(bp); | |
91447636 | 3739 | |
6d2010ae A |
3740 | if (ISSET(bp->b_flags, B_META)) |
3741 | buf_free_meta_store(bp); | |
91447636 A |
3742 | |
3743 | trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno); | |
3744 | ||
6d2010ae | 3745 | buf_release_credentials(bp); |
fe8ab488 | 3746 | |
b0d623f7 A |
3747 | /* If discarding, just move to the empty queue */ |
3748 | if (discard) { | |
3749 | lck_mtx_lock_spin(buf_mtxp); | |
3750 | CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE | B_FUA)); | |
3751 | bp->b_whichq = BQ_EMPTY; | |
3752 | binshash(bp, &invalhash); | |
3753 | binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY); | |
3754 | CLR(bp->b_lflags, BL_BUSY); | |
3755 | buf_busycount--; | |
3756 | } else { | |
3757 | /* Not discarding: clean up and prepare for reuse */ | |
3758 | bp->b_bufsize = 0; | |
3759 | bp->b_datap = (uintptr_t)NULL; | |
3760 | bp->b_upl = (void *)NULL; | |
3e170ce0 | 3761 | bp->b_fsprivate = (void *)NULL; |
b0d623f7 A |
3762 | /* |
3763 | * preserve the state of whether this buffer | |
3764 | * was allocated on the fly or not... | |
3765 | * the only other flag that should be set at | |
3766 | * this point is BL_BUSY... | |
3767 | */ | |
3768 | #ifdef JOE_DEBUG | |
3769 | bp->b_owner = current_thread(); | |
3770 | bp->b_tag = 3; | |
3771 | #endif | |
3772 | bp->b_lflags = BL_BUSY; | |
3773 | bp->b_flags = (bp->b_flags & B_HDRALLOC); | |
3e170ce0 | 3774 | bp->b_redundancy_flags = 0; |
b0d623f7 A |
3775 | bp->b_dev = NODEV; |
3776 | bp->b_blkno = bp->b_lblkno = 0; | |
3777 | bp->b_iodone = NULL; | |
3778 | bp->b_error = 0; | |
3779 | bp->b_resid = 0; | |
3780 | bp->b_bcount = 0; | |
3781 | bp->b_dirtyoff = bp->b_dirtyend = 0; | |
3782 | bp->b_validoff = bp->b_validend = 0; | |
7ddcb079 | 3783 | bzero(&bp->b_attr, sizeof(struct bufattr)); |
b0d623f7 A |
3784 | |
3785 | lck_mtx_lock_spin(buf_mtxp); | |
3786 | } | |
91447636 A |
3787 | return (0); |
3788 | } | |
3789 | ||
3790 | ||
3791 | ||
3792 | errno_t | |
3793 | buf_invalblkno(vnode_t vp, daddr64_t lblkno, int flags) | |
3794 | { | |
3795 | buf_t bp; | |
3796 | errno_t error; | |
2d21ac55 A |
3797 | struct bufhashhdr *dp; |
3798 | ||
3799 | dp = BUFHASH(vp, lblkno); | |
91447636 | 3800 | |
91447636 | 3801 | relook: |
b0d623f7 A |
3802 | lck_mtx_lock_spin(buf_mtxp); |
3803 | ||
2d21ac55 | 3804 | if ((bp = incore_locked(vp, lblkno, dp)) == (struct buf *)0) { |
91447636 A |
3805 | lck_mtx_unlock(buf_mtxp); |
3806 | return (0); | |
3807 | } | |
3808 | if (ISSET(bp->b_lflags, BL_BUSY)) { | |
3809 | if ( !ISSET(flags, BUF_WAIT)) { | |
3810 | lck_mtx_unlock(buf_mtxp); | |
3811 | return (EBUSY); | |
3812 | } | |
3813 | SET(bp->b_lflags, BL_WANTED); | |
3814 | ||
b0d623f7 | 3815 | error = msleep((caddr_t)bp, buf_mtxp, PDROP | (PRIBIO + 1), "buf_invalblkno", NULL); |
91447636 | 3816 | |
2d21ac55 | 3817 | if (error) { |
91447636 | 3818 | return (error); |
2d21ac55 | 3819 | } |
91447636 A |
3820 | goto relook; |
3821 | } | |
3822 | bremfree_locked(bp); | |
3823 | SET(bp->b_lflags, BL_BUSY); | |
3824 | SET(bp->b_flags, B_INVAL); | |
b0d623f7 | 3825 | buf_busycount++; |
91447636 A |
3826 | #ifdef JOE_DEBUG |
3827 | bp->b_owner = current_thread(); | |
3828 | bp->b_tag = 4; | |
3829 | #endif | |
3830 | lck_mtx_unlock(buf_mtxp); | |
3831 | buf_brelse(bp); | |
3832 | ||
3833 | return (0); | |
3834 | } | |
3835 | ||
3836 | ||
3837 | void | |
3838 | buf_drop(buf_t bp) | |
3839 | { | |
3840 | int need_wakeup = 0; | |
3841 | ||
2d21ac55 | 3842 | lck_mtx_lock_spin(buf_mtxp); |
91447636 A |
3843 | |
3844 | if (ISSET(bp->b_lflags, BL_WANTED)) { | |
3845 | /* | |
3846 | * delay the actual wakeup until after we | |
3847 | * clear BL_BUSY and we've dropped buf_mtxp | |
3848 | */ | |
3849 | need_wakeup = 1; | |
3850 | } | |
2d21ac55 A |
3851 | #ifdef JOE_DEBUG |
3852 | bp->b_owner = current_thread(); | |
3853 | bp->b_tag = 9; | |
3854 | #endif | |
91447636 A |
3855 | /* |
3856 | * Unlock the buffer. | |
3857 | */ | |
3858 | CLR(bp->b_lflags, (BL_BUSY | BL_WANTED)); | |
b0d623f7 | 3859 | buf_busycount--; |
1c79356b | 3860 | |
91447636 | 3861 | lck_mtx_unlock(buf_mtxp); |
1c79356b | 3862 | |
91447636 A |
3863 | if (need_wakeup) { |
3864 | /* | |
3865 | * Wake up any proceeses waiting for _this_ buffer to become free. | |
3866 | */ | |
3867 | wakeup(bp); | |
3868 | } | |
3869 | } | |
1c79356b | 3870 | |
1c79356b | 3871 | |
91447636 A |
3872 | errno_t |
3873 | buf_acquire(buf_t bp, int flags, int slpflag, int slptimeo) { | |
3874 | errno_t error; | |
1c79356b | 3875 | |
b0d623f7 | 3876 | lck_mtx_lock_spin(buf_mtxp); |
1c79356b | 3877 | |
91447636 | 3878 | error = buf_acquire_locked(bp, flags, slpflag, slptimeo); |
1c79356b | 3879 | |
91447636 | 3880 | lck_mtx_unlock(buf_mtxp); |
1c79356b | 3881 | |
91447636 A |
3882 | return (error); |
3883 | } | |
1c79356b | 3884 | |
91447636 A |
3885 | |
3886 | static errno_t | |
3887 | buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo) | |
3888 | { | |
3889 | errno_t error; | |
3890 | struct timespec ts; | |
3891 | ||
3892 | if (ISSET(bp->b_flags, B_LOCKED)) { | |
3893 | if ((flags & BAC_SKIP_LOCKED)) | |
3894 | return (EDEADLK); | |
3895 | } else { | |
3896 | if ((flags & BAC_SKIP_NONLOCKED)) | |
3897 | return (EDEADLK); | |
1c79356b | 3898 | } |
91447636 A |
3899 | if (ISSET(bp->b_lflags, BL_BUSY)) { |
3900 | /* | |
b0d623f7 | 3901 | * since the lck_mtx_lock may block, the buffer |
91447636 A |
3902 | * may become BUSY, so we need to |
3903 | * recheck for a NOWAIT request | |
3904 | */ | |
3905 | if (flags & BAC_NOWAIT) | |
3906 | return (EBUSY); | |
3907 | SET(bp->b_lflags, BL_WANTED); | |
3908 | ||
3909 | /* the hz value is 100; which leads to 10ms */ | |
3910 | ts.tv_sec = (slptimeo/100); | |
3911 | ts.tv_nsec = (slptimeo % 100) * 10 * NSEC_PER_USEC * 1000; | |
2d21ac55 | 3912 | error = msleep((caddr_t)bp, buf_mtxp, slpflag | (PRIBIO + 1), "buf_acquire", &ts); |
91447636 A |
3913 | |
3914 | if (error) | |
3915 | return (error); | |
3916 | return (EAGAIN); | |
1c79356b | 3917 | } |
91447636 A |
3918 | if (flags & BAC_REMOVE) |
3919 | bremfree_locked(bp); | |
3920 | SET(bp->b_lflags, BL_BUSY); | |
b0d623f7 A |
3921 | buf_busycount++; |
3922 | ||
91447636 A |
3923 | #ifdef JOE_DEBUG |
3924 | bp->b_owner = current_thread(); | |
3925 | bp->b_tag = 5; | |
3926 | #endif | |
1c79356b A |
3927 | return (0); |
3928 | } | |
3929 | ||
3930 | ||
3931 | /* | |
3932 | * Wait for operations on the buffer to complete. | |
3933 | * When they do, extract and return the I/O's error value. | |
3934 | */ | |
91447636 A |
3935 | errno_t |
3936 | buf_biowait(buf_t bp) | |
1c79356b | 3937 | { |
b0d623f7 | 3938 | while (!ISSET(bp->b_flags, B_DONE)) { |
1c79356b | 3939 | |
b0d623f7 | 3940 | lck_mtx_lock_spin(buf_mtxp); |
91447636 | 3941 | |
b0d623f7 A |
3942 | if (!ISSET(bp->b_flags, B_DONE)) { |
3943 | DTRACE_IO1(wait__start, buf_t, bp); | |
3944 | (void) msleep(bp, buf_mtxp, PDROP | (PRIBIO+1), "buf_biowait", NULL); | |
3945 | DTRACE_IO1(wait__done, buf_t, bp); | |
3946 | } else | |
3947 | lck_mtx_unlock(buf_mtxp); | |
3948 | } | |
1c79356b A |
3949 | /* check for interruption of I/O (e.g. via NFS), then errors. */ |
3950 | if (ISSET(bp->b_flags, B_EINTR)) { | |
3951 | CLR(bp->b_flags, B_EINTR); | |
3952 | return (EINTR); | |
3953 | } else if (ISSET(bp->b_flags, B_ERROR)) | |
3954 | return (bp->b_error ? bp->b_error : EIO); | |
3955 | else | |
3956 | return (0); | |
3957 | } | |
3958 | ||
2d21ac55 | 3959 | |
1c79356b A |
3960 | /* |
3961 | * Mark I/O complete on a buffer. | |
3962 | * | |
3963 | * If a callback has been requested, e.g. the pageout | |
3964 | * daemon, do so. Otherwise, awaken waiting processes. | |
3965 | * | |
3966 | * [ Leffler, et al., says on p.247: | |
3967 | * "This routine wakes up the blocked process, frees the buffer | |
3968 | * for an asynchronous write, or, for a request by the pagedaemon | |
3969 | * process, invokes a procedure specified in the buffer structure" ] | |
3970 | * | |
3971 | * In real life, the pagedaemon (or other system processes) wants | |
91447636 | 3972 | * to do async stuff to, and doesn't want the buffer buf_brelse()'d. |
1c79356b A |
3973 | * (for swap pager, that puts swap buffers on the free lists (!!!), |
3974 | * for the vn device, that puts malloc'd buffers on the free lists!) | |
3975 | */ | |
91447636 | 3976 | |
1c79356b | 3977 | void |
91447636 | 3978 | buf_biodone(buf_t bp) |
1c79356b | 3979 | { |
b0d623f7 | 3980 | mount_t mp; |
39236c6e | 3981 | struct bufattr *bap; |
5ba3f43e A |
3982 | struct timeval real_elapsed; |
3983 | uint64_t real_elapsed_usec = 0; | |
b0d623f7 | 3984 | |
1c79356b | 3985 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 387)) | DBG_FUNC_START, |
b0d623f7 | 3986 | bp, bp->b_datap, bp->b_flags, 0, 0); |
1c79356b A |
3987 | |
3988 | if (ISSET(bp->b_flags, B_DONE)) | |
3989 | panic("biodone already"); | |
1c79356b | 3990 | |
39236c6e A |
3991 | bap = &bp->b_attr; |
3992 | ||
b0d623f7 A |
3993 | if (bp->b_vp && bp->b_vp->v_mount) { |
3994 | mp = bp->b_vp->v_mount; | |
3995 | } else { | |
3996 | mp = NULL; | |
3997 | } | |
3998 | ||
d190cdc3 A |
3999 | if (ISSET(bp->b_flags, B_ERROR)) { |
4000 | if (mp && (MNT_ROOTFS & mp->mnt_flag)) { | |
4001 | dk_error_description_t desc; | |
4002 | bzero(&desc, sizeof(desc)); | |
4003 | desc.description = panic_disk_error_description; | |
4004 | desc.description_size = panic_disk_error_description_size; | |
4005 | VNOP_IOCTL(mp->mnt_devvp, DKIOCGETERRORDESCRIPTION, (caddr_t)&desc, 0, vfs_context_kernel()); | |
4006 | } | |
4007 | } | |
4008 | ||
b0d623f7 A |
4009 | if (mp && (bp->b_flags & B_READ) == 0) { |
4010 | update_last_io_time(mp); | |
4011 | INCR_PENDING_IO(-(pending_io_t)buf_count(bp), mp->mnt_pending_write_size); | |
4012 | } else if (mp) { | |
4013 | INCR_PENDING_IO(-(pending_io_t)buf_count(bp), mp->mnt_pending_read_size); | |
e2fac8b1 A |
4014 | } |
4015 | ||
39037602 A |
4016 | throttle_info_end_io(bp); |
4017 | ||
39236c6e A |
4018 | if (kdebug_enable) { |
4019 | int code = DKIO_DONE; | |
4020 | int io_tier = GET_BUFATTR_IO_TIER(bap); | |
9bccf70c | 4021 | |
91447636 A |
4022 | if (bp->b_flags & B_READ) |
4023 | code |= DKIO_READ; | |
4024 | if (bp->b_flags & B_ASYNC) | |
4025 | code |= DKIO_ASYNC; | |
9bccf70c | 4026 | |
91447636 A |
4027 | if (bp->b_flags & B_META) |
4028 | code |= DKIO_META; | |
4029 | else if (bp->b_flags & B_PAGEIO) | |
4030 | code |= DKIO_PAGING; | |
9bccf70c | 4031 | |
39236c6e | 4032 | if (io_tier != 0) |
6d2010ae | 4033 | code |= DKIO_THROTTLE; |
39236c6e A |
4034 | |
4035 | code |= ((io_tier << DKIO_TIER_SHIFT) & DKIO_TIER_MASK); | |
4036 | ||
4037 | if (bp->b_flags & B_PASSIVE) | |
6d2010ae A |
4038 | code |= DKIO_PASSIVE; |
4039 | ||
39236c6e | 4040 | if (bap->ba_flags & BA_NOCACHE) |
316670eb A |
4041 | code |= DKIO_NOCACHE; |
4042 | ||
d190cdc3 A |
4043 | if (bap->ba_flags & BA_IO_TIER_UPGRADE) { |
4044 | code |= DKIO_TIER_UPGRADE; | |
4045 | } | |
4046 | ||
316670eb | 4047 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE, |
39236c6e | 4048 | buf_kernel_addrperm_addr(bp), (uintptr_t)VM_KERNEL_ADDRPERM(bp->b_vp), bp->b_resid, bp->b_error, 0); |
9bccf70c | 4049 | } |
6d2010ae | 4050 | |
5ba3f43e A |
4051 | microuptime(&real_elapsed); |
4052 | timevalsub(&real_elapsed, &bp->b_timestamp_tv); | |
4053 | real_elapsed_usec = real_elapsed.tv_sec * USEC_PER_SEC + real_elapsed.tv_usec; | |
4054 | disk_conditioner_delay(bp, 1, bp->b_bcount, real_elapsed_usec); | |
4055 | ||
91447636 A |
4056 | /* |
4057 | * I/O was done, so don't believe | |
6d2010ae A |
4058 | * the DIRTY state from VM anymore... |
4059 | * and we need to reset the THROTTLED/PASSIVE | |
4060 | * indicators | |
91447636 | 4061 | */ |
39236c6e | 4062 | CLR(bp->b_flags, (B_WASDIRTY | B_PASSIVE)); |
d190cdc3 | 4063 | CLR(bap->ba_flags, (BA_META | BA_NOCACHE | BA_DELAYIDLESLEEP | BA_IO_TIER_UPGRADE)); |
39236c6e A |
4064 | |
4065 | SET_BUFATTR_IO_TIER(bap, 0); | |
4066 | ||
2d21ac55 | 4067 | DTRACE_IO1(done, buf_t, bp); |
b4c24cb9 | 4068 | |
91447636 A |
4069 | if (!ISSET(bp->b_flags, B_READ) && !ISSET(bp->b_flags, B_RAW)) |
4070 | /* | |
4071 | * wake up any writer's blocked | |
4072 | * on throttle or waiting for I/O | |
4073 | * to drain | |
4074 | */ | |
4075 | vnode_writedone(bp->b_vp); | |
4076 | ||
4077 | if (ISSET(bp->b_flags, (B_CALL | B_FILTER))) { /* if necessary, call out */ | |
4078 | void (*iodone_func)(struct buf *, void *) = bp->b_iodone; | |
6d2010ae | 4079 | void *arg = bp->b_transaction; |
91447636 A |
4080 | int callout = ISSET(bp->b_flags, B_CALL); |
4081 | ||
6d2010ae A |
4082 | if (iodone_func == NULL) |
4083 | panic("biodone: bp @ %p has NULL b_iodone!\n", bp); | |
4084 | ||
91447636 | 4085 | CLR(bp->b_flags, (B_CALL | B_FILTER)); /* filters and callouts are one-shot */ |
b4c24cb9 | 4086 | bp->b_iodone = NULL; |
91447636 | 4087 | bp->b_transaction = NULL; |
b4c24cb9 | 4088 | |
6d2010ae A |
4089 | if (callout) |
4090 | SET(bp->b_flags, B_DONE); /* note that it's done */ | |
2d21ac55 | 4091 | |
6d2010ae A |
4092 | (*iodone_func)(bp, arg); |
4093 | ||
4094 | if (callout) { | |
4095 | /* | |
2d21ac55 | 4096 | * assumes that the callback function takes |
91447636 A |
4097 | * ownership of the bp and deals with releasing it if necessary |
4098 | */ | |
2d21ac55 A |
4099 | goto biodone_done; |
4100 | } | |
91447636 A |
4101 | /* |
4102 | * in this case the call back function is acting | |
4103 | * strictly as a filter... it does not take | |
4104 | * ownership of the bp and is expecting us | |
4105 | * to finish cleaning up... this is currently used | |
4106 | * by the HFS journaling code | |
4107 | */ | |
1c79356b | 4108 | } |
91447636 A |
4109 | if (ISSET(bp->b_flags, B_ASYNC)) { /* if async, release it */ |
4110 | SET(bp->b_flags, B_DONE); /* note that it's done */ | |
1c79356b | 4111 | |
91447636 A |
4112 | buf_brelse(bp); |
4113 | } else { /* or just wakeup the buffer */ | |
4114 | /* | |
4115 | * by taking the mutex, we serialize | |
4116 | * the buf owner calling buf_biowait so that we'll | |
4117 | * only see him in one of 2 states... | |
4118 | * state 1: B_DONE wasn't set and he's | |
4119 | * blocked in msleep | |
4120 | * state 2: he's blocked trying to take the | |
4121 | * mutex before looking at B_DONE | |
4122 | * BL_WANTED is cleared in case anyone else | |
4123 | * is blocked waiting for the buffer... note | |
4124 | * that we haven't cleared B_BUSY yet, so if | |
4125 | * they do get to run, their going to re-set | |
4126 | * BL_WANTED and go back to sleep | |
4127 | */ | |
2d21ac55 | 4128 | lck_mtx_lock_spin(buf_mtxp); |
1c79356b | 4129 | |
91447636 A |
4130 | CLR(bp->b_lflags, BL_WANTED); |
4131 | SET(bp->b_flags, B_DONE); /* note that it's done */ | |
4132 | ||
4133 | lck_mtx_unlock(buf_mtxp); | |
4134 | ||
4135 | wakeup(bp); | |
4136 | } | |
4137 | biodone_done: | |
4138 | KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 387)) | DBG_FUNC_END, | |
b0d623f7 | 4139 | (uintptr_t)bp, (uintptr_t)bp->b_datap, bp->b_flags, 0, 0); |
1c79356b A |
4140 | } |
4141 | ||
39236c6e A |
4142 | /* |
4143 | * Obfuscate buf pointers. | |
4144 | */ | |
4145 | vm_offset_t | |
4146 | buf_kernel_addrperm_addr(void * addr) | |
4147 | { | |
4148 | if ((vm_offset_t)addr == 0) | |
4149 | return 0; | |
4150 | else | |
4151 | return ((vm_offset_t)addr + buf_kernel_addrperm); | |
4152 | } | |
4153 | ||
1c79356b A |
4154 | /* |
4155 | * Return a count of buffers on the "locked" queue. | |
4156 | */ | |
4157 | int | |
91447636 | 4158 | count_lock_queue(void) |
1c79356b | 4159 | { |
91447636 A |
4160 | buf_t bp; |
4161 | int n = 0; | |
4162 | ||
b0d623f7 | 4163 | lck_mtx_lock_spin(buf_mtxp); |
1c79356b A |
4164 | |
4165 | for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; | |
4166 | bp = bp->b_freelist.tqe_next) | |
4167 | n++; | |
91447636 A |
4168 | lck_mtx_unlock(buf_mtxp); |
4169 | ||
1c79356b A |
4170 | return (n); |
4171 | } | |
4172 | ||
4173 | /* | |
4174 | * Return a count of 'busy' buffers. Used at the time of shutdown. | |
316670eb | 4175 | * note: This is also called from the mach side in debug context in kdp.c |
1c79356b A |
4176 | */ |
4177 | int | |
91447636 | 4178 | count_busy_buffers(void) |
1c79356b | 4179 | { |
b0d623f7 | 4180 | return buf_busycount + bufstats.bufs_iobufinuse; |
1c79356b A |
4181 | } |
4182 | ||
9bccf70c | 4183 | #if DIAGNOSTIC |
1c79356b A |
4184 | /* |
4185 | * Print out statistics on the current allocation of the buffer pool. | |
4186 | * Can be enabled to print out on every ``sync'' by setting "syncprt" | |
4187 | * in vfs_syscalls.c using sysctl. | |
4188 | */ | |
4189 | void | |
4190 | vfs_bufstats() | |
4191 | { | |
91447636 | 4192 | int i, j, count; |
6d2010ae A |
4193 | struct buf *bp; |
4194 | struct bqueues *dp; | |
91447636 A |
4195 | int counts[MAXBSIZE/CLBYTES+1]; |
4196 | static char *bname[BQUEUES] = | |
4197 | { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" }; | |
4198 | ||
4199 | for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) { | |
4200 | count = 0; | |
4201 | for (j = 0; j <= MAXBSIZE/CLBYTES; j++) | |
4202 | counts[j] = 0; | |
4203 | ||
4204 | lck_mtx_lock(buf_mtxp); | |
4205 | ||
4206 | for (bp = dp->tqh_first; bp; bp = bp->b_freelist.tqe_next) { | |
4207 | counts[bp->b_bufsize/CLBYTES]++; | |
4208 | count++; | |
4209 | } | |
4210 | lck_mtx_unlock(buf_mtxp); | |
4211 | ||
4212 | printf("%s: total-%d", bname[i], count); | |
4213 | for (j = 0; j <= MAXBSIZE/CLBYTES; j++) | |
4214 | if (counts[j] != 0) | |
4215 | printf(", %d-%d", j * CLBYTES, counts[j]); | |
4216 | printf("\n"); | |
4217 | } | |
4218 | } | |
4219 | #endif /* DIAGNOSTIC */ | |
4220 | ||
6d2010ae | 4221 | #define NRESERVEDIOBUFS 128 |
91447636 | 4222 | |
39037602 A |
4223 | #define MNT_VIRTUALDEV_MAX_IOBUFS 16 |
4224 | #define VIRTUALDEV_MAX_IOBUFS ((40*niobuf_headers)/100) | |
91447636 A |
4225 | |
4226 | buf_t | |
4227 | alloc_io_buf(vnode_t vp, int priv) | |
4228 | { | |
4229 | buf_t bp; | |
39037602 A |
4230 | mount_t mp = NULL; |
4231 | int alloc_for_virtualdev = FALSE; | |
91447636 | 4232 | |
b0d623f7 | 4233 | lck_mtx_lock_spin(iobuffer_mtxp); |
91447636 | 4234 | |
39037602 A |
4235 | /* |
4236 | * We subject iobuf requests for diskimages to additional restrictions. | |
4237 | * | |
4238 | * a) A single diskimage mount cannot use up more than | |
4239 | * MNT_VIRTUALDEV_MAX_IOBUFS. However,vm privileged (pageout) requests | |
4240 | * are not subject to this restriction. | |
4241 | * b) iobuf headers used by all diskimage headers by all mount | |
4242 | * points cannot exceed VIRTUALDEV_MAX_IOBUFS. | |
4243 | */ | |
4244 | if (vp && ((mp = vp->v_mount)) && mp != dead_mountp && | |
4245 | mp->mnt_kern_flag & MNTK_VIRTUALDEV) { | |
4246 | alloc_for_virtualdev = TRUE; | |
4247 | while ((!priv && mp->mnt_iobufinuse > MNT_VIRTUALDEV_MAX_IOBUFS) || | |
4248 | bufstats.bufs_iobufinuse_vdev > VIRTUALDEV_MAX_IOBUFS) { | |
4249 | bufstats.bufs_iobufsleeps++; | |
4250 | ||
4251 | need_iobuffer = 1; | |
4252 | (void)msleep(&need_iobuffer, iobuffer_mtxp, | |
4253 | PSPIN | (PRIBIO+1), (const char *)"alloc_io_buf (1)", | |
4254 | NULL); | |
4255 | } | |
4256 | } | |
4257 | ||
2d21ac55 | 4258 | while (((niobuf_headers - NRESERVEDIOBUFS < bufstats.bufs_iobufinuse) && !priv) || |
91447636 A |
4259 | (bp = iobufqueue.tqh_first) == NULL) { |
4260 | bufstats.bufs_iobufsleeps++; | |
4261 | ||
4262 | need_iobuffer = 1; | |
39037602 A |
4263 | (void)msleep(&need_iobuffer, iobuffer_mtxp, PSPIN | (PRIBIO+1), |
4264 | (const char *)"alloc_io_buf (2)", NULL); | |
91447636 A |
4265 | } |
4266 | TAILQ_REMOVE(&iobufqueue, bp, b_freelist); | |
4267 | ||
4268 | bufstats.bufs_iobufinuse++; | |
4269 | if (bufstats.bufs_iobufinuse > bufstats.bufs_iobufmax) | |
4270 | bufstats.bufs_iobufmax = bufstats.bufs_iobufinuse; | |
4271 | ||
39037602 A |
4272 | if (alloc_for_virtualdev) { |
4273 | mp->mnt_iobufinuse++; | |
4274 | bufstats.bufs_iobufinuse_vdev++; | |
4275 | } | |
4276 | ||
91447636 A |
4277 | lck_mtx_unlock(iobuffer_mtxp); |
4278 | ||
4279 | /* | |
4280 | * initialize various fields | |
4281 | * we don't need to hold the mutex since the buffer | |
4282 | * is now private... the vp should have a reference | |
4283 | * on it and is not protected by this mutex in any event | |
4284 | */ | |
4285 | bp->b_timestamp = 0; | |
4286 | bp->b_proc = NULL; | |
4287 | ||
4288 | bp->b_datap = 0; | |
4289 | bp->b_flags = 0; | |
4290 | bp->b_lflags = BL_BUSY | BL_IOBUF; | |
39037602 A |
4291 | if (alloc_for_virtualdev) |
4292 | bp->b_lflags |= BL_IOBUF_VDEV; | |
6d2010ae | 4293 | bp->b_redundancy_flags = 0; |
91447636 A |
4294 | bp->b_blkno = bp->b_lblkno = 0; |
4295 | #ifdef JOE_DEBUG | |
4296 | bp->b_owner = current_thread(); | |
4297 | bp->b_tag = 6; | |
4298 | #endif | |
4299 | bp->b_iodone = NULL; | |
4300 | bp->b_error = 0; | |
4301 | bp->b_resid = 0; | |
4302 | bp->b_bcount = 0; | |
4303 | bp->b_bufsize = 0; | |
4304 | bp->b_upl = NULL; | |
3e170ce0 | 4305 | bp->b_fsprivate = (void *)NULL; |
91447636 | 4306 | bp->b_vp = vp; |
7ddcb079 | 4307 | bzero(&bp->b_attr, sizeof(struct bufattr)); |
91447636 A |
4308 | |
4309 | if (vp && (vp->v_type == VBLK || vp->v_type == VCHR)) | |
4310 | bp->b_dev = vp->v_rdev; | |
4311 | else | |
4312 | bp->b_dev = NODEV; | |
4313 | ||
4314 | return (bp); | |
4315 | } | |
4316 | ||
4317 | ||
4318 | void | |
4319 | free_io_buf(buf_t bp) | |
4320 | { | |
39037602 A |
4321 | int need_wakeup = 0; |
4322 | int free_for_virtualdev = FALSE; | |
4323 | mount_t mp = NULL; | |
4324 | ||
4325 | /* Was this iobuf for a diskimage ? */ | |
4326 | if (bp->b_lflags & BL_IOBUF_VDEV) { | |
4327 | free_for_virtualdev = TRUE; | |
4328 | if (bp->b_vp) | |
4329 | mp = bp->b_vp->v_mount; | |
4330 | } | |
91447636 A |
4331 | |
4332 | /* | |
4333 | * put buffer back on the head of the iobufqueue | |
4334 | */ | |
4335 | bp->b_vp = NULL; | |
4336 | bp->b_flags = B_INVAL; | |
4337 | ||
fe8ab488 A |
4338 | /* Zero out the bufattr and its flags before relinquishing this iobuf */ |
4339 | bzero (&bp->b_attr, sizeof(struct bufattr)); | |
4340 | ||
2d21ac55 | 4341 | lck_mtx_lock_spin(iobuffer_mtxp); |
91447636 A |
4342 | |
4343 | binsheadfree(bp, &iobufqueue, -1); | |
4344 | ||
4345 | if (need_iobuffer) { | |
4346 | /* | |
4347 | * Wake up any processes waiting because they need an io buffer | |
4348 | * | |
4349 | * do the wakeup after we drop the mutex... it's possible that the | |
4350 | * wakeup will be superfluous if need_iobuffer gets set again and | |
4351 | * another thread runs this path, but it's highly unlikely, doesn't | |
4352 | * hurt, and it means we don't hold up I/O progress if the wakeup blocks | |
4353 | * trying to grab a task related lock... | |
4354 | */ | |
4355 | need_iobuffer = 0; | |
4356 | need_wakeup = 1; | |
4357 | } | |
b0d623f7 A |
4358 | if (bufstats.bufs_iobufinuse <= 0) |
4359 | panic("free_io_buf: bp(%p) - bufstats.bufs_iobufinuse < 0", bp); | |
4360 | ||
91447636 A |
4361 | bufstats.bufs_iobufinuse--; |
4362 | ||
39037602 A |
4363 | if (free_for_virtualdev) { |
4364 | bufstats.bufs_iobufinuse_vdev--; | |
4365 | if (mp && mp != dead_mountp) | |
4366 | mp->mnt_iobufinuse--; | |
4367 | } | |
4368 | ||
91447636 A |
4369 | lck_mtx_unlock(iobuffer_mtxp); |
4370 | ||
4371 | if (need_wakeup) | |
4372 | wakeup(&need_iobuffer); | |
4373 | } | |
4374 | ||
4375 | ||
2d21ac55 A |
4376 | void |
4377 | buf_list_lock(void) | |
4378 | { | |
b0d623f7 | 4379 | lck_mtx_lock_spin(buf_mtxp); |
2d21ac55 A |
4380 | } |
4381 | ||
4382 | void | |
4383 | buf_list_unlock(void) | |
4384 | { | |
4385 | lck_mtx_unlock(buf_mtxp); | |
4386 | } | |
91447636 A |
4387 | |
4388 | /* | |
4389 | * If getnewbuf() calls bcleanbuf() on the same thread | |
4390 | * there is a potential for stack overrun and deadlocks. | |
4391 | * So we always handoff the work to a worker thread for completion | |
4392 | */ | |
91447636 A |
4393 | |
4394 | ||
4395 | static void | |
4396 | bcleanbuf_thread_init(void) | |
4397 | { | |
b0d623f7 A |
4398 | thread_t thread = THREAD_NULL; |
4399 | ||
91447636 | 4400 | /* create worker thread */ |
b0d623f7 A |
4401 | kernel_thread_start((thread_continue_t)bcleanbuf_thread, NULL, &thread); |
4402 | thread_deallocate(thread); | |
91447636 A |
4403 | } |
4404 | ||
6d2010ae A |
4405 | typedef int (*bcleanbufcontinuation)(int); |
4406 | ||
39037602 | 4407 | __attribute__((noreturn)) |
91447636 A |
4408 | static void |
4409 | bcleanbuf_thread(void) | |
4410 | { | |
4411 | struct buf *bp; | |
4412 | int error = 0; | |
4413 | int loopcnt = 0; | |
4414 | ||
4415 | for (;;) { | |
b0d623f7 | 4416 | lck_mtx_lock_spin(buf_mtxp); |
91447636 | 4417 | |
b0d623f7 | 4418 | while ( (bp = TAILQ_FIRST(&bufqueues[BQ_LAUNDRY])) == NULL) { |
6d2010ae | 4419 | (void)msleep0(&bufqueues[BQ_LAUNDRY], buf_mtxp, PRIBIO|PDROP, "blaundry", 0, (bcleanbufcontinuation)bcleanbuf_thread); |
b0d623f7 | 4420 | } |
6d2010ae | 4421 | |
91447636 A |
4422 | /* |
4423 | * Remove from the queue | |
4424 | */ | |
4425 | bremfree_locked(bp); | |
2d21ac55 A |
4426 | |
4427 | /* | |
4428 | * Buffer is no longer on any free list | |
4429 | */ | |
4430 | SET(bp->b_lflags, BL_BUSY); | |
b0d623f7 | 4431 | buf_busycount++; |
2d21ac55 A |
4432 | |
4433 | #ifdef JOE_DEBUG | |
4434 | bp->b_owner = current_thread(); | |
4435 | bp->b_tag = 10; | |
4436 | #endif | |
91447636 A |
4437 | |
4438 | lck_mtx_unlock(buf_mtxp); | |
4439 | /* | |
4440 | * do the IO | |
4441 | */ | |
4442 | error = bawrite_internal(bp, 0); | |
4443 | ||
4444 | if (error) { | |
2d21ac55 A |
4445 | bp->b_whichq = BQ_LAUNDRY; |
4446 | bp->b_timestamp = buf_timestamp(); | |
4447 | ||
4448 | lck_mtx_lock_spin(buf_mtxp); | |
91447636 A |
4449 | |
4450 | binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY); | |
4451 | blaundrycnt++; | |
4452 | ||
6d2010ae | 4453 | /* we never leave a busy page on the laundry queue */ |
2d21ac55 | 4454 | CLR(bp->b_lflags, BL_BUSY); |
b0d623f7 | 4455 | buf_busycount--; |
2d21ac55 A |
4456 | #ifdef JOE_DEBUG |
4457 | bp->b_owner = current_thread(); | |
4458 | bp->b_tag = 11; | |
4459 | #endif | |
4460 | ||
91447636 | 4461 | lck_mtx_unlock(buf_mtxp); |
6d2010ae A |
4462 | |
4463 | if (loopcnt > MAXLAUNDRY) { | |
4464 | /* | |
4465 | * bawrite_internal() can return errors if we're throttled. If we've | |
4466 | * done several I/Os and failed, give the system some time to unthrottle | |
4467 | * the vnode | |
4468 | */ | |
4469 | (void)tsleep((void *)&bufqueues[BQ_LAUNDRY], PRIBIO, "blaundry", 1); | |
91447636 A |
4470 | loopcnt = 0; |
4471 | } else { | |
6d2010ae A |
4472 | /* give other threads a chance to run */ |
4473 | (void)thread_block(THREAD_CONTINUE_NULL); | |
91447636 A |
4474 | loopcnt++; |
4475 | } | |
4476 | } | |
4477 | } | |
4478 | } | |
4479 | ||
4480 | ||
4481 | static int | |
4482 | brecover_data(buf_t bp) | |
4483 | { | |
4484 | int upl_offset; | |
4485 | upl_t upl; | |
4486 | upl_page_info_t *pl; | |
4487 | kern_return_t kret; | |
4488 | vnode_t vp = bp->b_vp; | |
4489 | int upl_flags; | |
4490 | ||
4491 | ||
4492 | if ( !UBCINFOEXISTS(vp) || bp->b_bufsize == 0) | |
4493 | goto dump_buffer; | |
4494 | ||
4495 | upl_flags = UPL_PRECIOUS; | |
4496 | if (! (buf_flags(bp) & B_READ)) { | |
4497 | /* | |
4498 | * "write" operation: let the UPL subsystem know | |
4499 | * that we intend to modify the buffer cache pages we're | |
4500 | * gathering. | |
4501 | */ | |
4502 | upl_flags |= UPL_WILL_MODIFY; | |
4503 | } | |
4504 | ||
5ba3f43e | 4505 | kret = ubc_create_upl_kernel(vp, |
91447636 A |
4506 | ubc_blktooff(vp, bp->b_lblkno), |
4507 | bp->b_bufsize, | |
4508 | &upl, | |
4509 | &pl, | |
5ba3f43e A |
4510 | upl_flags, |
4511 | VM_KERN_MEMORY_FILE); | |
91447636 A |
4512 | if (kret != KERN_SUCCESS) |
4513 | panic("Failed to create UPL"); | |
4514 | ||
4515 | for (upl_offset = 0; upl_offset < bp->b_bufsize; upl_offset += PAGE_SIZE) { | |
4516 | ||
4517 | if (!upl_valid_page(pl, upl_offset / PAGE_SIZE) || !upl_dirty_page(pl, upl_offset / PAGE_SIZE)) { | |
4518 | ubc_upl_abort(upl, 0); | |
4519 | goto dump_buffer; | |
4520 | } | |
4521 | } | |
4522 | bp->b_upl = upl; | |
4523 | ||
b0d623f7 | 4524 | kret = ubc_upl_map(upl, (vm_offset_t *)&(bp->b_datap)); |
91447636 A |
4525 | |
4526 | if (kret != KERN_SUCCESS) | |
4527 | panic("getblk: ubc_upl_map() failed with (%d)", kret); | |
4528 | return (1); | |
4529 | ||
4530 | dump_buffer: | |
4531 | bp->b_bufsize = 0; | |
4532 | SET(bp->b_flags, B_INVAL); | |
4533 | buf_brelse(bp); | |
4534 | ||
4535 | return(0); | |
4536 | } | |
4537 | ||
813fb2f6 A |
4538 | int |
4539 | fs_buffer_cache_gc_register(void (* callout)(int, void *), void *context) | |
4540 | { | |
4541 | lck_mtx_lock(buf_gc_callout); | |
4542 | for (int i = 0; i < FS_BUFFER_CACHE_GC_CALLOUTS_MAX_SIZE; i++) { | |
4543 | if (fs_callouts[i].callout == NULL) { | |
4544 | fs_callouts[i].callout = callout; | |
4545 | fs_callouts[i].context = context; | |
4546 | lck_mtx_unlock(buf_gc_callout); | |
4547 | return 0; | |
4548 | } | |
4549 | } | |
4550 | ||
4551 | lck_mtx_unlock(buf_gc_callout); | |
4552 | return ENOMEM; | |
4553 | } | |
4554 | ||
4555 | int | |
4556 | fs_buffer_cache_gc_unregister(void (* callout)(int, void *), void *context) | |
4557 | { | |
4558 | lck_mtx_lock(buf_gc_callout); | |
4559 | for (int i = 0; i < FS_BUFFER_CACHE_GC_CALLOUTS_MAX_SIZE; i++) { | |
4560 | if (fs_callouts[i].callout == callout && | |
4561 | fs_callouts[i].context == context) { | |
4562 | fs_callouts[i].callout = NULL; | |
4563 | fs_callouts[i].context = NULL; | |
4564 | } | |
4565 | } | |
4566 | lck_mtx_unlock(buf_gc_callout); | |
4567 | return 0; | |
4568 | } | |
4569 | ||
4570 | static void | |
4571 | fs_buffer_cache_gc_dispatch_callouts(int all) | |
4572 | { | |
4573 | lck_mtx_lock(buf_gc_callout); | |
4574 | for(int i = 0; i < FS_BUFFER_CACHE_GC_CALLOUTS_MAX_SIZE; i++) { | |
4575 | if (fs_callouts[i].callout != NULL) { | |
4576 | fs_callouts[i].callout(all, fs_callouts[i].context); | |
4577 | } | |
4578 | } | |
4579 | lck_mtx_unlock(buf_gc_callout); | |
4580 | } | |
4581 | ||
b7266188 | 4582 | boolean_t |
0b4c1975 | 4583 | buffer_cache_gc(int all) |
b0d623f7 A |
4584 | { |
4585 | buf_t bp; | |
4586 | boolean_t did_large_zfree = FALSE; | |
6d2010ae | 4587 | boolean_t need_wakeup = FALSE; |
b0d623f7 | 4588 | int now = buf_timestamp(); |
316670eb | 4589 | uint32_t found = 0; |
6d2010ae | 4590 | struct bqueues privq; |
0b4c1975 A |
4591 | int thresh_hold = BUF_STALE_THRESHHOLD; |
4592 | ||
4593 | if (all) | |
4594 | thresh_hold = 0; | |
6d2010ae A |
4595 | /* |
4596 | * We only care about metadata (incore storage comes from zalloc()). | |
316670eb A |
4597 | * Unless "all" is set (used to evict meta data buffers in preparation |
4598 | * for deep sleep), we only evict up to BUF_MAX_GC_BATCH_SIZE buffers | |
fe8ab488 A |
4599 | * that have not been accessed in the last BUF_STALE_THRESHOLD seconds. |
4600 | * BUF_MAX_GC_BATCH_SIZE controls both the hold time of the global lock | |
4601 | * "buf_mtxp" and the length of time we spend compute bound in the GC | |
4602 | * thread which calls this function | |
6d2010ae A |
4603 | */ |
4604 | lck_mtx_lock(buf_mtxp); | |
316670eb | 4605 | |
6d2010ae A |
4606 | do { |
4607 | found = 0; | |
4608 | TAILQ_INIT(&privq); | |
4609 | need_wakeup = FALSE; | |
b0d623f7 | 4610 | |
6d2010ae A |
4611 | while (((bp = TAILQ_FIRST(&bufqueues[BQ_META]))) && |
4612 | (now > bp->b_timestamp) && | |
4613 | (now - bp->b_timestamp > thresh_hold) && | |
4614 | (found < BUF_MAX_GC_BATCH_SIZE)) { | |
4615 | ||
4616 | /* Remove from free list */ | |
4617 | bremfree_locked(bp); | |
4618 | found++; | |
4619 | ||
4620 | #ifdef JOE_DEBUG | |
4621 | bp->b_owner = current_thread(); | |
4622 | bp->b_tag = 12; | |
4623 | #endif | |
4624 | ||
4625 | /* If dirty, move to laundry queue and remember to do wakeup */ | |
4626 | if (ISSET(bp->b_flags, B_DELWRI)) { | |
4627 | SET(bp->b_lflags, BL_WANTDEALLOC); | |
4628 | ||
4629 | bmovelaundry(bp); | |
4630 | need_wakeup = TRUE; | |
4631 | ||
4632 | continue; | |
4633 | } | |
4634 | ||
4635 | /* | |
4636 | * Mark busy and put on private list. We could technically get | |
4637 | * away without setting BL_BUSY here. | |
4638 | */ | |
4639 | SET(bp->b_lflags, BL_BUSY); | |
4640 | buf_busycount++; | |
b0d623f7 | 4641 | |
6d2010ae A |
4642 | /* |
4643 | * Remove from hash and dissociate from vp. | |
4644 | */ | |
4645 | bremhash(bp); | |
4646 | if (bp->b_vp) { | |
4647 | brelvp_locked(bp); | |
4648 | } | |
b0d623f7 | 4649 | |
6d2010ae A |
4650 | TAILQ_INSERT_TAIL(&privq, bp, b_freelist); |
4651 | } | |
b0d623f7 | 4652 | |
6d2010ae A |
4653 | if (found == 0) { |
4654 | break; | |
4655 | } | |
b0d623f7 | 4656 | |
6d2010ae A |
4657 | /* Drop lock for batch processing */ |
4658 | lck_mtx_unlock(buf_mtxp); | |
4659 | ||
4660 | /* Wakeup and yield for laundry if need be */ | |
4661 | if (need_wakeup) { | |
4662 | wakeup(&bufqueues[BQ_LAUNDRY]); | |
4663 | (void)thread_block(THREAD_CONTINUE_NULL); | |
b0d623f7 | 4664 | } |
6d2010ae A |
4665 | |
4666 | /* Clean up every buffer on private list */ | |
4667 | TAILQ_FOREACH(bp, &privq, b_freelist) { | |
4668 | /* Take note if we've definitely freed at least a page to a zone */ | |
4669 | if ((ISSET(bp->b_flags, B_ZALLOC)) && (buf_size(bp) >= PAGE_SIZE)) { | |
4670 | did_large_zfree = TRUE; | |
4671 | } | |
4672 | ||
4673 | trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno); | |
4674 | ||
4675 | /* Free Storage */ | |
4676 | buf_free_meta_store(bp); | |
4677 | ||
4678 | /* Release credentials */ | |
4679 | buf_release_credentials(bp); | |
4680 | ||
4681 | /* Prepare for moving to empty queue */ | |
4682 | CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | |
4683 | | B_AGE | B_ASYNC | B_NOCACHE | B_FUA)); | |
4684 | bp->b_whichq = BQ_EMPTY; | |
4685 | BLISTNONE(bp); | |
4686 | } | |
6d2010ae A |
4687 | lck_mtx_lock(buf_mtxp); |
4688 | ||
4689 | /* Back under lock, move them all to invalid hash and clear busy */ | |
4690 | TAILQ_FOREACH(bp, &privq, b_freelist) { | |
4691 | binshash(bp, &invalhash); | |
4692 | CLR(bp->b_lflags, BL_BUSY); | |
4693 | buf_busycount--; | |
4694 | ||
4695 | #ifdef JOE_DEBUG | |
4696 | if (bp->b_owner != current_thread()) { | |
4697 | panic("Buffer stolen from buffer_cache_gc()"); | |
4698 | } | |
4699 | bp->b_owner = current_thread(); | |
4700 | bp->b_tag = 13; | |
4701 | #endif | |
4702 | } | |
4703 | ||
4704 | /* And do a big bulk move to the empty queue */ | |
4705 | TAILQ_CONCAT(&bufqueues[BQ_EMPTY], &privq, b_freelist); | |
6d2010ae | 4706 | |
316670eb | 4707 | } while (all && (found == BUF_MAX_GC_BATCH_SIZE)); |
b0d623f7 A |
4708 | |
4709 | lck_mtx_unlock(buf_mtxp); | |
4710 | ||
813fb2f6 A |
4711 | fs_buffer_cache_gc_dispatch_callouts(all); |
4712 | ||
b0d623f7 A |
4713 | return did_large_zfree; |
4714 | } | |
91447636 A |
4715 | |
4716 | ||
4717 | /* | |
4718 | * disabled for now | |
4719 | */ | |
4720 | ||
4721 | #if FLUSH_QUEUES | |
4722 | ||
4723 | #define NFLUSH 32 | |
4724 | ||
4725 | static int | |
4726 | bp_cmp(void *a, void *b) | |
4727 | { | |
4728 | buf_t *bp_a = *(buf_t **)a, | |
4729 | *bp_b = *(buf_t **)b; | |
4730 | daddr64_t res; | |
1c79356b | 4731 | |
91447636 A |
4732 | // don't have to worry about negative block |
4733 | // numbers so this is ok to do. | |
4734 | // | |
4735 | res = (bp_a->b_blkno - bp_b->b_blkno); | |
4736 | ||
4737 | return (int)res; | |
1c79356b | 4738 | } |
1c79356b A |
4739 | |
4740 | ||
91447636 A |
4741 | int |
4742 | bflushq(int whichq, mount_t mp) | |
1c79356b | 4743 | { |
91447636 A |
4744 | buf_t bp, next; |
4745 | int i, buf_count; | |
4746 | int total_writes = 0; | |
4747 | static buf_t flush_table[NFLUSH]; | |
1c79356b | 4748 | |
91447636 A |
4749 | if (whichq < 0 || whichq >= BQUEUES) { |
4750 | return (0); | |
0b4e3aa0 A |
4751 | } |
4752 | ||
91447636 A |
4753 | restart: |
4754 | lck_mtx_lock(buf_mtxp); | |
0b4e3aa0 | 4755 | |
91447636 | 4756 | bp = TAILQ_FIRST(&bufqueues[whichq]); |
1c79356b | 4757 | |
91447636 A |
4758 | for (buf_count = 0; bp; bp = next) { |
4759 | next = bp->b_freelist.tqe_next; | |
4760 | ||
4761 | if (bp->b_vp == NULL || bp->b_vp->v_mount != mp) { | |
4762 | continue; | |
4763 | } | |
b4c24cb9 | 4764 | |
91447636 | 4765 | if (ISSET(bp->b_flags, B_DELWRI) && !ISSET(bp->b_lflags, BL_BUSY)) { |
1c79356b | 4766 | |
91447636 A |
4767 | bremfree_locked(bp); |
4768 | #ifdef JOE_DEBUG | |
4769 | bp->b_owner = current_thread(); | |
4770 | bp->b_tag = 7; | |
4771 | #endif | |
4772 | SET(bp->b_lflags, BL_BUSY); | |
b0d623f7 A |
4773 | buf_busycount++; |
4774 | ||
91447636 A |
4775 | flush_table[buf_count] = bp; |
4776 | buf_count++; | |
4777 | total_writes++; | |
1c79356b | 4778 | |
91447636 A |
4779 | if (buf_count >= NFLUSH) { |
4780 | lck_mtx_unlock(buf_mtxp); | |
1c79356b | 4781 | |
91447636 | 4782 | qsort(flush_table, buf_count, sizeof(struct buf *), bp_cmp); |
1c79356b | 4783 | |
91447636 A |
4784 | for (i = 0; i < buf_count; i++) { |
4785 | buf_bawrite(flush_table[i]); | |
4786 | } | |
4787 | goto restart; | |
4788 | } | |
4789 | } | |
4790 | } | |
4791 | lck_mtx_unlock(buf_mtxp); | |
1c79356b | 4792 | |
91447636 A |
4793 | if (buf_count > 0) { |
4794 | qsort(flush_table, buf_count, sizeof(struct buf *), bp_cmp); | |
1c79356b | 4795 | |
91447636 A |
4796 | for (i = 0; i < buf_count; i++) { |
4797 | buf_bawrite(flush_table[i]); | |
4798 | } | |
1c79356b | 4799 | } |
91447636 A |
4800 | |
4801 | return (total_writes); | |
1c79356b | 4802 | } |
91447636 | 4803 | #endif |