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
1843 nfs_removeit(struct sillyrename *sp)
1845 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1850 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1853 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1854 struct ucred *cred, struct thread *td)
1856 int error = 0, wccflag = NFSV3_WCCRATTR;
1857 struct nfsm_info info;
1860 info.v3 = NFS_ISV3(dvp);
1862 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1863 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1864 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1865 ERROROUT(nfsm_fhtom(&info, dvp));
1866 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1867 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1869 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1874 VTONFS(dvp)->n_flag |= NLMODIFIED;
1876 VTONFS(dvp)->n_attrstamp = 0;
1881 * nfs file rename call
1883 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1884 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1885 * struct vnode *a_tvp, struct componentname *a_tcnp)
1888 nfs_rename(struct vop_old_rename_args *ap)
1890 struct vnode *fvp = ap->a_fvp;
1891 struct vnode *tvp = ap->a_tvp;
1892 struct vnode *fdvp = ap->a_fdvp;
1893 struct vnode *tdvp = ap->a_tdvp;
1894 struct componentname *tcnp = ap->a_tcnp;
1895 struct componentname *fcnp = ap->a_fcnp;
1896 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1899 lwkt_gettoken(&nmp->nm_token);
1901 /* Check for cross-device rename */
1902 if ((fvp->v_mount != tdvp->v_mount) ||
1903 (tvp && (fvp->v_mount != tvp->v_mount))) {
1909 * We shouldn't have to flush fvp on rename for most server-side
1910 * filesystems as the file handle should not change. Unfortunately
1911 * the inode for some filesystems (msdosfs) might be tied to the
1912 * file name or directory position so to be completely safe
1913 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1916 * We must flush tvp on rename because it might become stale on the
1917 * server after the rename.
1919 if (nfs_flush_on_rename)
1920 VOP_FSYNC(fvp, MNT_WAIT, 0);
1922 VOP_FSYNC(tvp, MNT_WAIT, 0);
1925 * If the tvp exists and is in use, sillyrename it before doing the
1926 * rename of the new file over it.
1928 * XXX Can't sillyrename a directory.
1930 * We do not attempt to do any namecache purges in this old API
1931 * routine. The new API compat functions have access to the actual
1932 * namecache structures and will do it for us.
1934 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1935 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1942 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1943 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1947 lwkt_reltoken(&nmp->nm_token);
1957 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1959 if (error == ENOENT)
1965 * nfs file rename rpc called from nfs_remove() above
1968 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1969 struct sillyrename *sp)
1971 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1972 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1976 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1979 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1980 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1981 struct ucred *cred, struct thread *td)
1983 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1984 struct nfsm_info info;
1987 info.v3 = NFS_ISV3(fdvp);
1989 nfsstats.rpccnt[NFSPROC_RENAME]++;
1990 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1991 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1992 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1993 ERROROUT(nfsm_fhtom(&info, fdvp));
1994 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1995 ERROROUT(nfsm_fhtom(&info, tdvp));
1996 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1997 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1999 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2000 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2005 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2006 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2008 VTONFS(fdvp)->n_attrstamp = 0;
2010 VTONFS(tdvp)->n_attrstamp = 0;
2015 * nfs hard link create call
2017 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2018 * struct componentname *a_cnp)
2021 nfs_link(struct vop_old_link_args *ap)
2023 struct vnode *vp = ap->a_vp;
2024 struct vnode *tdvp = ap->a_tdvp;
2025 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2026 struct componentname *cnp = ap->a_cnp;
2027 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2028 struct nfsm_info info;
2030 if (vp->v_mount != tdvp->v_mount) {
2033 lwkt_gettoken(&nmp->nm_token);
2036 * The attribute cache may get out of sync with the server on link.
2037 * Pushing writes to the server before handle was inherited from
2038 * long long ago and it is unclear if we still need to do this.
2041 if (nfs_flush_on_hlink)
2042 VOP_FSYNC(vp, MNT_WAIT, 0);
2045 info.v3 = NFS_ISV3(vp);
2047 nfsstats.rpccnt[NFSPROC_LINK]++;
2048 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2049 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2050 nfsm_rndup(cnp->cn_namelen));
2051 ERROROUT(nfsm_fhtom(&info, vp));
2052 ERROROUT(nfsm_fhtom(&info, tdvp));
2053 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2055 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2056 cnp->cn_cred, &error));
2058 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2059 NFS_LATTR_NOSHRINK));
2060 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2065 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2067 VTONFS(vp)->n_attrstamp = 0;
2069 VTONFS(tdvp)->n_attrstamp = 0;
2071 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2073 if (error == EEXIST)
2075 lwkt_reltoken(&nmp->nm_token);
2080 * nfs symbolic link create call
2082 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2083 * struct componentname *a_cnp, struct vattr *a_vap,
2087 nfs_symlink(struct vop_old_symlink_args *ap)
2089 struct vnode *dvp = ap->a_dvp;
2090 struct vattr *vap = ap->a_vap;
2091 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2092 struct componentname *cnp = ap->a_cnp;
2093 struct nfsv2_sattr *sp;
2094 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2095 struct vnode *newvp = NULL;
2096 struct nfsm_info info;
2099 info.v3 = NFS_ISV3(dvp);
2100 lwkt_gettoken(&nmp->nm_token);
2102 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2103 slen = strlen(ap->a_target);
2104 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2105 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2106 nfsm_rndup(cnp->cn_namelen) +
2107 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2108 ERROROUT(nfsm_fhtom(&info, dvp));
2109 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2112 nfsm_v3attrbuild(&info, vap, FALSE);
2114 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2116 sp = nfsm_build(&info, NFSX_V2SATTR);
2117 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2118 sp->sa_uid = nfs_xdrneg1;
2119 sp->sa_gid = nfs_xdrneg1;
2120 sp->sa_size = nfs_xdrneg1;
2121 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2122 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2126 * Issue the NFS request and get the rpc response.
2128 * Only NFSv3 responses returning an error of 0 actually return
2129 * a file handle that can be converted into newvp without having
2130 * to do an extra lookup rpc.
2132 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2133 cnp->cn_cred, &error));
2136 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2138 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2142 * out code jumps -> here, mrep is also freed.
2150 * If we get an EEXIST error, silently convert it to no-error
2151 * in case of an NFS retry.
2153 if (error == EEXIST)
2157 * If we do not have (or no longer have) an error, and we could
2158 * not extract the newvp from the response due to the request being
2159 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2160 * to obtain a newvp to return.
2162 if (error == 0 && newvp == NULL) {
2163 struct nfsnode *np = NULL;
2165 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2166 cnp->cn_cred, cnp->cn_td, &np);
2176 VTONFS(dvp)->n_flag |= NLMODIFIED;
2178 VTONFS(dvp)->n_attrstamp = 0;
2179 lwkt_reltoken(&nmp->nm_token);
2187 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2188 * struct componentname *a_cnp, struct vattr *a_vap)
2191 nfs_mkdir(struct vop_old_mkdir_args *ap)
2193 struct vnode *dvp = ap->a_dvp;
2194 struct vattr *vap = ap->a_vap;
2195 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2196 struct componentname *cnp = ap->a_cnp;
2197 struct nfsv2_sattr *sp;
2198 struct nfsnode *np = NULL;
2199 struct vnode *newvp = NULL;
2201 int error = 0, wccflag = NFSV3_WCCRATTR;
2204 struct nfsm_info info;
2207 info.v3 = NFS_ISV3(dvp);
2208 lwkt_gettoken(&nmp->nm_token);
2210 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2211 lwkt_reltoken(&nmp->nm_token);
2214 len = cnp->cn_namelen;
2215 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2216 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2217 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2218 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2219 ERROROUT(nfsm_fhtom(&info, dvp));
2220 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2222 nfsm_v3attrbuild(&info, vap, FALSE);
2224 sp = nfsm_build(&info, NFSX_V2SATTR);
2225 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2226 sp->sa_uid = nfs_xdrneg1;
2227 sp->sa_gid = nfs_xdrneg1;
2228 sp->sa_size = nfs_xdrneg1;
2229 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2230 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2232 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2233 cnp->cn_cred, &error));
2235 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2238 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2243 VTONFS(dvp)->n_flag |= NLMODIFIED;
2245 VTONFS(dvp)->n_attrstamp = 0;
2247 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2248 * if we can succeed in looking up the directory.
2250 if (error == EEXIST || (!error && !gotvp)) {
2255 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2259 if (newvp->v_type != VDIR)
2269 lwkt_reltoken(&nmp->nm_token);
2274 * nfs remove directory call
2276 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2277 * struct componentname *a_cnp)
2280 nfs_rmdir(struct vop_old_rmdir_args *ap)
2282 struct vnode *vp = ap->a_vp;
2283 struct vnode *dvp = ap->a_dvp;
2284 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2285 struct componentname *cnp = ap->a_cnp;
2286 int error = 0, wccflag = NFSV3_WCCRATTR;
2287 struct nfsm_info info;
2290 info.v3 = NFS_ISV3(dvp);
2295 lwkt_gettoken(&nmp->nm_token);
2297 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2298 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2299 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2300 nfsm_rndup(cnp->cn_namelen));
2301 ERROROUT(nfsm_fhtom(&info, dvp));
2302 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2304 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2305 cnp->cn_cred, &error));
2307 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2312 VTONFS(dvp)->n_flag |= NLMODIFIED;
2314 VTONFS(dvp)->n_attrstamp = 0;
2316 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2318 if (error == ENOENT)
2320 lwkt_reltoken(&nmp->nm_token);
2328 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2331 nfs_readdir(struct vop_readdir_args *ap)
2333 struct vnode *vp = ap->a_vp;
2334 struct nfsnode *np = VTONFS(vp);
2335 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2336 struct uio *uio = ap->a_uio;
2340 if (vp->v_type != VDIR)
2343 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2347 lwkt_gettoken(&nmp->nm_token);
2350 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2351 * and then check that is still valid, or if this is an NQNFS mount
2352 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2353 * VOP_GETATTR() does not necessarily go to the wire.
2355 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2356 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2357 if (VOP_GETATTR(vp, &vattr) == 0 &&
2358 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2360 nfsstats.direofcache_hits++;
2366 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2367 * own cache coherency checks so we do not have to.
2369 tresid = uio->uio_resid;
2370 error = nfs_bioread(vp, uio, 0);
2372 if (!error && uio->uio_resid == tresid)
2373 nfsstats.direofcache_misses++;
2375 lwkt_reltoken(&nmp->nm_token);
2382 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2384 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2385 * offset/block and converts the nfs formatted directory entries for userland
2386 * consumption as well as deals with offsets into the middle of blocks.
2387 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2388 * be block-bounded. It must convert to cookies for the actual RPC.
2391 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2394 struct nfs_dirent *dp = NULL;
2399 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2400 struct nfsnode *dnp = VTONFS(vp);
2402 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2404 struct nfsm_info info;
2407 info.v3 = NFS_ISV3(vp);
2410 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2411 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2412 panic("nfs readdirrpc bad uio");
2416 * If there is no cookie, assume directory was stale.
2418 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2422 return (NFSERR_BAD_COOKIE);
2424 * Loop around doing readdir rpc's of size nm_readdirsize
2425 * truncated to a multiple of DIRBLKSIZ.
2426 * The stopping criteria is EOF or buffer full.
2428 while (more_dirs && bigenough) {
2429 nfsstats.rpccnt[NFSPROC_READDIR]++;
2430 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2431 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2432 ERROROUT(nfsm_fhtom(&info, vp));
2434 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2435 *tl++ = cookie.nfsuquad[0];
2436 *tl++ = cookie.nfsuquad[1];
2437 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2438 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2441 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2442 * WITH NFSv2!!! There's nothing I can really do
2443 * about it other than to hope the server supports
2446 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2447 *tl++ = cookie.nfsuquad[0];
2449 *tl = txdr_unsigned(nmp->nm_readdirsize);
2450 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2452 nfs_vpcred(vp, ND_READ), &error));
2454 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2455 NFS_LATTR_NOSHRINK));
2456 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2457 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2458 dnp->n_cookieverf.nfsuquad[1] = *tl;
2460 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2461 more_dirs = fxdr_unsigned(int, *tl);
2463 /* loop thru the dir entries, converting them to std form */
2464 while (more_dirs && bigenough) {
2466 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2467 fileno = fxdr_hyper(tl);
2468 len = fxdr_unsigned(int, *(tl + 2));
2470 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2471 fileno = fxdr_unsigned(u_quad_t, *tl++);
2472 len = fxdr_unsigned(int, *tl);
2474 if (len <= 0 || len > NFS_MAXNAMLEN) {
2482 * len is the number of bytes in the path element
2483 * name, not including the \0 termination.
2485 * tlen is the number of bytes w have to reserve for
2486 * the path element name.
2488 tlen = nfsm_rndup(len);
2490 tlen += 4; /* To ensure null termination */
2493 * If the entry would cross a DIRBLKSIZ boundary,
2494 * extend the previous nfs_dirent to cover the
2497 left = DIRBLKSIZ - blksiz;
2498 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2499 dp->nfs_reclen += left;
2500 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2501 uiop->uio_iov->iov_len -= left;
2502 uiop->uio_offset += left;
2503 uiop->uio_resid -= left;
2506 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2509 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2510 dp->nfs_ino = fileno;
2511 dp->nfs_namlen = len;
2512 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2513 dp->nfs_type = DT_UNKNOWN;
2514 blksiz += dp->nfs_reclen;
2515 if (blksiz == DIRBLKSIZ)
2517 uiop->uio_offset += sizeof(struct nfs_dirent);
2518 uiop->uio_resid -= sizeof(struct nfs_dirent);
2519 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2520 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2521 ERROROUT(nfsm_mtouio(&info, uiop, len));
2524 * The uiop has advanced by nfs_dirent + len
2525 * but really needs to advance by
2528 cp = uiop->uio_iov->iov_base;
2530 *cp = '\0'; /* null terminate */
2531 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2532 uiop->uio_iov->iov_len -= tlen;
2533 uiop->uio_offset += tlen;
2534 uiop->uio_resid -= tlen;
2537 * NFS strings must be rounded up (nfsm_myouio
2538 * handled that in the bigenough case).
2540 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2543 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2545 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2549 * If we were able to accomodate the last entry,
2550 * get the cookie for the next one. Otherwise
2551 * hold-over the cookie for the one we were not
2552 * able to accomodate.
2555 cookie.nfsuquad[0] = *tl++;
2557 cookie.nfsuquad[1] = *tl++;
2558 } else if (info.v3) {
2563 more_dirs = fxdr_unsigned(int, *tl);
2566 * If at end of rpc data, get the eof boolean
2569 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2570 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2576 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2577 * by increasing d_reclen for the last record.
2580 left = DIRBLKSIZ - blksiz;
2581 dp->nfs_reclen += left;
2582 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2583 uiop->uio_iov->iov_len -= left;
2584 uiop->uio_offset += left;
2585 uiop->uio_resid -= left;
2590 * We hit the end of the directory, update direofoffset.
2592 dnp->n_direofoffset = uiop->uio_offset;
2595 * There is more to go, insert the link cookie so the
2596 * next block can be read.
2598 if (uiop->uio_resid > 0)
2599 kprintf("EEK! readdirrpc resid > 0\n");
2600 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2608 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2611 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2614 struct nfs_dirent *dp;
2616 struct vnode *newvp;
2618 caddr_t dpossav1, dpossav2;
2620 struct mbuf *mdsav1, *mdsav2;
2622 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2623 struct nfsnode *dnp = VTONFS(vp), *np;
2626 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2627 int attrflag, fhsize;
2628 struct nchandle nch;
2629 struct nchandle dnch;
2630 struct nlcomponent nlc;
2631 struct nfsm_info info;
2640 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2641 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2642 panic("nfs readdirplusrpc bad uio");
2645 * Obtain the namecache record for the directory so we have something
2646 * to use as a basis for creating the entries. This function will
2647 * return a held (but not locked) ncp. The ncp may be disconnected
2648 * from the tree and cannot be used for upward traversals, and the
2649 * ncp may be unnamed. Note that other unrelated operations may
2650 * cause the ncp to be named at any time.
2652 * We have to lock the ncp to prevent a lock order reversal when
2653 * rdirplus does nlookups of the children, because the vnode is
2654 * locked and has to stay that way.
2656 cache_fromdvp(vp, NULL, 0, &dnch);
2657 bzero(&nlc, sizeof(nlc));
2661 * If there is no cookie, assume directory was stale.
2663 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2669 return (NFSERR_BAD_COOKIE);
2673 * Loop around doing readdir rpc's of size nm_readdirsize
2674 * truncated to a multiple of DIRBLKSIZ.
2675 * The stopping criteria is EOF or buffer full.
2677 while (more_dirs && bigenough) {
2678 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2679 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2680 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2681 ERROROUT(nfsm_fhtom(&info, vp));
2682 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2683 *tl++ = cookie.nfsuquad[0];
2684 *tl++ = cookie.nfsuquad[1];
2685 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2686 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2687 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2688 *tl = txdr_unsigned(nmp->nm_rsize);
2689 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2691 nfs_vpcred(vp, ND_READ), &error));
2692 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2693 NFS_LATTR_NOSHRINK));
2694 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2695 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2696 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2697 more_dirs = fxdr_unsigned(int, *tl);
2699 /* loop thru the dir entries, doctoring them to 4bsd form */
2700 while (more_dirs && bigenough) {
2701 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2702 fileno = fxdr_hyper(tl);
2703 len = fxdr_unsigned(int, *(tl + 2));
2704 if (len <= 0 || len > NFS_MAXNAMLEN) {
2710 tlen = nfsm_rndup(len);
2712 tlen += 4; /* To ensure null termination*/
2713 left = DIRBLKSIZ - blksiz;
2714 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2715 dp->nfs_reclen += left;
2716 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2717 uiop->uio_iov->iov_len -= left;
2718 uiop->uio_offset += left;
2719 uiop->uio_resid -= left;
2722 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2725 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2726 dp->nfs_ino = fileno;
2727 dp->nfs_namlen = len;
2728 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2729 dp->nfs_type = DT_UNKNOWN;
2730 blksiz += dp->nfs_reclen;
2731 if (blksiz == DIRBLKSIZ)
2733 uiop->uio_offset += sizeof(struct nfs_dirent);
2734 uiop->uio_resid -= sizeof(struct nfs_dirent);
2735 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2736 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2737 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2738 nlc.nlc_namelen = len;
2739 ERROROUT(nfsm_mtouio(&info, uiop, len));
2740 cp = uiop->uio_iov->iov_base;
2743 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2744 uiop->uio_iov->iov_len -= tlen;
2745 uiop->uio_offset += tlen;
2746 uiop->uio_resid -= tlen;
2748 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2750 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2752 cookie.nfsuquad[0] = *tl++;
2753 cookie.nfsuquad[1] = *tl++;
2759 * Since the attributes are before the file handle
2760 * (sigh), we must skip over the attributes and then
2761 * come back and get them.
2763 attrflag = fxdr_unsigned(int, *tl);
2765 dpossav1 = info.dpos;
2767 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2768 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2769 doit = fxdr_unsigned(int, *tl);
2771 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2773 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2774 !NFS_CMPFH(dnp, fhp, fhsize)
2778 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2779 nlc.nlc_namelen, nlc.nlc_namelen,
2783 * This is a bit hokey but there isn't
2784 * much we can do about it. We can't
2785 * hold the directory vp locked while
2786 * doing lookups and gets.
2788 nch = cache_nlookup_nonblock(&dnch, &nlc);
2789 if (nch.ncp == NULL)
2791 cache_setunresolved(&nch);
2792 error = nfs_nget_nonblock(vp->v_mount, fhp,
2800 dpossav2 = info.dpos;
2801 info.dpos = dpossav1;
2804 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2805 info.dpos = dpossav2;
2808 IFTODT(VTTOIF(np->n_vattr.va_type));
2809 nfs_cache_setvp(&nch, newvp,
2810 nfspos_cache_timeout);
2818 kprintf("Warning: NFS/rddirplus, "
2819 "UNABLE TO ENTER %*.*s\n",
2820 nlc.nlc_namelen, nlc.nlc_namelen,
2826 /* Just skip over the file handle */
2827 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2828 i = fxdr_unsigned(int, *tl);
2829 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2831 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2832 more_dirs = fxdr_unsigned(int, *tl);
2835 * If at end of rpc data, get the eof boolean
2838 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2839 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2845 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2846 * by increasing d_reclen for the last record.
2849 left = DIRBLKSIZ - blksiz;
2850 dp->nfs_reclen += left;
2851 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2852 uiop->uio_iov->iov_len -= left;
2853 uiop->uio_offset += left;
2854 uiop->uio_resid -= left;
2858 * We are now either at the end of the directory or have filled the
2862 dnp->n_direofoffset = uiop->uio_offset;
2864 if (uiop->uio_resid > 0)
2865 kprintf("EEK! readdirplusrpc resid > 0\n");
2866 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2870 if (newvp != NULLVP) {
2883 * Silly rename. To make the NFS filesystem that is stateless look a little
2884 * more like the "ufs" a remove of an active vnode is translated to a rename
2885 * to a funny looking filename that is removed by nfs_inactive on the
2886 * nfsnode. There is the potential for another process on a different client
2887 * to create the same funny name between the nfs_lookitup() fails and the
2888 * nfs_rename() completes, but...
2891 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2893 struct sillyrename *sp;
2898 * We previously purged dvp instead of vp. I don't know why, it
2899 * completely destroys performance. We can't do it anyway with the
2900 * new VFS API since we would be breaking the namecache topology.
2902 cache_purge(vp); /* XXX */
2905 if (vp->v_type == VDIR)
2906 panic("nfs: sillyrename dir");
2908 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2909 sp->s_cred = crdup(cnp->cn_cred);
2913 /* Fudge together a funny name */
2914 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2915 (int)(intptr_t)cnp->cn_td);
2917 /* Try lookitups until we get one that isn't there */
2918 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2919 cnp->cn_td, NULL) == 0) {
2921 if (sp->s_name[4] > 'z') {
2926 error = nfs_renameit(dvp, cnp, sp);
2929 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2931 np->n_sillyrename = sp;
2936 kfree((caddr_t)sp, M_NFSREQ);
2941 * Look up a file name and optionally either update the file handle or
2942 * allocate an nfsnode, depending on the value of npp.
2943 * npp == NULL --> just do the lookup
2944 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2946 * *npp != NULL --> update the file handle in the vnode
2949 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2950 struct thread *td, struct nfsnode **npp)
2952 struct vnode *newvp = NULL;
2953 struct nfsnode *np, *dnp = VTONFS(dvp);
2954 int error = 0, fhlen, attrflag;
2956 struct nfsm_info info;
2959 info.v3 = NFS_ISV3(dvp);
2961 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2962 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2963 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2964 ERROROUT(nfsm_fhtom(&info, dvp));
2965 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2966 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2967 if (npp && !error) {
2968 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2971 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2972 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2973 np->n_fhp = &np->n_fh;
2974 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2975 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2976 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2977 np->n_fhsize = fhlen;
2979 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2983 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2992 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2993 NFS_LATTR_NOSHRINK));
2994 if (!attrflag && *npp == NULL) {
3004 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3010 if (npp && *npp == NULL) {
3025 * Nfs Version 3 commit rpc
3027 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3031 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3033 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3034 int error = 0, wccflag = NFSV3_WCCRATTR;
3035 struct nfsm_info info;
3041 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3043 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3044 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3045 ERROROUT(nfsm_fhtom(&info, vp));
3046 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3047 txdr_hyper(offset, tl);
3049 *tl = txdr_unsigned(cnt);
3050 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3051 nfs_vpcred(vp, ND_WRITE), &error));
3052 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3054 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3055 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3056 NFSX_V3WRITEVERF)) {
3057 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3059 error = NFSERR_STALEWRITEVERF;
3070 * - make nfs_bmap() essentially a no-op that does no translation
3071 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3072 * (Maybe I could use the process's page mapping, but I was concerned that
3073 * Kernel Write might not be enabled and also figured copyout() would do
3074 * a lot more work than bcopy() and also it currently happens in the
3075 * context of the swapper process (2).
3077 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3078 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3081 nfs_bmap(struct vop_bmap_args *ap)
3083 /* no token lock required */
3084 if (ap->a_doffsetp != NULL)
3085 *ap->a_doffsetp = ap->a_loffset;
3086 if (ap->a_runp != NULL)
3088 if (ap->a_runb != NULL)
3097 nfs_strategy(struct vop_strategy_args *ap)
3099 struct bio *bio = ap->a_bio;
3101 struct buf *bp __debugvar = bio->bio_buf;
3102 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3106 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3107 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3108 KASSERT(BUF_REFCNT(bp) > 0,
3109 ("nfs_strategy: buffer %p not locked", bp));
3111 if (bio->bio_flags & BIO_SYNC)
3112 td = curthread; /* XXX */
3116 lwkt_gettoken(&nmp->nm_token);
3119 * We probably don't need to push an nbio any more since no
3120 * block conversion is required due to the use of 64 bit byte
3121 * offsets, but do it anyway.
3123 * NOTE: When NFS callers itself via this strategy routines and
3124 * sets up a synchronous I/O, it expects the I/O to run
3125 * synchronously (its bio_done routine just assumes it),
3126 * so for now we have to honor the bit.
3128 nbio = push_bio(bio);
3129 nbio->bio_offset = bio->bio_offset;
3130 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3133 * If the op is asynchronous and an i/o daemon is waiting
3134 * queue the request, wake it up and wait for completion
3135 * otherwise just do it ourselves.
3137 if (bio->bio_flags & BIO_SYNC) {
3138 error = nfs_doio(ap->a_vp, nbio, td);
3140 nfs_asyncio(ap->a_vp, nbio);
3143 lwkt_reltoken(&nmp->nm_token);
3151 * NB Currently unsupported.
3153 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3157 nfs_mmap(struct vop_mmap_args *ap)
3159 /* no token lock required */
3164 * fsync vnode op. Just call nfs_flush() with commit == 1.
3166 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3170 nfs_fsync(struct vop_fsync_args *ap)
3172 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3175 lwkt_gettoken(&nmp->nm_token);
3178 * NOTE: Because attributes are set synchronously we currently
3179 * do not have to implement vsetisdirty()/vclrisdirty().
3181 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3183 lwkt_reltoken(&nmp->nm_token);
3189 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3190 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3191 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3192 * set the buffer contains data that has already been written to the server
3193 * and which now needs a commit RPC.
3195 * If commit is 0 we only take one pass and only flush buffers containing new
3198 * If commit is 1 we take two passes, issuing a commit RPC in the second
3201 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3202 * to completely flush all pending data.
3204 * Note that the RB_SCAN code properly handles the case where the
3205 * callback might block and directly or indirectly (another thread) cause
3206 * the RB tree to change.
3209 #ifndef NFS_COMMITBVECSIZ
3210 #define NFS_COMMITBVECSIZ 16
3213 struct nfs_flush_info {
3214 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3221 struct buf *bvary[NFS_COMMITBVECSIZ];
3227 static int nfs_flush_bp(struct buf *bp, void *data);
3228 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3231 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3233 struct nfsnode *np = VTONFS(vp);
3234 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3235 struct nfs_flush_info info;
3238 bzero(&info, sizeof(info));
3241 info.waitfor = waitfor;
3242 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3244 lwkt_gettoken(&vp->v_token);
3250 info.mode = NFI_FLUSHNEW;
3251 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3252 nfs_flush_bp, &info);
3255 * Take a second pass if committing and no error occured.
3256 * Clean up any left over collection (whether an error
3259 if (commit && error == 0) {
3260 info.mode = NFI_COMMIT;
3261 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3262 nfs_flush_bp, &info);
3264 error = nfs_flush_docommit(&info, error);
3268 * Wait for pending I/O to complete before checking whether
3269 * any further dirty buffers exist.
3271 while (waitfor == MNT_WAIT &&
3272 bio_track_active(&vp->v_track_write)) {
3273 error = bio_track_wait(&vp->v_track_write,
3274 info.slpflag, info.slptimeo);
3277 * We have to be able to break out if this
3278 * is an 'intr' mount.
3280 if (nfs_sigintr(nmp, NULL, td)) {
3286 * Since we do not process pending signals,
3287 * once we get a PCATCH our tsleep() will no
3288 * longer sleep, switch to a fixed timeout
3291 if (info.slpflag == PCATCH) {
3293 info.slptimeo = 2 * hz;
3300 * Loop if we are flushing synchronous as well as committing,
3301 * and dirty buffers are still present. Otherwise we might livelock.
3303 } while (waitfor == MNT_WAIT && commit &&
3304 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3307 * The callbacks have to return a negative error to terminate the
3314 * Deal with any error collection
3316 if (np->n_flag & NWRITEERR) {
3317 error = np->n_error;
3318 np->n_flag &= ~NWRITEERR;
3320 lwkt_reltoken(&vp->v_token);
3326 nfs_flush_bp(struct buf *bp, void *data)
3328 struct nfs_flush_info *info = data;
3334 switch(info->mode) {
3336 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3337 if (error && info->loops && info->waitfor == MNT_WAIT) {
3338 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3340 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3341 if (info->slpflag & PCATCH)
3342 lkflags |= LK_PCATCH;
3343 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3349 * Ignore locking errors
3357 * The buffer may have changed out from under us, even if
3358 * we did not block (MPSAFE). Check again now that it is
3361 if (bp->b_vp == info->vp &&
3362 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3371 * Only process buffers in need of a commit which we can
3372 * immediately lock. This may prevent a buffer from being
3373 * committed, but the normal flush loop will block on the
3374 * same buffer so we shouldn't get into an endless loop.
3376 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3377 (B_DELWRI | B_NEEDCOMMIT)) {
3380 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3384 * We must recheck after successfully locking the buffer.
3386 if (bp->b_vp != info->vp ||
3387 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3388 (B_DELWRI | B_NEEDCOMMIT)) {
3394 * NOTE: storing the bp in the bvary[] basically sets
3395 * it up for a commit operation.
3397 * We must call vfs_busy_pages() now so the commit operation
3398 * is interlocked with user modifications to memory mapped
3399 * pages. The b_dirtyoff/b_dirtyend range is not correct
3400 * until after the pages have been busied.
3402 * Note: to avoid loopback deadlocks, we do not
3403 * assign b_runningbufspace.
3406 bp->b_cmd = BUF_CMD_WRITE;
3407 vfs_busy_pages(bp->b_vp, bp);
3408 info->bvary[info->bvsize] = bp;
3409 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3410 if (info->bvsize == 0 || toff < info->beg_off)
3411 info->beg_off = toff;
3412 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3413 if (info->bvsize == 0 || toff > info->end_off)
3414 info->end_off = toff;
3416 if (info->bvsize == NFS_COMMITBVECSIZ) {
3417 error = nfs_flush_docommit(info, 0);
3418 KKASSERT(info->bvsize == 0);
3426 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3436 if (info->bvsize > 0) {
3438 * Commit data on the server, as required. Note that
3439 * nfs_commit will use the vnode's cred for the commit.
3440 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3442 bytes = info->end_off - info->beg_off;
3443 if (bytes > 0x40000000)
3448 retv = nfs_commitrpc_uio(vp, info->beg_off,
3449 (int)bytes, info->td);
3450 if (retv == NFSERR_STALEWRITEVERF)
3451 nfs_clearcommit(vp->v_mount);
3455 * Now, either mark the blocks I/O done or mark the
3456 * blocks dirty, depending on whether the commit
3459 for (i = 0; i < info->bvsize; ++i) {
3460 bp = info->bvary[i];
3461 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3463 * Either an error or the original
3464 * vfs_busy_pages() cleared B_NEEDCOMMIT
3465 * due to finding new dirty VM pages in
3468 * Leave B_DELWRI intact.
3470 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3471 vfs_unbusy_pages(bp);
3472 bp->b_cmd = BUF_CMD_DONE;
3476 * Success, remove B_DELWRI ( bundirty() ).
3478 * b_dirtyoff/b_dirtyend seem to be NFS
3479 * specific. We should probably move that
3480 * into bundirty(). XXX
3482 * We are faking an I/O write, we have to
3483 * start the transaction in order to
3484 * immediately biodone() it.
3487 bp->b_flags &= ~B_ERROR;
3488 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3489 bp->b_dirtyoff = bp->b_dirtyend = 0;
3490 biodone(&bp->b_bio1);
3499 * NFS advisory byte-level locks.
3500 * Currently unsupported.
3502 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3506 nfs_advlock(struct vop_advlock_args *ap)
3508 struct nfsnode *np = VTONFS(ap->a_vp);
3510 /* no token lock currently required */
3512 * The following kludge is to allow diskless support to work
3513 * until a real NFS lockd is implemented. Basically, just pretend
3514 * that this is a local lock.
3516 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3520 * Print out the contents of an nfsnode.
3522 * nfs_print(struct vnode *a_vp)
3525 nfs_print(struct vop_print_args *ap)
3527 struct vnode *vp = ap->a_vp;
3528 struct nfsnode *np = VTONFS(vp);
3530 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3531 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3532 if (vp->v_type == VFIFO)
3539 * nfs special file access vnode op.
3541 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3544 nfs_laccess(struct vop_access_args *ap)
3546 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3550 lwkt_gettoken(&nmp->nm_token);
3551 error = VOP_GETATTR(ap->a_vp, &vattr);
3553 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3556 lwkt_reltoken(&nmp->nm_token);
3562 * Read wrapper for fifos.
3564 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3565 * struct ucred *a_cred)
3568 nfsfifo_read(struct vop_read_args *ap)
3570 struct nfsnode *np = VTONFS(ap->a_vp);
3572 /* no token access required */
3577 getnanotime(&np->n_atim);
3578 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3582 * Write wrapper for fifos.
3584 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3585 * struct ucred *a_cred)
3588 nfsfifo_write(struct vop_write_args *ap)
3590 struct nfsnode *np = VTONFS(ap->a_vp);
3592 /* no token access required */
3597 getnanotime(&np->n_mtim);
3598 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3602 * Close wrapper for fifos.
3604 * Update the times on the nfsnode then do fifo close.
3606 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3609 nfsfifo_close(struct vop_close_args *ap)
3611 struct vnode *vp = ap->a_vp;
3612 struct nfsnode *np = VTONFS(vp);
3616 /* no token access required */
3618 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3619 if (np->n_flag & (NACC | NUPD)) {
3621 if (np->n_flag & NACC)
3623 if (np->n_flag & NUPD)
3626 if (VREFCNT(vp) == 1 &&
3627 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3629 if (np->n_flag & NACC)
3630 vattr.va_atime = np->n_atim;
3631 if (np->n_flag & NUPD)
3632 vattr.va_mtime = np->n_mtim;
3633 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3636 return (VOCALL(&fifo_vnode_vops, &ap->a_head));