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