nfs_bio.c revision 1.39 1 /* $NetBSD: nfs_bio.c,v 1.39 1997/10/23 14:12:14 fvdl 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
39 */
40
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/resourcevar.h>
45 #include <sys/signalvar.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/vnode.h>
49 #include <sys/trace.h>
50 #include <sys/mount.h>
51 #include <sys/kernel.h>
52 #include <sys/namei.h>
53 #include <sys/dirent.h>
54
55 #include <vm/vm.h>
56
57 #include <nfs/rpcv2.h>
58 #include <nfs/nfsproto.h>
59 #include <nfs/nfs.h>
60 #include <nfs/nfsmount.h>
61 #include <nfs/nqnfs.h>
62 #include <nfs/nfsnode.h>
63 #include <nfs/nfs_var.h>
64
65 extern int nfs_numasync;
66 extern struct nfsstats nfsstats;
67
68 /*
69 * Vnode op for read using bio
70 * Any similarity to readip() is purely coincidental
71 */
72 int
73 nfs_bioread(vp, uio, ioflag, cred, cflag)
74 register struct vnode *vp;
75 register struct uio *uio;
76 int ioflag, cflag;
77 struct ucred *cred;
78 {
79 register struct nfsnode *np = VTONFS(vp);
80 register int biosize, diff;
81 struct buf *bp = NULL, *rabp;
82 struct vattr vattr;
83 struct proc *p;
84 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
85 struct nfsdircache *ndp = NULL, *nndp = NULL;
86 daddr_t lbn, bn, rabn;
87 caddr_t baddr, ep, edp;
88 int got_buf = 0, nra, error = 0, n = 0, on = 0, not_readin, en, enn;
89 int enough = 0;
90 struct dirent *dp, *pdp;
91 off_t curoff = 0;
92
93 #ifdef DIAGNOSTIC
94 if (uio->uio_rw != UIO_READ)
95 panic("nfs_read mode");
96 #endif
97 if (uio->uio_resid == 0)
98 return (0);
99 if (vp->v_type != VDIR && uio->uio_offset < 0)
100 return (EINVAL);
101 p = uio->uio_procp;
102 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
103 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
104 (void)nfs_fsinfo(nmp, vp, cred, p);
105 if (vp->v_type != VDIR &&
106 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
107 return (EFBIG);
108 biosize = nmp->nm_rsize;
109 /*
110 * For nfs, cache consistency can only be maintained approximately.
111 * Although RFC1094 does not specify the criteria, the following is
112 * believed to be compatible with the reference port.
113 * For nqnfs, full cache consistency is maintained within the loop.
114 * For nfs:
115 * If the file's modify time on the server has changed since the
116 * last read rpc or you have written to the file,
117 * you may have lost data cache consistency with the
118 * server, so flush all of the file's data out of the cache.
119 * Then force a getattr rpc to ensure that you have up to date
120 * attributes.
121 * NB: This implies that cache data can be read when up to
122 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
123 * attributes this could be forced by setting n_attrstamp to 0 before
124 * the VOP_GETATTR() call.
125 */
126 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
127 if (np->n_flag & NMODIFIED) {
128 if (vp->v_type != VREG) {
129 if (vp->v_type != VDIR)
130 panic("nfs: bioread, not dir");
131 nfs_invaldircache(vp, 0);
132 np->n_direofoffset = 0;
133 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
134 if (error)
135 return (error);
136 }
137 np->n_attrstamp = 0;
138 error = VOP_GETATTR(vp, &vattr, cred, p);
139 if (error)
140 return (error);
141 np->n_mtime = vattr.va_mtime.tv_sec;
142 } else {
143 error = VOP_GETATTR(vp, &vattr, cred, p);
144 if (error)
145 return (error);
146 if (np->n_mtime != vattr.va_mtime.tv_sec) {
147 if (vp->v_type == VDIR) {
148 nfs_invaldircache(vp, 0);
149 np->n_direofoffset = 0;
150 }
151 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
152 if (error)
153 return (error);
154 np->n_mtime = vattr.va_mtime.tv_sec;
155 }
156 }
157 }
158 do {
159
160 /*
161 * Get a valid lease. If cached data is stale, flush it.
162 */
163 if (nmp->nm_flag & NFSMNT_NQNFS) {
164 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
165 do {
166 error = nqnfs_getlease(vp, ND_READ, cred, p);
167 } while (error == NQNFS_EXPIRED);
168 if (error)
169 return (error);
170 if (np->n_lrev != np->n_brev ||
171 (np->n_flag & NQNFSNONCACHE) ||
172 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
173 if (vp->v_type == VDIR) {
174 nfs_invaldircache(vp, 0);
175 np->n_direofoffset = 0;
176 }
177 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
178 if (error)
179 return (error);
180 np->n_brev = np->n_lrev;
181 }
182 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
183 nfs_invaldircache(vp, 0);
184 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
185 np->n_direofoffset = 0;
186 if (error)
187 return (error);
188 }
189 }
190 /*
191 * Don't cache symlinks.
192 */
193 if (np->n_flag & NQNFSNONCACHE
194 || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
195 switch (vp->v_type) {
196 case VREG:
197 return (nfs_readrpc(vp, uio, cred));
198 case VLNK:
199 return (nfs_readlinkrpc(vp, uio, cred));
200 case VDIR:
201 break;
202 default:
203 printf(" NQNFSNONCACHE: type %x unexpected\n",
204 vp->v_type);
205 };
206 }
207 baddr = (caddr_t)0;
208 switch (vp->v_type) {
209 case VREG:
210 nfsstats.biocache_reads++;
211 lbn = uio->uio_offset / biosize;
212 on = uio->uio_offset & (biosize - 1);
213 bn = lbn * (biosize / DEV_BSIZE);
214 not_readin = 1;
215
216 /*
217 * Start the read ahead(s), as required.
218 */
219 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
220 lbn - 1 == vp->v_lastr) {
221 for (nra = 0; nra < nmp->nm_readahead &&
222 (lbn + 1 + nra) * biosize < np->n_size; nra++) {
223 rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);
224 if (!incore(vp, rabn)) {
225 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
226 if (!rabp)
227 return (EINTR);
228 if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {
229 rabp->b_flags |= (B_READ | B_ASYNC);
230 if (nfs_asyncio(rabp, cred)) {
231 rabp->b_flags |= B_INVAL;
232 brelse(rabp);
233 }
234 } else
235 brelse(rabp);
236 }
237 }
238 }
239
240 /*
241 * If the block is in the cache and has the required data
242 * in a valid region, just copy it out.
243 * Otherwise, get the block and write back/read in,
244 * as required.
245 */
246 if ((bp = incore(vp, bn)) &&
247 (bp->b_flags & (B_BUSY | B_WRITEINPROG)) ==
248 (B_BUSY | B_WRITEINPROG))
249 got_buf = 0;
250 else {
251 again:
252 bp = nfs_getcacheblk(vp, bn, biosize, p);
253 if (!bp)
254 return (EINTR);
255 got_buf = 1;
256 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
257 bp->b_flags |= B_READ;
258 not_readin = 0;
259 error = nfs_doio(bp, cred, p);
260 if (error) {
261 brelse(bp);
262 return (error);
263 }
264 }
265 }
266 n = min((unsigned)(biosize - on), uio->uio_resid);
267 diff = np->n_size - uio->uio_offset;
268 if (diff < n)
269 n = diff;
270 if (not_readin && n > 0) {
271 if (on < bp->b_validoff || (on + n) > bp->b_validend) {
272 if (!got_buf) {
273 bp = nfs_getcacheblk(vp, bn, biosize, p);
274 if (!bp)
275 return (EINTR);
276 got_buf = 1;
277 }
278 bp->b_flags |= B_INVAFTERWRITE;
279 if (bp->b_dirtyend > 0) {
280 if ((bp->b_flags & B_DELWRI) == 0)
281 panic("nfsbioread");
282 if (VOP_BWRITE(bp) == EINTR)
283 return (EINTR);
284 } else
285 brelse(bp);
286 goto again;
287 }
288 }
289 vp->v_lastr = lbn;
290 diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
291 if (diff < n)
292 n = diff;
293 break;
294 case VLNK:
295 nfsstats.biocache_readlinks++;
296 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
297 if (!bp)
298 return (EINTR);
299 if ((bp->b_flags & B_DONE) == 0) {
300 bp->b_flags |= B_READ;
301 error = nfs_doio(bp, cred, p);
302 if (error) {
303 brelse(bp);
304 return (error);
305 }
306 }
307 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
308 got_buf = 1;
309 on = 0;
310 break;
311 case VDIR:
312 diragain:
313 nfsstats.biocache_readdirs++;
314 ndp = nfs_searchdircache(vp, uio->uio_offset,
315 (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
316 if (!ndp) {
317 /*
318 * We've been handed a cookie that is not
319 * in the cache. If we're not translating
320 * 32 <-> 64, it may be a value that was
321 * flushed out of the cache because it grew
322 * too big. Let the server judge if it's
323 * valid or not. In the translation case,
324 * we have no way of validating this value,
325 * so punt.
326 */
327 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
328 return (EINVAL);
329 ndp = nfs_enterdircache(vp, uio->uio_offset,
330 uio->uio_offset, 0, 0);
331 }
332
333 if (uio->uio_offset != 0 &&
334 ndp->dc_cookie == np->n_direofoffset) {
335 nfsstats.direofcache_hits++;
336 return (0);
337 }
338
339 bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
340 if (!bp)
341 return (EINTR);
342 if ((bp->b_flags & B_DONE) == 0) {
343 bp->b_flags |= B_READ;
344 bp->b_dcookie = ndp->dc_blkcookie;
345 error = nfs_doio(bp, cred, p);
346 if (error) {
347 /*
348 * Yuck! The directory has been modified on the
349 * server. Punt and let the userland code
350 * deal with it.
351 */
352 brelse(bp);
353 if (error == NFSERR_BAD_COOKIE) {
354 nfs_invaldircache(vp, 0);
355 nfs_vinvalbuf(vp, 0, cred, p, 1);
356 error = EINVAL;
357 }
358 return (error);
359 }
360 /*
361 * Just return if we hit EOF right away with this
362 * block.
363 */
364 if (np->n_direofoffset != 0 &&
365 ndp->dc_blkcookie == np->n_direofoffset) {
366 brelse(bp);
367 return (0);
368 }
369 }
370
371 /*
372 * Find the entry we were looking for in the block.
373 */
374
375 en = ndp->dc_entry;
376
377 pdp = dp = (struct dirent *)bp->b_data;
378 edp = bp->b_data + bp->b_validend;
379 enn = 0;
380 while (enn < en && (caddr_t)dp < edp) {
381 pdp = dp;
382 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
383 enn++;
384 }
385
386 /*
387 * If the entry number was bigger than the number of
388 * entries in the block, or the cookie of the previous
389 * entry doesn't match, the directory cache is
390 * stale. Flush it and try again (i.e. go to
391 * the server).
392 */
393 if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
394 (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
395 #ifdef DEBUG
396 printf("invalid cache: %p %p %p off %lx %lx\n",
397 pdp, dp, edp,
398 (unsigned long)uio->uio_offset,
399 (unsigned long)NFS_GETCOOKIE(pdp));
400 #endif
401 brelse(bp);
402 nfs_invaldircache(vp, 0);
403 nfs_vinvalbuf(vp, 0, cred, p, 0);
404 goto diragain;
405 }
406
407 on = (caddr_t)dp - bp->b_data;
408
409 /*
410 * Cache all entries that may be exported to the
411 * user, as they may be thrown back at us. The
412 * NFSBIO_CACHECOOKIES flag indicates that all
413 * entries are being 'exported', so cache them all.
414 */
415
416 if (en == 0 && pdp == dp) {
417 dp = (struct dirent *)
418 ((caddr_t)dp + dp->d_reclen);
419 enn++;
420 }
421
422 if (uio->uio_resid < (bp->b_validend - on)) {
423 n = uio->uio_resid;
424 enough = 1;
425 } else
426 n = bp->b_validend - on;
427
428 ep = bp->b_data + on + n;
429
430 /*
431 * Find last complete entry to copy, caching entries
432 * (if requested) as we go.
433 */
434
435 while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
436 if (cflag & NFSBIO_CACHECOOKIES) {
437 nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
438 ndp->dc_blkcookie, enn, bp->b_lblkno);
439 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
440 NFS_STASHCOOKIE32(pdp,
441 nndp->dc_cookie32);
442 }
443 }
444 pdp = dp;
445 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
446 enn++;
447 }
448
449 /*
450 * If the last requested entry was not the last in the
451 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
452 * cache the cookie of the last requested one, and
453 * set of the offset to it.
454 */
455
456 if ((on + n) < bp->b_validend) {
457 curoff = NFS_GETCOOKIE(pdp);
458 nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
459 enn, bp->b_lblkno);
460 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
461 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
462 curoff = nndp->dc_cookie32;
463 }
464 } else
465 curoff = bp->b_dcookie;
466
467 /*
468 * Always cache the entry for the next block,
469 * so that readaheads can use it.
470 */
471 nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
472 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
473 if (curoff == bp->b_dcookie) {
474 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
475 curoff = nndp->dc_cookie32;
476 }
477 }
478
479 n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
480
481 /*
482 * If not eof and read aheads are enabled, start one.
483 * (You need the current block first, so that you have the
484 * directory offset cookie of the next block.)
485 */
486 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
487 np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
488 rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
489 NFS_DIRBLKSIZ, p);
490 if (rabp) {
491 if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
492 rabp->b_dcookie = nndp->dc_cookie;
493 rabp->b_flags |= (B_READ | B_ASYNC);
494 if (nfs_asyncio(rabp, cred)) {
495 rabp->b_flags |= B_INVAL;
496 brelse(rabp);
497 }
498 } else
499 brelse(rabp);
500 }
501 }
502 got_buf = 1;
503 break;
504 default:
505 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
506 break;
507 };
508
509 if (n > 0) {
510 if (!baddr)
511 baddr = bp->b_data;
512 error = uiomove(baddr + on, (int)n, uio);
513 }
514 switch (vp->v_type) {
515 case VREG:
516 break;
517 case VLNK:
518 n = 0;
519 break;
520 case VDIR:
521 if (np->n_flag & NQNFSNONCACHE)
522 bp->b_flags |= B_INVAL;
523 uio->uio_offset = curoff;
524 if (enough)
525 n = 0;
526 break;
527 default:
528 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
529 }
530 if (got_buf)
531 brelse(bp);
532 } while (error == 0 && uio->uio_resid > 0 && n > 0);
533 return (error);
534 }
535
536 /*
537 * Vnode op for write using bio
538 */
539 int
540 nfs_write(v)
541 void *v;
542 {
543 struct vop_write_args /* {
544 struct vnode *a_vp;
545 struct uio *a_uio;
546 int a_ioflag;
547 struct ucred *a_cred;
548 } */ *ap = v;
549 register int biosize;
550 register struct uio *uio = ap->a_uio;
551 struct proc *p = uio->uio_procp;
552 register struct vnode *vp = ap->a_vp;
553 struct nfsnode *np = VTONFS(vp);
554 register struct ucred *cred = ap->a_cred;
555 int ioflag = ap->a_ioflag;
556 struct buf *bp;
557 struct vattr vattr;
558 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
559 daddr_t lbn, bn;
560 int n, on, error = 0, iomode, must_commit;
561
562 #ifdef DIAGNOSTIC
563 if (uio->uio_rw != UIO_WRITE)
564 panic("nfs_write mode");
565 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
566 panic("nfs_write proc");
567 #endif
568 if (vp->v_type != VREG)
569 return (EIO);
570 if (np->n_flag & NWRITEERR) {
571 np->n_flag &= ~NWRITEERR;
572 return (np->n_error);
573 }
574 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
575 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
576 (void)nfs_fsinfo(nmp, vp, cred, p);
577 if (ioflag & (IO_APPEND | IO_SYNC)) {
578 if (np->n_flag & NMODIFIED) {
579 np->n_attrstamp = 0;
580 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
581 if (error)
582 return (error);
583 }
584 if (ioflag & IO_APPEND) {
585 np->n_attrstamp = 0;
586 error = VOP_GETATTR(vp, &vattr, cred, p);
587 if (error)
588 return (error);
589 uio->uio_offset = np->n_size;
590 }
591 }
592 if (uio->uio_offset < 0)
593 return (EINVAL);
594 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
595 return (EFBIG);
596 if (uio->uio_resid == 0)
597 return (0);
598 /*
599 * Maybe this should be above the vnode op call, but so long as
600 * file servers have no limits, i don't think it matters
601 */
602 if (p && uio->uio_offset + uio->uio_resid >
603 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
604 psignal(p, SIGXFSZ);
605 return (EFBIG);
606 }
607 /*
608 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
609 * will be the same size within a filesystem. nfs_writerpc will
610 * still use nm_wsize when sizing the rpc's.
611 */
612 biosize = nmp->nm_rsize;
613 do {
614
615 /*
616 * XXX make sure we aren't cached in the VM page cache
617 */
618 (void)vnode_pager_uncache(vp);
619
620 /*
621 * Check for a valid write lease.
622 */
623 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
624 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
625 do {
626 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
627 } while (error == NQNFS_EXPIRED);
628 if (error)
629 return (error);
630 if (np->n_lrev != np->n_brev ||
631 (np->n_flag & NQNFSNONCACHE)) {
632 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
633 if (error)
634 return (error);
635 np->n_brev = np->n_lrev;
636 }
637 }
638 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
639 iomode = NFSV3WRITE_FILESYNC;
640 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
641 if (must_commit)
642 nfs_clearcommit(vp->v_mount);
643 return (error);
644 }
645 nfsstats.biocache_writes++;
646 lbn = uio->uio_offset / biosize;
647 on = uio->uio_offset & (biosize-1);
648 n = min((unsigned)(biosize - on), uio->uio_resid);
649 bn = lbn * (biosize / DEV_BSIZE);
650 again:
651 bp = nfs_getcacheblk(vp, bn, biosize, p);
652 if (!bp)
653 return (EINTR);
654 if (bp->b_wcred == NOCRED) {
655 crhold(cred);
656 bp->b_wcred = cred;
657 }
658 np->n_flag |= NMODIFIED;
659 if (uio->uio_offset + n > np->n_size) {
660 np->n_size = uio->uio_offset + n;
661 vnode_pager_setsize(vp, np->n_size);
662 }
663
664 /*
665 * If the new write will leave a contiguous dirty
666 * area, just update the b_dirtyoff and b_dirtyend,
667 * otherwise force a write rpc of the old dirty area.
668 */
669 if (bp->b_dirtyend > 0 &&
670 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
671 bp->b_proc = p;
672 if (VOP_BWRITE(bp) == EINTR)
673 return (EINTR);
674 goto again;
675 }
676
677 /*
678 * Check for valid write lease and get one as required.
679 * In case getblk() and/or bwrite() delayed us.
680 */
681 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
682 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
683 do {
684 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
685 } while (error == NQNFS_EXPIRED);
686 if (error) {
687 brelse(bp);
688 return (error);
689 }
690 if (np->n_lrev != np->n_brev ||
691 (np->n_flag & NQNFSNONCACHE)) {
692 brelse(bp);
693 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
694 if (error)
695 return (error);
696 np->n_brev = np->n_lrev;
697 goto again;
698 }
699 }
700 error = uiomove((char *)bp->b_data + on, n, uio);
701 if (error) {
702 bp->b_flags |= B_ERROR;
703 brelse(bp);
704 return (error);
705 }
706 if (bp->b_dirtyend > 0) {
707 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
708 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
709 } else {
710 bp->b_dirtyoff = on;
711 bp->b_dirtyend = on + n;
712 }
713 if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
714 bp->b_validoff > bp->b_dirtyend) {
715 bp->b_validoff = bp->b_dirtyoff;
716 bp->b_validend = bp->b_dirtyend;
717 } else {
718 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
719 bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
720 }
721
722 /*
723 * Since this block is being modified, it must be written
724 * again and not just committed.
725 */
726 bp->b_flags &= ~B_NEEDCOMMIT;
727
728 /*
729 * If the lease is non-cachable or IO_SYNC do bwrite().
730 */
731 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
732 bp->b_proc = p;
733 error = VOP_BWRITE(bp);
734 if (error)
735 return (error);
736 if (np->n_flag & NQNFSNONCACHE) {
737 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
738 if (error)
739 return (error);
740 }
741 } else if ((n + on) == biosize &&
742 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
743 bp->b_proc = (struct proc *)0;
744 bp->b_flags |= B_ASYNC;
745 (void)nfs_writebp(bp, 0);
746 } else {
747 bdwrite(bp);
748 }
749 } while (uio->uio_resid > 0 && n > 0);
750 return (0);
751 }
752
753 /*
754 * Get an nfs cache block.
755 * Allocate a new one if the block isn't currently in the cache
756 * and return the block marked busy. If the calling process is
757 * interrupted by a signal for an interruptible mount point, return
758 * NULL.
759 */
760 struct buf *
761 nfs_getcacheblk(vp, bn, size, p)
762 struct vnode *vp;
763 daddr_t bn;
764 int size;
765 struct proc *p;
766 {
767 register struct buf *bp;
768 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
769
770 if (nmp->nm_flag & NFSMNT_INT) {
771 bp = getblk(vp, bn, size, PCATCH, 0);
772 while (bp == (struct buf *)0) {
773 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
774 return ((struct buf *)0);
775 bp = getblk(vp, bn, size, 0, 2 * hz);
776 }
777 } else
778 bp = getblk(vp, bn, size, 0, 0);
779 return (bp);
780 }
781
782 /*
783 * Flush and invalidate all dirty buffers. If another process is already
784 * doing the flush, just wait for completion.
785 */
786 int
787 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
788 struct vnode *vp;
789 int flags;
790 struct ucred *cred;
791 struct proc *p;
792 int intrflg;
793 {
794 register struct nfsnode *np = VTONFS(vp);
795 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
796 int error = 0, slpflag, slptimeo;
797
798 if ((nmp->nm_flag & NFSMNT_INT) == 0)
799 intrflg = 0;
800 if (intrflg) {
801 slpflag = PCATCH;
802 slptimeo = 2 * hz;
803 } else {
804 slpflag = 0;
805 slptimeo = 0;
806 }
807 /*
808 * First wait for any other process doing a flush to complete.
809 */
810 while (np->n_flag & NFLUSHINPROG) {
811 np->n_flag |= NFLUSHWANT;
812 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
813 slptimeo);
814 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
815 return (EINTR);
816 }
817
818 /*
819 * Now, flush as required.
820 */
821 np->n_flag |= NFLUSHINPROG;
822 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
823 while (error) {
824 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
825 np->n_flag &= ~NFLUSHINPROG;
826 if (np->n_flag & NFLUSHWANT) {
827 np->n_flag &= ~NFLUSHWANT;
828 wakeup((caddr_t)&np->n_flag);
829 }
830 return (EINTR);
831 }
832 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
833 }
834 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
835 if (np->n_flag & NFLUSHWANT) {
836 np->n_flag &= ~NFLUSHWANT;
837 wakeup((caddr_t)&np->n_flag);
838 }
839 return (0);
840 }
841
842 /*
843 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
844 * This is mainly to avoid queueing async I/O requests when the nfsiods
845 * are all hung on a dead server.
846 */
847 int
848 nfs_asyncio(bp, cred)
849 register struct buf *bp;
850 struct ucred *cred;
851 {
852 register int i;
853 register struct nfsmount *nmp;
854 int gotiod, slpflag = 0, slptimeo = 0, error;
855
856 if (nfs_numasync == 0)
857 return (EIO);
858
859
860 nmp = VFSTONFS(bp->b_vp->v_mount);
861 again:
862 if (nmp->nm_flag & NFSMNT_INT)
863 slpflag = PCATCH;
864 gotiod = FALSE;
865
866 /*
867 * Find a free iod to process this request.
868 */
869
870 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
871 if (nfs_iodwant[i]) {
872 /*
873 * Found one, so wake it up and tell it which
874 * mount to process.
875 */
876 nfs_iodwant[i] = (struct proc *)0;
877 nfs_iodmount[i] = nmp;
878 nmp->nm_bufqiods++;
879 wakeup((caddr_t)&nfs_iodwant[i]);
880 gotiod = TRUE;
881 break;
882 }
883 /*
884 * If none are free, we may already have an iod working on this mount
885 * point. If so, it will process our request.
886 */
887 if (!gotiod && nmp->nm_bufqiods > 0)
888 gotiod = TRUE;
889
890 /*
891 * If we have an iod which can process the request, then queue
892 * the buffer.
893 */
894 if (gotiod) {
895 /*
896 * Ensure that the queue never grows too large.
897 */
898 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
899 nmp->nm_bufqwant = TRUE;
900 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
901 "nfsaio", slptimeo);
902 if (error) {
903 if (nfs_sigintr(nmp, NULL, bp->b_proc))
904 return (EINTR);
905 if (slpflag == PCATCH) {
906 slpflag = 0;
907 slptimeo = 2 * hz;
908 }
909 }
910 /*
911 * We might have lost our iod while sleeping,
912 * so check and loop if nescessary.
913 */
914 if (nmp->nm_bufqiods == 0)
915 goto again;
916 }
917
918 if (bp->b_flags & B_READ) {
919 if (bp->b_rcred == NOCRED && cred != NOCRED) {
920 crhold(cred);
921 bp->b_rcred = cred;
922 }
923 } else {
924 bp->b_flags |= B_WRITEINPROG;
925 if (bp->b_wcred == NOCRED && cred != NOCRED) {
926 crhold(cred);
927 bp->b_wcred = cred;
928 }
929 }
930
931 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
932 nmp->nm_bufqlen++;
933 return (0);
934 }
935
936 /*
937 * All the iods are busy on other mounts, so return EIO to
938 * force the caller to process the i/o synchronously.
939 */
940 return (EIO);
941 }
942
943 /*
944 * Do an I/O operation to/from a cache block. This may be called
945 * synchronously or from an nfsiod.
946 */
947 int
948 nfs_doio(bp, cr, p)
949 register struct buf *bp;
950 struct ucred *cr;
951 struct proc *p;
952 {
953 register struct uio *uiop;
954 register struct vnode *vp;
955 struct nfsnode *np;
956 struct nfsmount *nmp;
957 int error = 0, diff, len, iomode, must_commit = 0;
958 struct uio uio;
959 struct iovec io;
960
961 vp = bp->b_vp;
962 np = VTONFS(vp);
963 nmp = VFSTONFS(vp->v_mount);
964 uiop = &uio;
965 uiop->uio_iov = &io;
966 uiop->uio_iovcnt = 1;
967 uiop->uio_segflg = UIO_SYSSPACE;
968 uiop->uio_procp = p;
969
970 /*
971 * Historically, paging was done with physio, but no more...
972 */
973 if (bp->b_flags & B_PHYS) {
974 /*
975 * ...though reading /dev/drum still gets us here.
976 */
977 io.iov_len = uiop->uio_resid = bp->b_bcount;
978 /* mapping was done by vmapbuf() */
979 io.iov_base = bp->b_data;
980 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
981 if (bp->b_flags & B_READ) {
982 uiop->uio_rw = UIO_READ;
983 nfsstats.read_physios++;
984 error = nfs_readrpc(vp, uiop, cr);
985 } else {
986 iomode = NFSV3WRITE_DATASYNC;
987 uiop->uio_rw = UIO_WRITE;
988 nfsstats.write_physios++;
989 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
990 }
991 if (error) {
992 bp->b_flags |= B_ERROR;
993 bp->b_error = error;
994 }
995 } else if (bp->b_flags & B_READ) {
996 io.iov_len = uiop->uio_resid = bp->b_bcount;
997 io.iov_base = bp->b_data;
998 uiop->uio_rw = UIO_READ;
999 switch (vp->v_type) {
1000 case VREG:
1001 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1002 nfsstats.read_bios++;
1003 error = nfs_readrpc(vp, uiop, cr);
1004 if (!error) {
1005 bp->b_validoff = 0;
1006 if (uiop->uio_resid) {
1007 /*
1008 * If len > 0, there is a hole in the file and
1009 * no writes after the hole have been pushed to
1010 * the server yet.
1011 * Just zero fill the rest of the valid area.
1012 */
1013 diff = bp->b_bcount - uiop->uio_resid;
1014 len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
1015 + diff);
1016 if (len > 0) {
1017 len = min(len, uiop->uio_resid);
1018 bzero((char *)bp->b_data + diff, len);
1019 bp->b_validend = diff + len;
1020 } else
1021 bp->b_validend = diff;
1022 } else
1023 bp->b_validend = bp->b_bcount;
1024 }
1025 if (p && (vp->v_flag & VTEXT) &&
1026 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1027 NQNFS_CKINVALID(vp, np, ND_READ) &&
1028 np->n_lrev != np->n_brev) ||
1029 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1030 np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
1031 uprintf("Process killed due to text file modification\n");
1032 psignal(p, SIGKILL);
1033 p->p_holdcnt++;
1034 }
1035 break;
1036 case VLNK:
1037 uiop->uio_offset = (off_t)0;
1038 nfsstats.readlink_bios++;
1039 error = nfs_readlinkrpc(vp, uiop, cr);
1040 break;
1041 case VDIR:
1042 nfsstats.readdir_bios++;
1043 uiop->uio_offset = bp->b_dcookie;
1044 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1045 error = nfs_readdirplusrpc(vp, uiop, cr);
1046 if (error == NFSERR_NOTSUPP)
1047 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1048 }
1049 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1050 error = nfs_readdirrpc(vp, uiop, cr);
1051 if (!error) {
1052 bp->b_dcookie = uiop->uio_offset;
1053 bp->b_validoff = 0;
1054 bp->b_validend = bp->b_bcount - uiop->uio_resid;
1055 }
1056 break;
1057 default:
1058 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1059 break;
1060 };
1061 if (error) {
1062 bp->b_flags |= B_ERROR;
1063 bp->b_error = error;
1064 }
1065 } else {
1066 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1067 - bp->b_dirtyoff;
1068 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
1069 + bp->b_dirtyoff;
1070 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1071 uiop->uio_rw = UIO_WRITE;
1072 nfsstats.write_bios++;
1073 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
1074 iomode = NFSV3WRITE_UNSTABLE;
1075 else
1076 iomode = NFSV3WRITE_FILESYNC;
1077 bp->b_flags |= B_WRITEINPROG;
1078 #ifdef fvdl_debug
1079 printf("nfs_doio(%x): bp %x doff %d dend %d\n",
1080 vp, bp, bp->b_dirtyoff, bp->b_dirtyend);
1081 #endif
1082 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1083 if (!error && iomode == NFSV3WRITE_UNSTABLE)
1084 bp->b_flags |= B_NEEDCOMMIT;
1085 else
1086 bp->b_flags &= ~B_NEEDCOMMIT;
1087 bp->b_flags &= ~B_WRITEINPROG;
1088
1089 /*
1090 * For an interrupted write, the buffer is still valid and the
1091 * write hasn't been pushed to the server yet, so we can't set
1092 * B_ERROR and report the interruption by setting B_EINTR. For
1093 * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
1094 * is essentially a noop.
1095 * For the case of a V3 write rpc not being committed to stable
1096 * storage, the block is still dirty and requires either a commit
1097 * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
1098 * before the block is reused. This is indicated by setting the
1099 * B_DELWRI and B_NEEDCOMMIT flags.
1100 */
1101 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1102 bp->b_flags |= B_DELWRI;
1103
1104 /*
1105 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
1106 * buffer to the clean list, we have to reassign it back to the
1107 * dirty one. Ugh.
1108 */
1109 if (bp->b_flags & B_ASYNC)
1110 reassignbuf(bp, vp);
1111 else if (error)
1112 bp->b_flags |= B_EINTR;
1113 } else {
1114 if (error) {
1115 bp->b_flags |= B_ERROR;
1116 bp->b_error = np->n_error = error;
1117 np->n_flag |= NWRITEERR;
1118 }
1119 bp->b_dirtyoff = bp->b_dirtyend = 0;
1120 }
1121 }
1122 bp->b_resid = uiop->uio_resid;
1123 if (must_commit)
1124 nfs_clearcommit(vp->v_mount);
1125 biodone(bp);
1126 return (error);
1127 }
1128