nfs_bio.c revision 1.188.12.1 1 1.188.12.1 tls /* $NetBSD: nfs_bio.c,v 1.188.12.1 2014/08/20 00:04:36 tls Exp $ */
2 1.15 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.107 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.24 fvdl * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
35 1.1 cgd */
36 1.71 lukem
37 1.71 lukem #include <sys/cdefs.h>
38 1.188.12.1 tls __KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.188.12.1 2014/08/20 00:04:36 tls Exp $");
39 1.1 cgd
40 1.181 ad #ifdef _KERNEL_OPT
41 1.51 bjh21 #include "opt_nfs.h"
42 1.54 chs #include "opt_ddb.h"
43 1.181 ad #endif
44 1.51 bjh21
45 1.8 mycroft #include <sys/param.h>
46 1.8 mycroft #include <sys/systm.h>
47 1.12 mycroft #include <sys/resourcevar.h>
48 1.24 fvdl #include <sys/signalvar.h>
49 1.8 mycroft #include <sys/proc.h>
50 1.8 mycroft #include <sys/buf.h>
51 1.8 mycroft #include <sys/vnode.h>
52 1.8 mycroft #include <sys/mount.h>
53 1.12 mycroft #include <sys/kernel.h>
54 1.23 christos #include <sys/namei.h>
55 1.34 fvdl #include <sys/dirent.h>
56 1.143 elad #include <sys/kauth.h>
57 1.1 cgd
58 1.41 mrg #include <uvm/uvm_extern.h>
59 1.54 chs #include <uvm/uvm.h>
60 1.41 mrg
61 1.12 mycroft #include <nfs/rpcv2.h>
62 1.24 fvdl #include <nfs/nfsproto.h>
63 1.8 mycroft #include <nfs/nfs.h>
64 1.8 mycroft #include <nfs/nfsmount.h>
65 1.24 fvdl #include <nfs/nfsnode.h>
66 1.23 christos #include <nfs/nfs_var.h>
67 1.1 cgd
68 1.12 mycroft extern int nfs_numasync;
69 1.74 chs extern int nfs_commitsize;
70 1.24 fvdl extern struct nfsstats nfsstats;
71 1.1 cgd
72 1.183 dsl static int nfs_doio_read(struct buf *, struct uio *);
73 1.183 dsl static int nfs_doio_write(struct buf *, struct uio *);
74 1.183 dsl static int nfs_doio_phys(struct buf *, struct uio *);
75 1.91 yamt
76 1.1 cgd /*
77 1.1 cgd * Vnode op for read using bio
78 1.1 cgd * Any similarity to readip() is purely coincidental
79 1.1 cgd */
80 1.23 christos int
81 1.174 yamt nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag,
82 1.174 yamt kauth_cred_t cred, int cflag)
83 1.1 cgd {
84 1.48 augustss struct nfsnode *np = VTONFS(vp);
85 1.23 christos struct buf *bp = NULL, *rabp;
86 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
87 1.35 fvdl struct nfsdircache *ndp = NULL, *nndp = NULL;
88 1.151 christos void *baddr;
89 1.54 chs int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
90 1.34 fvdl int enough = 0;
91 1.133 christos struct dirent *dp, *pdp, *edp, *ep;
92 1.54 chs off_t curoff = 0;
93 1.138 yamt int advice;
94 1.142 yamt struct lwp *l = curlwp;
95 1.1 cgd
96 1.1 cgd #ifdef DIAGNOSTIC
97 1.1 cgd if (uio->uio_rw != UIO_READ)
98 1.1 cgd panic("nfs_read mode");
99 1.1 cgd #endif
100 1.1 cgd if (uio->uio_resid == 0)
101 1.1 cgd return (0);
102 1.34 fvdl if (vp->v_type != VDIR && uio->uio_offset < 0)
103 1.1 cgd return (EINVAL);
104 1.51 bjh21 #ifndef NFS_V2_ONLY
105 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
106 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
107 1.139 christos (void)nfs_fsinfo(nmp, vp, cred, l);
108 1.51 bjh21 #endif
109 1.34 fvdl if (vp->v_type != VDIR &&
110 1.34 fvdl (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
111 1.33 fvdl return (EFBIG);
112 1.54 chs
113 1.1 cgd /*
114 1.12 mycroft * For nfs, cache consistency can only be maintained approximately.
115 1.12 mycroft * Although RFC1094 does not specify the criteria, the following is
116 1.12 mycroft * believed to be compatible with the reference port.
117 1.146 yamt *
118 1.1 cgd * If the file's modify time on the server has changed since the
119 1.1 cgd * last read rpc or you have written to the file,
120 1.1 cgd * you may have lost data cache consistency with the
121 1.1 cgd * server, so flush all of the file's data out of the cache.
122 1.1 cgd * Then force a getattr rpc to ensure that you have up to date
123 1.1 cgd * attributes.
124 1.1 cgd * NB: This implies that cache data can be read when up to
125 1.169 yamt * nfs_attrtimeo seconds out of date. If you find that you need current
126 1.1 cgd * attributes this could be forced by setting n_attrstamp to 0 before
127 1.12 mycroft * the VOP_GETATTR() call.
128 1.1 cgd */
129 1.54 chs
130 1.146 yamt if (vp->v_type != VLNK) {
131 1.139 christos error = nfs_flushstalebuf(vp, cred, l,
132 1.123 yamt NFS_FLUSHSTALEBUF_MYWRITE);
133 1.123 yamt if (error)
134 1.123 yamt return error;
135 1.1 cgd }
136 1.54 chs
137 1.1 cgd do {
138 1.26 fvdl /*
139 1.26 fvdl * Don't cache symlinks.
140 1.26 fvdl */
141 1.168 ad if ((vp->v_vflag & VV_ROOT) && vp->v_type == VLNK) {
142 1.146 yamt return (nfs_readlinkrpc(vp, uio, cred));
143 1.12 mycroft }
144 1.151 christos baddr = (void *)0;
145 1.1 cgd switch (vp->v_type) {
146 1.1 cgd case VREG:
147 1.1 cgd nfsstats.biocache_reads++;
148 1.12 mycroft
149 1.138 yamt advice = IO_ADV_DECODE(ioflag);
150 1.54 chs error = 0;
151 1.137 yamt while (uio->uio_resid > 0) {
152 1.137 yamt vsize_t bytelen;
153 1.54 chs
154 1.137 yamt nfs_delayedtruncate(vp);
155 1.137 yamt if (np->n_size <= uio->uio_offset) {
156 1.137 yamt break;
157 1.137 yamt }
158 1.137 yamt bytelen =
159 1.137 yamt MIN(np->n_size - uio->uio_offset, uio->uio_resid);
160 1.187 rmind error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
161 1.187 rmind UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
162 1.54 chs if (error) {
163 1.136 yamt /*
164 1.136 yamt * XXXkludge
165 1.136 yamt * the file has been truncated on the server.
166 1.136 yamt * there isn't much we can do.
167 1.136 yamt */
168 1.136 yamt if (uio->uio_offset >= np->n_size) {
169 1.136 yamt /* end of file */
170 1.136 yamt error = 0;
171 1.137 yamt } else {
172 1.137 yamt break;
173 1.136 yamt }
174 1.12 mycroft }
175 1.12 mycroft }
176 1.54 chs break;
177 1.12 mycroft
178 1.1 cgd case VLNK:
179 1.1 cgd nfsstats.biocache_readlinks++;
180 1.188 christos bp = nfs_getcacheblk(vp, (daddr_t)0, MAXPATHLEN, l);
181 1.12 mycroft if (!bp)
182 1.12 mycroft return (EINTR);
183 1.172 ad if ((bp->b_oflags & BO_DONE) == 0) {
184 1.12 mycroft bp->b_flags |= B_READ;
185 1.129 christos error = nfs_doio(bp);
186 1.24 fvdl if (error) {
187 1.167 ad brelse(bp, 0);
188 1.12 mycroft return (error);
189 1.12 mycroft }
190 1.12 mycroft }
191 1.188 christos n = MIN(uio->uio_resid, MAXPATHLEN - bp->b_resid);
192 1.12 mycroft got_buf = 1;
193 1.1 cgd on = 0;
194 1.1 cgd break;
195 1.1 cgd case VDIR:
196 1.34 fvdl diragain:
197 1.34 fvdl nfsstats.biocache_readdirs++;
198 1.35 fvdl ndp = nfs_searchdircache(vp, uio->uio_offset,
199 1.35 fvdl (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
200 1.35 fvdl if (!ndp) {
201 1.35 fvdl /*
202 1.35 fvdl * We've been handed a cookie that is not
203 1.35 fvdl * in the cache. If we're not translating
204 1.35 fvdl * 32 <-> 64, it may be a value that was
205 1.35 fvdl * flushed out of the cache because it grew
206 1.35 fvdl * too big. Let the server judge if it's
207 1.35 fvdl * valid or not. In the translation case,
208 1.35 fvdl * we have no way of validating this value,
209 1.35 fvdl * so punt.
210 1.35 fvdl */
211 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
212 1.35 fvdl return (EINVAL);
213 1.128 perry ndp = nfs_enterdircache(vp, uio->uio_offset,
214 1.35 fvdl uio->uio_offset, 0, 0);
215 1.35 fvdl }
216 1.35 fvdl
217 1.125 yamt if (NFS_EOFVALID(np) &&
218 1.35 fvdl ndp->dc_cookie == np->n_direofoffset) {
219 1.120 yamt nfs_putdircache(np, ndp);
220 1.35 fvdl nfsstats.direofcache_hits++;
221 1.18 mycroft return (0);
222 1.35 fvdl }
223 1.35 fvdl
224 1.139 christos bp = nfs_getcacheblk(vp, NFSDC_BLKNO(ndp), NFS_DIRBLKSIZ, l);
225 1.12 mycroft if (!bp)
226 1.24 fvdl return (EINTR);
227 1.172 ad if ((bp->b_oflags & BO_DONE) == 0) {
228 1.24 fvdl bp->b_flags |= B_READ;
229 1.35 fvdl bp->b_dcookie = ndp->dc_blkcookie;
230 1.129 christos error = nfs_doio(bp);
231 1.24 fvdl if (error) {
232 1.34 fvdl /*
233 1.34 fvdl * Yuck! The directory has been modified on the
234 1.34 fvdl * server. Punt and let the userland code
235 1.34 fvdl * deal with it.
236 1.34 fvdl */
237 1.120 yamt nfs_putdircache(np, ndp);
238 1.167 ad brelse(bp, 0);
239 1.144 yamt /*
240 1.144 yamt * nfs_request maps NFSERR_BAD_COOKIE to EINVAL.
241 1.144 yamt */
242 1.144 yamt if (error == EINVAL) { /* NFSERR_BAD_COOKIE */
243 1.35 fvdl nfs_invaldircache(vp, 0);
244 1.139 christos nfs_vinvalbuf(vp, 0, cred, l, 1);
245 1.12 mycroft }
246 1.34 fvdl return (error);
247 1.38 fvdl }
248 1.40 fvdl }
249 1.40 fvdl
250 1.40 fvdl /*
251 1.40 fvdl * Just return if we hit EOF right away with this
252 1.40 fvdl * block. Always check here, because direofoffset
253 1.40 fvdl * may have been set by an nfsiod since the last
254 1.40 fvdl * check.
255 1.127 yamt *
256 1.127 yamt * also, empty block implies EOF.
257 1.40 fvdl */
258 1.127 yamt
259 1.127 yamt if (bp->b_bcount == bp->b_resid ||
260 1.127 yamt (NFS_EOFVALID(np) &&
261 1.127 yamt ndp->dc_blkcookie == np->n_direofoffset)) {
262 1.127 yamt KASSERT(bp->b_bcount != bp->b_resid ||
263 1.127 yamt ndp->dc_blkcookie == bp->b_dcookie);
264 1.120 yamt nfs_putdircache(np, ndp);
265 1.167 ad brelse(bp, BC_NOCACHE);
266 1.127 yamt return 0;
267 1.12 mycroft }
268 1.12 mycroft
269 1.12 mycroft /*
270 1.34 fvdl * Find the entry we were looking for in the block.
271 1.34 fvdl */
272 1.34 fvdl
273 1.34 fvdl en = ndp->dc_entry;
274 1.34 fvdl
275 1.34 fvdl pdp = dp = (struct dirent *)bp->b_data;
276 1.151 christos edp = (struct dirent *)(void *)((char *)bp->b_data + bp->b_bcount -
277 1.133 christos bp->b_resid);
278 1.34 fvdl enn = 0;
279 1.133 christos while (enn < en && dp < edp) {
280 1.34 fvdl pdp = dp;
281 1.133 christos dp = _DIRENT_NEXT(dp);
282 1.34 fvdl enn++;
283 1.34 fvdl }
284 1.34 fvdl
285 1.34 fvdl /*
286 1.34 fvdl * If the entry number was bigger than the number of
287 1.34 fvdl * entries in the block, or the cookie of the previous
288 1.34 fvdl * entry doesn't match, the directory cache is
289 1.34 fvdl * stale. Flush it and try again (i.e. go to
290 1.34 fvdl * the server).
291 1.34 fvdl */
292 1.134 yamt if (dp >= edp || (struct dirent *)_DIRENT_NEXT(dp) > edp ||
293 1.35 fvdl (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
294 1.34 fvdl #ifdef DEBUG
295 1.182 yamt printf("invalid cache: %p %p %p off %jx %jx\n",
296 1.37 thorpej pdp, dp, edp,
297 1.182 yamt (uintmax_t)uio->uio_offset,
298 1.182 yamt (uintmax_t)NFS_GETCOOKIE(pdp));
299 1.34 fvdl #endif
300 1.120 yamt nfs_putdircache(np, ndp);
301 1.167 ad brelse(bp, 0);
302 1.35 fvdl nfs_invaldircache(vp, 0);
303 1.139 christos nfs_vinvalbuf(vp, 0, cred, l, 0);
304 1.34 fvdl goto diragain;
305 1.34 fvdl }
306 1.34 fvdl
307 1.151 christos on = (char *)dp - (char *)bp->b_data;
308 1.34 fvdl
309 1.34 fvdl /*
310 1.34 fvdl * Cache all entries that may be exported to the
311 1.34 fvdl * user, as they may be thrown back at us. The
312 1.34 fvdl * NFSBIO_CACHECOOKIES flag indicates that all
313 1.34 fvdl * entries are being 'exported', so cache them all.
314 1.34 fvdl */
315 1.34 fvdl
316 1.34 fvdl if (en == 0 && pdp == dp) {
317 1.133 christos dp = _DIRENT_NEXT(dp);
318 1.34 fvdl enn++;
319 1.34 fvdl }
320 1.34 fvdl
321 1.65 chs if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
322 1.34 fvdl n = uio->uio_resid;
323 1.34 fvdl enough = 1;
324 1.34 fvdl } else
325 1.65 chs n = bp->b_bcount - bp->b_resid - on;
326 1.34 fvdl
327 1.151 christos ep = (struct dirent *)(void *)((char *)bp->b_data + on + n);
328 1.34 fvdl
329 1.34 fvdl /*
330 1.34 fvdl * Find last complete entry to copy, caching entries
331 1.34 fvdl * (if requested) as we go.
332 1.34 fvdl */
333 1.34 fvdl
334 1.133 christos while (dp < ep && (struct dirent *)_DIRENT_NEXT(dp) <= ep) {
335 1.35 fvdl if (cflag & NFSBIO_CACHECOOKIES) {
336 1.35 fvdl nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
337 1.35 fvdl ndp->dc_blkcookie, enn, bp->b_lblkno);
338 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
339 1.35 fvdl NFS_STASHCOOKIE32(pdp,
340 1.35 fvdl nndp->dc_cookie32);
341 1.35 fvdl }
342 1.120 yamt nfs_putdircache(np, nndp);
343 1.35 fvdl }
344 1.34 fvdl pdp = dp;
345 1.133 christos dp = _DIRENT_NEXT(dp);
346 1.34 fvdl enn++;
347 1.34 fvdl }
348 1.120 yamt nfs_putdircache(np, ndp);
349 1.34 fvdl
350 1.34 fvdl /*
351 1.34 fvdl * If the last requested entry was not the last in the
352 1.128 perry * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
353 1.34 fvdl * cache the cookie of the last requested one, and
354 1.34 fvdl * set of the offset to it.
355 1.34 fvdl */
356 1.34 fvdl
357 1.65 chs if ((on + n) < bp->b_bcount - bp->b_resid) {
358 1.34 fvdl curoff = NFS_GETCOOKIE(pdp);
359 1.35 fvdl nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
360 1.35 fvdl enn, bp->b_lblkno);
361 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
362 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
363 1.35 fvdl curoff = nndp->dc_cookie32;
364 1.35 fvdl }
365 1.120 yamt nfs_putdircache(np, nndp);
366 1.34 fvdl } else
367 1.34 fvdl curoff = bp->b_dcookie;
368 1.34 fvdl
369 1.35 fvdl /*
370 1.35 fvdl * Always cache the entry for the next block,
371 1.35 fvdl * so that readaheads can use it.
372 1.35 fvdl */
373 1.35 fvdl nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
374 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
375 1.35 fvdl if (curoff == bp->b_dcookie) {
376 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
377 1.35 fvdl curoff = nndp->dc_cookie32;
378 1.35 fvdl }
379 1.35 fvdl }
380 1.35 fvdl
381 1.151 christos n = (char *)_DIRENT_NEXT(pdp) - ((char *)bp->b_data + on);
382 1.34 fvdl
383 1.34 fvdl /*
384 1.12 mycroft * If not eof and read aheads are enabled, start one.
385 1.12 mycroft * (You need the current block first, so that you have the
386 1.24 fvdl * directory offset cookie of the next block.)
387 1.12 mycroft */
388 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
389 1.146 yamt !NFS_EOFVALID(np)) {
390 1.122 yamt rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
391 1.139 christos NFS_DIRBLKSIZ, l);
392 1.12 mycroft if (rabp) {
393 1.172 ad if ((rabp->b_oflags & (BO_DONE | BO_DELWRI)) == 0) {
394 1.35 fvdl rabp->b_dcookie = nndp->dc_cookie;
395 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
396 1.54 chs if (nfs_asyncio(rabp)) {
397 1.167 ad brelse(rabp, BC_INVAL);
398 1.12 mycroft }
399 1.19 mycroft } else
400 1.167 ad brelse(rabp, 0);
401 1.12 mycroft }
402 1.12 mycroft }
403 1.120 yamt nfs_putdircache(np, nndp);
404 1.12 mycroft got_buf = 1;
405 1.1 cgd break;
406 1.24 fvdl default:
407 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
408 1.23 christos break;
409 1.54 chs }
410 1.12 mycroft
411 1.12 mycroft if (n > 0) {
412 1.12 mycroft if (!baddr)
413 1.12 mycroft baddr = bp->b_data;
414 1.151 christos error = uiomove((char *)baddr + on, (int)n, uio);
415 1.1 cgd }
416 1.1 cgd switch (vp->v_type) {
417 1.24 fvdl case VREG:
418 1.24 fvdl break;
419 1.1 cgd case VLNK:
420 1.1 cgd n = 0;
421 1.1 cgd break;
422 1.1 cgd case VDIR:
423 1.34 fvdl uio->uio_offset = curoff;
424 1.34 fvdl if (enough)
425 1.34 fvdl n = 0;
426 1.1 cgd break;
427 1.24 fvdl default:
428 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
429 1.24 fvdl }
430 1.12 mycroft if (got_buf)
431 1.167 ad brelse(bp, 0);
432 1.12 mycroft } while (error == 0 && uio->uio_resid > 0 && n > 0);
433 1.1 cgd return (error);
434 1.1 cgd }
435 1.1 cgd
436 1.1 cgd /*
437 1.1 cgd * Vnode op for write using bio
438 1.1 cgd */
439 1.23 christos int
440 1.174 yamt nfs_write(void *v)
441 1.23 christos {
442 1.12 mycroft struct vop_write_args /* {
443 1.24 fvdl struct vnode *a_vp;
444 1.12 mycroft struct uio *a_uio;
445 1.12 mycroft int a_ioflag;
446 1.143 elad kauth_cred_t a_cred;
447 1.23 christos } */ *ap = v;
448 1.48 augustss struct uio *uio = ap->a_uio;
449 1.142 yamt struct lwp *l = curlwp;
450 1.48 augustss struct vnode *vp = ap->a_vp;
451 1.12 mycroft struct nfsnode *np = VTONFS(vp);
452 1.143 elad kauth_cred_t cred = ap->a_cred;
453 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
454 1.69 chs voff_t oldoff, origoff;
455 1.69 chs vsize_t bytelen;
456 1.155 yamt int error = 0;
457 1.124 chs int ioflag = ap->a_ioflag;
458 1.124 chs int extended = 0, wrotedata = 0;
459 1.1 cgd
460 1.1 cgd #ifdef DIAGNOSTIC
461 1.1 cgd if (uio->uio_rw != UIO_WRITE)
462 1.1 cgd panic("nfs_write mode");
463 1.1 cgd #endif
464 1.1 cgd if (vp->v_type != VREG)
465 1.1 cgd return (EIO);
466 1.12 mycroft if (np->n_flag & NWRITEERR) {
467 1.12 mycroft np->n_flag &= ~NWRITEERR;
468 1.12 mycroft return (np->n_error);
469 1.12 mycroft }
470 1.51 bjh21 #ifndef NFS_V2_ONLY
471 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
472 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
473 1.139 christos (void)nfs_fsinfo(nmp, vp, cred, l);
474 1.51 bjh21 #endif
475 1.156 yamt if (ioflag & IO_APPEND) {
476 1.156 yamt NFS_INVALIDATE_ATTRCACHE(np);
477 1.156 yamt error = nfs_flushstalebuf(vp, cred, l,
478 1.156 yamt NFS_FLUSHSTALEBUF_MYWRITE);
479 1.156 yamt if (error)
480 1.156 yamt return (error);
481 1.156 yamt uio->uio_offset = np->n_size;
482 1.184 pooka
483 1.184 pooka /*
484 1.184 pooka * This is already checked above VOP_WRITE, but recheck
485 1.184 pooka * the append case here to make sure our idea of the
486 1.184 pooka * file size is as fresh as possible.
487 1.184 pooka */
488 1.184 pooka if (uio->uio_offset + uio->uio_resid >
489 1.184 pooka l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
490 1.184 pooka mutex_enter(proc_lock);
491 1.184 pooka psignal(l->l_proc, SIGXFSZ);
492 1.184 pooka mutex_exit(proc_lock);
493 1.184 pooka return (EFBIG);
494 1.184 pooka }
495 1.1 cgd }
496 1.1 cgd if (uio->uio_offset < 0)
497 1.1 cgd return (EINVAL);
498 1.33 fvdl if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
499 1.33 fvdl return (EFBIG);
500 1.1 cgd if (uio->uio_resid == 0)
501 1.1 cgd return (0);
502 1.54 chs
503 1.69 chs origoff = uio->uio_offset;
504 1.1 cgd do {
505 1.155 yamt bool overwrite; /* if we are overwriting whole pages */
506 1.85 yamt u_quad_t oldsize;
507 1.69 chs oldoff = uio->uio_offset;
508 1.69 chs bytelen = uio->uio_resid;
509 1.12 mycroft
510 1.1 cgd nfsstats.biocache_writes++;
511 1.54 chs
512 1.85 yamt oldsize = np->n_size;
513 1.12 mycroft np->n_flag |= NMODIFIED;
514 1.54 chs if (np->n_size < uio->uio_offset + bytelen) {
515 1.54 chs np->n_size = uio->uio_offset + bytelen;
516 1.12 mycroft }
517 1.155 yamt overwrite = false;
518 1.155 yamt if ((uio->uio_offset & PAGE_MASK) == 0) {
519 1.168 ad if ((vp->v_vflag & VV_MAPPED) == 0 &&
520 1.155 yamt bytelen > PAGE_SIZE) {
521 1.155 yamt bytelen = trunc_page(bytelen);
522 1.155 yamt overwrite = true;
523 1.155 yamt } else if ((bytelen & PAGE_MASK) == 0 &&
524 1.155 yamt uio->uio_offset >= vp->v_size) {
525 1.155 yamt overwrite = true;
526 1.155 yamt }
527 1.155 yamt }
528 1.155 yamt if (vp->v_size < uio->uio_offset + bytelen) {
529 1.155 yamt uvm_vnp_setwritesize(vp, uio->uio_offset + bytelen);
530 1.155 yamt }
531 1.155 yamt error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
532 1.162 yamt UVM_ADV_RANDOM, UBC_WRITE | UBC_PARTIALOK |
533 1.155 yamt (overwrite ? UBC_FAULTBUSY : 0) |
534 1.187 rmind UBC_UNMAP_FLAG(vp));
535 1.69 chs if (error) {
536 1.155 yamt uvm_vnp_setwritesize(vp, vp->v_size);
537 1.155 yamt if (overwrite && np->n_size != oldsize) {
538 1.85 yamt /*
539 1.85 yamt * backout size and free pages past eof.
540 1.85 yamt */
541 1.85 yamt np->n_size = oldsize;
542 1.186 rmind mutex_enter(vp->v_interlock);
543 1.85 yamt (void)VOP_PUTPAGES(vp, round_page(vp->v_size),
544 1.85 yamt 0, PGO_SYNCIO | PGO_FREE);
545 1.85 yamt }
546 1.69 chs break;
547 1.69 chs }
548 1.124 chs wrotedata = 1;
549 1.69 chs
550 1.69 chs /*
551 1.69 chs * update UVM's notion of the size now that we've
552 1.69 chs * copied the data into the vnode's pages.
553 1.69 chs */
554 1.69 chs
555 1.69 chs if (vp->v_size < uio->uio_offset) {
556 1.69 chs uvm_vnp_setsize(vp, uio->uio_offset);
557 1.84 jdolecek extended = 1;
558 1.69 chs }
559 1.69 chs
560 1.69 chs if ((oldoff & ~(nmp->nm_wsize - 1)) !=
561 1.54 chs (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
562 1.186 rmind mutex_enter(vp->v_interlock);
563 1.72 chs error = VOP_PUTPAGES(vp,
564 1.69 chs trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
565 1.69 chs round_page((uio->uio_offset + nmp->nm_wsize - 1) &
566 1.78 chs ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
567 1.52 fvdl }
568 1.54 chs } while (uio->uio_resid > 0);
569 1.124 chs if (wrotedata)
570 1.84 jdolecek VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
571 1.154 yamt if (error == 0 && (ioflag & IO_SYNC) != 0) {
572 1.186 rmind mutex_enter(vp->v_interlock);
573 1.72 chs error = VOP_PUTPAGES(vp,
574 1.69 chs trunc_page(origoff & ~(nmp->nm_wsize - 1)),
575 1.69 chs round_page((uio->uio_offset + nmp->nm_wsize - 1) &
576 1.69 chs ~(nmp->nm_wsize - 1)),
577 1.72 chs PGO_CLEANIT | PGO_SYNCIO);
578 1.69 chs }
579 1.54 chs return error;
580 1.12 mycroft }
581 1.12 mycroft
582 1.12 mycroft /*
583 1.12 mycroft * Get an nfs cache block.
584 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
585 1.12 mycroft * and return the block marked busy. If the calling process is
586 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
587 1.12 mycroft * NULL.
588 1.12 mycroft */
589 1.12 mycroft struct buf *
590 1.174 yamt nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct lwp *l)
591 1.12 mycroft {
592 1.48 augustss struct buf *bp;
593 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
594 1.12 mycroft
595 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
596 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
597 1.54 chs while (bp == NULL) {
598 1.139 christos if (nfs_sigintr(nmp, NULL, l))
599 1.54 chs return (NULL);
600 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
601 1.12 mycroft }
602 1.12 mycroft } else
603 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
604 1.12 mycroft return (bp);
605 1.12 mycroft }
606 1.12 mycroft
607 1.12 mycroft /*
608 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
609 1.12 mycroft * doing the flush, just wait for completion.
610 1.12 mycroft */
611 1.23 christos int
612 1.174 yamt nfs_vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred,
613 1.174 yamt struct lwp *l, int intrflg)
614 1.12 mycroft {
615 1.48 augustss struct nfsnode *np = VTONFS(vp);
616 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
617 1.188.12.1 tls int error = 0, allerror = 0, slptimeo;
618 1.172 ad bool catch;
619 1.12 mycroft
620 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
621 1.12 mycroft intrflg = 0;
622 1.12 mycroft if (intrflg) {
623 1.172 ad catch = true;
624 1.12 mycroft slptimeo = 2 * hz;
625 1.12 mycroft } else {
626 1.172 ad catch = false;
627 1.12 mycroft slptimeo = 0;
628 1.12 mycroft }
629 1.12 mycroft /*
630 1.12 mycroft * First wait for any other process doing a flush to complete.
631 1.12 mycroft */
632 1.186 rmind mutex_enter(vp->v_interlock);
633 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
634 1.12 mycroft np->n_flag |= NFLUSHWANT;
635 1.172 ad error = mtsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
636 1.186 rmind slptimeo, vp->v_interlock);
637 1.139 christos if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
638 1.186 rmind mutex_exit(vp->v_interlock);
639 1.103 yamt return EINTR;
640 1.103 yamt }
641 1.12 mycroft }
642 1.12 mycroft
643 1.12 mycroft /*
644 1.12 mycroft * Now, flush as required.
645 1.12 mycroft */
646 1.12 mycroft np->n_flag |= NFLUSHINPROG;
647 1.186 rmind mutex_exit(vp->v_interlock);
648 1.172 ad error = vinvalbuf(vp, flags, cred, l, catch, 0);
649 1.12 mycroft while (error) {
650 1.188.12.1 tls if (allerror == 0)
651 1.188.12.1 tls allerror = error;
652 1.139 christos if (intrflg && nfs_sigintr(nmp, NULL, l)) {
653 1.103 yamt error = EINTR;
654 1.103 yamt break;
655 1.12 mycroft }
656 1.139 christos error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
657 1.12 mycroft }
658 1.186 rmind mutex_enter(vp->v_interlock);
659 1.188.12.1 tls if (allerror != 0) {
660 1.188.12.1 tls /*
661 1.188.12.1 tls * Keep error from vinvalbuf so fsync/close will know.
662 1.188.12.1 tls */
663 1.188.12.1 tls np->n_error = allerror;
664 1.188.12.1 tls np->n_flag |= NWRITEERR;
665 1.188.12.1 tls }
666 1.103 yamt if (error == 0)
667 1.103 yamt np->n_flag &= ~NMODIFIED;
668 1.103 yamt np->n_flag &= ~NFLUSHINPROG;
669 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
670 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
671 1.103 yamt wakeup(&np->n_flag);
672 1.12 mycroft }
673 1.186 rmind mutex_exit(vp->v_interlock);
674 1.103 yamt return error;
675 1.12 mycroft }
676 1.12 mycroft
677 1.12 mycroft /*
678 1.128 perry * nfs_flushstalebuf: flush cache if it's stale.
679 1.123 yamt *
680 1.123 yamt * => caller shouldn't own any pages or buffers which belong to the vnode.
681 1.123 yamt */
682 1.123 yamt
683 1.123 yamt int
684 1.143 elad nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
685 1.123 yamt int flags)
686 1.123 yamt {
687 1.123 yamt struct nfsnode *np = VTONFS(vp);
688 1.123 yamt struct vattr vattr;
689 1.123 yamt int error;
690 1.123 yamt
691 1.123 yamt if (np->n_flag & NMODIFIED) {
692 1.123 yamt if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
693 1.123 yamt || vp->v_type != VREG) {
694 1.139 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
695 1.123 yamt if (error)
696 1.123 yamt return error;
697 1.123 yamt if (vp->v_type == VDIR) {
698 1.123 yamt nfs_invaldircache(vp, 0);
699 1.123 yamt }
700 1.123 yamt } else {
701 1.123 yamt /*
702 1.123 yamt * XXX assuming writes are ours.
703 1.123 yamt */
704 1.123 yamt }
705 1.123 yamt NFS_INVALIDATE_ATTRCACHE(np);
706 1.170 pooka error = VOP_GETATTR(vp, &vattr, cred);
707 1.123 yamt if (error)
708 1.123 yamt return error;
709 1.123 yamt np->n_mtime = vattr.va_mtime;
710 1.123 yamt } else {
711 1.170 pooka error = VOP_GETATTR(vp, &vattr, cred);
712 1.123 yamt if (error)
713 1.123 yamt return error;
714 1.123 yamt if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
715 1.123 yamt if (vp->v_type == VDIR) {
716 1.123 yamt nfs_invaldircache(vp, 0);
717 1.123 yamt }
718 1.139 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
719 1.123 yamt if (error)
720 1.123 yamt return error;
721 1.123 yamt np->n_mtime = vattr.va_mtime;
722 1.123 yamt }
723 1.123 yamt }
724 1.123 yamt
725 1.123 yamt return error;
726 1.123 yamt }
727 1.123 yamt
728 1.123 yamt /*
729 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
730 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
731 1.12 mycroft * are all hung on a dead server.
732 1.12 mycroft */
733 1.69 chs
734 1.23 christos int
735 1.174 yamt nfs_asyncio(struct buf *bp)
736 1.12 mycroft {
737 1.166 yamt struct nfs_iod *iod;
738 1.48 augustss struct nfsmount *nmp;
739 1.166 yamt int slptimeo = 0, error;
740 1.153 yamt bool catch = false;
741 1.12 mycroft
742 1.12 mycroft if (nfs_numasync == 0)
743 1.12 mycroft return (EIO);
744 1.30 thorpej
745 1.30 thorpej nmp = VFSTONFS(bp->b_vp->v_mount);
746 1.30 thorpej again:
747 1.30 thorpej if (nmp->nm_flag & NFSMNT_INT)
748 1.153 yamt catch = true;
749 1.128 perry
750 1.30 thorpej /*
751 1.30 thorpej * Find a free iod to process this request.
752 1.30 thorpej */
753 1.30 thorpej
754 1.165 yamt mutex_enter(&nfs_iodlist_lock);
755 1.166 yamt iod = LIST_FIRST(&nfs_iodlist_idle);
756 1.166 yamt if (iod) {
757 1.166 yamt /*
758 1.166 yamt * Found one, so wake it up and tell it which
759 1.166 yamt * mount to process.
760 1.166 yamt */
761 1.166 yamt LIST_REMOVE(iod, nid_idle);
762 1.153 yamt mutex_enter(&iod->nid_lock);
763 1.166 yamt mutex_exit(&nfs_iodlist_lock);
764 1.166 yamt KASSERT(iod->nid_mount == NULL);
765 1.166 yamt iod->nid_mount = nmp;
766 1.166 yamt cv_signal(&iod->nid_cv);
767 1.166 yamt mutex_enter(&nmp->nm_lock);
768 1.166 yamt mutex_exit(&iod->nid_lock);
769 1.166 yamt nmp->nm_bufqiods++;
770 1.166 yamt if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
771 1.166 yamt cv_broadcast(&nmp->nm_aiocv);
772 1.30 thorpej }
773 1.166 yamt } else {
774 1.166 yamt mutex_exit(&nfs_iodlist_lock);
775 1.153 yamt mutex_enter(&nmp->nm_lock);
776 1.99 yamt }
777 1.99 yamt
778 1.153 yamt KASSERT(mutex_owned(&nmp->nm_lock));
779 1.30 thorpej
780 1.30 thorpej /*
781 1.30 thorpej * If we have an iod which can process the request, then queue
782 1.128 perry * the buffer. However, even if we have an iod, do not initiate
783 1.111 jonathan * queue cleaning if curproc is the pageout daemon. if the NFS mount
784 1.111 jonathan * is via local loopback, we may put curproc (pagedaemon) to sleep
785 1.111 jonathan * waiting for the writes to complete. But the server (ourself)
786 1.111 jonathan * may block the write, waiting for its (ie., our) pagedaemon
787 1.111 jonathan * to produce clean pages to handle the write: deadlock.
788 1.111 jonathan * XXX: start non-loopback mounts straight away? If "lots free",
789 1.111 jonathan * let pagedaemon start loopback writes anyway?
790 1.30 thorpej */
791 1.166 yamt if (nmp->nm_bufqiods > 0) {
792 1.128 perry
793 1.30 thorpej /*
794 1.30 thorpej * Ensure that the queue never grows too large.
795 1.30 thorpej */
796 1.157 ad if (curlwp == uvm.pagedaemon_lwp) {
797 1.112 jonathan /* Enque for later, to avoid free-page deadlock */
798 1.165 yamt } else while (nmp->nm_bufqlen >= 2 * nmp->nm_bufqiods) {
799 1.153 yamt if (catch) {
800 1.171 yamt error = cv_timedwait_sig(&nmp->nm_aiocv,
801 1.153 yamt &nmp->nm_lock, slptimeo);
802 1.153 yamt } else {
803 1.153 yamt error = cv_timedwait(&nmp->nm_aiocv,
804 1.153 yamt &nmp->nm_lock, slptimeo);
805 1.153 yamt }
806 1.30 thorpej if (error) {
807 1.158 rmind if (nfs_sigintr(nmp, NULL, curlwp)) {
808 1.158 rmind mutex_exit(&nmp->nm_lock);
809 1.30 thorpej return (EINTR);
810 1.158 rmind }
811 1.153 yamt if (catch) {
812 1.153 yamt catch = false;
813 1.30 thorpej slptimeo = 2 * hz;
814 1.30 thorpej }
815 1.30 thorpej }
816 1.74 chs
817 1.30 thorpej /*
818 1.30 thorpej * We might have lost our iod while sleeping,
819 1.159 yamt * so check and loop if necessary.
820 1.30 thorpej */
821 1.74 chs
822 1.153 yamt if (nmp->nm_bufqiods == 0) {
823 1.153 yamt mutex_exit(&nmp->nm_lock);
824 1.30 thorpej goto again;
825 1.153 yamt }
826 1.30 thorpej }
827 1.30 thorpej TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
828 1.30 thorpej nmp->nm_bufqlen++;
829 1.153 yamt mutex_exit(&nmp->nm_lock);
830 1.12 mycroft return (0);
831 1.74 chs }
832 1.153 yamt mutex_exit(&nmp->nm_lock);
833 1.24 fvdl
834 1.24 fvdl /*
835 1.30 thorpej * All the iods are busy on other mounts, so return EIO to
836 1.30 thorpej * force the caller to process the i/o synchronously.
837 1.24 fvdl */
838 1.74 chs
839 1.30 thorpej return (EIO);
840 1.12 mycroft }
841 1.12 mycroft
842 1.12 mycroft /*
843 1.91 yamt * nfs_doio for read.
844 1.12 mycroft */
845 1.91 yamt static int
846 1.174 yamt nfs_doio_read(struct buf *bp, struct uio *uiop)
847 1.12 mycroft {
848 1.91 yamt struct vnode *vp = bp->b_vp;
849 1.91 yamt struct nfsnode *np = VTONFS(vp);
850 1.91 yamt struct nfsmount *nmp = VFSTONFS(vp->v_mount);
851 1.91 yamt int error = 0;
852 1.12 mycroft
853 1.91 yamt uiop->uio_rw = UIO_READ;
854 1.91 yamt switch (vp->v_type) {
855 1.91 yamt case VREG:
856 1.12 mycroft nfsstats.read_bios++;
857 1.54 chs error = nfs_readrpc(vp, uiop);
858 1.54 chs if (!error && uiop->uio_resid) {
859 1.91 yamt int diff, len;
860 1.54 chs
861 1.12 mycroft /*
862 1.119 yamt * If uio_resid > 0, there is a hole in the file and
863 1.12 mycroft * no writes after the hole have been pushed to
864 1.119 yamt * the server yet or the file has been truncated
865 1.119 yamt * on the server.
866 1.12 mycroft * Just zero fill the rest of the valid area.
867 1.12 mycroft */
868 1.54 chs
869 1.119 yamt KASSERT(vp->v_size >=
870 1.119 yamt uiop->uio_offset + uiop->uio_resid);
871 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
872 1.119 yamt len = uiop->uio_resid;
873 1.119 yamt memset((char *)bp->b_data + diff, 0, len);
874 1.141 yamt uiop->uio_resid = 0;
875 1.12 mycroft }
876 1.142 yamt #if 0
877 1.171 yamt if (uiop->uio_lwp && (vp->v_iflag & VI_TEXT) &&
878 1.146 yamt timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
879 1.175 ad mutex_enter(proc_lock);
880 1.139 christos killproc(uiop->uio_lwp->l_proc, "process text file was modified");
881 1.175 ad mutex_exit(proc_lock);
882 1.86 thorpej #if 0 /* XXX NJWLWP */
883 1.139 christos uiop->uio_lwp->l_proc->p_holdcnt++;
884 1.86 thorpej #endif
885 1.12 mycroft }
886 1.142 yamt #endif
887 1.12 mycroft break;
888 1.91 yamt case VLNK:
889 1.91 yamt KASSERT(uiop->uio_offset == (off_t)0);
890 1.12 mycroft nfsstats.readlink_bios++;
891 1.132 yamt error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
892 1.12 mycroft break;
893 1.91 yamt case VDIR:
894 1.12 mycroft nfsstats.readdir_bios++;
895 1.34 fvdl uiop->uio_offset = bp->b_dcookie;
896 1.117 christos #ifndef NFS_V2_ONLY
897 1.24 fvdl if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
898 1.139 christos error = nfs_readdirplusrpc(vp, uiop,
899 1.145 ad curlwp->l_cred);
900 1.144 yamt /*
901 1.144 yamt * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
902 1.144 yamt */
903 1.144 yamt if (error == ENOTSUP)
904 1.24 fvdl nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
905 1.24 fvdl }
906 1.117 christos #else
907 1.117 christos nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
908 1.117 christos #endif
909 1.24 fvdl if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
910 1.139 christos error = nfs_readdirrpc(vp, uiop,
911 1.145 ad curlwp->l_cred);
912 1.34 fvdl if (!error) {
913 1.34 fvdl bp->b_dcookie = uiop->uio_offset;
914 1.34 fvdl }
915 1.24 fvdl break;
916 1.91 yamt default:
917 1.91 yamt printf("nfs_doio: type %x unexpected\n", vp->v_type);
918 1.12 mycroft break;
919 1.91 yamt }
920 1.172 ad bp->b_error = error;
921 1.91 yamt return error;
922 1.91 yamt }
923 1.91 yamt
924 1.91 yamt /*
925 1.91 yamt * nfs_doio for write.
926 1.91 yamt */
927 1.91 yamt static int
928 1.174 yamt nfs_doio_write(struct buf *bp, struct uio *uiop)
929 1.91 yamt {
930 1.91 yamt struct vnode *vp = bp->b_vp;
931 1.91 yamt struct nfsnode *np = VTONFS(vp);
932 1.96 yamt struct nfsmount *nmp = VFSTONFS(vp->v_mount);
933 1.91 yamt int iomode;
934 1.149 thorpej bool stalewriteverf = false;
935 1.91 yamt int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
936 1.180 christos struct vm_page **pgs, *spgs[UBC_MAX_PAGES];
937 1.117 christos #ifndef NFS_V2_ONLY
938 1.149 thorpej bool needcommit = true; /* need only COMMIT RPC */
939 1.117 christos #else
940 1.149 thorpej bool needcommit = false; /* need only COMMIT RPC */
941 1.117 christos #endif
942 1.148 thorpej bool pageprotected;
943 1.91 yamt struct uvm_object *uobj = &vp->v_uobj;
944 1.91 yamt int error;
945 1.91 yamt off_t off, cnt;
946 1.91 yamt
947 1.179 christos if (npages < __arraycount(spgs))
948 1.179 christos pgs = spgs;
949 1.179 christos else {
950 1.179 christos if ((pgs = kmem_alloc(sizeof(*pgs) * npages, KM_NOSLEEP)) ==
951 1.179 christos NULL)
952 1.179 christos return ENOMEM;
953 1.179 christos }
954 1.179 christos
955 1.91 yamt if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
956 1.91 yamt iomode = NFSV3WRITE_UNSTABLE;
957 1.12 mycroft } else {
958 1.91 yamt iomode = NFSV3WRITE_FILESYNC;
959 1.91 yamt }
960 1.74 chs
961 1.117 christos #ifndef NFS_V2_ONLY
962 1.96 yamt again:
963 1.117 christos #endif
964 1.147 yamt rw_enter(&nmp->nm_writeverflock, RW_READER);
965 1.96 yamt
966 1.91 yamt for (i = 0; i < npages; i++) {
967 1.94 yamt pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
968 1.100 yamt if (pgs[i]->uobject == uobj &&
969 1.100 yamt pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
970 1.101 yamt KASSERT(pgs[i]->flags & PG_BUSY);
971 1.100 yamt /*
972 1.100 yamt * this page belongs to our object.
973 1.100 yamt */
974 1.186 rmind mutex_enter(uobj->vmobjlock);
975 1.115 yamt /*
976 1.115 yamt * write out the page stably if it's about to
977 1.115 yamt * be released because we can't resend it
978 1.115 yamt * on the server crash.
979 1.115 yamt *
980 1.115 yamt * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
981 1.115 yamt * changed until unbusy the page.
982 1.115 yamt */
983 1.100 yamt if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
984 1.100 yamt iomode = NFSV3WRITE_FILESYNC;
985 1.115 yamt /*
986 1.115 yamt * if we met a page which hasn't been sent yet,
987 1.115 yamt * we need do WRITE RPC.
988 1.115 yamt */
989 1.100 yamt if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
990 1.149 thorpej needcommit = false;
991 1.186 rmind mutex_exit(uobj->vmobjlock);
992 1.100 yamt } else {
993 1.100 yamt iomode = NFSV3WRITE_FILESYNC;
994 1.149 thorpej needcommit = false;
995 1.91 yamt }
996 1.91 yamt }
997 1.91 yamt if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
998 1.186 rmind mutex_enter(uobj->vmobjlock);
999 1.91 yamt for (i = 0; i < npages; i++) {
1000 1.91 yamt pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1001 1.91 yamt pmap_page_protect(pgs[i], VM_PROT_READ);
1002 1.91 yamt }
1003 1.186 rmind mutex_exit(uobj->vmobjlock);
1004 1.149 thorpej pageprotected = true; /* pages can't be modified during i/o. */
1005 1.102 yamt } else
1006 1.149 thorpej pageprotected = false;
1007 1.74 chs
1008 1.91 yamt /*
1009 1.91 yamt * Send the data to the server if necessary,
1010 1.91 yamt * otherwise just send a commit rpc.
1011 1.91 yamt */
1012 1.117 christos #ifndef NFS_V2_ONLY
1013 1.91 yamt if (needcommit) {
1014 1.74 chs
1015 1.74 chs /*
1016 1.74 chs * If the buffer is in the range that we already committed,
1017 1.74 chs * there's nothing to do.
1018 1.74 chs *
1019 1.74 chs * If it's in the range that we need to commit, push the
1020 1.74 chs * whole range at once, otherwise only push the buffer.
1021 1.74 chs * In both these cases, acquire the commit lock to avoid
1022 1.74 chs * other processes modifying the range.
1023 1.74 chs */
1024 1.74 chs
1025 1.88 yamt off = uiop->uio_offset;
1026 1.88 yamt cnt = bp->b_bcount;
1027 1.147 yamt mutex_enter(&np->n_commitlock);
1028 1.74 chs if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1029 1.148 thorpej bool pushedrange;
1030 1.74 chs if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1031 1.149 thorpej pushedrange = true;
1032 1.74 chs off = np->n_pushlo;
1033 1.74 chs cnt = np->n_pushhi - np->n_pushlo;
1034 1.74 chs } else {
1035 1.149 thorpej pushedrange = false;
1036 1.74 chs }
1037 1.139 christos error = nfs_commit(vp, off, cnt, curlwp);
1038 1.74 chs if (error == 0) {
1039 1.74 chs if (pushedrange) {
1040 1.74 chs nfs_merge_commit_ranges(vp);
1041 1.74 chs } else {
1042 1.74 chs nfs_add_committed_range(vp, off, cnt);
1043 1.74 chs }
1044 1.74 chs }
1045 1.95 yamt } else {
1046 1.95 yamt error = 0;
1047 1.74 chs }
1048 1.147 yamt mutex_exit(&np->n_commitlock);
1049 1.147 yamt rw_exit(&nmp->nm_writeverflock);
1050 1.74 chs if (!error) {
1051 1.97 yamt /*
1052 1.97 yamt * pages are now on stable storage.
1053 1.97 yamt */
1054 1.93 yamt uiop->uio_resid = 0;
1055 1.186 rmind mutex_enter(uobj->vmobjlock);
1056 1.74 chs for (i = 0; i < npages; i++) {
1057 1.74 chs pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1058 1.74 chs }
1059 1.186 rmind mutex_exit(uobj->vmobjlock);
1060 1.185 jakllsch goto out;
1061 1.74 chs } else if (error == NFSERR_STALEWRITEVERF) {
1062 1.96 yamt nfs_clearcommit(vp->v_mount);
1063 1.96 yamt goto again;
1064 1.96 yamt }
1065 1.96 yamt if (error) {
1066 1.96 yamt bp->b_error = np->n_error = error;
1067 1.96 yamt np->n_flag |= NWRITEERR;
1068 1.74 chs }
1069 1.176 christos goto out;
1070 1.91 yamt }
1071 1.117 christos #endif
1072 1.91 yamt off = uiop->uio_offset;
1073 1.91 yamt cnt = bp->b_bcount;
1074 1.91 yamt uiop->uio_rw = UIO_WRITE;
1075 1.91 yamt nfsstats.write_bios++;
1076 1.102 yamt error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1077 1.117 christos #ifndef NFS_V2_ONLY
1078 1.91 yamt if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1079 1.97 yamt /*
1080 1.97 yamt * we need to commit pages later.
1081 1.97 yamt */
1082 1.147 yamt mutex_enter(&np->n_commitlock);
1083 1.74 chs nfs_add_tobecommitted_range(vp, off, cnt);
1084 1.97 yamt /*
1085 1.97 yamt * if there can be too many uncommitted pages, commit them now.
1086 1.97 yamt */
1087 1.74 chs if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1088 1.74 chs off = np->n_pushlo;
1089 1.74 chs cnt = nfs_commitsize >> 1;
1090 1.139 christos error = nfs_commit(vp, off, cnt, curlwp);
1091 1.74 chs if (!error) {
1092 1.74 chs nfs_add_committed_range(vp, off, cnt);
1093 1.74 chs nfs_del_tobecommitted_range(vp, off, cnt);
1094 1.74 chs }
1095 1.97 yamt if (error == NFSERR_STALEWRITEVERF) {
1096 1.149 thorpej stalewriteverf = true;
1097 1.97 yamt error = 0; /* it isn't a real error */
1098 1.97 yamt }
1099 1.97 yamt } else {
1100 1.97 yamt /*
1101 1.97 yamt * re-dirty pages so that they will be passed
1102 1.97 yamt * to us later again.
1103 1.97 yamt */
1104 1.186 rmind mutex_enter(uobj->vmobjlock);
1105 1.97 yamt for (i = 0; i < npages; i++) {
1106 1.97 yamt pgs[i]->flags &= ~PG_CLEAN;
1107 1.97 yamt }
1108 1.186 rmind mutex_exit(uobj->vmobjlock);
1109 1.74 chs }
1110 1.147 yamt mutex_exit(&np->n_commitlock);
1111 1.117 christos } else
1112 1.117 christos #endif
1113 1.117 christos if (!error) {
1114 1.97 yamt /*
1115 1.97 yamt * pages are now on stable storage.
1116 1.97 yamt */
1117 1.147 yamt mutex_enter(&np->n_commitlock);
1118 1.74 chs nfs_del_committed_range(vp, off, cnt);
1119 1.147 yamt mutex_exit(&np->n_commitlock);
1120 1.186 rmind mutex_enter(uobj->vmobjlock);
1121 1.74 chs for (i = 0; i < npages; i++) {
1122 1.74 chs pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1123 1.74 chs }
1124 1.186 rmind mutex_exit(uobj->vmobjlock);
1125 1.91 yamt } else {
1126 1.97 yamt /*
1127 1.97 yamt * we got an error.
1128 1.97 yamt */
1129 1.97 yamt bp->b_error = np->n_error = error;
1130 1.97 yamt np->n_flag |= NWRITEERR;
1131 1.54 chs }
1132 1.96 yamt
1133 1.147 yamt rw_exit(&nmp->nm_writeverflock);
1134 1.96 yamt
1135 1.178 dogcow
1136 1.96 yamt if (stalewriteverf) {
1137 1.54 chs nfs_clearcommit(vp->v_mount);
1138 1.74 chs }
1139 1.178 dogcow #ifndef NFS_V2_ONLY
1140 1.176 christos out:
1141 1.178 dogcow #endif
1142 1.179 christos if (pgs != spgs)
1143 1.179 christos kmem_free(pgs, sizeof(*pgs) * npages);
1144 1.91 yamt return error;
1145 1.91 yamt }
1146 1.91 yamt
1147 1.91 yamt /*
1148 1.91 yamt * nfs_doio for B_PHYS.
1149 1.91 yamt */
1150 1.91 yamt static int
1151 1.174 yamt nfs_doio_phys(struct buf *bp, struct uio *uiop)
1152 1.91 yamt {
1153 1.91 yamt struct vnode *vp = bp->b_vp;
1154 1.91 yamt int error;
1155 1.91 yamt
1156 1.91 yamt uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1157 1.91 yamt if (bp->b_flags & B_READ) {
1158 1.91 yamt uiop->uio_rw = UIO_READ;
1159 1.91 yamt nfsstats.read_physios++;
1160 1.91 yamt error = nfs_readrpc(vp, uiop);
1161 1.91 yamt } else {
1162 1.91 yamt int iomode = NFSV3WRITE_DATASYNC;
1163 1.148 thorpej bool stalewriteverf;
1164 1.96 yamt struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1165 1.91 yamt
1166 1.91 yamt uiop->uio_rw = UIO_WRITE;
1167 1.91 yamt nfsstats.write_physios++;
1168 1.147 yamt rw_enter(&nmp->nm_writeverflock, RW_READER);
1169 1.149 thorpej error = nfs_writerpc(vp, uiop, &iomode, false, &stalewriteverf);
1170 1.147 yamt rw_exit(&nmp->nm_writeverflock);
1171 1.91 yamt if (stalewriteverf) {
1172 1.91 yamt nfs_clearcommit(bp->b_vp->v_mount);
1173 1.91 yamt }
1174 1.91 yamt }
1175 1.172 ad bp->b_error = error;
1176 1.91 yamt return error;
1177 1.91 yamt }
1178 1.91 yamt
1179 1.91 yamt /*
1180 1.91 yamt * Do an I/O operation to/from a cache block. This may be called
1181 1.91 yamt * synchronously or from an nfsiod.
1182 1.91 yamt */
1183 1.91 yamt int
1184 1.174 yamt nfs_doio(struct buf *bp)
1185 1.91 yamt {
1186 1.91 yamt int error;
1187 1.91 yamt struct uio uio;
1188 1.91 yamt struct uio *uiop = &uio;
1189 1.91 yamt struct iovec io;
1190 1.91 yamt UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
1191 1.91 yamt
1192 1.91 yamt uiop->uio_iov = &io;
1193 1.91 yamt uiop->uio_iovcnt = 1;
1194 1.91 yamt uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
1195 1.142 yamt UIO_SETUP_SYSSPACE(uiop);
1196 1.91 yamt io.iov_base = bp->b_data;
1197 1.91 yamt io.iov_len = uiop->uio_resid = bp->b_bcount;
1198 1.91 yamt
1199 1.91 yamt /*
1200 1.91 yamt * Historically, paging was done with physio, but no more...
1201 1.91 yamt */
1202 1.91 yamt if (bp->b_flags & B_PHYS) {
1203 1.91 yamt /*
1204 1.91 yamt * ...though reading /dev/drum still gets us here.
1205 1.91 yamt */
1206 1.91 yamt error = nfs_doio_phys(bp, uiop);
1207 1.91 yamt } else if (bp->b_flags & B_READ) {
1208 1.91 yamt error = nfs_doio_read(bp, uiop);
1209 1.91 yamt } else {
1210 1.91 yamt error = nfs_doio_write(bp, uiop);
1211 1.91 yamt }
1212 1.91 yamt bp->b_resid = uiop->uio_resid;
1213 1.54 chs biodone(bp);
1214 1.54 chs return (error);
1215 1.54 chs }
1216 1.54 chs
1217 1.54 chs /*
1218 1.54 chs * Vnode op for VM getpages.
1219 1.54 chs */
1220 1.69 chs
1221 1.54 chs int
1222 1.174 yamt nfs_getpages(void *v)
1223 1.54 chs {
1224 1.54 chs struct vop_getpages_args /* {
1225 1.54 chs struct vnode *a_vp;
1226 1.54 chs voff_t a_offset;
1227 1.67 chs struct vm_page **a_m;
1228 1.54 chs int *a_count;
1229 1.54 chs int a_centeridx;
1230 1.54 chs vm_prot_t a_access_type;
1231 1.54 chs int a_advice;
1232 1.54 chs int a_flags;
1233 1.54 chs } */ *ap = v;
1234 1.54 chs
1235 1.54 chs struct vnode *vp = ap->a_vp;
1236 1.69 chs struct uvm_object *uobj = &vp->v_uobj;
1237 1.54 chs struct nfsnode *np = VTONFS(vp);
1238 1.80 enami const int npages = *ap->a_count;
1239 1.180 christos struct vm_page *pg, **pgs, **opgs, *spgs[UBC_MAX_PAGES];
1240 1.74 chs off_t origoffset, len;
1241 1.80 enami int i, error;
1242 1.148 thorpej bool v3 = NFS_ISV3(vp);
1243 1.148 thorpej bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1244 1.148 thorpej bool locked = (ap->a_flags & PGO_LOCKED) != 0;
1245 1.54 chs
1246 1.54 chs /*
1247 1.179 christos * If we are not locked we are not really using opgs,
1248 1.179 christos * so just initialize it
1249 1.179 christos */
1250 1.179 christos if (!locked || npages < __arraycount(spgs))
1251 1.179 christos opgs = spgs;
1252 1.179 christos else {
1253 1.179 christos if ((opgs = kmem_alloc(npages * sizeof(*opgs), KM_NOSLEEP)) ==
1254 1.179 christos NULL)
1255 1.179 christos return ENOMEM;
1256 1.179 christos }
1257 1.179 christos
1258 1.179 christos /*
1259 1.80 enami * call the genfs code to get the pages. `pgs' may be NULL
1260 1.80 enami * when doing read-ahead.
1261 1.54 chs */
1262 1.80 enami pgs = ap->a_m;
1263 1.81 enami if (write && locked && v3) {
1264 1.80 enami KASSERT(pgs != NULL);
1265 1.80 enami #ifdef DEBUG
1266 1.80 enami
1267 1.80 enami /*
1268 1.80 enami * If PGO_LOCKED is set, real pages shouldn't exists
1269 1.80 enami * in the array.
1270 1.80 enami */
1271 1.80 enami
1272 1.80 enami for (i = 0; i < npages; i++)
1273 1.80 enami KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1274 1.80 enami #endif
1275 1.80 enami memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1276 1.80 enami }
1277 1.69 chs error = genfs_getpages(v);
1278 1.177 christos if (error)
1279 1.177 christos goto out;
1280 1.76 chs
1281 1.76 chs /*
1282 1.76 chs * for read faults where the nfs node is not yet marked NMODIFIED,
1283 1.76 chs * set PG_RDONLY on the pages so that we come back here if someone
1284 1.76 chs * tries to modify later via the mapping that will be entered for
1285 1.76 chs * this fault.
1286 1.76 chs */
1287 1.76 chs
1288 1.76 chs if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1289 1.76 chs if (!locked) {
1290 1.186 rmind mutex_enter(uobj->vmobjlock);
1291 1.76 chs }
1292 1.76 chs for (i = 0; i < npages; i++) {
1293 1.76 chs pg = pgs[i];
1294 1.76 chs if (pg == NULL || pg == PGO_DONTCARE) {
1295 1.76 chs continue;
1296 1.76 chs }
1297 1.76 chs pg->flags |= PG_RDONLY;
1298 1.76 chs }
1299 1.76 chs if (!locked) {
1300 1.186 rmind mutex_exit(uobj->vmobjlock);
1301 1.76 chs }
1302 1.76 chs }
1303 1.177 christos if (!write)
1304 1.177 christos goto out;
1305 1.54 chs
1306 1.54 chs /*
1307 1.69 chs * this is a write fault, update the commit info.
1308 1.54 chs */
1309 1.54 chs
1310 1.69 chs origoffset = ap->a_offset;
1311 1.74 chs len = npages << PAGE_SHIFT;
1312 1.54 chs
1313 1.76 chs if (v3) {
1314 1.147 yamt if (!locked) {
1315 1.147 yamt mutex_enter(&np->n_commitlock);
1316 1.147 yamt } else {
1317 1.150 yamt if (!mutex_tryenter(&np->n_commitlock)) {
1318 1.80 enami
1319 1.147 yamt /*
1320 1.147 yamt * Since PGO_LOCKED is set, we need to unbusy
1321 1.147 yamt * all pages fetched by genfs_getpages() above,
1322 1.147 yamt * tell the caller that there are no pages
1323 1.147 yamt * available and put back original pgs array.
1324 1.147 yamt */
1325 1.80 enami
1326 1.172 ad mutex_enter(&uvm_pageqlock);
1327 1.147 yamt uvm_page_unbusy(pgs, npages);
1328 1.172 ad mutex_exit(&uvm_pageqlock);
1329 1.147 yamt *ap->a_count = 0;
1330 1.147 yamt memcpy(pgs, opgs,
1331 1.147 yamt npages * sizeof(struct vm_pages *));
1332 1.177 christos error = EBUSY;
1333 1.177 christos goto out;
1334 1.147 yamt }
1335 1.80 enami }
1336 1.76 chs nfs_del_committed_range(vp, origoffset, len);
1337 1.76 chs nfs_del_tobecommitted_range(vp, origoffset, len);
1338 1.76 chs }
1339 1.80 enami np->n_flag |= NMODIFIED;
1340 1.73 chs if (!locked) {
1341 1.186 rmind mutex_enter(uobj->vmobjlock);
1342 1.73 chs }
1343 1.54 chs for (i = 0; i < npages; i++) {
1344 1.69 chs pg = pgs[i];
1345 1.69 chs if (pg == NULL || pg == PGO_DONTCARE) {
1346 1.54 chs continue;
1347 1.54 chs }
1348 1.74 chs pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1349 1.54 chs }
1350 1.73 chs if (!locked) {
1351 1.186 rmind mutex_exit(uobj->vmobjlock);
1352 1.73 chs }
1353 1.76 chs if (v3) {
1354 1.147 yamt mutex_exit(&np->n_commitlock);
1355 1.76 chs }
1356 1.177 christos out:
1357 1.179 christos if (opgs != spgs)
1358 1.179 christos kmem_free(opgs, sizeof(*opgs) * npages);
1359 1.177 christos return error;
1360 1.1 cgd }
1361