]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_bio.c
4117cb7a7085faed7c69e34e2413e502b86b4885
[apple/xnu.git] / bsd / nfs / nfs_bio.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
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.
45 *
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
56 * SUCH DAMAGE.
57 *
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 $
60 */
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>
75
76 #include <sys/vm.h>
77 #include <sys/vmparam.h>
78
79 #include <sys/time.h>
80 #include <kern/clock.h>
81 #include <libkern/OSAtomic.h>
82 #include <kern/kalloc.h>
83
84 #include <nfs/rpcv2.h>
85 #include <nfs/nfsproto.h>
86 #include <nfs/nfs.h>
87 #include <nfs/nfsmount.h>
88 #include <nfs/nfsnode.h>
89 #include <sys/buf_internal.h>
90
91 #include <sys/kdebug.h>
92
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)
102
103 extern int nfs_numasync;
104 extern int nfs_ioddelwri;
105 extern struct nfsstats nfsstats;
106
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;
111 u_long nfsbufhash;
112 int nfsbufcnt, nfsbufmin, nfsbufmax, nfsbufmetacnt, nfsbufmetamax;
113 int nfsbuffreecnt, nfsbuffreemetacnt, nfsbufdelwricnt, nfsneedbuffer;
114 int nfs_nbdwrite;
115 time_t nfsbuffreeuptimestamp;
116
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;
121
122 #define NFSBUFWRITE_THROTTLE 9
123 #define NFSBUF_LRU_STALE 120
124 #define NFSBUF_META_STALE 240
125
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
140
141 #define NFS_BUF_FREEUP() \
142 do { \
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)) \
147 nfs_buf_freeup(0); \
148 } while (0)
149
150 /*
151 * Initialize nfsbuf lists
152 */
153 void
154 nfs_nbinit(void)
155 {
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);
159
160 nfs_buf_lck_attr = lck_attr_alloc_init();
161
162 nfs_buf_mutex = lck_mtx_alloc_init(nfs_buf_lck_grp, nfs_buf_lck_attr);
163
164 nfsbufcnt = nfsbufmetacnt =
165 nfsbuffreecnt = nfsbuffreemetacnt = nfsbufdelwricnt = 0;
166 nfsbufmin = 128;
167 nfsbufmax = (sane_size >> PAGE_SHIFT) / 4;
168 nfsbufmetamax = (sane_size >> PAGE_SHIFT) / 16;
169 nfsneedbuffer = 0;
170 nfs_nbdwrite = 0;
171 nfsbuffreeuptimestamp = 0;
172
173 nfsbufhashtbl = hashinit(nfsbufmax/4, M_TEMP, &nfsbufhash);
174 TAILQ_INIT(&nfsbuffree);
175 TAILQ_INIT(&nfsbuffreemeta);
176 TAILQ_INIT(&nfsbufdelwri);
177
178 }
179
180 /*
181 * try to free up some excess, unused nfsbufs
182 */
183 void
184 nfs_buf_freeup(int timer)
185 {
186 struct nfsbuf *fbp;
187 struct timeval now;
188 int count;
189 struct nfsbuffreehead nfsbuffreeup;
190
191 TAILQ_INIT(&nfsbuffreeup);
192
193 lck_mtx_lock(nfs_buf_mutex);
194
195 microuptime(&now);
196 nfsbuffreeuptimestamp = now.tv_sec;
197
198 FSDBG(320, nfsbufcnt, nfsbuffreecnt, nfsbuffreemetacnt, 0);
199
200 count = timer ? nfsbuffreecnt/LRU_FREEUP_FRAC_ON_TIMER : LRU_TO_FREEUP;
201 while ((nfsbufcnt > nfsbufmin) && (count-- > 0)) {
202 fbp = TAILQ_FIRST(&nfsbuffree);
203 if (!fbp)
204 break;
205 if (fbp->nb_refs)
206 break;
207 if (NBUFSTAMPVALID(fbp) &&
208 (fbp->nb_timestamp + (2*NFSBUF_LRU_STALE)) > now.tv_sec)
209 break;
210 nfs_buf_remfree(fbp);
211 /* disassociate buffer from any vnode */
212 if (fbp->nb_vp) {
213 if (fbp->nb_vnbufs.le_next != NFSNOLIST) {
214 LIST_REMOVE(fbp, nb_vnbufs);
215 fbp->nb_vnbufs.le_next = NFSNOLIST;
216 }
217 fbp->nb_vp = NULL;
218 }
219 LIST_REMOVE(fbp, nb_hash);
220 TAILQ_INSERT_TAIL(&nfsbuffreeup, fbp, nb_free);
221 nfsbufcnt--;
222 }
223
224 count = timer ? nfsbuffreemetacnt/META_FREEUP_FRAC_ON_TIMER : META_TO_FREEUP;
225 while ((nfsbufcnt > nfsbufmin) && (count-- > 0)) {
226 fbp = TAILQ_FIRST(&nfsbuffreemeta);
227 if (!fbp)
228 break;
229 if (fbp->nb_refs)
230 break;
231 if (NBUFSTAMPVALID(fbp) &&
232 (fbp->nb_timestamp + (2*NFSBUF_META_STALE)) > now.tv_sec)
233 break;
234 nfs_buf_remfree(fbp);
235 /* disassociate buffer from any vnode */
236 if (fbp->nb_vp) {
237 if (fbp->nb_vnbufs.le_next != NFSNOLIST) {
238 LIST_REMOVE(fbp, nb_vnbufs);
239 fbp->nb_vnbufs.le_next = NFSNOLIST;
240 }
241 fbp->nb_vp = NULL;
242 }
243 LIST_REMOVE(fbp, nb_hash);
244 TAILQ_INSERT_TAIL(&nfsbuffreeup, fbp, nb_free);
245 nfsbufcnt--;
246 nfsbufmetacnt--;
247 }
248
249 FSDBG(320, nfsbufcnt, nfsbuffreecnt, nfsbuffreemetacnt, 0);
250 NFSBUFCNTCHK(1);
251
252 lck_mtx_unlock(nfs_buf_mutex);
253
254 while ((fbp = TAILQ_FIRST(&nfsbuffreeup))) {
255 TAILQ_REMOVE(&nfsbuffreeup, fbp, nb_free);
256 /* nuke any creds */
257 if (fbp->nb_rcred != NOCRED) {
258 kauth_cred_rele(fbp->nb_rcred);
259 fbp->nb_rcred = NOCRED;
260 }
261 if (fbp->nb_wcred != NOCRED) {
262 kauth_cred_rele(fbp->nb_wcred);
263 fbp->nb_wcred = NOCRED;
264 }
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);
268 FREE(fbp, M_TEMP);
269 }
270
271 }
272
273 /*
274 * remove a buffer from the freelist
275 * (must be called with nfs_buf_mutex held)
276 */
277 void
278 nfs_buf_remfree(struct nfsbuf *bp)
279 {
280 if (bp->nb_free.tqe_next == NFSNOLIST)
281 panic("nfsbuf not on free list");
282 if (ISSET(bp->nb_flags, NB_DELWRI)) {
283 nfsbufdelwricnt--;
284 TAILQ_REMOVE(&nfsbufdelwri, bp, nb_free);
285 } else if (ISSET(bp->nb_flags, NB_META)) {
286 nfsbuffreemetacnt--;
287 TAILQ_REMOVE(&nfsbuffreemeta, bp, nb_free);
288 } else {
289 nfsbuffreecnt--;
290 TAILQ_REMOVE(&nfsbuffree, bp, nb_free);
291 }
292 bp->nb_free.tqe_next = NFSNOLIST;
293 NFSBUFCNTCHK(1);
294 }
295
296 /*
297 * check for existence of nfsbuf in cache
298 */
299 boolean_t
300 nfs_buf_is_incore(vnode_t vp, daddr64_t blkno)
301 {
302 boolean_t rv;
303 lck_mtx_lock(nfs_buf_mutex);
304 if (nfs_buf_incore(vp, blkno))
305 rv = TRUE;
306 else
307 rv = FALSE;
308 lck_mtx_unlock(nfs_buf_mutex);
309 return (rv);
310 }
311
312 /*
313 * return incore buffer (must be called with nfs_buf_mutex held)
314 */
315 struct nfsbuf *
316 nfs_buf_incore(vnode_t vp, daddr64_t blkno)
317 {
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);
324 return (bp);
325 }
326 }
327 return (NULL);
328 }
329
330 /*
331 * Check if it's OK to drop a page.
332 *
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).
337 */
338 int
339 nfs_buf_page_inval(vnode_t vp, off_t offset)
340 {
341 struct nfsbuf *bp;
342 int error = 0;
343
344 lck_mtx_lock(nfs_buf_mutex);
345 bp = nfs_buf_incore(vp, ubc_offtoblk(vp, offset));
346 if (!bp)
347 goto out;
348 FSDBG(325, bp, bp->nb_flags, bp->nb_dirtyoff, bp->nb_dirtyend);
349 if (ISSET(bp->nb_lflags, NBL_BUSY)) {
350 error = EBUSY;
351 goto out;
352 }
353 /*
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.
357 */
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))
362 error = 0;
363 else
364 error = EBUSY;
365 }
366 out:
367 lck_mtx_unlock(nfs_buf_mutex);
368 return (error);
369 }
370
371 /*
372 * set up the UPL for a buffer
373 * (must NOT be called with nfs_buf_mutex held)
374 */
375 int
376 nfs_buf_upl_setup(struct nfsbuf *bp)
377 {
378 kern_return_t kret;
379 upl_t upl;
380 int upl_flags;
381
382 if (ISSET(bp->nb_flags, NB_PAGELIST))
383 return (0);
384
385 upl_flags = UPL_PRECIOUS;
386 if (! ISSET(bp->nb_flags, NB_READ)) {
387 /*
388 * We're doing a "write", so we intend to modify
389 * the pages we're gathering.
390 */
391 upl_flags |= UPL_WILL_MODIFY;
392 }
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;
398 return (EINVAL);
399 }
400 if (kret != KERN_SUCCESS) {
401 printf("nfs_buf_upl_setup(): failed to get pagelist %d\n", kret);
402 bp->nb_pagelist = NULL;
403 return (EIO);
404 }
405
406 FSDBG(538, bp, NBOFF(bp), bp->nb_bufsize, bp->nb_vp);
407
408 bp->nb_pagelist = upl;
409 SET(bp->nb_flags, NB_PAGELIST);
410 return (0);
411 }
412
413 /*
414 * update buffer's valid/dirty info from UBC
415 * (must NOT be called with nfs_buf_mutex held)
416 */
417 void
418 nfs_buf_upl_check(struct nfsbuf *bp)
419 {
420 upl_page_info_t *pl;
421 off_t filesize, fileoffset;
422 int i, npages;
423
424 if (!ISSET(bp->nb_flags, NB_PAGELIST))
425 return;
426
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);
432 else
433 CLR(bp->nb_flags, NB_CACHE);
434
435 pl = ubc_upl_pageinfo(bp->nb_pagelist);
436 bp->nb_valid = bp->nb_dirty = 0;
437
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)
441 break;
442 if (!upl_valid_page(pl, i)) {
443 CLR(bp->nb_flags, NB_CACHE);
444 continue;
445 }
446 NBPGVALID_SET(bp,i);
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);
451 }
452 }
453 fileoffset = NBOFF(bp);
454 if (ISSET(bp->nb_flags, NB_CACHE)) {
455 bp->nb_validoff = 0;
456 bp->nb_validend = bp->nb_bufsize;
457 if (fileoffset + bp->nb_validend > filesize)
458 bp->nb_validend = filesize - fileoffset;
459 } else {
460 bp->nb_validoff = bp->nb_validend = -1;
461 }
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);
464 }
465
466 /*
467 * make sure that a buffer is mapped
468 * (must NOT be called with nfs_buf_mutex held)
469 */
470 static int
471 nfs_buf_map(struct nfsbuf *bp)
472 {
473 kern_return_t kret;
474
475 if (bp->nb_data)
476 return (0);
477 if (!ISSET(bp->nb_flags, NB_PAGELIST))
478 return (EINVAL);
479
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);
486 return (0);
487 }
488
489 /*
490 * check range of pages in nfsbuf's UPL for validity
491 */
492 static int
493 nfs_buf_upl_valid_range(struct nfsbuf *bp, int off, int size)
494 {
495 off_t fileoffset, filesize;
496 int pg, lastpg;
497 upl_page_info_t *pl;
498
499 if (!ISSET(bp->nb_flags, NB_PAGELIST))
500 return (0);
501 pl = ubc_upl_pageinfo(bp->nb_pagelist);
502
503 size += off & PAGE_MASK;
504 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);
509
510 pg = off/PAGE_SIZE;
511 lastpg = (off + size - 1)/PAGE_SIZE;
512 while (pg <= lastpg) {
513 if (!upl_valid_page(pl, pg))
514 return (0);
515 pg++;
516 }
517 return (1);
518 }
519
520 /*
521 * normalize an nfsbuf's valid range
522 *
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.
528 */
529 static void
530 nfs_buf_normalize_valid_range(struct nfsnode *np, struct nfsbuf *bp)
531 {
532 int pg, npg;
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))
536 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))
542 pg++;
543 bp->nb_validend = pg * PAGE_SIZE;
544 /* clip to EOF */
545 if (NBOFF(bp) + bp->nb_validend > (off_t)np->n_size)
546 bp->nb_validend = np->n_size % bp->nb_bufsize;
547 }
548
549 /*
550 * try to push out some delayed/uncommitted writes
551 * ("locked" indicates whether nfs_buf_mutex is already held)
552 */
553 static void
554 nfs_buf_delwri_push(int locked)
555 {
556 struct nfsbuf *bp;
557 int i, error;
558
559 if (TAILQ_EMPTY(&nfsbufdelwri))
560 return;
561
562 /* first try to tell the nfsiods to do it */
563 if (nfs_asyncio(NULL, NULL) == 0)
564 return;
565
566 /* otherwise, try to do some of the work ourselves */
567 i = 0;
568 if (!locked)
569 lck_mtx_lock(nfs_buf_mutex);
570 while (i < 8 && (bp = TAILQ_FIRST(&nfsbufdelwri)) != NULL) {
571 struct nfsnode *np = VTONFS(bp->nb_vp);
572 nfs_buf_remfree(bp);
573 nfs_buf_refget(bp);
574 while ((error = nfs_buf_acquire(bp, 0, 0, 0)) == EAGAIN);
575 nfs_buf_refrele(bp);
576 if (error)
577 break;
578 if (!bp->nb_vp) {
579 /* buffer is no longer valid */
580 nfs_buf_drop(bp);
581 continue;
582 }
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);
588 nfsbufdelwricnt++;
589 nfs_buf_drop(bp);
590 lck_mtx_unlock(nfs_buf_mutex);
591 nfs_flushcommits(np->n_vnode, NULL, 1);
592 } else {
593 SET(bp->nb_flags, NB_ASYNC);
594 lck_mtx_unlock(nfs_buf_mutex);
595 nfs_buf_write(bp);
596 }
597 i++;
598 lck_mtx_lock(nfs_buf_mutex);
599 }
600 if (!locked)
601 lck_mtx_unlock(nfs_buf_mutex);
602 }
603
604 /*
605 * Get an nfs buffer.
606 *
607 * Returns errno on error, 0 otherwise.
608 * Any buffer is returned in *bpp.
609 *
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.
612 *
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.
617 *
618 * If available buffer found, prepare it, and return it.
619 *
620 * If the calling process is interrupted by a signal for
621 * an interruptible mount point, return EINTR.
622 */
623 int
624 nfs_buf_get(
625 vnode_t vp,
626 daddr64_t blkno,
627 int size,
628 proc_t p,
629 int flags,
630 struct nfsbuf **bpp)
631 {
632 struct nfsnode *np = VTONFS(vp);
633 struct nfsbuf *bp;
634 int biosize, bufsize;
635 kauth_cred_t cred;
636 int slpflag = PCATCH;
637 int operation = (flags & NBLK_OPMASK);
638 int error = 0;
639 struct timespec ts;
640
641 FSDBG_TOP(541, vp, blkno, size, flags);
642 *bpp = NULL;
643
644 bufsize = size;
645 if (bufsize > MAXBSIZE)
646 panic("nfs_buf_get: buffer larger than MAXBSIZE requested");
647
648 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
649
650 if (UBCINVALID(vp) || !UBCINFOEXISTS(vp)) {
651 operation = NBLK_META;
652 } else if (bufsize < biosize) {
653 /* reg files should always have biosize blocks */
654 bufsize = biosize;
655 }
656
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));
660
661 /* poke the delwri list */
662 nfs_buf_delwri_push(0);
663
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));
667 }
668
669 loop:
670 lck_mtx_lock(nfs_buf_mutex);
671
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);
679 return (0);
680 }
681 FSDBG_TOP(543, vp, blkno, bp, bp->nb_flags);
682 SET(bp->nb_lflags, NBL_WANTED);
683
684 ts.tv_sec = 2;
685 ts.tv_nsec = 0;
686 msleep(bp, nfs_buf_mutex, slpflag|(PRIBIO+1)|PDROP,
687 "nfsbufget", (slpflag == PCATCH) ? 0 : &ts);
688 slpflag = 0;
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);
692 return (error);
693 }
694 goto loop;
695 }
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);
700 nfs_buf_remfree(bp);
701 /* additional paranoia: */
702 if (ISSET(bp->nb_flags, NB_PAGELIST))
703 panic("pagelist buffer was not busy");
704 goto buffer_setup;
705 }
706
707 if (flags & NBLK_ONLYVALID) {
708 lck_mtx_unlock(nfs_buf_mutex);
709 FSDBG_BOT(541, vp, blkno, 0, 0x0000cace);
710 return (0);
711 }
712
713 /*
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
723 */
724
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;
732 struct timeval now;
733 microuptime(&now);
734
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)))
739 bp = lrubp;
740 metabp = TAILQ_FIRST(&nfsbuffreemeta);
741 if (!bp && metabp && (!NBUFSTAMPVALID(metabp) ||
742 ((metabp->nb_timestamp + NFSBUF_META_STALE) < now.tv_sec)))
743 bp = metabp;
744
745 if (!bp && (nfsbufcnt >= nfsbufmax)) {
746 /* we've already allocated all bufs, so */
747 /* choose the buffer that'll go stale first */
748 if (!metabp)
749 bp = lrubp;
750 else if (!lrubp)
751 bp = metabp;
752 else {
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)
757 bp = lrubp;
758 else
759 bp = metabp;
760 }
761 }
762 }
763
764 if (bp) {
765 /* we have a buffer to reuse */
766 FSDBG(544, vp, blkno, bp, bp->nb_flags);
767 nfs_buf_remfree(bp);
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 */
772 if (bp->nb_vp) {
773 if (bp->nb_vnbufs.le_next != NFSNOLIST) {
774 LIST_REMOVE(bp, nb_vnbufs);
775 bp->nb_vnbufs.le_next = NFSNOLIST;
776 }
777 bp->nb_vp = NULL;
778 }
779 LIST_REMOVE(bp, nb_hash);
780 /* nuke any creds we're holding */
781 cred = bp->nb_rcred;
782 if (cred != NOCRED) {
783 bp->nb_rcred = NOCRED;
784 kauth_cred_rele(cred);
785 }
786 cred = bp->nb_wcred;
787 if (cred != NOCRED) {
788 bp->nb_wcred = NOCRED;
789 kauth_cred_rele(cred);
790 }
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))
794 nfsbufmetacnt++;
795 } else if (ISSET(bp->nb_flags, NB_META)) {
796 if (bp->nb_data) {
797 kfree(bp->nb_data, bp->nb_bufsize);
798 bp->nb_data = NULL;
799 }
800 nfsbufmetacnt--;
801 }
802 /* re-init buf fields */
803 bp->nb_error = 0;
804 bp->nb_validoff = bp->nb_validend = -1;
805 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
806 bp->nb_valid = 0;
807 bp->nb_dirty = 0;
808 bp->nb_verf = 0;
809 } else {
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);
815 if (!bp) {
816 lck_mtx_unlock(nfs_buf_mutex);
817 FSDBG_BOT(541, vp, blkno, 0, error);
818 return (ENOMEM);
819 }
820 nfsbufcnt++;
821 if (operation == NBLK_META)
822 nfsbufmetacnt++;
823 NFSBUFCNTCHK(1);
824 /* init nfsbuf */
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);
829 } else {
830 /* too many bufs... wait for buffers to free up */
831 FSDBG_TOP(546, vp, blkno, nfsbufcnt, nfsbufmax);
832
833 /* poke the delwri list */
834 nfs_buf_delwri_push(1);
835
836 nfsneedbuffer = 1;
837 msleep(&nfsneedbuffer, nfs_buf_mutex, PCATCH|PDROP,
838 "nfsbufget", 0);
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);
842 return (error);
843 }
844 goto loop;
845 }
846 }
847
848 /* setup nfsbuf */
849 bp->nb_lflags = NBL_BUSY;
850 bp->nb_flags = 0;
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 */
855 bp->nb_vp = vp;
856 LIST_INSERT_HEAD(&np->n_cleanblkhd, bp, nb_vnbufs);
857
858 buffer_setup:
859
860 /* unlock hash */
861 lck_mtx_unlock(nfs_buf_mutex);
862
863 switch (operation) {
864 case NBLK_META:
865 SET(bp->nb_flags, NB_META);
866 if ((bp->nb_bufsize != bufsize) && bp->nb_data) {
867 kfree(bp->nb_data, bp->nb_bufsize);
868 bp->nb_data = NULL;
869 bp->nb_validoff = bp->nb_validend = -1;
870 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
871 bp->nb_valid = 0;
872 bp->nb_dirty = 0;
873 CLR(bp->nb_flags, NB_CACHE);
874 }
875 if (!bp->nb_data)
876 bp->nb_data = kalloc(bufsize);
877 if (!bp->nb_data) {
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;
883 bp->nb_vp = NULL;
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);
889 nfsbuffreecnt++;
890 lck_mtx_unlock(nfs_buf_mutex);
891 FSDBG_BOT(541, vp, blkno, 0xb00, ENOMEM);
892 return (ENOMEM);
893 }
894 bp->nb_bufsize = bufsize;
895 break;
896
897 case NBLK_READ:
898 case NBLK_WRITE:
899 /*
900 * Set or clear NB_READ now to let the UPL subsystem know
901 * if we intend to modify the pages or not.
902 */
903 if (operation == NBLK_READ) {
904 SET(bp->nb_flags, NB_READ);
905 } else {
906 CLR(bp->nb_flags, NB_READ);
907 }
908 if (bufsize < PAGE_SIZE)
909 bufsize = PAGE_SIZE;
910 bp->nb_bufsize = bufsize;
911 bp->nb_validoff = bp->nb_validend = -1;
912
913 if (UBCINFOEXISTS(vp)) {
914 /* setup upl */
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;
922 bp->nb_vp = NULL;
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);
928 nfsbuffreecnt++;
929 lck_mtx_unlock(nfs_buf_mutex);
930 FSDBG_BOT(541, vp, blkno, 0x2bc, EIO);
931 return (EIO);
932 }
933 nfs_buf_upl_check(bp);
934 }
935 break;
936
937 default:
938 panic("nfs_buf_get: %d unknown operation", operation);
939 }
940
941 *bpp = bp;
942
943 FSDBG_BOT(541, vp, blkno, bp, bp->nb_flags);
944
945 return (0);
946 }
947
948 void
949 nfs_buf_release(struct nfsbuf *bp, int freeup)
950 {
951 vnode_t vp = bp->nb_vp;
952 struct timeval now;
953 int wakeup_needbuffer, wakeup_buffer, wakeup_nbdwrite;
954
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);
958
959 if (UBCINFOEXISTS(vp) && bp->nb_bufsize) {
960 int upl_flags;
961 upl_t upl;
962 int i, rv;
963
964 if (!ISSET(bp->nb_flags, NB_PAGELIST) && !ISSET(bp->nb_flags, NB_INVAL)) {
965 rv = nfs_buf_upl_setup(bp);
966 if (rv)
967 printf("nfs_buf_release: upl create failed %d\n", rv);
968 else
969 nfs_buf_upl_check(bp);
970 }
971 upl = bp->nb_pagelist;
972 if (!upl)
973 goto pagelist_cleanup_done;
974 if (bp->nb_data) {
975 if (ubc_upl_unmap(upl) != KERN_SUCCESS)
976 panic("ubc_upl_unmap failed");
977 bp->nb_data = NULL;
978 }
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;
982 else
983 upl_flags = 0;
984 ubc_upl_abort(upl, upl_flags);
985 goto pagelist_cleanup_done;
986 }
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);
993 else {
994 if (NBPGDIRTY(bp,i))
995 upl_flags = UPL_COMMIT_SET_DIRTY;
996 else
997 upl_flags = UPL_COMMIT_CLEAR_DIRTY;
998 ubc_upl_commit_range(upl,
999 i*PAGE_SIZE, PAGE_SIZE,
1000 upl_flags |
1001 UPL_COMMIT_INACTIVATE |
1002 UPL_COMMIT_FREE_ON_EMPTY);
1003 }
1004 }
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;
1010 off_t start, end;
1011 start = trunc_page_64(VTONFS(vp)->n_size) + PAGE_SIZE_64;
1012 end = trunc_page_64(NBOFF(bp) + biosize);
1013 if (end > start) {
1014 if (!(rv = ubc_sync_range(vp, start, end, UBC_INVALIDATE)))
1015 printf("nfs_buf_release(): ubc_sync_range failed!\n");
1016 }
1017 }
1018 CLR(bp->nb_flags, NB_PAGELIST);
1019 bp->nb_pagelist = NULL;
1020 }
1021
1022 lck_mtx_lock(nfs_buf_mutex);
1023
1024 wakeup_needbuffer = wakeup_buffer = wakeup_nbdwrite = 0;
1025
1026 /* Wake up any processes waiting for any buffer to become free. */
1027 if (nfsneedbuffer) {
1028 nfsneedbuffer = 0;
1029 wakeup_needbuffer = 1;
1030 }
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);
1034 wakeup_buffer = 1;
1035 }
1036
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);
1040
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;
1046 }
1047 bp->nb_vp = NULL;
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);
1053 NFSBUFCNTCHK(1);
1054 wakeup_nbdwrite = 1;
1055 }
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++;
1065 } else {
1066 TAILQ_INSERT_HEAD(&nfsbuffree, bp, nb_free);
1067 nfsbuffreecnt++;
1068 }
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);
1074 nfsbufdelwricnt++;
1075 freeup = 0;
1076 } else {
1077 /* update usage timestamp */
1078 microuptime(&now);
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++;
1086 } else {
1087 TAILQ_INSERT_TAIL(&nfsbuffree, bp, nb_free);
1088 nfsbuffreecnt++;
1089 }
1090 }
1091
1092 NFSBUFCNTCHK(1);
1093
1094 /* Unlock the buffer. */
1095 CLR(bp->nb_flags, (NB_ASYNC | NB_NOCACHE | NB_STABLE | NB_IOD));
1096 CLR(bp->nb_lflags, NBL_BUSY);
1097
1098 FSDBG_BOT(548, bp, NBOFF(bp), bp->nb_flags, bp->nb_data);
1099
1100 lck_mtx_unlock(nfs_buf_mutex);
1101
1102 if (wakeup_needbuffer)
1103 wakeup(&nfsneedbuffer);
1104 if (wakeup_buffer)
1105 wakeup(bp);
1106 if (wakeup_nbdwrite)
1107 wakeup(&nfs_nbdwrite);
1108 if (freeup)
1109 NFS_BUF_FREEUP();
1110 }
1111
1112 /*
1113 * Wait for operations on the buffer to complete.
1114 * When they do, extract and return the I/O's error value.
1115 */
1116 int
1117 nfs_buf_iowait(struct nfsbuf *bp)
1118 {
1119 FSDBG_TOP(549, bp, NBOFF(bp), bp->nb_flags, bp->nb_error);
1120
1121 lck_mtx_lock(nfs_buf_mutex);
1122
1123 while (!ISSET(bp->nb_flags, NB_DONE))
1124 msleep(bp, nfs_buf_mutex, PRIBIO + 1, "nfs_buf_iowait", 0);
1125
1126 lck_mtx_unlock(nfs_buf_mutex);
1127
1128 FSDBG_BOT(549, bp, NBOFF(bp), bp->nb_flags, bp->nb_error);
1129
1130 /* check for interruption of I/O, then errors. */
1131 if (ISSET(bp->nb_flags, NB_EINTR)) {
1132 CLR(bp->nb_flags, NB_EINTR);
1133 return (EINTR);
1134 } else if (ISSET(bp->nb_flags, NB_ERROR))
1135 return (bp->nb_error ? bp->nb_error : EIO);
1136 return (0);
1137 }
1138
1139 /*
1140 * Mark I/O complete on a buffer.
1141 */
1142 void
1143 nfs_buf_iodone(struct nfsbuf *bp)
1144 {
1145
1146 FSDBG_TOP(550, bp, NBOFF(bp), bp->nb_flags, bp->nb_error);
1147
1148 if (ISSET(bp->nb_flags, NB_DONE))
1149 panic("nfs_buf_iodone already");
1150 /*
1151 * I/O was done, so don't believe
1152 * the DIRTY state from VM anymore
1153 */
1154 CLR(bp->nb_flags, NB_WASDIRTY);
1155
1156 if (!ISSET(bp->nb_flags, NB_READ)) {
1157 CLR(bp->nb_flags, NB_WRITEINPROG);
1158 /*
1159 * vnode_writedone() takes care of waking up
1160 * any throttled write operations
1161 */
1162 vnode_writedone(bp->nb_vp);
1163 }
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);
1172 wakeup(bp);
1173 }
1174
1175 FSDBG_BOT(550, bp, NBOFF(bp), bp->nb_flags, bp->nb_error);
1176 }
1177
1178 void
1179 nfs_buf_write_delayed(struct nfsbuf *bp, proc_t p)
1180 {
1181 vnode_t vp = bp->nb_vp;
1182
1183 FSDBG_TOP(551, bp, NBOFF(bp), bp->nb_flags, 0);
1184 FSDBG(551, bp, bp->nb_dirtyoff, bp->nb_dirtyend, bp->nb_dirty);
1185
1186 /*
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,
1191 */
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);
1197 NFSBUFCNTCHK(0);
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);
1204 }
1205
1206 /*
1207 * If the vnode has "too many" write operations in progress
1208 * wait for them to finish the IO
1209 */
1210 (void)vnode_waitforwrites(vp, VNODE_ASYNC_THROTTLE, 0, 0, "nfs_buf_write_delayed");
1211
1212 /*
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
1216 */
1217 if (nfs_nbdwrite < 0)
1218 panic("nfs_buf_write_delayed: Negative nfs_nbdwrite");
1219
1220 if (nfs_nbdwrite > ((nfsbufcnt/4)*3)) {
1221 /* issue async write */
1222 SET(bp->nb_flags, NB_ASYNC);
1223 nfs_buf_write(bp);
1224 FSDBG_BOT(551, bp, NBOFF(bp), bp->nb_flags, bp->nb_error);
1225 return;
1226 }
1227
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);
1232 return;
1233 }
1234
1235 /*
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.
1239 */
1240 void
1241 nfs_buf_check_write_verifier(struct nfsnode *np, struct nfsbuf *bp)
1242 {
1243 struct nfsmount *nmp;
1244
1245 if (!ISSET(bp->nb_flags, NB_NEEDCOMMIT))
1246 return;
1247
1248 nmp = VFSTONFS(vnode_mount(NFSTOV(np)));
1249 if (!nmp || (bp->nb_verf == nmp->nm_verf))
1250 return;
1251
1252 /* write verifier changed, clear commit flag */
1253 bp->nb_flags &= ~NB_NEEDCOMMIT;
1254 np->n_needcommitcnt--;
1255 CHECK_NEEDCOMMITCNT(np);
1256 }
1257
1258 /*
1259 * add a reference to a buffer so it doesn't disappear while being used
1260 * (must be called with nfs_buf_mutex held)
1261 */
1262 void
1263 nfs_buf_refget(struct nfsbuf *bp)
1264 {
1265 bp->nb_refs++;
1266 }
1267 /*
1268 * release a reference on a buffer
1269 * (must be called with nfs_buf_mutex held)
1270 */
1271 void
1272 nfs_buf_refrele(struct nfsbuf *bp)
1273 {
1274 bp->nb_refs--;
1275 }
1276
1277 /*
1278 * mark a particular buffer as BUSY
1279 * (must be called with nfs_buf_mutex held)
1280 */
1281 errno_t
1282 nfs_buf_acquire(struct nfsbuf *bp, int flags, int slpflag, int slptimeo)
1283 {
1284 errno_t error;
1285 struct timespec ts;
1286
1287 if (ISSET(bp->nb_lflags, NBL_BUSY)) {
1288 /*
1289 * since the mutex_lock may block, the buffer
1290 * may become BUSY, so we need to recheck for
1291 * a NOWAIT request
1292 */
1293 if (flags & NBAC_NOWAIT)
1294 return (EBUSY);
1295 SET(bp->nb_lflags, NBL_WANTED);
1296
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;
1300
1301 error = msleep(bp, nfs_buf_mutex, slpflag | (PRIBIO + 1),
1302 "nfs_buf_acquire", &ts);
1303 if (error)
1304 return (error);
1305 return (EAGAIN);
1306 }
1307 if (flags & NBAC_REMOVE)
1308 nfs_buf_remfree(bp);
1309 SET(bp->nb_lflags, NBL_BUSY);
1310
1311 return (0);
1312 }
1313
1314 /*
1315 * simply drop the BUSY status of a buffer
1316 * (must be called with nfs_buf_mutex held)
1317 */
1318 void
1319 nfs_buf_drop(struct nfsbuf *bp)
1320 {
1321 int need_wakeup = 0;
1322
1323 if (!ISSET(bp->nb_lflags, NBL_BUSY))
1324 panic("nfs_buf_drop: buffer not busy!");
1325 if (ISSET(bp->nb_lflags, NBL_WANTED)) {
1326 /*
1327 * delay the actual wakeup until after we
1328 * clear NBL_BUSY and we've dropped nfs_buf_mutex
1329 */
1330 need_wakeup = 1;
1331 }
1332 /* Unlock the buffer. */
1333 CLR(bp->nb_lflags, (NBL_BUSY | NBL_WANTED));
1334
1335 if (need_wakeup)
1336 wakeup(bp);
1337 }
1338
1339 /*
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)
1343 */
1344 int
1345 nfs_buf_iterprepare(struct nfsnode *np, struct nfsbuflists *iterheadp, int flags)
1346 {
1347 struct nfsbuflists *listheadp;
1348
1349 if (flags & NBI_DIRTY)
1350 listheadp = &np->n_dirtyblkhd;
1351 else
1352 listheadp = &np->n_cleanblkhd;
1353
1354 if ((flags & NBI_NOWAIT) && (np->n_bufiterflags & NBI_ITER)) {
1355 LIST_INIT(iterheadp);
1356 return(EWOULDBLOCK);
1357 }
1358
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);
1362 }
1363 if (LIST_EMPTY(listheadp)) {
1364 LIST_INIT(iterheadp);
1365 return(EINVAL);
1366 }
1367 np->n_bufiterflags |= NBI_ITER;
1368
1369 iterheadp->lh_first = listheadp->lh_first;
1370 listheadp->lh_first->nb_vnbufs.le_prev = &iterheadp->lh_first;
1371 LIST_INIT(listheadp);
1372
1373 return(0);
1374 }
1375
1376 /*
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)
1380 */
1381 void
1382 nfs_buf_itercomplete(struct nfsnode *np, struct nfsbuflists *iterheadp, int flags)
1383 {
1384 struct nfsbuflists * listheadp;
1385 struct nfsbuf *bp;
1386
1387 if (flags & NBI_DIRTY)
1388 listheadp = &np->n_dirtyblkhd;
1389 else
1390 listheadp = &np->n_cleanblkhd;
1391
1392 while (!LIST_EMPTY(iterheadp)) {
1393 bp = LIST_FIRST(iterheadp);
1394 LIST_REMOVE(bp, nb_vnbufs);
1395 LIST_INSERT_HEAD(listheadp, bp, nb_vnbufs);
1396 }
1397
1398 np->n_bufiterflags &= ~NBI_ITER;
1399 if (np->n_bufiterflags & NBI_ITERWANT) {
1400 np->n_bufiterflags &= ~NBI_ITERWANT;
1401 wakeup(&np->n_bufiterflags);
1402 }
1403 }
1404
1405
1406 /*
1407 * Vnode op for read using bio
1408 * Any similarity to readip() is purely coincidental
1409 */
1410 int
1411 nfs_bioread(
1412 vnode_t vp,
1413 struct uio *uio,
1414 __unused int ioflag,
1415 kauth_cred_t cred,
1416 proc_t p)
1417 {
1418 struct nfsnode *np = VTONFS(vp);
1419 int biosize;
1420 off_t diff;
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;
1425 int bufsize;
1426 int nra, error = 0, n = 0, on = 0;
1427 caddr_t dp;
1428 struct dirent *direntp = NULL;
1429 enum vtype vtype;
1430 int nocachereadahead = 0;
1431
1432 FSDBG_TOP(514, vp, uio->uio_offset, uio_uio_resid(uio), ioflag);
1433
1434 #if DIAGNOSTIC
1435 if (uio->uio_rw != UIO_READ)
1436 panic("nfs_read mode");
1437 #endif
1438 if (uio_uio_resid(uio) == 0) {
1439 FSDBG_BOT(514, vp, 0xd1e0001, 0, 0);
1440 return (0);
1441 }
1442 if (uio->uio_offset < 0) {
1443 FSDBG_BOT(514, vp, 0xd1e0002, 0, EINVAL);
1444 return (EINVAL);
1445 }
1446
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);
1451 /*
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.
1455 * For nfs:
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
1461 * attributes.
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.
1466 */
1467 if (np->n_flag & NNEEDINVALIDATE) {
1468 np->n_flag &= ~NNEEDINVALIDATE;
1469 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
1470 }
1471 if (np->n_flag & NMODIFIED) {
1472 if (vtype != VREG) {
1473 if (vtype != VDIR)
1474 panic("nfs: bioread, not dir");
1475 nfs_invaldir(vp);
1476 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1477 if (error) {
1478 FSDBG_BOT(514, vp, 0xd1e0003, 0, error);
1479 return (error);
1480 }
1481 }
1482 NATTRINVALIDATE(np);
1483 error = nfs_getattr(vp, &nvattr, cred, p);
1484 if (error) {
1485 FSDBG_BOT(514, vp, 0xd1e0004, 0, error);
1486 return (error);
1487 }
1488 if (vtype == VDIR) {
1489 /* if directory changed, purge any name cache entries */
1490 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
1491 cache_purge(vp);
1492 np->n_ncmtime = nvattr.nva_mtime;
1493 }
1494 np->n_mtime = nvattr.nva_mtime;
1495 } else {
1496 error = nfs_getattr(vp, &nvattr, cred, p);
1497 if (error) {
1498 FSDBG_BOT(514, vp, 0xd1e0005, 0, error);
1499 return (error);
1500 }
1501 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, !=)) {
1502 if (vtype == VDIR) {
1503 nfs_invaldir(vp);
1504 /* purge name cache entries */
1505 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
1506 cache_purge(vp);
1507 }
1508 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1509 if (error) {
1510 FSDBG_BOT(514, vp, 0xd1e0006, 0, error);
1511 return (error);
1512 }
1513 if (vtype == VDIR)
1514 np->n_ncmtime = nvattr.nva_mtime;
1515 np->n_mtime = nvattr.nva_mtime;
1516 }
1517 }
1518
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);
1523 if (error) {
1524 FSDBG_BOT(514, vp, 0xd1e000a, 0, error);
1525 return (error);
1526 }
1527 }
1528 np->n_flag |= NNOCACHE;
1529 }
1530 } else if (np->n_flag & NNOCACHE) {
1531 np->n_flag &= ~NNOCACHE;
1532 }
1533
1534 do {
1535 if (np->n_flag & NNOCACHE) {
1536 switch (vtype) {
1537 case VREG:
1538 /*
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
1544 */
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);
1548 return (error);
1549 }
1550 nocachereadahead = 1;
1551 break;
1552 case VLNK:
1553 error = nfs_readlinkrpc(vp, uio, cred, p);
1554 FSDBG_BOT(514, vp, uio->uio_offset, uio_uio_resid(uio), error);
1555 return (error);
1556 case VDIR:
1557 break;
1558 default:
1559 printf(" NFSNOCACHE: type %x unexpected\n", vtype);
1560 };
1561 }
1562 switch (vtype) {
1563 case VREG:
1564 lbn = uio->uio_offset / biosize;
1565
1566 /*
1567 * Copy directly from any cached pages without grabbing the bufs.
1568 *
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.
1572 */
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)
1581 io_resid = diff;
1582 if (io_resid > 0) {
1583 error = cluster_copy_ubc_data(vp, uio, &io_resid, 0);
1584 if (error) {
1585 FSDBG_BOT(514, vp, uio->uio_offset, 0xcacefeed, error);
1586 return (error);
1587 }
1588 }
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);
1593 }
1594 }
1595
1596 lbn = uio->uio_offset / biosize;
1597 on = uio->uio_offset % biosize;
1598
1599 /*
1600 * Start the read ahead(s), as required.
1601 */
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... */
1608 continue;
1609 }
1610 lastrabn = rabn;
1611 if ((off_t)rabn * biosize >= (off_t)np->n_size)
1612 break;
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 */
1616 break;
1617 /* check if block exists and is valid. */
1618 error = nfs_buf_get(vp, rabn, biosize, p, NBLK_READ|NBLK_NOWAIT, &rabp);
1619 if (error) {
1620 FSDBG_BOT(514, vp, 0xd1e000b, 1, error);
1621 return (error);
1622 }
1623 if (!rabp)
1624 continue;
1625 if (nfs_buf_upl_valid_range(rabp, 0, rabp->nb_bufsize)) {
1626 nfs_buf_release(rabp, 1);
1627 continue;
1628 }
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);
1635 }
1636 } else
1637 nfs_buf_release(rabp, 1);
1638 }
1639 }
1640
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);
1643 return (0);
1644 }
1645
1646 OSAddAtomic(1, (SInt32*)&nfsstats.biocache_reads);
1647
1648 /*
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,
1652 * as required.
1653 */
1654 again:
1655 bufsize = biosize;
1656 // LP64todo - fix this!
1657 n = min((unsigned)(bufsize - on), uio_uio_resid(uio));
1658 diff = np->n_size - uio->uio_offset;
1659 if (diff < n)
1660 n = diff;
1661
1662 error = nfs_buf_get(vp, lbn, bufsize, p, NBLK_READ, &bp);
1663 if (error) {
1664 FSDBG_BOT(514, vp, 0xd1e000c, 0, EINTR);
1665 return (EINTR);
1666 }
1667
1668 /* if any pages are valid... */
1669 if (bp->nb_valid) {
1670 /* ...check for any invalid pages in the read range */
1671 int pg, firstpg, lastpg, dirtypg;
1672 dirtypg = firstpg = lastpg = -1;
1673 pg = on/PAGE_SIZE;
1674 while (pg <= (on + n - 1)/PAGE_SIZE) {
1675 if (!NBPGVALID(bp,pg)) {
1676 if (firstpg < 0)
1677 firstpg = pg;
1678 lastpg = pg;
1679 } else if (firstpg >= 0 && dirtypg < 0 && NBPGDIRTY(bp,pg))
1680 dirtypg = pg;
1681 pg++;
1682 }
1683
1684 /* if there are no invalid pages, we're all set */
1685 if (firstpg < 0) {
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);
1692 }
1693 goto buffer_ready;
1694 }
1695
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;
1705 }
1706 error = nfs_buf_write(bp);
1707 if (error) {
1708 FSDBG_BOT(514, vp, 0xd1e000d, 0, error);
1709 return (error);
1710 }
1711 goto again;
1712 }
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 */
1717 bp->nb_valid = 0;
1718 } else {
1719 /* read the page range in */
1720 uio_t auio;
1721 char uio_buf[ UIO_SIZEOF(1) ];
1722
1723 NFS_BUF_MAP(bp);
1724 auio = uio_createwithbuffer(1, (NBOFF(bp) + firstpg * PAGE_SIZE_64),
1725 UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf));
1726 if (!auio) {
1727 error = ENOMEM;
1728 } else {
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);
1732 }
1733 if (error) {
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);
1738 return (error);
1739 }
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));
1748 }
1749 /* mark the pages (successfully read) as valid */
1750 for (pg=firstpg; pg <= lastpg; pg++)
1751 NBPGVALID_SET(bp,pg);
1752 }
1753 }
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);
1759 if (error) {
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);
1764 return (error);
1765 }
1766 }
1767 buffer_ready:
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);
1771 if (diff < n)
1772 n = diff;
1773 }
1774 if (n > 0)
1775 NFS_BUF_MAP(bp);
1776 break;
1777 case VLNK:
1778 OSAddAtomic(1, (SInt32*)&nfsstats.biocache_readlinks);
1779 error = nfs_buf_get(vp, 0, NFS_MAXPATHLEN, p, NBLK_READ, &bp);
1780 if (error) {
1781 FSDBG_BOT(514, vp, 0xd1e0010, 0, error);
1782 return (error);
1783 }
1784 if (!ISSET(bp->nb_flags, NB_CACHE)) {
1785 SET(bp->nb_flags, NB_READ);
1786 error = nfs_doio(bp, cred, p);
1787 if (error) {
1788 SET(bp->nb_flags, NB_ERROR);
1789 nfs_buf_release(bp, 1);
1790 FSDBG_BOT(514, vp, 0xd1e0011, 0, error);
1791 return (error);
1792 }
1793 }
1794 // LP64todo - fix this!
1795 n = min(uio_uio_resid(uio), bp->nb_validend);
1796 on = 0;
1797 break;
1798 case VDIR:
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);
1802 return (0);
1803 }
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);
1807 if (error) {
1808 FSDBG_BOT(514, vp, 0xd1e0012, 0, error);
1809 return (error);
1810 }
1811 if (!ISSET(bp->nb_flags, NB_CACHE)) {
1812 SET(bp->nb_flags, NB_READ);
1813 error = nfs_doio(bp, cred, p);
1814 if (error) {
1815 nfs_buf_release(bp, 1);
1816 }
1817 while (error == NFSERR_BAD_COOKIE) {
1818 nfs_invaldir(vp);
1819 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
1820 /*
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
1824 * offset cookies.
1825 */
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);
1830 return (0);
1831 }
1832 error = nfs_buf_get(vp, tlbn, NFS_DIRBLKSIZ, p, NBLK_READ, &bp);
1833 if (error) {
1834 FSDBG_BOT(514, vp, 0xd1e0013, 0, error);
1835 return (error);
1836 }
1837 if (!ISSET(bp->nb_flags, NB_CACHE)) {
1838 SET(bp->nb_flags, NB_READ);
1839 error = nfs_doio(bp, cred, p);
1840 /*
1841 * no error + NB_INVAL == directory EOF,
1842 * use the block.
1843 */
1844 if (error == 0 && (bp->nb_flags & NB_INVAL))
1845 break;
1846 }
1847 /*
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.
1852 */
1853 if (error || tlbn < lbn)
1854 nfs_buf_release(bp, 1);
1855 }
1856 }
1857 /*
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,
1860 * we give up.
1861 */
1862 if (error) {
1863 FSDBG_BOT(514, vp, 0xd1e0014, 0, error);
1864 return (error);
1865 }
1866 }
1867
1868 /*
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.)
1872 */
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);
1878 if (error) {
1879 FSDBG_BOT(514, vp, 0xd1e0015, 0, error);
1880 return (error);
1881 }
1882 if (rabp) {
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);
1889 }
1890 } else {
1891 nfs_buf_release(rabp, 1);
1892 }
1893 }
1894 }
1895 /*
1896 * Make sure we use a signed variant of min() since
1897 * the second term may be negative.
1898 */
1899 // LP64todo - fix this!
1900 n = lmin(uio_uio_resid(uio), bp->nb_validend - on);
1901 /*
1902 * We keep track of the directory eof in
1903 * np->n_direofoffset and chop it off as an
1904 * extra step right here.
1905 */
1906 if (np->n_direofoffset &&
1907 n > np->n_direofoffset - uio->uio_offset)
1908 n = np->n_direofoffset - uio->uio_offset;
1909 /*
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.
1913 *
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.
1916 */
1917 if (n > 0) {
1918 dp = bp->nb_data + on;
1919 while (dp < (bp->nb_data + on + n)) {
1920 direntp = (struct dirent *)dp;
1921 dp += direntp->d_reclen;
1922 }
1923 if (dp > (bp->nb_data + on + n))
1924 n = (dp - direntp->d_reclen) - (bp->nb_data + on);
1925 }
1926 break;
1927 default:
1928 printf("nfs_bioread: type %x unexpected\n", vtype);
1929 FSDBG_BOT(514, vp, 0xd1e0016, 0, EINVAL);
1930 return (EINVAL);
1931 };
1932
1933 if (n > 0) {
1934 error = uiomove(bp->nb_data + on, (int)n, uio);
1935 }
1936 switch (vtype) {
1937 case VREG:
1938 if (np->n_flag & NNOCACHE)
1939 SET(bp->nb_flags, NB_NOCACHE);
1940 break;
1941 case VLNK:
1942 n = 0;
1943 break;
1944 case VDIR:
1945 break;
1946 default:
1947 break;
1948 }
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);
1952 return (error);
1953 }
1954
1955
1956 /*
1957 * Vnode op for write using bio
1958 */
1959 int
1960 nfs_write(ap)
1961 struct vnop_write_args /* {
1962 struct vnodeop_desc *a_desc;
1963 vnode_t a_vp;
1964 struct uio *a_uio;
1965 int a_ioflag;
1966 vfs_context_t a_context;
1967 } */ *ap;
1968 {
1969 struct uio *uio = ap->a_uio;
1970 vnode_t vp = ap->a_vp;
1971 struct nfsnode *np = VTONFS(vp);
1972 proc_t p;
1973 kauth_cred_t cred;
1974 int ioflag = ap->a_ioflag;
1975 struct nfsbuf *bp;
1976 struct nfs_vattr nvattr;
1977 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1978 daddr64_t lbn;
1979 int biosize, bufsize;
1980 int n, on, error = 0;
1981 off_t boff, start, end, cureof;
1982 struct iovec_32 iov;
1983 struct uio auio;
1984
1985 FSDBG_TOP(515, vp, uio->uio_offset, uio_uio_resid(uio), ioflag);
1986
1987 #if DIAGNOSTIC
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");
1992 #endif
1993
1994 p = vfs_context_proc(ap->a_context);
1995 cred = vfs_context_ucred(ap->a_context);
1996
1997 if (vnode_vtype(vp) != VREG)
1998 return (EIO);
1999
2000 np->n_flag |= NWRBUSY;
2001
2002 if (np->n_flag & NNEEDINVALIDATE) {
2003 np->n_flag &= ~NNEEDINVALIDATE;
2004 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
2005 }
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);
2010 }
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);
2018 if (error) {
2019 np->n_flag &= ~NWRBUSY;
2020 FSDBG_BOT(515, vp, uio->uio_offset, 0x10bad01, error);
2021 return (error);
2022 }
2023 }
2024 if (ioflag & IO_APPEND) {
2025 NATTRINVALIDATE(np);
2026 error = nfs_getattr(vp, &nvattr, cred, p);
2027 if (error) {
2028 np->n_flag &= ~NWRBUSY;
2029 FSDBG_BOT(515, vp, uio->uio_offset, 0x10bad02, error);
2030 return (error);
2031 }
2032 uio->uio_offset = np->n_size;
2033 }
2034 }
2035 if (uio->uio_offset < 0) {
2036 np->n_flag &= ~NWRBUSY;
2037 FSDBG_BOT(515, vp, uio->uio_offset, 0xbad0ff, EINVAL);
2038 return (EINVAL);
2039 }
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);
2043 return (0);
2044 }
2045
2046 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
2047
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);
2052 if (error) {
2053 np->n_flag &= ~NWRBUSY;
2054 FSDBG_BOT(515, vp, 0, 0, error);
2055 return (error);
2056 }
2057 }
2058 np->n_flag |= NNOCACHE;
2059 }
2060 } else if (np->n_flag & NNOCACHE) {
2061 np->n_flag &= ~NNOCACHE;
2062 }
2063
2064 do {
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));
2070 again:
2071 bufsize = biosize;
2072 /*
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.
2077 */
2078 error = nfs_buf_get(vp, lbn, bufsize, p, NBLK_WRITE, &bp);
2079 if (error) {
2080 np->n_flag &= ~NWRBUSY;
2081 FSDBG_BOT(515, vp, uio->uio_offset, uio_uio_resid(uio), error);
2082 return (error);
2083 }
2084 /* map the block because we know we're going to write to it */
2085 NFS_BUF_MAP(bp);
2086
2087 if (np->n_flag & NNOCACHE)
2088 SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
2089
2090 if (bp->nb_wcred == NOCRED) {
2091 kauth_cred_ref(cred);
2092 bp->nb_wcred = cred;
2093 }
2094
2095 /*
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.
2098 *
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.)
2107 */
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);
2116 if (error) {
2117 np->n_flag &= ~NWRBUSY;
2118 FSDBG_BOT(515, vp, uio->uio_offset, uio_uio_resid(uio), error);
2119 return (error);
2120 }
2121 goto again;
2122 }
2123 } else if (bp->nb_dirty) {
2124 int firstpg, lastpg;
2125 u_int32_t pagemask;
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);
2138 if (error) {
2139 np->n_flag &= ~NWRBUSY;
2140 FSDBG_BOT(515, vp, uio->uio_offset, uio_uio_resid(uio), error);
2141 return (error);
2142 }
2143 goto again;
2144 }
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;
2152 /* clip to EOF */
2153 if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size)
2154 bp->nb_dirtyend = np->n_size - NBOFF(bp);
2155 } else
2156 bp->nb_dirtyend = on+n;
2157 }
2158 }
2159
2160 /*
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.
2165 */
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;
2172
2173 FSDBG(515, 0xb1ffa000, uio->uio_offset + n, eofoff, neweofoff);
2174
2175 if (eofoff && (eofbn < lbn)) {
2176 error = nfs_buf_get(vp, eofbn, biosize, p, NBLK_WRITE|NBLK_ONLYVALID, &eofbp);
2177 if (error) {
2178 np->n_flag &= ~NWRBUSY;
2179 FSDBG_BOT(515, vp, uio->uio_offset, uio_uio_resid(uio), error);
2180 return (error);
2181 }
2182 }
2183
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;
2189 u_int32_t pagemask;
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) {
2196 int i;
2197 for (i=bp->nb_validend/PAGE_SIZE; i <= (on - 1)/PAGE_SIZE; i++)
2198 NBPGVALID_SET(bp, i);
2199 NFS_BUF_MAP(bp);
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);
2203 }
2204 /* zerofill any trailing data in the last page */
2205 if (neweofoff) {
2206 NFS_BUF_MAP(bp);
2207 FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f);
2208 bzero((char *)bp->nb_data + neweofoff,
2209 PAGE_SIZE - (neweofoff & PAGE_MASK));
2210 }
2211 }
2212 }
2213 np->n_flag |= NMODIFIED;
2214 np->n_size = uio->uio_offset + n;
2215 ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */
2216 if (eofbp) {
2217 /*
2218 * We may need to zero any previously invalid data
2219 * after the old EOF in the previous EOF buffer.
2220 *
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
2223 * currently valid).
2224 * For pages after the old last page, zero them and
2225 * mark them as valid.
2226 */
2227 char *d;
2228 int i;
2229 if (np->n_flag & NNOCACHE)
2230 SET(eofbp->nb_flags, (NB_NOCACHE|NB_STABLE));
2231 NFS_BUF_MAP(eofbp);
2232 FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e);
2233 d = eofbp->nb_data;
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);
2240 }
2241 if (bp->nb_validend == eofoff)
2242 bp->nb_validend += PAGE_SIZE - poff;
2243 eofoff += PAGE_SIZE - poff;
2244 i++;
2245 }
2246 nfs_buf_release(eofbp, 1);
2247 }
2248 }
2249 /*
2250 * If dirtyend exceeds file size, chop it down. This should
2251 * not occur unless there is a race.
2252 */
2253 if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size)
2254 bp->nb_dirtyend = np->n_size - NBOFF(bp);
2255 /*
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.
2258 *
2259 * Writes that are smaller than a block are delayed if they
2260 * don't extend to the end of the block.
2261 *
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.
2268 *
2269 * Notes:
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
2274 * page cache.
2275 */
2276 if (!ISSET(bp->nb_flags, NB_CACHE) && n < biosize) {
2277 int firstpg, lastpg, dirtypg;
2278 int firstpgoff, lastpgoff;
2279 start = end = -1;
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;
2288 }
2289 if (lastpgoff && !NBPGVALID(bp,lastpg)) {
2290 /* need to read end of last page */
2291 if (start < 0)
2292 start = (lastpg * PAGE_SIZE) + lastpgoff;
2293 end = (lastpg + 1) * PAGE_SIZE;
2294 }
2295 if (end > start) {
2296 /* need to read the data in range: start...end-1 */
2297
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))
2303 break;
2304
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)))
2311 break;
2312 }
2313
2314 NFS_BUF_MAP(bp);
2315 /* setup uio for read(s) */
2316 boff = NBOFF(bp);
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;
2321 #else
2322 auio.uio_segflg = UIO_SYSSPACE32;
2323 #endif
2324 auio.uio_rw = UIO_READ;
2325
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);
2334 if (error) {
2335 bp->nb_error = error;
2336 SET(bp->nb_flags, NB_ERROR);
2337 printf("nfs_write: readrpc %d", error);
2338 }
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));
2343 }
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 */
2355 start = on+n;
2356 }
2357
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))
2364 break;
2365 }
2366
2367 if (((boff+start) >= cureof) || ((start >= on) && ((boff + on + n) >= cureof))) {
2368 /*
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.
2374 */
2375 FSDBG(516, bp, start, end - start, 0xd00dee00);
2376 bzero(bp->nb_data + start, end - start);
2377 } else {
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);
2384 if (error) {
2385 bp->nb_error = error;
2386 SET(bp->nb_flags, NB_ERROR);
2387 printf("nfs_write: readrpc %d", error);
2388 }
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));
2393 }
2394 }
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 */
2409 }
2410 }
2411
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);
2417 return (error);
2418 }
2419
2420 np->n_flag |= NMODIFIED;
2421
2422 NFS_BUF_MAP(bp);
2423 error = uiomove((char *)bp->nb_data + on, n, uio);
2424 if (error) {
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);
2429 return (error);
2430 }
2431
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);
2436 /*
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.
2444 */
2445 }
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);
2449 } else {
2450 bp->nb_dirtyoff = on;
2451 bp->nb_dirtyend = on + n;
2452 }
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;
2457 } else {
2458 bp->nb_validoff = min(bp->nb_validoff, bp->nb_dirtyoff);
2459 bp->nb_validend = max(bp->nb_validend, bp->nb_dirtyend);
2460 }
2461 if (!ISSET(bp->nb_flags, NB_CACHE))
2462 nfs_buf_normalize_valid_range(np, bp);
2463
2464 /*
2465 * Since this block is being modified, it must be written
2466 * again and not just committed.
2467 */
2468 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
2469 np->n_needcommitcnt--;
2470 CHECK_NEEDCOMMITCNT(np);
2471 }
2472 CLR(bp->nb_flags, NB_NEEDCOMMIT);
2473
2474 if (ioflag & IO_SYNC) {
2475 bp->nb_proc = p;
2476 error = nfs_buf_write(bp);
2477 if (error) {
2478 np->n_flag &= ~NWRBUSY;
2479 FSDBG_BOT(515, vp, uio->uio_offset,
2480 uio_uio_resid(uio), error);
2481 return (error);
2482 }
2483 } else if (((n + on) == biosize) || (np->n_flag & NNOCACHE)) {
2484 bp->nb_proc = NULL;
2485 SET(bp->nb_flags, NB_ASYNC);
2486 nfs_buf_write(bp);
2487 } else
2488 nfs_buf_write_delayed(bp, p);
2489
2490 if (np->n_needcommitcnt > (nfsbufcnt/16))
2491 nfs_flushcommits(vp, p, 1);
2492
2493 } while (uio_uio_resid(uio) > 0 && n > 0);
2494
2495 np->n_flag &= ~NWRBUSY;
2496 FSDBG_BOT(515, vp, uio->uio_offset, uio_uio_resid(uio), 0);
2497 return (0);
2498 }
2499
2500 /*
2501 * Flush out and invalidate all buffers associated with a vnode.
2502 * Called with the underlying object locked.
2503 */
2504 static int
2505 nfs_vinvalbuf_internal(
2506 vnode_t vp,
2507 int flags,
2508 kauth_cred_t cred,
2509 proc_t p,
2510 int slpflag,
2511 int slptimeo)
2512 {
2513 struct nfsbuf *bp;
2514 struct nfsbuflists blist;
2515 int list, error = 0;
2516 struct nfsnode *np = VTONFS(vp);
2517
2518 if (flags & V_SAVE) {
2519 if ((error = nfs_flush(vp, MNT_WAIT, cred, p,
2520 (flags & V_IGNORE_WRITEERR))))
2521 return (error);
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));
2525 }
2526
2527 lck_mtx_lock(nfs_buf_mutex);
2528 for (;;) {
2529 list = NBI_CLEAN;
2530 if (nfs_buf_iterprepare(np, &blist, list)) {
2531 list = NBI_DIRTY;
2532 if (nfs_buf_iterprepare(np, &blist, list))
2533 break;
2534 }
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);
2539 else
2540 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
2541 nfs_buf_refget(bp);
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);
2549 return (error);
2550 }
2551 }
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 */
2559 int mustwrite = 0;
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 */
2568 } else if (error)
2569 printf("nfs_vinvalbuf: upl setup failed %d\n", error);
2570 bp->nb_valid = bp->nb_dirty = 0;
2571 }
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;
2578 mustwrite++;
2579 }
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");
2584 mustwrite = 0;
2585 }
2586 if (mustwrite) {
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;
2598 }
2599 error = nfs_buf_write(bp);
2600 // Note: bp has been released
2601 if (error) {
2602 FSDBG(554, bp, 0xd00dee, 0xbad, error);
2603 np->n_error = error;
2604 np->n_flag |= NWRITEERR;
2605 /*
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)
2610 */
2611 NATTRINVALIDATE(np);
2612 error = 0;
2613 }
2614 lck_mtx_lock(nfs_buf_mutex);
2615 continue;
2616 }
2617 }
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);
2622 }
2623 nfs_buf_itercomplete(np, &blist, list);
2624 }
2625 lck_mtx_unlock(nfs_buf_mutex);
2626 NFS_BUF_FREEUP();
2627 if (NVALIDBUFS(np))
2628 panic("nfs_vinvalbuf: flush failed");
2629 return (0);
2630 }
2631
2632
2633 /*
2634 * Flush and invalidate all dirty buffers. If another process is already
2635 * doing the flush, just wait for completion.
2636 */
2637 int
2638 nfs_vinvalbuf(
2639 vnode_t vp,
2640 int flags,
2641 kauth_cred_t cred,
2642 proc_t p,
2643 int intrflg)
2644 {
2645 struct nfsnode *np = VTONFS(vp);
2646 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
2647 int error = 0, slpflag, slptimeo;
2648 off_t size;
2649
2650 FSDBG_TOP(554, vp, flags, intrflg, 0);
2651
2652 if (nmp && ((nmp->nm_flag & NFSMNT_INT) == 0))
2653 intrflg = 0;
2654 if (intrflg) {
2655 slpflag = PCATCH;
2656 slptimeo = 2 * hz;
2657 } else {
2658 slpflag = 0;
2659 slptimeo = 0;
2660 }
2661 /*
2662 * First wait for any other process doing a flush to complete.
2663 */
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);
2671 return (error);
2672 }
2673 }
2674
2675 /*
2676 * Now, flush as required.
2677 */
2678 np->n_flag |= NFLUSHINPROG;
2679 error = nfs_vinvalbuf_internal(vp, flags, cred, p, slpflag, 0);
2680 while (error) {
2681 FSDBG(554, vp, 0, 0, error);
2682 error = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
2683 if (error) {
2684 np->n_flag &= ~NFLUSHINPROG;
2685 if (np->n_flag & NFLUSHWANT) {
2686 np->n_flag &= ~NFLUSHWANT;
2687 wakeup((caddr_t)&np->n_flag);
2688 }
2689 FSDBG_BOT(554, vp, flags, intrflg, error);
2690 return (error);
2691 }
2692 error = nfs_vinvalbuf_internal(vp, flags, cred, p, 0, slptimeo);
2693 }
2694 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
2695 if (np->n_flag & NFLUSHWANT) {
2696 np->n_flag &= ~NFLUSHWANT;
2697 wakeup((caddr_t)&np->n_flag);
2698 }
2699 /*
2700 * get the pages out of vm also
2701 */
2702 if (UBCINFOEXISTS(vp) && (size = ubc_getsize(vp))) {
2703 int rv = ubc_sync_range(vp, 0, size, UBC_PUSHALL | UBC_INVALIDATE);
2704 if (!rv)
2705 panic("nfs_vinvalbuf(): ubc_sync_range failed!");
2706 }
2707
2708 FSDBG_BOT(554, vp, flags, intrflg, 0);
2709 return (0);
2710 }
2711
2712 /*
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.
2716 */
2717 int
2718 nfs_asyncio(bp, cred)
2719 struct nfsbuf *bp;
2720 kauth_cred_t cred;
2721 {
2722 struct nfsmount *nmp;
2723 int i;
2724 int gotiod;
2725 int slpflag = 0;
2726 int slptimeo = 0;
2727 int error, error2;
2728 void *wakeme = NULL;
2729 struct timespec ts;
2730
2731 if (nfs_numasync == 0)
2732 return (EIO);
2733
2734 FSDBG_TOP(552, bp, bp ? NBOFF(bp) : 0, bp ? bp->nb_flags : 0, 0);
2735
2736 nmp = ((bp != NULL) ? VFSTONFS(vnode_mount(bp->nb_vp)) : NULL);
2737 again:
2738 if (nmp && nmp->nm_flag & NFSMNT_INT)
2739 slpflag = PCATCH;
2740 gotiod = FALSE;
2741
2742 lck_mtx_lock(nfs_iod_mutex);
2743
2744 /* no nfsbuf means tell nfsiod to process delwri list */
2745 if (!bp)
2746 nfs_ioddelwri = 1;
2747
2748 /*
2749 * Find a free iod to process this request.
2750 */
2751 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
2752 if (nfs_iodwant[i]) {
2753 /*
2754 * Found one, so wake it up and tell it which
2755 * mount to process.
2756 */
2757 nfs_iodwant[i] = NULL;
2758 nfs_iodmount[i] = nmp;
2759 if (nmp)
2760 nmp->nm_bufqiods++;
2761 wakeme = &nfs_iodwant[i];
2762 gotiod = TRUE;
2763 break;
2764 }
2765
2766 /* if we're just poking the delwri list, we're done */
2767 if (!bp) {
2768 lck_mtx_unlock(nfs_iod_mutex);
2769 if (wakeme)
2770 wakeup(wakeme);
2771 FSDBG_BOT(552, bp, 0x10101010, wakeme, 0);
2772 return (0);
2773 }
2774
2775 /*
2776 * If none are free, we may already have an iod working on this mount
2777 * point. If so, it will process our request.
2778 */
2779 if (!gotiod) {
2780 if (nmp->nm_bufqiods > 0) {
2781 gotiod = TRUE;
2782 }
2783 }
2784
2785 /*
2786 * If we have an iod which can process the request, then queue
2787 * the buffer.
2788 */
2789 FSDBG(552, bp, gotiod, i, nmp->nm_bufqiods);
2790 if (gotiod) {
2791 /*
2792 * Ensure that the queue never grows too large.
2793 */
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. */
2800 goto out;
2801 }
2802 FSDBG(552, bp, nmp->nm_bufqlen, 2*nfs_numasync, -1);
2803 nmp->nm_bufqwant = TRUE;
2804
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;
2808
2809 error = msleep(&nmp->nm_bufq, nfs_iod_mutex, slpflag | PRIBIO,
2810 "nfsaio", &ts);
2811 if (error) {
2812 error2 = nfs_sigintr(nmp, NULL, bp->nb_proc);
2813 if (error2) {
2814 lck_mtx_unlock(nfs_iod_mutex);
2815 FSDBG_BOT(552, bp, NBOFF(bp), bp->nb_flags, error2);
2816 return (error2);
2817 }
2818 if (slpflag == PCATCH) {
2819 slpflag = 0;
2820 slptimeo = 2 * hz;
2821 }
2822 }
2823 /*
2824 * We might have lost our iod while sleeping,
2825 * so check and loop if nescessary.
2826 */
2827 if (nmp->nm_bufqiods == 0) {
2828 lck_mtx_unlock(nfs_iod_mutex);
2829 goto again;
2830 }
2831 }
2832
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;
2837 }
2838 } else {
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;
2843 }
2844 }
2845
2846 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, nb_free);
2847 nmp->nm_bufqlen++;
2848 lck_mtx_unlock(nfs_iod_mutex);
2849 if (wakeme)
2850 wakeup(wakeme);
2851 FSDBG_BOT(552, bp, NBOFF(bp), bp->nb_flags, 0);
2852 return (0);
2853 }
2854
2855 out:
2856 lck_mtx_unlock(nfs_iod_mutex);
2857 /*
2858 * All the iods are busy on other mounts, so return EIO to
2859 * force the caller to process the i/o synchronously.
2860 */
2861 FSDBG_BOT(552, bp, NBOFF(bp), bp->nb_flags, EIO);
2862 return (EIO);
2863 }
2864
2865 /*
2866 * Do an I/O operation to/from a cache block. This may be called
2867 * synchronously or from an nfsiod.
2868 */
2869 int
2870 nfs_doio(struct nfsbuf *bp, kauth_cred_t cr, proc_t p)
2871 {
2872 struct uio *uiop;
2873 vnode_t vp;
2874 struct nfsnode *np;
2875 struct nfsmount *nmp;
2876 int error = 0, diff, len, iomode, invalidate = 0;
2877 struct uio uio;
2878 struct iovec_32 io;
2879 enum vtype vtype;
2880
2881 vp = bp->nb_vp;
2882 vtype = vnode_vtype(vp);
2883 np = VTONFS(vp);
2884 nmp = VFSTONFS(vnode_mount(vp));
2885 uiop = &uio;
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;
2890 #else
2891 uiop->uio_segflg = UIO_SYSSPACE32;
2892 #endif
2893
2894 /*
2895 * we've decided to perform I/O for this block,
2896 * so we couldn't possibly NB_DONE. So, clear it.
2897 */
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);
2902 }
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,
2905 bp->nb_dirtyend);
2906
2907 if (ISSET(bp->nb_flags, NB_READ)) {
2908 if (vtype == VREG)
2909 NFS_BUF_MAP(bp);
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;
2914 switch (vtype) {
2915 case VREG:
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);
2920 if (!error) {
2921 /* update valid range */
2922 bp->nb_validoff = 0;
2923 if (uio_uio_resid(uiop) != 0) {
2924 /*
2925 * If len > 0, there is a hole in the file and
2926 * no writes after the hole have been pushed to
2927 * the server yet.
2928 * Just zero fill the rest of the valid area.
2929 */
2930 // LP64todo - fix this
2931 diff = bp->nb_bufsize - uio_uio_resid(uiop);
2932 len = np->n_size - (NBOFF(bp) + diff);
2933 if (len > 0) {
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);
2939 } else
2940 bp->nb_validend = diff;
2941 } else
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);
2952 }
2953 }
2954 break;
2955 case VLNK:
2956 uiop->uio_offset = (off_t)0;
2957 OSAddAtomic(1, (SInt32*)&nfsstats.readlink_bios);
2958 error = nfs_readlinkrpc(vp, uiop, cr, p);
2959 if (!error) {
2960 bp->nb_validoff = 0;
2961 bp->nb_validend = uiop->uio_offset;
2962 }
2963 break;
2964 case VDIR:
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;
2973 }
2974 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
2975 error = nfs_readdirrpc(vp, uiop, cr, p);
2976 if (!error) {
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;
2980 }
2981 break;
2982 default:
2983 printf("nfs_doio: type %x unexpected\n", vtype);
2984 break;
2985 };
2986 if (error) {
2987 SET(bp->nb_flags, NB_ERROR);
2988 bp->nb_error = error;
2989 }
2990
2991 } else {
2992 /* we're doing a write */
2993 int doff, dend = 0;
2994
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);
2999 if (error) {
3000 printf("nfs_doio: upl create failed %d\n", error);
3001 SET(bp->nb_flags, NB_ERROR);
3002 bp->nb_error = EIO;
3003 return (EIO);
3004 }
3005 nfs_buf_upl_check(bp);
3006 }
3007 }
3008
3009 if (ISSET(bp->nb_flags, NB_WASDIRTY)) {
3010 FSDBG(256, bp, NBOFF(bp), bp->nb_dirty, 0xd00dee);
3011 /*
3012 * There are pages marked dirty that need to be written out.
3013 *
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.
3018 *
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.
3024 */
3025 }
3026
3027 /*
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.
3031 */
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);
3040 if (!error) {
3041 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3042 CLR(bp->nb_flags, NB_NEEDCOMMIT);
3043 np->n_needcommitcnt--;
3044 CHECK_NEEDCOMMITCNT(np);
3045 }
3046 }
3047
3048 if (!error && bp->nb_dirtyend > 0) {
3049 /* there's a dirty range that needs to be written out */
3050 u_int32_t pagemask;
3051 int firstpg, lastpg;
3052
3053 if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size)
3054 bp->nb_dirtyend = np->n_size - NBOFF(bp);
3055
3056 NFS_BUF_MAP(bp);
3057
3058 doff = bp->nb_dirtyoff;
3059 dend = bp->nb_dirtyend;
3060
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))
3067 doff -= 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))
3074 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);
3083
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;
3091 else
3092 iomode = NFSV3WRITE_FILESYNC;
3093
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;
3100
3101 OSAddAtomic(1, (SInt32*)&nfsstats.write_bios);
3102
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 */
3106 if (!error)
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;
3116 } else {
3117 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
3118 np->n_needcommitcnt--;
3119 CHECK_NEEDCOMMITCNT(np);
3120 }
3121 CLR(bp->nb_flags, NB_NEEDCOMMIT);
3122 }
3123 CLR(bp->nb_flags, NB_WRITEINPROG);
3124 /*
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.
3129 *
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.
3135 */
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);
3141 NFSBUFCNTCHK(0);
3142 }
3143 FSDBG(261, bp->nb_validoff, bp->nb_validend,
3144 bp->nb_bufsize, 0);
3145 /*
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.
3149 */
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);
3157 } else {
3158 SET(bp->nb_flags, NB_EINTR);
3159 }
3160 } else {
3161 /* either there's an error or we don't need to commit */
3162 if (error) {
3163 SET(bp->nb_flags, NB_ERROR);
3164 bp->nb_error = np->n_error = error;
3165 np->n_flag |= NWRITEERR;
3166 /*
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)
3172 *
3173 * But we can't call vinvalbuf while holding
3174 * this buffer busy. Set a flag to do it after
3175 * releasing the buffer.
3176 *
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.
3183 */
3184 if (ISSET(bp->nb_flags, NB_ASYNC) &&
3185 !ISSET(bp->nb_flags, NB_IOD)) {
3186 invalidate = 1;
3187 } else {
3188 /* invalidate later */
3189 np->n_flag |= NNEEDINVALIDATE;
3190 }
3191 NATTRINVALIDATE(np);
3192 }
3193 /* clear the dirty range */
3194 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3195 }
3196 }
3197
3198 if (!error && bp->nb_dirty) {
3199 /* there are pages marked dirty that need to be written out */
3200 int pg, count, npages, off;
3201
3202 OSAddAtomic(1, (SInt32*)&nfsstats.write_bios);
3203
3204 NFS_BUF_MAP(bp);
3205
3206 /*
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.
3216 */
3217 iomode = NFSV3WRITE_FILESYNC;
3218 uiop->uio_rw = UIO_WRITE;
3219
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))
3224 continue;
3225 count = 1;
3226 while (((pg+count) < npages) && NBPGDIRTY(bp,pg+count))
3227 count++;
3228 /* write count pages starting with page pg */
3229 off = pg * PAGE_SIZE;
3230 len = count * PAGE_SIZE;
3231
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;
3235 if (len > 0) {
3236 io.iov_len = len;
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);
3241 if (error)
3242 break;
3243 }
3244 /* clear dirty bits */
3245 while (count--) {
3246 bp->nb_dirty &= ~(1 << pg);
3247 /* leave pg on last page */
3248 if (count) pg++;
3249 }
3250 }
3251 if (!error) {
3252 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
3253 np->n_needcommitcnt--;
3254 CHECK_NEEDCOMMITCNT(np);
3255 }
3256 CLR(bp->nb_flags, NB_NEEDCOMMIT);
3257 }
3258 CLR(bp->nb_flags, NB_WRITEINPROG);
3259 FSDBG_BOT(256, bp->nb_validoff, bp->nb_validend, bp->nb_bufsize,
3260 np->n_size);
3261 }
3262
3263 if (error) {
3264 SET(bp->nb_flags, NB_ERROR);
3265 bp->nb_error = error;
3266 }
3267 }
3268
3269 FSDBG_BOT(256, bp->nb_validoff, bp->nb_validend, bp->nb_bufsize, error);
3270
3271 nfs_buf_iodone(bp);
3272
3273 if (invalidate) {
3274 /*
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)
3280 *
3281 * But we couldn't call vinvalbuf while holding
3282 * the buffer busy. So we call vinvalbuf() after
3283 * releasing the buffer.
3284 *
3285 * Note: we don't bother calling nfs_vinvalbuf() if
3286 * there's already a flush in progress.
3287 */
3288 if (!(np->n_flag & NFLUSHINPROG))
3289 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cr, p, 1);
3290 }
3291
3292 return (error);
3293 }