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