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