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