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 * 3. 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 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
33 * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38 * vnode op calls for Sun NFS version 2 and 3
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/resourcevar.h>
48 #include <sys/mount.h>
50 #include <sys/malloc.h>
52 #include <sys/namei.h>
53 #include <sys/nlookup.h>
54 #include <sys/socket.h>
55 #include <sys/vnode.h>
56 #include <sys/dirent.h>
57 #include <sys/fcntl.h>
58 #include <sys/lockf.h>
60 #include <sys/sysctl.h>
64 #include <vm/vm_extern.h>
68 #include <vfs/fifofs/fifo.h>
69 #include <vfs/ufs/dir.h>
79 #include "nfsm_subs.h"
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
89 static int nfsfifo_read (struct vop_read_args *);
90 static int nfsfifo_write (struct vop_write_args *);
91 static int nfsfifo_close (struct vop_close_args *);
92 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
93 static int nfs_lookup (struct vop_old_lookup_args *);
94 static int nfs_create (struct vop_old_create_args *);
95 static int nfs_mknod (struct vop_old_mknod_args *);
96 static int nfs_open (struct vop_open_args *);
97 static int nfs_close (struct vop_close_args *);
98 static int nfs_access (struct vop_access_args *);
99 static int nfs_getattr (struct vop_getattr_args *);
100 static int nfs_setattr (struct vop_setattr_args *);
101 static int nfs_read (struct vop_read_args *);
102 static int nfs_fsync (struct vop_fsync_args *);
103 static int nfs_remove (struct vop_old_remove_args *);
104 static int nfs_link (struct vop_old_link_args *);
105 static int nfs_rename (struct vop_old_rename_args *);
106 static int nfs_mkdir (struct vop_old_mkdir_args *);
107 static int nfs_rmdir (struct vop_old_rmdir_args *);
108 static int nfs_symlink (struct vop_old_symlink_args *);
109 static int nfs_readdir (struct vop_readdir_args *);
110 static int nfs_bmap (struct vop_bmap_args *);
111 static int nfs_strategy (struct vop_strategy_args *);
112 static int nfs_lookitup (struct vnode *, const char *, int,
113 struct ucred *, struct thread *, struct nfsnode **);
114 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
115 static int nfs_laccess (struct vop_access_args *);
116 static int nfs_readlink (struct vop_readlink_args *);
117 static int nfs_print (struct vop_print_args *);
118 static int nfs_advlock (struct vop_advlock_args *);
119 static int nfs_kqfilter (struct vop_kqfilter_args *ap);
121 static int nfs_nresolve (struct vop_nresolve_args *);
123 * Global vfs data structures for nfs
125 struct vop_ops nfsv2_vnode_vops = {
126 .vop_default = vop_defaultop,
127 .vop_access = nfs_access,
128 .vop_advlock = nfs_advlock,
129 .vop_bmap = nfs_bmap,
130 .vop_close = nfs_close,
131 .vop_old_create = nfs_create,
132 .vop_fsync = nfs_fsync,
133 .vop_getattr = nfs_getattr,
134 .vop_getpages = vop_stdgetpages,
135 .vop_putpages = vop_stdputpages,
136 .vop_inactive = nfs_inactive,
137 .vop_old_link = nfs_link,
138 .vop_old_lookup = nfs_lookup,
139 .vop_old_mkdir = nfs_mkdir,
140 .vop_old_mknod = nfs_mknod,
141 .vop_open = nfs_open,
142 .vop_print = nfs_print,
143 .vop_read = nfs_read,
144 .vop_readdir = nfs_readdir,
145 .vop_readlink = nfs_readlink,
146 .vop_reclaim = nfs_reclaim,
147 .vop_old_remove = nfs_remove,
148 .vop_old_rename = nfs_rename,
149 .vop_old_rmdir = nfs_rmdir,
150 .vop_setattr = nfs_setattr,
151 .vop_strategy = nfs_strategy,
152 .vop_old_symlink = nfs_symlink,
153 .vop_write = nfs_write,
154 .vop_nresolve = nfs_nresolve,
155 .vop_kqfilter = nfs_kqfilter
159 * Special device vnode ops
161 struct vop_ops nfsv2_spec_vops = {
162 .vop_default = vop_defaultop,
163 .vop_access = nfs_laccess,
164 .vop_close = nfs_close,
165 .vop_fsync = nfs_fsync,
166 .vop_getattr = nfs_getattr,
167 .vop_inactive = nfs_inactive,
168 .vop_print = nfs_print,
169 .vop_read = vop_stdnoread,
170 .vop_reclaim = nfs_reclaim,
171 .vop_setattr = nfs_setattr,
172 .vop_write = vop_stdnowrite
175 struct vop_ops nfsv2_fifo_vops = {
176 .vop_default = fifo_vnoperate,
177 .vop_access = nfs_laccess,
178 .vop_close = nfsfifo_close,
179 .vop_fsync = nfs_fsync,
180 .vop_getattr = nfs_getattr,
181 .vop_inactive = nfs_inactive,
182 .vop_print = nfs_print,
183 .vop_read = nfsfifo_read,
184 .vop_reclaim = nfs_reclaim,
185 .vop_setattr = nfs_setattr,
186 .vop_write = nfsfifo_write
189 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
190 struct componentname *cnp,
192 static int nfs_removerpc (struct vnode *dvp, const char *name,
194 struct ucred *cred, struct thread *td);
195 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
196 int fnamelen, struct vnode *tdvp,
197 const char *tnameptr, int tnamelen,
198 struct ucred *cred, struct thread *td);
199 static int nfs_renameit (struct vnode *sdvp,
200 struct componentname *scnp,
201 struct sillyrename *sp);
203 SYSCTL_DECL(_vfs_nfs);
205 static int nfs_flush_on_rename = 1;
206 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
207 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
208 static int nfs_flush_on_hlink = 0;
209 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
210 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
212 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
213 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
214 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
216 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
217 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
218 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
220 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
221 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
222 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
224 static int nfsv3_commit_on_close = 0;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
226 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
228 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
229 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
231 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
232 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
235 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
236 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
237 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
241 nfs_knote(struct vnode *vp, int flags)
244 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
248 * Returns whether a name component is a degenerate '.' or '..'.
252 nlcdegenerate(struct nlcomponent *nlc)
254 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
256 if (nlc->nlc_namelen == 2 &&
257 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
263 nfs3_access_otw(struct vnode *vp, int wmode,
264 struct thread *td, struct ucred *cred)
266 struct nfsnode *np = VTONFS(vp);
271 struct nfsm_info info;
276 nfsstats.rpccnt[NFSPROC_ACCESS]++;
277 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
278 NFSX_FH(info.v3) + NFSX_UNSIGNED);
279 ERROROUT(nfsm_fhtom(&info, vp));
280 tl = nfsm_build(&info, NFSX_UNSIGNED);
281 *tl = txdr_unsigned(wmode);
282 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
283 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
285 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
286 rmode = fxdr_unsigned(u_int32_t, *tl);
288 np->n_modeuid = cred->cr_uid;
289 np->n_modestamp = mycpu->gd_time_seconds;
298 * nfs access vnode op.
299 * For nfs version 2, just return ok. File accesses may fail later.
300 * For nfs version 3, use the access rpc to check accessibility. If file modes
301 * are changed on the server, accesses might still fail later.
303 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
306 nfs_access(struct vop_access_args *ap)
309 struct vnode *vp = ap->a_vp;
310 thread_t td = curthread;
312 u_int32_t mode, wmode;
313 struct nfsnode *np = VTONFS(vp);
314 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
315 int v3 = NFS_ISV3(vp);
317 lwkt_gettoken(&nmp->nm_token);
320 * Disallow write attempts on filesystems mounted read-only;
321 * unless the file is a socket, fifo, or a block or character
322 * device resident on the filesystem.
324 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
325 switch (vp->v_type) {
329 lwkt_reltoken(&nmp->nm_token);
337 * The NFS protocol passes only the effective uid/gid over the wire but
338 * we need to check access against real ids if AT_EACCESS not set.
339 * Handle this case by cloning the credentials and setting the
340 * effective ids to the real ones.
342 * The crdup() here can cause a lot of ucred structures to build-up
343 * (up to maxvnodes), so do our best to avoid it.
345 if (ap->a_flags & AT_EACCESS) {
346 cred = crhold(ap->a_cred);
349 if (cred->cr_uid == cred->cr_ruid &&
350 cred->cr_gid == cred->cr_rgid) {
351 cred = crhold(ap->a_cred);
353 cred = crdup(ap->a_cred);
354 cred->cr_uid = cred->cr_ruid;
355 cred->cr_gid = cred->cr_rgid;
360 * For nfs v3, check to see if we have done this recently, and if
361 * so return our cached result instead of making an ACCESS call.
362 * If not, do an access rpc, otherwise you are stuck emulating
363 * ufs_access() locally using the vattr. This may not be correct,
364 * since the server may apply other access criteria such as
365 * client uid-->server uid mapping that we do not know about.
368 if (ap->a_mode & VREAD)
369 mode = NFSV3ACCESS_READ;
372 if (vp->v_type != VDIR) {
373 if (ap->a_mode & VWRITE)
374 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
375 if (ap->a_mode & VEXEC)
376 mode |= NFSV3ACCESS_EXECUTE;
378 if (ap->a_mode & VWRITE)
379 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
381 if (ap->a_mode & VEXEC)
382 mode |= NFSV3ACCESS_LOOKUP;
384 /* XXX safety belt, only make blanket request if caching */
385 if (nfsaccess_cache_timeout > 0) {
386 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
387 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
388 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
394 * Does our cached result allow us to give a definite yes to
397 if (np->n_modestamp &&
398 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
399 (cred->cr_uid == np->n_modeuid) &&
400 ((np->n_mode & mode) == mode)) {
401 nfsstats.accesscache_hits++;
404 * Either a no, or a don't know. Go to the wire.
406 nfsstats.accesscache_misses++;
407 error = nfs3_access_otw(vp, wmode, td, cred);
409 if ((np->n_mode & mode) != mode) {
415 if ((error = nfs_laccess(ap)) != 0) {
417 lwkt_reltoken(&nmp->nm_token);
422 * Attempt to prevent a mapped root from accessing a file
423 * which it shouldn't. We try to read a byte from the file
424 * if the user is root and the file is not zero length.
425 * After calling nfs_laccess, we should have the correct
428 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
429 && VTONFS(vp)->n_size > 0) {
436 auio.uio_iov = &aiov;
440 auio.uio_segflg = UIO_SYSSPACE;
441 auio.uio_rw = UIO_READ;
444 if (vp->v_type == VREG) {
445 error = nfs_readrpc_uio(vp, &auio);
446 } else if (vp->v_type == VDIR) {
448 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
450 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
451 error = nfs_readdirrpc_uio(vp, &auio);
453 } else if (vp->v_type == VLNK) {
454 error = nfs_readlinkrpc_uio(vp, &auio);
461 * [re]record creds for reading and/or writing if access
462 * was granted. Assume the NFS server will grant read access
463 * for execute requests.
466 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
469 crfree(np->n_rucred);
472 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
475 crfree(np->n_wucred);
479 lwkt_reltoken(&nmp->nm_token);
487 * Check to see if the type is ok
488 * and that deletion is not in progress.
489 * For paged in text files, you will need to flush the page cache
490 * if consistency is lost.
492 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
497 nfs_open(struct vop_open_args *ap)
499 struct vnode *vp = ap->a_vp;
500 struct nfsnode *np = VTONFS(vp);
501 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
505 lwkt_gettoken(&nmp->nm_token);
507 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
509 kprintf("open eacces vtyp=%d\n",vp->v_type);
511 lwkt_reltoken(&nmp->nm_token);
516 * Save valid creds for reading and writing for later RPCs.
518 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
521 crfree(np->n_rucred);
522 np->n_rucred = ap->a_cred;
524 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
527 crfree(np->n_wucred);
528 np->n_wucred = ap->a_cred;
532 * Clear the attribute cache only if opening with write access. It
533 * is unclear if we should do this at all here, but we certainly
534 * should not clear the cache unconditionally simply because a file
537 if (ap->a_mode & FWRITE)
541 * For normal NFS, reconcile changes made locally verses
542 * changes made remotely. Note that VOP_GETATTR only goes
543 * to the wire if the cached attribute has timed out or been
546 * If local modifications have been made clear the attribute
547 * cache to force an attribute and modified time check. If
548 * GETATTR detects that the file has been changed by someone
549 * other then us it will set NRMODIFIED.
551 * If we are opening a directory and local changes have been
552 * made we have to invalidate the cache in order to ensure
553 * that we get the most up-to-date information from the
556 if (np->n_flag & NLMODIFIED) {
558 if (vp->v_type == VDIR) {
559 error = nfs_vinvalbuf(vp, V_SAVE, 1);
560 if (error == EINTR) {
561 lwkt_reltoken(&nmp->nm_token);
567 error = VOP_GETATTR(vp, &vattr);
569 lwkt_reltoken(&nmp->nm_token);
572 if (np->n_flag & NRMODIFIED) {
573 if (vp->v_type == VDIR)
575 error = nfs_vinvalbuf(vp, V_SAVE, 1);
576 if (error == EINTR) {
577 lwkt_reltoken(&nmp->nm_token);
580 np->n_flag &= ~NRMODIFIED;
582 error = vop_stdopen(ap);
583 lwkt_reltoken(&nmp->nm_token);
590 * What an NFS client should do upon close after writing is a debatable issue.
591 * Most NFS clients push delayed writes to the server upon close, basically for
593 * 1 - So that any write errors may be reported back to the client process
594 * doing the close system call. By far the two most likely errors are
595 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
596 * 2 - To put a worst case upper bound on cache inconsistency between
597 * multiple clients for the file.
598 * There is also a consistency problem for Version 2 of the protocol w.r.t.
599 * not being able to tell if other clients are writing a file concurrently,
600 * since there is no way of knowing if the changed modify time in the reply
601 * is only due to the write for this client.
602 * (NFS Version 3 provides weak cache consistency data in the reply that
603 * should be sufficient to detect and handle this case.)
605 * The current code does the following:
606 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
607 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
608 * or commit them (this satisfies 1 and 2 except for the
609 * case where the server crashes after this close but
610 * before the commit RPC, which is felt to be "good
611 * enough". Changing the last argument to nfs_flush() to
612 * a 1 would force a commit operation, if it is felt a
613 * commit is necessary now.
614 * for NQNFS - do nothing now, since 2 is dealt with via leases and
615 * 1 should be dealt with via an fsync() system call for
616 * cases where write errors are important.
618 * nfs_close(struct vnode *a_vp, int a_fflag)
622 nfs_close(struct vop_close_args *ap)
624 struct vnode *vp = ap->a_vp;
625 struct nfsnode *np = VTONFS(vp);
626 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
628 thread_t td = curthread;
630 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
631 lwkt_gettoken(&nmp->nm_token);
633 if (vp->v_type == VREG) {
634 if (np->n_flag & NLMODIFIED) {
637 * Under NFSv3 we have dirty buffers to dispose of. We
638 * must flush them to the NFS server. We have the option
639 * of waiting all the way through the commit rpc or just
640 * waiting for the initial write. The default is to only
641 * wait through the initial write so the data is in the
642 * server's cache, which is roughly similar to the state
643 * a standard disk subsystem leaves the file in on close().
645 * We cannot clear the NLMODIFIED bit in np->n_flag due to
646 * potential races with other processes, and certainly
647 * cannot clear it if we don't commit.
649 int cm = nfsv3_commit_on_close ? 1 : 0;
650 error = nfs_flush(vp, MNT_WAIT, td, cm);
651 /* np->n_flag &= ~NLMODIFIED; */
653 error = nfs_vinvalbuf(vp, V_SAVE, 1);
657 if (np->n_flag & NWRITEERR) {
658 np->n_flag &= ~NWRITEERR;
663 lwkt_reltoken(&nmp->nm_token);
669 * nfs getattr call from vfs.
671 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
674 nfs_getattr(struct vop_getattr_args *ap)
676 struct vnode *vp = ap->a_vp;
677 struct nfsnode *np = VTONFS(vp);
678 struct nfsmount *nmp;
680 thread_t td = curthread;
681 struct nfsm_info info;
684 info.v3 = NFS_ISV3(vp);
685 nmp = VFSTONFS(vp->v_mount);
687 lwkt_gettoken(&nmp->nm_token);
690 * Update local times for special files.
692 if (np->n_flag & (NACC | NUPD))
695 * First look in the cache.
697 if (nfs_getattrcache(vp, ap->a_vap) == 0)
700 if (info.v3 && nfsaccess_cache_timeout > 0) {
701 nfsstats.accesscache_misses++;
702 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
703 if (nfs_getattrcache(vp, ap->a_vap) == 0)
707 nfsstats.rpccnt[NFSPROC_GETATTR]++;
708 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
709 ERROROUT(nfsm_fhtom(&info, vp));
710 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
711 nfs_vpcred(vp, ND_CHECK), &error));
713 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
719 * NFS doesn't support chflags flags. If the nfs mount was
720 * made -o cache set the UF_CACHE bit for swapcache.
722 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
723 ap->a_vap->va_flags |= UF_CACHE;
725 lwkt_reltoken(&nmp->nm_token);
732 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
735 nfs_setattr(struct vop_setattr_args *ap)
737 struct vnode *vp = ap->a_vp;
738 struct nfsnode *np = VTONFS(vp);
739 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
740 struct vattr *vap = ap->a_vap;
744 thread_t td = curthread;
750 * Setting of flags is not supported.
752 if (vap->va_flags != VNOVAL)
756 * Disallow write attempts if the filesystem is mounted read-only.
758 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
759 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
760 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
761 (vp->v_mount->mnt_flag & MNT_RDONLY))
764 lwkt_gettoken(&nmp->nm_token);
767 * Handle size changes
769 if (vap->va_size != VNOVAL) {
771 * truncation requested
773 switch (vp->v_type) {
775 lwkt_reltoken(&nmp->nm_token);
781 if (vap->va_mtime.tv_sec == VNOVAL &&
782 vap->va_atime.tv_sec == VNOVAL &&
783 vap->va_mode == (mode_t)VNOVAL &&
784 vap->va_uid == (uid_t)VNOVAL &&
785 vap->va_gid == (gid_t)VNOVAL) {
786 lwkt_reltoken(&nmp->nm_token);
789 vap->va_size = VNOVAL;
793 * Disallow write attempts if the filesystem is
796 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
797 lwkt_reltoken(&nmp->nm_token);
803 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
806 if (np->n_flag & NLMODIFIED) {
807 if (vap->va_size == 0)
808 error = nfs_vinvalbuf(vp, 0, 1);
810 error = nfs_vinvalbuf(vp, V_SAVE, 1);
814 * note: this loop case almost always happens at
815 * least once per truncation.
817 if (error == 0 && np->n_size != vap->va_size)
819 np->n_vattr.va_size = vap->va_size;
820 kflags |= NOTE_WRITE;
821 if (tsize < vap->va_size)
822 kflags |= NOTE_EXTEND;
828 * If setting the mtime or if server/other-client modifications have
829 * been detected, we must fully flush any pending writes.
831 * This will slow down cp/cpdup/rdist/rsync and other operations which
832 * might call [l]utimes() to set the mtime after writing to a file,
833 * but honestly there is no way to properly defer the write flush
834 * and still get reasonably accurate/dependable synchronization of
837 if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
838 if ((np->n_flag & NRMODIFIED) ||
839 (vap->va_mtime.tv_sec != VNOVAL)) {
840 error = nfs_vinvalbuf(vp, V_SAVE, 1);
841 if (error == EINTR) {
842 lwkt_reltoken(&nmp->nm_token);
849 * Get the blasted mtime to report properly.
851 if (vap->va_mtime.tv_sec != VNOVAL) {
852 np->n_mtime = vap->va_mtime.tv_sec;
854 np->n_vattr.va_mtime = vap->va_mtime;
858 * Issue the setattr rpc, adjust our mtime and make sure NUPD
859 * has been cleared so it does not get overridden.
861 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
863 kflags |= NOTE_EXTEND;
866 * Sanity check if a truncation was issued. This should only occur
867 * if multiple processes are racing on the same file.
869 if (error == 0 && vap->va_size != VNOVAL &&
870 np->n_size != vap->va_size) {
871 kprintf("NFS ftruncate: server disagrees on the file size: "
874 (intmax_t)vap->va_size,
875 (intmax_t)np->n_size);
878 if (error && vap->va_size != VNOVAL) {
879 np->n_size = np->n_vattr.va_size = tsize;
880 nfs_meta_setsize(vp, td, np->n_size, 0);
882 lwkt_reltoken(&nmp->nm_token);
883 nfs_knote(vp, kflags);
889 * Do an nfs setattr rpc.
892 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
893 struct ucred *cred, struct thread *td)
895 struct nfsv2_sattr *sp;
896 struct nfsnode *np = VTONFS(vp);
898 int error = 0, wccflag = NFSV3_WCCRATTR;
899 struct nfsm_info info;
902 info.v3 = NFS_ISV3(vp);
904 nfsstats.rpccnt[NFSPROC_SETATTR]++;
905 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
906 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
907 ERROROUT(nfsm_fhtom(&info, vp));
909 nfsm_v3attrbuild(&info, vap, TRUE);
910 tl = nfsm_build(&info, NFSX_UNSIGNED);
913 sp = nfsm_build(&info, NFSX_V2SATTR);
914 if (vap->va_mode == (mode_t)VNOVAL)
915 sp->sa_mode = nfs_xdrneg1;
917 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
918 if (vap->va_uid == (uid_t)VNOVAL)
919 sp->sa_uid = nfs_xdrneg1;
921 sp->sa_uid = txdr_unsigned(vap->va_uid);
922 if (vap->va_gid == (gid_t)VNOVAL)
923 sp->sa_gid = nfs_xdrneg1;
925 sp->sa_gid = txdr_unsigned(vap->va_gid);
926 sp->sa_size = txdr_unsigned(vap->va_size);
927 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
928 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
930 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
933 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
935 ERROROUT(nfsm_loadattr(&info, vp, NULL));
945 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
951 cache_setvp(nch, vp);
952 cache_settimeout(nch, nctimeout);
956 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
957 * nfs_lookup() until all remaining new api calls are implemented.
959 * Resolve a namecache entry. This function is passed a locked ncp and
960 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
963 nfs_nresolve(struct vop_nresolve_args *ap)
965 struct thread *td = curthread;
966 struct namecache *ncp;
967 struct nfsmount *nmp;
977 struct nfsm_info info;
980 nmp = VFSTONFS(dvp->v_mount);
982 lwkt_gettoken(&nmp->nm_token);
984 if ((error = vget(dvp, LK_SHARED)) != 0) {
985 lwkt_reltoken(&nmp->nm_token);
990 info.v3 = NFS_ISV3(dvp);
993 nfsstats.lookupcache_misses++;
994 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
995 ncp = ap->a_nch->ncp;
997 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
998 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
999 ERROROUT(nfsm_fhtom(&info, dvp));
1000 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
1001 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
1002 ap->a_cred, &error));
1005 * Cache negatve lookups to reduce NFS traffic, but use
1006 * a fast timeout. Otherwise use a timeout of 1 tick.
1007 * XXX we should add a namecache flag for no-caching
1008 * to uncache the negative hit as soon as possible, but
1009 * we cannot simply destroy the entry because it is used
1010 * as a placeholder by the caller.
1012 * The refactored nfs code will overwrite a non-zero error
1013 * with 0 when we use ERROROUT(), so don't here.
1015 if (error == ENOENT)
1016 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
1017 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1018 NFS_LATTR_NOSHRINK);
1029 * Success, get the file handle, do various checks, and load
1030 * post-operation data from the reply packet. Theoretically
1031 * we should never be looking up "." so, theoretically, we
1032 * should never get the same file handle as our directory. But
1033 * we check anyway. XXX
1035 * Note that no timeout is set for the positive cache hit. We
1036 * assume, theoretically, that ESTALE returns will be dealt with
1037 * properly to handle NFS races and in anycase we cannot depend
1038 * on a timeout to deal with NFS open/create/excl issues so instead
1039 * of a bad hack here the rest of the NFS client code needs to do
1042 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1045 if (NFS_CMPFH(np, fhp, fhsize)) {
1049 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1054 lwkt_reltoken(&nmp->nm_token);
1060 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1061 NFS_LATTR_NOSHRINK));
1062 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1063 NFS_LATTR_NOSHRINK));
1065 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1067 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1071 lwkt_reltoken(&nmp->nm_token);
1083 * 'cached' nfs directory lookup
1085 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1087 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1088 * struct componentname *a_cnp)
1091 nfs_lookup(struct vop_old_lookup_args *ap)
1093 struct componentname *cnp = ap->a_cnp;
1094 struct vnode *dvp = ap->a_dvp;
1095 struct vnode **vpp = ap->a_vpp;
1096 int flags = cnp->cn_flags;
1097 struct vnode *newvp;
1098 struct vnode *notvp;
1099 struct nfsmount *nmp;
1103 int lockparent, wantparent, attrflag, fhsize;
1106 struct nfsm_info info;
1109 info.v3 = NFS_ISV3(dvp);
1112 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1115 * Read-only mount check and directory check.
1118 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1119 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1122 if (dvp->v_type != VDIR)
1126 * Look it up in the cache. Note that ENOENT is only returned if we
1127 * previously entered a negative hit (see later on). The additional
1128 * nfsneg_cache_timeout check causes previously cached results to
1129 * be instantly ignored if the negative caching is turned off.
1131 lockparent = flags & CNP_LOCKPARENT;
1132 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1133 nmp = VFSTONFS(dvp->v_mount);
1136 lwkt_gettoken(&nmp->nm_token);
1143 nfsstats.lookupcache_misses++;
1144 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1145 len = cnp->cn_namelen;
1146 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1147 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1148 ERROROUT(nfsm_fhtom(&info, dvp));
1149 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1150 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1151 cnp->cn_cred, &error));
1153 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1154 NFS_LATTR_NOSHRINK);
1164 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1167 * Handle RENAME case...
1169 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1170 if (NFS_CMPFH(np, fhp, fhsize)) {
1173 lwkt_reltoken(&nmp->nm_token);
1176 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1180 lwkt_reltoken(&nmp->nm_token);
1185 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1186 NFS_LATTR_NOSHRINK));
1187 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1188 NFS_LATTR_NOSHRINK));
1190 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1197 cnp->cn_flags |= CNP_PDIRUNLOCK;
1199 lwkt_reltoken(&nmp->nm_token);
1203 if (flags & CNP_ISDOTDOT) {
1205 cnp->cn_flags |= CNP_PDIRUNLOCK;
1206 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1208 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1209 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1210 lwkt_reltoken(&nmp->nm_token);
1211 return (error); /* NOTE: return error from nget */
1215 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1218 lwkt_reltoken(&nmp->nm_token);
1221 cnp->cn_flags |= CNP_PDIRUNLOCK;
1223 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1227 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1231 lwkt_reltoken(&nmp->nm_token);
1236 cnp->cn_flags |= CNP_PDIRUNLOCK;
1241 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1242 NFS_LATTR_NOSHRINK));
1243 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1244 NFS_LATTR_NOSHRINK));
1246 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1249 /* XXX MOVE TO nfs_nremove() */
1250 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1251 cnp->cn_nameiop != NAMEI_DELETE) {
1252 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1260 if (newvp != NULLVP) {
1264 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1265 cnp->cn_nameiop == NAMEI_RENAME) &&
1269 cnp->cn_flags |= CNP_PDIRUNLOCK;
1271 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1274 error = EJUSTRETURN;
1277 lwkt_reltoken(&nmp->nm_token);
1283 * Just call nfs_bioread() to do the work.
1285 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1286 * struct ucred *a_cred)
1289 nfs_read(struct vop_read_args *ap)
1291 struct vnode *vp = ap->a_vp;
1292 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1295 lwkt_gettoken(&nmp->nm_token);
1296 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1297 lwkt_reltoken(&nmp->nm_token);
1305 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1308 nfs_readlink(struct vop_readlink_args *ap)
1310 struct vnode *vp = ap->a_vp;
1311 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1314 if (vp->v_type != VLNK)
1317 lwkt_gettoken(&nmp->nm_token);
1318 error = nfs_bioread(vp, ap->a_uio, 0);
1319 lwkt_reltoken(&nmp->nm_token);
1325 * Do a readlink rpc.
1326 * Called by nfs_doio() from below the buffer cache.
1329 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1331 int error = 0, len, attrflag;
1332 struct nfsm_info info;
1335 info.v3 = NFS_ISV3(vp);
1337 nfsstats.rpccnt[NFSPROC_READLINK]++;
1338 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1339 ERROROUT(nfsm_fhtom(&info, vp));
1340 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1341 nfs_vpcred(vp, ND_CHECK), &error));
1343 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1344 NFS_LATTR_NOSHRINK));
1347 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1348 if (len == NFS_MAXPATHLEN) {
1349 struct nfsnode *np = VTONFS(vp);
1350 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1353 ERROROUT(nfsm_mtouio(&info, uiop, len));
1362 * nfs synchronous read rpc using UIO
1365 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1368 struct nfsmount *nmp;
1369 int error = 0, len, retlen, tsiz, eof, attrflag;
1370 struct nfsm_info info;
1374 info.v3 = NFS_ISV3(vp);
1379 nmp = VFSTONFS(vp->v_mount);
1381 tsiz = uiop->uio_resid;
1382 tmp_off = uiop->uio_offset + tsiz;
1383 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1385 tmp_off = uiop->uio_offset;
1387 nfsstats.rpccnt[NFSPROC_READ]++;
1388 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1389 nfsm_reqhead(&info, vp, NFSPROC_READ,
1390 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1391 ERROROUT(nfsm_fhtom(&info, vp));
1392 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1394 txdr_hyper(uiop->uio_offset, tl);
1395 *(tl + 2) = txdr_unsigned(len);
1397 *tl++ = txdr_unsigned(uiop->uio_offset);
1398 *tl++ = txdr_unsigned(len);
1401 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1402 nfs_vpcred(vp, ND_READ), &error));
1404 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1405 NFS_LATTR_NOSHRINK));
1406 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1407 eof = fxdr_unsigned(int, *(tl + 1));
1409 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1411 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1412 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1417 * Handle short-read from server (NFSv3). If EOF is not
1418 * flagged (and no error occurred), but retlen is less
1419 * then the request size, we must zero-fill the remainder.
1421 if (retlen < len && info.v3 && eof == 0) {
1422 ERROROUT(uiomovez(len - retlen, uiop));
1428 * Terminate loop on EOF or zero-length read.
1430 * For NFSv2 a short-read indicates EOF, not zero-fill,
1431 * and also terminates the loop.
1434 if (eof || retlen == 0)
1436 } else if (retlen < len) {
1448 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1449 int *iomode, int *must_commit)
1453 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1454 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1455 int committed = NFSV3WRITE_FILESYNC;
1456 struct nfsm_info info;
1459 info.v3 = NFS_ISV3(vp);
1462 if (uiop->uio_iovcnt != 1)
1463 panic("nfs: writerpc iovcnt > 1");
1466 tsiz = uiop->uio_resid;
1467 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1470 nfsstats.rpccnt[NFSPROC_WRITE]++;
1471 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1472 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1473 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1475 ERROROUT(nfsm_fhtom(&info, vp));
1477 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1478 txdr_hyper(uiop->uio_offset, tl);
1480 *tl++ = txdr_unsigned(len);
1481 *tl++ = txdr_unsigned(*iomode);
1482 *tl = txdr_unsigned(len);
1486 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1487 /* Set both "begin" and "current" to non-garbage. */
1488 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1489 *tl++ = x; /* "begin offset" */
1490 *tl++ = x; /* "current offset" */
1491 x = txdr_unsigned(len);
1492 *tl++ = x; /* total to this offset */
1493 *tl = x; /* size of this write */
1495 ERROROUT(nfsm_uiotom(&info, uiop, len));
1496 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1497 nfs_vpcred(vp, ND_WRITE), &error));
1500 * The write RPC returns a before and after mtime. The
1501 * nfsm_wcc_data() macro checks the before n_mtime
1502 * against the before time and stores the after time
1503 * in the nfsnode's cached vattr and n_mtime field.
1504 * The NRMODIFIED bit will be set if the before
1505 * time did not match the original mtime.
1507 wccflag = NFSV3_WCCCHK;
1508 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1510 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1511 rlen = fxdr_unsigned(int, *tl++);
1517 } else if (rlen < len) {
1518 backup = len - rlen;
1519 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1520 uiop->uio_iov->iov_len += backup;
1521 uiop->uio_offset -= backup;
1522 uiop->uio_resid += backup;
1525 commit = fxdr_unsigned(int, *tl++);
1528 * Return the lowest committment level
1529 * obtained by any of the RPCs.
1531 if (committed == NFSV3WRITE_FILESYNC)
1533 else if (committed == NFSV3WRITE_DATASYNC &&
1534 commit == NFSV3WRITE_UNSTABLE)
1536 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1537 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1539 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1540 } else if (bcmp((caddr_t)tl,
1541 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1543 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1548 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1557 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1558 committed = NFSV3WRITE_FILESYNC;
1559 *iomode = committed;
1561 uiop->uio_resid = tsiz;
1567 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1568 * mode set to specify the file type and the size field for rdev.
1571 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1574 struct nfsv2_sattr *sp;
1576 struct vnode *newvp = NULL;
1577 struct nfsnode *np = NULL;
1579 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1581 struct nfsm_info info;
1584 info.v3 = NFS_ISV3(dvp);
1586 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1587 rmajor = txdr_unsigned(vap->va_rmajor);
1588 rminor = txdr_unsigned(vap->va_rminor);
1589 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1590 rmajor = nfs_xdrneg1;
1591 rminor = nfs_xdrneg1;
1593 return (EOPNOTSUPP);
1595 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1598 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1599 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1600 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1601 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1602 ERROROUT(nfsm_fhtom(&info, dvp));
1603 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1606 tl = nfsm_build(&info, NFSX_UNSIGNED);
1607 *tl++ = vtonfsv3_type(vap->va_type);
1608 nfsm_v3attrbuild(&info, vap, FALSE);
1609 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1610 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1611 *tl++ = txdr_unsigned(vap->va_rmajor);
1612 *tl = txdr_unsigned(vap->va_rminor);
1615 sp = nfsm_build(&info, NFSX_V2SATTR);
1616 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1617 sp->sa_uid = nfs_xdrneg1;
1618 sp->sa_gid = nfs_xdrneg1;
1619 sp->sa_size = makeudev(rmajor, rminor);
1620 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1621 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1623 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1624 cnp->cn_cred, &error));
1626 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1632 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1633 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1639 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1650 VTONFS(dvp)->n_flag |= NLMODIFIED;
1652 VTONFS(dvp)->n_attrstamp = 0;
1658 * just call nfs_mknodrpc() to do the work.
1660 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1661 * struct componentname *a_cnp, struct vattr *a_vap)
1665 nfs_mknod(struct vop_old_mknod_args *ap)
1667 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1670 lwkt_gettoken(&nmp->nm_token);
1671 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1672 lwkt_reltoken(&nmp->nm_token);
1674 nfs_knote(ap->a_dvp, NOTE_WRITE);
1679 static u_long create_verf;
1681 * nfs file create call
1683 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1684 * struct componentname *a_cnp, struct vattr *a_vap)
1687 nfs_create(struct vop_old_create_args *ap)
1689 struct vnode *dvp = ap->a_dvp;
1690 struct vattr *vap = ap->a_vap;
1691 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1692 struct componentname *cnp = ap->a_cnp;
1693 struct nfsv2_sattr *sp;
1695 struct nfsnode *np = NULL;
1696 struct vnode *newvp = NULL;
1697 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1699 struct nfsm_info info;
1702 info.v3 = NFS_ISV3(dvp);
1703 lwkt_gettoken(&nmp->nm_token);
1706 * Oops, not for me..
1708 if (vap->va_type == VSOCK) {
1709 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1710 lwkt_reltoken(&nmp->nm_token);
1714 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1715 lwkt_reltoken(&nmp->nm_token);
1718 if (vap->va_vaflags & VA_EXCLUSIVE)
1721 nfsstats.rpccnt[NFSPROC_CREATE]++;
1722 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1723 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1724 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1725 ERROROUT(nfsm_fhtom(&info, dvp));
1726 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1729 tl = nfsm_build(&info, NFSX_UNSIGNED);
1730 if (fmode & O_EXCL) {
1731 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1732 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1734 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1735 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1738 *tl++ = create_verf;
1739 *tl = ++create_verf;
1741 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1742 nfsm_v3attrbuild(&info, vap, FALSE);
1745 sp = nfsm_build(&info, NFSX_V2SATTR);
1746 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1747 sp->sa_uid = nfs_xdrneg1;
1748 sp->sa_gid = nfs_xdrneg1;
1750 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1751 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1753 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1754 cnp->cn_cred, &error));
1756 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1762 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1763 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1770 error = nfsm_wcc_data(&info, dvp, &wccflag);
1772 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1778 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1779 KKASSERT(newvp == NULL);
1783 } else if (info.v3 && (fmode & O_EXCL)) {
1785 * We are normally called with only a partially initialized
1786 * VAP. Since the NFSv3 spec says that server may use the
1787 * file attributes to store the verifier, the spec requires
1788 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1789 * in atime, but we can't really assume that all servers will
1790 * so we ensure that our SETATTR sets both atime and mtime.
1792 if (vap->va_mtime.tv_sec == VNOVAL)
1793 vfs_timestamp(&vap->va_mtime);
1794 if (vap->va_atime.tv_sec == VNOVAL)
1795 vap->va_atime = vap->va_mtime;
1796 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1800 * The new np may have enough info for access
1801 * checks, make sure rucred and wucred are
1802 * initialized for read and write rpc's.
1805 if (np->n_rucred == NULL)
1806 np->n_rucred = crhold(cnp->cn_cred);
1807 if (np->n_wucred == NULL)
1808 np->n_wucred = crhold(cnp->cn_cred);
1810 nfs_knote(dvp, NOTE_WRITE);
1814 VTONFS(dvp)->n_flag |= NLMODIFIED;
1816 VTONFS(dvp)->n_attrstamp = 0;
1817 lwkt_reltoken(&nmp->nm_token);
1822 * nfs file remove call
1823 * To try and make nfs semantics closer to ufs semantics, a file that has
1824 * other processes using the vnode is renamed instead of removed and then
1825 * removed later on the last close.
1827 * If a rename is not already in the works
1828 * call nfs_sillyrename() to set it up
1832 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1833 * struct componentname *a_cnp)
1836 nfs_remove(struct vop_old_remove_args *ap)
1838 struct vnode *vp = ap->a_vp;
1839 struct vnode *dvp = ap->a_dvp;
1840 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1841 struct componentname *cnp = ap->a_cnp;
1842 struct nfsnode *np = VTONFS(vp);
1846 lwkt_gettoken(&nmp->nm_token);
1848 if (VREFCNT(vp) < 1)
1849 panic("nfs_remove: bad v_refcnt");
1851 if (vp->v_type == VDIR) {
1853 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1854 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1856 * Force finalization so the VOP_INACTIVE() call is not delayed.
1857 * This prevents cred structures from building up in nfsnodes
1858 * for deleted files.
1860 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1861 np->n_flag |= NREMOVED;
1864 * Throw away biocache buffers, mainly to avoid
1865 * unnecessary delayed writes later.
1867 error = nfs_vinvalbuf(vp, 0, 1);
1869 if (error != EINTR) {
1870 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1872 cnp->cn_cred, cnp->cn_td);
1876 * Kludge City: If the first reply to the remove rpc is lost..
1877 * the reply to the retransmitted request will be ENOENT
1878 * since the file was in fact removed
1879 * Therefore, we cheat and return success.
1881 if (error == ENOENT)
1883 } else if (!np->n_sillyrename) {
1884 error = nfs_sillyrename(dvp, vp, cnp);
1886 np->n_attrstamp = 0;
1887 lwkt_reltoken(&nmp->nm_token);
1889 nfs_knote(vp, NOTE_DELETE);
1890 nfs_knote(dvp, NOTE_WRITE);
1897 * nfs file remove rpc called from nfs_inactive
1899 * NOTE: s_dvp can be VBAD during a forced unmount.
1902 nfs_removeit(struct sillyrename *sp)
1904 if (sp->s_dvp->v_type == VBAD)
1906 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1911 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1914 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1915 struct ucred *cred, struct thread *td)
1917 int error = 0, wccflag = NFSV3_WCCRATTR;
1918 struct nfsm_info info;
1921 info.v3 = NFS_ISV3(dvp);
1923 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1924 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1925 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1926 ERROROUT(nfsm_fhtom(&info, dvp));
1927 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1928 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1930 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1935 VTONFS(dvp)->n_flag |= NLMODIFIED;
1937 VTONFS(dvp)->n_attrstamp = 0;
1942 * nfs file rename call
1944 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1945 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1946 * struct vnode *a_tvp, struct componentname *a_tcnp)
1949 nfs_rename(struct vop_old_rename_args *ap)
1951 struct vnode *fvp = ap->a_fvp;
1952 struct vnode *tvp = ap->a_tvp;
1953 struct vnode *fdvp = ap->a_fdvp;
1954 struct vnode *tdvp = ap->a_tdvp;
1955 struct componentname *tcnp = ap->a_tcnp;
1956 struct componentname *fcnp = ap->a_fcnp;
1957 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1960 lwkt_gettoken(&nmp->nm_token);
1963 * Force finalization so the VOP_INACTIVE() call is not delayed.
1964 * This prevents cred structures from building up in nfsnodes
1965 * for deleted files.
1968 atomic_set_int(&tvp->v_refcnt, VREF_FINALIZE);
1970 VTONFS(tvp)->n_flag |= NREMOVED;
1973 /* Check for cross-device rename */
1974 if ((fvp->v_mount != tdvp->v_mount) ||
1975 (tvp && (fvp->v_mount != tvp->v_mount))) {
1981 * We shouldn't have to flush fvp on rename for most server-side
1982 * filesystems as the file handle should not change. Unfortunately
1983 * the inode for some filesystems (msdosfs) might be tied to the
1984 * file name or directory position so to be completely safe
1985 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1988 * We must flush tvp on rename because it might become stale on the
1989 * server after the rename.
1991 if (nfs_flush_on_rename)
1992 VOP_FSYNC(fvp, MNT_WAIT, 0);
1994 VOP_FSYNC(tvp, MNT_WAIT, 0);
1997 * If the tvp exists and is in use, sillyrename it before doing the
1998 * rename of the new file over it.
2000 * XXX Can't sillyrename a directory.
2002 * We do not attempt to do any namecache purges in this old API
2003 * routine. The new API compat functions have access to the actual
2004 * namecache structures and will do it for us.
2006 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2007 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2008 nfs_knote(tvp, NOTE_DELETE);
2012 nfs_knote(tvp, NOTE_DELETE);
2015 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2016 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2021 nfs_knote(fdvp, NOTE_WRITE);
2022 nfs_knote(tdvp, NOTE_WRITE);
2023 nfs_knote(fvp, NOTE_RENAME);
2025 lwkt_reltoken(&nmp->nm_token);
2035 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2037 if (error == ENOENT)
2043 * nfs file rename rpc called from nfs_remove() above
2046 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2047 struct sillyrename *sp)
2049 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2050 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
2054 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2057 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2058 struct vnode *tdvp, const char *tnameptr, int tnamelen,
2059 struct ucred *cred, struct thread *td)
2061 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2062 struct nfsm_info info;
2065 info.v3 = NFS_ISV3(fdvp);
2067 nfsstats.rpccnt[NFSPROC_RENAME]++;
2068 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2069 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2070 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2071 ERROROUT(nfsm_fhtom(&info, fdvp));
2072 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2073 ERROROUT(nfsm_fhtom(&info, tdvp));
2074 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2075 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2077 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2078 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2083 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2084 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2086 VTONFS(fdvp)->n_attrstamp = 0;
2088 VTONFS(tdvp)->n_attrstamp = 0;
2093 * nfs hard link create call
2095 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2096 * struct componentname *a_cnp)
2099 nfs_link(struct vop_old_link_args *ap)
2101 struct vnode *vp = ap->a_vp;
2102 struct vnode *tdvp = ap->a_tdvp;
2103 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2104 struct componentname *cnp = ap->a_cnp;
2105 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2106 struct nfsm_info info;
2108 if (vp->v_mount != tdvp->v_mount) {
2111 lwkt_gettoken(&nmp->nm_token);
2114 * The attribute cache may get out of sync with the server on link.
2115 * Pushing writes to the server before handle was inherited from
2116 * long long ago and it is unclear if we still need to do this.
2119 if (nfs_flush_on_hlink)
2120 VOP_FSYNC(vp, MNT_WAIT, 0);
2123 info.v3 = NFS_ISV3(vp);
2125 nfsstats.rpccnt[NFSPROC_LINK]++;
2126 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2127 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2128 nfsm_rndup(cnp->cn_namelen));
2129 ERROROUT(nfsm_fhtom(&info, vp));
2130 ERROROUT(nfsm_fhtom(&info, tdvp));
2131 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2133 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2134 cnp->cn_cred, &error));
2136 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2137 NFS_LATTR_NOSHRINK));
2138 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2143 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2145 VTONFS(vp)->n_attrstamp = 0;
2147 VTONFS(tdvp)->n_attrstamp = 0;
2149 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2151 if (error == EEXIST)
2153 lwkt_reltoken(&nmp->nm_token);
2155 nfs_knote(vp, NOTE_LINK);
2156 nfs_knote(tdvp, NOTE_WRITE);
2163 * nfs symbolic link create call
2165 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2166 * struct componentname *a_cnp, struct vattr *a_vap,
2170 nfs_symlink(struct vop_old_symlink_args *ap)
2172 struct vnode *dvp = ap->a_dvp;
2173 struct vattr *vap = ap->a_vap;
2174 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2175 struct componentname *cnp = ap->a_cnp;
2176 struct nfsv2_sattr *sp;
2177 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2178 struct vnode *newvp = NULL;
2179 struct nfsm_info info;
2182 info.v3 = NFS_ISV3(dvp);
2183 lwkt_gettoken(&nmp->nm_token);
2185 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2186 slen = strlen(ap->a_target);
2187 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2188 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2189 nfsm_rndup(cnp->cn_namelen) +
2190 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2191 ERROROUT(nfsm_fhtom(&info, dvp));
2192 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2195 nfsm_v3attrbuild(&info, vap, FALSE);
2197 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2199 sp = nfsm_build(&info, NFSX_V2SATTR);
2200 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2201 sp->sa_uid = nfs_xdrneg1;
2202 sp->sa_gid = nfs_xdrneg1;
2203 sp->sa_size = nfs_xdrneg1;
2204 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2205 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2209 * Issue the NFS request and get the rpc response.
2211 * Only NFSv3 responses returning an error of 0 actually return
2212 * a file handle that can be converted into newvp without having
2213 * to do an extra lookup rpc.
2215 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2216 cnp->cn_cred, &error));
2219 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2221 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2225 * out code jumps -> here, mrep is also freed.
2233 * If we get an EEXIST error, silently convert it to no-error
2234 * in case of an NFS retry.
2236 if (error == EEXIST)
2240 * If we do not have (or no longer have) an error, and we could
2241 * not extract the newvp from the response due to the request being
2242 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2243 * to obtain a newvp to return.
2245 if (error == 0 && newvp == NULL) {
2246 struct nfsnode *np = NULL;
2248 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2249 cnp->cn_cred, cnp->cn_td, &np);
2259 VTONFS(dvp)->n_flag |= NLMODIFIED;
2261 VTONFS(dvp)->n_attrstamp = 0;
2262 if (error == 0 && *ap->a_vpp)
2263 nfs_knote(*ap->a_vpp, NOTE_WRITE);
2264 lwkt_reltoken(&nmp->nm_token);
2272 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2273 * struct componentname *a_cnp, struct vattr *a_vap)
2276 nfs_mkdir(struct vop_old_mkdir_args *ap)
2278 struct vnode *dvp = ap->a_dvp;
2279 struct vattr *vap = ap->a_vap;
2280 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2281 struct componentname *cnp = ap->a_cnp;
2282 struct nfsv2_sattr *sp;
2283 struct nfsnode *np = NULL;
2284 struct vnode *newvp = NULL;
2286 int error = 0, wccflag = NFSV3_WCCRATTR;
2289 struct nfsm_info info;
2292 info.v3 = NFS_ISV3(dvp);
2293 lwkt_gettoken(&nmp->nm_token);
2295 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2296 lwkt_reltoken(&nmp->nm_token);
2299 len = cnp->cn_namelen;
2300 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2301 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2302 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2303 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2304 ERROROUT(nfsm_fhtom(&info, dvp));
2305 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2307 nfsm_v3attrbuild(&info, vap, FALSE);
2309 sp = nfsm_build(&info, NFSX_V2SATTR);
2310 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2311 sp->sa_uid = nfs_xdrneg1;
2312 sp->sa_gid = nfs_xdrneg1;
2313 sp->sa_size = nfs_xdrneg1;
2314 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2315 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2317 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2318 cnp->cn_cred, &error));
2320 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2323 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2328 VTONFS(dvp)->n_flag |= NLMODIFIED;
2330 VTONFS(dvp)->n_attrstamp = 0;
2332 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2333 * if we can succeed in looking up the directory.
2335 if (error == EEXIST || (!error && !gotvp)) {
2340 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2344 if (newvp->v_type != VDIR)
2352 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2355 lwkt_reltoken(&nmp->nm_token);
2360 * nfs remove directory call
2362 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2363 * struct componentname *a_cnp)
2366 nfs_rmdir(struct vop_old_rmdir_args *ap)
2368 struct vnode *vp = ap->a_vp;
2369 struct vnode *dvp = ap->a_dvp;
2370 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2371 struct componentname *cnp = ap->a_cnp;
2372 int error = 0, wccflag = NFSV3_WCCRATTR;
2373 struct nfsm_info info;
2376 info.v3 = NFS_ISV3(dvp);
2381 lwkt_gettoken(&nmp->nm_token);
2383 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2384 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2385 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2386 nfsm_rndup(cnp->cn_namelen));
2387 ERROROUT(nfsm_fhtom(&info, dvp));
2388 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2390 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2391 cnp->cn_cred, &error));
2393 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2398 VTONFS(dvp)->n_flag |= NLMODIFIED;
2400 VTONFS(dvp)->n_attrstamp = 0;
2402 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2404 if (error == ENOENT)
2407 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2408 lwkt_reltoken(&nmp->nm_token);
2416 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2419 nfs_readdir(struct vop_readdir_args *ap)
2421 struct vnode *vp = ap->a_vp;
2422 struct nfsnode *np = VTONFS(vp);
2423 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2424 struct uio *uio = ap->a_uio;
2428 if (vp->v_type != VDIR)
2431 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2435 lwkt_gettoken(&nmp->nm_token);
2438 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2439 * and then check that is still valid, or if this is an NQNFS mount
2440 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2441 * VOP_GETATTR() does not necessarily go to the wire.
2443 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2444 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2445 if (VOP_GETATTR(vp, &vattr) == 0 &&
2446 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2448 nfsstats.direofcache_hits++;
2454 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2455 * own cache coherency checks so we do not have to.
2457 tresid = uio->uio_resid;
2458 error = nfs_bioread(vp, uio, 0);
2460 if (!error && uio->uio_resid == tresid)
2461 nfsstats.direofcache_misses++;
2463 lwkt_reltoken(&nmp->nm_token);
2470 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2472 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2473 * offset/block and converts the nfs formatted directory entries for userland
2474 * consumption as well as deals with offsets into the middle of blocks.
2475 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2476 * be block-bounded. It must convert to cookies for the actual RPC.
2479 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2482 struct nfs_dirent *dp = NULL;
2487 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2488 struct nfsnode *dnp = VTONFS(vp);
2490 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2492 struct nfsm_info info;
2495 info.v3 = NFS_ISV3(vp);
2498 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2499 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2500 panic("nfs readdirrpc bad uio");
2504 * If there is no cookie, assume directory was stale.
2506 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2510 return (NFSERR_BAD_COOKIE);
2512 * Loop around doing readdir rpc's of size nm_readdirsize
2513 * truncated to a multiple of DIRBLKSIZ.
2514 * The stopping criteria is EOF or buffer full.
2516 while (more_dirs && bigenough) {
2517 nfsstats.rpccnt[NFSPROC_READDIR]++;
2518 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2519 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2520 ERROROUT(nfsm_fhtom(&info, vp));
2522 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2523 *tl++ = cookie.nfsuquad[0];
2524 *tl++ = cookie.nfsuquad[1];
2525 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2526 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2529 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2530 * WITH NFSv2!!! There's nothing I can really do
2531 * about it other than to hope the server supports
2534 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2535 *tl++ = cookie.nfsuquad[0];
2537 *tl = txdr_unsigned(nmp->nm_readdirsize);
2538 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2540 nfs_vpcred(vp, ND_READ), &error));
2542 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2543 NFS_LATTR_NOSHRINK));
2544 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2545 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2546 dnp->n_cookieverf.nfsuquad[1] = *tl;
2548 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2549 more_dirs = fxdr_unsigned(int, *tl);
2551 /* loop thru the dir entries, converting them to std form */
2552 while (more_dirs && bigenough) {
2554 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2555 fileno = fxdr_hyper(tl);
2556 len = fxdr_unsigned(int, *(tl + 2));
2558 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2559 fileno = fxdr_unsigned(u_quad_t, *tl++);
2560 len = fxdr_unsigned(int, *tl);
2562 if (len <= 0 || len > NFS_MAXNAMLEN) {
2570 * len is the number of bytes in the path element
2571 * name, not including the \0 termination.
2573 * tlen is the number of bytes w have to reserve for
2574 * the path element name.
2576 tlen = nfsm_rndup(len);
2578 tlen += 4; /* To ensure null termination */
2581 * If the entry would cross a DIRBLKSIZ boundary,
2582 * extend the previous nfs_dirent to cover the
2585 left = DIRBLKSIZ - blksiz;
2586 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2587 dp->nfs_reclen += left;
2588 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2589 uiop->uio_iov->iov_len -= left;
2590 uiop->uio_offset += left;
2591 uiop->uio_resid -= left;
2594 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2597 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2598 dp->nfs_ino = fileno;
2599 dp->nfs_namlen = len;
2600 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2601 dp->nfs_type = DT_UNKNOWN;
2602 blksiz += dp->nfs_reclen;
2603 if (blksiz == DIRBLKSIZ)
2605 uiop->uio_offset += sizeof(struct nfs_dirent);
2606 uiop->uio_resid -= sizeof(struct nfs_dirent);
2607 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2608 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2609 ERROROUT(nfsm_mtouio(&info, uiop, len));
2612 * The uiop has advanced by nfs_dirent + len
2613 * but really needs to advance by
2616 cp = uiop->uio_iov->iov_base;
2618 *cp = '\0'; /* null terminate */
2619 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2620 uiop->uio_iov->iov_len -= tlen;
2621 uiop->uio_offset += tlen;
2622 uiop->uio_resid -= tlen;
2625 * NFS strings must be rounded up (nfsm_myouio
2626 * handled that in the bigenough case).
2628 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2631 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2633 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2637 * If we were able to accomodate the last entry,
2638 * get the cookie for the next one. Otherwise
2639 * hold-over the cookie for the one we were not
2640 * able to accomodate.
2643 cookie.nfsuquad[0] = *tl++;
2645 cookie.nfsuquad[1] = *tl++;
2646 } else if (info.v3) {
2651 more_dirs = fxdr_unsigned(int, *tl);
2654 * If at end of rpc data, get the eof boolean
2657 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2658 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2664 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2665 * by increasing d_reclen for the last record.
2668 left = DIRBLKSIZ - blksiz;
2669 dp->nfs_reclen += left;
2670 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2671 uiop->uio_iov->iov_len -= left;
2672 uiop->uio_offset += left;
2673 uiop->uio_resid -= left;
2678 * We hit the end of the directory, update direofoffset.
2680 dnp->n_direofoffset = uiop->uio_offset;
2683 * There is more to go, insert the link cookie so the
2684 * next block can be read.
2686 if (uiop->uio_resid > 0)
2687 kprintf("EEK! readdirrpc resid > 0\n");
2688 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2696 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2699 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2702 struct nfs_dirent *dp;
2704 struct vnode *newvp;
2706 caddr_t dpossav1, dpossav2;
2708 struct mbuf *mdsav1, *mdsav2;
2710 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2711 struct nfsnode *dnp = VTONFS(vp), *np;
2714 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2715 int attrflag, fhsize;
2716 struct nchandle nch;
2717 struct nchandle dnch;
2718 struct nlcomponent nlc;
2719 struct nfsm_info info;
2728 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2729 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2730 panic("nfs readdirplusrpc bad uio");
2733 * Obtain the namecache record for the directory so we have something
2734 * to use as a basis for creating the entries. This function will
2735 * return a held (but not locked) ncp. The ncp may be disconnected
2736 * from the tree and cannot be used for upward traversals, and the
2737 * ncp may be unnamed. Note that other unrelated operations may
2738 * cause the ncp to be named at any time.
2740 * We have to lock the ncp to prevent a lock order reversal when
2741 * rdirplus does nlookups of the children, because the vnode is
2742 * locked and has to stay that way.
2744 cache_fromdvp(vp, NULL, 0, &dnch);
2745 bzero(&nlc, sizeof(nlc));
2749 * If there is no cookie, assume directory was stale.
2751 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2757 return (NFSERR_BAD_COOKIE);
2761 * Loop around doing readdir rpc's of size nm_readdirsize
2762 * truncated to a multiple of DIRBLKSIZ.
2763 * The stopping criteria is EOF or buffer full.
2765 while (more_dirs && bigenough) {
2766 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2767 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2768 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2769 ERROROUT(nfsm_fhtom(&info, vp));
2770 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2771 *tl++ = cookie.nfsuquad[0];
2772 *tl++ = cookie.nfsuquad[1];
2773 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2774 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2775 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2776 *tl = txdr_unsigned(nmp->nm_rsize);
2777 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2779 nfs_vpcred(vp, ND_READ), &error));
2780 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2781 NFS_LATTR_NOSHRINK));
2782 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2783 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2784 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2785 more_dirs = fxdr_unsigned(int, *tl);
2787 /* loop thru the dir entries, doctoring them to 4bsd form */
2788 while (more_dirs && bigenough) {
2789 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2790 fileno = fxdr_hyper(tl);
2791 len = fxdr_unsigned(int, *(tl + 2));
2792 if (len <= 0 || len > NFS_MAXNAMLEN) {
2798 tlen = nfsm_rndup(len);
2800 tlen += 4; /* To ensure null termination*/
2801 left = DIRBLKSIZ - blksiz;
2802 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2803 dp->nfs_reclen += left;
2804 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2805 uiop->uio_iov->iov_len -= left;
2806 uiop->uio_offset += left;
2807 uiop->uio_resid -= left;
2810 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2813 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2814 dp->nfs_ino = fileno;
2815 dp->nfs_namlen = len;
2816 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2817 dp->nfs_type = DT_UNKNOWN;
2818 blksiz += dp->nfs_reclen;
2819 if (blksiz == DIRBLKSIZ)
2821 uiop->uio_offset += sizeof(struct nfs_dirent);
2822 uiop->uio_resid -= sizeof(struct nfs_dirent);
2823 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2824 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2825 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2826 nlc.nlc_namelen = len;
2827 ERROROUT(nfsm_mtouio(&info, uiop, len));
2828 cp = uiop->uio_iov->iov_base;
2831 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2832 uiop->uio_iov->iov_len -= tlen;
2833 uiop->uio_offset += tlen;
2834 uiop->uio_resid -= tlen;
2836 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2838 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2840 cookie.nfsuquad[0] = *tl++;
2841 cookie.nfsuquad[1] = *tl++;
2847 * Since the attributes are before the file handle
2848 * (sigh), we must skip over the attributes and then
2849 * come back and get them.
2851 attrflag = fxdr_unsigned(int, *tl);
2853 dpossav1 = info.dpos;
2855 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2856 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2857 doit = fxdr_unsigned(int, *tl);
2859 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2861 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2862 !NFS_CMPFH(dnp, fhp, fhsize)
2866 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2867 nlc.nlc_namelen, nlc.nlc_namelen,
2871 * This is a bit hokey but there isn't
2872 * much we can do about it. We can't
2873 * hold the directory vp locked while
2874 * doing lookups and gets.
2876 nch = cache_nlookup_nonblock(&dnch, &nlc);
2877 if (nch.ncp == NULL)
2879 cache_setunresolved(&nch);
2880 error = nfs_nget_nonblock(vp->v_mount, fhp,
2888 dpossav2 = info.dpos;
2889 info.dpos = dpossav1;
2892 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2893 info.dpos = dpossav2;
2896 IFTODT(VTTOIF(np->n_vattr.va_type));
2897 nfs_cache_setvp(&nch, newvp,
2898 nfspos_cache_timeout);
2906 kprintf("Warning: NFS/rddirplus, "
2907 "UNABLE TO ENTER %*.*s\n",
2908 nlc.nlc_namelen, nlc.nlc_namelen,
2914 /* Just skip over the file handle */
2915 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2916 i = fxdr_unsigned(int, *tl);
2917 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2919 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2920 more_dirs = fxdr_unsigned(int, *tl);
2923 * If at end of rpc data, get the eof boolean
2926 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2927 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2933 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2934 * by increasing d_reclen for the last record.
2937 left = DIRBLKSIZ - blksiz;
2938 dp->nfs_reclen += left;
2939 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2940 uiop->uio_iov->iov_len -= left;
2941 uiop->uio_offset += left;
2942 uiop->uio_resid -= left;
2946 * We are now either at the end of the directory or have filled the
2950 dnp->n_direofoffset = uiop->uio_offset;
2952 if (uiop->uio_resid > 0)
2953 kprintf("EEK! readdirplusrpc resid > 0\n");
2954 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2958 if (newvp != NULLVP) {
2971 * Silly rename. To make the NFS filesystem that is stateless look a little
2972 * more like the "ufs" a remove of an active vnode is translated to a rename
2973 * to a funny looking filename that is removed by nfs_inactive on the
2974 * nfsnode. There is the potential for another process on a different client
2975 * to create the same funny name between the nfs_lookitup() fails and the
2976 * nfs_rename() completes, but...
2979 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2981 struct sillyrename *sp;
2986 * Force finalization so the VOP_INACTIVE() call is not delayed.
2987 * This prevents cred structures from building up in nfsnodes
2988 * for deleted files.
2990 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
2992 np->n_flag |= NREMOVED;
2995 * We previously purged dvp instead of vp. I don't know why, it
2996 * completely destroys performance. We can't do it anyway with the
2997 * new VFS API since we would be breaking the namecache topology.
2999 cache_purge(vp); /* XXX */
3001 if (vp->v_type == VDIR)
3002 panic("nfs: sillyrename dir");
3004 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
3005 sp->s_cred = crdup(cnp->cn_cred);
3009 /* Fudge together a funny name */
3010 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
3011 (int)(intptr_t)cnp->cn_td);
3013 /* Try lookitups until we get one that isn't there */
3014 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3015 cnp->cn_td, NULL) == 0) {
3017 if (sp->s_name[4] > 'z') {
3022 error = nfs_renameit(dvp, cnp, sp);
3025 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3027 np->n_sillyrename = sp;
3032 kfree((caddr_t)sp, M_NFSREQ);
3038 * Look up a file name and optionally either update the file handle or
3039 * allocate an nfsnode, depending on the value of npp.
3040 * npp == NULL --> just do the lookup
3041 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3043 * *npp != NULL --> update the file handle in the vnode
3046 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
3047 struct thread *td, struct nfsnode **npp)
3049 struct vnode *newvp = NULL;
3050 struct nfsnode *np, *dnp = VTONFS(dvp);
3051 int error = 0, fhlen, attrflag;
3053 struct nfsm_info info;
3056 info.v3 = NFS_ISV3(dvp);
3058 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3059 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3060 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3061 ERROROUT(nfsm_fhtom(&info, dvp));
3062 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3063 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
3064 if (npp && !error) {
3065 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
3068 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3069 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
3070 np->n_fhp = &np->n_fh;
3071 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3072 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
3073 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3074 np->n_fhsize = fhlen;
3076 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3080 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
3089 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3090 NFS_LATTR_NOSHRINK));
3091 if (!attrflag && *npp == NULL) {
3101 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3107 if (npp && *npp == NULL) {
3122 * Nfs Version 3 commit rpc
3124 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3128 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3130 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3131 int error = 0, wccflag = NFSV3_WCCRATTR;
3132 struct nfsm_info info;
3138 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3140 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3141 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3142 ERROROUT(nfsm_fhtom(&info, vp));
3143 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3144 txdr_hyper(offset, tl);
3146 *tl = txdr_unsigned(cnt);
3147 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3148 nfs_vpcred(vp, ND_WRITE), &error));
3149 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3151 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3152 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3153 NFSX_V3WRITEVERF)) {
3154 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3156 error = NFSERR_STALEWRITEVERF;
3167 * - make nfs_bmap() essentially a no-op that does no translation
3168 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3169 * (Maybe I could use the process's page mapping, but I was concerned that
3170 * Kernel Write might not be enabled and also figured copyout() would do
3171 * a lot more work than bcopy() and also it currently happens in the
3172 * context of the swapper process (2).
3174 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3175 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3178 nfs_bmap(struct vop_bmap_args *ap)
3180 /* no token lock required */
3181 if (ap->a_doffsetp != NULL)
3182 *ap->a_doffsetp = ap->a_loffset;
3183 if (ap->a_runp != NULL)
3185 if (ap->a_runb != NULL)
3194 nfs_strategy(struct vop_strategy_args *ap)
3196 struct bio *bio = ap->a_bio;
3198 struct buf *bp __debugvar = bio->bio_buf;
3199 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3203 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3204 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3205 KASSERT(BUF_LOCKINUSE(bp),
3206 ("nfs_strategy: buffer %p not locked", bp));
3208 if (bio->bio_flags & BIO_SYNC)
3209 td = curthread; /* XXX */
3213 lwkt_gettoken(&nmp->nm_token);
3216 * We probably don't need to push an nbio any more since no
3217 * block conversion is required due to the use of 64 bit byte
3218 * offsets, but do it anyway.
3220 * NOTE: When NFS callers itself via this strategy routines and
3221 * sets up a synchronous I/O, it expects the I/O to run
3222 * synchronously (its bio_done routine just assumes it),
3223 * so for now we have to honor the bit.
3225 nbio = push_bio(bio);
3226 nbio->bio_offset = bio->bio_offset;
3227 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3230 * If the op is asynchronous and an i/o daemon is waiting
3231 * queue the request, wake it up and wait for completion
3232 * otherwise just do it ourselves.
3234 if (bio->bio_flags & BIO_SYNC) {
3235 error = nfs_doio(ap->a_vp, nbio, td);
3237 nfs_asyncio(ap->a_vp, nbio);
3240 lwkt_reltoken(&nmp->nm_token);
3246 * fsync vnode op. Just call nfs_flush() with commit == 1.
3248 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3252 nfs_fsync(struct vop_fsync_args *ap)
3254 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3257 lwkt_gettoken(&nmp->nm_token);
3260 * NOTE: Because attributes are set synchronously we currently
3261 * do not have to implement vsetisdirty()/vclrisdirty().
3263 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3265 lwkt_reltoken(&nmp->nm_token);
3271 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3272 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3273 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3274 * set the buffer contains data that has already been written to the server
3275 * and which now needs a commit RPC.
3277 * If commit is 0 we only take one pass and only flush buffers containing new
3280 * If commit is 1 we take two passes, issuing a commit RPC in the second
3283 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3284 * to completely flush all pending data.
3286 * Note that the RB_SCAN code properly handles the case where the
3287 * callback might block and directly or indirectly (another thread) cause
3288 * the RB tree to change.
3291 #ifndef NFS_COMMITBVECSIZ
3292 #define NFS_COMMITBVECSIZ 16
3295 struct nfs_flush_info {
3296 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3303 struct buf *bvary[NFS_COMMITBVECSIZ];
3309 static int nfs_flush_bp(struct buf *bp, void *data);
3310 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3313 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3315 struct nfsnode *np = VTONFS(vp);
3316 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3317 struct nfs_flush_info info;
3320 bzero(&info, sizeof(info));
3323 info.waitfor = waitfor;
3324 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3326 lwkt_gettoken(&vp->v_token);
3332 info.mode = NFI_FLUSHNEW;
3333 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3334 nfs_flush_bp, &info);
3337 * Take a second pass if committing and no error occured.
3338 * Clean up any left over collection (whether an error
3341 if (commit && error == 0) {
3342 info.mode = NFI_COMMIT;
3343 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3344 nfs_flush_bp, &info);
3346 error = nfs_flush_docommit(&info, error);
3350 * Wait for pending I/O to complete before checking whether
3351 * any further dirty buffers exist.
3353 while (waitfor == MNT_WAIT &&
3354 bio_track_active(&vp->v_track_write)) {
3355 error = bio_track_wait(&vp->v_track_write,
3356 info.slpflag, info.slptimeo);
3359 * We have to be able to break out if this
3360 * is an 'intr' mount.
3362 if (nfs_sigintr(nmp, NULL, td)) {
3368 * Since we do not process pending signals,
3369 * once we get a PCATCH our tsleep() will no
3370 * longer sleep, switch to a fixed timeout
3373 if (info.slpflag == PCATCH) {
3375 info.slptimeo = 2 * hz;
3382 * Loop if we are flushing synchronous as well as committing,
3383 * and dirty buffers are still present. Otherwise we might livelock.
3385 } while (waitfor == MNT_WAIT && commit &&
3386 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3389 * The callbacks have to return a negative error to terminate the
3396 * Deal with any error collection
3398 if (np->n_flag & NWRITEERR) {
3399 error = np->n_error;
3400 np->n_flag &= ~NWRITEERR;
3402 lwkt_reltoken(&vp->v_token);
3408 nfs_flush_bp(struct buf *bp, void *data)
3410 struct nfs_flush_info *info = data;
3416 switch(info->mode) {
3418 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3419 if (error && info->loops && info->waitfor == MNT_WAIT) {
3420 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3422 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3423 if (info->slpflag & PCATCH)
3424 lkflags |= LK_PCATCH;
3425 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3431 * Ignore locking errors
3439 * The buffer may have changed out from under us, even if
3440 * we did not block (MPSAFE). Check again now that it is
3443 if (bp->b_vp == info->vp &&
3444 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3453 * Only process buffers in need of a commit which we can
3454 * immediately lock. This may prevent a buffer from being
3455 * committed, but the normal flush loop will block on the
3456 * same buffer so we shouldn't get into an endless loop.
3458 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3459 (B_DELWRI | B_NEEDCOMMIT)) {
3462 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3466 * We must recheck after successfully locking the buffer.
3468 if (bp->b_vp != info->vp ||
3469 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3470 (B_DELWRI | B_NEEDCOMMIT)) {
3476 * NOTE: storing the bp in the bvary[] basically sets
3477 * it up for a commit operation.
3479 * We must call vfs_busy_pages() now so the commit operation
3480 * is interlocked with user modifications to memory mapped
3481 * pages. The b_dirtyoff/b_dirtyend range is not correct
3482 * until after the pages have been busied.
3484 * Note: to avoid loopback deadlocks, we do not
3485 * assign b_runningbufspace.
3488 bp->b_cmd = BUF_CMD_WRITE;
3489 vfs_busy_pages(bp->b_vp, bp);
3490 info->bvary[info->bvsize] = bp;
3491 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3492 if (info->bvsize == 0 || toff < info->beg_off)
3493 info->beg_off = toff;
3494 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3495 if (info->bvsize == 0 || toff > info->end_off)
3496 info->end_off = toff;
3498 if (info->bvsize == NFS_COMMITBVECSIZ) {
3499 error = nfs_flush_docommit(info, 0);
3500 KKASSERT(info->bvsize == 0);
3508 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3518 if (info->bvsize > 0) {
3520 * Commit data on the server, as required. Note that
3521 * nfs_commit will use the vnode's cred for the commit.
3522 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3524 bytes = info->end_off - info->beg_off;
3525 if (bytes > 0x40000000)
3530 retv = nfs_commitrpc_uio(vp, info->beg_off,
3531 (int)bytes, info->td);
3532 if (retv == NFSERR_STALEWRITEVERF)
3533 nfs_clearcommit(vp->v_mount);
3537 * Now, either mark the blocks I/O done or mark the
3538 * blocks dirty, depending on whether the commit
3541 for (i = 0; i < info->bvsize; ++i) {
3542 bp = info->bvary[i];
3543 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3545 * Either an error or the original
3546 * vfs_busy_pages() cleared B_NEEDCOMMIT
3547 * due to finding new dirty VM pages in
3550 * Leave B_DELWRI intact.
3552 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3553 vfs_unbusy_pages(bp);
3554 bp->b_cmd = BUF_CMD_DONE;
3558 * Success, remove B_DELWRI ( bundirty() ).
3560 * b_dirtyoff/b_dirtyend seem to be NFS
3561 * specific. We should probably move that
3562 * into bundirty(). XXX
3564 * We are faking an I/O write, we have to
3565 * start the transaction in order to
3566 * immediately biodone() it.
3569 bp->b_flags &= ~B_ERROR;
3570 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3571 bp->b_dirtyoff = bp->b_dirtyend = 0;
3572 biodone(&bp->b_bio1);
3581 * NFS advisory byte-level locks.
3582 * Currently unsupported.
3584 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3588 nfs_advlock(struct vop_advlock_args *ap)
3590 struct nfsnode *np = VTONFS(ap->a_vp);
3592 /* no token lock currently required */
3594 * The following kludge is to allow diskless support to work
3595 * until a real NFS lockd is implemented. Basically, just pretend
3596 * that this is a local lock.
3598 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3602 * Print out the contents of an nfsnode.
3604 * nfs_print(struct vnode *a_vp)
3607 nfs_print(struct vop_print_args *ap)
3609 struct vnode *vp = ap->a_vp;
3610 struct nfsnode *np = VTONFS(vp);
3612 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3613 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3614 if (vp->v_type == VFIFO)
3621 * nfs special file access vnode op.
3623 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3626 nfs_laccess(struct vop_access_args *ap)
3628 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3632 lwkt_gettoken(&nmp->nm_token);
3633 error = VOP_GETATTR(ap->a_vp, &vattr);
3635 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3638 lwkt_reltoken(&nmp->nm_token);
3644 * Read wrapper for fifos.
3646 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3647 * struct ucred *a_cred)
3650 nfsfifo_read(struct vop_read_args *ap)
3652 struct nfsnode *np = VTONFS(ap->a_vp);
3654 /* no token access required */
3659 getnanotime(&np->n_atim);
3660 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3664 * Write wrapper for fifos.
3666 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3667 * struct ucred *a_cred)
3670 nfsfifo_write(struct vop_write_args *ap)
3672 struct nfsnode *np = VTONFS(ap->a_vp);
3674 /* no token access required */
3679 getnanotime(&np->n_mtim);
3680 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3684 * Close wrapper for fifos.
3686 * Update the times on the nfsnode then do fifo close.
3688 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3691 nfsfifo_close(struct vop_close_args *ap)
3693 struct vnode *vp = ap->a_vp;
3694 struct nfsnode *np = VTONFS(vp);
3698 /* no token access required */
3700 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3701 if (np->n_flag & (NACC | NUPD)) {
3703 if (np->n_flag & NACC)
3705 if (np->n_flag & NUPD)
3708 if (VREFCNT(vp) == 1 &&
3709 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3711 if (np->n_flag & NACC)
3712 vattr.va_atime = np->n_atim;
3713 if (np->n_flag & NUPD)
3714 vattr.va_mtime = np->n_mtim;
3715 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3718 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3721 /************************************************************************
3723 ************************************************************************/
3725 static void filt_nfsdetach(struct knote *kn);
3726 static int filt_nfsread(struct knote *kn, long hint);
3727 static int filt_nfswrite(struct knote *kn, long hint);
3728 static int filt_nfsvnode(struct knote *kn, long hint);
3730 static struct filterops nfsread_filtops =
3731 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3732 NULL, filt_nfsdetach, filt_nfsread };
3733 static struct filterops nfswrite_filtops =
3734 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3735 NULL, filt_nfsdetach, filt_nfswrite };
3736 static struct filterops nfsvnode_filtops =
3737 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3738 NULL, filt_nfsdetach, filt_nfsvnode };
3741 nfs_kqfilter (struct vop_kqfilter_args *ap)
3743 struct vnode *vp = ap->a_vp;
3744 struct knote *kn = ap->a_kn;
3746 switch (kn->kn_filter) {
3748 kn->kn_fop = &nfsread_filtops;
3751 kn->kn_fop = &nfswrite_filtops;
3754 kn->kn_fop = &nfsvnode_filtops;
3757 return (EOPNOTSUPP);
3760 kn->kn_hook = (caddr_t)vp;
3762 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3768 filt_nfsdetach(struct knote *kn)
3770 struct vnode *vp = (void *)kn->kn_hook;
3772 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3776 filt_nfsread(struct knote *kn, long hint)
3778 struct vnode *vp = (void *)kn->kn_hook;
3779 struct nfsnode *node = VTONFS(vp);
3782 if (hint == NOTE_REVOKE) {
3783 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3788 * Interlock against MP races when performing this function. XXX
3790 /* TMPFS_NODE_LOCK_SH(node); */
3791 off = node->n_size - kn->kn_fp->f_offset;
3792 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3793 if (kn->kn_sfflags & NOTE_OLDAPI) {
3794 /* TMPFS_NODE_UNLOCK(node); */
3797 if (kn->kn_data == 0) {
3798 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3800 /* TMPFS_NODE_UNLOCK(node); */
3801 return (kn->kn_data != 0);
3805 filt_nfswrite(struct knote *kn, long hint)
3807 if (hint == NOTE_REVOKE)
3808 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3814 filt_nfsvnode(struct knote *kn, long hint)
3816 if (kn->kn_sfflags & hint)
3817 kn->kn_fflags |= hint;
3818 if (hint == NOTE_REVOKE) {
3819 kn->kn_flags |= (EV_EOF | EV_NODATA);
3822 return (kn->kn_fflags != 0);