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