2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from nfs_vnops.c 8.16 (Berkeley) 5/27/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * vnode op calls for Sun NFS version 2, 3 and 4
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/resourcevar.h>
51 #include <sys/mount.h>
54 #include <sys/extattr.h>
55 #include <sys/filio.h>
57 #include <sys/malloc.h>
59 #include <sys/namei.h>
60 #include <sys/socket.h>
61 #include <sys/vnode.h>
62 #include <sys/dirent.h>
63 #include <sys/fcntl.h>
64 #include <sys/lockf.h>
66 #include <sys/sysctl.h>
67 #include <sys/signalvar.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_object.h>
73 #include <fs/nfs/nfsport.h>
74 #include <fs/nfsclient/nfsnode.h>
75 #include <fs/nfsclient/nfsmount.h>
76 #include <fs/nfsclient/nfs.h>
77 #include <fs/nfsclient/nfs_kdtrace.h>
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
83 #include <nfs/nfs_lock.h>
86 #include <sys/dtrace_bsd.h>
88 dtrace_nfsclient_accesscache_flush_probe_func_t
89 dtrace_nfscl_accesscache_flush_done_probe;
90 uint32_t nfscl_accesscache_flush_done_id;
92 dtrace_nfsclient_accesscache_get_probe_func_t
93 dtrace_nfscl_accesscache_get_hit_probe,
94 dtrace_nfscl_accesscache_get_miss_probe;
95 uint32_t nfscl_accesscache_get_hit_id;
96 uint32_t nfscl_accesscache_get_miss_id;
98 dtrace_nfsclient_accesscache_load_probe_func_t
99 dtrace_nfscl_accesscache_load_done_probe;
100 uint32_t nfscl_accesscache_load_done_id;
101 #endif /* !KDTRACE_HOOKS */
107 extern struct nfsstatsv1 nfsstatsv1;
108 extern int nfsrv_useacl;
109 extern int nfscl_debuglevel;
110 MALLOC_DECLARE(M_NEWNFSREQ);
112 static vop_read_t nfsfifo_read;
113 static vop_write_t nfsfifo_write;
114 static vop_close_t nfsfifo_close;
115 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
117 static vop_lookup_t nfs_lookup;
118 static vop_create_t nfs_create;
119 static vop_mknod_t nfs_mknod;
120 static vop_open_t nfs_open;
121 static vop_pathconf_t nfs_pathconf;
122 static vop_close_t nfs_close;
123 static vop_access_t nfs_access;
124 static vop_getattr_t nfs_getattr;
125 static vop_setattr_t nfs_setattr;
126 static vop_read_t nfs_read;
127 static vop_fsync_t nfs_fsync;
128 static vop_remove_t nfs_remove;
129 static vop_link_t nfs_link;
130 static vop_rename_t nfs_rename;
131 static vop_mkdir_t nfs_mkdir;
132 static vop_rmdir_t nfs_rmdir;
133 static vop_symlink_t nfs_symlink;
134 static vop_readdir_t nfs_readdir;
135 static vop_strategy_t nfs_strategy;
136 static int nfs_lookitup(struct vnode *, char *, int,
137 struct ucred *, struct thread *, struct nfsnode **);
138 static int nfs_sillyrename(struct vnode *, struct vnode *,
139 struct componentname *);
140 static vop_access_t nfsspec_access;
141 static vop_readlink_t nfs_readlink;
142 static vop_print_t nfs_print;
143 static vop_advlock_t nfs_advlock;
144 static vop_advlockasync_t nfs_advlockasync;
145 static vop_getacl_t nfs_getacl;
146 static vop_setacl_t nfs_setacl;
147 static vop_advise_t nfs_advise;
148 static vop_allocate_t nfs_allocate;
149 static vop_deallocate_t nfs_deallocate;
150 static vop_copy_file_range_t nfs_copy_file_range;
151 static vop_ioctl_t nfs_ioctl;
152 static vop_getextattr_t nfs_getextattr;
153 static vop_setextattr_t nfs_setextattr;
154 static vop_listextattr_t nfs_listextattr;
155 static vop_deleteextattr_t nfs_deleteextattr;
156 static vop_lock1_t nfs_lock;
159 * Global vfs data structures for nfs
162 static struct vop_vector newnfs_vnodeops_nosig = {
163 .vop_default = &default_vnodeops,
164 .vop_access = nfs_access,
165 .vop_advlock = nfs_advlock,
166 .vop_advlockasync = nfs_advlockasync,
167 .vop_close = nfs_close,
168 .vop_create = nfs_create,
169 .vop_fsync = nfs_fsync,
170 .vop_getattr = nfs_getattr,
171 .vop_getpages = ncl_getpages,
172 .vop_putpages = ncl_putpages,
173 .vop_inactive = ncl_inactive,
174 .vop_link = nfs_link,
175 .vop_lock1 = nfs_lock,
176 .vop_lookup = nfs_lookup,
177 .vop_mkdir = nfs_mkdir,
178 .vop_mknod = nfs_mknod,
179 .vop_open = nfs_open,
180 .vop_pathconf = nfs_pathconf,
181 .vop_print = nfs_print,
182 .vop_read = nfs_read,
183 .vop_readdir = nfs_readdir,
184 .vop_readlink = nfs_readlink,
185 .vop_reclaim = ncl_reclaim,
186 .vop_remove = nfs_remove,
187 .vop_rename = nfs_rename,
188 .vop_rmdir = nfs_rmdir,
189 .vop_setattr = nfs_setattr,
190 .vop_strategy = nfs_strategy,
191 .vop_symlink = nfs_symlink,
192 .vop_write = ncl_write,
193 .vop_getacl = nfs_getacl,
194 .vop_setacl = nfs_setacl,
195 .vop_advise = nfs_advise,
196 .vop_allocate = nfs_allocate,
197 .vop_deallocate = nfs_deallocate,
198 .vop_copy_file_range = nfs_copy_file_range,
199 .vop_ioctl = nfs_ioctl,
200 .vop_getextattr = nfs_getextattr,
201 .vop_setextattr = nfs_setextattr,
202 .vop_listextattr = nfs_listextattr,
203 .vop_deleteextattr = nfs_deleteextattr,
205 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops_nosig);
208 nfs_vnodeops_bypass(struct vop_generic_args *a)
211 return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
214 struct vop_vector newnfs_vnodeops = {
215 .vop_default = &default_vnodeops,
216 .vop_bypass = nfs_vnodeops_bypass,
218 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops);
220 static struct vop_vector newnfs_fifoops_nosig = {
221 .vop_default = &fifo_specops,
222 .vop_access = nfsspec_access,
223 .vop_close = nfsfifo_close,
224 .vop_fsync = nfs_fsync,
225 .vop_getattr = nfs_getattr,
226 .vop_inactive = ncl_inactive,
227 .vop_pathconf = nfs_pathconf,
228 .vop_print = nfs_print,
229 .vop_read = nfsfifo_read,
230 .vop_reclaim = ncl_reclaim,
231 .vop_setattr = nfs_setattr,
232 .vop_write = nfsfifo_write,
234 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops_nosig);
237 nfs_fifoops_bypass(struct vop_generic_args *a)
240 return (vop_sigdefer(&newnfs_fifoops_nosig, a));
243 struct vop_vector newnfs_fifoops = {
244 .vop_default = &default_vnodeops,
245 .vop_bypass = nfs_fifoops_bypass,
247 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops);
249 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
250 struct componentname *cnp, struct vattr *vap);
251 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
252 int namelen, struct ucred *cred, struct thread *td);
253 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
254 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
255 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
256 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
257 struct componentname *scnp, struct sillyrename *sp);
262 SYSCTL_DECL(_vfs_nfs);
264 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
265 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
266 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
268 static int nfs_prime_access_cache = 0;
269 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
270 &nfs_prime_access_cache, 0,
271 "Prime NFS ACCESS cache when fetching attributes");
273 static int newnfs_commit_on_close = 0;
274 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
275 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
277 static int nfs_clean_pages_on_close = 1;
278 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
279 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
281 int newnfs_directio_enable = 0;
282 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
283 &newnfs_directio_enable, 0, "Enable NFS directio");
285 int nfs_keep_dirty_on_error;
286 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
287 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
290 * This sysctl allows other processes to mmap a file that has been opened
291 * O_DIRECT by a process. In general, having processes mmap the file while
292 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
293 * this by default to prevent DoS attacks - to prevent a malicious user from
294 * opening up files O_DIRECT preventing other users from mmap'ing these
295 * files. "Protected" environments where stricter consistency guarantees are
296 * required can disable this knob. The process that opened the file O_DIRECT
297 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
300 int newnfs_directio_allow_mmap = 1;
301 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
302 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
304 static uint64_t nfs_maxalloclen = 64 * 1024 * 1024;
305 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxalloclen, CTLFLAG_RW,
306 &nfs_maxalloclen, 0, "NFS max allocate/deallocate length");
308 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
309 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
310 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
314 * The list of locks after the description of the lock is the ordering
315 * of other locks acquired with the lock held.
316 * np->n_mtx : Protects the fields in the nfsnode.
318 VI_MTX (acquired indirectly)
319 * nmp->nm_mtx : Protects the fields in the nfsmount.
321 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
322 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
325 * rep->r_mtx : Protects the fields in an nfsreq.
329 nfs_lock(struct vop_lock1_args *ap)
338 lktype = ap->a_flags & LK_TYPE_MASK;
339 error = VOP_LOCK1_APV(&default_vnodeops, ap);
340 if (error != 0 || vp->v_op != &newnfs_vnodeops)
346 if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
347 lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
348 lktype != LK_TRYUPGRADE)) {
352 onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
353 (ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
354 (lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
355 if (onfault && vp->v_vnlock->lk_recurse == 0) {
357 * Force retry in vm_fault(), to make the lock request
358 * sleepable, which allows us to piggy-back the
359 * sleepable call to vnode_pager_setsize().
365 if ((ap->a_flags & LK_NOWAIT) != 0 ||
366 (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
370 if (lktype == LK_SHARED) {
373 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
374 ap->a_flags |= LK_EXCLUSIVE;
375 error = VOP_LOCK1_APV(&default_vnodeops, ap);
376 if (error != 0 || vp->v_op != &newnfs_vnodeops)
378 if (vp->v_data == NULL)
380 MPASS(vp->v_data == np);
382 if ((np->n_flag & NVNSETSZSKIP) == 0) {
387 np->n_flag &= ~NVNSETSZSKIP;
390 vnode_pager_setsize(vp, nsize);
392 if (lktype == LK_SHARED) {
393 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
394 ap->a_flags |= LK_DOWNGRADE;
395 (void)VOP_LOCK1_APV(&default_vnodeops, ap);
401 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
402 struct ucred *cred, u_int32_t *retmode)
404 int error = 0, attrflag, i, lrupos;
406 struct nfsnode *np = VTONFS(vp);
407 struct nfsvattr nfsva;
409 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
412 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
416 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
417 if (np->n_accesscache[i].uid == cred->cr_uid) {
418 np->n_accesscache[i].mode = rmode;
419 np->n_accesscache[i].stamp = time_second;
422 if (i > 0 && np->n_accesscache[i].stamp <
423 np->n_accesscache[lrupos].stamp)
426 if (i == NFS_ACCESSCACHESIZE) {
427 np->n_accesscache[lrupos].uid = cred->cr_uid;
428 np->n_accesscache[lrupos].mode = rmode;
429 np->n_accesscache[lrupos].stamp = time_second;
434 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
435 } else if (NFS_ISV4(vp)) {
436 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
440 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
447 * nfs access vnode op.
448 * For nfs version 2, just return ok. File accesses may fail later.
449 * For nfs version 3, use the access rpc to check accessibility. If file modes
450 * are changed on the server, accesses might still fail later.
453 nfs_access(struct vop_access_args *ap)
455 struct vnode *vp = ap->a_vp;
456 int error = 0, i, gotahit;
457 u_int32_t mode, wmode, rmode;
458 int v34 = NFS_ISV34(vp);
459 struct nfsnode *np = VTONFS(vp);
462 * Disallow write attempts on filesystems mounted read-only;
463 * unless the file is a socket, fifo, or a block or character
464 * device resident on the filesystem.
466 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
467 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
468 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
469 switch (vp->v_type) {
479 * For nfs v3 or v4, check to see if we have done this recently, and if
480 * so return our cached result instead of making an ACCESS call.
481 * If not, do an access rpc, otherwise you are stuck emulating
482 * ufs_access() locally using the vattr. This may not be correct,
483 * since the server may apply other access criteria such as
484 * client uid-->server uid mapping that we do not know about.
487 if (ap->a_accmode & VREAD)
488 mode = NFSACCESS_READ;
491 if (vp->v_type != VDIR) {
492 if (ap->a_accmode & VWRITE)
493 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
494 if (ap->a_accmode & VAPPEND)
495 mode |= NFSACCESS_EXTEND;
496 if (ap->a_accmode & VEXEC)
497 mode |= NFSACCESS_EXECUTE;
498 if (ap->a_accmode & VDELETE)
499 mode |= NFSACCESS_DELETE;
501 if (ap->a_accmode & VWRITE)
502 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
503 if (ap->a_accmode & VAPPEND)
504 mode |= NFSACCESS_EXTEND;
505 if (ap->a_accmode & VEXEC)
506 mode |= NFSACCESS_LOOKUP;
507 if (ap->a_accmode & VDELETE)
508 mode |= NFSACCESS_DELETE;
509 if (ap->a_accmode & VDELETE_CHILD)
510 mode |= NFSACCESS_MODIFY;
512 /* XXX safety belt, only make blanket request if caching */
513 if (nfsaccess_cache_timeout > 0) {
514 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
515 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
516 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
522 * Does our cached result allow us to give a definite yes to
527 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
528 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
529 if (time_second < (np->n_accesscache[i].stamp
530 + nfsaccess_cache_timeout) &&
531 (np->n_accesscache[i].mode & mode) == mode) {
532 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
541 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
542 ap->a_cred->cr_uid, mode);
544 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
545 ap->a_cred->cr_uid, mode);
549 * Either a no, or a don't know. Go to the wire.
551 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
552 error = nfs34_access_otw(vp, wmode, ap->a_td,
555 (rmode & mode) != mode)
560 if ((error = nfsspec_access(ap)) != 0) {
564 * Attempt to prevent a mapped root from accessing a file
565 * which it shouldn't. We try to read a byte from the file
566 * if the user is root and the file is not zero length.
567 * After calling nfsspec_access, we should have the correct
571 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
572 && VTONFS(vp)->n_size > 0) {
580 auio.uio_iov = &aiov;
584 auio.uio_segflg = UIO_SYSSPACE;
585 auio.uio_rw = UIO_READ;
586 auio.uio_td = ap->a_td;
588 if (vp->v_type == VREG)
589 error = ncl_readrpc(vp, &auio, ap->a_cred);
590 else if (vp->v_type == VDIR) {
592 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
594 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
595 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
598 } else if (vp->v_type == VLNK)
599 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
610 * Check to see if the type is ok
611 * and that deletion is not in progress.
612 * For paged in text files, you will need to flush the page cache
613 * if consistency is lost.
617 nfs_open(struct vop_open_args *ap)
619 struct vnode *vp = ap->a_vp;
620 struct nfsnode *np = VTONFS(vp);
623 int fmode = ap->a_mode;
627 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
631 * For NFSv4, we need to do the Open Op before cache validation,
632 * so that we conform to RFC3530 Sec. 9.3.1.
635 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
637 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
644 * Now, if this Open will be doing reading, re-validate/flush the
645 * cache, so that Close/Open coherency is maintained.
648 if (np->n_flag & NMODIFIED) {
650 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
651 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
652 if (VN_IS_DOOMED(vp))
655 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
656 if (error == EINTR || error == EIO) {
658 (void) nfsrpc_close(vp, 0, ap->a_td);
663 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
664 if (vp->v_type == VDIR)
665 np->n_direofoffset = 0;
667 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
670 (void) nfsrpc_close(vp, 0, ap->a_td);
674 np->n_mtime = vattr.va_mtime;
676 np->n_change = vattr.va_filerev;
679 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
682 (void) nfsrpc_close(vp, 0, ap->a_td);
686 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
687 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
688 if (vp->v_type == VDIR)
689 np->n_direofoffset = 0;
691 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
692 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
693 if (VN_IS_DOOMED(vp))
696 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
697 if (error == EINTR || error == EIO) {
699 (void) nfsrpc_close(vp, 0, ap->a_td);
703 np->n_mtime = vattr.va_mtime;
705 np->n_change = vattr.va_filerev;
710 * If the object has >= 1 O_DIRECT active opens, we disable caching.
712 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
713 (vp->v_type == VREG)) {
714 if (np->n_directio_opens == 0) {
716 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
717 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
718 if (VN_IS_DOOMED(vp))
721 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
724 (void) nfsrpc_close(vp, 0, ap->a_td);
728 np->n_flag |= NNONCACHE;
730 np->n_directio_opens++;
733 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
734 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
735 np->n_flag |= NWRITEOPENED;
738 * If this is an open for writing, capture a reference to the
739 * credentials, so they can be used by ncl_putpages(). Using
740 * these write credentials is preferable to the credentials of
741 * whatever thread happens to be doing the VOP_PUTPAGES() since
742 * the write RPCs are less likely to fail with EACCES.
744 if ((fmode & FWRITE) != 0) {
745 cred = np->n_writecred;
746 np->n_writecred = crhold(ap->a_cred);
753 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
756 * If the text file has been mmap'd, flush any dirty pages to the
757 * buffer cache and then...
758 * Make sure all writes are pushed to the NFS server. If this is not
759 * done, the modify time of the file can change while the text
760 * file is being executed. This will cause the process that is
761 * executing the text file to be terminated.
763 if (vp->v_writecount <= -1) {
764 if ((obj = vp->v_object) != NULL &&
765 vm_object_mightbedirty(obj)) {
766 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
767 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
768 if (VN_IS_DOOMED(vp))
771 VM_OBJECT_WLOCK(obj);
772 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
773 VM_OBJECT_WUNLOCK(obj);
776 /* Now, flush the buffer cache. */
777 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
779 /* And, finally, make sure that n_mtime is up to date. */
782 np->n_mtime = np->n_vattr.na_mtime;
790 * What an NFS client should do upon close after writing is a debatable issue.
791 * Most NFS clients push delayed writes to the server upon close, basically for
793 * 1 - So that any write errors may be reported back to the client process
794 * doing the close system call. By far the two most likely errors are
795 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
796 * 2 - To put a worst case upper bound on cache inconsistency between
797 * multiple clients for the file.
798 * There is also a consistency problem for Version 2 of the protocol w.r.t.
799 * not being able to tell if other clients are writing a file concurrently,
800 * since there is no way of knowing if the changed modify time in the reply
801 * is only due to the write for this client.
802 * (NFS Version 3 provides weak cache consistency data in the reply that
803 * should be sufficient to detect and handle this case.)
805 * The current code does the following:
806 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
807 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
808 * or commit them (this satisfies 1 and 2 except for the
809 * case where the server crashes after this close but
810 * before the commit RPC, which is felt to be "good
811 * enough". Changing the last argument to ncl_flush() to
812 * a 1 would force a commit operation, if it is felt a
813 * commit is necessary now.
814 * for NFS Version 4 - flush the dirty buffers and commit them, if
815 * nfscl_mustflush() says this is necessary.
816 * It is necessary if there is no write delegation held,
817 * in order to satisfy open/close coherency.
818 * If the file isn't cached on local stable storage,
819 * it may be necessary in order to detect "out of space"
820 * errors from the server, if the write delegation
821 * issued by the server doesn't allow the file to grow.
825 nfs_close(struct vop_close_args *ap)
827 struct vnode *vp = ap->a_vp;
828 struct nfsnode *np = VTONFS(vp);
829 struct nfsvattr nfsva;
831 int error = 0, ret, localcred = 0;
832 int fmode = ap->a_fflag;
834 if (NFSCL_FORCEDISM(vp->v_mount))
837 * During shutdown, a_cred isn't valid, so just use root.
839 if (ap->a_cred == NOCRED) {
840 cred = newnfs_getcred();
845 if (vp->v_type == VREG) {
847 * Examine and clean dirty pages, regardless of NMODIFIED.
848 * This closes a major hole in close-to-open consistency.
849 * We want to push out all dirty pages (and buffers) on
850 * close, regardless of whether they were dirtied by
851 * mmap'ed writes or via write().
853 if (nfs_clean_pages_on_close && vp->v_object) {
854 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
855 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
856 if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
859 VM_OBJECT_WLOCK(vp->v_object);
860 vm_object_page_clean(vp->v_object, 0, 0, 0);
861 VM_OBJECT_WUNLOCK(vp->v_object);
864 if (np->n_flag & NMODIFIED) {
868 * Under NFSv3 we have dirty buffers to dispose of. We
869 * must flush them to the NFS server. We have the option
870 * of waiting all the way through the commit rpc or just
871 * waiting for the initial write. The default is to only
872 * wait through the initial write so the data is in the
873 * server's cache, which is roughly similar to the state
874 * a standard disk subsystem leaves the file in on close().
876 * We cannot clear the NMODIFIED bit in np->n_flag due to
877 * potential races with other processes, and certainly
878 * cannot clear it if we don't commit.
879 * These races occur when there is no longer the old
880 * traditional vnode locking implemented for Vnode Ops.
882 int cm = newnfs_commit_on_close ? 1 : 0;
883 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
884 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
885 if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
888 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
889 /* np->n_flag &= ~NMODIFIED; */
890 } else if (NFS_ISV4(vp)) {
891 if (nfscl_mustflush(vp) != 0) {
892 int cm = newnfs_commit_on_close ? 1 : 0;
893 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
894 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
895 if (VN_IS_DOOMED(vp) && ap->a_fflag !=
899 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
902 * as above w.r.t races when clearing
904 * np->n_flag &= ~NMODIFIED;
908 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
909 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
910 if (VN_IS_DOOMED(vp) && ap->a_fflag !=
914 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
919 * Invalidate the attribute cache in all cases.
920 * An open is going to fetch fresh attrs any way, other procs
921 * on this node that have file open will be forced to do an
922 * otw attr fetch, but this is safe.
923 * --> A user found that their RPC count dropped by 20% when
924 * this was commented out and I can't see any requirement
925 * for it, so I've disabled it when negative lookups are
926 * enabled. (What does this have to do with negative lookup
927 * caching? Well nothing, except it was reported by the
928 * same user that needed negative lookup caching and I wanted
929 * there to be a way to disable it to see if it
930 * is the cause of some caching/coherency issue that might
933 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
935 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
937 if (np->n_flag & NWRITEERR) {
938 np->n_flag &= ~NWRITEERR;
946 * Get attributes so "change" is up to date.
948 if (error == 0 && nfscl_mustflush(vp) != 0 &&
949 vp->v_type == VREG &&
950 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
951 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva);
953 np->n_change = nfsva.na_filerev;
954 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
962 ret = nfsrpc_close(vp, 0, ap->a_td);
966 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
969 if (newnfs_directio_enable)
970 KASSERT((np->n_directio_asyncwr == 0),
971 ("nfs_close: dirty unflushed (%d) directio buffers\n",
972 np->n_directio_asyncwr));
973 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
975 KASSERT((np->n_directio_opens > 0),
976 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
977 np->n_directio_opens--;
978 if (np->n_directio_opens == 0)
979 np->n_flag &= ~NNONCACHE;
988 * nfs getattr call from vfs.
991 nfs_getattr(struct vop_getattr_args *ap)
993 struct vnode *vp = ap->a_vp;
994 struct thread *td = curthread; /* XXX */
995 struct nfsnode *np = VTONFS(vp);
997 struct nfsvattr nfsva;
998 struct vattr *vap = ap->a_vap;
1002 * Update local times for special files.
1005 if (np->n_flag & (NACC | NUPD))
1009 * First look in the cache.
1011 if (ncl_getattrcache(vp, &vattr) == 0) {
1012 ncl_copy_vattr(vap, &vattr);
1015 * Get the local modify time for the case of a write
1018 nfscl_deleggetmodtime(vp, &vap->va_mtime);
1022 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1023 nfsaccess_cache_timeout > 0) {
1024 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1025 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1026 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1027 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1031 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva);
1033 error = nfscl_loadattrcache(&vp, &nfsva, vap, 0, 0);
1036 * Get the local modify time for the case of a write
1039 nfscl_deleggetmodtime(vp, &vap->va_mtime);
1040 } else if (NFS_ISV4(vp)) {
1041 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1050 nfs_setattr(struct vop_setattr_args *ap)
1052 struct vnode *vp = ap->a_vp;
1053 struct nfsnode *np = VTONFS(vp);
1054 struct thread *td = curthread; /* XXX */
1055 struct vattr *vap = ap->a_vap;
1061 tsize = (u_quad_t)0;
1065 * Setting of flags and marking of atimes are not supported.
1067 if (vap->va_flags != VNOVAL)
1068 return (EOPNOTSUPP);
1071 * Disallow write attempts if the filesystem is mounted read-only.
1073 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1074 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1075 vap->va_mtime.tv_sec != VNOVAL ||
1076 vap->va_birthtime.tv_sec != VNOVAL ||
1077 vap->va_mode != (mode_t)VNOVAL) &&
1078 (vp->v_mount->mnt_flag & MNT_RDONLY))
1080 if (vap->va_size != VNOVAL) {
1081 switch (vp->v_type) {
1088 if (vap->va_mtime.tv_sec == VNOVAL &&
1089 vap->va_atime.tv_sec == VNOVAL &&
1090 vap->va_birthtime.tv_sec == VNOVAL &&
1091 vap->va_mode == (mode_t)VNOVAL &&
1092 vap->va_uid == (uid_t)VNOVAL &&
1093 vap->va_gid == (gid_t)VNOVAL)
1095 vap->va_size = VNOVAL;
1099 * Disallow write attempts if the filesystem is
1100 * mounted read-only.
1102 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1105 * We run vnode_pager_setsize() early (why?),
1106 * we must set np->n_size now to avoid vinvalbuf
1107 * V_SAVE races that might setsize a lower
1113 error = ncl_meta_setsize(vp, td, vap->va_size);
1115 if (np->n_flag & NMODIFIED) {
1118 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1121 vnode_pager_setsize(vp, tsize);
1125 * Call nfscl_delegmodtime() to set the modify time
1126 * locally, as required.
1128 nfscl_delegmodtime(vp);
1132 * np->n_size has already been set to vap->va_size
1133 * in ncl_meta_setsize(). We must set it again since
1134 * nfs_loadattrcache() could be called through
1135 * ncl_meta_setsize() and could modify np->n_size.
1138 np->n_vattr.na_size = np->n_size = vap->va_size;
1143 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1144 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1146 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1147 if (error == EINTR || error == EIO)
1152 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1153 if (vap->va_size != VNOVAL) {
1157 np->n_localmodtime = ts;
1161 np->n_size = np->n_vattr.na_size = tsize;
1162 vnode_pager_setsize(vp, tsize);
1170 * Do an nfs setattr rpc.
1173 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1176 struct nfsnode *np = VTONFS(vp);
1177 int error, ret, attrflag, i;
1178 struct nfsvattr nfsva;
1180 if (NFS_ISV34(vp)) {
1182 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1183 np->n_accesscache[i].stamp = 0;
1184 np->n_flag |= NDELEGMOD;
1186 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1188 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag);
1190 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1194 if (error && NFS_ISV4(vp))
1195 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1200 * nfs lookup call, one step at a time...
1201 * First look in cache
1202 * If not found, unlock the directory nfsnode and do the rpc
1205 nfs_lookup(struct vop_lookup_args *ap)
1207 struct componentname *cnp = ap->a_cnp;
1208 struct vnode *dvp = ap->a_dvp;
1209 struct vnode **vpp = ap->a_vpp;
1210 struct mount *mp = dvp->v_mount;
1211 int flags = cnp->cn_flags;
1212 struct vnode *newvp;
1213 struct nfsmount *nmp;
1214 struct nfsnode *np, *newnp;
1215 int error = 0, attrflag, dattrflag, ltype, ncticks;
1216 struct thread *td = curthread;
1218 struct nfsvattr dnfsva, nfsva;
1220 struct timespec nctime, ts;
1224 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1225 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1227 if (dvp->v_type != VDIR)
1232 /* For NFSv4, wait until any remove is done. */
1234 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1235 np->n_flag |= NREMOVEWANT;
1236 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1240 error = vn_dir_check_exec(dvp, cnp);
1243 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1244 if (error > 0 && error != ENOENT)
1248 * Lookups of "." are special and always return the
1249 * current directory. cache_lookup() already handles
1250 * associated locking bookkeeping, etc.
1252 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1253 /* XXX: Is this really correct? */
1254 if (cnp->cn_nameiop != LOOKUP &&
1256 cnp->cn_flags |= SAVENAME;
1261 * We only accept a positive hit in the cache if the
1262 * change time of the file matches our cached copy.
1263 * Otherwise, we discard the cache entry and fallback
1264 * to doing a lookup RPC. We also only trust cache
1265 * entries for less than nm_nametimeo seconds.
1267 * To better handle stale file handles and attributes,
1268 * clear the attribute cache of this node if it is a
1269 * leaf component, part of an open() call, and not
1270 * locally modified before fetching the attributes.
1271 * This should allow stale file handles to be detected
1272 * here where we can fall back to a LOOKUP RPC to
1273 * recover rather than having nfs_open() detect the
1274 * stale file handle and failing open(2) with ESTALE.
1277 newnp = VTONFS(newvp);
1278 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1279 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1280 !(newnp->n_flag & NMODIFIED)) {
1282 newnp->n_attrstamp = 0;
1283 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1284 NFSUNLOCKNODE(newnp);
1286 if (nfscl_nodeleg(newvp, 0) == 0 ||
1287 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1288 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1289 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1290 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1291 if (cnp->cn_nameiop != LOOKUP &&
1293 cnp->cn_flags |= SAVENAME;
1302 } else if (error == ENOENT) {
1303 if (VN_IS_DOOMED(dvp))
1306 * We only accept a negative hit in the cache if the
1307 * modification time of the parent directory matches
1308 * the cached copy in the name cache entry.
1309 * Otherwise, we discard all of the negative cache
1310 * entries for this directory. We also only trust
1311 * negative cache entries for up to nm_negnametimeo
1314 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1315 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1316 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1317 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1320 cache_purge_negative(dvp);
1325 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1326 * option, it is possible to do the Open operation in the same
1327 * compound as Lookup, so long as delegations are not being
1328 * issued. This saves doing a separate RPC for Open.
1329 * For pnfs, do not do this, since the Open+LayoutGet will
1330 * be needed as a separate RPC.
1333 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) && !NFSHASPNFS(nmp) &&
1334 (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1335 (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1336 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN)) {
1337 if ((flags & OPENREAD) != 0)
1338 openmode |= NFSV4OPEN_ACCESSREAD;
1339 if ((flags & OPENWRITE) != 0)
1340 openmode |= NFSV4OPEN_ACCESSWRITE;
1345 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1347 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1348 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1351 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1353 if (newvp != NULLVP) {
1358 if (error != ENOENT) {
1360 error = nfscl_maperr(td, error, (uid_t)0,
1365 /* The requested file was not found. */
1366 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1367 (flags & ISLASTCN)) {
1369 * XXX: UFS does a full VOP_ACCESS(dvp,
1370 * VWRITE) here instead of just checking
1373 if (mp->mnt_flag & MNT_RDONLY)
1375 cnp->cn_flags |= SAVENAME;
1376 return (EJUSTRETURN);
1379 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1381 * Cache the modification time of the parent
1382 * directory from the post-op attributes in
1383 * the name cache entry. The negative cache
1384 * entry will be ignored once the directory
1385 * has changed. Don't bother adding the entry
1386 * if the directory has already changed.
1389 if (timespeccmp(&np->n_vattr.na_mtime,
1390 &dnfsva.na_mtime, ==)) {
1392 cache_enter_time(dvp, NULL, cnp,
1393 &dnfsva.na_mtime, NULL);
1401 * Handle RENAME case...
1403 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1404 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1405 free(nfhp, M_NFSFH);
1408 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, LK_EXCLUSIVE);
1413 * If n_localmodtime >= time before RPC, then
1414 * a file modification operation, such as
1415 * VOP_SETATTR() of size, has occurred while
1416 * the Lookup RPC and acquisition of the vnode
1417 * happened. As such, the attributes might
1418 * be stale, with possibly an incorrect size.
1421 if (timespecisset(&np->n_localmodtime) &&
1422 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1423 NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1424 "stale attributes\n");
1429 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1431 cnp->cn_flags |= SAVENAME;
1435 if (flags & ISDOTDOT) {
1436 ltype = NFSVOPISLOCKED(dvp);
1437 error = vfs_busy(mp, MBF_NOWAIT);
1441 error = vfs_busy(mp, 0);
1442 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1444 if (error == 0 && VN_IS_DOOMED(dvp)) {
1452 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1458 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1459 if (VN_IS_DOOMED(dvp)) {
1471 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1472 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1473 free(nfhp, M_NFSFH);
1477 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1479 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np,
1485 * If n_localmodtime >= time before RPC, then
1486 * a file modification operation, such as
1487 * VOP_SETATTR() of size, has occurred while
1488 * the Lookup RPC and acquisition of the vnode
1489 * happened. As such, the attributes might
1490 * be stale, with possibly an incorrect size.
1493 if (timespecisset(&np->n_localmodtime) &&
1494 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1495 NFSCL_DEBUG(4, "nfs_lookup: localmod "
1496 "stale attributes\n");
1501 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1502 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1503 !(np->n_flag & NMODIFIED)) {
1505 * Flush the attribute cache when opening a
1506 * leaf node to ensure that fresh attributes
1507 * are fetched in nfs_open() since we did not
1508 * fetch attributes from the LOOKUP reply.
1511 np->n_attrstamp = 0;
1512 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1516 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1517 cnp->cn_flags |= SAVENAME;
1518 if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1519 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1520 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1521 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1522 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1529 * Just call ncl_bioread() to do the work.
1532 nfs_read(struct vop_read_args *ap)
1534 struct vnode *vp = ap->a_vp;
1536 switch (vp->v_type) {
1538 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1542 return (EOPNOTSUPP);
1550 nfs_readlink(struct vop_readlink_args *ap)
1552 struct vnode *vp = ap->a_vp;
1554 if (vp->v_type != VLNK)
1556 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1560 * Do a readlink rpc.
1561 * Called by ncl_doio() from below the buffer cache.
1564 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1566 int error, ret, attrflag;
1567 struct nfsvattr nfsva;
1569 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1572 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1576 if (error && NFS_ISV4(vp))
1577 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1586 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1588 int error, ret, attrflag;
1589 struct nfsvattr nfsva;
1590 struct nfsmount *nmp;
1592 nmp = VFSTONFS(vp->v_mount);
1595 if (NFSHASPNFS(nmp))
1596 error = nfscl_doiods(vp, uiop, NULL, NULL,
1597 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1598 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1600 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1603 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1607 if (error && NFS_ISV4(vp))
1608 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1616 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1617 int *iomode, int *must_commit, int called_from_strategy, int ioflag)
1619 struct nfsvattr nfsva;
1620 int error, attrflag, ret;
1621 struct nfsmount *nmp;
1623 nmp = VFSTONFS(vp->v_mount);
1626 if (NFSHASPNFS(nmp))
1627 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1628 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1629 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1631 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1632 uiop->uio_td, &nfsva, &attrflag, called_from_strategy,
1635 if (VTONFS(vp)->n_flag & ND_NFSV4)
1636 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 1, 1);
1638 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
1643 *iomode = NFSWRITE_FILESYNC;
1644 if (error && NFS_ISV4(vp))
1645 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1651 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1652 * mode set to specify the file type and the size field for rdev.
1655 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1658 struct nfsvattr nfsva, dnfsva;
1659 struct vnode *newvp = NULL;
1660 struct nfsnode *np = NULL, *dnp;
1663 int error = 0, attrflag, dattrflag;
1666 if (vap->va_type == VCHR || vap->va_type == VBLK)
1667 rdev = vap->va_rdev;
1668 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1671 return (EOPNOTSUPP);
1672 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1674 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1675 rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1676 &nfsva, &nfhp, &attrflag, &dattrflag);
1679 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1680 cnp->cn_namelen, cnp->cn_cred, curthread,
1681 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1683 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1684 curthread, &np, LK_EXCLUSIVE);
1687 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1690 if (attrflag != 0) {
1691 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1698 } else if (NFS_ISV4(dvp)) {
1699 error = nfscl_maperr(curthread, error, vap->va_uid,
1704 dnp->n_flag |= NMODIFIED;
1706 dnp->n_attrstamp = 0;
1707 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1715 * just call nfs_mknodrpc() to do the work.
1719 nfs_mknod(struct vop_mknod_args *ap)
1721 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1724 static struct mtx nfs_cverf_mtx;
1725 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1731 static nfsquad_t cverf;
1733 static int cverf_initialized = 0;
1735 mtx_lock(&nfs_cverf_mtx);
1736 if (cverf_initialized == 0) {
1737 cverf.lval[0] = arc4random();
1738 cverf.lval[1] = arc4random();
1739 cverf_initialized = 1;
1743 mtx_unlock(&nfs_cverf_mtx);
1749 * nfs file create call
1752 nfs_create(struct vop_create_args *ap)
1754 struct vnode *dvp = ap->a_dvp;
1755 struct vattr *vap = ap->a_vap;
1756 struct componentname *cnp = ap->a_cnp;
1757 struct nfsnode *np = NULL, *dnp;
1758 struct vnode *newvp = NULL;
1759 struct nfsmount *nmp;
1760 struct nfsvattr dnfsva, nfsva;
1763 int error = 0, attrflag, dattrflag, fmode = 0;
1767 * Oops, not for me..
1769 if (vap->va_type == VSOCK)
1770 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1772 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1774 if (vap->va_vaflags & VA_EXCLUSIVE)
1777 nmp = VFSTONFS(dvp->v_mount);
1779 /* For NFSv4, wait until any remove is done. */
1781 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1782 dnp->n_flag |= NREMOVEWANT;
1783 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1787 cverf = nfs_get_cverf();
1788 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1789 vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1790 &nfhp, &attrflag, &dattrflag);
1793 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1794 cnp->cn_namelen, cnp->cn_cred, curthread,
1795 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag, 0);
1797 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1798 curthread, &np, LK_EXCLUSIVE);
1801 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1805 error = nfsrpc_getattr(newvp, cnp->cn_cred, curthread,
1808 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
1811 if (newvp != NULL) {
1815 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1816 error == NFSERR_NOTSUPP) {
1820 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1821 if (nfscl_checksattr(vap, &nfsva)) {
1822 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1823 curthread, &nfsva, &attrflag);
1824 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1825 vap->va_gid != (gid_t)VNOVAL)) {
1826 /* try again without setting uid/gid */
1827 vap->va_uid = (uid_t)VNOVAL;
1828 vap->va_gid = (uid_t)VNOVAL;
1829 error = nfsrpc_setattr(newvp, vap, NULL,
1830 cnp->cn_cred, curthread, &nfsva, &attrflag);
1833 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1840 if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1842 cache_enter_time(dvp, newvp, cnp,
1843 &nfsva.na_ctime, NULL);
1845 printf("nfs_create: bogus NFS server returned "
1846 "the directory as the new file object\n");
1849 } else if (NFS_ISV4(dvp)) {
1850 error = nfscl_maperr(curthread, error, vap->va_uid,
1854 dnp->n_flag |= NMODIFIED;
1856 dnp->n_attrstamp = 0;
1857 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1864 * nfs file remove call
1865 * To try and make nfs semantics closer to ufs semantics, a file that has
1866 * other processes using the vnode is renamed instead of removed and then
1867 * removed later on the last close.
1868 * - If v_usecount > 1
1869 * If a rename is not already in the works
1870 * call nfs_sillyrename() to set it up
1875 nfs_remove(struct vop_remove_args *ap)
1877 struct vnode *vp = ap->a_vp;
1878 struct vnode *dvp = ap->a_dvp;
1879 struct componentname *cnp = ap->a_cnp;
1880 struct nfsnode *np = VTONFS(vp);
1884 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1885 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1886 if (vp->v_type == VDIR)
1888 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1889 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1890 vattr.va_nlink > 1)) {
1892 * Purge the name cache so that the chance of a lookup for
1893 * the name succeeding while the remove is in progress is
1894 * minimized. Without node locking it can still happen, such
1895 * that an I/O op returns ESTALE, but since you get this if
1896 * another host removes the file..
1900 * throw away biocache buffers, mainly to avoid
1901 * unnecessary delayed writes later.
1903 error = ncl_vinvalbuf(vp, 0, curthread, 1);
1904 if (error != EINTR && error != EIO)
1906 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1907 cnp->cn_namelen, cnp->cn_cred, curthread);
1909 * Kludge City: If the first reply to the remove rpc is lost..
1910 * the reply to the retransmitted request will be ENOENT
1911 * since the file was in fact removed
1912 * Therefore, we cheat and return success.
1914 if (error == ENOENT)
1916 } else if (!np->n_sillyrename)
1917 error = nfs_sillyrename(dvp, vp, cnp);
1919 np->n_attrstamp = 0;
1921 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1926 * nfs file remove rpc called from nfs_inactive
1929 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1932 * Make sure that the directory vnode is still valid.
1933 * XXX we should lock sp->s_dvp here.
1935 if (sp->s_dvp->v_type == VBAD)
1937 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1942 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1945 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1946 int namelen, struct ucred *cred, struct thread *td)
1948 struct nfsvattr dnfsva;
1949 struct nfsnode *dnp = VTONFS(dvp);
1950 int error = 0, dattrflag;
1953 dnp->n_flag |= NREMOVEINPROG;
1955 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1958 if ((dnp->n_flag & NREMOVEWANT)) {
1959 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1961 wakeup((caddr_t)dnp);
1963 dnp->n_flag &= ~NREMOVEINPROG;
1967 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
1969 dnp->n_flag |= NMODIFIED;
1971 dnp->n_attrstamp = 0;
1972 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1975 if (error && NFS_ISV4(dvp))
1976 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1981 * nfs file rename call
1984 nfs_rename(struct vop_rename_args *ap)
1986 struct vnode *fvp = ap->a_fvp;
1987 struct vnode *tvp = ap->a_tvp;
1988 struct vnode *fdvp = ap->a_fdvp;
1989 struct vnode *tdvp = ap->a_tdvp;
1990 struct componentname *tcnp = ap->a_tcnp;
1991 struct componentname *fcnp = ap->a_fcnp;
1992 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1993 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1994 struct nfsv4node *newv4 = NULL;
1997 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1998 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1999 /* Check for cross-device rename */
2000 if ((fvp->v_mount != tdvp->v_mount) ||
2001 (tvp && (fvp->v_mount != tvp->v_mount))) {
2007 printf("nfs_rename: fvp == tvp (can't happen)\n");
2011 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2015 * We have to flush B_DELWRI data prior to renaming
2016 * the file. If we don't, the delayed-write buffers
2017 * can be flushed out later after the file has gone stale
2018 * under NFSV3. NFSV2 does not have this problem because
2019 * ( as far as I can tell ) it flushes dirty buffers more
2022 * Skip the rename operation if the fsync fails, this can happen
2023 * due to the server's volume being full, when we pushed out data
2024 * that was written back to our cache earlier. Not checking for
2025 * this condition can result in potential (silent) data loss.
2027 error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2030 error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2035 * If the tvp exists and is in use, sillyrename it before doing the
2036 * rename of the new file over it.
2037 * XXX Can't sillyrename a directory.
2039 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2040 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2045 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2046 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2049 if (error == 0 && NFS_ISV4(tdvp)) {
2051 * For NFSv4, check to see if it is the same name and
2052 * replace the name, if it is different.
2055 sizeof (struct nfsv4node) +
2056 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2057 M_NFSV4NODE, M_WAITOK);
2060 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2061 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2062 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2063 tcnp->cn_namelen) ||
2064 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2065 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2066 tdnp->n_fhp->nfh_len))) {
2068 { char nnn[100]; int nnnl;
2069 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
2070 bcopy(tcnp->cn_nameptr, nnn, nnnl);
2072 printf("ren replace=%s\n",nnn);
2075 free(fnp->n_v4, M_NFSV4NODE);
2078 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2079 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2080 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2081 tdnp->n_fhp->nfh_len);
2082 NFSBCOPY(tcnp->cn_nameptr,
2083 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2085 NFSUNLOCKNODE(tdnp);
2088 free(newv4, M_NFSV4NODE);
2091 if (fvp->v_type == VDIR) {
2092 if (tvp != NULL && tvp->v_type == VDIR)
2107 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2109 if (error == ENOENT)
2115 * nfs file rename rpc called from nfs_remove() above
2118 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2119 struct sillyrename *sp)
2122 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2123 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
2128 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2131 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2132 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2133 int tnamelen, struct ucred *cred, struct thread *td)
2135 struct nfsvattr fnfsva, tnfsva;
2136 struct nfsnode *fdnp = VTONFS(fdvp);
2137 struct nfsnode *tdnp = VTONFS(tdvp);
2138 int error = 0, fattrflag, tattrflag;
2140 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2141 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
2144 fdnp->n_flag |= NMODIFIED;
2145 if (fattrflag != 0) {
2146 NFSUNLOCKNODE(fdnp);
2147 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, 0, 1);
2149 fdnp->n_attrstamp = 0;
2150 NFSUNLOCKNODE(fdnp);
2151 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2154 tdnp->n_flag |= NMODIFIED;
2155 if (tattrflag != 0) {
2156 NFSUNLOCKNODE(tdnp);
2157 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, 0, 1);
2159 tdnp->n_attrstamp = 0;
2160 NFSUNLOCKNODE(tdnp);
2161 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2163 if (error && NFS_ISV4(fdvp))
2164 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2169 * nfs hard link create call
2172 nfs_link(struct vop_link_args *ap)
2174 struct vnode *vp = ap->a_vp;
2175 struct vnode *tdvp = ap->a_tdvp;
2176 struct componentname *cnp = ap->a_cnp;
2177 struct nfsnode *np, *tdnp;
2178 struct nfsvattr nfsva, dnfsva;
2179 int error = 0, attrflag, dattrflag;
2182 * Push all writes to the server, so that the attribute cache
2183 * doesn't get "out of sync" with the server.
2184 * XXX There should be a better way!
2186 VOP_FSYNC(vp, MNT_WAIT, curthread);
2188 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2189 cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag, &dattrflag);
2190 tdnp = VTONFS(tdvp);
2192 tdnp->n_flag |= NMODIFIED;
2193 if (dattrflag != 0) {
2194 NFSUNLOCKNODE(tdnp);
2195 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, 0, 1);
2197 tdnp->n_attrstamp = 0;
2198 NFSUNLOCKNODE(tdnp);
2199 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2202 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2206 np->n_attrstamp = 0;
2208 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2211 * If negative lookup caching is enabled, I might as well
2212 * add an entry for this node. Not necessary for correctness,
2213 * but if negative caching is enabled, then the system
2214 * must care about lookup caching hit rate, so...
2216 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2217 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2219 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2221 printf("nfs_link: bogus NFS server returned "
2222 "the directory as the new link\n");
2224 if (error && NFS_ISV4(vp))
2225 error = nfscl_maperr(curthread, error, (uid_t)0,
2231 * nfs symbolic link create call
2234 nfs_symlink(struct vop_symlink_args *ap)
2236 struct vnode *dvp = ap->a_dvp;
2237 struct vattr *vap = ap->a_vap;
2238 struct componentname *cnp = ap->a_cnp;
2239 struct nfsvattr nfsva, dnfsva;
2241 struct nfsnode *np = NULL, *dnp;
2242 struct vnode *newvp = NULL;
2243 int error = 0, attrflag, dattrflag, ret;
2245 vap->va_type = VLNK;
2246 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2247 ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2248 &nfsva, &nfhp, &attrflag, &dattrflag);
2250 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2257 if (newvp != NULL) {
2259 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2260 } else if (!error) {
2262 * If we do not have an error and we could not extract the
2263 * newvp from the response due to the request being NFSv2, we
2264 * have to do a lookup in order to obtain a newvp to return.
2266 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2267 cnp->cn_cred, curthread, &np);
2275 error = nfscl_maperr(curthread, error,
2276 vap->va_uid, vap->va_gid);
2283 dnp->n_flag |= NMODIFIED;
2284 if (dattrflag != 0) {
2286 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2288 dnp->n_attrstamp = 0;
2290 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2293 * If negative lookup caching is enabled, I might as well
2294 * add an entry for this node. Not necessary for correctness,
2295 * but if negative caching is enabled, then the system
2296 * must care about lookup caching hit rate, so...
2298 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2299 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2301 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2304 printf("nfs_symlink: bogus NFS server returned "
2305 "the directory as the new file object\n");
2314 nfs_mkdir(struct vop_mkdir_args *ap)
2316 struct vnode *dvp = ap->a_dvp;
2317 struct vattr *vap = ap->a_vap;
2318 struct componentname *cnp = ap->a_cnp;
2319 struct nfsnode *np = NULL, *dnp;
2320 struct vnode *newvp = NULL;
2323 struct nfsvattr nfsva, dnfsva;
2324 int error = 0, attrflag, dattrflag, ret;
2326 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2328 vap->va_type = VDIR;
2329 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2330 vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2331 &attrflag, &dattrflag);
2334 dnp->n_flag |= NMODIFIED;
2335 if (dattrflag != 0) {
2337 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2339 dnp->n_attrstamp = 0;
2341 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2344 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2349 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2354 if (!error && newvp == NULL) {
2355 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2356 cnp->cn_cred, curthread, &np);
2359 if (newvp->v_type != VDIR)
2367 error = nfscl_maperr(curthread, error,
2368 vap->va_uid, vap->va_gid);
2371 * If negative lookup caching is enabled, I might as well
2372 * add an entry for this node. Not necessary for correctness,
2373 * but if negative caching is enabled, then the system
2374 * must care about lookup caching hit rate, so...
2376 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2377 (cnp->cn_flags & MAKEENTRY) &&
2378 attrflag != 0 && dattrflag != 0) {
2380 cache_enter_time(dvp, newvp, cnp,
2381 &nfsva.na_ctime, &dnfsva.na_ctime);
2383 printf("nfs_mkdir: bogus NFS server returned "
2384 "the directory that the directory was "
2385 "created in as the new file object\n");
2393 * nfs remove directory call
2396 nfs_rmdir(struct vop_rmdir_args *ap)
2398 struct vnode *vp = ap->a_vp;
2399 struct vnode *dvp = ap->a_dvp;
2400 struct componentname *cnp = ap->a_cnp;
2401 struct nfsnode *dnp;
2402 struct nfsvattr dnfsva;
2403 int error, dattrflag;
2407 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2408 cnp->cn_cred, curthread, &dnfsva, &dattrflag);
2411 dnp->n_flag |= NMODIFIED;
2412 if (dattrflag != 0) {
2414 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2416 dnp->n_attrstamp = 0;
2418 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2423 if (error && NFS_ISV4(dvp))
2424 error = nfscl_maperr(curthread, error, (uid_t)0,
2427 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2429 if (error == ENOENT)
2438 nfs_readdir(struct vop_readdir_args *ap)
2440 struct vnode *vp = ap->a_vp;
2441 struct nfsnode *np = VTONFS(vp);
2442 struct uio *uio = ap->a_uio;
2443 ssize_t tresid, left;
2447 if (ap->a_eofflag != NULL)
2449 if (vp->v_type != VDIR)
2453 * First, check for hit on the EOF offset cache
2456 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2457 (np->n_flag & NMODIFIED) == 0) {
2459 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2461 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2462 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2464 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2465 if (ap->a_eofflag != NULL)
2475 * NFS always guarantees that directory entries don't straddle
2476 * DIRBLKSIZ boundaries. As such, we need to limit the size
2477 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2480 left = uio->uio_resid % DIRBLKSIZ;
2481 if (left == uio->uio_resid)
2483 uio->uio_resid -= left;
2486 * Call ncl_bioread() to do the real work.
2488 tresid = uio->uio_resid;
2489 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2491 if (!error && uio->uio_resid == tresid) {
2492 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2493 if (ap->a_eofflag != NULL)
2497 /* Add the partial DIRBLKSIZ (left) back in. */
2498 uio->uio_resid += left;
2504 * Called from below the buffer cache by ncl_doio().
2507 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2510 struct nfsvattr nfsva;
2511 nfsuint64 *cookiep, cookie;
2512 struct nfsnode *dnp = VTONFS(vp);
2513 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2514 int error = 0, eof, attrflag;
2516 KASSERT(uiop->uio_iovcnt == 1 &&
2517 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2518 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2519 ("nfs readdirrpc bad uio"));
2522 * If there is no cookie, assume directory was stale.
2524 ncl_dircookie_lock(dnp);
2526 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2529 ncl_dircookie_unlock(dnp);
2531 ncl_dircookie_unlock(dnp);
2532 return (NFSERR_BAD_COOKIE);
2535 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2536 (void)ncl_fsinfo(nmp, vp, cred, td);
2538 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2541 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2545 * We are now either at the end of the directory or have filled
2550 dnp->n_direofoffset = uiop->uio_offset;
2553 if (uiop->uio_resid > 0)
2554 printf("EEK! readdirrpc resid > 0\n");
2555 ncl_dircookie_lock(dnp);
2557 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2559 ncl_dircookie_unlock(dnp);
2561 } else if (NFS_ISV4(vp)) {
2562 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2568 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2571 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2574 struct nfsvattr nfsva;
2575 nfsuint64 *cookiep, cookie;
2576 struct nfsnode *dnp = VTONFS(vp);
2577 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2578 int error = 0, attrflag, eof;
2580 KASSERT(uiop->uio_iovcnt == 1 &&
2581 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2582 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2583 ("nfs readdirplusrpc bad uio"));
2586 * If there is no cookie, assume directory was stale.
2588 ncl_dircookie_lock(dnp);
2590 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2593 ncl_dircookie_unlock(dnp);
2595 ncl_dircookie_unlock(dnp);
2596 return (NFSERR_BAD_COOKIE);
2599 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2600 (void)ncl_fsinfo(nmp, vp, cred, td);
2601 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2604 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2608 * We are now either at end of the directory or have filled the
2613 dnp->n_direofoffset = uiop->uio_offset;
2616 if (uiop->uio_resid > 0)
2617 printf("EEK! readdirplusrpc resid > 0\n");
2618 ncl_dircookie_lock(dnp);
2620 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2622 ncl_dircookie_unlock(dnp);
2624 } else if (NFS_ISV4(vp)) {
2625 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2631 * Silly rename. To make the NFS filesystem that is stateless look a little
2632 * more like the "ufs" a remove of an active vnode is translated to a rename
2633 * to a funny looking filename that is removed by nfs_inactive on the
2634 * nfsnode. There is the potential for another process on a different client
2635 * to create the same funny name between the nfs_lookitup() fails and the
2636 * nfs_rename() completes, but...
2639 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2641 struct sillyrename *sp;
2645 unsigned int lticks;
2649 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2650 sp = malloc(sizeof (struct sillyrename),
2651 M_NEWNFSREQ, M_WAITOK);
2652 sp->s_cred = crhold(cnp->cn_cred);
2657 * Fudge together a funny name.
2658 * Changing the format of the funny name to accommodate more
2659 * sillynames per directory.
2660 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2661 * CPU ticks since boot.
2663 pid = curthread->td_proc->p_pid;
2664 lticks = (unsigned int)ticks;
2666 sp->s_namlen = sprintf(sp->s_name,
2667 ".nfs.%08x.%04x4.4", lticks,
2669 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2674 error = nfs_renameit(dvp, vp, cnp, sp);
2677 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2679 np->n_sillyrename = sp;
2684 free(sp, M_NEWNFSREQ);
2689 * Look up a file name and optionally either update the file handle or
2690 * allocate an nfsnode, depending on the value of npp.
2691 * npp == NULL --> just do the lookup
2692 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2694 * *npp != NULL --> update the file handle in the vnode
2697 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2698 struct thread *td, struct nfsnode **npp)
2700 struct vnode *newvp = NULL, *vp;
2701 struct nfsnode *np, *dnp = VTONFS(dvp);
2702 struct nfsfh *nfhp, *onfhp;
2703 struct nfsvattr nfsva, dnfsva;
2704 struct componentname cn;
2705 int error = 0, attrflag, dattrflag;
2710 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2711 &nfhp, &attrflag, &dattrflag, 0);
2713 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, 0, 1);
2714 if (npp && !error) {
2719 * For NFSv4, check to see if it is the same name and
2720 * replace the name, if it is different.
2722 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2723 (np->n_v4->n4_namelen != len ||
2724 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2725 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2726 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2727 dnp->n_fhp->nfh_len))) {
2729 { char nnn[100]; int nnnl;
2730 nnnl = (len < 100) ? len : 99;
2731 bcopy(name, nnn, nnnl);
2733 printf("replace=%s\n",nnn);
2736 free(np->n_v4, M_NFSV4NODE);
2738 sizeof (struct nfsv4node) +
2739 dnp->n_fhp->nfh_len + len - 1,
2740 M_NFSV4NODE, M_WAITOK);
2741 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2742 np->n_v4->n4_namelen = len;
2743 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2744 dnp->n_fhp->nfh_len);
2745 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2747 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2751 * Rehash node for new file handle.
2753 vfs_hash_rehash(vp, hash);
2756 free(onfhp, M_NFSFH);
2758 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2759 free(nfhp, M_NFSFH);
2763 cn.cn_nameptr = name;
2764 cn.cn_namelen = len;
2765 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2771 * If n_localmodtime >= time before RPC, then
2772 * a file modification operation, such as
2773 * VOP_SETATTR() of size, has occurred while
2774 * the Lookup RPC and acquisition of the vnode
2775 * happened. As such, the attributes might
2776 * be stale, with possibly an incorrect size.
2779 if (timespecisset(&np->n_localmodtime) &&
2780 timespeccmp(&np->n_localmodtime, &ts, >=)) {
2781 NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2782 "stale attributes\n");
2787 if (!attrflag && *npp == NULL) {
2795 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, 0, 1);
2797 if (npp && *npp == NULL) {
2808 if (error && NFS_ISV4(dvp))
2809 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2814 * Nfs Version 3 and 4 commit rpc
2817 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2820 struct nfsvattr nfsva;
2821 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2824 int error, attrflag;
2829 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2830 uio.uio_offset = offset;
2831 uio.uio_resid = cnt;
2832 error = nfscl_doiods(vp, &uio, NULL, NULL,
2833 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2836 np->n_flag &= ~NDSCOMMIT;
2841 mtx_lock(&nmp->nm_mtx);
2842 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2843 mtx_unlock(&nmp->nm_mtx);
2846 mtx_unlock(&nmp->nm_mtx);
2847 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2851 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
2852 if (error != 0 && NFS_ISV4(vp))
2853 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2859 * For async requests when nfsiod(s) are running, queue the request by
2860 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2864 nfs_strategy(struct vop_strategy_args *ap)
2872 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2873 KASSERT(!(bp->b_flags & B_DONE),
2874 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2876 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2877 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2879 if (bp->b_iocmd == BIO_READ)
2885 * If the op is asynchronous and an i/o daemon is waiting
2886 * queue the request, wake it up and wait for completion
2887 * otherwise just do it ourselves.
2889 if ((bp->b_flags & B_ASYNC) == 0 ||
2890 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2891 (void) ncl_doio(vp, bp, cr, curthread, 1);
2896 * fsync vnode op. Just call ncl_flush() with commit == 1.
2900 nfs_fsync(struct vop_fsync_args *ap)
2903 if (ap->a_vp->v_type != VREG) {
2905 * For NFS, metadata is changed synchronously on the server,
2906 * so there is nothing to flush. Also, ncl_flush() clears
2907 * the NMODIFIED flag and that shouldn't be done here for
2912 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2916 * Flush all the blocks associated with a vnode.
2917 * Walk through the buffer pool and push any dirty pages
2918 * associated with the vnode.
2919 * If the called_from_renewthread argument is TRUE, it has been called
2920 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2921 * waiting for a buffer write to complete.
2924 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2925 int commit, int called_from_renewthread)
2927 struct nfsnode *np = VTONFS(vp);
2931 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2932 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2933 int passone = 1, trycnt = 0;
2934 u_quad_t off, endoff, toff;
2935 struct ucred* wcred = NULL;
2936 struct buf **bvec = NULL;
2938 #ifndef NFS_COMMITBVECSIZ
2939 #define NFS_COMMITBVECSIZ 20
2941 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2942 u_int bvecsize = 0, bveccount;
2945 if (called_from_renewthread != 0)
2947 if (nmp->nm_flag & NFSMNT_INT)
2953 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2954 * server, but has not been committed to stable storage on the server
2955 * yet. On the first pass, the byte range is worked out and the commit
2956 * rpc is done. On the second pass, ncl_writebp() is called to do the
2963 if (NFS_ISV34(vp) && commit) {
2964 if (bvec != NULL && bvec != bvec_on_stack)
2967 * Count up how many buffers waiting for a commit.
2971 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2972 if (!BUF_ISLOCKED(bp) &&
2973 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2974 == (B_DELWRI | B_NEEDCOMMIT))
2978 * Allocate space to remember the list of bufs to commit. It is
2979 * important to use M_NOWAIT here to avoid a race with nfs_write.
2980 * If we can't get memory (for whatever reason), we will end up
2981 * committing the buffers one-by-one in the loop below.
2983 if (bveccount > NFS_COMMITBVECSIZ) {
2985 * Release the vnode interlock to avoid a lock
2989 bvec = (struct buf **)
2990 malloc(bveccount * sizeof(struct buf *),
2994 bvec = bvec_on_stack;
2995 bvecsize = NFS_COMMITBVECSIZ;
2997 bvecsize = bveccount;
2999 bvec = bvec_on_stack;
3000 bvecsize = NFS_COMMITBVECSIZ;
3002 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3003 if (bvecpos >= bvecsize)
3005 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3006 nbp = TAILQ_NEXT(bp, b_bobufs);
3009 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3010 (B_DELWRI | B_NEEDCOMMIT)) {
3012 nbp = TAILQ_NEXT(bp, b_bobufs);
3018 * Work out if all buffers are using the same cred
3019 * so we can deal with them all with one commit.
3021 * NOTE: we are not clearing B_DONE here, so we have
3022 * to do it later on in this routine if we intend to
3023 * initiate I/O on the bp.
3025 * Note: to avoid loopback deadlocks, we do not
3026 * assign b_runningbufspace.
3029 wcred = bp->b_wcred;
3030 else if (wcred != bp->b_wcred)
3032 vfs_busy_pages(bp, 1);
3036 * bp is protected by being locked, but nbp is not
3037 * and vfs_busy_pages() may sleep. We have to
3040 nbp = TAILQ_NEXT(bp, b_bobufs);
3043 * A list of these buffers is kept so that the
3044 * second loop knows which buffers have actually
3045 * been committed. This is necessary, since there
3046 * may be a race between the commit rpc and new
3047 * uncommitted writes on the file.
3049 bvec[bvecpos++] = bp;
3050 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3054 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3062 * Commit data on the server, as required.
3063 * If all bufs are using the same wcred, then use that with
3064 * one call for all of them, otherwise commit each one
3067 if (wcred != NOCRED)
3068 retv = ncl_commit(vp, off, (int)(endoff - off),
3072 for (i = 0; i < bvecpos; i++) {
3075 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3077 size = (u_quad_t)(bp->b_dirtyend
3079 retv = ncl_commit(vp, off, (int)size,
3085 if (retv == NFSERR_STALEWRITEVERF)
3086 ncl_clearcommit(vp->v_mount);
3089 * Now, either mark the blocks I/O done or mark the
3090 * blocks dirty, depending on whether the commit
3093 for (i = 0; i < bvecpos; i++) {
3095 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3096 if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3098 * Error, leave B_DELWRI intact
3100 vfs_unbusy_pages(bp);
3104 * Success, remove B_DELWRI ( bundirty() ).
3106 * b_dirtyoff/b_dirtyend seem to be NFS
3107 * specific. We should probably move that
3108 * into bundirty(). XXX
3111 bp->b_flags |= B_ASYNC;
3113 bp->b_flags &= ~B_DONE;
3114 bp->b_ioflags &= ~BIO_ERROR;
3115 bp->b_dirtyoff = bp->b_dirtyend = 0;
3122 * Start/do any write(s) that are required.
3126 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3127 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3128 if (waitfor != MNT_WAIT || passone)
3131 error = BUF_TIMELOCK(bp,
3132 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3133 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3138 if (error == ENOLCK) {
3142 if (called_from_renewthread != 0) {
3144 * Return EIO so the flush will be retried
3150 if (newnfs_sigintr(nmp, td)) {
3154 if (slpflag == PCATCH) {
3160 if ((bp->b_flags & B_DELWRI) == 0)
3161 panic("nfs_fsync: not dirty");
3162 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3168 bp->b_flags |= B_ASYNC;
3170 if (newnfs_sigintr(nmp, td)) {
3181 if (waitfor == MNT_WAIT) {
3182 while (bo->bo_numoutput) {
3183 error = bufobj_wwait(bo, slpflag, slptimeo);
3186 if (called_from_renewthread != 0) {
3188 * Return EIO so that the flush will be
3194 error = newnfs_sigintr(nmp, td);
3197 if (slpflag == PCATCH) {
3204 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3209 * Wait for all the async IO requests to drain
3213 while (np->n_directio_asyncwr > 0) {
3214 np->n_flag |= NFSYNCWAIT;
3215 error = newnfs_msleep(td, &np->n_directio_asyncwr,
3216 &np->n_mtx, slpflag | (PRIBIO + 1),
3219 if (newnfs_sigintr(nmp, td)) {
3229 if (NFSHASPNFS(nmp)) {
3230 nfscl_layoutcommit(vp, td);
3232 * Invalidate the attribute cache, since writes to a DS
3233 * won't update the size attribute.
3236 np->n_attrstamp = 0;
3239 if (np->n_flag & NWRITEERR) {
3240 error = np->n_error;
3241 np->n_flag &= ~NWRITEERR;
3243 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3244 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3245 np->n_flag &= ~NMODIFIED;
3248 if (bvec != NULL && bvec != bvec_on_stack)
3250 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3251 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3252 np->n_directio_asyncwr != 0)) {
3254 /* try, try again... */
3261 vn_printf(vp, "ncl_flush failed");
3262 error = called_from_renewthread != 0 ? EIO : EBUSY;
3267 np->n_localmodtime = ts;
3274 * NFS advisory byte-level locks.
3277 nfs_advlock(struct vop_advlock_args *ap)
3279 struct vnode *vp = ap->a_vp;
3281 struct nfsnode *np = VTONFS(ap->a_vp);
3282 struct proc *p = (struct proc *)ap->a_id;
3283 struct thread *td = curthread; /* XXX */
3287 struct nfsmount *nmp;
3289 error = NFSVOPLOCK(vp, LK_SHARED);
3292 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3293 if (vp->v_type != VREG) {
3297 if ((ap->a_flags & F_POSIX) != 0)
3300 cred = td->td_ucred;
3301 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3302 if (VN_IS_DOOMED(vp)) {
3308 * If this is unlocking a write locked region, flush and
3309 * commit them before unlocking. This is required by
3310 * RFC3530 Sec. 9.3.2.
3312 if (ap->a_op == F_UNLCK &&
3313 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3315 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3318 * Mark NFS node as might have acquired a lock.
3319 * This is separate from NHASBEENLOCKED, because it must
3320 * be done before the nfsrpc_advlock() call, which might
3321 * add a nfscllock structure to the client state.
3322 * It is used to check for the case where a nfscllock
3323 * state structure cannot exist for the file.
3324 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3326 nmp = VFSTONFS(vp->v_mount);
3327 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3329 np->n_flag |= NMIGHTBELOCKED;
3334 * Loop around doing the lock op, while a blocking lock
3335 * must wait for the lock op to succeed.
3338 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3339 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3340 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3341 ap->a_op == F_SETLK) {
3343 error = nfs_catnap(PZERO | PCATCH, ret,
3347 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3348 if (VN_IS_DOOMED(vp)) {
3353 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3354 ap->a_op == F_SETLK);
3355 if (ret == NFSERR_DENIED) {
3358 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3361 } else if (ret != 0) {
3367 * Now, if we just got a lock, invalidate data in the buffer
3368 * cache, as required, so that the coherency conforms with
3369 * RFC3530 Sec. 9.3.2.
3371 if (ap->a_op == F_SETLK) {
3372 if ((np->n_flag & NMODIFIED) == 0) {
3373 np->n_attrstamp = 0;
3374 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3375 ret = VOP_GETATTR(vp, &va, cred);
3377 if ((np->n_flag & NMODIFIED) || ret ||
3378 np->n_change != va.va_filerev) {
3379 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3380 np->n_attrstamp = 0;
3381 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3382 ret = VOP_GETATTR(vp, &va, cred);
3384 np->n_mtime = va.va_mtime;
3385 np->n_change = va.va_filerev;
3388 /* Mark that a file lock has been acquired. */
3390 np->n_flag |= NHASBEENLOCKED;
3393 } else if (!NFS_ISV4(vp)) {
3394 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3395 size = VTONFS(vp)->n_size;
3397 error = lf_advlock(ap, &(vp->v_lockf), size);
3399 if (nfs_advlock_p != NULL)
3400 error = nfs_advlock_p(ap);
3406 if (error == 0 && ap->a_op == F_SETLK) {
3407 error = NFSVOPLOCK(vp, LK_SHARED);
3409 /* Mark that a file lock has been acquired. */
3411 np->n_flag |= NHASBEENLOCKED;
3425 * NFS advisory byte-level locks.
3428 nfs_advlockasync(struct vop_advlockasync_args *ap)
3430 struct vnode *vp = ap->a_vp;
3435 return (EOPNOTSUPP);
3436 error = NFSVOPLOCK(vp, LK_SHARED);
3439 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3440 size = VTONFS(vp)->n_size;
3442 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3451 * Print out the contents of an nfsnode.
3454 nfs_print(struct vop_print_args *ap)
3456 struct vnode *vp = ap->a_vp;
3457 struct nfsnode *np = VTONFS(vp);
3459 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3460 (uintmax_t)np->n_vattr.na_fsid);
3461 if (vp->v_type == VFIFO)
3468 * This is the "real" nfs::bwrite(struct buf*).
3469 * We set B_CACHE if this is a VMIO buffer.
3472 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3474 int oldflags, rtval;
3476 if (bp->b_flags & B_INVAL) {
3481 oldflags = bp->b_flags;
3482 bp->b_flags |= B_CACHE;
3485 * Undirty the bp. We will redirty it later if the I/O fails.
3488 bp->b_flags &= ~B_DONE;
3489 bp->b_ioflags &= ~BIO_ERROR;
3490 bp->b_iocmd = BIO_WRITE;
3492 bufobj_wref(bp->b_bufobj);
3493 curthread->td_ru.ru_oublock++;
3496 * Note: to avoid loopback deadlocks, we do not
3497 * assign b_runningbufspace.
3499 vfs_busy_pages(bp, 1);
3502 bp->b_iooffset = dbtob(bp->b_blkno);
3505 if ((oldflags & B_ASYNC) != 0)
3508 rtval = bufwait(bp);
3509 if (oldflags & B_DELWRI)
3516 * nfs special file access vnode op.
3517 * Essentially just get vattr and then imitate iaccess() since the device is
3518 * local to the client.
3521 nfsspec_access(struct vop_access_args *ap)
3524 struct ucred *cred = ap->a_cred;
3525 struct vnode *vp = ap->a_vp;
3526 accmode_t accmode = ap->a_accmode;
3531 * Disallow write attempts on filesystems mounted read-only;
3532 * unless the file is a socket, fifo, or a block or character
3533 * device resident on the filesystem.
3535 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3536 switch (vp->v_type) {
3546 error = VOP_GETATTR(vp, vap, cred);
3549 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3556 * Read wrapper for fifos.
3559 nfsfifo_read(struct vop_read_args *ap)
3561 struct nfsnode *np = VTONFS(ap->a_vp);
3569 vfs_timestamp(&np->n_atim);
3571 error = fifo_specops.vop_read(ap);
3576 * Write wrapper for fifos.
3579 nfsfifo_write(struct vop_write_args *ap)
3581 struct nfsnode *np = VTONFS(ap->a_vp);
3588 vfs_timestamp(&np->n_mtim);
3590 return(fifo_specops.vop_write(ap));
3594 * Close wrapper for fifos.
3596 * Update the times on the nfsnode then do fifo close.
3599 nfsfifo_close(struct vop_close_args *ap)
3601 struct vnode *vp = ap->a_vp;
3602 struct nfsnode *np = VTONFS(vp);
3607 if (np->n_flag & (NACC | NUPD)) {
3609 if (np->n_flag & NACC)
3611 if (np->n_flag & NUPD)
3614 if (vrefcnt(vp) == 1 &&
3615 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3617 if (np->n_flag & NACC)
3618 vattr.va_atime = np->n_atim;
3619 if (np->n_flag & NUPD)
3620 vattr.va_mtime = np->n_mtim;
3622 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3628 return (fifo_specops.vop_close(ap));
3632 * Just call ncl_writebp() with the force argument set to 1.
3634 * NOTE: B_DONE may or may not be set in a_bp on call.
3637 nfs_bwrite(struct buf *bp)
3640 return (ncl_writebp(bp, 1, curthread));
3643 struct buf_ops buf_ops_newnfs = {
3644 .bop_name = "buf_ops_nfs",
3645 .bop_write = nfs_bwrite,
3646 .bop_strategy = bufstrategy,
3647 .bop_sync = bufsync,
3648 .bop_bdflush = bufbdflush,
3652 nfs_getacl(struct vop_getacl_args *ap)
3656 if (ap->a_type != ACL_TYPE_NFS4)
3657 return (EOPNOTSUPP);
3658 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3659 if (error > NFSERR_STALE) {
3660 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3667 nfs_setacl(struct vop_setacl_args *ap)
3671 if (ap->a_type != ACL_TYPE_NFS4)
3672 return (EOPNOTSUPP);
3673 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp);
3674 if (error > NFSERR_STALE) {
3675 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3682 * VOP_ADVISE for NFS.
3683 * Just return 0 for any errors, since it is just a hint.
3686 nfs_advise(struct vop_advise_args *ap)
3688 struct thread *td = curthread;
3689 struct nfsmount *nmp;
3694 * First do vop_stdadvise() to handle the buffer cache.
3696 error = vop_stdadvise(ap);
3699 if (ap->a_start < 0 || ap->a_end < 0)
3701 if (ap->a_end == OFF_MAX)
3703 else if (ap->a_end < ap->a_start)
3706 len = ap->a_end - ap->a_start + 1;
3707 nmp = VFSTONFS(ap->a_vp->v_mount);
3708 mtx_lock(&nmp->nm_mtx);
3709 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3710 (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3711 0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3712 mtx_unlock(&nmp->nm_mtx);
3715 mtx_unlock(&nmp->nm_mtx);
3716 error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3718 if (error == NFSERR_NOTSUPP) {
3719 mtx_lock(&nmp->nm_mtx);
3720 nmp->nm_privflag |= NFSMNTP_NOADVISE;
3721 mtx_unlock(&nmp->nm_mtx);
3730 nfs_allocate(struct vop_allocate_args *ap)
3732 struct vnode *vp = ap->a_vp;
3733 struct thread *td = curthread;
3734 struct nfsvattr nfsva;
3735 struct nfsmount *nmp;
3738 int attrflag, error, ret;
3743 nmp = VFSTONFS(vp->v_mount);
3745 mtx_lock(&nmp->nm_mtx);
3746 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3747 (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3748 mtx_unlock(&nmp->nm_mtx);
3750 if ((uint64_t)alen > nfs_maxalloclen)
3751 alen = nfs_maxalloclen;
3753 /* Check the file size limit. */
3754 io.uio_offset = *ap->a_offset;
3755 io.uio_resid = alen;
3756 error = vn_rlimit_fsize(vp, &io, td);
3759 * Flush first to ensure that the allocate adds to the
3760 * file's allocation on the server.
3763 error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3765 error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3766 &nfsva, &attrflag, ap->a_cred, td);
3768 *ap->a_offset += alen;
3772 np->n_localmodtime = ts;
3774 } else if (error == NFSERR_NOTSUPP) {
3775 mtx_lock(&nmp->nm_mtx);
3776 nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3777 mtx_unlock(&nmp->nm_mtx);
3781 mtx_unlock(&nmp->nm_mtx);
3784 if (attrflag != 0) {
3785 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3786 if (error == 0 && ret != 0)
3790 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3795 * nfs deallocate call
3798 nfs_deallocate(struct vop_deallocate_args *ap)
3800 struct vnode *vp = ap->a_vp;
3801 struct thread *td = curthread;
3802 struct nfsvattr nfsva;
3803 struct nfsmount *nmp;
3806 int attrflag, error, ret;
3812 nmp = VFSTONFS(vp->v_mount);
3814 mtx_lock(&nmp->nm_mtx);
3815 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3816 (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3817 mtx_unlock(&nmp->nm_mtx);
3818 tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3819 NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3820 (intmax_t)*ap->a_offset, (intmax_t)tlen,
3821 (uintmax_t)nmp->nm_maxfilesize);
3822 if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3823 /* Avoid EFBIG error return from the NFSv4.2 server. */
3828 if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3829 tlen = nmp->nm_maxfilesize - *ap->a_offset;
3830 if ((uint64_t)*ap->a_offset < np->n_size) {
3831 /* Limit the len to nfs_maxalloclen before EOF. */
3832 mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3833 if ((uint64_t)mlen > nfs_maxalloclen) {
3834 NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3835 tlen = nfs_maxalloclen;
3840 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3842 vnode_pager_purge_range(vp, *ap->a_offset,
3843 *ap->a_offset + tlen);
3844 error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3845 &nfsva, &attrflag, ap->a_cred, td);
3846 NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3849 NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3850 attrflag, (uintmax_t)nfsva.na_size);
3853 np->n_localmodtime = ts;
3855 if (attrflag != 0) {
3856 if ((uint64_t)*ap->a_offset < nfsva.na_size)
3857 *ap->a_offset += omin((off_t)
3858 nfsva.na_size - *ap->a_offset,
3861 if (clipped && tlen < *ap->a_len)
3865 } else if (error == NFSERR_NOTSUPP) {
3866 mtx_lock(&nmp->nm_mtx);
3867 nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3868 mtx_unlock(&nmp->nm_mtx);
3871 mtx_unlock(&nmp->nm_mtx);
3875 * If the NFS server cannot perform the Deallocate operation, just call
3876 * vop_stddeallocate() to perform it.
3878 if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
3879 error = vop_stddeallocate(ap);
3880 NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
3882 if (attrflag != 0) {
3883 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
3884 if (error == 0 && ret != 0)
3888 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3893 * nfs copy_file_range call
3896 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
3898 struct vnode *invp = ap->a_invp;
3899 struct vnode *outvp = ap->a_outvp;
3901 struct nfsvattr innfsva, outnfsva;
3904 struct nfsmount *nmp;
3906 int error, inattrflag, outattrflag, ret, ret2;
3907 off_t inoff, outoff;
3908 bool consecutive, must_commit, tryoutcred;
3911 nmp = VFSTONFS(invp->v_mount);
3912 mtx_lock(&nmp->nm_mtx);
3913 /* NFSv4.2 Copy is not permitted for infile == outfile. */
3914 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3915 (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0 || invp == outvp) {
3916 mtx_unlock(&nmp->nm_mtx);
3917 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
3918 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
3919 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
3922 mtx_unlock(&nmp->nm_mtx);
3924 /* Lock both vnodes, avoiding risk of deadlock. */
3927 error = vn_start_write(outvp, &mp, V_WAIT);
3929 error = vn_lock(outvp, LK_EXCLUSIVE);
3931 error = vn_lock(invp, LK_SHARED | LK_NOWAIT);
3936 vn_finished_write(mp);
3938 error = vn_lock(invp, LK_SHARED);
3944 vn_finished_write(mp);
3945 } while (error == 0);
3950 * Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
3952 io.uio_offset = *ap->a_outoffp;
3953 io.uio_resid = *ap->a_lenp;
3954 error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
3957 * Flush the input file so that the data is up to date before
3958 * the copy. Flush writes for the output file so that they
3959 * do not overwrite the data copied to the output file by the Copy.
3960 * Set the commit argument for both flushes so that the data is on
3961 * stable storage before the Copy RPC. This is done in case the
3962 * server reboots during the Copy and needs to be redone.
3965 error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
3967 error = ncl_flush(outvp, MNT_WAIT, curthread, 1, 0);
3969 /* Do the actual NFSv4.2 RPC. */
3971 mtx_lock(&nmp->nm_mtx);
3972 if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
3975 consecutive = false;
3976 mtx_unlock(&nmp->nm_mtx);
3977 inoff = *ap->a_inoffp;
3978 outoff = *ap->a_outoffp;
3980 must_commit = false;
3982 vap = &VTONFS(invp)->n_vattr.na_vattr;
3983 error = VOP_GETATTR(invp, vap, ap->a_incred);
3986 * Clip "len" at va_size so that RFC compliant servers
3987 * will not reply NFSERR_INVAL.
3988 * Setting "len == 0" for the RPC would be preferred,
3989 * but some Linux servers do not support that.
3991 if (inoff >= vap->va_size)
3992 *ap->a_lenp = len = 0;
3993 else if (inoff + len > vap->va_size)
3994 *ap->a_lenp = len = vap->va_size - inoff;
4000 * len will be set to 0 upon a successful Copy RPC.
4001 * As such, this only loops when the Copy RPC needs to be retried.
4003 while (len > 0 && error == 0) {
4004 inattrflag = outattrflag = 0;
4007 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4008 outvp, ap->a_outoffp, &len2, ap->a_flags,
4009 &inattrflag, &innfsva, &outattrflag, &outnfsva,
4010 ap->a_outcred, consecutive, &must_commit);
4012 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
4013 outvp, ap->a_outoffp, &len2, ap->a_flags,
4014 &inattrflag, &innfsva, &outattrflag, &outnfsva,
4015 ap->a_incred, consecutive, &must_commit);
4016 if (inattrflag != 0)
4017 ret = nfscl_loadattrcache(&invp, &innfsva, NULL, 0, 1);
4018 if (outattrflag != 0)
4019 ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
4022 if (consecutive == false) {
4024 mtx_lock(&nmp->nm_mtx);
4026 NFSMNTP_NOCONSECUTIVE;
4027 mtx_unlock(&nmp->nm_mtx);
4029 error = NFSERR_OFFLOADNOREQS;
4033 if (len2 > 0 && must_commit && error == 0)
4034 error = ncl_commit(outvp, outoff, *ap->a_lenp,
4035 ap->a_outcred, curthread);
4036 if (error == 0 && ret != 0)
4038 if (error == 0 && ret2 != 0)
4040 } else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4042 * Try consecutive == false, which is ok only if all
4044 * If only some bytes were copied when consecutive
4045 * is false, there is no way to know which bytes
4046 * still need to be written.
4048 consecutive = false;
4050 } else if (error == NFSERR_ACCES && tryoutcred) {
4051 /* Try again with incred. */
4055 if (error == NFSERR_STALEWRITEVERF) {
4057 * Server rebooted, so do it all again.
4059 *ap->a_inoffp = inoff;
4060 *ap->a_outoffp = outoff;
4062 must_commit = false;
4069 vn_finished_write(mp);
4070 if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4071 error == NFSERR_ACCES) {
4073 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4074 * use a_incred for the read and a_outcred for the write, so
4075 * try this for NFSERR_ACCES failures for the Copy.
4076 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4077 * never succeed, so disable it.
4079 if (error != NFSERR_ACCES) {
4080 /* Can never do Copy on this mount. */
4081 mtx_lock(&nmp->nm_mtx);
4082 nmp->nm_privflag |= NFSMNTP_NOCOPY;
4083 mtx_unlock(&nmp->nm_mtx);
4085 *ap->a_inoffp = inoff;
4086 *ap->a_outoffp = outoff;
4087 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4088 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4089 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4090 } else if (error != 0)
4094 error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4102 nfs_ioctl(struct vop_ioctl_args *ap)
4104 struct vnode *vp = ap->a_vp;
4105 struct nfsvattr nfsva;
4106 struct nfsmount *nmp;
4107 int attrflag, content, error, ret;
4108 bool eof = false; /* shut up compiler. */
4110 if (vp->v_type != VREG)
4112 nmp = VFSTONFS(vp->v_mount);
4113 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4114 error = vop_stdioctl(ap);
4118 /* Do the actual NFSv4.2 RPC. */
4119 switch (ap->a_command) {
4121 content = NFSV4CONTENT_DATA;
4124 content = NFSV4CONTENT_HOLE;
4130 error = vn_lock(vp, LK_SHARED);
4134 if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4138 * Flush all writes, so that the server is up to date.
4139 * Although a Commit is not required, the commit argument
4140 * is set so that, for a pNFS File/Flexible File Layout
4141 * server, the LayoutCommit will be done to ensure the file
4142 * size is up to date on the Metadata Server.
4144 error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4146 error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4147 content, ap->a_cred, &nfsva, &attrflag);
4148 /* If at eof for FIOSEEKDATA, return ENXIO. */
4149 if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4152 if (attrflag != 0) {
4153 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4154 if (error == 0 && ret != 0)
4165 * nfs getextattr call
4168 nfs_getextattr(struct vop_getextattr_args *ap)
4170 struct vnode *vp = ap->a_vp;
4171 struct nfsmount *nmp;
4173 struct thread *td = ap->a_td;
4174 struct nfsvattr nfsva;
4176 int attrflag, error, ret;
4178 nmp = VFSTONFS(vp->v_mount);
4179 mtx_lock(&nmp->nm_mtx);
4180 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4181 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4182 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4183 mtx_unlock(&nmp->nm_mtx);
4184 return (EOPNOTSUPP);
4186 mtx_unlock(&nmp->nm_mtx);
4190 cred = td->td_ucred;
4191 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4193 error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4194 &attrflag, cred, td);
4195 if (attrflag != 0) {
4196 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4197 if (error == 0 && ret != 0)
4200 if (error == 0 && ap->a_size != NULL)
4204 case NFSERR_NOTSUPP:
4205 case NFSERR_OPILLEGAL:
4206 mtx_lock(&nmp->nm_mtx);
4207 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4208 mtx_unlock(&nmp->nm_mtx);
4211 case NFSERR_NOXATTR:
4212 case NFSERR_XATTR2BIG:
4216 error = nfscl_maperr(td, error, 0, 0);
4223 * nfs setextattr call
4226 nfs_setextattr(struct vop_setextattr_args *ap)
4228 struct vnode *vp = ap->a_vp;
4229 struct nfsmount *nmp;
4231 struct thread *td = ap->a_td;
4232 struct nfsvattr nfsva;
4233 int attrflag, error, ret;
4235 nmp = VFSTONFS(vp->v_mount);
4236 mtx_lock(&nmp->nm_mtx);
4237 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4238 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4239 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4240 mtx_unlock(&nmp->nm_mtx);
4241 return (EOPNOTSUPP);
4243 mtx_unlock(&nmp->nm_mtx);
4245 if (ap->a_uio->uio_resid < 0)
4249 cred = td->td_ucred;
4250 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4252 error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4253 &attrflag, cred, td);
4254 if (attrflag != 0) {
4255 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4256 if (error == 0 && ret != 0)
4261 case NFSERR_NOTSUPP:
4262 case NFSERR_OPILLEGAL:
4263 mtx_lock(&nmp->nm_mtx);
4264 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4265 mtx_unlock(&nmp->nm_mtx);
4268 case NFSERR_NOXATTR:
4269 case NFSERR_XATTR2BIG:
4273 error = nfscl_maperr(td, error, 0, 0);
4280 * nfs listextattr call
4283 nfs_listextattr(struct vop_listextattr_args *ap)
4285 struct vnode *vp = ap->a_vp;
4286 struct nfsmount *nmp;
4288 struct thread *td = ap->a_td;
4289 struct nfsvattr nfsva;
4292 int attrflag, error, ret;
4295 nmp = VFSTONFS(vp->v_mount);
4296 mtx_lock(&nmp->nm_mtx);
4297 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4298 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4299 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4300 mtx_unlock(&nmp->nm_mtx);
4301 return (EOPNOTSUPP);
4303 mtx_unlock(&nmp->nm_mtx);
4307 cred = td->td_ucred;
4309 /* Loop around doing List Extended Attribute RPCs. */
4314 while (!eof && error == 0) {
4315 len = nmp->nm_rsize;
4317 error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4318 &nfsva, &attrflag, cred, td);
4319 if (attrflag != 0) {
4320 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4321 if (error == 0 && ret != 0)
4326 if (len2 > SSIZE_MAX)
4330 if (error == 0 && ap->a_size != NULL)
4334 case NFSERR_NOTSUPP:
4335 case NFSERR_OPILLEGAL:
4336 mtx_lock(&nmp->nm_mtx);
4337 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4338 mtx_unlock(&nmp->nm_mtx);
4341 case NFSERR_NOXATTR:
4342 case NFSERR_XATTR2BIG:
4346 error = nfscl_maperr(td, error, 0, 0);
4353 * nfs setextattr call
4356 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4358 struct vnode *vp = ap->a_vp;
4359 struct nfsmount *nmp;
4360 struct nfsvattr nfsva;
4361 int attrflag, error, ret;
4363 nmp = VFSTONFS(vp->v_mount);
4364 mtx_lock(&nmp->nm_mtx);
4365 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4366 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4367 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4368 mtx_unlock(&nmp->nm_mtx);
4369 return (EOPNOTSUPP);
4371 mtx_unlock(&nmp->nm_mtx);
4373 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4375 error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4377 if (attrflag != 0) {
4378 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4379 if (error == 0 && ret != 0)
4384 case NFSERR_NOTSUPP:
4385 case NFSERR_OPILLEGAL:
4386 mtx_lock(&nmp->nm_mtx);
4387 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4388 mtx_unlock(&nmp->nm_mtx);
4391 case NFSERR_NOXATTR:
4392 case NFSERR_XATTR2BIG:
4396 error = nfscl_maperr(ap->a_td, error, 0, 0);
4403 * Return POSIX pathconf information applicable to nfs filesystems.
4406 nfs_pathconf(struct vop_pathconf_args *ap)
4408 struct nfsv3_pathconf pc;
4409 struct nfsvattr nfsva;
4410 struct vnode *vp = ap->a_vp;
4411 struct nfsmount *nmp;
4412 struct thread *td = curthread;
4415 int attrflag, error;
4417 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4418 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4419 ap->a_name == _PC_NO_TRUNC)) ||
4420 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
4422 * Since only the above 4 a_names are returned by the NFSv3
4423 * Pathconf RPC, there is no point in doing it for others.
4424 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4425 * be used for _PC_NFS4_ACL as well.
4427 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
4430 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1);
4435 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4438 pc.pc_linkmax = NFS_LINK_MAX;
4439 pc.pc_namemax = NFS_MAXNAMLEN;
4441 pc.pc_chownrestricted = 1;
4442 pc.pc_caseinsensitive = 0;
4443 pc.pc_casepreserving = 1;
4446 switch (ap->a_name) {
4449 *ap->a_retval = pc.pc_linkmax;
4451 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4455 *ap->a_retval = pc.pc_namemax;
4458 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4459 *ap->a_retval = PIPE_BUF;
4463 case _PC_CHOWN_RESTRICTED:
4464 *ap->a_retval = pc.pc_chownrestricted;
4467 *ap->a_retval = pc.pc_notrunc;
4470 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4471 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4476 case _PC_ACL_PATH_MAX:
4478 *ap->a_retval = ACL_MAX_ENTRIES;
4488 case _PC_ALLOC_SIZE_MIN:
4489 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4491 case _PC_FILESIZEBITS:
4497 case _PC_REC_INCR_XFER_SIZE:
4498 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4500 case _PC_REC_MAX_XFER_SIZE:
4501 *ap->a_retval = -1; /* means ``unlimited'' */
4503 case _PC_REC_MIN_XFER_SIZE:
4504 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4506 case _PC_REC_XFER_ALIGN:
4507 *ap->a_retval = PAGE_SIZE;
4509 case _PC_SYMLINK_MAX:
4510 *ap->a_retval = NFS_MAXPATHLEN;
4512 case _PC_MIN_HOLE_SIZE:
4513 /* Only some NFSv4.2 servers support Seek for Holes. */
4515 nmp = VFSTONFS(vp->v_mount);
4516 if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4518 * NFSv4.2 doesn't have an attribute for hole size,
4519 * so all we can do is see if the Seek operation is
4520 * supported and then use f_iosize as a "best guess".
4522 mtx_lock(&nmp->nm_mtx);
4523 if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4524 mtx_unlock(&nmp->nm_mtx);
4527 error = nfsrpc_seek(vp, &off, &eof,
4528 NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4531 nfscl_loadattrcache(&vp, &nfsva,
4533 mtx_lock(&nmp->nm_mtx);
4534 if (error == NFSERR_NOTSUPP)
4535 nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4537 nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4541 if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4542 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4543 mtx_unlock(&nmp->nm_mtx);
4548 error = vop_stdpathconf(ap);