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>
47 #include <sys/resourcevar.h>
49 #include <sys/mount.h>
51 #include <sys/malloc.h>
53 #include <sys/namei.h>
54 #include <sys/nlookup.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
61 #include <sys/sysctl.h>
65 #include <vm/vm_extern.h>
69 #include <vfs/fifofs/fifo.h>
70 #include <vfs/ufs/dir.h>
80 #include "nfsm_subs.h"
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
90 static int nfsfifo_read (struct vop_read_args *);
91 static int nfsfifo_write (struct vop_write_args *);
92 static int nfsfifo_close (struct vop_close_args *);
93 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
94 static int nfs_lookup (struct vop_old_lookup_args *);
95 static int nfs_create (struct vop_old_create_args *);
96 static int nfs_mknod (struct vop_old_mknod_args *);
97 static int nfs_open (struct vop_open_args *);
98 static int nfs_close (struct vop_close_args *);
99 static int nfs_access (struct vop_access_args *);
100 static int nfs_getattr (struct vop_getattr_args *);
101 static int nfs_setattr (struct vop_setattr_args *);
102 static int nfs_read (struct vop_read_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_open = nfs_open,
143 .vop_print = nfs_print,
144 .vop_read = nfs_read,
145 .vop_readdir = nfs_readdir,
146 .vop_readlink = nfs_readlink,
147 .vop_reclaim = nfs_reclaim,
148 .vop_old_remove = nfs_remove,
149 .vop_old_rename = nfs_rename,
150 .vop_old_rmdir = nfs_rmdir,
151 .vop_setattr = nfs_setattr,
152 .vop_strategy = nfs_strategy,
153 .vop_old_symlink = nfs_symlink,
154 .vop_write = nfs_write,
155 .vop_nresolve = nfs_nresolve,
156 .vop_kqfilter = nfs_kqfilter
160 * Special device vnode ops
162 struct vop_ops nfsv2_spec_vops = {
163 .vop_default = vop_defaultop,
164 .vop_access = nfs_laccess,
165 .vop_close = nfs_close,
166 .vop_fsync = nfs_fsync,
167 .vop_getattr = nfs_getattr,
168 .vop_inactive = nfs_inactive,
169 .vop_print = nfs_print,
170 .vop_read = vop_stdnoread,
171 .vop_reclaim = nfs_reclaim,
172 .vop_setattr = nfs_setattr,
173 .vop_write = vop_stdnowrite
176 struct vop_ops nfsv2_fifo_vops = {
177 .vop_default = fifo_vnoperate,
178 .vop_access = nfs_laccess,
179 .vop_close = nfsfifo_close,
180 .vop_fsync = nfs_fsync,
181 .vop_getattr = nfs_getattr,
182 .vop_inactive = nfs_inactive,
183 .vop_print = nfs_print,
184 .vop_read = nfsfifo_read,
185 .vop_reclaim = nfs_reclaim,
186 .vop_setattr = nfs_setattr,
187 .vop_write = nfsfifo_write
190 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
191 struct componentname *cnp,
193 static int nfs_removerpc (struct vnode *dvp, const char *name,
195 struct ucred *cred, struct thread *td);
196 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
197 int fnamelen, struct vnode *tdvp,
198 const char *tnameptr, int tnamelen,
199 struct ucred *cred, struct thread *td);
200 static int nfs_renameit (struct vnode *sdvp,
201 struct componentname *scnp,
202 struct sillyrename *sp);
204 SYSCTL_DECL(_vfs_nfs);
206 static int nfs_flush_on_rename = 1;
207 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
208 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
209 static int nfs_flush_on_hlink = 0;
210 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
211 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
213 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
214 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
215 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
217 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
218 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
219 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
221 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
222 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
223 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
225 static int nfsv3_commit_on_close = 0;
226 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
227 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
230 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
232 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
233 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
236 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
237 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
238 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
242 nfs_knote(struct vnode *vp, int flags)
245 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
249 * Returns whether a name component is a degenerate '.' or '..'.
253 nlcdegenerate(struct nlcomponent *nlc)
255 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
257 if (nlc->nlc_namelen == 2 &&
258 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
264 nfs3_access_otw(struct vnode *vp, int wmode,
265 struct thread *td, struct ucred *cred)
267 struct nfsnode *np = VTONFS(vp);
272 struct nfsm_info info;
277 nfsstats.rpccnt[NFSPROC_ACCESS]++;
278 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
279 NFSX_FH(info.v3) + NFSX_UNSIGNED);
280 ERROROUT(nfsm_fhtom(&info, vp));
281 tl = nfsm_build(&info, NFSX_UNSIGNED);
282 *tl = txdr_unsigned(wmode);
283 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
284 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
286 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
287 rmode = fxdr_unsigned(u_int32_t, *tl);
289 np->n_modeuid = cred->cr_uid;
290 np->n_modestamp = mycpu->gd_time_seconds;
299 * nfs access vnode op.
300 * For nfs version 2, just return ok. File accesses may fail later.
301 * For nfs version 3, use the access rpc to check accessibility. If file modes
302 * are changed on the server, accesses might still fail later.
304 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
307 nfs_access(struct vop_access_args *ap)
312 struct vnode *vp = ap->a_vp;
313 thread_t td = curthread;
315 u_int32_t mode, wmode;
316 struct nfsnode *np = VTONFS(vp);
317 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
318 int v3 = NFS_ISV3(vp);
320 lwkt_gettoken(&nmp->nm_token);
323 * Disallow write attempts on filesystems mounted read-only;
324 * unless the file is a socket, fifo, or a block or character
325 * device resident on the filesystem.
327 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
328 switch (vp->v_type) {
332 lwkt_reltoken(&nmp->nm_token);
340 * The NFS protocol passes only the effective uid/gid over the wire but
341 * we need to check access against real ids if AT_EACCESS not set.
342 * Handle this case by cloning the credentials and setting the
343 * effective ids to the real ones.
345 * The crdup() here can cause a lot of ucred structures to build-up
346 * (up to maxvnodes), so do our best to avoid it.
348 if (ap->a_flags & AT_EACCESS) {
349 cred = crhold(ap->a_cred);
352 if (cred->cr_uid == cred->cr_ruid &&
353 cred->cr_gid == cred->cr_rgid) {
354 cred = crhold(ap->a_cred);
356 cred = crdup(ap->a_cred);
357 cred->cr_uid = cred->cr_ruid;
358 cred->cr_gid = cred->cr_rgid;
363 * For nfs v3, check to see if we have done this recently, and if
364 * so return our cached result instead of making an ACCESS call.
365 * If not, do an access rpc, otherwise you are stuck emulating
366 * ufs_access() locally using the vattr. This may not be correct,
367 * since the server may apply other access criteria such as
368 * client uid-->server uid mapping that we do not know about.
371 if (ap->a_mode & VREAD)
372 mode = NFSV3ACCESS_READ;
375 if (vp->v_type != VDIR) {
376 if (ap->a_mode & VWRITE)
377 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
378 if (ap->a_mode & VEXEC)
379 mode |= NFSV3ACCESS_EXECUTE;
381 if (ap->a_mode & VWRITE)
382 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
384 if (ap->a_mode & VEXEC)
385 mode |= NFSV3ACCESS_LOOKUP;
387 /* XXX safety belt, only make blanket request if caching */
388 if (nfsaccess_cache_timeout > 0) {
389 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
390 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
391 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
397 * Does our cached result allow us to give a definite yes to
400 if (np->n_modestamp &&
401 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
402 (cred->cr_uid == np->n_modeuid) &&
403 ((np->n_mode & mode) == mode)) {
404 nfsstats.accesscache_hits++;
407 * Either a no, or a don't know. Go to the wire.
409 nfsstats.accesscache_misses++;
410 error = nfs3_access_otw(vp, wmode, td, cred);
412 if ((np->n_mode & mode) != mode) {
418 if ((error = nfs_laccess(ap)) != 0) {
420 lwkt_reltoken(&nmp->nm_token);
425 * Attempt to prevent a mapped root from accessing a file
426 * which it shouldn't. We try to read a byte from the file
427 * if the user is root and the file is not zero length.
428 * After calling nfs_laccess, we should have the correct
431 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
432 && VTONFS(vp)->n_size > 0) {
439 auio.uio_iov = &aiov;
443 auio.uio_segflg = UIO_SYSSPACE;
444 auio.uio_rw = UIO_READ;
447 if (vp->v_type == VREG) {
448 error = nfs_readrpc_uio(vp, &auio);
449 } else if (vp->v_type == VDIR) {
451 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
453 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
454 error = nfs_readdirrpc_uio(vp, &auio);
456 } else if (vp->v_type == VLNK) {
457 error = nfs_readlinkrpc_uio(vp, &auio);
464 * [re]record creds for reading and/or writing if access
465 * was granted. Assume the NFS server will grant read access
466 * for execute requests.
469 if ((ap->a_mode & (VREAD|VEXEC)) &&
470 !nfs_crsame(cred, np->n_rucred)) {
471 ncred = nfs_crhold(cred);
472 ocred = np->n_rucred;
473 np->n_rucred = ncred;
477 if ((ap->a_mode & VWRITE) &&
478 !nfs_crsame(cred, np->n_wucred)) {
479 ncred = nfs_crhold(cred);
480 ocred = np->n_wucred;
481 np->n_wucred = ncred;
486 lwkt_reltoken(&nmp->nm_token);
494 * Check to see if the type is ok
495 * and that deletion is not in progress.
496 * For paged in text files, you will need to flush the page cache
497 * if consistency is lost.
499 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
504 nfs_open(struct vop_open_args *ap)
506 struct vnode *vp = ap->a_vp;
507 struct nfsnode *np = VTONFS(vp);
508 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
514 lwkt_gettoken(&nmp->nm_token);
516 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
518 kprintf("open eacces vtyp=%d\n",vp->v_type);
520 lwkt_reltoken(&nmp->nm_token);
525 * Save valid creds for reading and writing for later RPCs.
527 if ((ap->a_mode & FREAD) && !nfs_crsame(ap->a_cred, np->n_rucred)) {
528 ncred = nfs_crhold(ap->a_cred);
529 ocred = np->n_rucred;
530 np->n_rucred = ncred;
534 if ((ap->a_mode & FWRITE) && !nfs_crsame(ap->a_cred, np->n_wucred)) {
535 ncred = nfs_crhold(ap->a_cred);
536 ocred = np->n_wucred;
537 np->n_wucred = ncred;
543 * Clear the attribute cache only if opening with write access. It
544 * is unclear if we should do this at all here, but we certainly
545 * should not clear the cache unconditionally simply because a file
548 if (ap->a_mode & FWRITE)
552 * For normal NFS, reconcile changes made locally verses
553 * changes made remotely. Note that VOP_GETATTR only goes
554 * to the wire if the cached attribute has timed out or been
557 * If local modifications have been made clear the attribute
558 * cache to force an attribute and modified time check. If
559 * GETATTR detects that the file has been changed by someone
560 * other then us it will set NRMODIFIED.
562 * If we are opening a directory and local changes have been
563 * made we have to invalidate the cache in order to ensure
564 * that we get the most up-to-date information from the
567 if (np->n_flag & NLMODIFIED) {
569 if (vp->v_type == VDIR) {
570 error = nfs_vinvalbuf(vp, V_SAVE, 1);
571 if (error == EINTR) {
572 lwkt_reltoken(&nmp->nm_token);
578 error = VOP_GETATTR(vp, &vattr);
580 lwkt_reltoken(&nmp->nm_token);
583 if (np->n_flag & NRMODIFIED) {
584 if (vp->v_type == VDIR)
586 error = nfs_vinvalbuf(vp, V_SAVE, 1);
587 if (error == EINTR) {
588 lwkt_reltoken(&nmp->nm_token);
591 np->n_flag &= ~NRMODIFIED;
593 error = vop_stdopen(ap);
594 lwkt_reltoken(&nmp->nm_token);
601 * What an NFS client should do upon close after writing is a debatable issue.
602 * Most NFS clients push delayed writes to the server upon close, basically for
604 * 1 - So that any write errors may be reported back to the client process
605 * doing the close system call. By far the two most likely errors are
606 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
607 * 2 - To put a worst case upper bound on cache inconsistency between
608 * multiple clients for the file.
609 * There is also a consistency problem for Version 2 of the protocol w.r.t.
610 * not being able to tell if other clients are writing a file concurrently,
611 * since there is no way of knowing if the changed modify time in the reply
612 * is only due to the write for this client.
613 * (NFS Version 3 provides weak cache consistency data in the reply that
614 * should be sufficient to detect and handle this case.)
616 * The current code does the following:
617 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
618 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
619 * or commit them (this satisfies 1 and 2 except for the
620 * case where the server crashes after this close but
621 * before the commit RPC, which is felt to be "good
622 * enough". Changing the last argument to nfs_flush() to
623 * a 1 would force a commit operation, if it is felt a
624 * commit is necessary now.
625 * for NQNFS - do nothing now, since 2 is dealt with via leases and
626 * 1 should be dealt with via an fsync() system call for
627 * cases where write errors are important.
629 * nfs_close(struct vnode *a_vp, int a_fflag)
633 nfs_close(struct vop_close_args *ap)
635 struct vnode *vp = ap->a_vp;
636 struct nfsnode *np = VTONFS(vp);
637 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
639 thread_t td = curthread;
641 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
642 lwkt_gettoken(&nmp->nm_token);
644 if (vp->v_type == VREG) {
645 if (np->n_flag & NLMODIFIED) {
648 * Under NFSv3 we have dirty buffers to dispose of. We
649 * must flush them to the NFS server. We have the option
650 * of waiting all the way through the commit rpc or just
651 * waiting for the initial write. The default is to only
652 * wait through the initial write so the data is in the
653 * server's cache, which is roughly similar to the state
654 * a standard disk subsystem leaves the file in on close().
656 * We cannot clear the NLMODIFIED bit in np->n_flag due to
657 * potential races with other processes, and certainly
658 * cannot clear it if we don't commit.
660 int cm = nfsv3_commit_on_close ? 1 : 0;
661 error = nfs_flush(vp, MNT_WAIT, td, cm);
662 /* np->n_flag &= ~NLMODIFIED; */
664 error = nfs_vinvalbuf(vp, V_SAVE, 1);
668 if (np->n_flag & NWRITEERR) {
669 np->n_flag &= ~NWRITEERR;
674 lwkt_reltoken(&nmp->nm_token);
680 * nfs getattr call from vfs.
682 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
685 nfs_getattr(struct vop_getattr_args *ap)
687 struct vnode *vp = ap->a_vp;
688 struct nfsnode *np = VTONFS(vp);
689 struct nfsmount *nmp;
691 thread_t td = curthread;
692 struct nfsm_info info;
695 info.v3 = NFS_ISV3(vp);
696 nmp = VFSTONFS(vp->v_mount);
698 lwkt_gettoken(&nmp->nm_token);
701 * Update local times for special files.
703 if (np->n_flag & (NACC | NUPD))
706 * First look in the cache.
708 if (nfs_getattrcache(vp, ap->a_vap) == 0)
711 if (info.v3 && nfsaccess_cache_timeout > 0) {
712 nfsstats.accesscache_misses++;
713 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
714 if (nfs_getattrcache(vp, ap->a_vap) == 0)
718 nfsstats.rpccnt[NFSPROC_GETATTR]++;
719 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
720 ERROROUT(nfsm_fhtom(&info, vp));
721 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
722 nfs_vpcred(vp, ND_CHECK), &error));
724 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
730 * NFS doesn't support chflags flags. If the nfs mount was
731 * made -o cache set the UF_CACHE bit for swapcache.
733 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
734 ap->a_vap->va_flags |= UF_CACHE;
736 lwkt_reltoken(&nmp->nm_token);
743 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
746 nfs_setattr(struct vop_setattr_args *ap)
748 struct vnode *vp = ap->a_vp;
749 struct nfsnode *np = VTONFS(vp);
750 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
751 struct vattr *vap = ap->a_vap;
755 thread_t td = curthread;
761 * Setting of flags is not supported.
763 if (vap->va_flags != VNOVAL)
767 * Disallow write attempts if the filesystem is mounted read-only.
769 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
770 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
771 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
772 (vp->v_mount->mnt_flag & MNT_RDONLY))
775 lwkt_gettoken(&nmp->nm_token);
778 * Handle size changes
780 if (vap->va_size != VNOVAL) {
782 * truncation requested
784 switch (vp->v_type) {
786 lwkt_reltoken(&nmp->nm_token);
792 if (vap->va_mtime.tv_sec == VNOVAL &&
793 vap->va_atime.tv_sec == VNOVAL &&
794 vap->va_mode == (mode_t)VNOVAL &&
795 vap->va_uid == (uid_t)VNOVAL &&
796 vap->va_gid == (gid_t)VNOVAL) {
797 lwkt_reltoken(&nmp->nm_token);
800 vap->va_size = VNOVAL;
804 * Disallow write attempts if the filesystem is
807 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
808 lwkt_reltoken(&nmp->nm_token);
814 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
817 if (np->n_flag & NLMODIFIED) {
818 if (vap->va_size == 0)
819 error = nfs_vinvalbuf(vp, 0, 1);
821 error = nfs_vinvalbuf(vp, V_SAVE, 1);
825 * note: this loop case almost always happens at
826 * least once per truncation.
828 if (error == 0 && np->n_size != vap->va_size)
830 np->n_vattr.va_size = vap->va_size;
831 kflags |= NOTE_WRITE;
832 if (tsize < vap->va_size)
833 kflags |= NOTE_EXTEND;
839 * If setting the mtime or if server/other-client modifications have
840 * been detected, we must fully flush any pending writes.
842 * This will slow down cp/cpdup/rdist/rsync and other operations which
843 * might call [l]utimes() to set the mtime after writing to a file,
844 * but honestly there is no way to properly defer the write flush
845 * and still get reasonably accurate/dependable synchronization of
848 if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
849 if ((np->n_flag & NRMODIFIED) ||
850 (vap->va_mtime.tv_sec != VNOVAL)) {
851 error = nfs_vinvalbuf(vp, V_SAVE, 1);
852 if (error == EINTR) {
853 lwkt_reltoken(&nmp->nm_token);
860 * Get the blasted mtime to report properly.
862 if (vap->va_mtime.tv_sec != VNOVAL) {
863 np->n_mtime = vap->va_mtime.tv_sec;
865 np->n_vattr.va_mtime = vap->va_mtime;
869 * Issue the setattr rpc, adjust our mtime and make sure NUPD
870 * has been cleared so it does not get overridden.
872 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
874 kflags |= NOTE_EXTEND;
877 * Sanity check if a truncation was issued. This should only occur
878 * if multiple processes are racing on the same file.
880 if (error == 0 && vap->va_size != VNOVAL &&
881 np->n_size != vap->va_size) {
882 kprintf("NFS ftruncate: server disagrees on the file size: "
885 (intmax_t)vap->va_size,
886 (intmax_t)np->n_size);
889 if (error && vap->va_size != VNOVAL) {
890 np->n_size = np->n_vattr.va_size = tsize;
891 nfs_meta_setsize(vp, td, np->n_size, 0);
893 lwkt_reltoken(&nmp->nm_token);
894 nfs_knote(vp, kflags);
900 * Do an nfs setattr rpc.
903 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
904 struct ucred *cred, struct thread *td)
906 struct nfsv2_sattr *sp;
907 struct nfsnode *np = VTONFS(vp);
909 int error = 0, wccflag = NFSV3_WCCRATTR;
910 struct nfsm_info info;
913 info.v3 = NFS_ISV3(vp);
915 nfsstats.rpccnt[NFSPROC_SETATTR]++;
916 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
917 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
918 ERROROUT(nfsm_fhtom(&info, vp));
920 nfsm_v3attrbuild(&info, vap, TRUE);
921 tl = nfsm_build(&info, NFSX_UNSIGNED);
924 sp = nfsm_build(&info, NFSX_V2SATTR);
925 if (vap->va_mode == (mode_t)VNOVAL)
926 sp->sa_mode = nfs_xdrneg1;
928 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
929 if (vap->va_uid == (uid_t)VNOVAL)
930 sp->sa_uid = nfs_xdrneg1;
932 sp->sa_uid = txdr_unsigned(vap->va_uid);
933 if (vap->va_gid == (gid_t)VNOVAL)
934 sp->sa_gid = nfs_xdrneg1;
936 sp->sa_gid = txdr_unsigned(vap->va_gid);
937 sp->sa_size = txdr_unsigned(vap->va_size);
938 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
939 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
941 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
944 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
946 ERROROUT(nfsm_loadattr(&info, vp, NULL));
956 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
962 cache_setvp(nch, vp);
963 cache_settimeout(nch, nctimeout);
967 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
968 * nfs_lookup() until all remaining new api calls are implemented.
970 * Resolve a namecache entry. This function is passed a locked ncp and
971 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
974 nfs_nresolve(struct vop_nresolve_args *ap)
976 struct thread *td = curthread;
977 struct namecache *ncp;
978 struct nfsmount *nmp;
988 struct nfsm_info info;
991 nmp = VFSTONFS(dvp->v_mount);
993 lwkt_gettoken(&nmp->nm_token);
995 if ((error = vget(dvp, LK_SHARED)) != 0) {
996 lwkt_reltoken(&nmp->nm_token);
1001 info.v3 = NFS_ISV3(dvp);
1004 nfsstats.lookupcache_misses++;
1005 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1006 ncp = ap->a_nch->ncp;
1008 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1009 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1010 ERROROUT(nfsm_fhtom(&info, dvp));
1011 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
1012 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
1013 ap->a_cred, &error));
1016 * Cache negatve lookups to reduce NFS traffic, but use
1017 * a fast timeout. Otherwise use a timeout of 1 tick.
1018 * XXX we should add a namecache flag for no-caching
1019 * to uncache the negative hit as soon as possible, but
1020 * we cannot simply destroy the entry because it is used
1021 * as a placeholder by the caller.
1023 * The refactored nfs code will overwrite a non-zero error
1024 * with 0 when we use ERROROUT(), so don't here.
1026 if (error == ENOENT)
1027 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
1028 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1029 NFS_LATTR_NOSHRINK);
1040 * Success, get the file handle, do various checks, and load
1041 * post-operation data from the reply packet. Theoretically
1042 * we should never be looking up "." so, theoretically, we
1043 * should never get the same file handle as our directory. But
1044 * we check anyway. XXX
1046 * Note that no timeout is set for the positive cache hit. We
1047 * assume, theoretically, that ESTALE returns will be dealt with
1048 * properly to handle NFS races and in anycase we cannot depend
1049 * on a timeout to deal with NFS open/create/excl issues so instead
1050 * of a bad hack here the rest of the NFS client code needs to do
1053 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1056 if (NFS_CMPFH(np, fhp, fhsize)) {
1060 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1065 lwkt_reltoken(&nmp->nm_token);
1071 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1072 NFS_LATTR_NOSHRINK));
1073 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1074 NFS_LATTR_NOSHRINK));
1076 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1078 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1082 lwkt_reltoken(&nmp->nm_token);
1094 * 'cached' nfs directory lookup
1096 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1098 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1099 * struct componentname *a_cnp)
1102 nfs_lookup(struct vop_old_lookup_args *ap)
1104 struct componentname *cnp = ap->a_cnp;
1105 struct vnode *dvp = ap->a_dvp;
1106 struct vnode **vpp = ap->a_vpp;
1107 int flags = cnp->cn_flags;
1108 struct vnode *newvp;
1109 struct vnode *notvp;
1110 struct nfsmount *nmp;
1114 int lockparent, wantparent, attrflag, fhsize;
1117 struct nfsm_info info;
1120 info.v3 = NFS_ISV3(dvp);
1123 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1126 * Read-only mount check and directory check.
1129 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1130 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1133 if (dvp->v_type != VDIR)
1137 * Look it up in the cache. Note that ENOENT is only returned if we
1138 * previously entered a negative hit (see later on). The additional
1139 * nfsneg_cache_timeout check causes previously cached results to
1140 * be instantly ignored if the negative caching is turned off.
1142 lockparent = flags & CNP_LOCKPARENT;
1143 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1144 nmp = VFSTONFS(dvp->v_mount);
1147 lwkt_gettoken(&nmp->nm_token);
1154 nfsstats.lookupcache_misses++;
1155 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1156 len = cnp->cn_namelen;
1157 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1158 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1159 ERROROUT(nfsm_fhtom(&info, dvp));
1160 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1161 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1162 cnp->cn_cred, &error));
1164 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1165 NFS_LATTR_NOSHRINK);
1175 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1178 * Handle RENAME case...
1180 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1181 if (NFS_CMPFH(np, fhp, fhsize)) {
1184 lwkt_reltoken(&nmp->nm_token);
1187 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1191 lwkt_reltoken(&nmp->nm_token);
1196 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1197 NFS_LATTR_NOSHRINK));
1198 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1199 NFS_LATTR_NOSHRINK));
1201 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1208 cnp->cn_flags |= CNP_PDIRUNLOCK;
1210 lwkt_reltoken(&nmp->nm_token);
1214 if (flags & CNP_ISDOTDOT) {
1216 cnp->cn_flags |= CNP_PDIRUNLOCK;
1217 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1219 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1220 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1221 lwkt_reltoken(&nmp->nm_token);
1222 return (error); /* NOTE: return error from nget */
1226 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1229 lwkt_reltoken(&nmp->nm_token);
1232 cnp->cn_flags |= CNP_PDIRUNLOCK;
1234 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1238 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1242 lwkt_reltoken(&nmp->nm_token);
1247 cnp->cn_flags |= CNP_PDIRUNLOCK;
1252 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1253 NFS_LATTR_NOSHRINK));
1254 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1255 NFS_LATTR_NOSHRINK));
1257 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1260 /* XXX MOVE TO nfs_nremove() */
1261 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1262 cnp->cn_nameiop != NAMEI_DELETE) {
1263 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1271 if (newvp != NULLVP) {
1275 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1276 cnp->cn_nameiop == NAMEI_RENAME) &&
1280 cnp->cn_flags |= CNP_PDIRUNLOCK;
1282 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1285 error = EJUSTRETURN;
1288 lwkt_reltoken(&nmp->nm_token);
1294 * Just call nfs_bioread() to do the work.
1296 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1297 * struct ucred *a_cred)
1300 nfs_read(struct vop_read_args *ap)
1302 struct vnode *vp = ap->a_vp;
1303 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1306 lwkt_gettoken(&nmp->nm_token);
1307 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1308 lwkt_reltoken(&nmp->nm_token);
1316 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1319 nfs_readlink(struct vop_readlink_args *ap)
1321 struct vnode *vp = ap->a_vp;
1322 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1325 if (vp->v_type != VLNK)
1328 lwkt_gettoken(&nmp->nm_token);
1329 error = nfs_bioread(vp, ap->a_uio, 0);
1330 lwkt_reltoken(&nmp->nm_token);
1336 * Do a readlink rpc.
1337 * Called by nfs_doio() from below the buffer cache.
1340 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1342 int error = 0, len, attrflag;
1343 struct nfsm_info info;
1346 info.v3 = NFS_ISV3(vp);
1348 nfsstats.rpccnt[NFSPROC_READLINK]++;
1349 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1350 ERROROUT(nfsm_fhtom(&info, vp));
1351 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1352 nfs_vpcred(vp, ND_CHECK), &error));
1354 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1355 NFS_LATTR_NOSHRINK));
1358 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1359 if (len == NFS_MAXPATHLEN) {
1360 struct nfsnode *np = VTONFS(vp);
1361 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1364 ERROROUT(nfsm_mtouio(&info, uiop, len));
1373 * nfs synchronous read rpc using UIO
1376 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1379 struct nfsmount *nmp;
1380 int error = 0, len, retlen, tsiz, eof, attrflag;
1381 struct nfsm_info info;
1385 info.v3 = NFS_ISV3(vp);
1390 nmp = VFSTONFS(vp->v_mount);
1392 tsiz = uiop->uio_resid;
1393 tmp_off = uiop->uio_offset + tsiz;
1394 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1396 tmp_off = uiop->uio_offset;
1398 nfsstats.rpccnt[NFSPROC_READ]++;
1399 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1400 nfsm_reqhead(&info, vp, NFSPROC_READ,
1401 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1402 ERROROUT(nfsm_fhtom(&info, vp));
1403 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1405 txdr_hyper(uiop->uio_offset, tl);
1406 *(tl + 2) = txdr_unsigned(len);
1408 *tl++ = txdr_unsigned(uiop->uio_offset);
1409 *tl++ = txdr_unsigned(len);
1412 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1413 nfs_vpcred(vp, ND_READ), &error));
1415 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1416 NFS_LATTR_NOSHRINK));
1417 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1418 eof = fxdr_unsigned(int, *(tl + 1));
1420 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1422 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1423 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1428 * Handle short-read from server (NFSv3). If EOF is not
1429 * flagged (and no error occurred), but retlen is less
1430 * then the request size, we must zero-fill the remainder.
1432 if (retlen < len && info.v3 && eof == 0) {
1433 ERROROUT(uiomovez(len - retlen, uiop));
1439 * Terminate loop on EOF or zero-length read.
1441 * For NFSv2 a short-read indicates EOF, not zero-fill,
1442 * and also terminates the loop.
1445 if (eof || retlen == 0)
1447 } else if (retlen < len) {
1459 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1460 int *iomode, int *must_commit)
1464 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1465 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1466 int committed = NFSV3WRITE_FILESYNC;
1467 struct nfsm_info info;
1470 info.v3 = NFS_ISV3(vp);
1473 if (uiop->uio_iovcnt != 1)
1474 panic("nfs: writerpc iovcnt > 1");
1477 tsiz = uiop->uio_resid;
1478 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1481 nfsstats.rpccnt[NFSPROC_WRITE]++;
1482 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1483 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1484 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1486 ERROROUT(nfsm_fhtom(&info, vp));
1488 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1489 txdr_hyper(uiop->uio_offset, tl);
1491 *tl++ = txdr_unsigned(len);
1492 *tl++ = txdr_unsigned(*iomode);
1493 *tl = txdr_unsigned(len);
1497 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1498 /* Set both "begin" and "current" to non-garbage. */
1499 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1500 *tl++ = x; /* "begin offset" */
1501 *tl++ = x; /* "current offset" */
1502 x = txdr_unsigned(len);
1503 *tl++ = x; /* total to this offset */
1504 *tl = x; /* size of this write */
1506 ERROROUT(nfsm_uiotom(&info, uiop, len));
1507 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1508 nfs_vpcred(vp, ND_WRITE), &error));
1511 * The write RPC returns a before and after mtime. The
1512 * nfsm_wcc_data() macro checks the before n_mtime
1513 * against the before time and stores the after time
1514 * in the nfsnode's cached vattr and n_mtime field.
1515 * The NRMODIFIED bit will be set if the before
1516 * time did not match the original mtime.
1518 wccflag = NFSV3_WCCCHK;
1519 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1521 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1522 rlen = fxdr_unsigned(int, *tl++);
1528 } else if (rlen < len) {
1529 backup = len - rlen;
1530 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1531 uiop->uio_iov->iov_len += backup;
1532 uiop->uio_offset -= backup;
1533 uiop->uio_resid += backup;
1536 commit = fxdr_unsigned(int, *tl++);
1539 * Return the lowest committment level
1540 * obtained by any of the RPCs.
1542 if (committed == NFSV3WRITE_FILESYNC)
1544 else if (committed == NFSV3WRITE_DATASYNC &&
1545 commit == NFSV3WRITE_UNSTABLE)
1547 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1548 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1550 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1551 } else if (bcmp((caddr_t)tl,
1552 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1554 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1559 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1568 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1569 committed = NFSV3WRITE_FILESYNC;
1570 *iomode = committed;
1572 uiop->uio_resid = tsiz;
1578 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1579 * mode set to specify the file type and the size field for rdev.
1582 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1585 struct nfsv2_sattr *sp;
1587 struct vnode *newvp = NULL;
1588 struct nfsnode *np = NULL;
1590 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1592 struct nfsm_info info;
1595 info.v3 = NFS_ISV3(dvp);
1597 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1598 rmajor = txdr_unsigned(vap->va_rmajor);
1599 rminor = txdr_unsigned(vap->va_rminor);
1600 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1601 rmajor = nfs_xdrneg1;
1602 rminor = nfs_xdrneg1;
1604 return (EOPNOTSUPP);
1606 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1609 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1610 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1611 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1612 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1613 ERROROUT(nfsm_fhtom(&info, dvp));
1614 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1617 tl = nfsm_build(&info, NFSX_UNSIGNED);
1618 *tl++ = vtonfsv3_type(vap->va_type);
1619 nfsm_v3attrbuild(&info, vap, FALSE);
1620 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1621 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1622 *tl++ = txdr_unsigned(vap->va_rmajor);
1623 *tl = txdr_unsigned(vap->va_rminor);
1626 sp = nfsm_build(&info, NFSX_V2SATTR);
1627 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1628 sp->sa_uid = nfs_xdrneg1;
1629 sp->sa_gid = nfs_xdrneg1;
1630 sp->sa_size = makeudev(rmajor, rminor);
1631 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1632 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1634 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1635 cnp->cn_cred, &error));
1637 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1643 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1644 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1650 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1661 VTONFS(dvp)->n_flag |= NLMODIFIED;
1663 VTONFS(dvp)->n_attrstamp = 0;
1669 * just call nfs_mknodrpc() to do the work.
1671 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1672 * struct componentname *a_cnp, struct vattr *a_vap)
1676 nfs_mknod(struct vop_old_mknod_args *ap)
1678 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1681 lwkt_gettoken(&nmp->nm_token);
1682 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1683 lwkt_reltoken(&nmp->nm_token);
1685 nfs_knote(ap->a_dvp, NOTE_WRITE);
1690 static u_long create_verf;
1692 * nfs file create call
1694 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1695 * struct componentname *a_cnp, struct vattr *a_vap)
1698 nfs_create(struct vop_old_create_args *ap)
1700 struct vnode *dvp = ap->a_dvp;
1701 struct vattr *vap = ap->a_vap;
1702 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1703 struct componentname *cnp = ap->a_cnp;
1704 struct nfsv2_sattr *sp;
1706 struct nfsnode *np = NULL;
1707 struct vnode *newvp = NULL;
1708 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1710 struct nfsm_info info;
1713 info.v3 = NFS_ISV3(dvp);
1714 lwkt_gettoken(&nmp->nm_token);
1717 * Oops, not for me..
1719 if (vap->va_type == VSOCK) {
1720 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1721 lwkt_reltoken(&nmp->nm_token);
1725 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1726 lwkt_reltoken(&nmp->nm_token);
1729 if (vap->va_vaflags & VA_EXCLUSIVE)
1732 nfsstats.rpccnt[NFSPROC_CREATE]++;
1733 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1734 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1735 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1736 ERROROUT(nfsm_fhtom(&info, dvp));
1737 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1740 tl = nfsm_build(&info, NFSX_UNSIGNED);
1741 if (fmode & O_EXCL) {
1742 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1743 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1745 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1746 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1749 *tl++ = create_verf;
1750 *tl = ++create_verf;
1752 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1753 nfsm_v3attrbuild(&info, vap, FALSE);
1756 sp = nfsm_build(&info, NFSX_V2SATTR);
1757 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1758 sp->sa_uid = nfs_xdrneg1;
1759 sp->sa_gid = nfs_xdrneg1;
1761 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1762 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1764 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1765 cnp->cn_cred, &error));
1767 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1773 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1774 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1781 error = nfsm_wcc_data(&info, dvp, &wccflag);
1783 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1789 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1790 KKASSERT(newvp == NULL);
1794 } else if (info.v3 && (fmode & O_EXCL)) {
1796 * We are normally called with only a partially initialized
1797 * VAP. Since the NFSv3 spec says that server may use the
1798 * file attributes to store the verifier, the spec requires
1799 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1800 * in atime, but we can't really assume that all servers will
1801 * so we ensure that our SETATTR sets both atime and mtime.
1803 if (vap->va_mtime.tv_sec == VNOVAL)
1804 vfs_timestamp(&vap->va_mtime);
1805 if (vap->va_atime.tv_sec == VNOVAL)
1806 vap->va_atime = vap->va_mtime;
1807 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1811 * The new np may have enough info for access
1812 * checks, make sure rucred and wucred are
1813 * initialized for read and write rpc's.
1816 if (np->n_rucred == NULL)
1817 np->n_rucred = nfs_crhold(cnp->cn_cred);
1818 if (np->n_wucred == NULL)
1819 np->n_wucred = nfs_crhold(cnp->cn_cred);
1821 nfs_knote(dvp, NOTE_WRITE);
1825 VTONFS(dvp)->n_flag |= NLMODIFIED;
1827 VTONFS(dvp)->n_attrstamp = 0;
1828 lwkt_reltoken(&nmp->nm_token);
1833 * nfs file remove call
1834 * To try and make nfs semantics closer to ufs semantics, a file that has
1835 * other processes using the vnode is renamed instead of removed and then
1836 * removed later on the last close.
1838 * If a rename is not already in the works
1839 * call nfs_sillyrename() to set it up
1843 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1844 * struct componentname *a_cnp)
1847 nfs_remove(struct vop_old_remove_args *ap)
1849 struct vnode *vp = ap->a_vp;
1850 struct vnode *dvp = ap->a_dvp;
1851 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1852 struct componentname *cnp = ap->a_cnp;
1853 struct nfsnode *np = VTONFS(vp);
1857 lwkt_gettoken(&nmp->nm_token);
1859 if (VREFCNT(vp) < 1)
1860 panic("nfs_remove: bad v_refcnt");
1862 if (vp->v_type == VDIR) {
1864 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1865 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1867 * Force finalization so the VOP_INACTIVE() call is not delayed.
1868 * This prevents cred structures from building up in nfsnodes
1869 * for deleted files.
1871 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1872 np->n_flag |= NREMOVED;
1875 * Throw away biocache buffers, mainly to avoid
1876 * unnecessary delayed writes later.
1878 error = nfs_vinvalbuf(vp, 0, 1);
1880 if (error != EINTR) {
1881 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1883 cnp->cn_cred, cnp->cn_td);
1887 * Kludge City: If the first reply to the remove rpc is lost..
1888 * the reply to the retransmitted request will be ENOENT
1889 * since the file was in fact removed
1890 * Therefore, we cheat and return success.
1892 if (error == ENOENT)
1894 } else if (!np->n_sillyrename) {
1895 error = nfs_sillyrename(dvp, vp, cnp);
1897 np->n_attrstamp = 0;
1898 lwkt_reltoken(&nmp->nm_token);
1900 nfs_knote(vp, NOTE_DELETE);
1901 nfs_knote(dvp, NOTE_WRITE);
1908 * nfs file remove rpc called from nfs_inactive
1910 * NOTE: s_dvp can be VBAD during a forced unmount.
1913 nfs_removeit(struct sillyrename *sp)
1915 if (sp->s_dvp->v_type == VBAD)
1917 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1922 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1925 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1926 struct ucred *cred, struct thread *td)
1928 int error = 0, wccflag = NFSV3_WCCRATTR;
1929 struct nfsm_info info;
1932 info.v3 = NFS_ISV3(dvp);
1934 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1935 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1936 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1937 ERROROUT(nfsm_fhtom(&info, dvp));
1938 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1939 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1941 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1946 VTONFS(dvp)->n_flag |= NLMODIFIED;
1948 VTONFS(dvp)->n_attrstamp = 0;
1953 * nfs file rename call
1955 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1956 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1957 * struct vnode *a_tvp, struct componentname *a_tcnp)
1960 nfs_rename(struct vop_old_rename_args *ap)
1962 struct vnode *fvp = ap->a_fvp;
1963 struct vnode *tvp = ap->a_tvp;
1964 struct vnode *fdvp = ap->a_fdvp;
1965 struct vnode *tdvp = ap->a_tdvp;
1966 struct componentname *tcnp = ap->a_tcnp;
1967 struct componentname *fcnp = ap->a_fcnp;
1968 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1971 lwkt_gettoken(&nmp->nm_token);
1974 * Force finalization so the VOP_INACTIVE() call is not delayed.
1975 * This prevents cred structures from building up in nfsnodes
1976 * for deleted files.
1979 atomic_set_int(&tvp->v_refcnt, VREF_FINALIZE);
1981 VTONFS(tvp)->n_flag |= NREMOVED;
1984 /* Check for cross-device rename */
1985 if ((fvp->v_mount != tdvp->v_mount) ||
1986 (tvp && (fvp->v_mount != tvp->v_mount))) {
1992 * We shouldn't have to flush fvp on rename for most server-side
1993 * filesystems as the file handle should not change. Unfortunately
1994 * the inode for some filesystems (msdosfs) might be tied to the
1995 * file name or directory position so to be completely safe
1996 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1999 * We must flush tvp on rename because it might become stale on the
2000 * server after the rename.
2002 if (nfs_flush_on_rename)
2003 VOP_FSYNC(fvp, MNT_WAIT, 0);
2005 VOP_FSYNC(tvp, MNT_WAIT, 0);
2008 * If the tvp exists and is in use, sillyrename it before doing the
2009 * rename of the new file over it.
2011 * XXX Can't sillyrename a directory.
2013 * We do not attempt to do any namecache purges in this old API
2014 * routine. The new API compat functions have access to the actual
2015 * namecache structures and will do it for us.
2017 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2018 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2019 nfs_knote(tvp, NOTE_DELETE);
2023 nfs_knote(tvp, NOTE_DELETE);
2026 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2027 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2032 nfs_knote(fdvp, NOTE_WRITE);
2033 nfs_knote(tdvp, NOTE_WRITE);
2034 nfs_knote(fvp, NOTE_RENAME);
2036 lwkt_reltoken(&nmp->nm_token);
2046 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2048 if (error == ENOENT)
2054 * nfs file rename rpc called from nfs_remove() above
2057 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2058 struct sillyrename *sp)
2060 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2061 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
2065 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2068 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2069 struct vnode *tdvp, const char *tnameptr, int tnamelen,
2070 struct ucred *cred, struct thread *td)
2072 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2073 struct nfsm_info info;
2076 info.v3 = NFS_ISV3(fdvp);
2078 nfsstats.rpccnt[NFSPROC_RENAME]++;
2079 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2080 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2081 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2082 ERROROUT(nfsm_fhtom(&info, fdvp));
2083 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2084 ERROROUT(nfsm_fhtom(&info, tdvp));
2085 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2086 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2088 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2089 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2094 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2095 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2097 VTONFS(fdvp)->n_attrstamp = 0;
2099 VTONFS(tdvp)->n_attrstamp = 0;
2104 * nfs hard link create call
2106 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2107 * struct componentname *a_cnp)
2110 nfs_link(struct vop_old_link_args *ap)
2112 struct vnode *vp = ap->a_vp;
2113 struct vnode *tdvp = ap->a_tdvp;
2114 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2115 struct componentname *cnp = ap->a_cnp;
2116 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2117 struct nfsm_info info;
2119 if (vp->v_mount != tdvp->v_mount) {
2122 lwkt_gettoken(&nmp->nm_token);
2125 * The attribute cache may get out of sync with the server on link.
2126 * Pushing writes to the server before handle was inherited from
2127 * long long ago and it is unclear if we still need to do this.
2130 if (nfs_flush_on_hlink)
2131 VOP_FSYNC(vp, MNT_WAIT, 0);
2134 info.v3 = NFS_ISV3(vp);
2136 nfsstats.rpccnt[NFSPROC_LINK]++;
2137 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2138 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2139 nfsm_rndup(cnp->cn_namelen));
2140 ERROROUT(nfsm_fhtom(&info, vp));
2141 ERROROUT(nfsm_fhtom(&info, tdvp));
2142 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2144 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2145 cnp->cn_cred, &error));
2147 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2148 NFS_LATTR_NOSHRINK));
2149 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2154 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2156 VTONFS(vp)->n_attrstamp = 0;
2158 VTONFS(tdvp)->n_attrstamp = 0;
2160 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2162 if (error == EEXIST)
2164 lwkt_reltoken(&nmp->nm_token);
2166 nfs_knote(vp, NOTE_LINK);
2167 nfs_knote(tdvp, NOTE_WRITE);
2174 * nfs symbolic link create call
2176 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2177 * struct componentname *a_cnp, struct vattr *a_vap,
2181 nfs_symlink(struct vop_old_symlink_args *ap)
2183 struct vnode *dvp = ap->a_dvp;
2184 struct vattr *vap = ap->a_vap;
2185 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2186 struct componentname *cnp = ap->a_cnp;
2187 struct nfsv2_sattr *sp;
2188 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2189 struct vnode *newvp = NULL;
2190 struct nfsm_info info;
2193 info.v3 = NFS_ISV3(dvp);
2194 lwkt_gettoken(&nmp->nm_token);
2196 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2197 slen = strlen(ap->a_target);
2198 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2199 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2200 nfsm_rndup(cnp->cn_namelen) +
2201 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2202 ERROROUT(nfsm_fhtom(&info, dvp));
2203 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2206 nfsm_v3attrbuild(&info, vap, FALSE);
2208 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2210 sp = nfsm_build(&info, NFSX_V2SATTR);
2211 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2212 sp->sa_uid = nfs_xdrneg1;
2213 sp->sa_gid = nfs_xdrneg1;
2214 sp->sa_size = nfs_xdrneg1;
2215 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2216 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2220 * Issue the NFS request and get the rpc response.
2222 * Only NFSv3 responses returning an error of 0 actually return
2223 * a file handle that can be converted into newvp without having
2224 * to do an extra lookup rpc.
2226 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2227 cnp->cn_cred, &error));
2230 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2232 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2236 * out code jumps -> here, mrep is also freed.
2244 * If we get an EEXIST error, silently convert it to no-error
2245 * in case of an NFS retry.
2247 if (error == EEXIST)
2251 * If we do not have (or no longer have) an error, and we could
2252 * not extract the newvp from the response due to the request being
2253 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2254 * to obtain a newvp to return.
2256 if (error == 0 && newvp == NULL) {
2257 struct nfsnode *np = NULL;
2259 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2260 cnp->cn_cred, cnp->cn_td, &np);
2270 VTONFS(dvp)->n_flag |= NLMODIFIED;
2272 VTONFS(dvp)->n_attrstamp = 0;
2273 if (error == 0 && *ap->a_vpp)
2274 nfs_knote(*ap->a_vpp, NOTE_WRITE);
2275 lwkt_reltoken(&nmp->nm_token);
2283 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2284 * struct componentname *a_cnp, struct vattr *a_vap)
2287 nfs_mkdir(struct vop_old_mkdir_args *ap)
2289 struct vnode *dvp = ap->a_dvp;
2290 struct vattr *vap = ap->a_vap;
2291 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2292 struct componentname *cnp = ap->a_cnp;
2293 struct nfsv2_sattr *sp;
2294 struct nfsnode *np = NULL;
2295 struct vnode *newvp = NULL;
2297 int error = 0, wccflag = NFSV3_WCCRATTR;
2300 struct nfsm_info info;
2303 info.v3 = NFS_ISV3(dvp);
2304 lwkt_gettoken(&nmp->nm_token);
2306 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2307 lwkt_reltoken(&nmp->nm_token);
2310 len = cnp->cn_namelen;
2311 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2312 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2313 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2314 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2315 ERROROUT(nfsm_fhtom(&info, dvp));
2316 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2318 nfsm_v3attrbuild(&info, vap, FALSE);
2320 sp = nfsm_build(&info, NFSX_V2SATTR);
2321 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2322 sp->sa_uid = nfs_xdrneg1;
2323 sp->sa_gid = nfs_xdrneg1;
2324 sp->sa_size = nfs_xdrneg1;
2325 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2326 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2328 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2329 cnp->cn_cred, &error));
2331 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2334 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2339 VTONFS(dvp)->n_flag |= NLMODIFIED;
2341 VTONFS(dvp)->n_attrstamp = 0;
2343 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2344 * if we can succeed in looking up the directory.
2346 if (error == EEXIST || (!error && !gotvp)) {
2351 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2355 if (newvp->v_type != VDIR)
2363 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2366 lwkt_reltoken(&nmp->nm_token);
2371 * nfs remove directory call
2373 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2374 * struct componentname *a_cnp)
2377 nfs_rmdir(struct vop_old_rmdir_args *ap)
2379 struct vnode *vp = ap->a_vp;
2380 struct vnode *dvp = ap->a_dvp;
2381 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2382 struct componentname *cnp = ap->a_cnp;
2383 int error = 0, wccflag = NFSV3_WCCRATTR;
2384 struct nfsm_info info;
2387 info.v3 = NFS_ISV3(dvp);
2392 lwkt_gettoken(&nmp->nm_token);
2394 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2395 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2396 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2397 nfsm_rndup(cnp->cn_namelen));
2398 ERROROUT(nfsm_fhtom(&info, dvp));
2399 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2401 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2402 cnp->cn_cred, &error));
2404 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2409 VTONFS(dvp)->n_flag |= NLMODIFIED;
2411 VTONFS(dvp)->n_attrstamp = 0;
2413 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2415 if (error == ENOENT)
2418 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2419 lwkt_reltoken(&nmp->nm_token);
2427 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2430 nfs_readdir(struct vop_readdir_args *ap)
2432 struct vnode *vp = ap->a_vp;
2433 struct nfsnode *np = VTONFS(vp);
2434 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2435 struct uio *uio = ap->a_uio;
2439 if (vp->v_type != VDIR)
2442 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2446 lwkt_gettoken(&nmp->nm_token);
2449 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2450 * and then check that is still valid, or if this is an NQNFS mount
2451 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2452 * VOP_GETATTR() does not necessarily go to the wire.
2454 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2455 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2456 if (VOP_GETATTR(vp, &vattr) == 0 &&
2457 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2459 nfsstats.direofcache_hits++;
2465 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2466 * own cache coherency checks so we do not have to.
2468 tresid = uio->uio_resid;
2469 error = nfs_bioread(vp, uio, 0);
2471 if (!error && uio->uio_resid == tresid)
2472 nfsstats.direofcache_misses++;
2474 lwkt_reltoken(&nmp->nm_token);
2481 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2483 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2484 * offset/block and converts the nfs formatted directory entries for userland
2485 * consumption as well as deals with offsets into the middle of blocks.
2486 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2487 * be block-bounded. It must convert to cookies for the actual RPC.
2490 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2493 struct nfs_dirent *dp = NULL;
2498 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2499 struct nfsnode *dnp = VTONFS(vp);
2501 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2503 struct nfsm_info info;
2506 info.v3 = NFS_ISV3(vp);
2509 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2510 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2511 panic("nfs readdirrpc bad uio");
2515 * If there is no cookie, assume directory was stale.
2517 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2521 return (NFSERR_BAD_COOKIE);
2523 * Loop around doing readdir rpc's of size nm_readdirsize
2524 * truncated to a multiple of DIRBLKSIZ.
2525 * The stopping criteria is EOF or buffer full.
2527 while (more_dirs && bigenough) {
2528 nfsstats.rpccnt[NFSPROC_READDIR]++;
2529 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2530 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2531 ERROROUT(nfsm_fhtom(&info, vp));
2533 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2534 *tl++ = cookie.nfsuquad[0];
2535 *tl++ = cookie.nfsuquad[1];
2536 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2537 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2540 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2541 * WITH NFSv2!!! There's nothing I can really do
2542 * about it other than to hope the server supports
2545 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2546 *tl++ = cookie.nfsuquad[0];
2548 *tl = txdr_unsigned(nmp->nm_readdirsize);
2549 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2551 nfs_vpcred(vp, ND_READ), &error));
2553 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2554 NFS_LATTR_NOSHRINK));
2555 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2556 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2557 dnp->n_cookieverf.nfsuquad[1] = *tl;
2559 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2560 more_dirs = fxdr_unsigned(int, *tl);
2562 /* loop thru the dir entries, converting them to std form */
2563 while (more_dirs && bigenough) {
2565 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2566 fileno = fxdr_hyper(tl);
2567 len = fxdr_unsigned(int, *(tl + 2));
2569 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2570 fileno = fxdr_unsigned(u_quad_t, *tl++);
2571 len = fxdr_unsigned(int, *tl);
2573 if (len <= 0 || len > NFS_MAXNAMLEN) {
2581 * len is the number of bytes in the path element
2582 * name, not including the \0 termination.
2584 * tlen is the number of bytes w have to reserve for
2585 * the path element name.
2587 tlen = nfsm_rndup(len);
2589 tlen += 4; /* To ensure null termination */
2592 * If the entry would cross a DIRBLKSIZ boundary,
2593 * extend the previous nfs_dirent to cover the
2596 left = DIRBLKSIZ - blksiz;
2597 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2598 dp->nfs_reclen += left;
2599 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2600 uiop->uio_iov->iov_len -= left;
2601 uiop->uio_offset += left;
2602 uiop->uio_resid -= left;
2605 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2608 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2609 dp->nfs_ino = fileno;
2610 dp->nfs_namlen = len;
2611 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2612 dp->nfs_type = DT_UNKNOWN;
2613 blksiz += dp->nfs_reclen;
2614 if (blksiz == DIRBLKSIZ)
2616 uiop->uio_offset += sizeof(struct nfs_dirent);
2617 uiop->uio_resid -= sizeof(struct nfs_dirent);
2618 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2619 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2620 ERROROUT(nfsm_mtouio(&info, uiop, len));
2623 * The uiop has advanced by nfs_dirent + len
2624 * but really needs to advance by
2627 cp = uiop->uio_iov->iov_base;
2629 *cp = '\0'; /* null terminate */
2630 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2631 uiop->uio_iov->iov_len -= tlen;
2632 uiop->uio_offset += tlen;
2633 uiop->uio_resid -= tlen;
2636 * NFS strings must be rounded up (nfsm_myouio
2637 * handled that in the bigenough case).
2639 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2642 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2644 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2648 * If we were able to accomodate the last entry,
2649 * get the cookie for the next one. Otherwise
2650 * hold-over the cookie for the one we were not
2651 * able to accomodate.
2654 cookie.nfsuquad[0] = *tl++;
2656 cookie.nfsuquad[1] = *tl++;
2657 } else if (info.v3) {
2662 more_dirs = fxdr_unsigned(int, *tl);
2665 * If at end of rpc data, get the eof boolean
2668 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2669 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2675 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2676 * by increasing d_reclen for the last record.
2679 left = DIRBLKSIZ - blksiz;
2680 dp->nfs_reclen += left;
2681 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2682 uiop->uio_iov->iov_len -= left;
2683 uiop->uio_offset += left;
2684 uiop->uio_resid -= left;
2689 * We hit the end of the directory, update direofoffset.
2691 dnp->n_direofoffset = uiop->uio_offset;
2694 * There is more to go, insert the link cookie so the
2695 * next block can be read.
2697 if (uiop->uio_resid > 0)
2698 kprintf("EEK! readdirrpc resid > 0\n");
2699 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2707 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2710 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2713 struct nfs_dirent *dp;
2715 struct vnode *newvp;
2717 caddr_t dpossav1, dpossav2;
2719 struct mbuf *mdsav1, *mdsav2;
2721 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2722 struct nfsnode *dnp = VTONFS(vp), *np;
2725 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2726 int attrflag, fhsize;
2727 struct nchandle nch;
2728 struct nchandle dnch;
2729 struct nlcomponent nlc;
2730 struct nfsm_info info;
2739 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2740 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2741 panic("nfs readdirplusrpc bad uio");
2744 * Obtain the namecache record for the directory so we have something
2745 * to use as a basis for creating the entries. This function will
2746 * return a held (but not locked) ncp. The ncp may be disconnected
2747 * from the tree and cannot be used for upward traversals, and the
2748 * ncp may be unnamed. Note that other unrelated operations may
2749 * cause the ncp to be named at any time.
2751 * We have to lock the ncp to prevent a lock order reversal when
2752 * rdirplus does nlookups of the children, because the vnode is
2753 * locked and has to stay that way.
2755 cache_fromdvp(vp, NULL, 0, &dnch);
2756 bzero(&nlc, sizeof(nlc));
2760 * If there is no cookie, assume directory was stale.
2762 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2768 return (NFSERR_BAD_COOKIE);
2772 * Loop around doing readdir rpc's of size nm_readdirsize
2773 * truncated to a multiple of DIRBLKSIZ.
2774 * The stopping criteria is EOF or buffer full.
2776 while (more_dirs && bigenough) {
2777 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2778 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2779 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2780 ERROROUT(nfsm_fhtom(&info, vp));
2781 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2782 *tl++ = cookie.nfsuquad[0];
2783 *tl++ = cookie.nfsuquad[1];
2784 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2785 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2786 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2787 *tl = txdr_unsigned(nmp->nm_rsize);
2788 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2790 nfs_vpcred(vp, ND_READ), &error));
2791 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2792 NFS_LATTR_NOSHRINK));
2793 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2794 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2795 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2796 more_dirs = fxdr_unsigned(int, *tl);
2798 /* loop thru the dir entries, doctoring them to 4bsd form */
2799 while (more_dirs && bigenough) {
2800 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2801 fileno = fxdr_hyper(tl);
2802 len = fxdr_unsigned(int, *(tl + 2));
2803 if (len <= 0 || len > NFS_MAXNAMLEN) {
2809 tlen = nfsm_rndup(len);
2811 tlen += 4; /* To ensure null termination*/
2812 left = DIRBLKSIZ - blksiz;
2813 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2814 dp->nfs_reclen += left;
2815 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2816 uiop->uio_iov->iov_len -= left;
2817 uiop->uio_offset += left;
2818 uiop->uio_resid -= left;
2821 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2824 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2825 dp->nfs_ino = fileno;
2826 dp->nfs_namlen = len;
2827 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2828 dp->nfs_type = DT_UNKNOWN;
2829 blksiz += dp->nfs_reclen;
2830 if (blksiz == DIRBLKSIZ)
2832 uiop->uio_offset += sizeof(struct nfs_dirent);
2833 uiop->uio_resid -= sizeof(struct nfs_dirent);
2834 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2835 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2836 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2837 nlc.nlc_namelen = len;
2838 ERROROUT(nfsm_mtouio(&info, uiop, len));
2839 cp = uiop->uio_iov->iov_base;
2842 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2843 uiop->uio_iov->iov_len -= tlen;
2844 uiop->uio_offset += tlen;
2845 uiop->uio_resid -= tlen;
2847 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2849 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2851 cookie.nfsuquad[0] = *tl++;
2852 cookie.nfsuquad[1] = *tl++;
2858 * Since the attributes are before the file handle
2859 * (sigh), we must skip over the attributes and then
2860 * come back and get them.
2862 attrflag = fxdr_unsigned(int, *tl);
2864 dpossav1 = info.dpos;
2866 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2867 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2868 doit = fxdr_unsigned(int, *tl);
2870 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2872 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2873 !NFS_CMPFH(dnp, fhp, fhsize)
2877 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2878 nlc.nlc_namelen, nlc.nlc_namelen,
2882 * This is a bit hokey but there isn't
2883 * much we can do about it. We can't
2884 * hold the directory vp locked while
2885 * doing lookups and gets.
2887 nch = cache_nlookup_nonblock(&dnch, &nlc);
2888 if (nch.ncp == NULL)
2890 cache_setunresolved(&nch);
2891 error = nfs_nget_nonblock(vp->v_mount, fhp,
2899 dpossav2 = info.dpos;
2900 info.dpos = dpossav1;
2903 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2904 info.dpos = dpossav2;
2907 IFTODT(VTTOIF(np->n_vattr.va_type));
2908 nfs_cache_setvp(&nch, newvp,
2909 nfspos_cache_timeout);
2917 kprintf("Warning: NFS/rddirplus, "
2918 "UNABLE TO ENTER %*.*s\n",
2919 nlc.nlc_namelen, nlc.nlc_namelen,
2925 /* Just skip over the file handle */
2926 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2927 i = fxdr_unsigned(int, *tl);
2928 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2930 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2931 more_dirs = fxdr_unsigned(int, *tl);
2934 * If at end of rpc data, get the eof boolean
2937 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2938 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2944 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2945 * by increasing d_reclen for the last record.
2948 left = DIRBLKSIZ - blksiz;
2949 dp->nfs_reclen += left;
2950 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2951 uiop->uio_iov->iov_len -= left;
2952 uiop->uio_offset += left;
2953 uiop->uio_resid -= left;
2957 * We are now either at the end of the directory or have filled the
2961 dnp->n_direofoffset = uiop->uio_offset;
2963 if (uiop->uio_resid > 0)
2964 kprintf("EEK! readdirplusrpc resid > 0\n");
2965 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2969 if (newvp != NULLVP) {
2982 * Silly rename. To make the NFS filesystem that is stateless look a little
2983 * more like the "ufs" a remove of an active vnode is translated to a rename
2984 * to a funny looking filename that is removed by nfs_inactive on the
2985 * nfsnode. There is the potential for another process on a different client
2986 * to create the same funny name between the nfs_lookitup() fails and the
2987 * nfs_rename() completes, but...
2990 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2992 struct sillyrename *sp;
2997 * Force finalization so the VOP_INACTIVE() call is not delayed.
2998 * This prevents cred structures from building up in nfsnodes
2999 * for deleted files.
3001 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
3003 np->n_flag |= NREMOVED;
3006 * We previously purged dvp instead of vp. I don't know why, it
3007 * completely destroys performance. We can't do it anyway with the
3008 * new VFS API since we would be breaking the namecache topology.
3010 cache_purge(vp); /* XXX */
3012 if (vp->v_type == VDIR)
3013 panic("nfs: sillyrename dir");
3015 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
3016 sp->s_cred = crdup(cnp->cn_cred);
3020 /* Fudge together a funny name */
3021 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
3022 (int)(intptr_t)cnp->cn_td);
3024 /* Try lookitups until we get one that isn't there */
3025 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3026 cnp->cn_td, NULL) == 0) {
3028 if (sp->s_name[4] > 'z') {
3033 error = nfs_renameit(dvp, cnp, sp);
3036 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3038 np->n_sillyrename = sp;
3043 kfree((caddr_t)sp, M_NFSREQ);
3049 * Look up a file name and optionally either update the file handle or
3050 * allocate an nfsnode, depending on the value of npp.
3051 * npp == NULL --> just do the lookup
3052 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3054 * *npp != NULL --> update the file handle in the vnode
3057 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
3058 struct thread *td, struct nfsnode **npp)
3060 struct vnode *newvp = NULL;
3061 struct nfsnode *np, *dnp = VTONFS(dvp);
3062 int error = 0, fhlen, attrflag;
3064 struct nfsm_info info;
3067 info.v3 = NFS_ISV3(dvp);
3069 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3070 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3071 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3072 ERROROUT(nfsm_fhtom(&info, dvp));
3073 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3074 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
3075 if (npp && !error) {
3076 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
3079 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3080 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
3081 np->n_fhp = &np->n_fh;
3082 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3083 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
3084 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3085 np->n_fhsize = fhlen;
3087 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3091 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
3100 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3101 NFS_LATTR_NOSHRINK));
3102 if (!attrflag && *npp == NULL) {
3112 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3118 if (npp && *npp == NULL) {
3133 * Nfs Version 3 commit rpc
3135 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3139 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3141 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3142 int error = 0, wccflag = NFSV3_WCCRATTR;
3143 struct nfsm_info info;
3149 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3151 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3152 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3153 ERROROUT(nfsm_fhtom(&info, vp));
3154 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3155 txdr_hyper(offset, tl);
3157 *tl = txdr_unsigned(cnt);
3158 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3159 nfs_vpcred(vp, ND_WRITE), &error));
3160 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3162 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3163 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3164 NFSX_V3WRITEVERF)) {
3165 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3167 error = NFSERR_STALEWRITEVERF;
3178 * - make nfs_bmap() essentially a no-op that does no translation
3179 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3180 * (Maybe I could use the process's page mapping, but I was concerned that
3181 * Kernel Write might not be enabled and also figured copyout() would do
3182 * a lot more work than bcopy() and also it currently happens in the
3183 * context of the swapper process (2).
3185 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3186 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3189 nfs_bmap(struct vop_bmap_args *ap)
3191 /* no token lock required */
3192 if (ap->a_doffsetp != NULL)
3193 *ap->a_doffsetp = ap->a_loffset;
3194 if (ap->a_runp != NULL)
3196 if (ap->a_runb != NULL)
3205 nfs_strategy(struct vop_strategy_args *ap)
3207 struct bio *bio = ap->a_bio;
3209 struct buf *bp __debugvar = bio->bio_buf;
3210 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3214 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3215 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3216 KASSERT(BUF_LOCKINUSE(bp),
3217 ("nfs_strategy: buffer %p not locked", bp));
3219 if (bio->bio_flags & BIO_SYNC)
3220 td = curthread; /* XXX */
3224 lwkt_gettoken(&nmp->nm_token);
3227 * We probably don't need to push an nbio any more since no
3228 * block conversion is required due to the use of 64 bit byte
3229 * offsets, but do it anyway.
3231 * NOTE: When NFS callers itself via this strategy routines and
3232 * sets up a synchronous I/O, it expects the I/O to run
3233 * synchronously (its bio_done routine just assumes it),
3234 * so for now we have to honor the bit.
3236 nbio = push_bio(bio);
3237 nbio->bio_offset = bio->bio_offset;
3238 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3241 * If the op is asynchronous and an i/o daemon is waiting
3242 * queue the request, wake it up and wait for completion
3243 * otherwise just do it ourselves.
3245 if (bio->bio_flags & BIO_SYNC) {
3246 error = nfs_doio(ap->a_vp, nbio, td);
3248 nfs_asyncio(ap->a_vp, nbio);
3251 lwkt_reltoken(&nmp->nm_token);
3257 * fsync vnode op. Just call nfs_flush() with commit == 1.
3259 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3263 nfs_fsync(struct vop_fsync_args *ap)
3265 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3268 lwkt_gettoken(&nmp->nm_token);
3271 * NOTE: Because attributes are set synchronously we currently
3272 * do not have to implement vsetisdirty()/vclrisdirty().
3274 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3276 lwkt_reltoken(&nmp->nm_token);
3282 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3283 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3284 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3285 * set the buffer contains data that has already been written to the server
3286 * and which now needs a commit RPC.
3288 * If commit is 0 we only take one pass and only flush buffers containing new
3291 * If commit is 1 we take two passes, issuing a commit RPC in the second
3294 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3295 * to completely flush all pending data.
3297 * Note that the RB_SCAN code properly handles the case where the
3298 * callback might block and directly or indirectly (another thread) cause
3299 * the RB tree to change.
3302 #ifndef NFS_COMMITBVECSIZ
3303 #define NFS_COMMITBVECSIZ 16
3306 struct nfs_flush_info {
3307 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3314 struct buf *bvary[NFS_COMMITBVECSIZ];
3320 static int nfs_flush_bp(struct buf *bp, void *data);
3321 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3324 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3326 struct nfsnode *np = VTONFS(vp);
3327 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3328 struct nfs_flush_info info;
3331 bzero(&info, sizeof(info));
3334 info.waitfor = waitfor;
3335 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3337 lwkt_gettoken(&vp->v_token);
3343 info.mode = NFI_FLUSHNEW;
3344 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3345 nfs_flush_bp, &info);
3348 * Take a second pass if committing and no error occured.
3349 * Clean up any left over collection (whether an error
3352 if (commit && error == 0) {
3353 info.mode = NFI_COMMIT;
3354 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3355 nfs_flush_bp, &info);
3357 error = nfs_flush_docommit(&info, error);
3361 * Wait for pending I/O to complete before checking whether
3362 * any further dirty buffers exist.
3364 while (waitfor == MNT_WAIT &&
3365 bio_track_active(&vp->v_track_write)) {
3366 error = bio_track_wait(&vp->v_track_write,
3367 info.slpflag, info.slptimeo);
3370 * We have to be able to break out if this
3371 * is an 'intr' mount.
3373 if (nfs_sigintr(nmp, NULL, td)) {
3379 * Since we do not process pending signals,
3380 * once we get a PCATCH our tsleep() will no
3381 * longer sleep, switch to a fixed timeout
3384 if (info.slpflag == PCATCH) {
3386 info.slptimeo = 2 * hz;
3393 * Loop if we are flushing synchronous as well as committing,
3394 * and dirty buffers are still present. Otherwise we might livelock.
3396 } while (waitfor == MNT_WAIT && commit &&
3397 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3400 * The callbacks have to return a negative error to terminate the
3407 * Deal with any error collection
3409 if (np->n_flag & NWRITEERR) {
3410 error = np->n_error;
3411 np->n_flag &= ~NWRITEERR;
3413 lwkt_reltoken(&vp->v_token);
3419 nfs_flush_bp(struct buf *bp, void *data)
3421 struct nfs_flush_info *info = data;
3427 switch(info->mode) {
3429 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3430 if (error && info->loops && info->waitfor == MNT_WAIT) {
3431 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3433 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3434 if (info->slpflag & PCATCH)
3435 lkflags |= LK_PCATCH;
3436 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3442 * Ignore locking errors
3450 * The buffer may have changed out from under us, even if
3451 * we did not block (MPSAFE). Check again now that it is
3454 if (bp->b_vp == info->vp &&
3455 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3464 * Only process buffers in need of a commit which we can
3465 * immediately lock. This may prevent a buffer from being
3466 * committed, but the normal flush loop will block on the
3467 * same buffer so we shouldn't get into an endless loop.
3469 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3470 (B_DELWRI | B_NEEDCOMMIT)) {
3473 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3477 * We must recheck after successfully locking the buffer.
3479 if (bp->b_vp != info->vp ||
3480 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3481 (B_DELWRI | B_NEEDCOMMIT)) {
3487 * NOTE: storing the bp in the bvary[] basically sets
3488 * it up for a commit operation.
3490 * We must call vfs_busy_pages() now so the commit operation
3491 * is interlocked with user modifications to memory mapped
3492 * pages. The b_dirtyoff/b_dirtyend range is not correct
3493 * until after the pages have been busied.
3495 * Note: to avoid loopback deadlocks, we do not
3496 * assign b_runningbufspace.
3499 bp->b_cmd = BUF_CMD_WRITE;
3500 vfs_busy_pages(bp->b_vp, bp);
3501 info->bvary[info->bvsize] = bp;
3502 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3503 if (info->bvsize == 0 || toff < info->beg_off)
3504 info->beg_off = toff;
3505 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3506 if (info->bvsize == 0 || toff > info->end_off)
3507 info->end_off = toff;
3509 if (info->bvsize == NFS_COMMITBVECSIZ) {
3510 error = nfs_flush_docommit(info, 0);
3511 KKASSERT(info->bvsize == 0);
3519 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3529 if (info->bvsize > 0) {
3531 * Commit data on the server, as required. Note that
3532 * nfs_commit will use the vnode's cred for the commit.
3533 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3535 bytes = info->end_off - info->beg_off;
3536 if (bytes > 0x40000000)
3541 retv = nfs_commitrpc_uio(vp, info->beg_off,
3542 (int)bytes, info->td);
3543 if (retv == NFSERR_STALEWRITEVERF)
3544 nfs_clearcommit(vp->v_mount);
3548 * Now, either mark the blocks I/O done or mark the
3549 * blocks dirty, depending on whether the commit
3552 for (i = 0; i < info->bvsize; ++i) {
3553 bp = info->bvary[i];
3554 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3556 * Either an error or the original
3557 * vfs_busy_pages() cleared B_NEEDCOMMIT
3558 * due to finding new dirty VM pages in
3561 * Leave B_DELWRI intact.
3563 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3564 vfs_unbusy_pages(bp);
3565 bp->b_cmd = BUF_CMD_DONE;
3569 * Success, remove B_DELWRI ( bundirty() ).
3571 * b_dirtyoff/b_dirtyend seem to be NFS
3572 * specific. We should probably move that
3573 * into bundirty(). XXX
3575 * We are faking an I/O write, we have to
3576 * start the transaction in order to
3577 * immediately biodone() it.
3580 bp->b_flags &= ~B_ERROR;
3581 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3582 bp->b_dirtyoff = bp->b_dirtyend = 0;
3583 biodone(&bp->b_bio1);
3592 * NFS advisory byte-level locks.
3593 * Currently unsupported.
3595 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3599 nfs_advlock(struct vop_advlock_args *ap)
3601 struct nfsnode *np = VTONFS(ap->a_vp);
3603 /* no token lock currently required */
3605 * The following kludge is to allow diskless support to work
3606 * until a real NFS lockd is implemented. Basically, just pretend
3607 * that this is a local lock.
3609 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3613 * Print out the contents of an nfsnode.
3615 * nfs_print(struct vnode *a_vp)
3618 nfs_print(struct vop_print_args *ap)
3620 struct vnode *vp = ap->a_vp;
3621 struct nfsnode *np = VTONFS(vp);
3623 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3624 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3625 if (vp->v_type == VFIFO)
3632 * nfs special file access vnode op.
3634 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3637 nfs_laccess(struct vop_access_args *ap)
3639 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3643 lwkt_gettoken(&nmp->nm_token);
3644 error = VOP_GETATTR(ap->a_vp, &vattr);
3646 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3649 lwkt_reltoken(&nmp->nm_token);
3655 * Read wrapper for fifos.
3657 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3658 * struct ucred *a_cred)
3661 nfsfifo_read(struct vop_read_args *ap)
3663 struct nfsnode *np = VTONFS(ap->a_vp);
3665 /* no token access required */
3670 getnanotime(&np->n_atim);
3671 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3675 * Write wrapper for fifos.
3677 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3678 * struct ucred *a_cred)
3681 nfsfifo_write(struct vop_write_args *ap)
3683 struct nfsnode *np = VTONFS(ap->a_vp);
3685 /* no token access required */
3690 getnanotime(&np->n_mtim);
3691 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3695 * Close wrapper for fifos.
3697 * Update the times on the nfsnode then do fifo close.
3699 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3702 nfsfifo_close(struct vop_close_args *ap)
3704 struct vnode *vp = ap->a_vp;
3705 struct nfsnode *np = VTONFS(vp);
3709 /* no token access required */
3711 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3712 if (np->n_flag & (NACC | NUPD)) {
3714 if (np->n_flag & NACC)
3716 if (np->n_flag & NUPD)
3719 if (VREFCNT(vp) == 1 &&
3720 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3722 if (np->n_flag & NACC)
3723 vattr.va_atime = np->n_atim;
3724 if (np->n_flag & NUPD)
3725 vattr.va_mtime = np->n_mtim;
3726 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3729 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3732 /************************************************************************
3734 ************************************************************************/
3736 static void filt_nfsdetach(struct knote *kn);
3737 static int filt_nfsread(struct knote *kn, long hint);
3738 static int filt_nfswrite(struct knote *kn, long hint);
3739 static int filt_nfsvnode(struct knote *kn, long hint);
3741 static struct filterops nfsread_filtops =
3742 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3743 NULL, filt_nfsdetach, filt_nfsread };
3744 static struct filterops nfswrite_filtops =
3745 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3746 NULL, filt_nfsdetach, filt_nfswrite };
3747 static struct filterops nfsvnode_filtops =
3748 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3749 NULL, filt_nfsdetach, filt_nfsvnode };
3752 nfs_kqfilter (struct vop_kqfilter_args *ap)
3754 struct vnode *vp = ap->a_vp;
3755 struct knote *kn = ap->a_kn;
3757 switch (kn->kn_filter) {
3759 kn->kn_fop = &nfsread_filtops;
3762 kn->kn_fop = &nfswrite_filtops;
3765 kn->kn_fop = &nfsvnode_filtops;
3768 return (EOPNOTSUPP);
3771 kn->kn_hook = (caddr_t)vp;
3773 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3779 filt_nfsdetach(struct knote *kn)
3781 struct vnode *vp = (void *)kn->kn_hook;
3783 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3787 filt_nfsread(struct knote *kn, long hint)
3789 struct vnode *vp = (void *)kn->kn_hook;
3790 struct nfsnode *node = VTONFS(vp);
3793 if (hint == NOTE_REVOKE) {
3794 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3799 * Interlock against MP races when performing this function. XXX
3801 /* TMPFS_NODE_LOCK_SH(node); */
3802 off = node->n_size - kn->kn_fp->f_offset;
3803 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3804 if (kn->kn_sfflags & NOTE_OLDAPI) {
3805 /* TMPFS_NODE_UNLOCK(node); */
3808 if (kn->kn_data == 0) {
3809 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3811 /* TMPFS_NODE_UNLOCK(node); */
3812 return (kn->kn_data != 0);
3816 filt_nfswrite(struct knote *kn, long hint)
3818 if (hint == NOTE_REVOKE)
3819 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3825 filt_nfsvnode(struct knote *kn, long hint)
3827 if (kn->kn_sfflags & hint)
3828 kn->kn_fflags |= hint;
3829 if (hint == NOTE_REVOKE) {
3830 kn->kn_flags |= (EV_EOF | EV_NODATA);
3833 return (kn->kn_fflags != 0);