AMD64 - Refactor uio_resid and size_t assumptions.
[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
79e5012e 37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
a1a9228a 38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.45 2008/07/18 00:09:39 dillon 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>
edb90c22 51#include <sys/mbuf.h>
ce71fd38 52#include <sys/msfbuf.h>
984263bc
MD
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
edb90c22 61#include <sys/buf2.h>
165dba55
DR
62#include <sys/thread2.h>
63
1f2de5d4
MD
64#include "rpcv2.h"
65#include "nfsproto.h"
66#include "nfs.h"
67#include "nfsmount.h"
1f2de5d4 68#include "nfsnode.h"
edb90c22
MD
69#include "xdr_subs.h"
70#include "nfsm_subs.h"
71
984263bc 72
54078292
MD
73static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
74 int size, struct thread *td);
b66959e2 75static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
ae8e83e6 76static void nfsiodone_sync(struct bio *bio);
cc7d050e
MD
77static void nfs_readrpc_bio_done(nfsm_info_t info);
78static void nfs_writerpc_bio_done(nfsm_info_t info);
79static void nfs_commitrpc_bio_done(nfsm_info_t info);
984263bc 80
984263bc
MD
81/*
82 * Vnode op for VM getpages.
e851b29e
CP
83 *
84 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
85 * int a_reqpage, vm_ooffset_t a_offset)
984263bc
MD
86 */
87int
e851b29e 88nfs_getpages(struct vop_getpages_args *ap)
984263bc 89{
dadab5e9 90 struct thread *td = curthread; /* XXX */
984263bc
MD
91 int i, error, nextoff, size, toff, count, npages;
92 struct uio uio;
93 struct iovec iov;
6f901429 94 char *kva;
984263bc 95 struct vnode *vp;
984263bc
MD
96 struct nfsmount *nmp;
97 vm_page_t *pages;
8d429613 98 vm_page_t m;
ce71fd38 99 struct msf_buf *msf;
984263bc
MD
100
101 vp = ap->a_vp;
984263bc
MD
102 nmp = VFSTONFS(vp->v_mount);
103 pages = ap->a_m;
104 count = ap->a_count;
105
106 if (vp->v_object == NULL) {
086c1d7e 107 kprintf("nfs_getpages: called with non-merged cache vnode??\n");
984263bc
MD
108 return VM_PAGER_ERROR;
109 }
110
111 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
112 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 113 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
114
115 npages = btoc(count);
116
117 /*
8d429613
MD
118 * NOTE that partially valid pages may occur in cases other
119 * then file EOF, such as when a file is partially written and
120 * ftruncate()-extended to a larger size. It is also possible
121 * for the valid bits to be set on garbage beyond the file EOF and
122 * clear in the area before EOF (e.g. m->valid == 0xfc), which can
123 * occur due to vtruncbuf() and the buffer cache's handling of
124 * pages which 'straddle' buffers or when b_bufsize is not a
125 * multiple of PAGE_SIZE.... the buffer cache cannot normally
126 * clear the extra bits. This kind of situation occurs when you
127 * make a small write() (m->valid == 0x03) and then mmap() and
128 * fault in the buffer(m->valid = 0xFF). When NFS flushes the
129 * buffer (vinvalbuf() m->valid = 0xFC) we are left with a mess.
130 *
131 * This is combined with the possibility that the pages are partially
132 * dirty or that there is a buffer backing the pages that is dirty
133 * (even if m->dirty is 0).
134 *
135 * To solve this problem several hacks have been made: (1) NFS
136 * guarentees that the IO block size is a multiple of PAGE_SIZE and
137 * (2) The buffer cache, when invalidating an NFS buffer, will
138 * disregard the buffer's fragmentory b_bufsize and invalidate
139 * the whole page rather then just the piece the buffer owns.
140 *
141 * This allows us to assume that a partially valid page found here
142 * is fully valid (vm_fault will zero'd out areas of the page not
143 * marked as valid).
984263bc 144 */
8d429613
MD
145 m = pages[ap->a_reqpage];
146 if (m->valid != 0) {
147 for (i = 0; i < npages; ++i) {
148 if (i != ap->a_reqpage)
149 vnode_pager_freepage(pages[i]);
984263bc 150 }
8d429613 151 return(0);
984263bc
MD
152 }
153
154 /*
ce71fd38 155 * Use an MSF_BUF as a medium to retrieve data from the pages.
984263bc 156 */
a5d36a1d
MD
157 msf_map_pagelist(&msf, pages, npages, 0);
158 KKASSERT(msf);
ce71fd38 159 kva = msf_buf_kva(msf);
984263bc 160
6f901429 161 iov.iov_base = kva;
984263bc
MD
162 iov.iov_len = count;
163 uio.uio_iov = &iov;
164 uio.uio_iovcnt = 1;
165 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
166 uio.uio_resid = count;
167 uio.uio_segflg = UIO_SYSSPACE;
168 uio.uio_rw = UIO_READ;
dadab5e9 169 uio.uio_td = td;
984263bc 170
edb90c22 171 error = nfs_readrpc_uio(vp, &uio);
ce71fd38 172 msf_buf_free(msf);
984263bc 173
e54488bb 174 if (error && ((int)uio.uio_resid == count)) {
086c1d7e 175 kprintf("nfs_getpages: error %d\n", error);
984263bc
MD
176 for (i = 0; i < npages; ++i) {
177 if (i != ap->a_reqpage)
178 vnode_pager_freepage(pages[i]);
179 }
180 return VM_PAGER_ERROR;
181 }
182
183 /*
184 * Calculate the number of bytes read and validate only that number
185 * of bytes. Note that due to pending writes, size may be 0. This
186 * does not mean that the remaining data is invalid!
187 */
188
e54488bb 189 size = count - (int)uio.uio_resid;
984263bc
MD
190
191 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
984263bc
MD
192 nextoff = toff + PAGE_SIZE;
193 m = pages[i];
194
195 m->flags &= ~PG_ZERO;
196
197 if (nextoff <= size) {
198 /*
199 * Read operation filled an entire page
200 */
201 m->valid = VM_PAGE_BITS_ALL;
202 vm_page_undirty(m);
203 } else if (size > toff) {
204 /*
205 * Read operation filled a partial page.
206 */
207 m->valid = 0;
208 vm_page_set_validclean(m, 0, size - toff);
209 /* handled by vm_fault now */
210 /* vm_page_zero_invalid(m, TRUE); */
211 } else {
212 /*
213 * Read operation was short. If no error occured
214 * we may have hit a zero-fill section. We simply
215 * leave valid set to 0.
216 */
217 ;
218 }
219 if (i != ap->a_reqpage) {
220 /*
221 * Whether or not to leave the page activated is up in
222 * the air, but we should put the page on a page queue
223 * somewhere (it already is in the object). Result:
224 * It appears that emperical results show that
225 * deactivating pages is best.
226 */
227
228 /*
229 * Just in case someone was asking for this page we
230 * now tell them that it is ok to use.
231 */
232 if (!error) {
233 if (m->flags & PG_WANTED)
234 vm_page_activate(m);
235 else
236 vm_page_deactivate(m);
237 vm_page_wakeup(m);
238 } else {
239 vnode_pager_freepage(m);
240 }
241 }
242 }
243 return 0;
244}
245
246/*
247 * Vnode op for VM putpages.
e851b29e
CP
248 *
249 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
250 * int *a_rtvals, vm_ooffset_t a_offset)
984263bc
MD
251 */
252int
e851b29e 253nfs_putpages(struct vop_putpages_args *ap)
984263bc 254{
dadab5e9 255 struct thread *td = curthread;
984263bc
MD
256 struct uio uio;
257 struct iovec iov;
6f901429 258 char *kva;
984263bc
MD
259 int iomode, must_commit, i, error, npages, count;
260 off_t offset;
261 int *rtvals;
262 struct vnode *vp;
984263bc
MD
263 struct nfsmount *nmp;
264 struct nfsnode *np;
265 vm_page_t *pages;
ce71fd38 266 struct msf_buf *msf;
984263bc
MD
267
268 vp = ap->a_vp;
269 np = VTONFS(vp);
984263bc
MD
270 nmp = VFSTONFS(vp->v_mount);
271 pages = ap->a_m;
272 count = ap->a_count;
273 rtvals = ap->a_rtvals;
274 npages = btoc(count);
275 offset = IDX_TO_OFF(pages[0]->pindex);
276
277 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
278 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 279 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
280
281 for (i = 0; i < npages; i++) {
282 rtvals[i] = VM_PAGER_AGAIN;
283 }
284
285 /*
286 * When putting pages, do not extend file past EOF.
287 */
288
289 if (offset + count > np->n_size) {
290 count = np->n_size - offset;
291 if (count < 0)
292 count = 0;
293 }
294
295 /*
ce71fd38 296 * Use an MSF_BUF as a medium to retrieve data from the pages.
984263bc 297 */
a5d36a1d
MD
298 msf_map_pagelist(&msf, pages, npages, 0);
299 KKASSERT(msf);
ce71fd38 300 kva = msf_buf_kva(msf);
984263bc 301
6f901429 302 iov.iov_base = kva;
984263bc
MD
303 iov.iov_len = count;
304 uio.uio_iov = &iov;
305 uio.uio_iovcnt = 1;
306 uio.uio_offset = offset;
e54488bb 307 uio.uio_resid = (size_t)count;
984263bc
MD
308 uio.uio_segflg = UIO_SYSSPACE;
309 uio.uio_rw = UIO_WRITE;
dadab5e9 310 uio.uio_td = td;
984263bc
MD
311
312 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
313 iomode = NFSV3WRITE_UNSTABLE;
314 else
315 iomode = NFSV3WRITE_FILESYNC;
316
cc7d050e 317 error = nfs_writerpc_uio(vp, &uio, &iomode, &must_commit);
984263bc 318
ce71fd38 319 msf_buf_free(msf);
984263bc
MD
320
321 if (!error) {
e54488bb 322 int nwritten = round_page(count - (int)uio.uio_resid) / PAGE_SIZE;
984263bc
MD
323 for (i = 0; i < nwritten; i++) {
324 rtvals[i] = VM_PAGER_OK;
325 vm_page_undirty(pages[i]);
326 }
327 if (must_commit)
328 nfs_clearcommit(vp->v_mount);
329 }
330 return rtvals[0];
331}
332
333/*
334 * Vnode op for read using bio
335 */
336int
3b568787 337nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
984263bc 338{
dadab5e9
MD
339 struct nfsnode *np = VTONFS(vp);
340 int biosize, i;
984263bc
MD
341 struct buf *bp = 0, *rabp;
342 struct vattr vattr;
dadab5e9 343 struct thread *td;
984263bc
MD
344 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
345 daddr_t lbn, rabn;
54078292
MD
346 off_t raoffset;
347 off_t loffset;
984263bc
MD
348 int bcount;
349 int seqcount;
350 int nra, error = 0, n = 0, on = 0;
351
352#ifdef DIAGNOSTIC
353 if (uio->uio_rw != UIO_READ)
354 panic("nfs_read mode");
355#endif
356 if (uio->uio_resid == 0)
357 return (0);
358 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
359 return (EINVAL);
dadab5e9 360 td = uio->uio_td;
984263bc
MD
361
362 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
363 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 364 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
365 if (vp->v_type != VDIR &&
366 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
367 return (EFBIG);
368 biosize = vp->v_mount->mnt_stat.f_iosize;
369 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
5a9187cb 370
984263bc
MD
371 /*
372 * For nfs, cache consistency can only be maintained approximately.
373 * Although RFC1094 does not specify the criteria, the following is
374 * believed to be compatible with the reference port.
5a9187cb 375 *
5a9187cb
MD
376 * NFS: If local changes have been made and this is a
377 * directory, the directory must be invalidated and
378 * the attribute cache must be cleared.
379 *
380 * GETATTR is called to synchronize the file size.
381 *
382 * If remote changes are detected local data is flushed
383 * and the cache is invalidated.
384 *
5a9187cb
MD
385 * NOTE: In the normal case the attribute cache is not
386 * cleared which means GETATTR may use cached data and
387 * not immediately detect changes made on the server.
984263bc 388 */
e07fef60
MD
389 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
390 nfs_invaldir(vp);
87de5057 391 error = nfs_vinvalbuf(vp, V_SAVE, 1);
e07fef60
MD
392 if (error)
393 return (error);
394 np->n_attrstamp = 0;
395 }
87de5057 396 error = VOP_GETATTR(vp, &vattr);
e07fef60
MD
397 if (error)
398 return (error);
399 if (np->n_flag & NRMODIFIED) {
400 if (vp->v_type == VDIR)
5a9187cb 401 nfs_invaldir(vp);
87de5057 402 error = nfs_vinvalbuf(vp, V_SAVE, 1);
5a9187cb
MD
403 if (error)
404 return (error);
e07fef60 405 np->n_flag &= ~NRMODIFIED;
984263bc
MD
406 }
407 do {
e07fef60 408 if (np->n_flag & NDONTCACHE) {
984263bc
MD
409 switch (vp->v_type) {
410 case VREG:
edb90c22 411 return (nfs_readrpc_uio(vp, uio));
984263bc 412 case VLNK:
cc7d050e 413 return (nfs_readlinkrpc_uio(vp, uio));
984263bc
MD
414 case VDIR:
415 break;
416 default:
086c1d7e 417 kprintf(" NDONTCACHE: type %x unexpected\n", vp->v_type);
e07fef60 418 break;
984263bc
MD
419 };
420 }
421 switch (vp->v_type) {
422 case VREG:
423 nfsstats.biocache_reads++;
424 lbn = uio->uio_offset / biosize;
425 on = uio->uio_offset & (biosize - 1);
54078292 426 loffset = (off_t)lbn * biosize;
984263bc
MD
427
428 /*
429 * Start the read ahead(s), as required.
430 */
edb90c22 431 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
984263bc
MD
432 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
433 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
434 rabn = lbn + 1 + nra;
54078292 435 raoffset = (off_t)rabn * biosize;
b1c20cfa 436 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
54078292 437 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
984263bc
MD
438 if (!rabp)
439 return (EINTR);
440 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
10f3fee5
MD
441 rabp->b_cmd = BUF_CMD_READ;
442 vfs_busy_pages(vp, rabp);
edb90c22 443 nfs_asyncio(vp, &rabp->b_bio2);
984263bc
MD
444 } else {
445 brelse(rabp);
446 }
447 }
448 }
449 }
450
451 /*
452 * Obtain the buffer cache block. Figure out the buffer size
453 * when we are at EOF. If we are modifying the size of the
454 * buffer based on an EOF condition we need to hold
455 * nfs_rslock() through obtaining the buffer to prevent
456 * a potential writer-appender from messing with n_size.
457 * Otherwise we may accidently truncate the buffer and
458 * lose dirty data.
459 *
460 * Note that bcount is *not* DEV_BSIZE aligned.
461 */
462
463again:
464 bcount = biosize;
54078292 465 if (loffset >= np->n_size) {
984263bc 466 bcount = 0;
54078292
MD
467 } else if (loffset + biosize > np->n_size) {
468 bcount = np->n_size - loffset;
984263bc
MD
469 }
470 if (bcount != biosize) {
2313ec23 471 switch(nfs_rslock(np)) {
984263bc
MD
472 case ENOLCK:
473 goto again;
474 /* not reached */
475 case EINTR:
476 case ERESTART:
477 return(EINTR);
478 /* not reached */
479 default:
480 break;
481 }
482 }
483
54078292 484 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
485
486 if (bcount != biosize)
2313ec23 487 nfs_rsunlock(np);
984263bc
MD
488 if (!bp)
489 return (EINTR);
490
491 /*
492 * If B_CACHE is not set, we must issue the read. If this
493 * fails, we return an error.
494 */
495
496 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 497 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
498 bp->b_bio2.bio_done = nfsiodone_sync;
499 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 500 vfs_busy_pages(vp, bp);
cc7d050e 501 error = nfs_doio(vp, &bp->b_bio2, td);
984263bc
MD
502 if (error) {
503 brelse(bp);
504 return (error);
505 }
506 }
507
508 /*
509 * on is the offset into the current bp. Figure out how many
510 * bytes we can copy out of the bp. Note that bcount is
511 * NOT DEV_BSIZE aligned.
512 *
513 * Then figure out how many bytes we can copy into the uio.
514 */
515
516 n = 0;
517 if (on < bcount)
e54488bb 518 n = (int)szmin((unsigned)(bcount - on), uio->uio_resid);
984263bc
MD
519 break;
520 case VLNK:
ded0173f 521 biosize = min(NFS_MAXPATHLEN, np->n_size);
984263bc 522 nfsstats.biocache_readlinks++;
ded0173f 523 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
81b5c339 524 if (bp == NULL)
984263bc
MD
525 return (EINTR);
526 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 527 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
528 bp->b_bio2.bio_done = nfsiodone_sync;
529 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 530 vfs_busy_pages(vp, bp);
cc7d050e 531 error = nfs_doio(vp, &bp->b_bio2, td);
984263bc 532 if (error) {
a1a9228a 533 bp->b_flags |= B_ERROR | B_INVAL;
984263bc
MD
534 brelse(bp);
535 return (error);
536 }
537 }
e54488bb 538 n = (int)szmin(uio->uio_resid, bp->b_bcount - bp->b_resid);
984263bc
MD
539 on = 0;
540 break;
541 case VDIR:
542 nfsstats.biocache_readdirs++;
543 if (np->n_direofoffset
544 && uio->uio_offset >= np->n_direofoffset) {
545 return (0);
546 }
547 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
548 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
54078292
MD
549 loffset = uio->uio_offset - on;
550 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
81b5c339 551 if (bp == NULL)
984263bc 552 return (EINTR);
b66959e2 553
984263bc 554 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 555 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
556 bp->b_bio2.bio_done = nfsiodone_sync;
557 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 558 vfs_busy_pages(vp, bp);
cc7d050e
MD
559 error = nfs_doio(vp, &bp->b_bio2, td);
560 if (error)
984263bc 561 brelse(bp);
984263bc 562 while (error == NFSERR_BAD_COOKIE) {
086c1d7e 563 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
984263bc 564 nfs_invaldir(vp);
87de5057 565 error = nfs_vinvalbuf(vp, 0, 1);
984263bc
MD
566 /*
567 * Yuck! The directory has been modified on the
568 * server. The only way to get the block is by
569 * reading from the beginning to get all the
570 * offset cookies.
571 *
572 * Leave the last bp intact unless there is an error.
573 * Loop back up to the while if the error is another
574 * NFSERR_BAD_COOKIE (double yuch!).
575 */
576 for (i = 0; i <= lbn && !error; i++) {
577 if (np->n_direofoffset
578 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
579 return (0);
54078292
MD
580 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
581 NFS_DIRBLKSIZ, td);
984263bc
MD
582 if (!bp)
583 return (EINTR);
584 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 585 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
586 bp->b_bio2.bio_done = nfsiodone_sync;
587 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 588 vfs_busy_pages(vp, bp);
cc7d050e 589 error = nfs_doio(vp, &bp->b_bio2, td);
984263bc
MD
590 /*
591 * no error + B_INVAL == directory EOF,
592 * use the block.
593 */
594 if (error == 0 && (bp->b_flags & B_INVAL))
595 break;
596 }
597 /*
598 * An error will throw away the block and the
599 * for loop will break out. If no error and this
600 * is not the block we want, we throw away the
601 * block and go for the next one via the for loop.
602 */
603 if (error || i < lbn)
604 brelse(bp);
605 }
606 }
607 /*
608 * The above while is repeated if we hit another cookie
609 * error. If we hit an error and it wasn't a cookie error,
610 * we give up.
611 */
612 if (error)
613 return (error);
614 }
615
616 /*
617 * If not eof and read aheads are enabled, start one.
618 * (You need the current block first, so that you have the
619 * directory offset cookie of the next block.)
620 */
edb90c22 621 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
984263bc
MD
622 (bp->b_flags & B_INVAL) == 0 &&
623 (np->n_direofoffset == 0 ||
54078292 624 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
e07fef60 625 (np->n_flag & NDONTCACHE) == 0 &&
b1c20cfa
MD
626 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
627 ) {
54078292
MD
628 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
629 NFS_DIRBLKSIZ, td);
984263bc
MD
630 if (rabp) {
631 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
10f3fee5
MD
632 rabp->b_cmd = BUF_CMD_READ;
633 vfs_busy_pages(vp, rabp);
edb90c22 634 nfs_asyncio(vp, &rabp->b_bio2);
984263bc
MD
635 } else {
636 brelse(rabp);
637 }
638 }
639 }
640 /*
641 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
642 * chopped for the EOF condition, we cannot tell how large
643 * NFS directories are going to be until we hit EOF. So
644 * an NFS directory buffer is *not* chopped to its EOF. Now,
645 * it just so happens that b_resid will effectively chop it
646 * to EOF. *BUT* this information is lost if the buffer goes
647 * away and is reconstituted into a B_CACHE state ( due to
648 * being VMIO ) later. So we keep track of the directory eof
649 * in np->n_direofoffset and chop it off as an extra step
650 * right here.
651 */
e54488bb
MD
652 n = (int)szmin(uio->uio_resid,
653 NFS_DIRBLKSIZ - bp->b_resid - on);
984263bc
MD
654 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
655 n = np->n_direofoffset - uio->uio_offset;
656 break;
657 default:
086c1d7e 658 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
984263bc
MD
659 break;
660 };
661
984263bc
MD
662 switch (vp->v_type) {
663 case VREG:
01f31ab3
JS
664 if (n > 0)
665 error = uiomove(bp->b_data + on, (int)n, uio);
984263bc
MD
666 break;
667 case VLNK:
01f31ab3
JS
668 if (n > 0)
669 error = uiomove(bp->b_data + on, (int)n, uio);
984263bc
MD
670 n = 0;
671 break;
672 case VDIR:
01f31ab3
JS
673 if (n > 0) {
674 off_t old_off = uio->uio_offset;
675 caddr_t cpos, epos;
676 struct nfs_dirent *dp;
677
b66959e2
MD
678 /*
679 * We are casting cpos to nfs_dirent, it must be
680 * int-aligned.
681 */
682 if (on & 3) {
683 error = EINVAL;
684 break;
685 }
686
01f31ab3
JS
687 cpos = bp->b_data + on;
688 epos = bp->b_data + on + n;
689 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
690 dp = (struct nfs_dirent *)cpos;
b66959e2
MD
691 error = nfs_check_dirent(dp, (int)(epos - cpos));
692 if (error)
693 break;
01f31ab3 694 if (vop_write_dirent(&error, uio, dp->nfs_ino,
b66959e2 695 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
01f31ab3 696 break;
b66959e2 697 }
01f31ab3
JS
698 cpos += dp->nfs_reclen;
699 }
700 n = 0;
701 if (error == 0)
702 uio->uio_offset = old_off + cpos - bp->b_data - on;
703 }
984263bc
MD
704 /*
705 * Invalidate buffer if caching is disabled, forcing a
706 * re-read from the remote later.
707 */
e07fef60 708 if (np->n_flag & NDONTCACHE)
984263bc
MD
709 bp->b_flags |= B_INVAL;
710 break;
711 default:
086c1d7e 712 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
984263bc
MD
713 }
714 brelse(bp);
715 } while (error == 0 && uio->uio_resid > 0 && n > 0);
716 return (error);
717}
718
719/*
b66959e2
MD
720 * Userland can supply any 'seek' offset when reading a NFS directory.
721 * Validate the structure so we don't panic the kernel. Note that
722 * the element name is nul terminated and the nul is not included
723 * in nfs_namlen.
724 */
725static
726int
727nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
728{
729 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
730
731 if (nfs_name_off >= maxlen)
732 return (EINVAL);
733 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
734 return (EINVAL);
735 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
736 return (EINVAL);
737 if (dp->nfs_reclen & 3)
738 return (EINVAL);
739 return (0);
740}
741
742/*
984263bc 743 * Vnode op for write using bio
e851b29e
CP
744 *
745 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
746 * struct ucred *a_cred)
984263bc
MD
747 */
748int
e851b29e 749nfs_write(struct vop_write_args *ap)
984263bc 750{
984263bc 751 struct uio *uio = ap->a_uio;
dadab5e9 752 struct thread *td = uio->uio_td;
984263bc
MD
753 struct vnode *vp = ap->a_vp;
754 struct nfsnode *np = VTONFS(vp);
984263bc
MD
755 int ioflag = ap->a_ioflag;
756 struct buf *bp;
757 struct vattr vattr;
758 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
759 daddr_t lbn;
54078292 760 off_t loffset;
984263bc
MD
761 int n, on, error = 0, iomode, must_commit;
762 int haverslock = 0;
81b5c339
MD
763 int bcount;
764 int biosize;
984263bc
MD
765
766#ifdef DIAGNOSTIC
767 if (uio->uio_rw != UIO_WRITE)
768 panic("nfs_write mode");
7b95be2a 769 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
984263bc
MD
770 panic("nfs_write proc");
771#endif
772 if (vp->v_type != VREG)
773 return (EIO);
774 if (np->n_flag & NWRITEERR) {
775 np->n_flag &= ~NWRITEERR;
776 return (np->n_error);
777 }
778 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
779 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 780 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
781
782 /*
783 * Synchronously flush pending buffers if we are in synchronous
784 * mode or if we are appending.
785 */
786 if (ioflag & (IO_APPEND | IO_SYNC)) {
5a9187cb 787 if (np->n_flag & NLMODIFIED) {
984263bc 788 np->n_attrstamp = 0;
5a9187cb 789 error = nfs_flush(vp, MNT_WAIT, td, 0);
87de5057 790 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
984263bc
MD
791 if (error)
792 return (error);
793 }
794 }
795
796 /*
797 * If IO_APPEND then load uio_offset. We restart here if we cannot
798 * get the append lock.
799 */
800restart:
801 if (ioflag & IO_APPEND) {
802 np->n_attrstamp = 0;
87de5057 803 error = VOP_GETATTR(vp, &vattr);
984263bc
MD
804 if (error)
805 return (error);
806 uio->uio_offset = np->n_size;
807 }
808
809 if (uio->uio_offset < 0)
810 return (EINVAL);
811 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
812 return (EFBIG);
813 if (uio->uio_resid == 0)
814 return (0);
815
816 /*
817 * We need to obtain the rslock if we intend to modify np->n_size
818 * in order to guarentee the append point with multiple contending
819 * writers, to guarentee that no other appenders modify n_size
820 * while we are trying to obtain a truncated buffer (i.e. to avoid
821 * accidently truncating data written by another appender due to
822 * the race), and to ensure that the buffer is populated prior to
823 * our extending of the file. We hold rslock through the entire
824 * operation.
825 *
826 * Note that we do not synchronize the case where someone truncates
827 * the file while we are appending to it because attempting to lock
828 * this case may deadlock other parts of the system unexpectedly.
829 */
830 if ((ioflag & IO_APPEND) ||
831 uio->uio_offset + uio->uio_resid > np->n_size) {
2313ec23 832 switch(nfs_rslock(np)) {
984263bc
MD
833 case ENOLCK:
834 goto restart;
835 /* not reached */
836 case EINTR:
837 case ERESTART:
838 return(EINTR);
839 /* not reached */
840 default:
841 break;
842 }
843 haverslock = 1;
844 }
845
846 /*
847 * Maybe this should be above the vnode op call, but so long as
848 * file servers have no limits, i don't think it matters
849 */
dadab5e9
MD
850 if (td->td_proc && uio->uio_offset + uio->uio_resid >
851 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
7278a846 852 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
984263bc 853 if (haverslock)
2313ec23 854 nfs_rsunlock(np);
984263bc
MD
855 return (EFBIG);
856 }
857
858 biosize = vp->v_mount->mnt_stat.f_iosize;
859
860 do {
e07fef60 861 if ((np->n_flag & NDONTCACHE) && uio->uio_iovcnt == 1) {
984263bc 862 iomode = NFSV3WRITE_FILESYNC;
cc7d050e 863 error = nfs_writerpc_uio(vp, uio, &iomode, &must_commit);
984263bc
MD
864 if (must_commit)
865 nfs_clearcommit(vp->v_mount);
866 break;
867 }
868 nfsstats.biocache_writes++;
869 lbn = uio->uio_offset / biosize;
870 on = uio->uio_offset & (biosize-1);
54078292 871 loffset = uio->uio_offset - on;
e54488bb 872 n = (int)szmin((unsigned)(biosize - on), uio->uio_resid);
984263bc
MD
873again:
874 /*
875 * Handle direct append and file extension cases, calculate
876 * unaligned buffer size.
877 */
878
879 if (uio->uio_offset == np->n_size && n) {
880 /*
881 * Get the buffer (in its pre-append state to maintain
882 * B_CACHE if it was previously set). Resize the
883 * nfsnode after we have locked the buffer to prevent
884 * readers from reading garbage.
885 */
886 bcount = on;
54078292 887 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
888
889 if (bp != NULL) {
890 long save;
891
892 np->n_size = uio->uio_offset + n;
5a9187cb 893 np->n_flag |= NLMODIFIED;
984263bc
MD
894 vnode_pager_setsize(vp, np->n_size);
895
896 save = bp->b_flags & B_CACHE;
897 bcount += n;
898 allocbuf(bp, bcount);
899 bp->b_flags |= save;
900 }
901 } else {
902 /*
903 * Obtain the locked cache block first, and then
904 * adjust the file's size as appropriate.
905 */
906 bcount = on + n;
54078292
MD
907 if (loffset + bcount < np->n_size) {
908 if (loffset + biosize < np->n_size)
984263bc
MD
909 bcount = biosize;
910 else
54078292 911 bcount = np->n_size - loffset;
984263bc 912 }
54078292 913 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
914 if (uio->uio_offset + n > np->n_size) {
915 np->n_size = uio->uio_offset + n;
5a9187cb 916 np->n_flag |= NLMODIFIED;
984263bc
MD
917 vnode_pager_setsize(vp, np->n_size);
918 }
919 }
920
81b5c339 921 if (bp == NULL) {
984263bc
MD
922 error = EINTR;
923 break;
924 }
925
926 /*
927 * Issue a READ if B_CACHE is not set. In special-append
928 * mode, B_CACHE is based on the buffer prior to the write
929 * op and is typically set, avoiding the read. If a read
930 * is required in special append mode, the server will
931 * probably send us a short-read since we extended the file
932 * on our end, resulting in b_resid == 0 and, thusly,
933 * B_CACHE getting set.
934 *
935 * We can also avoid issuing the read if the write covers
936 * the entire buffer. We have to make sure the buffer state
937 * is reasonable in this case since we will not be initiating
938 * I/O. See the comments in kern/vfs_bio.c's getblk() for
939 * more information.
940 *
941 * B_CACHE may also be set due to the buffer being cached
942 * normally.
8aa7625b
MD
943 *
944 * When doing a UIO_NOCOPY write the buffer is not
945 * overwritten and we cannot just set B_CACHE unconditionally
946 * for full-block writes.
984263bc
MD
947 */
948
8aa7625b 949 if (on == 0 && n == bcount && uio->uio_segflg != UIO_NOCOPY) {
984263bc
MD
950 bp->b_flags |= B_CACHE;
951 bp->b_flags &= ~(B_ERROR | B_INVAL);
952 }
953
954 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 955 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
956 bp->b_bio2.bio_done = nfsiodone_sync;
957 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 958 vfs_busy_pages(vp, bp);
cc7d050e 959 error = nfs_doio(vp, &bp->b_bio2, td);
984263bc
MD
960 if (error) {
961 brelse(bp);
962 break;
963 }
964 }
965 if (!bp) {
966 error = EINTR;
967 break;
968 }
5a9187cb 969 np->n_flag |= NLMODIFIED;
984263bc
MD
970
971 /*
972 * If dirtyend exceeds file size, chop it down. This should
973 * not normally occur but there is an append race where it
974 * might occur XXX, so we log it.
975 *
976 * If the chopping creates a reverse-indexed or degenerate
977 * situation with dirtyoff/end, we 0 both of them.
978 */
979
980 if (bp->b_dirtyend > bcount) {
086c1d7e 981 kprintf("NFS append race @%08llx:%d\n",
973c11b9 982 (long long)bp->b_bio2.bio_offset,
984263bc
MD
983 bp->b_dirtyend - bcount);
984 bp->b_dirtyend = bcount;
985 }
986
987 if (bp->b_dirtyoff >= bp->b_dirtyend)
988 bp->b_dirtyoff = bp->b_dirtyend = 0;
989
990 /*
991 * If the new write will leave a contiguous dirty
992 * area, just update the b_dirtyoff and b_dirtyend,
993 * otherwise force a write rpc of the old dirty area.
994 *
995 * While it is possible to merge discontiguous writes due to
996 * our having a B_CACHE buffer ( and thus valid read data
997 * for the hole), we don't because it could lead to
998 * significant cache coherency problems with multiple clients,
999 * especially if locking is implemented later on.
1000 *
1001 * as an optimization we could theoretically maintain
1002 * a linked list of discontinuous areas, but we would still
1003 * have to commit them separately so there isn't much
1004 * advantage to it except perhaps a bit of asynchronization.
1005 */
1006
1007 if (bp->b_dirtyend > 0 &&
1008 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
62cfda27 1009 if (bwrite(bp) == EINTR) {
984263bc
MD
1010 error = EINTR;
1011 break;
1012 }
1013 goto again;
1014 }
1015
984263bc
MD
1016 error = uiomove((char *)bp->b_data + on, n, uio);
1017
1018 /*
1019 * Since this block is being modified, it must be written
1020 * again and not just committed. Since write clustering does
1021 * not work for the stage 1 data write, only the stage 2
1022 * commit rpc, we have to clear B_CLUSTEROK as well.
1023 */
1024 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1025
1026 if (error) {
1027 bp->b_flags |= B_ERROR;
1028 brelse(bp);
1029 break;
1030 }
1031
1032 /*
1033 * Only update dirtyoff/dirtyend if not a degenerate
1034 * condition.
1035 */
1036 if (n) {
1037 if (bp->b_dirtyend > 0) {
1038 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1039 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1040 } else {
1041 bp->b_dirtyoff = on;
1042 bp->b_dirtyend = on + n;
1043 }
1044 vfs_bio_set_validclean(bp, on, n);
1045 }
984263bc
MD
1046
1047 /*
1048 * If the lease is non-cachable or IO_SYNC do bwrite().
1049 *
1050 * IO_INVAL appears to be unused. The idea appears to be
1051 * to turn off caching in this case. Very odd. XXX
a482a28a
MD
1052 *
1053 * If nfs_async is set bawrite() will use an unstable write
1054 * (build dirty bufs on the server), so we might as well
1055 * push it out with bawrite(). If nfs_async is not set we
1056 * use bdwrite() to cache dirty bufs on the client.
984263bc 1057 */
e07fef60 1058 if ((np->n_flag & NDONTCACHE) || (ioflag & IO_SYNC)) {
984263bc
MD
1059 if (ioflag & IO_INVAL)
1060 bp->b_flags |= B_NOCACHE;
62cfda27 1061 error = bwrite(bp);
984263bc
MD
1062 if (error)
1063 break;
e07fef60 1064 if (np->n_flag & NDONTCACHE) {
87de5057 1065 error = nfs_vinvalbuf(vp, V_SAVE, 1);
984263bc
MD
1066 if (error)
1067 break;
1068 }
a482a28a
MD
1069 } else if ((n + on) == biosize && nfs_async) {
1070 bawrite(bp);
984263bc
MD
1071 } else {
1072 bdwrite(bp);
1073 }
1074 } while (uio->uio_resid > 0 && n > 0);
1075
1076 if (haverslock)
2313ec23 1077 nfs_rsunlock(np);
984263bc
MD
1078
1079 return (error);
1080}
1081
1082/*
1083 * Get an nfs cache block.
1084 *
1085 * Allocate a new one if the block isn't currently in the cache
1086 * and return the block marked busy. If the calling process is
1087 * interrupted by a signal for an interruptible mount point, return
1088 * NULL.
1089 *
1090 * The caller must carefully deal with the possible B_INVAL state of
edb90c22 1091 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
984263bc
MD
1092 * indirectly), so synchronous reads can be issued without worrying about
1093 * the B_INVAL state. We have to be a little more careful when dealing
1094 * with writes (see comments in nfs_write()) when extending a file past
1095 * its EOF.
1096 */
1097static struct buf *
54078292 1098nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
984263bc 1099{
40393ded 1100 struct buf *bp;
984263bc
MD
1101 struct mount *mp;
1102 struct nfsmount *nmp;
1103
1104 mp = vp->v_mount;
1105 nmp = VFSTONFS(mp);
1106
1107 if (nmp->nm_flag & NFSMNT_INT) {
4b958e7b 1108 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
81b5c339 1109 while (bp == NULL) {
60233e58 1110 if (nfs_sigintr(nmp, NULL, td))
81b5c339 1111 return (NULL);
54078292 1112 bp = getblk(vp, loffset, size, 0, 2 * hz);
984263bc
MD
1113 }
1114 } else {
54078292 1115 bp = getblk(vp, loffset, size, 0, 0);
984263bc
MD
1116 }
1117
81b5c339 1118 /*
54078292
MD
1119 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
1120 * now, no translation is necessary.
81b5c339 1121 */
54078292 1122 bp->b_bio2.bio_offset = loffset;
984263bc
MD
1123 return (bp);
1124}
1125
1126/*
1127 * Flush and invalidate all dirty buffers. If another process is already
1128 * doing the flush, just wait for completion.
1129 */
1130int
87de5057 1131nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
984263bc 1132{
40393ded 1133 struct nfsnode *np = VTONFS(vp);
984263bc
MD
1134 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1135 int error = 0, slpflag, slptimeo;
87de5057 1136 thread_t td = curthread;
984263bc 1137
5fd012e0 1138 if (vp->v_flag & VRECLAIMED)
984263bc 1139 return (0);
984263bc
MD
1140
1141 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1142 intrflg = 0;
1143 if (intrflg) {
1144 slpflag = PCATCH;
1145 slptimeo = 2 * hz;
1146 } else {
1147 slpflag = 0;
1148 slptimeo = 0;
1149 }
1150 /*
1151 * First wait for any other process doing a flush to complete.
1152 */
1153 while (np->n_flag & NFLUSHINPROG) {
1154 np->n_flag |= NFLUSHWANT;
377d4740 1155 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
87de5057 1156 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
984263bc
MD
1157 return (EINTR);
1158 }
1159
1160 /*
1161 * Now, flush as required.
1162 */
1163 np->n_flag |= NFLUSHINPROG;
87de5057 1164 error = vinvalbuf(vp, flags, slpflag, 0);
984263bc 1165 while (error) {
87de5057 1166 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
984263bc
MD
1167 np->n_flag &= ~NFLUSHINPROG;
1168 if (np->n_flag & NFLUSHWANT) {
1169 np->n_flag &= ~NFLUSHWANT;
1170 wakeup((caddr_t)&np->n_flag);
1171 }
1172 return (EINTR);
1173 }
87de5057 1174 error = vinvalbuf(vp, flags, 0, slptimeo);
984263bc 1175 }
5a9187cb 1176 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
984263bc
MD
1177 if (np->n_flag & NFLUSHWANT) {
1178 np->n_flag &= ~NFLUSHWANT;
1179 wakeup((caddr_t)&np->n_flag);
1180 }
1181 return (0);
1182}
1183
1184/*
edb90c22
MD
1185 * Return true (non-zero) if the txthread and rxthread are operational
1186 * and we do not already have too many not-yet-started BIO's built up.
984263bc
MD
1187 */
1188int
edb90c22
MD
1189nfs_asyncok(struct nfsmount *nmp)
1190{
cc7d050e 1191 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
f8565b0f 1192 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
edb90c22
MD
1193 nmp->nm_rxstate <= NFSSVC_PENDING &&
1194 nmp->nm_txstate <= NFSSVC_PENDING);
1195}
1196
1197/*
1198 * The read-ahead code calls this to queue a bio to the txthread.
1199 *
1200 * We don't touch the bio otherwise... that is, we do not even
1201 * construct or send the initial rpc. The txthread will do it
1202 * for us.
f8565b0f
MD
1203 *
1204 * NOTE! nm_bioqlen is not decremented until the request completes,
1205 * so it does not reflect the number of bio's on bioq.
edb90c22
MD
1206 */
1207void
1208nfs_asyncio(struct vnode *vp, struct bio *bio)
984263bc 1209{
81b5c339 1210 struct buf *bp = bio->bio_buf;
edb90c22 1211 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc 1212
81b5c339 1213 KKASSERT(vp->v_tag == VT_NFS);
52e1cf57 1214 BUF_KERNPROC(bp);
52e1cf57 1215 bio->bio_driver_info = vp;
f8565b0f 1216 crit_enter();
52e1cf57 1217 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
f8565b0f
MD
1218 atomic_add_int(&nmp->nm_bioqlen, 1);
1219 crit_exit();
52e1cf57 1220 nfssvc_iod_writer_wakeup(nmp);
984263bc
MD
1221}
1222
1223/*
cc7d050e
MD
1224 * nfs_dio() - Execute a BIO operation synchronously. The BIO will be
1225 * completed and its error returned. The caller is responsible
1226 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
1227 * our error probe will be against an invalid pointer.
edb90c22 1228 *
cc7d050e 1229 * nfs_startio()- Execute a BIO operation assynchronously.
dadab5e9 1230 *
cc7d050e
MD
1231 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
1232 * which basically just queues it to the txthread. nfs_startio()
1233 * actually initiates the I/O AFTER it has gotten to the txthread.
ae8e83e6 1234 *
cc7d050e 1235 * NOTE: td might be NULL.
984263bc 1236 */
edb90c22
MD
1237void
1238nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
984263bc 1239{
81b5c339 1240 struct buf *bp = bio->bio_buf;
cc7d050e
MD
1241 struct nfsnode *np;
1242 struct nfsmount *nmp;
1243
1244 KKASSERT(vp->v_tag == VT_NFS);
1245 np = VTONFS(vp);
1246 nmp = VFSTONFS(vp->v_mount);
1247
1248 /*
1249 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1250 * do this here so we do not have to do it in all the code that
1251 * calls us.
1252 */
1253 bp->b_flags &= ~(B_ERROR | B_INVAL);
1254
1255 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1256 ("nfs_doio: bp %p already marked done!", bp));
1257
1258 if (bp->b_cmd == BUF_CMD_READ) {
1259 switch (vp->v_type) {
1260 case VREG:
1261 nfsstats.read_bios++;
1262 nfs_readrpc_bio(vp, bio);
1263 break;
1264 case VLNK:
1265#if 0
1266 bio->bio_offset = 0;
1267 nfsstats.readlink_bios++;
1268 nfs_readlinkrpc_bio(vp, bio);
1269#else
1270 nfs_doio(vp, bio, td);
1271#endif
1272 break;
1273 case VDIR:
1274 /*
1275 * NOTE: If nfs_readdirplusrpc_bio() is requested but
1276 * not supported, it will chain to
1277 * nfs_readdirrpc_bio().
1278 */
1279#if 0
1280 nfsstats.readdir_bios++;
1281 uiop->uio_offset = bio->bio_offset;
1282 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
1283 nfs_readdirplusrpc_bio(vp, bio);
1284 else
1285 nfs_readdirrpc_bio(vp, bio);
1286#else
1287 nfs_doio(vp, bio, td);
1288#endif
1289 break;
1290 default:
1291 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1292 bp->b_flags |= B_ERROR;
1293 bp->b_error = EINVAL;
1294 biodone(bio);
1295 break;
1296 }
1297 } else {
1298 /*
1299 * If we only need to commit, try to commit. If this fails
1300 * it will chain through to the write. Basically all the logic
1301 * in nfs_doio() is replicated.
1302 */
1303 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1304 if (bp->b_flags & B_NEEDCOMMIT)
1305 nfs_commitrpc_bio(vp, bio);
1306 else
1307 nfs_writerpc_bio(vp, bio);
1308 }
1309}
1310
1311int
1312nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1313{
1314 struct buf *bp = bio->bio_buf;
984263bc 1315 struct uio *uiop;
984263bc
MD
1316 struct nfsnode *np;
1317 struct nfsmount *nmp;
cc7d050e
MD
1318 int error = 0;
1319 int iomode, must_commit;
984263bc
MD
1320 struct uio uio;
1321 struct iovec io;
1322
81b5c339 1323 KKASSERT(vp->v_tag == VT_NFS);
984263bc
MD
1324 np = VTONFS(vp);
1325 nmp = VFSTONFS(vp->v_mount);
1326 uiop = &uio;
1327 uiop->uio_iov = &io;
1328 uiop->uio_iovcnt = 1;
1329 uiop->uio_segflg = UIO_SYSSPACE;
dadab5e9 1330 uiop->uio_td = td;
984263bc
MD
1331
1332 /*
1333 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1334 * do this here so we do not have to do it in all the code that
1335 * calls us.
1336 */
1337 bp->b_flags &= ~(B_ERROR | B_INVAL);
1338
10f3fee5
MD
1339 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1340 ("nfs_doio: bp %p already marked done!", bp));
1341
1342 if (bp->b_cmd == BUF_CMD_READ) {
e54488bb 1343 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
984263bc
MD
1344 io.iov_base = bp->b_data;
1345 uiop->uio_rw = UIO_READ;
1346
1347 switch (vp->v_type) {
1348 case VREG:
984263bc 1349 nfsstats.read_bios++;
edb90c22
MD
1350 uiop->uio_offset = bio->bio_offset;
1351 error = nfs_readrpc_uio(vp, uiop);
1352 if (error == 0) {
984263bc
MD
1353 if (uiop->uio_resid) {
1354 /*
1355 * If we had a short read with no error, we must have
1356 * hit a file hole. We should zero-fill the remainder.
1357 * This can also occur if the server hits the file EOF.
1358 *
1359 * Holes used to be able to occur due to pending
1360 * writes, but that is not possible any longer.
1361 */
edb90c22
MD
1362 int nread = bp->b_bcount - bp->b_resid;
1363 int left = bp->b_resid;
984263bc
MD
1364
1365 if (left > 0)
1366 bzero((char *)bp->b_data + nread, left);
edb90c22 1367 bp->b_resid = 0;
984263bc
MD
1368 }
1369 }
dadab5e9 1370 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
e07fef60 1371 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
984263bc 1372 uprintf("Process killed due to text file modification\n");
84204577 1373 ksignal(td->td_proc, SIGKILL);
984263bc
MD
1374 }
1375 break;
1376 case VLNK:
81b5c339 1377 uiop->uio_offset = 0;
984263bc 1378 nfsstats.readlink_bios++;
cc7d050e 1379 error = nfs_readlinkrpc_uio(vp, uiop);
984263bc
MD
1380 break;
1381 case VDIR:
1382 nfsstats.readdir_bios++;
54078292 1383 uiop->uio_offset = bio->bio_offset;
984263bc 1384 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
cc7d050e 1385 error = nfs_readdirplusrpc_uio(vp, uiop);
984263bc
MD
1386 if (error == NFSERR_NOTSUPP)
1387 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1388 }
1389 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
cc7d050e 1390 error = nfs_readdirrpc_uio(vp, uiop);
984263bc
MD
1391 /*
1392 * end-of-directory sets B_INVAL but does not generate an
1393 * error.
1394 */
1395 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1396 bp->b_flags |= B_INVAL;
1397 break;
1398 default:
086c1d7e 1399 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
984263bc
MD
1400 break;
1401 };
1402 if (error) {
1403 bp->b_flags |= B_ERROR;
1404 bp->b_error = error;
1405 }
cc7d050e 1406 bp->b_resid = uiop->uio_resid;
984263bc
MD
1407 } else {
1408 /*
1409 * If we only need to commit, try to commit
1410 */
10f3fee5 1411 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
984263bc
MD
1412 if (bp->b_flags & B_NEEDCOMMIT) {
1413 int retv;
1414 off_t off;
1415
54078292 1416 off = bio->bio_offset + bp->b_dirtyoff;
cc7d050e
MD
1417 retv = nfs_commitrpc_uio(vp, off,
1418 bp->b_dirtyend - bp->b_dirtyoff,
1419 td);
984263bc
MD
1420 if (retv == 0) {
1421 bp->b_dirtyoff = bp->b_dirtyend = 0;
1422 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1423 bp->b_resid = 0;
81b5c339 1424 biodone(bio);
cc7d050e 1425 return(0);
984263bc
MD
1426 }
1427 if (retv == NFSERR_STALEWRITEVERF) {
81b5c339 1428 nfs_clearcommit(vp->v_mount);
984263bc
MD
1429 }
1430 }
1431
1432 /*
1433 * Setup for actual write
1434 */
54078292
MD
1435 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1436 bp->b_dirtyend = np->n_size - bio->bio_offset;
984263bc
MD
1437
1438 if (bp->b_dirtyend > bp->b_dirtyoff) {
1439 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1440 - bp->b_dirtyoff;
54078292 1441 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
984263bc
MD
1442 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1443 uiop->uio_rw = UIO_WRITE;
1444 nfsstats.write_bios++;
1445
ae8e83e6 1446 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
984263bc
MD
1447 iomode = NFSV3WRITE_UNSTABLE;
1448 else
1449 iomode = NFSV3WRITE_FILESYNC;
1450
cc7d050e
MD
1451 must_commit = 0;
1452 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
984263bc
MD
1453
1454 /*
1455 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1456 * to cluster the buffers needing commit. This will allow
1457 * the system to submit a single commit rpc for the whole
1458 * cluster. We can do this even if the buffer is not 100%
1459 * dirty (relative to the NFS blocksize), so we optimize the
1460 * append-to-file-case.
1461 *
1462 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1463 * cleared because write clustering only works for commit
1464 * rpc's, not for the data portion of the write).
1465 */
1466
1467 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1468 bp->b_flags |= B_NEEDCOMMIT;
1469 if (bp->b_dirtyoff == 0
1470 && bp->b_dirtyend == bp->b_bcount)
1471 bp->b_flags |= B_CLUSTEROK;
1472 } else {
1473 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1474 }
984263bc
MD
1475
1476 /*
1477 * For an interrupted write, the buffer is still valid
1478 * and the write hasn't been pushed to the server yet,
1479 * so we can't set B_ERROR and report the interruption
ae8e83e6 1480 * by setting B_EINTR. For the async case, B_EINTR
984263bc
MD
1481 * is not relevant, so the rpc attempt is essentially
1482 * a noop. For the case of a V3 write rpc not being
1483 * committed to stable storage, the block is still
1484 * dirty and requires either a commit rpc or another
1485 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1486 * the block is reused. This is indicated by setting
1487 * the B_DELWRI and B_NEEDCOMMIT flags.
1488 *
1489 * If the buffer is marked B_PAGING, it does not reside on
1490 * the vp's paging queues so we cannot call bdirty(). The
1491 * bp in this case is not an NFS cache block so we should
1492 * be safe. XXX
1493 */
1494 if (error == EINTR
1495 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
165dba55 1496 crit_enter();
984263bc 1497 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
10f3fee5 1498 if ((bp->b_flags & B_PAGING) == 0)
984263bc 1499 bdirty(bp);
ae8e83e6 1500 if (error)
984263bc 1501 bp->b_flags |= B_EINTR;
165dba55 1502 crit_exit();
984263bc
MD
1503 } else {
1504 if (error) {
1505 bp->b_flags |= B_ERROR;
1506 bp->b_error = np->n_error = error;
1507 np->n_flag |= NWRITEERR;
1508 }
1509 bp->b_dirtyoff = bp->b_dirtyend = 0;
1510 }
cc7d050e
MD
1511 if (must_commit)
1512 nfs_clearcommit(vp->v_mount);
1513 bp->b_resid = uiop->uio_resid;
984263bc
MD
1514 } else {
1515 bp->b_resid = 0;
984263bc
MD
1516 }
1517 }
cc7d050e
MD
1518
1519 /*
1520 * I/O was run synchronously, biodone() it and calculate the
1521 * error to return.
1522 */
81b5c339 1523 biodone(bio);
cc7d050e
MD
1524 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1525 if (bp->b_flags & B_EINTR)
1526 return (EINTR);
1527 if (bp->b_flags & B_ERROR)
1528 return (bp->b_error ? bp->b_error : EIO);
1529 return (0);
984263bc
MD
1530}
1531
1532/*
1533 * Used to aid in handling ftruncate() operations on the NFS client side.
1534 * Truncation creates a number of special problems for NFS. We have to
1535 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1536 * we have to properly handle VM pages or (potentially dirty) buffers
1537 * that straddle the truncation point.
1538 */
1539
1540int
3b568787 1541nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize)
984263bc
MD
1542{
1543 struct nfsnode *np = VTONFS(vp);
1544 u_quad_t tsize = np->n_size;
1545 int biosize = vp->v_mount->mnt_stat.f_iosize;
1546 int error = 0;
1547
1548 np->n_size = nsize;
1549
1550 if (np->n_size < tsize) {
1551 struct buf *bp;
1552 daddr_t lbn;
54078292 1553 off_t loffset;
984263bc
MD
1554 int bufsize;
1555
1556 /*
1557 * vtruncbuf() doesn't get the buffer overlapping the
1558 * truncation point. We may have a B_DELWRI and/or B_CACHE
1559 * buffer that now needs to be truncated.
1560 */
87de5057 1561 error = vtruncbuf(vp, nsize, biosize);
984263bc
MD
1562 lbn = nsize / biosize;
1563 bufsize = nsize & (biosize - 1);
54078292
MD
1564 loffset = nsize - bufsize;
1565 bp = nfs_getcacheblk(vp, loffset, bufsize, td);
984263bc
MD
1566 if (bp->b_dirtyoff > bp->b_bcount)
1567 bp->b_dirtyoff = bp->b_bcount;
1568 if (bp->b_dirtyend > bp->b_bcount)
1569 bp->b_dirtyend = bp->b_bcount;
1570 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1571 brelse(bp);
1572 } else {
1573 vnode_pager_setsize(vp, nsize);
1574 }
1575 return(error);
1576}
1577
ae8e83e6
MD
1578/*
1579 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1580 * Caller is responsible for brelse()'ing the bp.
1581 */
1582static void
1583nfsiodone_sync(struct bio *bio)
1584{
1585 bio->bio_flags = 0;
1586 bpdone(bio->bio_buf, 0);
1587}
edb90c22
MD
1588
1589/*
edb90c22
MD
1590 * nfs read rpc - BIO version
1591 */
edb90c22
MD
1592void
1593nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1594{
1595 struct buf *bp = bio->bio_buf;
1596 u_int32_t *tl;
1597 struct nfsmount *nmp;
1598 int error = 0, len, tsiz;
1599 struct nfsm_info *info;
1600
1601 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1602 info->mrep = NULL;
1603 info->v3 = NFS_ISV3(vp);
1604
1605 nmp = VFSTONFS(vp->v_mount);
1606 tsiz = bp->b_bcount;
cc7d050e 1607 KKASSERT(tsiz <= nmp->nm_rsize);
edb90c22
MD
1608 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1609 error = EFBIG;
1610 goto nfsmout;
1611 }
1612 nfsstats.rpccnt[NFSPROC_READ]++;
cc7d050e 1613 len = tsiz;
edb90c22
MD
1614 nfsm_reqhead(info, vp, NFSPROC_READ,
1615 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1616 ERROROUT(nfsm_fhtom(info, vp));
1617 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1618 if (info->v3) {
1619 txdr_hyper(bio->bio_offset, tl);
1620 *(tl + 2) = txdr_unsigned(len);
1621 } else {
1622 *tl++ = txdr_unsigned(bio->bio_offset);
1623 *tl++ = txdr_unsigned(len);
1624 *tl = 0;
1625 }
1626 info->bio = bio;
1627 info->done = nfs_readrpc_bio_done;
1628 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1629 nfs_vpcred(vp, ND_READ));
1630 return;
1631nfsmout:
1632 kfree(info, M_NFSREQ);
1633 bp->b_error = error;
1634 bp->b_flags |= B_ERROR;
1635 biodone(bio);
1636}
1637
1638static void
1639nfs_readrpc_bio_done(nfsm_info_t info)
1640{
1641 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1642 struct bio *bio = info->bio;
1643 struct buf *bp = bio->bio_buf;
1644 u_int32_t *tl;
1645 int attrflag;
1646 int retlen;
1647 int eof;
1648 int error = 0;
1649
1650 KKASSERT(info->state == NFSM_STATE_DONE);
1651
1652 if (info->v3) {
1653 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1654 NFS_LATTR_NOSHRINK));
1655 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1656 eof = fxdr_unsigned(int, *(tl + 1));
1657 } else {
1658 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1659 eof = 0;
1660 }
1661 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1662 ERROROUT(nfsm_mtobio(info, bio, retlen));
1663 m_freem(info->mrep);
1664 info->mrep = NULL;
1665
1666 /*
1667 * No error occured, fill the hole if any
1668 */
1669 if (retlen < bp->b_bcount) {
1670 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1671 }
1672 bp->b_resid = bp->b_bcount - retlen;
1673#if 0
1674 /* retlen */
1675 tsiz -= retlen;
1676 if (info.v3) {
1677 if (eof || retlen == 0) {
1678 tsiz = 0;
1679 }
1680 } else if (retlen < len) {
1681 tsiz = 0;
1682 }
1683#endif
1684nfsmout:
f8565b0f 1685 kfree(info, M_NFSREQ);
edb90c22
MD
1686 if (error) {
1687 bp->b_error = error;
1688 bp->b_flags |= B_ERROR;
1689 }
1690 biodone(bio);
1691}
1692
edb90c22
MD
1693/*
1694 * nfs write call - BIO version
1695 */
cc7d050e
MD
1696void
1697nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
edb90c22 1698{
edb90c22 1699 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
cc7d050e
MD
1700 struct nfsnode *np = VTONFS(vp);
1701 struct buf *bp = bio->bio_buf;
1702 u_int32_t *tl;
1703 int len;
1704 int iomode;
1705 int error = 0;
1706 struct nfsm_info *info;
1707 off_t offset;
edb90c22 1708
cc7d050e
MD
1709 /*
1710 * Setup for actual write. Just clean up the bio if there
1711 * is nothing to do.
1712 */
1713 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1714 bp->b_dirtyend = np->n_size - bio->bio_offset;
edb90c22 1715
cc7d050e
MD
1716 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1717 bp->b_resid = 0;
1718 biodone(bio);
1719 return;
1720 }
1721 len = bp->b_dirtyend - bp->b_dirtyoff;
1722 offset = bio->bio_offset + bp->b_dirtyoff;
1723 if (offset + len > nmp->nm_maxfilesize) {
1724 bp->b_flags |= B_ERROR;
1725 bp->b_error = EFBIG;
1726 biodone(bio);
1727 return;
1728 }
1729 bp->b_resid = len;
1730 nfsstats.write_bios++;
1731
1732 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1733 info->mrep = NULL;
1734 info->v3 = NFS_ISV3(vp);
1735 info->info_writerpc.must_commit = 0;
1736 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1737 iomode = NFSV3WRITE_UNSTABLE;
1738 else
1739 iomode = NFSV3WRITE_FILESYNC;
edb90c22 1740
cc7d050e
MD
1741 KKASSERT(len <= nmp->nm_wsize);
1742
1743 nfsstats.rpccnt[NFSPROC_WRITE]++;
1744 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1745 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1746 ERROROUT(nfsm_fhtom(info, vp));
1747 if (info->v3) {
1748 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1749 txdr_hyper(offset, tl);
1750 tl += 2;
1751 *tl++ = txdr_unsigned(len);
1752 *tl++ = txdr_unsigned(iomode);
1753 *tl = txdr_unsigned(len);
1754 } else {
1755 u_int32_t x;
1756
1757 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1758 /* Set both "begin" and "current" to non-garbage. */
1759 x = txdr_unsigned((u_int32_t)offset);
1760 *tl++ = x; /* "begin offset" */
1761 *tl++ = x; /* "current offset" */
1762 x = txdr_unsigned(len);
1763 *tl++ = x; /* total to this offset */
1764 *tl = x; /* size of this write */
1765 }
1766 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1767 info->bio = bio;
1768 info->done = nfs_writerpc_bio_done;
1769 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1770 nfs_vpcred(vp, ND_WRITE));
1771 return;
1772nfsmout:
1773 kfree(info, M_NFSREQ);
1774 bp->b_error = error;
1775 bp->b_flags |= B_ERROR;
1776 biodone(bio);
1777}
1778
1779static void
1780nfs_writerpc_bio_done(nfsm_info_t info)
1781{
1782 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1783 struct nfsnode *np = VTONFS(info->vp);
1784 struct bio *bio = info->bio;
1785 struct buf *bp = bio->bio_buf;
1786 int wccflag = NFSV3_WCCRATTR;
1787 int iomode = NFSV3WRITE_FILESYNC;
1788 int commit;
1789 int rlen;
1790 int error;
1791 int len = bp->b_resid; /* b_resid was set to shortened length */
1792 u_int32_t *tl;
1793
1794 if (info->v3) {
1795 /*
1796 * The write RPC returns a before and after mtime. The
1797 * nfsm_wcc_data() macro checks the before n_mtime
1798 * against the before time and stores the after time
1799 * in the nfsnode's cached vattr and n_mtime field.
1800 * The NRMODIFIED bit will be set if the before
1801 * time did not match the original mtime.
1802 */
1803 wccflag = NFSV3_WCCCHK;
1804 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1805 if (error == 0) {
1806 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1807 rlen = fxdr_unsigned(int, *tl++);
1808 if (rlen == 0) {
1809 error = NFSERR_IO;
1810 m_freem(info->mrep);
1811 info->mrep = NULL;
1812 goto nfsmout;
1813 } else if (rlen < len) {
1814#if 0
edb90c22 1815 /*
cc7d050e 1816 * XXX what do we do here?
edb90c22 1817 */
cc7d050e
MD
1818 backup = len - rlen;
1819 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1820 uiop->uio_iov->iov_len += backup;
1821 uiop->uio_offset -= backup;
1822 uiop->uio_resid += backup;
1823 len = rlen;
1824#endif
1825 }
1826 commit = fxdr_unsigned(int, *tl++);
1827
1828 /*
1829 * Return the lowest committment level
1830 * obtained by any of the RPCs.
1831 */
1832 if (iomode == NFSV3WRITE_FILESYNC)
1833 iomode = commit;
1834 else if (iomode == NFSV3WRITE_DATASYNC &&
1835 commit == NFSV3WRITE_UNSTABLE)
1836 iomode = commit;
1837 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1838 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1839 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1840 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1841 info->info_writerpc.must_commit = 1;
1842 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
edb90c22 1843 }
edb90c22 1844 }
cc7d050e
MD
1845 } else {
1846 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1847 }
1848 m_freem(info->mrep);
1849 info->mrep = NULL;
1850 len = 0;
1851nfsmout:
1852 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1853 iomode = NFSV3WRITE_FILESYNC;
1854 bp->b_resid = len;
1855
1856 /*
1857 * End of RPC. Now clean up the bp.
1858 *
1859 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1860 * to cluster the buffers needing commit. This will allow
1861 * the system to submit a single commit rpc for the whole
1862 * cluster. We can do this even if the buffer is not 100%
1863 * dirty (relative to the NFS blocksize), so we optimize the
1864 * append-to-file-case.
1865 *
1866 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1867 * cleared because write clustering only works for commit
1868 * rpc's, not for the data portion of the write).
1869 */
1870 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1871 bp->b_flags |= B_NEEDCOMMIT;
1872 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1873 bp->b_flags |= B_CLUSTEROK;
1874 } else {
1875 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1876 }
1877
1878 /*
1879 * For an interrupted write, the buffer is still valid
1880 * and the write hasn't been pushed to the server yet,
1881 * so we can't set B_ERROR and report the interruption
1882 * by setting B_EINTR. For the async case, B_EINTR
1883 * is not relevant, so the rpc attempt is essentially
1884 * a noop. For the case of a V3 write rpc not being
1885 * committed to stable storage, the block is still
1886 * dirty and requires either a commit rpc or another
1887 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1888 * the block is reused. This is indicated by setting
1889 * the B_DELWRI and B_NEEDCOMMIT flags.
1890 *
1891 * If the buffer is marked B_PAGING, it does not reside on
1892 * the vp's paging queues so we cannot call bdirty(). The
1893 * bp in this case is not an NFS cache block so we should
1894 * be safe. XXX
1895 */
1896 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1897 crit_enter();
1898 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1899 if ((bp->b_flags & B_PAGING) == 0)
1900 bdirty(bp);
edb90c22 1901 if (error)
cc7d050e
MD
1902 bp->b_flags |= B_EINTR;
1903 crit_exit();
1904 } else {
1905 if (error) {
1906 bp->b_flags |= B_ERROR;
1907 bp->b_error = np->n_error = error;
1908 np->n_flag |= NWRITEERR;
1909 }
1910 bp->b_dirtyoff = bp->b_dirtyend = 0;
1911 }
1912 if (info->info_writerpc.must_commit)
1913 nfs_clearcommit(info->vp->v_mount);
1914 kfree(info, M_NFSREQ);
1915 if (error) {
1916 bp->b_flags |= B_ERROR;
1917 bp->b_error = error;
1918 }
1919 biodone(bio);
1920}
1921
1922/*
1923 * Nfs Version 3 commit rpc - BIO version
1924 *
1925 * This function issues the commit rpc and will chain to a write
1926 * rpc if necessary.
1927 */
1928void
1929nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1930{
1931 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1932 struct buf *bp = bio->bio_buf;
1933 struct nfsm_info *info;
1934 int error = 0;
1935 u_int32_t *tl;
1936
1937 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1938 bp->b_dirtyoff = bp->b_dirtyend = 0;
1939 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1940 bp->b_resid = 0;
1941 biodone(bio);
1942 return;
1943 }
1944
1945 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1946 info->mrep = NULL;
1947 info->v3 = 1;
1948
1949 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1950 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1951 ERROROUT(nfsm_fhtom(info, vp));
1952 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1953 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1954 tl += 2;
1955 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1956 info->bio = bio;
1957 info->done = nfs_commitrpc_bio_done;
1958 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1959 nfs_vpcred(vp, ND_WRITE));
1960 return;
1961nfsmout:
1962 /*
1963 * Chain to write RPC on (early) error
1964 */
1965 kfree(info, M_NFSREQ);
1966 nfs_writerpc_bio(vp, bio);
1967}
1968
1969static void
1970nfs_commitrpc_bio_done(nfsm_info_t info)
1971{
1972 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1973 struct bio *bio = info->bio;
1974 struct buf *bp = bio->bio_buf;
1975 u_int32_t *tl;
1976 int wccflag = NFSV3_WCCRATTR;
1977 int error = 0;
1978
1979 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1980 if (error == 0) {
1981 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1982 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1983 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1984 error = NFSERR_STALEWRITEVERF;
1985 }
edb90c22 1986 }
cc7d050e
MD
1987 m_freem(info->mrep);
1988 info->mrep = NULL;
1989
1990 /*
1991 * On completion we must chain to a write bio if an
1992 * error occurred.
1993 */
edb90c22 1994nfsmout:
cc7d050e
MD
1995 kfree(info, M_NFSREQ);
1996 if (error == 0) {
1997 bp->b_dirtyoff = bp->b_dirtyend = 0;
1998 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1999 bp->b_resid = 0;
2000 biodone(bio);
2001 } else {
2002 kprintf("commitrpc_bioC %lld -> CHAIN WRITE\n", bio->bio_offset);
2003 nfs_writerpc_bio(info->vp, bio);
2004 }
edb90c22
MD
2005}
2006