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
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_lookup = nfs_lookup,
164 .vop_mkdir = nfs_mkdir,
165 .vop_mknod = nfs_mknod,
166 .vop_open = nfs_open,
167 .vop_pathconf = nfs_pathconf,
168 .vop_print = nfs_print,
169 .vop_read = nfs_read,
170 .vop_readdir = nfs_readdir,
171 .vop_readlink = nfs_readlink,
172 .vop_reclaim = ncl_reclaim,
173 .vop_remove = nfs_remove,
174 .vop_rename = nfs_rename,
175 .vop_rmdir = nfs_rmdir,
176 .vop_setattr = nfs_setattr,
177 .vop_strategy = nfs_strategy,
178 .vop_symlink = nfs_symlink,
179 .vop_write = ncl_write,
180 .vop_getacl = nfs_getacl,
181 .vop_setacl = nfs_setacl,
182 .vop_set_text = nfs_set_text,
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 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
277 struct ucred *cred, u_int32_t *retmode)
279 int error = 0, attrflag, i, lrupos;
281 struct nfsnode *np = VTONFS(vp);
282 struct nfsvattr nfsva;
284 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
287 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
290 mtx_lock(&np->n_mtx);
291 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
292 if (np->n_accesscache[i].uid == cred->cr_uid) {
293 np->n_accesscache[i].mode = rmode;
294 np->n_accesscache[i].stamp = time_second;
297 if (i > 0 && np->n_accesscache[i].stamp <
298 np->n_accesscache[lrupos].stamp)
301 if (i == NFS_ACCESSCACHESIZE) {
302 np->n_accesscache[lrupos].uid = cred->cr_uid;
303 np->n_accesscache[lrupos].mode = rmode;
304 np->n_accesscache[lrupos].stamp = time_second;
306 mtx_unlock(&np->n_mtx);
309 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
310 } else if (NFS_ISV4(vp)) {
311 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
315 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
322 * nfs access vnode op.
323 * For nfs version 2, just return ok. File accesses may fail later.
324 * For nfs version 3, use the access rpc to check accessibility. If file modes
325 * are changed on the server, accesses might still fail later.
328 nfs_access(struct vop_access_args *ap)
330 struct vnode *vp = ap->a_vp;
331 int error = 0, i, gotahit;
332 u_int32_t mode, wmode, rmode;
333 int v34 = NFS_ISV34(vp);
334 struct nfsnode *np = VTONFS(vp);
337 * Disallow write attempts on filesystems mounted read-only;
338 * unless the file is a socket, fifo, or a block or character
339 * device resident on the filesystem.
341 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
342 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
343 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
344 switch (vp->v_type) {
354 * For nfs v3 or v4, check to see if we have done this recently, and if
355 * so return our cached result instead of making an ACCESS call.
356 * If not, do an access rpc, otherwise you are stuck emulating
357 * ufs_access() locally using the vattr. This may not be correct,
358 * since the server may apply other access criteria such as
359 * client uid-->server uid mapping that we do not know about.
362 if (ap->a_accmode & VREAD)
363 mode = NFSACCESS_READ;
366 if (vp->v_type != VDIR) {
367 if (ap->a_accmode & VWRITE)
368 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
369 if (ap->a_accmode & VAPPEND)
370 mode |= NFSACCESS_EXTEND;
371 if (ap->a_accmode & VEXEC)
372 mode |= NFSACCESS_EXECUTE;
373 if (ap->a_accmode & VDELETE)
374 mode |= NFSACCESS_DELETE;
376 if (ap->a_accmode & VWRITE)
377 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
378 if (ap->a_accmode & VAPPEND)
379 mode |= NFSACCESS_EXTEND;
380 if (ap->a_accmode & VEXEC)
381 mode |= NFSACCESS_LOOKUP;
382 if (ap->a_accmode & VDELETE)
383 mode |= NFSACCESS_DELETE;
384 if (ap->a_accmode & VDELETE_CHILD)
385 mode |= NFSACCESS_MODIFY;
387 /* XXX safety belt, only make blanket request if caching */
388 if (nfsaccess_cache_timeout > 0) {
389 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
390 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
391 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
397 * Does our cached result allow us to give a definite yes to
401 mtx_lock(&np->n_mtx);
402 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
403 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
404 if (time_second < (np->n_accesscache[i].stamp
405 + nfsaccess_cache_timeout) &&
406 (np->n_accesscache[i].mode & mode) == mode) {
407 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
413 mtx_unlock(&np->n_mtx);
416 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
417 ap->a_cred->cr_uid, mode);
419 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
420 ap->a_cred->cr_uid, mode);
424 * Either a no, or a don't know. Go to the wire.
426 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
427 error = nfs34_access_otw(vp, wmode, ap->a_td,
430 (rmode & mode) != mode)
435 if ((error = nfsspec_access(ap)) != 0) {
439 * Attempt to prevent a mapped root from accessing a file
440 * which it shouldn't. We try to read a byte from the file
441 * if the user is root and the file is not zero length.
442 * After calling nfsspec_access, we should have the correct
445 mtx_lock(&np->n_mtx);
446 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
447 && VTONFS(vp)->n_size > 0) {
452 mtx_unlock(&np->n_mtx);
455 auio.uio_iov = &aiov;
459 auio.uio_segflg = UIO_SYSSPACE;
460 auio.uio_rw = UIO_READ;
461 auio.uio_td = ap->a_td;
463 if (vp->v_type == VREG)
464 error = ncl_readrpc(vp, &auio, ap->a_cred);
465 else if (vp->v_type == VDIR) {
467 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
469 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
470 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
473 } else if (vp->v_type == VLNK)
474 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
478 mtx_unlock(&np->n_mtx);
486 * Check to see if the type is ok
487 * and that deletion is not in progress.
488 * For paged in text files, you will need to flush the page cache
489 * if consistency is lost.
493 nfs_open(struct vop_open_args *ap)
495 struct vnode *vp = ap->a_vp;
496 struct nfsnode *np = VTONFS(vp);
499 int fmode = ap->a_mode;
502 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
506 * For NFSv4, we need to do the Open Op before cache validation,
507 * so that we conform to RFC3530 Sec. 9.3.1.
510 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
512 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
519 * Now, if this Open will be doing reading, re-validate/flush the
520 * cache, so that Close/Open coherency is maintained.
522 mtx_lock(&np->n_mtx);
523 if (np->n_flag & NMODIFIED) {
524 mtx_unlock(&np->n_mtx);
525 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
526 if (error == EINTR || error == EIO) {
528 (void) nfsrpc_close(vp, 0, ap->a_td);
531 mtx_lock(&np->n_mtx);
533 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
534 if (vp->v_type == VDIR)
535 np->n_direofoffset = 0;
536 mtx_unlock(&np->n_mtx);
537 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
540 (void) nfsrpc_close(vp, 0, ap->a_td);
543 mtx_lock(&np->n_mtx);
544 np->n_mtime = vattr.va_mtime;
546 np->n_change = vattr.va_filerev;
548 mtx_unlock(&np->n_mtx);
549 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
552 (void) nfsrpc_close(vp, 0, ap->a_td);
555 mtx_lock(&np->n_mtx);
556 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
557 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
558 if (vp->v_type == VDIR)
559 np->n_direofoffset = 0;
560 mtx_unlock(&np->n_mtx);
561 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
562 if (error == EINTR || error == EIO) {
564 (void) nfsrpc_close(vp, 0, ap->a_td);
567 mtx_lock(&np->n_mtx);
568 np->n_mtime = vattr.va_mtime;
570 np->n_change = vattr.va_filerev;
575 * If the object has >= 1 O_DIRECT active opens, we disable caching.
577 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
578 (vp->v_type == VREG)) {
579 if (np->n_directio_opens == 0) {
580 mtx_unlock(&np->n_mtx);
581 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
584 (void) nfsrpc_close(vp, 0, ap->a_td);
587 mtx_lock(&np->n_mtx);
588 np->n_flag |= NNONCACHE;
590 np->n_directio_opens++;
593 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
594 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
595 np->n_flag |= NWRITEOPENED;
598 * If this is an open for writing, capture a reference to the
599 * credentials, so they can be used by ncl_putpages(). Using
600 * these write credentials is preferable to the credentials of
601 * whatever thread happens to be doing the VOP_PUTPAGES() since
602 * the write RPCs are less likely to fail with EACCES.
604 if ((fmode & FWRITE) != 0) {
605 cred = np->n_writecred;
606 np->n_writecred = crhold(ap->a_cred);
609 mtx_unlock(&np->n_mtx);
613 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
619 * What an NFS client should do upon close after writing is a debatable issue.
620 * Most NFS clients push delayed writes to the server upon close, basically for
622 * 1 - So that any write errors may be reported back to the client process
623 * doing the close system call. By far the two most likely errors are
624 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
625 * 2 - To put a worst case upper bound on cache inconsistency between
626 * multiple clients for the file.
627 * There is also a consistency problem for Version 2 of the protocol w.r.t.
628 * not being able to tell if other clients are writing a file concurrently,
629 * since there is no way of knowing if the changed modify time in the reply
630 * is only due to the write for this client.
631 * (NFS Version 3 provides weak cache consistency data in the reply that
632 * should be sufficient to detect and handle this case.)
634 * The current code does the following:
635 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
636 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
637 * or commit them (this satisfies 1 and 2 except for the
638 * case where the server crashes after this close but
639 * before the commit RPC, which is felt to be "good
640 * enough". Changing the last argument to ncl_flush() to
641 * a 1 would force a commit operation, if it is felt a
642 * commit is necessary now.
643 * for NFS Version 4 - flush the dirty buffers and commit them, if
644 * nfscl_mustflush() says this is necessary.
645 * It is necessary if there is no write delegation held,
646 * in order to satisfy open/close coherency.
647 * If the file isn't cached on local stable storage,
648 * it may be necessary in order to detect "out of space"
649 * errors from the server, if the write delegation
650 * issued by the server doesn't allow the file to grow.
654 nfs_close(struct vop_close_args *ap)
656 struct vnode *vp = ap->a_vp;
657 struct nfsnode *np = VTONFS(vp);
658 struct nfsvattr nfsva;
660 int error = 0, ret, localcred = 0;
661 int fmode = ap->a_fflag;
663 if (NFSCL_FORCEDISM(vp->v_mount))
666 * During shutdown, a_cred isn't valid, so just use root.
668 if (ap->a_cred == NOCRED) {
669 cred = newnfs_getcred();
674 if (vp->v_type == VREG) {
676 * Examine and clean dirty pages, regardless of NMODIFIED.
677 * This closes a major hole in close-to-open consistency.
678 * We want to push out all dirty pages (and buffers) on
679 * close, regardless of whether they were dirtied by
680 * mmap'ed writes or via write().
682 if (nfs_clean_pages_on_close && vp->v_object) {
683 VM_OBJECT_WLOCK(vp->v_object);
684 vm_object_page_clean(vp->v_object, 0, 0, 0);
685 VM_OBJECT_WUNLOCK(vp->v_object);
687 mtx_lock(&np->n_mtx);
688 if (np->n_flag & NMODIFIED) {
689 mtx_unlock(&np->n_mtx);
692 * Under NFSv3 we have dirty buffers to dispose of. We
693 * must flush them to the NFS server. We have the option
694 * of waiting all the way through the commit rpc or just
695 * waiting for the initial write. The default is to only
696 * wait through the initial write so the data is in the
697 * server's cache, which is roughly similar to the state
698 * a standard disk subsystem leaves the file in on close().
700 * We cannot clear the NMODIFIED bit in np->n_flag due to
701 * potential races with other processes, and certainly
702 * cannot clear it if we don't commit.
703 * These races occur when there is no longer the old
704 * traditional vnode locking implemented for Vnode Ops.
706 int cm = newnfs_commit_on_close ? 1 : 0;
707 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
708 /* np->n_flag &= ~NMODIFIED; */
709 } else if (NFS_ISV4(vp)) {
710 if (nfscl_mustflush(vp) != 0) {
711 int cm = newnfs_commit_on_close ? 1 : 0;
712 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
715 * as above w.r.t races when clearing
717 * np->n_flag &= ~NMODIFIED;
721 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
723 mtx_lock(&np->n_mtx);
726 * Invalidate the attribute cache in all cases.
727 * An open is going to fetch fresh attrs any way, other procs
728 * on this node that have file open will be forced to do an
729 * otw attr fetch, but this is safe.
730 * --> A user found that their RPC count dropped by 20% when
731 * this was commented out and I can't see any requirement
732 * for it, so I've disabled it when negative lookups are
733 * enabled. (What does this have to do with negative lookup
734 * caching? Well nothing, except it was reported by the
735 * same user that needed negative lookup caching and I wanted
736 * there to be a way to disable it to see if it
737 * is the cause of some caching/coherency issue that might
740 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
742 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
744 if (np->n_flag & NWRITEERR) {
745 np->n_flag &= ~NWRITEERR;
748 mtx_unlock(&np->n_mtx);
753 * Get attributes so "change" is up to date.
755 if (error == 0 && nfscl_mustflush(vp) != 0 &&
756 vp->v_type == VREG &&
757 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
758 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
761 np->n_change = nfsva.na_filerev;
762 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
770 ret = nfsrpc_close(vp, 0, ap->a_td);
774 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
777 if (newnfs_directio_enable)
778 KASSERT((np->n_directio_asyncwr == 0),
779 ("nfs_close: dirty unflushed (%d) directio buffers\n",
780 np->n_directio_asyncwr));
781 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
782 mtx_lock(&np->n_mtx);
783 KASSERT((np->n_directio_opens > 0),
784 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
785 np->n_directio_opens--;
786 if (np->n_directio_opens == 0)
787 np->n_flag &= ~NNONCACHE;
788 mtx_unlock(&np->n_mtx);
796 * nfs getattr call from vfs.
799 nfs_getattr(struct vop_getattr_args *ap)
801 struct vnode *vp = ap->a_vp;
802 struct thread *td = curthread; /* XXX */
803 struct nfsnode *np = VTONFS(vp);
805 struct nfsvattr nfsva;
806 struct vattr *vap = ap->a_vap;
810 * Update local times for special files.
812 mtx_lock(&np->n_mtx);
813 if (np->n_flag & (NACC | NUPD))
815 mtx_unlock(&np->n_mtx);
817 * First look in the cache.
819 if (ncl_getattrcache(vp, &vattr) == 0) {
820 vap->va_type = vattr.va_type;
821 vap->va_mode = vattr.va_mode;
822 vap->va_nlink = vattr.va_nlink;
823 vap->va_uid = vattr.va_uid;
824 vap->va_gid = vattr.va_gid;
825 vap->va_fsid = vattr.va_fsid;
826 vap->va_fileid = vattr.va_fileid;
827 vap->va_size = vattr.va_size;
828 vap->va_blocksize = vattr.va_blocksize;
829 vap->va_atime = vattr.va_atime;
830 vap->va_mtime = vattr.va_mtime;
831 vap->va_ctime = vattr.va_ctime;
832 vap->va_gen = vattr.va_gen;
833 vap->va_flags = vattr.va_flags;
834 vap->va_rdev = vattr.va_rdev;
835 vap->va_bytes = vattr.va_bytes;
836 vap->va_filerev = vattr.va_filerev;
838 * Get the local modify time for the case of a write
841 nfscl_deleggetmodtime(vp, &vap->va_mtime);
845 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
846 nfsaccess_cache_timeout > 0) {
847 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
848 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
849 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
850 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
854 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
856 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
859 * Get the local modify time for the case of a write
862 nfscl_deleggetmodtime(vp, &vap->va_mtime);
863 } else if (NFS_ISV4(vp)) {
864 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
873 nfs_setattr(struct vop_setattr_args *ap)
875 struct vnode *vp = ap->a_vp;
876 struct nfsnode *np = VTONFS(vp);
877 struct thread *td = curthread; /* XXX */
878 struct vattr *vap = ap->a_vap;
887 * Setting of flags and marking of atimes are not supported.
889 if (vap->va_flags != VNOVAL)
893 * Disallow write attempts if the filesystem is mounted read-only.
895 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
896 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
897 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
898 (vp->v_mount->mnt_flag & MNT_RDONLY))
900 if (vap->va_size != VNOVAL) {
901 switch (vp->v_type) {
908 if (vap->va_mtime.tv_sec == VNOVAL &&
909 vap->va_atime.tv_sec == VNOVAL &&
910 vap->va_mode == (mode_t)VNOVAL &&
911 vap->va_uid == (uid_t)VNOVAL &&
912 vap->va_gid == (gid_t)VNOVAL)
914 vap->va_size = VNOVAL;
918 * Disallow write attempts if the filesystem is
921 if (vp->v_mount->mnt_flag & MNT_RDONLY)
924 * We run vnode_pager_setsize() early (why?),
925 * we must set np->n_size now to avoid vinvalbuf
926 * V_SAVE races that might setsize a lower
929 mtx_lock(&np->n_mtx);
931 mtx_unlock(&np->n_mtx);
932 error = ncl_meta_setsize(vp, ap->a_cred, td,
934 mtx_lock(&np->n_mtx);
935 if (np->n_flag & NMODIFIED) {
937 mtx_unlock(&np->n_mtx);
938 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
941 vnode_pager_setsize(vp, tsize);
945 * Call nfscl_delegmodtime() to set the modify time
946 * locally, as required.
948 nfscl_delegmodtime(vp);
950 mtx_unlock(&np->n_mtx);
952 * np->n_size has already been set to vap->va_size
953 * in ncl_meta_setsize(). We must set it again since
954 * nfs_loadattrcache() could be called through
955 * ncl_meta_setsize() and could modify np->n_size.
957 mtx_lock(&np->n_mtx);
958 np->n_vattr.na_size = np->n_size = vap->va_size;
959 mtx_unlock(&np->n_mtx);
962 mtx_lock(&np->n_mtx);
963 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
964 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
965 mtx_unlock(&np->n_mtx);
966 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
967 if (error == EINTR || error == EIO)
970 mtx_unlock(&np->n_mtx);
972 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
973 if (error && vap->va_size != VNOVAL) {
974 mtx_lock(&np->n_mtx);
975 np->n_size = np->n_vattr.na_size = tsize;
976 vnode_pager_setsize(vp, tsize);
977 mtx_unlock(&np->n_mtx);
983 * Do an nfs setattr rpc.
986 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
989 struct nfsnode *np = VTONFS(vp);
990 int error, ret, attrflag, i;
991 struct nfsvattr nfsva;
994 mtx_lock(&np->n_mtx);
995 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
996 np->n_accesscache[i].stamp = 0;
997 np->n_flag |= NDELEGMOD;
998 mtx_unlock(&np->n_mtx);
999 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1001 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1004 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1008 if (error && NFS_ISV4(vp))
1009 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1014 * nfs lookup call, one step at a time...
1015 * First look in cache
1016 * If not found, unlock the directory nfsnode and do the rpc
1019 nfs_lookup(struct vop_lookup_args *ap)
1021 struct componentname *cnp = ap->a_cnp;
1022 struct vnode *dvp = ap->a_dvp;
1023 struct vnode **vpp = ap->a_vpp;
1024 struct mount *mp = dvp->v_mount;
1025 int flags = cnp->cn_flags;
1026 struct vnode *newvp;
1027 struct nfsmount *nmp;
1028 struct nfsnode *np, *newnp;
1029 int error = 0, attrflag, dattrflag, ltype, ncticks;
1030 struct thread *td = cnp->cn_thread;
1032 struct nfsvattr dnfsva, nfsva;
1034 struct timespec nctime;
1037 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1038 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1040 if (dvp->v_type != VDIR)
1045 /* For NFSv4, wait until any remove is done. */
1046 mtx_lock(&np->n_mtx);
1047 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1048 np->n_flag |= NREMOVEWANT;
1049 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1051 mtx_unlock(&np->n_mtx);
1053 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1055 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1056 if (error > 0 && error != ENOENT)
1060 * Lookups of "." are special and always return the
1061 * current directory. cache_lookup() already handles
1062 * associated locking bookkeeping, etc.
1064 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1065 /* XXX: Is this really correct? */
1066 if (cnp->cn_nameiop != LOOKUP &&
1068 cnp->cn_flags |= SAVENAME;
1073 * We only accept a positive hit in the cache if the
1074 * change time of the file matches our cached copy.
1075 * Otherwise, we discard the cache entry and fallback
1076 * to doing a lookup RPC. We also only trust cache
1077 * entries for less than nm_nametimeo seconds.
1079 * To better handle stale file handles and attributes,
1080 * clear the attribute cache of this node if it is a
1081 * leaf component, part of an open() call, and not
1082 * locally modified before fetching the attributes.
1083 * This should allow stale file handles to be detected
1084 * here where we can fall back to a LOOKUP RPC to
1085 * recover rather than having nfs_open() detect the
1086 * stale file handle and failing open(2) with ESTALE.
1089 newnp = VTONFS(newvp);
1090 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1091 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1092 !(newnp->n_flag & NMODIFIED)) {
1093 mtx_lock(&newnp->n_mtx);
1094 newnp->n_attrstamp = 0;
1095 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1096 mtx_unlock(&newnp->n_mtx);
1098 if (nfscl_nodeleg(newvp, 0) == 0 ||
1099 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1100 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1101 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1102 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1103 if (cnp->cn_nameiop != LOOKUP &&
1105 cnp->cn_flags |= SAVENAME;
1114 } else if (error == ENOENT) {
1115 if (dvp->v_iflag & VI_DOOMED)
1118 * We only accept a negative hit in the cache if the
1119 * modification time of the parent directory matches
1120 * the cached copy in the name cache entry.
1121 * Otherwise, we discard all of the negative cache
1122 * entries for this directory. We also only trust
1123 * negative cache entries for up to nm_negnametimeo
1126 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1127 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1128 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1129 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1132 cache_purge_negative(dvp);
1137 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1138 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1139 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1142 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1144 if (newvp != NULLVP) {
1149 if (error != ENOENT) {
1151 error = nfscl_maperr(td, error, (uid_t)0,
1156 /* The requested file was not found. */
1157 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1158 (flags & ISLASTCN)) {
1160 * XXX: UFS does a full VOP_ACCESS(dvp,
1161 * VWRITE) here instead of just checking
1164 if (mp->mnt_flag & MNT_RDONLY)
1166 cnp->cn_flags |= SAVENAME;
1167 return (EJUSTRETURN);
1170 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1172 * Cache the modification time of the parent
1173 * directory from the post-op attributes in
1174 * the name cache entry. The negative cache
1175 * entry will be ignored once the directory
1176 * has changed. Don't bother adding the entry
1177 * if the directory has already changed.
1179 mtx_lock(&np->n_mtx);
1180 if (timespeccmp(&np->n_vattr.na_mtime,
1181 &dnfsva.na_mtime, ==)) {
1182 mtx_unlock(&np->n_mtx);
1183 cache_enter_time(dvp, NULL, cnp,
1184 &dnfsva.na_mtime, NULL);
1186 mtx_unlock(&np->n_mtx);
1192 * Handle RENAME case...
1194 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1195 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1196 FREE((caddr_t)nfhp, M_NFSFH);
1199 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1205 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1208 cnp->cn_flags |= SAVENAME;
1212 if (flags & ISDOTDOT) {
1213 ltype = NFSVOPISLOCKED(dvp);
1214 error = vfs_busy(mp, MBF_NOWAIT);
1217 NFSVOPUNLOCK(dvp, 0);
1218 error = vfs_busy(mp, 0);
1219 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1221 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1228 NFSVOPUNLOCK(dvp, 0);
1229 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1235 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1236 if (dvp->v_iflag & VI_DOOMED) {
1248 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1250 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1251 FREE((caddr_t)nfhp, M_NFSFH);
1255 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1258 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1264 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1266 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1267 !(np->n_flag & NMODIFIED)) {
1269 * Flush the attribute cache when opening a
1270 * leaf node to ensure that fresh attributes
1271 * are fetched in nfs_open() since we did not
1272 * fetch attributes from the LOOKUP reply.
1274 mtx_lock(&np->n_mtx);
1275 np->n_attrstamp = 0;
1276 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1277 mtx_unlock(&np->n_mtx);
1280 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1281 cnp->cn_flags |= SAVENAME;
1282 if ((cnp->cn_flags & MAKEENTRY) &&
1283 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1284 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1285 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1286 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1293 * Just call ncl_bioread() to do the work.
1296 nfs_read(struct vop_read_args *ap)
1298 struct vnode *vp = ap->a_vp;
1300 switch (vp->v_type) {
1302 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1306 return (EOPNOTSUPP);
1314 nfs_readlink(struct vop_readlink_args *ap)
1316 struct vnode *vp = ap->a_vp;
1318 if (vp->v_type != VLNK)
1320 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1324 * Do a readlink rpc.
1325 * Called by ncl_doio() from below the buffer cache.
1328 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1330 int error, ret, attrflag;
1331 struct nfsvattr nfsva;
1333 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1336 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1340 if (error && NFS_ISV4(vp))
1341 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1350 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1352 int error, ret, attrflag;
1353 struct nfsvattr nfsva;
1354 struct nfsmount *nmp;
1356 nmp = VFSTONFS(vnode_mount(vp));
1359 if (NFSHASPNFS(nmp))
1360 error = nfscl_doiods(vp, uiop, NULL, NULL,
1361 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1362 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1364 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1367 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1371 if (error && NFS_ISV4(vp))
1372 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1380 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1381 int *iomode, int *must_commit, int called_from_strategy)
1383 struct nfsvattr nfsva;
1384 int error, attrflag, ret;
1385 struct nfsmount *nmp;
1387 nmp = VFSTONFS(vnode_mount(vp));
1390 if (NFSHASPNFS(nmp))
1391 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1392 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1393 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1395 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1396 uiop->uio_td, &nfsva, &attrflag, NULL,
1397 called_from_strategy);
1399 if (VTONFS(vp)->n_flag & ND_NFSV4)
1400 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1403 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1409 *iomode = NFSWRITE_FILESYNC;
1410 if (error && NFS_ISV4(vp))
1411 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1417 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1418 * mode set to specify the file type and the size field for rdev.
1421 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1424 struct nfsvattr nfsva, dnfsva;
1425 struct vnode *newvp = NULL;
1426 struct nfsnode *np = NULL, *dnp;
1429 int error = 0, attrflag, dattrflag;
1432 if (vap->va_type == VCHR || vap->va_type == VBLK)
1433 rdev = vap->va_rdev;
1434 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1437 return (EOPNOTSUPP);
1438 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1440 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1441 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1442 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1445 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1446 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1447 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1450 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1451 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1454 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1457 if (attrflag != 0) {
1458 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1466 } else if (NFS_ISV4(dvp)) {
1467 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1471 mtx_lock(&dnp->n_mtx);
1472 dnp->n_flag |= NMODIFIED;
1474 dnp->n_attrstamp = 0;
1475 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1477 mtx_unlock(&dnp->n_mtx);
1483 * just call nfs_mknodrpc() to do the work.
1487 nfs_mknod(struct vop_mknod_args *ap)
1489 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1492 static struct mtx nfs_cverf_mtx;
1493 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1499 static nfsquad_t cverf;
1501 static int cverf_initialized = 0;
1503 mtx_lock(&nfs_cverf_mtx);
1504 if (cverf_initialized == 0) {
1505 cverf.lval[0] = arc4random();
1506 cverf.lval[1] = arc4random();
1507 cverf_initialized = 1;
1511 mtx_unlock(&nfs_cverf_mtx);
1517 * nfs file create call
1520 nfs_create(struct vop_create_args *ap)
1522 struct vnode *dvp = ap->a_dvp;
1523 struct vattr *vap = ap->a_vap;
1524 struct componentname *cnp = ap->a_cnp;
1525 struct nfsnode *np = NULL, *dnp;
1526 struct vnode *newvp = NULL;
1527 struct nfsmount *nmp;
1528 struct nfsvattr dnfsva, nfsva;
1531 int error = 0, attrflag, dattrflag, fmode = 0;
1535 * Oops, not for me..
1537 if (vap->va_type == VSOCK)
1538 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1540 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1542 if (vap->va_vaflags & VA_EXCLUSIVE)
1545 nmp = VFSTONFS(vnode_mount(dvp));
1547 /* For NFSv4, wait until any remove is done. */
1548 mtx_lock(&dnp->n_mtx);
1549 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1550 dnp->n_flag |= NREMOVEWANT;
1551 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1553 mtx_unlock(&dnp->n_mtx);
1555 cverf = nfs_get_cverf();
1556 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1557 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1558 &nfhp, &attrflag, &dattrflag, NULL);
1561 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1562 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1563 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1566 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1567 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1570 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1574 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1575 cnp->cn_thread, &nfsva, NULL);
1577 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1581 if (newvp != NULL) {
1585 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1586 error == NFSERR_NOTSUPP) {
1590 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1591 if (nfscl_checksattr(vap, &nfsva)) {
1592 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1593 cnp->cn_thread, &nfsva, &attrflag, NULL);
1594 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1595 vap->va_gid != (gid_t)VNOVAL)) {
1596 /* try again without setting uid/gid */
1597 vap->va_uid = (uid_t)VNOVAL;
1598 vap->va_gid = (uid_t)VNOVAL;
1599 error = nfsrpc_setattr(newvp, vap, NULL,
1600 cnp->cn_cred, cnp->cn_thread, &nfsva,
1604 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1611 if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1612 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1615 } else if (NFS_ISV4(dvp)) {
1616 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1619 mtx_lock(&dnp->n_mtx);
1620 dnp->n_flag |= NMODIFIED;
1622 dnp->n_attrstamp = 0;
1623 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1625 mtx_unlock(&dnp->n_mtx);
1630 * nfs file remove call
1631 * To try and make nfs semantics closer to ufs semantics, a file that has
1632 * other processes using the vnode is renamed instead of removed and then
1633 * removed later on the last close.
1634 * - If v_usecount > 1
1635 * If a rename is not already in the works
1636 * call nfs_sillyrename() to set it up
1641 nfs_remove(struct vop_remove_args *ap)
1643 struct vnode *vp = ap->a_vp;
1644 struct vnode *dvp = ap->a_dvp;
1645 struct componentname *cnp = ap->a_cnp;
1646 struct nfsnode *np = VTONFS(vp);
1650 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1651 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1652 if (vp->v_type == VDIR)
1654 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1655 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1656 vattr.va_nlink > 1)) {
1658 * Purge the name cache so that the chance of a lookup for
1659 * the name succeeding while the remove is in progress is
1660 * minimized. Without node locking it can still happen, such
1661 * that an I/O op returns ESTALE, but since you get this if
1662 * another host removes the file..
1666 * throw away biocache buffers, mainly to avoid
1667 * unnecessary delayed writes later.
1669 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1670 if (error != EINTR && error != EIO)
1672 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1673 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1675 * Kludge City: If the first reply to the remove rpc is lost..
1676 * the reply to the retransmitted request will be ENOENT
1677 * since the file was in fact removed
1678 * Therefore, we cheat and return success.
1680 if (error == ENOENT)
1682 } else if (!np->n_sillyrename)
1683 error = nfs_sillyrename(dvp, vp, cnp);
1684 mtx_lock(&np->n_mtx);
1685 np->n_attrstamp = 0;
1686 mtx_unlock(&np->n_mtx);
1687 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1692 * nfs file remove rpc called from nfs_inactive
1695 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1698 * Make sure that the directory vnode is still valid.
1699 * XXX we should lock sp->s_dvp here.
1701 if (sp->s_dvp->v_type == VBAD)
1703 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1708 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1711 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1712 int namelen, struct ucred *cred, struct thread *td)
1714 struct nfsvattr dnfsva;
1715 struct nfsnode *dnp = VTONFS(dvp);
1716 int error = 0, dattrflag;
1718 mtx_lock(&dnp->n_mtx);
1719 dnp->n_flag |= NREMOVEINPROG;
1720 mtx_unlock(&dnp->n_mtx);
1721 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1723 mtx_lock(&dnp->n_mtx);
1724 if ((dnp->n_flag & NREMOVEWANT)) {
1725 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1726 mtx_unlock(&dnp->n_mtx);
1727 wakeup((caddr_t)dnp);
1729 dnp->n_flag &= ~NREMOVEINPROG;
1730 mtx_unlock(&dnp->n_mtx);
1733 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1734 mtx_lock(&dnp->n_mtx);
1735 dnp->n_flag |= NMODIFIED;
1737 dnp->n_attrstamp = 0;
1738 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1740 mtx_unlock(&dnp->n_mtx);
1741 if (error && NFS_ISV4(dvp))
1742 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1747 * nfs file rename call
1750 nfs_rename(struct vop_rename_args *ap)
1752 struct vnode *fvp = ap->a_fvp;
1753 struct vnode *tvp = ap->a_tvp;
1754 struct vnode *fdvp = ap->a_fdvp;
1755 struct vnode *tdvp = ap->a_tdvp;
1756 struct componentname *tcnp = ap->a_tcnp;
1757 struct componentname *fcnp = ap->a_fcnp;
1758 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1759 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1760 struct nfsv4node *newv4 = NULL;
1763 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1764 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1765 /* Check for cross-device rename */
1766 if ((fvp->v_mount != tdvp->v_mount) ||
1767 (tvp && (fvp->v_mount != tvp->v_mount))) {
1773 printf("nfs_rename: fvp == tvp (can't happen)\n");
1777 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1781 * We have to flush B_DELWRI data prior to renaming
1782 * the file. If we don't, the delayed-write buffers
1783 * can be flushed out later after the file has gone stale
1784 * under NFSV3. NFSV2 does not have this problem because
1785 * ( as far as I can tell ) it flushes dirty buffers more
1788 * Skip the rename operation if the fsync fails, this can happen
1789 * due to the server's volume being full, when we pushed out data
1790 * that was written back to our cache earlier. Not checking for
1791 * this condition can result in potential (silent) data loss.
1793 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1794 NFSVOPUNLOCK(fvp, 0);
1796 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1801 * If the tvp exists and is in use, sillyrename it before doing the
1802 * rename of the new file over it.
1803 * XXX Can't sillyrename a directory.
1805 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1806 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1811 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1812 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1815 if (error == 0 && NFS_ISV4(tdvp)) {
1817 * For NFSv4, check to see if it is the same name and
1818 * replace the name, if it is different.
1820 MALLOC(newv4, struct nfsv4node *,
1821 sizeof (struct nfsv4node) +
1822 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1823 M_NFSV4NODE, M_WAITOK);
1824 mtx_lock(&tdnp->n_mtx);
1825 mtx_lock(&fnp->n_mtx);
1826 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1827 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1828 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1829 tcnp->cn_namelen) ||
1830 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1831 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1832 tdnp->n_fhp->nfh_len))) {
1834 { char nnn[100]; int nnnl;
1835 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1836 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1838 printf("ren replace=%s\n",nnn);
1841 FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1844 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1845 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1846 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1847 tdnp->n_fhp->nfh_len);
1848 NFSBCOPY(tcnp->cn_nameptr,
1849 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1851 mtx_unlock(&tdnp->n_mtx);
1852 mtx_unlock(&fnp->n_mtx);
1854 FREE((caddr_t)newv4, M_NFSV4NODE);
1857 if (fvp->v_type == VDIR) {
1858 if (tvp != NULL && tvp->v_type == VDIR)
1873 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1875 if (error == ENOENT)
1881 * nfs file rename rpc called from nfs_remove() above
1884 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1885 struct sillyrename *sp)
1888 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1889 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1894 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1897 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1898 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1899 int tnamelen, struct ucred *cred, struct thread *td)
1901 struct nfsvattr fnfsva, tnfsva;
1902 struct nfsnode *fdnp = VTONFS(fdvp);
1903 struct nfsnode *tdnp = VTONFS(tdvp);
1904 int error = 0, fattrflag, tattrflag;
1906 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1907 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1908 &tattrflag, NULL, NULL);
1909 mtx_lock(&fdnp->n_mtx);
1910 fdnp->n_flag |= NMODIFIED;
1911 if (fattrflag != 0) {
1912 mtx_unlock(&fdnp->n_mtx);
1913 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1915 fdnp->n_attrstamp = 0;
1916 mtx_unlock(&fdnp->n_mtx);
1917 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1919 mtx_lock(&tdnp->n_mtx);
1920 tdnp->n_flag |= NMODIFIED;
1921 if (tattrflag != 0) {
1922 mtx_unlock(&tdnp->n_mtx);
1923 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1925 tdnp->n_attrstamp = 0;
1926 mtx_unlock(&tdnp->n_mtx);
1927 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1929 if (error && NFS_ISV4(fdvp))
1930 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1935 * nfs hard link create call
1938 nfs_link(struct vop_link_args *ap)
1940 struct vnode *vp = ap->a_vp;
1941 struct vnode *tdvp = ap->a_tdvp;
1942 struct componentname *cnp = ap->a_cnp;
1943 struct nfsnode *np, *tdnp;
1944 struct nfsvattr nfsva, dnfsva;
1945 int error = 0, attrflag, dattrflag;
1948 * Push all writes to the server, so that the attribute cache
1949 * doesn't get "out of sync" with the server.
1950 * XXX There should be a better way!
1952 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1954 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1955 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1957 tdnp = VTONFS(tdvp);
1958 mtx_lock(&tdnp->n_mtx);
1959 tdnp->n_flag |= NMODIFIED;
1960 if (dattrflag != 0) {
1961 mtx_unlock(&tdnp->n_mtx);
1962 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1964 tdnp->n_attrstamp = 0;
1965 mtx_unlock(&tdnp->n_mtx);
1966 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1969 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1972 mtx_lock(&np->n_mtx);
1973 np->n_attrstamp = 0;
1974 mtx_unlock(&np->n_mtx);
1975 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1978 * If negative lookup caching is enabled, I might as well
1979 * add an entry for this node. Not necessary for correctness,
1980 * but if negative caching is enabled, then the system
1981 * must care about lookup caching hit rate, so...
1983 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
1984 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
1985 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
1987 if (error && NFS_ISV4(vp))
1988 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
1994 * nfs symbolic link create call
1997 nfs_symlink(struct vop_symlink_args *ap)
1999 struct vnode *dvp = ap->a_dvp;
2000 struct vattr *vap = ap->a_vap;
2001 struct componentname *cnp = ap->a_cnp;
2002 struct nfsvattr nfsva, dnfsva;
2004 struct nfsnode *np = NULL, *dnp;
2005 struct vnode *newvp = NULL;
2006 int error = 0, attrflag, dattrflag, ret;
2008 vap->va_type = VLNK;
2009 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2010 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2011 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2013 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2014 &np, NULL, LK_EXCLUSIVE);
2020 if (newvp != NULL) {
2022 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2024 } else if (!error) {
2026 * If we do not have an error and we could not extract the
2027 * newvp from the response due to the request being NFSv2, we
2028 * have to do a lookup in order to obtain a newvp to return.
2030 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2031 cnp->cn_cred, cnp->cn_thread, &np);
2039 error = nfscl_maperr(cnp->cn_thread, error,
2040 vap->va_uid, vap->va_gid);
2046 mtx_lock(&dnp->n_mtx);
2047 dnp->n_flag |= NMODIFIED;
2048 if (dattrflag != 0) {
2049 mtx_unlock(&dnp->n_mtx);
2050 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2052 dnp->n_attrstamp = 0;
2053 mtx_unlock(&dnp->n_mtx);
2054 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2057 * If negative lookup caching is enabled, I might as well
2058 * add an entry for this node. Not necessary for correctness,
2059 * but if negative caching is enabled, then the system
2060 * must care about lookup caching hit rate, so...
2062 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2063 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2064 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2073 nfs_mkdir(struct vop_mkdir_args *ap)
2075 struct vnode *dvp = ap->a_dvp;
2076 struct vattr *vap = ap->a_vap;
2077 struct componentname *cnp = ap->a_cnp;
2078 struct nfsnode *np = NULL, *dnp;
2079 struct vnode *newvp = NULL;
2082 struct nfsvattr nfsva, dnfsva;
2083 int error = 0, attrflag, dattrflag, ret;
2085 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2087 vap->va_type = VDIR;
2088 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2089 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2090 &attrflag, &dattrflag, NULL);
2092 mtx_lock(&dnp->n_mtx);
2093 dnp->n_flag |= NMODIFIED;
2094 if (dattrflag != 0) {
2095 mtx_unlock(&dnp->n_mtx);
2096 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2098 dnp->n_attrstamp = 0;
2099 mtx_unlock(&dnp->n_mtx);
2100 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2103 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2104 &np, NULL, LK_EXCLUSIVE);
2108 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2113 if (!error && newvp == NULL) {
2114 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2115 cnp->cn_cred, cnp->cn_thread, &np);
2118 if (newvp->v_type != VDIR)
2126 error = nfscl_maperr(cnp->cn_thread, error,
2127 vap->va_uid, vap->va_gid);
2130 * If negative lookup caching is enabled, I might as well
2131 * add an entry for this node. Not necessary for correctness,
2132 * but if negative caching is enabled, then the system
2133 * must care about lookup caching hit rate, so...
2135 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2136 (cnp->cn_flags & MAKEENTRY) &&
2137 attrflag != 0 && dattrflag != 0)
2138 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2146 * nfs remove directory call
2149 nfs_rmdir(struct vop_rmdir_args *ap)
2151 struct vnode *vp = ap->a_vp;
2152 struct vnode *dvp = ap->a_dvp;
2153 struct componentname *cnp = ap->a_cnp;
2154 struct nfsnode *dnp;
2155 struct nfsvattr dnfsva;
2156 int error, dattrflag;
2160 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2161 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2163 mtx_lock(&dnp->n_mtx);
2164 dnp->n_flag |= NMODIFIED;
2165 if (dattrflag != 0) {
2166 mtx_unlock(&dnp->n_mtx);
2167 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2169 dnp->n_attrstamp = 0;
2170 mtx_unlock(&dnp->n_mtx);
2171 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2176 if (error && NFS_ISV4(dvp))
2177 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2180 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2182 if (error == ENOENT)
2191 nfs_readdir(struct vop_readdir_args *ap)
2193 struct vnode *vp = ap->a_vp;
2194 struct nfsnode *np = VTONFS(vp);
2195 struct uio *uio = ap->a_uio;
2196 ssize_t tresid, left;
2200 if (ap->a_eofflag != NULL)
2202 if (vp->v_type != VDIR)
2206 * First, check for hit on the EOF offset cache
2208 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2209 (np->n_flag & NMODIFIED) == 0) {
2210 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2211 mtx_lock(&np->n_mtx);
2212 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2213 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2214 mtx_unlock(&np->n_mtx);
2215 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2216 if (ap->a_eofflag != NULL)
2220 mtx_unlock(&np->n_mtx);
2225 * NFS always guarantees that directory entries don't straddle
2226 * DIRBLKSIZ boundaries. As such, we need to limit the size
2227 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2230 left = uio->uio_resid % DIRBLKSIZ;
2231 if (left == uio->uio_resid)
2233 uio->uio_resid -= left;
2236 * Call ncl_bioread() to do the real work.
2238 tresid = uio->uio_resid;
2239 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2241 if (!error && uio->uio_resid == tresid) {
2242 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2243 if (ap->a_eofflag != NULL)
2247 /* Add the partial DIRBLKSIZ (left) back in. */
2248 uio->uio_resid += left;
2254 * Called from below the buffer cache by ncl_doio().
2257 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2260 struct nfsvattr nfsva;
2261 nfsuint64 *cookiep, cookie;
2262 struct nfsnode *dnp = VTONFS(vp);
2263 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2264 int error = 0, eof, attrflag;
2266 KASSERT(uiop->uio_iovcnt == 1 &&
2267 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2268 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2269 ("nfs readdirrpc bad uio"));
2272 * If there is no cookie, assume directory was stale.
2274 ncl_dircookie_lock(dnp);
2275 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2278 ncl_dircookie_unlock(dnp);
2280 ncl_dircookie_unlock(dnp);
2281 return (NFSERR_BAD_COOKIE);
2284 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2285 (void)ncl_fsinfo(nmp, vp, cred, td);
2287 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2288 &attrflag, &eof, NULL);
2290 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2294 * We are now either at the end of the directory or have filled
2298 dnp->n_direofoffset = uiop->uio_offset;
2300 if (uiop->uio_resid > 0)
2301 printf("EEK! readdirrpc resid > 0\n");
2302 ncl_dircookie_lock(dnp);
2303 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2305 ncl_dircookie_unlock(dnp);
2307 } else if (NFS_ISV4(vp)) {
2308 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2314 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2317 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2320 struct nfsvattr nfsva;
2321 nfsuint64 *cookiep, cookie;
2322 struct nfsnode *dnp = VTONFS(vp);
2323 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2324 int error = 0, attrflag, eof;
2326 KASSERT(uiop->uio_iovcnt == 1 &&
2327 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2328 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2329 ("nfs readdirplusrpc bad uio"));
2332 * If there is no cookie, assume directory was stale.
2334 ncl_dircookie_lock(dnp);
2335 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2338 ncl_dircookie_unlock(dnp);
2340 ncl_dircookie_unlock(dnp);
2341 return (NFSERR_BAD_COOKIE);
2344 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2345 (void)ncl_fsinfo(nmp, vp, cred, td);
2346 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2347 &attrflag, &eof, NULL);
2349 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2353 * We are now either at end of the directory or have filled the
2357 dnp->n_direofoffset = uiop->uio_offset;
2359 if (uiop->uio_resid > 0)
2360 printf("EEK! readdirplusrpc resid > 0\n");
2361 ncl_dircookie_lock(dnp);
2362 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2364 ncl_dircookie_unlock(dnp);
2366 } else if (NFS_ISV4(vp)) {
2367 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2373 * Silly rename. To make the NFS filesystem that is stateless look a little
2374 * more like the "ufs" a remove of an active vnode is translated to a rename
2375 * to a funny looking filename that is removed by nfs_inactive on the
2376 * nfsnode. There is the potential for another process on a different client
2377 * to create the same funny name between the nfs_lookitup() fails and the
2378 * nfs_rename() completes, but...
2381 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2383 struct sillyrename *sp;
2387 unsigned int lticks;
2391 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2392 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2393 M_NEWNFSREQ, M_WAITOK);
2394 sp->s_cred = crhold(cnp->cn_cred);
2399 * Fudge together a funny name.
2400 * Changing the format of the funny name to accommodate more
2401 * sillynames per directory.
2402 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2403 * CPU ticks since boot.
2405 pid = cnp->cn_thread->td_proc->p_pid;
2406 lticks = (unsigned int)ticks;
2408 sp->s_namlen = sprintf(sp->s_name,
2409 ".nfs.%08x.%04x4.4", lticks,
2411 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2412 cnp->cn_thread, NULL))
2416 error = nfs_renameit(dvp, vp, cnp, sp);
2419 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2420 cnp->cn_thread, &np);
2421 np->n_sillyrename = sp;
2426 free((caddr_t)sp, M_NEWNFSREQ);
2431 * Look up a file name and optionally either update the file handle or
2432 * allocate an nfsnode, depending on the value of npp.
2433 * npp == NULL --> just do the lookup
2434 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2436 * *npp != NULL --> update the file handle in the vnode
2439 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2440 struct thread *td, struct nfsnode **npp)
2442 struct vnode *newvp = NULL, *vp;
2443 struct nfsnode *np, *dnp = VTONFS(dvp);
2444 struct nfsfh *nfhp, *onfhp;
2445 struct nfsvattr nfsva, dnfsva;
2446 struct componentname cn;
2447 int error = 0, attrflag, dattrflag;
2450 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2451 &nfhp, &attrflag, &dattrflag, NULL);
2453 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2454 if (npp && !error) {
2459 * For NFSv4, check to see if it is the same name and
2460 * replace the name, if it is different.
2462 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2463 (np->n_v4->n4_namelen != len ||
2464 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2465 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2466 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2467 dnp->n_fhp->nfh_len))) {
2469 { char nnn[100]; int nnnl;
2470 nnnl = (len < 100) ? len : 99;
2471 bcopy(name, nnn, nnnl);
2473 printf("replace=%s\n",nnn);
2476 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2477 MALLOC(np->n_v4, struct nfsv4node *,
2478 sizeof (struct nfsv4node) +
2479 dnp->n_fhp->nfh_len + len - 1,
2480 M_NFSV4NODE, M_WAITOK);
2481 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2482 np->n_v4->n4_namelen = len;
2483 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2484 dnp->n_fhp->nfh_len);
2485 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2487 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2491 * Rehash node for new file handle.
2493 vfs_hash_rehash(vp, hash);
2496 FREE((caddr_t)onfhp, M_NFSFH);
2498 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2499 FREE((caddr_t)nfhp, M_NFSFH);
2503 cn.cn_nameptr = name;
2504 cn.cn_namelen = len;
2505 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2506 &np, NULL, LK_EXCLUSIVE);
2511 if (!attrflag && *npp == NULL) {
2519 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2522 if (npp && *npp == NULL) {
2533 if (error && NFS_ISV4(dvp))
2534 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2539 * Nfs Version 3 and 4 commit rpc
2542 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2545 struct nfsvattr nfsva;
2546 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2549 int error, attrflag;
2554 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2555 uio.uio_offset = offset;
2556 uio.uio_resid = cnt;
2557 error = nfscl_doiods(vp, &uio, NULL, NULL,
2558 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2560 mtx_lock(&np->n_mtx);
2561 np->n_flag &= ~NDSCOMMIT;
2562 mtx_unlock(&np->n_mtx);
2566 mtx_lock(&nmp->nm_mtx);
2567 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2568 mtx_unlock(&nmp->nm_mtx);
2571 mtx_unlock(&nmp->nm_mtx);
2572 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2576 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2578 if (error != 0 && NFS_ISV4(vp))
2579 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2585 * For async requests when nfsiod(s) are running, queue the request by
2586 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2590 nfs_strategy(struct vop_strategy_args *ap)
2598 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2599 KASSERT(!(bp->b_flags & B_DONE),
2600 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2601 BUF_ASSERT_HELD(bp);
2603 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2604 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2606 if (bp->b_iocmd == BIO_READ)
2612 * If the op is asynchronous and an i/o daemon is waiting
2613 * queue the request, wake it up and wait for completion
2614 * otherwise just do it ourselves.
2616 if ((bp->b_flags & B_ASYNC) == 0 ||
2617 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2618 (void) ncl_doio(vp, bp, cr, curthread, 1);
2623 * fsync vnode op. Just call ncl_flush() with commit == 1.
2627 nfs_fsync(struct vop_fsync_args *ap)
2630 if (ap->a_vp->v_type != VREG) {
2632 * For NFS, metadata is changed synchronously on the server,
2633 * so there is nothing to flush. Also, ncl_flush() clears
2634 * the NMODIFIED flag and that shouldn't be done here for
2639 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2643 * Flush all the blocks associated with a vnode.
2644 * Walk through the buffer pool and push any dirty pages
2645 * associated with the vnode.
2646 * If the called_from_renewthread argument is TRUE, it has been called
2647 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2648 * waiting for a buffer write to complete.
2651 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2652 int commit, int called_from_renewthread)
2654 struct nfsnode *np = VTONFS(vp);
2658 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2659 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2660 int passone = 1, trycnt = 0;
2661 u_quad_t off, endoff, toff;
2662 struct ucred* wcred = NULL;
2663 struct buf **bvec = NULL;
2665 #ifndef NFS_COMMITBVECSIZ
2666 #define NFS_COMMITBVECSIZ 20
2668 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2669 int bvecsize = 0, bveccount;
2671 if (called_from_renewthread != 0)
2673 if (nmp->nm_flag & NFSMNT_INT)
2679 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2680 * server, but has not been committed to stable storage on the server
2681 * yet. On the first pass, the byte range is worked out and the commit
2682 * rpc is done. On the second pass, ncl_writebp() is called to do the
2689 if (NFS_ISV34(vp) && commit) {
2690 if (bvec != NULL && bvec != bvec_on_stack)
2693 * Count up how many buffers waiting for a commit.
2697 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2698 if (!BUF_ISLOCKED(bp) &&
2699 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2700 == (B_DELWRI | B_NEEDCOMMIT))
2704 * Allocate space to remember the list of bufs to commit. It is
2705 * important to use M_NOWAIT here to avoid a race with nfs_write.
2706 * If we can't get memory (for whatever reason), we will end up
2707 * committing the buffers one-by-one in the loop below.
2709 if (bveccount > NFS_COMMITBVECSIZ) {
2711 * Release the vnode interlock to avoid a lock
2715 bvec = (struct buf **)
2716 malloc(bveccount * sizeof(struct buf *),
2720 bvec = bvec_on_stack;
2721 bvecsize = NFS_COMMITBVECSIZ;
2723 bvecsize = bveccount;
2725 bvec = bvec_on_stack;
2726 bvecsize = NFS_COMMITBVECSIZ;
2728 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2729 if (bvecpos >= bvecsize)
2731 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2732 nbp = TAILQ_NEXT(bp, b_bobufs);
2735 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2736 (B_DELWRI | B_NEEDCOMMIT)) {
2738 nbp = TAILQ_NEXT(bp, b_bobufs);
2744 * Work out if all buffers are using the same cred
2745 * so we can deal with them all with one commit.
2747 * NOTE: we are not clearing B_DONE here, so we have
2748 * to do it later on in this routine if we intend to
2749 * initiate I/O on the bp.
2751 * Note: to avoid loopback deadlocks, we do not
2752 * assign b_runningbufspace.
2755 wcred = bp->b_wcred;
2756 else if (wcred != bp->b_wcred)
2758 vfs_busy_pages(bp, 1);
2762 * bp is protected by being locked, but nbp is not
2763 * and vfs_busy_pages() may sleep. We have to
2766 nbp = TAILQ_NEXT(bp, b_bobufs);
2769 * A list of these buffers is kept so that the
2770 * second loop knows which buffers have actually
2771 * been committed. This is necessary, since there
2772 * may be a race between the commit rpc and new
2773 * uncommitted writes on the file.
2775 bvec[bvecpos++] = bp;
2776 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2780 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2788 * Commit data on the server, as required.
2789 * If all bufs are using the same wcred, then use that with
2790 * one call for all of them, otherwise commit each one
2793 if (wcred != NOCRED)
2794 retv = ncl_commit(vp, off, (int)(endoff - off),
2798 for (i = 0; i < bvecpos; i++) {
2801 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2803 size = (u_quad_t)(bp->b_dirtyend
2805 retv = ncl_commit(vp, off, (int)size,
2811 if (retv == NFSERR_STALEWRITEVERF)
2812 ncl_clearcommit(vp->v_mount);
2815 * Now, either mark the blocks I/O done or mark the
2816 * blocks dirty, depending on whether the commit
2819 for (i = 0; i < bvecpos; i++) {
2821 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2824 * Error, leave B_DELWRI intact
2826 vfs_unbusy_pages(bp);
2830 * Success, remove B_DELWRI ( bundirty() ).
2832 * b_dirtyoff/b_dirtyend seem to be NFS
2833 * specific. We should probably move that
2834 * into bundirty(). XXX
2837 bp->b_flags |= B_ASYNC;
2839 bp->b_flags &= ~B_DONE;
2840 bp->b_ioflags &= ~BIO_ERROR;
2841 bp->b_dirtyoff = bp->b_dirtyend = 0;
2848 * Start/do any write(s) that are required.
2852 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2853 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2854 if (waitfor != MNT_WAIT || passone)
2857 error = BUF_TIMELOCK(bp,
2858 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2859 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2864 if (error == ENOLCK) {
2868 if (called_from_renewthread != 0) {
2870 * Return EIO so the flush will be retried
2876 if (newnfs_sigintr(nmp, td)) {
2880 if (slpflag == PCATCH) {
2886 if ((bp->b_flags & B_DELWRI) == 0)
2887 panic("nfs_fsync: not dirty");
2888 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2894 if (passone || !commit)
2895 bp->b_flags |= B_ASYNC;
2897 bp->b_flags |= B_ASYNC;
2899 if (newnfs_sigintr(nmp, td)) {
2910 if (waitfor == MNT_WAIT) {
2911 while (bo->bo_numoutput) {
2912 error = bufobj_wwait(bo, slpflag, slptimeo);
2915 if (called_from_renewthread != 0) {
2917 * Return EIO so that the flush will be
2923 error = newnfs_sigintr(nmp, td);
2926 if (slpflag == PCATCH) {
2933 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2938 * Wait for all the async IO requests to drain
2941 mtx_lock(&np->n_mtx);
2942 while (np->n_directio_asyncwr > 0) {
2943 np->n_flag |= NFSYNCWAIT;
2944 error = newnfs_msleep(td, &np->n_directio_asyncwr,
2945 &np->n_mtx, slpflag | (PRIBIO + 1),
2948 if (newnfs_sigintr(nmp, td)) {
2949 mtx_unlock(&np->n_mtx);
2955 mtx_unlock(&np->n_mtx);
2958 if (NFSHASPNFS(nmp)) {
2959 nfscl_layoutcommit(vp, td);
2961 * Invalidate the attribute cache, since writes to a DS
2962 * won't update the size attribute.
2964 mtx_lock(&np->n_mtx);
2965 np->n_attrstamp = 0;
2967 mtx_lock(&np->n_mtx);
2968 if (np->n_flag & NWRITEERR) {
2969 error = np->n_error;
2970 np->n_flag &= ~NWRITEERR;
2972 if (commit && bo->bo_dirty.bv_cnt == 0 &&
2973 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2974 np->n_flag &= ~NMODIFIED;
2975 mtx_unlock(&np->n_mtx);
2977 if (bvec != NULL && bvec != bvec_on_stack)
2979 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2980 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2981 np->n_directio_asyncwr != 0)) {
2983 /* try, try again... */
2990 vn_printf(vp, "ncl_flush failed");
2991 error = called_from_renewthread != 0 ? EIO : EBUSY;
2997 * NFS advisory byte-level locks.
3000 nfs_advlock(struct vop_advlock_args *ap)
3002 struct vnode *vp = ap->a_vp;
3004 struct nfsnode *np = VTONFS(ap->a_vp);
3005 struct proc *p = (struct proc *)ap->a_id;
3006 struct thread *td = curthread; /* XXX */
3008 int ret, error = EOPNOTSUPP;
3011 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3012 if (vp->v_type != VREG)
3014 if ((ap->a_flags & F_POSIX) != 0)
3017 cred = td->td_ucred;
3018 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3019 if (vp->v_iflag & VI_DOOMED) {
3020 NFSVOPUNLOCK(vp, 0);
3025 * If this is unlocking a write locked region, flush and
3026 * commit them before unlocking. This is required by
3027 * RFC3530 Sec. 9.3.2.
3029 if (ap->a_op == F_UNLCK &&
3030 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3032 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3035 * Loop around doing the lock op, while a blocking lock
3036 * must wait for the lock op to succeed.
3039 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3040 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3041 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3042 ap->a_op == F_SETLK) {
3043 NFSVOPUNLOCK(vp, 0);
3044 error = nfs_catnap(PZERO | PCATCH, ret,
3048 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3049 if (vp->v_iflag & VI_DOOMED) {
3050 NFSVOPUNLOCK(vp, 0);
3054 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3055 ap->a_op == F_SETLK);
3056 if (ret == NFSERR_DENIED) {
3057 NFSVOPUNLOCK(vp, 0);
3059 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3060 NFSVOPUNLOCK(vp, 0);
3062 } else if (ret != 0) {
3063 NFSVOPUNLOCK(vp, 0);
3068 * Now, if we just got a lock, invalidate data in the buffer
3069 * cache, as required, so that the coherency conforms with
3070 * RFC3530 Sec. 9.3.2.
3072 if (ap->a_op == F_SETLK) {
3073 if ((np->n_flag & NMODIFIED) == 0) {
3074 np->n_attrstamp = 0;
3075 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3076 ret = VOP_GETATTR(vp, &va, cred);
3078 if ((np->n_flag & NMODIFIED) || ret ||
3079 np->n_change != va.va_filerev) {
3080 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3081 np->n_attrstamp = 0;
3082 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3083 ret = VOP_GETATTR(vp, &va, cred);
3085 np->n_mtime = va.va_mtime;
3086 np->n_change = va.va_filerev;
3089 /* Mark that a file lock has been acquired. */
3090 mtx_lock(&np->n_mtx);
3091 np->n_flag |= NHASBEENLOCKED;
3092 mtx_unlock(&np->n_mtx);
3094 NFSVOPUNLOCK(vp, 0);
3096 } else if (!NFS_ISV4(vp)) {
3097 error = NFSVOPLOCK(vp, LK_SHARED);
3100 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3101 size = VTONFS(vp)->n_size;
3102 NFSVOPUNLOCK(vp, 0);
3103 error = lf_advlock(ap, &(vp->v_lockf), size);
3105 if (nfs_advlock_p != NULL)
3106 error = nfs_advlock_p(ap);
3108 NFSVOPUNLOCK(vp, 0);
3112 if (error == 0 && ap->a_op == F_SETLK) {
3113 error = NFSVOPLOCK(vp, LK_SHARED);
3115 /* Mark that a file lock has been acquired. */
3116 mtx_lock(&np->n_mtx);
3117 np->n_flag |= NHASBEENLOCKED;
3118 mtx_unlock(&np->n_mtx);
3119 NFSVOPUNLOCK(vp, 0);
3127 * NFS advisory byte-level locks.
3130 nfs_advlockasync(struct vop_advlockasync_args *ap)
3132 struct vnode *vp = ap->a_vp;
3137 return (EOPNOTSUPP);
3138 error = NFSVOPLOCK(vp, LK_SHARED);
3141 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3142 size = VTONFS(vp)->n_size;
3143 NFSVOPUNLOCK(vp, 0);
3144 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3146 NFSVOPUNLOCK(vp, 0);
3153 * Print out the contents of an nfsnode.
3156 nfs_print(struct vop_print_args *ap)
3158 struct vnode *vp = ap->a_vp;
3159 struct nfsnode *np = VTONFS(vp);
3161 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3162 (uintmax_t)np->n_vattr.na_fsid);
3163 if (vp->v_type == VFIFO)
3170 * This is the "real" nfs::bwrite(struct buf*).
3171 * We set B_CACHE if this is a VMIO buffer.
3174 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3176 int oldflags, rtval;
3178 BUF_ASSERT_HELD(bp);
3180 if (bp->b_flags & B_INVAL) {
3185 oldflags = bp->b_flags;
3186 bp->b_flags |= B_CACHE;
3189 * Undirty the bp. We will redirty it later if the I/O fails.
3192 bp->b_flags &= ~B_DONE;
3193 bp->b_ioflags &= ~BIO_ERROR;
3194 bp->b_iocmd = BIO_WRITE;
3196 bufobj_wref(bp->b_bufobj);
3197 curthread->td_ru.ru_oublock++;
3200 * Note: to avoid loopback deadlocks, we do not
3201 * assign b_runningbufspace.
3203 vfs_busy_pages(bp, 1);
3206 bp->b_iooffset = dbtob(bp->b_blkno);
3209 if ((oldflags & B_ASYNC) != 0)
3212 rtval = bufwait(bp);
3213 if (oldflags & B_DELWRI)
3220 * nfs special file access vnode op.
3221 * Essentially just get vattr and then imitate iaccess() since the device is
3222 * local to the client.
3225 nfsspec_access(struct vop_access_args *ap)
3228 struct ucred *cred = ap->a_cred;
3229 struct vnode *vp = ap->a_vp;
3230 accmode_t accmode = ap->a_accmode;
3235 * Disallow write attempts on filesystems mounted read-only;
3236 * unless the file is a socket, fifo, or a block or character
3237 * device resident on the filesystem.
3239 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3240 switch (vp->v_type) {
3250 error = VOP_GETATTR(vp, vap, cred);
3253 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3254 accmode, cred, NULL);
3260 * Read wrapper for fifos.
3263 nfsfifo_read(struct vop_read_args *ap)
3265 struct nfsnode *np = VTONFS(ap->a_vp);
3271 mtx_lock(&np->n_mtx);
3273 vfs_timestamp(&np->n_atim);
3274 mtx_unlock(&np->n_mtx);
3275 error = fifo_specops.vop_read(ap);
3280 * Write wrapper for fifos.
3283 nfsfifo_write(struct vop_write_args *ap)
3285 struct nfsnode *np = VTONFS(ap->a_vp);
3290 mtx_lock(&np->n_mtx);
3292 vfs_timestamp(&np->n_mtim);
3293 mtx_unlock(&np->n_mtx);
3294 return(fifo_specops.vop_write(ap));
3298 * Close wrapper for fifos.
3300 * Update the times on the nfsnode then do fifo close.
3303 nfsfifo_close(struct vop_close_args *ap)
3305 struct vnode *vp = ap->a_vp;
3306 struct nfsnode *np = VTONFS(vp);
3310 mtx_lock(&np->n_mtx);
3311 if (np->n_flag & (NACC | NUPD)) {
3313 if (np->n_flag & NACC)
3315 if (np->n_flag & NUPD)
3318 if (vrefcnt(vp) == 1 &&
3319 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3321 if (np->n_flag & NACC)
3322 vattr.va_atime = np->n_atim;
3323 if (np->n_flag & NUPD)
3324 vattr.va_mtime = np->n_mtim;
3325 mtx_unlock(&np->n_mtx);
3326 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3330 mtx_unlock(&np->n_mtx);
3332 return (fifo_specops.vop_close(ap));
3336 * Just call ncl_writebp() with the force argument set to 1.
3338 * NOTE: B_DONE may or may not be set in a_bp on call.
3341 nfs_bwrite(struct buf *bp)
3344 return (ncl_writebp(bp, 1, curthread));
3347 struct buf_ops buf_ops_newnfs = {
3348 .bop_name = "buf_ops_nfs",
3349 .bop_write = nfs_bwrite,
3350 .bop_strategy = bufstrategy,
3351 .bop_sync = bufsync,
3352 .bop_bdflush = bufbdflush,
3356 nfs_getacl(struct vop_getacl_args *ap)
3360 if (ap->a_type != ACL_TYPE_NFS4)
3361 return (EOPNOTSUPP);
3362 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3364 if (error > NFSERR_STALE) {
3365 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3372 nfs_setacl(struct vop_setacl_args *ap)
3376 if (ap->a_type != ACL_TYPE_NFS4)
3377 return (EOPNOTSUPP);
3378 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3380 if (error > NFSERR_STALE) {
3381 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3388 nfs_set_text(struct vop_set_text_args *ap)
3390 struct vnode *vp = ap->a_vp;
3394 * If the text file has been mmap'd, flush any dirty pages to the
3395 * buffer cache and then...
3396 * Make sure all writes are pushed to the NFS server. If this is not
3397 * done, the modify time of the file can change while the text
3398 * file is being executed. This will cause the process that is
3399 * executing the text file to be terminated.
3401 if (vp->v_object != NULL) {
3402 VM_OBJECT_WLOCK(vp->v_object);
3403 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3404 VM_OBJECT_WUNLOCK(vp->v_object);
3407 /* Now, flush the buffer cache. */
3408 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
3410 /* And, finally, make sure that n_mtime is up to date. */
3412 mtx_lock(&np->n_mtx);
3413 np->n_mtime = np->n_vattr.na_mtime;
3414 mtx_unlock(&np->n_mtx);
3416 vp->v_vflag |= VV_TEXT;
3421 * Return POSIX pathconf information applicable to nfs filesystems.
3424 nfs_pathconf(struct vop_pathconf_args *ap)
3426 struct nfsv3_pathconf pc;
3427 struct nfsvattr nfsva;
3428 struct vnode *vp = ap->a_vp;
3429 struct thread *td = curthread;
3430 int attrflag, error;
3432 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3433 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3434 ap->a_name == _PC_NO_TRUNC)) ||
3435 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3437 * Since only the above 4 a_names are returned by the NFSv3
3438 * Pathconf RPC, there is no point in doing it for others.
3439 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3440 * be used for _PC_NFS4_ACL as well.
3442 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3445 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3451 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3454 pc.pc_linkmax = NFS_LINK_MAX;
3455 pc.pc_namemax = NFS_MAXNAMLEN;
3457 pc.pc_chownrestricted = 1;
3458 pc.pc_caseinsensitive = 0;
3459 pc.pc_casepreserving = 1;
3462 switch (ap->a_name) {
3465 *ap->a_retval = pc.pc_linkmax;
3467 *ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
3471 *ap->a_retval = pc.pc_namemax;
3474 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
3475 *ap->a_retval = PIPE_BUF;
3479 case _PC_CHOWN_RESTRICTED:
3480 *ap->a_retval = pc.pc_chownrestricted;
3483 *ap->a_retval = pc.pc_notrunc;
3485 case _PC_ACL_EXTENDED:
3489 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3490 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3495 case _PC_ACL_PATH_MAX:
3497 *ap->a_retval = ACL_MAX_ENTRIES;
3501 case _PC_MAC_PRESENT:
3510 case _PC_ALLOC_SIZE_MIN:
3511 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3513 case _PC_FILESIZEBITS:
3519 case _PC_REC_INCR_XFER_SIZE:
3520 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3522 case _PC_REC_MAX_XFER_SIZE:
3523 *ap->a_retval = -1; /* means ``unlimited'' */
3525 case _PC_REC_MIN_XFER_SIZE:
3526 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3528 case _PC_REC_XFER_ALIGN:
3529 *ap->a_retval = PAGE_SIZE;
3531 case _PC_SYMLINK_MAX:
3532 *ap->a_retval = NFS_MAXPATHLEN;
3536 error = vop_stdpathconf(ap);