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);
549 error = VOP_GETATTR(vp, &vattr);
551 lwkt_reltoken(&nmp->nm_token);
554 if (np->n_flag & NRMODIFIED) {
555 if (vp->v_type == VDIR)
557 error = nfs_vinvalbuf(vp, V_SAVE, 1);
558 if (error == EINTR) {
559 lwkt_reltoken(&nmp->nm_token);
562 np->n_flag &= ~NRMODIFIED;
564 error = vop_stdopen(ap);
565 lwkt_reltoken(&nmp->nm_token);
572 * What an NFS client should do upon close after writing is a debatable issue.
573 * Most NFS clients push delayed writes to the server upon close, basically for
575 * 1 - So that any write errors may be reported back to the client process
576 * doing the close system call. By far the two most likely errors are
577 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
578 * 2 - To put a worst case upper bound on cache inconsistency between
579 * multiple clients for the file.
580 * There is also a consistency problem for Version 2 of the protocol w.r.t.
581 * not being able to tell if other clients are writing a file concurrently,
582 * since there is no way of knowing if the changed modify time in the reply
583 * is only due to the write for this client.
584 * (NFS Version 3 provides weak cache consistency data in the reply that
585 * should be sufficient to detect and handle this case.)
587 * The current code does the following:
588 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
589 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
590 * or commit them (this satisfies 1 and 2 except for the
591 * case where the server crashes after this close but
592 * before the commit RPC, which is felt to be "good
593 * enough". Changing the last argument to nfs_flush() to
594 * a 1 would force a commit operation, if it is felt a
595 * commit is necessary now.
596 * for NQNFS - do nothing now, since 2 is dealt with via leases and
597 * 1 should be dealt with via an fsync() system call for
598 * cases where write errors are important.
600 * nfs_close(struct vnode *a_vp, int a_fflag)
604 nfs_close(struct vop_close_args *ap)
606 struct vnode *vp = ap->a_vp;
607 struct nfsnode *np = VTONFS(vp);
608 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
610 thread_t td = curthread;
612 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
613 lwkt_gettoken(&nmp->nm_token);
615 if (vp->v_type == VREG) {
616 if (np->n_flag & NLMODIFIED) {
619 * Under NFSv3 we have dirty buffers to dispose of. We
620 * must flush them to the NFS server. We have the option
621 * of waiting all the way through the commit rpc or just
622 * waiting for the initial write. The default is to only
623 * wait through the initial write so the data is in the
624 * server's cache, which is roughly similar to the state
625 * a standard disk subsystem leaves the file in on close().
627 * We cannot clear the NLMODIFIED bit in np->n_flag due to
628 * potential races with other processes, and certainly
629 * cannot clear it if we don't commit.
631 int cm = nfsv3_commit_on_close ? 1 : 0;
632 error = nfs_flush(vp, MNT_WAIT, td, cm);
633 /* np->n_flag &= ~NLMODIFIED; */
635 error = nfs_vinvalbuf(vp, V_SAVE, 1);
639 if (np->n_flag & NWRITEERR) {
640 np->n_flag &= ~NWRITEERR;
645 lwkt_reltoken(&nmp->nm_token);
651 * nfs getattr call from vfs.
653 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
656 nfs_getattr(struct vop_getattr_args *ap)
658 struct vnode *vp = ap->a_vp;
659 struct nfsnode *np = VTONFS(vp);
660 struct nfsmount *nmp;
662 thread_t td = curthread;
663 struct nfsm_info info;
666 info.v3 = NFS_ISV3(vp);
667 nmp = VFSTONFS(vp->v_mount);
669 lwkt_gettoken(&nmp->nm_token);
672 * Update local times for special files.
674 if (np->n_flag & (NACC | NUPD))
677 * First look in the cache.
679 if (nfs_getattrcache(vp, ap->a_vap) == 0)
682 if (info.v3 && nfsaccess_cache_timeout > 0) {
683 nfsstats.accesscache_misses++;
684 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
685 if (nfs_getattrcache(vp, ap->a_vap) == 0)
689 nfsstats.rpccnt[NFSPROC_GETATTR]++;
690 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
691 ERROROUT(nfsm_fhtom(&info, vp));
692 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
693 nfs_vpcred(vp, ND_CHECK), &error));
695 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
701 * NFS doesn't support chflags flags. If the nfs mount was
702 * made -o cache set the UF_CACHE bit for swapcache.
704 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
705 ap->a_vap->va_flags |= UF_CACHE;
707 lwkt_reltoken(&nmp->nm_token);
714 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
717 nfs_setattr(struct vop_setattr_args *ap)
719 struct vnode *vp = ap->a_vp;
720 struct nfsnode *np = VTONFS(vp);
721 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
722 struct vattr *vap = ap->a_vap;
725 thread_t td = curthread;
731 * Setting of flags is not supported.
733 if (vap->va_flags != VNOVAL)
737 * Disallow write attempts if the filesystem is mounted read-only.
739 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
740 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
741 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
742 (vp->v_mount->mnt_flag & MNT_RDONLY))
745 lwkt_gettoken(&nmp->nm_token);
747 if (vap->va_size != VNOVAL) {
749 * truncation requested
751 switch (vp->v_type) {
753 lwkt_reltoken(&nmp->nm_token);
759 if (vap->va_mtime.tv_sec == VNOVAL &&
760 vap->va_atime.tv_sec == VNOVAL &&
761 vap->va_mode == (mode_t)VNOVAL &&
762 vap->va_uid == (uid_t)VNOVAL &&
763 vap->va_gid == (gid_t)VNOVAL) {
764 lwkt_reltoken(&nmp->nm_token);
767 vap->va_size = VNOVAL;
771 * Disallow write attempts if the filesystem is
774 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
775 lwkt_reltoken(&nmp->nm_token);
781 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
784 if (np->n_flag & NLMODIFIED) {
785 if (vap->va_size == 0)
786 error = nfs_vinvalbuf(vp, 0, 1);
788 error = nfs_vinvalbuf(vp, V_SAVE, 1);
792 * note: this loop case almost always happens at
793 * least once per truncation.
795 if (error == 0 && np->n_size != vap->va_size)
797 np->n_vattr.va_size = vap->va_size;
800 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
802 * What to do. If we are modifying the mtime we lose
803 * mtime detection of changes made by the server or other
804 * clients. But programs like rsync/rdist/cpdup are going
805 * to call utimes a lot. We don't want to piecemeal sync.
807 * For now sync if any prior remote changes were detected,
808 * but allow us to lose track of remote changes made during
809 * the utimes operation.
811 if (np->n_flag & NRMODIFIED)
812 error = nfs_vinvalbuf(vp, V_SAVE, 1);
816 if (vap->va_mtime.tv_sec != VNOVAL) {
817 np->n_mtime = vap->va_mtime.tv_sec;
821 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
824 * Sanity check if a truncation was issued. This should only occur
825 * if multiple processes are racing on the same file.
827 if (error == 0 && vap->va_size != VNOVAL &&
828 np->n_size != vap->va_size) {
829 kprintf("NFS ftruncate: server disagrees on the file size: "
832 (intmax_t)vap->va_size,
833 (intmax_t)np->n_size);
836 if (error && vap->va_size != VNOVAL) {
837 np->n_size = np->n_vattr.va_size = tsize;
838 nfs_meta_setsize(vp, td, np->n_size, 0);
840 lwkt_reltoken(&nmp->nm_token);
846 * Do an nfs setattr rpc.
849 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
850 struct ucred *cred, struct thread *td)
852 struct nfsv2_sattr *sp;
853 struct nfsnode *np = VTONFS(vp);
855 int error = 0, wccflag = NFSV3_WCCRATTR;
856 struct nfsm_info info;
859 info.v3 = NFS_ISV3(vp);
861 nfsstats.rpccnt[NFSPROC_SETATTR]++;
862 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
863 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
864 ERROROUT(nfsm_fhtom(&info, vp));
866 nfsm_v3attrbuild(&info, vap, TRUE);
867 tl = nfsm_build(&info, NFSX_UNSIGNED);
870 sp = nfsm_build(&info, NFSX_V2SATTR);
871 if (vap->va_mode == (mode_t)VNOVAL)
872 sp->sa_mode = nfs_xdrneg1;
874 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
875 if (vap->va_uid == (uid_t)VNOVAL)
876 sp->sa_uid = nfs_xdrneg1;
878 sp->sa_uid = txdr_unsigned(vap->va_uid);
879 if (vap->va_gid == (gid_t)VNOVAL)
880 sp->sa_gid = nfs_xdrneg1;
882 sp->sa_gid = txdr_unsigned(vap->va_gid);
883 sp->sa_size = txdr_unsigned(vap->va_size);
884 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
885 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
887 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
890 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
892 ERROROUT(nfsm_loadattr(&info, vp, NULL));
902 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
908 cache_setvp(nch, vp);
909 cache_settimeout(nch, nctimeout);
913 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
914 * nfs_lookup() until all remaining new api calls are implemented.
916 * Resolve a namecache entry. This function is passed a locked ncp and
917 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
920 nfs_nresolve(struct vop_nresolve_args *ap)
922 struct thread *td = curthread;
923 struct namecache *ncp;
924 struct nfsmount *nmp;
934 struct nfsm_info info;
937 nmp = VFSTONFS(dvp->v_mount);
939 lwkt_gettoken(&nmp->nm_token);
941 if ((error = vget(dvp, LK_SHARED)) != 0) {
942 lwkt_reltoken(&nmp->nm_token);
947 info.v3 = NFS_ISV3(dvp);
950 nfsstats.lookupcache_misses++;
951 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
952 ncp = ap->a_nch->ncp;
954 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
955 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
956 ERROROUT(nfsm_fhtom(&info, dvp));
957 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
958 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
959 ap->a_cred, &error));
962 * Cache negatve lookups to reduce NFS traffic, but use
963 * a fast timeout. Otherwise use a timeout of 1 tick.
964 * XXX we should add a namecache flag for no-caching
965 * to uncache the negative hit as soon as possible, but
966 * we cannot simply destroy the entry because it is used
967 * as a placeholder by the caller.
969 * The refactored nfs code will overwrite a non-zero error
970 * with 0 when we use ERROROUT(), so don't here.
973 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
974 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
986 * Success, get the file handle, do various checks, and load
987 * post-operation data from the reply packet. Theoretically
988 * we should never be looking up "." so, theoretically, we
989 * should never get the same file handle as our directory. But
990 * we check anyway. XXX
992 * Note that no timeout is set for the positive cache hit. We
993 * assume, theoretically, that ESTALE returns will be dealt with
994 * properly to handle NFS races and in anycase we cannot depend
995 * on a timeout to deal with NFS open/create/excl issues so instead
996 * of a bad hack here the rest of the NFS client code needs to do
999 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1002 if (NFS_CMPFH(np, fhp, fhsize)) {
1006 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1011 lwkt_reltoken(&nmp->nm_token);
1017 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1018 NFS_LATTR_NOSHRINK));
1019 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1020 NFS_LATTR_NOSHRINK));
1022 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1024 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1028 lwkt_reltoken(&nmp->nm_token);
1040 * 'cached' nfs directory lookup
1042 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1044 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1045 * struct componentname *a_cnp)
1048 nfs_lookup(struct vop_old_lookup_args *ap)
1050 struct componentname *cnp = ap->a_cnp;
1051 struct vnode *dvp = ap->a_dvp;
1052 struct vnode **vpp = ap->a_vpp;
1053 int flags = cnp->cn_flags;
1054 struct vnode *newvp;
1055 struct vnode *notvp;
1056 struct nfsmount *nmp;
1060 int lockparent, wantparent, attrflag, fhsize;
1063 struct nfsm_info info;
1066 info.v3 = NFS_ISV3(dvp);
1069 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1072 * Read-only mount check and directory check.
1075 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1076 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1079 if (dvp->v_type != VDIR)
1083 * Look it up in the cache. Note that ENOENT is only returned if we
1084 * previously entered a negative hit (see later on). The additional
1085 * nfsneg_cache_timeout check causes previously cached results to
1086 * be instantly ignored if the negative caching is turned off.
1088 lockparent = flags & CNP_LOCKPARENT;
1089 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1090 nmp = VFSTONFS(dvp->v_mount);
1093 lwkt_gettoken(&nmp->nm_token);
1100 nfsstats.lookupcache_misses++;
1101 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1102 len = cnp->cn_namelen;
1103 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1104 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1105 ERROROUT(nfsm_fhtom(&info, dvp));
1106 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1107 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1108 cnp->cn_cred, &error));
1110 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1111 NFS_LATTR_NOSHRINK);
1121 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1124 * Handle RENAME case...
1126 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1127 if (NFS_CMPFH(np, fhp, fhsize)) {
1130 lwkt_reltoken(&nmp->nm_token);
1133 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1137 lwkt_reltoken(&nmp->nm_token);
1142 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1143 NFS_LATTR_NOSHRINK));
1144 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1145 NFS_LATTR_NOSHRINK));
1147 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1154 cnp->cn_flags |= CNP_PDIRUNLOCK;
1156 lwkt_reltoken(&nmp->nm_token);
1160 if (flags & CNP_ISDOTDOT) {
1162 cnp->cn_flags |= CNP_PDIRUNLOCK;
1163 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1165 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1166 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1167 lwkt_reltoken(&nmp->nm_token);
1168 return (error); /* NOTE: return error from nget */
1172 error = vn_lock(dvp, LK_EXCLUSIVE);
1175 lwkt_reltoken(&nmp->nm_token);
1178 cnp->cn_flags |= CNP_PDIRUNLOCK;
1180 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1184 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1188 lwkt_reltoken(&nmp->nm_token);
1193 cnp->cn_flags |= CNP_PDIRUNLOCK;
1198 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1199 NFS_LATTR_NOSHRINK));
1200 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1201 NFS_LATTR_NOSHRINK));
1203 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1206 /* XXX MOVE TO nfs_nremove() */
1207 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1208 cnp->cn_nameiop != NAMEI_DELETE) {
1209 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1217 if (newvp != NULLVP) {
1221 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1222 cnp->cn_nameiop == NAMEI_RENAME) &&
1226 cnp->cn_flags |= CNP_PDIRUNLOCK;
1228 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1231 error = EJUSTRETURN;
1234 lwkt_reltoken(&nmp->nm_token);
1240 * Just call nfs_bioread() to do the work.
1242 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1243 * struct ucred *a_cred)
1246 nfs_read(struct vop_read_args *ap)
1248 struct vnode *vp = ap->a_vp;
1249 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1252 lwkt_gettoken(&nmp->nm_token);
1253 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1254 lwkt_reltoken(&nmp->nm_token);
1262 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1265 nfs_readlink(struct vop_readlink_args *ap)
1267 struct vnode *vp = ap->a_vp;
1268 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1271 if (vp->v_type != VLNK)
1274 lwkt_gettoken(&nmp->nm_token);
1275 error = nfs_bioread(vp, ap->a_uio, 0);
1276 lwkt_reltoken(&nmp->nm_token);
1282 * Do a readlink rpc.
1283 * Called by nfs_doio() from below the buffer cache.
1286 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1288 int error = 0, len, attrflag;
1289 struct nfsm_info info;
1292 info.v3 = NFS_ISV3(vp);
1294 nfsstats.rpccnt[NFSPROC_READLINK]++;
1295 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1296 ERROROUT(nfsm_fhtom(&info, vp));
1297 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1298 nfs_vpcred(vp, ND_CHECK), &error));
1300 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1301 NFS_LATTR_NOSHRINK));
1304 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1305 if (len == NFS_MAXPATHLEN) {
1306 struct nfsnode *np = VTONFS(vp);
1307 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1310 ERROROUT(nfsm_mtouio(&info, uiop, len));
1319 * nfs synchronous read rpc using UIO
1322 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1325 struct nfsmount *nmp;
1326 int error = 0, len, retlen, tsiz, eof, attrflag;
1327 struct nfsm_info info;
1331 info.v3 = NFS_ISV3(vp);
1336 nmp = VFSTONFS(vp->v_mount);
1338 tsiz = uiop->uio_resid;
1339 tmp_off = uiop->uio_offset + tsiz;
1340 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1342 tmp_off = uiop->uio_offset;
1344 nfsstats.rpccnt[NFSPROC_READ]++;
1345 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1346 nfsm_reqhead(&info, vp, NFSPROC_READ,
1347 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1348 ERROROUT(nfsm_fhtom(&info, vp));
1349 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1351 txdr_hyper(uiop->uio_offset, tl);
1352 *(tl + 2) = txdr_unsigned(len);
1354 *tl++ = txdr_unsigned(uiop->uio_offset);
1355 *tl++ = txdr_unsigned(len);
1358 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1359 nfs_vpcred(vp, ND_READ), &error));
1361 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1362 NFS_LATTR_NOSHRINK));
1363 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1364 eof = fxdr_unsigned(int, *(tl + 1));
1366 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1368 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1369 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1374 * Handle short-read from server (NFSv3). If EOF is not
1375 * flagged (and no error occurred), but retlen is less
1376 * then the request size, we must zero-fill the remainder.
1378 if (retlen < len && info.v3 && eof == 0) {
1379 ERROROUT(uiomovez(len - retlen, uiop));
1385 * Terminate loop on EOF or zero-length read.
1387 * For NFSv2 a short-read indicates EOF, not zero-fill,
1388 * and also terminates the loop.
1391 if (eof || retlen == 0)
1393 } else if (retlen < len) {
1405 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1406 int *iomode, int *must_commit)
1410 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1411 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1412 int committed = NFSV3WRITE_FILESYNC;
1413 struct nfsm_info info;
1416 info.v3 = NFS_ISV3(vp);
1419 if (uiop->uio_iovcnt != 1)
1420 panic("nfs: writerpc iovcnt > 1");
1423 tsiz = uiop->uio_resid;
1424 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1427 nfsstats.rpccnt[NFSPROC_WRITE]++;
1428 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1429 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1430 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1432 ERROROUT(nfsm_fhtom(&info, vp));
1434 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1435 txdr_hyper(uiop->uio_offset, tl);
1437 *tl++ = txdr_unsigned(len);
1438 *tl++ = txdr_unsigned(*iomode);
1439 *tl = txdr_unsigned(len);
1443 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1444 /* Set both "begin" and "current" to non-garbage. */
1445 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1446 *tl++ = x; /* "begin offset" */
1447 *tl++ = x; /* "current offset" */
1448 x = txdr_unsigned(len);
1449 *tl++ = x; /* total to this offset */
1450 *tl = x; /* size of this write */
1452 ERROROUT(nfsm_uiotom(&info, uiop, len));
1453 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1454 nfs_vpcred(vp, ND_WRITE), &error));
1457 * The write RPC returns a before and after mtime. The
1458 * nfsm_wcc_data() macro checks the before n_mtime
1459 * against the before time and stores the after time
1460 * in the nfsnode's cached vattr and n_mtime field.
1461 * The NRMODIFIED bit will be set if the before
1462 * time did not match the original mtime.
1464 wccflag = NFSV3_WCCCHK;
1465 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1467 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1468 rlen = fxdr_unsigned(int, *tl++);
1474 } else if (rlen < len) {
1475 backup = len - rlen;
1476 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1477 uiop->uio_iov->iov_len += backup;
1478 uiop->uio_offset -= backup;
1479 uiop->uio_resid += backup;
1482 commit = fxdr_unsigned(int, *tl++);
1485 * Return the lowest committment level
1486 * obtained by any of the RPCs.
1488 if (committed == NFSV3WRITE_FILESYNC)
1490 else if (committed == NFSV3WRITE_DATASYNC &&
1491 commit == NFSV3WRITE_UNSTABLE)
1493 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1494 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1496 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1497 } else if (bcmp((caddr_t)tl,
1498 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1500 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1505 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1514 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1515 committed = NFSV3WRITE_FILESYNC;
1516 *iomode = committed;
1518 uiop->uio_resid = tsiz;
1524 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1525 * mode set to specify the file type and the size field for rdev.
1528 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1531 struct nfsv2_sattr *sp;
1533 struct vnode *newvp = NULL;
1534 struct nfsnode *np = NULL;
1536 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1538 struct nfsm_info info;
1541 info.v3 = NFS_ISV3(dvp);
1543 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1544 rmajor = txdr_unsigned(vap->va_rmajor);
1545 rminor = txdr_unsigned(vap->va_rminor);
1546 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1547 rmajor = nfs_xdrneg1;
1548 rminor = nfs_xdrneg1;
1550 return (EOPNOTSUPP);
1552 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1555 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1556 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1557 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1558 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1559 ERROROUT(nfsm_fhtom(&info, dvp));
1560 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1563 tl = nfsm_build(&info, NFSX_UNSIGNED);
1564 *tl++ = vtonfsv3_type(vap->va_type);
1565 nfsm_v3attrbuild(&info, vap, FALSE);
1566 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1567 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1568 *tl++ = txdr_unsigned(vap->va_rmajor);
1569 *tl = txdr_unsigned(vap->va_rminor);
1572 sp = nfsm_build(&info, NFSX_V2SATTR);
1573 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1574 sp->sa_uid = nfs_xdrneg1;
1575 sp->sa_gid = nfs_xdrneg1;
1576 sp->sa_size = makeudev(rmajor, rminor);
1577 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1578 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1580 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1581 cnp->cn_cred, &error));
1583 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1589 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1590 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1596 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1607 VTONFS(dvp)->n_flag |= NLMODIFIED;
1609 VTONFS(dvp)->n_attrstamp = 0;
1615 * just call nfs_mknodrpc() to do the work.
1617 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1618 * struct componentname *a_cnp, struct vattr *a_vap)
1622 nfs_mknod(struct vop_old_mknod_args *ap)
1624 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1627 lwkt_gettoken(&nmp->nm_token);
1628 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1629 lwkt_reltoken(&nmp->nm_token);
1634 static u_long create_verf;
1636 * nfs file create call
1638 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1639 * struct componentname *a_cnp, struct vattr *a_vap)
1642 nfs_create(struct vop_old_create_args *ap)
1644 struct vnode *dvp = ap->a_dvp;
1645 struct vattr *vap = ap->a_vap;
1646 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1647 struct componentname *cnp = ap->a_cnp;
1648 struct nfsv2_sattr *sp;
1650 struct nfsnode *np = NULL;
1651 struct vnode *newvp = NULL;
1652 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1654 struct nfsm_info info;
1657 info.v3 = NFS_ISV3(dvp);
1658 lwkt_gettoken(&nmp->nm_token);
1661 * Oops, not for me..
1663 if (vap->va_type == VSOCK) {
1664 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1665 lwkt_reltoken(&nmp->nm_token);
1669 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1670 lwkt_reltoken(&nmp->nm_token);
1673 if (vap->va_vaflags & VA_EXCLUSIVE)
1676 nfsstats.rpccnt[NFSPROC_CREATE]++;
1677 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1678 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1679 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1680 ERROROUT(nfsm_fhtom(&info, dvp));
1681 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1684 tl = nfsm_build(&info, NFSX_UNSIGNED);
1685 if (fmode & O_EXCL) {
1686 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1687 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1689 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1690 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1693 *tl++ = create_verf;
1694 *tl = ++create_verf;
1696 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1697 nfsm_v3attrbuild(&info, vap, FALSE);
1700 sp = nfsm_build(&info, NFSX_V2SATTR);
1701 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1702 sp->sa_uid = nfs_xdrneg1;
1703 sp->sa_gid = nfs_xdrneg1;
1705 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1706 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1708 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1709 cnp->cn_cred, &error));
1711 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1717 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1718 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1725 error = nfsm_wcc_data(&info, dvp, &wccflag);
1727 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1733 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1734 KKASSERT(newvp == NULL);
1738 } else if (info.v3 && (fmode & O_EXCL)) {
1740 * We are normally called with only a partially initialized
1741 * VAP. Since the NFSv3 spec says that server may use the
1742 * file attributes to store the verifier, the spec requires
1743 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1744 * in atime, but we can't really assume that all servers will
1745 * so we ensure that our SETATTR sets both atime and mtime.
1747 if (vap->va_mtime.tv_sec == VNOVAL)
1748 vfs_timestamp(&vap->va_mtime);
1749 if (vap->va_atime.tv_sec == VNOVAL)
1750 vap->va_atime = vap->va_mtime;
1751 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1755 * The new np may have enough info for access
1756 * checks, make sure rucred and wucred are
1757 * initialized for read and write rpc's.
1760 if (np->n_rucred == NULL)
1761 np->n_rucred = crhold(cnp->cn_cred);
1762 if (np->n_wucred == NULL)
1763 np->n_wucred = crhold(cnp->cn_cred);
1768 VTONFS(dvp)->n_flag |= NLMODIFIED;
1770 VTONFS(dvp)->n_attrstamp = 0;
1771 lwkt_reltoken(&nmp->nm_token);
1776 * nfs file remove call
1777 * To try and make nfs semantics closer to ufs semantics, a file that has
1778 * other processes using the vnode is renamed instead of removed and then
1779 * removed later on the last close.
1781 * If a rename is not already in the works
1782 * call nfs_sillyrename() to set it up
1786 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1787 * struct componentname *a_cnp)
1790 nfs_remove(struct vop_old_remove_args *ap)
1792 struct vnode *vp = ap->a_vp;
1793 struct vnode *dvp = ap->a_dvp;
1794 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1795 struct componentname *cnp = ap->a_cnp;
1796 struct nfsnode *np = VTONFS(vp);
1800 lwkt_gettoken(&nmp->nm_token);
1802 if (VREFCNT(vp) < 1)
1803 panic("nfs_remove: bad v_refcnt");
1805 if (vp->v_type == VDIR) {
1807 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1808 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1810 * throw away biocache buffers, mainly to avoid
1811 * unnecessary delayed writes later.
1813 error = nfs_vinvalbuf(vp, 0, 1);
1816 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1817 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1819 * Kludge City: If the first reply to the remove rpc is lost..
1820 * the reply to the retransmitted request will be ENOENT
1821 * since the file was in fact removed
1822 * Therefore, we cheat and return success.
1824 if (error == ENOENT)
1826 } else if (!np->n_sillyrename) {
1827 error = nfs_sillyrename(dvp, vp, cnp);
1829 np->n_attrstamp = 0;
1830 lwkt_reltoken(&nmp->nm_token);
1836 * nfs file remove rpc called from nfs_inactive
1839 nfs_removeit(struct sillyrename *sp)
1841 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1846 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1849 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1850 struct ucred *cred, struct thread *td)
1852 int error = 0, wccflag = NFSV3_WCCRATTR;
1853 struct nfsm_info info;
1856 info.v3 = NFS_ISV3(dvp);
1858 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1859 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1860 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1861 ERROROUT(nfsm_fhtom(&info, dvp));
1862 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1863 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1865 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1870 VTONFS(dvp)->n_flag |= NLMODIFIED;
1872 VTONFS(dvp)->n_attrstamp = 0;
1877 * nfs file rename call
1879 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1880 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1881 * struct vnode *a_tvp, struct componentname *a_tcnp)
1884 nfs_rename(struct vop_old_rename_args *ap)
1886 struct vnode *fvp = ap->a_fvp;
1887 struct vnode *tvp = ap->a_tvp;
1888 struct vnode *fdvp = ap->a_fdvp;
1889 struct vnode *tdvp = ap->a_tdvp;
1890 struct componentname *tcnp = ap->a_tcnp;
1891 struct componentname *fcnp = ap->a_fcnp;
1892 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1895 lwkt_gettoken(&nmp->nm_token);
1897 /* Check for cross-device rename */
1898 if ((fvp->v_mount != tdvp->v_mount) ||
1899 (tvp && (fvp->v_mount != tvp->v_mount))) {
1905 * We shouldn't have to flush fvp on rename for most server-side
1906 * filesystems as the file handle should not change. Unfortunately
1907 * the inode for some filesystems (msdosfs) might be tied to the
1908 * file name or directory position so to be completely safe
1909 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1912 * We must flush tvp on rename because it might become stale on the
1913 * server after the rename.
1915 if (nfs_flush_on_rename)
1916 VOP_FSYNC(fvp, MNT_WAIT, 0);
1918 VOP_FSYNC(tvp, MNT_WAIT, 0);
1921 * If the tvp exists and is in use, sillyrename it before doing the
1922 * rename of the new file over it.
1924 * XXX Can't sillyrename a directory.
1926 * We do not attempt to do any namecache purges in this old API
1927 * routine. The new API compat functions have access to the actual
1928 * namecache structures and will do it for us.
1930 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1931 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1938 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1939 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1943 lwkt_reltoken(&nmp->nm_token);
1953 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1955 if (error == ENOENT)
1961 * nfs file rename rpc called from nfs_remove() above
1964 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1965 struct sillyrename *sp)
1967 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1968 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1972 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1975 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1976 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1977 struct ucred *cred, struct thread *td)
1979 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1980 struct nfsm_info info;
1983 info.v3 = NFS_ISV3(fdvp);
1985 nfsstats.rpccnt[NFSPROC_RENAME]++;
1986 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1987 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1988 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1989 ERROROUT(nfsm_fhtom(&info, fdvp));
1990 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1991 ERROROUT(nfsm_fhtom(&info, tdvp));
1992 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1993 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1995 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
1996 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2001 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2002 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2004 VTONFS(fdvp)->n_attrstamp = 0;
2006 VTONFS(tdvp)->n_attrstamp = 0;
2011 * nfs hard link create call
2013 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2014 * struct componentname *a_cnp)
2017 nfs_link(struct vop_old_link_args *ap)
2019 struct vnode *vp = ap->a_vp;
2020 struct vnode *tdvp = ap->a_tdvp;
2021 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2022 struct componentname *cnp = ap->a_cnp;
2023 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2024 struct nfsm_info info;
2026 if (vp->v_mount != tdvp->v_mount) {
2029 lwkt_gettoken(&nmp->nm_token);
2032 * The attribute cache may get out of sync with the server on link.
2033 * Pushing writes to the server before handle was inherited from
2034 * long long ago and it is unclear if we still need to do this.
2037 if (nfs_flush_on_hlink)
2038 VOP_FSYNC(vp, MNT_WAIT, 0);
2041 info.v3 = NFS_ISV3(vp);
2043 nfsstats.rpccnt[NFSPROC_LINK]++;
2044 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2045 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2046 nfsm_rndup(cnp->cn_namelen));
2047 ERROROUT(nfsm_fhtom(&info, vp));
2048 ERROROUT(nfsm_fhtom(&info, tdvp));
2049 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2051 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2052 cnp->cn_cred, &error));
2054 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2055 NFS_LATTR_NOSHRINK));
2056 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2061 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2063 VTONFS(vp)->n_attrstamp = 0;
2065 VTONFS(tdvp)->n_attrstamp = 0;
2067 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2069 if (error == EEXIST)
2071 lwkt_reltoken(&nmp->nm_token);
2076 * nfs symbolic link create call
2078 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2079 * struct componentname *a_cnp, struct vattr *a_vap,
2083 nfs_symlink(struct vop_old_symlink_args *ap)
2085 struct vnode *dvp = ap->a_dvp;
2086 struct vattr *vap = ap->a_vap;
2087 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2088 struct componentname *cnp = ap->a_cnp;
2089 struct nfsv2_sattr *sp;
2090 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2091 struct vnode *newvp = NULL;
2092 struct nfsm_info info;
2095 info.v3 = NFS_ISV3(dvp);
2096 lwkt_gettoken(&nmp->nm_token);
2098 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2099 slen = strlen(ap->a_target);
2100 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2101 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2102 nfsm_rndup(cnp->cn_namelen) +
2103 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2104 ERROROUT(nfsm_fhtom(&info, dvp));
2105 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2108 nfsm_v3attrbuild(&info, vap, FALSE);
2110 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2112 sp = nfsm_build(&info, NFSX_V2SATTR);
2113 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2114 sp->sa_uid = nfs_xdrneg1;
2115 sp->sa_gid = nfs_xdrneg1;
2116 sp->sa_size = nfs_xdrneg1;
2117 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2118 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2122 * Issue the NFS request and get the rpc response.
2124 * Only NFSv3 responses returning an error of 0 actually return
2125 * a file handle that can be converted into newvp without having
2126 * to do an extra lookup rpc.
2128 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2129 cnp->cn_cred, &error));
2132 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2134 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2138 * out code jumps -> here, mrep is also freed.
2146 * If we get an EEXIST error, silently convert it to no-error
2147 * in case of an NFS retry.
2149 if (error == EEXIST)
2153 * If we do not have (or no longer have) an error, and we could
2154 * not extract the newvp from the response due to the request being
2155 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2156 * to obtain a newvp to return.
2158 if (error == 0 && newvp == NULL) {
2159 struct nfsnode *np = NULL;
2161 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2162 cnp->cn_cred, cnp->cn_td, &np);
2172 VTONFS(dvp)->n_flag |= NLMODIFIED;
2174 VTONFS(dvp)->n_attrstamp = 0;
2175 lwkt_reltoken(&nmp->nm_token);
2183 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2184 * struct componentname *a_cnp, struct vattr *a_vap)
2187 nfs_mkdir(struct vop_old_mkdir_args *ap)
2189 struct vnode *dvp = ap->a_dvp;
2190 struct vattr *vap = ap->a_vap;
2191 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2192 struct componentname *cnp = ap->a_cnp;
2193 struct nfsv2_sattr *sp;
2194 struct nfsnode *np = NULL;
2195 struct vnode *newvp = NULL;
2197 int error = 0, wccflag = NFSV3_WCCRATTR;
2200 struct nfsm_info info;
2203 info.v3 = NFS_ISV3(dvp);
2204 lwkt_gettoken(&nmp->nm_token);
2206 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2207 lwkt_reltoken(&nmp->nm_token);
2210 len = cnp->cn_namelen;
2211 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2212 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2213 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2214 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2215 ERROROUT(nfsm_fhtom(&info, dvp));
2216 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2218 nfsm_v3attrbuild(&info, vap, FALSE);
2220 sp = nfsm_build(&info, NFSX_V2SATTR);
2221 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2222 sp->sa_uid = nfs_xdrneg1;
2223 sp->sa_gid = nfs_xdrneg1;
2224 sp->sa_size = nfs_xdrneg1;
2225 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2226 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2228 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2229 cnp->cn_cred, &error));
2231 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2234 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2239 VTONFS(dvp)->n_flag |= NLMODIFIED;
2241 VTONFS(dvp)->n_attrstamp = 0;
2243 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2244 * if we can succeed in looking up the directory.
2246 if (error == EEXIST || (!error && !gotvp)) {
2251 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2255 if (newvp->v_type != VDIR)
2265 lwkt_reltoken(&nmp->nm_token);
2270 * nfs remove directory call
2272 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2273 * struct componentname *a_cnp)
2276 nfs_rmdir(struct vop_old_rmdir_args *ap)
2278 struct vnode *vp = ap->a_vp;
2279 struct vnode *dvp = ap->a_dvp;
2280 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2281 struct componentname *cnp = ap->a_cnp;
2282 int error = 0, wccflag = NFSV3_WCCRATTR;
2283 struct nfsm_info info;
2286 info.v3 = NFS_ISV3(dvp);
2291 lwkt_gettoken(&nmp->nm_token);
2293 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2294 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2295 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2296 nfsm_rndup(cnp->cn_namelen));
2297 ERROROUT(nfsm_fhtom(&info, dvp));
2298 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2300 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2301 cnp->cn_cred, &error));
2303 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2308 VTONFS(dvp)->n_flag |= NLMODIFIED;
2310 VTONFS(dvp)->n_attrstamp = 0;
2312 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2314 if (error == ENOENT)
2316 lwkt_reltoken(&nmp->nm_token);
2324 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2327 nfs_readdir(struct vop_readdir_args *ap)
2329 struct vnode *vp = ap->a_vp;
2330 struct nfsnode *np = VTONFS(vp);
2331 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2332 struct uio *uio = ap->a_uio;
2336 if (vp->v_type != VDIR)
2339 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2342 lwkt_gettoken(&nmp->nm_token);
2345 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2346 * and then check that is still valid, or if this is an NQNFS mount
2347 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2348 * VOP_GETATTR() does not necessarily go to the wire.
2350 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2351 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2352 if (VOP_GETATTR(vp, &vattr) == 0 &&
2353 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2355 nfsstats.direofcache_hits++;
2361 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2362 * own cache coherency checks so we do not have to.
2364 tresid = uio->uio_resid;
2365 error = nfs_bioread(vp, uio, 0);
2367 if (!error && uio->uio_resid == tresid)
2368 nfsstats.direofcache_misses++;
2370 lwkt_reltoken(&nmp->nm_token);
2377 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2379 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2380 * offset/block and converts the nfs formatted directory entries for userland
2381 * consumption as well as deals with offsets into the middle of blocks.
2382 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2383 * be block-bounded. It must convert to cookies for the actual RPC.
2386 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2389 struct nfs_dirent *dp = NULL;
2394 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2395 struct nfsnode *dnp = VTONFS(vp);
2397 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2399 struct nfsm_info info;
2402 info.v3 = NFS_ISV3(vp);
2405 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2406 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2407 panic("nfs readdirrpc bad uio");
2411 * If there is no cookie, assume directory was stale.
2413 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2417 return (NFSERR_BAD_COOKIE);
2419 * Loop around doing readdir rpc's of size nm_readdirsize
2420 * truncated to a multiple of DIRBLKSIZ.
2421 * The stopping criteria is EOF or buffer full.
2423 while (more_dirs && bigenough) {
2424 nfsstats.rpccnt[NFSPROC_READDIR]++;
2425 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2426 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2427 ERROROUT(nfsm_fhtom(&info, vp));
2429 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2430 *tl++ = cookie.nfsuquad[0];
2431 *tl++ = cookie.nfsuquad[1];
2432 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2433 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2436 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2437 * WITH NFSv2!!! There's nothing I can really do
2438 * about it other than to hope the server supports
2441 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2442 *tl++ = cookie.nfsuquad[0];
2444 *tl = txdr_unsigned(nmp->nm_readdirsize);
2445 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2447 nfs_vpcred(vp, ND_READ), &error));
2449 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2450 NFS_LATTR_NOSHRINK));
2451 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2452 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2453 dnp->n_cookieverf.nfsuquad[1] = *tl;
2455 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2456 more_dirs = fxdr_unsigned(int, *tl);
2458 /* loop thru the dir entries, converting them to std form */
2459 while (more_dirs && bigenough) {
2461 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2462 fileno = fxdr_hyper(tl);
2463 len = fxdr_unsigned(int, *(tl + 2));
2465 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2466 fileno = fxdr_unsigned(u_quad_t, *tl++);
2467 len = fxdr_unsigned(int, *tl);
2469 if (len <= 0 || len > NFS_MAXNAMLEN) {
2477 * len is the number of bytes in the path element
2478 * name, not including the \0 termination.
2480 * tlen is the number of bytes w have to reserve for
2481 * the path element name.
2483 tlen = nfsm_rndup(len);
2485 tlen += 4; /* To ensure null termination */
2488 * If the entry would cross a DIRBLKSIZ boundary,
2489 * extend the previous nfs_dirent to cover the
2492 left = DIRBLKSIZ - blksiz;
2493 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2494 dp->nfs_reclen += left;
2495 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2496 uiop->uio_iov->iov_len -= left;
2497 uiop->uio_offset += left;
2498 uiop->uio_resid -= left;
2501 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2504 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2505 dp->nfs_ino = fileno;
2506 dp->nfs_namlen = len;
2507 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2508 dp->nfs_type = DT_UNKNOWN;
2509 blksiz += dp->nfs_reclen;
2510 if (blksiz == DIRBLKSIZ)
2512 uiop->uio_offset += sizeof(struct nfs_dirent);
2513 uiop->uio_resid -= sizeof(struct nfs_dirent);
2514 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2515 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2516 ERROROUT(nfsm_mtouio(&info, uiop, len));
2519 * The uiop has advanced by nfs_dirent + len
2520 * but really needs to advance by
2523 cp = uiop->uio_iov->iov_base;
2525 *cp = '\0'; /* null terminate */
2526 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2527 uiop->uio_iov->iov_len -= tlen;
2528 uiop->uio_offset += tlen;
2529 uiop->uio_resid -= tlen;
2532 * NFS strings must be rounded up (nfsm_myouio
2533 * handled that in the bigenough case).
2535 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2538 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2540 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2544 * If we were able to accomodate the last entry,
2545 * get the cookie for the next one. Otherwise
2546 * hold-over the cookie for the one we were not
2547 * able to accomodate.
2550 cookie.nfsuquad[0] = *tl++;
2552 cookie.nfsuquad[1] = *tl++;
2553 } else if (info.v3) {
2558 more_dirs = fxdr_unsigned(int, *tl);
2561 * If at end of rpc data, get the eof boolean
2564 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2565 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2571 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2572 * by increasing d_reclen for the last record.
2575 left = DIRBLKSIZ - blksiz;
2576 dp->nfs_reclen += left;
2577 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2578 uiop->uio_iov->iov_len -= left;
2579 uiop->uio_offset += left;
2580 uiop->uio_resid -= left;
2585 * We hit the end of the directory, update direofoffset.
2587 dnp->n_direofoffset = uiop->uio_offset;
2590 * There is more to go, insert the link cookie so the
2591 * next block can be read.
2593 if (uiop->uio_resid > 0)
2594 kprintf("EEK! readdirrpc resid > 0\n");
2595 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2603 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2606 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2609 struct nfs_dirent *dp;
2611 struct vnode *newvp;
2613 caddr_t dpossav1, dpossav2;
2615 struct mbuf *mdsav1, *mdsav2;
2617 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2618 struct nfsnode *dnp = VTONFS(vp), *np;
2621 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2622 int attrflag, fhsize;
2623 struct nchandle nch;
2624 struct nchandle dnch;
2625 struct nlcomponent nlc;
2626 struct nfsm_info info;
2635 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2636 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2637 panic("nfs readdirplusrpc bad uio");
2640 * Obtain the namecache record for the directory so we have something
2641 * to use as a basis for creating the entries. This function will
2642 * return a held (but not locked) ncp. The ncp may be disconnected
2643 * from the tree and cannot be used for upward traversals, and the
2644 * ncp may be unnamed. Note that other unrelated operations may
2645 * cause the ncp to be named at any time.
2647 * We have to lock the ncp to prevent a lock order reversal when
2648 * rdirplus does nlookups of the children, because the vnode is
2649 * locked and has to stay that way.
2651 cache_fromdvp(vp, NULL, 0, &dnch);
2652 bzero(&nlc, sizeof(nlc));
2656 * If there is no cookie, assume directory was stale.
2658 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2664 return (NFSERR_BAD_COOKIE);
2668 * Loop around doing readdir rpc's of size nm_readdirsize
2669 * truncated to a multiple of DIRBLKSIZ.
2670 * The stopping criteria is EOF or buffer full.
2672 while (more_dirs && bigenough) {
2673 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2674 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2675 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2676 ERROROUT(nfsm_fhtom(&info, vp));
2677 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2678 *tl++ = cookie.nfsuquad[0];
2679 *tl++ = cookie.nfsuquad[1];
2680 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2681 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2682 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2683 *tl = txdr_unsigned(nmp->nm_rsize);
2684 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2686 nfs_vpcred(vp, ND_READ), &error));
2687 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2688 NFS_LATTR_NOSHRINK));
2689 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2690 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2691 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2692 more_dirs = fxdr_unsigned(int, *tl);
2694 /* loop thru the dir entries, doctoring them to 4bsd form */
2695 while (more_dirs && bigenough) {
2696 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2697 fileno = fxdr_hyper(tl);
2698 len = fxdr_unsigned(int, *(tl + 2));
2699 if (len <= 0 || len > NFS_MAXNAMLEN) {
2705 tlen = nfsm_rndup(len);
2707 tlen += 4; /* To ensure null termination*/
2708 left = DIRBLKSIZ - blksiz;
2709 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2710 dp->nfs_reclen += left;
2711 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2712 uiop->uio_iov->iov_len -= left;
2713 uiop->uio_offset += left;
2714 uiop->uio_resid -= left;
2717 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2720 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2721 dp->nfs_ino = fileno;
2722 dp->nfs_namlen = len;
2723 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2724 dp->nfs_type = DT_UNKNOWN;
2725 blksiz += dp->nfs_reclen;
2726 if (blksiz == DIRBLKSIZ)
2728 uiop->uio_offset += sizeof(struct nfs_dirent);
2729 uiop->uio_resid -= sizeof(struct nfs_dirent);
2730 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2731 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2732 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2733 nlc.nlc_namelen = len;
2734 ERROROUT(nfsm_mtouio(&info, uiop, len));
2735 cp = uiop->uio_iov->iov_base;
2738 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2739 uiop->uio_iov->iov_len -= tlen;
2740 uiop->uio_offset += tlen;
2741 uiop->uio_resid -= tlen;
2743 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2745 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2747 cookie.nfsuquad[0] = *tl++;
2748 cookie.nfsuquad[1] = *tl++;
2754 * Since the attributes are before the file handle
2755 * (sigh), we must skip over the attributes and then
2756 * come back and get them.
2758 attrflag = fxdr_unsigned(int, *tl);
2760 dpossav1 = info.dpos;
2762 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2763 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2764 doit = fxdr_unsigned(int, *tl);
2766 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2768 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2769 !NFS_CMPFH(dnp, fhp, fhsize)
2773 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2774 nlc.nlc_namelen, nlc.nlc_namelen,
2778 * This is a bit hokey but there isn't
2779 * much we can do about it. We can't
2780 * hold the directory vp locked while
2781 * doing lookups and gets.
2783 nch = cache_nlookup_nonblock(&dnch, &nlc);
2784 if (nch.ncp == NULL)
2786 cache_setunresolved(&nch);
2787 error = nfs_nget_nonblock(vp->v_mount, fhp,
2795 dpossav2 = info.dpos;
2796 info.dpos = dpossav1;
2799 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2800 info.dpos = dpossav2;
2803 IFTODT(VTTOIF(np->n_vattr.va_type));
2804 nfs_cache_setvp(&nch, newvp,
2805 nfspos_cache_timeout);
2813 kprintf("Warning: NFS/rddirplus, "
2814 "UNABLE TO ENTER %*.*s\n",
2815 nlc.nlc_namelen, nlc.nlc_namelen,
2821 /* Just skip over the file handle */
2822 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2823 i = fxdr_unsigned(int, *tl);
2824 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2826 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2827 more_dirs = fxdr_unsigned(int, *tl);
2830 * If at end of rpc data, get the eof boolean
2833 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2834 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2840 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2841 * by increasing d_reclen for the last record.
2844 left = DIRBLKSIZ - blksiz;
2845 dp->nfs_reclen += left;
2846 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2847 uiop->uio_iov->iov_len -= left;
2848 uiop->uio_offset += left;
2849 uiop->uio_resid -= left;
2853 * We are now either at the end of the directory or have filled the
2857 dnp->n_direofoffset = uiop->uio_offset;
2859 if (uiop->uio_resid > 0)
2860 kprintf("EEK! readdirplusrpc resid > 0\n");
2861 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2865 if (newvp != NULLVP) {
2878 * Silly rename. To make the NFS filesystem that is stateless look a little
2879 * more like the "ufs" a remove of an active vnode is translated to a rename
2880 * to a funny looking filename that is removed by nfs_inactive on the
2881 * nfsnode. There is the potential for another process on a different client
2882 * to create the same funny name between the nfs_lookitup() fails and the
2883 * nfs_rename() completes, but...
2886 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2888 struct sillyrename *sp;
2893 * We previously purged dvp instead of vp. I don't know why, it
2894 * completely destroys performance. We can't do it anyway with the
2895 * new VFS API since we would be breaking the namecache topology.
2897 cache_purge(vp); /* XXX */
2900 if (vp->v_type == VDIR)
2901 panic("nfs: sillyrename dir");
2903 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2904 sp->s_cred = crdup(cnp->cn_cred);
2908 /* Fudge together a funny name */
2909 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2910 (int)(intptr_t)cnp->cn_td);
2912 /* Try lookitups until we get one that isn't there */
2913 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2914 cnp->cn_td, NULL) == 0) {
2916 if (sp->s_name[4] > 'z') {
2921 error = nfs_renameit(dvp, cnp, sp);
2924 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2926 np->n_sillyrename = sp;
2931 kfree((caddr_t)sp, M_NFSREQ);
2936 * Look up a file name and optionally either update the file handle or
2937 * allocate an nfsnode, depending on the value of npp.
2938 * npp == NULL --> just do the lookup
2939 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2941 * *npp != NULL --> update the file handle in the vnode
2944 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2945 struct thread *td, struct nfsnode **npp)
2947 struct vnode *newvp = NULL;
2948 struct nfsnode *np, *dnp = VTONFS(dvp);
2949 int error = 0, fhlen, attrflag;
2951 struct nfsm_info info;
2954 info.v3 = NFS_ISV3(dvp);
2956 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2957 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2958 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2959 ERROROUT(nfsm_fhtom(&info, dvp));
2960 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2961 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2962 if (npp && !error) {
2963 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2966 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2967 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2968 np->n_fhp = &np->n_fh;
2969 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2970 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2971 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2972 np->n_fhsize = fhlen;
2974 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2978 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2987 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2988 NFS_LATTR_NOSHRINK));
2989 if (!attrflag && *npp == NULL) {
2999 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3005 if (npp && *npp == NULL) {
3020 * Nfs Version 3 commit rpc
3022 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3026 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3028 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3029 int error = 0, wccflag = NFSV3_WCCRATTR;
3030 struct nfsm_info info;
3036 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3038 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3039 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3040 ERROROUT(nfsm_fhtom(&info, vp));
3041 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3042 txdr_hyper(offset, tl);
3044 *tl = txdr_unsigned(cnt);
3045 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3046 nfs_vpcred(vp, ND_WRITE), &error));
3047 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3049 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3050 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3051 NFSX_V3WRITEVERF)) {
3052 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3054 error = NFSERR_STALEWRITEVERF;
3065 * - make nfs_bmap() essentially a no-op that does no translation
3066 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3067 * (Maybe I could use the process's page mapping, but I was concerned that
3068 * Kernel Write might not be enabled and also figured copyout() would do
3069 * a lot more work than bcopy() and also it currently happens in the
3070 * context of the swapper process (2).
3072 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3073 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3076 nfs_bmap(struct vop_bmap_args *ap)
3078 /* no token lock required */
3079 if (ap->a_doffsetp != NULL)
3080 *ap->a_doffsetp = ap->a_loffset;
3081 if (ap->a_runp != NULL)
3083 if (ap->a_runb != NULL)
3092 nfs_strategy(struct vop_strategy_args *ap)
3094 struct bio *bio = ap->a_bio;
3096 struct buf *bp __debugvar = bio->bio_buf;
3097 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3101 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3102 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3103 KASSERT(BUF_REFCNT(bp) > 0,
3104 ("nfs_strategy: buffer %p not locked", bp));
3106 if (bio->bio_flags & BIO_SYNC)
3107 td = curthread; /* XXX */
3111 lwkt_gettoken(&nmp->nm_token);
3114 * We probably don't need to push an nbio any more since no
3115 * block conversion is required due to the use of 64 bit byte
3116 * offsets, but do it anyway.
3118 * NOTE: When NFS callers itself via this strategy routines and
3119 * sets up a synchronous I/O, it expects the I/O to run
3120 * synchronously (its bio_done routine just assumes it),
3121 * so for now we have to honor the bit.
3123 nbio = push_bio(bio);
3124 nbio->bio_offset = bio->bio_offset;
3125 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3128 * If the op is asynchronous and an i/o daemon is waiting
3129 * queue the request, wake it up and wait for completion
3130 * otherwise just do it ourselves.
3132 if (bio->bio_flags & BIO_SYNC) {
3133 error = nfs_doio(ap->a_vp, nbio, td);
3135 nfs_asyncio(ap->a_vp, nbio);
3138 lwkt_reltoken(&nmp->nm_token);
3146 * NB Currently unsupported.
3148 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3152 nfs_mmap(struct vop_mmap_args *ap)
3154 /* no token lock required */
3159 * fsync vnode op. Just call nfs_flush() with commit == 1.
3161 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3165 nfs_fsync(struct vop_fsync_args *ap)
3167 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3170 lwkt_gettoken(&nmp->nm_token);
3173 * NOTE: Because attributes are set synchronously we currently
3174 * do not have to implement vsetisdirty()/vclrisdirty().
3176 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3178 lwkt_reltoken(&nmp->nm_token);
3184 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3185 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3186 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3187 * set the buffer contains data that has already been written to the server
3188 * and which now needs a commit RPC.
3190 * If commit is 0 we only take one pass and only flush buffers containing new
3193 * If commit is 1 we take two passes, issuing a commit RPC in the second
3196 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3197 * to completely flush all pending data.
3199 * Note that the RB_SCAN code properly handles the case where the
3200 * callback might block and directly or indirectly (another thread) cause
3201 * the RB tree to change.
3204 #ifndef NFS_COMMITBVECSIZ
3205 #define NFS_COMMITBVECSIZ 16
3208 struct nfs_flush_info {
3209 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3216 struct buf *bvary[NFS_COMMITBVECSIZ];
3222 static int nfs_flush_bp(struct buf *bp, void *data);
3223 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3226 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3228 struct nfsnode *np = VTONFS(vp);
3229 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3230 struct nfs_flush_info info;
3233 bzero(&info, sizeof(info));
3236 info.waitfor = waitfor;
3237 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3239 lwkt_gettoken(&vp->v_token);
3245 info.mode = NFI_FLUSHNEW;
3246 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3247 nfs_flush_bp, &info);
3250 * Take a second pass if committing and no error occured.
3251 * Clean up any left over collection (whether an error
3254 if (commit && error == 0) {
3255 info.mode = NFI_COMMIT;
3256 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3257 nfs_flush_bp, &info);
3259 error = nfs_flush_docommit(&info, error);
3263 * Wait for pending I/O to complete before checking whether
3264 * any further dirty buffers exist.
3266 while (waitfor == MNT_WAIT &&
3267 bio_track_active(&vp->v_track_write)) {
3268 error = bio_track_wait(&vp->v_track_write,
3269 info.slpflag, info.slptimeo);
3272 * We have to be able to break out if this
3273 * is an 'intr' mount.
3275 if (nfs_sigintr(nmp, NULL, td)) {
3281 * Since we do not process pending signals,
3282 * once we get a PCATCH our tsleep() will no
3283 * longer sleep, switch to a fixed timeout
3286 if (info.slpflag == PCATCH) {
3288 info.slptimeo = 2 * hz;
3295 * Loop if we are flushing synchronous as well as committing,
3296 * and dirty buffers are still present. Otherwise we might livelock.
3298 } while (waitfor == MNT_WAIT && commit &&
3299 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3302 * The callbacks have to return a negative error to terminate the
3309 * Deal with any error collection
3311 if (np->n_flag & NWRITEERR) {
3312 error = np->n_error;
3313 np->n_flag &= ~NWRITEERR;
3315 lwkt_reltoken(&vp->v_token);
3321 nfs_flush_bp(struct buf *bp, void *data)
3323 struct nfs_flush_info *info = data;
3329 switch(info->mode) {
3331 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3332 if (error && info->loops && info->waitfor == MNT_WAIT) {
3333 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3335 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3336 if (info->slpflag & PCATCH)
3337 lkflags |= LK_PCATCH;
3338 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3344 * Ignore locking errors
3352 * The buffer may have changed out from under us, even if
3353 * we did not block (MPSAFE). Check again now that it is
3356 if (bp->b_vp == info->vp &&
3357 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3366 * Only process buffers in need of a commit which we can
3367 * immediately lock. This may prevent a buffer from being
3368 * committed, but the normal flush loop will block on the
3369 * same buffer so we shouldn't get into an endless loop.
3371 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3372 (B_DELWRI | B_NEEDCOMMIT)) {
3375 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3379 * We must recheck after successfully locking the buffer.
3381 if (bp->b_vp != info->vp ||
3382 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3383 (B_DELWRI | B_NEEDCOMMIT)) {
3389 * NOTE: storing the bp in the bvary[] basically sets
3390 * it up for a commit operation.
3392 * We must call vfs_busy_pages() now so the commit operation
3393 * is interlocked with user modifications to memory mapped
3394 * pages. The b_dirtyoff/b_dirtyend range is not correct
3395 * until after the pages have been busied.
3397 * Note: to avoid loopback deadlocks, we do not
3398 * assign b_runningbufspace.
3401 bp->b_cmd = BUF_CMD_WRITE;
3402 vfs_busy_pages(bp->b_vp, bp);
3403 info->bvary[info->bvsize] = bp;
3404 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3405 if (info->bvsize == 0 || toff < info->beg_off)
3406 info->beg_off = toff;
3407 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3408 if (info->bvsize == 0 || toff > info->end_off)
3409 info->end_off = toff;
3411 if (info->bvsize == NFS_COMMITBVECSIZ) {
3412 error = nfs_flush_docommit(info, 0);
3413 KKASSERT(info->bvsize == 0);
3421 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3431 if (info->bvsize > 0) {
3433 * Commit data on the server, as required. Note that
3434 * nfs_commit will use the vnode's cred for the commit.
3435 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3437 bytes = info->end_off - info->beg_off;
3438 if (bytes > 0x40000000)
3443 retv = nfs_commitrpc_uio(vp, info->beg_off,
3444 (int)bytes, info->td);
3445 if (retv == NFSERR_STALEWRITEVERF)
3446 nfs_clearcommit(vp->v_mount);
3450 * Now, either mark the blocks I/O done or mark the
3451 * blocks dirty, depending on whether the commit
3454 for (i = 0; i < info->bvsize; ++i) {
3455 bp = info->bvary[i];
3456 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3458 * Either an error or the original
3459 * vfs_busy_pages() cleared B_NEEDCOMMIT
3460 * due to finding new dirty VM pages in
3463 * Leave B_DELWRI intact.
3465 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3466 vfs_unbusy_pages(bp);
3467 bp->b_cmd = BUF_CMD_DONE;
3471 * Success, remove B_DELWRI ( bundirty() ).
3473 * b_dirtyoff/b_dirtyend seem to be NFS
3474 * specific. We should probably move that
3475 * into bundirty(). XXX
3477 * We are faking an I/O write, we have to
3478 * start the transaction in order to
3479 * immediately biodone() it.
3482 bp->b_flags &= ~B_ERROR;
3483 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3484 bp->b_dirtyoff = bp->b_dirtyend = 0;
3485 biodone(&bp->b_bio1);
3494 * NFS advisory byte-level locks.
3495 * Currently unsupported.
3497 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3501 nfs_advlock(struct vop_advlock_args *ap)
3503 struct nfsnode *np = VTONFS(ap->a_vp);
3505 /* no token lock currently required */
3507 * The following kludge is to allow diskless support to work
3508 * until a real NFS lockd is implemented. Basically, just pretend
3509 * that this is a local lock.
3511 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3515 * Print out the contents of an nfsnode.
3517 * nfs_print(struct vnode *a_vp)
3520 nfs_print(struct vop_print_args *ap)
3522 struct vnode *vp = ap->a_vp;
3523 struct nfsnode *np = VTONFS(vp);
3525 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3526 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3527 if (vp->v_type == VFIFO)
3534 * nfs special file access vnode op.
3536 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3539 nfs_laccess(struct vop_access_args *ap)
3541 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3545 lwkt_gettoken(&nmp->nm_token);
3546 error = VOP_GETATTR(ap->a_vp, &vattr);
3548 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3551 lwkt_reltoken(&nmp->nm_token);
3557 * Read wrapper for fifos.
3559 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3560 * struct ucred *a_cred)
3563 nfsfifo_read(struct vop_read_args *ap)
3565 struct nfsnode *np = VTONFS(ap->a_vp);
3567 /* no token access required */
3572 getnanotime(&np->n_atim);
3573 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3577 * Write wrapper for fifos.
3579 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3580 * struct ucred *a_cred)
3583 nfsfifo_write(struct vop_write_args *ap)
3585 struct nfsnode *np = VTONFS(ap->a_vp);
3587 /* no token access required */
3592 getnanotime(&np->n_mtim);
3593 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3597 * Close wrapper for fifos.
3599 * Update the times on the nfsnode then do fifo close.
3601 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3604 nfsfifo_close(struct vop_close_args *ap)
3606 struct vnode *vp = ap->a_vp;
3607 struct nfsnode *np = VTONFS(vp);
3611 /* no token access required */
3613 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3614 if (np->n_flag & (NACC | NUPD)) {
3616 if (np->n_flag & NACC)
3618 if (np->n_flag & NUPD)
3621 if (VREFCNT(vp) == 1 &&
3622 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3624 if (np->n_flag & NACC)
3625 vattr.va_atime = np->n_atim;
3626 if (np->n_flag & NUPD)
3627 vattr.va_mtime = np->n_mtim;
3628 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3631 return (VOCALL(&fifo_vnode_vops, &ap->a_head));