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