2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * from nfs_vnops.c 8.16 (Berkeley) 5/27/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * vnode op calls for Sun NFS version 2, 3 and 4
42 #include "opt_kdtrace.h"
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/resourcevar.h>
50 #include <sys/mount.h>
54 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/socket.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
63 #include <sys/sysctl.h>
64 #include <sys/signalvar.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_object.h>
70 #include <fs/nfs/nfsport.h>
71 #include <fs/nfsclient/nfsnode.h>
72 #include <fs/nfsclient/nfsmount.h>
73 #include <fs/nfsclient/nfs.h>
74 #include <fs/nfsclient/nfs_kdtrace.h>
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
80 #include <nfs/nfs_lock.h>
83 #include <sys/dtrace_bsd.h>
85 dtrace_nfsclient_accesscache_flush_probe_func_t
86 dtrace_nfscl_accesscache_flush_done_probe;
87 uint32_t nfscl_accesscache_flush_done_id;
89 dtrace_nfsclient_accesscache_get_probe_func_t
90 dtrace_nfscl_accesscache_get_hit_probe,
91 dtrace_nfscl_accesscache_get_miss_probe;
92 uint32_t nfscl_accesscache_get_hit_id;
93 uint32_t nfscl_accesscache_get_miss_id;
95 dtrace_nfsclient_accesscache_load_probe_func_t
96 dtrace_nfscl_accesscache_load_done_probe;
97 uint32_t nfscl_accesscache_load_done_id;
98 #endif /* !KDTRACE_HOOKS */
104 extern struct nfsstats newnfsstats;
105 extern int nfsrv_useacl;
106 extern int nfscl_debuglevel;
107 MALLOC_DECLARE(M_NEWNFSREQ);
110 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
111 * calls are not in getblk() and brelse() so that they would not be necessary
115 #define vfs_busy_pages(bp, f)
118 static vop_read_t nfsfifo_read;
119 static vop_write_t nfsfifo_write;
120 static vop_close_t nfsfifo_close;
121 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
123 static vop_lookup_t nfs_lookup;
124 static vop_create_t nfs_create;
125 static vop_mknod_t nfs_mknod;
126 static vop_open_t nfs_open;
127 static vop_pathconf_t nfs_pathconf;
128 static vop_close_t nfs_close;
129 static vop_access_t nfs_access;
130 static vop_getattr_t nfs_getattr;
131 static vop_setattr_t nfs_setattr;
132 static vop_read_t nfs_read;
133 static vop_fsync_t nfs_fsync;
134 static vop_remove_t nfs_remove;
135 static vop_link_t nfs_link;
136 static vop_rename_t nfs_rename;
137 static vop_mkdir_t nfs_mkdir;
138 static vop_rmdir_t nfs_rmdir;
139 static vop_symlink_t nfs_symlink;
140 static vop_readdir_t nfs_readdir;
141 static vop_strategy_t nfs_strategy;
142 static vop_lock1_t nfs_lock1;
143 static int nfs_lookitup(struct vnode *, char *, int,
144 struct ucred *, struct thread *, struct nfsnode **);
145 static int nfs_sillyrename(struct vnode *, struct vnode *,
146 struct componentname *);
147 static vop_access_t nfsspec_access;
148 static vop_readlink_t nfs_readlink;
149 static vop_print_t nfs_print;
150 static vop_advlock_t nfs_advlock;
151 static vop_advlockasync_t nfs_advlockasync;
152 static vop_getacl_t nfs_getacl;
153 static vop_setacl_t nfs_setacl;
156 * Global vfs data structures for nfs
158 struct vop_vector newnfs_vnodeops = {
159 .vop_default = &default_vnodeops,
160 .vop_access = nfs_access,
161 .vop_advlock = nfs_advlock,
162 .vop_advlockasync = nfs_advlockasync,
163 .vop_close = nfs_close,
164 .vop_create = nfs_create,
165 .vop_fsync = nfs_fsync,
166 .vop_getattr = nfs_getattr,
167 .vop_getpages = ncl_getpages,
168 .vop_putpages = ncl_putpages,
169 .vop_inactive = ncl_inactive,
170 .vop_link = nfs_link,
171 .vop_lock1 = nfs_lock1,
172 .vop_lookup = nfs_lookup,
173 .vop_mkdir = nfs_mkdir,
174 .vop_mknod = nfs_mknod,
175 .vop_open = nfs_open,
176 .vop_pathconf = nfs_pathconf,
177 .vop_print = nfs_print,
178 .vop_read = nfs_read,
179 .vop_readdir = nfs_readdir,
180 .vop_readlink = nfs_readlink,
181 .vop_reclaim = ncl_reclaim,
182 .vop_remove = nfs_remove,
183 .vop_rename = nfs_rename,
184 .vop_rmdir = nfs_rmdir,
185 .vop_setattr = nfs_setattr,
186 .vop_strategy = nfs_strategy,
187 .vop_symlink = nfs_symlink,
188 .vop_write = ncl_write,
189 .vop_getacl = nfs_getacl,
190 .vop_setacl = nfs_setacl,
193 struct vop_vector newnfs_fifoops = {
194 .vop_default = &fifo_specops,
195 .vop_access = nfsspec_access,
196 .vop_close = nfsfifo_close,
197 .vop_fsync = nfs_fsync,
198 .vop_getattr = nfs_getattr,
199 .vop_inactive = ncl_inactive,
200 .vop_print = nfs_print,
201 .vop_read = nfsfifo_read,
202 .vop_reclaim = ncl_reclaim,
203 .vop_setattr = nfs_setattr,
204 .vop_write = nfsfifo_write,
207 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
208 struct componentname *cnp, struct vattr *vap);
209 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
210 int namelen, struct ucred *cred, struct thread *td);
211 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
212 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
213 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
214 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
215 struct componentname *scnp, struct sillyrename *sp);
220 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
222 SYSCTL_DECL(_vfs_nfs);
224 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
226 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
228 static int nfs_prime_access_cache = 0;
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
230 &nfs_prime_access_cache, 0,
231 "Prime NFS ACCESS cache when fetching attributes");
233 static int newnfs_commit_on_close = 0;
234 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
235 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
237 static int nfs_clean_pages_on_close = 1;
238 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
239 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
241 int newnfs_directio_enable = 0;
242 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
243 &newnfs_directio_enable, 0, "Enable NFS directio");
245 int nfs_keep_dirty_on_error;
246 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
247 &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
250 * This sysctl allows other processes to mmap a file that has been opened
251 * O_DIRECT by a process. In general, having processes mmap the file while
252 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
253 * this by default to prevent DoS attacks - to prevent a malicious user from
254 * opening up files O_DIRECT preventing other users from mmap'ing these
255 * files. "Protected" environments where stricter consistency guarantees are
256 * required can disable this knob. The process that opened the file O_DIRECT
257 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
260 int newnfs_directio_allow_mmap = 1;
261 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
262 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
265 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
266 &newnfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
268 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
269 &newnfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
272 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
273 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
274 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
278 * The list of locks after the description of the lock is the ordering
279 * of other locks acquired with the lock held.
280 * np->n_mtx : Protects the fields in the nfsnode.
282 VI_MTX (acquired indirectly)
283 * nmp->nm_mtx : Protects the fields in the nfsmount.
285 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
286 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
289 * rep->r_mtx : Protects the fields in an nfsreq.
293 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
294 struct ucred *cred, u_int32_t *retmode)
296 int error = 0, attrflag, i, lrupos;
298 struct nfsnode *np = VTONFS(vp);
299 struct nfsvattr nfsva;
301 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
304 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
307 mtx_lock(&np->n_mtx);
308 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
309 if (np->n_accesscache[i].uid == cred->cr_uid) {
310 np->n_accesscache[i].mode = rmode;
311 np->n_accesscache[i].stamp = time_second;
314 if (i > 0 && np->n_accesscache[i].stamp <
315 np->n_accesscache[lrupos].stamp)
318 if (i == NFS_ACCESSCACHESIZE) {
319 np->n_accesscache[lrupos].uid = cred->cr_uid;
320 np->n_accesscache[lrupos].mode = rmode;
321 np->n_accesscache[lrupos].stamp = time_second;
323 mtx_unlock(&np->n_mtx);
326 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
327 } else if (NFS_ISV4(vp)) {
328 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
332 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
339 * nfs access vnode op.
340 * For nfs version 2, just return ok. File accesses may fail later.
341 * For nfs version 3, use the access rpc to check accessibility. If file modes
342 * are changed on the server, accesses might still fail later.
345 nfs_access(struct vop_access_args *ap)
347 struct vnode *vp = ap->a_vp;
348 int error = 0, i, gotahit;
349 u_int32_t mode, wmode, rmode;
350 int v34 = NFS_ISV34(vp);
351 struct nfsnode *np = VTONFS(vp);
354 * Disallow write attempts on filesystems mounted read-only;
355 * unless the file is a socket, fifo, or a block or character
356 * device resident on the filesystem.
358 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
359 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
360 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
361 switch (vp->v_type) {
371 * For nfs v3 or v4, check to see if we have done this recently, and if
372 * so return our cached result instead of making an ACCESS call.
373 * If not, do an access rpc, otherwise you are stuck emulating
374 * ufs_access() locally using the vattr. This may not be correct,
375 * since the server may apply other access criteria such as
376 * client uid-->server uid mapping that we do not know about.
379 if (ap->a_accmode & VREAD)
380 mode = NFSACCESS_READ;
383 if (vp->v_type != VDIR) {
384 if (ap->a_accmode & VWRITE)
385 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
386 if (ap->a_accmode & VAPPEND)
387 mode |= NFSACCESS_EXTEND;
388 if (ap->a_accmode & VEXEC)
389 mode |= NFSACCESS_EXECUTE;
390 if (ap->a_accmode & VDELETE)
391 mode |= NFSACCESS_DELETE;
393 if (ap->a_accmode & VWRITE)
394 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
395 if (ap->a_accmode & VAPPEND)
396 mode |= NFSACCESS_EXTEND;
397 if (ap->a_accmode & VEXEC)
398 mode |= NFSACCESS_LOOKUP;
399 if (ap->a_accmode & VDELETE)
400 mode |= NFSACCESS_DELETE;
401 if (ap->a_accmode & VDELETE_CHILD)
402 mode |= NFSACCESS_MODIFY;
404 /* XXX safety belt, only make blanket request if caching */
405 if (nfsaccess_cache_timeout > 0) {
406 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
407 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
408 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
414 * Does our cached result allow us to give a definite yes to
418 mtx_lock(&np->n_mtx);
419 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
420 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
421 if (time_second < (np->n_accesscache[i].stamp
422 + nfsaccess_cache_timeout) &&
423 (np->n_accesscache[i].mode & mode) == mode) {
424 NFSINCRGLOBAL(newnfsstats.accesscache_hits);
430 mtx_unlock(&np->n_mtx);
433 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
434 ap->a_cred->cr_uid, mode);
436 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
437 ap->a_cred->cr_uid, mode);
441 * Either a no, or a don't know. Go to the wire.
443 NFSINCRGLOBAL(newnfsstats.accesscache_misses);
444 error = nfs34_access_otw(vp, wmode, ap->a_td,
447 (rmode & mode) != mode)
452 if ((error = nfsspec_access(ap)) != 0) {
456 * Attempt to prevent a mapped root from accessing a file
457 * which it shouldn't. We try to read a byte from the file
458 * if the user is root and the file is not zero length.
459 * After calling nfsspec_access, we should have the correct
462 mtx_lock(&np->n_mtx);
463 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
464 && VTONFS(vp)->n_size > 0) {
469 mtx_unlock(&np->n_mtx);
472 auio.uio_iov = &aiov;
476 auio.uio_segflg = UIO_SYSSPACE;
477 auio.uio_rw = UIO_READ;
478 auio.uio_td = ap->a_td;
480 if (vp->v_type == VREG)
481 error = ncl_readrpc(vp, &auio, ap->a_cred);
482 else if (vp->v_type == VDIR) {
484 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
486 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
487 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
490 } else if (vp->v_type == VLNK)
491 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
495 mtx_unlock(&np->n_mtx);
503 * Check to see if the type is ok
504 * and that deletion is not in progress.
505 * For paged in text files, you will need to flush the page cache
506 * if consistency is lost.
510 nfs_open(struct vop_open_args *ap)
512 struct vnode *vp = ap->a_vp;
513 struct nfsnode *np = VTONFS(vp);
516 int fmode = ap->a_mode;
519 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
523 * For NFSv4, we need to do the Open Op before cache validation,
524 * so that we conform to RFC3530 Sec. 9.3.1.
527 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
529 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
536 * Now, if this Open will be doing reading, re-validate/flush the
537 * cache, so that Close/Open coherency is maintained.
539 mtx_lock(&np->n_mtx);
540 if (np->n_flag & NMODIFIED) {
541 mtx_unlock(&np->n_mtx);
542 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
543 if (error == EINTR || error == EIO) {
545 (void) nfsrpc_close(vp, 0, ap->a_td);
548 mtx_lock(&np->n_mtx);
550 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
551 if (vp->v_type == VDIR)
552 np->n_direofoffset = 0;
553 mtx_unlock(&np->n_mtx);
554 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
557 (void) nfsrpc_close(vp, 0, ap->a_td);
560 mtx_lock(&np->n_mtx);
561 np->n_mtime = vattr.va_mtime;
563 np->n_change = vattr.va_filerev;
565 mtx_unlock(&np->n_mtx);
566 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
569 (void) nfsrpc_close(vp, 0, ap->a_td);
572 mtx_lock(&np->n_mtx);
573 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
574 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
575 if (vp->v_type == VDIR)
576 np->n_direofoffset = 0;
577 mtx_unlock(&np->n_mtx);
578 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
579 if (error == EINTR || error == EIO) {
581 (void) nfsrpc_close(vp, 0, ap->a_td);
584 mtx_lock(&np->n_mtx);
585 np->n_mtime = vattr.va_mtime;
587 np->n_change = vattr.va_filerev;
592 * If the object has >= 1 O_DIRECT active opens, we disable caching.
594 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
595 (vp->v_type == VREG)) {
596 if (np->n_directio_opens == 0) {
597 mtx_unlock(&np->n_mtx);
598 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
601 (void) nfsrpc_close(vp, 0, ap->a_td);
604 mtx_lock(&np->n_mtx);
605 np->n_flag |= NNONCACHE;
607 np->n_directio_opens++;
610 /* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
611 if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
612 np->n_flag |= NWRITEOPENED;
615 * If this is an open for writing, capture a reference to the
616 * credentials, so they can be used by ncl_putpages(). Using
617 * these write credentials is preferable to the credentials of
618 * whatever thread happens to be doing the VOP_PUTPAGES() since
619 * the write RPCs are less likely to fail with EACCES.
621 if ((fmode & FWRITE) != 0) {
622 cred = np->n_writecred;
623 np->n_writecred = crhold(ap->a_cred);
626 mtx_unlock(&np->n_mtx);
630 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
636 * What an NFS client should do upon close after writing is a debatable issue.
637 * Most NFS clients push delayed writes to the server upon close, basically for
639 * 1 - So that any write errors may be reported back to the client process
640 * doing the close system call. By far the two most likely errors are
641 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
642 * 2 - To put a worst case upper bound on cache inconsistency between
643 * multiple clients for the file.
644 * There is also a consistency problem for Version 2 of the protocol w.r.t.
645 * not being able to tell if other clients are writing a file concurrently,
646 * since there is no way of knowing if the changed modify time in the reply
647 * is only due to the write for this client.
648 * (NFS Version 3 provides weak cache consistency data in the reply that
649 * should be sufficient to detect and handle this case.)
651 * The current code does the following:
652 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
653 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
654 * or commit them (this satisfies 1 and 2 except for the
655 * case where the server crashes after this close but
656 * before the commit RPC, which is felt to be "good
657 * enough". Changing the last argument to ncl_flush() to
658 * a 1 would force a commit operation, if it is felt a
659 * commit is necessary now.
660 * for NFS Version 4 - flush the dirty buffers and commit them, if
661 * nfscl_mustflush() says this is necessary.
662 * It is necessary if there is no write delegation held,
663 * in order to satisfy open/close coherency.
664 * If the file isn't cached on local stable storage,
665 * it may be necessary in order to detect "out of space"
666 * errors from the server, if the write delegation
667 * issued by the server doesn't allow the file to grow.
671 nfs_close(struct vop_close_args *ap)
673 struct vnode *vp = ap->a_vp;
674 struct nfsnode *np = VTONFS(vp);
675 struct nfsvattr nfsva;
677 int error = 0, ret, localcred = 0;
678 int fmode = ap->a_fflag;
680 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
683 * During shutdown, a_cred isn't valid, so just use root.
685 if (ap->a_cred == NOCRED) {
686 cred = newnfs_getcred();
691 if (vp->v_type == VREG) {
693 * Examine and clean dirty pages, regardless of NMODIFIED.
694 * This closes a major hole in close-to-open consistency.
695 * We want to push out all dirty pages (and buffers) on
696 * close, regardless of whether they were dirtied by
697 * mmap'ed writes or via write().
699 if (nfs_clean_pages_on_close && vp->v_object) {
700 VM_OBJECT_WLOCK(vp->v_object);
701 vm_object_page_clean(vp->v_object, 0, 0, 0);
702 VM_OBJECT_WUNLOCK(vp->v_object);
704 mtx_lock(&np->n_mtx);
705 if (np->n_flag & NMODIFIED) {
706 mtx_unlock(&np->n_mtx);
709 * Under NFSv3 we have dirty buffers to dispose of. We
710 * must flush them to the NFS server. We have the option
711 * of waiting all the way through the commit rpc or just
712 * waiting for the initial write. The default is to only
713 * wait through the initial write so the data is in the
714 * server's cache, which is roughly similar to the state
715 * a standard disk subsystem leaves the file in on close().
717 * We cannot clear the NMODIFIED bit in np->n_flag due to
718 * potential races with other processes, and certainly
719 * cannot clear it if we don't commit.
720 * These races occur when there is no longer the old
721 * traditional vnode locking implemented for Vnode Ops.
723 int cm = newnfs_commit_on_close ? 1 : 0;
724 error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm, 0);
725 /* np->n_flag &= ~NMODIFIED; */
726 } else if (NFS_ISV4(vp)) {
727 if (nfscl_mustflush(vp) != 0) {
728 int cm = newnfs_commit_on_close ? 1 : 0;
729 error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
732 * as above w.r.t races when clearing
734 * np->n_flag &= ~NMODIFIED;
738 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
739 mtx_lock(&np->n_mtx);
742 * Invalidate the attribute cache in all cases.
743 * An open is going to fetch fresh attrs any way, other procs
744 * on this node that have file open will be forced to do an
745 * otw attr fetch, but this is safe.
746 * --> A user found that their RPC count dropped by 20% when
747 * this was commented out and I can't see any requirement
748 * for it, so I've disabled it when negative lookups are
749 * enabled. (What does this have to do with negative lookup
750 * caching? Well nothing, except it was reported by the
751 * same user that needed negative lookup caching and I wanted
752 * there to be a way to disable it to see if it
753 * is the cause of some caching/coherency issue that might
756 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
758 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
760 if (np->n_flag & NWRITEERR) {
761 np->n_flag &= ~NWRITEERR;
764 mtx_unlock(&np->n_mtx);
769 * Get attributes so "change" is up to date.
771 if (error == 0 && nfscl_mustflush(vp) != 0 &&
772 vp->v_type == VREG &&
773 (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
774 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
777 np->n_change = nfsva.na_filerev;
778 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
786 ret = nfsrpc_close(vp, 0, ap->a_td);
790 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
793 if (newnfs_directio_enable)
794 KASSERT((np->n_directio_asyncwr == 0),
795 ("nfs_close: dirty unflushed (%d) directio buffers\n",
796 np->n_directio_asyncwr));
797 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
798 mtx_lock(&np->n_mtx);
799 KASSERT((np->n_directio_opens > 0),
800 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
801 np->n_directio_opens--;
802 if (np->n_directio_opens == 0)
803 np->n_flag &= ~NNONCACHE;
804 mtx_unlock(&np->n_mtx);
812 * nfs getattr call from vfs.
815 nfs_getattr(struct vop_getattr_args *ap)
817 struct vnode *vp = ap->a_vp;
818 struct thread *td = curthread; /* XXX */
819 struct nfsnode *np = VTONFS(vp);
821 struct nfsvattr nfsva;
822 struct vattr *vap = ap->a_vap;
826 * Update local times for special files.
828 mtx_lock(&np->n_mtx);
829 if (np->n_flag & (NACC | NUPD))
831 mtx_unlock(&np->n_mtx);
833 * First look in the cache.
835 if (ncl_getattrcache(vp, &vattr) == 0) {
836 vap->va_type = vattr.va_type;
837 vap->va_mode = vattr.va_mode;
838 vap->va_nlink = vattr.va_nlink;
839 vap->va_uid = vattr.va_uid;
840 vap->va_gid = vattr.va_gid;
841 vap->va_fsid = vattr.va_fsid;
842 vap->va_fileid = vattr.va_fileid;
843 vap->va_size = vattr.va_size;
844 vap->va_blocksize = vattr.va_blocksize;
845 vap->va_atime = vattr.va_atime;
846 vap->va_mtime = vattr.va_mtime;
847 vap->va_ctime = vattr.va_ctime;
848 vap->va_gen = vattr.va_gen;
849 vap->va_flags = vattr.va_flags;
850 vap->va_rdev = vattr.va_rdev;
851 vap->va_bytes = vattr.va_bytes;
852 vap->va_filerev = vattr.va_filerev;
854 * Get the local modify time for the case of a write
857 nfscl_deleggetmodtime(vp, &vap->va_mtime);
861 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
862 nfsaccess_cache_timeout > 0) {
863 NFSINCRGLOBAL(newnfsstats.accesscache_misses);
864 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
865 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
866 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
870 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
872 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
875 * Get the local modify time for the case of a write
878 nfscl_deleggetmodtime(vp, &vap->va_mtime);
879 } else if (NFS_ISV4(vp)) {
880 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
889 nfs_setattr(struct vop_setattr_args *ap)
891 struct vnode *vp = ap->a_vp;
892 struct nfsnode *np = VTONFS(vp);
893 struct thread *td = curthread; /* XXX */
894 struct vattr *vap = ap->a_vap;
903 * Setting of flags and marking of atimes are not supported.
905 if (vap->va_flags != VNOVAL)
909 * Disallow write attempts if the filesystem is mounted read-only.
911 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
912 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
913 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
914 (vp->v_mount->mnt_flag & MNT_RDONLY))
916 if (vap->va_size != VNOVAL) {
917 switch (vp->v_type) {
924 if (vap->va_mtime.tv_sec == VNOVAL &&
925 vap->va_atime.tv_sec == VNOVAL &&
926 vap->va_mode == (mode_t)VNOVAL &&
927 vap->va_uid == (uid_t)VNOVAL &&
928 vap->va_gid == (gid_t)VNOVAL)
930 vap->va_size = VNOVAL;
934 * Disallow write attempts if the filesystem is
937 if (vp->v_mount->mnt_flag & MNT_RDONLY)
940 * We run vnode_pager_setsize() early (why?),
941 * we must set np->n_size now to avoid vinvalbuf
942 * V_SAVE races that might setsize a lower
945 mtx_lock(&np->n_mtx);
947 mtx_unlock(&np->n_mtx);
948 error = ncl_meta_setsize(vp, ap->a_cred, td,
950 mtx_lock(&np->n_mtx);
951 if (np->n_flag & NMODIFIED) {
953 mtx_unlock(&np->n_mtx);
954 if (vap->va_size == 0)
955 error = ncl_vinvalbuf(vp, 0, td, 1);
957 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
959 vnode_pager_setsize(vp, tsize);
963 * Call nfscl_delegmodtime() to set the modify time
964 * locally, as required.
966 nfscl_delegmodtime(vp);
968 mtx_unlock(&np->n_mtx);
970 * np->n_size has already been set to vap->va_size
971 * in ncl_meta_setsize(). We must set it again since
972 * nfs_loadattrcache() could be called through
973 * ncl_meta_setsize() and could modify np->n_size.
975 mtx_lock(&np->n_mtx);
976 np->n_vattr.na_size = np->n_size = vap->va_size;
977 mtx_unlock(&np->n_mtx);
980 mtx_lock(&np->n_mtx);
981 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
982 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
983 mtx_unlock(&np->n_mtx);
984 if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
985 (error == EINTR || error == EIO))
988 mtx_unlock(&np->n_mtx);
990 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
991 if (error && vap->va_size != VNOVAL) {
992 mtx_lock(&np->n_mtx);
993 np->n_size = np->n_vattr.na_size = tsize;
994 vnode_pager_setsize(vp, tsize);
995 mtx_unlock(&np->n_mtx);
1001 * Do an nfs setattr rpc.
1004 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1007 struct nfsnode *np = VTONFS(vp);
1008 int error, ret, attrflag, i;
1009 struct nfsvattr nfsva;
1011 if (NFS_ISV34(vp)) {
1012 mtx_lock(&np->n_mtx);
1013 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1014 np->n_accesscache[i].stamp = 0;
1015 np->n_flag |= NDELEGMOD;
1016 mtx_unlock(&np->n_mtx);
1017 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1019 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1022 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1026 if (error && NFS_ISV4(vp))
1027 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1032 * nfs lookup call, one step at a time...
1033 * First look in cache
1034 * If not found, unlock the directory nfsnode and do the rpc
1037 nfs_lookup(struct vop_lookup_args *ap)
1039 struct componentname *cnp = ap->a_cnp;
1040 struct vnode *dvp = ap->a_dvp;
1041 struct vnode **vpp = ap->a_vpp;
1042 struct mount *mp = dvp->v_mount;
1043 int flags = cnp->cn_flags;
1044 struct vnode *newvp;
1045 struct nfsmount *nmp;
1046 struct nfsnode *np, *newnp;
1047 int error = 0, attrflag, dattrflag, ltype, ncticks;
1048 struct thread *td = cnp->cn_thread;
1050 struct nfsvattr dnfsva, nfsva;
1052 struct timespec nctime;
1055 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1056 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1058 if (dvp->v_type != VDIR)
1063 /* For NFSv4, wait until any remove is done. */
1064 mtx_lock(&np->n_mtx);
1065 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1066 np->n_flag |= NREMOVEWANT;
1067 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1069 mtx_unlock(&np->n_mtx);
1071 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1073 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1074 if (error > 0 && error != ENOENT)
1078 * Lookups of "." are special and always return the
1079 * current directory. cache_lookup() already handles
1080 * associated locking bookkeeping, etc.
1082 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1083 /* XXX: Is this really correct? */
1084 if (cnp->cn_nameiop != LOOKUP &&
1086 cnp->cn_flags |= SAVENAME;
1091 * We only accept a positive hit in the cache if the
1092 * change time of the file matches our cached copy.
1093 * Otherwise, we discard the cache entry and fallback
1094 * to doing a lookup RPC. We also only trust cache
1095 * entries for less than nm_nametimeo seconds.
1097 * To better handle stale file handles and attributes,
1098 * clear the attribute cache of this node if it is a
1099 * leaf component, part of an open() call, and not
1100 * locally modified before fetching the attributes.
1101 * This should allow stale file handles to be detected
1102 * here where we can fall back to a LOOKUP RPC to
1103 * recover rather than having nfs_open() detect the
1104 * stale file handle and failing open(2) with ESTALE.
1107 newnp = VTONFS(newvp);
1108 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1109 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1110 !(newnp->n_flag & NMODIFIED)) {
1111 mtx_lock(&newnp->n_mtx);
1112 newnp->n_attrstamp = 0;
1113 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1114 mtx_unlock(&newnp->n_mtx);
1116 if (nfscl_nodeleg(newvp, 0) == 0 ||
1117 ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1118 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1119 timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1120 NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1121 if (cnp->cn_nameiop != LOOKUP &&
1123 cnp->cn_flags |= SAVENAME;
1132 } else if (error == ENOENT) {
1133 if (dvp->v_iflag & VI_DOOMED)
1136 * We only accept a negative hit in the cache if the
1137 * modification time of the parent directory matches
1138 * the cached copy in the name cache entry.
1139 * Otherwise, we discard all of the negative cache
1140 * entries for this directory. We also only trust
1141 * negative cache entries for up to nm_negnametimeo
1144 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1145 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1146 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1147 NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1150 cache_purge_negative(dvp);
1155 NFSINCRGLOBAL(newnfsstats.lookupcache_misses);
1156 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1157 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1160 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1162 if (newvp != NULLVP) {
1167 if (error != ENOENT) {
1169 error = nfscl_maperr(td, error, (uid_t)0,
1174 /* The requested file was not found. */
1175 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1176 (flags & ISLASTCN)) {
1178 * XXX: UFS does a full VOP_ACCESS(dvp,
1179 * VWRITE) here instead of just checking
1182 if (mp->mnt_flag & MNT_RDONLY)
1184 cnp->cn_flags |= SAVENAME;
1185 return (EJUSTRETURN);
1188 if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1190 * Cache the modification time of the parent
1191 * directory from the post-op attributes in
1192 * the name cache entry. The negative cache
1193 * entry will be ignored once the directory
1194 * has changed. Don't bother adding the entry
1195 * if the directory has already changed.
1197 mtx_lock(&np->n_mtx);
1198 if (timespeccmp(&np->n_vattr.na_mtime,
1199 &dnfsva.na_mtime, ==)) {
1200 mtx_unlock(&np->n_mtx);
1201 cache_enter_time(dvp, NULL, cnp,
1202 &dnfsva.na_mtime, NULL);
1204 mtx_unlock(&np->n_mtx);
1210 * Handle RENAME case...
1212 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1213 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1214 FREE((caddr_t)nfhp, M_NFSFH);
1217 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1223 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1226 cnp->cn_flags |= SAVENAME;
1230 if (flags & ISDOTDOT) {
1231 ltype = NFSVOPISLOCKED(dvp);
1232 error = vfs_busy(mp, MBF_NOWAIT);
1235 NFSVOPUNLOCK(dvp, 0);
1236 error = vfs_busy(mp, 0);
1237 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1239 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1246 NFSVOPUNLOCK(dvp, 0);
1247 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1253 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1254 if (dvp->v_iflag & VI_DOOMED) {
1266 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1268 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1269 FREE((caddr_t)nfhp, M_NFSFH);
1273 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1276 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1282 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1284 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1285 !(np->n_flag & NMODIFIED)) {
1287 * Flush the attribute cache when opening a
1288 * leaf node to ensure that fresh attributes
1289 * are fetched in nfs_open() since we did not
1290 * fetch attributes from the LOOKUP reply.
1292 mtx_lock(&np->n_mtx);
1293 np->n_attrstamp = 0;
1294 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1295 mtx_unlock(&np->n_mtx);
1298 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1299 cnp->cn_flags |= SAVENAME;
1300 if ((cnp->cn_flags & MAKEENTRY) &&
1301 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1302 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1303 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1304 newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1311 * Just call ncl_bioread() to do the work.
1314 nfs_read(struct vop_read_args *ap)
1316 struct vnode *vp = ap->a_vp;
1318 switch (vp->v_type) {
1320 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1324 return (EOPNOTSUPP);
1332 nfs_readlink(struct vop_readlink_args *ap)
1334 struct vnode *vp = ap->a_vp;
1336 if (vp->v_type != VLNK)
1338 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1342 * Do a readlink rpc.
1343 * Called by ncl_doio() from below the buffer cache.
1346 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1348 int error, ret, attrflag;
1349 struct nfsvattr nfsva;
1351 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1354 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1358 if (error && NFS_ISV4(vp))
1359 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1368 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1370 int error, ret, attrflag;
1371 struct nfsvattr nfsva;
1372 struct nfsmount *nmp;
1374 nmp = VFSTONFS(vnode_mount(vp));
1377 if (NFSHASPNFS(nmp))
1378 error = nfscl_doiods(vp, uiop, NULL, NULL,
1379 NFSV4OPEN_ACCESSREAD, cred, uiop->uio_td);
1380 NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1382 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1385 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1389 if (error && NFS_ISV4(vp))
1390 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1398 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1399 int *iomode, int *must_commit, int called_from_strategy)
1401 struct nfsvattr nfsva;
1402 int error, attrflag, ret;
1403 struct nfsmount *nmp;
1405 nmp = VFSTONFS(vnode_mount(vp));
1408 if (NFSHASPNFS(nmp))
1409 error = nfscl_doiods(vp, uiop, iomode, must_commit,
1410 NFSV4OPEN_ACCESSWRITE, cred, uiop->uio_td);
1411 NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1413 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1414 uiop->uio_td, &nfsva, &attrflag, NULL,
1415 called_from_strategy);
1417 if (VTONFS(vp)->n_flag & ND_NFSV4)
1418 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1421 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1427 *iomode = NFSWRITE_FILESYNC;
1428 if (error && NFS_ISV4(vp))
1429 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1435 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1436 * mode set to specify the file type and the size field for rdev.
1439 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1442 struct nfsvattr nfsva, dnfsva;
1443 struct vnode *newvp = NULL;
1444 struct nfsnode *np = NULL, *dnp;
1447 int error = 0, attrflag, dattrflag;
1450 if (vap->va_type == VCHR || vap->va_type == VBLK)
1451 rdev = vap->va_rdev;
1452 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1455 return (EOPNOTSUPP);
1456 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1458 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1459 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1460 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1463 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1464 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1465 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1468 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1469 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1472 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1475 if (attrflag != 0) {
1476 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1484 } else if (NFS_ISV4(dvp)) {
1485 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1489 mtx_lock(&dnp->n_mtx);
1490 dnp->n_flag |= NMODIFIED;
1492 dnp->n_attrstamp = 0;
1493 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1495 mtx_unlock(&dnp->n_mtx);
1501 * just call nfs_mknodrpc() to do the work.
1505 nfs_mknod(struct vop_mknod_args *ap)
1507 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1510 static struct mtx nfs_cverf_mtx;
1511 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1517 static nfsquad_t cverf;
1519 static int cverf_initialized = 0;
1521 mtx_lock(&nfs_cverf_mtx);
1522 if (cverf_initialized == 0) {
1523 cverf.lval[0] = arc4random();
1524 cverf.lval[1] = arc4random();
1525 cverf_initialized = 1;
1529 mtx_unlock(&nfs_cverf_mtx);
1535 * nfs file create call
1538 nfs_create(struct vop_create_args *ap)
1540 struct vnode *dvp = ap->a_dvp;
1541 struct vattr *vap = ap->a_vap;
1542 struct componentname *cnp = ap->a_cnp;
1543 struct nfsnode *np = NULL, *dnp;
1544 struct vnode *newvp = NULL;
1545 struct nfsmount *nmp;
1546 struct nfsvattr dnfsva, nfsva;
1549 int error = 0, attrflag, dattrflag, fmode = 0;
1553 * Oops, not for me..
1555 if (vap->va_type == VSOCK)
1556 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1558 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1560 if (vap->va_vaflags & VA_EXCLUSIVE)
1563 nmp = VFSTONFS(vnode_mount(dvp));
1565 /* For NFSv4, wait until any remove is done. */
1566 mtx_lock(&dnp->n_mtx);
1567 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1568 dnp->n_flag |= NREMOVEWANT;
1569 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1571 mtx_unlock(&dnp->n_mtx);
1573 cverf = nfs_get_cverf();
1574 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1575 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1576 &nfhp, &attrflag, &dattrflag, NULL);
1579 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1580 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1581 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1584 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1585 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1588 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1592 error = nfsrpc_getattr(newvp, cnp->cn_cred,
1593 cnp->cn_thread, &nfsva, NULL);
1595 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1599 if (newvp != NULL) {
1603 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1604 error == NFSERR_NOTSUPP) {
1608 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1609 if (nfscl_checksattr(vap, &nfsva)) {
1610 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1611 cnp->cn_thread, &nfsva, &attrflag, NULL);
1612 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1613 vap->va_gid != (gid_t)VNOVAL)) {
1614 /* try again without setting uid/gid */
1615 vap->va_uid = (uid_t)VNOVAL;
1616 vap->va_gid = (uid_t)VNOVAL;
1617 error = nfsrpc_setattr(newvp, vap, NULL,
1618 cnp->cn_cred, cnp->cn_thread, &nfsva,
1622 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1629 if ((cnp->cn_flags & MAKEENTRY) && attrflag)
1630 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1633 } else if (NFS_ISV4(dvp)) {
1634 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1637 mtx_lock(&dnp->n_mtx);
1638 dnp->n_flag |= NMODIFIED;
1640 dnp->n_attrstamp = 0;
1641 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1643 mtx_unlock(&dnp->n_mtx);
1648 * nfs file remove call
1649 * To try and make nfs semantics closer to ufs semantics, a file that has
1650 * other processes using the vnode is renamed instead of removed and then
1651 * removed later on the last close.
1652 * - If v_usecount > 1
1653 * If a rename is not already in the works
1654 * call nfs_sillyrename() to set it up
1659 nfs_remove(struct vop_remove_args *ap)
1661 struct vnode *vp = ap->a_vp;
1662 struct vnode *dvp = ap->a_dvp;
1663 struct componentname *cnp = ap->a_cnp;
1664 struct nfsnode *np = VTONFS(vp);
1668 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1669 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1670 if (vp->v_type == VDIR)
1672 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1673 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1674 vattr.va_nlink > 1)) {
1676 * Purge the name cache so that the chance of a lookup for
1677 * the name succeeding while the remove is in progress is
1678 * minimized. Without node locking it can still happen, such
1679 * that an I/O op returns ESTALE, but since you get this if
1680 * another host removes the file..
1684 * throw away biocache buffers, mainly to avoid
1685 * unnecessary delayed writes later.
1687 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1689 if (error != EINTR && error != EIO)
1690 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1691 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1693 * Kludge City: If the first reply to the remove rpc is lost..
1694 * the reply to the retransmitted request will be ENOENT
1695 * since the file was in fact removed
1696 * Therefore, we cheat and return success.
1698 if (error == ENOENT)
1700 } else if (!np->n_sillyrename)
1701 error = nfs_sillyrename(dvp, vp, cnp);
1702 mtx_lock(&np->n_mtx);
1703 np->n_attrstamp = 0;
1704 mtx_unlock(&np->n_mtx);
1705 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1710 * nfs file remove rpc called from nfs_inactive
1713 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1716 * Make sure that the directory vnode is still valid.
1717 * XXX we should lock sp->s_dvp here.
1719 if (sp->s_dvp->v_type == VBAD)
1721 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1726 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1729 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1730 int namelen, struct ucred *cred, struct thread *td)
1732 struct nfsvattr dnfsva;
1733 struct nfsnode *dnp = VTONFS(dvp);
1734 int error = 0, dattrflag;
1736 mtx_lock(&dnp->n_mtx);
1737 dnp->n_flag |= NREMOVEINPROG;
1738 mtx_unlock(&dnp->n_mtx);
1739 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1741 mtx_lock(&dnp->n_mtx);
1742 if ((dnp->n_flag & NREMOVEWANT)) {
1743 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1744 mtx_unlock(&dnp->n_mtx);
1745 wakeup((caddr_t)dnp);
1747 dnp->n_flag &= ~NREMOVEINPROG;
1748 mtx_unlock(&dnp->n_mtx);
1751 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1752 mtx_lock(&dnp->n_mtx);
1753 dnp->n_flag |= NMODIFIED;
1755 dnp->n_attrstamp = 0;
1756 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1758 mtx_unlock(&dnp->n_mtx);
1759 if (error && NFS_ISV4(dvp))
1760 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1765 * nfs file rename call
1768 nfs_rename(struct vop_rename_args *ap)
1770 struct vnode *fvp = ap->a_fvp;
1771 struct vnode *tvp = ap->a_tvp;
1772 struct vnode *fdvp = ap->a_fdvp;
1773 struct vnode *tdvp = ap->a_tdvp;
1774 struct componentname *tcnp = ap->a_tcnp;
1775 struct componentname *fcnp = ap->a_fcnp;
1776 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1777 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1778 struct nfsv4node *newv4 = NULL;
1781 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1782 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1783 /* Check for cross-device rename */
1784 if ((fvp->v_mount != tdvp->v_mount) ||
1785 (tvp && (fvp->v_mount != tvp->v_mount))) {
1791 ncl_printf("nfs_rename: fvp == tvp (can't happen)\n");
1795 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1799 * We have to flush B_DELWRI data prior to renaming
1800 * the file. If we don't, the delayed-write buffers
1801 * can be flushed out later after the file has gone stale
1802 * under NFSV3. NFSV2 does not have this problem because
1803 * ( as far as I can tell ) it flushes dirty buffers more
1806 * Skip the rename operation if the fsync fails, this can happen
1807 * due to the server's volume being full, when we pushed out data
1808 * that was written back to our cache earlier. Not checking for
1809 * this condition can result in potential (silent) data loss.
1811 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1812 NFSVOPUNLOCK(fvp, 0);
1814 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1819 * If the tvp exists and is in use, sillyrename it before doing the
1820 * rename of the new file over it.
1821 * XXX Can't sillyrename a directory.
1823 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1824 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1829 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1830 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1833 if (error == 0 && NFS_ISV4(tdvp)) {
1835 * For NFSv4, check to see if it is the same name and
1836 * replace the name, if it is different.
1838 MALLOC(newv4, struct nfsv4node *,
1839 sizeof (struct nfsv4node) +
1840 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1841 M_NFSV4NODE, M_WAITOK);
1842 mtx_lock(&tdnp->n_mtx);
1843 mtx_lock(&fnp->n_mtx);
1844 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1845 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1846 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1847 tcnp->cn_namelen) ||
1848 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1849 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1850 tdnp->n_fhp->nfh_len))) {
1852 { char nnn[100]; int nnnl;
1853 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1854 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1856 printf("ren replace=%s\n",nnn);
1859 FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1862 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1863 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1864 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1865 tdnp->n_fhp->nfh_len);
1866 NFSBCOPY(tcnp->cn_nameptr,
1867 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1869 mtx_unlock(&tdnp->n_mtx);
1870 mtx_unlock(&fnp->n_mtx);
1872 FREE((caddr_t)newv4, M_NFSV4NODE);
1875 if (fvp->v_type == VDIR) {
1876 if (tvp != NULL && tvp->v_type == VDIR)
1891 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1893 if (error == ENOENT)
1899 * nfs file rename rpc called from nfs_remove() above
1902 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1903 struct sillyrename *sp)
1906 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1907 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1912 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1915 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1916 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1917 int tnamelen, struct ucred *cred, struct thread *td)
1919 struct nfsvattr fnfsva, tnfsva;
1920 struct nfsnode *fdnp = VTONFS(fdvp);
1921 struct nfsnode *tdnp = VTONFS(tdvp);
1922 int error = 0, fattrflag, tattrflag;
1924 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1925 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1926 &tattrflag, NULL, NULL);
1927 mtx_lock(&fdnp->n_mtx);
1928 fdnp->n_flag |= NMODIFIED;
1929 if (fattrflag != 0) {
1930 mtx_unlock(&fdnp->n_mtx);
1931 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1933 fdnp->n_attrstamp = 0;
1934 mtx_unlock(&fdnp->n_mtx);
1935 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1937 mtx_lock(&tdnp->n_mtx);
1938 tdnp->n_flag |= NMODIFIED;
1939 if (tattrflag != 0) {
1940 mtx_unlock(&tdnp->n_mtx);
1941 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1943 tdnp->n_attrstamp = 0;
1944 mtx_unlock(&tdnp->n_mtx);
1945 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1947 if (error && NFS_ISV4(fdvp))
1948 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1953 * nfs hard link create call
1956 nfs_link(struct vop_link_args *ap)
1958 struct vnode *vp = ap->a_vp;
1959 struct vnode *tdvp = ap->a_tdvp;
1960 struct componentname *cnp = ap->a_cnp;
1961 struct nfsnode *np, *tdnp;
1962 struct nfsvattr nfsva, dnfsva;
1963 int error = 0, attrflag, dattrflag;
1966 * Push all writes to the server, so that the attribute cache
1967 * doesn't get "out of sync" with the server.
1968 * XXX There should be a better way!
1970 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1972 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1973 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1975 tdnp = VTONFS(tdvp);
1976 mtx_lock(&tdnp->n_mtx);
1977 tdnp->n_flag |= NMODIFIED;
1978 if (dattrflag != 0) {
1979 mtx_unlock(&tdnp->n_mtx);
1980 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1982 tdnp->n_attrstamp = 0;
1983 mtx_unlock(&tdnp->n_mtx);
1984 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1987 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1990 mtx_lock(&np->n_mtx);
1991 np->n_attrstamp = 0;
1992 mtx_unlock(&np->n_mtx);
1993 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1996 * If negative lookup caching is enabled, I might as well
1997 * add an entry for this node. Not necessary for correctness,
1998 * but if negative caching is enabled, then the system
1999 * must care about lookup caching hit rate, so...
2001 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2002 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2003 cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2005 if (error && NFS_ISV4(vp))
2006 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2012 * nfs symbolic link create call
2015 nfs_symlink(struct vop_symlink_args *ap)
2017 struct vnode *dvp = ap->a_dvp;
2018 struct vattr *vap = ap->a_vap;
2019 struct componentname *cnp = ap->a_cnp;
2020 struct nfsvattr nfsva, dnfsva;
2022 struct nfsnode *np = NULL, *dnp;
2023 struct vnode *newvp = NULL;
2024 int error = 0, attrflag, dattrflag, ret;
2026 vap->va_type = VLNK;
2027 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2028 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2029 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2031 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2032 &np, NULL, LK_EXCLUSIVE);
2038 if (newvp != NULL) {
2040 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2042 } else if (!error) {
2044 * If we do not have an error and we could not extract the
2045 * newvp from the response due to the request being NFSv2, we
2046 * have to do a lookup in order to obtain a newvp to return.
2048 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2049 cnp->cn_cred, cnp->cn_thread, &np);
2057 error = nfscl_maperr(cnp->cn_thread, error,
2058 vap->va_uid, vap->va_gid);
2064 mtx_lock(&dnp->n_mtx);
2065 dnp->n_flag |= NMODIFIED;
2066 if (dattrflag != 0) {
2067 mtx_unlock(&dnp->n_mtx);
2068 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2070 dnp->n_attrstamp = 0;
2071 mtx_unlock(&dnp->n_mtx);
2072 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2075 * If negative lookup caching is enabled, I might as well
2076 * add an entry for this node. Not necessary for correctness,
2077 * but if negative caching is enabled, then the system
2078 * must care about lookup caching hit rate, so...
2080 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2081 (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2082 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime, NULL);
2091 nfs_mkdir(struct vop_mkdir_args *ap)
2093 struct vnode *dvp = ap->a_dvp;
2094 struct vattr *vap = ap->a_vap;
2095 struct componentname *cnp = ap->a_cnp;
2096 struct nfsnode *np = NULL, *dnp;
2097 struct vnode *newvp = NULL;
2100 struct nfsvattr nfsva, dnfsva;
2101 int error = 0, attrflag, dattrflag, ret;
2103 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2105 vap->va_type = VDIR;
2106 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2107 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2108 &attrflag, &dattrflag, NULL);
2110 mtx_lock(&dnp->n_mtx);
2111 dnp->n_flag |= NMODIFIED;
2112 if (dattrflag != 0) {
2113 mtx_unlock(&dnp->n_mtx);
2114 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2116 dnp->n_attrstamp = 0;
2117 mtx_unlock(&dnp->n_mtx);
2118 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2121 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2122 &np, NULL, LK_EXCLUSIVE);
2126 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2131 if (!error && newvp == NULL) {
2132 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2133 cnp->cn_cred, cnp->cn_thread, &np);
2136 if (newvp->v_type != VDIR)
2144 error = nfscl_maperr(cnp->cn_thread, error,
2145 vap->va_uid, vap->va_gid);
2148 * If negative lookup caching is enabled, I might as well
2149 * add an entry for this node. Not necessary for correctness,
2150 * but if negative caching is enabled, then the system
2151 * must care about lookup caching hit rate, so...
2153 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2154 (cnp->cn_flags & MAKEENTRY) &&
2155 attrflag != 0 && dattrflag != 0)
2156 cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2164 * nfs remove directory call
2167 nfs_rmdir(struct vop_rmdir_args *ap)
2169 struct vnode *vp = ap->a_vp;
2170 struct vnode *dvp = ap->a_dvp;
2171 struct componentname *cnp = ap->a_cnp;
2172 struct nfsnode *dnp;
2173 struct nfsvattr dnfsva;
2174 int error, dattrflag;
2178 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2179 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2181 mtx_lock(&dnp->n_mtx);
2182 dnp->n_flag |= NMODIFIED;
2183 if (dattrflag != 0) {
2184 mtx_unlock(&dnp->n_mtx);
2185 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2187 dnp->n_attrstamp = 0;
2188 mtx_unlock(&dnp->n_mtx);
2189 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2194 if (error && NFS_ISV4(dvp))
2195 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2198 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2200 if (error == ENOENT)
2209 nfs_readdir(struct vop_readdir_args *ap)
2211 struct vnode *vp = ap->a_vp;
2212 struct nfsnode *np = VTONFS(vp);
2213 struct uio *uio = ap->a_uio;
2214 ssize_t tresid, left;
2218 if (ap->a_eofflag != NULL)
2220 if (vp->v_type != VDIR)
2224 * First, check for hit on the EOF offset cache
2226 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2227 (np->n_flag & NMODIFIED) == 0) {
2228 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2229 mtx_lock(&np->n_mtx);
2230 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2231 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2232 mtx_unlock(&np->n_mtx);
2233 NFSINCRGLOBAL(newnfsstats.direofcache_hits);
2234 if (ap->a_eofflag != NULL)
2238 mtx_unlock(&np->n_mtx);
2243 * NFS always guarantees that directory entries don't straddle
2244 * DIRBLKSIZ boundaries. As such, we need to limit the size
2245 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2248 left = uio->uio_resid % DIRBLKSIZ;
2249 if (left == uio->uio_resid)
2251 uio->uio_resid -= left;
2254 * Call ncl_bioread() to do the real work.
2256 tresid = uio->uio_resid;
2257 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2259 if (!error && uio->uio_resid == tresid) {
2260 NFSINCRGLOBAL(newnfsstats.direofcache_misses);
2261 if (ap->a_eofflag != NULL)
2265 /* Add the partial DIRBLKSIZ (left) back in. */
2266 uio->uio_resid += left;
2272 * Called from below the buffer cache by ncl_doio().
2275 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2278 struct nfsvattr nfsva;
2279 nfsuint64 *cookiep, cookie;
2280 struct nfsnode *dnp = VTONFS(vp);
2281 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2282 int error = 0, eof, attrflag;
2284 KASSERT(uiop->uio_iovcnt == 1 &&
2285 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2286 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2287 ("nfs readdirrpc bad uio"));
2290 * If there is no cookie, assume directory was stale.
2292 ncl_dircookie_lock(dnp);
2293 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2296 ncl_dircookie_unlock(dnp);
2298 ncl_dircookie_unlock(dnp);
2299 return (NFSERR_BAD_COOKIE);
2302 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2303 (void)ncl_fsinfo(nmp, vp, cred, td);
2305 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2306 &attrflag, &eof, NULL);
2308 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2312 * We are now either at the end of the directory or have filled
2316 dnp->n_direofoffset = uiop->uio_offset;
2318 if (uiop->uio_resid > 0)
2319 ncl_printf("EEK! readdirrpc resid > 0\n");
2320 ncl_dircookie_lock(dnp);
2321 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2323 ncl_dircookie_unlock(dnp);
2325 } else if (NFS_ISV4(vp)) {
2326 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2332 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2335 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2338 struct nfsvattr nfsva;
2339 nfsuint64 *cookiep, cookie;
2340 struct nfsnode *dnp = VTONFS(vp);
2341 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2342 int error = 0, attrflag, eof;
2344 KASSERT(uiop->uio_iovcnt == 1 &&
2345 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2346 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2347 ("nfs readdirplusrpc bad uio"));
2350 * If there is no cookie, assume directory was stale.
2352 ncl_dircookie_lock(dnp);
2353 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2356 ncl_dircookie_unlock(dnp);
2358 ncl_dircookie_unlock(dnp);
2359 return (NFSERR_BAD_COOKIE);
2362 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2363 (void)ncl_fsinfo(nmp, vp, cred, td);
2364 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2365 &attrflag, &eof, NULL);
2367 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2371 * We are now either at end of the directory or have filled the
2375 dnp->n_direofoffset = uiop->uio_offset;
2377 if (uiop->uio_resid > 0)
2378 ncl_printf("EEK! readdirplusrpc resid > 0\n");
2379 ncl_dircookie_lock(dnp);
2380 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2382 ncl_dircookie_unlock(dnp);
2384 } else if (NFS_ISV4(vp)) {
2385 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2391 * Silly rename. To make the NFS filesystem that is stateless look a little
2392 * more like the "ufs" a remove of an active vnode is translated to a rename
2393 * to a funny looking filename that is removed by nfs_inactive on the
2394 * nfsnode. There is the potential for another process on a different client
2395 * to create the same funny name between the nfs_lookitup() fails and the
2396 * nfs_rename() completes, but...
2399 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2401 struct sillyrename *sp;
2405 unsigned int lticks;
2409 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2410 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2411 M_NEWNFSREQ, M_WAITOK);
2412 sp->s_cred = crhold(cnp->cn_cred);
2417 * Fudge together a funny name.
2418 * Changing the format of the funny name to accomodate more
2419 * sillynames per directory.
2420 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2421 * CPU ticks since boot.
2423 pid = cnp->cn_thread->td_proc->p_pid;
2424 lticks = (unsigned int)ticks;
2426 sp->s_namlen = sprintf(sp->s_name,
2427 ".nfs.%08x.%04x4.4", lticks,
2429 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2430 cnp->cn_thread, NULL))
2434 error = nfs_renameit(dvp, vp, cnp, sp);
2437 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2438 cnp->cn_thread, &np);
2439 np->n_sillyrename = sp;
2444 free((caddr_t)sp, M_NEWNFSREQ);
2449 * Look up a file name and optionally either update the file handle or
2450 * allocate an nfsnode, depending on the value of npp.
2451 * npp == NULL --> just do the lookup
2452 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2454 * *npp != NULL --> update the file handle in the vnode
2457 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2458 struct thread *td, struct nfsnode **npp)
2460 struct vnode *newvp = NULL, *vp;
2461 struct nfsnode *np, *dnp = VTONFS(dvp);
2462 struct nfsfh *nfhp, *onfhp;
2463 struct nfsvattr nfsva, dnfsva;
2464 struct componentname cn;
2465 int error = 0, attrflag, dattrflag;
2468 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2469 &nfhp, &attrflag, &dattrflag, NULL);
2471 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2472 if (npp && !error) {
2477 * For NFSv4, check to see if it is the same name and
2478 * replace the name, if it is different.
2480 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2481 (np->n_v4->n4_namelen != len ||
2482 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2483 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2484 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2485 dnp->n_fhp->nfh_len))) {
2487 { char nnn[100]; int nnnl;
2488 nnnl = (len < 100) ? len : 99;
2489 bcopy(name, nnn, nnnl);
2491 printf("replace=%s\n",nnn);
2494 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2495 MALLOC(np->n_v4, struct nfsv4node *,
2496 sizeof (struct nfsv4node) +
2497 dnp->n_fhp->nfh_len + len - 1,
2498 M_NFSV4NODE, M_WAITOK);
2499 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2500 np->n_v4->n4_namelen = len;
2501 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2502 dnp->n_fhp->nfh_len);
2503 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2505 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2509 * Rehash node for new file handle.
2511 vfs_hash_rehash(vp, hash);
2514 FREE((caddr_t)onfhp, M_NFSFH);
2516 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2517 FREE((caddr_t)nfhp, M_NFSFH);
2521 cn.cn_nameptr = name;
2522 cn.cn_namelen = len;
2523 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2524 &np, NULL, LK_EXCLUSIVE);
2529 if (!attrflag && *npp == NULL) {
2537 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2540 if (npp && *npp == NULL) {
2551 if (error && NFS_ISV4(dvp))
2552 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2557 * Nfs Version 3 and 4 commit rpc
2560 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2563 struct nfsvattr nfsva;
2564 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2565 int error, attrflag;
2567 mtx_lock(&nmp->nm_mtx);
2568 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2569 mtx_unlock(&nmp->nm_mtx);
2572 mtx_unlock(&nmp->nm_mtx);
2573 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)
2592 struct buf *bp = ap->a_bp;
2595 KASSERT(!(bp->b_flags & B_DONE),
2596 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2597 BUF_ASSERT_HELD(bp);
2599 if (bp->b_iocmd == BIO_READ)
2605 * If the op is asynchronous and an i/o daemon is waiting
2606 * queue the request, wake it up and wait for completion
2607 * otherwise just do it ourselves.
2609 if ((bp->b_flags & B_ASYNC) == 0 ||
2610 ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2611 (void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
2616 * fsync vnode op. Just call ncl_flush() with commit == 1.
2620 nfs_fsync(struct vop_fsync_args *ap)
2623 if (ap->a_vp->v_type != VREG) {
2625 * For NFS, metadata is changed synchronously on the server,
2626 * so there is nothing to flush. Also, ncl_flush() clears
2627 * the NMODIFIED flag and that shouldn't be done here for
2632 return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
2636 * Flush all the blocks associated with a vnode.
2637 * Walk through the buffer pool and push any dirty pages
2638 * associated with the vnode.
2639 * If the called_from_renewthread argument is TRUE, it has been called
2640 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2641 * waiting for a buffer write to complete.
2644 ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2645 int commit, int called_from_renewthread)
2647 struct nfsnode *np = VTONFS(vp);
2651 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2652 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2653 int passone = 1, trycnt = 0;
2654 u_quad_t off, endoff, toff;
2655 struct ucred* wcred = NULL;
2656 struct buf **bvec = NULL;
2658 #ifndef NFS_COMMITBVECSIZ
2659 #define NFS_COMMITBVECSIZ 20
2661 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2662 int bvecsize = 0, bveccount;
2664 if (called_from_renewthread != 0)
2666 if (nmp->nm_flag & NFSMNT_INT)
2672 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2673 * server, but has not been committed to stable storage on the server
2674 * yet. On the first pass, the byte range is worked out and the commit
2675 * rpc is done. On the second pass, ncl_writebp() is called to do the
2682 if (NFS_ISV34(vp) && commit) {
2683 if (bvec != NULL && bvec != bvec_on_stack)
2686 * Count up how many buffers waiting for a commit.
2690 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2691 if (!BUF_ISLOCKED(bp) &&
2692 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2693 == (B_DELWRI | B_NEEDCOMMIT))
2697 * Allocate space to remember the list of bufs to commit. It is
2698 * important to use M_NOWAIT here to avoid a race with nfs_write.
2699 * If we can't get memory (for whatever reason), we will end up
2700 * committing the buffers one-by-one in the loop below.
2702 if (bveccount > NFS_COMMITBVECSIZ) {
2704 * Release the vnode interlock to avoid a lock
2708 bvec = (struct buf **)
2709 malloc(bveccount * sizeof(struct buf *),
2713 bvec = bvec_on_stack;
2714 bvecsize = NFS_COMMITBVECSIZ;
2716 bvecsize = bveccount;
2718 bvec = bvec_on_stack;
2719 bvecsize = NFS_COMMITBVECSIZ;
2721 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2722 if (bvecpos >= bvecsize)
2724 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2725 nbp = TAILQ_NEXT(bp, b_bobufs);
2728 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2729 (B_DELWRI | B_NEEDCOMMIT)) {
2731 nbp = TAILQ_NEXT(bp, b_bobufs);
2737 * Work out if all buffers are using the same cred
2738 * so we can deal with them all with one commit.
2740 * NOTE: we are not clearing B_DONE here, so we have
2741 * to do it later on in this routine if we intend to
2742 * initiate I/O on the bp.
2744 * Note: to avoid loopback deadlocks, we do not
2745 * assign b_runningbufspace.
2748 wcred = bp->b_wcred;
2749 else if (wcred != bp->b_wcred)
2751 vfs_busy_pages(bp, 1);
2755 * bp is protected by being locked, but nbp is not
2756 * and vfs_busy_pages() may sleep. We have to
2759 nbp = TAILQ_NEXT(bp, b_bobufs);
2762 * A list of these buffers is kept so that the
2763 * second loop knows which buffers have actually
2764 * been committed. This is necessary, since there
2765 * may be a race between the commit rpc and new
2766 * uncommitted writes on the file.
2768 bvec[bvecpos++] = bp;
2769 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2773 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2781 * Commit data on the server, as required.
2782 * If all bufs are using the same wcred, then use that with
2783 * one call for all of them, otherwise commit each one
2786 if (wcred != NOCRED)
2787 retv = ncl_commit(vp, off, (int)(endoff - off),
2791 for (i = 0; i < bvecpos; i++) {
2794 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2796 size = (u_quad_t)(bp->b_dirtyend
2798 retv = ncl_commit(vp, off, (int)size,
2804 if (retv == NFSERR_STALEWRITEVERF)
2805 ncl_clearcommit(vp->v_mount);
2808 * Now, either mark the blocks I/O done or mark the
2809 * blocks dirty, depending on whether the commit
2812 for (i = 0; i < bvecpos; i++) {
2814 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2817 * Error, leave B_DELWRI intact
2819 vfs_unbusy_pages(bp);
2823 * Success, remove B_DELWRI ( bundirty() ).
2825 * b_dirtyoff/b_dirtyend seem to be NFS
2826 * specific. We should probably move that
2827 * into bundirty(). XXX
2830 bp->b_flags |= B_ASYNC;
2832 bp->b_flags &= ~B_DONE;
2833 bp->b_ioflags &= ~BIO_ERROR;
2834 bp->b_dirtyoff = bp->b_dirtyend = 0;
2841 * Start/do any write(s) that are required.
2845 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2846 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2847 if (waitfor != MNT_WAIT || passone)
2850 error = BUF_TIMELOCK(bp,
2851 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2852 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
2857 if (error == ENOLCK) {
2861 if (called_from_renewthread != 0) {
2863 * Return EIO so the flush will be retried
2869 if (newnfs_sigintr(nmp, td)) {
2873 if (slpflag == PCATCH) {
2879 if ((bp->b_flags & B_DELWRI) == 0)
2880 panic("nfs_fsync: not dirty");
2881 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2887 if (passone || !commit)
2888 bp->b_flags |= B_ASYNC;
2890 bp->b_flags |= B_ASYNC;
2892 if (newnfs_sigintr(nmp, td)) {
2903 if (waitfor == MNT_WAIT) {
2904 while (bo->bo_numoutput) {
2905 error = bufobj_wwait(bo, slpflag, slptimeo);
2908 if (called_from_renewthread != 0) {
2910 * Return EIO so that the flush will be
2916 error = newnfs_sigintr(nmp, td);
2919 if (slpflag == PCATCH) {
2926 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2931 * Wait for all the async IO requests to drain
2934 mtx_lock(&np->n_mtx);
2935 while (np->n_directio_asyncwr > 0) {
2936 np->n_flag |= NFSYNCWAIT;
2937 error = newnfs_msleep(td, &np->n_directio_asyncwr,
2938 &np->n_mtx, slpflag | (PRIBIO + 1),
2941 if (newnfs_sigintr(nmp, td)) {
2942 mtx_unlock(&np->n_mtx);
2948 mtx_unlock(&np->n_mtx);
2951 if (NFSHASPNFS(nmp)) {
2952 nfscl_layoutcommit(vp, td);
2954 * Invalidate the attribute cache, since writes to a DS
2955 * won't update the size attribute.
2957 mtx_lock(&np->n_mtx);
2958 np->n_attrstamp = 0;
2960 mtx_lock(&np->n_mtx);
2961 if (np->n_flag & NWRITEERR) {
2962 error = np->n_error;
2963 np->n_flag &= ~NWRITEERR;
2965 if (commit && bo->bo_dirty.bv_cnt == 0 &&
2966 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2967 np->n_flag &= ~NMODIFIED;
2968 mtx_unlock(&np->n_mtx);
2970 if (bvec != NULL && bvec != bvec_on_stack)
2972 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2973 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2974 np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2975 /* try, try again... */
2980 printf("try%d\n", trycnt);
2987 * NFS advisory byte-level locks.
2990 nfs_advlock(struct vop_advlock_args *ap)
2992 struct vnode *vp = ap->a_vp;
2994 struct nfsnode *np = VTONFS(ap->a_vp);
2995 struct proc *p = (struct proc *)ap->a_id;
2996 struct thread *td = curthread; /* XXX */
2998 int ret, error = EOPNOTSUPP;
3001 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3002 if (vp->v_type != VREG)
3004 if ((ap->a_flags & F_POSIX) != 0)
3007 cred = td->td_ucred;
3008 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3009 if (vp->v_iflag & VI_DOOMED) {
3010 NFSVOPUNLOCK(vp, 0);
3015 * If this is unlocking a write locked region, flush and
3016 * commit them before unlocking. This is required by
3017 * RFC3530 Sec. 9.3.2.
3019 if (ap->a_op == F_UNLCK &&
3020 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3022 (void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
3025 * Loop around doing the lock op, while a blocking lock
3026 * must wait for the lock op to succeed.
3029 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3030 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3031 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3032 ap->a_op == F_SETLK) {
3033 NFSVOPUNLOCK(vp, 0);
3034 error = nfs_catnap(PZERO | PCATCH, ret,
3038 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3039 if (vp->v_iflag & VI_DOOMED) {
3040 NFSVOPUNLOCK(vp, 0);
3044 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3045 ap->a_op == F_SETLK);
3046 if (ret == NFSERR_DENIED) {
3047 NFSVOPUNLOCK(vp, 0);
3049 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3050 NFSVOPUNLOCK(vp, 0);
3052 } else if (ret != 0) {
3053 NFSVOPUNLOCK(vp, 0);
3058 * Now, if we just got a lock, invalidate data in the buffer
3059 * cache, as required, so that the coherency conforms with
3060 * RFC3530 Sec. 9.3.2.
3062 if (ap->a_op == F_SETLK) {
3063 if ((np->n_flag & NMODIFIED) == 0) {
3064 np->n_attrstamp = 0;
3065 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3066 ret = VOP_GETATTR(vp, &va, cred);
3068 if ((np->n_flag & NMODIFIED) || ret ||
3069 np->n_change != va.va_filerev) {
3070 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3071 np->n_attrstamp = 0;
3072 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3073 ret = VOP_GETATTR(vp, &va, cred);
3075 np->n_mtime = va.va_mtime;
3076 np->n_change = va.va_filerev;
3079 /* Mark that a file lock has been acquired. */
3080 mtx_lock(&np->n_mtx);
3081 np->n_flag |= NHASBEENLOCKED;
3082 mtx_unlock(&np->n_mtx);
3084 NFSVOPUNLOCK(vp, 0);
3086 } else if (!NFS_ISV4(vp)) {
3087 error = NFSVOPLOCK(vp, LK_SHARED);
3090 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3091 size = VTONFS(vp)->n_size;
3092 NFSVOPUNLOCK(vp, 0);
3093 error = lf_advlock(ap, &(vp->v_lockf), size);
3095 if (nfs_advlock_p != NULL)
3096 error = nfs_advlock_p(ap);
3098 NFSVOPUNLOCK(vp, 0);
3102 if (error == 0 && ap->a_op == F_SETLK) {
3103 /* Mark that a file lock has been acquired. */
3104 mtx_lock(&np->n_mtx);
3105 np->n_flag |= NHASBEENLOCKED;
3106 mtx_unlock(&np->n_mtx);
3113 * NFS advisory byte-level locks.
3116 nfs_advlockasync(struct vop_advlockasync_args *ap)
3118 struct vnode *vp = ap->a_vp;
3123 return (EOPNOTSUPP);
3124 error = NFSVOPLOCK(vp, LK_SHARED);
3127 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3128 size = VTONFS(vp)->n_size;
3129 NFSVOPUNLOCK(vp, 0);
3130 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3132 NFSVOPUNLOCK(vp, 0);
3139 * Print out the contents of an nfsnode.
3142 nfs_print(struct vop_print_args *ap)
3144 struct vnode *vp = ap->a_vp;
3145 struct nfsnode *np = VTONFS(vp);
3147 ncl_printf("\tfileid %ld fsid 0x%x",
3148 np->n_vattr.na_fileid, np->n_vattr.na_fsid);
3149 if (vp->v_type == VFIFO)
3156 * This is the "real" nfs::bwrite(struct buf*).
3157 * We set B_CACHE if this is a VMIO buffer.
3160 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3163 int oldflags = bp->b_flags;
3169 BUF_ASSERT_HELD(bp);
3171 if (bp->b_flags & B_INVAL) {
3176 bp->b_flags |= B_CACHE;
3179 * Undirty the bp. We will redirty it later if the I/O fails.
3184 bp->b_flags &= ~B_DONE;
3185 bp->b_ioflags &= ~BIO_ERROR;
3186 bp->b_iocmd = BIO_WRITE;
3188 bufobj_wref(bp->b_bufobj);
3189 curthread->td_ru.ru_oublock++;
3193 * Note: to avoid loopback deadlocks, we do not
3194 * assign b_runningbufspace.
3196 vfs_busy_pages(bp, 1);
3199 bp->b_iooffset = dbtob(bp->b_blkno);
3202 if( (oldflags & B_ASYNC) == 0) {
3203 int rtval = bufwait(bp);
3205 if (oldflags & B_DELWRI) {
3218 * nfs special file access vnode op.
3219 * Essentially just get vattr and then imitate iaccess() since the device is
3220 * local to the client.
3223 nfsspec_access(struct vop_access_args *ap)
3226 struct ucred *cred = ap->a_cred;
3227 struct vnode *vp = ap->a_vp;
3228 accmode_t accmode = ap->a_accmode;
3233 * Disallow write attempts on filesystems mounted read-only;
3234 * unless the file is a socket, fifo, or a block or character
3235 * device resident on the filesystem.
3237 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3238 switch (vp->v_type) {
3248 error = VOP_GETATTR(vp, vap, cred);
3251 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3252 accmode, cred, NULL);
3258 * Read wrapper for fifos.
3261 nfsfifo_read(struct vop_read_args *ap)
3263 struct nfsnode *np = VTONFS(ap->a_vp);
3269 mtx_lock(&np->n_mtx);
3271 vfs_timestamp(&np->n_atim);
3272 mtx_unlock(&np->n_mtx);
3273 error = fifo_specops.vop_read(ap);
3278 * Write wrapper for fifos.
3281 nfsfifo_write(struct vop_write_args *ap)
3283 struct nfsnode *np = VTONFS(ap->a_vp);
3288 mtx_lock(&np->n_mtx);
3290 vfs_timestamp(&np->n_mtim);
3291 mtx_unlock(&np->n_mtx);
3292 return(fifo_specops.vop_write(ap));
3296 * Close wrapper for fifos.
3298 * Update the times on the nfsnode then do fifo close.
3301 nfsfifo_close(struct vop_close_args *ap)
3303 struct vnode *vp = ap->a_vp;
3304 struct nfsnode *np = VTONFS(vp);
3308 mtx_lock(&np->n_mtx);
3309 if (np->n_flag & (NACC | NUPD)) {
3311 if (np->n_flag & NACC)
3313 if (np->n_flag & NUPD)
3316 if (vrefcnt(vp) == 1 &&
3317 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3319 if (np->n_flag & NACC)
3320 vattr.va_atime = np->n_atim;
3321 if (np->n_flag & NUPD)
3322 vattr.va_mtime = np->n_mtim;
3323 mtx_unlock(&np->n_mtx);
3324 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3328 mtx_unlock(&np->n_mtx);
3330 return (fifo_specops.vop_close(ap));
3334 * Just call ncl_writebp() with the force argument set to 1.
3336 * NOTE: B_DONE may or may not be set in a_bp on call.
3339 nfs_bwrite(struct buf *bp)
3342 return (ncl_writebp(bp, 1, curthread));
3345 struct buf_ops buf_ops_newnfs = {
3346 .bop_name = "buf_ops_nfs",
3347 .bop_write = nfs_bwrite,
3348 .bop_strategy = bufstrategy,
3349 .bop_sync = bufsync,
3350 .bop_bdflush = bufbdflush,
3354 * Cloned from vop_stdlock(), and then the ugly hack added.
3357 nfs_lock1(struct vop_lock1_args *ap)
3359 struct vnode *vp = ap->a_vp;
3363 * Since vfs_hash_get() calls vget() and it will no longer work
3364 * for FreeBSD8 with flags == 0, I can only think of this horrible
3365 * hack to work around it. I call vfs_hash_get() with LK_EXCLOTHER
3366 * and then handle it here. All I want for this case is a v_usecount
3367 * on the vnode to use for recovery, while another thread might
3368 * hold a lock on the vnode. I have the other threads blocked, so
3369 * there isn't any race problem.
3371 if ((ap->a_flags & LK_TYPE_MASK) == LK_EXCLOTHER) {
3372 if ((ap->a_flags & LK_INTERLOCK) == 0)
3374 if ((vp->v_iflag & VI_DOOMED))
3379 return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
3380 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
3385 nfs_getacl(struct vop_getacl_args *ap)
3389 if (ap->a_type != ACL_TYPE_NFS4)
3390 return (EOPNOTSUPP);
3391 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3393 if (error > NFSERR_STALE) {
3394 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3401 nfs_setacl(struct vop_setacl_args *ap)
3405 if (ap->a_type != ACL_TYPE_NFS4)
3406 return (EOPNOTSUPP);
3407 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3409 if (error > NFSERR_STALE) {
3410 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3417 * Return POSIX pathconf information applicable to nfs filesystems.
3420 nfs_pathconf(struct vop_pathconf_args *ap)
3422 struct nfsv3_pathconf pc;
3423 struct nfsvattr nfsva;
3424 struct vnode *vp = ap->a_vp;
3425 struct thread *td = curthread;
3426 int attrflag, error;
3428 if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
3429 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3430 ap->a_name == _PC_NO_TRUNC)) ||
3431 (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
3433 * Since only the above 4 a_names are returned by the NFSv3
3434 * Pathconf RPC, there is no point in doing it for others.
3435 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
3436 * be used for _PC_NFS4_ACL as well.
3438 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3441 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3447 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3450 pc.pc_linkmax = LINK_MAX;
3451 pc.pc_namemax = NFS_MAXNAMLEN;
3453 pc.pc_chownrestricted = 1;
3454 pc.pc_caseinsensitive = 0;
3455 pc.pc_casepreserving = 1;
3458 switch (ap->a_name) {
3460 *ap->a_retval = pc.pc_linkmax;
3463 *ap->a_retval = pc.pc_namemax;
3466 *ap->a_retval = PATH_MAX;
3469 *ap->a_retval = PIPE_BUF;
3471 case _PC_CHOWN_RESTRICTED:
3472 *ap->a_retval = pc.pc_chownrestricted;
3475 *ap->a_retval = pc.pc_notrunc;
3477 case _PC_ACL_EXTENDED:
3481 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3482 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3487 case _PC_ACL_PATH_MAX:
3489 *ap->a_retval = ACL_MAX_ENTRIES;
3493 case _PC_MAC_PRESENT:
3497 /* _PC_ASYNC_IO should have been handled by upper layers. */
3498 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
3507 case _PC_ALLOC_SIZE_MIN:
3508 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3510 case _PC_FILESIZEBITS:
3516 case _PC_REC_INCR_XFER_SIZE:
3517 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3519 case _PC_REC_MAX_XFER_SIZE:
3520 *ap->a_retval = -1; /* means ``unlimited'' */
3522 case _PC_REC_MIN_XFER_SIZE:
3523 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3525 case _PC_REC_XFER_ALIGN:
3526 *ap->a_retval = PAGE_SIZE;
3528 case _PC_SYMLINK_MAX:
3529 *ap->a_retval = NFS_MAXPATHLEN;