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_mmap (struct vop_mmap_args *);
103 static int nfs_fsync (struct vop_fsync_args *);
104 static int nfs_remove (struct vop_old_remove_args *);
105 static int nfs_link (struct vop_old_link_args *);
106 static int nfs_rename (struct vop_old_rename_args *);
107 static int nfs_mkdir (struct vop_old_mkdir_args *);
108 static int nfs_rmdir (struct vop_old_rmdir_args *);
109 static int nfs_symlink (struct vop_old_symlink_args *);
110 static int nfs_readdir (struct vop_readdir_args *);
111 static int nfs_bmap (struct vop_bmap_args *);
112 static int nfs_strategy (struct vop_strategy_args *);
113 static int nfs_lookitup (struct vnode *, const char *, int,
114 struct ucred *, struct thread *, struct nfsnode **);
115 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
116 static int nfs_laccess (struct vop_access_args *);
117 static int nfs_readlink (struct vop_readlink_args *);
118 static int nfs_print (struct vop_print_args *);
119 static int nfs_advlock (struct vop_advlock_args *);
120 static int nfs_kqfilter (struct vop_kqfilter_args *ap);
122 static int nfs_nresolve (struct vop_nresolve_args *);
124 * Global vfs data structures for nfs
126 struct vop_ops nfsv2_vnode_vops = {
127 .vop_default = vop_defaultop,
128 .vop_access = nfs_access,
129 .vop_advlock = nfs_advlock,
130 .vop_bmap = nfs_bmap,
131 .vop_close = nfs_close,
132 .vop_old_create = nfs_create,
133 .vop_fsync = nfs_fsync,
134 .vop_getattr = nfs_getattr,
135 .vop_getpages = vop_stdgetpages,
136 .vop_putpages = vop_stdputpages,
137 .vop_inactive = nfs_inactive,
138 .vop_old_link = nfs_link,
139 .vop_old_lookup = nfs_lookup,
140 .vop_old_mkdir = nfs_mkdir,
141 .vop_old_mknod = nfs_mknod,
142 .vop_mmap = nfs_mmap,
143 .vop_open = nfs_open,
144 .vop_print = nfs_print,
145 .vop_read = nfs_read,
146 .vop_readdir = nfs_readdir,
147 .vop_readlink = nfs_readlink,
148 .vop_reclaim = nfs_reclaim,
149 .vop_old_remove = nfs_remove,
150 .vop_old_rename = nfs_rename,
151 .vop_old_rmdir = nfs_rmdir,
152 .vop_setattr = nfs_setattr,
153 .vop_strategy = nfs_strategy,
154 .vop_old_symlink = nfs_symlink,
155 .vop_write = nfs_write,
156 .vop_nresolve = nfs_nresolve,
157 .vop_kqfilter = nfs_kqfilter
161 * Special device vnode ops
163 struct vop_ops nfsv2_spec_vops = {
164 .vop_default = vop_defaultop,
165 .vop_access = nfs_laccess,
166 .vop_close = nfs_close,
167 .vop_fsync = nfs_fsync,
168 .vop_getattr = nfs_getattr,
169 .vop_inactive = nfs_inactive,
170 .vop_print = nfs_print,
171 .vop_read = vop_stdnoread,
172 .vop_reclaim = nfs_reclaim,
173 .vop_setattr = nfs_setattr,
174 .vop_write = vop_stdnowrite
177 struct vop_ops nfsv2_fifo_vops = {
178 .vop_default = fifo_vnoperate,
179 .vop_access = nfs_laccess,
180 .vop_close = nfsfifo_close,
181 .vop_fsync = nfs_fsync,
182 .vop_getattr = nfs_getattr,
183 .vop_inactive = nfs_inactive,
184 .vop_print = nfs_print,
185 .vop_read = nfsfifo_read,
186 .vop_reclaim = nfs_reclaim,
187 .vop_setattr = nfs_setattr,
188 .vop_write = nfsfifo_write
191 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
192 struct componentname *cnp,
194 static int nfs_removerpc (struct vnode *dvp, const char *name,
196 struct ucred *cred, struct thread *td);
197 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
198 int fnamelen, struct vnode *tdvp,
199 const char *tnameptr, int tnamelen,
200 struct ucred *cred, struct thread *td);
201 static int nfs_renameit (struct vnode *sdvp,
202 struct componentname *scnp,
203 struct sillyrename *sp);
205 SYSCTL_DECL(_vfs_nfs);
207 static int nfs_flush_on_rename = 1;
208 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
209 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
210 static int nfs_flush_on_hlink = 0;
211 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
212 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
214 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
215 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
216 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
218 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
219 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
220 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
222 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
223 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
224 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
226 static int nfsv3_commit_on_close = 0;
227 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
228 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
231 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
234 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
237 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
238 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
239 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
243 nfs_knote(struct vnode *vp, int flags)
246 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
250 * Returns whether a name component is a degenerate '.' or '..'.
254 nlcdegenerate(struct nlcomponent *nlc)
256 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
258 if (nlc->nlc_namelen == 2 &&
259 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
265 nfs3_access_otw(struct vnode *vp, int wmode,
266 struct thread *td, struct ucred *cred)
268 struct nfsnode *np = VTONFS(vp);
273 struct nfsm_info info;
278 nfsstats.rpccnt[NFSPROC_ACCESS]++;
279 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
280 NFSX_FH(info.v3) + NFSX_UNSIGNED);
281 ERROROUT(nfsm_fhtom(&info, vp));
282 tl = nfsm_build(&info, NFSX_UNSIGNED);
283 *tl = txdr_unsigned(wmode);
284 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
285 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
287 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
288 rmode = fxdr_unsigned(u_int32_t, *tl);
290 np->n_modeuid = cred->cr_uid;
291 np->n_modestamp = mycpu->gd_time_seconds;
300 * nfs access vnode op.
301 * For nfs version 2, just return ok. File accesses may fail later.
302 * For nfs version 3, use the access rpc to check accessibility. If file modes
303 * are changed on the server, accesses might still fail later.
305 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
308 nfs_access(struct vop_access_args *ap)
311 struct vnode *vp = ap->a_vp;
312 thread_t td = curthread;
314 u_int32_t mode, wmode;
315 struct nfsnode *np = VTONFS(vp);
316 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
317 int v3 = NFS_ISV3(vp);
319 lwkt_gettoken(&nmp->nm_token);
322 * Disallow write attempts on filesystems mounted read-only;
323 * unless the file is a socket, fifo, or a block or character
324 * device resident on the filesystem.
326 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
327 switch (vp->v_type) {
331 lwkt_reltoken(&nmp->nm_token);
339 * The NFS protocol passes only the effective uid/gid over the wire but
340 * we need to check access against real ids if AT_EACCESS not set.
341 * Handle this case by cloning the credentials and setting the
342 * effective ids to the real ones.
344 * The crdup() here can cause a lot of ucred structures to build-up
345 * (up to maxvnodes), so do our best to avoid it.
347 if (ap->a_flags & AT_EACCESS) {
348 cred = crhold(ap->a_cred);
351 if (cred->cr_uid == cred->cr_ruid &&
352 cred->cr_gid == cred->cr_rgid) {
353 cred = crhold(ap->a_cred);
355 cred = crdup(ap->a_cred);
356 cred->cr_uid = cred->cr_ruid;
357 cred->cr_gid = cred->cr_rgid;
362 * For nfs v3, check to see if we have done this recently, and if
363 * so return our cached result instead of making an ACCESS call.
364 * If not, do an access rpc, otherwise you are stuck emulating
365 * ufs_access() locally using the vattr. This may not be correct,
366 * since the server may apply other access criteria such as
367 * client uid-->server uid mapping that we do not know about.
370 if (ap->a_mode & VREAD)
371 mode = NFSV3ACCESS_READ;
374 if (vp->v_type != VDIR) {
375 if (ap->a_mode & VWRITE)
376 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
377 if (ap->a_mode & VEXEC)
378 mode |= NFSV3ACCESS_EXECUTE;
380 if (ap->a_mode & VWRITE)
381 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
383 if (ap->a_mode & VEXEC)
384 mode |= NFSV3ACCESS_LOOKUP;
386 /* XXX safety belt, only make blanket request if caching */
387 if (nfsaccess_cache_timeout > 0) {
388 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
389 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
390 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
396 * Does our cached result allow us to give a definite yes to
399 if (np->n_modestamp &&
400 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
401 (cred->cr_uid == np->n_modeuid) &&
402 ((np->n_mode & mode) == mode)) {
403 nfsstats.accesscache_hits++;
406 * Either a no, or a don't know. Go to the wire.
408 nfsstats.accesscache_misses++;
409 error = nfs3_access_otw(vp, wmode, td, cred);
411 if ((np->n_mode & mode) != mode) {
417 if ((error = nfs_laccess(ap)) != 0) {
419 lwkt_reltoken(&nmp->nm_token);
424 * Attempt to prevent a mapped root from accessing a file
425 * which it shouldn't. We try to read a byte from the file
426 * if the user is root and the file is not zero length.
427 * After calling nfs_laccess, we should have the correct
430 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
431 && VTONFS(vp)->n_size > 0) {
438 auio.uio_iov = &aiov;
442 auio.uio_segflg = UIO_SYSSPACE;
443 auio.uio_rw = UIO_READ;
446 if (vp->v_type == VREG) {
447 error = nfs_readrpc_uio(vp, &auio);
448 } else if (vp->v_type == VDIR) {
450 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
452 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
453 error = nfs_readdirrpc_uio(vp, &auio);
455 } else if (vp->v_type == VLNK) {
456 error = nfs_readlinkrpc_uio(vp, &auio);
463 * [re]record creds for reading and/or writing if access
464 * was granted. Assume the NFS server will grant read access
465 * for execute requests.
468 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
471 crfree(np->n_rucred);
474 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
477 crfree(np->n_wucred);
481 lwkt_reltoken(&nmp->nm_token);
489 * Check to see if the type is ok
490 * and that deletion is not in progress.
491 * For paged in text files, you will need to flush the page cache
492 * if consistency is lost.
494 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
499 nfs_open(struct vop_open_args *ap)
501 struct vnode *vp = ap->a_vp;
502 struct nfsnode *np = VTONFS(vp);
503 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
507 lwkt_gettoken(&nmp->nm_token);
509 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
511 kprintf("open eacces vtyp=%d\n",vp->v_type);
513 lwkt_reltoken(&nmp->nm_token);
518 * Save valid creds for reading and writing for later RPCs.
520 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
523 crfree(np->n_rucred);
524 np->n_rucred = ap->a_cred;
526 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
529 crfree(np->n_wucred);
530 np->n_wucred = ap->a_cred;
534 * Clear the attribute cache only if opening with write access. It
535 * is unclear if we should do this at all here, but we certainly
536 * should not clear the cache unconditionally simply because a file
539 if (ap->a_mode & FWRITE)
543 * For normal NFS, reconcile changes made locally verses
544 * changes made remotely. Note that VOP_GETATTR only goes
545 * to the wire if the cached attribute has timed out or been
548 * If local modifications have been made clear the attribute
549 * cache to force an attribute and modified time check. If
550 * GETATTR detects that the file has been changed by someone
551 * other then us it will set NRMODIFIED.
553 * If we are opening a directory and local changes have been
554 * made we have to invalidate the cache in order to ensure
555 * that we get the most up-to-date information from the
558 if (np->n_flag & NLMODIFIED) {
560 if (vp->v_type == VDIR) {
561 error = nfs_vinvalbuf(vp, V_SAVE, 1);
562 if (error == EINTR) {
563 lwkt_reltoken(&nmp->nm_token);
569 error = VOP_GETATTR(vp, &vattr);
571 lwkt_reltoken(&nmp->nm_token);
574 if (np->n_flag & NRMODIFIED) {
575 if (vp->v_type == VDIR)
577 error = nfs_vinvalbuf(vp, V_SAVE, 1);
578 if (error == EINTR) {
579 lwkt_reltoken(&nmp->nm_token);
582 np->n_flag &= ~NRMODIFIED;
584 error = vop_stdopen(ap);
585 lwkt_reltoken(&nmp->nm_token);
592 * What an NFS client should do upon close after writing is a debatable issue.
593 * Most NFS clients push delayed writes to the server upon close, basically for
595 * 1 - So that any write errors may be reported back to the client process
596 * doing the close system call. By far the two most likely errors are
597 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
598 * 2 - To put a worst case upper bound on cache inconsistency between
599 * multiple clients for the file.
600 * There is also a consistency problem for Version 2 of the protocol w.r.t.
601 * not being able to tell if other clients are writing a file concurrently,
602 * since there is no way of knowing if the changed modify time in the reply
603 * is only due to the write for this client.
604 * (NFS Version 3 provides weak cache consistency data in the reply that
605 * should be sufficient to detect and handle this case.)
607 * The current code does the following:
608 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
609 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
610 * or commit them (this satisfies 1 and 2 except for the
611 * case where the server crashes after this close but
612 * before the commit RPC, which is felt to be "good
613 * enough". Changing the last argument to nfs_flush() to
614 * a 1 would force a commit operation, if it is felt a
615 * commit is necessary now.
616 * for NQNFS - do nothing now, since 2 is dealt with via leases and
617 * 1 should be dealt with via an fsync() system call for
618 * cases where write errors are important.
620 * nfs_close(struct vnode *a_vp, int a_fflag)
624 nfs_close(struct vop_close_args *ap)
626 struct vnode *vp = ap->a_vp;
627 struct nfsnode *np = VTONFS(vp);
628 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
630 thread_t td = curthread;
632 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
633 lwkt_gettoken(&nmp->nm_token);
635 if (vp->v_type == VREG) {
636 if (np->n_flag & NLMODIFIED) {
639 * Under NFSv3 we have dirty buffers to dispose of. We
640 * must flush them to the NFS server. We have the option
641 * of waiting all the way through the commit rpc or just
642 * waiting for the initial write. The default is to only
643 * wait through the initial write so the data is in the
644 * server's cache, which is roughly similar to the state
645 * a standard disk subsystem leaves the file in on close().
647 * We cannot clear the NLMODIFIED bit in np->n_flag due to
648 * potential races with other processes, and certainly
649 * cannot clear it if we don't commit.
651 int cm = nfsv3_commit_on_close ? 1 : 0;
652 error = nfs_flush(vp, MNT_WAIT, td, cm);
653 /* np->n_flag &= ~NLMODIFIED; */
655 error = nfs_vinvalbuf(vp, V_SAVE, 1);
659 if (np->n_flag & NWRITEERR) {
660 np->n_flag &= ~NWRITEERR;
665 lwkt_reltoken(&nmp->nm_token);
671 * nfs getattr call from vfs.
673 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
676 nfs_getattr(struct vop_getattr_args *ap)
678 struct vnode *vp = ap->a_vp;
679 struct nfsnode *np = VTONFS(vp);
680 struct nfsmount *nmp;
682 thread_t td = curthread;
683 struct nfsm_info info;
686 info.v3 = NFS_ISV3(vp);
687 nmp = VFSTONFS(vp->v_mount);
689 lwkt_gettoken(&nmp->nm_token);
692 * Update local times for special files.
694 if (np->n_flag & (NACC | NUPD))
697 * First look in the cache.
699 if (nfs_getattrcache(vp, ap->a_vap) == 0)
702 if (info.v3 && nfsaccess_cache_timeout > 0) {
703 nfsstats.accesscache_misses++;
704 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
705 if (nfs_getattrcache(vp, ap->a_vap) == 0)
709 nfsstats.rpccnt[NFSPROC_GETATTR]++;
710 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
711 ERROROUT(nfsm_fhtom(&info, vp));
712 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
713 nfs_vpcred(vp, ND_CHECK), &error));
715 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
721 * NFS doesn't support chflags flags. If the nfs mount was
722 * made -o cache set the UF_CACHE bit for swapcache.
724 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
725 ap->a_vap->va_flags |= UF_CACHE;
727 lwkt_reltoken(&nmp->nm_token);
734 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
737 nfs_setattr(struct vop_setattr_args *ap)
739 struct vnode *vp = ap->a_vp;
740 struct nfsnode *np = VTONFS(vp);
741 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
742 struct vattr *vap = ap->a_vap;
746 thread_t td = curthread;
752 * Setting of flags is not supported.
754 if (vap->va_flags != VNOVAL)
758 * Disallow write attempts if the filesystem is mounted read-only.
760 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
761 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
762 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
763 (vp->v_mount->mnt_flag & MNT_RDONLY))
766 lwkt_gettoken(&nmp->nm_token);
768 if (vap->va_size != VNOVAL) {
770 * truncation requested
772 switch (vp->v_type) {
774 lwkt_reltoken(&nmp->nm_token);
780 if (vap->va_mtime.tv_sec == VNOVAL &&
781 vap->va_atime.tv_sec == VNOVAL &&
782 vap->va_mode == (mode_t)VNOVAL &&
783 vap->va_uid == (uid_t)VNOVAL &&
784 vap->va_gid == (gid_t)VNOVAL) {
785 lwkt_reltoken(&nmp->nm_token);
788 vap->va_size = VNOVAL;
792 * Disallow write attempts if the filesystem is
795 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
796 lwkt_reltoken(&nmp->nm_token);
802 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
805 if (np->n_flag & NLMODIFIED) {
806 if (vap->va_size == 0)
807 error = nfs_vinvalbuf(vp, 0, 1);
809 error = nfs_vinvalbuf(vp, V_SAVE, 1);
813 * note: this loop case almost always happens at
814 * least once per truncation.
816 if (error == 0 && np->n_size != vap->va_size)
818 np->n_vattr.va_size = vap->va_size;
819 kflags |= NOTE_WRITE;
820 if (tsize < vap->va_size)
821 kflags |= NOTE_EXTEND;
824 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
826 * What to do. If we are modifying the mtime we lose
827 * mtime detection of changes made by the server or other
828 * clients. But programs like rsync/rdist/cpdup are going
829 * to call utimes a lot. We don't want to piecemeal sync.
831 * For now sync if any prior remote changes were detected,
832 * but allow us to lose track of remote changes made during
833 * the utimes operation.
835 if (np->n_flag & NRMODIFIED)
836 error = nfs_vinvalbuf(vp, V_SAVE, 1);
837 if (error == EINTR) {
838 lwkt_reltoken(&nmp->nm_token);
842 if (vap->va_mtime.tv_sec != VNOVAL) {
843 np->n_mtime = vap->va_mtime.tv_sec;
847 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
849 kflags |= NOTE_EXTEND;
852 * Sanity check if a truncation was issued. This should only occur
853 * if multiple processes are racing on the same file.
855 if (error == 0 && vap->va_size != VNOVAL &&
856 np->n_size != vap->va_size) {
857 kprintf("NFS ftruncate: server disagrees on the file size: "
860 (intmax_t)vap->va_size,
861 (intmax_t)np->n_size);
864 if (error && vap->va_size != VNOVAL) {
865 np->n_size = np->n_vattr.va_size = tsize;
866 nfs_meta_setsize(vp, td, np->n_size, 0);
868 lwkt_reltoken(&nmp->nm_token);
869 nfs_knote(vp, kflags);
875 * Do an nfs setattr rpc.
878 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
879 struct ucred *cred, struct thread *td)
881 struct nfsv2_sattr *sp;
882 struct nfsnode *np = VTONFS(vp);
884 int error = 0, wccflag = NFSV3_WCCRATTR;
885 struct nfsm_info info;
888 info.v3 = NFS_ISV3(vp);
890 nfsstats.rpccnt[NFSPROC_SETATTR]++;
891 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
892 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
893 ERROROUT(nfsm_fhtom(&info, vp));
895 nfsm_v3attrbuild(&info, vap, TRUE);
896 tl = nfsm_build(&info, NFSX_UNSIGNED);
899 sp = nfsm_build(&info, NFSX_V2SATTR);
900 if (vap->va_mode == (mode_t)VNOVAL)
901 sp->sa_mode = nfs_xdrneg1;
903 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
904 if (vap->va_uid == (uid_t)VNOVAL)
905 sp->sa_uid = nfs_xdrneg1;
907 sp->sa_uid = txdr_unsigned(vap->va_uid);
908 if (vap->va_gid == (gid_t)VNOVAL)
909 sp->sa_gid = nfs_xdrneg1;
911 sp->sa_gid = txdr_unsigned(vap->va_gid);
912 sp->sa_size = txdr_unsigned(vap->va_size);
913 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
914 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
916 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
919 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
921 ERROROUT(nfsm_loadattr(&info, vp, NULL));
931 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
937 cache_setvp(nch, vp);
938 cache_settimeout(nch, nctimeout);
942 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
943 * nfs_lookup() until all remaining new api calls are implemented.
945 * Resolve a namecache entry. This function is passed a locked ncp and
946 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
949 nfs_nresolve(struct vop_nresolve_args *ap)
951 struct thread *td = curthread;
952 struct namecache *ncp;
953 struct nfsmount *nmp;
963 struct nfsm_info info;
966 nmp = VFSTONFS(dvp->v_mount);
968 lwkt_gettoken(&nmp->nm_token);
970 if ((error = vget(dvp, LK_SHARED)) != 0) {
971 lwkt_reltoken(&nmp->nm_token);
976 info.v3 = NFS_ISV3(dvp);
979 nfsstats.lookupcache_misses++;
980 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
981 ncp = ap->a_nch->ncp;
983 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
984 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
985 ERROROUT(nfsm_fhtom(&info, dvp));
986 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
987 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
988 ap->a_cred, &error));
991 * Cache negatve lookups to reduce NFS traffic, but use
992 * a fast timeout. Otherwise use a timeout of 1 tick.
993 * XXX we should add a namecache flag for no-caching
994 * to uncache the negative hit as soon as possible, but
995 * we cannot simply destroy the entry because it is used
996 * as a placeholder by the caller.
998 * The refactored nfs code will overwrite a non-zero error
999 * with 0 when we use ERROROUT(), so don't here.
1001 if (error == ENOENT)
1002 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
1003 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1004 NFS_LATTR_NOSHRINK);
1015 * Success, get the file handle, do various checks, and load
1016 * post-operation data from the reply packet. Theoretically
1017 * we should never be looking up "." so, theoretically, we
1018 * should never get the same file handle as our directory. But
1019 * we check anyway. XXX
1021 * Note that no timeout is set for the positive cache hit. We
1022 * assume, theoretically, that ESTALE returns will be dealt with
1023 * properly to handle NFS races and in anycase we cannot depend
1024 * on a timeout to deal with NFS open/create/excl issues so instead
1025 * of a bad hack here the rest of the NFS client code needs to do
1028 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1031 if (NFS_CMPFH(np, fhp, fhsize)) {
1035 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1040 lwkt_reltoken(&nmp->nm_token);
1046 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1047 NFS_LATTR_NOSHRINK));
1048 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1049 NFS_LATTR_NOSHRINK));
1051 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1053 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1057 lwkt_reltoken(&nmp->nm_token);
1069 * 'cached' nfs directory lookup
1071 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1073 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1074 * struct componentname *a_cnp)
1077 nfs_lookup(struct vop_old_lookup_args *ap)
1079 struct componentname *cnp = ap->a_cnp;
1080 struct vnode *dvp = ap->a_dvp;
1081 struct vnode **vpp = ap->a_vpp;
1082 int flags = cnp->cn_flags;
1083 struct vnode *newvp;
1084 struct vnode *notvp;
1085 struct nfsmount *nmp;
1089 int lockparent, wantparent, attrflag, fhsize;
1092 struct nfsm_info info;
1095 info.v3 = NFS_ISV3(dvp);
1098 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1101 * Read-only mount check and directory check.
1104 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1105 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1108 if (dvp->v_type != VDIR)
1112 * Look it up in the cache. Note that ENOENT is only returned if we
1113 * previously entered a negative hit (see later on). The additional
1114 * nfsneg_cache_timeout check causes previously cached results to
1115 * be instantly ignored if the negative caching is turned off.
1117 lockparent = flags & CNP_LOCKPARENT;
1118 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1119 nmp = VFSTONFS(dvp->v_mount);
1122 lwkt_gettoken(&nmp->nm_token);
1129 nfsstats.lookupcache_misses++;
1130 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1131 len = cnp->cn_namelen;
1132 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1133 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1134 ERROROUT(nfsm_fhtom(&info, dvp));
1135 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1136 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1137 cnp->cn_cred, &error));
1139 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1140 NFS_LATTR_NOSHRINK);
1150 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1153 * Handle RENAME case...
1155 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1156 if (NFS_CMPFH(np, fhp, fhsize)) {
1159 lwkt_reltoken(&nmp->nm_token);
1162 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1166 lwkt_reltoken(&nmp->nm_token);
1171 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1172 NFS_LATTR_NOSHRINK));
1173 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1174 NFS_LATTR_NOSHRINK));
1176 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1183 cnp->cn_flags |= CNP_PDIRUNLOCK;
1185 lwkt_reltoken(&nmp->nm_token);
1189 if (flags & CNP_ISDOTDOT) {
1191 cnp->cn_flags |= CNP_PDIRUNLOCK;
1192 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1194 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1195 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1196 lwkt_reltoken(&nmp->nm_token);
1197 return (error); /* NOTE: return error from nget */
1201 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1204 lwkt_reltoken(&nmp->nm_token);
1207 cnp->cn_flags |= CNP_PDIRUNLOCK;
1209 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1213 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1217 lwkt_reltoken(&nmp->nm_token);
1222 cnp->cn_flags |= CNP_PDIRUNLOCK;
1227 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1228 NFS_LATTR_NOSHRINK));
1229 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1230 NFS_LATTR_NOSHRINK));
1232 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1235 /* XXX MOVE TO nfs_nremove() */
1236 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1237 cnp->cn_nameiop != NAMEI_DELETE) {
1238 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1246 if (newvp != NULLVP) {
1250 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1251 cnp->cn_nameiop == NAMEI_RENAME) &&
1255 cnp->cn_flags |= CNP_PDIRUNLOCK;
1257 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1260 error = EJUSTRETURN;
1263 lwkt_reltoken(&nmp->nm_token);
1269 * Just call nfs_bioread() to do the work.
1271 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1272 * struct ucred *a_cred)
1275 nfs_read(struct vop_read_args *ap)
1277 struct vnode *vp = ap->a_vp;
1278 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1281 lwkt_gettoken(&nmp->nm_token);
1282 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1283 lwkt_reltoken(&nmp->nm_token);
1291 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1294 nfs_readlink(struct vop_readlink_args *ap)
1296 struct vnode *vp = ap->a_vp;
1297 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1300 if (vp->v_type != VLNK)
1303 lwkt_gettoken(&nmp->nm_token);
1304 error = nfs_bioread(vp, ap->a_uio, 0);
1305 lwkt_reltoken(&nmp->nm_token);
1311 * Do a readlink rpc.
1312 * Called by nfs_doio() from below the buffer cache.
1315 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1317 int error = 0, len, attrflag;
1318 struct nfsm_info info;
1321 info.v3 = NFS_ISV3(vp);
1323 nfsstats.rpccnt[NFSPROC_READLINK]++;
1324 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1325 ERROROUT(nfsm_fhtom(&info, vp));
1326 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1327 nfs_vpcred(vp, ND_CHECK), &error));
1329 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1330 NFS_LATTR_NOSHRINK));
1333 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1334 if (len == NFS_MAXPATHLEN) {
1335 struct nfsnode *np = VTONFS(vp);
1336 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1339 ERROROUT(nfsm_mtouio(&info, uiop, len));
1348 * nfs synchronous read rpc using UIO
1351 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1354 struct nfsmount *nmp;
1355 int error = 0, len, retlen, tsiz, eof, attrflag;
1356 struct nfsm_info info;
1360 info.v3 = NFS_ISV3(vp);
1365 nmp = VFSTONFS(vp->v_mount);
1367 tsiz = uiop->uio_resid;
1368 tmp_off = uiop->uio_offset + tsiz;
1369 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1371 tmp_off = uiop->uio_offset;
1373 nfsstats.rpccnt[NFSPROC_READ]++;
1374 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1375 nfsm_reqhead(&info, vp, NFSPROC_READ,
1376 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1377 ERROROUT(nfsm_fhtom(&info, vp));
1378 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1380 txdr_hyper(uiop->uio_offset, tl);
1381 *(tl + 2) = txdr_unsigned(len);
1383 *tl++ = txdr_unsigned(uiop->uio_offset);
1384 *tl++ = txdr_unsigned(len);
1387 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1388 nfs_vpcred(vp, ND_READ), &error));
1390 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1391 NFS_LATTR_NOSHRINK));
1392 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1393 eof = fxdr_unsigned(int, *(tl + 1));
1395 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1397 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1398 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1403 * Handle short-read from server (NFSv3). If EOF is not
1404 * flagged (and no error occurred), but retlen is less
1405 * then the request size, we must zero-fill the remainder.
1407 if (retlen < len && info.v3 && eof == 0) {
1408 ERROROUT(uiomovez(len - retlen, uiop));
1414 * Terminate loop on EOF or zero-length read.
1416 * For NFSv2 a short-read indicates EOF, not zero-fill,
1417 * and also terminates the loop.
1420 if (eof || retlen == 0)
1422 } else if (retlen < len) {
1434 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1435 int *iomode, int *must_commit)
1439 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1440 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1441 int committed = NFSV3WRITE_FILESYNC;
1442 struct nfsm_info info;
1445 info.v3 = NFS_ISV3(vp);
1448 if (uiop->uio_iovcnt != 1)
1449 panic("nfs: writerpc iovcnt > 1");
1452 tsiz = uiop->uio_resid;
1453 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1456 nfsstats.rpccnt[NFSPROC_WRITE]++;
1457 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1458 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1459 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1461 ERROROUT(nfsm_fhtom(&info, vp));
1463 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1464 txdr_hyper(uiop->uio_offset, tl);
1466 *tl++ = txdr_unsigned(len);
1467 *tl++ = txdr_unsigned(*iomode);
1468 *tl = txdr_unsigned(len);
1472 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1473 /* Set both "begin" and "current" to non-garbage. */
1474 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1475 *tl++ = x; /* "begin offset" */
1476 *tl++ = x; /* "current offset" */
1477 x = txdr_unsigned(len);
1478 *tl++ = x; /* total to this offset */
1479 *tl = x; /* size of this write */
1481 ERROROUT(nfsm_uiotom(&info, uiop, len));
1482 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1483 nfs_vpcred(vp, ND_WRITE), &error));
1486 * The write RPC returns a before and after mtime. The
1487 * nfsm_wcc_data() macro checks the before n_mtime
1488 * against the before time and stores the after time
1489 * in the nfsnode's cached vattr and n_mtime field.
1490 * The NRMODIFIED bit will be set if the before
1491 * time did not match the original mtime.
1493 wccflag = NFSV3_WCCCHK;
1494 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1496 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1497 rlen = fxdr_unsigned(int, *tl++);
1503 } else if (rlen < len) {
1504 backup = len - rlen;
1505 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1506 uiop->uio_iov->iov_len += backup;
1507 uiop->uio_offset -= backup;
1508 uiop->uio_resid += backup;
1511 commit = fxdr_unsigned(int, *tl++);
1514 * Return the lowest committment level
1515 * obtained by any of the RPCs.
1517 if (committed == NFSV3WRITE_FILESYNC)
1519 else if (committed == NFSV3WRITE_DATASYNC &&
1520 commit == NFSV3WRITE_UNSTABLE)
1522 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1523 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1525 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1526 } else if (bcmp((caddr_t)tl,
1527 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1529 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1534 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1543 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1544 committed = NFSV3WRITE_FILESYNC;
1545 *iomode = committed;
1547 uiop->uio_resid = tsiz;
1553 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1554 * mode set to specify the file type and the size field for rdev.
1557 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1560 struct nfsv2_sattr *sp;
1562 struct vnode *newvp = NULL;
1563 struct nfsnode *np = NULL;
1565 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1567 struct nfsm_info info;
1570 info.v3 = NFS_ISV3(dvp);
1572 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1573 rmajor = txdr_unsigned(vap->va_rmajor);
1574 rminor = txdr_unsigned(vap->va_rminor);
1575 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1576 rmajor = nfs_xdrneg1;
1577 rminor = nfs_xdrneg1;
1579 return (EOPNOTSUPP);
1581 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1584 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1585 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1586 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1587 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1588 ERROROUT(nfsm_fhtom(&info, dvp));
1589 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1592 tl = nfsm_build(&info, NFSX_UNSIGNED);
1593 *tl++ = vtonfsv3_type(vap->va_type);
1594 nfsm_v3attrbuild(&info, vap, FALSE);
1595 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1596 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1597 *tl++ = txdr_unsigned(vap->va_rmajor);
1598 *tl = txdr_unsigned(vap->va_rminor);
1601 sp = nfsm_build(&info, NFSX_V2SATTR);
1602 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1603 sp->sa_uid = nfs_xdrneg1;
1604 sp->sa_gid = nfs_xdrneg1;
1605 sp->sa_size = makeudev(rmajor, rminor);
1606 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1607 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1609 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1610 cnp->cn_cred, &error));
1612 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1618 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1619 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1625 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1636 VTONFS(dvp)->n_flag |= NLMODIFIED;
1638 VTONFS(dvp)->n_attrstamp = 0;
1644 * just call nfs_mknodrpc() to do the work.
1646 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1647 * struct componentname *a_cnp, struct vattr *a_vap)
1651 nfs_mknod(struct vop_old_mknod_args *ap)
1653 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1656 lwkt_gettoken(&nmp->nm_token);
1657 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1658 lwkt_reltoken(&nmp->nm_token);
1660 nfs_knote(ap->a_dvp, NOTE_WRITE);
1665 static u_long create_verf;
1667 * nfs file create call
1669 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1670 * struct componentname *a_cnp, struct vattr *a_vap)
1673 nfs_create(struct vop_old_create_args *ap)
1675 struct vnode *dvp = ap->a_dvp;
1676 struct vattr *vap = ap->a_vap;
1677 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1678 struct componentname *cnp = ap->a_cnp;
1679 struct nfsv2_sattr *sp;
1681 struct nfsnode *np = NULL;
1682 struct vnode *newvp = NULL;
1683 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1685 struct nfsm_info info;
1688 info.v3 = NFS_ISV3(dvp);
1689 lwkt_gettoken(&nmp->nm_token);
1692 * Oops, not for me..
1694 if (vap->va_type == VSOCK) {
1695 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1696 lwkt_reltoken(&nmp->nm_token);
1700 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1701 lwkt_reltoken(&nmp->nm_token);
1704 if (vap->va_vaflags & VA_EXCLUSIVE)
1707 nfsstats.rpccnt[NFSPROC_CREATE]++;
1708 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1709 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1710 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1711 ERROROUT(nfsm_fhtom(&info, dvp));
1712 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1715 tl = nfsm_build(&info, NFSX_UNSIGNED);
1716 if (fmode & O_EXCL) {
1717 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1718 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1720 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1721 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1724 *tl++ = create_verf;
1725 *tl = ++create_verf;
1727 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1728 nfsm_v3attrbuild(&info, vap, FALSE);
1731 sp = nfsm_build(&info, NFSX_V2SATTR);
1732 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1733 sp->sa_uid = nfs_xdrneg1;
1734 sp->sa_gid = nfs_xdrneg1;
1736 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1737 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1739 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1740 cnp->cn_cred, &error));
1742 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1748 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1749 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1756 error = nfsm_wcc_data(&info, dvp, &wccflag);
1758 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1764 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1765 KKASSERT(newvp == NULL);
1769 } else if (info.v3 && (fmode & O_EXCL)) {
1771 * We are normally called with only a partially initialized
1772 * VAP. Since the NFSv3 spec says that server may use the
1773 * file attributes to store the verifier, the spec requires
1774 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1775 * in atime, but we can't really assume that all servers will
1776 * so we ensure that our SETATTR sets both atime and mtime.
1778 if (vap->va_mtime.tv_sec == VNOVAL)
1779 vfs_timestamp(&vap->va_mtime);
1780 if (vap->va_atime.tv_sec == VNOVAL)
1781 vap->va_atime = vap->va_mtime;
1782 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1786 * The new np may have enough info for access
1787 * checks, make sure rucred and wucred are
1788 * initialized for read and write rpc's.
1791 if (np->n_rucred == NULL)
1792 np->n_rucred = crhold(cnp->cn_cred);
1793 if (np->n_wucred == NULL)
1794 np->n_wucred = crhold(cnp->cn_cred);
1796 nfs_knote(dvp, NOTE_WRITE);
1800 VTONFS(dvp)->n_flag |= NLMODIFIED;
1802 VTONFS(dvp)->n_attrstamp = 0;
1803 lwkt_reltoken(&nmp->nm_token);
1808 * nfs file remove call
1809 * To try and make nfs semantics closer to ufs semantics, a file that has
1810 * other processes using the vnode is renamed instead of removed and then
1811 * removed later on the last close.
1813 * If a rename is not already in the works
1814 * call nfs_sillyrename() to set it up
1818 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1819 * struct componentname *a_cnp)
1822 nfs_remove(struct vop_old_remove_args *ap)
1824 struct vnode *vp = ap->a_vp;
1825 struct vnode *dvp = ap->a_dvp;
1826 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1827 struct componentname *cnp = ap->a_cnp;
1828 struct nfsnode *np = VTONFS(vp);
1832 lwkt_gettoken(&nmp->nm_token);
1834 if (VREFCNT(vp) < 1)
1835 panic("nfs_remove: bad v_refcnt");
1837 if (vp->v_type == VDIR) {
1839 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1840 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1842 * Force finalization so the VOP_INACTIVE() call is not delayed.
1843 * This prevents cred structures from building up in nfsnodes
1844 * for deleted files.
1846 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1847 np->n_flag |= NREMOVED;
1850 * Throw away biocache buffers, mainly to avoid
1851 * unnecessary delayed writes later.
1853 error = nfs_vinvalbuf(vp, 0, 1);
1855 if (error != EINTR) {
1856 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1858 cnp->cn_cred, cnp->cn_td);
1862 * Kludge City: If the first reply to the remove rpc is lost..
1863 * the reply to the retransmitted request will be ENOENT
1864 * since the file was in fact removed
1865 * Therefore, we cheat and return success.
1867 if (error == ENOENT)
1869 } else if (!np->n_sillyrename) {
1870 error = nfs_sillyrename(dvp, vp, cnp);
1872 np->n_attrstamp = 0;
1873 lwkt_reltoken(&nmp->nm_token);
1875 nfs_knote(vp, NOTE_DELETE);
1876 nfs_knote(dvp, NOTE_WRITE);
1883 * nfs file remove rpc called from nfs_inactive
1885 * NOTE: s_dvp can be VBAD during a forced unmount.
1888 nfs_removeit(struct sillyrename *sp)
1890 if (sp->s_dvp->v_type == VBAD)
1892 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1897 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1900 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1901 struct ucred *cred, struct thread *td)
1903 int error = 0, wccflag = NFSV3_WCCRATTR;
1904 struct nfsm_info info;
1907 info.v3 = NFS_ISV3(dvp);
1909 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1910 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1911 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1912 ERROROUT(nfsm_fhtom(&info, dvp));
1913 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1914 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1916 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1921 VTONFS(dvp)->n_flag |= NLMODIFIED;
1923 VTONFS(dvp)->n_attrstamp = 0;
1928 * nfs file rename call
1930 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1931 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1932 * struct vnode *a_tvp, struct componentname *a_tcnp)
1935 nfs_rename(struct vop_old_rename_args *ap)
1937 struct vnode *fvp = ap->a_fvp;
1938 struct vnode *tvp = ap->a_tvp;
1939 struct vnode *fdvp = ap->a_fdvp;
1940 struct vnode *tdvp = ap->a_tdvp;
1941 struct componentname *tcnp = ap->a_tcnp;
1942 struct componentname *fcnp = ap->a_fcnp;
1943 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1946 lwkt_gettoken(&nmp->nm_token);
1949 * Force finalization so the VOP_INACTIVE() call is not delayed.
1950 * This prevents cred structures from building up in nfsnodes
1951 * for deleted files.
1954 atomic_set_int(&tvp->v_refcnt, VREF_FINALIZE);
1956 VTONFS(tvp)->n_flag |= NREMOVED;
1959 /* Check for cross-device rename */
1960 if ((fvp->v_mount != tdvp->v_mount) ||
1961 (tvp && (fvp->v_mount != tvp->v_mount))) {
1967 * We shouldn't have to flush fvp on rename for most server-side
1968 * filesystems as the file handle should not change. Unfortunately
1969 * the inode for some filesystems (msdosfs) might be tied to the
1970 * file name or directory position so to be completely safe
1971 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1974 * We must flush tvp on rename because it might become stale on the
1975 * server after the rename.
1977 if (nfs_flush_on_rename)
1978 VOP_FSYNC(fvp, MNT_WAIT, 0);
1980 VOP_FSYNC(tvp, MNT_WAIT, 0);
1983 * If the tvp exists and is in use, sillyrename it before doing the
1984 * rename of the new file over it.
1986 * XXX Can't sillyrename a directory.
1988 * We do not attempt to do any namecache purges in this old API
1989 * routine. The new API compat functions have access to the actual
1990 * namecache structures and will do it for us.
1992 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1993 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1994 nfs_knote(tvp, NOTE_DELETE);
1998 nfs_knote(tvp, NOTE_DELETE);
2001 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2002 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2007 nfs_knote(fdvp, NOTE_WRITE);
2008 nfs_knote(tdvp, NOTE_WRITE);
2009 nfs_knote(fvp, NOTE_RENAME);
2011 lwkt_reltoken(&nmp->nm_token);
2021 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2023 if (error == ENOENT)
2029 * nfs file rename rpc called from nfs_remove() above
2032 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2033 struct sillyrename *sp)
2035 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2036 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
2040 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2043 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2044 struct vnode *tdvp, const char *tnameptr, int tnamelen,
2045 struct ucred *cred, struct thread *td)
2047 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2048 struct nfsm_info info;
2051 info.v3 = NFS_ISV3(fdvp);
2053 nfsstats.rpccnt[NFSPROC_RENAME]++;
2054 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2055 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2056 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2057 ERROROUT(nfsm_fhtom(&info, fdvp));
2058 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2059 ERROROUT(nfsm_fhtom(&info, tdvp));
2060 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2061 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2063 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2064 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2069 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2070 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2072 VTONFS(fdvp)->n_attrstamp = 0;
2074 VTONFS(tdvp)->n_attrstamp = 0;
2079 * nfs hard link create call
2081 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2082 * struct componentname *a_cnp)
2085 nfs_link(struct vop_old_link_args *ap)
2087 struct vnode *vp = ap->a_vp;
2088 struct vnode *tdvp = ap->a_tdvp;
2089 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2090 struct componentname *cnp = ap->a_cnp;
2091 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2092 struct nfsm_info info;
2094 if (vp->v_mount != tdvp->v_mount) {
2097 lwkt_gettoken(&nmp->nm_token);
2100 * The attribute cache may get out of sync with the server on link.
2101 * Pushing writes to the server before handle was inherited from
2102 * long long ago and it is unclear if we still need to do this.
2105 if (nfs_flush_on_hlink)
2106 VOP_FSYNC(vp, MNT_WAIT, 0);
2109 info.v3 = NFS_ISV3(vp);
2111 nfsstats.rpccnt[NFSPROC_LINK]++;
2112 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2113 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2114 nfsm_rndup(cnp->cn_namelen));
2115 ERROROUT(nfsm_fhtom(&info, vp));
2116 ERROROUT(nfsm_fhtom(&info, tdvp));
2117 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2119 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2120 cnp->cn_cred, &error));
2122 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2123 NFS_LATTR_NOSHRINK));
2124 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2129 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2131 VTONFS(vp)->n_attrstamp = 0;
2133 VTONFS(tdvp)->n_attrstamp = 0;
2135 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2137 if (error == EEXIST)
2139 lwkt_reltoken(&nmp->nm_token);
2141 nfs_knote(vp, NOTE_LINK);
2142 nfs_knote(tdvp, NOTE_WRITE);
2149 * nfs symbolic link create call
2151 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2152 * struct componentname *a_cnp, struct vattr *a_vap,
2156 nfs_symlink(struct vop_old_symlink_args *ap)
2158 struct vnode *dvp = ap->a_dvp;
2159 struct vattr *vap = ap->a_vap;
2160 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2161 struct componentname *cnp = ap->a_cnp;
2162 struct nfsv2_sattr *sp;
2163 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2164 struct vnode *newvp = NULL;
2165 struct nfsm_info info;
2168 info.v3 = NFS_ISV3(dvp);
2169 lwkt_gettoken(&nmp->nm_token);
2171 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2172 slen = strlen(ap->a_target);
2173 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2174 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2175 nfsm_rndup(cnp->cn_namelen) +
2176 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2177 ERROROUT(nfsm_fhtom(&info, dvp));
2178 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2181 nfsm_v3attrbuild(&info, vap, FALSE);
2183 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2185 sp = nfsm_build(&info, NFSX_V2SATTR);
2186 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2187 sp->sa_uid = nfs_xdrneg1;
2188 sp->sa_gid = nfs_xdrneg1;
2189 sp->sa_size = nfs_xdrneg1;
2190 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2191 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2195 * Issue the NFS request and get the rpc response.
2197 * Only NFSv3 responses returning an error of 0 actually return
2198 * a file handle that can be converted into newvp without having
2199 * to do an extra lookup rpc.
2201 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2202 cnp->cn_cred, &error));
2205 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2207 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2211 * out code jumps -> here, mrep is also freed.
2219 * If we get an EEXIST error, silently convert it to no-error
2220 * in case of an NFS retry.
2222 if (error == EEXIST)
2226 * If we do not have (or no longer have) an error, and we could
2227 * not extract the newvp from the response due to the request being
2228 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2229 * to obtain a newvp to return.
2231 if (error == 0 && newvp == NULL) {
2232 struct nfsnode *np = NULL;
2234 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2235 cnp->cn_cred, cnp->cn_td, &np);
2245 VTONFS(dvp)->n_flag |= NLMODIFIED;
2247 VTONFS(dvp)->n_attrstamp = 0;
2248 if (error == 0 && *ap->a_vpp)
2249 nfs_knote(*ap->a_vpp, NOTE_WRITE);
2250 lwkt_reltoken(&nmp->nm_token);
2258 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2259 * struct componentname *a_cnp, struct vattr *a_vap)
2262 nfs_mkdir(struct vop_old_mkdir_args *ap)
2264 struct vnode *dvp = ap->a_dvp;
2265 struct vattr *vap = ap->a_vap;
2266 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2267 struct componentname *cnp = ap->a_cnp;
2268 struct nfsv2_sattr *sp;
2269 struct nfsnode *np = NULL;
2270 struct vnode *newvp = NULL;
2272 int error = 0, wccflag = NFSV3_WCCRATTR;
2275 struct nfsm_info info;
2278 info.v3 = NFS_ISV3(dvp);
2279 lwkt_gettoken(&nmp->nm_token);
2281 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2282 lwkt_reltoken(&nmp->nm_token);
2285 len = cnp->cn_namelen;
2286 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2287 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2288 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2289 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2290 ERROROUT(nfsm_fhtom(&info, dvp));
2291 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2293 nfsm_v3attrbuild(&info, vap, FALSE);
2295 sp = nfsm_build(&info, NFSX_V2SATTR);
2296 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2297 sp->sa_uid = nfs_xdrneg1;
2298 sp->sa_gid = nfs_xdrneg1;
2299 sp->sa_size = nfs_xdrneg1;
2300 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2301 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2303 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2304 cnp->cn_cred, &error));
2306 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2309 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2314 VTONFS(dvp)->n_flag |= NLMODIFIED;
2316 VTONFS(dvp)->n_attrstamp = 0;
2318 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2319 * if we can succeed in looking up the directory.
2321 if (error == EEXIST || (!error && !gotvp)) {
2326 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2330 if (newvp->v_type != VDIR)
2338 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2341 lwkt_reltoken(&nmp->nm_token);
2346 * nfs remove directory call
2348 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2349 * struct componentname *a_cnp)
2352 nfs_rmdir(struct vop_old_rmdir_args *ap)
2354 struct vnode *vp = ap->a_vp;
2355 struct vnode *dvp = ap->a_dvp;
2356 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2357 struct componentname *cnp = ap->a_cnp;
2358 int error = 0, wccflag = NFSV3_WCCRATTR;
2359 struct nfsm_info info;
2362 info.v3 = NFS_ISV3(dvp);
2367 lwkt_gettoken(&nmp->nm_token);
2369 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2370 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2371 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2372 nfsm_rndup(cnp->cn_namelen));
2373 ERROROUT(nfsm_fhtom(&info, dvp));
2374 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2376 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2377 cnp->cn_cred, &error));
2379 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2384 VTONFS(dvp)->n_flag |= NLMODIFIED;
2386 VTONFS(dvp)->n_attrstamp = 0;
2388 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2390 if (error == ENOENT)
2393 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2394 lwkt_reltoken(&nmp->nm_token);
2402 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2405 nfs_readdir(struct vop_readdir_args *ap)
2407 struct vnode *vp = ap->a_vp;
2408 struct nfsnode *np = VTONFS(vp);
2409 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2410 struct uio *uio = ap->a_uio;
2414 if (vp->v_type != VDIR)
2417 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2421 lwkt_gettoken(&nmp->nm_token);
2424 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2425 * and then check that is still valid, or if this is an NQNFS mount
2426 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2427 * VOP_GETATTR() does not necessarily go to the wire.
2429 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2430 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2431 if (VOP_GETATTR(vp, &vattr) == 0 &&
2432 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2434 nfsstats.direofcache_hits++;
2440 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2441 * own cache coherency checks so we do not have to.
2443 tresid = uio->uio_resid;
2444 error = nfs_bioread(vp, uio, 0);
2446 if (!error && uio->uio_resid == tresid)
2447 nfsstats.direofcache_misses++;
2449 lwkt_reltoken(&nmp->nm_token);
2456 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2458 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2459 * offset/block and converts the nfs formatted directory entries for userland
2460 * consumption as well as deals with offsets into the middle of blocks.
2461 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2462 * be block-bounded. It must convert to cookies for the actual RPC.
2465 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2468 struct nfs_dirent *dp = NULL;
2473 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2474 struct nfsnode *dnp = VTONFS(vp);
2476 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2478 struct nfsm_info info;
2481 info.v3 = NFS_ISV3(vp);
2484 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2485 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2486 panic("nfs readdirrpc bad uio");
2490 * If there is no cookie, assume directory was stale.
2492 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2496 return (NFSERR_BAD_COOKIE);
2498 * Loop around doing readdir rpc's of size nm_readdirsize
2499 * truncated to a multiple of DIRBLKSIZ.
2500 * The stopping criteria is EOF or buffer full.
2502 while (more_dirs && bigenough) {
2503 nfsstats.rpccnt[NFSPROC_READDIR]++;
2504 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2505 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2506 ERROROUT(nfsm_fhtom(&info, vp));
2508 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2509 *tl++ = cookie.nfsuquad[0];
2510 *tl++ = cookie.nfsuquad[1];
2511 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2512 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2515 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2516 * WITH NFSv2!!! There's nothing I can really do
2517 * about it other than to hope the server supports
2520 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2521 *tl++ = cookie.nfsuquad[0];
2523 *tl = txdr_unsigned(nmp->nm_readdirsize);
2524 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2526 nfs_vpcred(vp, ND_READ), &error));
2528 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2529 NFS_LATTR_NOSHRINK));
2530 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2531 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2532 dnp->n_cookieverf.nfsuquad[1] = *tl;
2534 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2535 more_dirs = fxdr_unsigned(int, *tl);
2537 /* loop thru the dir entries, converting them to std form */
2538 while (more_dirs && bigenough) {
2540 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2541 fileno = fxdr_hyper(tl);
2542 len = fxdr_unsigned(int, *(tl + 2));
2544 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2545 fileno = fxdr_unsigned(u_quad_t, *tl++);
2546 len = fxdr_unsigned(int, *tl);
2548 if (len <= 0 || len > NFS_MAXNAMLEN) {
2556 * len is the number of bytes in the path element
2557 * name, not including the \0 termination.
2559 * tlen is the number of bytes w have to reserve for
2560 * the path element name.
2562 tlen = nfsm_rndup(len);
2564 tlen += 4; /* To ensure null termination */
2567 * If the entry would cross a DIRBLKSIZ boundary,
2568 * extend the previous nfs_dirent to cover the
2571 left = DIRBLKSIZ - blksiz;
2572 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2573 dp->nfs_reclen += left;
2574 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2575 uiop->uio_iov->iov_len -= left;
2576 uiop->uio_offset += left;
2577 uiop->uio_resid -= left;
2580 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2583 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2584 dp->nfs_ino = fileno;
2585 dp->nfs_namlen = len;
2586 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2587 dp->nfs_type = DT_UNKNOWN;
2588 blksiz += dp->nfs_reclen;
2589 if (blksiz == DIRBLKSIZ)
2591 uiop->uio_offset += sizeof(struct nfs_dirent);
2592 uiop->uio_resid -= sizeof(struct nfs_dirent);
2593 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2594 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2595 ERROROUT(nfsm_mtouio(&info, uiop, len));
2598 * The uiop has advanced by nfs_dirent + len
2599 * but really needs to advance by
2602 cp = uiop->uio_iov->iov_base;
2604 *cp = '\0'; /* null terminate */
2605 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2606 uiop->uio_iov->iov_len -= tlen;
2607 uiop->uio_offset += tlen;
2608 uiop->uio_resid -= tlen;
2611 * NFS strings must be rounded up (nfsm_myouio
2612 * handled that in the bigenough case).
2614 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2617 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2619 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2623 * If we were able to accomodate the last entry,
2624 * get the cookie for the next one. Otherwise
2625 * hold-over the cookie for the one we were not
2626 * able to accomodate.
2629 cookie.nfsuquad[0] = *tl++;
2631 cookie.nfsuquad[1] = *tl++;
2632 } else if (info.v3) {
2637 more_dirs = fxdr_unsigned(int, *tl);
2640 * If at end of rpc data, get the eof boolean
2643 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2644 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2650 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2651 * by increasing d_reclen for the last record.
2654 left = DIRBLKSIZ - blksiz;
2655 dp->nfs_reclen += left;
2656 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2657 uiop->uio_iov->iov_len -= left;
2658 uiop->uio_offset += left;
2659 uiop->uio_resid -= left;
2664 * We hit the end of the directory, update direofoffset.
2666 dnp->n_direofoffset = uiop->uio_offset;
2669 * There is more to go, insert the link cookie so the
2670 * next block can be read.
2672 if (uiop->uio_resid > 0)
2673 kprintf("EEK! readdirrpc resid > 0\n");
2674 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2682 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2685 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2688 struct nfs_dirent *dp;
2690 struct vnode *newvp;
2692 caddr_t dpossav1, dpossav2;
2694 struct mbuf *mdsav1, *mdsav2;
2696 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2697 struct nfsnode *dnp = VTONFS(vp), *np;
2700 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2701 int attrflag, fhsize;
2702 struct nchandle nch;
2703 struct nchandle dnch;
2704 struct nlcomponent nlc;
2705 struct nfsm_info info;
2714 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2715 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2716 panic("nfs readdirplusrpc bad uio");
2719 * Obtain the namecache record for the directory so we have something
2720 * to use as a basis for creating the entries. This function will
2721 * return a held (but not locked) ncp. The ncp may be disconnected
2722 * from the tree and cannot be used for upward traversals, and the
2723 * ncp may be unnamed. Note that other unrelated operations may
2724 * cause the ncp to be named at any time.
2726 * We have to lock the ncp to prevent a lock order reversal when
2727 * rdirplus does nlookups of the children, because the vnode is
2728 * locked and has to stay that way.
2730 cache_fromdvp(vp, NULL, 0, &dnch);
2731 bzero(&nlc, sizeof(nlc));
2735 * If there is no cookie, assume directory was stale.
2737 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2743 return (NFSERR_BAD_COOKIE);
2747 * Loop around doing readdir rpc's of size nm_readdirsize
2748 * truncated to a multiple of DIRBLKSIZ.
2749 * The stopping criteria is EOF or buffer full.
2751 while (more_dirs && bigenough) {
2752 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2753 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2754 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2755 ERROROUT(nfsm_fhtom(&info, vp));
2756 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2757 *tl++ = cookie.nfsuquad[0];
2758 *tl++ = cookie.nfsuquad[1];
2759 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2760 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2761 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2762 *tl = txdr_unsigned(nmp->nm_rsize);
2763 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2765 nfs_vpcred(vp, ND_READ), &error));
2766 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2767 NFS_LATTR_NOSHRINK));
2768 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2769 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2770 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2771 more_dirs = fxdr_unsigned(int, *tl);
2773 /* loop thru the dir entries, doctoring them to 4bsd form */
2774 while (more_dirs && bigenough) {
2775 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2776 fileno = fxdr_hyper(tl);
2777 len = fxdr_unsigned(int, *(tl + 2));
2778 if (len <= 0 || len > NFS_MAXNAMLEN) {
2784 tlen = nfsm_rndup(len);
2786 tlen += 4; /* To ensure null termination*/
2787 left = DIRBLKSIZ - blksiz;
2788 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2789 dp->nfs_reclen += left;
2790 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2791 uiop->uio_iov->iov_len -= left;
2792 uiop->uio_offset += left;
2793 uiop->uio_resid -= left;
2796 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2799 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2800 dp->nfs_ino = fileno;
2801 dp->nfs_namlen = len;
2802 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2803 dp->nfs_type = DT_UNKNOWN;
2804 blksiz += dp->nfs_reclen;
2805 if (blksiz == DIRBLKSIZ)
2807 uiop->uio_offset += sizeof(struct nfs_dirent);
2808 uiop->uio_resid -= sizeof(struct nfs_dirent);
2809 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2810 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2811 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2812 nlc.nlc_namelen = len;
2813 ERROROUT(nfsm_mtouio(&info, uiop, len));
2814 cp = uiop->uio_iov->iov_base;
2817 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2818 uiop->uio_iov->iov_len -= tlen;
2819 uiop->uio_offset += tlen;
2820 uiop->uio_resid -= tlen;
2822 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2824 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2826 cookie.nfsuquad[0] = *tl++;
2827 cookie.nfsuquad[1] = *tl++;
2833 * Since the attributes are before the file handle
2834 * (sigh), we must skip over the attributes and then
2835 * come back and get them.
2837 attrflag = fxdr_unsigned(int, *tl);
2839 dpossav1 = info.dpos;
2841 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2842 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2843 doit = fxdr_unsigned(int, *tl);
2845 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2847 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2848 !NFS_CMPFH(dnp, fhp, fhsize)
2852 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2853 nlc.nlc_namelen, nlc.nlc_namelen,
2857 * This is a bit hokey but there isn't
2858 * much we can do about it. We can't
2859 * hold the directory vp locked while
2860 * doing lookups and gets.
2862 nch = cache_nlookup_nonblock(&dnch, &nlc);
2863 if (nch.ncp == NULL)
2865 cache_setunresolved(&nch);
2866 error = nfs_nget_nonblock(vp->v_mount, fhp,
2874 dpossav2 = info.dpos;
2875 info.dpos = dpossav1;
2878 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2879 info.dpos = dpossav2;
2882 IFTODT(VTTOIF(np->n_vattr.va_type));
2883 nfs_cache_setvp(&nch, newvp,
2884 nfspos_cache_timeout);
2892 kprintf("Warning: NFS/rddirplus, "
2893 "UNABLE TO ENTER %*.*s\n",
2894 nlc.nlc_namelen, nlc.nlc_namelen,
2900 /* Just skip over the file handle */
2901 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2902 i = fxdr_unsigned(int, *tl);
2903 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2905 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2906 more_dirs = fxdr_unsigned(int, *tl);
2909 * If at end of rpc data, get the eof boolean
2912 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2913 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2919 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2920 * by increasing d_reclen for the last record.
2923 left = DIRBLKSIZ - blksiz;
2924 dp->nfs_reclen += left;
2925 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2926 uiop->uio_iov->iov_len -= left;
2927 uiop->uio_offset += left;
2928 uiop->uio_resid -= left;
2932 * We are now either at the end of the directory or have filled the
2936 dnp->n_direofoffset = uiop->uio_offset;
2938 if (uiop->uio_resid > 0)
2939 kprintf("EEK! readdirplusrpc resid > 0\n");
2940 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2944 if (newvp != NULLVP) {
2957 * Silly rename. To make the NFS filesystem that is stateless look a little
2958 * more like the "ufs" a remove of an active vnode is translated to a rename
2959 * to a funny looking filename that is removed by nfs_inactive on the
2960 * nfsnode. There is the potential for another process on a different client
2961 * to create the same funny name between the nfs_lookitup() fails and the
2962 * nfs_rename() completes, but...
2965 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2967 struct sillyrename *sp;
2972 * Force finalization so the VOP_INACTIVE() call is not delayed.
2973 * This prevents cred structures from building up in nfsnodes
2974 * for deleted files.
2976 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
2978 np->n_flag |= NREMOVED;
2981 * We previously purged dvp instead of vp. I don't know why, it
2982 * completely destroys performance. We can't do it anyway with the
2983 * new VFS API since we would be breaking the namecache topology.
2985 cache_purge(vp); /* XXX */
2987 if (vp->v_type == VDIR)
2988 panic("nfs: sillyrename dir");
2990 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2991 sp->s_cred = crdup(cnp->cn_cred);
2995 /* Fudge together a funny name */
2996 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2997 (int)(intptr_t)cnp->cn_td);
2999 /* Try lookitups until we get one that isn't there */
3000 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3001 cnp->cn_td, NULL) == 0) {
3003 if (sp->s_name[4] > 'z') {
3008 error = nfs_renameit(dvp, cnp, sp);
3011 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3013 np->n_sillyrename = sp;
3018 kfree((caddr_t)sp, M_NFSREQ);
3024 * Look up a file name and optionally either update the file handle or
3025 * allocate an nfsnode, depending on the value of npp.
3026 * npp == NULL --> just do the lookup
3027 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3029 * *npp != NULL --> update the file handle in the vnode
3032 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
3033 struct thread *td, struct nfsnode **npp)
3035 struct vnode *newvp = NULL;
3036 struct nfsnode *np, *dnp = VTONFS(dvp);
3037 int error = 0, fhlen, attrflag;
3039 struct nfsm_info info;
3042 info.v3 = NFS_ISV3(dvp);
3044 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3045 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3046 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3047 ERROROUT(nfsm_fhtom(&info, dvp));
3048 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3049 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
3050 if (npp && !error) {
3051 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
3054 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3055 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
3056 np->n_fhp = &np->n_fh;
3057 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3058 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
3059 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3060 np->n_fhsize = fhlen;
3062 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3066 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
3075 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3076 NFS_LATTR_NOSHRINK));
3077 if (!attrflag && *npp == NULL) {
3087 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3093 if (npp && *npp == NULL) {
3108 * Nfs Version 3 commit rpc
3110 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3114 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3116 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3117 int error = 0, wccflag = NFSV3_WCCRATTR;
3118 struct nfsm_info info;
3124 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3126 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3127 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3128 ERROROUT(nfsm_fhtom(&info, vp));
3129 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3130 txdr_hyper(offset, tl);
3132 *tl = txdr_unsigned(cnt);
3133 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3134 nfs_vpcred(vp, ND_WRITE), &error));
3135 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3137 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3138 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3139 NFSX_V3WRITEVERF)) {
3140 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3142 error = NFSERR_STALEWRITEVERF;
3153 * - make nfs_bmap() essentially a no-op that does no translation
3154 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3155 * (Maybe I could use the process's page mapping, but I was concerned that
3156 * Kernel Write might not be enabled and also figured copyout() would do
3157 * a lot more work than bcopy() and also it currently happens in the
3158 * context of the swapper process (2).
3160 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3161 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3164 nfs_bmap(struct vop_bmap_args *ap)
3166 /* no token lock required */
3167 if (ap->a_doffsetp != NULL)
3168 *ap->a_doffsetp = ap->a_loffset;
3169 if (ap->a_runp != NULL)
3171 if (ap->a_runb != NULL)
3180 nfs_strategy(struct vop_strategy_args *ap)
3182 struct bio *bio = ap->a_bio;
3184 struct buf *bp __debugvar = bio->bio_buf;
3185 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3189 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3190 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3191 KASSERT(BUF_LOCKINUSE(bp),
3192 ("nfs_strategy: buffer %p not locked", bp));
3194 if (bio->bio_flags & BIO_SYNC)
3195 td = curthread; /* XXX */
3199 lwkt_gettoken(&nmp->nm_token);
3202 * We probably don't need to push an nbio any more since no
3203 * block conversion is required due to the use of 64 bit byte
3204 * offsets, but do it anyway.
3206 * NOTE: When NFS callers itself via this strategy routines and
3207 * sets up a synchronous I/O, it expects the I/O to run
3208 * synchronously (its bio_done routine just assumes it),
3209 * so for now we have to honor the bit.
3211 nbio = push_bio(bio);
3212 nbio->bio_offset = bio->bio_offset;
3213 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3216 * If the op is asynchronous and an i/o daemon is waiting
3217 * queue the request, wake it up and wait for completion
3218 * otherwise just do it ourselves.
3220 if (bio->bio_flags & BIO_SYNC) {
3221 error = nfs_doio(ap->a_vp, nbio, td);
3223 nfs_asyncio(ap->a_vp, nbio);
3226 lwkt_reltoken(&nmp->nm_token);
3234 * NB Currently unsupported.
3236 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3240 nfs_mmap(struct vop_mmap_args *ap)
3242 /* no token lock required */
3247 * fsync vnode op. Just call nfs_flush() with commit == 1.
3249 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3253 nfs_fsync(struct vop_fsync_args *ap)
3255 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3258 lwkt_gettoken(&nmp->nm_token);
3261 * NOTE: Because attributes are set synchronously we currently
3262 * do not have to implement vsetisdirty()/vclrisdirty().
3264 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3266 lwkt_reltoken(&nmp->nm_token);
3272 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3273 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3274 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3275 * set the buffer contains data that has already been written to the server
3276 * and which now needs a commit RPC.
3278 * If commit is 0 we only take one pass and only flush buffers containing new
3281 * If commit is 1 we take two passes, issuing a commit RPC in the second
3284 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3285 * to completely flush all pending data.
3287 * Note that the RB_SCAN code properly handles the case where the
3288 * callback might block and directly or indirectly (another thread) cause
3289 * the RB tree to change.
3292 #ifndef NFS_COMMITBVECSIZ
3293 #define NFS_COMMITBVECSIZ 16
3296 struct nfs_flush_info {
3297 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3304 struct buf *bvary[NFS_COMMITBVECSIZ];
3310 static int nfs_flush_bp(struct buf *bp, void *data);
3311 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3314 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3316 struct nfsnode *np = VTONFS(vp);
3317 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3318 struct nfs_flush_info info;
3321 bzero(&info, sizeof(info));
3324 info.waitfor = waitfor;
3325 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3327 lwkt_gettoken(&vp->v_token);
3333 info.mode = NFI_FLUSHNEW;
3334 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3335 nfs_flush_bp, &info);
3338 * Take a second pass if committing and no error occured.
3339 * Clean up any left over collection (whether an error
3342 if (commit && error == 0) {
3343 info.mode = NFI_COMMIT;
3344 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3345 nfs_flush_bp, &info);
3347 error = nfs_flush_docommit(&info, error);
3351 * Wait for pending I/O to complete before checking whether
3352 * any further dirty buffers exist.
3354 while (waitfor == MNT_WAIT &&
3355 bio_track_active(&vp->v_track_write)) {
3356 error = bio_track_wait(&vp->v_track_write,
3357 info.slpflag, info.slptimeo);
3360 * We have to be able to break out if this
3361 * is an 'intr' mount.
3363 if (nfs_sigintr(nmp, NULL, td)) {
3369 * Since we do not process pending signals,
3370 * once we get a PCATCH our tsleep() will no
3371 * longer sleep, switch to a fixed timeout
3374 if (info.slpflag == PCATCH) {
3376 info.slptimeo = 2 * hz;
3383 * Loop if we are flushing synchronous as well as committing,
3384 * and dirty buffers are still present. Otherwise we might livelock.
3386 } while (waitfor == MNT_WAIT && commit &&
3387 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3390 * The callbacks have to return a negative error to terminate the
3397 * Deal with any error collection
3399 if (np->n_flag & NWRITEERR) {
3400 error = np->n_error;
3401 np->n_flag &= ~NWRITEERR;
3403 lwkt_reltoken(&vp->v_token);
3409 nfs_flush_bp(struct buf *bp, void *data)
3411 struct nfs_flush_info *info = data;
3417 switch(info->mode) {
3419 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3420 if (error && info->loops && info->waitfor == MNT_WAIT) {
3421 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3423 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3424 if (info->slpflag & PCATCH)
3425 lkflags |= LK_PCATCH;
3426 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3432 * Ignore locking errors
3440 * The buffer may have changed out from under us, even if
3441 * we did not block (MPSAFE). Check again now that it is
3444 if (bp->b_vp == info->vp &&
3445 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3454 * Only process buffers in need of a commit which we can
3455 * immediately lock. This may prevent a buffer from being
3456 * committed, but the normal flush loop will block on the
3457 * same buffer so we shouldn't get into an endless loop.
3459 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3460 (B_DELWRI | B_NEEDCOMMIT)) {
3463 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3467 * We must recheck after successfully locking the buffer.
3469 if (bp->b_vp != info->vp ||
3470 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3471 (B_DELWRI | B_NEEDCOMMIT)) {
3477 * NOTE: storing the bp in the bvary[] basically sets
3478 * it up for a commit operation.
3480 * We must call vfs_busy_pages() now so the commit operation
3481 * is interlocked with user modifications to memory mapped
3482 * pages. The b_dirtyoff/b_dirtyend range is not correct
3483 * until after the pages have been busied.
3485 * Note: to avoid loopback deadlocks, we do not
3486 * assign b_runningbufspace.
3489 bp->b_cmd = BUF_CMD_WRITE;
3490 vfs_busy_pages(bp->b_vp, bp);
3491 info->bvary[info->bvsize] = bp;
3492 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3493 if (info->bvsize == 0 || toff < info->beg_off)
3494 info->beg_off = toff;
3495 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3496 if (info->bvsize == 0 || toff > info->end_off)
3497 info->end_off = toff;
3499 if (info->bvsize == NFS_COMMITBVECSIZ) {
3500 error = nfs_flush_docommit(info, 0);
3501 KKASSERT(info->bvsize == 0);
3509 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3519 if (info->bvsize > 0) {
3521 * Commit data on the server, as required. Note that
3522 * nfs_commit will use the vnode's cred for the commit.
3523 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3525 bytes = info->end_off - info->beg_off;
3526 if (bytes > 0x40000000)
3531 retv = nfs_commitrpc_uio(vp, info->beg_off,
3532 (int)bytes, info->td);
3533 if (retv == NFSERR_STALEWRITEVERF)
3534 nfs_clearcommit(vp->v_mount);
3538 * Now, either mark the blocks I/O done or mark the
3539 * blocks dirty, depending on whether the commit
3542 for (i = 0; i < info->bvsize; ++i) {
3543 bp = info->bvary[i];
3544 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3546 * Either an error or the original
3547 * vfs_busy_pages() cleared B_NEEDCOMMIT
3548 * due to finding new dirty VM pages in
3551 * Leave B_DELWRI intact.
3553 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3554 vfs_unbusy_pages(bp);
3555 bp->b_cmd = BUF_CMD_DONE;
3559 * Success, remove B_DELWRI ( bundirty() ).
3561 * b_dirtyoff/b_dirtyend seem to be NFS
3562 * specific. We should probably move that
3563 * into bundirty(). XXX
3565 * We are faking an I/O write, we have to
3566 * start the transaction in order to
3567 * immediately biodone() it.
3570 bp->b_flags &= ~B_ERROR;
3571 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3572 bp->b_dirtyoff = bp->b_dirtyend = 0;
3573 biodone(&bp->b_bio1);
3582 * NFS advisory byte-level locks.
3583 * Currently unsupported.
3585 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3589 nfs_advlock(struct vop_advlock_args *ap)
3591 struct nfsnode *np = VTONFS(ap->a_vp);
3593 /* no token lock currently required */
3595 * The following kludge is to allow diskless support to work
3596 * until a real NFS lockd is implemented. Basically, just pretend
3597 * that this is a local lock.
3599 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3603 * Print out the contents of an nfsnode.
3605 * nfs_print(struct vnode *a_vp)
3608 nfs_print(struct vop_print_args *ap)
3610 struct vnode *vp = ap->a_vp;
3611 struct nfsnode *np = VTONFS(vp);
3613 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3614 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3615 if (vp->v_type == VFIFO)
3622 * nfs special file access vnode op.
3624 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3627 nfs_laccess(struct vop_access_args *ap)
3629 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3633 lwkt_gettoken(&nmp->nm_token);
3634 error = VOP_GETATTR(ap->a_vp, &vattr);
3636 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3639 lwkt_reltoken(&nmp->nm_token);
3645 * Read wrapper for fifos.
3647 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3648 * struct ucred *a_cred)
3651 nfsfifo_read(struct vop_read_args *ap)
3653 struct nfsnode *np = VTONFS(ap->a_vp);
3655 /* no token access required */
3660 getnanotime(&np->n_atim);
3661 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3665 * Write wrapper for fifos.
3667 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3668 * struct ucred *a_cred)
3671 nfsfifo_write(struct vop_write_args *ap)
3673 struct nfsnode *np = VTONFS(ap->a_vp);
3675 /* no token access required */
3680 getnanotime(&np->n_mtim);
3681 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3685 * Close wrapper for fifos.
3687 * Update the times on the nfsnode then do fifo close.
3689 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3692 nfsfifo_close(struct vop_close_args *ap)
3694 struct vnode *vp = ap->a_vp;
3695 struct nfsnode *np = VTONFS(vp);
3699 /* no token access required */
3701 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3702 if (np->n_flag & (NACC | NUPD)) {
3704 if (np->n_flag & NACC)
3706 if (np->n_flag & NUPD)
3709 if (VREFCNT(vp) == 1 &&
3710 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3712 if (np->n_flag & NACC)
3713 vattr.va_atime = np->n_atim;
3714 if (np->n_flag & NUPD)
3715 vattr.va_mtime = np->n_mtim;
3716 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3719 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3722 /************************************************************************
3724 ************************************************************************/
3726 static void filt_nfsdetach(struct knote *kn);
3727 static int filt_nfsread(struct knote *kn, long hint);
3728 static int filt_nfswrite(struct knote *kn, long hint);
3729 static int filt_nfsvnode(struct knote *kn, long hint);
3731 static struct filterops nfsread_filtops =
3732 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3733 NULL, filt_nfsdetach, filt_nfsread };
3734 static struct filterops nfswrite_filtops =
3735 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3736 NULL, filt_nfsdetach, filt_nfswrite };
3737 static struct filterops nfsvnode_filtops =
3738 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3739 NULL, filt_nfsdetach, filt_nfsvnode };
3742 nfs_kqfilter (struct vop_kqfilter_args *ap)
3744 struct vnode *vp = ap->a_vp;
3745 struct knote *kn = ap->a_kn;
3747 switch (kn->kn_filter) {
3749 kn->kn_fop = &nfsread_filtops;
3752 kn->kn_fop = &nfswrite_filtops;
3755 kn->kn_fop = &nfsvnode_filtops;
3758 return (EOPNOTSUPP);
3761 kn->kn_hook = (caddr_t)vp;
3763 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3769 filt_nfsdetach(struct knote *kn)
3771 struct vnode *vp = (void *)kn->kn_hook;
3773 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3777 filt_nfsread(struct knote *kn, long hint)
3779 struct vnode *vp = (void *)kn->kn_hook;
3780 struct nfsnode *node = VTONFS(vp);
3783 if (hint == NOTE_REVOKE) {
3784 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3789 * Interlock against MP races when performing this function. XXX
3791 /* TMPFS_NODE_LOCK_SH(node); */
3792 off = node->n_size - kn->kn_fp->f_offset;
3793 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3794 if (kn->kn_sfflags & NOTE_OLDAPI) {
3795 /* TMPFS_NODE_UNLOCK(node); */
3798 if (kn->kn_data == 0) {
3799 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3801 /* TMPFS_NODE_UNLOCK(node); */
3802 return (kn->kn_data != 0);
3806 filt_nfswrite(struct knote *kn, long hint)
3808 if (hint == NOTE_REVOKE)
3809 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3815 filt_nfsvnode(struct knote *kn, long hint)
3817 if (kn->kn_sfflags & hint)
3818 kn->kn_fflags |= hint;
3819 if (hint == NOTE_REVOKE) {
3820 kn->kn_flags |= (EV_EOF | EV_NODATA);
3823 return (kn->kn_fflags != 0);