1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. 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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/10.0/sys/nfsclient/nfs_vnops.c 252479 2013-07-01 21:16:19Z rmacklem $");
37
38 /*
39 * vnode op calls for Sun NFS version 2 and 3
40 */
41
42 #include "opt_inet.h"
43 #include "opt_kdtrace.h"
44
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/resourcevar.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/jail.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/namei.h>
57 #include <sys/socket.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/signalvar.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_object.h>
69
70 #include <nfs/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfs/nfs_kdtrace.h>
75 #include <nfs/nfs_lock.h>
76 #include <nfs/xdr_subs.h>
77 #include <nfsclient/nfsm_subs.h>
78
79 #include <net/if.h>
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82
83 #include <machine/stdarg.h>
84
85 #ifdef KDTRACE_HOOKS
86 #include <sys/dtrace_bsd.h>
87
88 dtrace_nfsclient_accesscache_flush_probe_func_t
89 dtrace_nfsclient_accesscache_flush_done_probe;
90 uint32_t nfsclient_accesscache_flush_done_id;
91
92 dtrace_nfsclient_accesscache_get_probe_func_t
93 dtrace_nfsclient_accesscache_get_hit_probe,
94 dtrace_nfsclient_accesscache_get_miss_probe;
95 uint32_t nfsclient_accesscache_get_hit_id;
96 uint32_t nfsclient_accesscache_get_miss_id;
97
98 dtrace_nfsclient_accesscache_load_probe_func_t
99 dtrace_nfsclient_accesscache_load_done_probe;
100 uint32_t nfsclient_accesscache_load_done_id;
101 #endif /* !KDTRACE_HOOKS */
102
103 /* Defs */
104 #define TRUE 1
105 #define FALSE 0
106
107 /*
108 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
109 * calls are not in getblk() and brelse() so that they would not be necessary
110 * here.
111 */
112 #ifndef B_VMIO
113 #define vfs_busy_pages(bp, f)
114 #endif
115
116 static vop_read_t nfsfifo_read;
117 static vop_write_t nfsfifo_write;
118 static vop_close_t nfsfifo_close;
119 static int nfs_flush(struct vnode *, int, int);
120 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *);
121 static vop_lookup_t nfs_lookup;
122 static vop_create_t nfs_create;
123 static vop_mknod_t nfs_mknod;
124 static vop_open_t nfs_open;
125 static vop_close_t nfs_close;
126 static vop_access_t nfs_access;
127 static vop_getattr_t nfs_getattr;
128 static vop_setattr_t nfs_setattr;
129 static vop_read_t nfs_read;
130 static vop_fsync_t nfs_fsync;
131 static vop_remove_t nfs_remove;
132 static vop_link_t nfs_link;
133 static vop_rename_t nfs_rename;
134 static vop_mkdir_t nfs_mkdir;
135 static vop_rmdir_t nfs_rmdir;
136 static vop_symlink_t nfs_symlink;
137 static vop_readdir_t nfs_readdir;
138 static vop_strategy_t nfs_strategy;
139 static int nfs_lookitup(struct vnode *, const char *, int,
140 struct ucred *, struct thread *, struct nfsnode **);
141 static int nfs_sillyrename(struct vnode *, struct vnode *,
142 struct componentname *);
143 static vop_access_t nfsspec_access;
144 static vop_readlink_t nfs_readlink;
145 static vop_print_t nfs_print;
146 static vop_advlock_t nfs_advlock;
147 static vop_advlockasync_t nfs_advlockasync;
148
149 /*
150 * Global vfs data structures for nfs
151 */
152 struct vop_vector nfs_vnodeops = {
153 .vop_default = &default_vnodeops,
154 .vop_access = nfs_access,
155 .vop_advlock = nfs_advlock,
156 .vop_advlockasync = nfs_advlockasync,
157 .vop_close = nfs_close,
158 .vop_create = nfs_create,
159 .vop_fsync = nfs_fsync,
160 .vop_getattr = nfs_getattr,
161 .vop_getpages = nfs_getpages,
162 .vop_putpages = nfs_putpages,
163 .vop_inactive = nfs_inactive,
164 .vop_link = nfs_link,
165 .vop_lookup = nfs_lookup,
166 .vop_mkdir = nfs_mkdir,
167 .vop_mknod = nfs_mknod,
168 .vop_open = nfs_open,
169 .vop_print = nfs_print,
170 .vop_read = nfs_read,
171 .vop_readdir = nfs_readdir,
172 .vop_readlink = nfs_readlink,
173 .vop_reclaim = nfs_reclaim,
174 .vop_remove = nfs_remove,
175 .vop_rename = nfs_rename,
176 .vop_rmdir = nfs_rmdir,
177 .vop_setattr = nfs_setattr,
178 .vop_strategy = nfs_strategy,
179 .vop_symlink = nfs_symlink,
180 .vop_write = nfs_write,
181 };
182
183 struct vop_vector nfs_fifoops = {
184 .vop_default = &fifo_specops,
185 .vop_access = nfsspec_access,
186 .vop_close = nfsfifo_close,
187 .vop_fsync = nfs_fsync,
188 .vop_getattr = nfs_getattr,
189 .vop_inactive = nfs_inactive,
190 .vop_print = nfs_print,
191 .vop_read = nfsfifo_read,
192 .vop_reclaim = nfs_reclaim,
193 .vop_setattr = nfs_setattr,
194 .vop_write = nfsfifo_write,
195 };
196
197 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
198 struct componentname *cnp, struct vattr *vap);
199 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
200 struct ucred *cred, struct thread *td);
201 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
202 int fnamelen, struct vnode *tdvp,
203 const char *tnameptr, int tnamelen,
204 struct ucred *cred, struct thread *td);
205 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
206 struct sillyrename *sp);
207
208 /*
209 * Global variables
210 */
211 struct mtx nfs_iod_mtx;
212 enum nfsiod_state nfs_iodwant[NFS_MAXASYNCDAEMON];
213 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
214 int nfs_numasync = 0;
215 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
216
217 SYSCTL_DECL(_vfs_oldnfs);
218
219 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
220 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
221 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
222
223 static int nfs_prime_access_cache = 0;
224 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
225 &nfs_prime_access_cache, 0,
226 "Prime NFS ACCESS cache when fetching attributes");
227
228 static int nfsv3_commit_on_close = 0;
229 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
230 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
231
232 static int nfs_clean_pages_on_close = 1;
233 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
234 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
235
236 int nfs_directio_enable = 0;
237 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
238 &nfs_directio_enable, 0, "Enable NFS directio");
239
240 /*
241 * This sysctl allows other processes to mmap a file that has been opened
242 * O_DIRECT by a process. In general, having processes mmap the file while
243 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
244 * this by default to prevent DoS attacks - to prevent a malicious user from
245 * opening up files O_DIRECT preventing other users from mmap'ing these
246 * files. "Protected" environments where stricter consistency guarantees are
247 * required can disable this knob. The process that opened the file O_DIRECT
248 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
249 * meaningful.
250 */
251 int nfs_directio_allow_mmap = 1;
252 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
253 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
254
255 #if 0
256 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
257 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
258
259 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
260 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
261 #endif
262
263 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
264 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
265 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
266
267 /*
268 * SMP Locking Note :
269 * The list of locks after the description of the lock is the ordering
270 * of other locks acquired with the lock held.
271 * np->n_mtx : Protects the fields in the nfsnode.
272 VM Object Lock
273 VI_MTX (acquired indirectly)
274 * nmp->nm_mtx : Protects the fields in the nfsmount.
275 rep->r_mtx
276 * nfs_iod_mtx : Global lock, protects shared nfsiod state.
277 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
278 nmp->nm_mtx
279 rep->r_mtx
280 * rep->r_mtx : Protects the fields in an nfsreq.
281 */
282
283 static int
284 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
285 struct ucred *cred, uint32_t *retmode)
286 {
287 const int v3 = 1;
288 u_int32_t *tl;
289 int error = 0, attrflag, i, lrupos;
290
291 struct mbuf *mreq, *mrep, *md, *mb;
292 caddr_t bpos, dpos;
293 u_int32_t rmode;
294 struct nfsnode *np = VTONFS(vp);
295
296 nfsstats.rpccnt[NFSPROC_ACCESS]++;
297 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED, M_WAITOK, MT_DATA, 0);
298 mb = mreq;
299 bpos = mtod(mb, caddr_t);
300 nfsm_fhtom(vp, v3);
301 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
302 *tl = txdr_unsigned(wmode);
303 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
304 nfsm_postop_attr(vp, attrflag);
305 if (!error) {
306 lrupos = 0;
307 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
308 rmode = fxdr_unsigned(u_int32_t, *tl);
309 mtx_lock(&np->n_mtx);
310 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
311 if (np->n_accesscache[i].uid == cred->cr_uid) {
312 np->n_accesscache[i].mode = rmode;
313 np->n_accesscache[i].stamp = time_second;
314 break;
315 }
316 if (i > 0 && np->n_accesscache[i].stamp <
317 np->n_accesscache[lrupos].stamp)
318 lrupos = i;
319 }
320 if (i == NFS_ACCESSCACHESIZE) {
321 np->n_accesscache[lrupos].uid = cred->cr_uid;
322 np->n_accesscache[lrupos].mode = rmode;
323 np->n_accesscache[lrupos].stamp = time_second;
324 }
325 mtx_unlock(&np->n_mtx);
326 if (retmode != NULL)
327 *retmode = rmode;
328 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
329 }
330 m_freem(mrep);
331 nfsmout:
332 #ifdef KDTRACE_HOOKS
333 if (error) {
334 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
335 error);
336 }
337 #endif
338 return (error);
339 }
340
341 /*
342 * nfs access vnode op.
343 * For nfs version 2, just return ok. File accesses may fail later.
344 * For nfs version 3, use the access rpc to check accessibility. If file modes
345 * are changed on the server, accesses might still fail later.
346 */
347 static int
348 nfs_access(struct vop_access_args *ap)
349 {
350 struct vnode *vp = ap->a_vp;
351 int error = 0, i, gotahit;
352 u_int32_t mode, rmode, wmode;
353 int v3 = NFS_ISV3(vp);
354 struct nfsnode *np = VTONFS(vp);
355
356 /*
357 * Disallow write attempts on filesystems mounted read-only;
358 * unless the file is a socket, fifo, or a block or character
359 * device resident on the filesystem.
360 */
361 if ((ap->a_accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
362 switch (vp->v_type) {
363 case VREG:
364 case VDIR:
365 case VLNK:
366 return (EROFS);
367 default:
368 break;
369 }
370 }
371 /*
372 * For nfs v3, check to see if we have done this recently, and if
373 * so return our cached result instead of making an ACCESS call.
374 * If not, do an access rpc, otherwise you are stuck emulating
375 * ufs_access() locally using the vattr. This may not be correct,
376 * since the server may apply other access criteria such as
377 * client uid-->server uid mapping that we do not know about.
378 */
379 if (v3) {
380 if (ap->a_accmode & VREAD)
381 mode = NFSV3ACCESS_READ;
382 else
383 mode = 0;
384 if (vp->v_type != VDIR) {
385 if (ap->a_accmode & VWRITE)
386 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
387 if (ap->a_accmode & VEXEC)
388 mode |= NFSV3ACCESS_EXECUTE;
389 } else {
390 if (ap->a_accmode & VWRITE)
391 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
392 NFSV3ACCESS_DELETE);
393 if (ap->a_accmode & VEXEC)
394 mode |= NFSV3ACCESS_LOOKUP;
395 }
396 /* XXX safety belt, only make blanket request if caching */
397 if (nfsaccess_cache_timeout > 0) {
398 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
399 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
400 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
401 } else {
402 wmode = mode;
403 }
404
405 /*
406 * Does our cached result allow us to give a definite yes to
407 * this request?
408 */
409 gotahit = 0;
410 mtx_lock(&np->n_mtx);
411 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
412 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
413 if (time_second < (np->n_accesscache[i].stamp +
414 nfsaccess_cache_timeout) &&
415 (np->n_accesscache[i].mode & mode) == mode) {
416 nfsstats.accesscache_hits++;
417 gotahit = 1;
418 }
419 break;
420 }
421 }
422 mtx_unlock(&np->n_mtx);
423 #ifdef KDTRACE_HOOKS
424 if (gotahit)
425 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
426 ap->a_cred->cr_uid, mode);
427 else
428 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
429 ap->a_cred->cr_uid, mode);
430 #endif
431 if (gotahit == 0) {
432 /*
433 * Either a no, or a don't know. Go to the wire.
434 */
435 nfsstats.accesscache_misses++;
436 error = nfs3_access_otw(vp, wmode, ap->a_td, ap->a_cred,
437 &rmode);
438 if (!error) {
439 if ((rmode & mode) != mode)
440 error = EACCES;
441 }
442 }
443 return (error);
444 } else {
445 if ((error = nfsspec_access(ap)) != 0) {
446 return (error);
447 }
448 /*
449 * Attempt to prevent a mapped root from accessing a file
450 * which it shouldn't. We try to read a byte from the file
451 * if the user is root and the file is not zero length.
452 * After calling nfsspec_access, we should have the correct
453 * file size cached.
454 */
455 mtx_lock(&np->n_mtx);
456 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
457 && VTONFS(vp)->n_size > 0) {
458 struct iovec aiov;
459 struct uio auio;
460 char buf[1];
461
462 mtx_unlock(&np->n_mtx);
463 aiov.iov_base = buf;
464 aiov.iov_len = 1;
465 auio.uio_iov = &aiov;
466 auio.uio_iovcnt = 1;
467 auio.uio_offset = 0;
468 auio.uio_resid = 1;
469 auio.uio_segflg = UIO_SYSSPACE;
470 auio.uio_rw = UIO_READ;
471 auio.uio_td = ap->a_td;
472
473 if (vp->v_type == VREG)
474 error = nfs_readrpc(vp, &auio, ap->a_cred);
475 else if (vp->v_type == VDIR) {
476 char* bp;
477 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
478 aiov.iov_base = bp;
479 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
480 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
481 free(bp, M_TEMP);
482 } else if (vp->v_type == VLNK)
483 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
484 else
485 error = EACCES;
486 } else
487 mtx_unlock(&np->n_mtx);
488 return (error);
489 }
490 }
491
492 int nfs_otw_getattr_avoid = 0;
493
494 /*
495 * nfs open vnode op
496 * Check to see if the type is ok
497 * and that deletion is not in progress.
498 * For paged in text files, you will need to flush the page cache
499 * if consistency is lost.
500 */
501 /* ARGSUSED */
502 static int
503 nfs_open(struct vop_open_args *ap)
504 {
505 struct vnode *vp = ap->a_vp;
506 struct nfsnode *np = VTONFS(vp);
507 struct vattr vattr;
508 int error;
509 int fmode = ap->a_mode;
510 struct ucred *cred;
511
512 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
513 return (EOPNOTSUPP);
514
515 /*
516 * Get a valid lease. If cached data is stale, flush it.
517 */
518 mtx_lock(&np->n_mtx);
519 if (np->n_flag & NMODIFIED) {
520 mtx_unlock(&np->n_mtx);
521 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
522 if (error == EINTR || error == EIO)
523 return (error);
524 mtx_lock(&np->n_mtx);
525 np->n_attrstamp = 0;
526 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
527 if (vp->v_type == VDIR)
528 np->n_direofoffset = 0;
529 mtx_unlock(&np->n_mtx);
530 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
531 if (error)
532 return (error);
533 mtx_lock(&np->n_mtx);
534 np->n_mtime = vattr.va_mtime;
535 } else {
536 mtx_unlock(&np->n_mtx);
537 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
538 if (error)
539 return (error);
540 mtx_lock(&np->n_mtx);
541 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
542 if (vp->v_type == VDIR)
543 np->n_direofoffset = 0;
544 mtx_unlock(&np->n_mtx);
545 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
546 if (error == EINTR || error == EIO) {
547 return (error);
548 }
549 mtx_lock(&np->n_mtx);
550 np->n_mtime = vattr.va_mtime;
551 }
552 }
553 /*
554 * If the object has >= 1 O_DIRECT active opens, we disable caching.
555 */
556 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
557 if (np->n_directio_opens == 0) {
558 mtx_unlock(&np->n_mtx);
559 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
560 if (error)
561 return (error);
562 mtx_lock(&np->n_mtx);
563 np->n_flag |= NNONCACHE;
564 }
565 np->n_directio_opens++;
566 }
567
568 /*
569 * If this is an open for writing, capture a reference to the
570 * credentials, so they can be used by nfs_putpages(). Using
571 * these write credentials is preferable to the credentials of
572 * whatever thread happens to be doing the VOP_PUTPAGES() since
573 * the write RPCs are less likely to fail with EACCES.
574 */
575 if ((fmode & FWRITE) != 0) {
576 cred = np->n_writecred;
577 np->n_writecred = crhold(ap->a_cred);
578 } else
579 cred = NULL;
580 mtx_unlock(&np->n_mtx);
581 if (cred != NULL)
582 crfree(cred);
583 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
584 return (0);
585 }
586
587 /*
588 * nfs close vnode op
589 * What an NFS client should do upon close after writing is a debatable issue.
590 * Most NFS clients push delayed writes to the server upon close, basically for
591 * two reasons:
592 * 1 - So that any write errors may be reported back to the client process
593 * doing the close system call. By far the two most likely errors are
594 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
595 * 2 - To put a worst case upper bound on cache inconsistency between
596 * multiple clients for the file.
597 * There is also a consistency problem for Version 2 of the protocol w.r.t.
598 * not being able to tell if other clients are writing a file concurrently,
599 * since there is no way of knowing if the changed modify time in the reply
600 * is only due to the write for this client.
601 * (NFS Version 3 provides weak cache consistency data in the reply that
602 * should be sufficient to detect and handle this case.)
603 *
604 * The current code does the following:
605 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
606 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
607 * or commit them (this satisfies 1 and 2 except for the
608 * case where the server crashes after this close but
609 * before the commit RPC, which is felt to be "good
610 * enough". Changing the last argument to nfs_flush() to
611 * a 1 would force a commit operation, if it is felt a
612 * commit is necessary now.
613 */
614 /* ARGSUSED */
615 static int
616 nfs_close(struct vop_close_args *ap)
617 {
618 struct vnode *vp = ap->a_vp;
619 struct nfsnode *np = VTONFS(vp);
620 int error = 0;
621 int fmode = ap->a_fflag;
622
623 if (vp->v_type == VREG) {
624 /*
625 * Examine and clean dirty pages, regardless of NMODIFIED.
626 * This closes a major hole in close-to-open consistency.
627 * We want to push out all dirty pages (and buffers) on
628 * close, regardless of whether they were dirtied by
629 * mmap'ed writes or via write().
630 */
631 if (nfs_clean_pages_on_close && vp->v_object) {
632 VM_OBJECT_WLOCK(vp->v_object);
633 vm_object_page_clean(vp->v_object, 0, 0, 0);
634 VM_OBJECT_WUNLOCK(vp->v_object);
635 }
636 mtx_lock(&np->n_mtx);
637 if (np->n_flag & NMODIFIED) {
638 mtx_unlock(&np->n_mtx);
639 if (NFS_ISV3(vp)) {
640 /*
641 * Under NFSv3 we have dirty buffers to dispose of. We
642 * must flush them to the NFS server. We have the option
643 * of waiting all the way through the commit rpc or just
644 * waiting for the initial write. The default is to only
645 * wait through the initial write so the data is in the
646 * server's cache, which is roughly similar to the state
647 * a standard disk subsystem leaves the file in on close().
648 *
649 * We cannot clear the NMODIFIED bit in np->n_flag due to
650 * potential races with other processes, and certainly
651 * cannot clear it if we don't commit.
652 */
653 int cm = nfsv3_commit_on_close ? 1 : 0;
654 error = nfs_flush(vp, MNT_WAIT, cm);
655 /* np->n_flag &= ~NMODIFIED; */
656 } else
657 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
658 mtx_lock(&np->n_mtx);
659 }
660 if (np->n_flag & NWRITEERR) {
661 np->n_flag &= ~NWRITEERR;
662 error = np->n_error;
663 }
664 mtx_unlock(&np->n_mtx);
665 }
666 if (nfs_directio_enable)
667 KASSERT((np->n_directio_asyncwr == 0),
668 ("nfs_close: dirty unflushed (%d) directio buffers\n",
669 np->n_directio_asyncwr));
670 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
671 mtx_lock(&np->n_mtx);
672 KASSERT((np->n_directio_opens > 0),
673 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
674 np->n_directio_opens--;
675 if (np->n_directio_opens == 0)
676 np->n_flag &= ~NNONCACHE;
677 mtx_unlock(&np->n_mtx);
678 }
679 return (error);
680 }
681
682 /*
683 * nfs getattr call from vfs.
684 */
685 static int
686 nfs_getattr(struct vop_getattr_args *ap)
687 {
688 struct vnode *vp = ap->a_vp;
689 struct nfsnode *np = VTONFS(vp);
690 struct thread *td = curthread;
691 struct vattr *vap = ap->a_vap;
692 struct vattr vattr;
693 caddr_t bpos, dpos;
694 int error = 0;
695 struct mbuf *mreq, *mrep, *md, *mb;
696 int v3 = NFS_ISV3(vp);
697
698 /*
699 * Update local times for special files.
700 */
701 mtx_lock(&np->n_mtx);
702 if (np->n_flag & (NACC | NUPD))
703 np->n_flag |= NCHG;
704 mtx_unlock(&np->n_mtx);
705 /*
706 * First look in the cache.
707 */
708 if (nfs_getattrcache(vp, &vattr) == 0)
709 goto nfsmout;
710 if (v3 && nfs_prime_access_cache && nfsaccess_cache_timeout > 0) {
711 nfsstats.accesscache_misses++;
712 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, ap->a_cred, NULL);
713 if (nfs_getattrcache(vp, &vattr) == 0)
714 goto nfsmout;
715 }
716 nfsstats.rpccnt[NFSPROC_GETATTR]++;
717 mreq = m_get2(NFSX_FH(v3), M_WAITOK, MT_DATA, 0);
718 mb = mreq;
719 bpos = mtod(mb, caddr_t);
720 nfsm_fhtom(vp, v3);
721 nfsm_request(vp, NFSPROC_GETATTR, td, ap->a_cred);
722 if (!error) {
723 nfsm_loadattr(vp, &vattr);
724 }
725 m_freem(mrep);
726 nfsmout:
727 vap->va_type = vattr.va_type;
728 vap->va_mode = vattr.va_mode;
729 vap->va_nlink = vattr.va_nlink;
730 vap->va_uid = vattr.va_uid;
731 vap->va_gid = vattr.va_gid;
732 vap->va_fsid = vattr.va_fsid;
733 vap->va_fileid = vattr.va_fileid;
734 vap->va_size = vattr.va_size;
735 vap->va_blocksize = vattr.va_blocksize;
736 vap->va_atime = vattr.va_atime;
737 vap->va_mtime = vattr.va_mtime;
738 vap->va_ctime = vattr.va_ctime;
739 vap->va_gen = vattr.va_gen;
740 vap->va_flags = vattr.va_flags;
741 vap->va_rdev = vattr.va_rdev;
742 vap->va_bytes = vattr.va_bytes;
743 vap->va_filerev = vattr.va_filerev;
744
745 return (error);
746 }
747
748 /*
749 * nfs setattr call.
750 */
751 static int
752 nfs_setattr(struct vop_setattr_args *ap)
753 {
754 struct vnode *vp = ap->a_vp;
755 struct nfsnode *np = VTONFS(vp);
756 struct vattr *vap = ap->a_vap;
757 struct thread *td = curthread;
758 int error = 0;
759 u_quad_t tsize;
760
761 #ifndef nolint
762 tsize = (u_quad_t)0;
763 #endif
764
765 /*
766 * Setting of flags is not supported.
767 */
768 if (vap->va_flags != VNOVAL)
769 return (EOPNOTSUPP);
770
771 /*
772 * Disallow write attempts if the filesystem is mounted read-only.
773 */
774 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
775 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
776 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
777 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
778 error = EROFS;
779 goto out;
780 }
781 if (vap->va_size != VNOVAL) {
782 switch (vp->v_type) {
783 case VDIR:
784 return (EISDIR);
785 case VCHR:
786 case VBLK:
787 case VSOCK:
788 case VFIFO:
789 if (vap->va_mtime.tv_sec == VNOVAL &&
790 vap->va_atime.tv_sec == VNOVAL &&
791 vap->va_mode == (mode_t)VNOVAL &&
792 vap->va_uid == (uid_t)VNOVAL &&
793 vap->va_gid == (gid_t)VNOVAL)
794 return (0);
795 vap->va_size = VNOVAL;
796 break;
797 default:
798 /*
799 * Disallow write attempts if the filesystem is
800 * mounted read-only.
801 */
802 if (vp->v_mount->mnt_flag & MNT_RDONLY)
803 return (EROFS);
804 /*
805 * We run vnode_pager_setsize() early (why?),
806 * we must set np->n_size now to avoid vinvalbuf
807 * V_SAVE races that might setsize a lower
808 * value.
809 */
810 mtx_lock(&np->n_mtx);
811 tsize = np->n_size;
812 mtx_unlock(&np->n_mtx);
813 error = nfs_meta_setsize(vp, ap->a_cred, td,
814 vap->va_size);
815 mtx_lock(&np->n_mtx);
816 if (np->n_flag & NMODIFIED) {
817 tsize = np->n_size;
818 mtx_unlock(&np->n_mtx);
819 if (vap->va_size == 0)
820 error = nfs_vinvalbuf(vp, 0, td, 1);
821 else
822 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
823 if (error) {
824 vnode_pager_setsize(vp, tsize);
825 goto out;
826 }
827 } else
828 mtx_unlock(&np->n_mtx);
829 /*
830 * np->n_size has already been set to vap->va_size
831 * in nfs_meta_setsize(). We must set it again since
832 * nfs_loadattrcache() could be called through
833 * nfs_meta_setsize() and could modify np->n_size.
834 */
835 mtx_lock(&np->n_mtx);
836 np->n_vattr.va_size = np->n_size = vap->va_size;
837 mtx_unlock(&np->n_mtx);
838 };
839 } else {
840 mtx_lock(&np->n_mtx);
841 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
842 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
843 mtx_unlock(&np->n_mtx);
844 if ((error = nfs_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
845 (error == EINTR || error == EIO))
846 return error;
847 } else
848 mtx_unlock(&np->n_mtx);
849 }
850 error = nfs_setattrrpc(vp, vap, ap->a_cred);
851 if (error && vap->va_size != VNOVAL) {
852 mtx_lock(&np->n_mtx);
853 np->n_size = np->n_vattr.va_size = tsize;
854 vnode_pager_setsize(vp, tsize);
855 mtx_unlock(&np->n_mtx);
856 }
857 out:
858 return (error);
859 }
860
861 /*
862 * Do an nfs setattr rpc.
863 */
864 static int
865 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred)
866 {
867 struct nfsv2_sattr *sp;
868 struct nfsnode *np = VTONFS(vp);
869 caddr_t bpos, dpos;
870 u_int32_t *tl;
871 int error = 0, i, wccflag = NFSV3_WCCRATTR;
872 struct mbuf *mreq, *mrep, *md, *mb;
873 int v3 = NFS_ISV3(vp);
874
875 nfsstats.rpccnt[NFSPROC_SETATTR]++;
876 mreq = m_get2(NFSX_FH(v3) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
877 mb = mreq;
878 bpos = mtod(mb, caddr_t);
879 nfsm_fhtom(vp, v3);
880 if (v3) {
881 nfsm_v3attrbuild(vap, TRUE);
882 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
883 *tl = nfs_false;
884 } else {
885 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
886 if (vap->va_mode == (mode_t)VNOVAL)
887 sp->sa_mode = nfs_xdrneg1;
888 else
889 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
890 if (vap->va_uid == (uid_t)VNOVAL)
891 sp->sa_uid = nfs_xdrneg1;
892 else
893 sp->sa_uid = txdr_unsigned(vap->va_uid);
894 if (vap->va_gid == (gid_t)VNOVAL)
895 sp->sa_gid = nfs_xdrneg1;
896 else
897 sp->sa_gid = txdr_unsigned(vap->va_gid);
898 sp->sa_size = txdr_unsigned(vap->va_size);
899 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
900 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
901 }
902 nfsm_request(vp, NFSPROC_SETATTR, curthread, cred);
903 if (v3) {
904 mtx_lock(&np->n_mtx);
905 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
906 np->n_accesscache[i].stamp = 0;
907 mtx_unlock(&np->n_mtx);
908 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
909 nfsm_wcc_data(vp, wccflag);
910 } else
911 nfsm_loadattr(vp, NULL);
912 m_freem(mrep);
913 nfsmout:
914 return (error);
915 }
916
917 /*
918 * nfs lookup call, one step at a time...
919 * First look in cache
920 * If not found, unlock the directory nfsnode and do the rpc
921 */
922 static int
923 nfs_lookup(struct vop_lookup_args *ap)
924 {
925 struct componentname *cnp = ap->a_cnp;
926 struct vnode *dvp = ap->a_dvp;
927 struct vnode **vpp = ap->a_vpp;
928 struct mount *mp = dvp->v_mount;
929 struct vattr dvattr, vattr;
930 struct timespec nctime;
931 int flags = cnp->cn_flags;
932 struct vnode *newvp;
933 struct nfsmount *nmp;
934 caddr_t bpos, dpos;
935 struct mbuf *mreq, *mrep, *md, *mb;
936 long len;
937 nfsfh_t *fhp;
938 struct nfsnode *np, *newnp;
939 int error = 0, attrflag, dattrflag, fhsize, ltype, ncticks;
940 int v3 = NFS_ISV3(dvp);
941 struct thread *td = cnp->cn_thread;
942
943 *vpp = NULLVP;
944 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
945 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
946 return (EROFS);
947 if (dvp->v_type != VDIR)
948 return (ENOTDIR);
949 nmp = VFSTONFS(mp);
950 np = VTONFS(dvp);
951 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
952 *vpp = NULLVP;
953 return (error);
954 }
955 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
956 if (error > 0 && error != ENOENT)
957 return (error);
958 if (error == -1) {
959 /*
960 * Lookups of "." are special and always return the
961 * current directory. cache_lookup() already handles
962 * associated locking bookkeeping, etc.
963 */
964 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
965 /* XXX: Is this really correct? */
966 if (cnp->cn_nameiop != LOOKUP &&
967 (flags & ISLASTCN))
968 cnp->cn_flags |= SAVENAME;
969 return (0);
970 }
971
972 /*
973 * We only accept a positive hit in the cache if the
974 * change time of the file matches our cached copy.
975 * Otherwise, we discard the cache entry and fallback
976 * to doing a lookup RPC. We also only trust cache
977 * entries for less than nm_nametimeo seconds.
978 *
979 * To better handle stale file handles and attributes,
980 * clear the attribute cache of this node if it is a
981 * leaf component, part of an open() call, and not
982 * locally modified before fetching the attributes.
983 * This should allow stale file handles to be detected
984 * here where we can fall back to a LOOKUP RPC to
985 * recover rather than having nfs_open() detect the
986 * stale file handle and failing open(2) with ESTALE.
987 */
988 newvp = *vpp;
989 newnp = VTONFS(newvp);
990 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
991 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
992 !(newnp->n_flag & NMODIFIED)) {
993 mtx_lock(&newnp->n_mtx);
994 newnp->n_attrstamp = 0;
995 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
996 mtx_unlock(&newnp->n_mtx);
997 }
998 if ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
999 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1000 timespeccmp(&vattr.va_ctime, &nctime, ==)) {
1001 nfsstats.lookupcache_hits++;
1002 if (cnp->cn_nameiop != LOOKUP &&
1003 (flags & ISLASTCN))
1004 cnp->cn_flags |= SAVENAME;
1005 return (0);
1006 }
1007 cache_purge(newvp);
1008 if (dvp != newvp)
1009 vput(newvp);
1010 else
1011 vrele(newvp);
1012 *vpp = NULLVP;
1013 } else if (error == ENOENT) {
1014 if (dvp->v_iflag & VI_DOOMED)
1015 return (ENOENT);
1016 /*
1017 * We only accept a negative hit in the cache if the
1018 * modification time of the parent directory matches
1019 * the cached copy in the name cache entry.
1020 * Otherwise, we discard all of the negative cache
1021 * entries for this directory. We also only trust
1022 * negative cache entries for up to nm_negnametimeo
1023 * seconds.
1024 */
1025 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1026 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1027 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1028 nfsstats.lookupcache_hits++;
1029 return (ENOENT);
1030 }
1031 cache_purge_negative(dvp);
1032 }
1033
1034 attrflag = dattrflag = 0;
1035 error = 0;
1036 newvp = NULLVP;
1037 nfsstats.lookupcache_misses++;
1038 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1039 len = cnp->cn_namelen;
1040 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len), M_WAITOK,
1041 MT_DATA, 0);
1042 mb = mreq;
1043 bpos = mtod(mb, caddr_t);
1044 nfsm_fhtom(dvp, v3);
1045 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1046 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
1047 if (error) {
1048 if (v3) {
1049 nfsm_postop_attr_va(dvp, dattrflag, &vattr);
1050 m_freem(mrep);
1051 }
1052 goto nfsmout;
1053 }
1054 nfsm_getfh(fhp, fhsize, v3);
1055
1056 /*
1057 * Handle RENAME case...
1058 */
1059 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1060 if (NFS_CMPFH(np, fhp, fhsize)) {
1061 m_freem(mrep);
1062 return (EISDIR);
1063 }
1064 error = nfs_nget(mp, fhp, fhsize, &np, LK_EXCLUSIVE);
1065 if (error) {
1066 m_freem(mrep);
1067 return (error);
1068 }
1069 newvp = NFSTOV(np);
1070 if (v3) {
1071 nfsm_postop_attr(newvp, attrflag);
1072 nfsm_postop_attr(dvp, attrflag);
1073 } else
1074 nfsm_loadattr(newvp, NULL);
1075 *vpp = newvp;
1076 m_freem(mrep);
1077 cnp->cn_flags |= SAVENAME;
1078 return (0);
1079 }
1080
1081 if (flags & ISDOTDOT) {
1082 ltype = VOP_ISLOCKED(dvp);
1083 error = vfs_busy(mp, MBF_NOWAIT);
1084 if (error != 0) {
1085 vfs_ref(mp);
1086 VOP_UNLOCK(dvp, 0);
1087 error = vfs_busy(mp, 0);
1088 vn_lock(dvp, ltype | LK_RETRY);
1089 vfs_rel(mp);
1090 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1091 vfs_unbusy(mp);
1092 error = ENOENT;
1093 }
1094 if (error != 0) {
1095 m_freem(mrep);
1096 return (error);
1097 }
1098 }
1099 VOP_UNLOCK(dvp, 0);
1100 error = nfs_nget(mp, fhp, fhsize, &np, cnp->cn_lkflags);
1101 if (error == 0)
1102 newvp = NFSTOV(np);
1103 vfs_unbusy(mp);
1104 if (newvp != dvp)
1105 vn_lock(dvp, ltype | LK_RETRY);
1106 if (dvp->v_iflag & VI_DOOMED) {
1107 if (error == 0) {
1108 if (newvp == dvp)
1109 vrele(newvp);
1110 else
1111 vput(newvp);
1112 }
1113 error = ENOENT;
1114 }
1115 if (error) {
1116 m_freem(mrep);
1117 return (error);
1118 }
1119 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1120 VREF(dvp);
1121 newvp = dvp;
1122 } else {
1123 error = nfs_nget(mp, fhp, fhsize, &np, cnp->cn_lkflags);
1124 if (error) {
1125 m_freem(mrep);
1126 return (error);
1127 }
1128 newvp = NFSTOV(np);
1129
1130 /*
1131 * Flush the attribute cache when opening a leaf node
1132 * to ensure that fresh attributes are fetched in
1133 * nfs_open() if we are unable to fetch attributes
1134 * from the LOOKUP reply.
1135 */
1136 if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1137 !(np->n_flag & NMODIFIED)) {
1138 mtx_lock(&np->n_mtx);
1139 np->n_attrstamp = 0;
1140 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1141 mtx_unlock(&np->n_mtx);
1142 }
1143 }
1144 if (v3) {
1145 nfsm_postop_attr_va(newvp, attrflag, &vattr);
1146 nfsm_postop_attr_va(dvp, dattrflag, &dvattr);
1147 } else {
1148 nfsm_loadattr(newvp, &vattr);
1149 attrflag = 1;
1150 }
1151 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1152 cnp->cn_flags |= SAVENAME;
1153 if ((cnp->cn_flags & MAKEENTRY) &&
1154 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1155 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1156 cache_enter_time(dvp, newvp, cnp, &vattr.va_ctime,
1157 newvp->v_type != VDIR ? NULL : &dvattr.va_ctime);
1158 *vpp = newvp;
1159 m_freem(mrep);
1160 nfsmout:
1161 if (error) {
1162 if (newvp != NULLVP) {
1163 vput(newvp);
1164 *vpp = NULLVP;
1165 }
1166
1167 if (error != ENOENT)
1168 goto done;
1169
1170 /* The requested file was not found. */
1171 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1172 (flags & ISLASTCN)) {
1173 /*
1174 * XXX: UFS does a full VOP_ACCESS(dvp,
1175 * VWRITE) here instead of just checking
1176 * MNT_RDONLY.
1177 */
1178 if (mp->mnt_flag & MNT_RDONLY)
1179 return (EROFS);
1180 cnp->cn_flags |= SAVENAME;
1181 return (EJUSTRETURN);
1182 }
1183
1184 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
1185 dattrflag) {
1186 /*
1187 * Cache the modification time of the parent
1188 * directory from the post-op attributes in
1189 * the name cache entry. The negative cache
1190 * entry will be ignored once the directory
1191 * has changed. Don't bother adding the entry
1192 * if the directory has already changed.
1193 */
1194 mtx_lock(&np->n_mtx);
1195 if (timespeccmp(&np->n_vattr.va_mtime,
1196 &vattr.va_mtime, ==)) {
1197 mtx_unlock(&np->n_mtx);
1198 cache_enter_time(dvp, NULL, cnp,
1199 &vattr.va_mtime, NULL);
1200 } else
1201 mtx_unlock(&np->n_mtx);
1202 }
1203 return (ENOENT);
1204 }
1205 done:
1206 return (error);
1207 }
1208
1209 /*
1210 * nfs read call.
1211 * Just call nfs_bioread() to do the work.
1212 */
1213 static int
1214 nfs_read(struct vop_read_args *ap)
1215 {
1216 struct vnode *vp = ap->a_vp;
1217
1218 switch (vp->v_type) {
1219 case VREG:
1220 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1221 case VDIR:
1222 return (EISDIR);
1223 default:
1224 return (EOPNOTSUPP);
1225 }
1226 }
1227
1228 /*
1229 * nfs readlink call
1230 */
1231 static int
1232 nfs_readlink(struct vop_readlink_args *ap)
1233 {
1234 struct vnode *vp = ap->a_vp;
1235
1236 if (vp->v_type != VLNK)
1237 return (EINVAL);
1238 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1239 }
1240
1241 /*
1242 * Do a readlink rpc.
1243 * Called by nfs_doio() from below the buffer cache.
1244 */
1245 int
1246 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1247 {
1248 caddr_t bpos, dpos;
1249 int error = 0, len, attrflag;
1250 struct mbuf *mreq, *mrep, *md, *mb;
1251 int v3 = NFS_ISV3(vp);
1252
1253 nfsstats.rpccnt[NFSPROC_READLINK]++;
1254 mreq = m_get2(NFSX_FH(v3), M_WAITOK, MT_DATA, 0);
1255 mb = mreq;
1256 bpos = mtod(mb, caddr_t);
1257 nfsm_fhtom(vp, v3);
1258 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
1259 if (v3)
1260 nfsm_postop_attr(vp, attrflag);
1261 if (!error) {
1262 nfsm_strsiz(len, NFS_MAXPATHLEN);
1263 if (len == NFS_MAXPATHLEN) {
1264 struct nfsnode *np = VTONFS(vp);
1265 mtx_lock(&np->n_mtx);
1266 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1267 len = np->n_size;
1268 mtx_unlock(&np->n_mtx);
1269 }
1270 nfsm_mtouio(uiop, len);
1271 }
1272 m_freem(mrep);
1273 nfsmout:
1274 return (error);
1275 }
1276
1277 /*
1278 * nfs read rpc call
1279 * Ditto above
1280 */
1281 int
1282 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1283 {
1284 u_int32_t *tl;
1285 caddr_t bpos, dpos;
1286 struct mbuf *mreq, *mrep, *md, *mb;
1287 struct nfsmount *nmp;
1288 off_t end;
1289 int error = 0, len, retlen, tsiz, eof, attrflag;
1290 int v3 = NFS_ISV3(vp);
1291 int rsize;
1292
1293 #ifndef nolint
1294 eof = 0;
1295 #endif
1296 nmp = VFSTONFS(vp->v_mount);
1297 tsiz = uiop->uio_resid;
1298 mtx_lock(&nmp->nm_mtx);
1299 end = uiop->uio_offset + tsiz;
1300 if (end > nmp->nm_maxfilesize || end < uiop->uio_offset) {
1301 mtx_unlock(&nmp->nm_mtx);
1302 return (EFBIG);
1303 }
1304 rsize = nmp->nm_rsize;
1305 mtx_unlock(&nmp->nm_mtx);
1306 while (tsiz > 0) {
1307 nfsstats.rpccnt[NFSPROC_READ]++;
1308 len = (tsiz > rsize) ? rsize : tsiz;
1309 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED * 3, M_WAITOK,
1310 MT_DATA, 0);
1311 mb = mreq;
1312 bpos = mtod(mb, caddr_t);
1313 nfsm_fhtom(vp, v3);
1314 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1315 if (v3) {
1316 txdr_hyper(uiop->uio_offset, tl);
1317 *(tl + 2) = txdr_unsigned(len);
1318 } else {
1319 *tl++ = txdr_unsigned(uiop->uio_offset);
1320 *tl++ = txdr_unsigned(len);
1321 *tl = 0;
1322 }
1323 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1324 if (v3) {
1325 nfsm_postop_attr(vp, attrflag);
1326 if (error) {
1327 m_freem(mrep);
1328 goto nfsmout;
1329 }
1330 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1331 eof = fxdr_unsigned(int, *(tl + 1));
1332 } else {
1333 nfsm_loadattr(vp, NULL);
1334 }
1335 nfsm_strsiz(retlen, rsize);
1336 nfsm_mtouio(uiop, retlen);
1337 m_freem(mrep);
1338 tsiz -= retlen;
1339 if (v3) {
1340 if (eof || retlen == 0) {
1341 tsiz = 0;
1342 }
1343 } else if (retlen < len) {
1344 tsiz = 0;
1345 }
1346 }
1347 nfsmout:
1348 return (error);
1349 }
1350
1351 /*
1352 * nfs write call
1353 */
1354 int
1355 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1356 int *iomode, int *must_commit)
1357 {
1358 u_int32_t *tl;
1359 int32_t backup;
1360 caddr_t bpos, dpos;
1361 struct mbuf *mreq, *mrep, *md, *mb;
1362 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1363 off_t end;
1364 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1365 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1366 int wsize;
1367
1368 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1369 *must_commit = 0;
1370 tsiz = uiop->uio_resid;
1371 mtx_lock(&nmp->nm_mtx);
1372 end = uiop->uio_offset + tsiz;
1373 if (end > nmp->nm_maxfilesize || end < uiop->uio_offset) {
1374 mtx_unlock(&nmp->nm_mtx);
1375 return (EFBIG);
1376 }
1377 wsize = nmp->nm_wsize;
1378 mtx_unlock(&nmp->nm_mtx);
1379 while (tsiz > 0) {
1380 nfsstats.rpccnt[NFSPROC_WRITE]++;
1381 len = (tsiz > wsize) ? wsize : tsiz;
1382 mreq = m_get2(NFSX_FH(v3) + 5 * NFSX_UNSIGNED, M_WAITOK,
1383 MT_DATA, 0);
1384 mb = mreq;
1385 bpos = mtod(mb, caddr_t);
1386 nfsm_fhtom(vp, v3);
1387 if (v3) {
1388 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1389 txdr_hyper(uiop->uio_offset, tl);
1390 tl += 2;
1391 *tl++ = txdr_unsigned(len);
1392 *tl++ = txdr_unsigned(*iomode);
1393 *tl = txdr_unsigned(len);
1394 } else {
1395 u_int32_t x;
1396
1397 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1398 /* Set both "begin" and "current" to non-garbage. */
1399 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1400 *tl++ = x; /* "begin offset" */
1401 *tl++ = x; /* "current offset" */
1402 x = txdr_unsigned(len);
1403 *tl++ = x; /* total to this offset */
1404 *tl = x; /* size of this write */
1405 }
1406 nfsm_uiotom(uiop, len);
1407 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1408 if (v3) {
1409 wccflag = NFSV3_WCCCHK;
1410 nfsm_wcc_data(vp, wccflag);
1411 if (!error) {
1412 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1413 + NFSX_V3WRITEVERF);
1414 rlen = fxdr_unsigned(int, *tl++);
1415 if (rlen == 0) {
1416 error = NFSERR_IO;
1417 m_freem(mrep);
1418 break;
1419 } else if (rlen < len) {
1420 backup = len - rlen;
1421 uiop->uio_iov->iov_base =
1422 (char *)uiop->uio_iov->iov_base -
1423 backup;
1424 uiop->uio_iov->iov_len += backup;
1425 uiop->uio_offset -= backup;
1426 uiop->uio_resid += backup;
1427 len = rlen;
1428 }
1429 commit = fxdr_unsigned(int, *tl++);
1430
1431 /*
1432 * Return the lowest committment level
1433 * obtained by any of the RPCs.
1434 */
1435 if (committed == NFSV3WRITE_FILESYNC)
1436 committed = commit;
1437 else if (committed == NFSV3WRITE_DATASYNC &&
1438 commit == NFSV3WRITE_UNSTABLE)
1439 committed = commit;
1440 mtx_lock(&nmp->nm_mtx);
1441 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1442 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1443 NFSX_V3WRITEVERF);
1444 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1445 } else if (bcmp((caddr_t)tl,
1446 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1447 *must_commit = 1;
1448 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1449 NFSX_V3WRITEVERF);
1450 }
1451 mtx_unlock(&nmp->nm_mtx);
1452 }
1453 } else {
1454 nfsm_loadattr(vp, NULL);
1455 }
1456 if (wccflag) {
1457 mtx_lock(&(VTONFS(vp))->n_mtx);
1458 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1459 mtx_unlock(&(VTONFS(vp))->n_mtx);
1460 }
1461 m_freem(mrep);
1462 if (error)
1463 break;
1464 tsiz -= len;
1465 }
1466 nfsmout:
1467 if (DOINGASYNC(vp))
1468 committed = NFSV3WRITE_FILESYNC;
1469 *iomode = committed;
1470 if (error)
1471 uiop->uio_resid = tsiz;
1472 return (error);
1473 }
1474
1475 /*
1476 * nfs mknod rpc
1477 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1478 * mode set to specify the file type and the size field for rdev.
1479 */
1480 static int
1481 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1482 struct vattr *vap)
1483 {
1484 struct nfsv2_sattr *sp;
1485 u_int32_t *tl;
1486 struct vnode *newvp = NULL;
1487 struct nfsnode *np = NULL;
1488 struct vattr vattr;
1489 caddr_t bpos, dpos;
1490 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1491 struct mbuf *mreq, *mrep, *md, *mb;
1492 u_int32_t rdev;
1493 int v3 = NFS_ISV3(dvp);
1494
1495 if (vap->va_type == VCHR || vap->va_type == VBLK)
1496 rdev = txdr_unsigned(vap->va_rdev);
1497 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1498 rdev = nfs_xdrneg1;
1499 else {
1500 return (EOPNOTSUPP);
1501 }
1502 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
1503 return (error);
1504 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1505 mreq = m_get2(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1506 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
1507 mb = mreq;
1508 bpos = mtod(mb, caddr_t);
1509 nfsm_fhtom(dvp, v3);
1510 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1511 if (v3) {
1512 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1513 *tl++ = vtonfsv3_type(vap->va_type);
1514 nfsm_v3attrbuild(vap, FALSE);
1515 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1516 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1517 *tl++ = txdr_unsigned(major(vap->va_rdev));
1518 *tl = txdr_unsigned(minor(vap->va_rdev));
1519 }
1520 } else {
1521 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1522 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1523 sp->sa_uid = nfs_xdrneg1;
1524 sp->sa_gid = nfs_xdrneg1;
1525 sp->sa_size = rdev;
1526 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1527 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1528 }
1529 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1530 if (!error) {
1531 nfsm_mtofh(dvp, newvp, v3, gotvp);
1532 if (!gotvp) {
1533 if (newvp) {
1534 vput(newvp);
1535 newvp = NULL;
1536 }
1537 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1538 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1539 if (!error)
1540 newvp = NFSTOV(np);
1541 }
1542 }
1543 if (v3)
1544 nfsm_wcc_data(dvp, wccflag);
1545 m_freem(mrep);
1546 nfsmout:
1547 if (error) {
1548 if (newvp)
1549 vput(newvp);
1550 } else {
1551 *vpp = newvp;
1552 }
1553 mtx_lock(&(VTONFS(dvp))->n_mtx);
1554 VTONFS(dvp)->n_flag |= NMODIFIED;
1555 if (!wccflag) {
1556 VTONFS(dvp)->n_attrstamp = 0;
1557 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1558 }
1559 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1560 return (error);
1561 }
1562
1563 /*
1564 * nfs mknod vop
1565 * just call nfs_mknodrpc() to do the work.
1566 */
1567 /* ARGSUSED */
1568 static int
1569 nfs_mknod(struct vop_mknod_args *ap)
1570 {
1571 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1572 }
1573
1574 static u_long create_verf;
1575 /*
1576 * nfs file create call
1577 */
1578 static int
1579 nfs_create(struct vop_create_args *ap)
1580 {
1581 struct vnode *dvp = ap->a_dvp;
1582 struct vattr *vap = ap->a_vap;
1583 struct componentname *cnp = ap->a_cnp;
1584 struct nfsv2_sattr *sp;
1585 u_int32_t *tl;
1586 struct nfsnode *np = NULL;
1587 struct vnode *newvp = NULL;
1588 caddr_t bpos, dpos;
1589 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1590 struct mbuf *mreq, *mrep, *md, *mb;
1591 struct vattr vattr;
1592 int v3 = NFS_ISV3(dvp);
1593
1594 /*
1595 * Oops, not for me..
1596 */
1597 if (vap->va_type == VSOCK) {
1598 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1599 return (error);
1600 }
1601
1602 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0) {
1603 return (error);
1604 }
1605 if (vap->va_vaflags & VA_EXCLUSIVE)
1606 fmode |= O_EXCL;
1607 again:
1608 nfsstats.rpccnt[NFSPROC_CREATE]++;
1609 mreq = m_get2(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1610 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
1611 mb = mreq;
1612 bpos = mtod(mb, caddr_t);
1613 nfsm_fhtom(dvp, v3);
1614 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1615 if (v3) {
1616 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1617 if (fmode & O_EXCL) {
1618 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1619 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1620 #ifdef INET
1621 CURVNET_SET(CRED_TO_VNET(cnp->cn_cred));
1622 IN_IFADDR_RLOCK();
1623 if (!TAILQ_EMPTY(&V_in_ifaddrhead))
1624 *tl++ = IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr.s_addr;
1625 else
1626 #endif
1627 *tl++ = create_verf;
1628 #ifdef INET
1629 IN_IFADDR_RUNLOCK();
1630 CURVNET_RESTORE();
1631 #endif
1632 *tl = ++create_verf;
1633 } else {
1634 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1635 nfsm_v3attrbuild(vap, FALSE);
1636 }
1637 } else {
1638 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1639 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1640 sp->sa_uid = nfs_xdrneg1;
1641 sp->sa_gid = nfs_xdrneg1;
1642 sp->sa_size = 0;
1643 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1644 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1645 }
1646 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1647 if (!error) {
1648 nfsm_mtofh(dvp, newvp, v3, gotvp);
1649 if (!gotvp) {
1650 if (newvp) {
1651 vput(newvp);
1652 newvp = NULL;
1653 }
1654 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1655 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1656 if (!error)
1657 newvp = NFSTOV(np);
1658 }
1659 }
1660 if (v3)
1661 nfsm_wcc_data(dvp, wccflag);
1662 m_freem(mrep);
1663 nfsmout:
1664 if (error) {
1665 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1666 fmode &= ~O_EXCL;
1667 goto again;
1668 }
1669 if (newvp)
1670 vput(newvp);
1671 } else if (v3 && (fmode & O_EXCL)) {
1672 /*
1673 * We are normally called with only a partially initialized
1674 * VAP. Since the NFSv3 spec says that server may use the
1675 * file attributes to store the verifier, the spec requires
1676 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1677 * in atime, but we can't really assume that all servers will
1678 * so we ensure that our SETATTR sets both atime and mtime.
1679 */
1680 if (vap->va_mtime.tv_sec == VNOVAL)
1681 vfs_timestamp(&vap->va_mtime);
1682 if (vap->va_atime.tv_sec == VNOVAL)
1683 vap->va_atime = vap->va_mtime;
1684 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred);
1685 if (error)
1686 vput(newvp);
1687 }
1688 if (!error) {
1689 *ap->a_vpp = newvp;
1690 }
1691 mtx_lock(&(VTONFS(dvp))->n_mtx);
1692 VTONFS(dvp)->n_flag |= NMODIFIED;
1693 if (!wccflag) {
1694 VTONFS(dvp)->n_attrstamp = 0;
1695 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1696 }
1697 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1698 return (error);
1699 }
1700
1701 /*
1702 * nfs file remove call
1703 * To try and make nfs semantics closer to ufs semantics, a file that has
1704 * other processes using the vnode is renamed instead of removed and then
1705 * removed later on the last close.
1706 * - If v_usecount > 1
1707 * If a rename is not already in the works
1708 * call nfs_sillyrename() to set it up
1709 * else
1710 * do the remove rpc
1711 */
1712 static int
1713 nfs_remove(struct vop_remove_args *ap)
1714 {
1715 struct vnode *vp = ap->a_vp;
1716 struct vnode *dvp = ap->a_dvp;
1717 struct componentname *cnp = ap->a_cnp;
1718 struct nfsnode *np = VTONFS(vp);
1719 int error = 0;
1720 struct vattr vattr;
1721
1722 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1723 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1724 if (vp->v_type == VDIR)
1725 error = EPERM;
1726 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1727 !VOP_GETATTR(vp, &vattr, cnp->cn_cred) && vattr.va_nlink > 1)) {
1728 /*
1729 * Purge the name cache so that the chance of a lookup for
1730 * the name succeeding while the remove is in progress is
1731 * minimized. Without node locking it can still happen, such
1732 * that an I/O op returns ESTALE, but since you get this if
1733 * another host removes the file..
1734 */
1735 cache_purge(vp);
1736 /*
1737 * throw away biocache buffers, mainly to avoid
1738 * unnecessary delayed writes later.
1739 */
1740 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1741 /* Do the rpc */
1742 if (error != EINTR && error != EIO)
1743 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1744 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1745 /*
1746 * Kludge City: If the first reply to the remove rpc is lost..
1747 * the reply to the retransmitted request will be ENOENT
1748 * since the file was in fact removed
1749 * Therefore, we cheat and return success.
1750 */
1751 if (error == ENOENT)
1752 error = 0;
1753 } else if (!np->n_sillyrename)
1754 error = nfs_sillyrename(dvp, vp, cnp);
1755 mtx_lock(&np->n_mtx);
1756 np->n_attrstamp = 0;
1757 mtx_unlock(&np->n_mtx);
1758 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1759 return (error);
1760 }
1761
1762 /*
1763 * nfs file remove rpc called from nfs_inactive
1764 */
1765 int
1766 nfs_removeit(struct sillyrename *sp)
1767 {
1768 /*
1769 * Make sure that the directory vnode is still valid.
1770 * XXX we should lock sp->s_dvp here.
1771 */
1772 if (sp->s_dvp->v_type == VBAD)
1773 return (0);
1774 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1775 NULL));
1776 }
1777
1778 /*
1779 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1780 */
1781 static int
1782 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1783 struct ucred *cred, struct thread *td)
1784 {
1785 caddr_t bpos, dpos;
1786 int error = 0, wccflag = NFSV3_WCCRATTR;
1787 struct mbuf *mreq, *mrep, *md, *mb;
1788 int v3 = NFS_ISV3(dvp);
1789
1790 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1791 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen),
1792 M_WAITOK, MT_DATA, 0);
1793 mb = mreq;
1794 bpos = mtod(mb, caddr_t);
1795 nfsm_fhtom(dvp, v3);
1796 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1797 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1798 if (v3)
1799 nfsm_wcc_data(dvp, wccflag);
1800 m_freem(mrep);
1801 nfsmout:
1802 mtx_lock(&(VTONFS(dvp))->n_mtx);
1803 VTONFS(dvp)->n_flag |= NMODIFIED;
1804 if (!wccflag) {
1805 VTONFS(dvp)->n_attrstamp = 0;
1806 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1807 }
1808 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1809 return (error);
1810 }
1811
1812 /*
1813 * nfs file rename call
1814 */
1815 static int
1816 nfs_rename(struct vop_rename_args *ap)
1817 {
1818 struct vnode *fvp = ap->a_fvp;
1819 struct vnode *tvp = ap->a_tvp;
1820 struct vnode *fdvp = ap->a_fdvp;
1821 struct vnode *tdvp = ap->a_tdvp;
1822 struct componentname *tcnp = ap->a_tcnp;
1823 struct componentname *fcnp = ap->a_fcnp;
1824 int error;
1825
1826 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1827 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1828 /* Check for cross-device rename */
1829 if ((fvp->v_mount != tdvp->v_mount) ||
1830 (tvp && (fvp->v_mount != tvp->v_mount))) {
1831 error = EXDEV;
1832 goto out;
1833 }
1834
1835 if (fvp == tvp) {
1836 nfs_printf("nfs_rename: fvp == tvp (can't happen)\n");
1837 error = 0;
1838 goto out;
1839 }
1840 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1841 goto out;
1842
1843 /*
1844 * We have to flush B_DELWRI data prior to renaming
1845 * the file. If we don't, the delayed-write buffers
1846 * can be flushed out later after the file has gone stale
1847 * under NFSV3. NFSV2 does not have this problem because
1848 * ( as far as I can tell ) it flushes dirty buffers more
1849 * often.
1850 *
1851 * Skip the rename operation if the fsync fails, this can happen
1852 * due to the server's volume being full, when we pushed out data
1853 * that was written back to our cache earlier. Not checking for
1854 * this condition can result in potential (silent) data loss.
1855 */
1856 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1857 VOP_UNLOCK(fvp, 0);
1858 if (!error && tvp)
1859 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1860 if (error)
1861 goto out;
1862
1863 /*
1864 * If the tvp exists and is in use, sillyrename it before doing the
1865 * rename of the new file over it.
1866 * XXX Can't sillyrename a directory.
1867 */
1868 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1869 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1870 vput(tvp);
1871 tvp = NULL;
1872 }
1873
1874 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1875 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1876 tcnp->cn_thread);
1877
1878 if (fvp->v_type == VDIR) {
1879 if (tvp != NULL && tvp->v_type == VDIR)
1880 cache_purge(tdvp);
1881 cache_purge(fdvp);
1882 }
1883
1884 out:
1885 if (tdvp == tvp)
1886 vrele(tdvp);
1887 else
1888 vput(tdvp);
1889 if (tvp)
1890 vput(tvp);
1891 vrele(fdvp);
1892 vrele(fvp);
1893 /*
1894 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1895 */
1896 if (error == ENOENT)
1897 error = 0;
1898 return (error);
1899 }
1900
1901 /*
1902 * nfs file rename rpc called from nfs_remove() above
1903 */
1904 static int
1905 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1906 struct sillyrename *sp)
1907 {
1908
1909 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1910 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1911 }
1912
1913 /*
1914 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1915 */
1916 static int
1917 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1918 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1919 struct thread *td)
1920 {
1921 caddr_t bpos, dpos;
1922 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1923 struct mbuf *mreq, *mrep, *md, *mb;
1924 int v3 = NFS_ISV3(fdvp);
1925
1926 nfsstats.rpccnt[NFSPROC_RENAME]++;
1927 mreq = m_get2((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1928 nfsm_rndup(tnamelen), M_WAITOK, MT_DATA, 0);
1929 mb = mreq;
1930 bpos = mtod(mb, caddr_t);
1931 nfsm_fhtom(fdvp, v3);
1932 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1933 nfsm_fhtom(tdvp, v3);
1934 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1935 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1936 if (v3) {
1937 nfsm_wcc_data(fdvp, fwccflag);
1938 nfsm_wcc_data(tdvp, twccflag);
1939 }
1940 m_freem(mrep);
1941 nfsmout:
1942 mtx_lock(&(VTONFS(fdvp))->n_mtx);
1943 VTONFS(fdvp)->n_flag |= NMODIFIED;
1944 mtx_unlock(&(VTONFS(fdvp))->n_mtx);
1945 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1946 VTONFS(tdvp)->n_flag |= NMODIFIED;
1947 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1948 if (!fwccflag) {
1949 VTONFS(fdvp)->n_attrstamp = 0;
1950 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1951 }
1952 if (!twccflag) {
1953 VTONFS(tdvp)->n_attrstamp = 0;
1954 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1955 }
1956 return (error);
1957 }
1958
1959 /*
1960 * nfs hard link create call
1961 */
1962 static int
1963 nfs_link(struct vop_link_args *ap)
1964 {
1965 struct vnode *vp = ap->a_vp;
1966 struct vnode *tdvp = ap->a_tdvp;
1967 struct componentname *cnp = ap->a_cnp;
1968 caddr_t bpos, dpos;
1969 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1970 struct mbuf *mreq, *mrep, *md, *mb;
1971 int v3;
1972
1973 if (vp->v_mount != tdvp->v_mount) {
1974 return (EXDEV);
1975 }
1976
1977 /*
1978 * Push all writes to the server, so that the attribute cache
1979 * doesn't get "out of sync" with the server.
1980 * XXX There should be a better way!
1981 */
1982 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1983
1984 v3 = NFS_ISV3(vp);
1985 nfsstats.rpccnt[NFSPROC_LINK]++;
1986 mreq = m_get2(NFSX_FH(v3)*2 + NFSX_UNSIGNED +
1987 nfsm_rndup(cnp->cn_namelen), M_WAITOK, MT_DATA, 0);
1988 mb = mreq;
1989 bpos = mtod(mb, caddr_t);
1990 nfsm_fhtom(vp, v3);
1991 nfsm_fhtom(tdvp, v3);
1992 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1993 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1994 if (v3) {
1995 nfsm_postop_attr(vp, attrflag);
1996 nfsm_wcc_data(tdvp, wccflag);
1997 }
1998 m_freem(mrep);
1999 nfsmout:
2000 mtx_lock(&(VTONFS(tdvp))->n_mtx);
2001 VTONFS(tdvp)->n_flag |= NMODIFIED;
2002 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
2003 if (!attrflag) {
2004 VTONFS(vp)->n_attrstamp = 0;
2005 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2006 }
2007 if (!wccflag) {
2008 VTONFS(tdvp)->n_attrstamp = 0;
2009 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2010 }
2011 return (error);
2012 }
2013
2014 /*
2015 * nfs symbolic link create call
2016 */
2017 static int
2018 nfs_symlink(struct vop_symlink_args *ap)
2019 {
2020 struct vnode *dvp = ap->a_dvp;
2021 struct vattr *vap = ap->a_vap;
2022 struct componentname *cnp = ap->a_cnp;
2023 struct nfsv2_sattr *sp;
2024 caddr_t bpos, dpos;
2025 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2026 struct mbuf *mreq, *mrep, *md, *mb;
2027 struct vnode *newvp = NULL;
2028 int v3 = NFS_ISV3(dvp);
2029
2030 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2031 slen = strlen(ap->a_target);
2032 mreq = m_get2(NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2033 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3),
2034 M_WAITOK, MT_DATA, 0);
2035 mb = mreq;
2036 bpos = mtod(mb, caddr_t);
2037 nfsm_fhtom(dvp, v3);
2038 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2039 if (v3) {
2040 nfsm_v3attrbuild(vap, FALSE);
2041 }
2042 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2043 if (!v3) {
2044 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
2045 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2046 sp->sa_uid = nfs_xdrneg1;
2047 sp->sa_gid = nfs_xdrneg1;
2048 sp->sa_size = nfs_xdrneg1;
2049 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2050 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2051 }
2052
2053 /*
2054 * Issue the NFS request and get the rpc response.
2055 *
2056 * Only NFSv3 responses returning an error of 0 actually return
2057 * a file handle that can be converted into newvp without having
2058 * to do an extra lookup rpc.
2059 */
2060 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
2061 if (v3) {
2062 if (error == 0)
2063 nfsm_mtofh(dvp, newvp, v3, gotvp);
2064 nfsm_wcc_data(dvp, wccflag);
2065 }
2066
2067 /*
2068 * out code jumps -> here, mrep is also freed.
2069 */
2070
2071 m_freem(mrep);
2072 nfsmout:
2073
2074 /*
2075 * If we do not have an error and we could not extract the newvp from
2076 * the response due to the request being NFSv2, we have to do a
2077 * lookup in order to obtain a newvp to return.
2078 */
2079 if (error == 0 && newvp == NULL) {
2080 struct nfsnode *np = NULL;
2081
2082 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2083 cnp->cn_cred, cnp->cn_thread, &np);
2084 if (!error)
2085 newvp = NFSTOV(np);
2086 }
2087 if (error) {
2088 if (newvp)
2089 vput(newvp);
2090 } else {
2091 *ap->a_vpp = newvp;
2092 }
2093 mtx_lock(&(VTONFS(dvp))->n_mtx);
2094 VTONFS(dvp)->n_flag |= NMODIFIED;
2095 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2096 if (!wccflag) {
2097 VTONFS(dvp)->n_attrstamp = 0;
2098 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2099 }
2100 return (error);
2101 }
2102
2103 /*
2104 * nfs make dir call
2105 */
2106 static int
2107 nfs_mkdir(struct vop_mkdir_args *ap)
2108 {
2109 struct vnode *dvp = ap->a_dvp;
2110 struct vattr *vap = ap->a_vap;
2111 struct componentname *cnp = ap->a_cnp;
2112 struct nfsv2_sattr *sp;
2113 int len;
2114 struct nfsnode *np = NULL;
2115 struct vnode *newvp = NULL;
2116 caddr_t bpos, dpos;
2117 int error = 0, wccflag = NFSV3_WCCRATTR;
2118 int gotvp = 0;
2119 struct mbuf *mreq, *mrep, *md, *mb;
2120 struct vattr vattr;
2121 int v3 = NFS_ISV3(dvp);
2122
2123 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2124 return (error);
2125 len = cnp->cn_namelen;
2126 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2127 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) +
2128 NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
2129 mb = mreq;
2130 bpos = mtod(mb, caddr_t);
2131 nfsm_fhtom(dvp, v3);
2132 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2133 if (v3) {
2134 nfsm_v3attrbuild(vap, FALSE);
2135 } else {
2136 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
2137 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2138 sp->sa_uid = nfs_xdrneg1;
2139 sp->sa_gid = nfs_xdrneg1;
2140 sp->sa_size = nfs_xdrneg1;
2141 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2142 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2143 }
2144 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
2145 if (!error)
2146 nfsm_mtofh(dvp, newvp, v3, gotvp);
2147 if (v3)
2148 nfsm_wcc_data(dvp, wccflag);
2149 m_freem(mrep);
2150 nfsmout:
2151 mtx_lock(&(VTONFS(dvp))->n_mtx);
2152 VTONFS(dvp)->n_flag |= NMODIFIED;
2153 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2154 if (!wccflag) {
2155 VTONFS(dvp)->n_attrstamp = 0;
2156 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2157 }
2158 if (error == 0 && newvp == NULL) {
2159 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2160 cnp->cn_thread, &np);
2161 if (!error) {
2162 newvp = NFSTOV(np);
2163 if (newvp->v_type != VDIR)
2164 error = EEXIST;
2165 }
2166 }
2167 if (error) {
2168 if (newvp)
2169 vput(newvp);
2170 } else
2171 *ap->a_vpp = newvp;
2172 return (error);
2173 }
2174
2175 /*
2176 * nfs remove directory call
2177 */
2178 static int
2179 nfs_rmdir(struct vop_rmdir_args *ap)
2180 {
2181 struct vnode *vp = ap->a_vp;
2182 struct vnode *dvp = ap->a_dvp;
2183 struct componentname *cnp = ap->a_cnp;
2184 caddr_t bpos, dpos;
2185 int error = 0, wccflag = NFSV3_WCCRATTR;
2186 struct mbuf *mreq, *mrep, *md, *mb;
2187 int v3 = NFS_ISV3(dvp);
2188
2189 if (dvp == vp)
2190 return (EINVAL);
2191 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2192 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED +
2193 nfsm_rndup(cnp->cn_namelen), M_WAITOK, MT_DATA, 0);
2194 mb = mreq;
2195 bpos = mtod(mb, caddr_t);
2196 nfsm_fhtom(dvp, v3);
2197 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2198 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
2199 if (v3)
2200 nfsm_wcc_data(dvp, wccflag);
2201 m_freem(mrep);
2202 nfsmout:
2203 mtx_lock(&(VTONFS(dvp))->n_mtx);
2204 VTONFS(dvp)->n_flag |= NMODIFIED;
2205 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2206 if (!wccflag) {
2207 VTONFS(dvp)->n_attrstamp = 0;
2208 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2209 }
2210 cache_purge(dvp);
2211 cache_purge(vp);
2212 /*
2213 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2214 */
2215 if (error == ENOENT)
2216 error = 0;
2217 return (error);
2218 }
2219
2220 /*
2221 * nfs readdir call
2222 */
2223 static int
2224 nfs_readdir(struct vop_readdir_args *ap)
2225 {
2226 struct vnode *vp = ap->a_vp;
2227 struct nfsnode *np = VTONFS(vp);
2228 struct uio *uio = ap->a_uio;
2229 int tresid, error = 0;
2230 struct vattr vattr;
2231
2232 if (vp->v_type != VDIR)
2233 return(EPERM);
2234
2235 /*
2236 * First, check for hit on the EOF offset cache
2237 */
2238 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2239 (np->n_flag & NMODIFIED) == 0) {
2240 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2241 mtx_lock(&np->n_mtx);
2242 if (!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2243 mtx_unlock(&np->n_mtx);
2244 nfsstats.direofcache_hits++;
2245 goto out;
2246 } else
2247 mtx_unlock(&np->n_mtx);
2248 }
2249 }
2250
2251 /*
2252 * Call nfs_bioread() to do the real work.
2253 */
2254 tresid = uio->uio_resid;
2255 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2256
2257 if (!error && uio->uio_resid == tresid) {
2258 nfsstats.direofcache_misses++;
2259 }
2260 out:
2261 return (error);
2262 }
2263
2264 /*
2265 * Readdir rpc call.
2266 * Called from below the buffer cache by nfs_doio().
2267 */
2268 int
2269 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2270 {
2271 int len, left;
2272 struct dirent *dp = NULL;
2273 u_int32_t *tl;
2274 caddr_t cp;
2275 nfsuint64 *cookiep;
2276 caddr_t bpos, dpos;
2277 struct mbuf *mreq, *mrep, *md, *mb;
2278 nfsuint64 cookie;
2279 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2280 struct nfsnode *dnp = VTONFS(vp);
2281 u_quad_t fileno;
2282 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2283 int attrflag;
2284 int v3 = NFS_ISV3(vp);
2285
2286 KASSERT(uiop->uio_iovcnt == 1 &&
2287 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2288 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2289 ("nfs readdirrpc bad uio"));
2290
2291 /*
2292 * If there is no cookie, assume directory was stale.
2293 */
2294 nfs_dircookie_lock(dnp);
2295 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2296 if (cookiep) {
2297 cookie = *cookiep;
2298 nfs_dircookie_unlock(dnp);
2299 } else {
2300 nfs_dircookie_unlock(dnp);
2301 return (NFSERR_BAD_COOKIE);
2302 }
2303
2304 /*
2305 * Loop around doing readdir rpc's of size nm_readdirsize
2306 * truncated to a multiple of DIRBLKSIZ.
2307 * The stopping criteria is EOF or buffer full.
2308 */
2309 while (more_dirs && bigenough) {
2310 nfsstats.rpccnt[NFSPROC_READDIR]++;
2311 mreq = m_get2(NFSX_FH(v3) + NFSX_READDIR(v3), M_WAITOK,
2312 MT_DATA, 0);
2313 mb = mreq;
2314 bpos = mtod(mb, caddr_t);
2315 nfsm_fhtom(vp, v3);
2316 if (v3) {
2317 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
2318 *tl++ = cookie.nfsuquad[0];
2319 *tl++ = cookie.nfsuquad[1];
2320 mtx_lock(&dnp->n_mtx);
2321 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2322 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2323 mtx_unlock(&dnp->n_mtx);
2324 } else {
2325 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2326 *tl++ = cookie.nfsuquad[0];
2327 }
2328 *tl = txdr_unsigned(nmp->nm_readdirsize);
2329 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
2330 if (v3) {
2331 nfsm_postop_attr(vp, attrflag);
2332 if (!error) {
2333 tl = nfsm_dissect(u_int32_t *,
2334 2 * NFSX_UNSIGNED);
2335 mtx_lock(&dnp->n_mtx);
2336 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2337 dnp->n_cookieverf.nfsuquad[1] = *tl;
2338 mtx_unlock(&dnp->n_mtx);
2339 } else {
2340 m_freem(mrep);
2341 goto nfsmout;
2342 }
2343 }
2344 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2345 more_dirs = fxdr_unsigned(int, *tl);
2346
2347 /* loop thru the dir entries, doctoring them to 4bsd form */
2348 while (more_dirs && bigenough) {
2349 if (v3) {
2350 tl = nfsm_dissect(u_int32_t *,
2351 3 * NFSX_UNSIGNED);
2352 fileno = fxdr_hyper(tl);
2353 len = fxdr_unsigned(int, *(tl + 2));
2354 } else {
2355 tl = nfsm_dissect(u_int32_t *,
2356 2 * NFSX_UNSIGNED);
2357 fileno = fxdr_unsigned(u_quad_t, *tl++);
2358 len = fxdr_unsigned(int, *tl);
2359 }
2360 if (len <= 0 || len > NFS_MAXNAMLEN) {
2361 error = EBADRPC;
2362 m_freem(mrep);
2363 goto nfsmout;
2364 }
2365 tlen = nfsm_rndup(len);
2366 if (tlen == len)
2367 tlen += 4; /* To ensure null termination */
2368 left = DIRBLKSIZ - blksiz;
2369 if ((tlen + DIRHDSIZ) > left) {
2370 dp->d_reclen += left;
2371 uiop->uio_iov->iov_base =
2372 (char *)uiop->uio_iov->iov_base + left;
2373 uiop->uio_iov->iov_len -= left;
2374 uiop->uio_offset += left;
2375 uiop->uio_resid -= left;
2376 blksiz = 0;
2377 }
2378 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2379 bigenough = 0;
2380 if (bigenough) {
2381 dp = (struct dirent *)uiop->uio_iov->iov_base;
2382 dp->d_fileno = (int)fileno;
2383 dp->d_namlen = len;
2384 dp->d_reclen = tlen + DIRHDSIZ;
2385 dp->d_type = DT_UNKNOWN;
2386 blksiz += dp->d_reclen;
2387 if (blksiz == DIRBLKSIZ)
2388 blksiz = 0;
2389 uiop->uio_offset += DIRHDSIZ;
2390 uiop->uio_resid -= DIRHDSIZ;
2391 uiop->uio_iov->iov_base =
2392 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2393 uiop->uio_iov->iov_len -= DIRHDSIZ;
2394 nfsm_mtouio(uiop, len);
2395 cp = uiop->uio_iov->iov_base;
2396 tlen -= len;
2397 *cp = '\0'; /* null terminate */
2398 uiop->uio_iov->iov_base =
2399 (char *)uiop->uio_iov->iov_base + tlen;
2400 uiop->uio_iov->iov_len -= tlen;
2401 uiop->uio_offset += tlen;
2402 uiop->uio_resid -= tlen;
2403 } else
2404 nfsm_adv(nfsm_rndup(len));
2405 if (v3) {
2406 tl = nfsm_dissect(u_int32_t *,
2407 3 * NFSX_UNSIGNED);
2408 } else {
2409 tl = nfsm_dissect(u_int32_t *,
2410 2 * NFSX_UNSIGNED);
2411 }
2412 if (bigenough) {
2413 cookie.nfsuquad[0] = *tl++;
2414 if (v3)
2415 cookie.nfsuquad[1] = *tl++;
2416 } else if (v3)
2417 tl += 2;
2418 else
2419 tl++;
2420 more_dirs = fxdr_unsigned(int, *tl);
2421 }
2422 /*
2423 * If at end of rpc data, get the eof boolean
2424 */
2425 if (!more_dirs) {
2426 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2427 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2428 }
2429 m_freem(mrep);
2430 }
2431 /*
2432 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2433 * by increasing d_reclen for the last record.
2434 */
2435 if (blksiz > 0) {
2436 left = DIRBLKSIZ - blksiz;
2437 dp->d_reclen += left;
2438 uiop->uio_iov->iov_base =
2439 (char *)uiop->uio_iov->iov_base + left;
2440 uiop->uio_iov->iov_len -= left;
2441 uiop->uio_offset += left;
2442 uiop->uio_resid -= left;
2443 }
2444
2445 /*
2446 * We are now either at the end of the directory or have filled the
2447 * block.
2448 */
2449 if (bigenough)
2450 dnp->n_direofoffset = uiop->uio_offset;
2451 else {
2452 if (uiop->uio_resid > 0)
2453 nfs_printf("EEK! readdirrpc resid > 0\n");
2454 nfs_dircookie_lock(dnp);
2455 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2456 *cookiep = cookie;
2457 nfs_dircookie_unlock(dnp);
2458 }
2459 nfsmout:
2460 return (error);
2461 }
2462
2463 /*
2464 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2465 */
2466 int
2467 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2468 {
2469 int len, left;
2470 struct dirent *dp;
2471 u_int32_t *tl;
2472 caddr_t cp;
2473 struct vnode *newvp;
2474 nfsuint64 *cookiep;
2475 caddr_t bpos, dpos, dpossav1, dpossav2;
2476 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2477 struct nameidata nami, *ndp = &nami;
2478 struct componentname *cnp = &ndp->ni_cnd;
2479 nfsuint64 cookie;
2480 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2481 struct nfsnode *dnp = VTONFS(vp), *np;
2482 struct vattr vattr, dvattr;
2483 nfsfh_t *fhp;
2484 u_quad_t fileno;
2485 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2486 int attrflag, dattrflag, fhsize;
2487
2488 #ifndef nolint
2489 dp = NULL;
2490 #endif
2491 KASSERT(uiop->uio_iovcnt == 1 &&
2492 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2493 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2494 ("nfs readdirplusrpc bad uio"));
2495 ndp->ni_dvp = vp;
2496 newvp = NULLVP;
2497
2498 /*
2499 * If there is no cookie, assume directory was stale.
2500 */
2501 nfs_dircookie_lock(dnp);
2502 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2503 if (cookiep) {
2504 cookie = *cookiep;
2505 nfs_dircookie_unlock(dnp);
2506 } else {
2507 nfs_dircookie_unlock(dnp);
2508 return (NFSERR_BAD_COOKIE);
2509 }
2510 /*
2511 * Loop around doing readdir rpc's of size nm_readdirsize
2512 * truncated to a multiple of DIRBLKSIZ.
2513 * The stopping criteria is EOF or buffer full.
2514 */
2515 while (more_dirs && bigenough) {
2516 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2517 mreq = m_get2(NFSX_FH(1) + 6 * NFSX_UNSIGNED, M_WAITOK,
2518 MT_DATA, 0);
2519 mb = mreq;
2520 bpos = mtod(mb, caddr_t);
2521 nfsm_fhtom(vp, 1);
2522 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2523 *tl++ = cookie.nfsuquad[0];
2524 *tl++ = cookie.nfsuquad[1];
2525 mtx_lock(&dnp->n_mtx);
2526 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2527 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2528 mtx_unlock(&dnp->n_mtx);
2529 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2530 *tl = txdr_unsigned(nmp->nm_rsize);
2531 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2532 nfsm_postop_attr_va(vp, dattrflag, &dvattr);
2533 if (error) {
2534 m_freem(mrep);
2535 goto nfsmout;
2536 }
2537 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2538 mtx_lock(&dnp->n_mtx);
2539 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2540 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2541 mtx_unlock(&dnp->n_mtx);
2542 more_dirs = fxdr_unsigned(int, *tl);
2543
2544 /* loop thru the dir entries, doctoring them to 4bsd form */
2545 while (more_dirs && bigenough) {
2546 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2547 fileno = fxdr_hyper(tl);
2548 len = fxdr_unsigned(int, *(tl + 2));
2549 if (len <= 0 || len > NFS_MAXNAMLEN) {
2550 error = EBADRPC;
2551 m_freem(mrep);
2552 goto nfsmout;
2553 }
2554 tlen = nfsm_rndup(len);
2555 if (tlen == len)
2556 tlen += 4; /* To ensure null termination*/
2557 left = DIRBLKSIZ - blksiz;
2558 if ((tlen + DIRHDSIZ) > left) {
2559 dp->d_reclen += left;
2560 uiop->uio_iov->iov_base =
2561 (char *)uiop->uio_iov->iov_base + left;
2562 uiop->uio_iov->iov_len -= left;
2563 uiop->uio_offset += left;
2564 uiop->uio_resid -= left;
2565 blksiz = 0;
2566 }
2567 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2568 bigenough = 0;
2569 if (bigenough) {
2570 dp = (struct dirent *)uiop->uio_iov->iov_base;
2571 dp->d_fileno = (int)fileno;
2572 dp->d_namlen = len;
2573 dp->d_reclen = tlen + DIRHDSIZ;
2574 dp->d_type = DT_UNKNOWN;
2575 blksiz += dp->d_reclen;
2576 if (blksiz == DIRBLKSIZ)
2577 blksiz = 0;
2578 uiop->uio_offset += DIRHDSIZ;
2579 uiop->uio_resid -= DIRHDSIZ;
2580 uiop->uio_iov->iov_base =
2581 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2582 uiop->uio_iov->iov_len -= DIRHDSIZ;
2583 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2584 cnp->cn_namelen = len;
2585 nfsm_mtouio(uiop, len);
2586 cp = uiop->uio_iov->iov_base;
2587 tlen -= len;
2588 *cp = '\0';
2589 uiop->uio_iov->iov_base =
2590 (char *)uiop->uio_iov->iov_base + tlen;
2591 uiop->uio_iov->iov_len -= tlen;
2592 uiop->uio_offset += tlen;
2593 uiop->uio_resid -= tlen;
2594 } else
2595 nfsm_adv(nfsm_rndup(len));
2596 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2597 if (bigenough) {
2598 cookie.nfsuquad[0] = *tl++;
2599 cookie.nfsuquad[1] = *tl++;
2600 } else
2601 tl += 2;
2602
2603 /*
2604 * Since the attributes are before the file handle
2605 * (sigh), we must skip over the attributes and then
2606 * come back and get them.
2607 */
2608 attrflag = fxdr_unsigned(int, *tl);
2609 if (attrflag) {
2610 dpossav1 = dpos;
2611 mdsav1 = md;
2612 nfsm_adv(NFSX_V3FATTR);
2613 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2614 doit = fxdr_unsigned(int, *tl);
2615 /*
2616 * Skip loading the attrs for "..". There's a
2617 * race between loading the attrs here and
2618 * lookups that look for the directory currently
2619 * being read (in the parent). We try to acquire
2620 * the exclusive lock on ".." here, owning the
2621 * lock on the directory being read. Lookup will
2622 * hold the lock on ".." and try to acquire the
2623 * lock on the directory being read.
2624 *
2625 * There are other ways of fixing this, one would
2626 * be to do a trylock on the ".." vnode and skip
2627 * loading the attrs on ".." if it happens to be
2628 * locked by another process. But skipping the
2629 * attrload on ".." seems the easiest option.
2630 */
2631 if (strcmp(dp->d_name, "..") == 0) {
2632 doit = 0;
2633 /*
2634 * We've already skipped over the attrs,
2635 * skip over the filehandle. And store d_type
2636 * as VDIR.
2637 */
2638 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2639 i = fxdr_unsigned(int, *tl);
2640 nfsm_adv(nfsm_rndup(i));
2641 dp->d_type = IFTODT(VTTOIF(VDIR));
2642 }
2643 if (doit) {
2644 nfsm_getfh(fhp, fhsize, 1);
2645 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2646 VREF(vp);
2647 newvp = vp;
2648 np = dnp;
2649 } else {
2650 error = nfs_nget(vp->v_mount, fhp,
2651 fhsize, &np, LK_EXCLUSIVE);
2652 if (error)
2653 doit = 0;
2654 else
2655 newvp = NFSTOV(np);
2656 }
2657 }
2658 if (doit && bigenough) {
2659 dpossav2 = dpos;
2660 dpos = dpossav1;
2661 mdsav2 = md;
2662 md = mdsav1;
2663 nfsm_loadattr(newvp, &vattr);
2664 dpos = dpossav2;
2665 md = mdsav2;
2666 dp->d_type = IFTODT(VTTOIF(vattr.va_type));
2667 ndp->ni_vp = newvp;
2668 if (newvp->v_type != VDIR || dattrflag != 0)
2669 cache_enter_time(ndp->ni_dvp, ndp->ni_vp,
2670 cnp, &vattr.va_ctime,
2671 newvp->v_type != VDIR ? NULL :
2672 &dvattr.va_ctime);
2673 }
2674 } else {
2675 /* Just skip over the file handle */
2676 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2677 i = fxdr_unsigned(int, *tl);
2678 if (i) {
2679 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2680 fhsize = fxdr_unsigned(int, *tl);
2681 nfsm_adv(nfsm_rndup(fhsize));
2682 }
2683 }
2684 if (newvp != NULLVP) {
2685 if (newvp == vp)
2686 vrele(newvp);
2687 else
2688 vput(newvp);
2689 newvp = NULLVP;
2690 }
2691 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2692 more_dirs = fxdr_unsigned(int, *tl);
2693 }
2694 /*
2695 * If at end of rpc data, get the eof boolean
2696 */
2697 if (!more_dirs) {
2698 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2699 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2700 }
2701 m_freem(mrep);
2702 }
2703 /*
2704 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2705 * by increasing d_reclen for the last record.
2706 */
2707 if (blksiz > 0) {
2708 left = DIRBLKSIZ - blksiz;
2709 dp->d_reclen += left;
2710 uiop->uio_iov->iov_base =
2711 (char *)uiop->uio_iov->iov_base + left;
2712 uiop->uio_iov->iov_len -= left;
2713 uiop->uio_offset += left;
2714 uiop->uio_resid -= left;
2715 }
2716
2717 /*
2718 * We are now either at the end of the directory or have filled the
2719 * block.
2720 */
2721 if (bigenough)
2722 dnp->n_direofoffset = uiop->uio_offset;
2723 else {
2724 if (uiop->uio_resid > 0)
2725 nfs_printf("EEK! readdirplusrpc resid > 0\n");
2726 nfs_dircookie_lock(dnp);
2727 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2728 *cookiep = cookie;
2729 nfs_dircookie_unlock(dnp);
2730 }
2731 nfsmout:
2732 if (newvp != NULLVP) {
2733 if (newvp == vp)
2734 vrele(newvp);
2735 else
2736 vput(newvp);
2737 newvp = NULLVP;
2738 }
2739 return (error);
2740 }
2741
2742 /*
2743 * Silly rename. To make the NFS filesystem that is stateless look a little
2744 * more like the "ufs" a remove of an active vnode is translated to a rename
2745 * to a funny looking filename that is removed by nfs_inactive on the
2746 * nfsnode. There is the potential for another process on a different client
2747 * to create the same funny name between the nfs_lookitup() fails and the
2748 * nfs_rename() completes, but...
2749 */
2750 static int
2751 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2752 {
2753 struct sillyrename *sp;
2754 struct nfsnode *np;
2755 int error;
2756 short pid;
2757 unsigned int lticks;
2758
2759 cache_purge(dvp);
2760 np = VTONFS(vp);
2761 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2762 sp = malloc(sizeof (struct sillyrename),
2763 M_NFSREQ, M_WAITOK);
2764 sp->s_cred = crhold(cnp->cn_cred);
2765 sp->s_dvp = dvp;
2766 sp->s_removeit = nfs_removeit;
2767 VREF(dvp);
2768
2769 /*
2770 * Fudge together a funny name.
2771 * Changing the format of the funny name to accomodate more
2772 * sillynames per directory.
2773 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2774 * CPU ticks since boot.
2775 */
2776 pid = cnp->cn_thread->td_proc->p_pid;
2777 lticks = (unsigned int)ticks;
2778 for ( ; ; ) {
2779 sp->s_namlen = sprintf(sp->s_name,
2780 ".nfs.%08x.%04x4.4", lticks,
2781 pid);
2782 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2783 cnp->cn_thread, NULL))
2784 break;
2785 lticks++;
2786 }
2787 error = nfs_renameit(dvp, cnp, sp);
2788 if (error)
2789 goto bad;
2790 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2791 cnp->cn_thread, &np);
2792 np->n_sillyrename = sp;
2793 return (0);
2794 bad:
2795 vrele(sp->s_dvp);
2796 crfree(sp->s_cred);
2797 free((caddr_t)sp, M_NFSREQ);
2798 return (error);
2799 }
2800
2801 /*
2802 * Look up a file name and optionally either update the file handle or
2803 * allocate an nfsnode, depending on the value of npp.
2804 * npp == NULL --> just do the lookup
2805 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2806 * handled too
2807 * *npp != NULL --> update the file handle in the vnode
2808 */
2809 static int
2810 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2811 struct thread *td, struct nfsnode **npp)
2812 {
2813 struct vnode *newvp = NULL;
2814 struct nfsnode *np, *dnp = VTONFS(dvp);
2815 caddr_t bpos, dpos;
2816 int error = 0, fhlen, attrflag;
2817 struct mbuf *mreq, *mrep, *md, *mb;
2818 nfsfh_t *nfhp;
2819 int v3 = NFS_ISV3(dvp);
2820
2821 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2822 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len),
2823 M_WAITOK, MT_DATA, 0);
2824 mb = mreq;
2825 bpos = mtod(mb, caddr_t);
2826 nfsm_fhtom(dvp, v3);
2827 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2828 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2829 if (npp && !error) {
2830 nfsm_getfh(nfhp, fhlen, v3);
2831 if (*npp) {
2832 np = *npp;
2833 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2834 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2835 np->n_fhp = &np->n_fh;
2836 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2837 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2838 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2839 np->n_fhsize = fhlen;
2840 newvp = NFSTOV(np);
2841 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2842 VREF(dvp);
2843 newvp = dvp;
2844 } else {
2845 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2846 if (error) {
2847 m_freem(mrep);
2848 return (error);
2849 }
2850 newvp = NFSTOV(np);
2851 }
2852 if (v3) {
2853 nfsm_postop_attr(newvp, attrflag);
2854 if (!attrflag && *npp == NULL) {
2855 m_freem(mrep);
2856 if (newvp == dvp)
2857 vrele(newvp);
2858 else
2859 vput(newvp);
2860 return (ENOENT);
2861 }
2862 } else
2863 nfsm_loadattr(newvp, NULL);
2864 }
2865 m_freem(mrep);
2866 nfsmout:
2867 if (npp && *npp == NULL) {
2868 if (error) {
2869 if (newvp) {
2870 if (newvp == dvp)
2871 vrele(newvp);
2872 else
2873 vput(newvp);
2874 }
2875 } else
2876 *npp = np;
2877 }
2878 return (error);
2879 }
2880
2881 /*
2882 * Nfs Version 3 commit rpc
2883 */
2884 int
2885 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2886 struct thread *td)
2887 {
2888 u_int32_t *tl;
2889 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2890 caddr_t bpos, dpos;
2891 int error = 0, wccflag = NFSV3_WCCRATTR;
2892 struct mbuf *mreq, *mrep, *md, *mb;
2893
2894 mtx_lock(&nmp->nm_mtx);
2895 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2896 mtx_unlock(&nmp->nm_mtx);
2897 return (0);
2898 }
2899 mtx_unlock(&nmp->nm_mtx);
2900 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2901 mreq = m_get2(NFSX_FH(1), M_WAITOK, MT_DATA, 0);
2902 mb = mreq;
2903 bpos = mtod(mb, caddr_t);
2904 nfsm_fhtom(vp, 1);
2905 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2906 txdr_hyper(offset, tl);
2907 tl += 2;
2908 *tl = txdr_unsigned(cnt);
2909 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2910 nfsm_wcc_data(vp, wccflag);
2911 if (!error) {
2912 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2913 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2914 NFSX_V3WRITEVERF)) {
2915 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2916 NFSX_V3WRITEVERF);
2917 error = NFSERR_STALEWRITEVERF;
2918 }
2919 }
2920 m_freem(mrep);
2921 nfsmout:
2922 return (error);
2923 }
2924
2925 /*
2926 * Strategy routine.
2927 * For async requests when nfsiod(s) are running, queue the request by
2928 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2929 * request.
2930 */
2931 static int
2932 nfs_strategy(struct vop_strategy_args *ap)
2933 {
2934 struct buf *bp = ap->a_bp;
2935 struct ucred *cr;
2936
2937 KASSERT(!(bp->b_flags & B_DONE),
2938 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2939 BUF_ASSERT_HELD(bp);
2940
2941 if (bp->b_iocmd == BIO_READ)
2942 cr = bp->b_rcred;
2943 else
2944 cr = bp->b_wcred;
2945
2946 /*
2947 * If the op is asynchronous and an i/o daemon is waiting
2948 * queue the request, wake it up and wait for completion
2949 * otherwise just do it ourselves.
2950 */
2951 if ((bp->b_flags & B_ASYNC) == 0 ||
2952 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2953 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2954 return (0);
2955 }
2956
2957 /*
2958 * fsync vnode op. Just call nfs_flush() with commit == 1.
2959 */
2960 /* ARGSUSED */
2961 static int
2962 nfs_fsync(struct vop_fsync_args *ap)
2963 {
2964
2965 return (nfs_flush(ap->a_vp, ap->a_waitfor, 1));
2966 }
2967
2968 /*
2969 * Flush all the blocks associated with a vnode.
2970 * Walk through the buffer pool and push any dirty pages
2971 * associated with the vnode.
2972 */
2973 static int
2974 nfs_flush(struct vnode *vp, int waitfor, int commit)
2975 {
2976 struct nfsnode *np = VTONFS(vp);
2977 struct buf *bp;
2978 int i;
2979 struct buf *nbp;
2980 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2981 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2982 int passone = 1;
2983 u_quad_t off, endoff, toff;
2984 struct ucred* wcred = NULL;
2985 struct buf **bvec = NULL;
2986 struct bufobj *bo;
2987 struct thread *td = curthread;
2988 #ifndef NFS_COMMITBVECSIZ
2989 #define NFS_COMMITBVECSIZ 20
2990 #endif
2991 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2992 int bvecsize = 0, bveccount;
2993
2994 if (nmp->nm_flag & NFSMNT_INT)
2995 slpflag = PCATCH;
2996 if (!commit)
2997 passone = 0;
2998 bo = &vp->v_bufobj;
2999 /*
3000 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3001 * server, but has not been committed to stable storage on the server
3002 * yet. On the first pass, the byte range is worked out and the commit
3003 * rpc is done. On the second pass, nfs_writebp() is called to do the
3004 * job.
3005 */
3006 again:
3007 off = (u_quad_t)-1;
3008 endoff = 0;
3009 bvecpos = 0;
3010 if (NFS_ISV3(vp) && commit) {
3011 if (bvec != NULL && bvec != bvec_on_stack)
3012 free(bvec, M_TEMP);
3013 /*
3014 * Count up how many buffers waiting for a commit.
3015 */
3016 bveccount = 0;
3017 BO_LOCK(bo);
3018 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3019 if (!BUF_ISLOCKED(bp) &&
3020 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3021 == (B_DELWRI | B_NEEDCOMMIT))
3022 bveccount++;
3023 }
3024 /*
3025 * Allocate space to remember the list of bufs to commit. It is
3026 * important to use M_NOWAIT here to avoid a race with nfs_write.
3027 * If we can't get memory (for whatever reason), we will end up
3028 * committing the buffers one-by-one in the loop below.
3029 */
3030 if (bveccount > NFS_COMMITBVECSIZ) {
3031 /*
3032 * Release the vnode interlock to avoid a lock
3033 * order reversal.
3034 */
3035 BO_UNLOCK(bo);
3036 bvec = (struct buf **)
3037 malloc(bveccount * sizeof(struct buf *),
3038 M_TEMP, M_NOWAIT);
3039 BO_LOCK(bo);
3040 if (bvec == NULL) {
3041 bvec = bvec_on_stack;
3042 bvecsize = NFS_COMMITBVECSIZ;
3043 } else
3044 bvecsize = bveccount;
3045 } else {
3046 bvec = bvec_on_stack;
3047 bvecsize = NFS_COMMITBVECSIZ;
3048 }
3049 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3050 if (bvecpos >= bvecsize)
3051 break;
3052 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3053 nbp = TAILQ_NEXT(bp, b_bobufs);
3054 continue;
3055 }
3056 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3057 (B_DELWRI | B_NEEDCOMMIT)) {
3058 BUF_UNLOCK(bp);
3059 nbp = TAILQ_NEXT(bp, b_bobufs);
3060 continue;
3061 }
3062 BO_UNLOCK(bo);
3063 bremfree(bp);
3064 /*
3065 * Work out if all buffers are using the same cred
3066 * so we can deal with them all with one commit.
3067 *
3068 * NOTE: we are not clearing B_DONE here, so we have
3069 * to do it later on in this routine if we intend to
3070 * initiate I/O on the bp.
3071 *
3072 * Note: to avoid loopback deadlocks, we do not
3073 * assign b_runningbufspace.
3074 */
3075 if (wcred == NULL)
3076 wcred = bp->b_wcred;
3077 else if (wcred != bp->b_wcred)
3078 wcred = NOCRED;
3079 vfs_busy_pages(bp, 1);
3080
3081 BO_LOCK(bo);
3082 /*
3083 * bp is protected by being locked, but nbp is not
3084 * and vfs_busy_pages() may sleep. We have to
3085 * recalculate nbp.
3086 */
3087 nbp = TAILQ_NEXT(bp, b_bobufs);
3088
3089 /*
3090 * A list of these buffers is kept so that the
3091 * second loop knows which buffers have actually
3092 * been committed. This is necessary, since there
3093 * may be a race between the commit rpc and new
3094 * uncommitted writes on the file.
3095 */
3096 bvec[bvecpos++] = bp;
3097 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3098 bp->b_dirtyoff;
3099 if (toff < off)
3100 off = toff;
3101 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3102 if (toff > endoff)
3103 endoff = toff;
3104 }
3105 BO_UNLOCK(bo);
3106 }
3107 if (bvecpos > 0) {
3108 /*
3109 * Commit data on the server, as required.
3110 * If all bufs are using the same wcred, then use that with
3111 * one call for all of them, otherwise commit each one
3112 * separately.
3113 */
3114 if (wcred != NOCRED)
3115 retv = nfs_commit(vp, off, (int)(endoff - off),
3116 wcred, td);
3117 else {
3118 retv = 0;
3119 for (i = 0; i < bvecpos; i++) {
3120 off_t off, size;
3121 bp = bvec[i];
3122 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3123 bp->b_dirtyoff;
3124 size = (u_quad_t)(bp->b_dirtyend
3125 - bp->b_dirtyoff);
3126 retv = nfs_commit(vp, off, (int)size,
3127 bp->b_wcred, td);
3128 if (retv) break;
3129 }
3130 }
3131
3132 if (retv == NFSERR_STALEWRITEVERF)
3133 nfs_clearcommit(vp->v_mount);
3134
3135 /*
3136 * Now, either mark the blocks I/O done or mark the
3137 * blocks dirty, depending on whether the commit
3138 * succeeded.
3139 */
3140 for (i = 0; i < bvecpos; i++) {
3141 bp = bvec[i];
3142 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3143 if (retv) {
3144 /*
3145 * Error, leave B_DELWRI intact
3146 */
3147 vfs_unbusy_pages(bp);
3148 brelse(bp);
3149 } else {
3150 /*
3151 * Success, remove B_DELWRI ( bundirty() ).
3152 *
3153 * b_dirtyoff/b_dirtyend seem to be NFS
3154 * specific. We should probably move that
3155 * into bundirty(). XXX
3156 */
3157 bufobj_wref(bo);
3158 bp->b_flags |= B_ASYNC;
3159 bundirty(bp);
3160 bp->b_flags &= ~B_DONE;
3161 bp->b_ioflags &= ~BIO_ERROR;
3162 bp->b_dirtyoff = bp->b_dirtyend = 0;
3163 bufdone(bp);
3164 }
3165 }
3166 }
3167
3168 /*
3169 * Start/do any write(s) that are required.
3170 */
3171 loop:
3172 BO_LOCK(bo);
3173 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3174 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3175 if (waitfor != MNT_WAIT || passone)
3176 continue;
3177
3178 error = BUF_TIMELOCK(bp,
3179 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3180 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3181 if (error == 0) {
3182 BUF_UNLOCK(bp);
3183 goto loop;
3184 }
3185 if (error == ENOLCK) {
3186 error = 0;
3187 goto loop;
3188 }
3189 if (nfs_sigintr(nmp, td)) {
3190 error = EINTR;
3191 goto done;
3192 }
3193 if (slpflag == PCATCH) {
3194 slpflag = 0;
3195 slptimeo = 2 * hz;
3196 }
3197 goto loop;
3198 }
3199 if ((bp->b_flags & B_DELWRI) == 0)
3200 panic("nfs_fsync: not dirty");
3201 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3202 BUF_UNLOCK(bp);
3203 continue;
3204 }
3205 BO_UNLOCK(bo);
3206 bremfree(bp);
3207 if (passone || !commit)
3208 bp->b_flags |= B_ASYNC;
3209 else
3210 bp->b_flags |= B_ASYNC;
3211 bwrite(bp);
3212 if (nfs_sigintr(nmp, td)) {
3213 error = EINTR;
3214 goto done;
3215 }
3216 goto loop;
3217 }
3218 if (passone) {
3219 passone = 0;
3220 BO_UNLOCK(bo);
3221 goto again;
3222 }
3223 if (waitfor == MNT_WAIT) {
3224 while (bo->bo_numoutput) {
3225 error = bufobj_wwait(bo, slpflag, slptimeo);
3226 if (error) {
3227 BO_UNLOCK(bo);
3228 error = nfs_sigintr(nmp, td);
3229 if (error)
3230 goto done;
3231 if (slpflag == PCATCH) {
3232 slpflag = 0;
3233 slptimeo = 2 * hz;
3234 }
3235 BO_LOCK(bo);
3236 }
3237 }
3238 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3239 BO_UNLOCK(bo);
3240 goto loop;
3241 }
3242 /*
3243 * Wait for all the async IO requests to drain
3244 */
3245 BO_UNLOCK(bo);
3246 mtx_lock(&np->n_mtx);
3247 while (np->n_directio_asyncwr > 0) {
3248 np->n_flag |= NFSYNCWAIT;
3249 error = nfs_msleep(td, (caddr_t)&np->n_directio_asyncwr,
3250 &np->n_mtx, slpflag | (PRIBIO + 1),
3251 "nfsfsync", 0);
3252 if (error) {
3253 if (nfs_sigintr(nmp, td)) {
3254 mtx_unlock(&np->n_mtx);
3255 error = EINTR;
3256 goto done;
3257 }
3258 }
3259 }
3260 mtx_unlock(&np->n_mtx);
3261 } else
3262 BO_UNLOCK(bo);
3263 mtx_lock(&np->n_mtx);
3264 if (np->n_flag & NWRITEERR) {
3265 error = np->n_error;
3266 np->n_flag &= ~NWRITEERR;
3267 }
3268 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3269 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3270 np->n_flag &= ~NMODIFIED;
3271 mtx_unlock(&np->n_mtx);
3272 done:
3273 if (bvec != NULL && bvec != bvec_on_stack)
3274 free(bvec, M_TEMP);
3275 return (error);
3276 }
3277
3278 /*
3279 * NFS advisory byte-level locks.
3280 */
3281 static int
3282 nfs_advlock(struct vop_advlock_args *ap)
3283 {
3284 struct vnode *vp = ap->a_vp;
3285 u_quad_t size;
3286 int error;
3287
3288 error = vn_lock(vp, LK_SHARED);
3289 if (error)
3290 return (error);
3291 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3292 size = VTONFS(vp)->n_size;
3293 VOP_UNLOCK(vp, 0);
3294 error = lf_advlock(ap, &(vp->v_lockf), size);
3295 } else {
3296 if (nfs_advlock_p)
3297 error = nfs_advlock_p(ap);
3298 else
3299 error = ENOLCK;
3300 }
3301
3302 return (error);
3303 }
3304
3305 /*
3306 * NFS advisory byte-level locks.
3307 */
3308 static int
3309 nfs_advlockasync(struct vop_advlockasync_args *ap)
3310 {
3311 struct vnode *vp = ap->a_vp;
3312 u_quad_t size;
3313 int error;
3314
3315 error = vn_lock(vp, LK_SHARED);
3316 if (error)
3317 return (error);
3318 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3319 size = VTONFS(vp)->n_size;
3320 VOP_UNLOCK(vp, 0);
3321 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3322 } else {
3323 VOP_UNLOCK(vp, 0);
3324 error = EOPNOTSUPP;
3325 }
3326 return (error);
3327 }
3328
3329 /*
3330 * Print out the contents of an nfsnode.
3331 */
3332 static int
3333 nfs_print(struct vop_print_args *ap)
3334 {
3335 struct vnode *vp = ap->a_vp;
3336 struct nfsnode *np = VTONFS(vp);
3337
3338 nfs_printf("\tfileid %ld fsid 0x%x",
3339 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3340 if (vp->v_type == VFIFO)
3341 fifo_printinfo(vp);
3342 printf("\n");
3343 return (0);
3344 }
3345
3346 /*
3347 * This is the "real" nfs::bwrite(struct buf*).
3348 * We set B_CACHE if this is a VMIO buffer.
3349 */
3350 int
3351 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
3352 {
3353 int s;
3354 int oldflags = bp->b_flags;
3355 #if 0
3356 int retv = 1;
3357 off_t off;
3358 #endif
3359
3360 BUF_ASSERT_HELD(bp);
3361
3362 if (bp->b_flags & B_INVAL) {
3363 brelse(bp);
3364 return(0);
3365 }
3366
3367 bp->b_flags |= B_CACHE;
3368
3369 /*
3370 * Undirty the bp. We will redirty it later if the I/O fails.
3371 */
3372
3373 s = splbio();
3374 bundirty(bp);
3375 bp->b_flags &= ~B_DONE;
3376 bp->b_ioflags &= ~BIO_ERROR;
3377 bp->b_iocmd = BIO_WRITE;
3378
3379 bufobj_wref(bp->b_bufobj);
3380 curthread->td_ru.ru_oublock++;
3381 splx(s);
3382
3383 /*
3384 * Note: to avoid loopback deadlocks, we do not
3385 * assign b_runningbufspace.
3386 */
3387 vfs_busy_pages(bp, 1);
3388
3389 BUF_KERNPROC(bp);
3390 bp->b_iooffset = dbtob(bp->b_blkno);
3391 bstrategy(bp);
3392
3393 if( (oldflags & B_ASYNC) == 0) {
3394 int rtval = bufwait(bp);
3395
3396 if (oldflags & B_DELWRI) {
3397 s = splbio();
3398 reassignbuf(bp);
3399 splx(s);
3400 }
3401 brelse(bp);
3402 return (rtval);
3403 }
3404
3405 return (0);
3406 }
3407
3408 /*
3409 * nfs special file access vnode op.
3410 * Essentially just get vattr and then imitate iaccess() since the device is
3411 * local to the client.
3412 */
3413 static int
3414 nfsspec_access(struct vop_access_args *ap)
3415 {
3416 struct vattr *vap;
3417 struct ucred *cred = ap->a_cred;
3418 struct vnode *vp = ap->a_vp;
3419 accmode_t accmode = ap->a_accmode;
3420 struct vattr vattr;
3421 int error;
3422
3423 /*
3424 * Disallow write attempts on filesystems mounted read-only;
3425 * unless the file is a socket, fifo, or a block or character
3426 * device resident on the filesystem.
3427 */
3428 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3429 switch (vp->v_type) {
3430 case VREG:
3431 case VDIR:
3432 case VLNK:
3433 return (EROFS);
3434 default:
3435 break;
3436 }
3437 }
3438 vap = &vattr;
3439 error = VOP_GETATTR(vp, vap, cred);
3440 if (error)
3441 goto out;
3442 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3443 accmode, cred, NULL);
3444 out:
3445 return error;
3446 }
3447
3448 /*
3449 * Read wrapper for fifos.
3450 */
3451 static int
3452 nfsfifo_read(struct vop_read_args *ap)
3453 {
3454 struct nfsnode *np = VTONFS(ap->a_vp);
3455 int error;
3456
3457 /*
3458 * Set access flag.
3459 */
3460 mtx_lock(&np->n_mtx);
3461 np->n_flag |= NACC;
3462 vfs_timestamp(&np->n_atim);
3463 mtx_unlock(&np->n_mtx);
3464 error = fifo_specops.vop_read(ap);
3465 return error;
3466 }
3467
3468 /*
3469 * Write wrapper for fifos.
3470 */
3471 static int
3472 nfsfifo_write(struct vop_write_args *ap)
3473 {
3474 struct nfsnode *np = VTONFS(ap->a_vp);
3475
3476 /*
3477 * Set update flag.
3478 */
3479 mtx_lock(&np->n_mtx);
3480 np->n_flag |= NUPD;
3481 vfs_timestamp(&np->n_mtim);
3482 mtx_unlock(&np->n_mtx);
3483 return(fifo_specops.vop_write(ap));
3484 }
3485
3486 /*
3487 * Close wrapper for fifos.
3488 *
3489 * Update the times on the nfsnode then do fifo close.
3490 */
3491 static int
3492 nfsfifo_close(struct vop_close_args *ap)
3493 {
3494 struct vnode *vp = ap->a_vp;
3495 struct nfsnode *np = VTONFS(vp);
3496 struct vattr vattr;
3497 struct timespec ts;
3498
3499 mtx_lock(&np->n_mtx);
3500 if (np->n_flag & (NACC | NUPD)) {
3501 vfs_timestamp(&ts);
3502 if (np->n_flag & NACC)
3503 np->n_atim = ts;
3504 if (np->n_flag & NUPD)
3505 np->n_mtim = ts;
3506 np->n_flag |= NCHG;
3507 if (vrefcnt(vp) == 1 &&
3508 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3509 VATTR_NULL(&vattr);
3510 if (np->n_flag & NACC)
3511 vattr.va_atime = np->n_atim;
3512 if (np->n_flag & NUPD)
3513 vattr.va_mtime = np->n_mtim;
3514 mtx_unlock(&np->n_mtx);
3515 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3516 goto out;
3517 }
3518 }
3519 mtx_unlock(&np->n_mtx);
3520 out:
3521 return (fifo_specops.vop_close(ap));
3522 }
3523
3524 /*
3525 * Just call nfs_writebp() with the force argument set to 1.
3526 *
3527 * NOTE: B_DONE may or may not be set in a_bp on call.
3528 */
3529 static int
3530 nfs_bwrite(struct buf *bp)
3531 {
3532
3533 return (nfs_writebp(bp, 1, curthread));
3534 }
3535
3536 struct buf_ops buf_ops_nfs = {
3537 .bop_name = "buf_ops_nfs",
3538 .bop_write = nfs_bwrite,
3539 .bop_strategy = bufstrategy,
3540 .bop_sync = bufsync,
3541 .bop_bdflush = bufbdflush,
3542 };
Cache object: 63ea07a313ee51dad59e7348257215bd
|