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>
85 #include <sys/thread2.h>
91 static int nfsfifo_read (struct vop_read_args *);
92 static int nfsfifo_write (struct vop_write_args *);
93 static int nfsfifo_close (struct vop_close_args *);
94 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
95 static int nfs_lookup (struct vop_old_lookup_args *);
96 static int nfs_create (struct vop_old_create_args *);
97 static int nfs_mknod (struct vop_old_mknod_args *);
98 static int nfs_open (struct vop_open_args *);
99 static int nfs_close (struct vop_close_args *);
100 static int nfs_access (struct vop_access_args *);
101 static int nfs_getattr (struct vop_getattr_args *);
102 static int nfs_setattr (struct vop_setattr_args *);
103 static int nfs_read (struct vop_read_args *);
104 static int nfs_mmap (struct vop_mmap_args *);
105 static int nfs_fsync (struct vop_fsync_args *);
106 static int nfs_remove (struct vop_old_remove_args *);
107 static int nfs_link (struct vop_old_link_args *);
108 static int nfs_rename (struct vop_old_rename_args *);
109 static int nfs_mkdir (struct vop_old_mkdir_args *);
110 static int nfs_rmdir (struct vop_old_rmdir_args *);
111 static int nfs_symlink (struct vop_old_symlink_args *);
112 static int nfs_readdir (struct vop_readdir_args *);
113 static int nfs_bmap (struct vop_bmap_args *);
114 static int nfs_strategy (struct vop_strategy_args *);
115 static int nfs_lookitup (struct vnode *, const char *, int,
116 struct ucred *, struct thread *, struct nfsnode **);
117 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
118 static int nfs_laccess (struct vop_access_args *);
119 static int nfs_readlink (struct vop_readlink_args *);
120 static int nfs_print (struct vop_print_args *);
121 static int nfs_advlock (struct vop_advlock_args *);
123 static int nfs_nresolve (struct vop_nresolve_args *);
125 * Global vfs data structures for nfs
127 struct vop_ops nfsv2_vnode_vops = {
128 .vop_default = vop_defaultop,
129 .vop_access = nfs_access,
130 .vop_advlock = nfs_advlock,
131 .vop_bmap = nfs_bmap,
132 .vop_close = nfs_close,
133 .vop_old_create = nfs_create,
134 .vop_fsync = nfs_fsync,
135 .vop_getattr = nfs_getattr,
136 .vop_getpages = vop_stdgetpages,
137 .vop_putpages = vop_stdputpages,
138 .vop_inactive = nfs_inactive,
139 .vop_old_link = nfs_link,
140 .vop_old_lookup = nfs_lookup,
141 .vop_old_mkdir = nfs_mkdir,
142 .vop_old_mknod = nfs_mknod,
143 .vop_mmap = nfs_mmap,
144 .vop_open = nfs_open,
145 .vop_print = nfs_print,
146 .vop_read = nfs_read,
147 .vop_readdir = nfs_readdir,
148 .vop_readlink = nfs_readlink,
149 .vop_reclaim = nfs_reclaim,
150 .vop_old_remove = nfs_remove,
151 .vop_old_rename = nfs_rename,
152 .vop_old_rmdir = nfs_rmdir,
153 .vop_setattr = nfs_setattr,
154 .vop_strategy = nfs_strategy,
155 .vop_old_symlink = nfs_symlink,
156 .vop_write = nfs_write,
157 .vop_nresolve = nfs_nresolve
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)
242 * Returns whether a name component is a degenerate '.' or '..'.
246 nlcdegenerate(struct nlcomponent *nlc)
248 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
250 if (nlc->nlc_namelen == 2 &&
251 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
257 nfs3_access_otw(struct vnode *vp, int wmode,
258 struct thread *td, struct ucred *cred)
260 struct nfsnode *np = VTONFS(vp);
265 struct nfsm_info info;
270 nfsstats.rpccnt[NFSPROC_ACCESS]++;
271 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
272 NFSX_FH(info.v3) + NFSX_UNSIGNED);
273 ERROROUT(nfsm_fhtom(&info, vp));
274 tl = nfsm_build(&info, NFSX_UNSIGNED);
275 *tl = txdr_unsigned(wmode);
276 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
277 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
279 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
280 rmode = fxdr_unsigned(u_int32_t, *tl);
282 np->n_modeuid = cred->cr_uid;
283 np->n_modestamp = mycpu->gd_time_seconds;
292 * nfs access vnode op.
293 * For nfs version 2, just return ok. File accesses may fail later.
294 * For nfs version 3, use the access rpc to check accessibility. If file modes
295 * are changed on the server, accesses might still fail later.
297 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
300 nfs_access(struct vop_access_args *ap)
303 struct vnode *vp = ap->a_vp;
304 thread_t td = curthread;
306 u_int32_t mode, wmode;
307 struct nfsnode *np = VTONFS(vp);
308 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
309 int v3 = NFS_ISV3(vp);
311 lwkt_gettoken(&nmp->nm_token);
314 * Disallow write attempts on filesystems mounted read-only;
315 * unless the file is a socket, fifo, or a block or character
316 * device resident on the filesystem.
318 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
319 switch (vp->v_type) {
323 lwkt_reltoken(&nmp->nm_token);
331 * The NFS protocol passes only the effective uid/gid over the wire but
332 * we need to check access against real ids if AT_EACCESS not set.
333 * Handle this case by cloning the credentials and setting the
334 * effective ids to the real ones.
336 if (ap->a_flags & AT_EACCESS) {
337 cred = crhold(ap->a_cred);
339 cred = crdup(ap->a_cred);
340 cred->cr_uid = cred->cr_ruid;
341 cred->cr_gid = cred->cr_rgid;
345 * For nfs v3, check to see if we have done this recently, and if
346 * so return our cached result instead of making an ACCESS call.
347 * If not, do an access rpc, otherwise you are stuck emulating
348 * ufs_access() locally using the vattr. This may not be correct,
349 * since the server may apply other access criteria such as
350 * client uid-->server uid mapping that we do not know about.
353 if (ap->a_mode & VREAD)
354 mode = NFSV3ACCESS_READ;
357 if (vp->v_type != VDIR) {
358 if (ap->a_mode & VWRITE)
359 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
360 if (ap->a_mode & VEXEC)
361 mode |= NFSV3ACCESS_EXECUTE;
363 if (ap->a_mode & VWRITE)
364 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
366 if (ap->a_mode & VEXEC)
367 mode |= NFSV3ACCESS_LOOKUP;
369 /* XXX safety belt, only make blanket request if caching */
370 if (nfsaccess_cache_timeout > 0) {
371 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
372 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
373 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
379 * Does our cached result allow us to give a definite yes to
382 if (np->n_modestamp &&
383 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
384 (cred->cr_uid == np->n_modeuid) &&
385 ((np->n_mode & mode) == mode)) {
386 nfsstats.accesscache_hits++;
389 * Either a no, or a don't know. Go to the wire.
391 nfsstats.accesscache_misses++;
392 error = nfs3_access_otw(vp, wmode, td, cred);
394 if ((np->n_mode & mode) != mode) {
400 if ((error = nfs_laccess(ap)) != 0) {
402 lwkt_reltoken(&nmp->nm_token);
407 * Attempt to prevent a mapped root from accessing a file
408 * which it shouldn't. We try to read a byte from the file
409 * if the user is root and the file is not zero length.
410 * After calling nfs_laccess, we should have the correct
413 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
414 && VTONFS(vp)->n_size > 0) {
421 auio.uio_iov = &aiov;
425 auio.uio_segflg = UIO_SYSSPACE;
426 auio.uio_rw = UIO_READ;
429 if (vp->v_type == VREG) {
430 error = nfs_readrpc_uio(vp, &auio);
431 } else if (vp->v_type == VDIR) {
433 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
435 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
436 error = nfs_readdirrpc_uio(vp, &auio);
438 } else if (vp->v_type == VLNK) {
439 error = nfs_readlinkrpc_uio(vp, &auio);
446 * [re]record creds for reading and/or writing if access
447 * was granted. Assume the NFS server will grant read access
448 * for execute requests.
451 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
454 crfree(np->n_rucred);
457 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
460 crfree(np->n_wucred);
464 lwkt_reltoken(&nmp->nm_token);
471 * Check to see if the type is ok
472 * and that deletion is not in progress.
473 * For paged in text files, you will need to flush the page cache
474 * if consistency is lost.
476 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
481 nfs_open(struct vop_open_args *ap)
483 struct vnode *vp = ap->a_vp;
484 struct nfsnode *np = VTONFS(vp);
485 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
489 lwkt_gettoken(&nmp->nm_token);
491 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
493 kprintf("open eacces vtyp=%d\n",vp->v_type);
495 lwkt_reltoken(&nmp->nm_token);
500 * Save valid creds for reading and writing for later RPCs.
502 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
505 crfree(np->n_rucred);
506 np->n_rucred = ap->a_cred;
508 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
511 crfree(np->n_wucred);
512 np->n_wucred = ap->a_cred;
516 * Clear the attribute cache only if opening with write access. It
517 * is unclear if we should do this at all here, but we certainly
518 * should not clear the cache unconditionally simply because a file
521 if (ap->a_mode & FWRITE)
525 * For normal NFS, reconcile changes made locally verses
526 * changes made remotely. Note that VOP_GETATTR only goes
527 * to the wire if the cached attribute has timed out or been
530 * If local modifications have been made clear the attribute
531 * cache to force an attribute and modified time check. If
532 * GETATTR detects that the file has been changed by someone
533 * other then us it will set NRMODIFIED.
535 * If we are opening a directory and local changes have been
536 * made we have to invalidate the cache in order to ensure
537 * that we get the most up-to-date information from the
540 if (np->n_flag & NLMODIFIED) {
542 if (vp->v_type == VDIR) {
543 error = nfs_vinvalbuf(vp, V_SAVE, 1);
544 if (error == EINTR) {
545 lwkt_reltoken(&nmp->nm_token);
551 error = VOP_GETATTR(vp, &vattr);
553 lwkt_reltoken(&nmp->nm_token);
556 if (np->n_flag & NRMODIFIED) {
557 if (vp->v_type == VDIR)
559 error = nfs_vinvalbuf(vp, V_SAVE, 1);
560 if (error == EINTR) {
561 lwkt_reltoken(&nmp->nm_token);
564 np->n_flag &= ~NRMODIFIED;
566 error = vop_stdopen(ap);
567 lwkt_reltoken(&nmp->nm_token);
574 * What an NFS client should do upon close after writing is a debatable issue.
575 * Most NFS clients push delayed writes to the server upon close, basically for
577 * 1 - So that any write errors may be reported back to the client process
578 * doing the close system call. By far the two most likely errors are
579 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
580 * 2 - To put a worst case upper bound on cache inconsistency between
581 * multiple clients for the file.
582 * There is also a consistency problem for Version 2 of the protocol w.r.t.
583 * not being able to tell if other clients are writing a file concurrently,
584 * since there is no way of knowing if the changed modify time in the reply
585 * is only due to the write for this client.
586 * (NFS Version 3 provides weak cache consistency data in the reply that
587 * should be sufficient to detect and handle this case.)
589 * The current code does the following:
590 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
591 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
592 * or commit them (this satisfies 1 and 2 except for the
593 * case where the server crashes after this close but
594 * before the commit RPC, which is felt to be "good
595 * enough". Changing the last argument to nfs_flush() to
596 * a 1 would force a commit operation, if it is felt a
597 * commit is necessary now.
598 * for NQNFS - do nothing now, since 2 is dealt with via leases and
599 * 1 should be dealt with via an fsync() system call for
600 * cases where write errors are important.
602 * nfs_close(struct vnode *a_vp, int a_fflag)
606 nfs_close(struct vop_close_args *ap)
608 struct vnode *vp = ap->a_vp;
609 struct nfsnode *np = VTONFS(vp);
610 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
612 thread_t td = curthread;
614 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
615 lwkt_gettoken(&nmp->nm_token);
617 if (vp->v_type == VREG) {
618 if (np->n_flag & NLMODIFIED) {
621 * Under NFSv3 we have dirty buffers to dispose of. We
622 * must flush them to the NFS server. We have the option
623 * of waiting all the way through the commit rpc or just
624 * waiting for the initial write. The default is to only
625 * wait through the initial write so the data is in the
626 * server's cache, which is roughly similar to the state
627 * a standard disk subsystem leaves the file in on close().
629 * We cannot clear the NLMODIFIED bit in np->n_flag due to
630 * potential races with other processes, and certainly
631 * cannot clear it if we don't commit.
633 int cm = nfsv3_commit_on_close ? 1 : 0;
634 error = nfs_flush(vp, MNT_WAIT, td, cm);
635 /* np->n_flag &= ~NLMODIFIED; */
637 error = nfs_vinvalbuf(vp, V_SAVE, 1);
641 if (np->n_flag & NWRITEERR) {
642 np->n_flag &= ~NWRITEERR;
647 lwkt_reltoken(&nmp->nm_token);
653 * nfs getattr call from vfs.
655 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
658 nfs_getattr(struct vop_getattr_args *ap)
660 struct vnode *vp = ap->a_vp;
661 struct nfsnode *np = VTONFS(vp);
662 struct nfsmount *nmp;
664 thread_t td = curthread;
665 struct nfsm_info info;
668 info.v3 = NFS_ISV3(vp);
669 nmp = VFSTONFS(vp->v_mount);
671 lwkt_gettoken(&nmp->nm_token);
674 * Update local times for special files.
676 if (np->n_flag & (NACC | NUPD))
679 * First look in the cache.
681 if (nfs_getattrcache(vp, ap->a_vap) == 0)
684 if (info.v3 && nfsaccess_cache_timeout > 0) {
685 nfsstats.accesscache_misses++;
686 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
687 if (nfs_getattrcache(vp, ap->a_vap) == 0)
691 nfsstats.rpccnt[NFSPROC_GETATTR]++;
692 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
693 ERROROUT(nfsm_fhtom(&info, vp));
694 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
695 nfs_vpcred(vp, ND_CHECK), &error));
697 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
703 * NFS doesn't support chflags flags. If the nfs mount was
704 * made -o cache set the UF_CACHE bit for swapcache.
706 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
707 ap->a_vap->va_flags |= UF_CACHE;
709 lwkt_reltoken(&nmp->nm_token);
716 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
719 nfs_setattr(struct vop_setattr_args *ap)
721 struct vnode *vp = ap->a_vp;
722 struct nfsnode *np = VTONFS(vp);
723 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
724 struct vattr *vap = ap->a_vap;
727 thread_t td = curthread;
733 * Setting of flags is not supported.
735 if (vap->va_flags != VNOVAL)
739 * Disallow write attempts if the filesystem is mounted read-only.
741 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
742 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
743 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
744 (vp->v_mount->mnt_flag & MNT_RDONLY))
747 lwkt_gettoken(&nmp->nm_token);
749 if (vap->va_size != VNOVAL) {
751 * truncation requested
753 switch (vp->v_type) {
755 lwkt_reltoken(&nmp->nm_token);
761 if (vap->va_mtime.tv_sec == VNOVAL &&
762 vap->va_atime.tv_sec == VNOVAL &&
763 vap->va_mode == (mode_t)VNOVAL &&
764 vap->va_uid == (uid_t)VNOVAL &&
765 vap->va_gid == (gid_t)VNOVAL) {
766 lwkt_reltoken(&nmp->nm_token);
769 vap->va_size = VNOVAL;
773 * Disallow write attempts if the filesystem is
776 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
777 lwkt_reltoken(&nmp->nm_token);
783 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
786 if (np->n_flag & NLMODIFIED) {
787 if (vap->va_size == 0)
788 error = nfs_vinvalbuf(vp, 0, 1);
790 error = nfs_vinvalbuf(vp, V_SAVE, 1);
794 * note: this loop case almost always happens at
795 * least once per truncation.
797 if (error == 0 && np->n_size != vap->va_size)
799 np->n_vattr.va_size = vap->va_size;
802 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
804 * What to do. If we are modifying the mtime we lose
805 * mtime detection of changes made by the server or other
806 * clients. But programs like rsync/rdist/cpdup are going
807 * to call utimes a lot. We don't want to piecemeal sync.
809 * For now sync if any prior remote changes were detected,
810 * but allow us to lose track of remote changes made during
811 * the utimes operation.
813 if (np->n_flag & NRMODIFIED)
814 error = nfs_vinvalbuf(vp, V_SAVE, 1);
815 if (error == EINTR) {
816 lwkt_reltoken(&nmp->nm_token);
820 if (vap->va_mtime.tv_sec != VNOVAL) {
821 np->n_mtime = vap->va_mtime.tv_sec;
825 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
828 * Sanity check if a truncation was issued. This should only occur
829 * if multiple processes are racing on the same file.
831 if (error == 0 && vap->va_size != VNOVAL &&
832 np->n_size != vap->va_size) {
833 kprintf("NFS ftruncate: server disagrees on the file size: "
836 (intmax_t)vap->va_size,
837 (intmax_t)np->n_size);
840 if (error && vap->va_size != VNOVAL) {
841 np->n_size = np->n_vattr.va_size = tsize;
842 nfs_meta_setsize(vp, td, np->n_size, 0);
844 lwkt_reltoken(&nmp->nm_token);
850 * Do an nfs setattr rpc.
853 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
854 struct ucred *cred, struct thread *td)
856 struct nfsv2_sattr *sp;
857 struct nfsnode *np = VTONFS(vp);
859 int error = 0, wccflag = NFSV3_WCCRATTR;
860 struct nfsm_info info;
863 info.v3 = NFS_ISV3(vp);
865 nfsstats.rpccnt[NFSPROC_SETATTR]++;
866 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
867 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
868 ERROROUT(nfsm_fhtom(&info, vp));
870 nfsm_v3attrbuild(&info, vap, TRUE);
871 tl = nfsm_build(&info, NFSX_UNSIGNED);
874 sp = nfsm_build(&info, NFSX_V2SATTR);
875 if (vap->va_mode == (mode_t)VNOVAL)
876 sp->sa_mode = nfs_xdrneg1;
878 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
879 if (vap->va_uid == (uid_t)VNOVAL)
880 sp->sa_uid = nfs_xdrneg1;
882 sp->sa_uid = txdr_unsigned(vap->va_uid);
883 if (vap->va_gid == (gid_t)VNOVAL)
884 sp->sa_gid = nfs_xdrneg1;
886 sp->sa_gid = txdr_unsigned(vap->va_gid);
887 sp->sa_size = txdr_unsigned(vap->va_size);
888 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
889 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
891 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
894 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
896 ERROROUT(nfsm_loadattr(&info, vp, NULL));
906 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
912 cache_setvp(nch, vp);
913 cache_settimeout(nch, nctimeout);
917 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
918 * nfs_lookup() until all remaining new api calls are implemented.
920 * Resolve a namecache entry. This function is passed a locked ncp and
921 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
924 nfs_nresolve(struct vop_nresolve_args *ap)
926 struct thread *td = curthread;
927 struct namecache *ncp;
928 struct nfsmount *nmp;
938 struct nfsm_info info;
941 nmp = VFSTONFS(dvp->v_mount);
943 lwkt_gettoken(&nmp->nm_token);
945 if ((error = vget(dvp, LK_SHARED)) != 0) {
946 lwkt_reltoken(&nmp->nm_token);
951 info.v3 = NFS_ISV3(dvp);
954 nfsstats.lookupcache_misses++;
955 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
956 ncp = ap->a_nch->ncp;
958 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
959 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
960 ERROROUT(nfsm_fhtom(&info, dvp));
961 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
962 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
963 ap->a_cred, &error));
966 * Cache negatve lookups to reduce NFS traffic, but use
967 * a fast timeout. Otherwise use a timeout of 1 tick.
968 * XXX we should add a namecache flag for no-caching
969 * to uncache the negative hit as soon as possible, but
970 * we cannot simply destroy the entry because it is used
971 * as a placeholder by the caller.
973 * The refactored nfs code will overwrite a non-zero error
974 * with 0 when we use ERROROUT(), so don't here.
977 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
978 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
990 * Success, get the file handle, do various checks, and load
991 * post-operation data from the reply packet. Theoretically
992 * we should never be looking up "." so, theoretically, we
993 * should never get the same file handle as our directory. But
994 * we check anyway. XXX
996 * Note that no timeout is set for the positive cache hit. We
997 * assume, theoretically, that ESTALE returns will be dealt with
998 * properly to handle NFS races and in anycase we cannot depend
999 * on a timeout to deal with NFS open/create/excl issues so instead
1000 * of a bad hack here the rest of the NFS client code needs to do
1003 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1006 if (NFS_CMPFH(np, fhp, fhsize)) {
1010 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1015 lwkt_reltoken(&nmp->nm_token);
1021 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1022 NFS_LATTR_NOSHRINK));
1023 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1024 NFS_LATTR_NOSHRINK));
1026 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1028 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1032 lwkt_reltoken(&nmp->nm_token);
1044 * 'cached' nfs directory lookup
1046 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1048 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1049 * struct componentname *a_cnp)
1052 nfs_lookup(struct vop_old_lookup_args *ap)
1054 struct componentname *cnp = ap->a_cnp;
1055 struct vnode *dvp = ap->a_dvp;
1056 struct vnode **vpp = ap->a_vpp;
1057 int flags = cnp->cn_flags;
1058 struct vnode *newvp;
1059 struct vnode *notvp;
1060 struct nfsmount *nmp;
1064 int lockparent, wantparent, attrflag, fhsize;
1067 struct nfsm_info info;
1070 info.v3 = NFS_ISV3(dvp);
1073 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1076 * Read-only mount check and directory check.
1079 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1080 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1083 if (dvp->v_type != VDIR)
1087 * Look it up in the cache. Note that ENOENT is only returned if we
1088 * previously entered a negative hit (see later on). The additional
1089 * nfsneg_cache_timeout check causes previously cached results to
1090 * be instantly ignored if the negative caching is turned off.
1092 lockparent = flags & CNP_LOCKPARENT;
1093 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1094 nmp = VFSTONFS(dvp->v_mount);
1097 lwkt_gettoken(&nmp->nm_token);
1104 nfsstats.lookupcache_misses++;
1105 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1106 len = cnp->cn_namelen;
1107 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1108 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1109 ERROROUT(nfsm_fhtom(&info, dvp));
1110 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1111 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1112 cnp->cn_cred, &error));
1114 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1115 NFS_LATTR_NOSHRINK);
1125 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1128 * Handle RENAME case...
1130 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1131 if (NFS_CMPFH(np, fhp, fhsize)) {
1134 lwkt_reltoken(&nmp->nm_token);
1137 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1141 lwkt_reltoken(&nmp->nm_token);
1146 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1147 NFS_LATTR_NOSHRINK));
1148 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1149 NFS_LATTR_NOSHRINK));
1151 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1158 cnp->cn_flags |= CNP_PDIRUNLOCK;
1160 lwkt_reltoken(&nmp->nm_token);
1164 if (flags & CNP_ISDOTDOT) {
1166 cnp->cn_flags |= CNP_PDIRUNLOCK;
1167 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1169 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1170 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1171 lwkt_reltoken(&nmp->nm_token);
1172 return (error); /* NOTE: return error from nget */
1176 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1179 lwkt_reltoken(&nmp->nm_token);
1182 cnp->cn_flags |= CNP_PDIRUNLOCK;
1184 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1188 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1192 lwkt_reltoken(&nmp->nm_token);
1197 cnp->cn_flags |= CNP_PDIRUNLOCK;
1202 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1203 NFS_LATTR_NOSHRINK));
1204 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1205 NFS_LATTR_NOSHRINK));
1207 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1210 /* XXX MOVE TO nfs_nremove() */
1211 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1212 cnp->cn_nameiop != NAMEI_DELETE) {
1213 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1221 if (newvp != NULLVP) {
1225 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1226 cnp->cn_nameiop == NAMEI_RENAME) &&
1230 cnp->cn_flags |= CNP_PDIRUNLOCK;
1232 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1235 error = EJUSTRETURN;
1238 lwkt_reltoken(&nmp->nm_token);
1244 * Just call nfs_bioread() to do the work.
1246 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1247 * struct ucred *a_cred)
1250 nfs_read(struct vop_read_args *ap)
1252 struct vnode *vp = ap->a_vp;
1253 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1256 lwkt_gettoken(&nmp->nm_token);
1257 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1258 lwkt_reltoken(&nmp->nm_token);
1266 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1269 nfs_readlink(struct vop_readlink_args *ap)
1271 struct vnode *vp = ap->a_vp;
1272 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1275 if (vp->v_type != VLNK)
1278 lwkt_gettoken(&nmp->nm_token);
1279 error = nfs_bioread(vp, ap->a_uio, 0);
1280 lwkt_reltoken(&nmp->nm_token);
1286 * Do a readlink rpc.
1287 * Called by nfs_doio() from below the buffer cache.
1290 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1292 int error = 0, len, attrflag;
1293 struct nfsm_info info;
1296 info.v3 = NFS_ISV3(vp);
1298 nfsstats.rpccnt[NFSPROC_READLINK]++;
1299 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1300 ERROROUT(nfsm_fhtom(&info, vp));
1301 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1302 nfs_vpcred(vp, ND_CHECK), &error));
1304 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1305 NFS_LATTR_NOSHRINK));
1308 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1309 if (len == NFS_MAXPATHLEN) {
1310 struct nfsnode *np = VTONFS(vp);
1311 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1314 ERROROUT(nfsm_mtouio(&info, uiop, len));
1323 * nfs synchronous read rpc using UIO
1326 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1329 struct nfsmount *nmp;
1330 int error = 0, len, retlen, tsiz, eof, attrflag;
1331 struct nfsm_info info;
1335 info.v3 = NFS_ISV3(vp);
1340 nmp = VFSTONFS(vp->v_mount);
1342 tsiz = uiop->uio_resid;
1343 tmp_off = uiop->uio_offset + tsiz;
1344 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1346 tmp_off = uiop->uio_offset;
1348 nfsstats.rpccnt[NFSPROC_READ]++;
1349 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1350 nfsm_reqhead(&info, vp, NFSPROC_READ,
1351 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1352 ERROROUT(nfsm_fhtom(&info, vp));
1353 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1355 txdr_hyper(uiop->uio_offset, tl);
1356 *(tl + 2) = txdr_unsigned(len);
1358 *tl++ = txdr_unsigned(uiop->uio_offset);
1359 *tl++ = txdr_unsigned(len);
1362 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1363 nfs_vpcred(vp, ND_READ), &error));
1365 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1366 NFS_LATTR_NOSHRINK));
1367 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1368 eof = fxdr_unsigned(int, *(tl + 1));
1370 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1372 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1373 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1378 * Handle short-read from server (NFSv3). If EOF is not
1379 * flagged (and no error occurred), but retlen is less
1380 * then the request size, we must zero-fill the remainder.
1382 if (retlen < len && info.v3 && eof == 0) {
1383 ERROROUT(uiomovez(len - retlen, uiop));
1389 * Terminate loop on EOF or zero-length read.
1391 * For NFSv2 a short-read indicates EOF, not zero-fill,
1392 * and also terminates the loop.
1395 if (eof || retlen == 0)
1397 } else if (retlen < len) {
1409 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1410 int *iomode, int *must_commit)
1414 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1415 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1416 int committed = NFSV3WRITE_FILESYNC;
1417 struct nfsm_info info;
1420 info.v3 = NFS_ISV3(vp);
1423 if (uiop->uio_iovcnt != 1)
1424 panic("nfs: writerpc iovcnt > 1");
1427 tsiz = uiop->uio_resid;
1428 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1431 nfsstats.rpccnt[NFSPROC_WRITE]++;
1432 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1433 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1434 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1436 ERROROUT(nfsm_fhtom(&info, vp));
1438 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1439 txdr_hyper(uiop->uio_offset, tl);
1441 *tl++ = txdr_unsigned(len);
1442 *tl++ = txdr_unsigned(*iomode);
1443 *tl = txdr_unsigned(len);
1447 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1448 /* Set both "begin" and "current" to non-garbage. */
1449 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1450 *tl++ = x; /* "begin offset" */
1451 *tl++ = x; /* "current offset" */
1452 x = txdr_unsigned(len);
1453 *tl++ = x; /* total to this offset */
1454 *tl = x; /* size of this write */
1456 ERROROUT(nfsm_uiotom(&info, uiop, len));
1457 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1458 nfs_vpcred(vp, ND_WRITE), &error));
1461 * The write RPC returns a before and after mtime. The
1462 * nfsm_wcc_data() macro checks the before n_mtime
1463 * against the before time and stores the after time
1464 * in the nfsnode's cached vattr and n_mtime field.
1465 * The NRMODIFIED bit will be set if the before
1466 * time did not match the original mtime.
1468 wccflag = NFSV3_WCCCHK;
1469 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1471 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1472 rlen = fxdr_unsigned(int, *tl++);
1478 } else if (rlen < len) {
1479 backup = len - rlen;
1480 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1481 uiop->uio_iov->iov_len += backup;
1482 uiop->uio_offset -= backup;
1483 uiop->uio_resid += backup;
1486 commit = fxdr_unsigned(int, *tl++);
1489 * Return the lowest committment level
1490 * obtained by any of the RPCs.
1492 if (committed == NFSV3WRITE_FILESYNC)
1494 else if (committed == NFSV3WRITE_DATASYNC &&
1495 commit == NFSV3WRITE_UNSTABLE)
1497 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1498 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1500 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1501 } else if (bcmp((caddr_t)tl,
1502 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1504 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1509 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1518 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1519 committed = NFSV3WRITE_FILESYNC;
1520 *iomode = committed;
1522 uiop->uio_resid = tsiz;
1528 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1529 * mode set to specify the file type and the size field for rdev.
1532 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1535 struct nfsv2_sattr *sp;
1537 struct vnode *newvp = NULL;
1538 struct nfsnode *np = NULL;
1540 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1542 struct nfsm_info info;
1545 info.v3 = NFS_ISV3(dvp);
1547 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1548 rmajor = txdr_unsigned(vap->va_rmajor);
1549 rminor = txdr_unsigned(vap->va_rminor);
1550 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1551 rmajor = nfs_xdrneg1;
1552 rminor = nfs_xdrneg1;
1554 return (EOPNOTSUPP);
1556 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1559 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1560 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1561 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1562 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1563 ERROROUT(nfsm_fhtom(&info, dvp));
1564 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1567 tl = nfsm_build(&info, NFSX_UNSIGNED);
1568 *tl++ = vtonfsv3_type(vap->va_type);
1569 nfsm_v3attrbuild(&info, vap, FALSE);
1570 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1571 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1572 *tl++ = txdr_unsigned(vap->va_rmajor);
1573 *tl = txdr_unsigned(vap->va_rminor);
1576 sp = nfsm_build(&info, NFSX_V2SATTR);
1577 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1578 sp->sa_uid = nfs_xdrneg1;
1579 sp->sa_gid = nfs_xdrneg1;
1580 sp->sa_size = makeudev(rmajor, rminor);
1581 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1582 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1584 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1585 cnp->cn_cred, &error));
1587 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1593 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1594 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1600 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1611 VTONFS(dvp)->n_flag |= NLMODIFIED;
1613 VTONFS(dvp)->n_attrstamp = 0;
1619 * just call nfs_mknodrpc() to do the work.
1621 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1622 * struct componentname *a_cnp, struct vattr *a_vap)
1626 nfs_mknod(struct vop_old_mknod_args *ap)
1628 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1631 lwkt_gettoken(&nmp->nm_token);
1632 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1633 lwkt_reltoken(&nmp->nm_token);
1638 static u_long create_verf;
1640 * nfs file create call
1642 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1643 * struct componentname *a_cnp, struct vattr *a_vap)
1646 nfs_create(struct vop_old_create_args *ap)
1648 struct vnode *dvp = ap->a_dvp;
1649 struct vattr *vap = ap->a_vap;
1650 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1651 struct componentname *cnp = ap->a_cnp;
1652 struct nfsv2_sattr *sp;
1654 struct nfsnode *np = NULL;
1655 struct vnode *newvp = NULL;
1656 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1658 struct nfsm_info info;
1661 info.v3 = NFS_ISV3(dvp);
1662 lwkt_gettoken(&nmp->nm_token);
1665 * Oops, not for me..
1667 if (vap->va_type == VSOCK) {
1668 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1669 lwkt_reltoken(&nmp->nm_token);
1673 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1674 lwkt_reltoken(&nmp->nm_token);
1677 if (vap->va_vaflags & VA_EXCLUSIVE)
1680 nfsstats.rpccnt[NFSPROC_CREATE]++;
1681 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1682 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1683 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1684 ERROROUT(nfsm_fhtom(&info, dvp));
1685 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1688 tl = nfsm_build(&info, NFSX_UNSIGNED);
1689 if (fmode & O_EXCL) {
1690 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1691 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1693 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1694 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1697 *tl++ = create_verf;
1698 *tl = ++create_verf;
1700 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1701 nfsm_v3attrbuild(&info, vap, FALSE);
1704 sp = nfsm_build(&info, NFSX_V2SATTR);
1705 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1706 sp->sa_uid = nfs_xdrneg1;
1707 sp->sa_gid = nfs_xdrneg1;
1709 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1710 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1712 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1713 cnp->cn_cred, &error));
1715 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1721 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1722 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1729 error = nfsm_wcc_data(&info, dvp, &wccflag);
1731 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1737 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1738 KKASSERT(newvp == NULL);
1742 } else if (info.v3 && (fmode & O_EXCL)) {
1744 * We are normally called with only a partially initialized
1745 * VAP. Since the NFSv3 spec says that server may use the
1746 * file attributes to store the verifier, the spec requires
1747 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1748 * in atime, but we can't really assume that all servers will
1749 * so we ensure that our SETATTR sets both atime and mtime.
1751 if (vap->va_mtime.tv_sec == VNOVAL)
1752 vfs_timestamp(&vap->va_mtime);
1753 if (vap->va_atime.tv_sec == VNOVAL)
1754 vap->va_atime = vap->va_mtime;
1755 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1759 * The new np may have enough info for access
1760 * checks, make sure rucred and wucred are
1761 * initialized for read and write rpc's.
1764 if (np->n_rucred == NULL)
1765 np->n_rucred = crhold(cnp->cn_cred);
1766 if (np->n_wucred == NULL)
1767 np->n_wucred = crhold(cnp->cn_cred);
1772 VTONFS(dvp)->n_flag |= NLMODIFIED;
1774 VTONFS(dvp)->n_attrstamp = 0;
1775 lwkt_reltoken(&nmp->nm_token);
1780 * nfs file remove call
1781 * To try and make nfs semantics closer to ufs semantics, a file that has
1782 * other processes using the vnode is renamed instead of removed and then
1783 * removed later on the last close.
1785 * If a rename is not already in the works
1786 * call nfs_sillyrename() to set it up
1790 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1791 * struct componentname *a_cnp)
1794 nfs_remove(struct vop_old_remove_args *ap)
1796 struct vnode *vp = ap->a_vp;
1797 struct vnode *dvp = ap->a_dvp;
1798 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1799 struct componentname *cnp = ap->a_cnp;
1800 struct nfsnode *np = VTONFS(vp);
1804 lwkt_gettoken(&nmp->nm_token);
1806 if (VREFCNT(vp) < 1)
1807 panic("nfs_remove: bad v_refcnt");
1809 if (vp->v_type == VDIR) {
1811 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1812 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1814 * throw away biocache buffers, mainly to avoid
1815 * unnecessary delayed writes later.
1817 error = nfs_vinvalbuf(vp, 0, 1);
1820 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1821 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1823 * Kludge City: If the first reply to the remove rpc is lost..
1824 * the reply to the retransmitted request will be ENOENT
1825 * since the file was in fact removed
1826 * Therefore, we cheat and return success.
1828 if (error == ENOENT)
1830 } else if (!np->n_sillyrename) {
1831 error = nfs_sillyrename(dvp, vp, cnp);
1833 np->n_attrstamp = 0;
1834 lwkt_reltoken(&nmp->nm_token);
1840 * nfs file remove rpc called from nfs_inactive
1842 * NOTE: s_dvp can be VBAD during a forced unmount.
1845 nfs_removeit(struct sillyrename *sp)
1847 if (sp->s_dvp->v_type == VBAD)
1849 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1854 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1857 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1858 struct ucred *cred, struct thread *td)
1860 int error = 0, wccflag = NFSV3_WCCRATTR;
1861 struct nfsm_info info;
1864 info.v3 = NFS_ISV3(dvp);
1866 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1867 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1868 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1869 ERROROUT(nfsm_fhtom(&info, dvp));
1870 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1871 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1873 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1878 VTONFS(dvp)->n_flag |= NLMODIFIED;
1880 VTONFS(dvp)->n_attrstamp = 0;
1885 * nfs file rename call
1887 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1888 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1889 * struct vnode *a_tvp, struct componentname *a_tcnp)
1892 nfs_rename(struct vop_old_rename_args *ap)
1894 struct vnode *fvp = ap->a_fvp;
1895 struct vnode *tvp = ap->a_tvp;
1896 struct vnode *fdvp = ap->a_fdvp;
1897 struct vnode *tdvp = ap->a_tdvp;
1898 struct componentname *tcnp = ap->a_tcnp;
1899 struct componentname *fcnp = ap->a_fcnp;
1900 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1903 lwkt_gettoken(&nmp->nm_token);
1905 /* Check for cross-device rename */
1906 if ((fvp->v_mount != tdvp->v_mount) ||
1907 (tvp && (fvp->v_mount != tvp->v_mount))) {
1913 * We shouldn't have to flush fvp on rename for most server-side
1914 * filesystems as the file handle should not change. Unfortunately
1915 * the inode for some filesystems (msdosfs) might be tied to the
1916 * file name or directory position so to be completely safe
1917 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1920 * We must flush tvp on rename because it might become stale on the
1921 * server after the rename.
1923 if (nfs_flush_on_rename)
1924 VOP_FSYNC(fvp, MNT_WAIT, 0);
1926 VOP_FSYNC(tvp, MNT_WAIT, 0);
1929 * If the tvp exists and is in use, sillyrename it before doing the
1930 * rename of the new file over it.
1932 * XXX Can't sillyrename a directory.
1934 * We do not attempt to do any namecache purges in this old API
1935 * routine. The new API compat functions have access to the actual
1936 * namecache structures and will do it for us.
1938 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1939 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1946 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1947 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1951 lwkt_reltoken(&nmp->nm_token);
1961 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1963 if (error == ENOENT)
1969 * nfs file rename rpc called from nfs_remove() above
1972 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1973 struct sillyrename *sp)
1975 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1976 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1980 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1983 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1984 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1985 struct ucred *cred, struct thread *td)
1987 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1988 struct nfsm_info info;
1991 info.v3 = NFS_ISV3(fdvp);
1993 nfsstats.rpccnt[NFSPROC_RENAME]++;
1994 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1995 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1996 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1997 ERROROUT(nfsm_fhtom(&info, fdvp));
1998 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1999 ERROROUT(nfsm_fhtom(&info, tdvp));
2000 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2001 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2003 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2004 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2009 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2010 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2012 VTONFS(fdvp)->n_attrstamp = 0;
2014 VTONFS(tdvp)->n_attrstamp = 0;
2019 * nfs hard link create call
2021 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2022 * struct componentname *a_cnp)
2025 nfs_link(struct vop_old_link_args *ap)
2027 struct vnode *vp = ap->a_vp;
2028 struct vnode *tdvp = ap->a_tdvp;
2029 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2030 struct componentname *cnp = ap->a_cnp;
2031 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2032 struct nfsm_info info;
2034 if (vp->v_mount != tdvp->v_mount) {
2037 lwkt_gettoken(&nmp->nm_token);
2040 * The attribute cache may get out of sync with the server on link.
2041 * Pushing writes to the server before handle was inherited from
2042 * long long ago and it is unclear if we still need to do this.
2045 if (nfs_flush_on_hlink)
2046 VOP_FSYNC(vp, MNT_WAIT, 0);
2049 info.v3 = NFS_ISV3(vp);
2051 nfsstats.rpccnt[NFSPROC_LINK]++;
2052 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2053 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2054 nfsm_rndup(cnp->cn_namelen));
2055 ERROROUT(nfsm_fhtom(&info, vp));
2056 ERROROUT(nfsm_fhtom(&info, tdvp));
2057 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2059 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2060 cnp->cn_cred, &error));
2062 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2063 NFS_LATTR_NOSHRINK));
2064 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2069 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2071 VTONFS(vp)->n_attrstamp = 0;
2073 VTONFS(tdvp)->n_attrstamp = 0;
2075 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2077 if (error == EEXIST)
2079 lwkt_reltoken(&nmp->nm_token);
2084 * nfs symbolic link create call
2086 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2087 * struct componentname *a_cnp, struct vattr *a_vap,
2091 nfs_symlink(struct vop_old_symlink_args *ap)
2093 struct vnode *dvp = ap->a_dvp;
2094 struct vattr *vap = ap->a_vap;
2095 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2096 struct componentname *cnp = ap->a_cnp;
2097 struct nfsv2_sattr *sp;
2098 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2099 struct vnode *newvp = NULL;
2100 struct nfsm_info info;
2103 info.v3 = NFS_ISV3(dvp);
2104 lwkt_gettoken(&nmp->nm_token);
2106 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2107 slen = strlen(ap->a_target);
2108 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2109 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2110 nfsm_rndup(cnp->cn_namelen) +
2111 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2112 ERROROUT(nfsm_fhtom(&info, dvp));
2113 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2116 nfsm_v3attrbuild(&info, vap, FALSE);
2118 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2120 sp = nfsm_build(&info, NFSX_V2SATTR);
2121 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2122 sp->sa_uid = nfs_xdrneg1;
2123 sp->sa_gid = nfs_xdrneg1;
2124 sp->sa_size = nfs_xdrneg1;
2125 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2126 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2130 * Issue the NFS request and get the rpc response.
2132 * Only NFSv3 responses returning an error of 0 actually return
2133 * a file handle that can be converted into newvp without having
2134 * to do an extra lookup rpc.
2136 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2137 cnp->cn_cred, &error));
2140 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2142 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2146 * out code jumps -> here, mrep is also freed.
2154 * If we get an EEXIST error, silently convert it to no-error
2155 * in case of an NFS retry.
2157 if (error == EEXIST)
2161 * If we do not have (or no longer have) an error, and we could
2162 * not extract the newvp from the response due to the request being
2163 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2164 * to obtain a newvp to return.
2166 if (error == 0 && newvp == NULL) {
2167 struct nfsnode *np = NULL;
2169 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2170 cnp->cn_cred, cnp->cn_td, &np);
2180 VTONFS(dvp)->n_flag |= NLMODIFIED;
2182 VTONFS(dvp)->n_attrstamp = 0;
2183 lwkt_reltoken(&nmp->nm_token);
2191 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2192 * struct componentname *a_cnp, struct vattr *a_vap)
2195 nfs_mkdir(struct vop_old_mkdir_args *ap)
2197 struct vnode *dvp = ap->a_dvp;
2198 struct vattr *vap = ap->a_vap;
2199 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2200 struct componentname *cnp = ap->a_cnp;
2201 struct nfsv2_sattr *sp;
2202 struct nfsnode *np = NULL;
2203 struct vnode *newvp = NULL;
2205 int error = 0, wccflag = NFSV3_WCCRATTR;
2208 struct nfsm_info info;
2211 info.v3 = NFS_ISV3(dvp);
2212 lwkt_gettoken(&nmp->nm_token);
2214 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2215 lwkt_reltoken(&nmp->nm_token);
2218 len = cnp->cn_namelen;
2219 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2220 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2221 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2222 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2223 ERROROUT(nfsm_fhtom(&info, dvp));
2224 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2226 nfsm_v3attrbuild(&info, vap, FALSE);
2228 sp = nfsm_build(&info, NFSX_V2SATTR);
2229 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2230 sp->sa_uid = nfs_xdrneg1;
2231 sp->sa_gid = nfs_xdrneg1;
2232 sp->sa_size = nfs_xdrneg1;
2233 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2234 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2236 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2237 cnp->cn_cred, &error));
2239 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2242 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2247 VTONFS(dvp)->n_flag |= NLMODIFIED;
2249 VTONFS(dvp)->n_attrstamp = 0;
2251 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2252 * if we can succeed in looking up the directory.
2254 if (error == EEXIST || (!error && !gotvp)) {
2259 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2263 if (newvp->v_type != VDIR)
2273 lwkt_reltoken(&nmp->nm_token);
2278 * nfs remove directory call
2280 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2281 * struct componentname *a_cnp)
2284 nfs_rmdir(struct vop_old_rmdir_args *ap)
2286 struct vnode *vp = ap->a_vp;
2287 struct vnode *dvp = ap->a_dvp;
2288 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2289 struct componentname *cnp = ap->a_cnp;
2290 int error = 0, wccflag = NFSV3_WCCRATTR;
2291 struct nfsm_info info;
2294 info.v3 = NFS_ISV3(dvp);
2299 lwkt_gettoken(&nmp->nm_token);
2301 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2302 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2303 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2304 nfsm_rndup(cnp->cn_namelen));
2305 ERROROUT(nfsm_fhtom(&info, dvp));
2306 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2308 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2309 cnp->cn_cred, &error));
2311 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2316 VTONFS(dvp)->n_flag |= NLMODIFIED;
2318 VTONFS(dvp)->n_attrstamp = 0;
2320 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2322 if (error == ENOENT)
2324 lwkt_reltoken(&nmp->nm_token);
2332 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2335 nfs_readdir(struct vop_readdir_args *ap)
2337 struct vnode *vp = ap->a_vp;
2338 struct nfsnode *np = VTONFS(vp);
2339 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2340 struct uio *uio = ap->a_uio;
2344 if (vp->v_type != VDIR)
2347 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2351 lwkt_gettoken(&nmp->nm_token);
2354 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2355 * and then check that is still valid, or if this is an NQNFS mount
2356 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2357 * VOP_GETATTR() does not necessarily go to the wire.
2359 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2360 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2361 if (VOP_GETATTR(vp, &vattr) == 0 &&
2362 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2364 nfsstats.direofcache_hits++;
2370 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2371 * own cache coherency checks so we do not have to.
2373 tresid = uio->uio_resid;
2374 error = nfs_bioread(vp, uio, 0);
2376 if (!error && uio->uio_resid == tresid)
2377 nfsstats.direofcache_misses++;
2379 lwkt_reltoken(&nmp->nm_token);
2386 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2388 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2389 * offset/block and converts the nfs formatted directory entries for userland
2390 * consumption as well as deals with offsets into the middle of blocks.
2391 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2392 * be block-bounded. It must convert to cookies for the actual RPC.
2395 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2398 struct nfs_dirent *dp = NULL;
2403 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2404 struct nfsnode *dnp = VTONFS(vp);
2406 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2408 struct nfsm_info info;
2411 info.v3 = NFS_ISV3(vp);
2414 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2415 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2416 panic("nfs readdirrpc bad uio");
2420 * If there is no cookie, assume directory was stale.
2422 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2426 return (NFSERR_BAD_COOKIE);
2428 * Loop around doing readdir rpc's of size nm_readdirsize
2429 * truncated to a multiple of DIRBLKSIZ.
2430 * The stopping criteria is EOF or buffer full.
2432 while (more_dirs && bigenough) {
2433 nfsstats.rpccnt[NFSPROC_READDIR]++;
2434 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2435 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2436 ERROROUT(nfsm_fhtom(&info, vp));
2438 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2439 *tl++ = cookie.nfsuquad[0];
2440 *tl++ = cookie.nfsuquad[1];
2441 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2442 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2445 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2446 * WITH NFSv2!!! There's nothing I can really do
2447 * about it other than to hope the server supports
2450 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2451 *tl++ = cookie.nfsuquad[0];
2453 *tl = txdr_unsigned(nmp->nm_readdirsize);
2454 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2456 nfs_vpcred(vp, ND_READ), &error));
2458 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2459 NFS_LATTR_NOSHRINK));
2460 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2461 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2462 dnp->n_cookieverf.nfsuquad[1] = *tl;
2464 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2465 more_dirs = fxdr_unsigned(int, *tl);
2467 /* loop thru the dir entries, converting them to std form */
2468 while (more_dirs && bigenough) {
2470 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2471 fileno = fxdr_hyper(tl);
2472 len = fxdr_unsigned(int, *(tl + 2));
2474 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2475 fileno = fxdr_unsigned(u_quad_t, *tl++);
2476 len = fxdr_unsigned(int, *tl);
2478 if (len <= 0 || len > NFS_MAXNAMLEN) {
2486 * len is the number of bytes in the path element
2487 * name, not including the \0 termination.
2489 * tlen is the number of bytes w have to reserve for
2490 * the path element name.
2492 tlen = nfsm_rndup(len);
2494 tlen += 4; /* To ensure null termination */
2497 * If the entry would cross a DIRBLKSIZ boundary,
2498 * extend the previous nfs_dirent to cover the
2501 left = DIRBLKSIZ - blksiz;
2502 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2503 dp->nfs_reclen += left;
2504 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2505 uiop->uio_iov->iov_len -= left;
2506 uiop->uio_offset += left;
2507 uiop->uio_resid -= left;
2510 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2513 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2514 dp->nfs_ino = fileno;
2515 dp->nfs_namlen = len;
2516 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2517 dp->nfs_type = DT_UNKNOWN;
2518 blksiz += dp->nfs_reclen;
2519 if (blksiz == DIRBLKSIZ)
2521 uiop->uio_offset += sizeof(struct nfs_dirent);
2522 uiop->uio_resid -= sizeof(struct nfs_dirent);
2523 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2524 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2525 ERROROUT(nfsm_mtouio(&info, uiop, len));
2528 * The uiop has advanced by nfs_dirent + len
2529 * but really needs to advance by
2532 cp = uiop->uio_iov->iov_base;
2534 *cp = '\0'; /* null terminate */
2535 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2536 uiop->uio_iov->iov_len -= tlen;
2537 uiop->uio_offset += tlen;
2538 uiop->uio_resid -= tlen;
2541 * NFS strings must be rounded up (nfsm_myouio
2542 * handled that in the bigenough case).
2544 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2547 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2549 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2553 * If we were able to accomodate the last entry,
2554 * get the cookie for the next one. Otherwise
2555 * hold-over the cookie for the one we were not
2556 * able to accomodate.
2559 cookie.nfsuquad[0] = *tl++;
2561 cookie.nfsuquad[1] = *tl++;
2562 } else if (info.v3) {
2567 more_dirs = fxdr_unsigned(int, *tl);
2570 * If at end of rpc data, get the eof boolean
2573 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2574 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2580 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2581 * by increasing d_reclen for the last record.
2584 left = DIRBLKSIZ - blksiz;
2585 dp->nfs_reclen += left;
2586 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2587 uiop->uio_iov->iov_len -= left;
2588 uiop->uio_offset += left;
2589 uiop->uio_resid -= left;
2594 * We hit the end of the directory, update direofoffset.
2596 dnp->n_direofoffset = uiop->uio_offset;
2599 * There is more to go, insert the link cookie so the
2600 * next block can be read.
2602 if (uiop->uio_resid > 0)
2603 kprintf("EEK! readdirrpc resid > 0\n");
2604 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2612 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2615 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2618 struct nfs_dirent *dp;
2620 struct vnode *newvp;
2622 caddr_t dpossav1, dpossav2;
2624 struct mbuf *mdsav1, *mdsav2;
2626 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2627 struct nfsnode *dnp = VTONFS(vp), *np;
2630 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2631 int attrflag, fhsize;
2632 struct nchandle nch;
2633 struct nchandle dnch;
2634 struct nlcomponent nlc;
2635 struct nfsm_info info;
2644 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2645 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2646 panic("nfs readdirplusrpc bad uio");
2649 * Obtain the namecache record for the directory so we have something
2650 * to use as a basis for creating the entries. This function will
2651 * return a held (but not locked) ncp. The ncp may be disconnected
2652 * from the tree and cannot be used for upward traversals, and the
2653 * ncp may be unnamed. Note that other unrelated operations may
2654 * cause the ncp to be named at any time.
2656 * We have to lock the ncp to prevent a lock order reversal when
2657 * rdirplus does nlookups of the children, because the vnode is
2658 * locked and has to stay that way.
2660 cache_fromdvp(vp, NULL, 0, &dnch);
2661 bzero(&nlc, sizeof(nlc));
2665 * If there is no cookie, assume directory was stale.
2667 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2673 return (NFSERR_BAD_COOKIE);
2677 * Loop around doing readdir rpc's of size nm_readdirsize
2678 * truncated to a multiple of DIRBLKSIZ.
2679 * The stopping criteria is EOF or buffer full.
2681 while (more_dirs && bigenough) {
2682 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2683 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2684 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2685 ERROROUT(nfsm_fhtom(&info, vp));
2686 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2687 *tl++ = cookie.nfsuquad[0];
2688 *tl++ = cookie.nfsuquad[1];
2689 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2690 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2691 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2692 *tl = txdr_unsigned(nmp->nm_rsize);
2693 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2695 nfs_vpcred(vp, ND_READ), &error));
2696 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2697 NFS_LATTR_NOSHRINK));
2698 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2699 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2700 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2701 more_dirs = fxdr_unsigned(int, *tl);
2703 /* loop thru the dir entries, doctoring them to 4bsd form */
2704 while (more_dirs && bigenough) {
2705 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2706 fileno = fxdr_hyper(tl);
2707 len = fxdr_unsigned(int, *(tl + 2));
2708 if (len <= 0 || len > NFS_MAXNAMLEN) {
2714 tlen = nfsm_rndup(len);
2716 tlen += 4; /* To ensure null termination*/
2717 left = DIRBLKSIZ - blksiz;
2718 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2719 dp->nfs_reclen += left;
2720 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2721 uiop->uio_iov->iov_len -= left;
2722 uiop->uio_offset += left;
2723 uiop->uio_resid -= left;
2726 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2729 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2730 dp->nfs_ino = fileno;
2731 dp->nfs_namlen = len;
2732 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2733 dp->nfs_type = DT_UNKNOWN;
2734 blksiz += dp->nfs_reclen;
2735 if (blksiz == DIRBLKSIZ)
2737 uiop->uio_offset += sizeof(struct nfs_dirent);
2738 uiop->uio_resid -= sizeof(struct nfs_dirent);
2739 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2740 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2741 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2742 nlc.nlc_namelen = len;
2743 ERROROUT(nfsm_mtouio(&info, uiop, len));
2744 cp = uiop->uio_iov->iov_base;
2747 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2748 uiop->uio_iov->iov_len -= tlen;
2749 uiop->uio_offset += tlen;
2750 uiop->uio_resid -= tlen;
2752 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2754 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2756 cookie.nfsuquad[0] = *tl++;
2757 cookie.nfsuquad[1] = *tl++;
2763 * Since the attributes are before the file handle
2764 * (sigh), we must skip over the attributes and then
2765 * come back and get them.
2767 attrflag = fxdr_unsigned(int, *tl);
2769 dpossav1 = info.dpos;
2771 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2772 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2773 doit = fxdr_unsigned(int, *tl);
2775 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2777 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2778 !NFS_CMPFH(dnp, fhp, fhsize)
2782 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2783 nlc.nlc_namelen, nlc.nlc_namelen,
2787 * This is a bit hokey but there isn't
2788 * much we can do about it. We can't
2789 * hold the directory vp locked while
2790 * doing lookups and gets.
2792 nch = cache_nlookup_nonblock(&dnch, &nlc);
2793 if (nch.ncp == NULL)
2795 cache_setunresolved(&nch);
2796 error = nfs_nget_nonblock(vp->v_mount, fhp,
2804 dpossav2 = info.dpos;
2805 info.dpos = dpossav1;
2808 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2809 info.dpos = dpossav2;
2812 IFTODT(VTTOIF(np->n_vattr.va_type));
2813 nfs_cache_setvp(&nch, newvp,
2814 nfspos_cache_timeout);
2822 kprintf("Warning: NFS/rddirplus, "
2823 "UNABLE TO ENTER %*.*s\n",
2824 nlc.nlc_namelen, nlc.nlc_namelen,
2830 /* Just skip over the file handle */
2831 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2832 i = fxdr_unsigned(int, *tl);
2833 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2835 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2836 more_dirs = fxdr_unsigned(int, *tl);
2839 * If at end of rpc data, get the eof boolean
2842 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2843 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2849 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2850 * by increasing d_reclen for the last record.
2853 left = DIRBLKSIZ - blksiz;
2854 dp->nfs_reclen += left;
2855 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2856 uiop->uio_iov->iov_len -= left;
2857 uiop->uio_offset += left;
2858 uiop->uio_resid -= left;
2862 * We are now either at the end of the directory or have filled the
2866 dnp->n_direofoffset = uiop->uio_offset;
2868 if (uiop->uio_resid > 0)
2869 kprintf("EEK! readdirplusrpc resid > 0\n");
2870 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2874 if (newvp != NULLVP) {
2887 * Silly rename. To make the NFS filesystem that is stateless look a little
2888 * more like the "ufs" a remove of an active vnode is translated to a rename
2889 * to a funny looking filename that is removed by nfs_inactive on the
2890 * nfsnode. There is the potential for another process on a different client
2891 * to create the same funny name between the nfs_lookitup() fails and the
2892 * nfs_rename() completes, but...
2895 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2897 struct sillyrename *sp;
2902 * We previously purged dvp instead of vp. I don't know why, it
2903 * completely destroys performance. We can't do it anyway with the
2904 * new VFS API since we would be breaking the namecache topology.
2906 cache_purge(vp); /* XXX */
2909 if (vp->v_type == VDIR)
2910 panic("nfs: sillyrename dir");
2912 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2913 sp->s_cred = crdup(cnp->cn_cred);
2917 /* Fudge together a funny name */
2918 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2919 (int)(intptr_t)cnp->cn_td);
2921 /* Try lookitups until we get one that isn't there */
2922 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2923 cnp->cn_td, NULL) == 0) {
2925 if (sp->s_name[4] > 'z') {
2930 error = nfs_renameit(dvp, cnp, sp);
2933 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2935 np->n_sillyrename = sp;
2940 kfree((caddr_t)sp, M_NFSREQ);
2945 * Look up a file name and optionally either update the file handle or
2946 * allocate an nfsnode, depending on the value of npp.
2947 * npp == NULL --> just do the lookup
2948 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2950 * *npp != NULL --> update the file handle in the vnode
2953 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2954 struct thread *td, struct nfsnode **npp)
2956 struct vnode *newvp = NULL;
2957 struct nfsnode *np, *dnp = VTONFS(dvp);
2958 int error = 0, fhlen, attrflag;
2960 struct nfsm_info info;
2963 info.v3 = NFS_ISV3(dvp);
2965 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2966 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2967 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2968 ERROROUT(nfsm_fhtom(&info, dvp));
2969 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2970 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2971 if (npp && !error) {
2972 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2975 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2976 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2977 np->n_fhp = &np->n_fh;
2978 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2979 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2980 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2981 np->n_fhsize = fhlen;
2983 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2987 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2996 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2997 NFS_LATTR_NOSHRINK));
2998 if (!attrflag && *npp == NULL) {
3008 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3014 if (npp && *npp == NULL) {
3029 * Nfs Version 3 commit rpc
3031 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3035 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3037 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3038 int error = 0, wccflag = NFSV3_WCCRATTR;
3039 struct nfsm_info info;
3045 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3047 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3048 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3049 ERROROUT(nfsm_fhtom(&info, vp));
3050 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3051 txdr_hyper(offset, tl);
3053 *tl = txdr_unsigned(cnt);
3054 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3055 nfs_vpcred(vp, ND_WRITE), &error));
3056 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3058 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3059 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3060 NFSX_V3WRITEVERF)) {
3061 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3063 error = NFSERR_STALEWRITEVERF;
3074 * - make nfs_bmap() essentially a no-op that does no translation
3075 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3076 * (Maybe I could use the process's page mapping, but I was concerned that
3077 * Kernel Write might not be enabled and also figured copyout() would do
3078 * a lot more work than bcopy() and also it currently happens in the
3079 * context of the swapper process (2).
3081 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3082 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3085 nfs_bmap(struct vop_bmap_args *ap)
3087 /* no token lock required */
3088 if (ap->a_doffsetp != NULL)
3089 *ap->a_doffsetp = ap->a_loffset;
3090 if (ap->a_runp != NULL)
3092 if (ap->a_runb != NULL)
3101 nfs_strategy(struct vop_strategy_args *ap)
3103 struct bio *bio = ap->a_bio;
3105 struct buf *bp __debugvar = bio->bio_buf;
3106 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3110 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3111 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3112 KASSERT(BUF_REFCNT(bp) > 0,
3113 ("nfs_strategy: buffer %p not locked", bp));
3115 if (bio->bio_flags & BIO_SYNC)
3116 td = curthread; /* XXX */
3120 lwkt_gettoken(&nmp->nm_token);
3123 * We probably don't need to push an nbio any more since no
3124 * block conversion is required due to the use of 64 bit byte
3125 * offsets, but do it anyway.
3127 * NOTE: When NFS callers itself via this strategy routines and
3128 * sets up a synchronous I/O, it expects the I/O to run
3129 * synchronously (its bio_done routine just assumes it),
3130 * so for now we have to honor the bit.
3132 nbio = push_bio(bio);
3133 nbio->bio_offset = bio->bio_offset;
3134 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3137 * If the op is asynchronous and an i/o daemon is waiting
3138 * queue the request, wake it up and wait for completion
3139 * otherwise just do it ourselves.
3141 if (bio->bio_flags & BIO_SYNC) {
3142 error = nfs_doio(ap->a_vp, nbio, td);
3144 nfs_asyncio(ap->a_vp, nbio);
3147 lwkt_reltoken(&nmp->nm_token);
3155 * NB Currently unsupported.
3157 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3161 nfs_mmap(struct vop_mmap_args *ap)
3163 /* no token lock required */
3168 * fsync vnode op. Just call nfs_flush() with commit == 1.
3170 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3174 nfs_fsync(struct vop_fsync_args *ap)
3176 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3179 lwkt_gettoken(&nmp->nm_token);
3182 * NOTE: Because attributes are set synchronously we currently
3183 * do not have to implement vsetisdirty()/vclrisdirty().
3185 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3187 lwkt_reltoken(&nmp->nm_token);
3193 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3194 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3195 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3196 * set the buffer contains data that has already been written to the server
3197 * and which now needs a commit RPC.
3199 * If commit is 0 we only take one pass and only flush buffers containing new
3202 * If commit is 1 we take two passes, issuing a commit RPC in the second
3205 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3206 * to completely flush all pending data.
3208 * Note that the RB_SCAN code properly handles the case where the
3209 * callback might block and directly or indirectly (another thread) cause
3210 * the RB tree to change.
3213 #ifndef NFS_COMMITBVECSIZ
3214 #define NFS_COMMITBVECSIZ 16
3217 struct nfs_flush_info {
3218 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3225 struct buf *bvary[NFS_COMMITBVECSIZ];
3231 static int nfs_flush_bp(struct buf *bp, void *data);
3232 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3235 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3237 struct nfsnode *np = VTONFS(vp);
3238 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3239 struct nfs_flush_info info;
3242 bzero(&info, sizeof(info));
3245 info.waitfor = waitfor;
3246 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3248 lwkt_gettoken(&vp->v_token);
3254 info.mode = NFI_FLUSHNEW;
3255 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3256 nfs_flush_bp, &info);
3259 * Take a second pass if committing and no error occured.
3260 * Clean up any left over collection (whether an error
3263 if (commit && error == 0) {
3264 info.mode = NFI_COMMIT;
3265 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3266 nfs_flush_bp, &info);
3268 error = nfs_flush_docommit(&info, error);
3272 * Wait for pending I/O to complete before checking whether
3273 * any further dirty buffers exist.
3275 while (waitfor == MNT_WAIT &&
3276 bio_track_active(&vp->v_track_write)) {
3277 error = bio_track_wait(&vp->v_track_write,
3278 info.slpflag, info.slptimeo);
3281 * We have to be able to break out if this
3282 * is an 'intr' mount.
3284 if (nfs_sigintr(nmp, NULL, td)) {
3290 * Since we do not process pending signals,
3291 * once we get a PCATCH our tsleep() will no
3292 * longer sleep, switch to a fixed timeout
3295 if (info.slpflag == PCATCH) {
3297 info.slptimeo = 2 * hz;
3304 * Loop if we are flushing synchronous as well as committing,
3305 * and dirty buffers are still present. Otherwise we might livelock.
3307 } while (waitfor == MNT_WAIT && commit &&
3308 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3311 * The callbacks have to return a negative error to terminate the
3318 * Deal with any error collection
3320 if (np->n_flag & NWRITEERR) {
3321 error = np->n_error;
3322 np->n_flag &= ~NWRITEERR;
3324 lwkt_reltoken(&vp->v_token);
3330 nfs_flush_bp(struct buf *bp, void *data)
3332 struct nfs_flush_info *info = data;
3338 switch(info->mode) {
3340 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3341 if (error && info->loops && info->waitfor == MNT_WAIT) {
3342 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3344 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3345 if (info->slpflag & PCATCH)
3346 lkflags |= LK_PCATCH;
3347 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3353 * Ignore locking errors
3361 * The buffer may have changed out from under us, even if
3362 * we did not block (MPSAFE). Check again now that it is
3365 if (bp->b_vp == info->vp &&
3366 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3375 * Only process buffers in need of a commit which we can
3376 * immediately lock. This may prevent a buffer from being
3377 * committed, but the normal flush loop will block on the
3378 * same buffer so we shouldn't get into an endless loop.
3380 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3381 (B_DELWRI | B_NEEDCOMMIT)) {
3384 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3388 * We must recheck after successfully locking the buffer.
3390 if (bp->b_vp != info->vp ||
3391 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3392 (B_DELWRI | B_NEEDCOMMIT)) {
3398 * NOTE: storing the bp in the bvary[] basically sets
3399 * it up for a commit operation.
3401 * We must call vfs_busy_pages() now so the commit operation
3402 * is interlocked with user modifications to memory mapped
3403 * pages. The b_dirtyoff/b_dirtyend range is not correct
3404 * until after the pages have been busied.
3406 * Note: to avoid loopback deadlocks, we do not
3407 * assign b_runningbufspace.
3410 bp->b_cmd = BUF_CMD_WRITE;
3411 vfs_busy_pages(bp->b_vp, bp);
3412 info->bvary[info->bvsize] = bp;
3413 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3414 if (info->bvsize == 0 || toff < info->beg_off)
3415 info->beg_off = toff;
3416 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3417 if (info->bvsize == 0 || toff > info->end_off)
3418 info->end_off = toff;
3420 if (info->bvsize == NFS_COMMITBVECSIZ) {
3421 error = nfs_flush_docommit(info, 0);
3422 KKASSERT(info->bvsize == 0);
3430 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3440 if (info->bvsize > 0) {
3442 * Commit data on the server, as required. Note that
3443 * nfs_commit will use the vnode's cred for the commit.
3444 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3446 bytes = info->end_off - info->beg_off;
3447 if (bytes > 0x40000000)
3452 retv = nfs_commitrpc_uio(vp, info->beg_off,
3453 (int)bytes, info->td);
3454 if (retv == NFSERR_STALEWRITEVERF)
3455 nfs_clearcommit(vp->v_mount);
3459 * Now, either mark the blocks I/O done or mark the
3460 * blocks dirty, depending on whether the commit
3463 for (i = 0; i < info->bvsize; ++i) {
3464 bp = info->bvary[i];
3465 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3467 * Either an error or the original
3468 * vfs_busy_pages() cleared B_NEEDCOMMIT
3469 * due to finding new dirty VM pages in
3472 * Leave B_DELWRI intact.
3474 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3475 vfs_unbusy_pages(bp);
3476 bp->b_cmd = BUF_CMD_DONE;
3480 * Success, remove B_DELWRI ( bundirty() ).
3482 * b_dirtyoff/b_dirtyend seem to be NFS
3483 * specific. We should probably move that
3484 * into bundirty(). XXX
3486 * We are faking an I/O write, we have to
3487 * start the transaction in order to
3488 * immediately biodone() it.
3491 bp->b_flags &= ~B_ERROR;
3492 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3493 bp->b_dirtyoff = bp->b_dirtyend = 0;
3494 biodone(&bp->b_bio1);
3503 * NFS advisory byte-level locks.
3504 * Currently unsupported.
3506 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3510 nfs_advlock(struct vop_advlock_args *ap)
3512 struct nfsnode *np = VTONFS(ap->a_vp);
3514 /* no token lock currently required */
3516 * The following kludge is to allow diskless support to work
3517 * until a real NFS lockd is implemented. Basically, just pretend
3518 * that this is a local lock.
3520 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3524 * Print out the contents of an nfsnode.
3526 * nfs_print(struct vnode *a_vp)
3529 nfs_print(struct vop_print_args *ap)
3531 struct vnode *vp = ap->a_vp;
3532 struct nfsnode *np = VTONFS(vp);
3534 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3535 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3536 if (vp->v_type == VFIFO)
3543 * nfs special file access vnode op.
3545 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3548 nfs_laccess(struct vop_access_args *ap)
3550 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3554 lwkt_gettoken(&nmp->nm_token);
3555 error = VOP_GETATTR(ap->a_vp, &vattr);
3557 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3560 lwkt_reltoken(&nmp->nm_token);
3566 * Read wrapper for fifos.
3568 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3569 * struct ucred *a_cred)
3572 nfsfifo_read(struct vop_read_args *ap)
3574 struct nfsnode *np = VTONFS(ap->a_vp);
3576 /* no token access required */
3581 getnanotime(&np->n_atim);
3582 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3586 * Write wrapper for fifos.
3588 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3589 * struct ucred *a_cred)
3592 nfsfifo_write(struct vop_write_args *ap)
3594 struct nfsnode *np = VTONFS(ap->a_vp);
3596 /* no token access required */
3601 getnanotime(&np->n_mtim);
3602 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3606 * Close wrapper for fifos.
3608 * Update the times on the nfsnode then do fifo close.
3610 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3613 nfsfifo_close(struct vop_close_args *ap)
3615 struct vnode *vp = ap->a_vp;
3616 struct nfsnode *np = VTONFS(vp);
3620 /* no token access required */
3622 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3623 if (np->n_flag & (NACC | NUPD)) {
3625 if (np->n_flag & NACC)
3627 if (np->n_flag & NUPD)
3630 if (VREFCNT(vp) == 1 &&
3631 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3633 if (np->n_flag & NACC)
3634 vattr.va_atime = np->n_atim;
3635 if (np->n_flag & NUPD)
3636 vattr.va_mtime = np->n_mtim;
3637 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3640 return (VOCALL(&fifo_vnode_vops, &ap->a_head));