2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
33 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/resourcevar.h>
40 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_pager.h>
53 #include <vm/vnode_pager.h>
56 #include <sys/thread2.h>
57 #include <vm/vm_page2.h>
65 #include "nfsm_subs.h"
68 static struct buf
*nfs_getcacheblk(struct vnode
*vp
, off_t loffset
,
69 int size
, struct thread
*td
);
70 static int nfs_check_dirent(struct nfs_dirent
*dp
, int maxlen
);
71 static void nfsiodone_sync(struct bio
*bio
);
72 static void nfs_readrpc_bio_done(nfsm_info_t info
);
73 static void nfs_writerpc_bio_done(nfsm_info_t info
);
74 static void nfs_commitrpc_bio_done(nfsm_info_t info
);
78 nfs_knote(struct vnode
*vp
, int flags
)
81 KNOTE(&vp
->v_pollinfo
.vpi_kqinfo
.ki_note
, flags
);
85 * Vnode op for read using bio
88 nfs_bioread(struct vnode
*vp
, struct uio
*uio
, int ioflag
)
90 struct nfsnode
*np
= VTONFS(vp
);
92 struct buf
*bp
, *rabp
;
95 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
105 if (uio
->uio_rw
!= UIO_READ
)
106 panic("nfs_read mode");
108 if (uio
->uio_resid
== 0)
110 if (uio
->uio_offset
< 0) /* XXX VDIR cookies can be negative */
114 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) != 0 &&
115 (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0)
116 (void)nfs_fsinfo(nmp
, vp
, td
);
117 if (vp
->v_type
!= VDIR
&&
118 (uio
->uio_offset
+ uio
->uio_resid
) > nmp
->nm_maxfilesize
)
120 biosize
= vp
->v_mount
->mnt_stat
.f_iosize
;
121 seqcount
= (int)((off_t
)(ioflag
>> IO_SEQSHIFT
) * biosize
/ MAXBSIZE
);
124 * For nfs, cache consistency can only be maintained approximately.
125 * Although RFC1094 does not specify the criteria, the following is
126 * believed to be compatible with the reference port.
128 * NFS: If local changes have been made and this is a
129 * directory, the directory must be invalidated and
130 * the attribute cache must be cleared.
132 * GETATTR is called to synchronize the file size.
134 * If remote changes are detected local data is flushed
135 * and the cache is invalidated.
137 * NOTE: In the normal case the attribute cache is not
138 * cleared which means GETATTR may use cached data and
139 * not immediately detect changes made on the server.
141 if ((np
->n_flag
& NLMODIFIED
) && vp
->v_type
== VDIR
) {
143 error
= nfs_vinvalbuf(vp
, V_SAVE
, 1);
148 error
= VOP_GETATTR(vp
, &vattr
);
153 * This can deadlock getpages/putpages for regular
154 * files. Only do it for directories.
156 if (np
->n_flag
& NRMODIFIED
) {
157 if (vp
->v_type
== VDIR
) {
159 error
= nfs_vinvalbuf(vp
, V_SAVE
, 1);
162 np
->n_flag
&= ~NRMODIFIED
;
167 * Loop until uio exhausted or we hit EOF
172 switch (vp
->v_type
) {
174 nfsstats
.biocache_reads
++;
175 lbn
= uio
->uio_offset
/ biosize
;
176 boff
= uio
->uio_offset
& (biosize
- 1);
177 loffset
= lbn
* biosize
;
180 * Start the read ahead(s), as required.
182 if (nmp
->nm_readahead
> 0 && nfs_asyncok(nmp
)) {
183 for (nra
= 0; nra
< nmp
->nm_readahead
&& nra
< seqcount
&&
184 (off_t
)(lbn
+ 1 + nra
) * biosize
< np
->n_size
; nra
++) {
185 rabn
= lbn
+ 1 + nra
;
186 raoffset
= rabn
* biosize
;
187 if (findblk(vp
, raoffset
, FINDBLK_TEST
) == NULL
) {
188 rabp
= nfs_getcacheblk(vp
, raoffset
, biosize
, td
);
191 if ((rabp
->b_flags
& (B_CACHE
|B_DELWRI
)) == 0) {
192 rabp
->b_cmd
= BUF_CMD_READ
;
193 vfs_busy_pages(vp
, rabp
);
194 nfs_asyncio(vp
, &rabp
->b_bio2
);
203 * Obtain the buffer cache block. Figure out the buffer size
204 * when we are at EOF. If we are modifying the size of the
205 * buffer based on an EOF condition we need to hold
206 * nfs_rslock() through obtaining the buffer to prevent
207 * a potential writer-appender from messing with n_size.
208 * Otherwise we may accidently truncate the buffer and
211 * Note that bcount is *not* DEV_BSIZE aligned.
213 if (loffset
+ boff
>= np
->n_size
) {
217 bp
= nfs_getcacheblk(vp
, loffset
, biosize
, td
);
223 * If B_CACHE is not set, we must issue the read. If this
224 * fails, we return an error.
226 if ((bp
->b_flags
& B_CACHE
) == 0) {
227 bp
->b_cmd
= BUF_CMD_READ
;
228 bp
->b_bio2
.bio_done
= nfsiodone_sync
;
229 bp
->b_bio2
.bio_flags
|= BIO_SYNC
;
230 vfs_busy_pages(vp
, bp
);
231 error
= nfs_doio(vp
, &bp
->b_bio2
, td
);
239 * on is the offset into the current bp. Figure out how many
240 * bytes we can copy out of the bp. Note that bcount is
241 * NOT DEV_BSIZE aligned.
243 * Then figure out how many bytes we can copy into the uio.
246 if (n
> uio
->uio_resid
)
248 if (loffset
+ boff
+ n
> np
->n_size
)
249 n
= np
->n_size
- loffset
- boff
;
252 biosize
= min(NFS_MAXPATHLEN
, np
->n_size
);
253 nfsstats
.biocache_readlinks
++;
254 bp
= nfs_getcacheblk(vp
, (off_t
)0, biosize
, td
);
257 if ((bp
->b_flags
& B_CACHE
) == 0) {
258 bp
->b_cmd
= BUF_CMD_READ
;
259 bp
->b_bio2
.bio_done
= nfsiodone_sync
;
260 bp
->b_bio2
.bio_flags
|= BIO_SYNC
;
261 vfs_busy_pages(vp
, bp
);
262 error
= nfs_doio(vp
, &bp
->b_bio2
, td
);
264 bp
->b_flags
|= B_ERROR
| B_INVAL
;
269 n
= szmin(uio
->uio_resid
, (size_t)bp
->b_bcount
- bp
->b_resid
);
273 nfsstats
.biocache_readdirs
++;
274 if (np
->n_direofoffset
&&
275 uio
->uio_offset
>= np
->n_direofoffset
279 lbn
= (uoff_t
)uio
->uio_offset
/ NFS_DIRBLKSIZ
;
280 boff
= uio
->uio_offset
& (NFS_DIRBLKSIZ
- 1);
281 loffset
= uio
->uio_offset
- boff
;
282 bp
= nfs_getcacheblk(vp
, loffset
, NFS_DIRBLKSIZ
, td
);
286 if ((bp
->b_flags
& B_CACHE
) == 0) {
287 bp
->b_cmd
= BUF_CMD_READ
;
288 bp
->b_bio2
.bio_done
= nfsiodone_sync
;
289 bp
->b_bio2
.bio_flags
|= BIO_SYNC
;
290 vfs_busy_pages(vp
, bp
);
291 error
= nfs_doio(vp
, &bp
->b_bio2
, td
);
294 while (error
== NFSERR_BAD_COOKIE
) {
295 kprintf("got bad cookie vp %p bp %p\n", vp
, bp
);
297 error
= nfs_vinvalbuf(vp
, 0, 1);
299 * Yuck! The directory has been modified on the
300 * server. The only way to get the block is by
301 * reading from the beginning to get all the
304 * Leave the last bp intact unless there is an error.
305 * Loop back up to the while if the error is another
306 * NFSERR_BAD_COOKIE (double yuch!).
308 for (i
= 0; i
<= lbn
&& !error
; i
++) {
309 if (np
->n_direofoffset
310 && (i
* NFS_DIRBLKSIZ
) >= np
->n_direofoffset
)
312 bp
= nfs_getcacheblk(vp
, (off_t
)i
* NFS_DIRBLKSIZ
,
316 if ((bp
->b_flags
& B_CACHE
) == 0) {
317 bp
->b_cmd
= BUF_CMD_READ
;
318 bp
->b_bio2
.bio_done
= nfsiodone_sync
;
319 bp
->b_bio2
.bio_flags
|= BIO_SYNC
;
320 vfs_busy_pages(vp
, bp
);
321 error
= nfs_doio(vp
, &bp
->b_bio2
, td
);
323 * no error + B_INVAL == directory EOF,
326 if (error
== 0 && (bp
->b_flags
& B_INVAL
))
330 * An error will throw away the block and the
331 * for loop will break out. If no error and this
332 * is not the block we want, we throw away the
333 * block and go for the next one via the for loop.
335 if (error
|| i
< lbn
)
340 * The above while is repeated if we hit another cookie
341 * error. If we hit an error and it wasn't a cookie error,
349 * If not eof and read aheads are enabled, start one.
350 * (You need the current block first, so that you have the
351 * directory offset cookie of the next block.)
353 if (nmp
->nm_readahead
> 0 && nfs_asyncok(nmp
) &&
354 (bp
->b_flags
& B_INVAL
) == 0 &&
355 (np
->n_direofoffset
== 0 ||
356 loffset
+ NFS_DIRBLKSIZ
< np
->n_direofoffset
) &&
357 findblk(vp
, loffset
+ NFS_DIRBLKSIZ
, FINDBLK_TEST
) == NULL
359 rabp
= nfs_getcacheblk(vp
, loffset
+ NFS_DIRBLKSIZ
,
362 if ((rabp
->b_flags
& (B_CACHE
|B_DELWRI
)) == 0) {
363 rabp
->b_cmd
= BUF_CMD_READ
;
364 vfs_busy_pages(vp
, rabp
);
365 nfs_asyncio(vp
, &rabp
->b_bio2
);
372 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
373 * chopped for the EOF condition, we cannot tell how large
374 * NFS directories are going to be until we hit EOF. So
375 * an NFS directory buffer is *not* chopped to its EOF. Now,
376 * it just so happens that b_resid will effectively chop it
377 * to EOF. *BUT* this information is lost if the buffer goes
378 * away and is reconstituted into a B_CACHE state ( due to
379 * being VMIO ) later. So we keep track of the directory eof
380 * in np->n_direofoffset and chop it off as an extra step
383 * NOTE: boff could already be beyond EOF.
385 if ((size_t)boff
> NFS_DIRBLKSIZ
- bp
->b_resid
) {
388 n
= szmin(uio
->uio_resid
,
389 NFS_DIRBLKSIZ
- bp
->b_resid
- (size_t)boff
);
391 if (np
->n_direofoffset
&&
392 n
> (size_t)(np
->n_direofoffset
- uio
->uio_offset
)) {
393 n
= (size_t)(np
->n_direofoffset
- uio
->uio_offset
);
397 kprintf(" nfs_bioread: type %x unexpected\n",vp
->v_type
);
402 switch (vp
->v_type
) {
405 error
= uiomovebp(bp
, bp
->b_data
+ boff
, n
, uio
);
409 error
= uiomovebp(bp
, bp
->b_data
+ boff
, n
, uio
);
414 off_t old_off
= uio
->uio_offset
;
416 struct nfs_dirent
*dp
;
419 * We are casting cpos to nfs_dirent, it must be
427 cpos
= bp
->b_data
+ boff
;
428 epos
= bp
->b_data
+ boff
+ n
;
429 while (cpos
< epos
&& error
== 0 && uio
->uio_resid
> 0) {
430 dp
= (struct nfs_dirent
*)cpos
;
431 error
= nfs_check_dirent(dp
, (int)(epos
- cpos
));
434 if (vop_write_dirent(&error
, uio
, dp
->nfs_ino
,
435 dp
->nfs_type
, dp
->nfs_namlen
, dp
->nfs_name
)) {
438 cpos
+= dp
->nfs_reclen
;
442 uio
->uio_offset
= old_off
+ cpos
-
448 kprintf(" nfs_bioread: type %x unexpected\n",vp
->v_type
);
452 } while (error
== 0 && uio
->uio_resid
> 0 && n
> 0);
457 * Userland can supply any 'seek' offset when reading a NFS directory.
458 * Validate the structure so we don't panic the kernel. Note that
459 * the element name is nul terminated and the nul is not included
464 nfs_check_dirent(struct nfs_dirent
*dp
, int maxlen
)
466 int nfs_name_off
= offsetof(struct nfs_dirent
, nfs_name
[0]);
468 if (nfs_name_off
>= maxlen
)
470 if (dp
->nfs_reclen
< nfs_name_off
|| dp
->nfs_reclen
> maxlen
)
472 if (nfs_name_off
+ dp
->nfs_namlen
>= dp
->nfs_reclen
)
474 if (dp
->nfs_reclen
& 3)
480 * Vnode op for write using bio
482 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
483 * struct ucred *a_cred)
486 nfs_write(struct vop_write_args
*ap
)
488 struct uio
*uio
= ap
->a_uio
;
489 struct thread
*td
= uio
->uio_td
;
490 struct vnode
*vp
= ap
->a_vp
;
491 struct nfsnode
*np
= VTONFS(vp
);
492 int ioflag
= ap
->a_ioflag
;
495 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
506 if (uio
->uio_rw
!= UIO_WRITE
)
507 panic("nfs_write mode");
508 if (uio
->uio_segflg
== UIO_USERSPACE
&& uio
->uio_td
!= curthread
)
509 panic("nfs_write proc");
511 if (vp
->v_type
!= VREG
)
514 lwkt_gettoken(&nmp
->nm_token
);
516 if (np
->n_flag
& NWRITEERR
) {
517 np
->n_flag
&= ~NWRITEERR
;
518 lwkt_reltoken(&nmp
->nm_token
);
519 return (np
->n_error
);
521 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) != 0 &&
522 (nmp
->nm_state
& NFSSTA_GOTFSINFO
) == 0) {
523 (void)nfs_fsinfo(nmp
, vp
, td
);
527 * Synchronously flush pending buffers if we are in synchronous
528 * mode or if we are appending.
530 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
531 if (np
->n_flag
& NLMODIFIED
) {
533 error
= nfs_flush(vp
, MNT_WAIT
, td
, 0);
534 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
541 * If IO_APPEND then load uio_offset. We restart here if we cannot
542 * get the append lock.
545 if (ioflag
& IO_APPEND
) {
547 error
= VOP_GETATTR(vp
, &vattr
);
550 uio
->uio_offset
= np
->n_size
;
553 if (uio
->uio_offset
< 0) {
557 if ((uio
->uio_offset
+ uio
->uio_resid
) > nmp
->nm_maxfilesize
) {
561 if (uio
->uio_resid
== 0) {
567 * We need to obtain the rslock if we intend to modify np->n_size
568 * in order to guarentee the append point with multiple contending
569 * writers, to guarentee that no other appenders modify n_size
570 * while we are trying to obtain a truncated buffer (i.e. to avoid
571 * accidently truncating data written by another appender due to
572 * the race), and to ensure that the buffer is populated prior to
573 * our extending of the file. We hold rslock through the entire
576 * Note that we do not synchronize the case where someone truncates
577 * the file while we are appending to it because attempting to lock
578 * this case may deadlock other parts of the system unexpectedly.
580 if ((ioflag
& IO_APPEND
) ||
581 uio
->uio_offset
+ uio
->uio_resid
> np
->n_size
) {
582 switch(nfs_rslock(np
)) {
598 * Maybe this should be above the vnode op call, but so long as
599 * file servers have no limits, i don't think it matters
601 if (td
&& td
->td_proc
&& uio
->uio_offset
+ uio
->uio_resid
>
602 td
->td_proc
->p_rlimit
[RLIMIT_FSIZE
].rlim_cur
) {
603 lwpsignal(td
->td_proc
, td
->td_lwp
, SIGXFSZ
);
610 biosize
= vp
->v_mount
->mnt_stat
.f_iosize
;
613 nfsstats
.biocache_writes
++;
614 boff
= uio
->uio_offset
& (biosize
-1);
615 loffset
= uio
->uio_offset
- boff
;
616 bytes
= (int)szmin((unsigned)(biosize
- boff
), uio
->uio_resid
);
619 * Handle direct append and file extension cases, calculate
620 * unaligned buffer size. When extending B_CACHE will be
621 * set if possible. See UIO_NOCOPY note below.
623 if (uio
->uio_offset
+ bytes
> np
->n_size
) {
624 np
->n_flag
|= NLMODIFIED
;
625 trivial
= (uio
->uio_segflg
!= UIO_NOCOPY
&&
626 uio
->uio_offset
<= np
->n_size
);
627 nfs_meta_setsize(vp
, td
, uio
->uio_offset
+ bytes
,
629 kflags
|= NOTE_EXTEND
;
631 bp
= nfs_getcacheblk(vp
, loffset
, biosize
, td
);
638 * Actual bytes in buffer which we care about
640 if (loffset
+ biosize
< np
->n_size
)
643 bcount
= (int)(np
->n_size
- loffset
);
646 * Avoid a read by setting B_CACHE where the data we
647 * intend to write covers the entire buffer. Note
648 * that the buffer may have been set to B_CACHE by
649 * nfs_meta_setsize() above or otherwise inherited the
650 * flag, but if B_CACHE isn't set the buffer may be
651 * uninitialized and must be zero'd to accomodate
652 * future seek+write's.
654 * See the comments in kern/vfs_bio.c's getblk() for
657 * When doing a UIO_NOCOPY write the buffer is not
658 * overwritten and we cannot just set B_CACHE unconditionally
659 * for full-block writes.
661 if (boff
== 0 && bytes
== biosize
&&
662 uio
->uio_segflg
!= UIO_NOCOPY
) {
663 bp
->b_flags
|= B_CACHE
;
664 bp
->b_flags
&= ~(B_ERROR
| B_INVAL
);
668 * b_resid may be set due to file EOF if we extended out.
669 * The NFS bio code will zero the difference anyway so
670 * just acknowledged the fact and set b_resid to 0.
672 if ((bp
->b_flags
& B_CACHE
) == 0) {
673 bp
->b_cmd
= BUF_CMD_READ
;
674 bp
->b_bio2
.bio_done
= nfsiodone_sync
;
675 bp
->b_bio2
.bio_flags
|= BIO_SYNC
;
676 vfs_busy_pages(vp
, bp
);
677 error
= nfs_doio(vp
, &bp
->b_bio2
, td
);
684 np
->n_flag
|= NLMODIFIED
;
685 kflags
|= NOTE_WRITE
;
688 * If dirtyend exceeds file size, chop it down. This should
689 * not normally occur but there is an append race where it
690 * might occur XXX, so we log it.
692 * If the chopping creates a reverse-indexed or degenerate
693 * situation with dirtyoff/end, we 0 both of them.
695 if (bp
->b_dirtyend
> bcount
) {
696 kprintf("NFS append race @%08llx:%d\n",
697 (long long)bp
->b_bio2
.bio_offset
,
698 bp
->b_dirtyend
- bcount
);
699 bp
->b_dirtyend
= bcount
;
702 if (bp
->b_dirtyoff
>= bp
->b_dirtyend
)
703 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
706 * If the new write will leave a contiguous dirty
707 * area, just update the b_dirtyoff and b_dirtyend,
708 * otherwise force a write rpc of the old dirty area.
710 * While it is possible to merge discontiguous writes due to
711 * our having a B_CACHE buffer ( and thus valid read data
712 * for the hole), we don't because it could lead to
713 * significant cache coherency problems with multiple clients,
714 * especially if locking is implemented later on.
716 * as an optimization we could theoretically maintain
717 * a linked list of discontinuous areas, but we would still
718 * have to commit them separately so there isn't much
719 * advantage to it except perhaps a bit of asynchronization.
721 if (bp
->b_dirtyend
> 0 &&
722 (boff
> bp
->b_dirtyend
||
723 (boff
+ bytes
) < bp
->b_dirtyoff
)
725 if (bwrite(bp
) == EINTR
) {
732 error
= uiomovebp(bp
, bp
->b_data
+ boff
, bytes
, uio
);
735 * Since this block is being modified, it must be written
736 * again and not just committed. Since write clustering does
737 * not work for the stage 1 data write, only the stage 2
738 * commit rpc, we have to clear B_CLUSTEROK as well.
740 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
748 * Only update dirtyoff/dirtyend if not a degenerate
751 * The underlying VM pages have been marked valid by
752 * virtue of acquiring the bp. Because the entire buffer
753 * is marked dirty we do not have to worry about cleaning
754 * out the related dirty bits (and wouldn't really know
755 * how to deal with byte ranges anyway)
758 if (bp
->b_dirtyend
> 0) {
759 bp
->b_dirtyoff
= imin(boff
, bp
->b_dirtyoff
);
760 bp
->b_dirtyend
= imax(boff
+ bytes
,
763 bp
->b_dirtyoff
= boff
;
764 bp
->b_dirtyend
= boff
+ bytes
;
769 * If the lease is non-cachable or IO_SYNC do bwrite().
771 * IO_INVAL appears to be unused. The idea appears to be
772 * to turn off caching in this case. Very odd. XXX
774 * If nfs_async is set bawrite() will use an unstable write
775 * (build dirty bufs on the server), so we might as well
776 * push it out with bawrite(). If nfs_async is not set we
777 * use bdwrite() to cache dirty bufs on the client.
779 if (ioflag
& IO_SYNC
) {
780 if (ioflag
& IO_INVAL
)
781 bp
->b_flags
|= B_NOCACHE
;
785 } else if (boff
+ bytes
== biosize
&& nfs_async
) {
790 } while (uio
->uio_resid
> 0 && bytes
> 0);
796 nfs_knote(vp
, kflags
);
797 lwkt_reltoken(&nmp
->nm_token
);
802 * Get an nfs cache block.
804 * Allocate a new one if the block isn't currently in the cache
805 * and return the block marked busy. If the calling process is
806 * interrupted by a signal for an interruptible mount point, return
809 * The caller must carefully deal with the possible B_INVAL state of
810 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
811 * indirectly), so synchronous reads can be issued without worrying about
812 * the B_INVAL state. We have to be a little more careful when dealing
813 * with writes (see comments in nfs_write()) when extending a file past
817 nfs_getcacheblk(struct vnode
*vp
, off_t loffset
, int size
, struct thread
*td
)
821 struct nfsmount
*nmp
;
826 if (nmp
->nm_flag
& NFSMNT_INT
) {
827 bp
= getblk(vp
, loffset
, size
, GETBLK_PCATCH
, 0);
829 if (nfs_sigintr(nmp
, NULL
, td
))
831 bp
= getblk(vp
, loffset
, size
, 0, 2 * hz
);
834 bp
= getblk(vp
, loffset
, size
, 0, 0);
838 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
839 * now, no translation is necessary.
841 bp
->b_bio2
.bio_offset
= loffset
;
846 * Flush and invalidate all dirty buffers. If another process is already
847 * doing the flush, just wait for completion.
850 nfs_vinvalbuf(struct vnode
*vp
, int flags
, int intrflg
)
852 struct nfsnode
*np
= VTONFS(vp
);
853 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
854 int error
= 0, slpflag
, slptimeo
;
855 thread_t td
= curthread
;
857 if (vp
->v_flag
& VRECLAIMED
)
860 if ((nmp
->nm_flag
& NFSMNT_INT
) == 0)
870 * First wait for any other process doing a flush to complete.
872 while (np
->n_flag
& NFLUSHINPROG
) {
873 np
->n_flag
|= NFLUSHWANT
;
874 error
= tsleep((caddr_t
)&np
->n_flag
, 0, "nfsvinval", slptimeo
);
875 if (error
&& intrflg
&& nfs_sigintr(nmp
, NULL
, td
))
880 * Now, flush as required.
882 np
->n_flag
|= NFLUSHINPROG
;
883 error
= vinvalbuf(vp
, flags
, slpflag
, 0);
885 if (intrflg
&& nfs_sigintr(nmp
, NULL
, td
)) {
886 np
->n_flag
&= ~NFLUSHINPROG
;
887 if (np
->n_flag
& NFLUSHWANT
) {
888 np
->n_flag
&= ~NFLUSHWANT
;
889 wakeup((caddr_t
)&np
->n_flag
);
893 error
= vinvalbuf(vp
, flags
, 0, slptimeo
);
895 np
->n_flag
&= ~(NLMODIFIED
| NFLUSHINPROG
);
896 if (np
->n_flag
& NFLUSHWANT
) {
897 np
->n_flag
&= ~NFLUSHWANT
;
898 wakeup((caddr_t
)&np
->n_flag
);
904 * Return true (non-zero) if the txthread and rxthread are operational
905 * and we do not already have too many not-yet-started BIO's built up.
908 nfs_asyncok(struct nfsmount
*nmp
)
910 return (nmp
->nm_bioqlen
< nfs_maxasyncbio
&&
911 nmp
->nm_bioqlen
< nmp
->nm_maxasync_scaled
/ NFS_ASYSCALE
&&
912 nmp
->nm_rxstate
<= NFSSVC_PENDING
&&
913 nmp
->nm_txstate
<= NFSSVC_PENDING
);
917 * The read-ahead code calls this to queue a bio to the txthread.
919 * We don't touch the bio otherwise... that is, we do not even
920 * construct or send the initial rpc. The txthread will do it
923 * NOTE! nm_bioqlen is not decremented until the request completes,
924 * so it does not reflect the number of bio's on bioq.
927 nfs_asyncio(struct vnode
*vp
, struct bio
*bio
)
929 struct buf
*bp
= bio
->bio_buf
;
930 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
932 KKASSERT(vp
->v_tag
== VT_NFS
);
936 * Shortcut swap cache (not done automatically because we are not
939 if (vn_cache_strategy(vp
, bio
))
942 bio
->bio_driver_info
= vp
;
944 TAILQ_INSERT_TAIL(&nmp
->nm_bioq
, bio
, bio_act
);
945 atomic_add_int(&nmp
->nm_bioqlen
, 1);
947 nfssvc_iod_writer_wakeup(nmp
);
951 * nfs_doio() - Execute a BIO operation synchronously. The BIO will be
952 * completed and its error returned. The caller is responsible
953 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
954 * our error probe will be against an invalid pointer.
956 * nfs_startio()- Execute a BIO operation assynchronously.
958 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
959 * which basically just queues it to the txthread. nfs_startio()
960 * actually initiates the I/O AFTER it has gotten to the txthread.
962 * NOTE: td might be NULL.
964 * NOTE: Caller has already busied the I/O.
967 nfs_startio(struct vnode
*vp
, struct bio
*bio
, struct thread
*td
)
969 struct buf
*bp
= bio
->bio_buf
;
971 KKASSERT(vp
->v_tag
== VT_NFS
);
974 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
975 * do this here so we do not have to do it in all the code that
978 bp
->b_flags
&= ~(B_ERROR
| B_INVAL
);
980 KASSERT(bp
->b_cmd
!= BUF_CMD_DONE
,
981 ("nfs_doio: bp %p already marked done!", bp
));
983 if (bp
->b_cmd
== BUF_CMD_READ
) {
984 switch (vp
->v_type
) {
986 nfsstats
.read_bios
++;
987 nfs_readrpc_bio(vp
, bio
);
992 nfsstats
.readlink_bios
++;
993 nfs_readlinkrpc_bio(vp
, bio
);
995 nfs_doio(vp
, bio
, td
);
1000 * NOTE: If nfs_readdirplusrpc_bio() is requested but
1001 * not supported, it will chain to
1002 * nfs_readdirrpc_bio().
1005 nfsstats
.readdir_bios
++;
1006 uiop
->uio_offset
= bio
->bio_offset
;
1007 if (nmp
->nm_flag
& NFSMNT_RDIRPLUS
)
1008 nfs_readdirplusrpc_bio(vp
, bio
);
1010 nfs_readdirrpc_bio(vp
, bio
);
1012 nfs_doio(vp
, bio
, td
);
1016 kprintf("nfs_doio: type %x unexpected\n",vp
->v_type
);
1017 bp
->b_flags
|= B_ERROR
;
1018 bp
->b_error
= EINVAL
;
1024 * If we only need to commit, try to commit. If this fails
1025 * it will chain through to the write. Basically all the logic
1026 * in nfs_doio() is replicated.
1028 KKASSERT(bp
->b_cmd
== BUF_CMD_WRITE
);
1029 if (bp
->b_flags
& B_NEEDCOMMIT
)
1030 nfs_commitrpc_bio(vp
, bio
);
1032 nfs_writerpc_bio(vp
, bio
);
1037 nfs_doio(struct vnode
*vp
, struct bio
*bio
, struct thread
*td
)
1039 struct buf
*bp
= bio
->bio_buf
;
1042 struct nfsmount
*nmp
;
1044 int iomode
, must_commit
;
1051 * Shortcut swap cache (not done automatically because we are not
1054 * XXX The biowait is a hack until we can figure out how to stop a
1055 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1056 * set so the bp shouldn't get ripped out from under us. The only
1057 * use-cases are fully synchronous I/O cases.
1059 * XXX This is having problems, give up for now.
1061 if (vn_cache_strategy(vp
, bio
)) {
1062 error
= biowait(&bio
->bio_buf
->b_bio1
, "nfsrsw");
1067 KKASSERT(vp
->v_tag
== VT_NFS
);
1069 nmp
= VFSTONFS(vp
->v_mount
);
1071 uiop
->uio_iov
= &io
;
1072 uiop
->uio_iovcnt
= 1;
1073 uiop
->uio_segflg
= UIO_SYSSPACE
;
1077 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1078 * do this here so we do not have to do it in all the code that
1081 bp
->b_flags
&= ~(B_ERROR
| B_INVAL
);
1083 KASSERT(bp
->b_cmd
!= BUF_CMD_DONE
,
1084 ("nfs_doio: bp %p already marked done!", bp
));
1086 if (bp
->b_cmd
== BUF_CMD_READ
) {
1087 io
.iov_len
= uiop
->uio_resid
= (size_t)bp
->b_bcount
;
1088 io
.iov_base
= bp
->b_data
;
1089 uiop
->uio_rw
= UIO_READ
;
1091 switch (vp
->v_type
) {
1094 * When reading from a regular file zero-fill any residual.
1095 * Note that this residual has nothing to do with NFS short
1096 * reads, which nfs_readrpc_uio() will handle for us.
1098 * We have to do this because when we are write extending
1099 * a file the server may not have the same notion of
1100 * filesize as we do. Our BIOs should already be sized
1101 * (b_bcount) to account for the file EOF.
1103 nfsstats
.read_bios
++;
1104 uiop
->uio_offset
= bio
->bio_offset
;
1105 error
= nfs_readrpc_uio(vp
, uiop
);
1106 if (error
== 0 && uiop
->uio_resid
) {
1107 n
= (size_t)bp
->b_bcount
- uiop
->uio_resid
;
1108 bzero(bp
->b_data
+ n
, bp
->b_bcount
- n
);
1109 uiop
->uio_resid
= 0;
1111 if (td
&& td
->td_proc
&& (vp
->v_flag
& VTEXT
) &&
1112 np
->n_mtime
!= np
->n_vattr
.va_mtime
.tv_sec
) {
1113 uprintf("Process killed due to text file modification\n");
1114 ksignal(td
->td_proc
, SIGKILL
);
1118 uiop
->uio_offset
= 0;
1119 nfsstats
.readlink_bios
++;
1120 error
= nfs_readlinkrpc_uio(vp
, uiop
);
1123 nfsstats
.readdir_bios
++;
1124 uiop
->uio_offset
= bio
->bio_offset
;
1125 if (nmp
->nm_flag
& NFSMNT_RDIRPLUS
) {
1126 error
= nfs_readdirplusrpc_uio(vp
, uiop
);
1127 if (error
== NFSERR_NOTSUPP
)
1128 nmp
->nm_flag
&= ~NFSMNT_RDIRPLUS
;
1130 if ((nmp
->nm_flag
& NFSMNT_RDIRPLUS
) == 0)
1131 error
= nfs_readdirrpc_uio(vp
, uiop
);
1133 * end-of-directory sets B_INVAL but does not generate an
1136 if (error
== 0 && uiop
->uio_resid
== bp
->b_bcount
)
1137 bp
->b_flags
|= B_INVAL
;
1140 kprintf("nfs_doio: type %x unexpected\n",vp
->v_type
);
1144 bp
->b_flags
|= B_ERROR
;
1145 bp
->b_error
= error
;
1147 bp
->b_resid
= uiop
->uio_resid
;
1150 * If we only need to commit, try to commit.
1152 * NOTE: The I/O has already been staged for the write and
1153 * its pages busied, so b_dirtyoff/end is valid.
1155 KKASSERT(bp
->b_cmd
== BUF_CMD_WRITE
);
1156 if (bp
->b_flags
& B_NEEDCOMMIT
) {
1160 off
= bio
->bio_offset
+ bp
->b_dirtyoff
;
1161 retv
= nfs_commitrpc_uio(vp
, off
,
1162 bp
->b_dirtyend
- bp
->b_dirtyoff
,
1165 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1166 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
1171 if (retv
== NFSERR_STALEWRITEVERF
) {
1172 nfs_clearcommit(vp
->v_mount
);
1177 * Setup for actual write
1179 if (bio
->bio_offset
+ bp
->b_dirtyend
> np
->n_size
)
1180 bp
->b_dirtyend
= np
->n_size
- bio
->bio_offset
;
1182 if (bp
->b_dirtyend
> bp
->b_dirtyoff
) {
1183 io
.iov_len
= uiop
->uio_resid
= bp
->b_dirtyend
1185 uiop
->uio_offset
= bio
->bio_offset
+ bp
->b_dirtyoff
;
1186 io
.iov_base
= (char *)bp
->b_data
+ bp
->b_dirtyoff
;
1187 uiop
->uio_rw
= UIO_WRITE
;
1188 nfsstats
.write_bios
++;
1190 if ((bp
->b_flags
& (B_NEEDCOMMIT
| B_NOCACHE
| B_CLUSTER
)) == 0)
1191 iomode
= NFSV3WRITE_UNSTABLE
;
1193 iomode
= NFSV3WRITE_FILESYNC
;
1196 error
= nfs_writerpc_uio(vp
, uiop
, &iomode
, &must_commit
);
1199 * We no longer try to use kern/vfs_bio's cluster code to
1200 * cluster commits, so B_CLUSTEROK is no longer set with
1201 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1202 * may have to clear B_NEEDCOMMIT if it finds underlying
1203 * pages have been redirtied through a memory mapping
1204 * and doing this on a clustered bp will probably cause
1205 * a panic, plus the flag in the underlying NFS bufs
1206 * making up the cluster bp will not be properly cleared.
1208 if (!error
&& iomode
== NFSV3WRITE_UNSTABLE
) {
1209 bp
->b_flags
|= B_NEEDCOMMIT
;
1211 /* XXX do not enable commit clustering */
1212 if (bp
->b_dirtyoff
== 0
1213 && bp
->b_dirtyend
== bp
->b_bcount
)
1214 bp
->b_flags
|= B_CLUSTEROK
;
1217 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
1221 * For an interrupted write, the buffer is still valid
1222 * and the write hasn't been pushed to the server yet,
1223 * so we can't set B_ERROR and report the interruption
1224 * by setting B_EINTR. For the async case, B_EINTR
1225 * is not relevant, so the rpc attempt is essentially
1226 * a noop. For the case of a V3 write rpc not being
1227 * committed to stable storage, the block is still
1228 * dirty and requires either a commit rpc or another
1229 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1230 * the block is reused. This is indicated by setting
1231 * the B_DELWRI and B_NEEDCOMMIT flags.
1233 * If the buffer is marked B_PAGING, it does not reside on
1234 * the vp's paging queues so we cannot call bdirty(). The
1235 * bp in this case is not an NFS cache block so we should
1239 || (!error
&& (bp
->b_flags
& B_NEEDCOMMIT
))) {
1241 bp
->b_flags
&= ~(B_INVAL
|B_NOCACHE
);
1242 if ((bp
->b_flags
& B_PAGING
) == 0)
1245 bp
->b_flags
|= B_EINTR
;
1249 bp
->b_flags
|= B_ERROR
;
1250 bp
->b_error
= np
->n_error
= error
;
1251 np
->n_flag
|= NWRITEERR
;
1253 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1256 nfs_clearcommit(vp
->v_mount
);
1257 bp
->b_resid
= uiop
->uio_resid
;
1264 * I/O was run synchronously, biodone() it and calculate the
1268 KKASSERT(bp
->b_cmd
== BUF_CMD_DONE
);
1269 if (bp
->b_flags
& B_EINTR
)
1271 if (bp
->b_flags
& B_ERROR
)
1272 return (bp
->b_error
? bp
->b_error
: EIO
);
1277 * Handle all truncation, write-extend, and ftruncate()-extend operations
1278 * on the NFS lcient side.
1280 * We use the new API in kern/vfs_vm.c to perform these operations in a
1281 * VM-friendly way. With this API VM pages are properly zerod and pages
1282 * still mapped into the buffer straddling EOF are not invalidated.
1285 nfs_meta_setsize(struct vnode
*vp
, struct thread
*td
, off_t nsize
, int trivial
)
1287 struct nfsnode
*np
= VTONFS(vp
);
1289 int biosize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1295 if (nsize
< osize
) {
1296 error
= nvtruncbuf(vp
, nsize
, biosize
, -1, 0);
1298 error
= nvextendbuf(vp
, osize
, nsize
,
1299 biosize
, biosize
, -1, -1,
1306 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1307 * Caller is responsible for brelse()'ing the bp.
1310 nfsiodone_sync(struct bio
*bio
)
1313 bpdone(bio
->bio_buf
, 0);
1317 * nfs read rpc - BIO version
1320 nfs_readrpc_bio(struct vnode
*vp
, struct bio
*bio
)
1322 struct buf
*bp
= bio
->bio_buf
;
1324 struct nfsmount
*nmp
;
1325 int error
= 0, len
, tsiz
;
1326 struct nfsm_info
*info
;
1328 info
= kmalloc(sizeof(*info
), M_NFSREQ
, M_WAITOK
);
1330 info
->v3
= NFS_ISV3(vp
);
1332 nmp
= VFSTONFS(vp
->v_mount
);
1333 tsiz
= bp
->b_bcount
;
1334 KKASSERT(tsiz
<= nmp
->nm_rsize
);
1335 if (bio
->bio_offset
+ tsiz
> nmp
->nm_maxfilesize
) {
1339 nfsstats
.rpccnt
[NFSPROC_READ
]++;
1341 nfsm_reqhead(info
, vp
, NFSPROC_READ
,
1342 NFSX_FH(info
->v3
) + NFSX_UNSIGNED
* 3);
1343 ERROROUT(nfsm_fhtom(info
, vp
));
1344 tl
= nfsm_build(info
, NFSX_UNSIGNED
* 3);
1346 txdr_hyper(bio
->bio_offset
, tl
);
1347 *(tl
+ 2) = txdr_unsigned(len
);
1349 *tl
++ = txdr_unsigned(bio
->bio_offset
);
1350 *tl
++ = txdr_unsigned(len
);
1354 info
->done
= nfs_readrpc_bio_done
;
1355 nfsm_request_bio(info
, vp
, NFSPROC_READ
, NULL
,
1356 nfs_vpcred(vp
, ND_READ
));
1359 kfree(info
, M_NFSREQ
);
1360 bp
->b_error
= error
;
1361 bp
->b_flags
|= B_ERROR
;
1366 nfs_readrpc_bio_done(nfsm_info_t info
)
1368 struct nfsmount
*nmp
= VFSTONFS(info
->vp
->v_mount
);
1369 struct bio
*bio
= info
->bio
;
1370 struct buf
*bp
= bio
->bio_buf
;
1377 KKASSERT(info
->state
== NFSM_STATE_DONE
);
1379 lwkt_gettoken(&nmp
->nm_token
);
1381 ERROROUT(info
->error
);
1383 ERROROUT(nfsm_postop_attr(info
, info
->vp
, &attrflag
,
1384 NFS_LATTR_NOSHRINK
));
1385 NULLOUT(tl
= nfsm_dissect(info
, 2 * NFSX_UNSIGNED
));
1386 eof
= fxdr_unsigned(int, *(tl
+ 1));
1388 ERROROUT(nfsm_loadattr(info
, info
->vp
, NULL
));
1391 NEGATIVEOUT(retlen
= nfsm_strsiz(info
, nmp
->nm_rsize
));
1392 ERROROUT(nfsm_mtobio(info
, bio
, retlen
));
1393 m_freem(info
->mrep
);
1397 * No error occured, if retlen is less then bcount and no EOF
1398 * and NFSv3 a zero-fill short read occured.
1400 * For NFSv2 a short-read indicates EOF.
1402 if (retlen
< bp
->b_bcount
&& info
->v3
&& eof
== 0) {
1403 bzero(bp
->b_data
+ retlen
, bp
->b_bcount
- retlen
);
1404 retlen
= bp
->b_bcount
;
1408 * If we hit an EOF we still zero-fill, but return the expected
1409 * b_resid anyway. This should normally not occur since async
1410 * BIOs are not used for read-before-write case. Races against
1411 * the server can cause it though and we don't want to leave
1412 * garbage in the buffer.
1414 if (retlen
< bp
->b_bcount
) {
1415 bzero(bp
->b_data
+ retlen
, bp
->b_bcount
- retlen
);
1418 /* bp->b_resid = bp->b_bcount - retlen; */
1420 lwkt_reltoken(&nmp
->nm_token
);
1421 kfree(info
, M_NFSREQ
);
1423 bp
->b_error
= error
;
1424 bp
->b_flags
|= B_ERROR
;
1430 * nfs write call - BIO version
1432 * NOTE: Caller has already busied the I/O.
1435 nfs_writerpc_bio(struct vnode
*vp
, struct bio
*bio
)
1437 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
1438 struct nfsnode
*np
= VTONFS(vp
);
1439 struct buf
*bp
= bio
->bio_buf
;
1444 struct nfsm_info
*info
;
1448 * Setup for actual write. Just clean up the bio if there
1449 * is nothing to do. b_dirtyoff/end have already been staged
1450 * by the bp's pages getting busied.
1452 if (bio
->bio_offset
+ bp
->b_dirtyend
> np
->n_size
)
1453 bp
->b_dirtyend
= np
->n_size
- bio
->bio_offset
;
1455 if (bp
->b_dirtyend
<= bp
->b_dirtyoff
) {
1460 len
= bp
->b_dirtyend
- bp
->b_dirtyoff
;
1461 offset
= bio
->bio_offset
+ bp
->b_dirtyoff
;
1462 if (offset
+ len
> nmp
->nm_maxfilesize
) {
1463 bp
->b_flags
|= B_ERROR
;
1464 bp
->b_error
= EFBIG
;
1469 nfsstats
.write_bios
++;
1471 info
= kmalloc(sizeof(*info
), M_NFSREQ
, M_WAITOK
);
1473 info
->v3
= NFS_ISV3(vp
);
1474 info
->info_writerpc
.must_commit
= 0;
1475 if ((bp
->b_flags
& (B_NEEDCOMMIT
| B_NOCACHE
| B_CLUSTER
)) == 0)
1476 iomode
= NFSV3WRITE_UNSTABLE
;
1478 iomode
= NFSV3WRITE_FILESYNC
;
1480 KKASSERT(len
<= nmp
->nm_wsize
);
1482 nfsstats
.rpccnt
[NFSPROC_WRITE
]++;
1483 nfsm_reqhead(info
, vp
, NFSPROC_WRITE
,
1484 NFSX_FH(info
->v3
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
1485 ERROROUT(nfsm_fhtom(info
, vp
));
1487 tl
= nfsm_build(info
, 5 * NFSX_UNSIGNED
);
1488 txdr_hyper(offset
, tl
);
1490 *tl
++ = txdr_unsigned(len
);
1491 *tl
++ = txdr_unsigned(iomode
);
1492 *tl
= txdr_unsigned(len
);
1496 tl
= nfsm_build(info
, 4 * NFSX_UNSIGNED
);
1497 /* Set both "begin" and "current" to non-garbage. */
1498 x
= txdr_unsigned((u_int32_t
)offset
);
1499 *tl
++ = x
; /* "begin offset" */
1500 *tl
++ = x
; /* "current offset" */
1501 x
= txdr_unsigned(len
);
1502 *tl
++ = x
; /* total to this offset */
1503 *tl
= x
; /* size of this write */
1505 ERROROUT(nfsm_biotom(info
, bio
, bp
->b_dirtyoff
, len
));
1507 info
->done
= nfs_writerpc_bio_done
;
1508 nfsm_request_bio(info
, vp
, NFSPROC_WRITE
, NULL
,
1509 nfs_vpcred(vp
, ND_WRITE
));
1512 kfree(info
, M_NFSREQ
);
1513 bp
->b_error
= error
;
1514 bp
->b_flags
|= B_ERROR
;
1519 nfs_writerpc_bio_done(nfsm_info_t info
)
1521 struct nfsmount
*nmp
= VFSTONFS(info
->vp
->v_mount
);
1522 struct nfsnode
*np
= VTONFS(info
->vp
);
1523 struct bio
*bio
= info
->bio
;
1524 struct buf
*bp
= bio
->bio_buf
;
1525 int wccflag
= NFSV3_WCCRATTR
;
1526 int iomode
= NFSV3WRITE_FILESYNC
;
1530 int len
= bp
->b_resid
; /* b_resid was set to shortened length */
1533 lwkt_gettoken(&nmp
->nm_token
);
1535 ERROROUT(info
->error
);
1538 * The write RPC returns a before and after mtime. The
1539 * nfsm_wcc_data() macro checks the before n_mtime
1540 * against the before time and stores the after time
1541 * in the nfsnode's cached vattr and n_mtime field.
1542 * The NRMODIFIED bit will be set if the before
1543 * time did not match the original mtime.
1545 wccflag
= NFSV3_WCCCHK
;
1546 ERROROUT(nfsm_wcc_data(info
, info
->vp
, &wccflag
));
1548 NULLOUT(tl
= nfsm_dissect(info
, 2 * NFSX_UNSIGNED
+ NFSX_V3WRITEVERF
));
1549 rlen
= fxdr_unsigned(int, *tl
++);
1552 m_freem(info
->mrep
);
1555 } else if (rlen
< len
) {
1558 * XXX what do we do here?
1560 backup
= len
- rlen
;
1561 uiop
->uio_iov
->iov_base
= (char *)uiop
->uio_iov
->iov_base
- backup
;
1562 uiop
->uio_iov
->iov_len
+= backup
;
1563 uiop
->uio_offset
-= backup
;
1564 uiop
->uio_resid
+= backup
;
1568 commit
= fxdr_unsigned(int, *tl
++);
1571 * Return the lowest committment level
1572 * obtained by any of the RPCs.
1574 if (iomode
== NFSV3WRITE_FILESYNC
)
1576 else if (iomode
== NFSV3WRITE_DATASYNC
&&
1577 commit
== NFSV3WRITE_UNSTABLE
)
1579 if ((nmp
->nm_state
& NFSSTA_HASWRITEVERF
) == 0){
1580 bcopy(tl
, (caddr_t
)nmp
->nm_verf
, NFSX_V3WRITEVERF
);
1581 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
1582 } else if (bcmp(tl
, nmp
->nm_verf
, NFSX_V3WRITEVERF
)) {
1583 info
->info_writerpc
.must_commit
= 1;
1584 bcopy(tl
, (caddr_t
)nmp
->nm_verf
, NFSX_V3WRITEVERF
);
1588 ERROROUT(nfsm_loadattr(info
, info
->vp
, NULL
));
1590 m_freem(info
->mrep
);
1594 if (info
->vp
->v_mount
->mnt_flag
& MNT_ASYNC
)
1595 iomode
= NFSV3WRITE_FILESYNC
;
1599 * End of RPC. Now clean up the bp.
1601 * We no longer enable write clustering for commit operations,
1602 * See around line 1157 for a more detailed comment.
1604 if (!error
&& iomode
== NFSV3WRITE_UNSTABLE
) {
1605 bp
->b_flags
|= B_NEEDCOMMIT
;
1607 /* XXX do not enable commit clustering */
1608 if (bp
->b_dirtyoff
== 0 && bp
->b_dirtyend
== bp
->b_bcount
)
1609 bp
->b_flags
|= B_CLUSTEROK
;
1612 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
1616 * For an interrupted write, the buffer is still valid
1617 * and the write hasn't been pushed to the server yet,
1618 * so we can't set B_ERROR and report the interruption
1619 * by setting B_EINTR. For the async case, B_EINTR
1620 * is not relevant, so the rpc attempt is essentially
1621 * a noop. For the case of a V3 write rpc not being
1622 * committed to stable storage, the block is still
1623 * dirty and requires either a commit rpc or another
1624 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1625 * the block is reused. This is indicated by setting
1626 * the B_DELWRI and B_NEEDCOMMIT flags.
1628 * If the buffer is marked B_PAGING, it does not reside on
1629 * the vp's paging queues so we cannot call bdirty(). The
1630 * bp in this case is not an NFS cache block so we should
1633 if (error
== EINTR
|| (!error
&& (bp
->b_flags
& B_NEEDCOMMIT
))) {
1635 bp
->b_flags
&= ~(B_INVAL
|B_NOCACHE
);
1636 if ((bp
->b_flags
& B_PAGING
) == 0)
1639 bp
->b_flags
|= B_EINTR
;
1643 bp
->b_flags
|= B_ERROR
;
1644 bp
->b_error
= np
->n_error
= error
;
1645 np
->n_flag
|= NWRITEERR
;
1647 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1649 if (info
->info_writerpc
.must_commit
)
1650 nfs_clearcommit(info
->vp
->v_mount
);
1651 lwkt_reltoken(&nmp
->nm_token
);
1653 kfree(info
, M_NFSREQ
);
1655 bp
->b_flags
|= B_ERROR
;
1656 bp
->b_error
= error
;
1662 * Nfs Version 3 commit rpc - BIO version
1664 * This function issues the commit rpc and will chain to a write
1668 nfs_commitrpc_bio(struct vnode
*vp
, struct bio
*bio
)
1670 struct nfsmount
*nmp
= VFSTONFS(vp
->v_mount
);
1671 struct buf
*bp
= bio
->bio_buf
;
1672 struct nfsm_info
*info
;
1676 if ((nmp
->nm_state
& NFSSTA_HASWRITEVERF
) == 0) {
1677 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1678 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
1684 info
= kmalloc(sizeof(*info
), M_NFSREQ
, M_WAITOK
);
1688 nfsstats
.rpccnt
[NFSPROC_COMMIT
]++;
1689 nfsm_reqhead(info
, vp
, NFSPROC_COMMIT
, NFSX_FH(1));
1690 ERROROUT(nfsm_fhtom(info
, vp
));
1691 tl
= nfsm_build(info
, 3 * NFSX_UNSIGNED
);
1692 txdr_hyper(bio
->bio_offset
+ bp
->b_dirtyoff
, tl
);
1694 *tl
= txdr_unsigned(bp
->b_dirtyend
- bp
->b_dirtyoff
);
1696 info
->done
= nfs_commitrpc_bio_done
;
1697 nfsm_request_bio(info
, vp
, NFSPROC_COMMIT
, NULL
,
1698 nfs_vpcred(vp
, ND_WRITE
));
1702 * Chain to write RPC on (early) error
1704 kfree(info
, M_NFSREQ
);
1705 nfs_writerpc_bio(vp
, bio
);
1709 nfs_commitrpc_bio_done(nfsm_info_t info
)
1711 struct nfsmount
*nmp
= VFSTONFS(info
->vp
->v_mount
);
1712 struct bio
*bio
= info
->bio
;
1713 struct buf
*bp
= bio
->bio_buf
;
1715 int wccflag
= NFSV3_WCCRATTR
;
1718 lwkt_gettoken(&nmp
->nm_token
);
1720 ERROROUT(info
->error
);
1721 ERROROUT(nfsm_wcc_data(info
, info
->vp
, &wccflag
));
1723 NULLOUT(tl
= nfsm_dissect(info
, NFSX_V3WRITEVERF
));
1724 if (bcmp(nmp
->nm_verf
, tl
, NFSX_V3WRITEVERF
)) {
1725 bcopy(tl
, nmp
->nm_verf
, NFSX_V3WRITEVERF
);
1726 error
= NFSERR_STALEWRITEVERF
;
1729 m_freem(info
->mrep
);
1733 * On completion we must chain to a write bio if an
1738 bp
->b_dirtyoff
= bp
->b_dirtyend
= 0;
1739 bp
->b_flags
&= ~(B_NEEDCOMMIT
| B_CLUSTEROK
);
1743 nfs_writerpc_bio(info
->vp
, bio
);
1745 kfree(info
, M_NFSREQ
);
1746 lwkt_reltoken(&nmp
->nm_token
);