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, 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 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
651 if (error == EINTR || error == EIO) {
653 (void) nfsrpc_close(vp, 0, ap->a_td);
658 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
659 if (vp->v_type == VDIR)
660 np->n_direofoffset = 0;
662 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
665 (void) nfsrpc_close(vp, 0, ap->a_td);
669 np->n_mtime = vattr.va_mtime;
671 np->n_change = vattr.va_filerev;
674 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
677 (void) nfsrpc_close(vp, 0, ap->a_td);
681 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
682 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
683 if (vp->v_type == VDIR)
684 np->n_direofoffset = 0;
686 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
687 if (error == EINTR || error == EIO) {
689 (void) nfsrpc_close(vp, 0, ap->a_td);
693 np->n_mtime = vattr.va_mtime;
695 np->n_change = vattr.va_filerev;
700 * If the object has >= 1 O_DIRECT active opens, we disable caching.
702 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
703 (vp->v_type == VREG)) {
704 if (np->n_directio_opens == 0) {
706 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
709 (void) nfsrpc_close(vp, 0, ap->a_td);
713 np->n_flag |= NNONCACHE;
715 np->n_directio_opens++;
718 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
719 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
720 np->n_flag |= NWRITEOPENED;
723 * If this is an open for writing, capture a reference to the
724 * credentials, so they can be used by ncl_putpages(). Using
725 * these write credentials is preferable to the credentials of
726 * whatever thread happens to be doing the VOP_PUTPAGES() since
727 * the write RPCs are less likely to fail with EACCES.
729 if ((fmode & FWRITE) != 0) {
730 cred = np->n_writecred;
731 np->n_writecred = crhold(ap->a_cred);
738 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
741 * If the text file has been mmap'd, flush any dirty pages to the
742 * buffer cache and then...
743 * Make sure all writes are pushed to the NFS server. If this is not
744 * done, the modify time of the file can change while the text
745 * file is being executed. This will cause the process that is
746 * executing the text file to be terminated.
748 if (vp->v_writecount <= -1) {
749 if ((obj = vp->v_object) != NULL &&
750 vm_object_mightbedirty(obj)) {
751 VM_OBJECT_WLOCK(obj);
752 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
753 VM_OBJECT_WUNLOCK(obj);
756 /* Now, flush the buffer cache. */
757 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
759 /* And, finally, make sure that n_mtime is up to date. */
762 np->n_mtime = np->n_vattr.na_mtime;
770 * What an NFS client should do upon close after writing is a debatable issue.
771 * Most NFS clients push delayed writes to the server upon close, basically for
773 * 1 - So that any write errors may be reported back to the client process
774 * doing the close system call. By far the two most likely errors are
775 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
776 * 2 - To put a worst case upper bound on cache inconsistency between
777 * multiple clients for the file.
778 * There is also a consistency problem for Version 2 of the protocol w.r.t.
779 * not being able to tell if other clients are writing a file concurrently,
780 * since there is no way of knowing if the changed modify time in the reply
781 * is only due to the write for this client.
782 * (NFS Version 3 provides weak cache consistency data in the reply that
783 * should be sufficient to detect and handle this case.)
785 * The current code does the following:
786 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
787 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
788 * or commit them (this satisfies 1 and 2 except for the
789 * case where the server crashes after this close but
790 * before the commit RPC, which is felt to be "good
791 * enough". Changing the last argument to ncl_flush() to
792 * a 1 would force a commit operation, if it is felt a
793 * commit is necessary now.
794 * for NFS Version 4 - flush the dirty buffers and commit them, if
795 * nfscl_mustflush() says this is necessary.
796 * It is necessary if there is no write delegation held,
797 * in order to satisfy open/close coherency.
798 * If the file isn't cached on local stable storage,
799 * it may be necessary in order to detect "out of space"
800 * errors from the server, if the write delegation
801 * issued by the server doesn't allow the file to grow.
805 nfs_close(struct vop_close_args *ap)
807 struct vnode *vp = ap->a_vp;
808 struct nfsnode *np = VTONFS(vp);
809 struct nfsvattr nfsva;
811 int error = 0, ret, localcred = 0;
812 int fmode = ap->a_fflag;
814 if (NFSCL_FORCEDISM(vp->v_mount))
817 * During shutdown, a_cred isn't valid, so just use root.
819 if (ap->a_cred == NOCRED) {
820 cred = newnfs_getcred();
825 if (vp->v_type == VREG) {
827 * Examine and clean dirty pages, regardless of NMODIFIED.
828 * This closes a major hole in close-to-open consistency.
829 * We want to push out all dirty pages (and buffers) on
830 * close, regardless of whether they were dirtied by
831 * mmap'ed writes or via write().
833 if (nfs_clean_pages_on_close && vp->v_object) {
834 VM_OBJECT_WLOCK(vp->v_object);
835 vm_object_page_clean(vp->v_object, 0, 0, 0);
836 VM_OBJECT_WUNLOCK(vp->v_object);
839 if (np->n_flag & NMODIFIED) {
843 * Under NFSv3 we have dirty buffers to dispose of. We
844 * must flush them to the NFS server. We have the option
845 * of waiting all the way through the commit rpc or just
846 * waiting for the initial write. The default is to only
847 * wait through the initial write so the data is in the
848 * server's cache, which is roughly similar to the state
849 * a standard disk subsystem leaves the file in on close().
851 * We cannot clear the NMODIFIED bit in np->n_flag due to
852 * potential races with other processes, and certainly
853 * cannot clear it if we don't commit.
854 * These races occur when there is no longer the old
855 * traditional vnode locking implemented for Vnode Ops.
857 int cm = newnfs_commit_on_close ? 1 : 0;
858 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
859 /* np->n_flag &= ~NMODIFIED; */
860 } else if (NFS_ISV4(vp)) {
861 if (nfscl_mustflush(vp) != 0) {
862 int cm = newnfs_commit_on_close ? 1 : 0;
863 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
866 * as above w.r.t races when clearing
868 * np->n_flag &= ~NMODIFIED;
872 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
877 * Invalidate the attribute cache in all cases.
878 * An open is going to fetch fresh attrs any way, other procs
879 * on this node that have file open will be forced to do an
880 * otw attr fetch, but this is safe.
881 * --> A user found that their RPC count dropped by 20% when
882 * this was commented out and I can't see any requirement
883 * for it, so I've disabled it when negative lookups are
884 * enabled. (What does this have to do with negative lookup
885 * caching? Well nothing, except it was reported by the
886 * same user that needed negative lookup caching and I wanted
887 * there to be a way to disable it to see if it
888 * is the cause of some caching/coherency issue that might
891 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
893 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
895 if (np->n_flag & NWRITEERR) {
896 np->n_flag &= ~NWRITEERR;
904 * Get attributes so "change" is up to date.
906 if (error == 0 && nfscl_mustflush(vp) != 0 &&
907 vp->v_type == VREG &&
908 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
909 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
912 np->n_change = nfsva.na_filerev;
913 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
921 ret = nfsrpc_close(vp, 0, ap->a_td);
925 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
928 if (newnfs_directio_enable)
929 KASSERT((np->n_directio_asyncwr == 0),
930 ("nfs_close: dirty unflushed (%d) directio buffers\n",
931 np->n_directio_asyncwr));
932 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
934 KASSERT((np->n_directio_opens > 0),
935 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
936 np->n_directio_opens--;
937 if (np->n_directio_opens == 0)
938 np->n_flag &= ~NNONCACHE;
947 * nfs getattr call from vfs.
950 nfs_getattr(struct vop_getattr_args *ap)
952 struct vnode *vp = ap->a_vp;
953 struct thread *td = curthread; /* XXX */
954 struct nfsnode *np = VTONFS(vp);
956 struct nfsvattr nfsva;
957 struct vattr *vap = ap->a_vap;
961 * Update local times for special files.
964 if (np->n_flag & (NACC | NUPD))
968 * First look in the cache.
970 if (ncl_getattrcache(vp, &vattr) == 0) {
971 ncl_copy_vattr(vap, &vattr);
974 * Get the local modify time for the case of a write
977 nfscl_deleggetmodtime(vp, &vap->va_mtime);
981 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
982 nfsaccess_cache_timeout > 0) {
983 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
984 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
985 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
986 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
990 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
992 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
995 * Get the local modify time for the case of a write
998 nfscl_deleggetmodtime(vp, &vap->va_mtime);
999 } else if (NFS_ISV4(vp)) {
1000 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1009 nfs_setattr(struct vop_setattr_args *ap)
1011 struct vnode *vp = ap->a_vp;
1012 struct nfsnode *np = VTONFS(vp);
1013 struct thread *td = curthread; /* XXX */
1014 struct vattr *vap = ap->a_vap;
1020 tsize = (u_quad_t)0;
1024 * Setting of flags and marking of atimes are not supported.
1026 if (vap->va_flags != VNOVAL)
1027 return (EOPNOTSUPP);
1030 * Disallow write attempts if the filesystem is mounted read-only.
1032 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1033 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1034 vap->va_mtime.tv_sec != VNOVAL ||
1035 vap->va_birthtime.tv_sec != VNOVAL ||
1036 vap->va_mode != (mode_t)VNOVAL) &&
1037 (vp->v_mount->mnt_flag & MNT_RDONLY))
1039 if (vap->va_size != VNOVAL) {
1040 switch (vp->v_type) {
1047 if (vap->va_mtime.tv_sec == VNOVAL &&
1048 vap->va_atime.tv_sec == VNOVAL &&
1049 vap->va_birthtime.tv_sec == VNOVAL &&
1050 vap->va_mode == (mode_t)VNOVAL &&
1051 vap->va_uid == (uid_t)VNOVAL &&
1052 vap->va_gid == (gid_t)VNOVAL)
1054 vap->va_size = VNOVAL;
1058 * Disallow write attempts if the filesystem is
1059 * mounted read-only.
1061 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1064 * We run vnode_pager_setsize() early (why?),
1065 * we must set np->n_size now to avoid vinvalbuf
1066 * V_SAVE races that might setsize a lower
1072 error = ncl_meta_setsize(vp, td, vap->va_size);
1074 if (np->n_flag & NMODIFIED) {
1077 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1080 vnode_pager_setsize(vp, tsize);
1084 * Call nfscl_delegmodtime() to set the modify time
1085 * locally, as required.
1087 nfscl_delegmodtime(vp);
1091 * np->n_size has already been set to vap->va_size
1092 * in ncl_meta_setsize(). We must set it again since
1093 * nfs_loadattrcache() could be called through
1094 * ncl_meta_setsize() and could modify np->n_size.
1097 np->n_vattr.na_size = np->n_size = vap->va_size;
1102 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1103 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1105 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1106 if (error == EINTR || error == EIO)
1111 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1112 if (vap->va_size != VNOVAL) {
1116 np->n_localmodtime = ts;
1120 np->n_size = np->n_vattr.na_size = tsize;
1121 vnode_pager_setsize(vp, tsize);
1129 * Do an nfs setattr rpc.
1132 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1135 struct nfsnode *np = VTONFS(vp);
1136 int error, ret, attrflag, i;
1137 struct nfsvattr nfsva;
1139 if (NFS_ISV34(vp)) {
1141 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1142 np->n_accesscache[i].stamp = 0;
1143 np->n_flag |= NDELEGMOD;
1145 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1147 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1150 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1154 if (error && NFS_ISV4(vp))
1155 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1160 * nfs lookup call, one step at a time...
1161 * First look in cache
1162 * If not found, unlock the directory nfsnode and do the rpc
1165 nfs_lookup(struct vop_lookup_args *ap)
1167 struct componentname *cnp = ap->a_cnp;
1168 struct vnode *dvp = ap->a_dvp;
1169 struct vnode **vpp = ap->a_vpp;
1170 struct mount *mp = dvp->v_mount;
1171 int flags = cnp->cn_flags;
1172 struct vnode *newvp;
1173 struct nfsmount *nmp;
1174 struct nfsnode *np, *newnp;
1175 int error = 0, attrflag, dattrflag, ltype, ncticks;
1176 struct thread *td = curthread;
1178 struct nfsvattr dnfsva, nfsva;
1180 struct timespec nctime, ts;
1184 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1185 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1187 if (dvp->v_type != VDIR)
1192 /* For NFSv4, wait until any remove is done. */
1194 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1195 np->n_flag |= NREMOVEWANT;
1196 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1200 error = vn_dir_check_exec(dvp, cnp);
1203 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1204 if (error > 0 && error != ENOENT)
1208 * Lookups of "." are special and always return the
1209 * current directory. cache_lookup() already handles
1210 * associated locking bookkeeping, etc.
1212 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1213 /* XXX: Is this really correct? */
1214 if (cnp->cn_nameiop != LOOKUP &&
1216 cnp->cn_flags |= SAVENAME;
1221 * We only accept a positive hit in the cache if the
1222 * change time of the file matches our cached copy.
1223 * Otherwise, we discard the cache entry and fallback
1224 * to doing a lookup RPC. We also only trust cache
1225 * entries for less than nm_nametimeo seconds.
1227 * To better handle stale file handles and attributes,
1228 * clear the attribute cache of this node if it is a
1229 * leaf component, part of an open() call, and not
1230 * locally modified before fetching the attributes.
1231 * This should allow stale file handles to be detected
1232 * here where we can fall back to a LOOKUP RPC to
1233 * recover rather than having nfs_open() detect the
1234 * stale file handle and failing open(2) with ESTALE.
1237 newnp = VTONFS(newvp);
1238 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1239 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1240 !(newnp->n_flag & NMODIFIED)) {
1242 newnp->n_attrstamp = 0;
1243 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1244 NFSUNLOCKNODE(newnp);
1246 if (nfscl_nodeleg(newvp, 0) == 0 ||
1247 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1248 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1249 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1250 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1251 if (cnp->cn_nameiop != LOOKUP &&
1253 cnp->cn_flags |= SAVENAME;
1262 } else if (error == ENOENT) {
1263 if (VN_IS_DOOMED(dvp))
1266 * We only accept a negative hit in the cache if the
1267 * modification time of the parent directory matches
1268 * the cached copy in the name cache entry.
1269 * Otherwise, we discard all of the negative cache
1270 * entries for this directory. We also only trust
1271 * negative cache entries for up to nm_negnametimeo
1274 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1275 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1276 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1277 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1280 cache_purge_negative(dvp);
1284 * If this an NFSv4.1/4.2 mount using the "oneopenown" mount
1285 * option, it is possible to do the Open operation in the same
1286 * compound as Lookup, so long as delegations are not being
1287 * issued. This saves doing a separate RPC for Open.
1291 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp) &&
1292 (nmp->nm_privflag & NFSMNTP_DELEGISSUED) == 0 &&
1293 (!NFSMNT_RDONLY(mp) || (flags & OPENWRITE) == 0) &&
1294 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN)) {
1295 if ((flags & OPENREAD) != 0)
1296 openmode |= NFSV4OPEN_ACCESSREAD;
1297 if ((flags & OPENWRITE) != 0)
1298 openmode |= NFSV4OPEN_ACCESSWRITE;
1303 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1305 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1306 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1309 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1311 if (newvp != NULLVP) {
1316 if (error != ENOENT) {
1318 error = nfscl_maperr(td, error, (uid_t)0,
1323 /* The requested file was not found. */
1324 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1325 (flags & ISLASTCN)) {
1327 * XXX: UFS does a full VOP_ACCESS(dvp,
1328 * VWRITE) here instead of just checking
1331 if (mp->mnt_flag & MNT_RDONLY)
1333 cnp->cn_flags |= SAVENAME;
1334 return (EJUSTRETURN);
1337 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1339 * Cache the modification time of the parent
1340 * directory from the post-op attributes in
1341 * the name cache entry. The negative cache
1342 * entry will be ignored once the directory
1343 * has changed. Don't bother adding the entry
1344 * if the directory has already changed.
1347 if (timespeccmp(&np->n_vattr.na_mtime,
1348 &dnfsva.na_mtime, ==)) {
1350 cache_enter_time(dvp, NULL, cnp,
1351 &dnfsva.na_mtime, NULL);
1359 * Handle RENAME case...
1361 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1362 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1363 free(nfhp, M_NFSFH);
1366 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1372 * If n_localmodtime >= time before RPC, then
1373 * a file modification operation, such as
1374 * VOP_SETATTR() of size, has occurred while
1375 * the Lookup RPC and acquisition of the vnode
1376 * happened. As such, the attributes might
1377 * be stale, with possibly an incorrect size.
1380 if (timespecisset(&np->n_localmodtime) &&
1381 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1382 NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1383 "stale attributes\n");
1388 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1391 cnp->cn_flags |= SAVENAME;
1395 if (flags & ISDOTDOT) {
1396 ltype = NFSVOPISLOCKED(dvp);
1397 error = vfs_busy(mp, MBF_NOWAIT);
1401 error = vfs_busy(mp, 0);
1402 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1404 if (error == 0 && VN_IS_DOOMED(dvp)) {
1412 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1418 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1419 if (VN_IS_DOOMED(dvp)) {
1431 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1433 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1434 free(nfhp, M_NFSFH);
1438 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1441 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1447 * If n_localmodtime >= time before RPC, then
1448 * a file modification operation, such as
1449 * VOP_SETATTR() of size, has occurred while
1450 * the Lookup RPC and acquisition of the vnode
1451 * happened. As such, the attributes might
1452 * be stale, with possibly an incorrect size.
1455 if (timespecisset(&np->n_localmodtime) &&
1456 timespeccmp(&np->n_localmodtime, &ts, >=)) {
1457 NFSCL_DEBUG(4, "nfs_lookup: localmod "
1458 "stale attributes\n");
1463 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1465 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1466 !(np->n_flag & NMODIFIED)) {
1468 * Flush the attribute cache when opening a
1469 * leaf node to ensure that fresh attributes
1470 * are fetched in nfs_open() since we did not
1471 * fetch attributes from the LOOKUP reply.
1474 np->n_attrstamp = 0;
1475 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1479 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1480 cnp->cn_flags |= SAVENAME;
1481 if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1482 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1483 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1484 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1485 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1492 * Just call ncl_bioread() to do the work.
1495 nfs_read(struct vop_read_args *ap)
1497 struct vnode *vp = ap->a_vp;
1499 switch (vp->v_type) {
1501 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1505 return (EOPNOTSUPP);
1513 nfs_readlink(struct vop_readlink_args *ap)
1515 struct vnode *vp = ap->a_vp;
1517 if (vp->v_type != VLNK)
1519 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1523 * Do a readlink rpc.
1524 * Called by ncl_doio() from below the buffer cache.
1527 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1529 int error, ret, attrflag;
1530 struct nfsvattr nfsva;
1532 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1535 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1539 if (error && NFS_ISV4(vp))
1540 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1549 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1551 int error, ret, attrflag;
1552 struct nfsvattr nfsva;
1553 struct nfsmount *nmp;
1555 nmp = VFSTONFS(vp->v_mount);
1558 if (NFSHASPNFS(nmp))
1559 error = nfscl_doiods(vp, uiop, NULL, NULL,
1560 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1561 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1563 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1566 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1570 if (error && NFS_ISV4(vp))
1571 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1579 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1580 int *iomode, int *must_commit, int called_from_strategy)
1582 struct nfsvattr nfsva;
1583 int error, attrflag, ret;
1584 struct nfsmount *nmp;
1586 nmp = VFSTONFS(vp->v_mount);
1589 if (NFSHASPNFS(nmp))
1590 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1591 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1592 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1594 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1595 uiop->uio_td, &nfsva, &attrflag, NULL,
1596 called_from_strategy);
1598 if (VTONFS(vp)->n_flag & ND_NFSV4)
1599 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1602 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1608 *iomode = NFSWRITE_FILESYNC;
1609 if (error && NFS_ISV4(vp))
1610 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1616 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1617 * mode set to specify the file type and the size field for rdev.
1620 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1623 struct nfsvattr nfsva, dnfsva;
1624 struct vnode *newvp = NULL;
1625 struct nfsnode *np = NULL, *dnp;
1628 int error = 0, attrflag, dattrflag;
1631 if (vap->va_type == VCHR || vap->va_type == VBLK)
1632 rdev = vap->va_rdev;
1633 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1636 return (EOPNOTSUPP);
1637 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1639 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1640 rdev, vap->va_type, cnp->cn_cred, curthread, &dnfsva,
1641 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1644 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1645 cnp->cn_namelen, cnp->cn_cred, curthread,
1646 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1649 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1650 curthread, &np, NULL, LK_EXCLUSIVE);
1653 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1656 if (attrflag != 0) {
1657 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1665 } else if (NFS_ISV4(dvp)) {
1666 error = nfscl_maperr(curthread, error, vap->va_uid,
1671 dnp->n_flag |= NMODIFIED;
1673 dnp->n_attrstamp = 0;
1674 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1682 * just call nfs_mknodrpc() to do the work.
1686 nfs_mknod(struct vop_mknod_args *ap)
1688 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1691 static struct mtx nfs_cverf_mtx;
1692 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1698 static nfsquad_t cverf;
1700 static int cverf_initialized = 0;
1702 mtx_lock(&nfs_cverf_mtx);
1703 if (cverf_initialized == 0) {
1704 cverf.lval[0] = arc4random();
1705 cverf.lval[1] = arc4random();
1706 cverf_initialized = 1;
1710 mtx_unlock(&nfs_cverf_mtx);
1716 * nfs file create call
1719 nfs_create(struct vop_create_args *ap)
1721 struct vnode *dvp = ap->a_dvp;
1722 struct vattr *vap = ap->a_vap;
1723 struct componentname *cnp = ap->a_cnp;
1724 struct nfsnode *np = NULL, *dnp;
1725 struct vnode *newvp = NULL;
1726 struct nfsmount *nmp;
1727 struct nfsvattr dnfsva, nfsva;
1730 int error = 0, attrflag, dattrflag, fmode = 0;
1734 * Oops, not for me..
1736 if (vap->va_type == VSOCK)
1737 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1739 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1741 if (vap->va_vaflags & VA_EXCLUSIVE)
1744 nmp = VFSTONFS(dvp->v_mount);
1746 /* For NFSv4, wait until any remove is done. */
1748 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1749 dnp->n_flag |= NREMOVEWANT;
1750 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1754 cverf = nfs_get_cverf();
1755 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1756 vap, cverf, fmode, cnp->cn_cred, curthread, &dnfsva, &nfsva,
1757 &nfhp, &attrflag, &dattrflag, NULL);
1760 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1761 cnp->cn_namelen, cnp->cn_cred, curthread,
1762 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1765 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1766 curthread, &np, NULL, LK_EXCLUSIVE);
1769 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1773 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1774 curthread, &nfsva, NULL);
1776 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1780 if (newvp != NULL) {
1784 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1785 error == NFSERR_NOTSUPP) {
1789 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1790 if (nfscl_checksattr(vap, &nfsva)) {
1791 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1792 curthread, &nfsva, &attrflag, NULL);
1793 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1794 vap->va_gid != (gid_t)VNOVAL)) {
1795 /* try again without setting uid/gid */
1796 vap->va_uid = (uid_t)VNOVAL;
1797 vap->va_gid = (uid_t)VNOVAL;
1798 error = nfsrpc_setattr(newvp, vap, NULL,
1799 cnp->cn_cred, curthread, &nfsva,
1803 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1810 if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1812 cache_enter_time(dvp, newvp, cnp,
1813 &nfsva.na_ctime, NULL);
1815 printf("nfs_create: bogus NFS server returned "
1816 "the directory as the new file object\n");
1819 } else if (NFS_ISV4(dvp)) {
1820 error = nfscl_maperr(curthread, error, vap->va_uid,
1824 dnp->n_flag |= NMODIFIED;
1826 dnp->n_attrstamp = 0;
1827 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1834 * nfs file remove call
1835 * To try and make nfs semantics closer to ufs semantics, a file that has
1836 * other processes using the vnode is renamed instead of removed and then
1837 * removed later on the last close.
1838 * - If v_usecount > 1
1839 * If a rename is not already in the works
1840 * call nfs_sillyrename() to set it up
1845 nfs_remove(struct vop_remove_args *ap)
1847 struct vnode *vp = ap->a_vp;
1848 struct vnode *dvp = ap->a_dvp;
1849 struct componentname *cnp = ap->a_cnp;
1850 struct nfsnode *np = VTONFS(vp);
1854 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1855 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1856 if (vp->v_type == VDIR)
1858 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1859 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1860 vattr.va_nlink > 1)) {
1862 * Purge the name cache so that the chance of a lookup for
1863 * the name succeeding while the remove is in progress is
1864 * minimized. Without node locking it can still happen, such
1865 * that an I/O op returns ESTALE, but since you get this if
1866 * another host removes the file..
1870 * throw away biocache buffers, mainly to avoid
1871 * unnecessary delayed writes later.
1873 error = ncl_vinvalbuf(vp, 0, curthread, 1);
1874 if (error != EINTR && error != EIO)
1876 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1877 cnp->cn_namelen, cnp->cn_cred, curthread);
1879 * Kludge City: If the first reply to the remove rpc is lost..
1880 * the reply to the retransmitted request will be ENOENT
1881 * since the file was in fact removed
1882 * Therefore, we cheat and return success.
1884 if (error == ENOENT)
1886 } else if (!np->n_sillyrename)
1887 error = nfs_sillyrename(dvp, vp, cnp);
1889 np->n_attrstamp = 0;
1891 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1896 * nfs file remove rpc called from nfs_inactive
1899 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1902 * Make sure that the directory vnode is still valid.
1903 * XXX we should lock sp->s_dvp here.
1905 if (sp->s_dvp->v_type == VBAD)
1907 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1912 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1915 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1916 int namelen, struct ucred *cred, struct thread *td)
1918 struct nfsvattr dnfsva;
1919 struct nfsnode *dnp = VTONFS(dvp);
1920 int error = 0, dattrflag;
1923 dnp->n_flag |= NREMOVEINPROG;
1925 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1928 if ((dnp->n_flag & NREMOVEWANT)) {
1929 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1931 wakeup((caddr_t)dnp);
1933 dnp->n_flag &= ~NREMOVEINPROG;
1937 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1939 dnp->n_flag |= NMODIFIED;
1941 dnp->n_attrstamp = 0;
1942 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1945 if (error && NFS_ISV4(dvp))
1946 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1951 * nfs file rename call
1954 nfs_rename(struct vop_rename_args *ap)
1956 struct vnode *fvp = ap->a_fvp;
1957 struct vnode *tvp = ap->a_tvp;
1958 struct vnode *fdvp = ap->a_fdvp;
1959 struct vnode *tdvp = ap->a_tdvp;
1960 struct componentname *tcnp = ap->a_tcnp;
1961 struct componentname *fcnp = ap->a_fcnp;
1962 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1963 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1964 struct nfsv4node *newv4 = NULL;
1967 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1968 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1969 /* Check for cross-device rename */
1970 if ((fvp->v_mount != tdvp->v_mount) ||
1971 (tvp && (fvp->v_mount != tvp->v_mount))) {
1977 printf("nfs_rename: fvp == tvp (can't happen)\n");
1981 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1985 * We have to flush B_DELWRI data prior to renaming
1986 * the file. If we don't, the delayed-write buffers
1987 * can be flushed out later after the file has gone stale
1988 * under NFSV3. NFSV2 does not have this problem because
1989 * ( as far as I can tell ) it flushes dirty buffers more
1992 * Skip the rename operation if the fsync fails, this can happen
1993 * due to the server's volume being full, when we pushed out data
1994 * that was written back to our cache earlier. Not checking for
1995 * this condition can result in potential (silent) data loss.
1997 error = VOP_FSYNC(fvp, MNT_WAIT, curthread);
2000 error = VOP_FSYNC(tvp, MNT_WAIT, curthread);
2005 * If the tvp exists and is in use, sillyrename it before doing the
2006 * rename of the new file over it.
2007 * XXX Can't sillyrename a directory.
2009 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2010 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2015 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2016 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2019 if (error == 0 && NFS_ISV4(tdvp)) {
2021 * For NFSv4, check to see if it is the same name and
2022 * replace the name, if it is different.
2025 sizeof (struct nfsv4node) +
2026 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2027 M_NFSV4NODE, M_WAITOK);
2030 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2031 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2032 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2033 tcnp->cn_namelen) ||
2034 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2035 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2036 tdnp->n_fhp->nfh_len))) {
2038 { char nnn[100]; int nnnl;
2039 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
2040 bcopy(tcnp->cn_nameptr, nnn, nnnl);
2042 printf("ren replace=%s\n",nnn);
2045 free(fnp->n_v4, M_NFSV4NODE);
2048 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2049 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2050 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2051 tdnp->n_fhp->nfh_len);
2052 NFSBCOPY(tcnp->cn_nameptr,
2053 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2055 NFSUNLOCKNODE(tdnp);
2058 free(newv4, M_NFSV4NODE);
2061 if (fvp->v_type == VDIR) {
2062 if (tvp != NULL && tvp->v_type == VDIR)
2077 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2079 if (error == ENOENT)
2085 * nfs file rename rpc called from nfs_remove() above
2088 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2089 struct sillyrename *sp)
2092 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2093 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
2098 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2101 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2102 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2103 int tnamelen, struct ucred *cred, struct thread *td)
2105 struct nfsvattr fnfsva, tnfsva;
2106 struct nfsnode *fdnp = VTONFS(fdvp);
2107 struct nfsnode *tdnp = VTONFS(tdvp);
2108 int error = 0, fattrflag, tattrflag;
2110 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2111 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
2112 &tattrflag, NULL, NULL);
2114 fdnp->n_flag |= NMODIFIED;
2115 if (fattrflag != 0) {
2116 NFSUNLOCKNODE(fdnp);
2117 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
2119 fdnp->n_attrstamp = 0;
2120 NFSUNLOCKNODE(fdnp);
2121 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2124 tdnp->n_flag |= NMODIFIED;
2125 if (tattrflag != 0) {
2126 NFSUNLOCKNODE(tdnp);
2127 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
2129 tdnp->n_attrstamp = 0;
2130 NFSUNLOCKNODE(tdnp);
2131 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2133 if (error && NFS_ISV4(fdvp))
2134 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2139 * nfs hard link create call
2142 nfs_link(struct vop_link_args *ap)
2144 struct vnode *vp = ap->a_vp;
2145 struct vnode *tdvp = ap->a_tdvp;
2146 struct componentname *cnp = ap->a_cnp;
2147 struct nfsnode *np, *tdnp;
2148 struct nfsvattr nfsva, dnfsva;
2149 int error = 0, attrflag, dattrflag;
2152 * Push all writes to the server, so that the attribute cache
2153 * doesn't get "out of sync" with the server.
2154 * XXX There should be a better way!
2156 VOP_FSYNC(vp, MNT_WAIT, curthread);
2158 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2159 cnp->cn_cred, curthread, &dnfsva, &nfsva, &attrflag,
2161 tdnp = VTONFS(tdvp);
2163 tdnp->n_flag |= NMODIFIED;
2164 if (dattrflag != 0) {
2165 NFSUNLOCKNODE(tdnp);
2166 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
2168 tdnp->n_attrstamp = 0;
2169 NFSUNLOCKNODE(tdnp);
2170 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2173 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2177 np->n_attrstamp = 0;
2179 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2182 * If negative lookup caching is enabled, I might as well
2183 * add an entry for this node. Not necessary for correctness,
2184 * but if negative caching is enabled, then the system
2185 * must care about lookup caching hit rate, so...
2187 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2188 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2190 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2192 printf("nfs_link: bogus NFS server returned "
2193 "the directory as the new link\n");
2195 if (error && NFS_ISV4(vp))
2196 error = nfscl_maperr(curthread, error, (uid_t)0,
2202 * nfs symbolic link create call
2205 nfs_symlink(struct vop_symlink_args *ap)
2207 struct vnode *dvp = ap->a_dvp;
2208 struct vattr *vap = ap->a_vap;
2209 struct componentname *cnp = ap->a_cnp;
2210 struct nfsvattr nfsva, dnfsva;
2212 struct nfsnode *np = NULL, *dnp;
2213 struct vnode *newvp = NULL;
2214 int error = 0, attrflag, dattrflag, ret;
2216 vap->va_type = VLNK;
2217 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2218 ap->a_target, vap, cnp->cn_cred, curthread, &dnfsva,
2219 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2221 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2222 &np, NULL, LK_EXCLUSIVE);
2228 if (newvp != NULL) {
2230 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2232 } else if (!error) {
2234 * If we do not have an error and we could not extract the
2235 * newvp from the response due to the request being NFSv2, we
2236 * have to do a lookup in order to obtain a newvp to return.
2238 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2239 cnp->cn_cred, curthread, &np);
2247 error = nfscl_maperr(curthread, error,
2248 vap->va_uid, vap->va_gid);
2255 dnp->n_flag |= NMODIFIED;
2256 if (dattrflag != 0) {
2258 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2260 dnp->n_attrstamp = 0;
2262 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2265 * If negative lookup caching is enabled, I might as well
2266 * add an entry for this node. Not necessary for correctness,
2267 * but if negative caching is enabled, then the system
2268 * must care about lookup caching hit rate, so...
2270 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2271 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2273 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2276 printf("nfs_symlink: bogus NFS server returned "
2277 "the directory as the new file object\n");
2286 nfs_mkdir(struct vop_mkdir_args *ap)
2288 struct vnode *dvp = ap->a_dvp;
2289 struct vattr *vap = ap->a_vap;
2290 struct componentname *cnp = ap->a_cnp;
2291 struct nfsnode *np = NULL, *dnp;
2292 struct vnode *newvp = NULL;
2295 struct nfsvattr nfsva, dnfsva;
2296 int error = 0, attrflag, dattrflag, ret;
2298 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2300 vap->va_type = VDIR;
2301 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2302 vap, cnp->cn_cred, curthread, &dnfsva, &nfsva, &nfhp,
2303 &attrflag, &dattrflag, NULL);
2306 dnp->n_flag |= NMODIFIED;
2307 if (dattrflag != 0) {
2309 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2311 dnp->n_attrstamp = 0;
2313 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2316 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, curthread,
2317 &np, NULL, LK_EXCLUSIVE);
2321 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2326 if (!error && newvp == NULL) {
2327 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2328 cnp->cn_cred, curthread, &np);
2331 if (newvp->v_type != VDIR)
2339 error = nfscl_maperr(curthread, error,
2340 vap->va_uid, vap->va_gid);
2343 * If negative lookup caching is enabled, I might as well
2344 * add an entry for this node. Not necessary for correctness,
2345 * but if negative caching is enabled, then the system
2346 * must care about lookup caching hit rate, so...
2348 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2349 (cnp->cn_flags & MAKEENTRY) &&
2350 attrflag != 0 && dattrflag != 0) {
2352 cache_enter_time(dvp, newvp, cnp,
2353 &nfsva.na_ctime, &dnfsva.na_ctime);
2355 printf("nfs_mkdir: bogus NFS server returned "
2356 "the directory that the directory was "
2357 "created in as the new file object\n");
2365 * nfs remove directory call
2368 nfs_rmdir(struct vop_rmdir_args *ap)
2370 struct vnode *vp = ap->a_vp;
2371 struct vnode *dvp = ap->a_dvp;
2372 struct componentname *cnp = ap->a_cnp;
2373 struct nfsnode *dnp;
2374 struct nfsvattr dnfsva;
2375 int error, dattrflag;
2379 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2380 cnp->cn_cred, curthread, &dnfsva, &dattrflag, NULL);
2383 dnp->n_flag |= NMODIFIED;
2384 if (dattrflag != 0) {
2386 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2388 dnp->n_attrstamp = 0;
2390 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2395 if (error && NFS_ISV4(dvp))
2396 error = nfscl_maperr(curthread, error, (uid_t)0,
2399 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2401 if (error == ENOENT)
2410 nfs_readdir(struct vop_readdir_args *ap)
2412 struct vnode *vp = ap->a_vp;
2413 struct nfsnode *np = VTONFS(vp);
2414 struct uio *uio = ap->a_uio;
2415 ssize_t tresid, left;
2419 if (ap->a_eofflag != NULL)
2421 if (vp->v_type != VDIR)
2425 * First, check for hit on the EOF offset cache
2428 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2429 (np->n_flag & NMODIFIED) == 0) {
2431 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2433 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2434 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2436 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2437 if (ap->a_eofflag != NULL)
2447 * NFS always guarantees that directory entries don't straddle
2448 * DIRBLKSIZ boundaries. As such, we need to limit the size
2449 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2452 left = uio->uio_resid % DIRBLKSIZ;
2453 if (left == uio->uio_resid)
2455 uio->uio_resid -= left;
2458 * Call ncl_bioread() to do the real work.
2460 tresid = uio->uio_resid;
2461 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2463 if (!error && uio->uio_resid == tresid) {
2464 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2465 if (ap->a_eofflag != NULL)
2469 /* Add the partial DIRBLKSIZ (left) back in. */
2470 uio->uio_resid += left;
2476 * Called from below the buffer cache by ncl_doio().
2479 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2482 struct nfsvattr nfsva;
2483 nfsuint64 *cookiep, cookie;
2484 struct nfsnode *dnp = VTONFS(vp);
2485 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2486 int error = 0, eof, attrflag;
2488 KASSERT(uiop->uio_iovcnt == 1 &&
2489 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2490 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2491 ("nfs readdirrpc bad uio"));
2494 * If there is no cookie, assume directory was stale.
2496 ncl_dircookie_lock(dnp);
2498 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2501 ncl_dircookie_unlock(dnp);
2503 ncl_dircookie_unlock(dnp);
2504 return (NFSERR_BAD_COOKIE);
2507 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2508 (void)ncl_fsinfo(nmp, vp, cred, td);
2510 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2511 &attrflag, &eof, NULL);
2513 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2517 * We are now either at the end of the directory or have filled
2522 dnp->n_direofoffset = uiop->uio_offset;
2525 if (uiop->uio_resid > 0)
2526 printf("EEK! readdirrpc resid > 0\n");
2527 ncl_dircookie_lock(dnp);
2529 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2531 ncl_dircookie_unlock(dnp);
2533 } else if (NFS_ISV4(vp)) {
2534 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2540 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2543 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2546 struct nfsvattr nfsva;
2547 nfsuint64 *cookiep, cookie;
2548 struct nfsnode *dnp = VTONFS(vp);
2549 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2550 int error = 0, attrflag, eof;
2552 KASSERT(uiop->uio_iovcnt == 1 &&
2553 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2554 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2555 ("nfs readdirplusrpc bad uio"));
2558 * If there is no cookie, assume directory was stale.
2560 ncl_dircookie_lock(dnp);
2562 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2565 ncl_dircookie_unlock(dnp);
2567 ncl_dircookie_unlock(dnp);
2568 return (NFSERR_BAD_COOKIE);
2571 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2572 (void)ncl_fsinfo(nmp, vp, cred, td);
2573 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2574 &attrflag, &eof, NULL);
2576 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2580 * We are now either at end of the directory or have filled the
2585 dnp->n_direofoffset = uiop->uio_offset;
2588 if (uiop->uio_resid > 0)
2589 printf("EEK! readdirplusrpc resid > 0\n");
2590 ncl_dircookie_lock(dnp);
2592 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2594 ncl_dircookie_unlock(dnp);
2596 } else if (NFS_ISV4(vp)) {
2597 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2603 * Silly rename. To make the NFS filesystem that is stateless look a little
2604 * more like the "ufs" a remove of an active vnode is translated to a rename
2605 * to a funny looking filename that is removed by nfs_inactive on the
2606 * nfsnode. There is the potential for another process on a different client
2607 * to create the same funny name between the nfs_lookitup() fails and the
2608 * nfs_rename() completes, but...
2611 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2613 struct sillyrename *sp;
2617 unsigned int lticks;
2621 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2622 sp = malloc(sizeof (struct sillyrename),
2623 M_NEWNFSREQ, M_WAITOK);
2624 sp->s_cred = crhold(cnp->cn_cred);
2629 * Fudge together a funny name.
2630 * Changing the format of the funny name to accommodate more
2631 * sillynames per directory.
2632 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2633 * CPU ticks since boot.
2635 pid = curthread->td_proc->p_pid;
2636 lticks = (unsigned int)ticks;
2638 sp->s_namlen = sprintf(sp->s_name,
2639 ".nfs.%08x.%04x4.4", lticks,
2641 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2646 error = nfs_renameit(dvp, vp, cnp, sp);
2649 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2651 np->n_sillyrename = sp;
2656 free(sp, M_NEWNFSREQ);
2661 * Look up a file name and optionally either update the file handle or
2662 * allocate an nfsnode, depending on the value of npp.
2663 * npp == NULL --> just do the lookup
2664 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2666 * *npp != NULL --> update the file handle in the vnode
2669 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2670 struct thread *td, struct nfsnode **npp)
2672 struct vnode *newvp = NULL, *vp;
2673 struct nfsnode *np, *dnp = VTONFS(dvp);
2674 struct nfsfh *nfhp, *onfhp;
2675 struct nfsvattr nfsva, dnfsva;
2676 struct componentname cn;
2677 int error = 0, attrflag, dattrflag;
2682 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2683 &nfhp, &attrflag, &dattrflag, NULL, 0);
2685 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2686 if (npp && !error) {
2691 * For NFSv4, check to see if it is the same name and
2692 * replace the name, if it is different.
2694 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2695 (np->n_v4->n4_namelen != len ||
2696 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2697 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2698 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2699 dnp->n_fhp->nfh_len))) {
2701 { char nnn[100]; int nnnl;
2702 nnnl = (len < 100) ? len : 99;
2703 bcopy(name, nnn, nnnl);
2705 printf("replace=%s\n",nnn);
2708 free(np->n_v4, M_NFSV4NODE);
2710 sizeof (struct nfsv4node) +
2711 dnp->n_fhp->nfh_len + len - 1,
2712 M_NFSV4NODE, M_WAITOK);
2713 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2714 np->n_v4->n4_namelen = len;
2715 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2716 dnp->n_fhp->nfh_len);
2717 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2719 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2723 * Rehash node for new file handle.
2725 vfs_hash_rehash(vp, hash);
2728 free(onfhp, M_NFSFH);
2730 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2731 free(nfhp, M_NFSFH);
2735 cn.cn_nameptr = name;
2736 cn.cn_namelen = len;
2737 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2738 &np, NULL, LK_EXCLUSIVE);
2743 * If n_localmodtime >= time before RPC, then
2744 * a file modification operation, such as
2745 * VOP_SETATTR() of size, has occurred while
2746 * the Lookup RPC and acquisition of the vnode
2747 * happened. As such, the attributes might
2748 * be stale, with possibly an incorrect size.
2751 if (timespecisset(&np->n_localmodtime) &&
2752 timespeccmp(&np->n_localmodtime, &ts, >=)) {
2753 NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2754 "stale attributes\n");
2759 if (!attrflag && *npp == NULL) {
2767 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2770 if (npp && *npp == NULL) {
2781 if (error && NFS_ISV4(dvp))
2782 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2787 * Nfs Version 3 and 4 commit rpc
2790 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2793 struct nfsvattr nfsva;
2794 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2797 int error, attrflag;
2802 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2803 uio.uio_offset = offset;
2804 uio.uio_resid = cnt;
2805 error = nfscl_doiods(vp, &uio, NULL, NULL,
2806 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2809 np->n_flag &= ~NDSCOMMIT;
2814 mtx_lock(&nmp->nm_mtx);
2815 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2816 mtx_unlock(&nmp->nm_mtx);
2819 mtx_unlock(&nmp->nm_mtx);
2820 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2824 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2826 if (error != 0 && NFS_ISV4(vp))
2827 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2833 * For async requests when nfsiod(s) are running, queue the request by
2834 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2838 nfs_strategy(struct vop_strategy_args *ap)
2846 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2847 KASSERT(!(bp->b_flags & B_DONE),
2848 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2850 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2851 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2853 if (bp->b_iocmd == BIO_READ)
2859 * If the op is asynchronous and an i/o daemon is waiting
2860 * queue the request, wake it up and wait for completion
2861 * otherwise just do it ourselves.
2863 if ((bp->b_flags & B_ASYNC) == 0 ||
2864 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2865 (void) ncl_doio(vp, bp, cr, curthread, 1);
2870 * fsync vnode op. Just call ncl_flush() with commit == 1.
2874 nfs_fsync(struct vop_fsync_args *ap)
2877 if (ap->a_vp->v_type != VREG) {
2879 * For NFS, metadata is changed synchronously on the server,
2880 * so there is nothing to flush. Also, ncl_flush() clears
2881 * the NMODIFIED flag and that shouldn't be done here for
2886 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2890 * Flush all the blocks associated with a vnode.
2891 * Walk through the buffer pool and push any dirty pages
2892 * associated with the vnode.
2893 * If the called_from_renewthread argument is TRUE, it has been called
2894 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2895 * waiting for a buffer write to complete.
2898 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2899 int commit, int called_from_renewthread)
2901 struct nfsnode *np = VTONFS(vp);
2905 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2906 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2907 int passone = 1, trycnt = 0;
2908 u_quad_t off, endoff, toff;
2909 struct ucred* wcred = NULL;
2910 struct buf **bvec = NULL;
2912 #ifndef NFS_COMMITBVECSIZ
2913 #define NFS_COMMITBVECSIZ 20
2915 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2916 u_int bvecsize = 0, bveccount;
2919 if (called_from_renewthread != 0)
2921 if (nmp->nm_flag & NFSMNT_INT)
2927 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2928 * server, but has not been committed to stable storage on the server
2929 * yet. On the first pass, the byte range is worked out and the commit
2930 * rpc is done. On the second pass, ncl_writebp() is called to do the
2937 if (NFS_ISV34(vp) && commit) {
2938 if (bvec != NULL && bvec != bvec_on_stack)
2941 * Count up how many buffers waiting for a commit.
2945 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2946 if (!BUF_ISLOCKED(bp) &&
2947 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2948 == (B_DELWRI | B_NEEDCOMMIT))
2952 * Allocate space to remember the list of bufs to commit. It is
2953 * important to use M_NOWAIT here to avoid a race with nfs_write.
2954 * If we can't get memory (for whatever reason), we will end up
2955 * committing the buffers one-by-one in the loop below.
2957 if (bveccount > NFS_COMMITBVECSIZ) {
2959 * Release the vnode interlock to avoid a lock
2963 bvec = (struct buf **)
2964 malloc(bveccount * sizeof(struct buf *),
2968 bvec = bvec_on_stack;
2969 bvecsize = NFS_COMMITBVECSIZ;
2971 bvecsize = bveccount;
2973 bvec = bvec_on_stack;
2974 bvecsize = NFS_COMMITBVECSIZ;
2976 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2977 if (bvecpos >= bvecsize)
2979 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2980 nbp = TAILQ_NEXT(bp, b_bobufs);
2983 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2984 (B_DELWRI | B_NEEDCOMMIT)) {
2986 nbp = TAILQ_NEXT(bp, b_bobufs);
2992 * Work out if all buffers are using the same cred
2993 * so we can deal with them all with one commit.
2995 * NOTE: we are not clearing B_DONE here, so we have
2996 * to do it later on in this routine if we intend to
2997 * initiate I/O on the bp.
2999 * Note: to avoid loopback deadlocks, we do not
3000 * assign b_runningbufspace.
3003 wcred = bp->b_wcred;
3004 else if (wcred != bp->b_wcred)
3006 vfs_busy_pages(bp, 1);
3010 * bp is protected by being locked, but nbp is not
3011 * and vfs_busy_pages() may sleep. We have to
3014 nbp = TAILQ_NEXT(bp, b_bobufs);
3017 * A list of these buffers is kept so that the
3018 * second loop knows which buffers have actually
3019 * been committed. This is necessary, since there
3020 * may be a race between the commit rpc and new
3021 * uncommitted writes on the file.
3023 bvec[bvecpos++] = bp;
3024 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3028 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3036 * Commit data on the server, as required.
3037 * If all bufs are using the same wcred, then use that with
3038 * one call for all of them, otherwise commit each one
3041 if (wcred != NOCRED)
3042 retv = ncl_commit(vp, off, (int)(endoff - off),
3046 for (i = 0; i < bvecpos; i++) {
3049 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3051 size = (u_quad_t)(bp->b_dirtyend
3053 retv = ncl_commit(vp, off, (int)size,
3059 if (retv == NFSERR_STALEWRITEVERF)
3060 ncl_clearcommit(vp->v_mount);
3063 * Now, either mark the blocks I/O done or mark the
3064 * blocks dirty, depending on whether the commit
3067 for (i = 0; i < bvecpos; i++) {
3069 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3070 if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3072 * Error, leave B_DELWRI intact
3074 vfs_unbusy_pages(bp);
3078 * Success, remove B_DELWRI ( bundirty() ).
3080 * b_dirtyoff/b_dirtyend seem to be NFS
3081 * specific. We should probably move that
3082 * into bundirty(). XXX
3085 bp->b_flags |= B_ASYNC;
3087 bp->b_flags &= ~B_DONE;
3088 bp->b_ioflags &= ~BIO_ERROR;
3089 bp->b_dirtyoff = bp->b_dirtyend = 0;
3096 * Start/do any write(s) that are required.
3100 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3101 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3102 if (waitfor != MNT_WAIT || passone)
3105 error = BUF_TIMELOCK(bp,
3106 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3107 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3112 if (error == ENOLCK) {
3116 if (called_from_renewthread != 0) {
3118 * Return EIO so the flush will be retried
3124 if (newnfs_sigintr(nmp, td)) {
3128 if (slpflag == PCATCH) {
3134 if ((bp->b_flags & B_DELWRI) == 0)
3135 panic("nfs_fsync: not dirty");
3136 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3142 bp->b_flags |= B_ASYNC;
3144 if (newnfs_sigintr(nmp, td)) {
3155 if (waitfor == MNT_WAIT) {
3156 while (bo->bo_numoutput) {
3157 error = bufobj_wwait(bo, slpflag, slptimeo);
3160 if (called_from_renewthread != 0) {
3162 * Return EIO so that the flush will be
3168 error = newnfs_sigintr(nmp, td);
3171 if (slpflag == PCATCH) {
3178 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3183 * Wait for all the async IO requests to drain
3187 while (np->n_directio_asyncwr > 0) {
3188 np->n_flag |= NFSYNCWAIT;
3189 error = newnfs_msleep(td, &np->n_directio_asyncwr,
3190 &np->n_mtx, slpflag | (PRIBIO + 1),
3193 if (newnfs_sigintr(nmp, td)) {
3203 if (NFSHASPNFS(nmp)) {
3204 nfscl_layoutcommit(vp, td);
3206 * Invalidate the attribute cache, since writes to a DS
3207 * won't update the size attribute.
3210 np->n_attrstamp = 0;
3213 if (np->n_flag & NWRITEERR) {
3214 error = np->n_error;
3215 np->n_flag &= ~NWRITEERR;
3217 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3218 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3219 np->n_flag &= ~NMODIFIED;
3222 if (bvec != NULL && bvec != bvec_on_stack)
3224 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3225 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3226 np->n_directio_asyncwr != 0)) {
3228 /* try, try again... */
3235 vn_printf(vp, "ncl_flush failed");
3236 error = called_from_renewthread != 0 ? EIO : EBUSY;
3241 np->n_localmodtime = ts;
3248 * NFS advisory byte-level locks.
3251 nfs_advlock(struct vop_advlock_args *ap)
3253 struct vnode *vp = ap->a_vp;
3255 struct nfsnode *np = VTONFS(ap->a_vp);
3256 struct proc *p = (struct proc *)ap->a_id;
3257 struct thread *td = curthread; /* XXX */
3261 struct nfsmount *nmp;
3263 error = NFSVOPLOCK(vp, LK_SHARED);
3266 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3267 if (vp->v_type != VREG) {
3271 if ((ap->a_flags & F_POSIX) != 0)
3274 cred = td->td_ucred;
3275 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3276 if (VN_IS_DOOMED(vp)) {
3282 * If this is unlocking a write locked region, flush and
3283 * commit them before unlocking. This is required by
3284 * RFC3530 Sec. 9.3.2.
3286 if (ap->a_op == F_UNLCK &&
3287 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3289 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3292 * Mark NFS node as might have acquired a lock.
3293 * This is separate from NHASBEENLOCKED, because it must
3294 * be done before the nfsrpc_advlock() call, which might
3295 * add a nfscllock structure to the client state.
3296 * It is used to check for the case where a nfscllock
3297 * state structure cannot exist for the file.
3298 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3300 nmp = VFSTONFS(vp->v_mount);
3301 if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3303 np->n_flag |= NMIGHTBELOCKED;
3308 * Loop around doing the lock op, while a blocking lock
3309 * must wait for the lock op to succeed.
3312 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3313 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3314 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3315 ap->a_op == F_SETLK) {
3317 error = nfs_catnap(PZERO | PCATCH, ret,
3321 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3322 if (VN_IS_DOOMED(vp)) {
3327 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3328 ap->a_op == F_SETLK);
3329 if (ret == NFSERR_DENIED) {
3332 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3335 } else if (ret != 0) {
3341 * Now, if we just got a lock, invalidate data in the buffer
3342 * cache, as required, so that the coherency conforms with
3343 * RFC3530 Sec. 9.3.2.
3345 if (ap->a_op == F_SETLK) {
3346 if ((np->n_flag & NMODIFIED) == 0) {
3347 np->n_attrstamp = 0;
3348 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3349 ret = VOP_GETATTR(vp, &va, cred);
3351 if ((np->n_flag & NMODIFIED) || ret ||
3352 np->n_change != va.va_filerev) {
3353 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3354 np->n_attrstamp = 0;
3355 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3356 ret = VOP_GETATTR(vp, &va, cred);
3358 np->n_mtime = va.va_mtime;
3359 np->n_change = va.va_filerev;
3362 /* Mark that a file lock has been acquired. */
3364 np->n_flag |= NHASBEENLOCKED;
3367 } else if (!NFS_ISV4(vp)) {
3368 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3369 size = VTONFS(vp)->n_size;
3371 error = lf_advlock(ap, &(vp->v_lockf), size);
3373 if (nfs_advlock_p != NULL)
3374 error = nfs_advlock_p(ap);
3380 if (error == 0 && ap->a_op == F_SETLK) {
3381 error = NFSVOPLOCK(vp, LK_SHARED);
3383 /* Mark that a file lock has been acquired. */
3385 np->n_flag |= NHASBEENLOCKED;
3399 * NFS advisory byte-level locks.
3402 nfs_advlockasync(struct vop_advlockasync_args *ap)
3404 struct vnode *vp = ap->a_vp;
3409 return (EOPNOTSUPP);
3410 error = NFSVOPLOCK(vp, LK_SHARED);
3413 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3414 size = VTONFS(vp)->n_size;
3416 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3425 * Print out the contents of an nfsnode.
3428 nfs_print(struct vop_print_args *ap)
3430 struct vnode *vp = ap->a_vp;
3431 struct nfsnode *np = VTONFS(vp);
3433 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3434 (uintmax_t)np->n_vattr.na_fsid);
3435 if (vp->v_type == VFIFO)
3442 * This is the "real" nfs::bwrite(struct buf*).
3443 * We set B_CACHE if this is a VMIO buffer.
3446 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3448 int oldflags, rtval;
3450 if (bp->b_flags & B_INVAL) {
3455 oldflags = bp->b_flags;
3456 bp->b_flags |= B_CACHE;
3459 * Undirty the bp. We will redirty it later if the I/O fails.
3462 bp->b_flags &= ~B_DONE;
3463 bp->b_ioflags &= ~BIO_ERROR;
3464 bp->b_iocmd = BIO_WRITE;
3466 bufobj_wref(bp->b_bufobj);
3467 curthread->td_ru.ru_oublock++;
3470 * Note: to avoid loopback deadlocks, we do not
3471 * assign b_runningbufspace.
3473 vfs_busy_pages(bp, 1);
3476 bp->b_iooffset = dbtob(bp->b_blkno);
3479 if ((oldflags & B_ASYNC) != 0)
3482 rtval = bufwait(bp);
3483 if (oldflags & B_DELWRI)
3490 * nfs special file access vnode op.
3491 * Essentially just get vattr and then imitate iaccess() since the device is
3492 * local to the client.
3495 nfsspec_access(struct vop_access_args *ap)
3498 struct ucred *cred = ap->a_cred;
3499 struct vnode *vp = ap->a_vp;
3500 accmode_t accmode = ap->a_accmode;
3505 * Disallow write attempts on filesystems mounted read-only;
3506 * unless the file is a socket, fifo, or a block or character
3507 * device resident on the filesystem.
3509 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3510 switch (vp->v_type) {
3520 error = VOP_GETATTR(vp, vap, cred);
3523 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3530 * Read wrapper for fifos.
3533 nfsfifo_read(struct vop_read_args *ap)
3535 struct nfsnode *np = VTONFS(ap->a_vp);
3543 vfs_timestamp(&np->n_atim);
3545 error = fifo_specops.vop_read(ap);
3550 * Write wrapper for fifos.
3553 nfsfifo_write(struct vop_write_args *ap)
3555 struct nfsnode *np = VTONFS(ap->a_vp);
3562 vfs_timestamp(&np->n_mtim);
3564 return(fifo_specops.vop_write(ap));
3568 * Close wrapper for fifos.
3570 * Update the times on the nfsnode then do fifo close.
3573 nfsfifo_close(struct vop_close_args *ap)
3575 struct vnode *vp = ap->a_vp;
3576 struct nfsnode *np = VTONFS(vp);
3581 if (np->n_flag & (NACC | NUPD)) {
3583 if (np->n_flag & NACC)
3585 if (np->n_flag & NUPD)
3588 if (vrefcnt(vp) == 1 &&
3589 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3591 if (np->n_flag & NACC)
3592 vattr.va_atime = np->n_atim;
3593 if (np->n_flag & NUPD)
3594 vattr.va_mtime = np->n_mtim;
3596 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3602 return (fifo_specops.vop_close(ap));
3606 * Just call ncl_writebp() with the force argument set to 1.
3608 * NOTE: B_DONE may or may not be set in a_bp on call.
3611 nfs_bwrite(struct buf *bp)
3614 return (ncl_writebp(bp, 1, curthread));
3617 struct buf_ops buf_ops_newnfs = {
3618 .bop_name = "buf_ops_nfs",
3619 .bop_write = nfs_bwrite,
3620 .bop_strategy = bufstrategy,
3621 .bop_sync = bufsync,
3622 .bop_bdflush = bufbdflush,
3626 nfs_getacl(struct vop_getacl_args *ap)
3630 if (ap->a_type != ACL_TYPE_NFS4)
3631 return (EOPNOTSUPP);
3632 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3634 if (error > NFSERR_STALE) {
3635 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3642 nfs_setacl(struct vop_setacl_args *ap)
3646 if (ap->a_type != ACL_TYPE_NFS4)
3647 return (EOPNOTSUPP);
3648 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3650 if (error > NFSERR_STALE) {
3651 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3658 * VOP_ADVISE for NFS.
3659 * Just return 0 for any errors, since it is just a hint.
3662 nfs_advise(struct vop_advise_args *ap)
3664 struct thread *td = curthread;
3665 struct nfsmount *nmp;
3670 * First do vop_stdadvise() to handle the buffer cache.
3672 error = vop_stdadvise(ap);
3675 if (ap->a_start < 0 || ap->a_end < 0)
3677 if (ap->a_end == OFF_MAX)
3679 else if (ap->a_end < ap->a_start)
3682 len = ap->a_end - ap->a_start + 1;
3683 nmp = VFSTONFS(ap->a_vp->v_mount);
3684 mtx_lock(&nmp->nm_mtx);
3685 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3686 (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3687 0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3688 mtx_unlock(&nmp->nm_mtx);
3691 mtx_unlock(&nmp->nm_mtx);
3692 error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3694 if (error == NFSERR_NOTSUPP) {
3695 mtx_lock(&nmp->nm_mtx);
3696 nmp->nm_privflag |= NFSMNTP_NOADVISE;
3697 mtx_unlock(&nmp->nm_mtx);
3706 nfs_allocate(struct vop_allocate_args *ap)
3708 struct vnode *vp = ap->a_vp;
3709 struct thread *td = curthread;
3710 struct nfsvattr nfsva;
3711 struct nfsmount *nmp;
3714 int attrflag, error, ret;
3718 nmp = VFSTONFS(vp->v_mount);
3720 mtx_lock(&nmp->nm_mtx);
3721 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3722 (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3723 mtx_unlock(&nmp->nm_mtx);
3725 * Flush first to ensure that the allocate adds to the
3726 * file's allocation on the server.
3728 error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3731 if ((uint64_t)alen > nfs_maxalloclen)
3732 alen = nfs_maxalloclen;
3733 error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3734 &nfsva, &attrflag, ap->a_cred, td, NULL);
3737 *ap->a_offset += alen;
3741 np->n_localmodtime = ts;
3743 } else if (error == NFSERR_NOTSUPP) {
3744 mtx_lock(&nmp->nm_mtx);
3745 nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3746 mtx_unlock(&nmp->nm_mtx);
3750 mtx_unlock(&nmp->nm_mtx);
3753 if (attrflag != 0) {
3754 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
3755 if (error == 0 && ret != 0)
3759 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3764 * nfs deallocate call
3767 nfs_deallocate(struct vop_deallocate_args *ap)
3769 struct vnode *vp = ap->a_vp;
3770 struct thread *td = curthread;
3771 struct nfsvattr nfsva;
3772 struct nfsmount *nmp;
3775 int attrflag, error, ret;
3781 nmp = VFSTONFS(vp->v_mount);
3783 mtx_lock(&nmp->nm_mtx);
3784 if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3785 (nmp->nm_privflag & NFSMNTP_NODEALLOCATE) == 0) {
3786 mtx_unlock(&nmp->nm_mtx);
3787 tlen = omin(OFF_MAX - *ap->a_offset, *ap->a_len);
3788 NFSCL_DEBUG(4, "dealloc: off=%jd len=%jd maxfilesize=%ju\n",
3789 (intmax_t)*ap->a_offset, (intmax_t)tlen,
3790 (uintmax_t)nmp->nm_maxfilesize);
3791 if ((uint64_t)*ap->a_offset >= nmp->nm_maxfilesize) {
3792 /* Avoid EFBIG error return from the NFSv4.2 server. */
3797 if ((uint64_t)*ap->a_offset + tlen > nmp->nm_maxfilesize)
3798 tlen = nmp->nm_maxfilesize - *ap->a_offset;
3799 if ((uint64_t)*ap->a_offset < np->n_size) {
3800 /* Limit the len to nfs_maxalloclen before EOF. */
3801 mlen = omin((off_t)np->n_size - *ap->a_offset, tlen);
3802 if ((uint64_t)mlen > nfs_maxalloclen) {
3803 NFSCL_DEBUG(4, "dealloc: tlen maxalloclen\n");
3804 tlen = nfs_maxalloclen;
3809 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
3811 vnode_pager_purge_range(vp, *ap->a_offset,
3812 *ap->a_offset + tlen);
3813 error = nfsrpc_deallocate(vp, *ap->a_offset, tlen,
3814 &nfsva, &attrflag, ap->a_cred, td, NULL);
3815 NFSCL_DEBUG(4, "dealloc: rpc=%d\n", error);
3818 NFSCL_DEBUG(4, "dealloc: attrflag=%d na_size=%ju\n",
3819 attrflag, (uintmax_t)nfsva.na_size);
3822 np->n_localmodtime = ts;
3824 if (attrflag != 0) {
3825 if ((uint64_t)*ap->a_offset < nfsva.na_size)
3826 *ap->a_offset += omin((off_t)
3827 nfsva.na_size - *ap->a_offset,
3830 if (clipped && tlen < *ap->a_len)
3834 } else if (error == NFSERR_NOTSUPP) {
3835 mtx_lock(&nmp->nm_mtx);
3836 nmp->nm_privflag |= NFSMNTP_NODEALLOCATE;
3837 mtx_unlock(&nmp->nm_mtx);
3840 mtx_unlock(&nmp->nm_mtx);
3844 * If the NFS server cannot perform the Deallocate operation, just call
3845 * vop_stddeallocate() to perform it.
3847 if (error != 0 && error != NFSERR_FBIG && error != NFSERR_INVAL) {
3848 error = vop_stddeallocate(ap);
3849 NFSCL_DEBUG(4, "dealloc: stddeallocate=%d\n", error);
3851 if (attrflag != 0) {
3852 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
3853 if (error == 0 && ret != 0)
3857 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3862 * nfs copy_file_range call
3865 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
3867 struct vnode *invp = ap->a_invp;
3868 struct vnode *outvp = ap->a_outvp;
3870 struct nfsvattr innfsva, outnfsva;
3873 struct nfsmount *nmp;
3875 int error, inattrflag, outattrflag, ret, ret2;
3876 off_t inoff, outoff;
3877 bool consecutive, must_commit, tryoutcred;
3880 nmp = VFSTONFS(invp->v_mount);
3881 mtx_lock(&nmp->nm_mtx);
3882 /* NFSv4.2 Copy is not permitted for infile == outfile. */
3883 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3884 (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0 || invp == outvp) {
3885 mtx_unlock(&nmp->nm_mtx);
3886 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
3887 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
3888 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
3891 mtx_unlock(&nmp->nm_mtx);
3893 /* Lock both vnodes, avoiding risk of deadlock. */
3896 error = vn_start_write(outvp, &mp, V_WAIT);
3898 error = vn_lock(outvp, LK_EXCLUSIVE);
3900 error = vn_lock(invp, LK_SHARED | LK_NOWAIT);
3905 vn_finished_write(mp);
3907 error = vn_lock(invp, LK_SHARED);
3913 vn_finished_write(mp);
3914 } while (error == 0);
3919 * Do the vn_rlimit_fsize() check. Should this be above the VOP layer?
3921 io.uio_offset = *ap->a_outoffp;
3922 io.uio_resid = *ap->a_lenp;
3923 error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
3926 * Flush the input file so that the data is up to date before
3927 * the copy. Flush writes for the output file so that they
3928 * do not overwrite the data copied to the output file by the Copy.
3929 * Set the commit argument for both flushes so that the data is on
3930 * stable storage before the Copy RPC. This is done in case the
3931 * server reboots during the Copy and needs to be redone.
3934 error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
3936 error = ncl_flush(outvp, MNT_WAIT, curthread, 1, 0);
3938 /* Do the actual NFSv4.2 RPC. */
3940 mtx_lock(&nmp->nm_mtx);
3941 if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
3944 consecutive = false;
3945 mtx_unlock(&nmp->nm_mtx);
3946 inoff = *ap->a_inoffp;
3947 outoff = *ap->a_outoffp;
3949 must_commit = false;
3951 vap = &VTONFS(invp)->n_vattr.na_vattr;
3952 error = VOP_GETATTR(invp, vap, ap->a_incred);
3955 * Clip "len" at va_size so that RFC compliant servers
3956 * will not reply NFSERR_INVAL.
3957 * Setting "len == 0" for the RPC would be preferred,
3958 * but some Linux servers do not support that.
3960 if (inoff >= vap->va_size)
3961 *ap->a_lenp = len = 0;
3962 else if (inoff + len > vap->va_size)
3963 *ap->a_lenp = len = vap->va_size - inoff;
3969 * len will be set to 0 upon a successful Copy RPC.
3970 * As such, this only loops when the Copy RPC needs to be retried.
3972 while (len > 0 && error == 0) {
3973 inattrflag = outattrflag = 0;
3976 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3977 outvp, ap->a_outoffp, &len2, ap->a_flags,
3978 &inattrflag, &innfsva, &outattrflag, &outnfsva,
3979 ap->a_outcred, consecutive, &must_commit);
3981 error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3982 outvp, ap->a_outoffp, &len2, ap->a_flags,
3983 &inattrflag, &innfsva, &outattrflag, &outnfsva,
3984 ap->a_incred, consecutive, &must_commit);
3985 if (inattrflag != 0)
3986 ret = nfscl_loadattrcache(&invp, &innfsva, NULL, NULL,
3988 if (outattrflag != 0)
3989 ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
3992 if (consecutive == false) {
3994 mtx_lock(&nmp->nm_mtx);
3996 NFSMNTP_NOCONSECUTIVE;
3997 mtx_unlock(&nmp->nm_mtx);
3999 error = NFSERR_OFFLOADNOREQS;
4003 if (len2 > 0 && must_commit && error == 0)
4004 error = ncl_commit(outvp, outoff, *ap->a_lenp,
4005 ap->a_outcred, curthread);
4006 if (error == 0 && ret != 0)
4008 if (error == 0 && ret2 != 0)
4010 } else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
4012 * Try consecutive == false, which is ok only if all
4014 * If only some bytes were copied when consecutive
4015 * is false, there is no way to know which bytes
4016 * still need to be written.
4018 consecutive = false;
4020 } else if (error == NFSERR_ACCES && tryoutcred) {
4021 /* Try again with incred. */
4025 if (error == NFSERR_STALEWRITEVERF) {
4027 * Server rebooted, so do it all again.
4029 *ap->a_inoffp = inoff;
4030 *ap->a_outoffp = outoff;
4032 must_commit = false;
4039 vn_finished_write(mp);
4040 if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
4041 error == NFSERR_ACCES) {
4043 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
4044 * use a_incred for the read and a_outcred for the write, so
4045 * try this for NFSERR_ACCES failures for the Copy.
4046 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
4047 * never succeed, so disable it.
4049 if (error != NFSERR_ACCES) {
4050 /* Can never do Copy on this mount. */
4051 mtx_lock(&nmp->nm_mtx);
4052 nmp->nm_privflag |= NFSMNTP_NOCOPY;
4053 mtx_unlock(&nmp->nm_mtx);
4055 *ap->a_inoffp = inoff;
4056 *ap->a_outoffp = outoff;
4057 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
4058 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
4059 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
4060 } else if (error != 0)
4064 error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
4072 nfs_ioctl(struct vop_ioctl_args *ap)
4074 struct vnode *vp = ap->a_vp;
4075 struct nfsvattr nfsva;
4076 struct nfsmount *nmp;
4077 int attrflag, content, error, ret;
4078 bool eof = false; /* shut up compiler. */
4080 if (vp->v_type != VREG)
4082 nmp = VFSTONFS(vp->v_mount);
4083 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4084 error = vop_stdioctl(ap);
4088 /* Do the actual NFSv4.2 RPC. */
4089 switch (ap->a_command) {
4091 content = NFSV4CONTENT_DATA;
4094 content = NFSV4CONTENT_HOLE;
4100 error = vn_lock(vp, LK_SHARED);
4104 if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4108 * Flush all writes, so that the server is up to date.
4109 * Although a Commit is not required, the commit argument
4110 * is set so that, for a pNFS File/Flexible File Layout
4111 * server, the LayoutCommit will be done to ensure the file
4112 * size is up to date on the Metadata Server.
4114 error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4116 error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4117 content, ap->a_cred, &nfsva, &attrflag);
4118 /* If at eof for FIOSEEKDATA, return ENXIO. */
4119 if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4122 if (attrflag != 0) {
4123 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4124 if (error == 0 && ret != 0)
4135 * nfs getextattr call
4138 nfs_getextattr(struct vop_getextattr_args *ap)
4140 struct vnode *vp = ap->a_vp;
4141 struct nfsmount *nmp;
4143 struct thread *td = ap->a_td;
4144 struct nfsvattr nfsva;
4146 int attrflag, error, ret;
4148 nmp = VFSTONFS(vp->v_mount);
4149 mtx_lock(&nmp->nm_mtx);
4150 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4151 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4152 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4153 mtx_unlock(&nmp->nm_mtx);
4154 return (EOPNOTSUPP);
4156 mtx_unlock(&nmp->nm_mtx);
4160 cred = td->td_ucred;
4161 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4163 error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4164 &attrflag, cred, td);
4165 if (attrflag != 0) {
4166 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4167 if (error == 0 && ret != 0)
4170 if (error == 0 && ap->a_size != NULL)
4174 case NFSERR_NOTSUPP:
4175 case NFSERR_OPILLEGAL:
4176 mtx_lock(&nmp->nm_mtx);
4177 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4178 mtx_unlock(&nmp->nm_mtx);
4181 case NFSERR_NOXATTR:
4182 case NFSERR_XATTR2BIG:
4186 error = nfscl_maperr(td, error, 0, 0);
4193 * nfs setextattr call
4196 nfs_setextattr(struct vop_setextattr_args *ap)
4198 struct vnode *vp = ap->a_vp;
4199 struct nfsmount *nmp;
4201 struct thread *td = ap->a_td;
4202 struct nfsvattr nfsva;
4203 int attrflag, error, ret;
4205 nmp = VFSTONFS(vp->v_mount);
4206 mtx_lock(&nmp->nm_mtx);
4207 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4208 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4209 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4210 mtx_unlock(&nmp->nm_mtx);
4211 return (EOPNOTSUPP);
4213 mtx_unlock(&nmp->nm_mtx);
4215 if (ap->a_uio->uio_resid < 0)
4219 cred = td->td_ucred;
4220 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4222 error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4223 &attrflag, cred, td);
4224 if (attrflag != 0) {
4225 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4226 if (error == 0 && ret != 0)
4231 case NFSERR_NOTSUPP:
4232 case NFSERR_OPILLEGAL:
4233 mtx_lock(&nmp->nm_mtx);
4234 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4235 mtx_unlock(&nmp->nm_mtx);
4238 case NFSERR_NOXATTR:
4239 case NFSERR_XATTR2BIG:
4243 error = nfscl_maperr(td, error, 0, 0);
4250 * nfs listextattr call
4253 nfs_listextattr(struct vop_listextattr_args *ap)
4255 struct vnode *vp = ap->a_vp;
4256 struct nfsmount *nmp;
4258 struct thread *td = ap->a_td;
4259 struct nfsvattr nfsva;
4262 int attrflag, error, ret;
4265 nmp = VFSTONFS(vp->v_mount);
4266 mtx_lock(&nmp->nm_mtx);
4267 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4268 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4269 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4270 mtx_unlock(&nmp->nm_mtx);
4271 return (EOPNOTSUPP);
4273 mtx_unlock(&nmp->nm_mtx);
4277 cred = td->td_ucred;
4279 /* Loop around doing List Extended Attribute RPCs. */
4284 while (!eof && error == 0) {
4285 len = nmp->nm_rsize;
4287 error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4288 &nfsva, &attrflag, cred, td);
4289 if (attrflag != 0) {
4290 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4292 if (error == 0 && ret != 0)
4297 if (len2 > SSIZE_MAX)
4301 if (error == 0 && ap->a_size != NULL)
4305 case NFSERR_NOTSUPP:
4306 case NFSERR_OPILLEGAL:
4307 mtx_lock(&nmp->nm_mtx);
4308 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4309 mtx_unlock(&nmp->nm_mtx);
4312 case NFSERR_NOXATTR:
4313 case NFSERR_XATTR2BIG:
4317 error = nfscl_maperr(td, error, 0, 0);
4324 * nfs setextattr call
4327 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4329 struct vnode *vp = ap->a_vp;
4330 struct nfsmount *nmp;
4331 struct nfsvattr nfsva;
4332 int attrflag, error, ret;
4334 nmp = VFSTONFS(vp->v_mount);
4335 mtx_lock(&nmp->nm_mtx);
4336 if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4337 (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4338 ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4339 mtx_unlock(&nmp->nm_mtx);
4340 return (EOPNOTSUPP);
4342 mtx_unlock(&nmp->nm_mtx);
4344 /* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4346 error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4348 if (attrflag != 0) {
4349 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4350 if (error == 0 && ret != 0)
4355 case NFSERR_NOTSUPP:
4356 case NFSERR_OPILLEGAL:
4357 mtx_lock(&nmp->nm_mtx);
4358 nmp->nm_privflag |= NFSMNTP_NOXATTR;
4359 mtx_unlock(&nmp->nm_mtx);
4362 case NFSERR_NOXATTR:
4363 case NFSERR_XATTR2BIG:
4367 error = nfscl_maperr(ap->a_td, error, 0, 0);
4374 * Return POSIX pathconf information applicable to nfs filesystems.
4377 nfs_pathconf(struct vop_pathconf_args *ap)
4379 struct nfsv3_pathconf pc;
4380 struct nfsvattr nfsva;
4381 struct vnode *vp = ap->a_vp;
4382 struct nfsmount *nmp;
4383 struct thread *td = curthread;
4386 int attrflag, error;
4388 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4389 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4390 ap->a_name == _PC_NO_TRUNC)) ||
4391 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
4393 * Since only the above 4 a_names are returned by the NFSv3
4394 * Pathconf RPC, there is no point in doing it for others.
4395 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4396 * be used for _PC_NFS4_ACL as well.
4398 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
4401 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4407 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4410 pc.pc_linkmax = NFS_LINK_MAX;
4411 pc.pc_namemax = NFS_MAXNAMLEN;
4413 pc.pc_chownrestricted = 1;
4414 pc.pc_caseinsensitive = 0;
4415 pc.pc_casepreserving = 1;
4418 switch (ap->a_name) {
4421 *ap->a_retval = pc.pc_linkmax;
4423 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4427 *ap->a_retval = pc.pc_namemax;
4430 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4431 *ap->a_retval = PIPE_BUF;
4435 case _PC_CHOWN_RESTRICTED:
4436 *ap->a_retval = pc.pc_chownrestricted;
4439 *ap->a_retval = pc.pc_notrunc;
4442 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4443 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4448 case _PC_ACL_PATH_MAX:
4450 *ap->a_retval = ACL_MAX_ENTRIES;
4460 case _PC_ALLOC_SIZE_MIN:
4461 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4463 case _PC_FILESIZEBITS:
4469 case _PC_REC_INCR_XFER_SIZE:
4470 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4472 case _PC_REC_MAX_XFER_SIZE:
4473 *ap->a_retval = -1; /* means ``unlimited'' */
4475 case _PC_REC_MIN_XFER_SIZE:
4476 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4478 case _PC_REC_XFER_ALIGN:
4479 *ap->a_retval = PAGE_SIZE;
4481 case _PC_SYMLINK_MAX:
4482 *ap->a_retval = NFS_MAXPATHLEN;
4484 case _PC_MIN_HOLE_SIZE:
4485 /* Only some NFSv4.2 servers support Seek for Holes. */
4487 nmp = VFSTONFS(vp->v_mount);
4488 if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4490 * NFSv4.2 doesn't have an attribute for hole size,
4491 * so all we can do is see if the Seek operation is
4492 * supported and then use f_iosize as a "best guess".
4494 mtx_lock(&nmp->nm_mtx);
4495 if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4496 mtx_unlock(&nmp->nm_mtx);
4499 error = nfsrpc_seek(vp, &off, &eof,
4500 NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4503 nfscl_loadattrcache(&vp, &nfsva,
4505 mtx_lock(&nmp->nm_mtx);
4506 if (error == NFSERR_NOTSUPP)
4507 nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4509 nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4513 if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4514 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4515 mtx_unlock(&nmp->nm_mtx);
4520 error = vop_stdpathconf(ap);