2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_bio.c,v 1.44 1997/09/10 19:52:25 phk Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/resourcevar.h>
64 #include <sys/signalvar.h>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
67 #include <sys/malloc.h>
68 #include <sys/vnode.h>
69 #include <sys/dirent.h>
70 #include <sys/mount_internal.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/ubc_internal.h>
74 #include <sys/uio_internal.h>
77 #include <sys/vmparam.h>
80 #include <kern/clock.h>
81 #include <libkern/OSAtomic.h>
82 #include <kern/kalloc.h>
84 #include <nfs/rpcv2.h>
85 #include <nfs/nfsproto.h>
87 #include <nfs/nfsmount.h>
88 #include <nfs/nfsnode.h>
89 #include <sys/buf_internal.h>
91 #include <sys/kdebug.h>
93 #define FSDBG(A, B, C, D, E) \
94 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
95 (int)(B), (int)(C), (int)(D), (int)(E), 0)
96 #define FSDBG_TOP(A, B, C, D, E) \
97 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
98 (int)(B), (int)(C), (int)(D), (int)(E), 0)
99 #define FSDBG_BOT(A, B, C, D, E) \
100 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
101 (int)(B), (int)(C), (int)(D), (int)(E), 0)
103 extern int nfs_numasync
;
104 extern int nfs_ioddelwri
;
105 extern struct nfsstats nfsstats
;
107 #define NFSBUFHASH(np, lbn) \
108 (&nfsbufhashtbl[((long)(np) / sizeof(*(np)) + (int)(lbn)) & nfsbufhash])
109 LIST_HEAD(nfsbufhashhead
, nfsbuf
) *nfsbufhashtbl
;
110 struct nfsbuffreehead nfsbuffree
, nfsbuffreemeta
, nfsbufdelwri
;
112 int nfsbufcnt
, nfsbufmin
, nfsbufmax
, nfsbufmetacnt
, nfsbufmetamax
;
113 int nfsbuffreecnt
, nfsbuffreemetacnt
, nfsbufdelwricnt
, nfsneedbuffer
;
115 time_t nfsbuffreeuptimestamp
;
117 lck_grp_t
*nfs_buf_lck_grp
;
118 lck_grp_attr_t
*nfs_buf_lck_grp_attr
;
119 lck_attr_t
*nfs_buf_lck_attr
;
120 lck_mtx_t
*nfs_buf_mutex
;
122 #define NFSBUFWRITE_THROTTLE 9
123 #define NFSBUF_LRU_STALE 120
124 #define NFSBUF_META_STALE 240
126 /* number of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffree list */
127 #define LRU_TO_FREEUP 6
128 /* number of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffreemeta list */
129 #define META_TO_FREEUP 3
130 /* total number of nfsbufs nfs_buf_freeup() should attempt to free */
131 #define TOTAL_TO_FREEUP (LRU_TO_FREEUP+META_TO_FREEUP)
132 /* fraction of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffree list when called from nfs_timer() */
133 #define LRU_FREEUP_FRAC_ON_TIMER 8
134 /* fraction of nfsbufs nfs_buf_freeup() should attempt to free from nfsbuffreemeta list when called from nfs_timer() */
135 #define META_FREEUP_FRAC_ON_TIMER 16
136 /* fraction of total nfsbufs that nfsbuffreecnt should exceed before bothering to call nfs_buf_freeup() */
137 #define LRU_FREEUP_MIN_FRAC 4
138 /* fraction of total nfsbufs that nfsbuffreemetacnt should exceed before bothering to call nfs_buf_freeup() */
139 #define META_FREEUP_MIN_FRAC 2
141 #define NFS_BUF_FREEUP() \
143 /* only call nfs_buf_freeup() if it has work to do: */ \
144 if (((nfsbuffreecnt > nfsbufcnt/LRU_FREEUP_MIN_FRAC) || \
145 (nfsbuffreemetacnt > nfsbufcnt/META_FREEUP_MIN_FRAC)) && \
146 ((nfsbufcnt - TOTAL_TO_FREEUP) > nfsbufmin)) \
151 * Initialize nfsbuf lists
156 nfs_buf_lck_grp_attr
= lck_grp_attr_alloc_init();
157 lck_grp_attr_setstat(nfs_buf_lck_grp_attr
);
158 nfs_buf_lck_grp
= lck_grp_alloc_init("nfs_buf", nfs_buf_lck_grp_attr
);
160 nfs_buf_lck_attr
= lck_attr_alloc_init();
162 nfs_buf_mutex
= lck_mtx_alloc_init(nfs_buf_lck_grp
, nfs_buf_lck_attr
);
164 nfsbufcnt
= nfsbufmetacnt
=
165 nfsbuffreecnt
= nfsbuffreemetacnt
= nfsbufdelwricnt
= 0;
167 nfsbufmax
= (sane_size
>> PAGE_SHIFT
) / 4;
168 nfsbufmetamax
= (sane_size
>> PAGE_SHIFT
) / 16;
171 nfsbuffreeuptimestamp
= 0;
173 nfsbufhashtbl
= hashinit(nfsbufmax
/4, M_TEMP
, &nfsbufhash
);
174 TAILQ_INIT(&nfsbuffree
);
175 TAILQ_INIT(&nfsbuffreemeta
);
176 TAILQ_INIT(&nfsbufdelwri
);
181 * try to free up some excess, unused nfsbufs
184 nfs_buf_freeup(int timer
)
189 struct nfsbuffreehead nfsbuffreeup
;
191 TAILQ_INIT(&nfsbuffreeup
);
193 lck_mtx_lock(nfs_buf_mutex
);
196 nfsbuffreeuptimestamp
= now
.tv_sec
;
198 FSDBG(320, nfsbufcnt
, nfsbuffreecnt
, nfsbuffreemetacnt
, 0);
200 count
= timer
? nfsbuffreecnt
/LRU_FREEUP_FRAC_ON_TIMER
: LRU_TO_FREEUP
;
201 while ((nfsbufcnt
> nfsbufmin
) && (count
-- > 0)) {
202 fbp
= TAILQ_FIRST(&nfsbuffree
);
207 if (NBUFSTAMPVALID(fbp
) &&
208 (fbp
->nb_timestamp
+ (2*NFSBUF_LRU_STALE
)) > now
.tv_sec
)
210 nfs_buf_remfree(fbp
);
211 /* disassociate buffer from any vnode */
213 if (fbp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
214 LIST_REMOVE(fbp
, nb_vnbufs
);
215 fbp
->nb_vnbufs
.le_next
= NFSNOLIST
;
219 LIST_REMOVE(fbp
, nb_hash
);
220 TAILQ_INSERT_TAIL(&nfsbuffreeup
, fbp
, nb_free
);
224 count
= timer
? nfsbuffreemetacnt
/META_FREEUP_FRAC_ON_TIMER
: META_TO_FREEUP
;
225 while ((nfsbufcnt
> nfsbufmin
) && (count
-- > 0)) {
226 fbp
= TAILQ_FIRST(&nfsbuffreemeta
);
231 if (NBUFSTAMPVALID(fbp
) &&
232 (fbp
->nb_timestamp
+ (2*NFSBUF_META_STALE
)) > now
.tv_sec
)
234 nfs_buf_remfree(fbp
);
235 /* disassociate buffer from any vnode */
237 if (fbp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
238 LIST_REMOVE(fbp
, nb_vnbufs
);
239 fbp
->nb_vnbufs
.le_next
= NFSNOLIST
;
243 LIST_REMOVE(fbp
, nb_hash
);
244 TAILQ_INSERT_TAIL(&nfsbuffreeup
, fbp
, nb_free
);
249 FSDBG(320, nfsbufcnt
, nfsbuffreecnt
, nfsbuffreemetacnt
, 0);
252 lck_mtx_unlock(nfs_buf_mutex
);
254 while ((fbp
= TAILQ_FIRST(&nfsbuffreeup
))) {
255 TAILQ_REMOVE(&nfsbuffreeup
, fbp
, nb_free
);
257 if (fbp
->nb_rcred
!= NOCRED
) {
258 kauth_cred_rele(fbp
->nb_rcred
);
259 fbp
->nb_rcred
= NOCRED
;
261 if (fbp
->nb_wcred
!= NOCRED
) {
262 kauth_cred_rele(fbp
->nb_wcred
);
263 fbp
->nb_wcred
= NOCRED
;
265 /* if buf was NB_META, dump buffer */
266 if (ISSET(fbp
->nb_flags
, NB_META
) && fbp
->nb_data
)
267 kfree(fbp
->nb_data
, fbp
->nb_bufsize
);
274 * remove a buffer from the freelist
275 * (must be called with nfs_buf_mutex held)
278 nfs_buf_remfree(struct nfsbuf
*bp
)
280 if (bp
->nb_free
.tqe_next
== NFSNOLIST
)
281 panic("nfsbuf not on free list");
282 if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
284 TAILQ_REMOVE(&nfsbufdelwri
, bp
, nb_free
);
285 } else if (ISSET(bp
->nb_flags
, NB_META
)) {
287 TAILQ_REMOVE(&nfsbuffreemeta
, bp
, nb_free
);
290 TAILQ_REMOVE(&nfsbuffree
, bp
, nb_free
);
292 bp
->nb_free
.tqe_next
= NFSNOLIST
;
297 * check for existence of nfsbuf in cache
300 nfs_buf_is_incore(vnode_t vp
, daddr64_t blkno
)
303 lck_mtx_lock(nfs_buf_mutex
);
304 if (nfs_buf_incore(vp
, blkno
))
308 lck_mtx_unlock(nfs_buf_mutex
);
313 * return incore buffer (must be called with nfs_buf_mutex held)
316 nfs_buf_incore(vnode_t vp
, daddr64_t blkno
)
318 /* Search hash chain */
319 struct nfsbuf
* bp
= NFSBUFHASH(VTONFS(vp
), blkno
)->lh_first
;
320 for (; bp
!= NULL
; bp
= bp
->nb_hash
.le_next
)
321 if (bp
->nb_lblkno
== blkno
&& bp
->nb_vp
== vp
) {
322 if (!ISSET(bp
->nb_flags
, NB_INVAL
)) {
323 FSDBG(547, bp
, blkno
, bp
->nb_flags
, bp
->nb_vp
);
331 * Check if it's OK to drop a page.
333 * Called by vnode_pager() on pageout request of non-dirty page.
334 * We need to make sure that it's not part of a delayed write.
335 * If it is, we can't let the VM drop it because we may need it
336 * later when/if we need to write the data (again).
339 nfs_buf_page_inval(vnode_t vp
, off_t offset
)
344 lck_mtx_lock(nfs_buf_mutex
);
345 bp
= nfs_buf_incore(vp
, ubc_offtoblk(vp
, offset
));
348 FSDBG(325, bp
, bp
->nb_flags
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
349 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
354 * If there's a dirty range in the buffer, check to
355 * see if this page intersects with the dirty range.
356 * If it does, we can't let the pager drop the page.
358 if (bp
->nb_dirtyend
> 0) {
359 int start
= offset
- NBOFF(bp
);
360 if (bp
->nb_dirtyend
<= start
||
361 bp
->nb_dirtyoff
>= (start
+ PAGE_SIZE
))
367 lck_mtx_unlock(nfs_buf_mutex
);
372 * set up the UPL for a buffer
373 * (must NOT be called with nfs_buf_mutex held)
376 nfs_buf_upl_setup(struct nfsbuf
*bp
)
382 if (ISSET(bp
->nb_flags
, NB_PAGELIST
))
385 upl_flags
= UPL_PRECIOUS
;
386 if (! ISSET(bp
->nb_flags
, NB_READ
)) {
388 * We're doing a "write", so we intend to modify
389 * the pages we're gathering.
391 upl_flags
|= UPL_WILL_MODIFY
;
393 kret
= ubc_create_upl(bp
->nb_vp
, NBOFF(bp
), bp
->nb_bufsize
,
394 &upl
, NULL
, upl_flags
);
395 if (kret
== KERN_INVALID_ARGUMENT
) {
396 /* vm object probably doesn't exist any more */
397 bp
->nb_pagelist
= NULL
;
400 if (kret
!= KERN_SUCCESS
) {
401 printf("nfs_buf_upl_setup(): failed to get pagelist %d\n", kret
);
402 bp
->nb_pagelist
= NULL
;
406 FSDBG(538, bp
, NBOFF(bp
), bp
->nb_bufsize
, bp
->nb_vp
);
408 bp
->nb_pagelist
= upl
;
409 SET(bp
->nb_flags
, NB_PAGELIST
);
414 * update buffer's valid/dirty info from UBC
415 * (must NOT be called with nfs_buf_mutex held)
418 nfs_buf_upl_check(struct nfsbuf
*bp
)
421 off_t filesize
, fileoffset
;
424 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
427 npages
= round_page_32(bp
->nb_bufsize
) / PAGE_SIZE
;
428 filesize
= ubc_getsize(bp
->nb_vp
);
429 fileoffset
= NBOFF(bp
);
430 if (fileoffset
< filesize
)
431 SET(bp
->nb_flags
, NB_CACHE
);
433 CLR(bp
->nb_flags
, NB_CACHE
);
435 pl
= ubc_upl_pageinfo(bp
->nb_pagelist
);
436 bp
->nb_valid
= bp
->nb_dirty
= 0;
438 for (i
=0; i
< npages
; i
++, fileoffset
+= PAGE_SIZE_64
) {
439 /* anything beyond the end of the file is not valid or dirty */
440 if (fileoffset
>= filesize
)
442 if (!upl_valid_page(pl
, i
)) {
443 CLR(bp
->nb_flags
, NB_CACHE
);
447 if (upl_dirty_page(pl
, i
)) {
448 NBPGDIRTY_SET(bp
, i
);
449 if (!ISSET(bp
->nb_flags
, NB_WASDIRTY
))
450 SET(bp
->nb_flags
, NB_WASDIRTY
);
453 fileoffset
= NBOFF(bp
);
454 if (ISSET(bp
->nb_flags
, NB_CACHE
)) {
456 bp
->nb_validend
= bp
->nb_bufsize
;
457 if (fileoffset
+ bp
->nb_validend
> filesize
)
458 bp
->nb_validend
= filesize
- fileoffset
;
460 bp
->nb_validoff
= bp
->nb_validend
= -1;
462 FSDBG(539, bp
, fileoffset
, bp
->nb_valid
, bp
->nb_dirty
);
463 FSDBG(539, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
467 * make sure that a buffer is mapped
468 * (must NOT be called with nfs_buf_mutex held)
471 nfs_buf_map(struct nfsbuf
*bp
)
477 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
480 kret
= ubc_upl_map(bp
->nb_pagelist
, (vm_address_t
*)&(bp
->nb_data
));
481 if (kret
!= KERN_SUCCESS
)
482 panic("nfs_buf_map: ubc_upl_map() failed with (%d)", kret
);
483 if (bp
->nb_data
== 0)
484 panic("ubc_upl_map mapped 0");
485 FSDBG(540, bp
, bp
->nb_flags
, NBOFF(bp
), bp
->nb_data
);
490 * check range of pages in nfsbuf's UPL for validity
493 nfs_buf_upl_valid_range(struct nfsbuf
*bp
, int off
, int size
)
495 off_t fileoffset
, filesize
;
499 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
501 pl
= ubc_upl_pageinfo(bp
->nb_pagelist
);
503 size
+= off
& PAGE_MASK
;
505 fileoffset
= NBOFF(bp
);
506 filesize
= VTONFS(bp
->nb_vp
)->n_size
;
507 if ((fileoffset
+ off
+ size
) > filesize
)
508 size
= filesize
- (fileoffset
+ off
);
511 lastpg
= (off
+ size
- 1)/PAGE_SIZE
;
512 while (pg
<= lastpg
) {
513 if (!upl_valid_page(pl
, pg
))
521 * normalize an nfsbuf's valid range
523 * the read/write code guarantees that we'll always have a valid
524 * region that is an integral number of pages. If either end
525 * of the valid range isn't page-aligned, it gets corrected
526 * here as we extend the valid range through all of the
527 * contiguous valid pages.
530 nfs_buf_normalize_valid_range(struct nfsnode
*np
, struct nfsbuf
*bp
)
533 /* pull validoff back to start of contiguous valid page range */
534 pg
= bp
->nb_validoff
/PAGE_SIZE
;
535 while (pg
>= 0 && NBPGVALID(bp
,pg
))
537 bp
->nb_validoff
= (pg
+1) * PAGE_SIZE
;
538 /* push validend forward to end of contiguous valid page range */
539 npg
= bp
->nb_bufsize
/PAGE_SIZE
;
540 pg
= bp
->nb_validend
/PAGE_SIZE
;
541 while (pg
< npg
&& NBPGVALID(bp
,pg
))
543 bp
->nb_validend
= pg
* PAGE_SIZE
;
545 if (NBOFF(bp
) + bp
->nb_validend
> (off_t
)np
->n_size
)
546 bp
->nb_validend
= np
->n_size
% bp
->nb_bufsize
;
550 * try to push out some delayed/uncommitted writes
551 * ("locked" indicates whether nfs_buf_mutex is already held)
554 nfs_buf_delwri_push(int locked
)
559 if (TAILQ_EMPTY(&nfsbufdelwri
))
562 /* first try to tell the nfsiods to do it */
563 if (nfs_asyncio(NULL
, NULL
) == 0)
566 /* otherwise, try to do some of the work ourselves */
569 lck_mtx_lock(nfs_buf_mutex
);
570 while (i
< 8 && (bp
= TAILQ_FIRST(&nfsbufdelwri
)) != NULL
) {
571 struct nfsnode
*np
= VTONFS(bp
->nb_vp
);
574 while ((error
= nfs_buf_acquire(bp
, 0, 0, 0)) == EAGAIN
);
579 /* buffer is no longer valid */
583 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
584 nfs_buf_check_write_verifier(np
, bp
);
585 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
586 /* put buffer at end of delwri list */
587 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
590 lck_mtx_unlock(nfs_buf_mutex
);
591 nfs_flushcommits(np
->n_vnode
, NULL
, 1);
593 SET(bp
->nb_flags
, NB_ASYNC
);
594 lck_mtx_unlock(nfs_buf_mutex
);
598 lck_mtx_lock(nfs_buf_mutex
);
601 lck_mtx_unlock(nfs_buf_mutex
);
607 * Returns errno on error, 0 otherwise.
608 * Any buffer is returned in *bpp.
610 * If NBLK_ONLYVALID is set, only return buffer if found in cache.
611 * If NBLK_NOWAIT is set, don't wait for the buffer if it's marked BUSY.
613 * Check for existence of buffer in cache.
614 * Or attempt to reuse a buffer from one of the free lists.
615 * Or allocate a new buffer if we haven't already hit max allocation.
616 * Or wait for a free buffer.
618 * If available buffer found, prepare it, and return it.
620 * If the calling process is interrupted by a signal for
621 * an interruptible mount point, return EINTR.
632 struct nfsnode
*np
= VTONFS(vp
);
634 int biosize
, bufsize
;
636 int slpflag
= PCATCH
;
637 int operation
= (flags
& NBLK_OPMASK
);
641 FSDBG_TOP(541, vp
, blkno
, size
, flags
);
645 if (bufsize
> MAXBSIZE
)
646 panic("nfs_buf_get: buffer larger than MAXBSIZE requested");
648 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
650 if (UBCINVALID(vp
) || !UBCINFOEXISTS(vp
)) {
651 operation
= NBLK_META
;
652 } else if (bufsize
< biosize
) {
653 /* reg files should always have biosize blocks */
657 /* if NBLK_WRITE, check for too many delayed/uncommitted writes */
658 if ((operation
== NBLK_WRITE
) && (nfs_nbdwrite
> ((nfsbufcnt
*3)/4))) {
659 FSDBG_TOP(542, vp
, blkno
, nfs_nbdwrite
, ((nfsbufcnt
*3)/4));
661 /* poke the delwri list */
662 nfs_buf_delwri_push(0);
664 /* sleep to let other threads run... */
665 tsleep(&nfs_nbdwrite
, PCATCH
, "nfs_nbdwrite", 1);
666 FSDBG_BOT(542, vp
, blkno
, nfs_nbdwrite
, ((nfsbufcnt
*3)/4));
670 lck_mtx_lock(nfs_buf_mutex
);
672 /* check for existence of nfsbuf in cache */
673 if ((bp
= nfs_buf_incore(vp
, blkno
))) {
674 /* if busy, set wanted and wait */
675 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
676 if (flags
& NBLK_NOWAIT
) {
677 lck_mtx_unlock(nfs_buf_mutex
);
678 FSDBG_BOT(541, vp
, blkno
, bp
, 0xbcbcbcbc);
681 FSDBG_TOP(543, vp
, blkno
, bp
, bp
->nb_flags
);
682 SET(bp
->nb_lflags
, NBL_WANTED
);
686 msleep(bp
, nfs_buf_mutex
, slpflag
|(PRIBIO
+1)|PDROP
,
687 "nfsbufget", (slpflag
== PCATCH
) ? 0 : &ts
);
689 FSDBG_BOT(543, vp
, blkno
, bp
, bp
->nb_flags
);
690 if ((error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
691 FSDBG_BOT(541, vp
, blkno
, 0, error
);
696 if (bp
->nb_bufsize
!= bufsize
)
697 panic("nfsbuf size mismatch");
698 SET(bp
->nb_lflags
, NBL_BUSY
);
699 SET(bp
->nb_flags
, NB_CACHE
);
701 /* additional paranoia: */
702 if (ISSET(bp
->nb_flags
, NB_PAGELIST
))
703 panic("pagelist buffer was not busy");
707 if (flags
& NBLK_ONLYVALID
) {
708 lck_mtx_unlock(nfs_buf_mutex
);
709 FSDBG_BOT(541, vp
, blkno
, 0, 0x0000cace);
714 * where to get a free buffer:
715 * - if meta and maxmeta reached, must reuse meta
716 * - alloc new if we haven't reached min bufs
717 * - if free lists are NOT empty
718 * - if free list is stale, use it
719 * - else if freemeta list is stale, use it
720 * - else if max bufs allocated, use least-time-to-stale
721 * - alloc new if we haven't reached max allowed
722 * - start clearing out delwri list and try again
725 if ((operation
== NBLK_META
) && (nfsbufmetacnt
>= nfsbufmetamax
)) {
726 /* if we've hit max meta buffers, must reuse a meta buffer */
727 bp
= TAILQ_FIRST(&nfsbuffreemeta
);
728 } else if ((nfsbufcnt
> nfsbufmin
) &&
729 (!TAILQ_EMPTY(&nfsbuffree
) || !TAILQ_EMPTY(&nfsbuffreemeta
))) {
730 /* try to pull an nfsbuf off a free list */
731 struct nfsbuf
*lrubp
, *metabp
;
735 /* if the next LRU or META buffer is invalid or stale, use it */
736 lrubp
= TAILQ_FIRST(&nfsbuffree
);
737 if (lrubp
&& (!NBUFSTAMPVALID(lrubp
) ||
738 ((lrubp
->nb_timestamp
+ NFSBUF_LRU_STALE
) < now
.tv_sec
)))
740 metabp
= TAILQ_FIRST(&nfsbuffreemeta
);
741 if (!bp
&& metabp
&& (!NBUFSTAMPVALID(metabp
) ||
742 ((metabp
->nb_timestamp
+ NFSBUF_META_STALE
) < now
.tv_sec
)))
745 if (!bp
&& (nfsbufcnt
>= nfsbufmax
)) {
746 /* we've already allocated all bufs, so */
747 /* choose the buffer that'll go stale first */
753 int32_t lru_stale_time
, meta_stale_time
;
754 lru_stale_time
= lrubp
->nb_timestamp
+ NFSBUF_LRU_STALE
;
755 meta_stale_time
= metabp
->nb_timestamp
+ NFSBUF_META_STALE
;
756 if (lru_stale_time
<= meta_stale_time
)
765 /* we have a buffer to reuse */
766 FSDBG(544, vp
, blkno
, bp
, bp
->nb_flags
);
768 if (ISSET(bp
->nb_flags
, NB_DELWRI
))
769 panic("nfs_buf_get: delwri");
770 SET(bp
->nb_lflags
, NBL_BUSY
);
771 /* disassociate buffer from previous vnode */
773 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
774 LIST_REMOVE(bp
, nb_vnbufs
);
775 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
779 LIST_REMOVE(bp
, nb_hash
);
780 /* nuke any creds we're holding */
782 if (cred
!= NOCRED
) {
783 bp
->nb_rcred
= NOCRED
;
784 kauth_cred_rele(cred
);
787 if (cred
!= NOCRED
) {
788 bp
->nb_wcred
= NOCRED
;
789 kauth_cred_rele(cred
);
791 /* if buf will no longer be NB_META, dump old buffer */
792 if (operation
== NBLK_META
) {
793 if (!ISSET(bp
->nb_flags
, NB_META
))
795 } else if (ISSET(bp
->nb_flags
, NB_META
)) {
797 kfree(bp
->nb_data
, bp
->nb_bufsize
);
802 /* re-init buf fields */
804 bp
->nb_validoff
= bp
->nb_validend
= -1;
805 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
810 /* no buffer to reuse */
811 if ((nfsbufcnt
< nfsbufmax
) &&
812 ((operation
!= NBLK_META
) || (nfsbufmetacnt
< nfsbufmetamax
))) {
813 /* just alloc a new one */
814 MALLOC(bp
, struct nfsbuf
*, sizeof(struct nfsbuf
), M_TEMP
, M_WAITOK
);
816 lck_mtx_unlock(nfs_buf_mutex
);
817 FSDBG_BOT(541, vp
, blkno
, 0, error
);
821 if (operation
== NBLK_META
)
825 bzero(bp
, sizeof(*bp
));
826 bp
->nb_free
.tqe_next
= NFSNOLIST
;
827 bp
->nb_validoff
= bp
->nb_validend
= -1;
828 FSDBG(545, vp
, blkno
, bp
, 0);
830 /* too many bufs... wait for buffers to free up */
831 FSDBG_TOP(546, vp
, blkno
, nfsbufcnt
, nfsbufmax
);
833 /* poke the delwri list */
834 nfs_buf_delwri_push(1);
837 msleep(&nfsneedbuffer
, nfs_buf_mutex
, PCATCH
|PDROP
,
839 FSDBG_BOT(546, vp
, blkno
, nfsbufcnt
, nfsbufmax
);
840 if ((error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
841 FSDBG_BOT(541, vp
, blkno
, 0, error
);
849 bp
->nb_lflags
= NBL_BUSY
;
851 bp
->nb_lblkno
= blkno
;
852 /* insert buf in hash */
853 LIST_INSERT_HEAD(NFSBUFHASH(np
, blkno
), bp
, nb_hash
);
854 /* associate buffer with new vnode */
856 LIST_INSERT_HEAD(&np
->n_cleanblkhd
, bp
, nb_vnbufs
);
861 lck_mtx_unlock(nfs_buf_mutex
);
865 SET(bp
->nb_flags
, NB_META
);
866 if ((bp
->nb_bufsize
!= bufsize
) && bp
->nb_data
) {
867 kfree(bp
->nb_data
, bp
->nb_bufsize
);
869 bp
->nb_validoff
= bp
->nb_validend
= -1;
870 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
873 CLR(bp
->nb_flags
, NB_CACHE
);
876 bp
->nb_data
= kalloc(bufsize
);
878 /* Ack! couldn't allocate the data buffer! */
879 /* cleanup buffer and return error */
880 lck_mtx_lock(nfs_buf_mutex
);
881 LIST_REMOVE(bp
, nb_vnbufs
);
882 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
884 /* invalidate usage timestamp to allow immediate freeing */
885 NBUFSTAMPINVALIDATE(bp
);
886 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
887 panic("nfsbuf on freelist");
888 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
890 lck_mtx_unlock(nfs_buf_mutex
);
891 FSDBG_BOT(541, vp
, blkno
, 0xb00, ENOMEM
);
894 bp
->nb_bufsize
= bufsize
;
900 * Set or clear NB_READ now to let the UPL subsystem know
901 * if we intend to modify the pages or not.
903 if (operation
== NBLK_READ
) {
904 SET(bp
->nb_flags
, NB_READ
);
906 CLR(bp
->nb_flags
, NB_READ
);
908 if (bufsize
< PAGE_SIZE
)
910 bp
->nb_bufsize
= bufsize
;
911 bp
->nb_validoff
= bp
->nb_validend
= -1;
913 if (UBCINFOEXISTS(vp
)) {
915 if (nfs_buf_upl_setup(bp
)) {
916 /* unable to create upl */
917 /* vm object must no longer exist */
918 /* cleanup buffer and return error */
919 lck_mtx_lock(nfs_buf_mutex
);
920 LIST_REMOVE(bp
, nb_vnbufs
);
921 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
923 /* invalidate usage timestamp to allow immediate freeing */
924 NBUFSTAMPINVALIDATE(bp
);
925 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
926 panic("nfsbuf on freelist");
927 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
929 lck_mtx_unlock(nfs_buf_mutex
);
930 FSDBG_BOT(541, vp
, blkno
, 0x2bc, EIO
);
933 nfs_buf_upl_check(bp
);
938 panic("nfs_buf_get: %d unknown operation", operation
);
943 FSDBG_BOT(541, vp
, blkno
, bp
, bp
->nb_flags
);
949 nfs_buf_release(struct nfsbuf
*bp
, int freeup
)
951 vnode_t vp
= bp
->nb_vp
;
953 int wakeup_needbuffer
, wakeup_buffer
, wakeup_nbdwrite
;
955 FSDBG_TOP(548, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_data
);
956 FSDBG(548, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
);
957 FSDBG(548, bp
->nb_valid
, 0, bp
->nb_dirty
, 0);
959 if (UBCINFOEXISTS(vp
) && bp
->nb_bufsize
) {
964 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
) && !ISSET(bp
->nb_flags
, NB_INVAL
)) {
965 rv
= nfs_buf_upl_setup(bp
);
967 printf("nfs_buf_release: upl create failed %d\n", rv
);
969 nfs_buf_upl_check(bp
);
971 upl
= bp
->nb_pagelist
;
973 goto pagelist_cleanup_done
;
975 if (ubc_upl_unmap(upl
) != KERN_SUCCESS
)
976 panic("ubc_upl_unmap failed");
979 if (bp
->nb_flags
& (NB_ERROR
| NB_INVAL
| NB_NOCACHE
)) {
980 if (bp
->nb_flags
& (NB_READ
| NB_INVAL
| NB_NOCACHE
))
981 upl_flags
= UPL_ABORT_DUMP_PAGES
;
984 ubc_upl_abort(upl
, upl_flags
);
985 goto pagelist_cleanup_done
;
987 for (i
=0; i
<= (bp
->nb_bufsize
- 1)/PAGE_SIZE
; i
++) {
988 if (!NBPGVALID(bp
,i
))
989 ubc_upl_abort_range(upl
,
990 i
*PAGE_SIZE
, PAGE_SIZE
,
991 UPL_ABORT_DUMP_PAGES
|
992 UPL_ABORT_FREE_ON_EMPTY
);
995 upl_flags
= UPL_COMMIT_SET_DIRTY
;
997 upl_flags
= UPL_COMMIT_CLEAR_DIRTY
;
998 ubc_upl_commit_range(upl
,
999 i
*PAGE_SIZE
, PAGE_SIZE
,
1001 UPL_COMMIT_INACTIVATE
|
1002 UPL_COMMIT_FREE_ON_EMPTY
);
1005 pagelist_cleanup_done
:
1006 /* was this the last buffer in the file? */
1007 if (NBOFF(bp
) + bp
->nb_bufsize
> (off_t
)(VTONFS(vp
)->n_size
)) {
1008 /* if so, invalidate all pages of last buffer past EOF */
1009 int biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
1011 start
= trunc_page_64(VTONFS(vp
)->n_size
) + PAGE_SIZE_64
;
1012 end
= trunc_page_64(NBOFF(bp
) + biosize
);
1014 if (!(rv
= ubc_sync_range(vp
, start
, end
, UBC_INVALIDATE
)))
1015 printf("nfs_buf_release(): ubc_sync_range failed!\n");
1018 CLR(bp
->nb_flags
, NB_PAGELIST
);
1019 bp
->nb_pagelist
= NULL
;
1022 lck_mtx_lock(nfs_buf_mutex
);
1024 wakeup_needbuffer
= wakeup_buffer
= wakeup_nbdwrite
= 0;
1026 /* Wake up any processes waiting for any buffer to become free. */
1027 if (nfsneedbuffer
) {
1029 wakeup_needbuffer
= 1;
1031 /* Wake up any processes waiting for _this_ buffer to become free. */
1032 if (ISSET(bp
->nb_lflags
, NBL_WANTED
)) {
1033 CLR(bp
->nb_lflags
, NBL_WANTED
);
1037 /* If it's not cacheable, or an error, mark it invalid. */
1038 if (ISSET(bp
->nb_flags
, (NB_NOCACHE
|NB_ERROR
)))
1039 SET(bp
->nb_flags
, NB_INVAL
);
1041 if ((bp
->nb_bufsize
<= 0) || ISSET(bp
->nb_flags
, NB_INVAL
)) {
1042 /* If it's invalid or empty, dissociate it from its vnode */
1043 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
) {
1044 LIST_REMOVE(bp
, nb_vnbufs
);
1045 bp
->nb_vnbufs
.le_next
= NFSNOLIST
;
1048 /* if this was a delayed write, wakeup anyone */
1049 /* waiting for delayed writes to complete */
1050 if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1051 CLR(bp
->nb_flags
, NB_DELWRI
);
1052 OSAddAtomic(-1, (SInt32
*)&nfs_nbdwrite
);
1054 wakeup_nbdwrite
= 1;
1056 /* invalidate usage timestamp to allow immediate freeing */
1057 NBUFSTAMPINVALIDATE(bp
);
1058 /* put buffer at head of free list */
1059 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1060 panic("nfsbuf on freelist");
1061 SET(bp
->nb_flags
, NB_INVAL
);
1062 if (ISSET(bp
->nb_flags
, NB_META
)) {
1063 TAILQ_INSERT_HEAD(&nfsbuffreemeta
, bp
, nb_free
);
1064 nfsbuffreemetacnt
++;
1066 TAILQ_INSERT_HEAD(&nfsbuffree
, bp
, nb_free
);
1069 } else if (ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1070 /* put buffer at end of delwri list */
1071 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1072 panic("nfsbuf on freelist");
1073 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
1077 /* update usage timestamp */
1079 bp
->nb_timestamp
= now
.tv_sec
;
1080 /* put buffer at end of free list */
1081 if (bp
->nb_free
.tqe_next
!= NFSNOLIST
)
1082 panic("nfsbuf on freelist");
1083 if (ISSET(bp
->nb_flags
, NB_META
)) {
1084 TAILQ_INSERT_TAIL(&nfsbuffreemeta
, bp
, nb_free
);
1085 nfsbuffreemetacnt
++;
1087 TAILQ_INSERT_TAIL(&nfsbuffree
, bp
, nb_free
);
1094 /* Unlock the buffer. */
1095 CLR(bp
->nb_flags
, (NB_ASYNC
| NB_NOCACHE
| NB_STABLE
| NB_IOD
));
1096 CLR(bp
->nb_lflags
, NBL_BUSY
);
1098 FSDBG_BOT(548, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_data
);
1100 lck_mtx_unlock(nfs_buf_mutex
);
1102 if (wakeup_needbuffer
)
1103 wakeup(&nfsneedbuffer
);
1106 if (wakeup_nbdwrite
)
1107 wakeup(&nfs_nbdwrite
);
1113 * Wait for operations on the buffer to complete.
1114 * When they do, extract and return the I/O's error value.
1117 nfs_buf_iowait(struct nfsbuf
*bp
)
1119 FSDBG_TOP(549, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1121 lck_mtx_lock(nfs_buf_mutex
);
1123 while (!ISSET(bp
->nb_flags
, NB_DONE
))
1124 msleep(bp
, nfs_buf_mutex
, PRIBIO
+ 1, "nfs_buf_iowait", 0);
1126 lck_mtx_unlock(nfs_buf_mutex
);
1128 FSDBG_BOT(549, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1130 /* check for interruption of I/O, then errors. */
1131 if (ISSET(bp
->nb_flags
, NB_EINTR
)) {
1132 CLR(bp
->nb_flags
, NB_EINTR
);
1134 } else if (ISSET(bp
->nb_flags
, NB_ERROR
))
1135 return (bp
->nb_error
? bp
->nb_error
: EIO
);
1140 * Mark I/O complete on a buffer.
1143 nfs_buf_iodone(struct nfsbuf
*bp
)
1146 FSDBG_TOP(550, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1148 if (ISSET(bp
->nb_flags
, NB_DONE
))
1149 panic("nfs_buf_iodone already");
1151 * I/O was done, so don't believe
1152 * the DIRTY state from VM anymore
1154 CLR(bp
->nb_flags
, NB_WASDIRTY
);
1156 if (!ISSET(bp
->nb_flags
, NB_READ
)) {
1157 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
1159 * vnode_writedone() takes care of waking up
1160 * any throttled write operations
1162 vnode_writedone(bp
->nb_vp
);
1164 if (ISSET(bp
->nb_flags
, NB_ASYNC
)) { /* if async, release it */
1165 SET(bp
->nb_flags
, NB_DONE
); /* note that it's done */
1166 nfs_buf_release(bp
, 1);
1167 } else { /* or just wakeup the buffer */
1168 lck_mtx_lock(nfs_buf_mutex
);
1169 SET(bp
->nb_flags
, NB_DONE
); /* note that it's done */
1170 CLR(bp
->nb_lflags
, NBL_WANTED
);
1171 lck_mtx_unlock(nfs_buf_mutex
);
1175 FSDBG_BOT(550, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1179 nfs_buf_write_delayed(struct nfsbuf
*bp
, proc_t p
)
1181 vnode_t vp
= bp
->nb_vp
;
1183 FSDBG_TOP(551, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
1184 FSDBG(551, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, bp
->nb_dirty
);
1187 * If the block hasn't been seen before:
1188 * (1) Mark it as having been seen,
1189 * (2) Charge for the write.
1190 * (3) Make sure it's on its vnode's correct block list,
1192 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
1193 SET(bp
->nb_flags
, NB_DELWRI
);
1194 if (p
&& p
->p_stats
)
1195 p
->p_stats
->p_ru
.ru_oublock
++; /* XXX */
1196 OSAddAtomic(1, (SInt32
*)&nfs_nbdwrite
);
1198 /* move to dirty list */
1199 lck_mtx_lock(nfs_buf_mutex
);
1200 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
1201 LIST_REMOVE(bp
, nb_vnbufs
);
1202 LIST_INSERT_HEAD(&VTONFS(vp
)->n_dirtyblkhd
, bp
, nb_vnbufs
);
1203 lck_mtx_unlock(nfs_buf_mutex
);
1207 * If the vnode has "too many" write operations in progress
1208 * wait for them to finish the IO
1210 (void)vnode_waitforwrites(vp
, VNODE_ASYNC_THROTTLE
, 0, 0, "nfs_buf_write_delayed");
1213 * If we have too many delayed write buffers,
1214 * more than we can "safely" handle, just fall back to
1215 * doing the async write
1217 if (nfs_nbdwrite
< 0)
1218 panic("nfs_buf_write_delayed: Negative nfs_nbdwrite");
1220 if (nfs_nbdwrite
> ((nfsbufcnt
/4)*3)) {
1221 /* issue async write */
1222 SET(bp
->nb_flags
, NB_ASYNC
);
1224 FSDBG_BOT(551, bp
, NBOFF(bp
), bp
->nb_flags
, bp
->nb_error
);
1228 /* Otherwise, the "write" is done, so mark and release the buffer. */
1229 SET(bp
->nb_flags
, NB_DONE
);
1230 nfs_buf_release(bp
, 1);
1231 FSDBG_BOT(551, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
1236 * Check that a "needcommit" buffer can still be committed.
1237 * If the write verifier has changed, we need to clear the
1238 * the needcommit flag.
1241 nfs_buf_check_write_verifier(struct nfsnode
*np
, struct nfsbuf
*bp
)
1243 struct nfsmount
*nmp
;
1245 if (!ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
1248 nmp
= VFSTONFS(vnode_mount(NFSTOV(np
)));
1249 if (!nmp
|| (bp
->nb_verf
== nmp
->nm_verf
))
1252 /* write verifier changed, clear commit flag */
1253 bp
->nb_flags
&= ~NB_NEEDCOMMIT
;
1254 np
->n_needcommitcnt
--;
1255 CHECK_NEEDCOMMITCNT(np
);
1259 * add a reference to a buffer so it doesn't disappear while being used
1260 * (must be called with nfs_buf_mutex held)
1263 nfs_buf_refget(struct nfsbuf
*bp
)
1268 * release a reference on a buffer
1269 * (must be called with nfs_buf_mutex held)
1272 nfs_buf_refrele(struct nfsbuf
*bp
)
1278 * mark a particular buffer as BUSY
1279 * (must be called with nfs_buf_mutex held)
1282 nfs_buf_acquire(struct nfsbuf
*bp
, int flags
, int slpflag
, int slptimeo
)
1287 if (ISSET(bp
->nb_lflags
, NBL_BUSY
)) {
1289 * since the mutex_lock may block, the buffer
1290 * may become BUSY, so we need to recheck for
1293 if (flags
& NBAC_NOWAIT
)
1295 SET(bp
->nb_lflags
, NBL_WANTED
);
1297 ts
.tv_sec
= (slptimeo
/100);
1298 /* the hz value is 100; which leads to 10ms */
1299 ts
.tv_nsec
= (slptimeo
% 100) * 10 * NSEC_PER_USEC
* 1000;
1301 error
= msleep(bp
, nfs_buf_mutex
, slpflag
| (PRIBIO
+ 1),
1302 "nfs_buf_acquire", &ts
);
1307 if (flags
& NBAC_REMOVE
)
1308 nfs_buf_remfree(bp
);
1309 SET(bp
->nb_lflags
, NBL_BUSY
);
1315 * simply drop the BUSY status of a buffer
1316 * (must be called with nfs_buf_mutex held)
1319 nfs_buf_drop(struct nfsbuf
*bp
)
1321 int need_wakeup
= 0;
1323 if (!ISSET(bp
->nb_lflags
, NBL_BUSY
))
1324 panic("nfs_buf_drop: buffer not busy!");
1325 if (ISSET(bp
->nb_lflags
, NBL_WANTED
)) {
1327 * delay the actual wakeup until after we
1328 * clear NBL_BUSY and we've dropped nfs_buf_mutex
1332 /* Unlock the buffer. */
1333 CLR(bp
->nb_lflags
, (NBL_BUSY
| NBL_WANTED
));
1340 * prepare for iterating over an nfsnode's buffer list
1341 * this lock protects the queue manipulation
1342 * (must be called with nfs_buf_mutex held)
1345 nfs_buf_iterprepare(struct nfsnode
*np
, struct nfsbuflists
*iterheadp
, int flags
)
1347 struct nfsbuflists
*listheadp
;
1349 if (flags
& NBI_DIRTY
)
1350 listheadp
= &np
->n_dirtyblkhd
;
1352 listheadp
= &np
->n_cleanblkhd
;
1354 if ((flags
& NBI_NOWAIT
) && (np
->n_bufiterflags
& NBI_ITER
)) {
1355 LIST_INIT(iterheadp
);
1356 return(EWOULDBLOCK
);
1359 while (np
->n_bufiterflags
& NBI_ITER
) {
1360 np
->n_bufiterflags
|= NBI_ITERWANT
;
1361 msleep(&np
->n_bufiterflags
, nfs_buf_mutex
, 0, "nfs_buf_iterprepare", 0);
1363 if (LIST_EMPTY(listheadp
)) {
1364 LIST_INIT(iterheadp
);
1367 np
->n_bufiterflags
|= NBI_ITER
;
1369 iterheadp
->lh_first
= listheadp
->lh_first
;
1370 listheadp
->lh_first
->nb_vnbufs
.le_prev
= &iterheadp
->lh_first
;
1371 LIST_INIT(listheadp
);
1377 * cleanup after iterating over an nfsnode's buffer list
1378 * this lock protects the queue manipulation
1379 * (must be called with nfs_buf_mutex held)
1382 nfs_buf_itercomplete(struct nfsnode
*np
, struct nfsbuflists
*iterheadp
, int flags
)
1384 struct nfsbuflists
* listheadp
;
1387 if (flags
& NBI_DIRTY
)
1388 listheadp
= &np
->n_dirtyblkhd
;
1390 listheadp
= &np
->n_cleanblkhd
;
1392 while (!LIST_EMPTY(iterheadp
)) {
1393 bp
= LIST_FIRST(iterheadp
);
1394 LIST_REMOVE(bp
, nb_vnbufs
);
1395 LIST_INSERT_HEAD(listheadp
, bp
, nb_vnbufs
);
1398 np
->n_bufiterflags
&= ~NBI_ITER
;
1399 if (np
->n_bufiterflags
& NBI_ITERWANT
) {
1400 np
->n_bufiterflags
&= ~NBI_ITERWANT
;
1401 wakeup(&np
->n_bufiterflags
);
1407 * Vnode op for read using bio
1408 * Any similarity to readip() is purely coincidental
1414 __unused
int ioflag
,
1418 struct nfsnode
*np
= VTONFS(vp
);
1421 struct nfsbuf
*bp
= NULL
, *rabp
;
1422 struct nfs_vattr nvattr
;
1423 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
1424 daddr64_t lbn
, rabn
, lastrabn
= -1, tlbn
;
1426 int nra
, error
= 0, n
= 0, on
= 0;
1428 struct dirent
*direntp
= NULL
;
1430 int nocachereadahead
= 0;
1432 FSDBG_TOP(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), ioflag
);
1435 if (uio
->uio_rw
!= UIO_READ
)
1436 panic("nfs_read mode");
1438 if (uio_uio_resid(uio
) == 0) {
1439 FSDBG_BOT(514, vp
, 0xd1e0001, 0, 0);
1442 if (uio
->uio_offset
< 0) {
1443 FSDBG_BOT(514, vp
, 0xd1e0002, 0, EINVAL
);
1447 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) && !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
1448 nfs_fsinfo(nmp
, vp
, cred
, p
);
1449 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
1450 vtype
= vnode_vtype(vp
);
1452 * For nfs, cache consistency can only be maintained approximately.
1453 * Although RFC1094 does not specify the criteria, the following is
1454 * believed to be compatible with the reference port.
1456 * If the file's modify time on the server has changed since the
1457 * last read rpc or you have written to the file,
1458 * you may have lost data cache consistency with the
1459 * server, so flush all of the file's data out of the cache.
1460 * Then force a getattr rpc to ensure that you have up to date
1462 * NB: This implies that cache data can be read when up to
1463 * NFS_MAXATTRTIMEO seconds out of date. If you find that you need
1464 * current attributes this could be forced by setting calling
1465 * NATTRINVALIDATE() before the nfs_getattr() call.
1467 if (np
->n_flag
& NNEEDINVALIDATE
) {
1468 np
->n_flag
&= ~NNEEDINVALIDATE
;
1469 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
1471 if (np
->n_flag
& NMODIFIED
) {
1472 if (vtype
!= VREG
) {
1474 panic("nfs: bioread, not dir");
1476 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1478 FSDBG_BOT(514, vp
, 0xd1e0003, 0, error
);
1482 NATTRINVALIDATE(np
);
1483 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
1485 FSDBG_BOT(514, vp
, 0xd1e0004, 0, error
);
1488 if (vtype
== VDIR
) {
1489 /* if directory changed, purge any name cache entries */
1490 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
1492 np
->n_ncmtime
= nvattr
.nva_mtime
;
1494 np
->n_mtime
= nvattr
.nva_mtime
;
1496 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
1498 FSDBG_BOT(514, vp
, 0xd1e0005, 0, error
);
1501 if (nfstimespeccmp(&np
->n_mtime
, &nvattr
.nva_mtime
, !=)) {
1502 if (vtype
== VDIR
) {
1504 /* purge name cache entries */
1505 if (nfstimespeccmp(&np
->n_ncmtime
, &nvattr
.nva_mtime
, !=))
1508 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1510 FSDBG_BOT(514, vp
, 0xd1e0006, 0, error
);
1514 np
->n_ncmtime
= nvattr
.nva_mtime
;
1515 np
->n_mtime
= nvattr
.nva_mtime
;
1519 if (vnode_isnocache(vp
)) {
1520 if (!(np
->n_flag
& NNOCACHE
)) {
1521 if (NVALIDBUFS(np
)) {
1522 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
1524 FSDBG_BOT(514, vp
, 0xd1e000a, 0, error
);
1528 np
->n_flag
|= NNOCACHE
;
1530 } else if (np
->n_flag
& NNOCACHE
) {
1531 np
->n_flag
&= ~NNOCACHE
;
1535 if (np
->n_flag
& NNOCACHE
) {
1539 * If we have only a block or so to read,
1540 * just do the rpc directly.
1541 * If we have a couple blocks or more to read,
1542 * then we'll take advantage of readahead within
1543 * this loop to try to fetch all the data in parallel
1545 if (!nocachereadahead
&& (uio_uio_resid(uio
) < 2*biosize
)) {
1546 error
= nfs_readrpc(vp
, uio
, cred
, p
);
1547 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1550 nocachereadahead
= 1;
1553 error
= nfs_readlinkrpc(vp
, uio
, cred
, p
);
1554 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1559 printf(" NFSNOCACHE: type %x unexpected\n", vtype
);
1564 lbn
= uio
->uio_offset
/ biosize
;
1567 * Copy directly from any cached pages without grabbing the bufs.
1569 * Note: for "nocache" reads, we don't copy directly from UBC
1570 * because any cached pages will be for readahead buffers that
1571 * need to be invalidated anyway before we finish this request.
1573 if (!(np
->n_flag
& NNOCACHE
) &&
1574 (uio
->uio_segflg
== UIO_USERSPACE32
||
1575 uio
->uio_segflg
== UIO_USERSPACE64
||
1576 uio
->uio_segflg
== UIO_USERSPACE
)) {
1577 // LP64todo - fix this!
1578 int io_resid
= uio_uio_resid(uio
);
1579 diff
= np
->n_size
- uio
->uio_offset
;
1580 if (diff
< io_resid
)
1583 error
= cluster_copy_ubc_data(vp
, uio
, &io_resid
, 0);
1585 FSDBG_BOT(514, vp
, uio
->uio_offset
, 0xcacefeed, error
);
1589 /* count any biocache reads that we just copied directly */
1590 if (lbn
!= uio
->uio_offset
/ biosize
) {
1591 OSAddAtomic((uio
->uio_offset
/ biosize
) - lbn
, (SInt32
*)&nfsstats
.biocache_reads
);
1592 FSDBG(514, vp
, 0xcacefeed, uio
->uio_offset
, error
);
1596 lbn
= uio
->uio_offset
/ biosize
;
1597 on
= uio
->uio_offset
% biosize
;
1600 * Start the read ahead(s), as required.
1602 if (nfs_numasync
> 0 && nmp
->nm_readahead
> 0) {
1603 for (nra
= 0; nra
< nmp
->nm_readahead
; nra
++) {
1604 rabn
= lbn
+ 1 + nra
;
1605 if (rabn
<= lastrabn
) {
1606 /* we've already (tried to) read this block */
1607 /* no need to try it again... */
1611 if ((off_t
)rabn
* biosize
>= (off_t
)np
->n_size
)
1613 if ((np
->n_flag
& NNOCACHE
) &&
1614 (((off_t
)rabn
* biosize
) >= (uio
->uio_offset
+ uio_uio_resid(uio
))))
1615 /* for uncached readahead, don't go beyond end of request */
1617 /* check if block exists and is valid. */
1618 error
= nfs_buf_get(vp
, rabn
, biosize
, p
, NBLK_READ
|NBLK_NOWAIT
, &rabp
);
1620 FSDBG_BOT(514, vp
, 0xd1e000b, 1, error
);
1625 if (nfs_buf_upl_valid_range(rabp
, 0, rabp
->nb_bufsize
)) {
1626 nfs_buf_release(rabp
, 1);
1629 if (!ISSET(rabp
->nb_flags
, (NB_CACHE
|NB_DELWRI
))) {
1630 SET(rabp
->nb_flags
, (NB_READ
|NB_ASYNC
));
1631 if (nfs_asyncio(rabp
, cred
)) {
1632 SET(rabp
->nb_flags
, (NB_INVAL
|NB_ERROR
));
1633 rabp
->nb_error
= EIO
;
1634 nfs_buf_release(rabp
, 1);
1637 nfs_buf_release(rabp
, 1);
1641 if ((uio_uio_resid(uio
) <= 0) || (uio
->uio_offset
>= (off_t
)np
->n_size
)) {
1642 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0xaaaaaaaa);
1646 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_reads
);
1649 * If the block is in the cache and has the required data
1650 * in a valid region, just copy it out.
1651 * Otherwise, get the block and write back/read in,
1656 // LP64todo - fix this!
1657 n
= min((unsigned)(bufsize
- on
), uio_uio_resid(uio
));
1658 diff
= np
->n_size
- uio
->uio_offset
;
1662 error
= nfs_buf_get(vp
, lbn
, bufsize
, p
, NBLK_READ
, &bp
);
1664 FSDBG_BOT(514, vp
, 0xd1e000c, 0, EINTR
);
1668 /* if any pages are valid... */
1670 /* ...check for any invalid pages in the read range */
1671 int pg
, firstpg
, lastpg
, dirtypg
;
1672 dirtypg
= firstpg
= lastpg
= -1;
1674 while (pg
<= (on
+ n
- 1)/PAGE_SIZE
) {
1675 if (!NBPGVALID(bp
,pg
)) {
1679 } else if (firstpg
>= 0 && dirtypg
< 0 && NBPGDIRTY(bp
,pg
))
1684 /* if there are no invalid pages, we're all set */
1686 if (bp
->nb_validoff
< 0) {
1687 /* valid range isn't set up, so */
1688 /* set it to what we know is valid */
1689 bp
->nb_validoff
= trunc_page(on
);
1690 bp
->nb_validend
= round_page(on
+n
);
1691 nfs_buf_normalize_valid_range(np
, bp
);
1696 /* there are invalid pages in the read range */
1697 if ((dirtypg
> firstpg
) && (dirtypg
< lastpg
)) {
1698 /* there are also dirty page(s) in the range, */
1699 /* so write the buffer out and try again */
1700 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
1701 SET(bp
->nb_flags
, NB_ASYNC
);
1702 if (bp
->nb_wcred
== NOCRED
) {
1703 kauth_cred_ref(cred
);
1704 bp
->nb_wcred
= cred
;
1706 error
= nfs_buf_write(bp
);
1708 FSDBG_BOT(514, vp
, 0xd1e000d, 0, error
);
1713 if (!bp
->nb_dirty
&& bp
->nb_dirtyend
<= 0 &&
1714 (lastpg
- firstpg
+ 1) > (bufsize
/PAGE_SIZE
)/2) {
1715 /* we need to read in more than half the buffer and the */
1716 /* buffer's not dirty, so just fetch the whole buffer */
1719 /* read the page range in */
1721 char uio_buf
[ UIO_SIZEOF(1) ];
1724 auio
= uio_createwithbuffer(1, (NBOFF(bp
) + firstpg
* PAGE_SIZE_64
),
1725 UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
1729 uio_addiov(auio
, CAST_USER_ADDR_T((bp
->nb_data
+ firstpg
* PAGE_SIZE
)),
1730 ((lastpg
- firstpg
+ 1) * PAGE_SIZE
));
1731 error
= nfs_readrpc(vp
, auio
, cred
, p
);
1734 if (np
->n_flag
& NNOCACHE
)
1735 SET(bp
->nb_flags
, NB_NOCACHE
);
1736 nfs_buf_release(bp
, 1);
1737 FSDBG_BOT(514, vp
, 0xd1e000e, 0, error
);
1740 /* Make sure that the valid range is set to cover this read. */
1741 bp
->nb_validoff
= trunc_page_32(on
);
1742 bp
->nb_validend
= round_page_32(on
+n
);
1743 nfs_buf_normalize_valid_range(np
, bp
);
1744 if (uio_resid(auio
) > 0) {
1745 /* if short read, must have hit EOF, */
1746 /* so zero the rest of the range */
1747 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
1749 /* mark the pages (successfully read) as valid */
1750 for (pg
=firstpg
; pg
<= lastpg
; pg
++)
1751 NBPGVALID_SET(bp
,pg
);
1754 /* if no pages are valid, read the whole block */
1755 if (!bp
->nb_valid
) {
1756 SET(bp
->nb_flags
, NB_READ
);
1757 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
1758 error
= nfs_doio(bp
, cred
, p
);
1760 if (np
->n_flag
& NNOCACHE
)
1761 SET(bp
->nb_flags
, NB_NOCACHE
);
1762 nfs_buf_release(bp
, 1);
1763 FSDBG_BOT(514, vp
, 0xd1e000f, 0, error
);
1768 /* validate read range against valid range and clip */
1769 if (bp
->nb_validend
> 0) {
1770 diff
= (on
>= bp
->nb_validend
) ? 0 : (bp
->nb_validend
- on
);
1778 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_readlinks
);
1779 error
= nfs_buf_get(vp
, 0, NFS_MAXPATHLEN
, p
, NBLK_READ
, &bp
);
1781 FSDBG_BOT(514, vp
, 0xd1e0010, 0, error
);
1784 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1785 SET(bp
->nb_flags
, NB_READ
);
1786 error
= nfs_doio(bp
, cred
, p
);
1788 SET(bp
->nb_flags
, NB_ERROR
);
1789 nfs_buf_release(bp
, 1);
1790 FSDBG_BOT(514, vp
, 0xd1e0011, 0, error
);
1794 // LP64todo - fix this!
1795 n
= min(uio_uio_resid(uio
), bp
->nb_validend
);
1799 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_readdirs
);
1800 if (np
->n_direofoffset
&& uio
->uio_offset
>= np
->n_direofoffset
) {
1801 FSDBG_BOT(514, vp
, 0xde0f0001, 0, 0);
1804 lbn
= uio
->uio_offset
/ NFS_DIRBLKSIZ
;
1805 on
= uio
->uio_offset
& (NFS_DIRBLKSIZ
- 1);
1806 error
= nfs_buf_get(vp
, lbn
, NFS_DIRBLKSIZ
, p
, NBLK_READ
, &bp
);
1808 FSDBG_BOT(514, vp
, 0xd1e0012, 0, error
);
1811 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1812 SET(bp
->nb_flags
, NB_READ
);
1813 error
= nfs_doio(bp
, cred
, p
);
1815 nfs_buf_release(bp
, 1);
1817 while (error
== NFSERR_BAD_COOKIE
) {
1819 error
= nfs_vinvalbuf(vp
, 0, cred
, p
, 1);
1821 * Yuck! The directory has been modified on the
1822 * server. The only way to get the block is by
1823 * reading from the beginning to get all the
1826 for (tlbn
= 0; tlbn
<= lbn
&& !error
; tlbn
++) {
1827 if (np
->n_direofoffset
1828 && (tlbn
* NFS_DIRBLKSIZ
) >= np
->n_direofoffset
) {
1829 FSDBG_BOT(514, vp
, 0xde0f0002, 0, 0);
1832 error
= nfs_buf_get(vp
, tlbn
, NFS_DIRBLKSIZ
, p
, NBLK_READ
, &bp
);
1834 FSDBG_BOT(514, vp
, 0xd1e0013, 0, error
);
1837 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
1838 SET(bp
->nb_flags
, NB_READ
);
1839 error
= nfs_doio(bp
, cred
, p
);
1841 * no error + NB_INVAL == directory EOF,
1844 if (error
== 0 && (bp
->nb_flags
& NB_INVAL
))
1848 * An error will throw away the block and the
1849 * for loop will break out. If no error and this
1850 * is not the block we want, we throw away the
1851 * block and go for the next one via the for loop.
1853 if (error
|| tlbn
< lbn
)
1854 nfs_buf_release(bp
, 1);
1858 * The above while is repeated if we hit another cookie
1859 * error. If we hit an error and it wasn't a cookie error,
1863 FSDBG_BOT(514, vp
, 0xd1e0014, 0, error
);
1869 * If not eof and read aheads are enabled, start one.
1870 * (You need the current block first, so that you have the
1871 * directory offset cookie of the next block.)
1873 if (nfs_numasync
> 0 && nmp
->nm_readahead
> 0 &&
1874 (np
->n_direofoffset
== 0 ||
1875 (lbn
+ 1) * NFS_DIRBLKSIZ
< np
->n_direofoffset
) &&
1876 !nfs_buf_is_incore(vp
, lbn
+ 1)) {
1877 error
= nfs_buf_get(vp
, lbn
+ 1, NFS_DIRBLKSIZ
, p
, NBLK_READ
|NBLK_NOWAIT
, &rabp
);
1879 FSDBG_BOT(514, vp
, 0xd1e0015, 0, error
);
1883 if (!ISSET(rabp
->nb_flags
, (NB_CACHE
))) {
1884 SET(rabp
->nb_flags
, (NB_READ
| NB_ASYNC
));
1885 if (nfs_asyncio(rabp
, cred
)) {
1886 SET(rabp
->nb_flags
, (NB_INVAL
|NB_ERROR
));
1887 rabp
->nb_error
= EIO
;
1888 nfs_buf_release(rabp
, 1);
1891 nfs_buf_release(rabp
, 1);
1896 * Make sure we use a signed variant of min() since
1897 * the second term may be negative.
1899 // LP64todo - fix this!
1900 n
= lmin(uio_uio_resid(uio
), bp
->nb_validend
- on
);
1902 * We keep track of the directory eof in
1903 * np->n_direofoffset and chop it off as an
1904 * extra step right here.
1906 if (np
->n_direofoffset
&&
1907 n
> np
->n_direofoffset
- uio
->uio_offset
)
1908 n
= np
->n_direofoffset
- uio
->uio_offset
;
1910 * Make sure that we return an integral number of entries so
1911 * that any subsequent calls will start copying from the start
1912 * of the next entry.
1914 * If the current value of n has the last entry cut short,
1915 * set n to copy everything up to the last entry instead.
1918 dp
= bp
->nb_data
+ on
;
1919 while (dp
< (bp
->nb_data
+ on
+ n
)) {
1920 direntp
= (struct dirent
*)dp
;
1921 dp
+= direntp
->d_reclen
;
1923 if (dp
> (bp
->nb_data
+ on
+ n
))
1924 n
= (dp
- direntp
->d_reclen
) - (bp
->nb_data
+ on
);
1928 printf("nfs_bioread: type %x unexpected\n", vtype
);
1929 FSDBG_BOT(514, vp
, 0xd1e0016, 0, EINVAL
);
1934 error
= uiomove(bp
->nb_data
+ on
, (int)n
, uio
);
1938 if (np
->n_flag
& NNOCACHE
)
1939 SET(bp
->nb_flags
, NB_NOCACHE
);
1949 nfs_buf_release(bp
, 1);
1950 } while (error
== 0 && uio_uio_resid(uio
) > 0 && n
> 0);
1951 FSDBG_BOT(514, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
1957 * Vnode op for write using bio
1961 struct vnop_write_args
/* {
1962 struct vnodeop_desc *a_desc;
1966 vfs_context_t a_context;
1969 struct uio
*uio
= ap
->a_uio
;
1970 vnode_t vp
= ap
->a_vp
;
1971 struct nfsnode
*np
= VTONFS(vp
);
1974 int ioflag
= ap
->a_ioflag
;
1976 struct nfs_vattr nvattr
;
1977 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
1979 int biosize
, bufsize
;
1980 int n
, on
, error
= 0;
1981 off_t boff
, start
, end
, cureof
;
1982 struct iovec_32 iov
;
1985 FSDBG_TOP(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), ioflag
);
1988 if (uio
->uio_rw
!= UIO_WRITE
)
1989 panic("nfs_write mode");
1990 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
1991 panic("nfs_write proc");
1994 p
= vfs_context_proc(ap
->a_context
);
1995 cred
= vfs_context_ucred(ap
->a_context
);
1997 if (vnode_vtype(vp
) != VREG
)
2000 np
->n_flag
|= NWRBUSY
;
2002 if (np
->n_flag
& NNEEDINVALIDATE
) {
2003 np
->n_flag
&= ~NNEEDINVALIDATE
;
2004 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cred
, p
, 1);
2006 if (np
->n_flag
& NWRITEERR
) {
2007 np
->n_flag
&= ~(NWRITEERR
| NWRBUSY
);
2008 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), np
->n_error
);
2009 return (np
->n_error
);
2011 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
2012 !(nmp
->nm_state
& NFSSTA_GOTFSINFO
))
2013 (void)nfs_fsinfo(nmp
, vp
, cred
, p
);
2014 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
2015 if (np
->n_flag
& NMODIFIED
) {
2016 NATTRINVALIDATE(np
);
2017 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
2019 np
->n_flag
&= ~NWRBUSY
;
2020 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0x10bad01, error
);
2024 if (ioflag
& IO_APPEND
) {
2025 NATTRINVALIDATE(np
);
2026 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
2028 np
->n_flag
&= ~NWRBUSY
;
2029 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0x10bad02, error
);
2032 uio
->uio_offset
= np
->n_size
;
2035 if (uio
->uio_offset
< 0) {
2036 np
->n_flag
&= ~NWRBUSY
;
2037 FSDBG_BOT(515, vp
, uio
->uio_offset
, 0xbad0ff, EINVAL
);
2040 if (uio_uio_resid(uio
) == 0) {
2041 np
->n_flag
&= ~NWRBUSY
;
2042 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0);
2046 biosize
= vfs_statfs(vnode_mount(vp
))->f_iosize
;
2048 if (vnode_isnocache(vp
)) {
2049 if (!(np
->n_flag
& NNOCACHE
)) {
2050 if (NVALIDBUFS(np
)) {
2051 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
2053 np
->n_flag
&= ~NWRBUSY
;
2054 FSDBG_BOT(515, vp
, 0, 0, error
);
2058 np
->n_flag
|= NNOCACHE
;
2060 } else if (np
->n_flag
& NNOCACHE
) {
2061 np
->n_flag
&= ~NNOCACHE
;
2065 OSAddAtomic(1, (SInt32
*)&nfsstats
.biocache_writes
);
2066 lbn
= uio
->uio_offset
/ biosize
;
2067 on
= uio
->uio_offset
% biosize
;
2068 // LP64todo - fix this
2069 n
= min((unsigned)(biosize
- on
), uio_uio_resid(uio
));
2073 * Get a cache block for writing. The range to be written is
2074 * (off..off+n) within the block. We ensure that the block
2075 * either has no dirty region or that the given range is
2076 * contiguous with the existing dirty region.
2078 error
= nfs_buf_get(vp
, lbn
, bufsize
, p
, NBLK_WRITE
, &bp
);
2080 np
->n_flag
&= ~NWRBUSY
;
2081 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2084 /* map the block because we know we're going to write to it */
2087 if (np
->n_flag
& NNOCACHE
)
2088 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2090 if (bp
->nb_wcred
== NOCRED
) {
2091 kauth_cred_ref(cred
);
2092 bp
->nb_wcred
= cred
;
2096 * If there's already a dirty range AND dirty pages in this block we
2097 * need to send a commit AND write the dirty pages before continuing.
2099 * If there's already a dirty range OR dirty pages in this block
2100 * and the new write range is not contiguous with the existing range,
2101 * then force the buffer to be written out now.
2102 * (We used to just extend the dirty range to cover the valid,
2103 * but unwritten, data in between also. But writing ranges
2104 * of data that weren't actually written by an application
2105 * risks overwriting some other client's data with stale data
2106 * that's just masquerading as new written data.)
2108 if (bp
->nb_dirtyend
> 0) {
2109 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
2110 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c001);
2111 /* write/commit buffer "synchronously" */
2112 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2113 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2114 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2115 error
= nfs_buf_write(bp
);
2117 np
->n_flag
&= ~NWRBUSY
;
2118 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2123 } else if (bp
->nb_dirty
) {
2124 int firstpg
, lastpg
;
2126 /* calculate write range pagemask */
2127 firstpg
= on
/PAGE_SIZE
;
2128 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2129 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
2130 /* check if there are dirty pages outside the write range */
2131 if (bp
->nb_dirty
& ~pagemask
) {
2132 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c002);
2133 /* write/commit buffer "synchronously" */
2134 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2135 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2136 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2137 error
= nfs_buf_write(bp
);
2139 np
->n_flag
&= ~NWRBUSY
;
2140 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2145 /* if the first or last pages are already dirty */
2146 /* make sure that the dirty range encompasses those pages */
2147 if (NBPGDIRTY(bp
,firstpg
) || NBPGDIRTY(bp
,lastpg
)) {
2148 FSDBG(515, vp
, uio
->uio_offset
, bp
, 0xd15c003);
2149 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
2150 if (NBPGDIRTY(bp
,lastpg
)) {
2151 bp
->nb_dirtyend
= (lastpg
+1) * PAGE_SIZE
;
2153 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
2154 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2156 bp
->nb_dirtyend
= on
+n
;
2161 * Are we extending the size of the file with this write?
2162 * If so, update file size now that we have the block.
2163 * If there was a partial buf at the old eof, validate
2164 * and zero the new bytes.
2166 cureof
= (off_t
)np
->n_size
;
2167 if (uio
->uio_offset
+ n
> (off_t
)np
->n_size
) {
2168 struct nfsbuf
*eofbp
= NULL
;
2169 daddr64_t eofbn
= np
->n_size
/ biosize
;
2170 int eofoff
= np
->n_size
% biosize
;
2171 int neweofoff
= (uio
->uio_offset
+ n
) % biosize
;
2173 FSDBG(515, 0xb1ffa000, uio
->uio_offset
+ n
, eofoff
, neweofoff
);
2175 if (eofoff
&& (eofbn
< lbn
)) {
2176 error
= nfs_buf_get(vp
, eofbn
, biosize
, p
, NBLK_WRITE
|NBLK_ONLYVALID
, &eofbp
);
2178 np
->n_flag
&= ~NWRBUSY
;
2179 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2184 /* if we're extending within the same last block */
2185 /* and the block is flagged as being cached... */
2186 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
2187 /* ...check that all pages in buffer are valid */
2188 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1)/PAGE_SIZE
;
2190 /* pagemask only has to extend to last page being written to */
2191 pagemask
= (1 << (endpg
+1)) - 1;
2192 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
2193 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
2194 /* zerofill any hole */
2195 if (on
> bp
->nb_validend
) {
2197 for (i
=bp
->nb_validend
/PAGE_SIZE
; i
<= (on
- 1)/PAGE_SIZE
; i
++)
2198 NBPGVALID_SET(bp
, i
);
2200 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
2201 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
2202 on
- bp
->nb_validend
);
2204 /* zerofill any trailing data in the last page */
2207 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
2208 bzero((char *)bp
->nb_data
+ neweofoff
,
2209 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
2213 np
->n_flag
|= NMODIFIED
;
2214 np
->n_size
= uio
->uio_offset
+ n
;
2215 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
2218 * We may need to zero any previously invalid data
2219 * after the old EOF in the previous EOF buffer.
2221 * For the old last page, don't zero bytes if there
2222 * are invalid bytes in that page (i.e. the page isn't
2224 * For pages after the old last page, zero them and
2225 * mark them as valid.
2229 if (np
->n_flag
& NNOCACHE
)
2230 SET(eofbp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2232 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
2234 i
= eofoff
/PAGE_SIZE
;
2235 while (eofoff
< biosize
) {
2236 int poff
= eofoff
& PAGE_MASK
;
2237 if (!poff
|| NBPGVALID(eofbp
,i
)) {
2238 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
2239 NBPGVALID_SET(eofbp
, i
);
2241 if (bp
->nb_validend
== eofoff
)
2242 bp
->nb_validend
+= PAGE_SIZE
- poff
;
2243 eofoff
+= PAGE_SIZE
- poff
;
2246 nfs_buf_release(eofbp
, 1);
2250 * If dirtyend exceeds file size, chop it down. This should
2251 * not occur unless there is a race.
2253 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
2254 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2256 * UBC doesn't handle partial pages, so we need to make sure
2257 * that any pages left in the page cache are completely valid.
2259 * Writes that are smaller than a block are delayed if they
2260 * don't extend to the end of the block.
2262 * If the block isn't (completely) cached, we may need to read
2263 * in some parts of pages that aren't covered by the write.
2264 * If the write offset (on) isn't page aligned, we'll need to
2265 * read the start of the first page being written to. Likewise,
2266 * if the offset of the end of the write (on+n) isn't page aligned,
2267 * we'll need to read the end of the last page being written to.
2270 * We don't want to read anything we're just going to write over.
2271 * We don't want to issue multiple I/Os if we don't have to
2272 * (because they're synchronous rpcs).
2273 * We don't want to read anything we already have modified in the
2276 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && n
< biosize
) {
2277 int firstpg
, lastpg
, dirtypg
;
2278 int firstpgoff
, lastpgoff
;
2280 firstpg
= on
/PAGE_SIZE
;
2281 firstpgoff
= on
& PAGE_MASK
;
2282 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2283 lastpgoff
= (on
+n
) & PAGE_MASK
;
2284 if (firstpgoff
&& !NBPGVALID(bp
,firstpg
)) {
2285 /* need to read start of first page */
2286 start
= firstpg
* PAGE_SIZE
;
2287 end
= start
+ firstpgoff
;
2289 if (lastpgoff
&& !NBPGVALID(bp
,lastpg
)) {
2290 /* need to read end of last page */
2292 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
2293 end
= (lastpg
+ 1) * PAGE_SIZE
;
2296 /* need to read the data in range: start...end-1 */
2298 /* first, check for dirty pages in between */
2299 /* if there are, we'll have to do two reads because */
2300 /* we don't want to overwrite the dirty pages. */
2301 for (dirtypg
=start
/PAGE_SIZE
; dirtypg
<= (end
-1)/PAGE_SIZE
; dirtypg
++)
2302 if (NBPGDIRTY(bp
,dirtypg
))
2305 /* if start is at beginning of page, try */
2306 /* to get any preceeding pages as well. */
2307 if (!(start
& PAGE_MASK
)) {
2308 /* stop at next dirty/valid page or start of block */
2309 for (; start
> 0; start
-=PAGE_SIZE
)
2310 if (NBPGVALID(bp
,((start
-1)/PAGE_SIZE
)))
2315 /* setup uio for read(s) */
2317 auio
.uio_iovs
.iov32p
= &iov
;
2318 auio
.uio_iovcnt
= 1;
2319 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
2320 auio
.uio_segflg
= UIO_SYSSPACE
;
2322 auio
.uio_segflg
= UIO_SYSSPACE32
;
2324 auio
.uio_rw
= UIO_READ
;
2326 if (dirtypg
<= (end
-1)/PAGE_SIZE
) {
2327 /* there's a dirty page in the way, so just do two reads */
2328 /* we'll read the preceding data here */
2329 auio
.uio_offset
= boff
+ start
;
2330 iov
.iov_len
= on
- start
;
2331 uio_uio_resid_set(&auio
, iov
.iov_len
);
2332 iov
.iov_base
= (uintptr_t) bp
->nb_data
+ start
;
2333 error
= nfs_readrpc(vp
, &auio
, cred
, p
);
2335 bp
->nb_error
= error
;
2336 SET(bp
->nb_flags
, NB_ERROR
);
2337 printf("nfs_write: readrpc %d", error
);
2339 if (uio_uio_resid(&auio
) > 0) {
2340 FSDBG(516, bp
, iov
.iov_base
- bp
->nb_data
, uio_uio_resid(&auio
), 0xd00dee01);
2341 // LP64todo - fix this
2342 bzero((caddr_t
)iov
.iov_base
, uio_uio_resid(&auio
));
2344 /* update validoff/validend if necessary */
2345 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2346 bp
->nb_validoff
= start
;
2347 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
))
2348 bp
->nb_validend
= on
;
2349 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2350 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2351 /* validate any pages before the write offset */
2352 for (; start
< on
/PAGE_SIZE
; start
+=PAGE_SIZE
)
2353 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2354 /* adjust start to read any trailing data */
2358 /* if end is at end of page, try to */
2359 /* get any following pages as well. */
2360 if (!(end
& PAGE_MASK
)) {
2361 /* stop at next valid page or end of block */
2362 for (; end
< bufsize
; end
+=PAGE_SIZE
)
2363 if (NBPGVALID(bp
,end
/PAGE_SIZE
))
2367 if (((boff
+start
) >= cureof
) || ((start
>= on
) && ((boff
+ on
+ n
) >= cureof
))) {
2369 * Either this entire read is beyond the current EOF
2370 * or the range that we won't be modifying (on+n...end)
2371 * is all beyond the current EOF.
2372 * No need to make a trip across the network to
2373 * read nothing. So, just zero the buffer instead.
2375 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
2376 bzero(bp
->nb_data
+ start
, end
- start
);
2378 /* now we'll read the (rest of the) data */
2379 auio
.uio_offset
= boff
+ start
;
2380 iov
.iov_len
= end
- start
;
2381 uio_uio_resid_set(&auio
, iov
.iov_len
);
2382 iov
.iov_base
= (uintptr_t) (bp
->nb_data
+ start
);
2383 error
= nfs_readrpc(vp
, &auio
, cred
, p
);
2385 bp
->nb_error
= error
;
2386 SET(bp
->nb_flags
, NB_ERROR
);
2387 printf("nfs_write: readrpc %d", error
);
2389 if (uio_uio_resid(&auio
) > 0) {
2390 FSDBG(516, bp
, iov
.iov_base
- bp
->nb_data
, uio_uio_resid(&auio
), 0xd00dee02);
2391 // LP64todo - fix this
2392 bzero((caddr_t
)iov
.iov_base
, uio_uio_resid(&auio
));
2395 /* update validoff/validend if necessary */
2396 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2397 bp
->nb_validoff
= start
;
2398 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
))
2399 bp
->nb_validend
= end
;
2400 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2401 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2402 /* validate any pages before the write offset's page */
2403 for (; start
< trunc_page_32(on
); start
+=PAGE_SIZE
)
2404 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2405 /* validate any pages after the range of pages being written to */
2406 for (; (end
- 1) > round_page_32(on
+n
-1); end
-=PAGE_SIZE
)
2407 NBPGVALID_SET(bp
, (end
-1)/PAGE_SIZE
);
2408 /* Note: pages being written to will be validated when written */
2412 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
2413 error
= bp
->nb_error
;
2414 nfs_buf_release(bp
, 1);
2415 np
->n_flag
&= ~NWRBUSY
;
2416 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2420 np
->n_flag
|= NMODIFIED
;
2423 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
2425 SET(bp
->nb_flags
, NB_ERROR
);
2426 nfs_buf_release(bp
, 1);
2427 np
->n_flag
&= ~NWRBUSY
;
2428 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), error
);
2432 /* validate any pages written to */
2433 start
= on
& ~PAGE_MASK
;
2434 for (; start
< on
+n
; start
+= PAGE_SIZE
) {
2435 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2437 * This may seem a little weird, but we don't actually set the
2438 * dirty bits for writes. This is because we keep the dirty range
2439 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
2440 * delayed writes, when we give the pages back to the VM we don't
2441 * want to keep them marked dirty, because when we later write the
2442 * buffer we won't be able to tell which pages were written dirty
2443 * and which pages were mmapped and dirtied.
2446 if (bp
->nb_dirtyend
> 0) {
2447 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
2448 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
2450 bp
->nb_dirtyoff
= on
;
2451 bp
->nb_dirtyend
= on
+ n
;
2453 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
2454 bp
->nb_validoff
> bp
->nb_dirtyend
) {
2455 bp
->nb_validoff
= bp
->nb_dirtyoff
;
2456 bp
->nb_validend
= bp
->nb_dirtyend
;
2458 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
2459 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
2461 if (!ISSET(bp
->nb_flags
, NB_CACHE
))
2462 nfs_buf_normalize_valid_range(np
, bp
);
2465 * Since this block is being modified, it must be written
2466 * again and not just committed.
2468 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
2469 np
->n_needcommitcnt
--;
2470 CHECK_NEEDCOMMITCNT(np
);
2472 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
2474 if (ioflag
& IO_SYNC
) {
2476 error
= nfs_buf_write(bp
);
2478 np
->n_flag
&= ~NWRBUSY
;
2479 FSDBG_BOT(515, vp
, uio
->uio_offset
,
2480 uio_uio_resid(uio
), error
);
2483 } else if (((n
+ on
) == biosize
) || (np
->n_flag
& NNOCACHE
)) {
2485 SET(bp
->nb_flags
, NB_ASYNC
);
2488 nfs_buf_write_delayed(bp
, p
);
2490 if (np
->n_needcommitcnt
> (nfsbufcnt
/16))
2491 nfs_flushcommits(vp
, p
, 1);
2493 } while (uio_uio_resid(uio
) > 0 && n
> 0);
2495 np
->n_flag
&= ~NWRBUSY
;
2496 FSDBG_BOT(515, vp
, uio
->uio_offset
, uio_uio_resid(uio
), 0);
2501 * Flush out and invalidate all buffers associated with a vnode.
2502 * Called with the underlying object locked.
2505 nfs_vinvalbuf_internal(
2514 struct nfsbuflists blist
;
2515 int list
, error
= 0;
2516 struct nfsnode
*np
= VTONFS(vp
);
2518 if (flags
& V_SAVE
) {
2519 if ((error
= nfs_flush(vp
, MNT_WAIT
, cred
, p
,
2520 (flags
& V_IGNORE_WRITEERR
))))
2522 if (!LIST_EMPTY(&np
->n_dirtyblkhd
))
2523 panic("nfs_vinvalbuf: dirty bufs (vp 0x%x, bp 0x%x)",
2524 vp
, LIST_FIRST(&np
->n_dirtyblkhd
));
2527 lck_mtx_lock(nfs_buf_mutex
);
2530 if (nfs_buf_iterprepare(np
, &blist
, list
)) {
2532 if (nfs_buf_iterprepare(np
, &blist
, list
))
2535 while ((bp
= LIST_FIRST(&blist
))) {
2536 LIST_REMOVE(bp
, nb_vnbufs
);
2537 if (list
== NBI_CLEAN
)
2538 LIST_INSERT_HEAD(&np
->n_cleanblkhd
, bp
, nb_vnbufs
);
2540 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
2542 while ((error
= nfs_buf_acquire(bp
, NBAC_REMOVE
, slpflag
, slptimeo
))) {
2543 FSDBG(556, vp
, bp
, NBOFF(bp
), bp
->nb_flags
);
2544 if (error
!= EAGAIN
) {
2545 FSDBG(554, vp
, bp
, -1, error
);
2546 nfs_buf_refrele(bp
);
2547 nfs_buf_itercomplete(np
, &blist
, list
);
2548 lck_mtx_unlock(nfs_buf_mutex
);
2552 nfs_buf_refrele(bp
);
2553 FSDBG(554, vp
, bp
, NBOFF(bp
), bp
->nb_flags
);
2554 lck_mtx_unlock(nfs_buf_mutex
);
2555 if ((flags
& V_SAVE
) && UBCINFOEXISTS(vp
) && bp
->nb_vp
&&
2556 (NBOFF(bp
) < (off_t
)np
->n_size
)) {
2557 /* XXX extra paranoia: make sure we're not */
2558 /* somehow leaving any dirty data around */
2560 int end
= (NBOFF(bp
) + bp
->nb_bufsize
> (off_t
)np
->n_size
) ?
2561 ((off_t
)np
->n_size
- NBOFF(bp
)) : bp
->nb_bufsize
;
2562 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
)) {
2563 error
= nfs_buf_upl_setup(bp
);
2564 if (error
== EINVAL
) {
2565 /* vm object must no longer exist */
2566 /* hopefully we don't need to do */
2567 /* anything for this buffer */
2569 printf("nfs_vinvalbuf: upl setup failed %d\n", error
);
2570 bp
->nb_valid
= bp
->nb_dirty
= 0;
2572 nfs_buf_upl_check(bp
);
2573 /* check for any dirty data before the EOF */
2574 if (bp
->nb_dirtyend
&& bp
->nb_dirtyoff
< end
) {
2575 /* clip dirty range to EOF */
2576 if (bp
->nb_dirtyend
> end
)
2577 bp
->nb_dirtyend
= end
;
2580 bp
->nb_dirty
&= (1 << (round_page_32(end
)/PAGE_SIZE
)) - 1;
2581 /* also make sure we'll have a credential to do the write */
2582 if (mustwrite
&& (bp
->nb_wcred
== NOCRED
) && (cred
== NOCRED
)) {
2583 printf("nfs_vinvalbuf: found dirty buffer with no write creds\n");
2587 FSDBG(554, vp
, bp
, 0xd00dee, bp
->nb_flags
);
2588 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
))
2589 panic("nfs_vinvalbuf: dirty buffer without upl");
2590 /* gotta write out dirty data before invalidating */
2591 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2592 /* (NB_NOCACHE indicates buffer should be discarded) */
2593 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
));
2594 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
2595 if (bp
->nb_wcred
== NOCRED
) {
2596 kauth_cred_ref(cred
);
2597 bp
->nb_wcred
= cred
;
2599 error
= nfs_buf_write(bp
);
2600 // Note: bp has been released
2602 FSDBG(554, bp
, 0xd00dee, 0xbad, error
);
2603 np
->n_error
= error
;
2604 np
->n_flag
|= NWRITEERR
;
2606 * There was a write error and we need to
2607 * invalidate attrs to sync with server.
2608 * (if this write was extending the file,
2609 * we may no longer know the correct size)
2611 NATTRINVALIDATE(np
);
2614 lck_mtx_lock(nfs_buf_mutex
);
2618 SET(bp
->nb_flags
, NB_INVAL
);
2619 // hold off on FREEUPs until we're done here
2620 nfs_buf_release(bp
, 0);
2621 lck_mtx_lock(nfs_buf_mutex
);
2623 nfs_buf_itercomplete(np
, &blist
, list
);
2625 lck_mtx_unlock(nfs_buf_mutex
);
2628 panic("nfs_vinvalbuf: flush failed");
2634 * Flush and invalidate all dirty buffers. If another process is already
2635 * doing the flush, just wait for completion.
2645 struct nfsnode
*np
= VTONFS(vp
);
2646 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
2647 int error
= 0, slpflag
, slptimeo
;
2650 FSDBG_TOP(554, vp
, flags
, intrflg
, 0);
2652 if (nmp
&& ((nmp
->nm_flag
& NFSMNT_INT
) == 0))
2662 * First wait for any other process doing a flush to complete.
2664 while (np
->n_flag
& NFLUSHINPROG
) {
2665 np
->n_flag
|= NFLUSHWANT
;
2666 FSDBG_TOP(555, vp
, flags
, intrflg
, np
->n_flag
);
2667 error
= tsleep((caddr_t
)&np
->n_flag
, PRIBIO
+ 2, "nfsvinval", slptimeo
);
2668 FSDBG_BOT(555, vp
, flags
, intrflg
, np
->n_flag
);
2669 if (error
&& (error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
))) {
2670 FSDBG_BOT(554, vp
, flags
, intrflg
, error
);
2676 * Now, flush as required.
2678 np
->n_flag
|= NFLUSHINPROG
;
2679 error
= nfs_vinvalbuf_internal(vp
, flags
, cred
, p
, slpflag
, 0);
2681 FSDBG(554, vp
, 0, 0, error
);
2682 error
= nfs_sigintr(VFSTONFS(vnode_mount(vp
)), NULL
, p
);
2684 np
->n_flag
&= ~NFLUSHINPROG
;
2685 if (np
->n_flag
& NFLUSHWANT
) {
2686 np
->n_flag
&= ~NFLUSHWANT
;
2687 wakeup((caddr_t
)&np
->n_flag
);
2689 FSDBG_BOT(554, vp
, flags
, intrflg
, error
);
2692 error
= nfs_vinvalbuf_internal(vp
, flags
, cred
, p
, 0, slptimeo
);
2694 np
->n_flag
&= ~(NMODIFIED
| NFLUSHINPROG
);
2695 if (np
->n_flag
& NFLUSHWANT
) {
2696 np
->n_flag
&= ~NFLUSHWANT
;
2697 wakeup((caddr_t
)&np
->n_flag
);
2700 * get the pages out of vm also
2702 if (UBCINFOEXISTS(vp
) && (size
= ubc_getsize(vp
))) {
2703 int rv
= ubc_sync_range(vp
, 0, size
, UBC_PUSHALL
| UBC_INVALIDATE
);
2705 panic("nfs_vinvalbuf(): ubc_sync_range failed!");
2708 FSDBG_BOT(554, vp
, flags
, intrflg
, 0);
2713 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
2714 * This is mainly to avoid queueing async I/O requests when the nfsiods
2715 * are all hung on a dead server.
2718 nfs_asyncio(bp
, cred
)
2722 struct nfsmount
*nmp
;
2728 void *wakeme
= NULL
;
2731 if (nfs_numasync
== 0)
2734 FSDBG_TOP(552, bp
, bp
? NBOFF(bp
) : 0, bp
? bp
->nb_flags
: 0, 0);
2736 nmp
= ((bp
!= NULL
) ? VFSTONFS(vnode_mount(bp
->nb_vp
)) : NULL
);
2738 if (nmp
&& nmp
->nm_flag
& NFSMNT_INT
)
2742 lck_mtx_lock(nfs_iod_mutex
);
2744 /* no nfsbuf means tell nfsiod to process delwri list */
2749 * Find a free iod to process this request.
2751 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
2752 if (nfs_iodwant
[i
]) {
2754 * Found one, so wake it up and tell it which
2757 nfs_iodwant
[i
] = NULL
;
2758 nfs_iodmount
[i
] = nmp
;
2761 wakeme
= &nfs_iodwant
[i
];
2766 /* if we're just poking the delwri list, we're done */
2768 lck_mtx_unlock(nfs_iod_mutex
);
2771 FSDBG_BOT(552, bp
, 0x10101010, wakeme
, 0);
2776 * If none are free, we may already have an iod working on this mount
2777 * point. If so, it will process our request.
2780 if (nmp
->nm_bufqiods
> 0) {
2786 * If we have an iod which can process the request, then queue
2789 FSDBG(552, bp
, gotiod
, i
, nmp
->nm_bufqiods
);
2792 * Ensure that the queue never grows too large.
2794 while (nmp
->nm_bufqlen
>= 2*nfs_numasync
) {
2795 if (ISSET(bp
->nb_flags
, NB_IOD
)) {
2796 /* An nfsiod is attempting this async operation so */
2797 /* we must not fall asleep on the bufq because we */
2798 /* could be waiting on ourself. Just return error */
2799 /* and we'll do this operation syncrhonously. */
2802 FSDBG(552, bp
, nmp
->nm_bufqlen
, 2*nfs_numasync
, -1);
2803 nmp
->nm_bufqwant
= TRUE
;
2805 ts
.tv_sec
= (slptimeo
/100);
2806 /* the hz value is 100; which leads to 10ms */
2807 ts
.tv_nsec
= (slptimeo
% 100) * 10 * NSEC_PER_USEC
* 1000;
2809 error
= msleep(&nmp
->nm_bufq
, nfs_iod_mutex
, slpflag
| PRIBIO
,
2812 error2
= nfs_sigintr(nmp
, NULL
, bp
->nb_proc
);
2814 lck_mtx_unlock(nfs_iod_mutex
);
2815 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, error2
);
2818 if (slpflag
== PCATCH
) {
2824 * We might have lost our iod while sleeping,
2825 * so check and loop if nescessary.
2827 if (nmp
->nm_bufqiods
== 0) {
2828 lck_mtx_unlock(nfs_iod_mutex
);
2833 if (ISSET(bp
->nb_flags
, NB_READ
)) {
2834 if (bp
->nb_rcred
== NOCRED
&& cred
!= NOCRED
) {
2835 kauth_cred_ref(cred
);
2836 bp
->nb_rcred
= cred
;
2839 SET(bp
->nb_flags
, NB_WRITEINPROG
);
2840 if (bp
->nb_wcred
== NOCRED
&& cred
!= NOCRED
) {
2841 kauth_cred_ref(cred
);
2842 bp
->nb_wcred
= cred
;
2846 TAILQ_INSERT_TAIL(&nmp
->nm_bufq
, bp
, nb_free
);
2848 lck_mtx_unlock(nfs_iod_mutex
);
2851 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, 0);
2856 lck_mtx_unlock(nfs_iod_mutex
);
2858 * All the iods are busy on other mounts, so return EIO to
2859 * force the caller to process the i/o synchronously.
2861 FSDBG_BOT(552, bp
, NBOFF(bp
), bp
->nb_flags
, EIO
);
2866 * Do an I/O operation to/from a cache block. This may be called
2867 * synchronously or from an nfsiod.
2870 nfs_doio(struct nfsbuf
*bp
, kauth_cred_t cr
, proc_t p
)
2875 struct nfsmount
*nmp
;
2876 int error
= 0, diff
, len
, iomode
, invalidate
= 0;
2882 vtype
= vnode_vtype(vp
);
2884 nmp
= VFSTONFS(vnode_mount(vp
));
2886 uiop
->uio_iovs
.iov32p
= &io
;
2887 uiop
->uio_iovcnt
= 1;
2888 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
2889 uiop
->uio_segflg
= UIO_SYSSPACE
;
2891 uiop
->uio_segflg
= UIO_SYSSPACE32
;
2895 * we've decided to perform I/O for this block,
2896 * so we couldn't possibly NB_DONE. So, clear it.
2898 if (ISSET(bp
->nb_flags
, NB_DONE
)) {
2899 if (!ISSET(bp
->nb_flags
, NB_ASYNC
))
2900 panic("nfs_doio: done and not async");
2901 CLR(bp
->nb_flags
, NB_DONE
);
2903 FSDBG_TOP(256, np
->n_size
, NBOFF(bp
), bp
->nb_bufsize
, bp
->nb_flags
);
2904 FSDBG(257, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_dirtyoff
,
2907 if (ISSET(bp
->nb_flags
, NB_READ
)) {
2910 io
.iov_len
= bp
->nb_bufsize
;
2911 uio_uio_resid_set(uiop
, io
.iov_len
);
2912 io
.iov_base
= (uintptr_t) bp
->nb_data
;
2913 uiop
->uio_rw
= UIO_READ
;
2916 uiop
->uio_offset
= NBOFF(bp
);
2917 OSAddAtomic(1, (SInt32
*)&nfsstats
.read_bios
);
2918 error
= nfs_readrpc(vp
, uiop
, cr
, p
);
2919 FSDBG(262, np
->n_size
, NBOFF(bp
), uio_uio_resid(uiop
), error
);
2921 /* update valid range */
2922 bp
->nb_validoff
= 0;
2923 if (uio_uio_resid(uiop
) != 0) {
2925 * If len > 0, there is a hole in the file and
2926 * no writes after the hole have been pushed to
2928 * Just zero fill the rest of the valid area.
2930 // LP64todo - fix this
2931 diff
= bp
->nb_bufsize
- uio_uio_resid(uiop
);
2932 len
= np
->n_size
- (NBOFF(bp
) + diff
);
2934 // LP64todo - fix this
2935 len
= min(len
, uio_uio_resid(uiop
));
2936 bzero((char *)bp
->nb_data
+ diff
, len
);
2937 bp
->nb_validend
= diff
+ len
;
2938 FSDBG(258, diff
, len
, 0, 1);
2940 bp
->nb_validend
= diff
;
2942 bp
->nb_validend
= bp
->nb_bufsize
;
2943 bp
->nb_valid
= (1 << (round_page_32(bp
->nb_validend
)/PAGE_SIZE
)) - 1;
2944 if (bp
->nb_validend
& PAGE_MASK
) {
2945 /* valid range ends in the middle of a page so we */
2946 /* need to zero-fill any invalid data at the end */
2947 /* of the last page */
2948 bzero((caddr_t
)(bp
->nb_data
+ bp
->nb_validend
),
2949 bp
->nb_bufsize
- bp
->nb_validend
);
2950 FSDBG(258, bp
->nb_validend
,
2951 bp
->nb_bufsize
- bp
->nb_validend
, 0, 2);
2956 uiop
->uio_offset
= (off_t
)0;
2957 OSAddAtomic(1, (SInt32
*)&nfsstats
.readlink_bios
);
2958 error
= nfs_readlinkrpc(vp
, uiop
, cr
, p
);
2960 bp
->nb_validoff
= 0;
2961 bp
->nb_validend
= uiop
->uio_offset
;
2965 OSAddAtomic(1, (SInt32
*)&nfsstats
.readdir_bios
);
2966 uiop
->uio_offset
= NBOFF(bp
);
2967 if (!(nmp
->nm_flag
& NFSMNT_NFSV3
))
2968 nmp
->nm_flag
&= ~NFSMNT_RDIRPLUS
; /* dk@farm.org */
2969 if (nmp
->nm_flag
& NFSMNT_RDIRPLUS
) {
2970 error
= nfs_readdirplusrpc(vp
, uiop
, cr
, p
);
2971 if (error
== NFSERR_NOTSUPP
)
2972 nmp
->nm_flag
&= ~NFSMNT_RDIRPLUS
;
2974 if ((nmp
->nm_flag
& NFSMNT_RDIRPLUS
) == 0)
2975 error
= nfs_readdirrpc(vp
, uiop
, cr
, p
);
2977 bp
->nb_validoff
= 0;
2978 bp
->nb_validend
= uiop
->uio_offset
- NBOFF(bp
);
2979 bp
->nb_valid
= (1 << (round_page_32(bp
->nb_validend
)/PAGE_SIZE
)) - 1;
2983 printf("nfs_doio: type %x unexpected\n", vtype
);
2987 SET(bp
->nb_flags
, NB_ERROR
);
2988 bp
->nb_error
= error
;
2992 /* we're doing a write */
2995 /* We need to make sure the pages are locked before doing I/O. */
2996 if (!ISSET(bp
->nb_flags
, NB_META
) && UBCINFOEXISTS(vp
)) {
2997 if (!ISSET(bp
->nb_flags
, NB_PAGELIST
)) {
2998 error
= nfs_buf_upl_setup(bp
);
3000 printf("nfs_doio: upl create failed %d\n", error
);
3001 SET(bp
->nb_flags
, NB_ERROR
);
3005 nfs_buf_upl_check(bp
);
3009 if (ISSET(bp
->nb_flags
, NB_WASDIRTY
)) {
3010 FSDBG(256, bp
, NBOFF(bp
), bp
->nb_dirty
, 0xd00dee);
3012 * There are pages marked dirty that need to be written out.
3014 * We don't want to just combine the write range with the
3015 * range of pages that are dirty because that could cause us
3016 * to write data that wasn't actually written to.
3017 * We also don't want to write data more than once.
3019 * If the dirty range just needs to be committed, we do that.
3020 * Otherwise, we write the dirty range and clear the dirty bits
3021 * for any COMPLETE pages covered by that range.
3022 * If there are dirty pages left after that, we write out the
3023 * parts that we haven't written yet.
3028 * If NB_NEEDCOMMIT is set, a commit rpc may do the trick. If not
3029 * an actual write will have to be done.
3030 * If NB_WRITEINPROG is already set, then push it with a write anyhow.
3032 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
3033 nfs_buf_check_write_verifier(np
, bp
);
3034 if ((bp
->nb_flags
& (NB_NEEDCOMMIT
| NB_WRITEINPROG
)) == NB_NEEDCOMMIT
) {
3035 doff
= NBOFF(bp
) + bp
->nb_dirtyoff
;
3036 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3037 error
= nfs_commit(vp
, doff
, bp
->nb_dirtyend
- bp
->nb_dirtyoff
,
3038 bp
->nb_wcred
, bp
->nb_proc
);
3039 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3041 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3042 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3043 np
->n_needcommitcnt
--;
3044 CHECK_NEEDCOMMITCNT(np
);
3048 if (!error
&& bp
->nb_dirtyend
> 0) {
3049 /* there's a dirty range that needs to be written out */
3051 int firstpg
, lastpg
;
3053 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
)
3054 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3058 doff
= bp
->nb_dirtyoff
;
3059 dend
= bp
->nb_dirtyend
;
3061 /* if doff page is dirty, move doff to start of page */
3062 if (NBPGDIRTY(bp
,doff
/PAGE_SIZE
))
3063 doff
-= doff
& PAGE_MASK
;
3064 /* try to expand write range to include preceding dirty pages */
3065 if (!(doff
& PAGE_MASK
))
3066 while (doff
> 0 && NBPGDIRTY(bp
,(doff
-1)/PAGE_SIZE
))
3068 /* if dend page is dirty, move dend to start of next page */
3069 if ((dend
& PAGE_MASK
) && NBPGDIRTY(bp
,dend
/PAGE_SIZE
))
3070 dend
= round_page_32(dend
);
3071 /* try to expand write range to include trailing dirty pages */
3072 if (!(dend
& PAGE_MASK
))
3073 while (dend
< bp
->nb_bufsize
&& NBPGDIRTY(bp
,dend
/PAGE_SIZE
))
3075 /* make sure to keep dend clipped to EOF */
3076 if (NBOFF(bp
) + dend
> (off_t
)np
->n_size
)
3077 dend
= np
->n_size
- NBOFF(bp
);
3078 /* calculate range of complete pages being written */
3079 firstpg
= round_page_32(doff
) / PAGE_SIZE
;
3080 lastpg
= (trunc_page_32(dend
) - 1)/ PAGE_SIZE
;
3081 /* calculate mask for that page range */
3082 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
3084 /* compare page mask to nb_dirty; if there are other dirty pages */
3085 /* then write FILESYNC; otherwise, write UNSTABLE if async and */
3086 /* not needcommit/nocache/call; otherwise write FILESYNC */
3087 if (bp
->nb_dirty
& ~pagemask
)
3088 iomode
= NFSV3WRITE_FILESYNC
;
3089 else if ((bp
->nb_flags
& (NB_ASYNC
| NB_NEEDCOMMIT
| NB_NOCACHE
| NB_STABLE
)) == NB_ASYNC
)
3090 iomode
= NFSV3WRITE_UNSTABLE
;
3092 iomode
= NFSV3WRITE_FILESYNC
;
3094 /* write the dirty range */
3095 io
.iov_len
= dend
- doff
;
3096 uio_uio_resid_set(uiop
, io
.iov_len
);
3097 uiop
->uio_offset
= NBOFF(bp
) + doff
;
3098 io
.iov_base
= (uintptr_t) bp
->nb_data
+ doff
;
3099 uiop
->uio_rw
= UIO_WRITE
;
3101 OSAddAtomic(1, (SInt32
*)&nfsstats
.write_bios
);
3103 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3104 error
= nfs_writerpc(vp
, uiop
, cr
, p
, &iomode
, &bp
->nb_verf
);
3105 /* clear dirty bits for pages we've written */
3107 bp
->nb_dirty
&= ~pagemask
;
3108 /* set/clear needcommit flag */
3109 if (!error
&& iomode
== NFSV3WRITE_UNSTABLE
) {
3110 if (!ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
))
3111 np
->n_needcommitcnt
++;
3112 SET(bp
->nb_flags
, NB_NEEDCOMMIT
);
3113 /* make sure nb_dirtyoff/nb_dirtyend reflect actual range written */
3114 bp
->nb_dirtyoff
= doff
;
3115 bp
->nb_dirtyend
= dend
;
3117 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3118 np
->n_needcommitcnt
--;
3119 CHECK_NEEDCOMMITCNT(np
);
3121 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3123 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3125 * For an interrupted write, the buffer is still valid and the write
3126 * hasn't been pushed to the server yet, so we can't set NB_ERROR and
3127 * report the interruption by setting NB_EINTR. For the NB_ASYNC case,
3128 * NB_EINTR is not relevant.
3130 * For the case of a V3 write rpc not being committed to stable
3131 * storage, the block is still dirty and requires either a commit rpc
3132 * or another write rpc with iomode == NFSV3WRITE_FILESYNC before the
3133 * block is reused. This is indicated by setting the NB_DELWRI and
3134 * NB_NEEDCOMMIT flags.
3136 if (error
== EINTR
|| (!error
&& bp
->nb_flags
& NB_NEEDCOMMIT
)) {
3137 CLR(bp
->nb_flags
, NB_INVAL
| NB_NOCACHE
);
3138 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3139 SET(bp
->nb_flags
, NB_DELWRI
);
3140 OSAddAtomic(1, (SInt32
*)&nfs_nbdwrite
);
3143 FSDBG(261, bp
->nb_validoff
, bp
->nb_validend
,
3146 * Since for the NB_ASYNC case, nfs_bwrite() has
3147 * reassigned the buffer to the clean list, we have to
3148 * reassign it back to the dirty one. Ugh.
3150 if (ISSET(bp
->nb_flags
, NB_ASYNC
)) {
3151 /* move to dirty list */
3152 lck_mtx_lock(nfs_buf_mutex
);
3153 if (bp
->nb_vnbufs
.le_next
!= NFSNOLIST
)
3154 LIST_REMOVE(bp
, nb_vnbufs
);
3155 LIST_INSERT_HEAD(&np
->n_dirtyblkhd
, bp
, nb_vnbufs
);
3156 lck_mtx_unlock(nfs_buf_mutex
);
3158 SET(bp
->nb_flags
, NB_EINTR
);
3161 /* either there's an error or we don't need to commit */
3163 SET(bp
->nb_flags
, NB_ERROR
);
3164 bp
->nb_error
= np
->n_error
= error
;
3165 np
->n_flag
|= NWRITEERR
;
3167 * There was a write error and we need to
3168 * invalidate attrs and flush buffers in
3169 * order to sync up with the server.
3170 * (if this write was extending the file,
3171 * we may no longer know the correct size)
3173 * But we can't call vinvalbuf while holding
3174 * this buffer busy. Set a flag to do it after
3175 * releasing the buffer.
3177 * Note we can only invalidate in this function
3178 * if this is an async write and so the iodone
3179 * below will release the buffer. Also, we
3180 * shouldn't call vinvalbuf from nfsiod because
3181 * that may deadlock waiting for the completion
3182 * of writes that are queued up behind this one.
3184 if (ISSET(bp
->nb_flags
, NB_ASYNC
) &&
3185 !ISSET(bp
->nb_flags
, NB_IOD
)) {
3188 /* invalidate later */
3189 np
->n_flag
|= NNEEDINVALIDATE
;
3191 NATTRINVALIDATE(np
);
3193 /* clear the dirty range */
3194 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3198 if (!error
&& bp
->nb_dirty
) {
3199 /* there are pages marked dirty that need to be written out */
3200 int pg
, count
, npages
, off
;
3202 OSAddAtomic(1, (SInt32
*)&nfsstats
.write_bios
);
3207 * we do these writes synchronously because we can't really
3208 * support the unstable/needommit method. We could write
3209 * them unstable, clear the dirty bits, and then commit the
3210 * whole block later, but if we need to rewrite the data, we
3211 * won't have any idea which pages were written because that
3212 * info can't be stored in the nb_dirtyoff/nb_dirtyend. We
3213 * also can't leave the dirty bits set because then we wouldn't
3214 * be able to tell if the pages were re-dirtied between the end
3215 * of the write and the commit.
3217 iomode
= NFSV3WRITE_FILESYNC
;
3218 uiop
->uio_rw
= UIO_WRITE
;
3220 SET(bp
->nb_flags
, NB_WRITEINPROG
);
3221 npages
= bp
->nb_bufsize
/PAGE_SIZE
;
3222 for (pg
=0; pg
< npages
; pg
++) {
3223 if (!NBPGDIRTY(bp
,pg
))
3226 while (((pg
+count
) < npages
) && NBPGDIRTY(bp
,pg
+count
))
3228 /* write count pages starting with page pg */
3229 off
= pg
* PAGE_SIZE
;
3230 len
= count
* PAGE_SIZE
;
3232 /* clip writes to EOF */
3233 if (NBOFF(bp
) + off
+ len
> (off_t
)np
->n_size
)
3234 len
-= (NBOFF(bp
) + off
+ len
) - np
->n_size
;
3237 uio_uio_resid_set(uiop
, io
.iov_len
);
3238 uiop
->uio_offset
= NBOFF(bp
) + off
;
3239 io
.iov_base
= (uintptr_t) bp
->nb_data
+ off
;
3240 error
= nfs_writerpc(vp
, uiop
, cr
, p
, &iomode
, &bp
->nb_verf
);
3244 /* clear dirty bits */
3246 bp
->nb_dirty
&= ~(1 << pg
);
3247 /* leave pg on last page */
3252 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3253 np
->n_needcommitcnt
--;
3254 CHECK_NEEDCOMMITCNT(np
);
3256 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3258 CLR(bp
->nb_flags
, NB_WRITEINPROG
);
3259 FSDBG_BOT(256, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_bufsize
,
3264 SET(bp
->nb_flags
, NB_ERROR
);
3265 bp
->nb_error
= error
;
3269 FSDBG_BOT(256, bp
->nb_validoff
, bp
->nb_validend
, bp
->nb_bufsize
, error
);
3275 * There was a write error and we need to
3276 * invalidate attrs and flush buffers in
3277 * order to sync up with the server.
3278 * (if this write was extending the file,
3279 * we may no longer know the correct size)
3281 * But we couldn't call vinvalbuf while holding
3282 * the buffer busy. So we call vinvalbuf() after
3283 * releasing the buffer.
3285 * Note: we don't bother calling nfs_vinvalbuf() if
3286 * there's already a flush in progress.
3288 if (!(np
->n_flag
& NFLUSHINPROG
))
3289 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, cr
, p
, 1);