2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfs_bio.c,v 1.44 1997/09/10 19:52:25 phk Exp $
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/resourcevar.h>
70 #include <sys/signalvar.h>
71 #include <sys/proc_internal.h>
72 #include <sys/kauth.h>
73 #include <sys/malloc.h>
74 #include <sys/vnode.h>
75 #include <sys/dirent.h>
76 #include <sys/mount_internal.h>
77 #include <sys/kernel.h>
78 #include <sys/sysctl.h>
79 #include <sys/ubc_internal.h>
80 #include <sys/uio_internal.h>
83 #include <sys/vmparam.h>
86 #include <kern/clock.h>
87 #include <libkern/OSAtomic.h>
88 #include <kern/kalloc.h>
90 #include <nfs/rpcv2.h>
91 #include <nfs/nfsproto.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/nfsnode.h>
95 #include <sys/buf_internal.h>
97 #include <sys/kdebug.h>
99 #define FSDBG(A, B, C, D, E) \
100 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
101 (int)(B), (int)(C), (int)(D), (int)(E), 0)
102 #define FSDBG_TOP(A, B, C, D, E) \
103 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
104 (int)(B), (int)(C), (int)(D), (int)(E), 0)
105 #define FSDBG_BOT(A, B, C, D, E) \
106 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
107 (int)(B), (int)(C), (int)(D), (int)(E), 0)
109 extern int nfs_numasync
;
110 extern int nfs_ioddelwri
;
111 extern struct nfsstats nfsstats
;
113 #define NFSBUFHASH(np, lbn) \
114 (&nfsbufhashtbl[((long)(np) / sizeof(*(np)) + (int)(lbn)) & nfsbufhash])
115 LIST_HEAD(nfsbufhashhead
, nfsbuf
) *nfsbufhashtbl
;
116 struct nfsbuffreehead nfsbuffree
, nfsbuffreemeta
, nfsbufdelwri
;
118 int nfsbufcnt
, nfsbufmin
, nfsbufmax
, nfsbufmetacnt
, nfsbufmetamax
;
119 int nfsbuffreecnt
, nfsbuffreemetacnt
, nfsbufdelwricnt
, nfsneedbuffer
;
121 time_t nfsbuffreeuptimestamp
;
123 lck_grp_t
*nfs_buf_lck_grp
;
124 lck_grp_attr_t
*nfs_buf_lck_grp_attr
;
125 lck_attr_t
*nfs_buf_lck_attr
;
126 lck_mtx_t
*nfs_buf_mutex
;
128 #define NFSBUFWRITE_THROTTLE 9
129 #define NFSBUF_LRU_STALE 120
130 #define NFSBUF_META_STALE 240
132 /* number of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffree list */
133 #define LRU_TO_FREEUP 6
134 /* number of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffreemeta list */
135 #define META_TO_FREEUP 3
136 /* total number of nfsbufs nfs_buf_freeup() should attempt to free */
137 #define TOTAL_TO_FREEUP (LRU_TO_FREEUP+META_TO_FREEUP)
138 /* fraction of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffree list when called from nfs_timer() */
139 #define LRU_FREEUP_FRAC_ON_TIMER 8
140 /* fraction of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffreemeta list when called from nfs_timer() */
141 #define META_FREEUP_FRAC_ON_TIMER 16
142 /* fraction of total nfsbufs that nfsbuffreecnt should exceed before bothering to call nfs_buf_freeup() */
143 #define LRU_FREEUP_MIN_FRAC 4
144 /* fraction of total nfsbufs that nfsbuffreemetacnt should exceed before bothering to call nfs_buf_freeup() */
145 #define META_FREEUP_MIN_FRAC 2
147 #define NFS_BUF_FREEUP() \
149 /* only call nfs_buf_freeup() if it has work to do: */ \
150 if (((nfsbuffreecnt > nfsbufcnt/LRU_FREEUP_MIN_FRAC) || \
151 (nfsbuffreemetacnt > nfsbufcnt/META_FREEUP_MIN_FRAC)) && \
152 ((nfsbufcnt - TOTAL_TO_FREEUP) > nfsbufmin)) \
157 * Initialize nfsbuf lists
162 nfs_buf_lck_grp_attr
= lck_grp_attr_alloc_init();
163 lck_grp_attr_setstat(nfs_buf_lck_grp_attr
);
164 nfs_buf_lck_grp
= lck_grp_alloc_init("nfs_buf", nfs_buf_lck_grp_attr
);
166 nfs_buf_lck_attr
= lck_attr_alloc_init();
168 nfs_buf_mutex
= lck_mtx_alloc_init(nfs_buf_lck_grp
, nfs_buf_lck_attr
);
170 nfsbufcnt
= nfsbufmetacnt
=
171 nfsbuffreecnt
= nfsbuffreemetacnt
= nfsbufdelwricnt
= 0;
173 nfsbufmax
= (sane_size
>> PAGE_SHIFT
) / 4;
174 nfsbufmetamax
= (sane_size
>> PAGE_SHIFT
) / 16;
177 nfsbuffreeuptimestamp
= 0;
179 nfsbufhashtbl
= hashinit(nfsbufmax
/4, M_TEMP
, &nfsbufhash
);
180 TAILQ_INIT(&nfsbuffree
);
181 TAILQ_INIT(&nfsbuffreemeta
);
182 TAILQ_INIT(&nfsbufdelwri
);
187 * try to free up some excess, unused nfsbufs
190 nfs_buf_freeup(int timer
)
195 struct nfsbuffreehead nfsbuffreeup
;
197 TAILQ_INIT(&nfsbuffreeup
);
199 lck_mtx_lock(nfs_buf_mutex
);
202 nfsbuffreeuptimestamp
= now
.tv_sec
;
204 FSDBG(320, nfsbufcnt
, nfsbuffreecnt
, nfsbuffreemetacnt
, 0);
206 count
= timer
? nfsbuffreecnt
/LRU_FREEUP_FRAC_ON_TIMER
: LRU_TO_FREEUP
;
207 while ((nfsbufcnt
> nfsbufmin
) && (count
-- > 0)) {
208 fbp
= TAILQ_FIRST(&nfsbuffree
);
213 if (NBUFSTAMPVALID(fbp
) &&
214 (fbp
->nb_timestamp
+ (2*NFSBUF_LRU_STALE
)) > now
.tv_sec
)
216 nfs_buf_remfree(fbp
);
217 /* disassociate buffer from any vnode */
219 if (fbp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
220 LIST_REMOVE(fbp
, nb_vnbufs
);
221 fbp
->nb_vnbufs
.le_next
= NFSNOLIST
;
225 LIST_REMOVE(fbp
, nb_hash
);
226 TAILQ_INSERT_TAIL(&nfsbuffreeup
, fbp
, nb_free
);
230 count
= timer
? nfsbuffreemetacnt
/META_FREEUP_FRAC_ON_TIMER
: META_TO_FREEUP
;
231 while ((nfsbufcnt
> nfsbufmin
) && (count
-- > 0)) {
232 fbp
= TAILQ_FIRST(&nfsbuffreemeta
);
237 if (NBUFSTAMPVALID(fbp
) &&
238 (fbp
->nb_timestamp
+ (2*NFSBUF_META_STALE
)) > now
.tv_sec
)
240 nfs_buf_remfree(fbp
);
241 /* disassociate buffer from any vnode */
243 if (fbp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
244 LIST_REMOVE(fbp
, nb_vnbufs
);
245 fbp
->nb_vnbufs
.le_next
= NFSNOLIST
;
249 LIST_REMOVE(fbp
, nb_hash
);
250 TAILQ_INSERT_TAIL(&nfsbuffreeup
, fbp
, nb_free
);
255 FSDBG(320, nfsbufcnt
, nfsbuffreecnt
, nfsbuffreemetacnt
, 0);
258 lck_mtx_unlock(nfs_buf_mutex
);
260 while ((fbp
= TAILQ_FIRST(&nfsbuffreeup
))) {
261 TAILQ_REMOVE(&nfsbuffreeup
, fbp
, nb_free
);
263 if (fbp
->nb_rcred
!= NOCRED
) {
264 kauth_cred_rele(fbp
->nb_rcred
);
265 fbp
->nb_rcred
= NOCRED
;
267 if (fbp
->nb_wcred
!= NOCRED
) {
268 kauth_cred_rele(fbp
->nb_wcred
);
269 fbp
->nb_wcred
= NOCRED
;
271 /* if buf was NB_META, dump buffer */
272 if (ISSET(fbp
->nb_flags
, NB_META
) && fbp
->nb_data
)
273 kfree(fbp
->nb_data
, fbp
->nb_bufsize
);
280 * remove a buffer from the freelist
281 * (must be called with nfs_buf_mutex held)
284 nfs_buf_remfree(struct nfsbuf
*bp
)
286 if (bp
->nb_free
.tqe_next
== NFSNOLIST
)
287 panic("nfsbuf not on free list");
288 if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
290 TAILQ_REMOVE(&nfsbufdelwri
, bp
, nb_free
);
291 } else if (ISSET(bp
->nb_flags
, NB_META
)) {
293 TAILQ_REMOVE(&nfsbuffreemeta
, bp
, nb_free
);
296 TAILQ_REMOVE(&nfsbuffree
, bp
, nb_free
);
298 bp
->nb_free
.tqe_next
= NFSNOLIST
;
303 * check for existence of nfsbuf in cache
306 nfs_buf_is_incore(vnode_t vp
, daddr64_t blkno
)
309 lck_mtx_lock(nfs_buf_mutex
);
310 if (nfs_buf_incore(vp
, blkno
))
314 lck_mtx_unlock(nfs_buf_mutex
);
319 * return incore buffer (must be called with nfs_buf_mutex held)
322 nfs_buf_incore(vnode_t vp
, daddr64_t blkno
)
324 /* Search hash chain */
325 struct nfsbuf
* bp
= NFSBUFHASH(VTONFS(vp
), blkno
)->lh_first
;
326 for (; bp
!= NULL
; bp
= bp
->nb_hash
.le_next
)
327 if (bp
->nb_lblkno
== blkno
&& bp
->nb_vp
== vp
) {
328 if (!ISSET(bp
->nb_flags
, NB_INVAL
)) {
329 FSDBG(547, bp
, blkno
, bp
->nb_flags
, bp
->nb_vp
);
337 * Check if it's OK to drop a page.
339 * Called by vnode_pager() on pageout request of non-dirty page.
340 * We need to make sure that it's not part of a delayed write.
341 * If it is, we can't let the VM drop it because we may need it
342 * later when/if we need to write the data (again).
345 nfs_buf_page_inval(vnode_t vp
, off_t offset
)
350 lck_mtx_lock(nfs_buf_mutex
);
351 bp
= nfs_buf_incore(vp
, ubc_offtoblk(vp
, offset
));
354 FSDBG(325, bp
, bp
->nb_flags
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
355 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
360 * If there's a dirty range in the buffer, check to
361 * see if this page intersects with the dirty range.
362 * If it does, we can't let the pager drop the page.
364 if (bp
->nb_dirtyend
> 0) {
365 int start
= offset
- NBOFF(bp
);
366 if (bp
->nb_dirtyend
<= start
||
367 bp
->nb_dirtyoff
>= (start
+ PAGE_SIZE
))
373 lck_mtx_unlock(nfs_buf_mutex
);
378 * set up the UPL for a buffer
379 * (must NOT be called with nfs_buf_mutex held)
382 nfs_buf_upl_setup(struct nfsbuf
*bp
)
388 if (ISSET(bp
->nb_flags
, NB_PAGELIST
))
391 upl_flags
= UPL_PRECIOUS
;
392 if (! ISSET(bp
->nb_flags
, NB_READ
)) {
394 * We're doing a "write", so we intend to modify
395 * the pages we're gathering.
397 upl_flags
|= UPL_WILL_MODIFY
;
399 kret
= ubc_create_upl(bp
->nb_vp
, NBOFF(bp
), bp
->nb_bufsize
,
400 &upl
, NULL
, upl_flags
);
401 if (kret
== KERN_INVALID_ARGUMENT
) {
402 /* vm object probably doesn't exist any more */
403 bp
->nb_pagelist
= NULL
;
406 if (kret
!= KERN_SUCCESS
) {
407 printf("nfs_buf_upl_setup(): failed to get pagelist %d\n", kret
);
408 bp
->nb_pagelist
= NULL
;
412 FSDBG(538, bp
, NBOFF(bp
), bp
->nb_bufsize
, bp
->nb_vp
);
414 bp
->nb_pagelist
= upl
;
415 SET(bp
->nb_flags
, NB_PAGELIST
);
420 * update buffer's valid/dirty info from UBC
421 * (must NOT be called with nfs_buf_mutex held)
424 nfs_buf_upl_check(struct nfsbuf
*bp
)
427 off_t filesize
, fileoffset
;
430 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
433 npages
= round_page_32(bp
->nb_bufsize
) / PAGE_SIZE
;
434 filesize
= ubc_getsize(bp
->nb_vp
);
435 fileoffset
= NBOFF(bp
);
436 if (fileoffset
< filesize
)
437 SET(bp
->nb_flags
, NB_CACHE
);
439 CLR(bp
->nb_flags
, NB_CACHE
);
441 pl
= ubc_upl_pageinfo(bp
->nb_pagelist
);
442 bp
->nb_valid
= bp
->nb_dirty
= 0;
444 for (i
=0; i
< npages
; i
++, fileoffset
+= PAGE_SIZE_64
) {
445 /* anything beyond the end of the file is not valid or dirty */
446 if (fileoffset
>= filesize
)
448 if (!upl_valid_page(pl
, i
)) {
449 CLR(bp
->nb_flags
, NB_CACHE
);
453 if (upl_dirty_page(pl
, i
)) {
454 NBPGDIRTY_SET(bp
, i
);
455 if (!ISSET(bp
->nb_flags
, NB_WASDIRTY
))
456 SET(bp
->nb_flags
, NB_WASDIRTY
);
459 fileoffset
= NBOFF(bp
);
460 if (ISSET(bp
->nb_flags
, NB_CACHE
)) {
462 bp
->nb_validend
= bp
->nb_bufsize
;
463 if (fileoffset
+ bp
->nb_validend
> filesize
)
464 bp
->nb_validend
= filesize
- fileoffset
;
466 bp
->nb_validoff
= bp
->nb_validend
= -1;
468 FSDBG(539, bp
, fileoffset
, bp
->nb_valid
, bp
->nb_dirty
);
469 FSDBG(539, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
473 * make sure that a buffer is mapped
474 * (must NOT be called with nfs_buf_mutex held)
477 nfs_buf_map(struct nfsbuf
*bp
)
483 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
486 kret
= ubc_upl_map(bp
->nb_pagelist
, (vm_address_t
*)&(bp
->nb_data
));
487 if (kret
!= KERN_SUCCESS
)
488 panic("nfs_buf_map: ubc_upl_map() failed with (%d)", kret
);
489 if (bp
->nb_data
== 0)
490 panic("ubc_upl_map mapped 0");
491 FSDBG(540, bp
, bp
->nb_flags
, NBOFF(bp
), bp
->nb_data
);
496 * check range of pages in nfsbuf's UPL for validity
499 nfs_buf_upl_valid_range(struct nfsbuf
*bp
, int off
, int size
)
501 off_t fileoffset
, filesize
;
505 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
507 pl
= ubc_upl_pageinfo(bp
->nb_pagelist
);
509 size
+= off
& PAGE_MASK
;
511 fileoffset
= NBOFF(bp
);
512 filesize
= VTONFS(bp
->nb_vp
)->n_size
;
513 if ((fileoffset
+ off
+ size
) > filesize
)
514 size
= filesize
- (fileoffset
+ off
);
517 lastpg
= (off
+ size
- 1)/PAGE_SIZE
;
518 while (pg
<= lastpg
) {
519 if (!upl_valid_page(pl
, pg
))
527 * normalize an nfsbuf's valid range
529 * the read/write code guarantees that we'll always have a valid
530 * region that is an integral number of pages. If either end
531 * of the valid range isn't page-aligned, it gets corrected
532 * here as we extend the valid range through all of the
533 * contiguous valid pages.
536 nfs_buf_normalize_valid_range(struct nfsnode
*np
, struct nfsbuf
*bp
)
539 /* pull validoff back to start of contiguous valid page range */
540 pg
= bp
->nb_validoff
/PAGE_SIZE
;
541 while (pg
>= 0 && NBPGVALID(bp
,pg
))
543 bp
->nb_validoff
= (pg
+1) * PAGE_SIZE
;
544 /* push validend forward to end of contiguous valid page range */
545 npg
= bp
->nb_bufsize
/PAGE_SIZE
;
546 pg
= bp
->nb_validend
/PAGE_SIZE
;
547 while (pg
< npg
&& NBPGVALID(bp
,pg
))
549 bp
->nb_validend
= pg
* PAGE_SIZE
;
551 if (NBOFF(bp
) + bp
->nb_validend
> (off_t
)np
->n_size
)
552 bp
->nb_validend
= np
->n_size
% bp
->nb_bufsize
;
556 * try to push out some delayed/uncommitted writes
557 * ("locked" indicates whether nfs_buf_mutex is already held)
560 nfs_buf_delwri_push(int locked
)
565 if (TAILQ_EMPTY(&nfsbufdelwri
))
568 /* first try to tell the nfsiods to do it */
569 if (nfs_asyncio(NULL
, NULL
) == 0)
572 /* otherwise, try to do some of the work ourselves */
575 lck_mtx_lock(nfs_buf_mutex
);
576 while (i
< 8 && (bp
= TAILQ_FIRST(&nfsbufdelwri
)) != NULL
) {
577 struct nfsnode
*np
= VTONFS(bp
->nb_vp
);
580 while ((error
= nfs_buf_acquire(bp
, 0, 0, 0)) == EAGAIN
);
585 /* buffer is no longer valid */
589 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
590 nfs_buf_check_write_verifier(np
, bp
);
591 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
592 /* put buffer at end of delwri list */
593 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
596 lck_mtx_unlock(nfs_buf_mutex
);
597 nfs_flushcommits(np
->n_vnode
, NULL
, 1);
599 SET(bp
->nb_flags
, NB_ASYNC
);
600 lck_mtx_unlock(nfs_buf_mutex
);
604 lck_mtx_lock(nfs_buf_mutex
);
607 lck_mtx_unlock(nfs_buf_mutex
);
613 * Returns errno on error, 0 otherwise.
614 * Any buffer is returned in *bpp.
616 * If NBLK_ONLYVALID is set, only return buffer if found in cache.
617 * If NBLK_NOWAIT is set, don't wait for the buffer if it's marked BUSY.
619 * Check for existence of buffer in cache.
620 * Or attempt to reuse a buffer from one of the free lists.
621 * Or allocate a new buffer if we haven't already hit max allocation.
622 * Or wait for a free buffer.
624 * If available buffer found, prepare it, and return it.
626 * If the calling process is interrupted by a signal for
627 * an interruptible mount point, return EINTR.
638 struct nfsnode
*np
= VTONFS(vp
);
640 int biosize
, bufsize
;
642 int slpflag
= PCATCH
;
643 int operation
= (flags
& NBLK_OPMASK
);
647 FSDBG_TOP(541, vp
, blkno
, size
, flags
);
651 if (bufsize
> MAXBSIZE
)
652 panic("nfs_buf_get: buffer larger than MAXBSIZE requested");
654 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
656 if (UBCINVALID(vp
) || !UBCINFOEXISTS(vp
)) {
657 operation
= NBLK_META
;
658 } else if (bufsize
< biosize
) {
659 /* reg files should always have biosize blocks */
663 /* if NBLK_WRITE, check for too many delayed/uncommitted writes */
664 if ((operation
== NBLK_WRITE
) && (nfs_nbdwrite
> ((nfsbufcnt
*3)/4))) {
665 FSDBG_TOP(542, vp
, blkno
, nfs_nbdwrite
, ((nfsbufcnt
*3)/4));
667 /* poke the delwri list */
668 nfs_buf_delwri_push(0);
670 /* sleep to let other threads run... */
671 tsleep(&nfs_nbdwrite
, PCATCH
, "nfs_nbdwrite", 1);
672 FSDBG_BOT(542, vp
, blkno
, nfs_nbdwrite
, ((nfsbufcnt
*3)/4));
676 lck_mtx_lock(nfs_buf_mutex
);
678 /* check for existence of nfsbuf in cache */
679 if ((bp
= nfs_buf_incore(vp
, blkno
))) {
680 /* if busy, set wanted and wait */
681 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
682 if (flags
& NBLK_NOWAIT
) {
683 lck_mtx_unlock(nfs_buf_mutex
);
684 FSDBG_BOT(541, vp
, blkno
, bp
, 0xbcbcbcbc);
687 FSDBG_TOP(543, vp
, blkno
, bp
, bp
->nb_flags
);
688 SET(bp
->nb_lflags
, NBL_WANTED
);
692 msleep(bp
, nfs_buf_mutex
, slpflag
|(PRIBIO
+1)|PDROP
,
693 "nfsbufget", (slpflag
== PCATCH
) ? 0 : &ts
);
695 FSDBG_BOT(543, vp
, blkno
, bp
, bp
->nb_flags
);
696 if ((error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
697 FSDBG_BOT(541, vp
, blkno
, 0, error
);
702 if (bp
->nb_bufsize
!= bufsize
)
703 panic("nfsbuf size mismatch");
704 SET(bp
->nb_lflags
, NBL_BUSY
);
705 SET(bp
->nb_flags
, NB_CACHE
);
707 /* additional paranoia: */
708 if (ISSET(bp
->nb_flags
, NB_PAGELIST
))
709 panic("pagelist buffer was not busy");
713 if (flags
& NBLK_ONLYVALID
) {
714 lck_mtx_unlock(nfs_buf_mutex
);
715 FSDBG_BOT(541, vp
, blkno
, 0, 0x0000cace);
720 * where to get a free buffer:
721 * - if meta and maxmeta reached, must reuse meta
722 * - alloc new if we haven't reached min bufs
723 * - if free lists are NOT empty
724 * - if free list is stale, use it
725 * - else if freemeta list is stale, use it
726 * - else if max bufs allocated, use least-time-to-stale
727 * - alloc new if we haven't reached max allowed
728 * - start clearing out delwri list and try again
731 if ((operation
== NBLK_META
) && (nfsbufmetacnt
>= nfsbufmetamax
)) {
732 /* if we've hit max meta buffers, must reuse a meta buffer */
733 bp
= TAILQ_FIRST(&nfsbuffreemeta
);
734 } else if ((nfsbufcnt
> nfsbufmin
) &&
735 (!TAILQ_EMPTY(&nfsbuffree
) || !TAILQ_EMPTY(&nfsbuffreemeta
))) {
736 /* try to pull an nfsbuf off a free list */
737 struct nfsbuf
*lrubp
, *metabp
;
741 /* if the next LRU or META buffer is invalid or stale, use it */
742 lrubp
= TAILQ_FIRST(&nfsbuffree
);
743 if (lrubp
&& (!NBUFSTAMPVALID(lrubp
) ||
744 ((lrubp
->nb_timestamp
+ NFSBUF_LRU_STALE
) < now
.tv_sec
)))
746 metabp
= TAILQ_FIRST(&nfsbuffreemeta
);
747 if (!bp
&& metabp
&& (!NBUFSTAMPVALID(metabp
) ||
748 ((metabp
->nb_timestamp
+ NFSBUF_META_STALE
) < now
.tv_sec
)))
751 if (!bp
&& (nfsbufcnt
>= nfsbufmax
)) {
752 /* we've already allocated all bufs, so */
753 /* choose the buffer that'll go stale first */
759 int32_t lru_stale_time
, meta_stale_time
;
760 lru_stale_time
= lrubp
->nb_timestamp
+ NFSBUF_LRU_STALE
;
761 meta_stale_time
= metabp
->nb_timestamp
+ NFSBUF_META_STALE
;
762 if (lru_stale_time
<= meta_stale_time
)
771 /* we have a buffer to reuse */
772 FSDBG(544, vp
, blkno
, bp
, bp
->nb_flags
);
774 if (ISSET(bp
->nb_flags
, NB_DELWRI
))
775 panic("nfs_buf_get: delwri");
776 SET(bp
->nb_lflags
, NBL_BUSY
);
777 /* disassociate buffer from previous vnode */
779 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
780 LIST_REMOVE(bp
, nb_vnbufs
);
781 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
785 LIST_REMOVE(bp
, nb_hash
);
786 /* nuke any creds we're holding */
788 if (cred
!= NOCRED
) {
789 bp
->nb_rcred
= NOCRED
;
790 kauth_cred_rele(cred
);
793 if (cred
!= NOCRED
) {
794 bp
->nb_wcred
= NOCRED
;
795 kauth_cred_rele(cred
);
797 /* if buf will no longer be NB_META, dump old buffer */
798 if (operation
== NBLK_META
) {
799 if (!ISSET(bp
->nb_flags
, NB_META
))
801 } else if (ISSET(bp
->nb_flags
, NB_META
)) {
803 kfree(bp
->nb_data
, bp
->nb_bufsize
);
808 /* re-init buf fields */
810 bp
->nb_validoff
= bp
->nb_validend
= -1;
811 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
816 /* no buffer to reuse */
817 if ((nfsbufcnt
< nfsbufmax
) &&
818 ((operation
!= NBLK_META
) || (nfsbufmetacnt
< nfsbufmetamax
))) {
819 /* just alloc a new one */
820 MALLOC(bp
, struct nfsbuf
*, sizeof(struct nfsbuf
), M_TEMP
, M_WAITOK
);
822 lck_mtx_unlock(nfs_buf_mutex
);
823 FSDBG_BOT(541, vp
, blkno
, 0, error
);
827 if (operation
== NBLK_META
)
831 bzero(bp
, sizeof(*bp
));
832 bp
->nb_free
.tqe_next
= NFSNOLIST
;
833 bp
->nb_validoff
= bp
->nb_validend
= -1;
834 FSDBG(545, vp
, blkno
, bp
, 0);
836 /* too many bufs... wait for buffers to free up */
837 FSDBG_TOP(546, vp
, blkno
, nfsbufcnt
, nfsbufmax
);
839 /* poke the delwri list */
840 nfs_buf_delwri_push(1);
843 msleep(&nfsneedbuffer
, nfs_buf_mutex
, PCATCH
|PDROP
,
845 FSDBG_BOT(546, vp
, blkno
, nfsbufcnt
, nfsbufmax
);
846 if ((error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
847 FSDBG_BOT(541, vp
, blkno
, 0, error
);
855 bp
->nb_lflags
= NBL_BUSY
;
857 bp
->nb_lblkno
= blkno
;
858 /* insert buf in hash */
859 LIST_INSERT_HEAD(NFSBUFHASH(np
, blkno
), bp
, nb_hash
);
860 /* associate buffer with new vnode */
862 LIST_INSERT_HEAD(&np
->n_cleanblkhd
, bp
, nb_vnbufs
);
867 lck_mtx_unlock(nfs_buf_mutex
);
871 SET(bp
->nb_flags
, NB_META
);
872 if ((bp
->nb_bufsize
!= bufsize
) && bp
->nb_data
) {
873 kfree(bp
->nb_data
, bp
->nb_bufsize
);
875 bp
->nb_validoff
= bp
->nb_validend
= -1;
876 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
879 CLR(bp
->nb_flags
, NB_CACHE
);
882 bp
->nb_data
= kalloc(bufsize
);
884 /* Ack! couldn't allocate the data buffer! */
885 /* cleanup buffer and return error */
886 lck_mtx_lock(nfs_buf_mutex
);
887 LIST_REMOVE(bp
, nb_vnbufs
);
888 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
890 /* invalidate usage timestamp to allow immediate freeing */
891 NBUFSTAMPINVALIDATE(bp
);
892 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
893 panic("nfsbuf on freelist");
894 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
896 lck_mtx_unlock(nfs_buf_mutex
);
897 FSDBG_BOT(541, vp
, blkno
, 0xb00, ENOMEM
);
900 bp
->nb_bufsize
= bufsize
;
906 * Set or clear NB_READ now to let the UPL subsystem know
907 * if we intend to modify the pages or not.
909 if (operation
== NBLK_READ
) {
910 SET(bp
->nb_flags
, NB_READ
);
912 CLR(bp
->nb_flags
, NB_READ
);
914 if (bufsize
< PAGE_SIZE
)
916 bp
->nb_bufsize
= bufsize
;
917 bp
->nb_validoff
= bp
->nb_validend
= -1;
919 if (UBCINFOEXISTS(vp
)) {
921 if (nfs_buf_upl_setup(bp
)) {
922 /* unable to create upl */
923 /* vm object must no longer exist */
924 /* cleanup buffer and return error */
925 lck_mtx_lock(nfs_buf_mutex
);
926 LIST_REMOVE(bp
, nb_vnbufs
);
927 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
929 /* invalidate usage timestamp to allow immediate freeing */
930 NBUFSTAMPINVALIDATE(bp
);
931 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
932 panic("nfsbuf on freelist");
933 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
935 lck_mtx_unlock(nfs_buf_mutex
);
936 FSDBG_BOT(541, vp
, blkno
, 0x2bc, EIO
);
939 nfs_buf_upl_check(bp
);
944 panic("nfs_buf_get: %d unknown operation", operation
);
949 FSDBG_BOT(541, vp
, blkno
, bp
, bp
->nb_flags
);
955 nfs_buf_release(struct nfsbuf
*bp
, int freeup
)
957 vnode_t vp
= bp
->nb_vp
;
959 int wakeup_needbuffer
, wakeup_buffer
, wakeup_nbdwrite
;
961 FSDBG_TOP(548, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_data
);
962 FSDBG(548, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
963 FSDBG(548, bp
->nb_valid
, 0, bp
->nb_dirty
, 0);
965 if (UBCINFOEXISTS(vp
) && bp
->nb_bufsize
) {
970 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
) && !ISSET(bp
->nb_flags
, NB_INVAL
)) {
971 rv
= nfs_buf_upl_setup(bp
);
973 printf("nfs_buf_release: upl create failed %d\n", rv
);
975 nfs_buf_upl_check(bp
);
977 upl
= bp
->nb_pagelist
;
979 goto pagelist_cleanup_done
;
981 if (ubc_upl_unmap(upl
) != KERN_SUCCESS
)
982 panic("ubc_upl_unmap failed");
985 if (bp
->nb_flags
& (NB_ERROR
| NB_INVAL
| NB_NOCACHE
)) {
986 if (bp
->nb_flags
& (NB_READ
| NB_INVAL
| NB_NOCACHE
))
987 upl_flags
= UPL_ABORT_DUMP_PAGES
;
990 ubc_upl_abort(upl
, upl_flags
);
991 goto pagelist_cleanup_done
;
993 for (i
=0; i
<= (bp
->nb_bufsize
- 1)/PAGE_SIZE
; i
++) {
994 if (!NBPGVALID(bp
,i
))
995 ubc_upl_abort_range(upl
,
996 i
*PAGE_SIZE
, PAGE_SIZE
,
997 UPL_ABORT_DUMP_PAGES
|
998 UPL_ABORT_FREE_ON_EMPTY
);
1000 if (NBPGDIRTY(bp
,i
))
1001 upl_flags
= UPL_COMMIT_SET_DIRTY
;
1003 upl_flags
= UPL_COMMIT_CLEAR_DIRTY
;
1004 ubc_upl_commit_range(upl
,
1005 i
*PAGE_SIZE
, PAGE_SIZE
,
1007 UPL_COMMIT_INACTIVATE
|
1008 UPL_COMMIT_FREE_ON_EMPTY
);
1011 pagelist_cleanup_done
:
1012 /* was this the last buffer in the file? */
1013 if (NBOFF(bp
) + bp
->nb_bufsize
> (off_t
)(VTONFS(vp
)->n_size
)) {
1014 /* if so, invalidate all pages of last buffer past EOF */
1015 int biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
1017 start
= trunc_page_64(VTONFS(vp
)->n_size
) + PAGE_SIZE_64
;
1018 end
= trunc_page_64(NBOFF(bp
) + biosize
);
1020 if (!(rv
= ubc_sync_range(vp
, start
, end
, UBC_INVALIDATE
)))
1021 printf("nfs_buf_release(): ubc_sync_range failed!\n");
1024 CLR(bp
->nb_flags
, NB_PAGELIST
);
1025 bp
->nb_pagelist
= NULL
;
1028 lck_mtx_lock(nfs_buf_mutex
);
1030 wakeup_needbuffer
= wakeup_buffer
= wakeup_nbdwrite
= 0;
1032 /* Wake up any processes waiting for any buffer to become free. */
1033 if (nfsneedbuffer
) {
1035 wakeup_needbuffer
= 1;
1037 /* Wake up any processes waiting for _this_ buffer to become free. */
1038 if (ISSET(bp
->nb_lflags
, NBL_WANTED
)) {
1039 CLR(bp
->nb_lflags
, NBL_WANTED
);
1043 /* If it's not cacheable, or an error, mark it invalid. */
1044 if (ISSET(bp
->nb_flags
, (NB_NOCACHE
|NB_ERROR
)))
1045 SET(bp
->nb_flags
, NB_INVAL
);
1047 if ((bp
->nb_bufsize
<= 0) || ISSET(bp
->nb_flags
, NB_INVAL
)) {
1048 /* If it's invalid or empty, dissociate it from its vnode */
1049 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
1050 LIST_REMOVE(bp
, nb_vnbufs
);
1051 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
1054 /* if this was a delayed write, wakeup anyone */
1055 /* waiting for delayed writes to complete */
1056 if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1057 CLR(bp
->nb_flags
, NB_DELWRI
);
1058 OSAddAtomic(-1, (SInt32
*)&nfs_nbdwrite
);
1060 wakeup_nbdwrite
= 1;
1062 /* invalidate usage timestamp to allow immediate freeing */
1063 NBUFSTAMPINVALIDATE(bp
);
1064 /* put buffer at head of free list */
1065 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1066 panic("nfsbuf on freelist");
1067 SET(bp
->nb_flags
, NB_INVAL
);
1068 if (ISSET(bp
->nb_flags
, NB_META
)) {
1069 TAILQ_INSERT_HEAD(&nfsbuffreemeta
, bp
, nb_free
);
1070 nfsbuffreemetacnt
++;
1072 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
1075 } else if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1076 /* put buffer at end of delwri list */
1077 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1078 panic("nfsbuf on freelist");
1079 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
1083 /* update usage timestamp */
1085 bp
->nb_timestamp
= now
.tv_sec
;
1086 /* put buffer at end of free list */
1087 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1088 panic("nfsbuf on freelist");
1089 if (ISSET(bp
->nb_flags
, NB_META
)) {
1090 TAILQ_INSERT_TAIL(&nfsbuffreemeta
, bp
, nb_free
);
1091 nfsbuffreemetacnt
++;
1093 TAILQ_INSERT_TAIL(&nfsbuffree
, bp
, nb_free
);
1100 /* Unlock the buffer. */
1101 CLR(bp
->nb_flags
, (NB_ASYNC
| NB_NOCACHE
| NB_STABLE
| NB_IOD
));
1102 CLR(bp
->nb_lflags
, NBL_BUSY
);
1104 FSDBG_BOT(548, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_data
);
1106 lck_mtx_unlock(nfs_buf_mutex
);
1108 if (wakeup_needbuffer
)
1109 wakeup(&nfsneedbuffer
);
1112 if (wakeup_nbdwrite
)
1113 wakeup(&nfs_nbdwrite
);
1119 * Wait for operations on the buffer to complete.
1120 * When they do, extract and return the I/O's error value.
1123 nfs_buf_iowait(struct nfsbuf
*bp
)
1125 FSDBG_TOP(549, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1127 lck_mtx_lock(nfs_buf_mutex
);
1129 while (!ISSET(bp
->nb_flags
, NB_DONE
))
1130 msleep(bp
, nfs_buf_mutex
, PRIBIO
+ 1, "nfs_buf_iowait", 0);
1132 lck_mtx_unlock(nfs_buf_mutex
);
1134 FSDBG_BOT(549, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1136 /* check for interruption of I/O, then errors. */
1137 if (ISSET(bp
->nb_flags
, NB_EINTR
)) {
1138 CLR(bp
->nb_flags
, NB_EINTR
);
1140 } else if (ISSET(bp
->nb_flags
, NB_ERROR
))
1141 return (bp
->nb_error
? bp
->nb_error
: EIO
);
1146 * Mark I/O complete on a buffer.
1149 nfs_buf_iodone(struct nfsbuf
*bp
)
1152 FSDBG_TOP(550, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1154 if (ISSET(bp
->nb_flags
, NB_DONE
))
1155 panic("nfs_buf_iodone already");
1157 * I/O was done, so don't believe
1158 * the DIRTY state from VM anymore
1160 CLR(bp
->nb_flags
, NB_WASDIRTY
);
1162 if (!ISSET(bp
->nb_flags
, NB_READ
)) {
1163 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
1165 * vnode_writedone() takes care of waking up
1166 * any throttled write operations
1168 vnode_writedone(bp
->nb_vp
);
1170 if (ISSET(bp
->nb_flags
, NB_ASYNC
)) { /* if async, release it */
1171 SET(bp
->nb_flags
, NB_DONE
); /* note that it's done */
1172 nfs_buf_release(bp
, 1);
1173 } else { /* or just wakeup the buffer */
1174 lck_mtx_lock(nfs_buf_mutex
);
1175 SET(bp
->nb_flags
, NB_DONE
); /* note that it's done */
1176 CLR(bp
->nb_lflags
, NBL_WANTED
);
1177 lck_mtx_unlock(nfs_buf_mutex
);
1181 FSDBG_BOT(550, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1185 nfs_buf_write_delayed(struct nfsbuf
*bp
, proc_t p
)
1187 vnode_t vp
= bp
->nb_vp
;
1189 FSDBG_TOP(551, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
1190 FSDBG(551, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, bp
->nb_dirty
);
1193 * If the block hasn't been seen before:
1194 * (1) Mark it as having been seen,
1195 * (2) Charge for the write.
1196 * (3) Make sure it's on its vnode's correct block list,
1198 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1199 SET(bp
->nb_flags
, NB_DELWRI
);
1200 if (p
&& p
->p_stats
)
1201 p
->p_stats
->p_ru
.ru_oublock
++; /* XXX */
1202 OSAddAtomic(1, (SInt32
*)&nfs_nbdwrite
);
1204 /* move to dirty list */
1205 lck_mtx_lock(nfs_buf_mutex
);
1206 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
1207 LIST_REMOVE(bp
, nb_vnbufs
);
1208 LIST_INSERT_HEAD(&VTONFS(vp
)->n_dirtyblkhd
, bp
, nb_vnbufs
);
1209 lck_mtx_unlock(nfs_buf_mutex
);
1213 * If the vnode has "too many" write operations in progress
1214 * wait for them to finish the IO
1216 (void)vnode_waitforwrites(vp
, VNODE_ASYNC_THROTTLE
, 0, 0, "nfs_buf_write_delayed");
1219 * If we have too many delayed write buffers,
1220 * more than we can "safely" handle, just fall back to
1221 * doing the async write
1223 if (nfs_nbdwrite
< 0)
1224 panic("nfs_buf_write_delayed: Negative nfs_nbdwrite");
1226 if (nfs_nbdwrite
> ((nfsbufcnt
/4)*3)) {
1227 /* issue async write */
1228 SET(bp
->nb_flags
, NB_ASYNC
);
1230 FSDBG_BOT(551, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1234 /* Otherwise, the "write" is done, so mark and release the buffer. */
1235 SET(bp
->nb_flags
, NB_DONE
);
1236 nfs_buf_release(bp
, 1);
1237 FSDBG_BOT(551, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
1242 * Check that a "needcommit" buffer can still be committed.
1243 * If the write verifier has changed, we need to clear the
1244 * the needcommit flag.
1247 nfs_buf_check_write_verifier(struct nfsnode
*np
, struct nfsbuf
*bp
)
1249 struct nfsmount
*nmp
;
1251 if (!ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
1254 nmp
= VFSTONFS(vnode_mount(NFSTOV(np
)));
1255 if (!nmp
|| (bp
->nb_verf
== nmp
->nm_verf
))
1258 /* write verifier changed, clear commit flag */
1259 bp
->nb_flags
&= ~NB_NEEDCOMMIT
;
1260 np
->n_needcommitcnt
--;
1261 CHECK_NEEDCOMMITCNT(np
);
1265 * add a reference to a buffer so it doesn't disappear while being used
1266 * (must be called with nfs_buf_mutex held)
1269 nfs_buf_refget(struct nfsbuf
*bp
)
1274 * release a reference on a buffer
1275 * (must be called with nfs_buf_mutex held)
1278 nfs_buf_refrele(struct nfsbuf
*bp
)
1284 * mark a particular buffer as BUSY
1285 * (must be called with nfs_buf_mutex held)
1288 nfs_buf_acquire(struct nfsbuf
*bp
, int flags
, int slpflag
, int slptimeo
)
1293 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
1295 * since the mutex_lock may block, the buffer
1296 * may become BUSY, so we need to recheck for
1299 if (flags
& NBAC_NOWAIT
)
1301 SET(bp
->nb_lflags
, NBL_WANTED
);
1303 ts
.tv_sec
= (slptimeo
/100);
1304 /* the hz value is 100; which leads to 10ms */
1305 ts
.tv_nsec
= (slptimeo
% 100) * 10 * NSEC_PER_USEC
* 1000;
1307 error
= msleep(bp
, nfs_buf_mutex
, slpflag
| (PRIBIO
+ 1),
1308 "nfs_buf_acquire", &ts
);
1313 if (flags
& NBAC_REMOVE
)
1314 nfs_buf_remfree(bp
);
1315 SET(bp
->nb_lflags
, NBL_BUSY
);
1321 * simply drop the BUSY status of a buffer
1322 * (must be called with nfs_buf_mutex held)
1325 nfs_buf_drop(struct nfsbuf
*bp
)
1327 int need_wakeup
= 0;
1329 if (!ISSET(bp
->nb_lflags
, NBL_BUSY
))
1330 panic("nfs_buf_drop: buffer not busy!");
1331 if (ISSET(bp
->nb_lflags
, NBL_WANTED
)) {
1333 * delay the actual wakeup until after we
1334 * clear NBL_BUSY and we've dropped nfs_buf_mutex
1338 /* Unlock the buffer. */
1339 CLR(bp
->nb_lflags
, (NBL_BUSY
| NBL_WANTED
));
1346 * prepare for iterating over an nfsnode's buffer list
1347 * this lock protects the queue manipulation
1348 * (must be called with nfs_buf_mutex held)
1351 nfs_buf_iterprepare(struct nfsnode
*np
, struct nfsbuflists
*iterheadp
, int flags
)
1353 struct nfsbuflists
*listheadp
;
1355 if (flags
& NBI_DIRTY
)
1356 listheadp
= &np
->n_dirtyblkhd
;
1358 listheadp
= &np
->n_cleanblkhd
;
1360 if ((flags
& NBI_NOWAIT
) && (np
->n_bufiterflags
& NBI_ITER
)) {
1361 LIST_INIT(iterheadp
);
1362 return(EWOULDBLOCK
);
1365 while (np
->n_bufiterflags
& NBI_ITER
) {
1366 np
->n_bufiterflags
|= NBI_ITERWANT
;
1367 msleep(&np
->n_bufiterflags
, nfs_buf_mutex
, 0, "nfs_buf_iterprepare", 0);
1369 if (LIST_EMPTY(listheadp
)) {
1370 LIST_INIT(iterheadp
);
1373 np
->n_bufiterflags
|= NBI_ITER
;
1375 iterheadp
->lh_first
= listheadp
->lh_first
;
1376 listheadp
->lh_first
->nb_vnbufs
.le_prev
= &iterheadp
->lh_first
;
1377 LIST_INIT(listheadp
);
1383 * cleanup after iterating over an nfsnode's buffer list
1384 * this lock protects the queue manipulation
1385 * (must be called with nfs_buf_mutex held)
1388 nfs_buf_itercomplete(struct nfsnode
*np
, struct nfsbuflists
*iterheadp
, int flags
)
1390 struct nfsbuflists
* listheadp
;
1393 if (flags
& NBI_DIRTY
)
1394 listheadp
= &np
->n_dirtyblkhd
;
1396 listheadp
= &np
->n_cleanblkhd
;
1398 while (!LIST_EMPTY(iterheadp
)) {
1399 bp
= LIST_FIRST(iterheadp
);
1400 LIST_REMOVE(bp
, nb_vnbufs
);
1401 LIST_INSERT_HEAD(listheadp
, bp
, nb_vnbufs
);
1404 np
->n_bufiterflags
&= ~NBI_ITER
;
1405 if (np
->n_bufiterflags
& NBI_ITERWANT
) {
1406 np
->n_bufiterflags
&= ~NBI_ITERWANT
;
1407 wakeup(&np
->n_bufiterflags
);
1413 * Vnode op for read using bio
1414 * Any similarity to readip() is purely coincidental
1420 __unused
int ioflag
,
1424 struct nfsnode
*np
= VTONFS(vp
);
1427 struct nfsbuf
*bp
= NULL
, *rabp
;
1428 struct nfs_vattr nvattr
;
1429 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
1430 daddr64_t lbn
, rabn
, lastrabn
= -1, tlbn
;
1432 int nra
, error
= 0, n
= 0, on
= 0;
1434 struct dirent
*direntp
= NULL
;
1436 int nocachereadahead
= 0;
1438 FSDBG_TOP(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), ioflag
);
1441 if (uio
->uio_rw
!= UIO_READ
)
1442 panic("nfs_read mode");
1444 if (uio_uio_resid(uio
) == 0) {
1445 FSDBG_BOT(514, vp
, 0xd1e0001, 0, 0);
1448 if (uio
->uio_offset
< 0) {
1449 FSDBG_BOT(514, vp
, 0xd1e0002, 0, EINVAL
);
1453 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) && !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
1454 nfs_fsinfo(nmp
, vp
, cred
, p
);
1455 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
1456 vtype
= vnode_vtype(vp
);
1458 * For nfs, cache consistency can only be maintained approximately.
1459 * Although RFC1094 does not specify the criteria, the following is
1460 * believed to be compatible with the reference port.
1462 * If the file's modify time on the server has changed since the
1463 * last read rpc or you have written to the file,
1464 * you may have lost data cache consistency with the
1465 * server, so flush all of the file's data out of the cache.
1466 * Then force a getattr rpc to ensure that you have up to date
1468 * NB: This implies that cache data can be read when up to
1469 * NFS_MAXATTRTIMEO seconds out of date. If you find that you need
1470 * current attributes this could be forced by setting calling
1471 * NATTRINVALIDATE() before the nfs_getattr() call.
1473 if (np
->n_flag
& NNEEDINVALIDATE
) {
1474 np
->n_flag
&= ~NNEEDINVALIDATE
;
1475 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
1477 if (np
->n_flag
& NMODIFIED
) {
1478 if (vtype
!= VREG
) {
1480 panic("nfs: bioread, not dir");
1482 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1484 FSDBG_BOT(514, vp
, 0xd1e0003, 0, error
);
1488 NATTRINVALIDATE(np
);
1489 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
1491 FSDBG_BOT(514, vp
, 0xd1e0004, 0, error
);
1494 if (vtype
== VDIR
) {
1495 /* if directory changed, purge any name cache entries */
1496 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
1498 np
->n_ncmtime
= nvattr
.nva_mtime
;
1500 np
->n_mtime
= nvattr
.nva_mtime
;
1502 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
1504 FSDBG_BOT(514, vp
, 0xd1e0005, 0, error
);
1507 if (nfstimespeccmp(&np
->n_mtime
, &nvattr
.nva_mtime
, !=)) {
1508 if (vtype
== VDIR
) {
1510 /* purge name cache entries */
1511 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
1514 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1516 FSDBG_BOT(514, vp
, 0xd1e0006, 0, error
);
1520 np
->n_ncmtime
= nvattr
.nva_mtime
;
1521 np
->n_mtime
= nvattr
.nva_mtime
;
1525 if (vnode_isnocache(vp
)) {
1526 if (!(np
->n_flag
& NNOCACHE
)) {
1527 if (NVALIDBUFS(np
)) {
1528 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1530 FSDBG_BOT(514, vp
, 0xd1e000a, 0, error
);
1534 np
->n_flag
|= NNOCACHE
;
1536 } else if (np
->n_flag
& NNOCACHE
) {
1537 np
->n_flag
&= ~NNOCACHE
;
1541 if (np
->n_flag
& NNOCACHE
) {
1545 * If we have only a block or so to read,
1546 * just do the rpc directly.
1547 * If we have a couple blocks or more to read,
1548 * then we'll take advantage of readahead within
1549 * this loop to try to fetch all the data in parallel
1551 if (!nocachereadahead
&& (uio_uio_resid(uio
) < 2*biosize
)) {
1552 error
= nfs_readrpc(vp
, uio
, cred
, p
);
1553 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1556 nocachereadahead
= 1;
1559 error
= nfs_readlinkrpc(vp
, uio
, cred
, p
);
1560 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1565 printf(" NFSNOCACHE: type %x unexpected\n", vtype
);
1570 lbn
= uio
->uio_offset
/ biosize
;
1573 * Copy directly from any cached pages without grabbing the bufs.
1575 * Note: for "nocache" reads, we don't copy directly from UBC
1576 * because any cached pages will be for readahead buffers that
1577 * need to be invalidated anyway before we finish this request.
1579 if (!(np
->n_flag
& NNOCACHE
) &&
1580 (uio
->uio_segflg
== UIO_USERSPACE32
||
1581 uio
->uio_segflg
== UIO_USERSPACE64
||
1582 uio
->uio_segflg
== UIO_USERSPACE
)) {
1583 // LP64todo - fix this!
1584 int io_resid
= uio_uio_resid(uio
);
1585 diff
= np
->n_size
- uio
->uio_offset
;
1586 if (diff
< io_resid
)
1589 error
= cluster_copy_ubc_data(vp
, uio
, &io_resid
, 0);
1591 FSDBG_BOT(514, vp
, uio
->uio_offset
, 0xcacefeed, error
);
1595 /* count any biocache reads that we just copied directly */
1596 if (lbn
!= uio
->uio_offset
/ biosize
) {
1597 OSAddAtomic((uio
->uio_offset
/ biosize
) - lbn
, (SInt32
*)&nfsstats
.biocache_reads
);
1598 FSDBG(514, vp
, 0xcacefeed, uio
->uio_offset
, error
);
1602 lbn
= uio
->uio_offset
/ biosize
;
1603 on
= uio
->uio_offset
% biosize
;
1606 * Start the read ahead(s), as required.
1608 if (nfs_numasync
> 0 && nmp
->nm_readahead
> 0) {
1609 for (nra
= 0; nra
< nmp
->nm_readahead
; nra
++) {
1610 rabn
= lbn
+ 1 + nra
;
1611 if (rabn
<= lastrabn
) {
1612 /* we've already (tried to) read this block */
1613 /* no need to try it again... */
1617 if ((off_t
)rabn
* biosize
>= (off_t
)np
->n_size
)
1619 if ((np
->n_flag
& NNOCACHE
) &&
1620 (((off_t
)rabn
* biosize
) >= (uio
->uio_offset
+ uio_uio_resid(uio
))))
1621 /* for uncached readahead, don't go beyond end of request */
1623 /* check if block exists and is valid. */
1624 error
= nfs_buf_get(vp
, rabn
, biosize
, p
, NBLK_READ
|NBLK_NOWAIT
, &rabp
);
1626 FSDBG_BOT(514, vp
, 0xd1e000b, 1, error
);
1631 if (nfs_buf_upl_valid_range(rabp
, 0, rabp
->nb_bufsize
)) {
1632 nfs_buf_release(rabp
, 1);
1635 if (!ISSET(rabp
->nb_flags
, (NB_CACHE
|NB_DELWRI
))) {
1636 SET(rabp
->nb_flags
, (NB_READ
|NB_ASYNC
));
1637 if (nfs_asyncio(rabp
, cred
)) {
1638 SET(rabp
->nb_flags
, (NB_INVAL
|NB_ERROR
));
1639 rabp
->nb_error
= EIO
;
1640 nfs_buf_release(rabp
, 1);
1643 nfs_buf_release(rabp
, 1);
1647 if ((uio_uio_resid(uio
) <= 0) || (uio
->uio_offset
>= (off_t
)np
->n_size
)) {
1648 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0xaaaaaaaa);
1652 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_reads
);
1655 * If the block is in the cache and has the required data
1656 * in a valid region, just copy it out.
1657 * Otherwise, get the block and write back/read in,
1662 // LP64todo - fix this!
1663 n
= min((unsigned)(bufsize
- on
), uio_uio_resid(uio
));
1664 diff
= np
->n_size
- uio
->uio_offset
;
1668 error
= nfs_buf_get(vp
, lbn
, bufsize
, p
, NBLK_READ
, &bp
);
1670 FSDBG_BOT(514, vp
, 0xd1e000c, 0, EINTR
);
1674 /* if any pages are valid... */
1676 /* ...check for any invalid pages in the read range */
1677 int pg
, firstpg
, lastpg
, dirtypg
;
1678 dirtypg
= firstpg
= lastpg
= -1;
1680 while (pg
<= (on
+ n
- 1)/PAGE_SIZE
) {
1681 if (!NBPGVALID(bp
,pg
)) {
1685 } else if (firstpg
>= 0 && dirtypg
< 0 && NBPGDIRTY(bp
,pg
))
1690 /* if there are no invalid pages, we're all set */
1692 if (bp
->nb_validoff
< 0) {
1693 /* valid range isn't set up, so */
1694 /* set it to what we know is valid */
1695 bp
->nb_validoff
= trunc_page(on
);
1696 bp
->nb_validend
= round_page(on
+n
);
1697 nfs_buf_normalize_valid_range(np
, bp
);
1702 /* there are invalid pages in the read range */
1703 if ((dirtypg
> firstpg
) && (dirtypg
< lastpg
)) {
1704 /* there are also dirty page(s) in the range, */
1705 /* so write the buffer out and try again */
1706 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
1707 SET(bp
->nb_flags
, NB_ASYNC
);
1708 if (bp
->nb_wcred
== NOCRED
) {
1709 kauth_cred_ref(cred
);
1710 bp
->nb_wcred
= cred
;
1712 error
= nfs_buf_write(bp
);
1714 FSDBG_BOT(514, vp
, 0xd1e000d, 0, error
);
1719 if (!bp
->nb_dirty
&& bp
->nb_dirtyend
<= 0 &&
1720 (lastpg
- firstpg
+ 1) > (bufsize
/PAGE_SIZE
)/2) {
1721 /* we need to read in more than half the buffer and the */
1722 /* buffer's not dirty, so just fetch the whole buffer */
1725 /* read the page range in */
1727 char uio_buf
[ UIO_SIZEOF(1) ];
1730 auio
= uio_createwithbuffer(1, (NBOFF(bp
) + firstpg
* PAGE_SIZE_64
),
1731 UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
1735 uio_addiov(auio
, CAST_USER_ADDR_T((bp
->nb_data
+ firstpg
* PAGE_SIZE
)),
1736 ((lastpg
- firstpg
+ 1) * PAGE_SIZE
));
1737 error
= nfs_readrpc(vp
, auio
, cred
, p
);
1740 if (np
->n_flag
& NNOCACHE
)
1741 SET(bp
->nb_flags
, NB_NOCACHE
);
1742 nfs_buf_release(bp
, 1);
1743 FSDBG_BOT(514, vp
, 0xd1e000e, 0, error
);
1746 /* Make sure that the valid range is set to cover this read. */
1747 bp
->nb_validoff
= trunc_page_32(on
);
1748 bp
->nb_validend
= round_page_32(on
+n
);
1749 nfs_buf_normalize_valid_range(np
, bp
);
1750 if (uio_resid(auio
) > 0) {
1751 /* if short read, must have hit EOF, */
1752 /* so zero the rest of the range */
1753 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
1755 /* mark the pages (successfully read) as valid */
1756 for (pg
=firstpg
; pg
<= lastpg
; pg
++)
1757 NBPGVALID_SET(bp
,pg
);
1760 /* if no pages are valid, read the whole block */
1761 if (!bp
->nb_valid
) {
1762 SET(bp
->nb_flags
, NB_READ
);
1763 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
1764 error
= nfs_doio(bp
, cred
, p
);
1766 if (np
->n_flag
& NNOCACHE
)
1767 SET(bp
->nb_flags
, NB_NOCACHE
);
1768 nfs_buf_release(bp
, 1);
1769 FSDBG_BOT(514, vp
, 0xd1e000f, 0, error
);
1774 /* validate read range against valid range and clip */
1775 if (bp
->nb_validend
> 0) {
1776 diff
= (on
>= bp
->nb_validend
) ? 0 : (bp
->nb_validend
- on
);
1784 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_readlinks
);
1785 error
= nfs_buf_get(vp
, 0, NFS_MAXPATHLEN
, p
, NBLK_READ
, &bp
);
1787 FSDBG_BOT(514, vp
, 0xd1e0010, 0, error
);
1790 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1791 SET(bp
->nb_flags
, NB_READ
);
1792 error
= nfs_doio(bp
, cred
, p
);
1794 SET(bp
->nb_flags
, NB_ERROR
);
1795 nfs_buf_release(bp
, 1);
1796 FSDBG_BOT(514, vp
, 0xd1e0011, 0, error
);
1800 // LP64todo - fix this!
1801 n
= min(uio_uio_resid(uio
), bp
->nb_validend
);
1805 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_readdirs
);
1806 if (np
->n_direofoffset
&& uio
->uio_offset
>= np
->n_direofoffset
) {
1807 FSDBG_BOT(514, vp
, 0xde0f0001, 0, 0);
1810 lbn
= uio
->uio_offset
/ NFS_DIRBLKSIZ
;
1811 on
= uio
->uio_offset
& (NFS_DIRBLKSIZ
- 1);
1812 error
= nfs_buf_get(vp
, lbn
, NFS_DIRBLKSIZ
, p
, NBLK_READ
, &bp
);
1814 FSDBG_BOT(514, vp
, 0xd1e0012, 0, error
);
1817 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1818 SET(bp
->nb_flags
, NB_READ
);
1819 error
= nfs_doio(bp
, cred
, p
);
1821 nfs_buf_release(bp
, 1);
1823 while (error
== NFSERR_BAD_COOKIE
) {
1825 error
= nfs_vinvalbuf(vp
, 0, cred
, p
, 1);
1827 * Yuck! The directory has been modified on the
1828 * server. The only way to get the block is by
1829 * reading from the beginning to get all the
1832 for (tlbn
= 0; tlbn
<= lbn
&& !error
; tlbn
++) {
1833 if (np
->n_direofoffset
1834 && (tlbn
* NFS_DIRBLKSIZ
) >= np
->n_direofoffset
) {
1835 FSDBG_BOT(514, vp
, 0xde0f0002, 0, 0);
1838 error
= nfs_buf_get(vp
, tlbn
, NFS_DIRBLKSIZ
, p
, NBLK_READ
, &bp
);
1840 FSDBG_BOT(514, vp
, 0xd1e0013, 0, error
);
1843 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1844 SET(bp
->nb_flags
, NB_READ
);
1845 error
= nfs_doio(bp
, cred
, p
);
1847 * no error + NB_INVAL == directory EOF,
1850 if (error
== 0 && (bp
->nb_flags
& NB_INVAL
))
1854 * An error will throw away the block and the
1855 * for loop will break out. If no error and this
1856 * is not the block we want, we throw away the
1857 * block and go for the next one via the for loop.
1859 if (error
|| tlbn
< lbn
)
1860 nfs_buf_release(bp
, 1);
1864 * The above while is repeated if we hit another cookie
1865 * error. If we hit an error and it wasn't a cookie error,
1869 FSDBG_BOT(514, vp
, 0xd1e0014, 0, error
);
1875 * If not eof and read aheads are enabled, start one.
1876 * (You need the current block first, so that you have the
1877 * directory offset cookie of the next block.)
1879 if (nfs_numasync
> 0 && nmp
->nm_readahead
> 0 &&
1880 (np
->n_direofoffset
== 0 ||
1881 (lbn
+ 1) * NFS_DIRBLKSIZ
< np
->n_direofoffset
) &&
1882 !nfs_buf_is_incore(vp
, lbn
+ 1)) {
1883 error
= nfs_buf_get(vp
, lbn
+ 1, NFS_DIRBLKSIZ
, p
, NBLK_READ
|NBLK_NOWAIT
, &rabp
);
1885 FSDBG_BOT(514, vp
, 0xd1e0015, 0, error
);
1889 if (!ISSET(rabp
->nb_flags
, (NB_CACHE
))) {
1890 SET(rabp
->nb_flags
, (NB_READ
| NB_ASYNC
));
1891 if (nfs_asyncio(rabp
, cred
)) {
1892 SET(rabp
->nb_flags
, (NB_INVAL
|NB_ERROR
));
1893 rabp
->nb_error
= EIO
;
1894 nfs_buf_release(rabp
, 1);
1897 nfs_buf_release(rabp
, 1);
1902 * Make sure we use a signed variant of min() since
1903 * the second term may be negative.
1905 // LP64todo - fix this!
1906 n
= lmin(uio_uio_resid(uio
), bp
->nb_validend
- on
);
1908 * We keep track of the directory eof in
1909 * np->n_direofoffset and chop it off as an
1910 * extra step right here.
1912 if (np
->n_direofoffset
&&
1913 n
> np
->n_direofoffset
- uio
->uio_offset
)
1914 n
= np
->n_direofoffset
- uio
->uio_offset
;
1916 * Make sure that we return an integral number of entries so
1917 * that any subsequent calls will start copying from the start
1918 * of the next entry.
1920 * If the current value of n has the last entry cut short,
1921 * set n to copy everything up to the last entry instead.
1924 dp
= bp
->nb_data
+ on
;
1925 while (dp
< (bp
->nb_data
+ on
+ n
)) {
1926 direntp
= (struct dirent
*)dp
;
1927 dp
+= direntp
->d_reclen
;
1929 if (dp
> (bp
->nb_data
+ on
+ n
))
1930 n
= (dp
- direntp
->d_reclen
) - (bp
->nb_data
+ on
);
1934 printf("nfs_bioread: type %x unexpected\n", vtype
);
1935 FSDBG_BOT(514, vp
, 0xd1e0016, 0, EINVAL
);
1940 error
= uiomove(bp
->nb_data
+ on
, (int)n
, uio
);
1944 if (np
->n_flag
& NNOCACHE
)
1945 SET(bp
->nb_flags
, NB_NOCACHE
);
1955 nfs_buf_release(bp
, 1);
1956 } while (error
== 0 && uio_uio_resid(uio
) > 0 && n
> 0);
1957 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1963 * Vnode op for write using bio
1967 struct vnop_write_args
/* {
1968 struct vnodeop_desc *a_desc;
1972 vfs_context_t a_context;
1975 struct uio
*uio
= ap
->a_uio
;
1976 vnode_t vp
= ap
->a_vp
;
1977 struct nfsnode
*np
= VTONFS(vp
);
1980 int ioflag
= ap
->a_ioflag
;
1982 struct nfs_vattr nvattr
;
1983 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
1985 int biosize
, bufsize
;
1986 int n
, on
, error
= 0;
1987 off_t boff
, start
, end
, cureof
;
1988 struct iovec_32 iov
;
1991 FSDBG_TOP(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), ioflag
);
1994 if (uio
->uio_rw
!= UIO_WRITE
)
1995 panic("nfs_write mode");
1996 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
1997 panic("nfs_write proc");
2000 p
= vfs_context_proc(ap
->a_context
);
2001 cred
= vfs_context_ucred(ap
->a_context
);
2003 if (vnode_vtype(vp
) != VREG
)
2006 np
->n_flag
|= NWRBUSY
;
2008 if (np
->n_flag
& NNEEDINVALIDATE
) {
2009 np
->n_flag
&= ~NNEEDINVALIDATE
;
2010 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
2012 if (np
->n_flag
& NWRITEERR
) {
2013 np
->n_flag
&= ~(NWRITEERR
| NWRBUSY
);
2014 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), np
->n_error
);
2015 return (np
->n_error
);
2017 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
2018 !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
2019 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
2020 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
2021 if (np
->n_flag
& NMODIFIED
) {
2022 NATTRINVALIDATE(np
);
2023 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
2025 np
->n_flag
&= ~NWRBUSY
;
2026 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0x10bad01, error
);
2030 if (ioflag
& IO_APPEND
) {
2031 NATTRINVALIDATE(np
);
2032 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
2034 np
->n_flag
&= ~NWRBUSY
;
2035 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0x10bad02, error
);
2038 uio
->uio_offset
= np
->n_size
;
2041 if (uio
->uio_offset
< 0) {
2042 np
->n_flag
&= ~NWRBUSY
;
2043 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0xbad0ff, EINVAL
);
2046 if (uio_uio_resid(uio
) == 0) {
2047 np
->n_flag
&= ~NWRBUSY
;
2048 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0);
2052 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
2054 if (vnode_isnocache(vp
)) {
2055 if (!(np
->n_flag
& NNOCACHE
)) {
2056 if (NVALIDBUFS(np
)) {
2057 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
2059 np
->n_flag
&= ~NWRBUSY
;
2060 FSDBG_BOT(515, vp
, 0, 0, error
);
2064 np
->n_flag
|= NNOCACHE
;
2066 } else if (np
->n_flag
& NNOCACHE
) {
2067 np
->n_flag
&= ~NNOCACHE
;
2071 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_writes
);
2072 lbn
= uio
->uio_offset
/ biosize
;
2073 on
= uio
->uio_offset
% biosize
;
2074 // LP64todo - fix this
2075 n
= min((unsigned)(biosize
- on
), uio_uio_resid(uio
));
2079 * Get a cache block for writing. The range to be written is
2080 * (off..off+n) within the block. We ensure that the block
2081 * either has no dirty region or that the given range is
2082 * contiguous with the existing dirty region.
2084 error
= nfs_buf_get(vp
, lbn
, bufsize
, p
, NBLK_WRITE
, &bp
);
2086 np
->n_flag
&= ~NWRBUSY
;
2087 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2090 /* map the block because we know we're going to write to it */
2093 if (np
->n_flag
& NNOCACHE
)
2094 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2096 if (bp
->nb_wcred
== NOCRED
) {
2097 kauth_cred_ref(cred
);
2098 bp
->nb_wcred
= cred
;
2102 * If there's already a dirty range AND dirty pages in this block we
2103 * need to send a commit AND write the dirty pages before continuing.
2105 * If there's already a dirty range OR dirty pages in this block
2106 * and the new write range is not contiguous with the existing range,
2107 * then force the buffer to be written out now.
2108 * (We used to just extend the dirty range to cover the valid,
2109 * but unwritten, data in between also. But writing ranges
2110 * of data that weren't actually written by an application
2111 * risks overwriting some other client's data with stale data
2112 * that's just masquerading as new written data.)
2114 if (bp
->nb_dirtyend
> 0) {
2115 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
2116 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c001);
2117 /* write/commit buffer "synchronously" */
2118 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2119 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2120 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2121 error
= nfs_buf_write(bp
);
2123 np
->n_flag
&= ~NWRBUSY
;
2124 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2129 } else if (bp
->nb_dirty
) {
2130 int firstpg
, lastpg
;
2132 /* calculate write range pagemask */
2133 firstpg
= on
/PAGE_SIZE
;
2134 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2135 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
2136 /* check if there are dirty pages outside the write range */
2137 if (bp
->nb_dirty
& ~pagemask
) {
2138 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c002);
2139 /* write/commit buffer "synchronously" */
2140 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2141 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2142 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2143 error
= nfs_buf_write(bp
);
2145 np
->n_flag
&= ~NWRBUSY
;
2146 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2151 /* if the first or last pages are already dirty */
2152 /* make sure that the dirty range encompasses those pages */
2153 if (NBPGDIRTY(bp
,firstpg
) || NBPGDIRTY(bp
,lastpg
)) {
2154 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c003);
2155 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
2156 if (NBPGDIRTY(bp
,lastpg
)) {
2157 bp
->nb_dirtyend
= (lastpg
+1) * PAGE_SIZE
;
2159 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
2160 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2162 bp
->nb_dirtyend
= on
+n
;
2167 * Are we extending the size of the file with this write?
2168 * If so, update file size now that we have the block.
2169 * If there was a partial buf at the old eof, validate
2170 * and zero the new bytes.
2172 cureof
= (off_t
)np
->n_size
;
2173 if (uio
->uio_offset
+ n
> (off_t
)np
->n_size
) {
2174 struct nfsbuf
*eofbp
= NULL
;
2175 daddr64_t eofbn
= np
->n_size
/ biosize
;
2176 int eofoff
= np
->n_size
% biosize
;
2177 int neweofoff
= (uio
->uio_offset
+ n
) % biosize
;
2179 FSDBG(515, 0xb1ffa000, uio
->uio_offset
+ n
, eofoff
, neweofoff
);
2181 if (eofoff
&& (eofbn
< lbn
)) {
2182 error
= nfs_buf_get(vp
, eofbn
, biosize
, p
, NBLK_WRITE
|NBLK_ONLYVALID
, &eofbp
);
2184 np
->n_flag
&= ~NWRBUSY
;
2185 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2190 /* if we're extending within the same last block */
2191 /* and the block is flagged as being cached... */
2192 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
2193 /* ...check that all pages in buffer are valid */
2194 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1)/PAGE_SIZE
;
2196 /* pagemask only has to extend to last page being written to */
2197 pagemask
= (1 << (endpg
+1)) - 1;
2198 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
2199 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
2200 /* zerofill any hole */
2201 if (on
> bp
->nb_validend
) {
2203 for (i
=bp
->nb_validend
/PAGE_SIZE
; i
<= (on
- 1)/PAGE_SIZE
; i
++)
2204 NBPGVALID_SET(bp
, i
);
2206 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
2207 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
2208 on
- bp
->nb_validend
);
2210 /* zerofill any trailing data in the last page */
2213 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
2214 bzero((char *)bp
->nb_data
+ neweofoff
,
2215 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
2219 np
->n_flag
|= NMODIFIED
;
2220 np
->n_size
= uio
->uio_offset
+ n
;
2221 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
2224 * We may need to zero any previously invalid data
2225 * after the old EOF in the previous EOF buffer.
2227 * For the old last page, don't zero bytes if there
2228 * are invalid bytes in that page (i.e. the page isn't
2230 * For pages after the old last page, zero them and
2231 * mark them as valid.
2235 if (np
->n_flag
& NNOCACHE
)
2236 SET(eofbp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2238 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
2240 i
= eofoff
/PAGE_SIZE
;
2241 while (eofoff
< biosize
) {
2242 int poff
= eofoff
& PAGE_MASK
;
2243 if (!poff
|| NBPGVALID(eofbp
,i
)) {
2244 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
2245 NBPGVALID_SET(eofbp
, i
);
2247 if (bp
->nb_validend
== eofoff
)
2248 bp
->nb_validend
+= PAGE_SIZE
- poff
;
2249 eofoff
+= PAGE_SIZE
- poff
;
2252 nfs_buf_release(eofbp
, 1);
2256 * If dirtyend exceeds file size, chop it down. This should
2257 * not occur unless there is a race.
2259 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
2260 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2262 * UBC doesn't handle partial pages, so we need to make sure
2263 * that any pages left in the page cache are completely valid.
2265 * Writes that are smaller than a block are delayed if they
2266 * don't extend to the end of the block.
2268 * If the block isn't (completely) cached, we may need to read
2269 * in some parts of pages that aren't covered by the write.
2270 * If the write offset (on) isn't page aligned, we'll need to
2271 * read the start of the first page being written to. Likewise,
2272 * if the offset of the end of the write (on+n) isn't page aligned,
2273 * we'll need to read the end of the last page being written to.
2276 * We don't want to read anything we're just going to write over.
2277 * We don't want to issue multiple I/Os if we don't have to
2278 * (because they're synchronous rpcs).
2279 * We don't want to read anything we already have modified in the
2282 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && n
< biosize
) {
2283 int firstpg
, lastpg
, dirtypg
;
2284 int firstpgoff
, lastpgoff
;
2286 firstpg
= on
/PAGE_SIZE
;
2287 firstpgoff
= on
& PAGE_MASK
;
2288 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2289 lastpgoff
= (on
+n
) & PAGE_MASK
;
2290 if (firstpgoff
&& !NBPGVALID(bp
,firstpg
)) {
2291 /* need to read start of first page */
2292 start
= firstpg
* PAGE_SIZE
;
2293 end
= start
+ firstpgoff
;
2295 if (lastpgoff
&& !NBPGVALID(bp
,lastpg
)) {
2296 /* need to read end of last page */
2298 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
2299 end
= (lastpg
+ 1) * PAGE_SIZE
;
2302 /* need to read the data in range: start...end-1 */
2304 /* first, check for dirty pages in between */
2305 /* if there are, we'll have to do two reads because */
2306 /* we don't want to overwrite the dirty pages. */
2307 for (dirtypg
=start
/PAGE_SIZE
; dirtypg
<= (end
-1)/PAGE_SIZE
; dirtypg
++)
2308 if (NBPGDIRTY(bp
,dirtypg
))
2311 /* if start is at beginning of page, try */
2312 /* to get any preceeding pages as well. */
2313 if (!(start
& PAGE_MASK
)) {
2314 /* stop at next dirty/valid page or start of block */
2315 for (; start
> 0; start
-=PAGE_SIZE
)
2316 if (NBPGVALID(bp
,((start
-1)/PAGE_SIZE
)))
2321 /* setup uio for read(s) */
2323 auio
.uio_iovs
.iov32p
= &iov
;
2324 auio
.uio_iovcnt
= 1;
2325 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
2326 auio
.uio_segflg
= UIO_SYSSPACE
;
2328 auio
.uio_segflg
= UIO_SYSSPACE32
;
2330 auio
.uio_rw
= UIO_READ
;
2332 if (dirtypg
<= (end
-1)/PAGE_SIZE
) {
2333 /* there's a dirty page in the way, so just do two reads */
2334 /* we'll read the preceding data here */
2335 auio
.uio_offset
= boff
+ start
;
2336 iov
.iov_len
= on
- start
;
2337 uio_uio_resid_set(&auio
, iov
.iov_len
);
2338 iov
.iov_base
= (uintptr_t) bp
->nb_data
+ start
;
2339 error
= nfs_readrpc(vp
, &auio
, cred
, p
);
2341 bp
->nb_error
= error
;
2342 SET(bp
->nb_flags
, NB_ERROR
);
2343 printf("nfs_write: readrpc %d", error
);
2345 if (uio_uio_resid(&auio
) > 0) {
2346 FSDBG(516, bp
, iov
.iov_base
- bp
->nb_data
, uio_uio_resid(&auio
), 0xd00dee01);
2347 // LP64todo - fix this
2348 bzero((caddr_t
)iov
.iov_base
, uio_uio_resid(&auio
));
2350 /* update validoff/validend if necessary */
2351 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2352 bp
->nb_validoff
= start
;
2353 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
))
2354 bp
->nb_validend
= on
;
2355 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2356 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2357 /* validate any pages before the write offset */
2358 for (; start
< on
/PAGE_SIZE
; start
+=PAGE_SIZE
)
2359 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2360 /* adjust start to read any trailing data */
2364 /* if end is at end of page, try to */
2365 /* get any following pages as well. */
2366 if (!(end
& PAGE_MASK
)) {
2367 /* stop at next valid page or end of block */
2368 for (; end
< bufsize
; end
+=PAGE_SIZE
)
2369 if (NBPGVALID(bp
,end
/PAGE_SIZE
))
2373 if (((boff
+start
) >= cureof
) || ((start
>= on
) && ((boff
+ on
+ n
) >= cureof
))) {
2375 * Either this entire read is beyond the current EOF
2376 * or the range that we won't be modifying (on+n...end)
2377 * is all beyond the current EOF.
2378 * No need to make a trip across the network to
2379 * read nothing. So, just zero the buffer instead.
2381 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
2382 bzero(bp
->nb_data
+ start
, end
- start
);
2384 /* now we'll read the (rest of the) data */
2385 auio
.uio_offset
= boff
+ start
;
2386 iov
.iov_len
= end
- start
;
2387 uio_uio_resid_set(&auio
, iov
.iov_len
);
2388 iov
.iov_base
= (uintptr_t) (bp
->nb_data
+ start
);
2389 error
= nfs_readrpc(vp
, &auio
, cred
, p
);
2391 bp
->nb_error
= error
;
2392 SET(bp
->nb_flags
, NB_ERROR
);
2393 printf("nfs_write: readrpc %d", error
);
2395 if (uio_uio_resid(&auio
) > 0) {
2396 FSDBG(516, bp
, iov
.iov_base
- bp
->nb_data
, uio_uio_resid(&auio
), 0xd00dee02);
2397 // LP64todo - fix this
2398 bzero((caddr_t
)iov
.iov_base
, uio_uio_resid(&auio
));
2401 /* update validoff/validend if necessary */
2402 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2403 bp
->nb_validoff
= start
;
2404 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
))
2405 bp
->nb_validend
= end
;
2406 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2407 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2408 /* validate any pages before the write offset's page */
2409 for (; start
< trunc_page_32(on
); start
+=PAGE_SIZE
)
2410 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2411 /* validate any pages after the range of pages being written to */
2412 for (; (end
- 1) > round_page_32(on
+n
-1); end
-=PAGE_SIZE
)
2413 NBPGVALID_SET(bp
, (end
-1)/PAGE_SIZE
);
2414 /* Note: pages being written to will be validated when written */
2418 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
2419 error
= bp
->nb_error
;
2420 nfs_buf_release(bp
, 1);
2421 np
->n_flag
&= ~NWRBUSY
;
2422 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2426 np
->n_flag
|= NMODIFIED
;
2429 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
2431 SET(bp
->nb_flags
, NB_ERROR
);
2432 nfs_buf_release(bp
, 1);
2433 np
->n_flag
&= ~NWRBUSY
;
2434 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2438 /* validate any pages written to */
2439 start
= on
& ~PAGE_MASK
;
2440 for (; start
< on
+n
; start
+= PAGE_SIZE
) {
2441 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2443 * This may seem a little weird, but we don't actually set the
2444 * dirty bits for writes. This is because we keep the dirty range
2445 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
2446 * delayed writes, when we give the pages back to the VM we don't
2447 * want to keep them marked dirty, because when we later write the
2448 * buffer we won't be able to tell which pages were written dirty
2449 * and which pages were mmapped and dirtied.
2452 if (bp
->nb_dirtyend
> 0) {
2453 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
2454 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
2456 bp
->nb_dirtyoff
= on
;
2457 bp
->nb_dirtyend
= on
+ n
;
2459 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
2460 bp
->nb_validoff
> bp
->nb_dirtyend
) {
2461 bp
->nb_validoff
= bp
->nb_dirtyoff
;
2462 bp
->nb_validend
= bp
->nb_dirtyend
;
2464 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
2465 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
2467 if (!ISSET(bp
->nb_flags
, NB_CACHE
))
2468 nfs_buf_normalize_valid_range(np
, bp
);
2471 * Since this block is being modified, it must be written
2472 * again and not just committed.
2474 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
2475 np
->n_needcommitcnt
--;
2476 CHECK_NEEDCOMMITCNT(np
);
2478 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
2480 if (ioflag
& IO_SYNC
) {
2482 error
= nfs_buf_write(bp
);
2484 np
->n_flag
&= ~NWRBUSY
;
2485 FSDBG_BOT(515, vp
, uio
->uio_offset
,
2486 uio_uio_resid(uio
), error
);
2489 } else if (((n
+ on
) == biosize
) || (np
->n_flag
& NNOCACHE
)) {
2491 SET(bp
->nb_flags
, NB_ASYNC
);
2494 nfs_buf_write_delayed(bp
, p
);
2496 if (np
->n_needcommitcnt
> (nfsbufcnt
/16))
2497 nfs_flushcommits(vp
, p
, 1);
2499 } while (uio_uio_resid(uio
) > 0 && n
> 0);
2501 np
->n_flag
&= ~NWRBUSY
;
2502 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0);
2507 * Flush out and invalidate all buffers associated with a vnode.
2508 * Called with the underlying object locked.
2511 nfs_vinvalbuf_internal(
2520 struct nfsbuflists blist
;
2521 int list
, error
= 0;
2522 struct nfsnode
*np
= VTONFS(vp
);
2524 if (flags
& V_SAVE
) {
2525 if ((error
= nfs_flush(vp
, MNT_WAIT
, cred
, p
,
2526 (flags
& V_IGNORE_WRITEERR
))))
2528 if (!LIST_EMPTY(&np
->n_dirtyblkhd
))
2529 panic("nfs_vinvalbuf: dirty bufs (vp 0x%x, bp 0x%x)",
2530 vp
, LIST_FIRST(&np
->n_dirtyblkhd
));
2533 lck_mtx_lock(nfs_buf_mutex
);
2536 if (nfs_buf_iterprepare(np
, &blist
, list
)) {
2538 if (nfs_buf_iterprepare(np
, &blist
, list
))
2541 while ((bp
= LIST_FIRST(&blist
))) {
2542 LIST_REMOVE(bp
, nb_vnbufs
);
2543 if (list
== NBI_CLEAN
)
2544 LIST_INSERT_HEAD(&np
->n_cleanblkhd
, bp
, nb_vnbufs
);
2546 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
2548 while ((error
= nfs_buf_acquire(bp
, NBAC_REMOVE
, slpflag
, slptimeo
))) {
2549 FSDBG(556, vp
, bp
, NBOFF(bp
), bp
->nb_flags
);
2550 if (error
!= EAGAIN
) {
2551 FSDBG(554, vp
, bp
, -1, error
);
2552 nfs_buf_refrele(bp
);
2553 nfs_buf_itercomplete(np
, &blist
, list
);
2554 lck_mtx_unlock(nfs_buf_mutex
);
2558 nfs_buf_refrele(bp
);
2559 FSDBG(554, vp
, bp
, NBOFF(bp
), bp
->nb_flags
);
2560 lck_mtx_unlock(nfs_buf_mutex
);
2561 if ((flags
& V_SAVE
) && UBCINFOEXISTS(vp
) && bp
->nb_vp
&&
2562 (NBOFF(bp
) < (off_t
)np
->n_size
)) {
2563 /* XXX extra paranoia: make sure we're not */
2564 /* somehow leaving any dirty data around */
2566 int end
= (NBOFF(bp
) + bp
->nb_bufsize
> (off_t
)np
->n_size
) ?
2567 ((off_t
)np
->n_size
- NBOFF(bp
)) : bp
->nb_bufsize
;
2568 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
)) {
2569 error
= nfs_buf_upl_setup(bp
);
2570 if (error
== EINVAL
) {
2571 /* vm object must no longer exist */
2572 /* hopefully we don't need to do */
2573 /* anything for this buffer */
2575 printf("nfs_vinvalbuf: upl setup failed %d\n", error
);
2576 bp
->nb_valid
= bp
->nb_dirty
= 0;
2578 nfs_buf_upl_check(bp
);
2579 /* check for any dirty data before the EOF */
2580 if (bp
->nb_dirtyend
&& bp
->nb_dirtyoff
< end
) {
2581 /* clip dirty range to EOF */
2582 if (bp
->nb_dirtyend
> end
)
2583 bp
->nb_dirtyend
= end
;
2586 bp
->nb_dirty
&= (1 << (round_page_32(end
)/PAGE_SIZE
)) - 1;
2587 /* also make sure we'll have a credential to do the write */
2588 if (mustwrite
&& (bp
->nb_wcred
== NOCRED
) && (cred
== NOCRED
)) {
2589 printf("nfs_vinvalbuf: found dirty buffer with no write creds\n");
2593 FSDBG(554, vp
, bp
, 0xd00dee, bp
->nb_flags
);
2594 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
2595 panic("nfs_vinvalbuf: dirty buffer without upl");
2596 /* gotta write out dirty data before invalidating */
2597 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2598 /* (NB_NOCACHE indicates buffer should be discarded) */
2599 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
));
2600 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
2601 if (bp
->nb_wcred
== NOCRED
) {
2602 kauth_cred_ref(cred
);
2603 bp
->nb_wcred
= cred
;
2605 error
= nfs_buf_write(bp
);
2606 // Note: bp has been released
2608 FSDBG(554, bp
, 0xd00dee, 0xbad, error
);
2609 np
->n_error
= error
;
2610 np
->n_flag
|= NWRITEERR
;
2612 * There was a write error and we need to
2613 * invalidate attrs to sync with server.
2614 * (if this write was extending the file,
2615 * we may no longer know the correct size)
2617 NATTRINVALIDATE(np
);
2620 lck_mtx_lock(nfs_buf_mutex
);
2624 SET(bp
->nb_flags
, NB_INVAL
);
2625 // hold off on FREEUPs until we're done here
2626 nfs_buf_release(bp
, 0);
2627 lck_mtx_lock(nfs_buf_mutex
);
2629 nfs_buf_itercomplete(np
, &blist
, list
);
2631 lck_mtx_unlock(nfs_buf_mutex
);
2634 panic("nfs_vinvalbuf: flush failed");
2640 * Flush and invalidate all dirty buffers. If another process is already
2641 * doing the flush, just wait for completion.
2651 struct nfsnode
*np
= VTONFS(vp
);
2652 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
2653 int error
= 0, slpflag
, slptimeo
;
2656 FSDBG_TOP(554, vp
, flags
, intrflg
, 0);
2658 if (nmp
&& ((nmp
->nm_flag
& NFSMNT_INT
) == 0))
2668 * First wait for any other process doing a flush to complete.
2670 while (np
->n_flag
& NFLUSHINPROG
) {
2671 np
->n_flag
|= NFLUSHWANT
;
2672 FSDBG_TOP(555, vp
, flags
, intrflg
, np
->n_flag
);
2673 error
= tsleep((caddr_t
)&np
->n_flag
, PRIBIO
+ 2, "nfsvinval", slptimeo
);
2674 FSDBG_BOT(555, vp
, flags
, intrflg
, np
->n_flag
);
2675 if (error
&& (error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
2676 FSDBG_BOT(554, vp
, flags
, intrflg
, error
);
2682 * Now, flush as required.
2684 np
->n_flag
|= NFLUSHINPROG
;
2685 error
= nfs_vinvalbuf_internal(vp
, flags
, cred
, p
, slpflag
, 0);
2687 FSDBG(554, vp
, 0, 0, error
);
2688 error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
);
2690 np
->n_flag
&= ~NFLUSHINPROG
;
2691 if (np
->n_flag
& NFLUSHWANT
) {
2692 np
->n_flag
&= ~NFLUSHWANT
;
2693 wakeup((caddr_t
)&np
->n_flag
);
2695 FSDBG_BOT(554, vp
, flags
, intrflg
, error
);
2698 error
= nfs_vinvalbuf_internal(vp
, flags
, cred
, p
, 0, slptimeo
);
2700 np
->n_flag
&= ~(NMODIFIED
| NFLUSHINPROG
);
2701 if (np
->n_flag
& NFLUSHWANT
) {
2702 np
->n_flag
&= ~NFLUSHWANT
;
2703 wakeup((caddr_t
)&np
->n_flag
);
2706 * get the pages out of vm also
2708 if (UBCINFOEXISTS(vp
) && (size
= ubc_getsize(vp
))) {
2709 int rv
= ubc_sync_range(vp
, 0, size
, UBC_PUSHALL
| UBC_INVALIDATE
);
2711 panic("nfs_vinvalbuf(): ubc_sync_range failed!");
2714 FSDBG_BOT(554, vp
, flags
, intrflg
, 0);
2719 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
2720 * This is mainly to avoid queueing async I/O requests when the nfsiods
2721 * are all hung on a dead server.
2724 nfs_asyncio(bp
, cred
)
2728 struct nfsmount
*nmp
;
2734 void *wakeme
= NULL
;
2737 if (nfs_numasync
== 0)
2740 FSDBG_TOP(552, bp
, bp
? NBOFF(bp
) : 0, bp
? bp
->nb_flags
: 0, 0);
2742 nmp
= ((bp
!= NULL
) ? VFSTONFS(vnode_mount(bp
->nb_vp
)) : NULL
);
2744 if (nmp
&& nmp
->nm_flag
& NFSMNT_INT
)
2748 lck_mtx_lock(nfs_iod_mutex
);
2750 /* no nfsbuf means tell nfsiod to process delwri list */
2755 * Find a free iod to process this request.
2757 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
2758 if (nfs_iodwant
[i
]) {
2760 * Found one, so wake it up and tell it which
2763 nfs_iodwant
[i
] = NULL
;
2764 nfs_iodmount
[i
] = nmp
;
2767 wakeme
= &nfs_iodwant
[i
];
2772 /* if we're just poking the delwri list, we're done */
2774 lck_mtx_unlock(nfs_iod_mutex
);
2777 FSDBG_BOT(552, bp
, 0x10101010, wakeme
, 0);
2782 * If none are free, we may already have an iod working on this mount
2783 * point. If so, it will process our request.
2786 if (nmp
->nm_bufqiods
> 0) {
2792 * If we have an iod which can process the request, then queue
2795 FSDBG(552, bp
, gotiod
, i
, nmp
->nm_bufqiods
);
2798 * Ensure that the queue never grows too large.
2800 while (nmp
->nm_bufqlen
>= 2*nfs_numasync
) {
2801 if (ISSET(bp
->nb_flags
, NB_IOD
)) {
2802 /* An nfsiod is attempting this async operation so */
2803 /* we must not fall asleep on the bufq because we */
2804 /* could be waiting on ourself. Just return error */
2805 /* and we'll do this operation syncrhonously. */
2808 FSDBG(552, bp
, nmp
->nm_bufqlen
, 2*nfs_numasync
, -1);
2809 nmp
->nm_bufqwant
= TRUE
;
2811 ts
.tv_sec
= (slptimeo
/100);
2812 /* the hz value is 100; which leads to 10ms */
2813 ts
.tv_nsec
= (slptimeo
% 100) * 10 * NSEC_PER_USEC
* 1000;
2815 error
= msleep(&nmp
->nm_bufq
, nfs_iod_mutex
, slpflag
| PRIBIO
,
2818 error2
= nfs_sigintr(nmp
, NULL
, bp
->nb_proc
);
2820 lck_mtx_unlock(nfs_iod_mutex
);
2821 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, error2
);
2824 if (slpflag
== PCATCH
) {
2830 * We might have lost our iod while sleeping,
2831 * so check and loop if nescessary.
2833 if (nmp
->nm_bufqiods
== 0) {
2834 lck_mtx_unlock(nfs_iod_mutex
);
2839 if (ISSET(bp
->nb_flags
, NB_READ
)) {
2840 if (bp
->nb_rcred
== NOCRED
&& cred
!= NOCRED
) {
2841 kauth_cred_ref(cred
);
2842 bp
->nb_rcred
= cred
;
2845 SET(bp
->nb_flags
, NB_WRITEINPROG
);
2846 if (bp
->nb_wcred
== NOCRED
&& cred
!= NOCRED
) {
2847 kauth_cred_ref(cred
);
2848 bp
->nb_wcred
= cred
;
2852 TAILQ_INSERT_TAIL(&nmp
->nm_bufq
, bp
, nb_free
);
2854 lck_mtx_unlock(nfs_iod_mutex
);
2857 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
2862 lck_mtx_unlock(nfs_iod_mutex
);
2864 * All the iods are busy on other mounts, so return EIO to
2865 * force the caller to process the i/o synchronously.
2867 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, EIO
);
2872 * Do an I/O operation to/from a cache block. This may be called
2873 * synchronously or from an nfsiod.
2876 nfs_doio(struct nfsbuf
*bp
, kauth_cred_t cr
, proc_t p
)
2881 struct nfsmount
*nmp
;
2882 int error
= 0, diff
, len
, iomode
, invalidate
= 0;
2888 vtype
= vnode_vtype(vp
);
2890 nmp
= VFSTONFS(vnode_mount(vp
));
2892 uiop
->uio_iovs
.iov32p
= &io
;
2893 uiop
->uio_iovcnt
= 1;
2894 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
2895 uiop
->uio_segflg
= UIO_SYSSPACE
;
2897 uiop
->uio_segflg
= UIO_SYSSPACE32
;
2901 * we've decided to perform I/O for this block,
2902 * so we couldn't possibly NB_DONE. So, clear it.
2904 if (ISSET(bp
->nb_flags
, NB_DONE
)) {
2905 if (!ISSET(bp
->nb_flags
, NB_ASYNC
))
2906 panic("nfs_doio: done and not async");
2907 CLR(bp
->nb_flags
, NB_DONE
);
2909 FSDBG_TOP(256, np
->n_size
, NBOFF(bp
), bp
->nb_bufsize
, bp
->nb_flags
);
2910 FSDBG(257, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
,
2913 if (ISSET(bp
->nb_flags
, NB_READ
)) {
2916 io
.iov_len
= bp
->nb_bufsize
;
2917 uio_uio_resid_set(uiop
, io
.iov_len
);
2918 io
.iov_base
= (uintptr_t) bp
->nb_data
;
2919 uiop
->uio_rw
= UIO_READ
;
2922 uiop
->uio_offset
= NBOFF(bp
);
2923 OSAddAtomic(1, (SInt32
*)&nfsstats
.read_bios
);
2924 error
= nfs_readrpc(vp
, uiop
, cr
, p
);
2925 FSDBG(262, np
->n_size
, NBOFF(bp
), uio_uio_resid(uiop
), error
);
2927 /* update valid range */
2928 bp
->nb_validoff
= 0;
2929 if (uio_uio_resid(uiop
) != 0) {
2931 * If len > 0, there is a hole in the file and
2932 * no writes after the hole have been pushed to
2934 * Just zero fill the rest of the valid area.
2936 // LP64todo - fix this
2937 diff
= bp
->nb_bufsize
- uio_uio_resid(uiop
);
2938 len
= np
->n_size
- (NBOFF(bp
) + diff
);
2940 // LP64todo - fix this
2941 len
= min(len
, uio_uio_resid(uiop
));
2942 bzero((char *)bp
->nb_data
+ diff
, len
);
2943 bp
->nb_validend
= diff
+ len
;
2944 FSDBG(258, diff
, len
, 0, 1);
2946 bp
->nb_validend
= diff
;
2948 bp
->nb_validend
= bp
->nb_bufsize
;
2949 bp
->nb_valid
= (1 << (round_page_32(bp
->nb_validend
)/PAGE_SIZE
)) - 1;
2950 if (bp
->nb_validend
& PAGE_MASK
) {
2951 /* valid range ends in the middle of a page so we */
2952 /* need to zero-fill any invalid data at the end */
2953 /* of the last page */
2954 bzero((caddr_t
)(bp
->nb_data
+ bp
->nb_validend
),
2955 bp
->nb_bufsize
- bp
->nb_validend
);
2956 FSDBG(258, bp
->nb_validend
,
2957 bp
->nb_bufsize
- bp
->nb_validend
, 0, 2);
2962 uiop
->uio_offset
= (off_t
)0;
2963 OSAddAtomic(1, (SInt32
*)&nfsstats
.readlink_bios
);
2964 error
= nfs_readlinkrpc(vp
, uiop
, cr
, p
);
2966 bp
->nb_validoff
= 0;
2967 bp
->nb_validend
= uiop
->uio_offset
;
2971 OSAddAtomic(1, (SInt32
*)&nfsstats
.readdir_bios
);
2972 uiop
->uio_offset
= NBOFF(bp
);
2973 if (!(nmp
->nm_flag
& NFSMNT_NFSV3
))
2974 nmp
->nm_flag
&= ~NFSMNT_RDIRPLUS
; /* dk@farm.org */
2975 if (nmp
->nm_flag
& NFSMNT_RDIRPLUS
) {
2976 error
= nfs_readdirplusrpc(vp
, uiop
, cr
, p
);
2977 if (error
== NFSERR_NOTSUPP
)
2978 nmp
->nm_flag
&= ~NFSMNT_RDIRPLUS
;
2980 if ((nmp
->nm_flag
& NFSMNT_RDIRPLUS
) == 0)
2981 error
= nfs_readdirrpc(vp
, uiop
, cr
, p
);
2983 bp
->nb_validoff
= 0;
2984 bp
->nb_validend
= uiop
->uio_offset
- NBOFF(bp
);
2985 bp
->nb_valid
= (1 << (round_page_32(bp
->nb_validend
)/PAGE_SIZE
)) - 1;
2989 printf("nfs_doio: type %x unexpected\n", vtype
);
2993 SET(bp
->nb_flags
, NB_ERROR
);
2994 bp
->nb_error
= error
;
2998 /* we're doing a write */
3001 /* We need to make sure the pages are locked before doing I/O. */
3002 if (!ISSET(bp
->nb_flags
, NB_META
) && UBCINFOEXISTS(vp
)) {
3003 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
)) {
3004 error
= nfs_buf_upl_setup(bp
);
3006 printf("nfs_doio: upl create failed %d\n", error
);
3007 SET(bp
->nb_flags
, NB_ERROR
);
3011 nfs_buf_upl_check(bp
);
3015 if (ISSET(bp
->nb_flags
, NB_WASDIRTY
)) {
3016 FSDBG(256, bp
, NBOFF(bp
), bp
->nb_dirty
, 0xd00dee);
3018 * There are pages marked dirty that need to be written out.
3020 * We don't want to just combine the write range with the
3021 * range of pages that are dirty because that could cause us
3022 * to write data that wasn't actually written to.
3023 * We also don't want to write data more than once.
3025 * If the dirty range just needs to be committed, we do that.
3026 * Otherwise, we write the dirty range and clear the dirty bits
3027 * for any COMPLETE pages covered by that range.
3028 * If there are dirty pages left after that, we write out the
3029 * parts that we haven't written yet.
3034 * If NB_NEEDCOMMIT is set, a commit rpc may do the trick. If not
3035 * an actual write will have to be done.
3036 * If NB_WRITEINPROG is already set, then push it with a write anyhow.
3038 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
3039 nfs_buf_check_write_verifier(np
, bp
);
3040 if ((bp
->nb_flags
& (NB_NEEDCOMMIT
| NB_WRITEINPROG
)) == NB_NEEDCOMMIT
) {
3041 doff
= NBOFF(bp
) + bp
->nb_dirtyoff
;
3042 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3043 error
= nfs_commit(vp
, doff
, bp
->nb_dirtyend
- bp
->nb_dirtyoff
,
3044 bp
->nb_wcred
, bp
->nb_proc
);
3045 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3047 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3048 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3049 np
->n_needcommitcnt
--;
3050 CHECK_NEEDCOMMITCNT(np
);
3054 if (!error
&& bp
->nb_dirtyend
> 0) {
3055 /* there's a dirty range that needs to be written out */
3057 int firstpg
, lastpg
;
3059 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
3060 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3064 doff
= bp
->nb_dirtyoff
;
3065 dend
= bp
->nb_dirtyend
;
3067 /* if doff page is dirty, move doff to start of page */
3068 if (NBPGDIRTY(bp
,doff
/PAGE_SIZE
))
3069 doff
-= doff
& PAGE_MASK
;
3070 /* try to expand write range to include preceding dirty pages */
3071 if (!(doff
& PAGE_MASK
))
3072 while (doff
> 0 && NBPGDIRTY(bp
,(doff
-1)/PAGE_SIZE
))
3074 /* if dend page is dirty, move dend to start of next page */
3075 if ((dend
& PAGE_MASK
) && NBPGDIRTY(bp
,dend
/PAGE_SIZE
))
3076 dend
= round_page_32(dend
);
3077 /* try to expand write range to include trailing dirty pages */
3078 if (!(dend
& PAGE_MASK
))
3079 while (dend
< bp
->nb_bufsize
&& NBPGDIRTY(bp
,dend
/PAGE_SIZE
))
3081 /* make sure to keep dend clipped to EOF */
3082 if (NBOFF(bp
) + dend
> (off_t
)np
->n_size
)
3083 dend
= np
->n_size
- NBOFF(bp
);
3084 /* calculate range of complete pages being written */
3085 firstpg
= round_page_32(doff
) / PAGE_SIZE
;
3086 lastpg
= (trunc_page_32(dend
) - 1)/ PAGE_SIZE
;
3087 /* calculate mask for that page range */
3088 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
3090 /* compare page mask to nb_dirty; if there are other dirty pages */
3091 /* then write FILESYNC; otherwise, write UNSTABLE if async and */
3092 /* not needcommit/nocache/call; otherwise write FILESYNC */
3093 if (bp
->nb_dirty
& ~pagemask
)
3094 iomode
= NFSV3WRITE_FILESYNC
;
3095 else if ((bp
->nb_flags
& (NB_ASYNC
| NB_NEEDCOMMIT
| NB_NOCACHE
| NB_STABLE
)) == NB_ASYNC
)
3096 iomode
= NFSV3WRITE_UNSTABLE
;
3098 iomode
= NFSV3WRITE_FILESYNC
;
3100 /* write the dirty range */
3101 io
.iov_len
= dend
- doff
;
3102 uio_uio_resid_set(uiop
, io
.iov_len
);
3103 uiop
->uio_offset
= NBOFF(bp
) + doff
;
3104 io
.iov_base
= (uintptr_t) bp
->nb_data
+ doff
;
3105 uiop
->uio_rw
= UIO_WRITE
;
3107 OSAddAtomic(1, (SInt32
*)&nfsstats
.write_bios
);
3109 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3110 error
= nfs_writerpc(vp
, uiop
, cr
, p
, &iomode
, &bp
->nb_verf
);
3111 /* clear dirty bits for pages we've written */
3113 bp
->nb_dirty
&= ~pagemask
;
3114 /* set/clear needcommit flag */
3115 if (!error
&& iomode
== NFSV3WRITE_UNSTABLE
) {
3116 if (!ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
3117 np
->n_needcommitcnt
++;
3118 SET(bp
->nb_flags
, NB_NEEDCOMMIT
);
3119 /* make sure nb_dirtyoff/nb_dirtyend reflect actual range written */
3120 bp
->nb_dirtyoff
= doff
;
3121 bp
->nb_dirtyend
= dend
;
3123 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3124 np
->n_needcommitcnt
--;
3125 CHECK_NEEDCOMMITCNT(np
);
3127 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3129 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3131 * For an interrupted write, the buffer is still valid and the write
3132 * hasn't been pushed to the server yet, so we can't set NB_ERROR and
3133 * report the interruption by setting NB_EINTR. For the NB_ASYNC case,
3134 * NB_EINTR is not relevant.
3136 * For the case of a V3 write rpc not being committed to stable
3137 * storage, the block is still dirty and requires either a commit rpc
3138 * or another write rpc with iomode == NFSV3WRITE_FILESYNC before the
3139 * block is reused. This is indicated by setting the NB_DELWRI and
3140 * NB_NEEDCOMMIT flags.
3142 if (error
== EINTR
|| (!error
&& bp
->nb_flags
& NB_NEEDCOMMIT
)) {
3143 CLR(bp
->nb_flags
, NB_INVAL
| NB_NOCACHE
);
3144 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3145 SET(bp
->nb_flags
, NB_DELWRI
);
3146 OSAddAtomic(1, (SInt32
*)&nfs_nbdwrite
);
3149 FSDBG(261, bp
->nb_validoff
, bp
->nb_validend
,
3152 * Since for the NB_ASYNC case, nfs_bwrite() has
3153 * reassigned the buffer to the clean list, we have to
3154 * reassign it back to the dirty one. Ugh.
3156 if (ISSET(bp
->nb_flags
, NB_ASYNC
)) {
3157 /* move to dirty list */
3158 lck_mtx_lock(nfs_buf_mutex
);
3159 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
3160 LIST_REMOVE(bp
, nb_vnbufs
);
3161 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
3162 lck_mtx_unlock(nfs_buf_mutex
);
3164 SET(bp
->nb_flags
, NB_EINTR
);
3167 /* either there's an error or we don't need to commit */
3169 SET(bp
->nb_flags
, NB_ERROR
);
3170 bp
->nb_error
= np
->n_error
= error
;
3171 np
->n_flag
|= NWRITEERR
;
3173 * There was a write error and we need to
3174 * invalidate attrs and flush buffers in
3175 * order to sync up with the server.
3176 * (if this write was extending the file,
3177 * we may no longer know the correct size)
3179 * But we can't call vinvalbuf while holding
3180 * this buffer busy. Set a flag to do it after
3181 * releasing the buffer.
3183 * Note we can only invalidate in this function
3184 * if this is an async write and so the iodone
3185 * below will release the buffer. Also, we
3186 * shouldn't call vinvalbuf from nfsiod because
3187 * that may deadlock waiting for the completion
3188 * of writes that are queued up behind this one.
3190 if (ISSET(bp
->nb_flags
, NB_ASYNC
) &&
3191 !ISSET(bp
->nb_flags
, NB_IOD
)) {
3194 /* invalidate later */
3195 np
->n_flag
|= NNEEDINVALIDATE
;
3197 NATTRINVALIDATE(np
);
3199 /* clear the dirty range */
3200 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3204 if (!error
&& bp
->nb_dirty
) {
3205 /* there are pages marked dirty that need to be written out */
3206 int pg
, count
, npages
, off
;
3208 OSAddAtomic(1, (SInt32
*)&nfsstats
.write_bios
);
3213 * we do these writes synchronously because we can't really
3214 * support the unstable/needommit method. We could write
3215 * them unstable, clear the dirty bits, and then commit the
3216 * whole block later, but if we need to rewrite the data, we
3217 * won't have any idea which pages were written because that
3218 * info can't be stored in the nb_dirtyoff/nb_dirtyend. We
3219 * also can't leave the dirty bits set because then we wouldn't
3220 * be able to tell if the pages were re-dirtied between the end
3221 * of the write and the commit.
3223 iomode
= NFSV3WRITE_FILESYNC
;
3224 uiop
->uio_rw
= UIO_WRITE
;
3226 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3227 npages
= bp
->nb_bufsize
/PAGE_SIZE
;
3228 for (pg
=0; pg
< npages
; pg
++) {
3229 if (!NBPGDIRTY(bp
,pg
))
3232 while (((pg
+count
) < npages
) && NBPGDIRTY(bp
,pg
+count
))
3234 /* write count pages starting with page pg */
3235 off
= pg
* PAGE_SIZE
;
3236 len
= count
* PAGE_SIZE
;
3238 /* clip writes to EOF */
3239 if (NBOFF(bp
) + off
+ len
> (off_t
)np
->n_size
)
3240 len
-= (NBOFF(bp
) + off
+ len
) - np
->n_size
;
3243 uio_uio_resid_set(uiop
, io
.iov_len
);
3244 uiop
->uio_offset
= NBOFF(bp
) + off
;
3245 io
.iov_base
= (uintptr_t) bp
->nb_data
+ off
;
3246 error
= nfs_writerpc(vp
, uiop
, cr
, p
, &iomode
, &bp
->nb_verf
);
3250 /* clear dirty bits */
3252 bp
->nb_dirty
&= ~(1 << pg
);
3253 /* leave pg on last page */
3258 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3259 np
->n_needcommitcnt
--;
3260 CHECK_NEEDCOMMITCNT(np
);
3262 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3264 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3265 FSDBG_BOT(256, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_bufsize
,
3270 SET(bp
->nb_flags
, NB_ERROR
);
3271 bp
->nb_error
= error
;
3275 FSDBG_BOT(256, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_bufsize
, error
);
3281 * There was a write error and we need to
3282 * invalidate attrs and flush buffers in
3283 * order to sync up with the server.
3284 * (if this write was extending the file,
3285 * we may no longer know the correct size)
3287 * But we couldn't call vinvalbuf while holding
3288 * the buffer busy. So we call vinvalbuf() after
3289 * releasing the buffer.
3291 * Note: we don't bother calling nfs_vinvalbuf() if
3292 * there's already a flush in progress.
3294 if (!(np
->n_flag
& NFLUSHINPROG
))
3295 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cr
, p
, 1);