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_print = nfs_print,
193 .vop_read = nfsfifo_read,
194 .vop_reclaim = ncl_reclaim,
195 .vop_setattr = nfs_setattr,
196 .vop_write = nfsfifo_write,
199 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
200 struct componentname *cnp, struct vattr *vap);
201 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
202 int namelen, struct ucred *cred, struct thread *td);
203 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
204 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
205 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
206 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
207 struct componentname *scnp, struct sillyrename *sp);
212 SYSCTL_DECL(_vfs_nfs);
214 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
215 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
216 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
218 static int nfs_prime_access_cache = 0;
219 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
220 &nfs_prime_access_cache, 0,
221 "Prime NFS ACCESS cache when fetching attributes");
223 static int newnfs_commit_on_close = 0;
224 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
225 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
227 static int nfs_clean_pages_on_close = 1;
228 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
229 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
231 int newnfs_directio_enable = 0;
232 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
233 &newnfs_directio_enable, 0, "Enable NFS directio");
235 int nfs_keep_dirty_on_error;
236 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
237 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
240 * This sysctl allows other processes to mmap a file that has been opened
241 * O_DIRECT by a process. In general, having processes mmap the file while
242 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
243 * this by default to prevent DoS attacks - to prevent a malicious user from
244 * opening up files O_DIRECT preventing other users from mmap'ing these
245 * files. "Protected" environments where stricter consistency guarantees are
246 * required can disable this knob. The process that opened the file O_DIRECT
247 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
250 int newnfs_directio_allow_mmap = 1;
251 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
252 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
254 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
255 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
256 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
260 * The list of locks after the description of the lock is the ordering
261 * of other locks acquired with the lock held.
262 * np->n_mtx : Protects the fields in the nfsnode.
264 VI_MTX (acquired indirectly)
265 * nmp->nm_mtx : Protects the fields in the nfsmount.
267 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
268 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
271 * rep->r_mtx : Protects the fields in an nfsreq.
275 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
276 struct ucred *cred, u_int32_t *retmode)
278 int error = 0, attrflag, i, lrupos;
280 struct nfsnode *np = VTONFS(vp);
281 struct nfsvattr nfsva;
283 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
286 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
289 mtx_lock(&np->n_mtx);
290 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
291 if (np->n_accesscache[i].uid == cred->cr_uid) {
292 np->n_accesscache[i].mode = rmode;
293 np->n_accesscache[i].stamp = time_second;
296 if (i > 0 && np->n_accesscache[i].stamp <
297 np->n_accesscache[lrupos].stamp)
300 if (i == NFS_ACCESSCACHESIZE) {
301 np->n_accesscache[lrupos].uid = cred->cr_uid;
302 np->n_accesscache[lrupos].mode = rmode;
303 np->n_accesscache[lrupos].stamp = time_second;
305 mtx_unlock(&np->n_mtx);
308 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
309 } else if (NFS_ISV4(vp)) {
310 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
314 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
321 * nfs access vnode op.
322 * For nfs version 2, just return ok. File accesses may fail later.
323 * For nfs version 3, use the access rpc to check accessibility. If file modes
324 * are changed on the server, accesses might still fail later.
327 nfs_access(struct vop_access_args *ap)
329 struct vnode *vp = ap->a_vp;
330 int error = 0, i, gotahit;
331 u_int32_t mode, wmode, rmode;
332 int v34 = NFS_ISV34(vp);
333 struct nfsnode *np = VTONFS(vp);
336 * Disallow write attempts on filesystems mounted read-only;
337 * unless the file is a socket, fifo, or a block or character
338 * device resident on the filesystem.
340 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
341 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
342 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
343 switch (vp->v_type) {
353 * For nfs v3 or v4, check to see if we have done this recently, and if
354 * so return our cached result instead of making an ACCESS call.
355 * If not, do an access rpc, otherwise you are stuck emulating
356 * ufs_access() locally using the vattr. This may not be correct,
357 * since the server may apply other access criteria such as
358 * client uid-->server uid mapping that we do not know about.
361 if (ap->a_accmode & VREAD)
362 mode = NFSACCESS_READ;
365 if (vp->v_type != VDIR) {
366 if (ap->a_accmode & VWRITE)
367 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
368 if (ap->a_accmode & VAPPEND)
369 mode |= NFSACCESS_EXTEND;
370 if (ap->a_accmode & VEXEC)
371 mode |= NFSACCESS_EXECUTE;
372 if (ap->a_accmode & VDELETE)
373 mode |= NFSACCESS_DELETE;
375 if (ap->a_accmode & VWRITE)
376 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
377 if (ap->a_accmode & VAPPEND)
378 mode |= NFSACCESS_EXTEND;
379 if (ap->a_accmode & VEXEC)
380 mode |= NFSACCESS_LOOKUP;
381 if (ap->a_accmode & VDELETE)
382 mode |= NFSACCESS_DELETE;
383 if (ap->a_accmode & VDELETE_CHILD)
384 mode |= NFSACCESS_MODIFY;
386 /* XXX safety belt, only make blanket request if caching */
387 if (nfsaccess_cache_timeout > 0) {
388 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
389 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
390 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
396 * Does our cached result allow us to give a definite yes to
400 mtx_lock(&np->n_mtx);
401 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
402 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
403 if (time_second < (np->n_accesscache[i].stamp
404 + nfsaccess_cache_timeout) &&
405 (np->n_accesscache[i].mode & mode) == mode) {
406 NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
412 mtx_unlock(&np->n_mtx);
415 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
416 ap->a_cred->cr_uid, mode);
418 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
419 ap->a_cred->cr_uid, mode);
423 * Either a no, or a don't know. Go to the wire.
425 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
426 error = nfs34_access_otw(vp, wmode, ap->a_td,
429 (rmode & mode) != mode)
434 if ((error = nfsspec_access(ap)) != 0) {
438 * Attempt to prevent a mapped root from accessing a file
439 * which it shouldn't. We try to read a byte from the file
440 * if the user is root and the file is not zero length.
441 * After calling nfsspec_access, we should have the correct
444 mtx_lock(&np->n_mtx);
445 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
446 && VTONFS(vp)->n_size > 0) {
451 mtx_unlock(&np->n_mtx);
454 auio.uio_iov = &aiov;
458 auio.uio_segflg = UIO_SYSSPACE;
459 auio.uio_rw = UIO_READ;
460 auio.uio_td = ap->a_td;
462 if (vp->v_type == VREG)
463 error = ncl_readrpc(vp, &auio, ap->a_cred);
464 else if (vp->v_type == VDIR) {
466 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
468 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
469 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
472 } else if (vp->v_type == VLNK)
473 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
477 mtx_unlock(&np->n_mtx);
485 * Check to see if the type is ok
486 * and that deletion is not in progress.
487 * For paged in text files, you will need to flush the page cache
488 * if consistency is lost.
492 nfs_open(struct vop_open_args *ap)
494 struct vnode *vp = ap->a_vp;
495 struct nfsnode *np = VTONFS(vp);
498 int fmode = ap->a_mode;
501 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
505 * For NFSv4, we need to do the Open Op before cache validation,
506 * so that we conform to RFC3530 Sec. 9.3.1.
509 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
511 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
518 * Now, if this Open will be doing reading, re-validate/flush the
519 * cache, so that Close/Open coherency is maintained.
521 mtx_lock(&np->n_mtx);
522 if (np->n_flag & NMODIFIED) {
523 mtx_unlock(&np->n_mtx);
524 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
525 if (error == EINTR || error == EIO) {
527 (void) nfsrpc_close(vp, 0, ap->a_td);
530 mtx_lock(&np->n_mtx);
532 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
533 if (vp->v_type == VDIR)
534 np->n_direofoffset = 0;
535 mtx_unlock(&np->n_mtx);
536 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
539 (void) nfsrpc_close(vp, 0, ap->a_td);
542 mtx_lock(&np->n_mtx);
543 np->n_mtime = vattr.va_mtime;
545 np->n_change = vattr.va_filerev;
547 mtx_unlock(&np->n_mtx);
548 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
551 (void) nfsrpc_close(vp, 0, ap->a_td);
554 mtx_lock(&np->n_mtx);
555 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
556 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
557 if (vp->v_type == VDIR)
558 np->n_direofoffset = 0;
559 mtx_unlock(&np->n_mtx);
560 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
561 if (error == EINTR || error == EIO) {
563 (void) nfsrpc_close(vp, 0, ap->a_td);
566 mtx_lock(&np->n_mtx);
567 np->n_mtime = vattr.va_mtime;
569 np->n_change = vattr.va_filerev;
574 * If the object has >= 1 O_DIRECT active opens, we disable caching.
576 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
577 (vp->v_type == VREG)) {
578 if (np->n_directio_opens == 0) {
579 mtx_unlock(&np->n_mtx);
580 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
583 (void) nfsrpc_close(vp, 0, ap->a_td);
586 mtx_lock(&np->n_mtx);
587 np->n_flag |= NNONCACHE;
589 np->n_directio_opens++;
592 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
593 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
594 np->n_flag |= NWRITEOPENED;
597 * If this is an open for writing, capture a reference to the
598 * credentials, so they can be used by ncl_putpages(). Using
599 * these write credentials is preferable to the credentials of
600 * whatever thread happens to be doing the VOP_PUTPAGES() since
601 * the write RPCs are less likely to fail with EACCES.
603 if ((fmode & FWRITE) != 0) {
604 cred = np->n_writecred;
605 np->n_writecred = crhold(ap->a_cred);
608 mtx_unlock(&np->n_mtx);
612 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
618 * What an NFS client should do upon close after writing is a debatable issue.
619 * Most NFS clients push delayed writes to the server upon close, basically for
621 * 1 - So that any write errors may be reported back to the client process
622 * doing the close system call. By far the two most likely errors are
623 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
624 * 2 - To put a worst case upper bound on cache inconsistency between
625 * multiple clients for the file.
626 * There is also a consistency problem for Version 2 of the protocol w.r.t.
627 * not being able to tell if other clients are writing a file concurrently,
628 * since there is no way of knowing if the changed modify time in the reply
629 * is only due to the write for this client.
630 * (NFS Version 3 provides weak cache consistency data in the reply that
631 * should be sufficient to detect and handle this case.)
633 * The current code does the following:
634 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
635 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
636 * or commit them (this satisfies 1 and 2 except for the
637 * case where the server crashes after this close but
638 * before the commit RPC, which is felt to be "good
639 * enough". Changing the last argument to ncl_flush() to
640 * a 1 would force a commit operation, if it is felt a
641 * commit is necessary now.
642 * for NFS Version 4 - flush the dirty buffers and commit them, if
643 * nfscl_mustflush() says this is necessary.
644 * It is necessary if there is no write delegation held,
645 * in order to satisfy open/close coherency.
646 * If the file isn't cached on local stable storage,
647 * it may be necessary in order to detect "out of space"
648 * errors from the server, if the write delegation
649 * issued by the server doesn't allow the file to grow.
653 nfs_close(struct vop_close_args *ap)
655 struct vnode *vp = ap->a_vp;
656 struct nfsnode *np = VTONFS(vp);
657 struct nfsvattr nfsva;
659 int error = 0, ret, localcred = 0;
660 int fmode = ap->a_fflag;
662 if (NFSCL_FORCEDISM(vp->v_mount))
665 * During shutdown, a_cred isn't valid, so just use root.
667 if (ap->a_cred == NOCRED) {
668 cred = newnfs_getcred();
673 if (vp->v_type == VREG) {
675 * Examine and clean dirty pages, regardless of NMODIFIED.
676 * This closes a major hole in close-to-open consistency.
677 * We want to push out all dirty pages (and buffers) on
678 * close, regardless of whether they were dirtied by
679 * mmap'ed writes or via write().
681 if (nfs_clean_pages_on_close && vp->v_object) {
682 VM_OBJECT_WLOCK(vp->v_object);
683 vm_object_page_clean(vp->v_object, 0, 0, 0);
684 VM_OBJECT_WUNLOCK(vp->v_object);
686 mtx_lock(&np->n_mtx);
687 if (np->n_flag & NMODIFIED) {
688 mtx_unlock(&np->n_mtx);
691 * Under NFSv3 we have dirty buffers to dispose of. We
692 * must flush them to the NFS server. We have the option
693 * of waiting all the way through the commit rpc or just
694 * waiting for the initial write. The default is to only
695 * wait through the initial write so the data is in the
696 * server's cache, which is roughly similar to the state
697 * a standard disk subsystem leaves the file in on close().
699 * We cannot clear the NMODIFIED bit in np->n_flag due to
700 * potential races with other processes, and certainly
701 * cannot clear it if we don't commit.
702 * These races occur when there is no longer the old
703 * traditional vnode locking implemented for Vnode Ops.
705 int cm = newnfs_commit_on_close ? 1 : 0;
706 error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
707 /* np->n_flag &= ~NMODIFIED; */
708 } else if (NFS_ISV4(vp)) {
709 if (nfscl_mustflush(vp) != 0) {
710 int cm = newnfs_commit_on_close ? 1 : 0;
711 error = ncl_flush(vp, MNT_WAIT, ap->a_td,
714 * as above w.r.t races when clearing
716 * np->n_flag &= ~NMODIFIED;
720 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
722 mtx_lock(&np->n_mtx);
725 * Invalidate the attribute cache in all cases.
726 * An open is going to fetch fresh attrs any way, other procs
727 * on this node that have file open will be forced to do an
728 * otw attr fetch, but this is safe.
729 * --> A user found that their RPC count dropped by 20% when
730 * this was commented out and I can't see any requirement
731 * for it, so I've disabled it when negative lookups are
732 * enabled. (What does this have to do with negative lookup
733 * caching? Well nothing, except it was reported by the
734 * same user that needed negative lookup caching and I wanted
735 * there to be a way to disable it to see if it
736 * is the cause of some caching/coherency issue that might
739 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
741 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
743 if (np->n_flag & NWRITEERR) {
744 np->n_flag &= ~NWRITEERR;
747 mtx_unlock(&np->n_mtx);
752 * Get attributes so "change" is up to date.
754 if (error == 0 && nfscl_mustflush(vp) != 0 &&
755 vp->v_type == VREG &&
756 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
757 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
760 np->n_change = nfsva.na_filerev;
761 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
769 ret = nfsrpc_close(vp, 0, ap->a_td);
773 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
776 if (newnfs_directio_enable)
777 KASSERT((np->n_directio_asyncwr == 0),
778 ("nfs_close: dirty unflushed (%d) directio buffers\n",
779 np->n_directio_asyncwr));
780 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
781 mtx_lock(&np->n_mtx);
782 KASSERT((np->n_directio_opens > 0),
783 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
784 np->n_directio_opens--;
785 if (np->n_directio_opens == 0)
786 np->n_flag &= ~NNONCACHE;
787 mtx_unlock(&np->n_mtx);
795 * nfs getattr call from vfs.
798 nfs_getattr(struct vop_getattr_args *ap)
800 struct vnode *vp = ap->a_vp;
801 struct thread *td = curthread; /* XXX */
802 struct nfsnode *np = VTONFS(vp);
804 struct nfsvattr nfsva;
805 struct vattr *vap = ap->a_vap;
809 * Update local times for special files.
811 mtx_lock(&np->n_mtx);
812 if (np->n_flag & (NACC | NUPD))
814 mtx_unlock(&np->n_mtx);
816 * First look in the cache.
818 if (ncl_getattrcache(vp, &vattr) == 0) {
819 vap->va_type = vattr.va_type;
820 vap->va_mode = vattr.va_mode;
821 vap->va_nlink = vattr.va_nlink;
822 vap->va_uid = vattr.va_uid;
823 vap->va_gid = vattr.va_gid;
824 vap->va_fsid = vattr.va_fsid;
825 vap->va_fileid = vattr.va_fileid;
826 vap->va_size = vattr.va_size;
827 vap->va_blocksize = vattr.va_blocksize;
828 vap->va_atime = vattr.va_atime;
829 vap->va_mtime = vattr.va_mtime;
830 vap->va_ctime = vattr.va_ctime;
831 vap->va_gen = vattr.va_gen;
832 vap->va_flags = vattr.va_flags;
833 vap->va_rdev = vattr.va_rdev;
834 vap->va_bytes = vattr.va_bytes;
835 vap->va_filerev = vattr.va_filerev;
837 * Get the local modify time for the case of a write
840 nfscl_deleggetmodtime(vp, &vap->va_mtime);
844 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
845 nfsaccess_cache_timeout > 0) {
846 NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
847 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
848 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
849 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
853 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
855 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
858 * Get the local modify time for the case of a write
861 nfscl_deleggetmodtime(vp, &vap->va_mtime);
862 } else if (NFS_ISV4(vp)) {
863 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
872 nfs_setattr(struct vop_setattr_args *ap)
874 struct vnode *vp = ap->a_vp;
875 struct nfsnode *np = VTONFS(vp);
876 struct thread *td = curthread; /* XXX */
877 struct vattr *vap = ap->a_vap;
886 * Setting of flags and marking of atimes are not supported.
888 if (vap->va_flags != VNOVAL)
892 * Disallow write attempts if the filesystem is mounted read-only.
894 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
895 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
896 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
897 (vp->v_mount->mnt_flag & MNT_RDONLY))
899 if (vap->va_size != VNOVAL) {
900 switch (vp->v_type) {
907 if (vap->va_mtime.tv_sec == VNOVAL &&
908 vap->va_atime.tv_sec == VNOVAL &&
909 vap->va_mode == (mode_t)VNOVAL &&
910 vap->va_uid == (uid_t)VNOVAL &&
911 vap->va_gid == (gid_t)VNOVAL)
913 vap->va_size = VNOVAL;
917 * Disallow write attempts if the filesystem is
920 if (vp->v_mount->mnt_flag & MNT_RDONLY)
923 * We run vnode_pager_setsize() early (why?),
924 * we must set np->n_size now to avoid vinvalbuf
925 * V_SAVE races that might setsize a lower
928 mtx_lock(&np->n_mtx);
930 mtx_unlock(&np->n_mtx);
931 error = ncl_meta_setsize(vp, ap->a_cred, td,
933 mtx_lock(&np->n_mtx);
934 if (np->n_flag & NMODIFIED) {
936 mtx_unlock(&np->n_mtx);
937 error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
940 vnode_pager_setsize(vp, tsize);
944 * Call nfscl_delegmodtime() to set the modify time
945 * locally, as required.
947 nfscl_delegmodtime(vp);
949 mtx_unlock(&np->n_mtx);
951 * np->n_size has already been set to vap->va_size
952 * in ncl_meta_setsize(). We must set it again since
953 * nfs_loadattrcache() could be called through
954 * ncl_meta_setsize() and could modify np->n_size.
956 mtx_lock(&np->n_mtx);
957 np->n_vattr.na_size = np->n_size = vap->va_size;
958 mtx_unlock(&np->n_mtx);
961 mtx_lock(&np->n_mtx);
962 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
963 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
964 mtx_unlock(&np->n_mtx);
965 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
966 if (error == EINTR || error == EIO)
969 mtx_unlock(&np->n_mtx);
971 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
972 if (error && vap->va_size != VNOVAL) {
973 mtx_lock(&np->n_mtx);
974 np->n_size = np->n_vattr.na_size = tsize;
975 vnode_pager_setsize(vp, tsize);
976 mtx_unlock(&np->n_mtx);
982 * Do an nfs setattr rpc.
985 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
988 struct nfsnode *np = VTONFS(vp);
989 int error, ret, attrflag, i;
990 struct nfsvattr nfsva;
993 mtx_lock(&np->n_mtx);
994 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
995 np->n_accesscache[i].stamp = 0;
996 np->n_flag |= NDELEGMOD;
997 mtx_unlock(&np->n_mtx);
998 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1000 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1003 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1007 if (error && NFS_ISV4(vp))
1008 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1013 * nfs lookup call, one step at a time...
1014 * First look in cache
1015 * If not found, unlock the directory nfsnode and do the rpc
1018 nfs_lookup(struct vop_lookup_args *ap)
1020 struct componentname *cnp = ap->a_cnp;
1021 struct vnode *dvp = ap->a_dvp;
1022 struct vnode **vpp = ap->a_vpp;
1023 struct mount *mp = dvp->v_mount;
1024 int flags = cnp->cn_flags;
1025 struct vnode *newvp;
1026 struct nfsmount *nmp;
1027 struct nfsnode *np, *newnp;
1028 int error = 0, attrflag, dattrflag, ltype, ncticks;
1029 struct thread *td = cnp->cn_thread;
1031 struct nfsvattr dnfsva, nfsva;
1033 struct timespec nctime;
1036 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1037 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1039 if (dvp->v_type != VDIR)
1044 /* For NFSv4, wait until any remove is done. */
1045 mtx_lock(&np->n_mtx);
1046 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1047 np->n_flag |= NREMOVEWANT;
1048 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1050 mtx_unlock(&np->n_mtx);
1052 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1054 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1055 if (error > 0 && error != ENOENT)
1059 * Lookups of "." are special and always return the
1060 * current directory. cache_lookup() already handles
1061 * associated locking bookkeeping, etc.
1063 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1064 /* XXX: Is this really correct? */
1065 if (cnp->cn_nameiop != LOOKUP &&
1067 cnp->cn_flags |= SAVENAME;
1072 * We only accept a positive hit in the cache if the
1073 * change time of the file matches our cached copy.
1074 * Otherwise, we discard the cache entry and fallback
1075 * to doing a lookup RPC. We also only trust cache
1076 * entries for less than nm_nametimeo seconds.
1078 * To better handle stale file handles and attributes,
1079 * clear the attribute cache of this node if it is a
1080 * leaf component, part of an open() call, and not
1081 * locally modified before fetching the attributes.
1082 * This should allow stale file handles to be detected
1083 * here where we can fall back to a LOOKUP RPC to
1084 * recover rather than having nfs_open() detect the
1085 * stale file handle and failing open(2) with ESTALE.
1088 newnp = VTONFS(newvp);
1089 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1090 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1091 !(newnp->n_flag & NMODIFIED)) {
1092 mtx_lock(&newnp->n_mtx);
1093 newnp->n_attrstamp = 0;
1094 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1095 mtx_unlock(&newnp->n_mtx);
1097 if (nfscl_nodeleg(newvp, 0) == 0 ||
1098 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1099 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1100 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1101 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1102 if (cnp->cn_nameiop != LOOKUP &&
1104 cnp->cn_flags |= SAVENAME;
1113 } else if (error == ENOENT) {
1114 if (dvp->v_iflag & VI_DOOMED)
1117 * We only accept a negative hit in the cache if the
1118 * modification time of the parent directory matches
1119 * the cached copy in the name cache entry.
1120 * Otherwise, we discard all of the negative cache
1121 * entries for this directory. We also only trust
1122 * negative cache entries for up to nm_negnametimeo
1125 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1126 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1127 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1128 NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1131 cache_purge_negative(dvp);
1136 NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1137 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1138 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1141 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1143 if (newvp != NULLVP) {
1148 if (error != ENOENT) {
1150 error = nfscl_maperr(td, error, (uid_t)0,
1155 /* The requested file was not found. */
1156 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1157 (flags & ISLASTCN)) {
1159 * XXX: UFS does a full VOP_ACCESS(dvp,
1160 * VWRITE) here instead of just checking
1163 if (mp->mnt_flag & MNT_RDONLY)
1165 cnp->cn_flags |= SAVENAME;
1166 return (EJUSTRETURN);
1169 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1171 * Cache the modification time of the parent
1172 * directory from the post-op attributes in
1173 * the name cache entry. The negative cache
1174 * entry will be ignored once the directory
1175 * has changed. Don't bother adding the entry
1176 * if the directory has already changed.
1178 mtx_lock(&np->n_mtx);
1179 if (timespeccmp(&np->n_vattr.na_mtime,
1180 &dnfsva.na_mtime, ==)) {
1181 mtx_unlock(&np->n_mtx);
1182 cache_enter_time(dvp, NULL, cnp,
1183 &dnfsva.na_mtime, NULL);
1185 mtx_unlock(&np->n_mtx);
1191 * Handle RENAME case...
1193 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1194 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1195 FREE((caddr_t)nfhp, M_NFSFH);
1198 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1204 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1207 cnp->cn_flags |= SAVENAME;
1211 if (flags & ISDOTDOT) {
1212 ltype = NFSVOPISLOCKED(dvp);
1213 error = vfs_busy(mp, MBF_NOWAIT);
1216 NFSVOPUNLOCK(dvp, 0);
1217 error = vfs_busy(mp, 0);
1218 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1220 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1227 NFSVOPUNLOCK(dvp, 0);
1228 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1234 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1235 if (dvp->v_iflag & VI_DOOMED) {
1247 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1249 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1250 FREE((caddr_t)nfhp, M_NFSFH);
1254 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1257 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1263 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1265 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1266 !(np->n_flag & NMODIFIED)) {
1268 * Flush the attribute cache when opening a
1269 * leaf node to ensure that fresh attributes
1270 * are fetched in nfs_open() since we did not
1271 * fetch attributes from the LOOKUP reply.
1273 mtx_lock(&np->n_mtx);
1274 np->n_attrstamp = 0;
1275 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1276 mtx_unlock(&np->n_mtx);
1279 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1280 cnp->cn_flags |= SAVENAME;
1281 if ((cnp->cn_flags & MAKEENTRY) &&
1282 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1283 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1284 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1285 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1292 * Just call ncl_bioread() to do the work.
1295 nfs_read(struct vop_read_args *ap)
1297 struct vnode *vp = ap->a_vp;
1299 switch (vp->v_type) {
1301 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1305 return (EOPNOTSUPP);
1313 nfs_readlink(struct vop_readlink_args *ap)
1315 struct vnode *vp = ap->a_vp;
1317 if (vp->v_type != VLNK)
1319 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1323 * Do a readlink rpc.
1324 * Called by ncl_doio() from below the buffer cache.
1327 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1329 int error, ret, attrflag;
1330 struct nfsvattr nfsva;
1332 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1335 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1339 if (error && NFS_ISV4(vp))
1340 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1349 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1351 int error, ret, attrflag;
1352 struct nfsvattr nfsva;
1353 struct nfsmount *nmp;
1355 nmp = VFSTONFS(vnode_mount(vp));
1358 if (NFSHASPNFS(nmp))
1359 error = nfscl_doiods(vp, uiop, NULL, NULL,
1360 NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1361 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1363 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1366 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1370 if (error && NFS_ISV4(vp))
1371 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1379 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1380 int *iomode, int *must_commit, int called_from_strategy)
1382 struct nfsvattr nfsva;
1383 int error, attrflag, ret;
1384 struct nfsmount *nmp;
1386 nmp = VFSTONFS(vnode_mount(vp));
1389 if (NFSHASPNFS(nmp))
1390 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1391 NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1392 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1394 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1395 uiop->uio_td, &nfsva, &attrflag, NULL,
1396 called_from_strategy);
1398 if (VTONFS(vp)->n_flag & ND_NFSV4)
1399 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1402 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1408 *iomode = NFSWRITE_FILESYNC;
1409 if (error && NFS_ISV4(vp))
1410 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1416 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1417 * mode set to specify the file type and the size field for rdev.
1420 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1423 struct nfsvattr nfsva, dnfsva;
1424 struct vnode *newvp = NULL;
1425 struct nfsnode *np = NULL, *dnp;
1428 int error = 0, attrflag, dattrflag;
1431 if (vap->va_type == VCHR || vap->va_type == VBLK)
1432 rdev = vap->va_rdev;
1433 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1436 return (EOPNOTSUPP);
1437 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1439 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1440 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1441 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1444 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1445 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1446 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1449 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1450 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1453 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1456 if (attrflag != 0) {
1457 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1465 } else if (NFS_ISV4(dvp)) {
1466 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1470 mtx_lock(&dnp->n_mtx);
1471 dnp->n_flag |= NMODIFIED;
1473 dnp->n_attrstamp = 0;
1474 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1476 mtx_unlock(&dnp->n_mtx);
1482 * just call nfs_mknodrpc() to do the work.
1486 nfs_mknod(struct vop_mknod_args *ap)
1488 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1491 static struct mtx nfs_cverf_mtx;
1492 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1498 static nfsquad_t cverf;
1500 static int cverf_initialized = 0;
1502 mtx_lock(&nfs_cverf_mtx);
1503 if (cverf_initialized == 0) {
1504 cverf.lval[0] = arc4random();
1505 cverf.lval[1] = arc4random();
1506 cverf_initialized = 1;
1510 mtx_unlock(&nfs_cverf_mtx);
1516 * nfs file create call
1519 nfs_create(struct vop_create_args *ap)
1521 struct vnode *dvp = ap->a_dvp;
1522 struct vattr *vap = ap->a_vap;
1523 struct componentname *cnp = ap->a_cnp;
1524 struct nfsnode *np = NULL, *dnp;
1525 struct vnode *newvp = NULL;
1526 struct nfsmount *nmp;
1527 struct nfsvattr dnfsva, nfsva;
1530 int error = 0, attrflag, dattrflag, fmode = 0;
1534 * Oops, not for me..
1536 if (vap->va_type == VSOCK)
1537 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1539 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1541 if (vap->va_vaflags & VA_EXCLUSIVE)
1544 nmp = VFSTONFS(vnode_mount(dvp));
1546 /* For NFSv4, wait until any remove is done. */
1547 mtx_lock(&dnp->n_mtx);
1548 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1549 dnp->n_flag |= NREMOVEWANT;
1550 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1552 mtx_unlock(&dnp->n_mtx);
1554 cverf = nfs_get_cverf();
1555 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1556 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1557 &nfhp, &attrflag, &dattrflag, NULL);
1560 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1561 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1562 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1565 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1566 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1569 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1573 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1574 cnp->cn_thread, &nfsva, NULL);
1576 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1580 if (newvp != NULL) {
1584 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1585 error == NFSERR_NOTSUPP) {
1589 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1590 if (nfscl_checksattr(vap, &nfsva)) {
1591 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1592 cnp->cn_thread, &nfsva, &attrflag, NULL);
1593 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1594 vap->va_gid != (gid_t)VNOVAL)) {
1595 /* try again without setting uid/gid */
1596 vap->va_uid = (uid_t)VNOVAL;
1597 vap->va_gid = (uid_t)VNOVAL;
1598 error = nfsrpc_setattr(newvp, vap, NULL,
1599 cnp->cn_cred, cnp->cn_thread, &nfsva,
1603 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1610 if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1611 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1614 } else if (NFS_ISV4(dvp)) {
1615 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1618 mtx_lock(&dnp->n_mtx);
1619 dnp->n_flag |= NMODIFIED;
1621 dnp->n_attrstamp = 0;
1622 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1624 mtx_unlock(&dnp->n_mtx);
1629 * nfs file remove call
1630 * To try and make nfs semantics closer to ufs semantics, a file that has
1631 * other processes using the vnode is renamed instead of removed and then
1632 * removed later on the last close.
1633 * - If v_usecount > 1
1634 * If a rename is not already in the works
1635 * call nfs_sillyrename() to set it up
1640 nfs_remove(struct vop_remove_args *ap)
1642 struct vnode *vp = ap->a_vp;
1643 struct vnode *dvp = ap->a_dvp;
1644 struct componentname *cnp = ap->a_cnp;
1645 struct nfsnode *np = VTONFS(vp);
1649 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1650 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1651 if (vp->v_type == VDIR)
1653 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1654 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1655 vattr.va_nlink > 1)) {
1657 * Purge the name cache so that the chance of a lookup for
1658 * the name succeeding while the remove is in progress is
1659 * minimized. Without node locking it can still happen, such
1660 * that an I/O op returns ESTALE, but since you get this if
1661 * another host removes the file..
1665 * throw away biocache buffers, mainly to avoid
1666 * unnecessary delayed writes later.
1668 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1669 if (error != EINTR && error != EIO)
1671 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1672 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1674 * Kludge City: If the first reply to the remove rpc is lost..
1675 * the reply to the retransmitted request will be ENOENT
1676 * since the file was in fact removed
1677 * Therefore, we cheat and return success.
1679 if (error == ENOENT)
1681 } else if (!np->n_sillyrename)
1682 error = nfs_sillyrename(dvp, vp, cnp);
1683 mtx_lock(&np->n_mtx);
1684 np->n_attrstamp = 0;
1685 mtx_unlock(&np->n_mtx);
1686 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1691 * nfs file remove rpc called from nfs_inactive
1694 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1697 * Make sure that the directory vnode is still valid.
1698 * XXX we should lock sp->s_dvp here.
1700 if (sp->s_dvp->v_type == VBAD)
1702 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1707 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1710 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1711 int namelen, struct ucred *cred, struct thread *td)
1713 struct nfsvattr dnfsva;
1714 struct nfsnode *dnp = VTONFS(dvp);
1715 int error = 0, dattrflag;
1717 mtx_lock(&dnp->n_mtx);
1718 dnp->n_flag |= NREMOVEINPROG;
1719 mtx_unlock(&dnp->n_mtx);
1720 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1722 mtx_lock(&dnp->n_mtx);
1723 if ((dnp->n_flag & NREMOVEWANT)) {
1724 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1725 mtx_unlock(&dnp->n_mtx);
1726 wakeup((caddr_t)dnp);
1728 dnp->n_flag &= ~NREMOVEINPROG;
1729 mtx_unlock(&dnp->n_mtx);
1732 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1733 mtx_lock(&dnp->n_mtx);
1734 dnp->n_flag |= NMODIFIED;
1736 dnp->n_attrstamp = 0;
1737 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1739 mtx_unlock(&dnp->n_mtx);
1740 if (error && NFS_ISV4(dvp))
1741 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1746 * nfs file rename call
1749 nfs_rename(struct vop_rename_args *ap)
1751 struct vnode *fvp = ap->a_fvp;
1752 struct vnode *tvp = ap->a_tvp;
1753 struct vnode *fdvp = ap->a_fdvp;
1754 struct vnode *tdvp = ap->a_tdvp;
1755 struct componentname *tcnp = ap->a_tcnp;
1756 struct componentname *fcnp = ap->a_fcnp;
1757 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1758 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1759 struct nfsv4node *newv4 = NULL;
1762 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1763 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1764 /* Check for cross-device rename */
1765 if ((fvp->v_mount != tdvp->v_mount) ||
1766 (tvp && (fvp->v_mount != tvp->v_mount))) {
1772 printf("nfs_rename: fvp == tvp (can't happen)\n");
1776 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1780 * We have to flush B_DELWRI data prior to renaming
1781 * the file. If we don't, the delayed-write buffers
1782 * can be flushed out later after the file has gone stale
1783 * under NFSV3. NFSV2 does not have this problem because
1784 * ( as far as I can tell ) it flushes dirty buffers more
1787 * Skip the rename operation if the fsync fails, this can happen
1788 * due to the server's volume being full, when we pushed out data
1789 * that was written back to our cache earlier. Not checking for
1790 * this condition can result in potential (silent) data loss.
1792 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1793 NFSVOPUNLOCK(fvp, 0);
1795 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1800 * If the tvp exists and is in use, sillyrename it before doing the
1801 * rename of the new file over it.
1802 * XXX Can't sillyrename a directory.
1804 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1805 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1810 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1811 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1814 if (error == 0 && NFS_ISV4(tdvp)) {
1816 * For NFSv4, check to see if it is the same name and
1817 * replace the name, if it is different.
1819 MALLOC(newv4, struct nfsv4node *,
1820 sizeof (struct nfsv4node) +
1821 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1822 M_NFSV4NODE, M_WAITOK);
1823 mtx_lock(&tdnp->n_mtx);
1824 mtx_lock(&fnp->n_mtx);
1825 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1826 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1827 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1828 tcnp->cn_namelen) ||
1829 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1830 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1831 tdnp->n_fhp->nfh_len))) {
1833 { char nnn[100]; int nnnl;
1834 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1835 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1837 printf("ren replace=%s\n",nnn);
1840 FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1843 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1844 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1845 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1846 tdnp->n_fhp->nfh_len);
1847 NFSBCOPY(tcnp->cn_nameptr,
1848 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1850 mtx_unlock(&tdnp->n_mtx);
1851 mtx_unlock(&fnp->n_mtx);
1853 FREE((caddr_t)newv4, M_NFSV4NODE);
1856 if (fvp->v_type == VDIR) {
1857 if (tvp != NULL && tvp->v_type == VDIR)
1872 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1874 if (error == ENOENT)
1880 * nfs file rename rpc called from nfs_remove() above
1883 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1884 struct sillyrename *sp)
1887 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1888 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1893 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1896 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1897 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1898 int tnamelen, struct ucred *cred, struct thread *td)
1900 struct nfsvattr fnfsva, tnfsva;
1901 struct nfsnode *fdnp = VTONFS(fdvp);
1902 struct nfsnode *tdnp = VTONFS(tdvp);
1903 int error = 0, fattrflag, tattrflag;
1905 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1906 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1907 &tattrflag, NULL, NULL);
1908 mtx_lock(&fdnp->n_mtx);
1909 fdnp->n_flag |= NMODIFIED;
1910 if (fattrflag != 0) {
1911 mtx_unlock(&fdnp->n_mtx);
1912 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1914 fdnp->n_attrstamp = 0;
1915 mtx_unlock(&fdnp->n_mtx);
1916 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1918 mtx_lock(&tdnp->n_mtx);
1919 tdnp->n_flag |= NMODIFIED;
1920 if (tattrflag != 0) {
1921 mtx_unlock(&tdnp->n_mtx);
1922 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1924 tdnp->n_attrstamp = 0;
1925 mtx_unlock(&tdnp->n_mtx);
1926 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1928 if (error && NFS_ISV4(fdvp))
1929 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1934 * nfs hard link create call
1937 nfs_link(struct vop_link_args *ap)
1939 struct vnode *vp = ap->a_vp;
1940 struct vnode *tdvp = ap->a_tdvp;
1941 struct componentname *cnp = ap->a_cnp;
1942 struct nfsnode *np, *tdnp;
1943 struct nfsvattr nfsva, dnfsva;
1944 int error = 0, attrflag, dattrflag;
1947 * Push all writes to the server, so that the attribute cache
1948 * doesn't get "out of sync" with the server.
1949 * XXX There should be a better way!
1951 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1953 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1954 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1956 tdnp = VTONFS(tdvp);
1957 mtx_lock(&tdnp->n_mtx);
1958 tdnp->n_flag |= NMODIFIED;
1959 if (dattrflag != 0) {
1960 mtx_unlock(&tdnp->n_mtx);
1961 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1963 tdnp->n_attrstamp = 0;
1964 mtx_unlock(&tdnp->n_mtx);
1965 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1968 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1971 mtx_lock(&np->n_mtx);
1972 np->n_attrstamp = 0;
1973 mtx_unlock(&np->n_mtx);
1974 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1977 * If negative lookup caching is enabled, I might as well
1978 * add an entry for this node. Not necessary for correctness,
1979 * but if negative caching is enabled, then the system
1980 * must care about lookup caching hit rate, so...
1982 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
1983 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
1984 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
1986 if (error && NFS_ISV4(vp))
1987 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
1993 * nfs symbolic link create call
1996 nfs_symlink(struct vop_symlink_args *ap)
1998 struct vnode *dvp = ap->a_dvp;
1999 struct vattr *vap = ap->a_vap;
2000 struct componentname *cnp = ap->a_cnp;
2001 struct nfsvattr nfsva, dnfsva;
2003 struct nfsnode *np = NULL, *dnp;
2004 struct vnode *newvp = NULL;
2005 int error = 0, attrflag, dattrflag, ret;
2007 vap->va_type = VLNK;
2008 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2009 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2010 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2012 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2013 &np, NULL, LK_EXCLUSIVE);
2019 if (newvp != NULL) {
2021 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2023 } else if (!error) {
2025 * If we do not have an error and we could not extract the
2026 * newvp from the response due to the request being NFSv2, we
2027 * have to do a lookup in order to obtain a newvp to return.
2029 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2030 cnp->cn_cred, cnp->cn_thread, &np);
2038 error = nfscl_maperr(cnp->cn_thread, error,
2039 vap->va_uid, vap->va_gid);
2045 mtx_lock(&dnp->n_mtx);
2046 dnp->n_flag |= NMODIFIED;
2047 if (dattrflag != 0) {
2048 mtx_unlock(&dnp->n_mtx);
2049 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2051 dnp->n_attrstamp = 0;
2052 mtx_unlock(&dnp->n_mtx);
2053 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2056 * If negative lookup caching is enabled, I might as well
2057 * add an entry for this node. Not necessary for correctness,
2058 * but if negative caching is enabled, then the system
2059 * must care about lookup caching hit rate, so...
2061 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2062 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2063 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2072 nfs_mkdir(struct vop_mkdir_args *ap)
2074 struct vnode *dvp = ap->a_dvp;
2075 struct vattr *vap = ap->a_vap;
2076 struct componentname *cnp = ap->a_cnp;
2077 struct nfsnode *np = NULL, *dnp;
2078 struct vnode *newvp = NULL;
2081 struct nfsvattr nfsva, dnfsva;
2082 int error = 0, attrflag, dattrflag, ret;
2084 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2086 vap->va_type = VDIR;
2087 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2088 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2089 &attrflag, &dattrflag, NULL);
2091 mtx_lock(&dnp->n_mtx);
2092 dnp->n_flag |= NMODIFIED;
2093 if (dattrflag != 0) {
2094 mtx_unlock(&dnp->n_mtx);
2095 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2097 dnp->n_attrstamp = 0;
2098 mtx_unlock(&dnp->n_mtx);
2099 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2102 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2103 &np, NULL, LK_EXCLUSIVE);
2107 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2112 if (!error && newvp == NULL) {
2113 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2114 cnp->cn_cred, cnp->cn_thread, &np);
2117 if (newvp->v_type != VDIR)
2125 error = nfscl_maperr(cnp->cn_thread, error,
2126 vap->va_uid, vap->va_gid);
2129 * If negative lookup caching is enabled, I might as well
2130 * add an entry for this node. Not necessary for correctness,
2131 * but if negative caching is enabled, then the system
2132 * must care about lookup caching hit rate, so...
2134 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2135 (cnp->cn_flags & MAKEENTRY) &&
2136 attrflag != 0 && dattrflag != 0)
2137 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2145 * nfs remove directory call
2148 nfs_rmdir(struct vop_rmdir_args *ap)
2150 struct vnode *vp = ap->a_vp;
2151 struct vnode *dvp = ap->a_dvp;
2152 struct componentname *cnp = ap->a_cnp;
2153 struct nfsnode *dnp;
2154 struct nfsvattr dnfsva;
2155 int error, dattrflag;
2159 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2160 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2162 mtx_lock(&dnp->n_mtx);
2163 dnp->n_flag |= NMODIFIED;
2164 if (dattrflag != 0) {
2165 mtx_unlock(&dnp->n_mtx);
2166 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2168 dnp->n_attrstamp = 0;
2169 mtx_unlock(&dnp->n_mtx);
2170 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2175 if (error && NFS_ISV4(dvp))
2176 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2179 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2181 if (error == ENOENT)
2190 nfs_readdir(struct vop_readdir_args *ap)
2192 struct vnode *vp = ap->a_vp;
2193 struct nfsnode *np = VTONFS(vp);
2194 struct uio *uio = ap->a_uio;
2195 ssize_t tresid, left;
2199 if (ap->a_eofflag != NULL)
2201 if (vp->v_type != VDIR)
2205 * First, check for hit on the EOF offset cache
2207 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2208 (np->n_flag & NMODIFIED) == 0) {
2209 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2210 mtx_lock(&np->n_mtx);
2211 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2212 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2213 mtx_unlock(&np->n_mtx);
2214 NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2215 if (ap->a_eofflag != NULL)
2219 mtx_unlock(&np->n_mtx);
2224 * NFS always guarantees that directory entries don't straddle
2225 * DIRBLKSIZ boundaries. As such, we need to limit the size
2226 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2229 left = uio->uio_resid % DIRBLKSIZ;
2230 if (left == uio->uio_resid)
2232 uio->uio_resid -= left;
2235 * Call ncl_bioread() to do the real work.
2237 tresid = uio->uio_resid;
2238 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2240 if (!error && uio->uio_resid == tresid) {
2241 NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2242 if (ap->a_eofflag != NULL)
2246 /* Add the partial DIRBLKSIZ (left) back in. */
2247 uio->uio_resid += left;
2253 * Called from below the buffer cache by ncl_doio().
2256 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2259 struct nfsvattr nfsva;
2260 nfsuint64 *cookiep, cookie;
2261 struct nfsnode *dnp = VTONFS(vp);
2262 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2263 int error = 0, eof, attrflag;
2265 KASSERT(uiop->uio_iovcnt == 1 &&
2266 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2267 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2268 ("nfs readdirrpc bad uio"));
2271 * If there is no cookie, assume directory was stale.
2273 ncl_dircookie_lock(dnp);
2274 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2277 ncl_dircookie_unlock(dnp);
2279 ncl_dircookie_unlock(dnp);
2280 return (NFSERR_BAD_COOKIE);
2283 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2284 (void)ncl_fsinfo(nmp, vp, cred, td);
2286 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2287 &attrflag, &eof, NULL);
2289 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2293 * We are now either at the end of the directory or have filled
2297 dnp->n_direofoffset = uiop->uio_offset;
2299 if (uiop->uio_resid > 0)
2300 printf("EEK! readdirrpc resid > 0\n");
2301 ncl_dircookie_lock(dnp);
2302 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2304 ncl_dircookie_unlock(dnp);
2306 } else if (NFS_ISV4(vp)) {
2307 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2313 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2316 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2319 struct nfsvattr nfsva;
2320 nfsuint64 *cookiep, cookie;
2321 struct nfsnode *dnp = VTONFS(vp);
2322 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2323 int error = 0, attrflag, eof;
2325 KASSERT(uiop->uio_iovcnt == 1 &&
2326 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2327 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2328 ("nfs readdirplusrpc bad uio"));
2331 * If there is no cookie, assume directory was stale.
2333 ncl_dircookie_lock(dnp);
2334 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2337 ncl_dircookie_unlock(dnp);
2339 ncl_dircookie_unlock(dnp);
2340 return (NFSERR_BAD_COOKIE);
2343 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2344 (void)ncl_fsinfo(nmp, vp, cred, td);
2345 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2346 &attrflag, &eof, NULL);
2348 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2352 * We are now either at end of the directory or have filled the
2356 dnp->n_direofoffset = uiop->uio_offset;
2358 if (uiop->uio_resid > 0)
2359 printf("EEK! readdirplusrpc resid > 0\n");
2360 ncl_dircookie_lock(dnp);
2361 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2363 ncl_dircookie_unlock(dnp);
2365 } else if (NFS_ISV4(vp)) {
2366 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2372 * Silly rename. To make the NFS filesystem that is stateless look a little
2373 * more like the "ufs" a remove of an active vnode is translated to a rename
2374 * to a funny looking filename that is removed by nfs_inactive on the
2375 * nfsnode. There is the potential for another process on a different client
2376 * to create the same funny name between the nfs_lookitup() fails and the
2377 * nfs_rename() completes, but...
2380 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2382 struct sillyrename *sp;
2386 unsigned int lticks;
2390 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2391 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2392 M_NEWNFSREQ, M_WAITOK);
2393 sp->s_cred = crhold(cnp->cn_cred);
2398 * Fudge together a funny name.
2399 * Changing the format of the funny name to accommodate more
2400 * sillynames per directory.
2401 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2402 * CPU ticks since boot.
2404 pid = cnp->cn_thread->td_proc->p_pid;
2405 lticks = (unsigned int)ticks;
2407 sp->s_namlen = sprintf(sp->s_name,
2408 ".nfs.%08x.%04x4.4", lticks,
2410 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2411 cnp->cn_thread, NULL))
2415 error = nfs_renameit(dvp, vp, cnp, sp);
2418 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2419 cnp->cn_thread, &np);
2420 np->n_sillyrename = sp;
2425 free((caddr_t)sp, M_NEWNFSREQ);
2430 * Look up a file name and optionally either update the file handle or
2431 * allocate an nfsnode, depending on the value of npp.
2432 * npp == NULL --> just do the lookup
2433 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2435 * *npp != NULL --> update the file handle in the vnode
2438 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2439 struct thread *td, struct nfsnode **npp)
2441 struct vnode *newvp = NULL, *vp;
2442 struct nfsnode *np, *dnp = VTONFS(dvp);
2443 struct nfsfh *nfhp, *onfhp;
2444 struct nfsvattr nfsva, dnfsva;
2445 struct componentname cn;
2446 int error = 0, attrflag, dattrflag;
2449 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2450 &nfhp, &attrflag, &dattrflag, NULL);
2452 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2453 if (npp && !error) {
2458 * For NFSv4, check to see if it is the same name and
2459 * replace the name, if it is different.
2461 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2462 (np->n_v4->n4_namelen != len ||
2463 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2464 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2465 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2466 dnp->n_fhp->nfh_len))) {
2468 { char nnn[100]; int nnnl;
2469 nnnl = (len < 100) ? len : 99;
2470 bcopy(name, nnn, nnnl);
2472 printf("replace=%s\n",nnn);
2475 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2476 MALLOC(np->n_v4, struct nfsv4node *,
2477 sizeof (struct nfsv4node) +
2478 dnp->n_fhp->nfh_len + len - 1,
2479 M_NFSV4NODE, M_WAITOK);
2480 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2481 np->n_v4->n4_namelen = len;
2482 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2483 dnp->n_fhp->nfh_len);
2484 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2486 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2490 * Rehash node for new file handle.
2492 vfs_hash_rehash(vp, hash);
2495 FREE((caddr_t)onfhp, M_NFSFH);
2497 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2498 FREE((caddr_t)nfhp, M_NFSFH);
2502 cn.cn_nameptr = name;
2503 cn.cn_namelen = len;
2504 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2505 &np, NULL, LK_EXCLUSIVE);
2510 if (!attrflag && *npp == NULL) {
2518 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2521 if (npp && *npp == NULL) {
2532 if (error && NFS_ISV4(dvp))
2533 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2538 * Nfs Version 3 and 4 commit rpc
2541 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2544 struct nfsvattr nfsva;
2545 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2548 int error, attrflag;
2553 if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2554 uio.uio_offset = offset;
2555 uio.uio_resid = cnt;
2556 error = nfscl_doiods(vp, &uio, NULL, NULL,
2557 NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2559 mtx_lock(&np->n_mtx);
2560 np->n_flag &= ~NDSCOMMIT;
2561 mtx_unlock(&np->n_mtx);
2565 mtx_lock(&nmp->nm_mtx);
2566 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2567 mtx_unlock(&nmp->nm_mtx);
2570 mtx_unlock(&nmp->nm_mtx);
2571 error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2575 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2577 if (error != 0 && NFS_ISV4(vp))
2578 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2584 * For async requests when nfsiod(s) are running, queue the request by
2585 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2589 nfs_strategy(struct vop_strategy_args *ap)
2597 KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2598 KASSERT(!(bp->b_flags & B_DONE),
2599 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2600 BUF_ASSERT_HELD(bp);
2602 if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2603 bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2605 if (bp->b_iocmd == BIO_READ)
2611 * If the op is asynchronous and an i/o daemon is waiting
2612 * queue the request, wake it up and wait for completion
2613 * otherwise just do it ourselves.
2615 if ((bp->b_flags & B_ASYNC) == 0 ||
2616 ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2617 (void) ncl_doio(vp, bp, cr, curthread, 1);
2622 * fsync vnode op. Just call ncl_flush() with commit == 1.
2626 nfs_fsync(struct vop_fsync_args *ap)
2629 if (ap->a_vp->v_type != VREG) {
2631 * For NFS, metadata is changed synchronously on the server,
2632 * so there is nothing to flush. Also, ncl_flush() clears
2633 * the NMODIFIED flag and that shouldn't be done here for
2638 return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2642 * Flush all the blocks associated with a vnode.
2643 * Walk through the buffer pool and push any dirty pages
2644 * associated with the vnode.
2645 * If the called_from_renewthread argument is TRUE, it has been called
2646 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2647 * waiting for a buffer write to complete.
2650 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2651 int commit, int called_from_renewthread)
2653 struct nfsnode *np = VTONFS(vp);
2657 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2658 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2659 int passone = 1, trycnt = 0;
2660 u_quad_t off, endoff, toff;
2661 struct ucred* wcred = NULL;
2662 struct buf **bvec = NULL;
2664 #ifndef NFS_COMMITBVECSIZ
2665 #define NFS_COMMITBVECSIZ 20
2667 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2668 int bvecsize = 0, bveccount;
2670 if (called_from_renewthread != 0)
2672 if (nmp->nm_flag & NFSMNT_INT)
2678 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2679 * server, but has not been committed to stable storage on the server
2680 * yet. On the first pass, the byte range is worked out and the commit
2681 * rpc is done. On the second pass, ncl_writebp() is called to do the
2688 if (NFS_ISV34(vp) && commit) {
2689 if (bvec != NULL && bvec != bvec_on_stack)
2692 * Count up how many buffers waiting for a commit.
2696 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2697 if (!BUF_ISLOCKED(bp) &&
2698 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2699 == (B_DELWRI | B_NEEDCOMMIT))
2703 * Allocate space to remember the list of bufs to commit. It is
2704 * important to use M_NOWAIT here to avoid a race with nfs_write.
2705 * If we can't get memory (for whatever reason), we will end up
2706 * committing the buffers one-by-one in the loop below.
2708 if (bveccount > NFS_COMMITBVECSIZ) {
2710 * Release the vnode interlock to avoid a lock
2714 bvec = (struct buf **)
2715 malloc(bveccount * sizeof(struct buf *),
2719 bvec = bvec_on_stack;
2720 bvecsize = NFS_COMMITBVECSIZ;
2722 bvecsize = bveccount;
2724 bvec = bvec_on_stack;
2725 bvecsize = NFS_COMMITBVECSIZ;
2727 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2728 if (bvecpos >= bvecsize)
2730 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2731 nbp = TAILQ_NEXT(bp, b_bobufs);
2734 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2735 (B_DELWRI | B_NEEDCOMMIT)) {
2737 nbp = TAILQ_NEXT(bp, b_bobufs);
2743 * Work out if all buffers are using the same cred
2744 * so we can deal with them all with one commit.
2746 * NOTE: we are not clearing B_DONE here, so we have
2747 * to do it later on in this routine if we intend to
2748 * initiate I/O on the bp.
2750 * Note: to avoid loopback deadlocks, we do not
2751 * assign b_runningbufspace.
2754 wcred = bp->b_wcred;
2755 else if (wcred != bp->b_wcred)
2757 vfs_busy_pages(bp, 1);
2761 * bp is protected by being locked, but nbp is not
2762 * and vfs_busy_pages() may sleep. We have to
2765 nbp = TAILQ_NEXT(bp, b_bobufs);
2768 * A list of these buffers is kept so that the
2769 * second loop knows which buffers have actually
2770 * been committed. This is necessary, since there
2771 * may be a race between the commit rpc and new
2772 * uncommitted writes on the file.
2774 bvec[bvecpos++] = bp;
2775 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2779 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2787 * Commit data on the server, as required.
2788 * If all bufs are using the same wcred, then use that with
2789 * one call for all of them, otherwise commit each one
2792 if (wcred != NOCRED)
2793 retv = ncl_commit(vp, off, (int)(endoff - off),
2797 for (i = 0; i < bvecpos; i++) {
2800 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2802 size = (u_quad_t)(bp->b_dirtyend
2804 retv = ncl_commit(vp, off, (int)size,
2810 if (retv == NFSERR_STALEWRITEVERF)
2811 ncl_clearcommit(vp->v_mount);
2814 * Now, either mark the blocks I/O done or mark the
2815 * blocks dirty, depending on whether the commit
2818 for (i = 0; i < bvecpos; i++) {
2820 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2823 * Error, leave B_DELWRI intact
2825 vfs_unbusy_pages(bp);
2829 * Success, remove B_DELWRI ( bundirty() ).
2831 * b_dirtyoff/b_dirtyend seem to be NFS
2832 * specific. We should probably move that
2833 * into bundirty(). XXX
2836 bp->b_flags |= B_ASYNC;
2838 bp->b_flags &= ~B_DONE;
2839 bp->b_ioflags &= ~BIO_ERROR;
2840 bp->b_dirtyoff = bp->b_dirtyend = 0;
2847 * Start/do any write(s) that are required.
2851 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2852 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2853 if (waitfor != MNT_WAIT || passone)
2856 error = BUF_TIMELOCK(bp,
2857 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2858 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2863 if (error == ENOLCK) {
2867 if (called_from_renewthread != 0) {
2869 * Return EIO so the flush will be retried
2875 if (newnfs_sigintr(nmp, td)) {
2879 if (slpflag == PCATCH) {
2885 if ((bp->b_flags & B_DELWRI) == 0)
2886 panic("nfs_fsync: not dirty");
2887 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2893 if (passone || !commit)
2894 bp->b_flags |= B_ASYNC;
2896 bp->b_flags |= B_ASYNC;
2898 if (newnfs_sigintr(nmp, td)) {
2909 if (waitfor == MNT_WAIT) {
2910 while (bo->bo_numoutput) {
2911 error = bufobj_wwait(bo, slpflag, slptimeo);
2914 if (called_from_renewthread != 0) {
2916 * Return EIO so that the flush will be
2922 error = newnfs_sigintr(nmp, td);
2925 if (slpflag == PCATCH) {
2932 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2937 * Wait for all the async IO requests to drain
2940 mtx_lock(&np->n_mtx);
2941 while (np->n_directio_asyncwr > 0) {
2942 np->n_flag |= NFSYNCWAIT;
2943 error = newnfs_msleep(td, &np->n_directio_asyncwr,
2944 &np->n_mtx, slpflag | (PRIBIO + 1),
2947 if (newnfs_sigintr(nmp, td)) {
2948 mtx_unlock(&np->n_mtx);
2954 mtx_unlock(&np->n_mtx);
2957 if (NFSHASPNFS(nmp)) {
2958 nfscl_layoutcommit(vp, td);
2960 * Invalidate the attribute cache, since writes to a DS
2961 * won't update the size attribute.
2963 mtx_lock(&np->n_mtx);
2964 np->n_attrstamp = 0;
2966 mtx_lock(&np->n_mtx);
2967 if (np->n_flag & NWRITEERR) {
2968 error = np->n_error;
2969 np->n_flag &= ~NWRITEERR;
2971 if (commit && bo->bo_dirty.bv_cnt == 0 &&
2972 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2973 np->n_flag &= ~NMODIFIED;
2974 mtx_unlock(&np->n_mtx);
2976 if (bvec != NULL && bvec != bvec_on_stack)
2978 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2979 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2980 np->n_directio_asyncwr != 0)) {
2982 /* try, try again... */
2989 vn_printf(vp, "ncl_flush failed");
2990 error = called_from_renewthread != 0 ? EIO : EBUSY;
2996 * NFS advisory byte-level locks.
2999 nfs_advlock(struct vop_advlock_args *ap)
3001 struct vnode *vp = ap->a_vp;
3003 struct nfsnode *np = VTONFS(ap->a_vp);
3004 struct proc *p = (struct proc *)ap->a_id;
3005 struct thread *td = curthread; /* XXX */
3007 int ret, error = EOPNOTSUPP;
3010 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3011 if (vp->v_type != VREG)
3013 if ((ap->a_flags & F_POSIX) != 0)
3016 cred = td->td_ucred;
3017 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3018 if (vp->v_iflag & VI_DOOMED) {
3019 NFSVOPUNLOCK(vp, 0);
3024 * If this is unlocking a write locked region, flush and
3025 * commit them before unlocking. This is required by
3026 * RFC3530 Sec. 9.3.2.
3028 if (ap->a_op == F_UNLCK &&
3029 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3031 (void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3034 * Loop around doing the lock op, while a blocking lock
3035 * must wait for the lock op to succeed.
3038 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3039 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3040 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3041 ap->a_op == F_SETLK) {
3042 NFSVOPUNLOCK(vp, 0);
3043 error = nfs_catnap(PZERO | PCATCH, ret,
3047 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3048 if (vp->v_iflag & VI_DOOMED) {
3049 NFSVOPUNLOCK(vp, 0);
3053 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3054 ap->a_op == F_SETLK);
3055 if (ret == NFSERR_DENIED) {
3056 NFSVOPUNLOCK(vp, 0);
3058 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3059 NFSVOPUNLOCK(vp, 0);
3061 } else if (ret != 0) {
3062 NFSVOPUNLOCK(vp, 0);
3067 * Now, if we just got a lock, invalidate data in the buffer
3068 * cache, as required, so that the coherency conforms with
3069 * RFC3530 Sec. 9.3.2.
3071 if (ap->a_op == F_SETLK) {
3072 if ((np->n_flag & NMODIFIED) == 0) {
3073 np->n_attrstamp = 0;
3074 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3075 ret = VOP_GETATTR(vp, &va, cred);
3077 if ((np->n_flag & NMODIFIED) || ret ||
3078 np->n_change != va.va_filerev) {
3079 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3080 np->n_attrstamp = 0;
3081 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3082 ret = VOP_GETATTR(vp, &va, cred);
3084 np->n_mtime = va.va_mtime;
3085 np->n_change = va.va_filerev;
3088 /* Mark that a file lock has been acquired. */
3089 mtx_lock(&np->n_mtx);
3090 np->n_flag |= NHASBEENLOCKED;
3091 mtx_unlock(&np->n_mtx);
3093 NFSVOPUNLOCK(vp, 0);
3095 } else if (!NFS_ISV4(vp)) {
3096 error = NFSVOPLOCK(vp, LK_SHARED);
3099 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3100 size = VTONFS(vp)->n_size;
3101 NFSVOPUNLOCK(vp, 0);
3102 error = lf_advlock(ap, &(vp->v_lockf), size);
3104 if (nfs_advlock_p != NULL)
3105 error = nfs_advlock_p(ap);
3107 NFSVOPUNLOCK(vp, 0);
3111 if (error == 0 && ap->a_op == F_SETLK) {
3112 error = NFSVOPLOCK(vp, LK_SHARED);
3114 /* Mark that a file lock has been acquired. */
3115 mtx_lock(&np->n_mtx);
3116 np->n_flag |= NHASBEENLOCKED;
3117 mtx_unlock(&np->n_mtx);
3118 NFSVOPUNLOCK(vp, 0);
3126 * NFS advisory byte-level locks.
3129 nfs_advlockasync(struct vop_advlockasync_args *ap)
3131 struct vnode *vp = ap->a_vp;
3136 return (EOPNOTSUPP);
3137 error = NFSVOPLOCK(vp, LK_SHARED);
3140 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3141 size = VTONFS(vp)->n_size;
3142 NFSVOPUNLOCK(vp, 0);
3143 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3145 NFSVOPUNLOCK(vp, 0);
3152 * Print out the contents of an nfsnode.
3155 nfs_print(struct vop_print_args *ap)
3157 struct vnode *vp = ap->a_vp;
3158 struct nfsnode *np = VTONFS(vp);
3160 printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3161 (uintmax_t)np->n_vattr.na_fsid);
3162 if (vp->v_type == VFIFO)
3169 * This is the "real" nfs::bwrite(struct buf*).
3170 * We set B_CACHE if this is a VMIO buffer.
3173 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3175 int oldflags, rtval;
3177 BUF_ASSERT_HELD(bp);
3179 if (bp->b_flags & B_INVAL) {
3184 oldflags = bp->b_flags;
3185 bp->b_flags |= B_CACHE;
3188 * Undirty the bp. We will redirty it later if the I/O fails.
3191 bp->b_flags &= ~B_DONE;
3192 bp->b_ioflags &= ~BIO_ERROR;
3193 bp->b_iocmd = BIO_WRITE;
3195 bufobj_wref(bp->b_bufobj);
3196 curthread->td_ru.ru_oublock++;
3199 * Note: to avoid loopback deadlocks, we do not
3200 * assign b_runningbufspace.
3202 vfs_busy_pages(bp, 1);
3205 bp->b_iooffset = dbtob(bp->b_blkno);
3208 if ((oldflags & B_ASYNC) != 0)
3211 rtval = bufwait(bp);
3212 if (oldflags & B_DELWRI)
3219 * nfs special file access vnode op.
3220 * Essentially just get vattr and then imitate iaccess() since the device is
3221 * local to the client.
3224 nfsspec_access(struct vop_access_args *ap)
3227 struct ucred *cred = ap->a_cred;
3228 struct vnode *vp = ap->a_vp;
3229 accmode_t accmode = ap->a_accmode;
3234 * Disallow write attempts on filesystems mounted read-only;
3235 * unless the file is a socket, fifo, or a block or character
3236 * device resident on the filesystem.
3238 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3239 switch (vp->v_type) {
3249 error = VOP_GETATTR(vp, vap, cred);
3252 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3253 accmode, cred, NULL);
3259 * Read wrapper for fifos.
3262 nfsfifo_read(struct vop_read_args *ap)
3264 struct nfsnode *np = VTONFS(ap->a_vp);
3270 mtx_lock(&np->n_mtx);
3272 vfs_timestamp(&np->n_atim);
3273 mtx_unlock(&np->n_mtx);
3274 error = fifo_specops.vop_read(ap);
3279 * Write wrapper for fifos.
3282 nfsfifo_write(struct vop_write_args *ap)
3284 struct nfsnode *np = VTONFS(ap->a_vp);
3289 mtx_lock(&np->n_mtx);
3291 vfs_timestamp(&np->n_mtim);
3292 mtx_unlock(&np->n_mtx);
3293 return(fifo_specops.vop_write(ap));
3297 * Close wrapper for fifos.
3299 * Update the times on the nfsnode then do fifo close.
3302 nfsfifo_close(struct vop_close_args *ap)
3304 struct vnode *vp = ap->a_vp;
3305 struct nfsnode *np = VTONFS(vp);
3309 mtx_lock(&np->n_mtx);
3310 if (np->n_flag & (NACC | NUPD)) {
3312 if (np->n_flag & NACC)
3314 if (np->n_flag & NUPD)
3317 if (vrefcnt(vp) == 1 &&
3318 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3320 if (np->n_flag & NACC)
3321 vattr.va_atime = np->n_atim;
3322 if (np->n_flag & NUPD)
3323 vattr.va_mtime = np->n_mtim;
3324 mtx_unlock(&np->n_mtx);
3325 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3329 mtx_unlock(&np->n_mtx);
3331 return (fifo_specops.vop_close(ap));
3335 * Just call ncl_writebp() with the force argument set to 1.
3337 * NOTE: B_DONE may or may not be set in a_bp on call.
3340 nfs_bwrite(struct buf *bp)
3343 return (ncl_writebp(bp, 1, curthread));
3346 struct buf_ops buf_ops_newnfs = {
3347 .bop_name = "buf_ops_nfs",
3348 .bop_write = nfs_bwrite,
3349 .bop_strategy = bufstrategy,
3350 .bop_sync = bufsync,
3351 .bop_bdflush = bufbdflush,
3355 nfs_getacl(struct vop_getacl_args *ap)
3359 if (ap->a_type != ACL_TYPE_NFS4)
3360 return (EOPNOTSUPP);
3361 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3363 if (error > NFSERR_STALE) {
3364 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3371 nfs_setacl(struct vop_setacl_args *ap)
3375 if (ap->a_type != ACL_TYPE_NFS4)
3376 return (EOPNOTSUPP);
3377 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3379 if (error > NFSERR_STALE) {
3380 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3387 nfs_set_text(struct vop_set_text_args *ap)
3389 struct vnode *vp = ap->a_vp;
3393 * If the text file has been mmap'd, flush any dirty pages to the
3394 * buffer cache and then...
3395 * Make sure all writes are pushed to the NFS server. If this is not
3396 * done, the modify time of the file can change while the text
3397 * file is being executed. This will cause the process that is
3398 * executing the text file to be terminated.
3400 if (vp->v_object != NULL) {
3401 VM_OBJECT_WLOCK(vp->v_object);
3402 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3403 VM_OBJECT_WUNLOCK(vp->v_object);
3406 /* Now, flush the buffer cache. */
3407 ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
3409 /* And, finally, make sure that n_mtime is up to date. */
3411 mtx_lock(&np->n_mtx);
3412 np->n_mtime = np->n_vattr.na_mtime;
3413 mtx_unlock(&np->n_mtx);
3415 vp->v_vflag |= VV_TEXT;
3420 * Return POSIX pathconf information applicable to nfs filesystems.
3423 nfs_pathconf(struct vop_pathconf_args *ap)
3425 struct nfsv3_pathconf pc;
3426 struct nfsvattr nfsva;
3427 struct vnode *vp = ap->a_vp;
3428 struct thread *td = curthread;
3429 int attrflag, error;
3431 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3432 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3433 ap->a_name == _PC_NO_TRUNC)) ||
3434 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3436 * Since only the above 4 a_names are returned by the NFSv3
3437 * Pathconf RPC, there is no point in doing it for others.
3438 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3439 * be used for _PC_NFS4_ACL as well.
3441 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3444 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3450 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3453 pc.pc_linkmax = LINK_MAX;
3454 pc.pc_namemax = NFS_MAXNAMLEN;
3456 pc.pc_chownrestricted = 1;
3457 pc.pc_caseinsensitive = 0;
3458 pc.pc_casepreserving = 1;
3461 switch (ap->a_name) {
3463 *ap->a_retval = pc.pc_linkmax;
3466 *ap->a_retval = pc.pc_namemax;
3468 case _PC_CHOWN_RESTRICTED:
3469 *ap->a_retval = pc.pc_chownrestricted;
3472 *ap->a_retval = pc.pc_notrunc;
3474 case _PC_ACL_EXTENDED:
3478 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3479 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3484 case _PC_ACL_PATH_MAX:
3486 *ap->a_retval = ACL_MAX_ENTRIES;
3490 case _PC_MAC_PRESENT:
3499 case _PC_ALLOC_SIZE_MIN:
3500 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3502 case _PC_FILESIZEBITS:
3508 case _PC_REC_INCR_XFER_SIZE:
3509 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3511 case _PC_REC_MAX_XFER_SIZE:
3512 *ap->a_retval = -1; /* means ``unlimited'' */
3514 case _PC_REC_MIN_XFER_SIZE:
3515 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3517 case _PC_REC_XFER_ALIGN:
3518 *ap->a_retval = PAGE_SIZE;
3520 case _PC_SYMLINK_MAX:
3521 *ap->a_retval = NFS_MAXPATHLEN;
3525 error = vop_stdpathconf(ap);