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_set_text_t nfs_set_text;
148 * Global vfs data structures for nfs
151 static struct vop_vector newnfs_vnodeops_nosig = {
152 .vop_default = &default_vnodeops,
153 .vop_access = nfs_access,
154 .vop_advlock = nfs_advlock,
155 .vop_advlockasync = nfs_advlockasync,
156 .vop_close = nfs_close,
157 .vop_create = nfs_create,
158 .vop_fsync = nfs_fsync,
159 .vop_getattr = nfs_getattr,
160 .vop_getpages = ncl_getpages,
161 .vop_putpages = ncl_putpages,
162 .vop_inactive = ncl_inactive,
163 .vop_link = nfs_link,
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,
183 .vop_set_text = nfs_set_text,
187 nfs_vnodeops_bypass(struct vop_generic_args *a)
190 return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
193 struct vop_vector newnfs_vnodeops = {
194 .vop_default = &default_vnodeops,
195 .vop_bypass = nfs_vnodeops_bypass,
198 static struct vop_vector newnfs_fifoops_nosig = {
199 .vop_default = &fifo_specops,
200 .vop_access = nfsspec_access,
201 .vop_close = nfsfifo_close,
202 .vop_fsync = nfs_fsync,
203 .vop_getattr = nfs_getattr,
204 .vop_inactive = ncl_inactive,
205 .vop_pathconf = nfs_pathconf,
206 .vop_print = nfs_print,
207 .vop_read = nfsfifo_read,
208 .vop_reclaim = ncl_reclaim,
209 .vop_setattr = nfs_setattr,
210 .vop_write = nfsfifo_write,
214 nfs_fifoops_bypass(struct vop_generic_args *a)
217 return (vop_sigdefer(&newnfs_fifoops_nosig, a));
220 struct vop_vector newnfs_fifoops = {
221 .vop_default = &default_vnodeops,
222 .vop_bypass = nfs_fifoops_bypass,
225 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
226 struct componentname *cnp, struct vattr *vap);
227 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
228 int namelen, struct ucred *cred, struct thread *td);
229 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
230 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
231 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
232 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
233 struct componentname *scnp, struct sillyrename *sp);
238 SYSCTL_DECL(_vfs_nfs);
240 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
241 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
242 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
244 static int nfs_prime_access_cache = 0;
245 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
246 &nfs_prime_access_cache, 0,
247 "Prime NFS ACCESS cache when fetching attributes");
249 static int newnfs_commit_on_close = 0;
250 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
251 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
253 static int nfs_clean_pages_on_close = 1;
254 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
255 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
257 int newnfs_directio_enable = 0;
258 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
259 &newnfs_directio_enable, 0, "Enable NFS directio");
261 int nfs_keep_dirty_on_error;
262 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
263 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
266 * This sysctl allows other processes to mmap a file that has been opened
267 * O_DIRECT by a process. In general, having processes mmap the file while
268 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
269 * this by default to prevent DoS attacks - to prevent a malicious user from
270 * opening up files O_DIRECT preventing other users from mmap'ing these
271 * files. "Protected" environments where stricter consistency guarantees are
272 * required can disable this knob. The process that opened the file O_DIRECT
273 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
276 int newnfs_directio_allow_mmap = 1;
277 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
278 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
280 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
281 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
282 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
286 * The list of locks after the description of the lock is the ordering
287 * of other locks acquired with the lock held.
288 * np->n_mtx : Protects the fields in the nfsnode.
290 VI_MTX (acquired indirectly)
291 * nmp->nm_mtx : Protects the fields in the nfsmount.
293 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
294 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
297 * rep->r_mtx : Protects the fields in an nfsreq.
301 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
302 struct ucred *cred, u_int32_t *retmode)
304 int error = 0, attrflag, i, lrupos;
306 struct nfsnode *np = VTONFS(vp);
307 struct nfsvattr nfsva;
309 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
312 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
315 mtx_lock(&np->n_mtx);
316 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
317 if (np->n_accesscache[i].uid == cred->cr_uid) {
318 np->n_accesscache[i].mode = rmode;
319 np->n_accesscache[i].stamp = time_second;
322 if (i > 0 && np->n_accesscache[i].stamp <
323 np->n_accesscache[lrupos].stamp)
326 if (i == NFS_ACCESSCACHESIZE) {
327 np->n_accesscache[lrupos].uid = cred->cr_uid;
328 np->n_accesscache[lrupos].mode = rmode;
329 np->n_accesscache[lrupos].stamp = time_second;
331 mtx_unlock(&np->n_mtx);
334 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
335 } else if (NFS_ISV4(vp)) {
336 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
340 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
347 * nfs access vnode op.
348 * For nfs version 2, just return ok. File accesses may fail later.
349 * For nfs version 3, use the access rpc to check accessibility. If file modes
350 * are changed on the server, accesses might still fail later.
353 nfs_access(struct vop_access_args *ap)
355 struct vnode *vp = ap->a_vp;
356 int error = 0, i, gotahit;
357 u_int32_t mode, wmode, rmode;
358 int v34 = NFS_ISV34(vp);
359 struct nfsnode *np = VTONFS(vp);
362 * Disallow write attempts on filesystems mounted read-only;
363 * unless the file is a socket, fifo, or a block or character
364 * device resident on the filesystem.
366 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
367 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
368 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
369 switch (vp->v_type) {
379 * For nfs v3 or v4, check to see if we have done this recently, and if
380 * so return our cached result instead of making an ACCESS call.
381 * If not, do an access rpc, otherwise you are stuck emulating
382 * ufs_access() locally using the vattr. This may not be correct,
383 * since the server may apply other access criteria such as
384 * client uid-->server uid mapping that we do not know about.
387 if (ap->a_accmode & VREAD)
388 mode = NFSACCESS_READ;
391 if (vp->v_type != VDIR) {
392 if (ap->a_accmode & VWRITE)
393 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
394 if (ap->a_accmode & VAPPEND)
395 mode |= NFSACCESS_EXTEND;
396 if (ap->a_accmode & VEXEC)
397 mode |= NFSACCESS_EXECUTE;
398 if (ap->a_accmode & VDELETE)
399 mode |= NFSACCESS_DELETE;
401 if (ap->a_accmode & VWRITE)
402 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
403 if (ap->a_accmode & VAPPEND)
404 mode |= NFSACCESS_EXTEND;
405 if (ap->a_accmode & VEXEC)
406 mode |= NFSACCESS_LOOKUP;
407 if (ap->a_accmode & VDELETE)
408 mode |= NFSACCESS_DELETE;
409 if (ap->a_accmode & VDELETE_CHILD)
410 mode |= NFSACCESS_MODIFY;
412 /* XXX safety belt, only make blanket request if caching */
413 if (nfsaccess_cache_timeout > 0) {
414 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
415 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
416 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
422 * Does our cached result allow us to give a definite yes to
426 mtx_lock(&np->n_mtx);
427 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
428 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
429 if (time_second < (np->n_accesscache[i].stamp
430 + nfsaccess_cache_timeout) &&
431 (np->n_accesscache[i].mode & mode) == mode) {
432 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
438 mtx_unlock(&np->n_mtx);
441 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
442 ap->a_cred->cr_uid, mode);
444 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
445 ap->a_cred->cr_uid, mode);
449 * Either a no, or a don't know. Go to the wire.
451 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
452 error = nfs34_access_otw(vp, wmode, ap->a_td,
455 (rmode & mode) != mode)
460 if ((error = nfsspec_access(ap)) != 0) {
464 * Attempt to prevent a mapped root from accessing a file
465 * which it shouldn't. We try to read a byte from the file
466 * if the user is root and the file is not zero length.
467 * After calling nfsspec_access, we should have the correct
470 mtx_lock(&np->n_mtx);
471 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
472 && VTONFS(vp)->n_size > 0) {
477 mtx_unlock(&np->n_mtx);
480 auio.uio_iov = &aiov;
484 auio.uio_segflg = UIO_SYSSPACE;
485 auio.uio_rw = UIO_READ;
486 auio.uio_td = ap->a_td;
488 if (vp->v_type == VREG)
489 error = ncl_readrpc(vp, &auio, ap->a_cred);
490 else if (vp->v_type == VDIR) {
492 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
494 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
495 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
498 } else if (vp->v_type == VLNK)
499 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
503 mtx_unlock(&np->n_mtx);
511 * Check to see if the type is ok
512 * and that deletion is not in progress.
513 * For paged in text files, you will need to flush the page cache
514 * if consistency is lost.
518 nfs_open(struct vop_open_args *ap)
520 struct vnode *vp = ap->a_vp;
521 struct nfsnode *np = VTONFS(vp);
524 int fmode = ap->a_mode;
527 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
531 * For NFSv4, we need to do the Open Op before cache validation,
532 * so that we conform to RFC3530 Sec. 9.3.1.
535 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
537 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
544 * Now, if this Open will be doing reading, re-validate/flush the
545 * cache, so that Close/Open coherency is maintained.
547 mtx_lock(&np->n_mtx);
548 if (np->n_flag & NMODIFIED) {
549 mtx_unlock(&np->n_mtx);
550 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
551 if (error == EINTR || error == EIO) {
553 (void) nfsrpc_close(vp, 0, ap->a_td);
556 mtx_lock(&np->n_mtx);
558 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
559 if (vp->v_type == VDIR)
560 np->n_direofoffset = 0;
561 mtx_unlock(&np->n_mtx);
562 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
565 (void) nfsrpc_close(vp, 0, ap->a_td);
568 mtx_lock(&np->n_mtx);
569 np->n_mtime = vattr.va_mtime;
571 np->n_change = vattr.va_filerev;
573 mtx_unlock(&np->n_mtx);
574 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
577 (void) nfsrpc_close(vp, 0, ap->a_td);
580 mtx_lock(&np->n_mtx);
581 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
582 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
583 if (vp->v_type == VDIR)
584 np->n_direofoffset = 0;
585 mtx_unlock(&np->n_mtx);
586 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
587 if (error == EINTR || error == EIO) {
589 (void) nfsrpc_close(vp, 0, ap->a_td);
592 mtx_lock(&np->n_mtx);
593 np->n_mtime = vattr.va_mtime;
595 np->n_change = vattr.va_filerev;
600 * If the object has >= 1 O_DIRECT active opens, we disable caching.
602 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
603 (vp->v_type == VREG)) {
604 if (np->n_directio_opens == 0) {
605 mtx_unlock(&np->n_mtx);
606 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
609 (void) nfsrpc_close(vp, 0, ap->a_td);
612 mtx_lock(&np->n_mtx);
613 np->n_flag |= NNONCACHE;
615 np->n_directio_opens++;
618 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
619 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
620 np->n_flag |= NWRITEOPENED;
623 * If this is an open for writing, capture a reference to the
624 * credentials, so they can be used by ncl_putpages(). Using
625 * these write credentials is preferable to the credentials of
626 * whatever thread happens to be doing the VOP_PUTPAGES() since
627 * the write RPCs are less likely to fail with EACCES.
629 if ((fmode & FWRITE) != 0) {
630 cred = np->n_writecred;
631 np->n_writecred = crhold(ap->a_cred);
634 mtx_unlock(&np->n_mtx);
638 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
644 * What an NFS client should do upon close after writing is a debatable issue.
645 * Most NFS clients push delayed writes to the server upon close, basically for
647 * 1 - So that any write errors may be reported back to the client process
648 * doing the close system call. By far the two most likely errors are
649 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
650 * 2 - To put a worst case upper bound on cache inconsistency between
651 * multiple clients for the file.
652 * There is also a consistency problem for Version 2 of the protocol w.r.t.
653 * not being able to tell if other clients are writing a file concurrently,
654 * since there is no way of knowing if the changed modify time in the reply
655 * is only due to the write for this client.
656 * (NFS Version 3 provides weak cache consistency data in the reply that
657 * should be sufficient to detect and handle this case.)
659 * The current code does the following:
660 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
661 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
662 * or commit them (this satisfies 1 and 2 except for the
663 * case where the server crashes after this close but
664 * before the commit RPC, which is felt to be "good
665 * enough". Changing the last argument to ncl_flush() to
666 * a 1 would force a commit operation, if it is felt a
667 * commit is necessary now.
668 * for NFS Version 4 - flush the dirty buffers and commit them, if
669 * nfscl_mustflush() says this is necessary.
670 * It is necessary if there is no write delegation held,
671 * in order to satisfy open/close coherency.
672 * If the file isn't cached on local stable storage,
673 * it may be necessary in order to detect "out of space"
674 * errors from the server, if the write delegation
675 * issued by the server doesn't allow the file to grow.
679 nfs_close(struct vop_close_args *ap)
681 struct vnode *vp = ap->a_vp;
682 struct nfsnode *np = VTONFS(vp);
683 struct nfsvattr nfsva;
685 int error = 0, ret, localcred = 0;
686 int fmode = ap->a_fflag;
688 if (NFSCL_FORCEDISM(vp->v_mount))
691 * During shutdown, a_cred isn't valid, so just use root.
693 if (ap->a_cred == NOCRED) {
694 cred = newnfs_getcred();
699 if (vp->v_type == VREG) {
701 * Examine and clean dirty pages, regardless of NMODIFIED.
702 * This closes a major hole in close-to-open consistency.
703 * We want to push out all dirty pages (and buffers) on
704 * close, regardless of whether they were dirtied by
705 * mmap'ed writes or via write().
707 if (nfs_clean_pages_on_close && vp->v_object) {
708 VM_OBJECT_WLOCK(vp->v_object);
709 vm_object_page_clean(vp->v_object, 0, 0, 0);
710 VM_OBJECT_WUNLOCK(vp->v_object);
712 mtx_lock(&np->n_mtx);
713 if (np->n_flag & NMODIFIED) {
714 mtx_unlock(&np->n_mtx);
717 * Under NFSv3 we have dirty buffers to dispose of. We
718 * must flush them to the NFS server. We have the option
719 * of waiting all the way through the commit rpc or just
720 * waiting for the initial write. The default is to only
721 * wait through the initial write so the data is in the
722 * server's cache, which is roughly similar to the state
723 * a standard disk subsystem leaves the file in on close().
725 * We cannot clear the NMODIFIED bit in np->n_flag due to
726 * potential races with other processes, and certainly
727 * cannot clear it if we don't commit.
728 * These races occur when there is no longer the old
729 * traditional vnode locking implemented for Vnode Ops.
731 int cm = newnfs_commit_on_close ? 1 : 0;
732 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
733 /* np->n_flag &= ~NMODIFIED; */
734 } else if (NFS_ISV4(vp)) {
735 if (nfscl_mustflush(vp) != 0) {
736 int cm = newnfs_commit_on_close ? 1 : 0;
737 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
740 * as above w.r.t races when clearing
742 * np->n_flag &= ~NMODIFIED;
746 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
748 mtx_lock(&np->n_mtx);
751 * Invalidate the attribute cache in all cases.
752 * An open is going to fetch fresh attrs any way, other procs
753 * on this node that have file open will be forced to do an
754 * otw attr fetch, but this is safe.
755 * --> A user found that their RPC count dropped by 20% when
756 * this was commented out and I can't see any requirement
757 * for it, so I've disabled it when negative lookups are
758 * enabled. (What does this have to do with negative lookup
759 * caching? Well nothing, except it was reported by the
760 * same user that needed negative lookup caching and I wanted
761 * there to be a way to disable it to see if it
762 * is the cause of some caching/coherency issue that might
765 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
767 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
769 if (np->n_flag & NWRITEERR) {
770 np->n_flag &= ~NWRITEERR;
773 mtx_unlock(&np->n_mtx);
778 * Get attributes so "change" is up to date.
780 if (error == 0 && nfscl_mustflush(vp) != 0 &&
781 vp->v_type == VREG &&
782 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
783 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
786 np->n_change = nfsva.na_filerev;
787 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
795 ret = nfsrpc_close(vp, 0, ap->a_td);
799 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
802 if (newnfs_directio_enable)
803 KASSERT((np->n_directio_asyncwr == 0),
804 ("nfs_close: dirty unflushed (%d) directio buffers\n",
805 np->n_directio_asyncwr));
806 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
807 mtx_lock(&np->n_mtx);
808 KASSERT((np->n_directio_opens > 0),
809 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
810 np->n_directio_opens--;
811 if (np->n_directio_opens == 0)
812 np->n_flag &= ~NNONCACHE;
813 mtx_unlock(&np->n_mtx);
821 * nfs getattr call from vfs.
824 nfs_getattr(struct vop_getattr_args *ap)
826 struct vnode *vp = ap->a_vp;
827 struct thread *td = curthread; /* XXX */
828 struct nfsnode *np = VTONFS(vp);
830 struct nfsvattr nfsva;
831 struct vattr *vap = ap->a_vap;
835 * Update local times for special files.
837 mtx_lock(&np->n_mtx);
838 if (np->n_flag & (NACC | NUPD))
840 mtx_unlock(&np->n_mtx);
842 * First look in the cache.
844 if (ncl_getattrcache(vp, &vattr) == 0) {
845 vap->va_type = vattr.va_type;
846 vap->va_mode = vattr.va_mode;
847 vap->va_nlink = vattr.va_nlink;
848 vap->va_uid = vattr.va_uid;
849 vap->va_gid = vattr.va_gid;
850 vap->va_fsid = vattr.va_fsid;
851 vap->va_fileid = vattr.va_fileid;
852 vap->va_size = vattr.va_size;
853 vap->va_blocksize = vattr.va_blocksize;
854 vap->va_atime = vattr.va_atime;
855 vap->va_mtime = vattr.va_mtime;
856 vap->va_ctime = vattr.va_ctime;
857 vap->va_gen = vattr.va_gen;
858 vap->va_flags = vattr.va_flags;
859 vap->va_rdev = vattr.va_rdev;
860 vap->va_bytes = vattr.va_bytes;
861 vap->va_filerev = vattr.va_filerev;
863 * Get the local modify time for the case of a write
866 nfscl_deleggetmodtime(vp, &vap->va_mtime);
870 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
871 nfsaccess_cache_timeout > 0) {
872 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
873 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
874 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
875 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
879 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
881 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
884 * Get the local modify time for the case of a write
887 nfscl_deleggetmodtime(vp, &vap->va_mtime);
888 } else if (NFS_ISV4(vp)) {
889 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
898 nfs_setattr(struct vop_setattr_args *ap)
900 struct vnode *vp = ap->a_vp;
901 struct nfsnode *np = VTONFS(vp);
902 struct thread *td = curthread; /* XXX */
903 struct vattr *vap = ap->a_vap;
912 * Setting of flags and marking of atimes are not supported.
914 if (vap->va_flags != VNOVAL)
918 * Disallow write attempts if the filesystem is mounted read-only.
920 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
921 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
922 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
923 (vp->v_mount->mnt_flag & MNT_RDONLY))
925 if (vap->va_size != VNOVAL) {
926 switch (vp->v_type) {
933 if (vap->va_mtime.tv_sec == VNOVAL &&
934 vap->va_atime.tv_sec == VNOVAL &&
935 vap->va_mode == (mode_t)VNOVAL &&
936 vap->va_uid == (uid_t)VNOVAL &&
937 vap->va_gid == (gid_t)VNOVAL)
939 vap->va_size = VNOVAL;
943 * Disallow write attempts if the filesystem is
946 if (vp->v_mount->mnt_flag & MNT_RDONLY)
949 * We run vnode_pager_setsize() early (why?),
950 * we must set np->n_size now to avoid vinvalbuf
951 * V_SAVE races that might setsize a lower
954 mtx_lock(&np->n_mtx);
956 mtx_unlock(&np->n_mtx);
957 error = ncl_meta_setsize(vp, ap->a_cred, td,
959 mtx_lock(&np->n_mtx);
960 if (np->n_flag & NMODIFIED) {
962 mtx_unlock(&np->n_mtx);
963 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
966 vnode_pager_setsize(vp, tsize);
970 * Call nfscl_delegmodtime() to set the modify time
971 * locally, as required.
973 nfscl_delegmodtime(vp);
975 mtx_unlock(&np->n_mtx);
977 * np->n_size has already been set to vap->va_size
978 * in ncl_meta_setsize(). We must set it again since
979 * nfs_loadattrcache() could be called through
980 * ncl_meta_setsize() and could modify np->n_size.
982 mtx_lock(&np->n_mtx);
983 np->n_vattr.na_size = np->n_size = vap->va_size;
984 mtx_unlock(&np->n_mtx);
987 mtx_lock(&np->n_mtx);
988 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
989 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
990 mtx_unlock(&np->n_mtx);
991 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
992 if (error == EINTR || error == EIO)
995 mtx_unlock(&np->n_mtx);
997 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
998 if (error && vap->va_size != VNOVAL) {
999 mtx_lock(&np->n_mtx);
1000 np->n_size = np->n_vattr.na_size = tsize;
1001 vnode_pager_setsize(vp, tsize);
1002 mtx_unlock(&np->n_mtx);
1008 * Do an nfs setattr rpc.
1011 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1014 struct nfsnode *np = VTONFS(vp);
1015 int error, ret, attrflag, i;
1016 struct nfsvattr nfsva;
1018 if (NFS_ISV34(vp)) {
1019 mtx_lock(&np->n_mtx);
1020 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1021 np->n_accesscache[i].stamp = 0;
1022 np->n_flag |= NDELEGMOD;
1023 mtx_unlock(&np->n_mtx);
1024 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1026 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1029 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1033 if (error && NFS_ISV4(vp))
1034 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1039 * nfs lookup call, one step at a time...
1040 * First look in cache
1041 * If not found, unlock the directory nfsnode and do the rpc
1044 nfs_lookup(struct vop_lookup_args *ap)
1046 struct componentname *cnp = ap->a_cnp;
1047 struct vnode *dvp = ap->a_dvp;
1048 struct vnode **vpp = ap->a_vpp;
1049 struct mount *mp = dvp->v_mount;
1050 int flags = cnp->cn_flags;
1051 struct vnode *newvp;
1052 struct nfsmount *nmp;
1053 struct nfsnode *np, *newnp;
1054 int error = 0, attrflag, dattrflag, ltype, ncticks;
1055 struct thread *td = cnp->cn_thread;
1057 struct nfsvattr dnfsva, nfsva;
1059 struct timespec nctime;
1062 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1063 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1065 if (dvp->v_type != VDIR)
1070 /* For NFSv4, wait until any remove is done. */
1071 mtx_lock(&np->n_mtx);
1072 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1073 np->n_flag |= NREMOVEWANT;
1074 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1076 mtx_unlock(&np->n_mtx);
1078 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1080 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1081 if (error > 0 && error != ENOENT)
1085 * Lookups of "." are special and always return the
1086 * current directory. cache_lookup() already handles
1087 * associated locking bookkeeping, etc.
1089 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1090 /* XXX: Is this really correct? */
1091 if (cnp->cn_nameiop != LOOKUP &&
1093 cnp->cn_flags |= SAVENAME;
1098 * We only accept a positive hit in the cache if the
1099 * change time of the file matches our cached copy.
1100 * Otherwise, we discard the cache entry and fallback
1101 * to doing a lookup RPC. We also only trust cache
1102 * entries for less than nm_nametimeo seconds.
1104 * To better handle stale file handles and attributes,
1105 * clear the attribute cache of this node if it is a
1106 * leaf component, part of an open() call, and not
1107 * locally modified before fetching the attributes.
1108 * This should allow stale file handles to be detected
1109 * here where we can fall back to a LOOKUP RPC to
1110 * recover rather than having nfs_open() detect the
1111 * stale file handle and failing open(2) with ESTALE.
1114 newnp = VTONFS(newvp);
1115 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1116 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1117 !(newnp->n_flag & NMODIFIED)) {
1118 mtx_lock(&newnp->n_mtx);
1119 newnp->n_attrstamp = 0;
1120 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1121 mtx_unlock(&newnp->n_mtx);
1123 if (nfscl_nodeleg(newvp, 0) == 0 ||
1124 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1125 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1126 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1127 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1128 if (cnp->cn_nameiop != LOOKUP &&
1130 cnp->cn_flags |= SAVENAME;
1139 } else if (error == ENOENT) {
1140 if (dvp->v_iflag & VI_DOOMED)
1143 * We only accept a negative hit in the cache if the
1144 * modification time of the parent directory matches
1145 * the cached copy in the name cache entry.
1146 * Otherwise, we discard all of the negative cache
1147 * entries for this directory. We also only trust
1148 * negative cache entries for up to nm_negnametimeo
1151 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1152 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1153 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1154 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1157 cache_purge_negative(dvp);
1162 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1163 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1164 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1167 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1169 if (newvp != NULLVP) {
1174 if (error != ENOENT) {
1176 error = nfscl_maperr(td, error, (uid_t)0,
1181 /* The requested file was not found. */
1182 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1183 (flags & ISLASTCN)) {
1185 * XXX: UFS does a full VOP_ACCESS(dvp,
1186 * VWRITE) here instead of just checking
1189 if (mp->mnt_flag & MNT_RDONLY)
1191 cnp->cn_flags |= SAVENAME;
1192 return (EJUSTRETURN);
1195 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1197 * Cache the modification time of the parent
1198 * directory from the post-op attributes in
1199 * the name cache entry. The negative cache
1200 * entry will be ignored once the directory
1201 * has changed. Don't bother adding the entry
1202 * if the directory has already changed.
1204 mtx_lock(&np->n_mtx);
1205 if (timespeccmp(&np->n_vattr.na_mtime,
1206 &dnfsva.na_mtime, ==)) {
1207 mtx_unlock(&np->n_mtx);
1208 cache_enter_time(dvp, NULL, cnp,
1209 &dnfsva.na_mtime, NULL);
1211 mtx_unlock(&np->n_mtx);
1217 * Handle RENAME case...
1219 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1220 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1221 free(nfhp, M_NFSFH);
1224 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1230 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1233 cnp->cn_flags |= SAVENAME;
1237 if (flags & ISDOTDOT) {
1238 ltype = NFSVOPISLOCKED(dvp);
1239 error = vfs_busy(mp, MBF_NOWAIT);
1242 NFSVOPUNLOCK(dvp, 0);
1243 error = vfs_busy(mp, 0);
1244 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1246 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1253 NFSVOPUNLOCK(dvp, 0);
1254 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1260 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1261 if (dvp->v_iflag & VI_DOOMED) {
1273 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1275 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1276 free(nfhp, M_NFSFH);
1280 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1283 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1289 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1291 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1292 !(np->n_flag & NMODIFIED)) {
1294 * Flush the attribute cache when opening a
1295 * leaf node to ensure that fresh attributes
1296 * are fetched in nfs_open() since we did not
1297 * fetch attributes from the LOOKUP reply.
1299 mtx_lock(&np->n_mtx);
1300 np->n_attrstamp = 0;
1301 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1302 mtx_unlock(&np->n_mtx);
1305 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1306 cnp->cn_flags |= SAVENAME;
1307 if ((cnp->cn_flags & MAKEENTRY) &&
1308 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1309 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1310 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1311 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1318 * Just call ncl_bioread() to do the work.
1321 nfs_read(struct vop_read_args *ap)
1323 struct vnode *vp = ap->a_vp;
1325 switch (vp->v_type) {
1327 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1331 return (EOPNOTSUPP);
1339 nfs_readlink(struct vop_readlink_args *ap)
1341 struct vnode *vp = ap->a_vp;
1343 if (vp->v_type != VLNK)
1345 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1349 * Do a readlink rpc.
1350 * Called by ncl_doio() from below the buffer cache.
1353 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1355 int error, ret, attrflag;
1356 struct nfsvattr nfsva;
1358 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1361 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1365 if (error && NFS_ISV4(vp))
1366 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1375 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1377 int error, ret, attrflag;
1378 struct nfsvattr nfsva;
1379 struct nfsmount *nmp;
1381 nmp = VFSTONFS(vnode_mount(vp));
1384 if (NFSHASPNFS(nmp))
1385 error = nfscl_doiods(vp, uiop, NULL, NULL,
1386 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1387 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1389 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1392 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1396 if (error && NFS_ISV4(vp))
1397 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1405 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1406 int *iomode, int *must_commit, int called_from_strategy)
1408 struct nfsvattr nfsva;
1409 int error, attrflag, ret;
1410 struct nfsmount *nmp;
1412 nmp = VFSTONFS(vnode_mount(vp));
1415 if (NFSHASPNFS(nmp))
1416 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1417 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1418 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1420 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1421 uiop->uio_td, &nfsva, &attrflag, NULL,
1422 called_from_strategy);
1424 if (VTONFS(vp)->n_flag & ND_NFSV4)
1425 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1428 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1434 *iomode = NFSWRITE_FILESYNC;
1435 if (error && NFS_ISV4(vp))
1436 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1442 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1443 * mode set to specify the file type and the size field for rdev.
1446 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1449 struct nfsvattr nfsva, dnfsva;
1450 struct vnode *newvp = NULL;
1451 struct nfsnode *np = NULL, *dnp;
1454 int error = 0, attrflag, dattrflag;
1457 if (vap->va_type == VCHR || vap->va_type == VBLK)
1458 rdev = vap->va_rdev;
1459 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1462 return (EOPNOTSUPP);
1463 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1465 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1466 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1467 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1470 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1471 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1472 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1475 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1476 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1479 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1482 if (attrflag != 0) {
1483 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1491 } else if (NFS_ISV4(dvp)) {
1492 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1496 mtx_lock(&dnp->n_mtx);
1497 dnp->n_flag |= NMODIFIED;
1499 dnp->n_attrstamp = 0;
1500 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1502 mtx_unlock(&dnp->n_mtx);
1508 * just call nfs_mknodrpc() to do the work.
1512 nfs_mknod(struct vop_mknod_args *ap)
1514 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1517 static struct mtx nfs_cverf_mtx;
1518 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1524 static nfsquad_t cverf;
1526 static int cverf_initialized = 0;
1528 mtx_lock(&nfs_cverf_mtx);
1529 if (cverf_initialized == 0) {
1530 cverf.lval[0] = arc4random();
1531 cverf.lval[1] = arc4random();
1532 cverf_initialized = 1;
1536 mtx_unlock(&nfs_cverf_mtx);
1542 * nfs file create call
1545 nfs_create(struct vop_create_args *ap)
1547 struct vnode *dvp = ap->a_dvp;
1548 struct vattr *vap = ap->a_vap;
1549 struct componentname *cnp = ap->a_cnp;
1550 struct nfsnode *np = NULL, *dnp;
1551 struct vnode *newvp = NULL;
1552 struct nfsmount *nmp;
1553 struct nfsvattr dnfsva, nfsva;
1556 int error = 0, attrflag, dattrflag, fmode = 0;
1560 * Oops, not for me..
1562 if (vap->va_type == VSOCK)
1563 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1565 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1567 if (vap->va_vaflags & VA_EXCLUSIVE)
1570 nmp = VFSTONFS(vnode_mount(dvp));
1572 /* For NFSv4, wait until any remove is done. */
1573 mtx_lock(&dnp->n_mtx);
1574 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1575 dnp->n_flag |= NREMOVEWANT;
1576 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1578 mtx_unlock(&dnp->n_mtx);
1580 cverf = nfs_get_cverf();
1581 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1582 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1583 &nfhp, &attrflag, &dattrflag, NULL);
1586 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1587 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1588 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1591 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1592 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1595 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1599 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1600 cnp->cn_thread, &nfsva, NULL);
1602 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1606 if (newvp != NULL) {
1610 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1611 error == NFSERR_NOTSUPP) {
1615 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1616 if (nfscl_checksattr(vap, &nfsva)) {
1617 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1618 cnp->cn_thread, &nfsva, &attrflag, NULL);
1619 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1620 vap->va_gid != (gid_t)VNOVAL)) {
1621 /* try again without setting uid/gid */
1622 vap->va_uid = (uid_t)VNOVAL;
1623 vap->va_gid = (uid_t)VNOVAL;
1624 error = nfsrpc_setattr(newvp, vap, NULL,
1625 cnp->cn_cred, cnp->cn_thread, &nfsva,
1629 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1636 if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1637 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1640 } else if (NFS_ISV4(dvp)) {
1641 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1644 mtx_lock(&dnp->n_mtx);
1645 dnp->n_flag |= NMODIFIED;
1647 dnp->n_attrstamp = 0;
1648 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1650 mtx_unlock(&dnp->n_mtx);
1655 * nfs file remove call
1656 * To try and make nfs semantics closer to ufs semantics, a file that has
1657 * other processes using the vnode is renamed instead of removed and then
1658 * removed later on the last close.
1659 * - If v_usecount > 1
1660 * If a rename is not already in the works
1661 * call nfs_sillyrename() to set it up
1666 nfs_remove(struct vop_remove_args *ap)
1668 struct vnode *vp = ap->a_vp;
1669 struct vnode *dvp = ap->a_dvp;
1670 struct componentname *cnp = ap->a_cnp;
1671 struct nfsnode *np = VTONFS(vp);
1675 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1676 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1677 if (vp->v_type == VDIR)
1679 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1680 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1681 vattr.va_nlink > 1)) {
1683 * Purge the name cache so that the chance of a lookup for
1684 * the name succeeding while the remove is in progress is
1685 * minimized. Without node locking it can still happen, such
1686 * that an I/O op returns ESTALE, but since you get this if
1687 * another host removes the file..
1691 * throw away biocache buffers, mainly to avoid
1692 * unnecessary delayed writes later.
1694 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1695 if (error != EINTR && error != EIO)
1697 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1698 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1700 * Kludge City: If the first reply to the remove rpc is lost..
1701 * the reply to the retransmitted request will be ENOENT
1702 * since the file was in fact removed
1703 * Therefore, we cheat and return success.
1705 if (error == ENOENT)
1707 } else if (!np->n_sillyrename)
1708 error = nfs_sillyrename(dvp, vp, cnp);
1709 mtx_lock(&np->n_mtx);
1710 np->n_attrstamp = 0;
1711 mtx_unlock(&np->n_mtx);
1712 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1717 * nfs file remove rpc called from nfs_inactive
1720 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1723 * Make sure that the directory vnode is still valid.
1724 * XXX we should lock sp->s_dvp here.
1726 if (sp->s_dvp->v_type == VBAD)
1728 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1733 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1736 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1737 int namelen, struct ucred *cred, struct thread *td)
1739 struct nfsvattr dnfsva;
1740 struct nfsnode *dnp = VTONFS(dvp);
1741 int error = 0, dattrflag;
1743 mtx_lock(&dnp->n_mtx);
1744 dnp->n_flag |= NREMOVEINPROG;
1745 mtx_unlock(&dnp->n_mtx);
1746 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1748 mtx_lock(&dnp->n_mtx);
1749 if ((dnp->n_flag & NREMOVEWANT)) {
1750 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1751 mtx_unlock(&dnp->n_mtx);
1752 wakeup((caddr_t)dnp);
1754 dnp->n_flag &= ~NREMOVEINPROG;
1755 mtx_unlock(&dnp->n_mtx);
1758 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1759 mtx_lock(&dnp->n_mtx);
1760 dnp->n_flag |= NMODIFIED;
1762 dnp->n_attrstamp = 0;
1763 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1765 mtx_unlock(&dnp->n_mtx);
1766 if (error && NFS_ISV4(dvp))
1767 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1772 * nfs file rename call
1775 nfs_rename(struct vop_rename_args *ap)
1777 struct vnode *fvp = ap->a_fvp;
1778 struct vnode *tvp = ap->a_tvp;
1779 struct vnode *fdvp = ap->a_fdvp;
1780 struct vnode *tdvp = ap->a_tdvp;
1781 struct componentname *tcnp = ap->a_tcnp;
1782 struct componentname *fcnp = ap->a_fcnp;
1783 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1784 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1785 struct nfsv4node *newv4 = NULL;
1788 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1789 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1790 /* Check for cross-device rename */
1791 if ((fvp->v_mount != tdvp->v_mount) ||
1792 (tvp && (fvp->v_mount != tvp->v_mount))) {
1798 printf("nfs_rename: fvp == tvp (can't happen)\n");
1802 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1806 * We have to flush B_DELWRI data prior to renaming
1807 * the file. If we don't, the delayed-write buffers
1808 * can be flushed out later after the file has gone stale
1809 * under NFSV3. NFSV2 does not have this problem because
1810 * ( as far as I can tell ) it flushes dirty buffers more
1813 * Skip the rename operation if the fsync fails, this can happen
1814 * due to the server's volume being full, when we pushed out data
1815 * that was written back to our cache earlier. Not checking for
1816 * this condition can result in potential (silent) data loss.
1818 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1819 NFSVOPUNLOCK(fvp, 0);
1821 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1826 * If the tvp exists and is in use, sillyrename it before doing the
1827 * rename of the new file over it.
1828 * XXX Can't sillyrename a directory.
1830 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1831 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1836 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1837 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1840 if (error == 0 && NFS_ISV4(tdvp)) {
1842 * For NFSv4, check to see if it is the same name and
1843 * replace the name, if it is different.
1846 sizeof (struct nfsv4node) +
1847 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1848 M_NFSV4NODE, M_WAITOK);
1849 mtx_lock(&tdnp->n_mtx);
1850 mtx_lock(&fnp->n_mtx);
1851 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1852 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1853 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1854 tcnp->cn_namelen) ||
1855 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1856 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1857 tdnp->n_fhp->nfh_len))) {
1859 { char nnn[100]; int nnnl;
1860 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1861 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1863 printf("ren replace=%s\n",nnn);
1866 free(fnp->n_v4, M_NFSV4NODE);
1869 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1870 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1871 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1872 tdnp->n_fhp->nfh_len);
1873 NFSBCOPY(tcnp->cn_nameptr,
1874 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1876 mtx_unlock(&tdnp->n_mtx);
1877 mtx_unlock(&fnp->n_mtx);
1879 free(newv4, M_NFSV4NODE);
1882 if (fvp->v_type == VDIR) {
1883 if (tvp != NULL && tvp->v_type == VDIR)
1898 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1900 if (error == ENOENT)
1906 * nfs file rename rpc called from nfs_remove() above
1909 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1910 struct sillyrename *sp)
1913 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1914 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1919 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1922 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1923 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1924 int tnamelen, struct ucred *cred, struct thread *td)
1926 struct nfsvattr fnfsva, tnfsva;
1927 struct nfsnode *fdnp = VTONFS(fdvp);
1928 struct nfsnode *tdnp = VTONFS(tdvp);
1929 int error = 0, fattrflag, tattrflag;
1931 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1932 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1933 &tattrflag, NULL, NULL);
1934 mtx_lock(&fdnp->n_mtx);
1935 fdnp->n_flag |= NMODIFIED;
1936 if (fattrflag != 0) {
1937 mtx_unlock(&fdnp->n_mtx);
1938 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1940 fdnp->n_attrstamp = 0;
1941 mtx_unlock(&fdnp->n_mtx);
1942 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1944 mtx_lock(&tdnp->n_mtx);
1945 tdnp->n_flag |= NMODIFIED;
1946 if (tattrflag != 0) {
1947 mtx_unlock(&tdnp->n_mtx);
1948 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1950 tdnp->n_attrstamp = 0;
1951 mtx_unlock(&tdnp->n_mtx);
1952 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1954 if (error && NFS_ISV4(fdvp))
1955 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1960 * nfs hard link create call
1963 nfs_link(struct vop_link_args *ap)
1965 struct vnode *vp = ap->a_vp;
1966 struct vnode *tdvp = ap->a_tdvp;
1967 struct componentname *cnp = ap->a_cnp;
1968 struct nfsnode *np, *tdnp;
1969 struct nfsvattr nfsva, dnfsva;
1970 int error = 0, attrflag, dattrflag;
1973 * Push all writes to the server, so that the attribute cache
1974 * doesn't get "out of sync" with the server.
1975 * XXX There should be a better way!
1977 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1979 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1980 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1982 tdnp = VTONFS(tdvp);
1983 mtx_lock(&tdnp->n_mtx);
1984 tdnp->n_flag |= NMODIFIED;
1985 if (dattrflag != 0) {
1986 mtx_unlock(&tdnp->n_mtx);
1987 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1989 tdnp->n_attrstamp = 0;
1990 mtx_unlock(&tdnp->n_mtx);
1991 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1994 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1997 mtx_lock(&np->n_mtx);
1998 np->n_attrstamp = 0;
1999 mtx_unlock(&np->n_mtx);
2000 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2003 * If negative lookup caching is enabled, I might as well
2004 * add an entry for this node. Not necessary for correctness,
2005 * but if negative caching is enabled, then the system
2006 * must care about lookup caching hit rate, so...
2008 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2009 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2010 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2012 if (error && NFS_ISV4(vp))
2013 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2019 * nfs symbolic link create call
2022 nfs_symlink(struct vop_symlink_args *ap)
2024 struct vnode *dvp = ap->a_dvp;
2025 struct vattr *vap = ap->a_vap;
2026 struct componentname *cnp = ap->a_cnp;
2027 struct nfsvattr nfsva, dnfsva;
2029 struct nfsnode *np = NULL, *dnp;
2030 struct vnode *newvp = NULL;
2031 int error = 0, attrflag, dattrflag, ret;
2033 vap->va_type = VLNK;
2034 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2035 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2036 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2038 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2039 &np, NULL, LK_EXCLUSIVE);
2045 if (newvp != NULL) {
2047 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2049 } else if (!error) {
2051 * If we do not have an error and we could not extract the
2052 * newvp from the response due to the request being NFSv2, we
2053 * have to do a lookup in order to obtain a newvp to return.
2055 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2056 cnp->cn_cred, cnp->cn_thread, &np);
2064 error = nfscl_maperr(cnp->cn_thread, error,
2065 vap->va_uid, vap->va_gid);
2071 mtx_lock(&dnp->n_mtx);
2072 dnp->n_flag |= NMODIFIED;
2073 if (dattrflag != 0) {
2074 mtx_unlock(&dnp->n_mtx);
2075 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2077 dnp->n_attrstamp = 0;
2078 mtx_unlock(&dnp->n_mtx);
2079 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2082 * If negative lookup caching is enabled, I might as well
2083 * add an entry for this node. Not necessary for correctness,
2084 * but if negative caching is enabled, then the system
2085 * must care about lookup caching hit rate, so...
2087 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2088 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2089 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2098 nfs_mkdir(struct vop_mkdir_args *ap)
2100 struct vnode *dvp = ap->a_dvp;
2101 struct vattr *vap = ap->a_vap;
2102 struct componentname *cnp = ap->a_cnp;
2103 struct nfsnode *np = NULL, *dnp;
2104 struct vnode *newvp = NULL;
2107 struct nfsvattr nfsva, dnfsva;
2108 int error = 0, attrflag, dattrflag, ret;
2110 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2112 vap->va_type = VDIR;
2113 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2114 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2115 &attrflag, &dattrflag, NULL);
2117 mtx_lock(&dnp->n_mtx);
2118 dnp->n_flag |= NMODIFIED;
2119 if (dattrflag != 0) {
2120 mtx_unlock(&dnp->n_mtx);
2121 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2123 dnp->n_attrstamp = 0;
2124 mtx_unlock(&dnp->n_mtx);
2125 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2128 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2129 &np, NULL, LK_EXCLUSIVE);
2133 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2138 if (!error && newvp == NULL) {
2139 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2140 cnp->cn_cred, cnp->cn_thread, &np);
2143 if (newvp->v_type != VDIR)
2151 error = nfscl_maperr(cnp->cn_thread, error,
2152 vap->va_uid, vap->va_gid);
2155 * If negative lookup caching is enabled, I might as well
2156 * add an entry for this node. Not necessary for correctness,
2157 * but if negative caching is enabled, then the system
2158 * must care about lookup caching hit rate, so...
2160 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2161 (cnp->cn_flags & MAKEENTRY) &&
2162 attrflag != 0 && dattrflag != 0)
2163 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2171 * nfs remove directory call
2174 nfs_rmdir(struct vop_rmdir_args *ap)
2176 struct vnode *vp = ap->a_vp;
2177 struct vnode *dvp = ap->a_dvp;
2178 struct componentname *cnp = ap->a_cnp;
2179 struct nfsnode *dnp;
2180 struct nfsvattr dnfsva;
2181 int error, dattrflag;
2185 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2186 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2188 mtx_lock(&dnp->n_mtx);
2189 dnp->n_flag |= NMODIFIED;
2190 if (dattrflag != 0) {
2191 mtx_unlock(&dnp->n_mtx);
2192 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2194 dnp->n_attrstamp = 0;
2195 mtx_unlock(&dnp->n_mtx);
2196 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2201 if (error && NFS_ISV4(dvp))
2202 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2205 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2207 if (error == ENOENT)
2216 nfs_readdir(struct vop_readdir_args *ap)
2218 struct vnode *vp = ap->a_vp;
2219 struct nfsnode *np = VTONFS(vp);
2220 struct uio *uio = ap->a_uio;
2221 ssize_t tresid, left;
2225 if (ap->a_eofflag != NULL)
2227 if (vp->v_type != VDIR)
2231 * First, check for hit on the EOF offset cache
2233 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2234 (np->n_flag & NMODIFIED) == 0) {
2235 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2236 mtx_lock(&np->n_mtx);
2237 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2238 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2239 mtx_unlock(&np->n_mtx);
2240 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2241 if (ap->a_eofflag != NULL)
2245 mtx_unlock(&np->n_mtx);
2250 * NFS always guarantees that directory entries don't straddle
2251 * DIRBLKSIZ boundaries. As such, we need to limit the size
2252 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2255 left = uio->uio_resid % DIRBLKSIZ;
2256 if (left == uio->uio_resid)
2258 uio->uio_resid -= left;
2261 * Call ncl_bioread() to do the real work.
2263 tresid = uio->uio_resid;
2264 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2266 if (!error && uio->uio_resid == tresid) {
2267 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2268 if (ap->a_eofflag != NULL)
2272 /* Add the partial DIRBLKSIZ (left) back in. */
2273 uio->uio_resid += left;
2279 * Called from below the buffer cache by ncl_doio().
2282 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2285 struct nfsvattr nfsva;
2286 nfsuint64 *cookiep, cookie;
2287 struct nfsnode *dnp = VTONFS(vp);
2288 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2289 int error = 0, eof, attrflag;
2291 KASSERT(uiop->uio_iovcnt == 1 &&
2292 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2293 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2294 ("nfs readdirrpc bad uio"));
2297 * If there is no cookie, assume directory was stale.
2299 ncl_dircookie_lock(dnp);
2300 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2303 ncl_dircookie_unlock(dnp);
2305 ncl_dircookie_unlock(dnp);
2306 return (NFSERR_BAD_COOKIE);
2309 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2310 (void)ncl_fsinfo(nmp, vp, cred, td);
2312 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2313 &attrflag, &eof, NULL);
2315 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2319 * We are now either at the end of the directory or have filled
2323 dnp->n_direofoffset = uiop->uio_offset;
2325 if (uiop->uio_resid > 0)
2326 printf("EEK! readdirrpc resid > 0\n");
2327 ncl_dircookie_lock(dnp);
2328 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2330 ncl_dircookie_unlock(dnp);
2332 } else if (NFS_ISV4(vp)) {
2333 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2339 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2342 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2345 struct nfsvattr nfsva;
2346 nfsuint64 *cookiep, cookie;
2347 struct nfsnode *dnp = VTONFS(vp);
2348 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2349 int error = 0, attrflag, eof;
2351 KASSERT(uiop->uio_iovcnt == 1 &&
2352 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2353 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2354 ("nfs readdirplusrpc bad uio"));
2357 * If there is no cookie, assume directory was stale.
2359 ncl_dircookie_lock(dnp);
2360 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2363 ncl_dircookie_unlock(dnp);
2365 ncl_dircookie_unlock(dnp);
2366 return (NFSERR_BAD_COOKIE);
2369 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2370 (void)ncl_fsinfo(nmp, vp, cred, td);
2371 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2372 &attrflag, &eof, NULL);
2374 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2378 * We are now either at end of the directory or have filled the
2382 dnp->n_direofoffset = uiop->uio_offset;
2384 if (uiop->uio_resid > 0)
2385 printf("EEK! readdirplusrpc resid > 0\n");
2386 ncl_dircookie_lock(dnp);
2387 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2389 ncl_dircookie_unlock(dnp);
2391 } else if (NFS_ISV4(vp)) {
2392 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2398 * Silly rename. To make the NFS filesystem that is stateless look a little
2399 * more like the "ufs" a remove of an active vnode is translated to a rename
2400 * to a funny looking filename that is removed by nfs_inactive on the
2401 * nfsnode. There is the potential for another process on a different client
2402 * to create the same funny name between the nfs_lookitup() fails and the
2403 * nfs_rename() completes, but...
2406 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2408 struct sillyrename *sp;
2412 unsigned int lticks;
2416 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2417 sp = malloc(sizeof (struct sillyrename),
2418 M_NEWNFSREQ, M_WAITOK);
2419 sp->s_cred = crhold(cnp->cn_cred);
2424 * Fudge together a funny name.
2425 * Changing the format of the funny name to accommodate more
2426 * sillynames per directory.
2427 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2428 * CPU ticks since boot.
2430 pid = cnp->cn_thread->td_proc->p_pid;
2431 lticks = (unsigned int)ticks;
2433 sp->s_namlen = sprintf(sp->s_name,
2434 ".nfs.%08x.%04x4.4", lticks,
2436 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2437 cnp->cn_thread, NULL))
2441 error = nfs_renameit(dvp, vp, cnp, sp);
2444 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2445 cnp->cn_thread, &np);
2446 np->n_sillyrename = sp;
2451 free(sp, M_NEWNFSREQ);
2456 * Look up a file name and optionally either update the file handle or
2457 * allocate an nfsnode, depending on the value of npp.
2458 * npp == NULL --> just do the lookup
2459 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2461 * *npp != NULL --> update the file handle in the vnode
2464 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2465 struct thread *td, struct nfsnode **npp)
2467 struct vnode *newvp = NULL, *vp;
2468 struct nfsnode *np, *dnp = VTONFS(dvp);
2469 struct nfsfh *nfhp, *onfhp;
2470 struct nfsvattr nfsva, dnfsva;
2471 struct componentname cn;
2472 int error = 0, attrflag, dattrflag;
2475 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2476 &nfhp, &attrflag, &dattrflag, NULL);
2478 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2479 if (npp && !error) {
2484 * For NFSv4, check to see if it is the same name and
2485 * replace the name, if it is different.
2487 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2488 (np->n_v4->n4_namelen != len ||
2489 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2490 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2491 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2492 dnp->n_fhp->nfh_len))) {
2494 { char nnn[100]; int nnnl;
2495 nnnl = (len < 100) ? len : 99;
2496 bcopy(name, nnn, nnnl);
2498 printf("replace=%s\n",nnn);
2501 free(np->n_v4, M_NFSV4NODE);
2503 sizeof (struct nfsv4node) +
2504 dnp->n_fhp->nfh_len + len - 1,
2505 M_NFSV4NODE, M_WAITOK);
2506 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2507 np->n_v4->n4_namelen = len;
2508 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2509 dnp->n_fhp->nfh_len);
2510 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2512 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2516 * Rehash node for new file handle.
2518 vfs_hash_rehash(vp, hash);
2521 free(onfhp, M_NFSFH);
2523 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2524 free(nfhp, M_NFSFH);
2528 cn.cn_nameptr = name;
2529 cn.cn_namelen = len;
2530 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2531 &np, NULL, LK_EXCLUSIVE);
2536 if (!attrflag && *npp == NULL) {
2544 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2547 if (npp && *npp == NULL) {
2558 if (error && NFS_ISV4(dvp))
2559 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2564 * Nfs Version 3 and 4 commit rpc
2567 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2570 struct nfsvattr nfsva;
2571 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2574 int error, attrflag;
2579 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2580 uio.uio_offset = offset;
2581 uio.uio_resid = cnt;
2582 error = nfscl_doiods(vp, &uio, NULL, NULL,
2583 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2585 mtx_lock(&np->n_mtx);
2586 np->n_flag &= ~NDSCOMMIT;
2587 mtx_unlock(&np->n_mtx);
2591 mtx_lock(&nmp->nm_mtx);
2592 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2593 mtx_unlock(&nmp->nm_mtx);
2596 mtx_unlock(&nmp->nm_mtx);
2597 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2601 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2603 if (error != 0 && NFS_ISV4(vp))
2604 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2610 * For async requests when nfsiod(s) are running, queue the request by
2611 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2615 nfs_strategy(struct vop_strategy_args *ap)
2623 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2624 KASSERT(!(bp->b_flags & B_DONE),
2625 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2626 BUF_ASSERT_HELD(bp);
2628 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2629 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2631 if (bp->b_iocmd == BIO_READ)
2637 * If the op is asynchronous and an i/o daemon is waiting
2638 * queue the request, wake it up and wait for completion
2639 * otherwise just do it ourselves.
2641 if ((bp->b_flags & B_ASYNC) == 0 ||
2642 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2643 (void) ncl_doio(vp, bp, cr, curthread, 1);
2648 * fsync vnode op. Just call ncl_flush() with commit == 1.
2652 nfs_fsync(struct vop_fsync_args *ap)
2655 if (ap->a_vp->v_type != VREG) {
2657 * For NFS, metadata is changed synchronously on the server,
2658 * so there is nothing to flush. Also, ncl_flush() clears
2659 * the NMODIFIED flag and that shouldn't be done here for
2664 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2668 * Flush all the blocks associated with a vnode.
2669 * Walk through the buffer pool and push any dirty pages
2670 * associated with the vnode.
2671 * If the called_from_renewthread argument is TRUE, it has been called
2672 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2673 * waiting for a buffer write to complete.
2676 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2677 int commit, int called_from_renewthread)
2679 struct nfsnode *np = VTONFS(vp);
2683 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2684 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2685 int passone = 1, trycnt = 0;
2686 u_quad_t off, endoff, toff;
2687 struct ucred* wcred = NULL;
2688 struct buf **bvec = NULL;
2690 #ifndef NFS_COMMITBVECSIZ
2691 #define NFS_COMMITBVECSIZ 20
2693 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2694 u_int bvecsize = 0, bveccount;
2696 if (called_from_renewthread != 0)
2698 if (nmp->nm_flag & NFSMNT_INT)
2704 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2705 * server, but has not been committed to stable storage on the server
2706 * yet. On the first pass, the byte range is worked out and the commit
2707 * rpc is done. On the second pass, ncl_writebp() is called to do the
2714 if (NFS_ISV34(vp) && commit) {
2715 if (bvec != NULL && bvec != bvec_on_stack)
2718 * Count up how many buffers waiting for a commit.
2722 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2723 if (!BUF_ISLOCKED(bp) &&
2724 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2725 == (B_DELWRI | B_NEEDCOMMIT))
2729 * Allocate space to remember the list of bufs to commit. It is
2730 * important to use M_NOWAIT here to avoid a race with nfs_write.
2731 * If we can't get memory (for whatever reason), we will end up
2732 * committing the buffers one-by-one in the loop below.
2734 if (bveccount > NFS_COMMITBVECSIZ) {
2736 * Release the vnode interlock to avoid a lock
2740 bvec = (struct buf **)
2741 malloc(bveccount * sizeof(struct buf *),
2745 bvec = bvec_on_stack;
2746 bvecsize = NFS_COMMITBVECSIZ;
2748 bvecsize = bveccount;
2750 bvec = bvec_on_stack;
2751 bvecsize = NFS_COMMITBVECSIZ;
2753 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2754 if (bvecpos >= bvecsize)
2756 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2757 nbp = TAILQ_NEXT(bp, b_bobufs);
2760 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2761 (B_DELWRI | B_NEEDCOMMIT)) {
2763 nbp = TAILQ_NEXT(bp, b_bobufs);
2769 * Work out if all buffers are using the same cred
2770 * so we can deal with them all with one commit.
2772 * NOTE: we are not clearing B_DONE here, so we have
2773 * to do it later on in this routine if we intend to
2774 * initiate I/O on the bp.
2776 * Note: to avoid loopback deadlocks, we do not
2777 * assign b_runningbufspace.
2780 wcred = bp->b_wcred;
2781 else if (wcred != bp->b_wcred)
2783 vfs_busy_pages(bp, 1);
2787 * bp is protected by being locked, but nbp is not
2788 * and vfs_busy_pages() may sleep. We have to
2791 nbp = TAILQ_NEXT(bp, b_bobufs);
2794 * A list of these buffers is kept so that the
2795 * second loop knows which buffers have actually
2796 * been committed. This is necessary, since there
2797 * may be a race between the commit rpc and new
2798 * uncommitted writes on the file.
2800 bvec[bvecpos++] = bp;
2801 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2805 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2813 * Commit data on the server, as required.
2814 * If all bufs are using the same wcred, then use that with
2815 * one call for all of them, otherwise commit each one
2818 if (wcred != NOCRED)
2819 retv = ncl_commit(vp, off, (int)(endoff - off),
2823 for (i = 0; i < bvecpos; i++) {
2826 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2828 size = (u_quad_t)(bp->b_dirtyend
2830 retv = ncl_commit(vp, off, (int)size,
2836 if (retv == NFSERR_STALEWRITEVERF)
2837 ncl_clearcommit(vp->v_mount);
2840 * Now, either mark the blocks I/O done or mark the
2841 * blocks dirty, depending on whether the commit
2844 for (i = 0; i < bvecpos; i++) {
2846 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2849 * Error, leave B_DELWRI intact
2851 vfs_unbusy_pages(bp);
2855 * Success, remove B_DELWRI ( bundirty() ).
2857 * b_dirtyoff/b_dirtyend seem to be NFS
2858 * specific. We should probably move that
2859 * into bundirty(). XXX
2862 bp->b_flags |= B_ASYNC;
2864 bp->b_flags &= ~B_DONE;
2865 bp->b_ioflags &= ~BIO_ERROR;
2866 bp->b_dirtyoff = bp->b_dirtyend = 0;
2873 * Start/do any write(s) that are required.
2877 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2878 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2879 if (waitfor != MNT_WAIT || passone)
2882 error = BUF_TIMELOCK(bp,
2883 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2884 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2889 if (error == ENOLCK) {
2893 if (called_from_renewthread != 0) {
2895 * Return EIO so the flush will be retried
2901 if (newnfs_sigintr(nmp, td)) {
2905 if (slpflag == PCATCH) {
2911 if ((bp->b_flags & B_DELWRI) == 0)
2912 panic("nfs_fsync: not dirty");
2913 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2919 if (passone || !commit)
2920 bp->b_flags |= B_ASYNC;
2922 bp->b_flags |= B_ASYNC;
2924 if (newnfs_sigintr(nmp, td)) {
2935 if (waitfor == MNT_WAIT) {
2936 while (bo->bo_numoutput) {
2937 error = bufobj_wwait(bo, slpflag, slptimeo);
2940 if (called_from_renewthread != 0) {
2942 * Return EIO so that the flush will be
2948 error = newnfs_sigintr(nmp, td);
2951 if (slpflag == PCATCH) {
2958 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2963 * Wait for all the async IO requests to drain
2966 mtx_lock(&np->n_mtx);
2967 while (np->n_directio_asyncwr > 0) {
2968 np->n_flag |= NFSYNCWAIT;
2969 error = newnfs_msleep(td, &np->n_directio_asyncwr,
2970 &np->n_mtx, slpflag | (PRIBIO + 1),
2973 if (newnfs_sigintr(nmp, td)) {
2974 mtx_unlock(&np->n_mtx);
2980 mtx_unlock(&np->n_mtx);
2983 if (NFSHASPNFS(nmp)) {
2984 nfscl_layoutcommit(vp, td);
2986 * Invalidate the attribute cache, since writes to a DS
2987 * won't update the size attribute.
2989 mtx_lock(&np->n_mtx);
2990 np->n_attrstamp = 0;
2992 mtx_lock(&np->n_mtx);
2993 if (np->n_flag & NWRITEERR) {
2994 error = np->n_error;
2995 np->n_flag &= ~NWRITEERR;
2997 if (commit && bo->bo_dirty.bv_cnt == 0 &&
2998 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2999 np->n_flag &= ~NMODIFIED;
3000 mtx_unlock(&np->n_mtx);
3002 if (bvec != NULL && bvec != bvec_on_stack)
3004 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3005 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3006 np->n_directio_asyncwr != 0)) {
3008 /* try, try again... */
3015 vn_printf(vp, "ncl_flush failed");
3016 error = called_from_renewthread != 0 ? EIO : EBUSY;
3022 * NFS advisory byte-level locks.
3025 nfs_advlock(struct vop_advlock_args *ap)
3027 struct vnode *vp = ap->a_vp;
3029 struct nfsnode *np = VTONFS(ap->a_vp);
3030 struct proc *p = (struct proc *)ap->a_id;
3031 struct thread *td = curthread; /* XXX */
3036 error = NFSVOPLOCK(vp, LK_SHARED);
3039 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3040 if (vp->v_type != VREG) {
3044 if ((ap->a_flags & F_POSIX) != 0)
3047 cred = td->td_ucred;
3048 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3049 if (vp->v_iflag & VI_DOOMED) {
3055 * If this is unlocking a write locked region, flush and
3056 * commit them before unlocking. This is required by
3057 * RFC3530 Sec. 9.3.2.
3059 if (ap->a_op == F_UNLCK &&
3060 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3062 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3065 * Loop around doing the lock op, while a blocking lock
3066 * must wait for the lock op to succeed.
3069 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3070 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3071 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3072 ap->a_op == F_SETLK) {
3073 NFSVOPUNLOCK(vp, 0);
3074 error = nfs_catnap(PZERO | PCATCH, ret,
3078 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3079 if (vp->v_iflag & VI_DOOMED) {
3084 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3085 ap->a_op == F_SETLK);
3086 if (ret == NFSERR_DENIED) {
3089 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3092 } else if (ret != 0) {
3098 * Now, if we just got a lock, invalidate data in the buffer
3099 * cache, as required, so that the coherency conforms with
3100 * RFC3530 Sec. 9.3.2.
3102 if (ap->a_op == F_SETLK) {
3103 if ((np->n_flag & NMODIFIED) == 0) {
3104 np->n_attrstamp = 0;
3105 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3106 ret = VOP_GETATTR(vp, &va, cred);
3108 if ((np->n_flag & NMODIFIED) || ret ||
3109 np->n_change != va.va_filerev) {
3110 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3111 np->n_attrstamp = 0;
3112 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3113 ret = VOP_GETATTR(vp, &va, cred);
3115 np->n_mtime = va.va_mtime;
3116 np->n_change = va.va_filerev;
3119 /* Mark that a file lock has been acquired. */
3120 mtx_lock(&np->n_mtx);
3121 np->n_flag |= NHASBEENLOCKED;
3122 mtx_unlock(&np->n_mtx);
3124 } else if (!NFS_ISV4(vp)) {
3125 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3126 size = VTONFS(vp)->n_size;
3127 NFSVOPUNLOCK(vp, 0);
3128 error = lf_advlock(ap, &(vp->v_lockf), size);
3130 if (nfs_advlock_p != NULL)
3131 error = nfs_advlock_p(ap);
3133 NFSVOPUNLOCK(vp, 0);
3137 if (error == 0 && ap->a_op == F_SETLK) {
3138 error = NFSVOPLOCK(vp, LK_SHARED);
3140 /* Mark that a file lock has been acquired. */
3141 mtx_lock(&np->n_mtx);
3142 np->n_flag |= NHASBEENLOCKED;
3143 mtx_unlock(&np->n_mtx);
3144 NFSVOPUNLOCK(vp, 0);
3151 NFSVOPUNLOCK(vp, 0);
3156 * NFS advisory byte-level locks.
3159 nfs_advlockasync(struct vop_advlockasync_args *ap)
3161 struct vnode *vp = ap->a_vp;
3166 return (EOPNOTSUPP);
3167 error = NFSVOPLOCK(vp, LK_SHARED);
3170 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3171 size = VTONFS(vp)->n_size;
3172 NFSVOPUNLOCK(vp, 0);
3173 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3175 NFSVOPUNLOCK(vp, 0);
3182 * Print out the contents of an nfsnode.
3185 nfs_print(struct vop_print_args *ap)
3187 struct vnode *vp = ap->a_vp;
3188 struct nfsnode *np = VTONFS(vp);
3190 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3191 (uintmax_t)np->n_vattr.na_fsid);
3192 if (vp->v_type == VFIFO)
3199 * This is the "real" nfs::bwrite(struct buf*).
3200 * We set B_CACHE if this is a VMIO buffer.
3203 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3205 int oldflags, rtval;
3207 BUF_ASSERT_HELD(bp);
3209 if (bp->b_flags & B_INVAL) {
3214 oldflags = bp->b_flags;
3215 bp->b_flags |= B_CACHE;
3218 * Undirty the bp. We will redirty it later if the I/O fails.
3221 bp->b_flags &= ~B_DONE;
3222 bp->b_ioflags &= ~BIO_ERROR;
3223 bp->b_iocmd = BIO_WRITE;
3225 bufobj_wref(bp->b_bufobj);
3226 curthread->td_ru.ru_oublock++;
3229 * Note: to avoid loopback deadlocks, we do not
3230 * assign b_runningbufspace.
3232 vfs_busy_pages(bp, 1);
3235 bp->b_iooffset = dbtob(bp->b_blkno);
3238 if ((oldflags & B_ASYNC) != 0)
3241 rtval = bufwait(bp);
3242 if (oldflags & B_DELWRI)
3249 * nfs special file access vnode op.
3250 * Essentially just get vattr and then imitate iaccess() since the device is
3251 * local to the client.
3254 nfsspec_access(struct vop_access_args *ap)
3257 struct ucred *cred = ap->a_cred;
3258 struct vnode *vp = ap->a_vp;
3259 accmode_t accmode = ap->a_accmode;
3264 * Disallow write attempts on filesystems mounted read-only;
3265 * unless the file is a socket, fifo, or a block or character
3266 * device resident on the filesystem.
3268 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3269 switch (vp->v_type) {
3279 error = VOP_GETATTR(vp, vap, cred);
3282 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3283 accmode, cred, NULL);
3289 * Read wrapper for fifos.
3292 nfsfifo_read(struct vop_read_args *ap)
3294 struct nfsnode *np = VTONFS(ap->a_vp);
3300 mtx_lock(&np->n_mtx);
3302 vfs_timestamp(&np->n_atim);
3303 mtx_unlock(&np->n_mtx);
3304 error = fifo_specops.vop_read(ap);
3309 * Write wrapper for fifos.
3312 nfsfifo_write(struct vop_write_args *ap)
3314 struct nfsnode *np = VTONFS(ap->a_vp);
3319 mtx_lock(&np->n_mtx);
3321 vfs_timestamp(&np->n_mtim);
3322 mtx_unlock(&np->n_mtx);
3323 return(fifo_specops.vop_write(ap));
3327 * Close wrapper for fifos.
3329 * Update the times on the nfsnode then do fifo close.
3332 nfsfifo_close(struct vop_close_args *ap)
3334 struct vnode *vp = ap->a_vp;
3335 struct nfsnode *np = VTONFS(vp);
3339 mtx_lock(&np->n_mtx);
3340 if (np->n_flag & (NACC | NUPD)) {
3342 if (np->n_flag & NACC)
3344 if (np->n_flag & NUPD)
3347 if (vrefcnt(vp) == 1 &&
3348 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3350 if (np->n_flag & NACC)
3351 vattr.va_atime = np->n_atim;
3352 if (np->n_flag & NUPD)
3353 vattr.va_mtime = np->n_mtim;
3354 mtx_unlock(&np->n_mtx);
3355 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3359 mtx_unlock(&np->n_mtx);
3361 return (fifo_specops.vop_close(ap));
3365 * Just call ncl_writebp() with the force argument set to 1.
3367 * NOTE: B_DONE may or may not be set in a_bp on call.
3370 nfs_bwrite(struct buf *bp)
3373 return (ncl_writebp(bp, 1, curthread));
3376 struct buf_ops buf_ops_newnfs = {
3377 .bop_name = "buf_ops_nfs",
3378 .bop_write = nfs_bwrite,
3379 .bop_strategy = bufstrategy,
3380 .bop_sync = bufsync,
3381 .bop_bdflush = bufbdflush,
3385 nfs_getacl(struct vop_getacl_args *ap)
3389 if (ap->a_type != ACL_TYPE_NFS4)
3390 return (EOPNOTSUPP);
3391 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3393 if (error > NFSERR_STALE) {
3394 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3401 nfs_setacl(struct vop_setacl_args *ap)
3405 if (ap->a_type != ACL_TYPE_NFS4)
3406 return (EOPNOTSUPP);
3407 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3409 if (error > NFSERR_STALE) {
3410 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3417 nfs_set_text(struct vop_set_text_args *ap)
3419 struct vnode *vp = ap->a_vp;
3423 * If the text file has been mmap'd, flush any dirty pages to the
3424 * buffer cache and then...
3425 * Make sure all writes are pushed to the NFS server. If this is not
3426 * done, the modify time of the file can change while the text
3427 * file is being executed. This will cause the process that is
3428 * executing the text file to be terminated.
3430 if (vp->v_object != NULL) {
3431 VM_OBJECT_WLOCK(vp->v_object);
3432 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3433 VM_OBJECT_WUNLOCK(vp->v_object);
3436 /* Now, flush the buffer cache. */
3437 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
3439 /* And, finally, make sure that n_mtime is up to date. */
3441 mtx_lock(&np->n_mtx);
3442 np->n_mtime = np->n_vattr.na_mtime;
3443 mtx_unlock(&np->n_mtx);
3445 vp->v_vflag |= VV_TEXT;
3450 * Return POSIX pathconf information applicable to nfs filesystems.
3453 nfs_pathconf(struct vop_pathconf_args *ap)
3455 struct nfsv3_pathconf pc;
3456 struct nfsvattr nfsva;
3457 struct vnode *vp = ap->a_vp;
3458 struct thread *td = curthread;
3459 int attrflag, error;
3461 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3462 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3463 ap->a_name == _PC_NO_TRUNC)) ||
3464 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3466 * Since only the above 4 a_names are returned by the NFSv3
3467 * Pathconf RPC, there is no point in doing it for others.
3468 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3469 * be used for _PC_NFS4_ACL as well.
3471 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3474 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3480 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3483 pc.pc_linkmax = NFS_LINK_MAX;
3484 pc.pc_namemax = NFS_MAXNAMLEN;
3486 pc.pc_chownrestricted = 1;
3487 pc.pc_caseinsensitive = 0;
3488 pc.pc_casepreserving = 1;
3491 switch (ap->a_name) {
3494 *ap->a_retval = pc.pc_linkmax;
3496 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
3500 *ap->a_retval = pc.pc_namemax;
3503 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
3504 *ap->a_retval = PIPE_BUF;
3508 case _PC_CHOWN_RESTRICTED:
3509 *ap->a_retval = pc.pc_chownrestricted;
3512 *ap->a_retval = pc.pc_notrunc;
3514 case _PC_ACL_EXTENDED:
3518 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3519 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3524 case _PC_ACL_PATH_MAX:
3526 *ap->a_retval = ACL_MAX_ENTRIES;
3530 case _PC_MAC_PRESENT:
3539 case _PC_ALLOC_SIZE_MIN:
3540 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3542 case _PC_FILESIZEBITS:
3548 case _PC_REC_INCR_XFER_SIZE:
3549 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3551 case _PC_REC_MAX_XFER_SIZE:
3552 *ap->a_retval = -1; /* means ``unlimited'' */
3554 case _PC_REC_MIN_XFER_SIZE:
3555 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3557 case _PC_REC_XFER_ALIGN:
3558 *ap->a_retval = PAGE_SIZE;
3560 case _PC_SYMLINK_MAX:
3561 *ap->a_retval = NFS_MAXPATHLEN;
3565 error = vop_stdpathconf(ap);