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
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
49 #include <sys/mount.h>
53 #include <sys/malloc.h>
55 #include <sys/namei.h>
56 #include <sys/socket.h>
57 #include <sys/vnode.h>
58 #include <sys/dirent.h>
59 #include <sys/fcntl.h>
60 #include <sys/lockf.h>
62 #include <sys/sysctl.h>
63 #include <sys/signalvar.h>
66 #include <vm/vm_object.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>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
79 #include <nfs/nfs_lock.h>
85 extern struct nfsstats newnfsstats;
86 extern int nfsrv_useacl;
87 MALLOC_DECLARE(M_NEWNFSREQ);
90 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
91 * calls are not in getblk() and brelse() so that they would not be necessary
95 #define vfs_busy_pages(bp, f)
98 static vop_read_t nfsfifo_read;
99 static vop_write_t nfsfifo_write;
100 static vop_close_t nfsfifo_close;
101 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
103 static vop_lookup_t nfs_lookup;
104 static vop_create_t nfs_create;
105 static vop_mknod_t nfs_mknod;
106 static vop_open_t nfs_open;
107 static vop_pathconf_t nfs_pathconf;
108 static vop_close_t nfs_close;
109 static vop_access_t nfs_access;
110 static vop_getattr_t nfs_getattr;
111 static vop_setattr_t nfs_setattr;
112 static vop_read_t nfs_read;
113 static vop_fsync_t nfs_fsync;
114 static vop_remove_t nfs_remove;
115 static vop_link_t nfs_link;
116 static vop_rename_t nfs_rename;
117 static vop_mkdir_t nfs_mkdir;
118 static vop_rmdir_t nfs_rmdir;
119 static vop_symlink_t nfs_symlink;
120 static vop_readdir_t nfs_readdir;
121 static vop_strategy_t nfs_strategy;
122 static vop_lock1_t nfs_lock1;
123 static int nfs_lookitup(struct vnode *, char *, int,
124 struct ucred *, struct thread *, struct nfsnode **);
125 static int nfs_sillyrename(struct vnode *, struct vnode *,
126 struct componentname *);
127 static vop_access_t nfsspec_access;
128 static vop_readlink_t nfs_readlink;
129 static vop_print_t nfs_print;
130 static vop_advlock_t nfs_advlock;
131 static vop_advlockasync_t nfs_advlockasync;
132 static vop_getacl_t nfs_getacl;
133 static vop_setacl_t nfs_setacl;
136 * Global vfs data structures for nfs
138 struct vop_vector newnfs_vnodeops = {
139 .vop_default = &default_vnodeops,
140 .vop_access = nfs_access,
141 .vop_advlock = nfs_advlock,
142 .vop_advlockasync = nfs_advlockasync,
143 .vop_close = nfs_close,
144 .vop_create = nfs_create,
145 .vop_fsync = nfs_fsync,
146 .vop_getattr = nfs_getattr,
147 .vop_getpages = ncl_getpages,
148 .vop_putpages = ncl_putpages,
149 .vop_inactive = ncl_inactive,
150 .vop_link = nfs_link,
151 .vop_lock1 = nfs_lock1,
152 .vop_lookup = nfs_lookup,
153 .vop_mkdir = nfs_mkdir,
154 .vop_mknod = nfs_mknod,
155 .vop_open = nfs_open,
156 .vop_pathconf = nfs_pathconf,
157 .vop_print = nfs_print,
158 .vop_read = nfs_read,
159 .vop_readdir = nfs_readdir,
160 .vop_readlink = nfs_readlink,
161 .vop_reclaim = ncl_reclaim,
162 .vop_remove = nfs_remove,
163 .vop_rename = nfs_rename,
164 .vop_rmdir = nfs_rmdir,
165 .vop_setattr = nfs_setattr,
166 .vop_strategy = nfs_strategy,
167 .vop_symlink = nfs_symlink,
168 .vop_write = ncl_write,
169 .vop_getacl = nfs_getacl,
170 .vop_setacl = nfs_setacl,
173 struct vop_vector newnfs_fifoops = {
174 .vop_default = &fifo_specops,
175 .vop_access = nfsspec_access,
176 .vop_close = nfsfifo_close,
177 .vop_fsync = nfs_fsync,
178 .vop_getattr = nfs_getattr,
179 .vop_inactive = ncl_inactive,
180 .vop_print = nfs_print,
181 .vop_read = nfsfifo_read,
182 .vop_reclaim = ncl_reclaim,
183 .vop_setattr = nfs_setattr,
184 .vop_write = nfsfifo_write,
187 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
188 struct componentname *cnp, struct vattr *vap);
189 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
190 int namelen, struct ucred *cred, struct thread *td);
191 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
192 char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
193 char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
194 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
195 struct componentname *scnp, struct sillyrename *sp);
200 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
202 SYSCTL_DECL(_vfs_newnfs);
204 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
205 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
206 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
208 static int nfs_prime_access_cache = 0;
209 SYSCTL_INT(_vfs_newnfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
210 &nfs_prime_access_cache, 0,
211 "Prime NFS ACCESS cache when fetching attributes");
213 static int newnfs_commit_on_close = 0;
214 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
215 &newnfs_commit_on_close, 0, "write+commit on close, else only write");
217 static int nfs_clean_pages_on_close = 1;
218 SYSCTL_INT(_vfs_newnfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
219 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
221 int newnfs_directio_enable = 0;
222 SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
223 &newnfs_directio_enable, 0, "Enable NFS directio");
226 * This sysctl allows other processes to mmap a file that has been opened
227 * O_DIRECT by a process. In general, having processes mmap the file while
228 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
229 * this by default to prevent DoS attacks - to prevent a malicious user from
230 * opening up files O_DIRECT preventing other users from mmap'ing these
231 * files. "Protected" environments where stricter consistency guarantees are
232 * required can disable this knob. The process that opened the file O_DIRECT
233 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
236 int newnfs_directio_allow_mmap = 1;
237 SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
238 &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
241 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
242 &newnfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
244 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
245 &newnfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
248 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY \
249 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
250 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
254 * The list of locks after the description of the lock is the ordering
255 * of other locks acquired with the lock held.
256 * np->n_mtx : Protects the fields in the nfsnode.
258 VI_MTX (acquired indirectly)
259 * nmp->nm_mtx : Protects the fields in the nfsmount.
261 * ncl_iod_mutex : Global lock, protects shared nfsiod state.
262 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
265 * rep->r_mtx : Protects the fields in an nfsreq.
269 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
270 struct ucred *cred, u_int32_t *retmode)
272 int error = 0, attrflag, i, lrupos;
274 struct nfsnode *np = VTONFS(vp);
275 struct nfsvattr nfsva;
277 error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
280 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
283 mtx_lock(&np->n_mtx);
284 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
285 if (np->n_accesscache[i].uid == cred->cr_uid) {
286 np->n_accesscache[i].mode = rmode;
287 np->n_accesscache[i].stamp = time_second;
290 if (i > 0 && np->n_accesscache[i].stamp <
291 np->n_accesscache[lrupos].stamp)
294 if (i == NFS_ACCESSCACHESIZE) {
295 np->n_accesscache[lrupos].uid = cred->cr_uid;
296 np->n_accesscache[lrupos].mode = rmode;
297 np->n_accesscache[lrupos].stamp = time_second;
299 mtx_unlock(&np->n_mtx);
302 } else if (NFS_ISV4(vp)) {
303 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
309 * nfs access vnode op.
310 * For nfs version 2, just return ok. File accesses may fail later.
311 * For nfs version 3, use the access rpc to check accessibility. If file modes
312 * are changed on the server, accesses might still fail later.
315 nfs_access(struct vop_access_args *ap)
317 struct vnode *vp = ap->a_vp;
318 int error = 0, i, gotahit;
319 u_int32_t mode, wmode, rmode;
320 int v34 = NFS_ISV34(vp);
321 struct nfsnode *np = VTONFS(vp);
324 * Disallow write attempts on filesystems mounted read-only;
325 * unless the file is a socket, fifo, or a block or character
326 * device resident on the filesystem.
328 if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
329 VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
330 VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
331 switch (vp->v_type) {
341 * For nfs v3 or v4, check to see if we have done this recently, and if
342 * so return our cached result instead of making an ACCESS call.
343 * If not, do an access rpc, otherwise you are stuck emulating
344 * ufs_access() locally using the vattr. This may not be correct,
345 * since the server may apply other access criteria such as
346 * client uid-->server uid mapping that we do not know about.
349 if (ap->a_accmode & VREAD)
350 mode = NFSACCESS_READ;
353 if (vp->v_type != VDIR) {
354 if (ap->a_accmode & VWRITE)
355 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
356 if (ap->a_accmode & VAPPEND)
357 mode |= NFSACCESS_EXTEND;
358 if (ap->a_accmode & VEXEC)
359 mode |= NFSACCESS_EXECUTE;
360 if (ap->a_accmode & VDELETE)
361 mode |= NFSACCESS_DELETE;
363 if (ap->a_accmode & VWRITE)
364 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
365 if (ap->a_accmode & VAPPEND)
366 mode |= NFSACCESS_EXTEND;
367 if (ap->a_accmode & VEXEC)
368 mode |= NFSACCESS_LOOKUP;
369 if (ap->a_accmode & VDELETE)
370 mode |= NFSACCESS_DELETE;
371 if (ap->a_accmode & VDELETE_CHILD)
372 mode |= NFSACCESS_MODIFY;
374 /* XXX safety belt, only make blanket request if caching */
375 if (nfsaccess_cache_timeout > 0) {
376 wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
377 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
378 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
384 * Does our cached result allow us to give a definite yes to
388 mtx_lock(&np->n_mtx);
389 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
390 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
391 if (time_second < (np->n_accesscache[i].stamp
392 + nfsaccess_cache_timeout) &&
393 (np->n_accesscache[i].mode & mode) == mode) {
394 NFSINCRGLOBAL(newnfsstats.accesscache_hits);
400 mtx_unlock(&np->n_mtx);
403 * Either a no, or a don't know. Go to the wire.
405 NFSINCRGLOBAL(newnfsstats.accesscache_misses);
406 error = nfs34_access_otw(vp, wmode, ap->a_td,
409 (rmode & mode) != mode)
414 if ((error = nfsspec_access(ap)) != 0) {
418 * Attempt to prevent a mapped root from accessing a file
419 * which it shouldn't. We try to read a byte from the file
420 * if the user is root and the file is not zero length.
421 * After calling nfsspec_access, we should have the correct
424 mtx_lock(&np->n_mtx);
425 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
426 && VTONFS(vp)->n_size > 0) {
431 mtx_unlock(&np->n_mtx);
434 auio.uio_iov = &aiov;
438 auio.uio_segflg = UIO_SYSSPACE;
439 auio.uio_rw = UIO_READ;
440 auio.uio_td = ap->a_td;
442 if (vp->v_type == VREG)
443 error = ncl_readrpc(vp, &auio, ap->a_cred);
444 else if (vp->v_type == VDIR) {
446 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
448 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
449 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
452 } else if (vp->v_type == VLNK)
453 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
457 mtx_unlock(&np->n_mtx);
465 * Check to see if the type is ok
466 * and that deletion is not in progress.
467 * For paged in text files, you will need to flush the page cache
468 * if consistency is lost.
472 nfs_open(struct vop_open_args *ap)
474 struct vnode *vp = ap->a_vp;
475 struct nfsnode *np = VTONFS(vp);
478 int fmode = ap->a_mode;
480 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
484 * For NFSv4, we need to do the Open Op before cache validation,
485 * so that we conform to RFC3530 Sec. 9.3.1.
488 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
490 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
497 * Now, if this Open will be doing reading, re-validate/flush the
498 * cache, so that Close/Open coherency is maintained.
500 mtx_lock(&np->n_mtx);
501 if (np->n_flag & NMODIFIED) {
502 mtx_unlock(&np->n_mtx);
503 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
504 if (error == EINTR || error == EIO) {
506 (void) nfsrpc_close(vp, 0, ap->a_td);
509 mtx_lock(&np->n_mtx);
511 if (vp->v_type == VDIR)
512 np->n_direofoffset = 0;
513 mtx_unlock(&np->n_mtx);
514 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
517 (void) nfsrpc_close(vp, 0, ap->a_td);
520 mtx_lock(&np->n_mtx);
521 np->n_mtime = vattr.va_mtime;
523 np->n_change = vattr.va_filerev;
525 mtx_unlock(&np->n_mtx);
526 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
529 (void) nfsrpc_close(vp, 0, ap->a_td);
532 mtx_lock(&np->n_mtx);
533 if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
534 NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
535 if (vp->v_type == VDIR)
536 np->n_direofoffset = 0;
537 mtx_unlock(&np->n_mtx);
538 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
539 if (error == EINTR || error == EIO) {
541 (void) nfsrpc_close(vp, 0, ap->a_td);
544 mtx_lock(&np->n_mtx);
545 np->n_mtime = vattr.va_mtime;
547 np->n_change = vattr.va_filerev;
552 * If the object has >= 1 O_DIRECT active opens, we disable caching.
554 if (newnfs_directio_enable && (fmode & O_DIRECT) &&
555 (vp->v_type == VREG)) {
556 if (np->n_directio_opens == 0) {
557 mtx_unlock(&np->n_mtx);
558 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
561 (void) nfsrpc_close(vp, 0, ap->a_td);
564 mtx_lock(&np->n_mtx);
565 np->n_flag |= NNONCACHE;
567 np->n_directio_opens++;
569 mtx_unlock(&np->n_mtx);
570 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
576 * What an NFS client should do upon close after writing is a debatable issue.
577 * Most NFS clients push delayed writes to the server upon close, basically for
579 * 1 - So that any write errors may be reported back to the client process
580 * doing the close system call. By far the two most likely errors are
581 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
582 * 2 - To put a worst case upper bound on cache inconsistency between
583 * multiple clients for the file.
584 * There is also a consistency problem for Version 2 of the protocol w.r.t.
585 * not being able to tell if other clients are writing a file concurrently,
586 * since there is no way of knowing if the changed modify time in the reply
587 * is only due to the write for this client.
588 * (NFS Version 3 provides weak cache consistency data in the reply that
589 * should be sufficient to detect and handle this case.)
591 * The current code does the following:
592 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
593 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
594 * or commit them (this satisfies 1 and 2 except for the
595 * case where the server crashes after this close but
596 * before the commit RPC, which is felt to be "good
597 * enough". Changing the last argument to ncl_flush() to
598 * a 1 would force a commit operation, if it is felt a
599 * commit is necessary now.
600 * for NFS Version 4 - flush the dirty buffers and commit them, if
601 * nfscl_mustflush() says this is necessary.
602 * It is necessary if there is no write delegation held,
603 * in order to satisfy open/close coherency.
604 * If the file isn't cached on local stable storage,
605 * it may be necessary in order to detect "out of space"
606 * errors from the server, if the write delegation
607 * issued by the server doesn't allow the file to grow.
611 nfs_close(struct vop_close_args *ap)
613 struct vnode *vp = ap->a_vp;
614 struct nfsnode *np = VTONFS(vp);
615 struct nfsvattr nfsva;
617 int error = 0, ret, localcred = 0;
618 int fmode = ap->a_fflag;
620 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
623 * During shutdown, a_cred isn't valid, so just use root.
625 if (ap->a_cred == NOCRED) {
626 cred = newnfs_getcred();
631 if (vp->v_type == VREG) {
633 * Examine and clean dirty pages, regardless of NMODIFIED.
634 * This closes a major hole in close-to-open consistency.
635 * We want to push out all dirty pages (and buffers) on
636 * close, regardless of whether they were dirtied by
637 * mmap'ed writes or via write().
639 if (nfs_clean_pages_on_close && vp->v_object) {
640 VM_OBJECT_LOCK(vp->v_object);
641 vm_object_page_clean(vp->v_object, 0, 0, 0);
642 VM_OBJECT_UNLOCK(vp->v_object);
644 mtx_lock(&np->n_mtx);
645 if (np->n_flag & NMODIFIED) {
646 mtx_unlock(&np->n_mtx);
649 * Under NFSv3 we have dirty buffers to dispose of. We
650 * must flush them to the NFS server. We have the option
651 * of waiting all the way through the commit rpc or just
652 * waiting for the initial write. The default is to only
653 * wait through the initial write so the data is in the
654 * server's cache, which is roughly similar to the state
655 * a standard disk subsystem leaves the file in on close().
657 * We cannot clear the NMODIFIED bit in np->n_flag due to
658 * potential races with other processes, and certainly
659 * cannot clear it if we don't commit.
660 * These races occur when there is no longer the old
661 * traditional vnode locking implemented for Vnode Ops.
663 int cm = newnfs_commit_on_close ? 1 : 0;
664 error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm, 0);
665 /* np->n_flag &= ~NMODIFIED; */
666 } else if (NFS_ISV4(vp)) {
667 if (nfscl_mustflush(vp) != 0) {
668 int cm = newnfs_commit_on_close ? 1 : 0;
669 error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
672 * as above w.r.t races when clearing
674 * np->n_flag &= ~NMODIFIED;
678 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
679 mtx_lock(&np->n_mtx);
682 * Invalidate the attribute cache in all cases.
683 * An open is going to fetch fresh attrs any way, other procs
684 * on this node that have file open will be forced to do an
685 * otw attr fetch, but this is safe.
686 * --> A user found that their RPC count dropped by 20% when
687 * this was commented out and I can't see any requirement
688 * for it, so I've disabled it when negative lookups are
689 * enabled. (What does this have to do with negative lookup
690 * caching? Well nothing, except it was reported by the
691 * same user that needed negative lookup caching and I wanted
692 * there to be a way to disable it to see if it
693 * is the cause of some caching/coherency issue that might
696 if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0)
698 if (np->n_flag & NWRITEERR) {
699 np->n_flag &= ~NWRITEERR;
702 mtx_unlock(&np->n_mtx);
707 * Get attributes so "change" is up to date.
709 if (error == 0 && nfscl_mustflush(vp) != 0) {
710 ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
713 np->n_change = nfsva.na_filerev;
714 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
722 ret = nfsrpc_close(vp, 0, ap->a_td);
726 error = nfscl_maperr(ap->a_td, error, (uid_t)0,
729 if (newnfs_directio_enable)
730 KASSERT((np->n_directio_asyncwr == 0),
731 ("nfs_close: dirty unflushed (%d) directio buffers\n",
732 np->n_directio_asyncwr));
733 if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
734 mtx_lock(&np->n_mtx);
735 KASSERT((np->n_directio_opens > 0),
736 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
737 np->n_directio_opens--;
738 if (np->n_directio_opens == 0)
739 np->n_flag &= ~NNONCACHE;
740 mtx_unlock(&np->n_mtx);
748 * nfs getattr call from vfs.
751 nfs_getattr(struct vop_getattr_args *ap)
753 struct vnode *vp = ap->a_vp;
754 struct thread *td = curthread; /* XXX */
755 struct nfsnode *np = VTONFS(vp);
757 struct nfsvattr nfsva;
758 struct vattr *vap = ap->a_vap;
762 * Update local times for special files.
764 mtx_lock(&np->n_mtx);
765 if (np->n_flag & (NACC | NUPD))
767 mtx_unlock(&np->n_mtx);
769 * First look in the cache.
771 if (ncl_getattrcache(vp, &vattr) == 0) {
772 vap->va_type = vattr.va_type;
773 vap->va_mode = vattr.va_mode;
774 vap->va_nlink = vattr.va_nlink;
775 vap->va_uid = vattr.va_uid;
776 vap->va_gid = vattr.va_gid;
777 vap->va_fsid = vattr.va_fsid;
778 vap->va_fileid = vattr.va_fileid;
779 vap->va_size = vattr.va_size;
780 vap->va_blocksize = vattr.va_blocksize;
781 vap->va_atime = vattr.va_atime;
782 vap->va_mtime = vattr.va_mtime;
783 vap->va_ctime = vattr.va_ctime;
784 vap->va_gen = vattr.va_gen;
785 vap->va_flags = vattr.va_flags;
786 vap->va_rdev = vattr.va_rdev;
787 vap->va_bytes = vattr.va_bytes;
788 vap->va_filerev = vattr.va_filerev;
790 * Get the local modify time for the case of a write
793 nfscl_deleggetmodtime(vp, &vap->va_mtime);
797 if (NFS_ISV34(vp) && nfs_prime_access_cache &&
798 nfsaccess_cache_timeout > 0) {
799 NFSINCRGLOBAL(newnfsstats.accesscache_misses);
800 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
801 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
802 nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
806 error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
808 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
811 * Get the local modify time for the case of a write
814 nfscl_deleggetmodtime(vp, &vap->va_mtime);
815 } else if (NFS_ISV4(vp)) {
816 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
825 nfs_setattr(struct vop_setattr_args *ap)
827 struct vnode *vp = ap->a_vp;
828 struct nfsnode *np = VTONFS(vp);
829 struct thread *td = curthread; /* XXX */
830 struct vattr *vap = ap->a_vap;
839 * Setting of flags and marking of atimes are not supported.
841 if (vap->va_flags != VNOVAL)
845 * Disallow write attempts if the filesystem is mounted read-only.
847 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
848 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
849 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
850 (vp->v_mount->mnt_flag & MNT_RDONLY))
852 if (vap->va_size != VNOVAL) {
853 switch (vp->v_type) {
860 if (vap->va_mtime.tv_sec == VNOVAL &&
861 vap->va_atime.tv_sec == VNOVAL &&
862 vap->va_mode == (mode_t)VNOVAL &&
863 vap->va_uid == (uid_t)VNOVAL &&
864 vap->va_gid == (gid_t)VNOVAL)
866 vap->va_size = VNOVAL;
870 * Disallow write attempts if the filesystem is
873 if (vp->v_mount->mnt_flag & MNT_RDONLY)
876 * We run vnode_pager_setsize() early (why?),
877 * we must set np->n_size now to avoid vinvalbuf
878 * V_SAVE races that might setsize a lower
881 mtx_lock(&np->n_mtx);
883 mtx_unlock(&np->n_mtx);
884 error = ncl_meta_setsize(vp, ap->a_cred, td,
886 mtx_lock(&np->n_mtx);
887 if (np->n_flag & NMODIFIED) {
889 mtx_unlock(&np->n_mtx);
890 if (vap->va_size == 0)
891 error = ncl_vinvalbuf(vp, 0, td, 1);
893 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
895 vnode_pager_setsize(vp, tsize);
899 * Call nfscl_delegmodtime() to set the modify time
900 * locally, as required.
902 nfscl_delegmodtime(vp);
904 mtx_unlock(&np->n_mtx);
906 * np->n_size has already been set to vap->va_size
907 * in ncl_meta_setsize(). We must set it again since
908 * nfs_loadattrcache() could be called through
909 * ncl_meta_setsize() and could modify np->n_size.
911 mtx_lock(&np->n_mtx);
912 np->n_vattr.na_size = np->n_size = vap->va_size;
913 mtx_unlock(&np->n_mtx);
916 mtx_lock(&np->n_mtx);
917 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
918 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
919 mtx_unlock(&np->n_mtx);
920 if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
921 (error == EINTR || error == EIO))
924 mtx_unlock(&np->n_mtx);
926 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
927 if (error && vap->va_size != VNOVAL) {
928 mtx_lock(&np->n_mtx);
929 np->n_size = np->n_vattr.na_size = tsize;
930 vnode_pager_setsize(vp, tsize);
931 mtx_unlock(&np->n_mtx);
937 * Do an nfs setattr rpc.
940 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
943 struct nfsnode *np = VTONFS(vp);
944 int error, ret, attrflag, i;
945 struct nfsvattr nfsva;
948 mtx_lock(&np->n_mtx);
949 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
950 np->n_accesscache[i].stamp = 0;
951 np->n_flag |= NDELEGMOD;
952 mtx_unlock(&np->n_mtx);
954 error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
957 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
961 if (error && NFS_ISV4(vp))
962 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
967 * nfs lookup call, one step at a time...
968 * First look in cache
969 * If not found, unlock the directory nfsnode and do the rpc
972 nfs_lookup(struct vop_lookup_args *ap)
974 struct componentname *cnp = ap->a_cnp;
975 struct vnode *dvp = ap->a_dvp;
976 struct vnode **vpp = ap->a_vpp;
977 struct mount *mp = dvp->v_mount;
978 int flags = cnp->cn_flags;
980 struct nfsmount *nmp;
981 struct nfsnode *np, *newnp;
982 int error = 0, attrflag, dattrflag, ltype;
983 struct thread *td = cnp->cn_thread;
985 struct nfsvattr dnfsva, nfsva;
987 struct timespec dmtime;
990 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
991 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
993 if (dvp->v_type != VDIR)
998 /* For NFSv4, wait until any remove is done. */
999 mtx_lock(&np->n_mtx);
1000 while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1001 np->n_flag |= NREMOVEWANT;
1002 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1004 mtx_unlock(&np->n_mtx);
1006 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1008 error = cache_lookup(dvp, vpp, cnp);
1009 if (error > 0 && error != ENOENT)
1013 * We only accept a positive hit in the cache if the
1014 * change time of the file matches our cached copy.
1015 * Otherwise, we discard the cache entry and fallback
1016 * to doing a lookup RPC.
1018 * To better handle stale file handles and attributes,
1019 * clear the attribute cache of this node if it is a
1020 * leaf component, part of an open() call, and not
1021 * locally modified before fetching the attributes.
1022 * This should allow stale file handles to be detected
1023 * here where we can fall back to a LOOKUP RPC to
1024 * recover rather than having nfs_open() detect the
1025 * stale file handle and failing open(2) with ESTALE.
1028 newnp = VTONFS(newvp);
1029 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1030 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1031 !(newnp->n_flag & NMODIFIED)) {
1032 mtx_lock(&newnp->n_mtx);
1033 newnp->n_attrstamp = 0;
1034 mtx_unlock(&newnp->n_mtx);
1036 if (nfscl_nodeleg(newvp, 0) == 0 ||
1037 (VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1038 timespeccmp(&vattr.va_ctime, &newnp->n_ctime, ==))) {
1039 NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1040 if (cnp->cn_nameiop != LOOKUP &&
1042 cnp->cn_flags |= SAVENAME;
1051 } else if (error == ENOENT) {
1052 if (dvp->v_iflag & VI_DOOMED)
1055 * We only accept a negative hit in the cache if the
1056 * modification time of the parent directory matches
1057 * our cached copy. Otherwise, we discard all of the
1058 * negative cache entries for this directory. We also
1059 * only trust -ve cache entries for less than
1060 * nm_negative_namecache_timeout seconds.
1062 if ((u_int)(ticks - np->n_dmtime_ticks) <
1063 (nmp->nm_negnametimeo * hz) &&
1064 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1065 timespeccmp(&vattr.va_mtime, &np->n_dmtime, ==)) {
1066 NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1069 cache_purge_negative(dvp);
1070 mtx_lock(&np->n_mtx);
1071 timespecclear(&np->n_dmtime);
1072 mtx_unlock(&np->n_mtx);
1076 * Cache the modification time of the parent directory in case
1077 * the lookup fails and results in adding the first negative
1078 * name cache entry for the directory. Since this is reading
1079 * a single time_t, don't bother with locking. The
1080 * modification time may be a bit stale, but it must be read
1081 * before performing the lookup RPC to prevent a race where
1082 * another lookup updates the timestamp on the directory after
1083 * the lookup RPC has been performed on the server but before
1084 * n_dmtime is set at the end of this function.
1086 dmtime = np->n_vattr.na_mtime;
1089 NFSINCRGLOBAL(newnfsstats.lookupcache_misses);
1090 error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1091 cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1094 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1096 if (newvp != NULLVP) {
1101 if (error != ENOENT) {
1103 error = nfscl_maperr(td, error, (uid_t)0,
1108 /* The requested file was not found. */
1109 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1110 (flags & ISLASTCN)) {
1112 * XXX: UFS does a full VOP_ACCESS(dvp,
1113 * VWRITE) here instead of just checking
1116 if (mp->mnt_flag & MNT_RDONLY)
1118 cnp->cn_flags |= SAVENAME;
1119 return (EJUSTRETURN);
1122 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
1124 * Maintain n_dmtime as the modification time
1125 * of the parent directory when the oldest -ve
1126 * name cache entry for this directory was
1127 * added. If a -ve cache entry has already
1128 * been added with a newer modification time
1129 * by a concurrent lookup, then don't bother
1130 * adding a cache entry. The modification
1131 * time of the directory might have changed
1132 * due to the file this lookup failed to find
1133 * being created. In that case a subsequent
1134 * lookup would incorrectly use the entry
1135 * added here instead of doing an extra
1138 mtx_lock(&np->n_mtx);
1139 if (timespeccmp(&np->n_dmtime, &dmtime, <=)) {
1140 if (!timespecisset(&np->n_dmtime)) {
1141 np->n_dmtime = dmtime;
1142 np->n_dmtime_ticks = ticks;
1144 mtx_unlock(&np->n_mtx);
1145 cache_enter(dvp, NULL, cnp);
1147 mtx_unlock(&np->n_mtx);
1153 * Handle RENAME case...
1155 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1156 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1157 FREE((caddr_t)nfhp, M_NFSFH);
1160 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1166 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1169 cnp->cn_flags |= SAVENAME;
1173 if (flags & ISDOTDOT) {
1174 ltype = NFSVOPISLOCKED(dvp);
1175 error = vfs_busy(mp, MBF_NOWAIT);
1178 NFSVOPUNLOCK(dvp, 0);
1179 error = vfs_busy(mp, 0);
1180 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1182 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1189 NFSVOPUNLOCK(dvp, 0);
1190 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1196 NFSVOPLOCK(dvp, ltype | LK_RETRY);
1197 if (dvp->v_iflag & VI_DOOMED) {
1209 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1211 } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1212 FREE((caddr_t)nfhp, M_NFSFH);
1216 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1219 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1225 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1227 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1228 !(np->n_flag & NMODIFIED)) {
1230 * Flush the attribute cache when opening a
1231 * leaf node to ensure that fresh attributes
1232 * are fetched in nfs_open() since we did not
1233 * fetch attributes from the LOOKUP reply.
1235 mtx_lock(&np->n_mtx);
1236 np->n_attrstamp = 0;
1237 mtx_unlock(&np->n_mtx);
1240 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1241 cnp->cn_flags |= SAVENAME;
1242 if ((cnp->cn_flags & MAKEENTRY) &&
1243 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1244 np->n_ctime = np->n_vattr.na_vattr.va_ctime;
1245 cache_enter(dvp, newvp, cnp);
1253 * Just call ncl_bioread() to do the work.
1256 nfs_read(struct vop_read_args *ap)
1258 struct vnode *vp = ap->a_vp;
1260 switch (vp->v_type) {
1262 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1266 return (EOPNOTSUPP);
1274 nfs_readlink(struct vop_readlink_args *ap)
1276 struct vnode *vp = ap->a_vp;
1278 if (vp->v_type != VLNK)
1280 return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1284 * Do a readlink rpc.
1285 * Called by ncl_doio() from below the buffer cache.
1288 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1290 int error, ret, attrflag;
1291 struct nfsvattr nfsva;
1293 error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1296 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1300 if (error && NFS_ISV4(vp))
1301 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1310 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1312 int error, ret, attrflag;
1313 struct nfsvattr nfsva;
1315 error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva, &attrflag,
1318 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1322 if (error && NFS_ISV4(vp))
1323 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1331 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1332 int *iomode, int *must_commit, int called_from_strategy)
1334 struct nfsvattr nfsva;
1335 int error = 0, attrflag, ret;
1337 error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1338 uiop->uio_td, &nfsva, &attrflag, NULL, called_from_strategy);
1340 if (VTONFS(vp)->n_flag & ND_NFSV4)
1341 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1344 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1349 if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1350 *iomode = NFSWRITE_FILESYNC;
1351 if (error && NFS_ISV4(vp))
1352 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1358 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1359 * mode set to specify the file type and the size field for rdev.
1362 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1365 struct nfsvattr nfsva, dnfsva;
1366 struct vnode *newvp = NULL;
1367 struct nfsnode *np = NULL, *dnp;
1370 int error = 0, attrflag, dattrflag;
1373 if (vap->va_type == VCHR || vap->va_type == VBLK)
1374 rdev = vap->va_rdev;
1375 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1378 return (EOPNOTSUPP);
1379 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1381 error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1382 rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1383 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1386 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1387 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1388 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1391 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1392 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1395 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1398 if (attrflag != 0) {
1399 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1406 if ((cnp->cn_flags & MAKEENTRY))
1407 cache_enter(dvp, newvp, cnp);
1409 } else if (NFS_ISV4(dvp)) {
1410 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1414 mtx_lock(&dnp->n_mtx);
1415 dnp->n_flag |= NMODIFIED;
1417 dnp->n_attrstamp = 0;
1418 mtx_unlock(&dnp->n_mtx);
1424 * just call nfs_mknodrpc() to do the work.
1428 nfs_mknod(struct vop_mknod_args *ap)
1430 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1433 static u_long create_verf;
1435 * nfs file create call
1438 nfs_create(struct vop_create_args *ap)
1440 struct vnode *dvp = ap->a_dvp;
1441 struct vattr *vap = ap->a_vap;
1442 struct componentname *cnp = ap->a_cnp;
1443 struct nfsnode *np = NULL, *dnp;
1444 struct vnode *newvp = NULL;
1445 struct nfsmount *nmp;
1446 struct nfsvattr dnfsva, nfsva;
1449 int error = 0, attrflag, dattrflag, fmode = 0;
1453 * Oops, not for me..
1455 if (vap->va_type == VSOCK)
1456 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1458 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1460 if (vap->va_vaflags & VA_EXCLUSIVE)
1463 nmp = VFSTONFS(vnode_mount(dvp));
1465 /* For NFSv4, wait until any remove is done. */
1466 mtx_lock(&dnp->n_mtx);
1467 while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1468 dnp->n_flag |= NREMOVEWANT;
1469 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1471 mtx_unlock(&dnp->n_mtx);
1474 CURVNET_SET(CRED_TO_VNET(cnp->cn_cred));
1476 if (!TAILQ_EMPTY(&V_in_ifaddrhead))
1477 cverf.lval[0] = IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr.s_addr;
1480 cverf.lval[0] = create_verf;
1482 IN_IFADDR_RUNLOCK();
1485 cverf.lval[1] = ++create_verf;
1486 error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1487 vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1488 &nfhp, &attrflag, &dattrflag, NULL);
1491 (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1492 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1493 &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1496 error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1497 cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1500 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1504 error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1508 if (newvp != NULL) {
1512 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1513 error == NFSERR_NOTSUPP) {
1517 } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1518 if (nfscl_checksattr(vap, &nfsva)) {
1520 * We are normally called with only a partially
1521 * initialized VAP. Since the NFSv3 spec says that
1522 * the server may use the file attributes to
1523 * store the verifier, the spec requires us to do a
1524 * SETATTR RPC. FreeBSD servers store the verifier in
1525 * atime, but we can't really assume that all servers
1526 * will so we ensure that our SETATTR sets both atime
1529 if (vap->va_mtime.tv_sec == VNOVAL)
1530 vfs_timestamp(&vap->va_mtime);
1531 if (vap->va_atime.tv_sec == VNOVAL)
1532 vap->va_atime = vap->va_mtime;
1533 error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1534 cnp->cn_thread, &nfsva, &attrflag, NULL);
1535 if (error && (vap->va_uid != (uid_t)VNOVAL ||
1536 vap->va_gid != (gid_t)VNOVAL)) {
1537 /* try again without setting uid/gid */
1538 vap->va_uid = (uid_t)VNOVAL;
1539 vap->va_gid = (uid_t)VNOVAL;
1540 error = nfsrpc_setattr(newvp, vap, NULL,
1541 cnp->cn_cred, cnp->cn_thread, &nfsva,
1545 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1552 if (cnp->cn_flags & MAKEENTRY)
1553 cache_enter(dvp, newvp, cnp);
1555 } else if (NFS_ISV4(dvp)) {
1556 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1559 mtx_lock(&dnp->n_mtx);
1560 dnp->n_flag |= NMODIFIED;
1562 dnp->n_attrstamp = 0;
1563 mtx_unlock(&dnp->n_mtx);
1568 * nfs file remove call
1569 * To try and make nfs semantics closer to ufs semantics, a file that has
1570 * other processes using the vnode is renamed instead of removed and then
1571 * removed later on the last close.
1572 * - If v_usecount > 1
1573 * If a rename is not already in the works
1574 * call nfs_sillyrename() to set it up
1579 nfs_remove(struct vop_remove_args *ap)
1581 struct vnode *vp = ap->a_vp;
1582 struct vnode *dvp = ap->a_dvp;
1583 struct componentname *cnp = ap->a_cnp;
1584 struct nfsnode *np = VTONFS(vp);
1588 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1589 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1590 if (vp->v_type == VDIR)
1592 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1593 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1594 vattr.va_nlink > 1)) {
1596 * Purge the name cache so that the chance of a lookup for
1597 * the name succeeding while the remove is in progress is
1598 * minimized. Without node locking it can still happen, such
1599 * that an I/O op returns ESTALE, but since you get this if
1600 * another host removes the file..
1604 * throw away biocache buffers, mainly to avoid
1605 * unnecessary delayed writes later.
1607 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1609 if (error != EINTR && error != EIO)
1610 error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1611 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1613 * Kludge City: If the first reply to the remove rpc is lost..
1614 * the reply to the retransmitted request will be ENOENT
1615 * since the file was in fact removed
1616 * Therefore, we cheat and return success.
1618 if (error == ENOENT)
1620 } else if (!np->n_sillyrename)
1621 error = nfs_sillyrename(dvp, vp, cnp);
1622 mtx_lock(&np->n_mtx);
1623 np->n_attrstamp = 0;
1624 mtx_unlock(&np->n_mtx);
1629 * nfs file remove rpc called from nfs_inactive
1632 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1635 * Make sure that the directory vnode is still valid.
1636 * XXX we should lock sp->s_dvp here.
1638 if (sp->s_dvp->v_type == VBAD)
1640 return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1645 * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1648 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1649 int namelen, struct ucred *cred, struct thread *td)
1651 struct nfsvattr dnfsva;
1652 struct nfsnode *dnp = VTONFS(dvp);
1653 int error = 0, dattrflag;
1655 mtx_lock(&dnp->n_mtx);
1656 dnp->n_flag |= NREMOVEINPROG;
1657 mtx_unlock(&dnp->n_mtx);
1658 error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1660 mtx_lock(&dnp->n_mtx);
1661 if ((dnp->n_flag & NREMOVEWANT)) {
1662 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1663 mtx_unlock(&dnp->n_mtx);
1664 wakeup((caddr_t)dnp);
1666 dnp->n_flag &= ~NREMOVEINPROG;
1667 mtx_unlock(&dnp->n_mtx);
1670 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1671 mtx_lock(&dnp->n_mtx);
1672 dnp->n_flag |= NMODIFIED;
1674 dnp->n_attrstamp = 0;
1675 mtx_unlock(&dnp->n_mtx);
1676 if (error && NFS_ISV4(dvp))
1677 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1682 * nfs file rename call
1685 nfs_rename(struct vop_rename_args *ap)
1687 struct vnode *fvp = ap->a_fvp;
1688 struct vnode *tvp = ap->a_tvp;
1689 struct vnode *fdvp = ap->a_fdvp;
1690 struct vnode *tdvp = ap->a_tdvp;
1691 struct componentname *tcnp = ap->a_tcnp;
1692 struct componentname *fcnp = ap->a_fcnp;
1693 struct nfsnode *fnp = VTONFS(ap->a_fvp);
1694 struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1695 struct nfsv4node *newv4 = NULL;
1698 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1699 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1700 /* Check for cross-device rename */
1701 if ((fvp->v_mount != tdvp->v_mount) ||
1702 (tvp && (fvp->v_mount != tvp->v_mount))) {
1708 ncl_printf("nfs_rename: fvp == tvp (can't happen)\n");
1712 if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
1716 * We have to flush B_DELWRI data prior to renaming
1717 * the file. If we don't, the delayed-write buffers
1718 * can be flushed out later after the file has gone stale
1719 * under NFSV3. NFSV2 does not have this problem because
1720 * ( as far as I can tell ) it flushes dirty buffers more
1723 * Skip the rename operation if the fsync fails, this can happen
1724 * due to the server's volume being full, when we pushed out data
1725 * that was written back to our cache earlier. Not checking for
1726 * this condition can result in potential (silent) data loss.
1728 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1729 NFSVOPUNLOCK(fvp, 0);
1731 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1736 * If the tvp exists and is in use, sillyrename it before doing the
1737 * rename of the new file over it.
1738 * XXX Can't sillyrename a directory.
1740 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1741 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1746 error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1747 tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1750 if (error == 0 && NFS_ISV4(tdvp)) {
1752 * For NFSv4, check to see if it is the same name and
1753 * replace the name, if it is different.
1755 MALLOC(newv4, struct nfsv4node *,
1756 sizeof (struct nfsv4node) +
1757 tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1758 M_NFSV4NODE, M_WAITOK);
1759 mtx_lock(&tdnp->n_mtx);
1760 mtx_lock(&fnp->n_mtx);
1761 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1762 (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1763 NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1764 tcnp->cn_namelen) ||
1765 tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1766 NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1767 tdnp->n_fhp->nfh_len))) {
1769 { char nnn[100]; int nnnl;
1770 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1771 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1773 printf("ren replace=%s\n",nnn);
1776 FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1779 fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1780 fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1781 NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1782 tdnp->n_fhp->nfh_len);
1783 NFSBCOPY(tcnp->cn_nameptr,
1784 NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1786 mtx_unlock(&tdnp->n_mtx);
1787 mtx_unlock(&fnp->n_mtx);
1789 FREE((caddr_t)newv4, M_NFSV4NODE);
1792 if (fvp->v_type == VDIR) {
1793 if (tvp != NULL && tvp->v_type == VDIR)
1808 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1810 if (error == ENOENT)
1816 * nfs file rename rpc called from nfs_remove() above
1819 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1820 struct sillyrename *sp)
1823 return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1824 sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1829 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1832 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1833 int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1834 int tnamelen, struct ucred *cred, struct thread *td)
1836 struct nfsvattr fnfsva, tnfsva;
1837 struct nfsnode *fdnp = VTONFS(fdvp);
1838 struct nfsnode *tdnp = VTONFS(tdvp);
1839 int error = 0, fattrflag, tattrflag;
1841 error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1842 tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1843 &tattrflag, NULL, NULL);
1844 mtx_lock(&fdnp->n_mtx);
1845 fdnp->n_flag |= NMODIFIED;
1846 if (fattrflag != 0) {
1847 mtx_unlock(&fdnp->n_mtx);
1848 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1850 fdnp->n_attrstamp = 0;
1851 mtx_unlock(&fdnp->n_mtx);
1853 mtx_lock(&tdnp->n_mtx);
1854 tdnp->n_flag |= NMODIFIED;
1855 if (tattrflag != 0) {
1856 mtx_unlock(&tdnp->n_mtx);
1857 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1859 tdnp->n_attrstamp = 0;
1860 mtx_unlock(&tdnp->n_mtx);
1862 if (error && NFS_ISV4(fdvp))
1863 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1868 * nfs hard link create call
1871 nfs_link(struct vop_link_args *ap)
1873 struct vnode *vp = ap->a_vp;
1874 struct vnode *tdvp = ap->a_tdvp;
1875 struct componentname *cnp = ap->a_cnp;
1876 struct nfsnode *np, *tdnp;
1877 struct nfsvattr nfsva, dnfsva;
1878 int error = 0, attrflag, dattrflag;
1880 if (vp->v_mount != tdvp->v_mount) {
1885 * Push all writes to the server, so that the attribute cache
1886 * doesn't get "out of sync" with the server.
1887 * XXX There should be a better way!
1889 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1891 error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1892 cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1894 tdnp = VTONFS(tdvp);
1895 mtx_lock(&tdnp->n_mtx);
1896 tdnp->n_flag |= NMODIFIED;
1897 if (dattrflag != 0) {
1898 mtx_unlock(&tdnp->n_mtx);
1899 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1901 tdnp->n_attrstamp = 0;
1902 mtx_unlock(&tdnp->n_mtx);
1905 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1908 mtx_lock(&np->n_mtx);
1909 np->n_attrstamp = 0;
1910 mtx_unlock(&np->n_mtx);
1913 * If negative lookup caching is enabled, I might as well
1914 * add an entry for this node. Not necessary for correctness,
1915 * but if negative caching is enabled, then the system
1916 * must care about lookup caching hit rate, so...
1918 if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
1919 (cnp->cn_flags & MAKEENTRY))
1920 cache_enter(tdvp, vp, cnp);
1921 if (error && NFS_ISV4(vp))
1922 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
1928 * nfs symbolic link create call
1931 nfs_symlink(struct vop_symlink_args *ap)
1933 struct vnode *dvp = ap->a_dvp;
1934 struct vattr *vap = ap->a_vap;
1935 struct componentname *cnp = ap->a_cnp;
1936 struct nfsvattr nfsva, dnfsva;
1938 struct nfsnode *np = NULL, *dnp;
1939 struct vnode *newvp = NULL;
1940 int error = 0, attrflag, dattrflag, ret;
1942 vap->va_type = VLNK;
1943 error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1944 ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1945 &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1947 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
1948 &np, NULL, LK_EXCLUSIVE);
1954 if (newvp != NULL) {
1956 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1958 } else if (!error) {
1960 * If we do not have an error and we could not extract the
1961 * newvp from the response due to the request being NFSv2, we
1962 * have to do a lookup in order to obtain a newvp to return.
1964 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1965 cnp->cn_cred, cnp->cn_thread, &np);
1973 error = nfscl_maperr(cnp->cn_thread, error,
1974 vap->va_uid, vap->va_gid);
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(dvp->v_mount)->nm_negnametimeo != 0 &&
1983 (cnp->cn_flags & MAKEENTRY))
1984 cache_enter(dvp, newvp, cnp);
1989 mtx_lock(&dnp->n_mtx);
1990 dnp->n_flag |= NMODIFIED;
1991 if (dattrflag != 0) {
1992 mtx_unlock(&dnp->n_mtx);
1993 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1995 dnp->n_attrstamp = 0;
1996 mtx_unlock(&dnp->n_mtx);
2005 nfs_mkdir(struct vop_mkdir_args *ap)
2007 struct vnode *dvp = ap->a_dvp;
2008 struct vattr *vap = ap->a_vap;
2009 struct componentname *cnp = ap->a_cnp;
2010 struct nfsnode *np = NULL, *dnp;
2011 struct vnode *newvp = NULL;
2014 struct nfsvattr nfsva, dnfsva;
2015 int error = 0, attrflag, dattrflag, ret;
2017 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2019 vap->va_type = VDIR;
2020 error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2021 vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2022 &attrflag, &dattrflag, NULL);
2024 mtx_lock(&dnp->n_mtx);
2025 dnp->n_flag |= NMODIFIED;
2026 if (dattrflag != 0) {
2027 mtx_unlock(&dnp->n_mtx);
2028 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2030 dnp->n_attrstamp = 0;
2031 mtx_unlock(&dnp->n_mtx);
2034 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2035 &np, NULL, LK_EXCLUSIVE);
2039 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2044 if (!error && newvp == NULL) {
2045 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2046 cnp->cn_cred, cnp->cn_thread, &np);
2049 if (newvp->v_type != VDIR)
2057 error = nfscl_maperr(cnp->cn_thread, error,
2058 vap->va_uid, vap->va_gid);
2061 * If negative lookup caching is enabled, I might as well
2062 * add an entry for this node. Not necessary for correctness,
2063 * but if negative caching is enabled, then the system
2064 * must care about lookup caching hit rate, so...
2066 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2067 (cnp->cn_flags & MAKEENTRY))
2068 cache_enter(dvp, newvp, cnp);
2075 * nfs remove directory call
2078 nfs_rmdir(struct vop_rmdir_args *ap)
2080 struct vnode *vp = ap->a_vp;
2081 struct vnode *dvp = ap->a_dvp;
2082 struct componentname *cnp = ap->a_cnp;
2083 struct nfsnode *dnp;
2084 struct nfsvattr dnfsva;
2085 int error, dattrflag;
2089 error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2090 cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2092 mtx_lock(&dnp->n_mtx);
2093 dnp->n_flag |= NMODIFIED;
2094 if (dattrflag != 0) {
2095 mtx_unlock(&dnp->n_mtx);
2096 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2098 dnp->n_attrstamp = 0;
2099 mtx_unlock(&dnp->n_mtx);
2104 if (error && NFS_ISV4(dvp))
2105 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2108 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2110 if (error == ENOENT)
2119 nfs_readdir(struct vop_readdir_args *ap)
2121 struct vnode *vp = ap->a_vp;
2122 struct nfsnode *np = VTONFS(vp);
2123 struct uio *uio = ap->a_uio;
2124 int tresid, error = 0;
2127 if (vp->v_type != VDIR)
2131 * First, check for hit on the EOF offset cache
2133 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2134 (np->n_flag & NMODIFIED) == 0) {
2135 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2136 mtx_lock(&np->n_mtx);
2137 if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2138 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2139 mtx_unlock(&np->n_mtx);
2140 NFSINCRGLOBAL(newnfsstats.direofcache_hits);
2143 mtx_unlock(&np->n_mtx);
2148 * Call ncl_bioread() to do the real work.
2150 tresid = uio->uio_resid;
2151 error = ncl_bioread(vp, uio, 0, ap->a_cred);
2153 if (!error && uio->uio_resid == tresid)
2154 NFSINCRGLOBAL(newnfsstats.direofcache_misses);
2160 * Called from below the buffer cache by ncl_doio().
2163 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2166 struct nfsvattr nfsva;
2167 nfsuint64 *cookiep, cookie;
2168 struct nfsnode *dnp = VTONFS(vp);
2169 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2170 int error = 0, eof, attrflag;
2172 KASSERT(uiop->uio_iovcnt == 1 &&
2173 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2174 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2175 ("nfs readdirrpc bad uio"));
2178 * If there is no cookie, assume directory was stale.
2180 ncl_dircookie_lock(dnp);
2181 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2184 ncl_dircookie_unlock(dnp);
2186 ncl_dircookie_unlock(dnp);
2187 return (NFSERR_BAD_COOKIE);
2190 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2191 (void)ncl_fsinfo(nmp, vp, cred, td);
2193 error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2194 &attrflag, &eof, NULL);
2196 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2200 * We are now either at the end of the directory or have filled
2204 dnp->n_direofoffset = uiop->uio_offset;
2206 if (uiop->uio_resid > 0)
2207 ncl_printf("EEK! readdirrpc resid > 0\n");
2208 ncl_dircookie_lock(dnp);
2209 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2211 ncl_dircookie_unlock(dnp);
2213 } else if (NFS_ISV4(vp)) {
2214 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2220 * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2223 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2226 struct nfsvattr nfsva;
2227 nfsuint64 *cookiep, cookie;
2228 struct nfsnode *dnp = VTONFS(vp);
2229 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2230 int error = 0, attrflag, eof;
2232 KASSERT(uiop->uio_iovcnt == 1 &&
2233 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2234 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2235 ("nfs readdirplusrpc bad uio"));
2238 * If there is no cookie, assume directory was stale.
2240 ncl_dircookie_lock(dnp);
2241 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2244 ncl_dircookie_unlock(dnp);
2246 ncl_dircookie_unlock(dnp);
2247 return (NFSERR_BAD_COOKIE);
2250 if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2251 (void)ncl_fsinfo(nmp, vp, cred, td);
2252 error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2253 &attrflag, &eof, NULL);
2255 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2259 * We are now either at end of the directory or have filled the
2263 dnp->n_direofoffset = uiop->uio_offset;
2265 if (uiop->uio_resid > 0)
2266 ncl_printf("EEK! readdirplusrpc resid > 0\n");
2267 ncl_dircookie_lock(dnp);
2268 cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2270 ncl_dircookie_unlock(dnp);
2272 } else if (NFS_ISV4(vp)) {
2273 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2279 * Silly rename. To make the NFS filesystem that is stateless look a little
2280 * more like the "ufs" a remove of an active vnode is translated to a rename
2281 * to a funny looking filename that is removed by nfs_inactive on the
2282 * nfsnode. There is the potential for another process on a different client
2283 * to create the same funny name between the nfs_lookitup() fails and the
2284 * nfs_rename() completes, but...
2287 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2289 struct sillyrename *sp;
2293 unsigned int lticks;
2297 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2298 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2299 M_NEWNFSREQ, M_WAITOK);
2300 sp->s_cred = crhold(cnp->cn_cred);
2305 * Fudge together a funny name.
2306 * Changing the format of the funny name to accomodate more
2307 * sillynames per directory.
2308 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2309 * CPU ticks since boot.
2311 pid = cnp->cn_thread->td_proc->p_pid;
2312 lticks = (unsigned int)ticks;
2314 sp->s_namlen = sprintf(sp->s_name,
2315 ".nfs.%08x.%04x4.4", lticks,
2317 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2318 cnp->cn_thread, NULL))
2322 error = nfs_renameit(dvp, vp, cnp, sp);
2325 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2326 cnp->cn_thread, &np);
2327 np->n_sillyrename = sp;
2332 free((caddr_t)sp, M_NEWNFSREQ);
2337 * Look up a file name and optionally either update the file handle or
2338 * allocate an nfsnode, depending on the value of npp.
2339 * npp == NULL --> just do the lookup
2340 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2342 * *npp != NULL --> update the file handle in the vnode
2345 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2346 struct thread *td, struct nfsnode **npp)
2348 struct vnode *newvp = NULL, *vp;
2349 struct nfsnode *np, *dnp = VTONFS(dvp);
2350 struct nfsfh *nfhp, *onfhp;
2351 struct nfsvattr nfsva, dnfsva;
2352 struct componentname cn;
2353 int error = 0, attrflag, dattrflag;
2356 error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2357 &nfhp, &attrflag, &dattrflag, NULL);
2359 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2360 if (npp && !error) {
2365 * For NFSv4, check to see if it is the same name and
2366 * replace the name, if it is different.
2368 if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2369 (np->n_v4->n4_namelen != len ||
2370 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2371 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2372 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2373 dnp->n_fhp->nfh_len))) {
2375 { char nnn[100]; int nnnl;
2376 nnnl = (len < 100) ? len : 99;
2377 bcopy(name, nnn, nnnl);
2379 printf("replace=%s\n",nnn);
2382 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2383 MALLOC(np->n_v4, struct nfsv4node *,
2384 sizeof (struct nfsv4node) +
2385 dnp->n_fhp->nfh_len + len - 1,
2386 M_NFSV4NODE, M_WAITOK);
2387 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2388 np->n_v4->n4_namelen = len;
2389 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2390 dnp->n_fhp->nfh_len);
2391 NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2393 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2397 * Rehash node for new file handle.
2399 vfs_hash_rehash(vp, hash);
2402 FREE((caddr_t)onfhp, M_NFSFH);
2404 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2405 FREE((caddr_t)nfhp, M_NFSFH);
2409 cn.cn_nameptr = name;
2410 cn.cn_namelen = len;
2411 error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2412 &np, NULL, LK_EXCLUSIVE);
2417 if (!attrflag && *npp == NULL) {
2425 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2428 if (npp && *npp == NULL) {
2439 if (error && NFS_ISV4(dvp))
2440 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2445 * Nfs Version 3 and 4 commit rpc
2448 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2451 struct nfsvattr nfsva;
2452 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2453 int error, attrflag;
2454 u_char verf[NFSX_VERF];
2456 mtx_lock(&nmp->nm_mtx);
2457 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2458 mtx_unlock(&nmp->nm_mtx);
2461 mtx_unlock(&nmp->nm_mtx);
2462 error = nfsrpc_commit(vp, offset, cnt, cred, td, verf, &nfsva,
2465 mtx_lock(&nmp->nm_mtx);
2466 if (NFSBCMP((caddr_t)nmp->nm_verf, verf, NFSX_VERF)) {
2467 NFSBCOPY(verf, (caddr_t)nmp->nm_verf, NFSX_VERF);
2468 error = NFSERR_STALEWRITEVERF;
2470 mtx_unlock(&nmp->nm_mtx);
2471 if (!error && attrflag)
2472 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2474 } else if (NFS_ISV4(vp)) {
2475 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2482 * For async requests when nfsiod(s) are running, queue the request by
2483 * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2487 nfs_strategy(struct vop_strategy_args *ap)
2489 struct buf *bp = ap->a_bp;
2492 KASSERT(!(bp->b_flags & B_DONE),
2493 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2494 BUF_ASSERT_HELD(bp);
2496 if (bp->b_iocmd == BIO_READ)
2502 * If the op is asynchronous and an i/o daemon is waiting
2503 * queue the request, wake it up and wait for completion
2504 * otherwise just do it ourselves.
2506 if ((bp->b_flags & B_ASYNC) == 0 ||
2507 ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2508 (void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
2513 * fsync vnode op. Just call ncl_flush() with commit == 1.
2517 nfs_fsync(struct vop_fsync_args *ap)
2519 return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
2523 * Flush all the blocks associated with a vnode.
2524 * Walk through the buffer pool and push any dirty pages
2525 * associated with the vnode.
2526 * If the called_from_renewthread argument is TRUE, it has been called
2527 * from the NFSv4 renew thread and, as such, cannot block indefinitely
2528 * waiting for a buffer write to complete.
2531 ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2532 int commit, int called_from_renewthread)
2534 struct nfsnode *np = VTONFS(vp);
2538 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2539 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2540 int passone = 1, trycnt = 0;
2541 u_quad_t off, endoff, toff;
2542 struct ucred* wcred = NULL;
2543 struct buf **bvec = NULL;
2545 #ifndef NFS_COMMITBVECSIZ
2546 #define NFS_COMMITBVECSIZ 20
2548 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2549 int bvecsize = 0, bveccount;
2551 if (called_from_renewthread != 0)
2553 if (nmp->nm_flag & NFSMNT_INT)
2554 slpflag = NFS_PCATCH;
2559 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2560 * server, but has not been committed to stable storage on the server
2561 * yet. On the first pass, the byte range is worked out and the commit
2562 * rpc is done. On the second pass, ncl_writebp() is called to do the
2569 if (NFS_ISV34(vp) && commit) {
2570 if (bvec != NULL && bvec != bvec_on_stack)
2573 * Count up how many buffers waiting for a commit.
2577 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2578 if (!BUF_ISLOCKED(bp) &&
2579 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2580 == (B_DELWRI | B_NEEDCOMMIT))
2584 * Allocate space to remember the list of bufs to commit. It is
2585 * important to use M_NOWAIT here to avoid a race with nfs_write.
2586 * If we can't get memory (for whatever reason), we will end up
2587 * committing the buffers one-by-one in the loop below.
2589 if (bveccount > NFS_COMMITBVECSIZ) {
2591 * Release the vnode interlock to avoid a lock
2595 bvec = (struct buf **)
2596 malloc(bveccount * sizeof(struct buf *),
2600 bvec = bvec_on_stack;
2601 bvecsize = NFS_COMMITBVECSIZ;
2603 bvecsize = bveccount;
2605 bvec = bvec_on_stack;
2606 bvecsize = NFS_COMMITBVECSIZ;
2608 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2609 if (bvecpos >= bvecsize)
2611 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2612 nbp = TAILQ_NEXT(bp, b_bobufs);
2615 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2616 (B_DELWRI | B_NEEDCOMMIT)) {
2618 nbp = TAILQ_NEXT(bp, b_bobufs);
2624 * Work out if all buffers are using the same cred
2625 * so we can deal with them all with one commit.
2627 * NOTE: we are not clearing B_DONE here, so we have
2628 * to do it later on in this routine if we intend to
2629 * initiate I/O on the bp.
2631 * Note: to avoid loopback deadlocks, we do not
2632 * assign b_runningbufspace.
2635 wcred = bp->b_wcred;
2636 else if (wcred != bp->b_wcred)
2638 vfs_busy_pages(bp, 1);
2642 * bp is protected by being locked, but nbp is not
2643 * and vfs_busy_pages() may sleep. We have to
2646 nbp = TAILQ_NEXT(bp, b_bobufs);
2649 * A list of these buffers is kept so that the
2650 * second loop knows which buffers have actually
2651 * been committed. This is necessary, since there
2652 * may be a race between the commit rpc and new
2653 * uncommitted writes on the file.
2655 bvec[bvecpos++] = bp;
2656 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2660 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2668 * Commit data on the server, as required.
2669 * If all bufs are using the same wcred, then use that with
2670 * one call for all of them, otherwise commit each one
2673 if (wcred != NOCRED)
2674 retv = ncl_commit(vp, off, (int)(endoff - off),
2678 for (i = 0; i < bvecpos; i++) {
2681 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2683 size = (u_quad_t)(bp->b_dirtyend
2685 retv = ncl_commit(vp, off, (int)size,
2691 if (retv == NFSERR_STALEWRITEVERF)
2692 ncl_clearcommit(vp->v_mount);
2695 * Now, either mark the blocks I/O done or mark the
2696 * blocks dirty, depending on whether the commit
2699 for (i = 0; i < bvecpos; i++) {
2701 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2704 * Error, leave B_DELWRI intact
2706 vfs_unbusy_pages(bp);
2710 * Success, remove B_DELWRI ( bundirty() ).
2712 * b_dirtyoff/b_dirtyend seem to be NFS
2713 * specific. We should probably move that
2714 * into bundirty(). XXX
2717 bp->b_flags |= B_ASYNC;
2719 bp->b_flags &= ~B_DONE;
2720 bp->b_ioflags &= ~BIO_ERROR;
2721 bp->b_dirtyoff = bp->b_dirtyend = 0;
2728 * Start/do any write(s) that are required.
2732 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2733 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2734 if (waitfor != MNT_WAIT || passone)
2737 error = BUF_TIMELOCK(bp,
2738 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2739 BO_MTX(bo), "nfsfsync", slpflag, slptimeo);
2744 if (error == ENOLCK) {
2748 if (called_from_renewthread != 0) {
2750 * Return EIO so the flush will be retried
2756 if (newnfs_sigintr(nmp, td)) {
2760 if (slpflag & PCATCH) {
2766 if ((bp->b_flags & B_DELWRI) == 0)
2767 panic("nfs_fsync: not dirty");
2768 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2774 if (passone || !commit)
2775 bp->b_flags |= B_ASYNC;
2777 bp->b_flags |= B_ASYNC;
2779 if (newnfs_sigintr(nmp, td)) {
2790 if (waitfor == MNT_WAIT) {
2791 while (bo->bo_numoutput) {
2792 error = bufobj_wwait(bo, slpflag, slptimeo);
2795 if (called_from_renewthread != 0) {
2797 * Return EIO so that the flush will be
2803 error = newnfs_sigintr(nmp, td);
2806 if (slpflag & PCATCH) {
2813 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2818 * Wait for all the async IO requests to drain
2821 mtx_lock(&np->n_mtx);
2822 while (np->n_directio_asyncwr > 0) {
2823 np->n_flag |= NFSYNCWAIT;
2824 error = newnfs_msleep(td, &np->n_directio_asyncwr,
2825 &np->n_mtx, slpflag | (PRIBIO + 1),
2828 if (newnfs_sigintr(nmp, td)) {
2829 mtx_unlock(&np->n_mtx);
2835 mtx_unlock(&np->n_mtx);
2838 mtx_lock(&np->n_mtx);
2839 if (np->n_flag & NWRITEERR) {
2840 error = np->n_error;
2841 np->n_flag &= ~NWRITEERR;
2843 if (commit && bo->bo_dirty.bv_cnt == 0 &&
2844 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2845 np->n_flag &= ~NMODIFIED;
2846 mtx_unlock(&np->n_mtx);
2848 if (bvec != NULL && bvec != bvec_on_stack)
2850 if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2851 (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2852 np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2853 /* try, try again... */
2858 printf("try%d\n", trycnt);
2865 * NFS advisory byte-level locks.
2868 nfs_advlock(struct vop_advlock_args *ap)
2870 struct vnode *vp = ap->a_vp;
2872 struct nfsnode *np = VTONFS(ap->a_vp);
2873 struct proc *p = (struct proc *)ap->a_id;
2874 struct thread *td = curthread; /* XXX */
2876 int ret, error = EOPNOTSUPP;
2879 if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
2880 if (vp->v_type != VREG)
2882 if ((ap->a_flags & F_POSIX) != 0)
2885 cred = td->td_ucred;
2886 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2887 if (vp->v_iflag & VI_DOOMED) {
2888 NFSVOPUNLOCK(vp, 0);
2893 * If this is unlocking a write locked region, flush and
2894 * commit them before unlocking. This is required by
2895 * RFC3530 Sec. 9.3.2.
2897 if (ap->a_op == F_UNLCK &&
2898 nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
2900 (void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
2903 * Loop around doing the lock op, while a blocking lock
2904 * must wait for the lock op to succeed.
2907 ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
2908 ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
2909 if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2910 ap->a_op == F_SETLK) {
2911 NFSVOPUNLOCK(vp, 0);
2912 error = nfs_catnap(PZERO | PCATCH, ret,
2916 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
2917 if (vp->v_iflag & VI_DOOMED) {
2918 NFSVOPUNLOCK(vp, 0);
2922 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2923 ap->a_op == F_SETLK);
2924 if (ret == NFSERR_DENIED) {
2925 NFSVOPUNLOCK(vp, 0);
2927 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
2928 NFSVOPUNLOCK(vp, 0);
2930 } else if (ret != 0) {
2931 NFSVOPUNLOCK(vp, 0);
2936 * Now, if we just got a lock, invalidate data in the buffer
2937 * cache, as required, so that the coherency conforms with
2938 * RFC3530 Sec. 9.3.2.
2940 if (ap->a_op == F_SETLK) {
2941 if ((np->n_flag & NMODIFIED) == 0) {
2942 np->n_attrstamp = 0;
2943 ret = VOP_GETATTR(vp, &va, cred);
2945 if ((np->n_flag & NMODIFIED) || ret ||
2946 np->n_change != va.va_filerev) {
2947 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
2948 np->n_attrstamp = 0;
2949 ret = VOP_GETATTR(vp, &va, cred);
2951 np->n_mtime = va.va_mtime;
2952 np->n_change = va.va_filerev;
2956 NFSVOPUNLOCK(vp, 0);
2958 } else if (!NFS_ISV4(vp)) {
2959 error = NFSVOPLOCK(vp, LK_SHARED);
2962 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2963 size = VTONFS(vp)->n_size;
2964 NFSVOPUNLOCK(vp, 0);
2965 error = lf_advlock(ap, &(vp->v_lockf), size);
2967 if (nfs_advlock_p != NULL)
2968 error = nfs_advlock_p(ap);
2970 NFSVOPUNLOCK(vp, 0);
2979 * NFS advisory byte-level locks.
2982 nfs_advlockasync(struct vop_advlockasync_args *ap)
2984 struct vnode *vp = ap->a_vp;
2989 return (EOPNOTSUPP);
2990 error = NFSVOPLOCK(vp, LK_SHARED);
2993 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2994 size = VTONFS(vp)->n_size;
2995 NFSVOPUNLOCK(vp, 0);
2996 error = lf_advlockasync(ap, &(vp->v_lockf), size);
2998 NFSVOPUNLOCK(vp, 0);
3005 * Print out the contents of an nfsnode.
3008 nfs_print(struct vop_print_args *ap)
3010 struct vnode *vp = ap->a_vp;
3011 struct nfsnode *np = VTONFS(vp);
3013 ncl_printf("\tfileid %ld fsid 0x%x",
3014 np->n_vattr.na_fileid, np->n_vattr.na_fsid);
3015 if (vp->v_type == VFIFO)
3022 * This is the "real" nfs::bwrite(struct buf*).
3023 * We set B_CACHE if this is a VMIO buffer.
3026 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3029 int oldflags = bp->b_flags;
3035 BUF_ASSERT_HELD(bp);
3037 if (bp->b_flags & B_INVAL) {
3042 bp->b_flags |= B_CACHE;
3045 * Undirty the bp. We will redirty it later if the I/O fails.
3050 bp->b_flags &= ~B_DONE;
3051 bp->b_ioflags &= ~BIO_ERROR;
3052 bp->b_iocmd = BIO_WRITE;
3054 bufobj_wref(bp->b_bufobj);
3055 curthread->td_ru.ru_oublock++;
3059 * Note: to avoid loopback deadlocks, we do not
3060 * assign b_runningbufspace.
3062 vfs_busy_pages(bp, 1);
3065 bp->b_iooffset = dbtob(bp->b_blkno);
3068 if( (oldflags & B_ASYNC) == 0) {
3069 int rtval = bufwait(bp);
3071 if (oldflags & B_DELWRI) {
3084 * nfs special file access vnode op.
3085 * Essentially just get vattr and then imitate iaccess() since the device is
3086 * local to the client.
3089 nfsspec_access(struct vop_access_args *ap)
3092 struct ucred *cred = ap->a_cred;
3093 struct vnode *vp = ap->a_vp;
3094 accmode_t accmode = ap->a_accmode;
3099 * Disallow write attempts on filesystems mounted read-only;
3100 * unless the file is a socket, fifo, or a block or character
3101 * device resident on the filesystem.
3103 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3104 switch (vp->v_type) {
3114 error = VOP_GETATTR(vp, vap, cred);
3117 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3118 accmode, cred, NULL);
3124 * Read wrapper for fifos.
3127 nfsfifo_read(struct vop_read_args *ap)
3129 struct nfsnode *np = VTONFS(ap->a_vp);
3135 mtx_lock(&np->n_mtx);
3137 getnanotime(&np->n_atim);
3138 mtx_unlock(&np->n_mtx);
3139 error = fifo_specops.vop_read(ap);
3144 * Write wrapper for fifos.
3147 nfsfifo_write(struct vop_write_args *ap)
3149 struct nfsnode *np = VTONFS(ap->a_vp);
3154 mtx_lock(&np->n_mtx);
3156 getnanotime(&np->n_mtim);
3157 mtx_unlock(&np->n_mtx);
3158 return(fifo_specops.vop_write(ap));
3162 * Close wrapper for fifos.
3164 * Update the times on the nfsnode then do fifo close.
3167 nfsfifo_close(struct vop_close_args *ap)
3169 struct vnode *vp = ap->a_vp;
3170 struct nfsnode *np = VTONFS(vp);
3174 mtx_lock(&np->n_mtx);
3175 if (np->n_flag & (NACC | NUPD)) {
3177 if (np->n_flag & NACC)
3179 if (np->n_flag & NUPD)
3182 if (vrefcnt(vp) == 1 &&
3183 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3185 if (np->n_flag & NACC)
3186 vattr.va_atime = np->n_atim;
3187 if (np->n_flag & NUPD)
3188 vattr.va_mtime = np->n_mtim;
3189 mtx_unlock(&np->n_mtx);
3190 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3194 mtx_unlock(&np->n_mtx);
3196 return (fifo_specops.vop_close(ap));
3200 * Just call ncl_writebp() with the force argument set to 1.
3202 * NOTE: B_DONE may or may not be set in a_bp on call.
3205 nfs_bwrite(struct buf *bp)
3208 return (ncl_writebp(bp, 1, curthread));
3211 struct buf_ops buf_ops_newnfs = {
3212 .bop_name = "buf_ops_nfs",
3213 .bop_write = nfs_bwrite,
3214 .bop_strategy = bufstrategy,
3215 .bop_sync = bufsync,
3216 .bop_bdflush = bufbdflush,
3220 * Cloned from vop_stdlock(), and then the ugly hack added.
3223 nfs_lock1(struct vop_lock1_args *ap)
3225 struct vnode *vp = ap->a_vp;
3229 * Since vfs_hash_get() calls vget() and it will no longer work
3230 * for FreeBSD8 with flags == 0, I can only think of this horrible
3231 * hack to work around it. I call vfs_hash_get() with LK_EXCLOTHER
3232 * and then handle it here. All I want for this case is a v_usecount
3233 * on the vnode to use for recovery, while another thread might
3234 * hold a lock on the vnode. I have the other threads blocked, so
3235 * there isn't any race problem.
3237 if ((ap->a_flags & LK_TYPE_MASK) == LK_EXCLOTHER) {
3238 if ((ap->a_flags & LK_INTERLOCK) == 0)
3240 if ((vp->v_iflag & VI_DOOMED))
3245 return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
3246 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
3251 nfs_getacl(struct vop_getacl_args *ap)
3255 if (ap->a_type != ACL_TYPE_NFS4)
3256 return (EOPNOTSUPP);
3257 error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3259 if (error > NFSERR_STALE) {
3260 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3267 nfs_setacl(struct vop_setacl_args *ap)
3271 if (ap->a_type != ACL_TYPE_NFS4)
3272 return (EOPNOTSUPP);
3273 error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3275 if (error > NFSERR_STALE) {
3276 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3283 * Return POSIX pathconf information applicable to nfs filesystems.
3286 nfs_pathconf(struct vop_pathconf_args *ap)
3288 struct nfsv3_pathconf pc;
3289 struct nfsvattr nfsva;
3290 struct vnode *vp = ap->a_vp;
3291 struct thread *td = curthread;
3292 int attrflag, error;
3294 if (NFS_ISV4(vp) || (NFS_ISV3(vp) && (ap->a_name == _PC_LINK_MAX ||
3295 ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
3296 ap->a_name == _PC_NO_TRUNC))) {
3298 * Since only the above 4 a_names are returned by the NFSv3
3299 * Pathconf RPC, there is no point in doing it for others.
3301 error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
3304 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
3310 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
3313 pc.pc_linkmax = LINK_MAX;
3314 pc.pc_namemax = NFS_MAXNAMLEN;
3316 pc.pc_chownrestricted = 1;
3317 pc.pc_caseinsensitive = 0;
3318 pc.pc_casepreserving = 1;
3321 switch (ap->a_name) {
3323 *ap->a_retval = pc.pc_linkmax;
3326 *ap->a_retval = pc.pc_namemax;
3329 *ap->a_retval = PATH_MAX;
3332 *ap->a_retval = PIPE_BUF;
3334 case _PC_CHOWN_RESTRICTED:
3335 *ap->a_retval = pc.pc_chownrestricted;
3338 *ap->a_retval = pc.pc_notrunc;
3340 case _PC_ACL_EXTENDED:
3344 if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
3345 NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
3350 case _PC_ACL_PATH_MAX:
3352 *ap->a_retval = ACL_MAX_ENTRIES;
3356 case _PC_MAC_PRESENT:
3360 /* _PC_ASYNC_IO should have been handled by upper layers. */
3361 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
3370 case _PC_ALLOC_SIZE_MIN:
3371 *ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
3373 case _PC_FILESIZEBITS:
3379 case _PC_REC_INCR_XFER_SIZE:
3380 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3382 case _PC_REC_MAX_XFER_SIZE:
3383 *ap->a_retval = -1; /* means ``unlimited'' */
3385 case _PC_REC_MIN_XFER_SIZE:
3386 *ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
3388 case _PC_REC_XFER_ALIGN:
3389 *ap->a_retval = PAGE_SIZE;
3391 case _PC_SYMLINK_MAX:
3392 *ap->a_retval = NFS_MAXPATHLEN;