FreeBSD/Linux Kernel Cross Reference
sys/nfs/nfs_bio.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
37 * $FreeBSD$
38 */
39
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
45 #include <sys/proc.h>
46 #include <sys/buf.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vnode_pager.h>
57
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
60 #include <nfs/nfs.h>
61 #include <nfs/nfsmount.h>
62 #include <nfs/nqnfs.h>
63 #include <nfs/nfsnode.h>
64
65 static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
66 struct proc *p));
67
68 extern int nfs_numasync;
69 extern int nfs_pbuf_freecnt;
70 extern struct nfsstats nfsstats;
71
72 /*
73 * Vnode op for VM getpages.
74 */
75 int
76 nfs_getpages(ap)
77 struct vop_getpages_args /* {
78 struct vnode *a_vp;
79 vm_page_t *a_m;
80 int a_count;
81 int a_reqpage;
82 vm_ooffset_t a_offset;
83 } */ *ap;
84 {
85 int i, error, nextoff, size, toff, count, npages;
86 struct uio uio;
87 struct iovec iov;
88 vm_offset_t kva;
89 struct buf *bp;
90 struct vnode *vp;
91 struct proc *p;
92 struct ucred *cred;
93 struct nfsmount *nmp;
94 vm_page_t *pages;
95
96 vp = ap->a_vp;
97 p = curproc; /* XXX */
98 cred = curproc->p_ucred; /* XXX */
99 nmp = VFSTONFS(vp->v_mount);
100 pages = ap->a_m;
101 count = ap->a_count;
102
103 if (vp->v_object == NULL) {
104 printf("nfs_getpages: called with non-merged cache vnode??\n");
105 return VM_PAGER_ERROR;
106 }
107
108 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
109 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
110 (void)nfs_fsinfo(nmp, vp, cred, p);
111
112 npages = btoc(count);
113
114 /*
115 * If the requested page is partially valid, just return it and
116 * allow the pager to zero-out the blanks. Partially valid pages
117 * can only occur at the file EOF.
118 */
119
120 {
121 vm_page_t m = pages[ap->a_reqpage];
122
123 if (m->valid != 0) {
124 /* handled by vm_fault now */
125 /* vm_page_zero_invalid(m, TRUE); */
126 for (i = 0; i < npages; ++i) {
127 if (i != ap->a_reqpage)
128 vnode_pager_freepage(pages[i]);
129 }
130 return(0);
131 }
132 }
133
134 /*
135 * We use only the kva address for the buffer, but this is extremely
136 * convienient and fast.
137 */
138 bp = getpbuf(&nfs_pbuf_freecnt);
139
140 kva = (vm_offset_t) bp->b_data;
141 pmap_qenter(kva, pages, npages);
142
143 iov.iov_base = (caddr_t) kva;
144 iov.iov_len = count;
145 uio.uio_iov = &iov;
146 uio.uio_iovcnt = 1;
147 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
148 uio.uio_resid = count;
149 uio.uio_segflg = UIO_SYSSPACE;
150 uio.uio_rw = UIO_READ;
151 uio.uio_procp = p;
152
153 error = nfs_readrpc(vp, &uio, cred);
154 pmap_qremove(kva, npages);
155
156 relpbuf(bp, &nfs_pbuf_freecnt);
157
158 if (error && (uio.uio_resid == count)) {
159 printf("nfs_getpages: error %d\n", error);
160 for (i = 0; i < npages; ++i) {
161 if (i != ap->a_reqpage)
162 vnode_pager_freepage(pages[i]);
163 }
164 return VM_PAGER_ERROR;
165 }
166
167 /*
168 * Calculate the number of bytes read and validate only that number
169 * of bytes. Note that due to pending writes, size may be 0. This
170 * does not mean that the remaining data is invalid!
171 */
172
173 size = count - uio.uio_resid;
174
175 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
176 vm_page_t m;
177 nextoff = toff + PAGE_SIZE;
178 m = pages[i];
179
180 m->flags &= ~PG_ZERO;
181
182 if (nextoff <= size) {
183 /*
184 * Read operation filled an entire page
185 */
186 m->valid = VM_PAGE_BITS_ALL;
187 vm_page_undirty(m);
188 } else if (size > toff) {
189 /*
190 * Read operation filled a partial page.
191 */
192 m->valid = 0;
193 vm_page_set_validclean(m, 0, size - toff);
194 /* handled by vm_fault now */
195 /* vm_page_zero_invalid(m, TRUE); */
196 } else {
197 /*
198 * Read operation was short. If no error occured
199 * we may have hit a zero-fill section. We simply
200 * leave valid set to 0.
201 */
202 ;
203 }
204 if (i != ap->a_reqpage) {
205 /*
206 * Whether or not to leave the page activated is up in
207 * the air, but we should put the page on a page queue
208 * somewhere (it already is in the object). Result:
209 * It appears that emperical results show that
210 * deactivating pages is best.
211 */
212
213 /*
214 * Just in case someone was asking for this page we
215 * now tell them that it is ok to use.
216 */
217 if (!error) {
218 if (m->flags & PG_WANTED)
219 vm_page_activate(m);
220 else
221 vm_page_deactivate(m);
222 vm_page_wakeup(m);
223 } else {
224 vnode_pager_freepage(m);
225 }
226 }
227 }
228 return 0;
229 }
230
231 /*
232 * Vnode op for VM putpages.
233 */
234 int
235 nfs_putpages(ap)
236 struct vop_putpages_args /* {
237 struct vnode *a_vp;
238 vm_page_t *a_m;
239 int a_count;
240 int a_sync;
241 int *a_rtvals;
242 vm_ooffset_t a_offset;
243 } */ *ap;
244 {
245 struct uio uio;
246 struct iovec iov;
247 vm_offset_t kva;
248 struct buf *bp;
249 int iomode, must_commit, i, error, npages, count;
250 off_t offset;
251 int *rtvals;
252 struct vnode *vp;
253 struct proc *p;
254 struct ucred *cred;
255 struct nfsmount *nmp;
256 struct nfsnode *np;
257 vm_page_t *pages;
258
259 vp = ap->a_vp;
260 np = VTONFS(vp);
261 p = curproc; /* XXX */
262 cred = curproc->p_ucred; /* XXX */
263 nmp = VFSTONFS(vp->v_mount);
264 pages = ap->a_m;
265 count = ap->a_count;
266 rtvals = ap->a_rtvals;
267 npages = btoc(count);
268 offset = IDX_TO_OFF(pages[0]->pindex);
269
270 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
271 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
272 (void)nfs_fsinfo(nmp, vp, cred, p);
273
274 for (i = 0; i < npages; i++) {
275 rtvals[i] = VM_PAGER_AGAIN;
276 }
277
278 /*
279 * When putting pages, do not extend file past EOF.
280 */
281
282 if (offset + count > np->n_size) {
283 count = np->n_size - offset;
284 if (count < 0)
285 count = 0;
286 }
287
288 /*
289 * We use only the kva address for the buffer, but this is extremely
290 * convienient and fast.
291 */
292 bp = getpbuf(&nfs_pbuf_freecnt);
293
294 kva = (vm_offset_t) bp->b_data;
295 pmap_qenter(kva, pages, npages);
296
297 iov.iov_base = (caddr_t) kva;
298 iov.iov_len = count;
299 uio.uio_iov = &iov;
300 uio.uio_iovcnt = 1;
301 uio.uio_offset = offset;
302 uio.uio_resid = count;
303 uio.uio_segflg = UIO_SYSSPACE;
304 uio.uio_rw = UIO_WRITE;
305 uio.uio_procp = p;
306
307 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
308 iomode = NFSV3WRITE_UNSTABLE;
309 else
310 iomode = NFSV3WRITE_FILESYNC;
311
312 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
313
314 pmap_qremove(kva, npages);
315 relpbuf(bp, &nfs_pbuf_freecnt);
316
317 if (!error) {
318 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
319 for (i = 0; i < nwritten; i++) {
320 rtvals[i] = VM_PAGER_OK;
321 vm_page_undirty(pages[i]);
322 }
323 if (must_commit)
324 nfs_clearcommit(vp->v_mount);
325 }
326 return rtvals[0];
327 }
328
329 /*
330 * Vnode op for read using bio
331 */
332 int
333 nfs_bioread(vp, uio, ioflag, cred)
334 register struct vnode *vp;
335 register struct uio *uio;
336 int ioflag;
337 struct ucred *cred;
338 {
339 register struct nfsnode *np = VTONFS(vp);
340 register int biosize, i;
341 struct buf *bp = 0, *rabp;
342 struct vattr vattr;
343 struct proc *p;
344 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
345 daddr_t lbn, rabn;
346 int bcount;
347 int seqcount;
348 int nra, error = 0, n = 0, on = 0;
349
350 #ifdef DIAGNOSTIC
351 if (uio->uio_rw != UIO_READ)
352 panic("nfs_read mode");
353 #endif
354 if (uio->uio_resid == 0)
355 return (0);
356 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
357 return (EINVAL);
358 p = uio->uio_procp;
359
360 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
361 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
362 (void)nfs_fsinfo(nmp, vp, cred, p);
363 if (vp->v_type != VDIR &&
364 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
365 return (EFBIG);
366 biosize = vp->v_mount->mnt_stat.f_iosize;
367 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
368 /*
369 * For nfs, cache consistency can only be maintained approximately.
370 * Although RFC1094 does not specify the criteria, the following is
371 * believed to be compatible with the reference port.
372 * For nqnfs, full cache consistency is maintained within the loop.
373 * For nfs:
374 * If the file's modify time on the server has changed since the
375 * last read rpc or you have written to the file,
376 * you may have lost data cache consistency with the
377 * server, so flush all of the file's data out of the cache.
378 * Then force a getattr rpc to ensure that you have up to date
379 * attributes.
380 * NB: This implies that cache data can be read when up to
381 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
382 * attributes this could be forced by setting n_attrstamp to 0 before
383 * the VOP_GETATTR() call.
384 */
385 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
386 if (np->n_flag & NMODIFIED) {
387 if (vp->v_type != VREG) {
388 if (vp->v_type != VDIR)
389 panic("nfs: bioread, not dir");
390 nfs_invaldir(vp);
391 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
392 if (error)
393 return (error);
394 }
395 np->n_attrstamp = 0;
396 error = VOP_GETATTR(vp, &vattr, cred, p);
397 if (error)
398 return (error);
399 np->n_mtime = vattr.va_mtime.tv_sec;
400 } else {
401 error = VOP_GETATTR(vp, &vattr, cred, p);
402 if (error)
403 return (error);
404 if ((np->n_flag & NSIZECHANGED)
405 || np->n_mtime != vattr.va_mtime.tv_sec) {
406 if (vp->v_type == VDIR)
407 nfs_invaldir(vp);
408 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
409 if (error)
410 return (error);
411 np->n_mtime = vattr.va_mtime.tv_sec;
412 np->n_flag &= ~NSIZECHANGED;
413 }
414 }
415 }
416 do {
417
418 /*
419 * Get a valid lease. If cached data is stale, flush it.
420 */
421 if (nmp->nm_flag & NFSMNT_NQNFS) {
422 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
423 do {
424 error = nqnfs_getlease(vp, ND_READ, cred, p);
425 } while (error == NQNFS_EXPIRED);
426 if (error)
427 return (error);
428 if (np->n_lrev != np->n_brev ||
429 (np->n_flag & NQNFSNONCACHE) ||
430 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
431 if (vp->v_type == VDIR)
432 nfs_invaldir(vp);
433 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
434 if (error)
435 return (error);
436 np->n_brev = np->n_lrev;
437 }
438 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
439 nfs_invaldir(vp);
440 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
441 if (error)
442 return (error);
443 }
444 }
445 if (np->n_flag & NQNFSNONCACHE) {
446 switch (vp->v_type) {
447 case VREG:
448 return (nfs_readrpc(vp, uio, cred));
449 case VLNK:
450 return (nfs_readlinkrpc(vp, uio, cred));
451 case VDIR:
452 break;
453 default:
454 printf(" NQNFSNONCACHE: type %x unexpected\n",
455 vp->v_type);
456 };
457 }
458 switch (vp->v_type) {
459 case VREG:
460 nfsstats.biocache_reads++;
461 lbn = uio->uio_offset / biosize;
462 on = uio->uio_offset & (biosize - 1);
463
464 /*
465 * Start the read ahead(s), as required.
466 */
467 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
468 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
469 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
470 rabn = lbn + 1 + nra;
471 if (!incore(vp, rabn)) {
472 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
473 if (!rabp)
474 return (EINTR);
475 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
476 rabp->b_flags |= (B_READ | B_ASYNC);
477 vfs_busy_pages(rabp, 0);
478 if (nfs_asyncio(rabp, cred, p)) {
479 rabp->b_flags |= B_INVAL|B_ERROR;
480 vfs_unbusy_pages(rabp);
481 brelse(rabp);
482 break;
483 }
484 } else {
485 brelse(rabp);
486 }
487 }
488 }
489 }
490
491 /*
492 * Obtain the buffer cache block. Figure out the buffer size
493 * when we are at EOF. If we are modifying the size of the
494 * buffer based on an EOF condition we need to hold
495 * nfs_rslock() through obtaining the buffer to prevent
496 * a potential writer-appender from messing with n_size.
497 * Otherwise we may accidently truncate the buffer and
498 * lose dirty data.
499 *
500 * Note that bcount is *not* DEV_BSIZE aligned.
501 */
502
503 again:
504 bcount = biosize;
505 if ((off_t)lbn * biosize >= np->n_size) {
506 bcount = 0;
507 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
508 bcount = np->n_size - (off_t)lbn * biosize;
509 }
510 if (bcount != biosize) {
511 switch(nfs_rslock(np, p)) {
512 case ENOLCK:
513 goto again;
514 /* not reached */
515 case EINTR:
516 case ERESTART:
517 return(EINTR);
518 /* not reached */
519 default:
520 break;
521 }
522 }
523
524 bp = nfs_getcacheblk(vp, lbn, bcount, p);
525
526 if (bcount != biosize)
527 nfs_rsunlock(np, p);
528 if (!bp)
529 return (EINTR);
530
531 /*
532 * If B_CACHE is not set, we must issue the read. If this
533 * fails, we return an error.
534 */
535
536 if ((bp->b_flags & B_CACHE) == 0) {
537 bp->b_flags |= B_READ;
538 vfs_busy_pages(bp, 0);
539 error = nfs_doio(bp, cred, p);
540 if (error) {
541 brelse(bp);
542 return (error);
543 }
544 }
545
546 /*
547 * on is the offset into the current bp. Figure out how many
548 * bytes we can copy out of the bp. Note that bcount is
549 * NOT DEV_BSIZE aligned.
550 *
551 * Then figure out how many bytes we can copy into the uio.
552 */
553
554 n = 0;
555 if (on < bcount)
556 n = min((unsigned)(bcount - on), uio->uio_resid);
557 break;
558 case VLNK:
559 nfsstats.biocache_readlinks++;
560 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
561 if (!bp)
562 return (EINTR);
563 if ((bp->b_flags & B_CACHE) == 0) {
564 bp->b_flags |= B_READ;
565 vfs_busy_pages(bp, 0);
566 error = nfs_doio(bp, cred, p);
567 if (error) {
568 bp->b_flags |= B_ERROR;
569 brelse(bp);
570 return (error);
571 }
572 }
573 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
574 on = 0;
575 break;
576 case VDIR:
577 nfsstats.biocache_readdirs++;
578 if (np->n_direofoffset
579 && uio->uio_offset >= np->n_direofoffset) {
580 return (0);
581 }
582 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
583 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
584 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
585 if (!bp)
586 return (EINTR);
587 if ((bp->b_flags & B_CACHE) == 0) {
588 bp->b_flags |= B_READ;
589 vfs_busy_pages(bp, 0);
590 error = nfs_doio(bp, cred, p);
591 if (error) {
592 brelse(bp);
593 }
594 while (error == NFSERR_BAD_COOKIE) {
595 printf("got bad cookie vp %p bp %p\n", vp, bp);
596 nfs_invaldir(vp);
597 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
598 /*
599 * Yuck! The directory has been modified on the
600 * server. The only way to get the block is by
601 * reading from the beginning to get all the
602 * offset cookies.
603 *
604 * Leave the last bp intact unless there is an error.
605 * Loop back up to the while if the error is another
606 * NFSERR_BAD_COOKIE (double yuch!).
607 */
608 for (i = 0; i <= lbn && !error; i++) {
609 if (np->n_direofoffset
610 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
611 return (0);
612 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
613 if (!bp)
614 return (EINTR);
615 if ((bp->b_flags & B_CACHE) == 0) {
616 bp->b_flags |= B_READ;
617 vfs_busy_pages(bp, 0);
618 error = nfs_doio(bp, cred, p);
619 /*
620 * no error + B_INVAL == directory EOF,
621 * use the block.
622 */
623 if (error == 0 && (bp->b_flags & B_INVAL))
624 break;
625 }
626 /*
627 * An error will throw away the block and the
628 * for loop will break out. If no error and this
629 * is not the block we want, we throw away the
630 * block and go for the next one via the for loop.
631 */
632 if (error || i < lbn)
633 brelse(bp);
634 }
635 }
636 /*
637 * The above while is repeated if we hit another cookie
638 * error. If we hit an error and it wasn't a cookie error,
639 * we give up.
640 */
641 if (error)
642 return (error);
643 }
644
645 /*
646 * If not eof and read aheads are enabled, start one.
647 * (You need the current block first, so that you have the
648 * directory offset cookie of the next block.)
649 */
650 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
651 (bp->b_flags & B_INVAL) == 0 &&
652 (np->n_direofoffset == 0 ||
653 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
654 !(np->n_flag & NQNFSNONCACHE) &&
655 !incore(vp, lbn + 1)) {
656 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
657 if (rabp) {
658 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
659 rabp->b_flags |= (B_READ | B_ASYNC);
660 vfs_busy_pages(rabp, 0);
661 if (nfs_asyncio(rabp, cred, p)) {
662 rabp->b_flags |= B_INVAL|B_ERROR;
663 vfs_unbusy_pages(rabp);
664 brelse(rabp);
665 }
666 } else {
667 brelse(rabp);
668 }
669 }
670 }
671 /*
672 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
673 * chopped for the EOF condition, we cannot tell how large
674 * NFS directories are going to be until we hit EOF. So
675 * an NFS directory buffer is *not* chopped to its EOF. Now,
676 * it just so happens that b_resid will effectively chop it
677 * to EOF. *BUT* this information is lost if the buffer goes
678 * away and is reconstituted into a B_CACHE state ( due to
679 * being VMIO ) later. So we keep track of the directory eof
680 * in np->n_direofoffset and chop it off as an extra step
681 * right here.
682 */
683 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
684 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
685 n = np->n_direofoffset - uio->uio_offset;
686 break;
687 default:
688 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
689 break;
690 };
691
692 if (n > 0) {
693 error = uiomove(bp->b_data + on, (int)n, uio);
694 }
695 switch (vp->v_type) {
696 case VREG:
697 break;
698 case VLNK:
699 n = 0;
700 break;
701 case VDIR:
702 /*
703 * Invalidate buffer if caching is disabled, forcing a
704 * re-read from the remote later.
705 */
706 if (np->n_flag & NQNFSNONCACHE)
707 bp->b_flags |= B_INVAL;
708 break;
709 default:
710 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
711 }
712 brelse(bp);
713 } while (error == 0 && uio->uio_resid > 0 && n > 0);
714 return (error);
715 }
716
717 /*
718 * Vnode op for write using bio
719 */
720 int
721 nfs_write(ap)
722 struct vop_write_args /* {
723 struct vnode *a_vp;
724 struct uio *a_uio;
725 int a_ioflag;
726 struct ucred *a_cred;
727 } */ *ap;
728 {
729 int biosize;
730 struct uio *uio = ap->a_uio;
731 struct proc *p = uio->uio_procp;
732 struct vnode *vp = ap->a_vp;
733 struct nfsnode *np = VTONFS(vp);
734 struct ucred *cred = ap->a_cred;
735 int ioflag = ap->a_ioflag;
736 struct buf *bp;
737 struct vattr vattr;
738 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
739 daddr_t lbn;
740 int bcount;
741 int n, on, error = 0, iomode, must_commit;
742 int haverslock = 0;
743
744 #ifdef DIAGNOSTIC
745 if (uio->uio_rw != UIO_WRITE)
746 panic("nfs_write mode");
747 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
748 panic("nfs_write proc");
749 #endif
750 if (vp->v_type != VREG)
751 return (EIO);
752 if (np->n_flag & NWRITEERR) {
753 np->n_flag &= ~NWRITEERR;
754 return (np->n_error);
755 }
756 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
757 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
758 (void)nfs_fsinfo(nmp, vp, cred, p);
759
760 /*
761 * Synchronously flush pending buffers if we are in synchronous
762 * mode or if we are appending.
763 */
764 if (ioflag & (IO_APPEND | IO_SYNC)) {
765 if (np->n_flag & NMODIFIED) {
766 np->n_attrstamp = 0;
767 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
768 if (error)
769 return (error);
770 }
771 }
772
773 /*
774 * If IO_APPEND then load uio_offset. We restart here if we cannot
775 * get the append lock.
776 */
777 restart:
778 if (ioflag & IO_APPEND) {
779 np->n_attrstamp = 0;
780 error = VOP_GETATTR(vp, &vattr, cred, p);
781 if (error)
782 return (error);
783 uio->uio_offset = np->n_size;
784 }
785
786 if (uio->uio_offset < 0)
787 return (EINVAL);
788 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
789 return (EFBIG);
790 if (uio->uio_resid == 0)
791 return (0);
792
793 /*
794 * We need to obtain the rslock if we intend to modify np->n_size
795 * in order to guarentee the append point with multiple contending
796 * writers, to guarentee that no other appenders modify n_size
797 * while we are trying to obtain a truncated buffer (i.e. to avoid
798 * accidently truncating data written by another appender due to
799 * the race), and to ensure that the buffer is populated prior to
800 * our extending of the file. We hold rslock through the entire
801 * operation.
802 *
803 * Note that we do not synchronize the case where someone truncates
804 * the file while we are appending to it because attempting to lock
805 * this case may deadlock other parts of the system unexpectedly.
806 */
807 if ((ioflag & IO_APPEND) ||
808 uio->uio_offset + uio->uio_resid > np->n_size) {
809 switch(nfs_rslock(np, p)) {
810 case ENOLCK:
811 goto restart;
812 /* not reached */
813 case EINTR:
814 case ERESTART:
815 return(EINTR);
816 /* not reached */
817 default:
818 break;
819 }
820 haverslock = 1;
821 }
822
823 /*
824 * Maybe this should be above the vnode op call, but so long as
825 * file servers have no limits, i don't think it matters
826 */
827 if (p && uio->uio_offset + uio->uio_resid >
828 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
829 psignal(p, SIGXFSZ);
830 if (haverslock)
831 nfs_rsunlock(np, p);
832 return (EFBIG);
833 }
834
835 biosize = vp->v_mount->mnt_stat.f_iosize;
836
837 do {
838 /*
839 * Check for a valid write lease.
840 */
841 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
842 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
843 do {
844 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
845 } while (error == NQNFS_EXPIRED);
846 if (error)
847 break;
848 if (np->n_lrev != np->n_brev ||
849 (np->n_flag & NQNFSNONCACHE)) {
850 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
851 if (error)
852 break;
853 np->n_brev = np->n_lrev;
854 }
855 }
856 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
857 iomode = NFSV3WRITE_FILESYNC;
858 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
859 if (must_commit)
860 nfs_clearcommit(vp->v_mount);
861 break;
862 }
863 nfsstats.biocache_writes++;
864 lbn = uio->uio_offset / biosize;
865 on = uio->uio_offset & (biosize-1);
866 n = min((unsigned)(biosize - on), uio->uio_resid);
867 again:
868 /*
869 * Handle direct append and file extension cases, calculate
870 * unaligned buffer size.
871 */
872
873 if (uio->uio_offset == np->n_size && n) {
874 /*
875 * Get the buffer (in its pre-append state to maintain
876 * B_CACHE if it was previously set). Resize the
877 * nfsnode after we have locked the buffer to prevent
878 * readers from reading garbage.
879 */
880 bcount = on;
881 bp = nfs_getcacheblk(vp, lbn, bcount, p);
882
883 if (bp != NULL) {
884 long save;
885
886 np->n_size = uio->uio_offset + n;
887 np->n_flag |= NMODIFIED;
888 vnode_pager_setsize(vp, np->n_size);
889
890 save = bp->b_flags & B_CACHE;
891 bcount += n;
892 allocbuf(bp, bcount);
893 bp->b_flags |= save;
894 }
895 } else {
896 /*
897 * Obtain the locked cache block first, and then
898 * adjust the file's size as appropriate.
899 */
900 bcount = on + n;
901 if ((off_t)lbn * biosize + bcount < np->n_size) {
902 if ((off_t)(lbn + 1) * biosize < np->n_size)
903 bcount = biosize;
904 else
905 bcount = np->n_size - (off_t)lbn * biosize;
906 }
907 bp = nfs_getcacheblk(vp, lbn, bcount, p);
908 if (uio->uio_offset + n > np->n_size) {
909 np->n_size = uio->uio_offset + n;
910 np->n_flag |= NMODIFIED;
911 vnode_pager_setsize(vp, np->n_size);
912 }
913 }
914
915 if (!bp) {
916 error = EINTR;
917 break;
918 }
919
920 /*
921 * Issue a READ if B_CACHE is not set. In special-append
922 * mode, B_CACHE is based on the buffer prior to the write
923 * op and is typically set, avoiding the read. If a read
924 * is required in special append mode, the server will
925 * probably send us a short-read since we extended the file
926 * on our end, resulting in b_resid == 0 and, thusly,
927 * B_CACHE getting set.
928 *
929 * We can also avoid issuing the read if the write covers
930 * the entire buffer. We have to make sure the buffer state
931 * is reasonable in this case since we will not be initiating
932 * I/O. See the comments in kern/vfs_bio.c's getblk() for
933 * more information.
934 *
935 * B_CACHE may also be set due to the buffer being cached
936 * normally.
937 */
938
939 if (on == 0 && n == bcount) {
940 bp->b_flags |= B_CACHE;
941 bp->b_flags &= ~(B_ERROR | B_INVAL);
942 }
943
944 if ((bp->b_flags & B_CACHE) == 0) {
945 bp->b_flags |= B_READ;
946 vfs_busy_pages(bp, 0);
947 error = nfs_doio(bp, cred, p);
948 if (error) {
949 brelse(bp);
950 break;
951 }
952 }
953 if (!bp) {
954 error = EINTR;
955 break;
956 }
957 if (bp->b_wcred == NOCRED) {
958 crhold(cred);
959 bp->b_wcred = cred;
960 }
961 np->n_flag |= NMODIFIED;
962
963 /*
964 * If dirtyend exceeds file size, chop it down. This should
965 * not normally occur but there is an append race where it
966 * might occur XXX, so we log it.
967 *
968 * If the chopping creates a reverse-indexed or degenerate
969 * situation with dirtyoff/end, we 0 both of them.
970 */
971
972 if (bp->b_dirtyend > bcount) {
973 printf("NFS append race @%lx:%d\n",
974 (long)bp->b_blkno * DEV_BSIZE,
975 bp->b_dirtyend - bcount);
976 bp->b_dirtyend = bcount;
977 }
978
979 if (bp->b_dirtyoff >= bp->b_dirtyend)
980 bp->b_dirtyoff = bp->b_dirtyend = 0;
981
982 /*
983 * If the new write will leave a contiguous dirty
984 * area, just update the b_dirtyoff and b_dirtyend,
985 * otherwise force a write rpc of the old dirty area.
986 *
987 * While it is possible to merge discontiguous writes due to
988 * our having a B_CACHE buffer ( and thus valid read data
989 * for the hole), we don't because it could lead to
990 * significant cache coherency problems with multiple clients,
991 * especially if locking is implemented later on.
992 *
993 * as an optimization we could theoretically maintain
994 * a linked list of discontinuous areas, but we would still
995 * have to commit them separately so there isn't much
996 * advantage to it except perhaps a bit of asynchronization.
997 */
998
999 if (bp->b_dirtyend > 0 &&
1000 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1001 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) {
1002 error = EINTR;
1003 break;
1004 }
1005 goto again;
1006 }
1007
1008 /*
1009 * Check for valid write lease and get one as required.
1010 * In case getblk() and/or bwrite() delayed us.
1011 */
1012 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
1013 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
1014 do {
1015 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
1016 } while (error == NQNFS_EXPIRED);
1017 if (error) {
1018 brelse(bp);
1019 break;
1020 }
1021 if (np->n_lrev != np->n_brev ||
1022 (np->n_flag & NQNFSNONCACHE)) {
1023 brelse(bp);
1024 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1025 if (error)
1026 break;
1027 np->n_brev = np->n_lrev;
1028 goto again;
1029 }
1030 }
1031
1032 error = uiomove((char *)bp->b_data + on, n, uio);
1033
1034 /*
1035 * Since this block is being modified, it must be written
1036 * again and not just committed. Since write clustering does
1037 * not work for the stage 1 data write, only the stage 2
1038 * commit rpc, we have to clear B_CLUSTEROK as well.
1039 */
1040 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1041
1042 if (error) {
1043 bp->b_flags |= B_ERROR;
1044 brelse(bp);
1045 break;
1046 }
1047
1048 /*
1049 * Only update dirtyoff/dirtyend if not a degenerate
1050 * condition.
1051 */
1052 if (n) {
1053 if (bp->b_dirtyend > 0) {
1054 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1055 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1056 } else {
1057 bp->b_dirtyoff = on;
1058 bp->b_dirtyend = on + n;
1059 }
1060 vfs_bio_set_validclean(bp, on, n);
1061 }
1062 /*
1063 * If IO_NOWDRAIN then set B_NOWDRAIN (e.g. nfs-backed VN
1064 * filesystem). XXX also use for loopback NFS mounts.
1065 */
1066 if (ioflag & IO_NOWDRAIN)
1067 bp->b_flags |= B_NOWDRAIN;
1068
1069 /*
1070 * If the lease is non-cachable or IO_SYNC do bwrite().
1071 *
1072 * IO_INVAL appears to be unused. The idea appears to be
1073 * to turn off caching in this case. Very odd. XXX
1074 */
1075 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
1076 if (ioflag & IO_INVAL)
1077 bp->b_flags |= B_NOCACHE;
1078 error = VOP_BWRITE(bp->b_vp, bp);
1079 if (error)
1080 break;
1081 if (np->n_flag & NQNFSNONCACHE) {
1082 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1083 if (error)
1084 break;
1085 }
1086 } else if ((n + on) == biosize &&
1087 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
1088 bp->b_flags |= B_ASYNC;
1089 (void)nfs_writebp(bp, 0, 0);
1090 } else {
1091 bdwrite(bp);
1092 }
1093 } while (uio->uio_resid > 0 && n > 0);
1094
1095 if (haverslock)
1096 nfs_rsunlock(np, p);
1097
1098 return (error);
1099 }
1100
1101 /*
1102 * Get an nfs cache block.
1103 *
1104 * Allocate a new one if the block isn't currently in the cache
1105 * and return the block marked busy. If the calling process is
1106 * interrupted by a signal for an interruptible mount point, return
1107 * NULL.
1108 *
1109 * The caller must carefully deal with the possible B_INVAL state of
1110 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1111 * indirectly), so synchronous reads can be issued without worrying about
1112 * the B_INVAL state. We have to be a little more careful when dealing
1113 * with writes (see comments in nfs_write()) when extending a file past
1114 * its EOF.
1115 */
1116 static struct buf *
1117 nfs_getcacheblk(vp, bn, size, p)
1118 struct vnode *vp;
1119 daddr_t bn;
1120 int size;
1121 struct proc *p;
1122 {
1123 register struct buf *bp;
1124 struct mount *mp;
1125 struct nfsmount *nmp;
1126
1127 mp = vp->v_mount;
1128 nmp = VFSTONFS(mp);
1129
1130 if (nmp->nm_flag & NFSMNT_INT) {
1131 bp = getblk(vp, bn, size, PCATCH, 0);
1132 while (bp == (struct buf *)0) {
1133 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
1134 return ((struct buf *)0);
1135 bp = getblk(vp, bn, size, 0, 2 * hz);
1136 }
1137 } else {
1138 bp = getblk(vp, bn, size, 0, 0);
1139 }
1140
1141 if (vp->v_type == VREG) {
1142 int biosize;
1143
1144 biosize = mp->mnt_stat.f_iosize;
1145 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1146 }
1147 return (bp);
1148 }
1149
1150 /*
1151 * Flush and invalidate all dirty buffers. If another process is already
1152 * doing the flush, just wait for completion.
1153 */
1154 int
1155 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
1156 struct vnode *vp;
1157 int flags;
1158 struct ucred *cred;
1159 struct proc *p;
1160 int intrflg;
1161 {
1162 register struct nfsnode *np = VTONFS(vp);
1163 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1164 int error = 0, slpflag, slptimeo;
1165
1166 if (vp->v_flag & VXLOCK) {
1167 return (0);
1168 }
1169
1170 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1171 intrflg = 0;
1172 if (intrflg) {
1173 slpflag = PCATCH;
1174 slptimeo = 2 * hz;
1175 } else {
1176 slpflag = 0;
1177 slptimeo = 0;
1178 }
1179 /*
1180 * First wait for any other process doing a flush to complete.
1181 */
1182 while (np->n_flag & NFLUSHINPROG) {
1183 np->n_flag |= NFLUSHWANT;
1184 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
1185 slptimeo);
1186 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
1187 return (EINTR);
1188 }
1189
1190 /*
1191 * Now, flush as required.
1192 */
1193 np->n_flag |= NFLUSHINPROG;
1194 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
1195 while (error) {
1196 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
1197 np->n_flag &= ~NFLUSHINPROG;
1198 if (np->n_flag & NFLUSHWANT) {
1199 np->n_flag &= ~NFLUSHWANT;
1200 wakeup((caddr_t)&np->n_flag);
1201 }
1202 return (EINTR);
1203 }
1204 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
1205 }
1206 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1207 if (np->n_flag & NFLUSHWANT) {
1208 np->n_flag &= ~NFLUSHWANT;
1209 wakeup((caddr_t)&np->n_flag);
1210 }
1211 return (0);
1212 }
1213
1214 /*
1215 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1216 * This is mainly to avoid queueing async I/O requests when the nfsiods
1217 * are all hung on a dead server.
1218 *
1219 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1220 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1221 */
1222 int
1223 nfs_asyncio(bp, cred, procp)
1224 register struct buf *bp;
1225 struct ucred *cred;
1226 struct proc *procp;
1227 {
1228 struct nfsmount *nmp;
1229 int i;
1230 int gotiod;
1231 int slpflag = 0;
1232 int slptimeo = 0;
1233 int error;
1234
1235 /*
1236 * If no async daemons then return EIO to force caller to run the rpc
1237 * synchronously.
1238 */
1239 if (nfs_numasync == 0)
1240 return (EIO);
1241
1242 nmp = VFSTONFS(bp->b_vp->v_mount);
1243
1244 /*
1245 * Commits are usually short and sweet so lets save some cpu and
1246 * leave the async daemons for more important rpc's (such as reads
1247 * and writes).
1248 */
1249 if ((bp->b_flags & (B_READ|B_NEEDCOMMIT)) == B_NEEDCOMMIT &&
1250 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1251 return(EIO);
1252 }
1253
1254 again:
1255 if (nmp->nm_flag & NFSMNT_INT)
1256 slpflag = PCATCH;
1257 gotiod = FALSE;
1258
1259 /*
1260 * Find a free iod to process this request.
1261 */
1262 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1263 if (nfs_iodwant[i]) {
1264 /*
1265 * Found one, so wake it up and tell it which
1266 * mount to process.
1267 */
1268 NFS_DPF(ASYNCIO,
1269 ("nfs_asyncio: waking iod %d for mount %p\n",
1270 i, nmp));
1271 nfs_iodwant[i] = (struct proc *)0;
1272 nfs_iodmount[i] = nmp;
1273 nmp->nm_bufqiods++;
1274 wakeup((caddr_t)&nfs_iodwant[i]);
1275 gotiod = TRUE;
1276 break;
1277 }
1278
1279 /*
1280 * If none are free, we may already have an iod working on this mount
1281 * point. If so, it will process our request.
1282 */
1283 if (!gotiod) {
1284 if (nmp->nm_bufqiods > 0) {
1285 NFS_DPF(ASYNCIO,
1286 ("nfs_asyncio: %d iods are already processing mount %p\n",
1287 nmp->nm_bufqiods, nmp));
1288 gotiod = TRUE;
1289 }
1290 }
1291
1292 /*
1293 * If we have an iod which can process the request, then queue
1294 * the buffer.
1295 */
1296 if (gotiod) {
1297 /*
1298 * Ensure that the queue never grows too large. We still want
1299 * to asynchronize so we block rather then return EIO.
1300 */
1301 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1302 NFS_DPF(ASYNCIO,
1303 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1304 nmp->nm_bufqwant = TRUE;
1305 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1306 "nfsaio", slptimeo);
1307 if (error) {
1308 if (nfs_sigintr(nmp, NULL, procp))
1309 return (EINTR);
1310 if (slpflag == PCATCH) {
1311 slpflag = 0;
1312 slptimeo = 2 * hz;
1313 }
1314 }
1315 /*
1316 * We might have lost our iod while sleeping,
1317 * so check and loop if nescessary.
1318 */
1319 if (nmp->nm_bufqiods == 0) {
1320 NFS_DPF(ASYNCIO,
1321 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1322 goto again;
1323 }
1324 }
1325
1326 if (bp->b_flags & B_READ) {
1327 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1328 crhold(cred);
1329 bp->b_rcred = cred;
1330 }
1331 } else {
1332 bp->b_flags |= B_WRITEINPROG;
1333 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1334 crhold(cred);
1335 bp->b_wcred = cred;
1336 }
1337 }
1338
1339 BUF_KERNPROC(bp);
1340 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1341 nmp->nm_bufqlen++;
1342 return (0);
1343 }
1344
1345 /*
1346 * All the iods are busy on other mounts, so return EIO to
1347 * force the caller to process the i/o synchronously.
1348 */
1349 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1350 return (EIO);
1351 }
1352
1353 /*
1354 * Do an I/O operation to/from a cache block. This may be called
1355 * synchronously or from an nfsiod.
1356 */
1357 int
1358 nfs_doio(bp, cr, p)
1359 struct buf *bp;
1360 struct ucred *cr;
1361 struct proc *p;
1362 {
1363 struct uio *uiop;
1364 struct vnode *vp;
1365 struct nfsnode *np;
1366 struct nfsmount *nmp;
1367 int error = 0, iomode, must_commit = 0;
1368 struct uio uio;
1369 struct iovec io;
1370
1371 vp = bp->b_vp;
1372 np = VTONFS(vp);
1373 nmp = VFSTONFS(vp->v_mount);
1374 uiop = &uio;
1375 uiop->uio_iov = &io;
1376 uiop->uio_iovcnt = 1;
1377 uiop->uio_segflg = UIO_SYSSPACE;
1378 uiop->uio_procp = p;
1379
1380 /*
1381 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1382 * do this here so we do not have to do it in all the code that
1383 * calls us.
1384 */
1385 bp->b_flags &= ~(B_ERROR | B_INVAL);
1386
1387 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1388
1389 /*
1390 * Historically, paging was done with physio, but no more.
1391 */
1392 if (bp->b_flags & B_PHYS) {
1393 /*
1394 * ...though reading /dev/drum still gets us here.
1395 */
1396 io.iov_len = uiop->uio_resid = bp->b_bcount;
1397 /* mapping was done by vmapbuf() */
1398 io.iov_base = bp->b_data;
1399 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1400 if (bp->b_flags & B_READ) {
1401 uiop->uio_rw = UIO_READ;
1402 nfsstats.read_physios++;
1403 error = nfs_readrpc(vp, uiop, cr);
1404 } else {
1405 int com;
1406
1407 iomode = NFSV3WRITE_DATASYNC;
1408 uiop->uio_rw = UIO_WRITE;
1409 nfsstats.write_physios++;
1410 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
1411 }
1412 if (error) {
1413 bp->b_flags |= B_ERROR;
1414 bp->b_error = error;
1415 }
1416 } else if (bp->b_flags & B_READ) {
1417 io.iov_len = uiop->uio_resid = bp->b_bcount;
1418 io.iov_base = bp->b_data;
1419 uiop->uio_rw = UIO_READ;
1420
1421 switch (vp->v_type) {
1422 case VREG:
1423 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1424 nfsstats.read_bios++;
1425 error = nfs_readrpc(vp, uiop, cr);
1426
1427 if (!error) {
1428 if (uiop->uio_resid) {
1429 /*
1430 * If we had a short read with no error, we must have
1431 * hit a file hole. We should zero-fill the remainder.
1432 * This can also occur if the server hits the file EOF.
1433 *
1434 * Holes used to be able to occur due to pending
1435 * writes, but that is not possible any longer.
1436 */
1437 int nread = bp->b_bcount - uiop->uio_resid;
1438 int left = uiop->uio_resid;
1439
1440 if (left > 0)
1441 bzero((char *)bp->b_data + nread, left);
1442 uiop->uio_resid = 0;
1443 }
1444 }
1445 if (p && (vp->v_flag & VTEXT) &&
1446 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1447 NQNFS_CKINVALID(vp, np, ND_READ) &&
1448 np->n_lrev != np->n_brev) ||
1449 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1450 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1451 uprintf("Process killed due to text file modification\n");
1452 psignal(p, SIGKILL);
1453 PHOLD(p);
1454 }
1455 break;
1456 case VLNK:
1457 uiop->uio_offset = (off_t)0;
1458 nfsstats.readlink_bios++;
1459 error = nfs_readlinkrpc(vp, uiop, cr);
1460 break;
1461 case VDIR:
1462 nfsstats.readdir_bios++;
1463 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1464 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1465 error = nfs_readdirplusrpc(vp, uiop, cr);
1466 if (error == NFSERR_NOTSUPP)
1467 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1468 }
1469 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1470 error = nfs_readdirrpc(vp, uiop, cr);
1471 /*
1472 * end-of-directory sets B_INVAL but does not generate an
1473 * error.
1474 */
1475 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1476 bp->b_flags |= B_INVAL;
1477 break;
1478 default:
1479 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1480 break;
1481 };
1482 if (error) {
1483 bp->b_flags |= B_ERROR;
1484 bp->b_error = error;
1485 }
1486 } else {
1487 /*
1488 * If we only need to commit, try to commit
1489 */
1490 if (bp->b_flags & B_NEEDCOMMIT) {
1491 int retv;
1492 off_t off;
1493
1494 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1495 bp->b_flags |= B_WRITEINPROG;
1496 retv = nfs_commit(
1497 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1498 bp->b_wcred, p);
1499 bp->b_flags &= ~B_WRITEINPROG;
1500 if (retv == 0) {
1501 bp->b_dirtyoff = bp->b_dirtyend = 0;
1502 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1503 bp->b_resid = 0;
1504 biodone(bp);
1505 return (0);
1506 }
1507 if (retv == NFSERR_STALEWRITEVERF) {
1508 nfs_clearcommit(bp->b_vp->v_mount);
1509 }
1510 }
1511
1512 /*
1513 * Setup for actual write
1514 */
1515
1516 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1517 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1518
1519 if (bp->b_dirtyend > bp->b_dirtyoff) {
1520 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1521 - bp->b_dirtyoff;
1522 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1523 + bp->b_dirtyoff;
1524 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1525 uiop->uio_rw = UIO_WRITE;
1526 nfsstats.write_bios++;
1527
1528 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1529 iomode = NFSV3WRITE_UNSTABLE;
1530 else
1531 iomode = NFSV3WRITE_FILESYNC;
1532
1533 bp->b_flags |= B_WRITEINPROG;
1534 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1535
1536 /*
1537 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1538 * to cluster the buffers needing commit. This will allow
1539 * the system to submit a single commit rpc for the whole
1540 * cluster. We can do this even if the buffer is not 100%
1541 * dirty (relative to the NFS blocksize), so we optimize the
1542 * append-to-file-case.
1543 *
1544 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1545 * cleared because write clustering only works for commit
1546 * rpc's, not for the data portion of the write).
1547 */
1548
1549 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1550 bp->b_flags |= B_NEEDCOMMIT;
1551 if (bp->b_dirtyoff == 0
1552 && bp->b_dirtyend == bp->b_bcount)
1553 bp->b_flags |= B_CLUSTEROK;
1554 } else {
1555 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1556 }
1557 bp->b_flags &= ~B_WRITEINPROG;
1558
1559 /*
1560 * For an interrupted write, the buffer is still valid
1561 * and the write hasn't been pushed to the server yet,
1562 * so we can't set B_ERROR and report the interruption
1563 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1564 * is not relevant, so the rpc attempt is essentially
1565 * a noop. For the case of a V3 write rpc not being
1566 * committed to stable storage, the block is still
1567 * dirty and requires either a commit rpc or another
1568 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1569 * the block is reused. This is indicated by setting
1570 * the B_DELWRI and B_NEEDCOMMIT flags.
1571 *
1572 * If the buffer is marked B_PAGING, it does not reside on
1573 * the vp's paging queues so we cannot call bdirty(). The
1574 * bp in this case is not an NFS cache block so we should
1575 * be safe. XXX
1576 */
1577 if (error == EINTR
1578 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1579 int s;
1580
1581 s = splbio();
1582 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1583 if ((bp->b_flags & B_PAGING) == 0) {
1584 bdirty(bp);
1585 bp->b_flags &= ~B_DONE;
1586 }
1587 if (error && (bp->b_flags & B_ASYNC) == 0)
1588 bp->b_flags |= B_EINTR;
1589 splx(s);
1590 } else {
1591 if (error) {
1592 bp->b_flags |= B_ERROR;
1593 bp->b_error = np->n_error = error;
1594 np->n_flag |= NWRITEERR;
1595 }
1596 bp->b_dirtyoff = bp->b_dirtyend = 0;
1597 }
1598 } else {
1599 bp->b_resid = 0;
1600 biodone(bp);
1601 return (0);
1602 }
1603 }
1604 bp->b_resid = uiop->uio_resid;
1605 if (must_commit)
1606 nfs_clearcommit(vp->v_mount);
1607 biodone(bp);
1608 return (error);
1609 }
1610
1611 /*
1612 * Used to aid in handling ftruncate() operations on the NFS client side.
1613 * Truncation creates a number of special problems for NFS. We have to
1614 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1615 * we have to properly handle VM pages or (potentially dirty) buffers
1616 * that straddle the truncation point.
1617 */
1618
1619 int
1620 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct proc *p, u_quad_t nsize)
1621 {
1622 struct nfsnode *np = VTONFS(vp);
1623 u_quad_t tsize = np->n_size;
1624 int biosize = vp->v_mount->mnt_stat.f_iosize;
1625 int error = 0;
1626
1627 np->n_size = nsize;
1628
1629 if (np->n_size < tsize) {
1630 struct buf *bp;
1631 daddr_t lbn;
1632 int bufsize;
1633
1634 /*
1635 * vtruncbuf() doesn't get the buffer overlapping the
1636 * truncation point. We may have a B_DELWRI and/or B_CACHE
1637 * buffer that now needs to be truncated.
1638 */
1639 error = vtruncbuf(vp, cred, p, nsize, biosize);
1640 lbn = nsize / biosize;
1641 bufsize = nsize & (biosize - 1);
1642 bp = nfs_getcacheblk(vp, lbn, bufsize, p);
1643 if (bp->b_dirtyoff > bp->b_bcount)
1644 bp->b_dirtyoff = bp->b_bcount;
1645 if (bp->b_dirtyend > bp->b_bcount)
1646 bp->b_dirtyend = bp->b_bcount;
1647 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1648 brelse(bp);
1649 } else {
1650 vnode_pager_setsize(vp, nsize);
1651 }
1652 return(error);
1653 }
1654
Cache object: f90a15c872d517485541660472cd4e9f
|