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>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
64 #include <sys/sysctl.h>
65 #include <sys/signalvar.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
71 #include <fs/nfs/nfsport.h>
72 #include <fs/nfsclient/nfsnode.h>
73 #include <fs/nfsclient/nfsmount.h>
74 #include <fs/nfsclient/nfs.h>
75 #include <fs/nfsclient/nfs_kdtrace.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
81 #include <nfs/nfs_lock.h>
84 #include <sys/dtrace_bsd.h>
86 dtrace_nfsclient_accesscache_flush_probe_func_t
87 dtrace_nfscl_accesscache_flush_done_probe;
88 uint32_t nfscl_accesscache_flush_done_id;
90 dtrace_nfsclient_accesscache_get_probe_func_t
91 dtrace_nfscl_accesscache_get_hit_probe,
92 dtrace_nfscl_accesscache_get_miss_probe;
93 uint32_t nfscl_accesscache_get_hit_id;
94 uint32_t nfscl_accesscache_get_miss_id;
96 dtrace_nfsclient_accesscache_load_probe_func_t
97 dtrace_nfscl_accesscache_load_done_probe;
98 uint32_t nfscl_accesscache_load_done_id;
99 #endif /* !KDTRACE_HOOKS */
105 extern struct nfsstatsv1 nfsstatsv1;
106 extern int nfsrv_useacl;
107 extern int nfscl_debuglevel;
108 MALLOC_DECLARE(M_NEWNFSREQ);
110 static vop_read_t nfsfifo_read;
111 static vop_write_t nfsfifo_write;
112 static vop_close_t nfsfifo_close;
113 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
115 static vop_lookup_t nfs_lookup;
116 static vop_create_t nfs_create;
117 static vop_mknod_t nfs_mknod;
118 static vop_open_t nfs_open;
119 static vop_pathconf_t nfs_pathconf;
120 static vop_close_t nfs_close;
121 static vop_access_t nfs_access;
122 static vop_getattr_t nfs_getattr;
123 static vop_setattr_t nfs_setattr;
124 static vop_read_t nfs_read;
125 static vop_fsync_t nfs_fsync;
126 static vop_remove_t nfs_remove;
127 static vop_link_t nfs_link;
128 static vop_rename_t nfs_rename;
129 static vop_mkdir_t nfs_mkdir;
130 static vop_rmdir_t nfs_rmdir;
131 static vop_symlink_t nfs_symlink;
132 static vop_readdir_t nfs_readdir;
133 static vop_strategy_t nfs_strategy;
134 static int nfs_lookitup(struct vnode *, char *, int,
135 struct ucred *, struct thread *, struct nfsnode **);
136 static int nfs_sillyrename(struct vnode *, struct vnode *,
137 struct componentname *);
138 static vop_access_t nfsspec_access;
139 static vop_readlink_t nfs_readlink;
140 static vop_print_t nfs_print;
141 static vop_advlock_t nfs_advlock;
142 static vop_advlockasync_t nfs_advlockasync;
143 static vop_getacl_t nfs_getacl;
144 static vop_setacl_t nfs_setacl;
145 static vop_lock1_t nfs_lock;
148 * Global vfs data structures for nfs
150 struct vop_vector newnfs_vnodeops = {
151 .vop_default = &default_vnodeops,
152 .vop_access = nfs_access,
153 .vop_advlock = nfs_advlock,
154 .vop_advlockasync = nfs_advlockasync,
155 .vop_close = nfs_close,
156 .vop_create = nfs_create,
157 .vop_fsync = nfs_fsync,
158 .vop_getattr = nfs_getattr,
159 .vop_getpages = ncl_getpages,
160 .vop_putpages = ncl_putpages,
161 .vop_inactive = ncl_inactive,
162 .vop_link = nfs_link,
163 .vop_lock1 = nfs_lock,
164 .vop_lookup = nfs_lookup,
165 .vop_mkdir = nfs_mkdir,
166 .vop_mknod = nfs_mknod,
167 .vop_open = nfs_open,
168 .vop_pathconf = nfs_pathconf,
169 .vop_print = nfs_print,
170 .vop_read = nfs_read,
171 .vop_readdir = nfs_readdir,
172 .vop_readlink = nfs_readlink,
173 .vop_reclaim = ncl_reclaim,
174 .vop_remove = nfs_remove,
175 .vop_rename = nfs_rename,
176 .vop_rmdir = nfs_rmdir,
177 .vop_setattr = nfs_setattr,
178 .vop_strategy = nfs_strategy,
179 .vop_symlink = nfs_symlink,
180 .vop_write = ncl_write,
181 .vop_getacl = nfs_getacl,
182 .vop_setacl = nfs_setacl,
185 struct vop_vector newnfs_fifoops = {
186 .vop_default = &fifo_specops,
187 .vop_access = nfsspec_access,
188 .vop_close = nfsfifo_close,
189 .vop_fsync = nfs_fsync,
190 .vop_getattr = nfs_getattr,
191 .vop_inactive = ncl_inactive,
192 .vop_pathconf = nfs_pathconf,
193 .vop_print = nfs_print,
194 .vop_read = nfsfifo_read,
195 .vop_reclaim = ncl_reclaim,
196 .vop_setattr = nfs_setattr,
197 .vop_write = nfsfifo_write,
200 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
201 struct componentname *cnp, struct vattr *vap);
202 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
203 int namelen, struct ucred *cred, struct thread *td);
204 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
205 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
206 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
207 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
208 struct componentname *scnp, struct sillyrename *sp);
213 SYSCTL_DECL(_vfs_nfs);
215 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
216 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
217 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
219 static int nfs_prime_access_cache = 0;
220 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
221 &nfs_prime_access_cache, 0,
222 "Prime NFS ACCESS cache when fetching attributes");
224 static int newnfs_commit_on_close = 0;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
226 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
228 static int nfs_clean_pages_on_close = 1;
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
230 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
232 int newnfs_directio_enable = 0;
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
234 &newnfs_directio_enable, 0, "Enable NFS directio");
236 int nfs_keep_dirty_on_error;
237 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
238 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
241 * This sysctl allows other processes to mmap a file that has been opened
242 * O_DIRECT by a process. In general, having processes mmap the file while
243 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
244 * this by default to prevent DoS attacks - to prevent a malicious user from
245 * opening up files O_DIRECT preventing other users from mmap'ing these
246 * files. "Protected" environments where stricter consistency guarantees are
247 * required can disable this knob. The process that opened the file O_DIRECT
248 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
251 int newnfs_directio_allow_mmap = 1;
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
253 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
255 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
256 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
257 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
261 * The list of locks after the description of the lock is the ordering
262 * of other locks acquired with the lock held.
263 * np->n_mtx : Protects the fields in the nfsnode.
265 VI_MTX (acquired indirectly)
266 * nmp->nm_mtx : Protects the fields in the nfsmount.
268 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
269 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
272 * rep->r_mtx : Protects the fields in an nfsreq.
276 nfs_lock(struct vop_lock1_args *ap)
285 lktype = ap->a_flags & LK_TYPE_MASK;
286 error = VOP_LOCK1_APV(&default_vnodeops, ap);
287 if (error != 0 || vp->v_op != &newnfs_vnodeops)
293 if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
294 lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
295 lktype != LK_TRYUPGRADE)) {
299 onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
300 (ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
301 (lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
302 if (onfault && vp->v_vnlock->lk_recurse == 0) {
304 * Force retry in vm_fault(), to make the lock request
305 * sleepable, which allows us to piggy-back the
306 * sleepable call to vnode_pager_setsize().
312 if ((ap->a_flags & LK_NOWAIT) != 0 ||
313 (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
317 if (lktype == LK_SHARED) {
320 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
321 ap->a_flags |= LK_EXCLUSIVE;
322 error = VOP_LOCK1_APV(&default_vnodeops, ap);
323 if (error != 0 || vp->v_op != &newnfs_vnodeops)
325 if (vp->v_data == NULL)
327 MPASS(vp->v_data == np);
329 if ((np->n_flag & NVNSETSZSKIP) == 0) {
334 np->n_flag &= ~NVNSETSZSKIP;
337 vnode_pager_setsize(vp, nsize);
339 if (lktype == LK_SHARED) {
340 ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
341 ap->a_flags |= LK_DOWNGRADE;
342 (void)VOP_LOCK1_APV(&default_vnodeops, ap);
348 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
349 struct ucred *cred, u_int32_t *retmode)
351 int error = 0, attrflag, i, lrupos;
353 struct nfsnode *np = VTONFS(vp);
354 struct nfsvattr nfsva;
356 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
359 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
363 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
364 if (np->n_accesscache[i].uid == cred->cr_uid) {
365 np->n_accesscache[i].mode = rmode;
366 np->n_accesscache[i].stamp = time_second;
369 if (i > 0 && np->n_accesscache[i].stamp <
370 np->n_accesscache[lrupos].stamp)
373 if (i == NFS_ACCESSCACHESIZE) {
374 np->n_accesscache[lrupos].uid = cred->cr_uid;
375 np->n_accesscache[lrupos].mode = rmode;
376 np->n_accesscache[lrupos].stamp = time_second;
381 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
382 } else if (NFS_ISV4(vp)) {
383 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
387 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
394 * nfs access vnode op.
395 * For nfs version 2, just return ok. File accesses may fail later.
396 * For nfs version 3, use the access rpc to check accessibility. If file modes
397 * are changed on the server, accesses might still fail later.
400 nfs_access(struct vop_access_args *ap)
402 struct vnode *vp = ap->a_vp;
403 int error = 0, i, gotahit;
404 u_int32_t mode, wmode, rmode;
405 int v34 = NFS_ISV34(vp);
406 struct nfsnode *np = VTONFS(vp);
409 * Disallow write attempts on filesystems mounted read-only;
410 * unless the file is a socket, fifo, or a block or character
411 * device resident on the filesystem.
413 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
414 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
415 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
416 switch (vp->v_type) {
426 * For nfs v3 or v4, check to see if we have done this recently, and if
427 * so return our cached result instead of making an ACCESS call.
428 * If not, do an access rpc, otherwise you are stuck emulating
429 * ufs_access() locally using the vattr. This may not be correct,
430 * since the server may apply other access criteria such as
431 * client uid-->server uid mapping that we do not know about.
434 if (ap->a_accmode & VREAD)
435 mode = NFSACCESS_READ;
438 if (vp->v_type != VDIR) {
439 if (ap->a_accmode & VWRITE)
440 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
441 if (ap->a_accmode & VAPPEND)
442 mode |= NFSACCESS_EXTEND;
443 if (ap->a_accmode & VEXEC)
444 mode |= NFSACCESS_EXECUTE;
445 if (ap->a_accmode & VDELETE)
446 mode |= NFSACCESS_DELETE;
448 if (ap->a_accmode & VWRITE)
449 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
450 if (ap->a_accmode & VAPPEND)
451 mode |= NFSACCESS_EXTEND;
452 if (ap->a_accmode & VEXEC)
453 mode |= NFSACCESS_LOOKUP;
454 if (ap->a_accmode & VDELETE)
455 mode |= NFSACCESS_DELETE;
456 if (ap->a_accmode & VDELETE_CHILD)
457 mode |= NFSACCESS_MODIFY;
459 /* XXX safety belt, only make blanket request if caching */
460 if (nfsaccess_cache_timeout > 0) {
461 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
462 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
463 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
469 * Does our cached result allow us to give a definite yes to
474 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
475 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
476 if (time_second < (np->n_accesscache[i].stamp
477 + nfsaccess_cache_timeout) &&
478 (np->n_accesscache[i].mode & mode) == mode) {
479 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
488 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
489 ap->a_cred->cr_uid, mode);
491 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
492 ap->a_cred->cr_uid, mode);
496 * Either a no, or a don't know. Go to the wire.
498 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
499 error = nfs34_access_otw(vp, wmode, ap->a_td,
502 (rmode & mode) != mode)
507 if ((error = nfsspec_access(ap)) != 0) {
511 * Attempt to prevent a mapped root from accessing a file
512 * which it shouldn't. We try to read a byte from the file
513 * if the user is root and the file is not zero length.
514 * After calling nfsspec_access, we should have the correct
518 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
519 && VTONFS(vp)->n_size > 0) {
527 auio.uio_iov = &aiov;
531 auio.uio_segflg = UIO_SYSSPACE;
532 auio.uio_rw = UIO_READ;
533 auio.uio_td = ap->a_td;
535 if (vp->v_type == VREG)
536 error = ncl_readrpc(vp, &auio, ap->a_cred);
537 else if (vp->v_type == VDIR) {
539 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
541 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
542 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
545 } else if (vp->v_type == VLNK)
546 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
558 * Check to see if the type is ok
559 * and that deletion is not in progress.
560 * For paged in text files, you will need to flush the page cache
561 * if consistency is lost.
565 nfs_open(struct vop_open_args *ap)
567 struct vnode *vp = ap->a_vp;
568 struct nfsnode *np = VTONFS(vp);
571 int fmode = ap->a_mode;
575 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
579 * For NFSv4, we need to do the Open Op before cache validation,
580 * so that we conform to RFC3530 Sec. 9.3.1.
583 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
585 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
592 * Now, if this Open will be doing reading, re-validate/flush the
593 * cache, so that Close/Open coherency is maintained.
596 if (np->n_flag & NMODIFIED) {
598 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
599 if (error == EINTR || error == EIO) {
601 (void) nfsrpc_close(vp, 0, ap->a_td);
606 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
607 if (vp->v_type == VDIR)
608 np->n_direofoffset = 0;
610 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
613 (void) nfsrpc_close(vp, 0, ap->a_td);
617 np->n_mtime = vattr.va_mtime;
619 np->n_change = vattr.va_filerev;
622 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
625 (void) nfsrpc_close(vp, 0, ap->a_td);
629 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
630 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
631 if (vp->v_type == VDIR)
632 np->n_direofoffset = 0;
634 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
635 if (error == EINTR || error == EIO) {
637 (void) nfsrpc_close(vp, 0, ap->a_td);
641 np->n_mtime = vattr.va_mtime;
643 np->n_change = vattr.va_filerev;
648 * If the object has >= 1 O_DIRECT active opens, we disable caching.
650 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
651 (vp->v_type == VREG)) {
652 if (np->n_directio_opens == 0) {
654 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
657 (void) nfsrpc_close(vp, 0, ap->a_td);
661 np->n_flag |= NNONCACHE;
663 np->n_directio_opens++;
666 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
667 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
668 np->n_flag |= NWRITEOPENED;
671 * If this is an open for writing, capture a reference to the
672 * credentials, so they can be used by ncl_putpages(). Using
673 * these write credentials is preferable to the credentials of
674 * whatever thread happens to be doing the VOP_PUTPAGES() since
675 * the write RPCs are less likely to fail with EACCES.
677 if ((fmode & FWRITE) != 0) {
678 cred = np->n_writecred;
679 np->n_writecred = crhold(ap->a_cred);
686 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
689 * If the text file has been mmap'd, flush any dirty pages to the
690 * buffer cache and then...
691 * Make sure all writes are pushed to the NFS server. If this is not
692 * done, the modify time of the file can change while the text
693 * file is being executed. This will cause the process that is
694 * executing the text file to be terminated.
696 if (vp->v_writecount <= -1) {
697 if ((obj = vp->v_object) != NULL &&
698 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
699 VM_OBJECT_WLOCK(obj);
700 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
701 VM_OBJECT_WUNLOCK(obj);
704 /* Now, flush the buffer cache. */
705 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
707 /* And, finally, make sure that n_mtime is up to date. */
710 np->n_mtime = np->n_vattr.na_mtime;
718 * What an NFS client should do upon close after writing is a debatable issue.
719 * Most NFS clients push delayed writes to the server upon close, basically for
721 * 1 - So that any write errors may be reported back to the client process
722 * doing the close system call. By far the two most likely errors are
723 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
724 * 2 - To put a worst case upper bound on cache inconsistency between
725 * multiple clients for the file.
726 * There is also a consistency problem for Version 2 of the protocol w.r.t.
727 * not being able to tell if other clients are writing a file concurrently,
728 * since there is no way of knowing if the changed modify time in the reply
729 * is only due to the write for this client.
730 * (NFS Version 3 provides weak cache consistency data in the reply that
731 * should be sufficient to detect and handle this case.)
733 * The current code does the following:
734 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
735 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
736 * or commit them (this satisfies 1 and 2 except for the
737 * case where the server crashes after this close but
738 * before the commit RPC, which is felt to be "good
739 * enough". Changing the last argument to ncl_flush() to
740 * a 1 would force a commit operation, if it is felt a
741 * commit is necessary now.
742 * for NFS Version 4 - flush the dirty buffers and commit them, if
743 * nfscl_mustflush() says this is necessary.
744 * It is necessary if there is no write delegation held,
745 * in order to satisfy open/close coherency.
746 * If the file isn't cached on local stable storage,
747 * it may be necessary in order to detect "out of space"
748 * errors from the server, if the write delegation
749 * issued by the server doesn't allow the file to grow.
753 nfs_close(struct vop_close_args *ap)
755 struct vnode *vp = ap->a_vp;
756 struct nfsnode *np = VTONFS(vp);
757 struct nfsvattr nfsva;
759 int error = 0, ret, localcred = 0;
760 int fmode = ap->a_fflag;
762 if (NFSCL_FORCEDISM(vp->v_mount))
765 * During shutdown, a_cred isn't valid, so just use root.
767 if (ap->a_cred == NOCRED) {
768 cred = newnfs_getcred();
773 if (vp->v_type == VREG) {
775 * Examine and clean dirty pages, regardless of NMODIFIED.
776 * This closes a major hole in close-to-open consistency.
777 * We want to push out all dirty pages (and buffers) on
778 * close, regardless of whether they were dirtied by
779 * mmap'ed writes or via write().
781 if (nfs_clean_pages_on_close && vp->v_object) {
782 VM_OBJECT_WLOCK(vp->v_object);
783 vm_object_page_clean(vp->v_object, 0, 0, 0);
784 VM_OBJECT_WUNLOCK(vp->v_object);
787 if (np->n_flag & NMODIFIED) {
791 * Under NFSv3 we have dirty buffers to dispose of. We
792 * must flush them to the NFS server. We have the option
793 * of waiting all the way through the commit rpc or just
794 * waiting for the initial write. The default is to only
795 * wait through the initial write so the data is in the
796 * server's cache, which is roughly similar to the state
797 * a standard disk subsystem leaves the file in on close().
799 * We cannot clear the NMODIFIED bit in np->n_flag due to
800 * potential races with other processes, and certainly
801 * cannot clear it if we don't commit.
802 * These races occur when there is no longer the old
803 * traditional vnode locking implemented for Vnode Ops.
805 int cm = newnfs_commit_on_close ? 1 : 0;
806 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
807 /* np->n_flag &= ~NMODIFIED; */
808 } else if (NFS_ISV4(vp)) {
809 if (nfscl_mustflush(vp) != 0) {
810 int cm = newnfs_commit_on_close ? 1 : 0;
811 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
814 * as above w.r.t races when clearing
816 * np->n_flag &= ~NMODIFIED;
820 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
825 * Invalidate the attribute cache in all cases.
826 * An open is going to fetch fresh attrs any way, other procs
827 * on this node that have file open will be forced to do an
828 * otw attr fetch, but this is safe.
829 * --> A user found that their RPC count dropped by 20% when
830 * this was commented out and I can't see any requirement
831 * for it, so I've disabled it when negative lookups are
832 * enabled. (What does this have to do with negative lookup
833 * caching? Well nothing, except it was reported by the
834 * same user that needed negative lookup caching and I wanted
835 * there to be a way to disable it to see if it
836 * is the cause of some caching/coherency issue that might
839 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
841 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
843 if (np->n_flag & NWRITEERR) {
844 np->n_flag &= ~NWRITEERR;
852 * Get attributes so "change" is up to date.
854 if (error == 0 && nfscl_mustflush(vp) != 0 &&
855 vp->v_type == VREG &&
856 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
857 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
860 np->n_change = nfsva.na_filerev;
861 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
869 ret = nfsrpc_close(vp, 0, ap->a_td);
873 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
876 if (newnfs_directio_enable)
877 KASSERT((np->n_directio_asyncwr == 0),
878 ("nfs_close: dirty unflushed (%d) directio buffers\n",
879 np->n_directio_asyncwr));
880 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
882 KASSERT((np->n_directio_opens > 0),
883 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
884 np->n_directio_opens--;
885 if (np->n_directio_opens == 0)
886 np->n_flag &= ~NNONCACHE;
895 * nfs getattr call from vfs.
898 nfs_getattr(struct vop_getattr_args *ap)
900 struct vnode *vp = ap->a_vp;
901 struct thread *td = curthread; /* XXX */
902 struct nfsnode *np = VTONFS(vp);
904 struct nfsvattr nfsva;
905 struct vattr *vap = ap->a_vap;
909 * Update local times for special files.
912 if (np->n_flag & (NACC | NUPD))
916 * First look in the cache.
918 if (ncl_getattrcache(vp, &vattr) == 0) {
919 ncl_copy_vattr(vap, &vattr);
922 * Get the local modify time for the case of a write
925 nfscl_deleggetmodtime(vp, &vap->va_mtime);
929 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
930 nfsaccess_cache_timeout > 0) {
931 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
932 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
933 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
934 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
938 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
940 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
943 * Get the local modify time for the case of a write
946 nfscl_deleggetmodtime(vp, &vap->va_mtime);
947 } else if (NFS_ISV4(vp)) {
948 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
957 nfs_setattr(struct vop_setattr_args *ap)
959 struct vnode *vp = ap->a_vp;
960 struct nfsnode *np = VTONFS(vp);
961 struct thread *td = curthread; /* XXX */
962 struct vattr *vap = ap->a_vap;
971 * Setting of flags and marking of atimes are not supported.
973 if (vap->va_flags != VNOVAL)
977 * Disallow write attempts if the filesystem is mounted read-only.
979 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
980 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
981 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
982 (vp->v_mount->mnt_flag & MNT_RDONLY))
984 if (vap->va_size != VNOVAL) {
985 switch (vp->v_type) {
992 if (vap->va_mtime.tv_sec == VNOVAL &&
993 vap->va_atime.tv_sec == VNOVAL &&
994 vap->va_mode == (mode_t)VNOVAL &&
995 vap->va_uid == (uid_t)VNOVAL &&
996 vap->va_gid == (gid_t)VNOVAL)
998 vap->va_size = VNOVAL;
1002 * Disallow write attempts if the filesystem is
1003 * mounted read-only.
1005 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1008 * We run vnode_pager_setsize() early (why?),
1009 * we must set np->n_size now to avoid vinvalbuf
1010 * V_SAVE races that might setsize a lower
1016 error = ncl_meta_setsize(vp, td, vap->va_size);
1018 if (np->n_flag & NMODIFIED) {
1021 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1024 vnode_pager_setsize(vp, tsize);
1028 * Call nfscl_delegmodtime() to set the modify time
1029 * locally, as required.
1031 nfscl_delegmodtime(vp);
1035 * np->n_size has already been set to vap->va_size
1036 * in ncl_meta_setsize(). We must set it again since
1037 * nfs_loadattrcache() could be called through
1038 * ncl_meta_setsize() and could modify np->n_size.
1041 np->n_vattr.na_size = np->n_size = vap->va_size;
1046 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1047 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1049 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1050 if (error == EINTR || error == EIO)
1055 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1056 if (error && vap->va_size != VNOVAL) {
1058 np->n_size = np->n_vattr.na_size = tsize;
1059 vnode_pager_setsize(vp, tsize);
1066 * Do an nfs setattr rpc.
1069 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1072 struct nfsnode *np = VTONFS(vp);
1073 int error, ret, attrflag, i;
1074 struct nfsvattr nfsva;
1076 if (NFS_ISV34(vp)) {
1078 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1079 np->n_accesscache[i].stamp = 0;
1080 np->n_flag |= NDELEGMOD;
1082 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1084 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1087 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1091 if (error && NFS_ISV4(vp))
1092 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1097 * nfs lookup call, one step at a time...
1098 * First look in cache
1099 * If not found, unlock the directory nfsnode and do the rpc
1102 nfs_lookup(struct vop_lookup_args *ap)
1104 struct componentname *cnp = ap->a_cnp;
1105 struct vnode *dvp = ap->a_dvp;
1106 struct vnode **vpp = ap->a_vpp;
1107 struct mount *mp = dvp->v_mount;
1108 int flags = cnp->cn_flags;
1109 struct vnode *newvp;
1110 struct nfsmount *nmp;
1111 struct nfsnode *np, *newnp;
1112 int error = 0, attrflag, dattrflag, ltype, ncticks;
1113 struct thread *td = cnp->cn_thread;
1115 struct nfsvattr dnfsva, nfsva;
1117 struct timespec nctime;
1120 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1121 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1123 if (dvp->v_type != VDIR)
1128 /* For NFSv4, wait until any remove is done. */
1130 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1131 np->n_flag |= NREMOVEWANT;
1132 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1136 error = vn_dir_check_exec(dvp, cnp);
1139 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1140 if (error > 0 && error != ENOENT)
1144 * Lookups of "." are special and always return the
1145 * current directory. cache_lookup() already handles
1146 * associated locking bookkeeping, etc.
1148 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1149 /* XXX: Is this really correct? */
1150 if (cnp->cn_nameiop != LOOKUP &&
1152 cnp->cn_flags |= SAVENAME;
1157 * We only accept a positive hit in the cache if the
1158 * change time of the file matches our cached copy.
1159 * Otherwise, we discard the cache entry and fallback
1160 * to doing a lookup RPC. We also only trust cache
1161 * entries for less than nm_nametimeo seconds.
1163 * To better handle stale file handles and attributes,
1164 * clear the attribute cache of this node if it is a
1165 * leaf component, part of an open() call, and not
1166 * locally modified before fetching the attributes.
1167 * This should allow stale file handles to be detected
1168 * here where we can fall back to a LOOKUP RPC to
1169 * recover rather than having nfs_open() detect the
1170 * stale file handle and failing open(2) with ESTALE.
1173 newnp = VTONFS(newvp);
1174 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1175 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1176 !(newnp->n_flag & NMODIFIED)) {
1178 newnp->n_attrstamp = 0;
1179 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1180 NFSUNLOCKNODE(newnp);
1182 if (nfscl_nodeleg(newvp, 0) == 0 ||
1183 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1184 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1185 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1186 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1187 if (cnp->cn_nameiop != LOOKUP &&
1189 cnp->cn_flags |= SAVENAME;
1198 } else if (error == ENOENT) {
1199 if (dvp->v_iflag & VI_DOOMED)
1202 * We only accept a negative hit in the cache if the
1203 * modification time of the parent directory matches
1204 * the cached copy in the name cache entry.
1205 * Otherwise, we discard all of the negative cache
1206 * entries for this directory. We also only trust
1207 * negative cache entries for up to nm_negnametimeo
1210 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1211 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1212 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1213 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1216 cache_purge_negative(dvp);
1221 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1222 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1223 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1226 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1228 if (newvp != NULLVP) {
1233 if (error != ENOENT) {
1235 error = nfscl_maperr(td, error, (uid_t)0,
1240 /* The requested file was not found. */
1241 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1242 (flags & ISLASTCN)) {
1244 * XXX: UFS does a full VOP_ACCESS(dvp,
1245 * VWRITE) here instead of just checking
1248 if (mp->mnt_flag & MNT_RDONLY)
1250 cnp->cn_flags |= SAVENAME;
1251 return (EJUSTRETURN);
1254 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1256 * Cache the modification time of the parent
1257 * directory from the post-op attributes in
1258 * the name cache entry. The negative cache
1259 * entry will be ignored once the directory
1260 * has changed. Don't bother adding the entry
1261 * if the directory has already changed.
1264 if (timespeccmp(&np->n_vattr.na_mtime,
1265 &dnfsva.na_mtime, ==)) {
1267 cache_enter_time(dvp, NULL, cnp,
1268 &dnfsva.na_mtime, NULL);
1276 * Handle RENAME case...
1278 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1279 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1280 free(nfhp, M_NFSFH);
1283 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1289 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1292 cnp->cn_flags |= SAVENAME;
1296 if (flags & ISDOTDOT) {
1297 ltype = NFSVOPISLOCKED(dvp);
1298 error = vfs_busy(mp, MBF_NOWAIT);
1301 NFSVOPUNLOCK(dvp, 0);
1302 error = vfs_busy(mp, 0);
1303 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1305 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1312 NFSVOPUNLOCK(dvp, 0);
1313 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1319 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1320 if (dvp->v_iflag & VI_DOOMED) {
1332 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1334 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1335 free(nfhp, M_NFSFH);
1339 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1342 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1348 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1350 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1351 !(np->n_flag & NMODIFIED)) {
1353 * Flush the attribute cache when opening a
1354 * leaf node to ensure that fresh attributes
1355 * are fetched in nfs_open() since we did not
1356 * fetch attributes from the LOOKUP reply.
1359 np->n_attrstamp = 0;
1360 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1364 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1365 cnp->cn_flags |= SAVENAME;
1366 if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1367 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1368 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1369 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1370 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1377 * Just call ncl_bioread() to do the work.
1380 nfs_read(struct vop_read_args *ap)
1382 struct vnode *vp = ap->a_vp;
1384 switch (vp->v_type) {
1386 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1390 return (EOPNOTSUPP);
1398 nfs_readlink(struct vop_readlink_args *ap)
1400 struct vnode *vp = ap->a_vp;
1402 if (vp->v_type != VLNK)
1404 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1408 * Do a readlink rpc.
1409 * Called by ncl_doio() from below the buffer cache.
1412 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1414 int error, ret, attrflag;
1415 struct nfsvattr nfsva;
1417 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1420 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1424 if (error && NFS_ISV4(vp))
1425 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1434 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1436 int error, ret, attrflag;
1437 struct nfsvattr nfsva;
1438 struct nfsmount *nmp;
1440 nmp = VFSTONFS(vnode_mount(vp));
1443 if (NFSHASPNFS(nmp))
1444 error = nfscl_doiods(vp, uiop, NULL, NULL,
1445 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1446 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1448 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1451 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1455 if (error && NFS_ISV4(vp))
1456 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1464 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1465 int *iomode, int *must_commit, int called_from_strategy)
1467 struct nfsvattr nfsva;
1468 int error, attrflag, ret;
1469 struct nfsmount *nmp;
1471 nmp = VFSTONFS(vnode_mount(vp));
1474 if (NFSHASPNFS(nmp))
1475 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1476 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1477 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1479 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1480 uiop->uio_td, &nfsva, &attrflag, NULL,
1481 called_from_strategy);
1483 if (VTONFS(vp)->n_flag & ND_NFSV4)
1484 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1487 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1493 *iomode = NFSWRITE_FILESYNC;
1494 if (error && NFS_ISV4(vp))
1495 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1501 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1502 * mode set to specify the file type and the size field for rdev.
1505 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1508 struct nfsvattr nfsva, dnfsva;
1509 struct vnode *newvp = NULL;
1510 struct nfsnode *np = NULL, *dnp;
1513 int error = 0, attrflag, dattrflag;
1516 if (vap->va_type == VCHR || vap->va_type == VBLK)
1517 rdev = vap->va_rdev;
1518 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1521 return (EOPNOTSUPP);
1522 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1524 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1525 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1526 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1529 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1530 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1531 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1534 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1535 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1538 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1541 if (attrflag != 0) {
1542 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1550 } else if (NFS_ISV4(dvp)) {
1551 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1556 dnp->n_flag |= NMODIFIED;
1558 dnp->n_attrstamp = 0;
1559 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1567 * just call nfs_mknodrpc() to do the work.
1571 nfs_mknod(struct vop_mknod_args *ap)
1573 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1576 static struct mtx nfs_cverf_mtx;
1577 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1583 static nfsquad_t cverf;
1585 static int cverf_initialized = 0;
1587 mtx_lock(&nfs_cverf_mtx);
1588 if (cverf_initialized == 0) {
1589 cverf.lval[0] = arc4random();
1590 cverf.lval[1] = arc4random();
1591 cverf_initialized = 1;
1595 mtx_unlock(&nfs_cverf_mtx);
1601 * nfs file create call
1604 nfs_create(struct vop_create_args *ap)
1606 struct vnode *dvp = ap->a_dvp;
1607 struct vattr *vap = ap->a_vap;
1608 struct componentname *cnp = ap->a_cnp;
1609 struct nfsnode *np = NULL, *dnp;
1610 struct vnode *newvp = NULL;
1611 struct nfsmount *nmp;
1612 struct nfsvattr dnfsva, nfsva;
1615 int error = 0, attrflag, dattrflag, fmode = 0;
1619 * Oops, not for me..
1621 if (vap->va_type == VSOCK)
1622 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1624 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1626 if (vap->va_vaflags & VA_EXCLUSIVE)
1629 nmp = VFSTONFS(vnode_mount(dvp));
1631 /* For NFSv4, wait until any remove is done. */
1633 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1634 dnp->n_flag |= NREMOVEWANT;
1635 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1639 cverf = nfs_get_cverf();
1640 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1641 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1642 &nfhp, &attrflag, &dattrflag, NULL);
1645 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1646 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1647 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1650 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1651 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1654 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1658 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1659 cnp->cn_thread, &nfsva, NULL);
1661 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1665 if (newvp != NULL) {
1669 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1670 error == NFSERR_NOTSUPP) {
1674 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1675 if (nfscl_checksattr(vap, &nfsva)) {
1676 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1677 cnp->cn_thread, &nfsva, &attrflag, NULL);
1678 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1679 vap->va_gid != (gid_t)VNOVAL)) {
1680 /* try again without setting uid/gid */
1681 vap->va_uid = (uid_t)VNOVAL;
1682 vap->va_gid = (uid_t)VNOVAL;
1683 error = nfsrpc_setattr(newvp, vap, NULL,
1684 cnp->cn_cred, cnp->cn_thread, &nfsva,
1688 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1695 if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1697 cache_enter_time(dvp, newvp, cnp,
1698 &nfsva.na_ctime, NULL);
1700 printf("nfs_create: bogus NFS server returned "
1701 "the directory as the new file object\n");
1704 } else if (NFS_ISV4(dvp)) {
1705 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1709 dnp->n_flag |= NMODIFIED;
1711 dnp->n_attrstamp = 0;
1712 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1719 * nfs file remove call
1720 * To try and make nfs semantics closer to ufs semantics, a file that has
1721 * other processes using the vnode is renamed instead of removed and then
1722 * removed later on the last close.
1723 * - If v_usecount > 1
1724 * If a rename is not already in the works
1725 * call nfs_sillyrename() to set it up
1730 nfs_remove(struct vop_remove_args *ap)
1732 struct vnode *vp = ap->a_vp;
1733 struct vnode *dvp = ap->a_dvp;
1734 struct componentname *cnp = ap->a_cnp;
1735 struct nfsnode *np = VTONFS(vp);
1739 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1740 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1741 if (vp->v_type == VDIR)
1743 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1744 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1745 vattr.va_nlink > 1)) {
1747 * Purge the name cache so that the chance of a lookup for
1748 * the name succeeding while the remove is in progress is
1749 * minimized. Without node locking it can still happen, such
1750 * that an I/O op returns ESTALE, but since you get this if
1751 * another host removes the file..
1755 * throw away biocache buffers, mainly to avoid
1756 * unnecessary delayed writes later.
1758 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1759 if (error != EINTR && error != EIO)
1761 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1762 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1764 * Kludge City: If the first reply to the remove rpc is lost..
1765 * the reply to the retransmitted request will be ENOENT
1766 * since the file was in fact removed
1767 * Therefore, we cheat and return success.
1769 if (error == ENOENT)
1771 } else if (!np->n_sillyrename)
1772 error = nfs_sillyrename(dvp, vp, cnp);
1774 np->n_attrstamp = 0;
1776 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1781 * nfs file remove rpc called from nfs_inactive
1784 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1787 * Make sure that the directory vnode is still valid.
1788 * XXX we should lock sp->s_dvp here.
1790 if (sp->s_dvp->v_type == VBAD)
1792 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1797 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1800 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1801 int namelen, struct ucred *cred, struct thread *td)
1803 struct nfsvattr dnfsva;
1804 struct nfsnode *dnp = VTONFS(dvp);
1805 int error = 0, dattrflag;
1808 dnp->n_flag |= NREMOVEINPROG;
1810 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1813 if ((dnp->n_flag & NREMOVEWANT)) {
1814 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1816 wakeup((caddr_t)dnp);
1818 dnp->n_flag &= ~NREMOVEINPROG;
1822 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1824 dnp->n_flag |= NMODIFIED;
1826 dnp->n_attrstamp = 0;
1827 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1830 if (error && NFS_ISV4(dvp))
1831 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1836 * nfs file rename call
1839 nfs_rename(struct vop_rename_args *ap)
1841 struct vnode *fvp = ap->a_fvp;
1842 struct vnode *tvp = ap->a_tvp;
1843 struct vnode *fdvp = ap->a_fdvp;
1844 struct vnode *tdvp = ap->a_tdvp;
1845 struct componentname *tcnp = ap->a_tcnp;
1846 struct componentname *fcnp = ap->a_fcnp;
1847 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1848 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1849 struct nfsv4node *newv4 = NULL;
1852 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1853 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1854 /* Check for cross-device rename */
1855 if ((fvp->v_mount != tdvp->v_mount) ||
1856 (tvp && (fvp->v_mount != tvp->v_mount))) {
1862 printf("nfs_rename: fvp == tvp (can't happen)\n");
1866 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1870 * We have to flush B_DELWRI data prior to renaming
1871 * the file. If we don't, the delayed-write buffers
1872 * can be flushed out later after the file has gone stale
1873 * under NFSV3. NFSV2 does not have this problem because
1874 * ( as far as I can tell ) it flushes dirty buffers more
1877 * Skip the rename operation if the fsync fails, this can happen
1878 * due to the server's volume being full, when we pushed out data
1879 * that was written back to our cache earlier. Not checking for
1880 * this condition can result in potential (silent) data loss.
1882 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1883 NFSVOPUNLOCK(fvp, 0);
1885 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1890 * If the tvp exists and is in use, sillyrename it before doing the
1891 * rename of the new file over it.
1892 * XXX Can't sillyrename a directory.
1894 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1895 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1900 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1901 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1904 if (error == 0 && NFS_ISV4(tdvp)) {
1906 * For NFSv4, check to see if it is the same name and
1907 * replace the name, if it is different.
1910 sizeof (struct nfsv4node) +
1911 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1912 M_NFSV4NODE, M_WAITOK);
1915 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1916 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1917 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1918 tcnp->cn_namelen) ||
1919 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1920 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1921 tdnp->n_fhp->nfh_len))) {
1923 { char nnn[100]; int nnnl;
1924 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1925 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1927 printf("ren replace=%s\n",nnn);
1930 free(fnp->n_v4, M_NFSV4NODE);
1933 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1934 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1935 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1936 tdnp->n_fhp->nfh_len);
1937 NFSBCOPY(tcnp->cn_nameptr,
1938 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1940 NFSUNLOCKNODE(tdnp);
1943 free(newv4, M_NFSV4NODE);
1946 if (fvp->v_type == VDIR) {
1947 if (tvp != NULL && tvp->v_type == VDIR)
1962 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1964 if (error == ENOENT)
1970 * nfs file rename rpc called from nfs_remove() above
1973 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1974 struct sillyrename *sp)
1977 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1978 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1983 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1986 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1987 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1988 int tnamelen, struct ucred *cred, struct thread *td)
1990 struct nfsvattr fnfsva, tnfsva;
1991 struct nfsnode *fdnp = VTONFS(fdvp);
1992 struct nfsnode *tdnp = VTONFS(tdvp);
1993 int error = 0, fattrflag, tattrflag;
1995 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1996 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1997 &tattrflag, NULL, NULL);
1999 fdnp->n_flag |= NMODIFIED;
2000 if (fattrflag != 0) {
2001 NFSUNLOCKNODE(fdnp);
2002 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
2004 fdnp->n_attrstamp = 0;
2005 NFSUNLOCKNODE(fdnp);
2006 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2009 tdnp->n_flag |= NMODIFIED;
2010 if (tattrflag != 0) {
2011 NFSUNLOCKNODE(tdnp);
2012 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
2014 tdnp->n_attrstamp = 0;
2015 NFSUNLOCKNODE(tdnp);
2016 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2018 if (error && NFS_ISV4(fdvp))
2019 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2024 * nfs hard link create call
2027 nfs_link(struct vop_link_args *ap)
2029 struct vnode *vp = ap->a_vp;
2030 struct vnode *tdvp = ap->a_tdvp;
2031 struct componentname *cnp = ap->a_cnp;
2032 struct nfsnode *np, *tdnp;
2033 struct nfsvattr nfsva, dnfsva;
2034 int error = 0, attrflag, dattrflag;
2037 * Push all writes to the server, so that the attribute cache
2038 * doesn't get "out of sync" with the server.
2039 * XXX There should be a better way!
2041 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
2043 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2044 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
2046 tdnp = VTONFS(tdvp);
2048 tdnp->n_flag |= NMODIFIED;
2049 if (dattrflag != 0) {
2050 NFSUNLOCKNODE(tdnp);
2051 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
2053 tdnp->n_attrstamp = 0;
2054 NFSUNLOCKNODE(tdnp);
2055 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2058 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2062 np->n_attrstamp = 0;
2064 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2067 * If negative lookup caching is enabled, I might as well
2068 * add an entry for this node. Not necessary for correctness,
2069 * but if negative caching is enabled, then the system
2070 * must care about lookup caching hit rate, so...
2072 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2073 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2075 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2077 printf("nfs_link: bogus NFS server returned "
2078 "the directory as the new link\n");
2080 if (error && NFS_ISV4(vp))
2081 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2087 * nfs symbolic link create call
2090 nfs_symlink(struct vop_symlink_args *ap)
2092 struct vnode *dvp = ap->a_dvp;
2093 struct vattr *vap = ap->a_vap;
2094 struct componentname *cnp = ap->a_cnp;
2095 struct nfsvattr nfsva, dnfsva;
2097 struct nfsnode *np = NULL, *dnp;
2098 struct vnode *newvp = NULL;
2099 int error = 0, attrflag, dattrflag, ret;
2101 vap->va_type = VLNK;
2102 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2103 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2104 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2106 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2107 &np, NULL, LK_EXCLUSIVE);
2113 if (newvp != NULL) {
2115 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2117 } else if (!error) {
2119 * If we do not have an error and we could not extract the
2120 * newvp from the response due to the request being NFSv2, we
2121 * have to do a lookup in order to obtain a newvp to return.
2123 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2124 cnp->cn_cred, cnp->cn_thread, &np);
2132 error = nfscl_maperr(cnp->cn_thread, error,
2133 vap->va_uid, vap->va_gid);
2140 dnp->n_flag |= NMODIFIED;
2141 if (dattrflag != 0) {
2143 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2145 dnp->n_attrstamp = 0;
2147 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2150 * If negative lookup caching is enabled, I might as well
2151 * add an entry for this node. Not necessary for correctness,
2152 * but if negative caching is enabled, then the system
2153 * must care about lookup caching hit rate, so...
2155 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2156 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2158 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2161 printf("nfs_symlink: bogus NFS server returned "
2162 "the directory as the new file object\n");
2171 nfs_mkdir(struct vop_mkdir_args *ap)
2173 struct vnode *dvp = ap->a_dvp;
2174 struct vattr *vap = ap->a_vap;
2175 struct componentname *cnp = ap->a_cnp;
2176 struct nfsnode *np = NULL, *dnp;
2177 struct vnode *newvp = NULL;
2180 struct nfsvattr nfsva, dnfsva;
2181 int error = 0, attrflag, dattrflag, ret;
2183 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2185 vap->va_type = VDIR;
2186 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2187 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2188 &attrflag, &dattrflag, NULL);
2191 dnp->n_flag |= NMODIFIED;
2192 if (dattrflag != 0) {
2194 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2196 dnp->n_attrstamp = 0;
2198 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2201 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2202 &np, NULL, LK_EXCLUSIVE);
2206 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2211 if (!error && newvp == NULL) {
2212 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2213 cnp->cn_cred, cnp->cn_thread, &np);
2216 if (newvp->v_type != VDIR)
2224 error = nfscl_maperr(cnp->cn_thread, error,
2225 vap->va_uid, vap->va_gid);
2228 * If negative lookup caching is enabled, I might as well
2229 * add an entry for this node. Not necessary for correctness,
2230 * but if negative caching is enabled, then the system
2231 * must care about lookup caching hit rate, so...
2233 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2234 (cnp->cn_flags & MAKEENTRY) &&
2235 attrflag != 0 && dattrflag != 0) {
2237 cache_enter_time(dvp, newvp, cnp,
2238 &nfsva.na_ctime, &dnfsva.na_ctime);
2240 printf("nfs_mkdir: bogus NFS server returned "
2241 "the directory that the directory was "
2242 "created in as the new file object\n");
2250 * nfs remove directory call
2253 nfs_rmdir(struct vop_rmdir_args *ap)
2255 struct vnode *vp = ap->a_vp;
2256 struct vnode *dvp = ap->a_dvp;
2257 struct componentname *cnp = ap->a_cnp;
2258 struct nfsnode *dnp;
2259 struct nfsvattr dnfsva;
2260 int error, dattrflag;
2264 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2265 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2268 dnp->n_flag |= NMODIFIED;
2269 if (dattrflag != 0) {
2271 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2273 dnp->n_attrstamp = 0;
2275 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2280 if (error && NFS_ISV4(dvp))
2281 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2284 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2286 if (error == ENOENT)
2295 nfs_readdir(struct vop_readdir_args *ap)
2297 struct vnode *vp = ap->a_vp;
2298 struct nfsnode *np = VTONFS(vp);
2299 struct uio *uio = ap->a_uio;
2300 ssize_t tresid, left;
2304 if (ap->a_eofflag != NULL)
2306 if (vp->v_type != VDIR)
2310 * First, check for hit on the EOF offset cache
2313 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2314 (np->n_flag & NMODIFIED) == 0) {
2316 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2318 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2319 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2321 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2322 if (ap->a_eofflag != NULL)
2332 * NFS always guarantees that directory entries don't straddle
2333 * DIRBLKSIZ boundaries. As such, we need to limit the size
2334 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2337 left = uio->uio_resid % DIRBLKSIZ;
2338 if (left == uio->uio_resid)
2340 uio->uio_resid -= left;
2343 * Call ncl_bioread() to do the real work.
2345 tresid = uio->uio_resid;
2346 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2348 if (!error && uio->uio_resid == tresid) {
2349 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2350 if (ap->a_eofflag != NULL)
2354 /* Add the partial DIRBLKSIZ (left) back in. */
2355 uio->uio_resid += left;
2361 * Called from below the buffer cache by ncl_doio().
2364 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2367 struct nfsvattr nfsva;
2368 nfsuint64 *cookiep, cookie;
2369 struct nfsnode *dnp = VTONFS(vp);
2370 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2371 int error = 0, eof, attrflag;
2373 KASSERT(uiop->uio_iovcnt == 1 &&
2374 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2375 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2376 ("nfs readdirrpc bad uio"));
2379 * If there is no cookie, assume directory was stale.
2381 ncl_dircookie_lock(dnp);
2383 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2386 ncl_dircookie_unlock(dnp);
2388 ncl_dircookie_unlock(dnp);
2389 return (NFSERR_BAD_COOKIE);
2392 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2393 (void)ncl_fsinfo(nmp, vp, cred, td);
2395 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2396 &attrflag, &eof, NULL);
2398 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2402 * We are now either at the end of the directory or have filled
2407 dnp->n_direofoffset = uiop->uio_offset;
2410 if (uiop->uio_resid > 0)
2411 printf("EEK! readdirrpc resid > 0\n");
2412 ncl_dircookie_lock(dnp);
2414 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2416 ncl_dircookie_unlock(dnp);
2418 } else if (NFS_ISV4(vp)) {
2419 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2425 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2428 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2431 struct nfsvattr nfsva;
2432 nfsuint64 *cookiep, cookie;
2433 struct nfsnode *dnp = VTONFS(vp);
2434 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2435 int error = 0, attrflag, eof;
2437 KASSERT(uiop->uio_iovcnt == 1 &&
2438 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2439 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2440 ("nfs readdirplusrpc bad uio"));
2443 * If there is no cookie, assume directory was stale.
2445 ncl_dircookie_lock(dnp);
2447 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2450 ncl_dircookie_unlock(dnp);
2452 ncl_dircookie_unlock(dnp);
2453 return (NFSERR_BAD_COOKIE);
2456 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2457 (void)ncl_fsinfo(nmp, vp, cred, td);
2458 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2459 &attrflag, &eof, NULL);
2461 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2465 * We are now either at end of the directory or have filled the
2470 dnp->n_direofoffset = uiop->uio_offset;
2473 if (uiop->uio_resid > 0)
2474 printf("EEK! readdirplusrpc resid > 0\n");
2475 ncl_dircookie_lock(dnp);
2477 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2479 ncl_dircookie_unlock(dnp);
2481 } else if (NFS_ISV4(vp)) {
2482 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2488 * Silly rename. To make the NFS filesystem that is stateless look a little
2489 * more like the "ufs" a remove of an active vnode is translated to a rename
2490 * to a funny looking filename that is removed by nfs_inactive on the
2491 * nfsnode. There is the potential for another process on a different client
2492 * to create the same funny name between the nfs_lookitup() fails and the
2493 * nfs_rename() completes, but...
2496 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2498 struct sillyrename *sp;
2502 unsigned int lticks;
2506 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2507 sp = malloc(sizeof (struct sillyrename),
2508 M_NEWNFSREQ, M_WAITOK);
2509 sp->s_cred = crhold(cnp->cn_cred);
2514 * Fudge together a funny name.
2515 * Changing the format of the funny name to accommodate more
2516 * sillynames per directory.
2517 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2518 * CPU ticks since boot.
2520 pid = cnp->cn_thread->td_proc->p_pid;
2521 lticks = (unsigned int)ticks;
2523 sp->s_namlen = sprintf(sp->s_name,
2524 ".nfs.%08x.%04x4.4", lticks,
2526 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2527 cnp->cn_thread, NULL))
2531 error = nfs_renameit(dvp, vp, cnp, sp);
2534 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2535 cnp->cn_thread, &np);
2536 np->n_sillyrename = sp;
2541 free(sp, M_NEWNFSREQ);
2546 * Look up a file name and optionally either update the file handle or
2547 * allocate an nfsnode, depending on the value of npp.
2548 * npp == NULL --> just do the lookup
2549 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2551 * *npp != NULL --> update the file handle in the vnode
2554 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2555 struct thread *td, struct nfsnode **npp)
2557 struct vnode *newvp = NULL, *vp;
2558 struct nfsnode *np, *dnp = VTONFS(dvp);
2559 struct nfsfh *nfhp, *onfhp;
2560 struct nfsvattr nfsva, dnfsva;
2561 struct componentname cn;
2562 int error = 0, attrflag, dattrflag;
2565 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2566 &nfhp, &attrflag, &dattrflag, NULL);
2568 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2569 if (npp && !error) {
2574 * For NFSv4, check to see if it is the same name and
2575 * replace the name, if it is different.
2577 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2578 (np->n_v4->n4_namelen != len ||
2579 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2580 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2581 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2582 dnp->n_fhp->nfh_len))) {
2584 { char nnn[100]; int nnnl;
2585 nnnl = (len < 100) ? len : 99;
2586 bcopy(name, nnn, nnnl);
2588 printf("replace=%s\n",nnn);
2591 free(np->n_v4, M_NFSV4NODE);
2593 sizeof (struct nfsv4node) +
2594 dnp->n_fhp->nfh_len + len - 1,
2595 M_NFSV4NODE, M_WAITOK);
2596 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2597 np->n_v4->n4_namelen = len;
2598 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2599 dnp->n_fhp->nfh_len);
2600 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2602 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2606 * Rehash node for new file handle.
2608 vfs_hash_rehash(vp, hash);
2611 free(onfhp, M_NFSFH);
2613 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2614 free(nfhp, M_NFSFH);
2618 cn.cn_nameptr = name;
2619 cn.cn_namelen = len;
2620 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2621 &np, NULL, LK_EXCLUSIVE);
2626 if (!attrflag && *npp == NULL) {
2634 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2637 if (npp && *npp == NULL) {
2648 if (error && NFS_ISV4(dvp))
2649 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2654 * Nfs Version 3 and 4 commit rpc
2657 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2660 struct nfsvattr nfsva;
2661 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2664 int error, attrflag;
2669 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2670 uio.uio_offset = offset;
2671 uio.uio_resid = cnt;
2672 error = nfscl_doiods(vp, &uio, NULL, NULL,
2673 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2676 np->n_flag &= ~NDSCOMMIT;
2681 mtx_lock(&nmp->nm_mtx);
2682 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2683 mtx_unlock(&nmp->nm_mtx);
2686 mtx_unlock(&nmp->nm_mtx);
2687 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2691 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2693 if (error != 0 && NFS_ISV4(vp))
2694 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2700 * For async requests when nfsiod(s) are running, queue the request by
2701 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2705 nfs_strategy(struct vop_strategy_args *ap)
2713 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2714 KASSERT(!(bp->b_flags & B_DONE),
2715 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2716 BUF_ASSERT_HELD(bp);
2718 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2719 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2721 if (bp->b_iocmd == BIO_READ)
2727 * If the op is asynchronous and an i/o daemon is waiting
2728 * queue the request, wake it up and wait for completion
2729 * otherwise just do it ourselves.
2731 if ((bp->b_flags & B_ASYNC) == 0 ||
2732 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2733 (void) ncl_doio(vp, bp, cr, curthread, 1);
2738 * fsync vnode op. Just call ncl_flush() with commit == 1.
2742 nfs_fsync(struct vop_fsync_args *ap)
2745 if (ap->a_vp->v_type != VREG) {
2747 * For NFS, metadata is changed synchronously on the server,
2748 * so there is nothing to flush. Also, ncl_flush() clears
2749 * the NMODIFIED flag and that shouldn't be done here for
2754 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2758 * Flush all the blocks associated with a vnode.
2759 * Walk through the buffer pool and push any dirty pages
2760 * associated with the vnode.
2761 * If the called_from_renewthread argument is TRUE, it has been called
2762 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2763 * waiting for a buffer write to complete.
2766 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2767 int commit, int called_from_renewthread)
2769 struct nfsnode *np = VTONFS(vp);
2773 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2774 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2775 int passone = 1, trycnt = 0;
2776 u_quad_t off, endoff, toff;
2777 struct ucred* wcred = NULL;
2778 struct buf **bvec = NULL;
2780 #ifndef NFS_COMMITBVECSIZ
2781 #define NFS_COMMITBVECSIZ 20
2783 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2784 u_int bvecsize = 0, bveccount;
2786 if (called_from_renewthread != 0)
2788 if (nmp->nm_flag & NFSMNT_INT)
2794 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2795 * server, but has not been committed to stable storage on the server
2796 * yet. On the first pass, the byte range is worked out and the commit
2797 * rpc is done. On the second pass, ncl_writebp() is called to do the
2804 if (NFS_ISV34(vp) && commit) {
2805 if (bvec != NULL && bvec != bvec_on_stack)
2808 * Count up how many buffers waiting for a commit.
2812 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2813 if (!BUF_ISLOCKED(bp) &&
2814 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2815 == (B_DELWRI | B_NEEDCOMMIT))
2819 * Allocate space to remember the list of bufs to commit. It is
2820 * important to use M_NOWAIT here to avoid a race with nfs_write.
2821 * If we can't get memory (for whatever reason), we will end up
2822 * committing the buffers one-by-one in the loop below.
2824 if (bveccount > NFS_COMMITBVECSIZ) {
2826 * Release the vnode interlock to avoid a lock
2830 bvec = (struct buf **)
2831 malloc(bveccount * sizeof(struct buf *),
2835 bvec = bvec_on_stack;
2836 bvecsize = NFS_COMMITBVECSIZ;
2838 bvecsize = bveccount;
2840 bvec = bvec_on_stack;
2841 bvecsize = NFS_COMMITBVECSIZ;
2843 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2844 if (bvecpos >= bvecsize)
2846 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2847 nbp = TAILQ_NEXT(bp, b_bobufs);
2850 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2851 (B_DELWRI | B_NEEDCOMMIT)) {
2853 nbp = TAILQ_NEXT(bp, b_bobufs);
2859 * Work out if all buffers are using the same cred
2860 * so we can deal with them all with one commit.
2862 * NOTE: we are not clearing B_DONE here, so we have
2863 * to do it later on in this routine if we intend to
2864 * initiate I/O on the bp.
2866 * Note: to avoid loopback deadlocks, we do not
2867 * assign b_runningbufspace.
2870 wcred = bp->b_wcred;
2871 else if (wcred != bp->b_wcred)
2873 vfs_busy_pages(bp, 1);
2877 * bp is protected by being locked, but nbp is not
2878 * and vfs_busy_pages() may sleep. We have to
2881 nbp = TAILQ_NEXT(bp, b_bobufs);
2884 * A list of these buffers is kept so that the
2885 * second loop knows which buffers have actually
2886 * been committed. This is necessary, since there
2887 * may be a race between the commit rpc and new
2888 * uncommitted writes on the file.
2890 bvec[bvecpos++] = bp;
2891 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2895 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2903 * Commit data on the server, as required.
2904 * If all bufs are using the same wcred, then use that with
2905 * one call for all of them, otherwise commit each one
2908 if (wcred != NOCRED)
2909 retv = ncl_commit(vp, off, (int)(endoff - off),
2913 for (i = 0; i < bvecpos; i++) {
2916 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2918 size = (u_quad_t)(bp->b_dirtyend
2920 retv = ncl_commit(vp, off, (int)size,
2926 if (retv == NFSERR_STALEWRITEVERF)
2927 ncl_clearcommit(vp->v_mount);
2930 * Now, either mark the blocks I/O done or mark the
2931 * blocks dirty, depending on whether the commit
2934 for (i = 0; i < bvecpos; i++) {
2936 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2939 * Error, leave B_DELWRI intact
2941 vfs_unbusy_pages(bp);
2945 * Success, remove B_DELWRI ( bundirty() ).
2947 * b_dirtyoff/b_dirtyend seem to be NFS
2948 * specific. We should probably move that
2949 * into bundirty(). XXX
2952 bp->b_flags |= B_ASYNC;
2954 bp->b_flags &= ~B_DONE;
2955 bp->b_ioflags &= ~BIO_ERROR;
2956 bp->b_dirtyoff = bp->b_dirtyend = 0;
2963 * Start/do any write(s) that are required.
2967 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2968 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2969 if (waitfor != MNT_WAIT || passone)
2972 error = BUF_TIMELOCK(bp,
2973 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2974 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2979 if (error == ENOLCK) {
2983 if (called_from_renewthread != 0) {
2985 * Return EIO so the flush will be retried
2991 if (newnfs_sigintr(nmp, td)) {
2995 if (slpflag == PCATCH) {
3001 if ((bp->b_flags & B_DELWRI) == 0)
3002 panic("nfs_fsync: not dirty");
3003 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3009 if (passone || !commit)
3010 bp->b_flags |= B_ASYNC;
3012 bp->b_flags |= B_ASYNC;
3014 if (newnfs_sigintr(nmp, td)) {
3025 if (waitfor == MNT_WAIT) {
3026 while (bo->bo_numoutput) {
3027 error = bufobj_wwait(bo, slpflag, slptimeo);
3030 if (called_from_renewthread != 0) {
3032 * Return EIO so that the flush will be
3038 error = newnfs_sigintr(nmp, td);
3041 if (slpflag == PCATCH) {
3048 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3053 * Wait for all the async IO requests to drain
3057 while (np->n_directio_asyncwr > 0) {
3058 np->n_flag |= NFSYNCWAIT;
3059 error = newnfs_msleep(td, &np->n_directio_asyncwr,
3060 &np->n_mtx, slpflag | (PRIBIO + 1),
3063 if (newnfs_sigintr(nmp, td)) {
3073 if (NFSHASPNFS(nmp)) {
3074 nfscl_layoutcommit(vp, td);
3076 * Invalidate the attribute cache, since writes to a DS
3077 * won't update the size attribute.
3080 np->n_attrstamp = 0;
3083 if (np->n_flag & NWRITEERR) {
3084 error = np->n_error;
3085 np->n_flag &= ~NWRITEERR;
3087 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3088 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3089 np->n_flag &= ~NMODIFIED;
3092 if (bvec != NULL && bvec != bvec_on_stack)
3094 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3095 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3096 np->n_directio_asyncwr != 0)) {
3098 /* try, try again... */
3105 vn_printf(vp, "ncl_flush failed");
3106 error = called_from_renewthread != 0 ? EIO : EBUSY;
3112 * NFS advisory byte-level locks.
3115 nfs_advlock(struct vop_advlock_args *ap)
3117 struct vnode *vp = ap->a_vp;
3119 struct nfsnode *np = VTONFS(ap->a_vp);
3120 struct proc *p = (struct proc *)ap->a_id;
3121 struct thread *td = curthread; /* XXX */
3123 int ret, error = EOPNOTSUPP;
3126 ret = NFSVOPLOCK(vp, LK_SHARED);
3129 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3130 if (vp->v_type != VREG) {
3131 NFSVOPUNLOCK(vp, 0);
3134 if ((ap->a_flags & F_POSIX) != 0)
3137 cred = td->td_ucred;
3138 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3139 if (vp->v_iflag & VI_DOOMED) {
3140 NFSVOPUNLOCK(vp, 0);
3145 * If this is unlocking a write locked region, flush and
3146 * commit them before unlocking. This is required by
3147 * RFC3530 Sec. 9.3.2.
3149 if (ap->a_op == F_UNLCK &&
3150 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3152 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3155 * Loop around doing the lock op, while a blocking lock
3156 * must wait for the lock op to succeed.
3159 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3160 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3161 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3162 ap->a_op == F_SETLK) {
3163 NFSVOPUNLOCK(vp, 0);
3164 error = nfs_catnap(PZERO | PCATCH, ret,
3168 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3169 if (vp->v_iflag & VI_DOOMED) {
3170 NFSVOPUNLOCK(vp, 0);
3174 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3175 ap->a_op == F_SETLK);
3176 if (ret == NFSERR_DENIED) {
3177 NFSVOPUNLOCK(vp, 0);
3179 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3180 NFSVOPUNLOCK(vp, 0);
3182 } else if (ret != 0) {
3183 NFSVOPUNLOCK(vp, 0);
3188 * Now, if we just got a lock, invalidate data in the buffer
3189 * cache, as required, so that the coherency conforms with
3190 * RFC3530 Sec. 9.3.2.
3192 if (ap->a_op == F_SETLK) {
3193 if ((np->n_flag & NMODIFIED) == 0) {
3194 np->n_attrstamp = 0;
3195 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3196 ret = VOP_GETATTR(vp, &va, cred);
3198 if ((np->n_flag & NMODIFIED) || ret ||
3199 np->n_change != va.va_filerev) {
3200 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3201 np->n_attrstamp = 0;
3202 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3203 ret = VOP_GETATTR(vp, &va, cred);
3205 np->n_mtime = va.va_mtime;
3206 np->n_change = va.va_filerev;
3209 /* Mark that a file lock has been acquired. */
3211 np->n_flag |= NHASBEENLOCKED;
3214 NFSVOPUNLOCK(vp, 0);
3216 } else if (!NFS_ISV4(vp)) {
3217 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3218 size = VTONFS(vp)->n_size;
3219 NFSVOPUNLOCK(vp, 0);
3220 error = lf_advlock(ap, &(vp->v_lockf), size);
3222 if (nfs_advlock_p != NULL)
3223 error = nfs_advlock_p(ap);
3225 NFSVOPUNLOCK(vp, 0);
3229 if (error == 0 && ap->a_op == F_SETLK) {
3230 error = NFSVOPLOCK(vp, LK_SHARED);
3232 /* Mark that a file lock has been acquired. */
3234 np->n_flag |= NHASBEENLOCKED;
3236 NFSVOPUNLOCK(vp, 0);
3240 NFSVOPUNLOCK(vp, 0);
3245 * NFS advisory byte-level locks.
3248 nfs_advlockasync(struct vop_advlockasync_args *ap)
3250 struct vnode *vp = ap->a_vp;
3255 return (EOPNOTSUPP);
3256 error = NFSVOPLOCK(vp, LK_SHARED);
3259 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3260 size = VTONFS(vp)->n_size;
3261 NFSVOPUNLOCK(vp, 0);
3262 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3264 NFSVOPUNLOCK(vp, 0);
3271 * Print out the contents of an nfsnode.
3274 nfs_print(struct vop_print_args *ap)
3276 struct vnode *vp = ap->a_vp;
3277 struct nfsnode *np = VTONFS(vp);
3279 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3280 (uintmax_t)np->n_vattr.na_fsid);
3281 if (vp->v_type == VFIFO)
3288 * This is the "real" nfs::bwrite(struct buf*).
3289 * We set B_CACHE if this is a VMIO buffer.
3292 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3294 int oldflags, rtval;
3296 BUF_ASSERT_HELD(bp);
3298 if (bp->b_flags & B_INVAL) {
3303 oldflags = bp->b_flags;
3304 bp->b_flags |= B_CACHE;
3307 * Undirty the bp. We will redirty it later if the I/O fails.
3310 bp->b_flags &= ~B_DONE;
3311 bp->b_ioflags &= ~BIO_ERROR;
3312 bp->b_iocmd = BIO_WRITE;
3314 bufobj_wref(bp->b_bufobj);
3315 curthread->td_ru.ru_oublock++;
3318 * Note: to avoid loopback deadlocks, we do not
3319 * assign b_runningbufspace.
3321 vfs_busy_pages(bp, 1);
3324 bp->b_iooffset = dbtob(bp->b_blkno);
3327 if ((oldflags & B_ASYNC) != 0)
3330 rtval = bufwait(bp);
3331 if (oldflags & B_DELWRI)
3338 * nfs special file access vnode op.
3339 * Essentially just get vattr and then imitate iaccess() since the device is
3340 * local to the client.
3343 nfsspec_access(struct vop_access_args *ap)
3346 struct ucred *cred = ap->a_cred;
3347 struct vnode *vp = ap->a_vp;
3348 accmode_t accmode = ap->a_accmode;
3353 * Disallow write attempts on filesystems mounted read-only;
3354 * unless the file is a socket, fifo, or a block or character
3355 * device resident on the filesystem.
3357 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3358 switch (vp->v_type) {
3368 error = VOP_GETATTR(vp, vap, cred);
3371 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3372 accmode, cred, NULL);
3378 * Read wrapper for fifos.
3381 nfsfifo_read(struct vop_read_args *ap)
3383 struct nfsnode *np = VTONFS(ap->a_vp);
3391 vfs_timestamp(&np->n_atim);
3393 error = fifo_specops.vop_read(ap);
3398 * Write wrapper for fifos.
3401 nfsfifo_write(struct vop_write_args *ap)
3403 struct nfsnode *np = VTONFS(ap->a_vp);
3410 vfs_timestamp(&np->n_mtim);
3412 return(fifo_specops.vop_write(ap));
3416 * Close wrapper for fifos.
3418 * Update the times on the nfsnode then do fifo close.
3421 nfsfifo_close(struct vop_close_args *ap)
3423 struct vnode *vp = ap->a_vp;
3424 struct nfsnode *np = VTONFS(vp);
3429 if (np->n_flag & (NACC | NUPD)) {
3431 if (np->n_flag & NACC)
3433 if (np->n_flag & NUPD)
3436 if (vrefcnt(vp) == 1 &&
3437 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3439 if (np->n_flag & NACC)
3440 vattr.va_atime = np->n_atim;
3441 if (np->n_flag & NUPD)
3442 vattr.va_mtime = np->n_mtim;
3444 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3450 return (fifo_specops.vop_close(ap));
3454 * Just call ncl_writebp() with the force argument set to 1.
3456 * NOTE: B_DONE may or may not be set in a_bp on call.
3459 nfs_bwrite(struct buf *bp)
3462 return (ncl_writebp(bp, 1, curthread));
3465 struct buf_ops buf_ops_newnfs = {
3466 .bop_name = "buf_ops_nfs",
3467 .bop_write = nfs_bwrite,
3468 .bop_strategy = bufstrategy,
3469 .bop_sync = bufsync,
3470 .bop_bdflush = bufbdflush,
3474 nfs_getacl(struct vop_getacl_args *ap)
3478 if (ap->a_type != ACL_TYPE_NFS4)
3479 return (EOPNOTSUPP);
3480 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3482 if (error > NFSERR_STALE) {
3483 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3490 nfs_setacl(struct vop_setacl_args *ap)
3494 if (ap->a_type != ACL_TYPE_NFS4)
3495 return (EOPNOTSUPP);
3496 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3498 if (error > NFSERR_STALE) {
3499 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3506 * Return POSIX pathconf information applicable to nfs filesystems.
3509 nfs_pathconf(struct vop_pathconf_args *ap)
3511 struct nfsv3_pathconf pc;
3512 struct nfsvattr nfsva;
3513 struct vnode *vp = ap->a_vp;
3514 struct thread *td = curthread;
3515 int attrflag, error;
3517 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3518 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3519 ap->a_name == _PC_NO_TRUNC)) ||
3520 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3522 * Since only the above 4 a_names are returned by the NFSv3
3523 * Pathconf RPC, there is no point in doing it for others.
3524 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3525 * be used for _PC_NFS4_ACL as well.
3527 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3530 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3536 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3539 pc.pc_linkmax = NFS_LINK_MAX;
3540 pc.pc_namemax = NFS_MAXNAMLEN;
3542 pc.pc_chownrestricted = 1;
3543 pc.pc_caseinsensitive = 0;
3544 pc.pc_casepreserving = 1;
3547 switch (ap->a_name) {
3550 *ap->a_retval = pc.pc_linkmax;
3552 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
3556 *ap->a_retval = pc.pc_namemax;
3559 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
3560 *ap->a_retval = PIPE_BUF;
3564 case _PC_CHOWN_RESTRICTED:
3565 *ap->a_retval = pc.pc_chownrestricted;
3568 *ap->a_retval = pc.pc_notrunc;
3571 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3572 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3577 case _PC_ACL_PATH_MAX:
3579 *ap->a_retval = ACL_MAX_ENTRIES;
3589 case _PC_ALLOC_SIZE_MIN:
3590 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3592 case _PC_FILESIZEBITS:
3598 case _PC_REC_INCR_XFER_SIZE:
3599 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3601 case _PC_REC_MAX_XFER_SIZE:
3602 *ap->a_retval = -1; /* means ``unlimited'' */
3604 case _PC_REC_MIN_XFER_SIZE:
3605 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3607 case _PC_REC_XFER_ALIGN:
3608 *ap->a_retval = PAGE_SIZE;
3610 case _PC_SYMLINK_MAX:
3611 *ap->a_retval = NFS_MAXPATHLEN;
3615 error = vop_stdpathconf(ap);