nfs_bio.c revision 1.159 1 /* $NetBSD: nfs_bio.c,v 1.159 2007/07/17 10:23:33 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.159 2007/07/17 10:23:33 yamt Exp $");
39
40 #include "opt_nfs.h"
41 #include "opt_ddb.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/proc.h>
48 #include <sys/buf.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/kernel.h>
52 #include <sys/namei.h>
53 #include <sys/dirent.h>
54 #include <sys/malloc.h>
55 #include <sys/kauth.h>
56
57 #include <uvm/uvm_extern.h>
58 #include <uvm/uvm.h>
59
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfs/nfs.h>
63 #include <nfs/nfsmount.h>
64 #include <nfs/nfsnode.h>
65 #include <nfs/nfs_var.h>
66
67 extern int nfs_numasync;
68 extern int nfs_commitsize;
69 extern struct nfsstats nfsstats;
70
71 static int nfs_doio_read __P((struct buf *, struct uio *));
72 static int nfs_doio_write __P((struct buf *, struct uio *));
73 static int nfs_doio_phys __P((struct buf *, struct uio *));
74
75 /*
76 * Vnode op for read using bio
77 * Any similarity to readip() is purely coincidental
78 */
79 int
80 nfs_bioread(vp, uio, ioflag, cred, cflag)
81 struct vnode *vp;
82 struct uio *uio;
83 int ioflag, cflag;
84 kauth_cred_t cred;
85 {
86 struct nfsnode *np = VTONFS(vp);
87 struct buf *bp = NULL, *rabp;
88 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
89 struct nfsdircache *ndp = NULL, *nndp = NULL;
90 void *baddr;
91 int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
92 int enough = 0;
93 struct dirent *dp, *pdp, *edp, *ep;
94 off_t curoff = 0;
95 int advice;
96 struct lwp *l = curlwp;
97
98 #ifdef DIAGNOSTIC
99 if (uio->uio_rw != UIO_READ)
100 panic("nfs_read mode");
101 #endif
102 if (uio->uio_resid == 0)
103 return (0);
104 if (vp->v_type != VDIR && uio->uio_offset < 0)
105 return (EINVAL);
106 #ifndef NFS_V2_ONLY
107 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
108 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
109 (void)nfs_fsinfo(nmp, vp, cred, l);
110 #endif
111 if (vp->v_type != VDIR &&
112 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
113 return (EFBIG);
114
115 /*
116 * For nfs, cache consistency can only be maintained approximately.
117 * Although RFC1094 does not specify the criteria, the following is
118 * believed to be compatible with the reference port.
119 *
120 * If the file's modify time on the server has changed since the
121 * last read rpc or you have written to the file,
122 * you may have lost data cache consistency with the
123 * server, so flush all of the file's data out of the cache.
124 * Then force a getattr rpc to ensure that you have up to date
125 * attributes.
126 * NB: This implies that cache data can be read when up to
127 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
128 * attributes this could be forced by setting n_attrstamp to 0 before
129 * the VOP_GETATTR() call.
130 */
131
132 if (vp->v_type != VLNK) {
133 error = nfs_flushstalebuf(vp, cred, l,
134 NFS_FLUSHSTALEBUF_MYWRITE);
135 if (error)
136 return error;
137 }
138
139 do {
140 /*
141 * Don't cache symlinks.
142 */
143 if ((vp->v_flag & VROOT) && vp->v_type == VLNK) {
144 return (nfs_readlinkrpc(vp, uio, cred));
145 }
146 baddr = (void *)0;
147 switch (vp->v_type) {
148 case VREG:
149 nfsstats.biocache_reads++;
150
151 advice = IO_ADV_DECODE(ioflag);
152 error = 0;
153 while (uio->uio_resid > 0) {
154 void *win;
155 int flags;
156 vsize_t bytelen;
157
158 nfs_delayedtruncate(vp);
159 if (np->n_size <= uio->uio_offset) {
160 break;
161 }
162 bytelen =
163 MIN(np->n_size - uio->uio_offset, uio->uio_resid);
164 win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
165 &bytelen, advice, UBC_READ);
166 error = uiomove(win, bytelen, uio);
167 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
168 ubc_release(win, flags);
169 if (error) {
170 /*
171 * XXXkludge
172 * the file has been truncated on the server.
173 * there isn't much we can do.
174 */
175 if (uio->uio_offset >= np->n_size) {
176 /* end of file */
177 error = 0;
178 } else {
179 break;
180 }
181 }
182 }
183 break;
184
185 case VLNK:
186 nfsstats.biocache_readlinks++;
187 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, l);
188 if (!bp)
189 return (EINTR);
190 if ((bp->b_flags & B_DONE) == 0) {
191 bp->b_flags |= B_READ;
192 error = nfs_doio(bp);
193 if (error) {
194 brelse(bp);
195 return (error);
196 }
197 }
198 n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
199 got_buf = 1;
200 on = 0;
201 break;
202 case VDIR:
203 diragain:
204 nfsstats.biocache_readdirs++;
205 ndp = nfs_searchdircache(vp, uio->uio_offset,
206 (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
207 if (!ndp) {
208 /*
209 * We've been handed a cookie that is not
210 * in the cache. If we're not translating
211 * 32 <-> 64, it may be a value that was
212 * flushed out of the cache because it grew
213 * too big. Let the server judge if it's
214 * valid or not. In the translation case,
215 * we have no way of validating this value,
216 * so punt.
217 */
218 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
219 return (EINVAL);
220 ndp = nfs_enterdircache(vp, uio->uio_offset,
221 uio->uio_offset, 0, 0);
222 }
223
224 if (NFS_EOFVALID(np) &&
225 ndp->dc_cookie == np->n_direofoffset) {
226 nfs_putdircache(np, ndp);
227 nfsstats.direofcache_hits++;
228 return (0);
229 }
230
231 bp = nfs_getcacheblk(vp, NFSDC_BLKNO(ndp), NFS_DIRBLKSIZ, l);
232 if (!bp)
233 return (EINTR);
234 if ((bp->b_flags & B_DONE) == 0) {
235 bp->b_flags |= B_READ;
236 bp->b_dcookie = ndp->dc_blkcookie;
237 error = nfs_doio(bp);
238 if (error) {
239 /*
240 * Yuck! The directory has been modified on the
241 * server. Punt and let the userland code
242 * deal with it.
243 */
244 nfs_putdircache(np, ndp);
245 brelse(bp);
246 /*
247 * nfs_request maps NFSERR_BAD_COOKIE to EINVAL.
248 */
249 if (error == EINVAL) { /* NFSERR_BAD_COOKIE */
250 nfs_invaldircache(vp, 0);
251 nfs_vinvalbuf(vp, 0, cred, l, 1);
252 }
253 return (error);
254 }
255 }
256
257 /*
258 * Just return if we hit EOF right away with this
259 * block. Always check here, because direofoffset
260 * may have been set by an nfsiod since the last
261 * check.
262 *
263 * also, empty block implies EOF.
264 */
265
266 if (bp->b_bcount == bp->b_resid ||
267 (NFS_EOFVALID(np) &&
268 ndp->dc_blkcookie == np->n_direofoffset)) {
269 KASSERT(bp->b_bcount != bp->b_resid ||
270 ndp->dc_blkcookie == bp->b_dcookie);
271 nfs_putdircache(np, ndp);
272 bp->b_flags |= B_NOCACHE;
273 brelse(bp);
274 return 0;
275 }
276
277 /*
278 * Find the entry we were looking for in the block.
279 */
280
281 en = ndp->dc_entry;
282
283 pdp = dp = (struct dirent *)bp->b_data;
284 edp = (struct dirent *)(void *)((char *)bp->b_data + bp->b_bcount -
285 bp->b_resid);
286 enn = 0;
287 while (enn < en && dp < edp) {
288 pdp = dp;
289 dp = _DIRENT_NEXT(dp);
290 enn++;
291 }
292
293 /*
294 * If the entry number was bigger than the number of
295 * entries in the block, or the cookie of the previous
296 * entry doesn't match, the directory cache is
297 * stale. Flush it and try again (i.e. go to
298 * the server).
299 */
300 if (dp >= edp || (struct dirent *)_DIRENT_NEXT(dp) > edp ||
301 (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
302 #ifdef DEBUG
303 printf("invalid cache: %p %p %p off %lx %lx\n",
304 pdp, dp, edp,
305 (unsigned long)uio->uio_offset,
306 (unsigned long)NFS_GETCOOKIE(pdp));
307 #endif
308 nfs_putdircache(np, ndp);
309 brelse(bp);
310 nfs_invaldircache(vp, 0);
311 nfs_vinvalbuf(vp, 0, cred, l, 0);
312 goto diragain;
313 }
314
315 on = (char *)dp - (char *)bp->b_data;
316
317 /*
318 * Cache all entries that may be exported to the
319 * user, as they may be thrown back at us. The
320 * NFSBIO_CACHECOOKIES flag indicates that all
321 * entries are being 'exported', so cache them all.
322 */
323
324 if (en == 0 && pdp == dp) {
325 dp = _DIRENT_NEXT(dp);
326 enn++;
327 }
328
329 if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
330 n = uio->uio_resid;
331 enough = 1;
332 } else
333 n = bp->b_bcount - bp->b_resid - on;
334
335 ep = (struct dirent *)(void *)((char *)bp->b_data + on + n);
336
337 /*
338 * Find last complete entry to copy, caching entries
339 * (if requested) as we go.
340 */
341
342 while (dp < ep && (struct dirent *)_DIRENT_NEXT(dp) <= ep) {
343 if (cflag & NFSBIO_CACHECOOKIES) {
344 nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
345 ndp->dc_blkcookie, enn, bp->b_lblkno);
346 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
347 NFS_STASHCOOKIE32(pdp,
348 nndp->dc_cookie32);
349 }
350 nfs_putdircache(np, nndp);
351 }
352 pdp = dp;
353 dp = _DIRENT_NEXT(dp);
354 enn++;
355 }
356 nfs_putdircache(np, ndp);
357
358 /*
359 * If the last requested entry was not the last in the
360 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
361 * cache the cookie of the last requested one, and
362 * set of the offset to it.
363 */
364
365 if ((on + n) < bp->b_bcount - bp->b_resid) {
366 curoff = NFS_GETCOOKIE(pdp);
367 nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
368 enn, bp->b_lblkno);
369 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
370 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
371 curoff = nndp->dc_cookie32;
372 }
373 nfs_putdircache(np, nndp);
374 } else
375 curoff = bp->b_dcookie;
376
377 /*
378 * Always cache the entry for the next block,
379 * so that readaheads can use it.
380 */
381 nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
382 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
383 if (curoff == bp->b_dcookie) {
384 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
385 curoff = nndp->dc_cookie32;
386 }
387 }
388
389 n = (char *)_DIRENT_NEXT(pdp) - ((char *)bp->b_data + on);
390
391 /*
392 * If not eof and read aheads are enabled, start one.
393 * (You need the current block first, so that you have the
394 * directory offset cookie of the next block.)
395 */
396 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
397 !NFS_EOFVALID(np)) {
398 rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
399 NFS_DIRBLKSIZ, l);
400 if (rabp) {
401 if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
402 rabp->b_dcookie = nndp->dc_cookie;
403 rabp->b_flags |= (B_READ | B_ASYNC);
404 if (nfs_asyncio(rabp)) {
405 rabp->b_flags |= B_INVAL;
406 brelse(rabp);
407 }
408 } else
409 brelse(rabp);
410 }
411 }
412 nfs_putdircache(np, nndp);
413 got_buf = 1;
414 break;
415 default:
416 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
417 break;
418 }
419
420 if (n > 0) {
421 if (!baddr)
422 baddr = bp->b_data;
423 error = uiomove((char *)baddr + on, (int)n, uio);
424 }
425 switch (vp->v_type) {
426 case VREG:
427 break;
428 case VLNK:
429 n = 0;
430 break;
431 case VDIR:
432 uio->uio_offset = curoff;
433 if (enough)
434 n = 0;
435 break;
436 default:
437 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
438 }
439 if (got_buf)
440 brelse(bp);
441 } while (error == 0 && uio->uio_resid > 0 && n > 0);
442 return (error);
443 }
444
445 /*
446 * Vnode op for write using bio
447 */
448 int
449 nfs_write(v)
450 void *v;
451 {
452 struct vop_write_args /* {
453 struct vnode *a_vp;
454 struct uio *a_uio;
455 int a_ioflag;
456 kauth_cred_t a_cred;
457 } */ *ap = v;
458 struct uio *uio = ap->a_uio;
459 struct lwp *l = curlwp;
460 struct vnode *vp = ap->a_vp;
461 struct nfsnode *np = VTONFS(vp);
462 kauth_cred_t cred = ap->a_cred;
463 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
464 voff_t oldoff, origoff;
465 vsize_t bytelen;
466 int error = 0;
467 int ioflag = ap->a_ioflag;
468 int extended = 0, wrotedata = 0;
469
470 #ifdef DIAGNOSTIC
471 if (uio->uio_rw != UIO_WRITE)
472 panic("nfs_write mode");
473 #endif
474 if (vp->v_type != VREG)
475 return (EIO);
476 if (np->n_flag & NWRITEERR) {
477 np->n_flag &= ~NWRITEERR;
478 return (np->n_error);
479 }
480 #ifndef NFS_V2_ONLY
481 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
482 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
483 (void)nfs_fsinfo(nmp, vp, cred, l);
484 #endif
485 if (ioflag & IO_APPEND) {
486 NFS_INVALIDATE_ATTRCACHE(np);
487 error = nfs_flushstalebuf(vp, cred, l,
488 NFS_FLUSHSTALEBUF_MYWRITE);
489 if (error)
490 return (error);
491 uio->uio_offset = np->n_size;
492 }
493 if (uio->uio_offset < 0)
494 return (EINVAL);
495 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
496 return (EFBIG);
497 if (uio->uio_resid == 0)
498 return (0);
499 /*
500 * Maybe this should be above the vnode op call, but so long as
501 * file servers have no limits, i don't think it matters
502 */
503 if (l && l->l_proc && uio->uio_offset + uio->uio_resid >
504 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
505 mutex_enter(&proclist_mutex);
506 psignal(l->l_proc, SIGXFSZ);
507 mutex_exit(&proclist_mutex);
508 return (EFBIG);
509 }
510
511 origoff = uio->uio_offset;
512 do {
513 bool overwrite; /* if we are overwriting whole pages */
514 u_quad_t oldsize;
515 oldoff = uio->uio_offset;
516 bytelen = uio->uio_resid;
517
518 nfsstats.biocache_writes++;
519
520 oldsize = np->n_size;
521 np->n_flag |= NMODIFIED;
522 if (np->n_size < uio->uio_offset + bytelen) {
523 np->n_size = uio->uio_offset + bytelen;
524 }
525 overwrite = false;
526 if ((uio->uio_offset & PAGE_MASK) == 0) {
527 if ((vp->v_flag & VMAPPED) == 0 &&
528 bytelen > PAGE_SIZE) {
529 bytelen = trunc_page(bytelen);
530 overwrite = true;
531 } else if ((bytelen & PAGE_MASK) == 0 &&
532 uio->uio_offset >= vp->v_size) {
533 overwrite = true;
534 }
535 }
536 if (vp->v_size < uio->uio_offset + bytelen) {
537 uvm_vnp_setwritesize(vp, uio->uio_offset + bytelen);
538 }
539 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
540 UBC_WRITE | UBC_PARTIALOK |
541 (overwrite ? UBC_FAULTBUSY : 0) |
542 (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
543 if (error) {
544 uvm_vnp_setwritesize(vp, vp->v_size);
545 if (overwrite && np->n_size != oldsize) {
546 /*
547 * backout size and free pages past eof.
548 */
549 np->n_size = oldsize;
550 simple_lock(&vp->v_interlock);
551 (void)VOP_PUTPAGES(vp, round_page(vp->v_size),
552 0, PGO_SYNCIO | PGO_FREE);
553 }
554 break;
555 }
556 wrotedata = 1;
557
558 /*
559 * update UVM's notion of the size now that we've
560 * copied the data into the vnode's pages.
561 */
562
563 if (vp->v_size < uio->uio_offset) {
564 uvm_vnp_setsize(vp, uio->uio_offset);
565 extended = 1;
566 }
567
568 if ((oldoff & ~(nmp->nm_wsize - 1)) !=
569 (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
570 simple_lock(&vp->v_interlock);
571 error = VOP_PUTPAGES(vp,
572 trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
573 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
574 ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
575 }
576 } while (uio->uio_resid > 0);
577 if (wrotedata)
578 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
579 if (error == 0 && (ioflag & IO_SYNC) != 0) {
580 simple_lock(&vp->v_interlock);
581 error = VOP_PUTPAGES(vp,
582 trunc_page(origoff & ~(nmp->nm_wsize - 1)),
583 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
584 ~(nmp->nm_wsize - 1)),
585 PGO_CLEANIT | PGO_SYNCIO);
586 }
587 return error;
588 }
589
590 /*
591 * Get an nfs cache block.
592 * Allocate a new one if the block isn't currently in the cache
593 * and return the block marked busy. If the calling process is
594 * interrupted by a signal for an interruptible mount point, return
595 * NULL.
596 */
597 struct buf *
598 nfs_getcacheblk(vp, bn, size, l)
599 struct vnode *vp;
600 daddr_t bn;
601 int size;
602 struct lwp *l;
603 {
604 struct buf *bp;
605 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
606
607 if (nmp->nm_flag & NFSMNT_INT) {
608 bp = getblk(vp, bn, size, PCATCH, 0);
609 while (bp == NULL) {
610 if (nfs_sigintr(nmp, NULL, l))
611 return (NULL);
612 bp = getblk(vp, bn, size, 0, 2 * hz);
613 }
614 } else
615 bp = getblk(vp, bn, size, 0, 0);
616 return (bp);
617 }
618
619 /*
620 * Flush and invalidate all dirty buffers. If another process is already
621 * doing the flush, just wait for completion.
622 */
623 int
624 nfs_vinvalbuf(vp, flags, cred, l, intrflg)
625 struct vnode *vp;
626 int flags;
627 kauth_cred_t cred;
628 struct lwp *l;
629 int intrflg;
630 {
631 struct nfsnode *np = VTONFS(vp);
632 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
633 int error = 0, slpflag, slptimeo;
634
635 if ((nmp->nm_flag & NFSMNT_INT) == 0)
636 intrflg = 0;
637 if (intrflg) {
638 slpflag = PCATCH;
639 slptimeo = 2 * hz;
640 } else {
641 slpflag = 0;
642 slptimeo = 0;
643 }
644 /*
645 * First wait for any other process doing a flush to complete.
646 */
647 simple_lock(&vp->v_interlock);
648 while (np->n_flag & NFLUSHINPROG) {
649 np->n_flag |= NFLUSHWANT;
650 error = ltsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
651 slptimeo, &vp->v_interlock);
652 if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
653 simple_unlock(&vp->v_interlock);
654 return EINTR;
655 }
656 }
657
658 /*
659 * Now, flush as required.
660 */
661 np->n_flag |= NFLUSHINPROG;
662 simple_unlock(&vp->v_interlock);
663 error = vinvalbuf(vp, flags, cred, l, slpflag, 0);
664 while (error) {
665 if (intrflg && nfs_sigintr(nmp, NULL, l)) {
666 error = EINTR;
667 break;
668 }
669 error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
670 }
671 simple_lock(&vp->v_interlock);
672 if (error == 0)
673 np->n_flag &= ~NMODIFIED;
674 np->n_flag &= ~NFLUSHINPROG;
675 if (np->n_flag & NFLUSHWANT) {
676 np->n_flag &= ~NFLUSHWANT;
677 wakeup(&np->n_flag);
678 }
679 simple_unlock(&vp->v_interlock);
680 return error;
681 }
682
683 /*
684 * nfs_flushstalebuf: flush cache if it's stale.
685 *
686 * => caller shouldn't own any pages or buffers which belong to the vnode.
687 */
688
689 int
690 nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
691 int flags)
692 {
693 struct nfsnode *np = VTONFS(vp);
694 struct vattr vattr;
695 int error;
696
697 if (np->n_flag & NMODIFIED) {
698 if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
699 || vp->v_type != VREG) {
700 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
701 if (error)
702 return error;
703 if (vp->v_type == VDIR) {
704 nfs_invaldircache(vp, 0);
705 }
706 } else {
707 /*
708 * XXX assuming writes are ours.
709 */
710 }
711 NFS_INVALIDATE_ATTRCACHE(np);
712 error = VOP_GETATTR(vp, &vattr, cred, l);
713 if (error)
714 return error;
715 np->n_mtime = vattr.va_mtime;
716 } else {
717 error = VOP_GETATTR(vp, &vattr, cred, l);
718 if (error)
719 return error;
720 if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
721 if (vp->v_type == VDIR) {
722 nfs_invaldircache(vp, 0);
723 }
724 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
725 if (error)
726 return error;
727 np->n_mtime = vattr.va_mtime;
728 }
729 }
730
731 return error;
732 }
733
734 /*
735 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
736 * This is mainly to avoid queueing async I/O requests when the nfsiods
737 * are all hung on a dead server.
738 */
739
740 int
741 nfs_asyncio(bp)
742 struct buf *bp;
743 {
744 int i;
745 struct nfsmount *nmp;
746 int gotiod, slptimeo = 0, error;
747 bool catch = false;
748
749 if (nfs_numasync == 0)
750 return (EIO);
751
752 nmp = VFSTONFS(bp->b_vp->v_mount);
753 again:
754 if (nmp->nm_flag & NFSMNT_INT)
755 catch = true;
756 gotiod = false;
757
758 /*
759 * Find a free iod to process this request.
760 */
761
762 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
763 struct nfs_iod *iod = &nfs_asyncdaemon[i];
764
765 mutex_enter(&iod->nid_lock);
766 if (iod->nid_want) {
767 /*
768 * Found one, so wake it up and tell it which
769 * mount to process.
770 */
771 iod->nid_want = NULL;
772 iod->nid_mount = nmp;
773 cv_signal(&iod->nid_cv);
774 mutex_enter(&nmp->nm_lock);
775 mutex_exit(&iod->nid_lock);
776 nmp->nm_bufqiods++;
777 gotiod = true;
778 break;
779 }
780 mutex_exit(&iod->nid_lock);
781 }
782
783 /*
784 * If none are free, we may already have an iod working on this mount
785 * point. If so, it will process our request.
786 */
787
788 if (!gotiod) {
789 mutex_enter(&nmp->nm_lock);
790 if (nmp->nm_bufqiods > 0)
791 gotiod = true;
792 }
793
794 KASSERT(mutex_owned(&nmp->nm_lock));
795
796 /*
797 * If we have an iod which can process the request, then queue
798 * the buffer. However, even if we have an iod, do not initiate
799 * queue cleaning if curproc is the pageout daemon. if the NFS mount
800 * is via local loopback, we may put curproc (pagedaemon) to sleep
801 * waiting for the writes to complete. But the server (ourself)
802 * may block the write, waiting for its (ie., our) pagedaemon
803 * to produce clean pages to handle the write: deadlock.
804 * XXX: start non-loopback mounts straight away? If "lots free",
805 * let pagedaemon start loopback writes anyway?
806 */
807 if (gotiod) {
808
809 /*
810 * Ensure that the queue never grows too large.
811 */
812 if (curlwp == uvm.pagedaemon_lwp) {
813 /* Enque for later, to avoid free-page deadlock */
814 (void) 0;
815 } else while (nmp->nm_bufqlen >= 2*nfs_numasync) {
816 if (catch) {
817 error = cv_timedwait_sig(&nmp->nm_aiocv,
818 &nmp->nm_lock, slptimeo);
819 } else {
820 error = cv_timedwait(&nmp->nm_aiocv,
821 &nmp->nm_lock, slptimeo);
822 }
823 if (error) {
824 if (nfs_sigintr(nmp, NULL, curlwp)) {
825 mutex_exit(&nmp->nm_lock);
826 return (EINTR);
827 }
828 if (catch) {
829 catch = false;
830 slptimeo = 2 * hz;
831 }
832 }
833
834 /*
835 * We might have lost our iod while sleeping,
836 * so check and loop if necessary.
837 */
838
839 if (nmp->nm_bufqiods == 0) {
840 mutex_exit(&nmp->nm_lock);
841 goto again;
842 }
843 }
844 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
845 nmp->nm_bufqlen++;
846 mutex_exit(&nmp->nm_lock);
847 return (0);
848 }
849 mutex_exit(&nmp->nm_lock);
850
851 /*
852 * All the iods are busy on other mounts, so return EIO to
853 * force the caller to process the i/o synchronously.
854 */
855
856 return (EIO);
857 }
858
859 /*
860 * nfs_doio for read.
861 */
862 static int
863 nfs_doio_read(bp, uiop)
864 struct buf *bp;
865 struct uio *uiop;
866 {
867 struct vnode *vp = bp->b_vp;
868 struct nfsnode *np = VTONFS(vp);
869 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
870 int error = 0;
871
872 uiop->uio_rw = UIO_READ;
873 switch (vp->v_type) {
874 case VREG:
875 nfsstats.read_bios++;
876 error = nfs_readrpc(vp, uiop);
877 if (!error && uiop->uio_resid) {
878 int diff, len;
879
880 /*
881 * If uio_resid > 0, there is a hole in the file and
882 * no writes after the hole have been pushed to
883 * the server yet or the file has been truncated
884 * on the server.
885 * Just zero fill the rest of the valid area.
886 */
887
888 KASSERT(vp->v_size >=
889 uiop->uio_offset + uiop->uio_resid);
890 diff = bp->b_bcount - uiop->uio_resid;
891 len = uiop->uio_resid;
892 memset((char *)bp->b_data + diff, 0, len);
893 uiop->uio_resid = 0;
894 }
895 #if 0
896 if (uiop->uio_lwp && (vp->v_flag & VTEXT) &&
897 timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
898 killproc(uiop->uio_lwp->l_proc, "process text file was modified");
899 #if 0 /* XXX NJWLWP */
900 uiop->uio_lwp->l_proc->p_holdcnt++;
901 #endif
902 }
903 #endif
904 break;
905 case VLNK:
906 KASSERT(uiop->uio_offset == (off_t)0);
907 nfsstats.readlink_bios++;
908 error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
909 break;
910 case VDIR:
911 nfsstats.readdir_bios++;
912 uiop->uio_offset = bp->b_dcookie;
913 #ifndef NFS_V2_ONLY
914 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
915 error = nfs_readdirplusrpc(vp, uiop,
916 curlwp->l_cred);
917 /*
918 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
919 */
920 if (error == ENOTSUP)
921 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
922 }
923 #else
924 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
925 #endif
926 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
927 error = nfs_readdirrpc(vp, uiop,
928 curlwp->l_cred);
929 if (!error) {
930 bp->b_dcookie = uiop->uio_offset;
931 }
932 break;
933 default:
934 printf("nfs_doio: type %x unexpected\n", vp->v_type);
935 break;
936 }
937 if (error) {
938 bp->b_flags |= B_ERROR;
939 bp->b_error = error;
940 }
941 return error;
942 }
943
944 /*
945 * nfs_doio for write.
946 */
947 static int
948 nfs_doio_write(bp, uiop)
949 struct buf *bp;
950 struct uio *uiop;
951 {
952 struct vnode *vp = bp->b_vp;
953 struct nfsnode *np = VTONFS(vp);
954 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
955 int iomode;
956 bool stalewriteverf = false;
957 int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
958 struct vm_page *pgs[npages];
959 #ifndef NFS_V2_ONLY
960 bool needcommit = true; /* need only COMMIT RPC */
961 #else
962 bool needcommit = false; /* need only COMMIT RPC */
963 #endif
964 bool pageprotected;
965 struct uvm_object *uobj = &vp->v_uobj;
966 int error;
967 off_t off, cnt;
968
969 if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
970 iomode = NFSV3WRITE_UNSTABLE;
971 } else {
972 iomode = NFSV3WRITE_FILESYNC;
973 }
974
975 #ifndef NFS_V2_ONLY
976 again:
977 #endif
978 rw_enter(&nmp->nm_writeverflock, RW_READER);
979
980 for (i = 0; i < npages; i++) {
981 pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
982 if (pgs[i]->uobject == uobj &&
983 pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
984 KASSERT(pgs[i]->flags & PG_BUSY);
985 /*
986 * this page belongs to our object.
987 */
988 simple_lock(&uobj->vmobjlock);
989 /*
990 * write out the page stably if it's about to
991 * be released because we can't resend it
992 * on the server crash.
993 *
994 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
995 * changed until unbusy the page.
996 */
997 if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
998 iomode = NFSV3WRITE_FILESYNC;
999 /*
1000 * if we met a page which hasn't been sent yet,
1001 * we need do WRITE RPC.
1002 */
1003 if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
1004 needcommit = false;
1005 simple_unlock(&uobj->vmobjlock);
1006 } else {
1007 iomode = NFSV3WRITE_FILESYNC;
1008 needcommit = false;
1009 }
1010 }
1011 if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
1012 simple_lock(&uobj->vmobjlock);
1013 for (i = 0; i < npages; i++) {
1014 pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1015 pmap_page_protect(pgs[i], VM_PROT_READ);
1016 }
1017 simple_unlock(&uobj->vmobjlock);
1018 pageprotected = true; /* pages can't be modified during i/o. */
1019 } else
1020 pageprotected = false;
1021
1022 /*
1023 * Send the data to the server if necessary,
1024 * otherwise just send a commit rpc.
1025 */
1026 #ifndef NFS_V2_ONLY
1027 if (needcommit) {
1028
1029 /*
1030 * If the buffer is in the range that we already committed,
1031 * there's nothing to do.
1032 *
1033 * If it's in the range that we need to commit, push the
1034 * whole range at once, otherwise only push the buffer.
1035 * In both these cases, acquire the commit lock to avoid
1036 * other processes modifying the range.
1037 */
1038
1039 off = uiop->uio_offset;
1040 cnt = bp->b_bcount;
1041 mutex_enter(&np->n_commitlock);
1042 if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1043 bool pushedrange;
1044 if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1045 pushedrange = true;
1046 off = np->n_pushlo;
1047 cnt = np->n_pushhi - np->n_pushlo;
1048 } else {
1049 pushedrange = false;
1050 }
1051 error = nfs_commit(vp, off, cnt, curlwp);
1052 if (error == 0) {
1053 if (pushedrange) {
1054 nfs_merge_commit_ranges(vp);
1055 } else {
1056 nfs_add_committed_range(vp, off, cnt);
1057 }
1058 }
1059 } else {
1060 error = 0;
1061 }
1062 mutex_exit(&np->n_commitlock);
1063 rw_exit(&nmp->nm_writeverflock);
1064 if (!error) {
1065 /*
1066 * pages are now on stable storage.
1067 */
1068 uiop->uio_resid = 0;
1069 simple_lock(&uobj->vmobjlock);
1070 for (i = 0; i < npages; i++) {
1071 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1072 }
1073 simple_unlock(&uobj->vmobjlock);
1074 return 0;
1075 } else if (error == NFSERR_STALEWRITEVERF) {
1076 nfs_clearcommit(vp->v_mount);
1077 goto again;
1078 }
1079 if (error) {
1080 bp->b_flags |= B_ERROR;
1081 bp->b_error = np->n_error = error;
1082 np->n_flag |= NWRITEERR;
1083 }
1084 return error;
1085 }
1086 #endif
1087 off = uiop->uio_offset;
1088 cnt = bp->b_bcount;
1089 uiop->uio_rw = UIO_WRITE;
1090 nfsstats.write_bios++;
1091 error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1092 #ifndef NFS_V2_ONLY
1093 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1094 /*
1095 * we need to commit pages later.
1096 */
1097 mutex_enter(&np->n_commitlock);
1098 nfs_add_tobecommitted_range(vp, off, cnt);
1099 /*
1100 * if there can be too many uncommitted pages, commit them now.
1101 */
1102 if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1103 off = np->n_pushlo;
1104 cnt = nfs_commitsize >> 1;
1105 error = nfs_commit(vp, off, cnt, curlwp);
1106 if (!error) {
1107 nfs_add_committed_range(vp, off, cnt);
1108 nfs_del_tobecommitted_range(vp, off, cnt);
1109 }
1110 if (error == NFSERR_STALEWRITEVERF) {
1111 stalewriteverf = true;
1112 error = 0; /* it isn't a real error */
1113 }
1114 } else {
1115 /*
1116 * re-dirty pages so that they will be passed
1117 * to us later again.
1118 */
1119 simple_lock(&uobj->vmobjlock);
1120 for (i = 0; i < npages; i++) {
1121 pgs[i]->flags &= ~PG_CLEAN;
1122 }
1123 simple_unlock(&uobj->vmobjlock);
1124 }
1125 mutex_exit(&np->n_commitlock);
1126 } else
1127 #endif
1128 if (!error) {
1129 /*
1130 * pages are now on stable storage.
1131 */
1132 mutex_enter(&np->n_commitlock);
1133 nfs_del_committed_range(vp, off, cnt);
1134 mutex_exit(&np->n_commitlock);
1135 simple_lock(&uobj->vmobjlock);
1136 for (i = 0; i < npages; i++) {
1137 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1138 }
1139 simple_unlock(&uobj->vmobjlock);
1140 } else {
1141 /*
1142 * we got an error.
1143 */
1144 bp->b_flags |= B_ERROR;
1145 bp->b_error = np->n_error = error;
1146 np->n_flag |= NWRITEERR;
1147 }
1148
1149 rw_exit(&nmp->nm_writeverflock);
1150
1151 if (stalewriteverf) {
1152 nfs_clearcommit(vp->v_mount);
1153 }
1154 return error;
1155 }
1156
1157 /*
1158 * nfs_doio for B_PHYS.
1159 */
1160 static int
1161 nfs_doio_phys(bp, uiop)
1162 struct buf *bp;
1163 struct uio *uiop;
1164 {
1165 struct vnode *vp = bp->b_vp;
1166 int error;
1167
1168 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1169 if (bp->b_flags & B_READ) {
1170 uiop->uio_rw = UIO_READ;
1171 nfsstats.read_physios++;
1172 error = nfs_readrpc(vp, uiop);
1173 } else {
1174 int iomode = NFSV3WRITE_DATASYNC;
1175 bool stalewriteverf;
1176 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1177
1178 uiop->uio_rw = UIO_WRITE;
1179 nfsstats.write_physios++;
1180 rw_enter(&nmp->nm_writeverflock, RW_READER);
1181 error = nfs_writerpc(vp, uiop, &iomode, false, &stalewriteverf);
1182 rw_exit(&nmp->nm_writeverflock);
1183 if (stalewriteverf) {
1184 nfs_clearcommit(bp->b_vp->v_mount);
1185 }
1186 }
1187 if (error) {
1188 bp->b_flags |= B_ERROR;
1189 bp->b_error = error;
1190 }
1191 return error;
1192 }
1193
1194 /*
1195 * Do an I/O operation to/from a cache block. This may be called
1196 * synchronously or from an nfsiod.
1197 */
1198 int
1199 nfs_doio(bp)
1200 struct buf *bp;
1201 {
1202 int error;
1203 struct uio uio;
1204 struct uio *uiop = &uio;
1205 struct iovec io;
1206 UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
1207
1208 uiop->uio_iov = &io;
1209 uiop->uio_iovcnt = 1;
1210 uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
1211 UIO_SETUP_SYSSPACE(uiop);
1212 io.iov_base = bp->b_data;
1213 io.iov_len = uiop->uio_resid = bp->b_bcount;
1214
1215 /*
1216 * Historically, paging was done with physio, but no more...
1217 */
1218 if (bp->b_flags & B_PHYS) {
1219 /*
1220 * ...though reading /dev/drum still gets us here.
1221 */
1222 error = nfs_doio_phys(bp, uiop);
1223 } else if (bp->b_flags & B_READ) {
1224 error = nfs_doio_read(bp, uiop);
1225 } else {
1226 error = nfs_doio_write(bp, uiop);
1227 }
1228 bp->b_resid = uiop->uio_resid;
1229 biodone(bp);
1230 return (error);
1231 }
1232
1233 /*
1234 * Vnode op for VM getpages.
1235 */
1236
1237 int
1238 nfs_getpages(v)
1239 void *v;
1240 {
1241 struct vop_getpages_args /* {
1242 struct vnode *a_vp;
1243 voff_t a_offset;
1244 struct vm_page **a_m;
1245 int *a_count;
1246 int a_centeridx;
1247 vm_prot_t a_access_type;
1248 int a_advice;
1249 int a_flags;
1250 } */ *ap = v;
1251
1252 struct vnode *vp = ap->a_vp;
1253 struct uvm_object *uobj = &vp->v_uobj;
1254 struct nfsnode *np = VTONFS(vp);
1255 const int npages = *ap->a_count;
1256 struct vm_page *pg, **pgs, *opgs[npages];
1257 off_t origoffset, len;
1258 int i, error;
1259 bool v3 = NFS_ISV3(vp);
1260 bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1261 bool locked = (ap->a_flags & PGO_LOCKED) != 0;
1262
1263 /*
1264 * call the genfs code to get the pages. `pgs' may be NULL
1265 * when doing read-ahead.
1266 */
1267
1268 pgs = ap->a_m;
1269 if (write && locked && v3) {
1270 KASSERT(pgs != NULL);
1271 #ifdef DEBUG
1272
1273 /*
1274 * If PGO_LOCKED is set, real pages shouldn't exists
1275 * in the array.
1276 */
1277
1278 for (i = 0; i < npages; i++)
1279 KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1280 #endif
1281 memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1282 }
1283 error = genfs_getpages(v);
1284 if (error) {
1285 return (error);
1286 }
1287
1288 /*
1289 * for read faults where the nfs node is not yet marked NMODIFIED,
1290 * set PG_RDONLY on the pages so that we come back here if someone
1291 * tries to modify later via the mapping that will be entered for
1292 * this fault.
1293 */
1294
1295 if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1296 if (!locked) {
1297 simple_lock(&uobj->vmobjlock);
1298 }
1299 for (i = 0; i < npages; i++) {
1300 pg = pgs[i];
1301 if (pg == NULL || pg == PGO_DONTCARE) {
1302 continue;
1303 }
1304 pg->flags |= PG_RDONLY;
1305 }
1306 if (!locked) {
1307 simple_unlock(&uobj->vmobjlock);
1308 }
1309 }
1310 if (!write) {
1311 return (0);
1312 }
1313
1314 /*
1315 * this is a write fault, update the commit info.
1316 */
1317
1318 origoffset = ap->a_offset;
1319 len = npages << PAGE_SHIFT;
1320
1321 if (v3) {
1322 if (!locked) {
1323 mutex_enter(&np->n_commitlock);
1324 } else {
1325 if (!mutex_tryenter(&np->n_commitlock)) {
1326
1327 /*
1328 * Since PGO_LOCKED is set, we need to unbusy
1329 * all pages fetched by genfs_getpages() above,
1330 * tell the caller that there are no pages
1331 * available and put back original pgs array.
1332 */
1333
1334 uvm_lock_pageq();
1335 uvm_page_unbusy(pgs, npages);
1336 uvm_unlock_pageq();
1337 *ap->a_count = 0;
1338 memcpy(pgs, opgs,
1339 npages * sizeof(struct vm_pages *));
1340 return EBUSY;
1341 }
1342 }
1343 nfs_del_committed_range(vp, origoffset, len);
1344 nfs_del_tobecommitted_range(vp, origoffset, len);
1345 }
1346 np->n_flag |= NMODIFIED;
1347 if (!locked) {
1348 simple_lock(&uobj->vmobjlock);
1349 }
1350 for (i = 0; i < npages; i++) {
1351 pg = pgs[i];
1352 if (pg == NULL || pg == PGO_DONTCARE) {
1353 continue;
1354 }
1355 pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1356 }
1357 if (!locked) {
1358 simple_unlock(&uobj->vmobjlock);
1359 }
1360 if (v3) {
1361 mutex_exit(&np->n_commitlock);
1362 }
1363 return (0);
1364 }
1365