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 lwkt_gettoken(&nmp->nm_token);
614 if (vp->v_type == VREG) {
615 if (np->n_flag & NLMODIFIED) {
618 * Under NFSv3 we have dirty buffers to dispose of. We
619 * must flush them to the NFS server. We have the option
620 * of waiting all the way through the commit rpc or just
621 * waiting for the initial write. The default is to only
622 * wait through the initial write so the data is in the
623 * server's cache, which is roughly similar to the state
624 * a standard disk subsystem leaves the file in on close().
626 * We cannot clear the NLMODIFIED bit in np->n_flag due to
627 * potential races with other processes, and certainly
628 * cannot clear it if we don't commit.
630 int cm = nfsv3_commit_on_close ? 1 : 0;
631 error = nfs_flush(vp, MNT_WAIT, td, cm);
632 /* np->n_flag &= ~NLMODIFIED; */
634 error = nfs_vinvalbuf(vp, V_SAVE, 1);
638 if (np->n_flag & NWRITEERR) {
639 np->n_flag &= ~NWRITEERR;
644 lwkt_reltoken(&nmp->nm_token);
650 * nfs getattr call from vfs.
652 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
655 nfs_getattr(struct vop_getattr_args *ap)
657 struct vnode *vp = ap->a_vp;
658 struct nfsnode *np = VTONFS(vp);
659 struct nfsmount *nmp;
661 thread_t td = curthread;
662 struct nfsm_info info;
665 info.v3 = NFS_ISV3(vp);
666 nmp = VFSTONFS(vp->v_mount);
668 lwkt_gettoken(&nmp->nm_token);
671 * Update local times for special files.
673 if (np->n_flag & (NACC | NUPD))
676 * First look in the cache.
678 if (nfs_getattrcache(vp, ap->a_vap) == 0)
681 if (info.v3 && nfsaccess_cache_timeout > 0) {
682 nfsstats.accesscache_misses++;
683 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
684 if (nfs_getattrcache(vp, ap->a_vap) == 0)
688 nfsstats.rpccnt[NFSPROC_GETATTR]++;
689 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
690 ERROROUT(nfsm_fhtom(&info, vp));
691 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
692 nfs_vpcred(vp, ND_CHECK), &error));
694 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
700 * NFS doesn't support chflags flags. If the nfs mount was
701 * made -o cache set the UF_CACHE bit for swapcache.
703 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
704 ap->a_vap->va_flags |= UF_CACHE;
706 lwkt_reltoken(&nmp->nm_token);
713 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
716 nfs_setattr(struct vop_setattr_args *ap)
718 struct vnode *vp = ap->a_vp;
719 struct nfsnode *np = VTONFS(vp);
720 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
721 struct vattr *vap = ap->a_vap;
724 thread_t td = curthread;
730 * Setting of flags is not supported.
732 if (vap->va_flags != VNOVAL)
736 * Disallow write attempts if the filesystem is mounted read-only.
738 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
739 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
740 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
741 (vp->v_mount->mnt_flag & MNT_RDONLY))
744 lwkt_gettoken(&nmp->nm_token);
746 if (vap->va_size != VNOVAL) {
748 * truncation requested
750 switch (vp->v_type) {
752 lwkt_reltoken(&nmp->nm_token);
758 if (vap->va_mtime.tv_sec == VNOVAL &&
759 vap->va_atime.tv_sec == VNOVAL &&
760 vap->va_mode == (mode_t)VNOVAL &&
761 vap->va_uid == (uid_t)VNOVAL &&
762 vap->va_gid == (gid_t)VNOVAL) {
763 lwkt_reltoken(&nmp->nm_token);
766 vap->va_size = VNOVAL;
770 * Disallow write attempts if the filesystem is
773 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
774 lwkt_reltoken(&nmp->nm_token);
780 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
783 if (np->n_flag & NLMODIFIED) {
784 if (vap->va_size == 0)
785 error = nfs_vinvalbuf(vp, 0, 1);
787 error = nfs_vinvalbuf(vp, V_SAVE, 1);
791 * note: this loop case almost always happens at
792 * least once per truncation.
794 if (error == 0 && np->n_size != vap->va_size)
796 np->n_vattr.va_size = vap->va_size;
799 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
801 * What to do. If we are modifying the mtime we lose
802 * mtime detection of changes made by the server or other
803 * clients. But programs like rsync/rdist/cpdup are going
804 * to call utimes a lot. We don't want to piecemeal sync.
806 * For now sync if any prior remote changes were detected,
807 * but allow us to lose track of remote changes made during
808 * the utimes operation.
810 if (np->n_flag & NRMODIFIED)
811 error = nfs_vinvalbuf(vp, V_SAVE, 1);
815 if (vap->va_mtime.tv_sec != VNOVAL) {
816 np->n_mtime = vap->va_mtime.tv_sec;
820 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
823 * Sanity check if a truncation was issued. This should only occur
824 * if multiple processes are racing on the same file.
826 if (error == 0 && vap->va_size != VNOVAL &&
827 np->n_size != vap->va_size) {
828 kprintf("NFS ftruncate: server disagrees on the file size: "
831 (intmax_t)vap->va_size,
832 (intmax_t)np->n_size);
835 if (error && vap->va_size != VNOVAL) {
836 np->n_size = np->n_vattr.va_size = tsize;
837 nfs_meta_setsize(vp, td, np->n_size, 0);
839 lwkt_reltoken(&nmp->nm_token);
845 * Do an nfs setattr rpc.
848 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
849 struct ucred *cred, struct thread *td)
851 struct nfsv2_sattr *sp;
852 struct nfsnode *np = VTONFS(vp);
854 int error = 0, wccflag = NFSV3_WCCRATTR;
855 struct nfsm_info info;
858 info.v3 = NFS_ISV3(vp);
860 nfsstats.rpccnt[NFSPROC_SETATTR]++;
861 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
862 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
863 ERROROUT(nfsm_fhtom(&info, vp));
865 nfsm_v3attrbuild(&info, vap, TRUE);
866 tl = nfsm_build(&info, NFSX_UNSIGNED);
869 sp = nfsm_build(&info, NFSX_V2SATTR);
870 if (vap->va_mode == (mode_t)VNOVAL)
871 sp->sa_mode = nfs_xdrneg1;
873 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
874 if (vap->va_uid == (uid_t)VNOVAL)
875 sp->sa_uid = nfs_xdrneg1;
877 sp->sa_uid = txdr_unsigned(vap->va_uid);
878 if (vap->va_gid == (gid_t)VNOVAL)
879 sp->sa_gid = nfs_xdrneg1;
881 sp->sa_gid = txdr_unsigned(vap->va_gid);
882 sp->sa_size = txdr_unsigned(vap->va_size);
883 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
884 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
886 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
889 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
891 ERROROUT(nfsm_loadattr(&info, vp, NULL));
901 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
907 cache_setvp(nch, vp);
908 cache_settimeout(nch, nctimeout);
912 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
913 * nfs_lookup() until all remaining new api calls are implemented.
915 * Resolve a namecache entry. This function is passed a locked ncp and
916 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
919 nfs_nresolve(struct vop_nresolve_args *ap)
921 struct thread *td = curthread;
922 struct namecache *ncp;
923 struct nfsmount *nmp;
933 struct nfsm_info info;
936 nmp = VFSTONFS(dvp->v_mount);
938 lwkt_gettoken(&nmp->nm_token);
940 if ((error = vget(dvp, LK_SHARED)) != 0) {
941 lwkt_reltoken(&nmp->nm_token);
946 info.v3 = NFS_ISV3(dvp);
949 nfsstats.lookupcache_misses++;
950 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
951 ncp = ap->a_nch->ncp;
953 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
954 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
955 ERROROUT(nfsm_fhtom(&info, dvp));
956 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
957 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
958 ap->a_cred, &error));
961 * Cache negatve lookups to reduce NFS traffic, but use
962 * a fast timeout. Otherwise use a timeout of 1 tick.
963 * XXX we should add a namecache flag for no-caching
964 * to uncache the negative hit as soon as possible, but
965 * we cannot simply destroy the entry because it is used
966 * as a placeholder by the caller.
968 * The refactored nfs code will overwrite a non-zero error
969 * with 0 when we use ERROROUT(), so don't here.
972 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
973 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
985 * Success, get the file handle, do various checks, and load
986 * post-operation data from the reply packet. Theoretically
987 * we should never be looking up "." so, theoretically, we
988 * should never get the same file handle as our directory. But
989 * we check anyway. XXX
991 * Note that no timeout is set for the positive cache hit. We
992 * assume, theoretically, that ESTALE returns will be dealt with
993 * properly to handle NFS races and in anycase we cannot depend
994 * on a timeout to deal with NFS open/create/excl issues so instead
995 * of a bad hack here the rest of the NFS client code needs to do
998 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1001 if (NFS_CMPFH(np, fhp, fhsize)) {
1005 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1010 lwkt_reltoken(&nmp->nm_token);
1016 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1017 NFS_LATTR_NOSHRINK));
1018 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1019 NFS_LATTR_NOSHRINK));
1021 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1023 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1027 lwkt_reltoken(&nmp->nm_token);
1039 * 'cached' nfs directory lookup
1041 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1043 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1044 * struct componentname *a_cnp)
1047 nfs_lookup(struct vop_old_lookup_args *ap)
1049 struct componentname *cnp = ap->a_cnp;
1050 struct vnode *dvp = ap->a_dvp;
1051 struct vnode **vpp = ap->a_vpp;
1052 int flags = cnp->cn_flags;
1053 struct vnode *newvp;
1054 struct vnode *notvp;
1055 struct nfsmount *nmp;
1059 int lockparent, wantparent, attrflag, fhsize;
1062 struct nfsm_info info;
1065 info.v3 = NFS_ISV3(dvp);
1068 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1071 * Read-only mount check and directory check.
1074 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1075 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1078 if (dvp->v_type != VDIR)
1082 * Look it up in the cache. Note that ENOENT is only returned if we
1083 * previously entered a negative hit (see later on). The additional
1084 * nfsneg_cache_timeout check causes previously cached results to
1085 * be instantly ignored if the negative caching is turned off.
1087 lockparent = flags & CNP_LOCKPARENT;
1088 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1089 nmp = VFSTONFS(dvp->v_mount);
1092 lwkt_gettoken(&nmp->nm_token);
1099 nfsstats.lookupcache_misses++;
1100 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1101 len = cnp->cn_namelen;
1102 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1103 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1104 ERROROUT(nfsm_fhtom(&info, dvp));
1105 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1106 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1107 cnp->cn_cred, &error));
1109 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1110 NFS_LATTR_NOSHRINK);
1120 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1123 * Handle RENAME case...
1125 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1126 if (NFS_CMPFH(np, fhp, fhsize)) {
1129 lwkt_reltoken(&nmp->nm_token);
1132 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1136 lwkt_reltoken(&nmp->nm_token);
1141 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1142 NFS_LATTR_NOSHRINK));
1143 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1144 NFS_LATTR_NOSHRINK));
1146 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1153 cnp->cn_flags |= CNP_PDIRUNLOCK;
1155 lwkt_reltoken(&nmp->nm_token);
1159 if (flags & CNP_ISDOTDOT) {
1161 cnp->cn_flags |= CNP_PDIRUNLOCK;
1162 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1164 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1165 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1166 lwkt_reltoken(&nmp->nm_token);
1167 return (error); /* NOTE: return error from nget */
1171 error = vn_lock(dvp, LK_EXCLUSIVE);
1174 lwkt_reltoken(&nmp->nm_token);
1177 cnp->cn_flags |= CNP_PDIRUNLOCK;
1179 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1183 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1187 lwkt_reltoken(&nmp->nm_token);
1192 cnp->cn_flags |= CNP_PDIRUNLOCK;
1197 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1198 NFS_LATTR_NOSHRINK));
1199 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1200 NFS_LATTR_NOSHRINK));
1202 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1205 /* XXX MOVE TO nfs_nremove() */
1206 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1207 cnp->cn_nameiop != NAMEI_DELETE) {
1208 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1216 if (newvp != NULLVP) {
1220 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1221 cnp->cn_nameiop == NAMEI_RENAME) &&
1225 cnp->cn_flags |= CNP_PDIRUNLOCK;
1227 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1230 error = EJUSTRETURN;
1233 lwkt_reltoken(&nmp->nm_token);
1239 * Just call nfs_bioread() to do the work.
1241 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1242 * struct ucred *a_cred)
1245 nfs_read(struct vop_read_args *ap)
1247 struct vnode *vp = ap->a_vp;
1248 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1251 lwkt_gettoken(&nmp->nm_token);
1252 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1253 lwkt_reltoken(&nmp->nm_token);
1261 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1264 nfs_readlink(struct vop_readlink_args *ap)
1266 struct vnode *vp = ap->a_vp;
1267 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1270 if (vp->v_type != VLNK)
1273 lwkt_gettoken(&nmp->nm_token);
1274 error = nfs_bioread(vp, ap->a_uio, 0);
1275 lwkt_reltoken(&nmp->nm_token);
1281 * Do a readlink rpc.
1282 * Called by nfs_doio() from below the buffer cache.
1285 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1287 int error = 0, len, attrflag;
1288 struct nfsm_info info;
1291 info.v3 = NFS_ISV3(vp);
1293 nfsstats.rpccnt[NFSPROC_READLINK]++;
1294 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1295 ERROROUT(nfsm_fhtom(&info, vp));
1296 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1297 nfs_vpcred(vp, ND_CHECK), &error));
1299 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1300 NFS_LATTR_NOSHRINK));
1303 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1304 if (len == NFS_MAXPATHLEN) {
1305 struct nfsnode *np = VTONFS(vp);
1306 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1309 ERROROUT(nfsm_mtouio(&info, uiop, len));
1318 * nfs synchronous read rpc using UIO
1321 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1324 struct nfsmount *nmp;
1325 int error = 0, len, retlen, tsiz, eof, attrflag;
1326 struct nfsm_info info;
1330 info.v3 = NFS_ISV3(vp);
1335 nmp = VFSTONFS(vp->v_mount);
1337 tsiz = uiop->uio_resid;
1338 tmp_off = uiop->uio_offset + tsiz;
1339 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1341 tmp_off = uiop->uio_offset;
1343 nfsstats.rpccnt[NFSPROC_READ]++;
1344 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1345 nfsm_reqhead(&info, vp, NFSPROC_READ,
1346 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1347 ERROROUT(nfsm_fhtom(&info, vp));
1348 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1350 txdr_hyper(uiop->uio_offset, tl);
1351 *(tl + 2) = txdr_unsigned(len);
1353 *tl++ = txdr_unsigned(uiop->uio_offset);
1354 *tl++ = txdr_unsigned(len);
1357 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1358 nfs_vpcred(vp, ND_READ), &error));
1360 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1361 NFS_LATTR_NOSHRINK));
1362 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1363 eof = fxdr_unsigned(int, *(tl + 1));
1365 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1367 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1368 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1373 * Handle short-read from server (NFSv3). If EOF is not
1374 * flagged (and no error occurred), but retlen is less
1375 * then the request size, we must zero-fill the remainder.
1377 if (retlen < len && info.v3 && eof == 0) {
1378 ERROROUT(uiomovez(len - retlen, uiop));
1384 * Terminate loop on EOF or zero-length read.
1386 * For NFSv2 a short-read indicates EOF, not zero-fill,
1387 * and also terminates the loop.
1390 if (eof || retlen == 0)
1392 } else if (retlen < len) {
1404 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1405 int *iomode, int *must_commit)
1409 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1410 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1411 int committed = NFSV3WRITE_FILESYNC;
1412 struct nfsm_info info;
1415 info.v3 = NFS_ISV3(vp);
1418 if (uiop->uio_iovcnt != 1)
1419 panic("nfs: writerpc iovcnt > 1");
1422 tsiz = uiop->uio_resid;
1423 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1426 nfsstats.rpccnt[NFSPROC_WRITE]++;
1427 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1428 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1429 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1431 ERROROUT(nfsm_fhtom(&info, vp));
1433 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1434 txdr_hyper(uiop->uio_offset, tl);
1436 *tl++ = txdr_unsigned(len);
1437 *tl++ = txdr_unsigned(*iomode);
1438 *tl = txdr_unsigned(len);
1442 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1443 /* Set both "begin" and "current" to non-garbage. */
1444 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1445 *tl++ = x; /* "begin offset" */
1446 *tl++ = x; /* "current offset" */
1447 x = txdr_unsigned(len);
1448 *tl++ = x; /* total to this offset */
1449 *tl = x; /* size of this write */
1451 ERROROUT(nfsm_uiotom(&info, uiop, len));
1452 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1453 nfs_vpcred(vp, ND_WRITE), &error));
1456 * The write RPC returns a before and after mtime. The
1457 * nfsm_wcc_data() macro checks the before n_mtime
1458 * against the before time and stores the after time
1459 * in the nfsnode's cached vattr and n_mtime field.
1460 * The NRMODIFIED bit will be set if the before
1461 * time did not match the original mtime.
1463 wccflag = NFSV3_WCCCHK;
1464 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1466 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1467 rlen = fxdr_unsigned(int, *tl++);
1473 } else if (rlen < len) {
1474 backup = len - rlen;
1475 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1476 uiop->uio_iov->iov_len += backup;
1477 uiop->uio_offset -= backup;
1478 uiop->uio_resid += backup;
1481 commit = fxdr_unsigned(int, *tl++);
1484 * Return the lowest committment level
1485 * obtained by any of the RPCs.
1487 if (committed == NFSV3WRITE_FILESYNC)
1489 else if (committed == NFSV3WRITE_DATASYNC &&
1490 commit == NFSV3WRITE_UNSTABLE)
1492 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1493 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1495 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1496 } else if (bcmp((caddr_t)tl,
1497 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1499 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1504 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1513 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1514 committed = NFSV3WRITE_FILESYNC;
1515 *iomode = committed;
1517 uiop->uio_resid = tsiz;
1523 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1524 * mode set to specify the file type and the size field for rdev.
1527 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1530 struct nfsv2_sattr *sp;
1532 struct vnode *newvp = NULL;
1533 struct nfsnode *np = NULL;
1535 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1537 struct nfsm_info info;
1540 info.v3 = NFS_ISV3(dvp);
1542 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1543 rmajor = txdr_unsigned(vap->va_rmajor);
1544 rminor = txdr_unsigned(vap->va_rminor);
1545 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1546 rmajor = nfs_xdrneg1;
1547 rminor = nfs_xdrneg1;
1549 return (EOPNOTSUPP);
1551 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1554 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1555 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1556 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1557 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1558 ERROROUT(nfsm_fhtom(&info, dvp));
1559 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1562 tl = nfsm_build(&info, NFSX_UNSIGNED);
1563 *tl++ = vtonfsv3_type(vap->va_type);
1564 nfsm_v3attrbuild(&info, vap, FALSE);
1565 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1566 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1567 *tl++ = txdr_unsigned(vap->va_rmajor);
1568 *tl = txdr_unsigned(vap->va_rminor);
1571 sp = nfsm_build(&info, NFSX_V2SATTR);
1572 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1573 sp->sa_uid = nfs_xdrneg1;
1574 sp->sa_gid = nfs_xdrneg1;
1575 sp->sa_size = makeudev(rmajor, rminor);
1576 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1577 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1579 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1580 cnp->cn_cred, &error));
1582 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1588 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1589 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1595 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1606 VTONFS(dvp)->n_flag |= NLMODIFIED;
1608 VTONFS(dvp)->n_attrstamp = 0;
1614 * just call nfs_mknodrpc() to do the work.
1616 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1617 * struct componentname *a_cnp, struct vattr *a_vap)
1621 nfs_mknod(struct vop_old_mknod_args *ap)
1623 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1626 lwkt_gettoken(&nmp->nm_token);
1627 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1628 lwkt_reltoken(&nmp->nm_token);
1633 static u_long create_verf;
1635 * nfs file create call
1637 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1638 * struct componentname *a_cnp, struct vattr *a_vap)
1641 nfs_create(struct vop_old_create_args *ap)
1643 struct vnode *dvp = ap->a_dvp;
1644 struct vattr *vap = ap->a_vap;
1645 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1646 struct componentname *cnp = ap->a_cnp;
1647 struct nfsv2_sattr *sp;
1649 struct nfsnode *np = NULL;
1650 struct vnode *newvp = NULL;
1651 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1653 struct nfsm_info info;
1656 info.v3 = NFS_ISV3(dvp);
1657 lwkt_gettoken(&nmp->nm_token);
1660 * Oops, not for me..
1662 if (vap->va_type == VSOCK) {
1663 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1664 lwkt_reltoken(&nmp->nm_token);
1668 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1669 lwkt_reltoken(&nmp->nm_token);
1672 if (vap->va_vaflags & VA_EXCLUSIVE)
1675 nfsstats.rpccnt[NFSPROC_CREATE]++;
1676 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1677 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1678 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1679 ERROROUT(nfsm_fhtom(&info, dvp));
1680 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1683 tl = nfsm_build(&info, NFSX_UNSIGNED);
1684 if (fmode & O_EXCL) {
1685 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1686 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1688 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1689 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1692 *tl++ = create_verf;
1693 *tl = ++create_verf;
1695 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1696 nfsm_v3attrbuild(&info, vap, FALSE);
1699 sp = nfsm_build(&info, NFSX_V2SATTR);
1700 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1701 sp->sa_uid = nfs_xdrneg1;
1702 sp->sa_gid = nfs_xdrneg1;
1704 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1705 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1707 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1708 cnp->cn_cred, &error));
1710 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1716 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1717 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1724 error = nfsm_wcc_data(&info, dvp, &wccflag);
1726 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1732 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1733 KKASSERT(newvp == NULL);
1737 } else if (info.v3 && (fmode & O_EXCL)) {
1739 * We are normally called with only a partially initialized
1740 * VAP. Since the NFSv3 spec says that server may use the
1741 * file attributes to store the verifier, the spec requires
1742 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1743 * in atime, but we can't really assume that all servers will
1744 * so we ensure that our SETATTR sets both atime and mtime.
1746 if (vap->va_mtime.tv_sec == VNOVAL)
1747 vfs_timestamp(&vap->va_mtime);
1748 if (vap->va_atime.tv_sec == VNOVAL)
1749 vap->va_atime = vap->va_mtime;
1750 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1754 * The new np may have enough info for access
1755 * checks, make sure rucred and wucred are
1756 * initialized for read and write rpc's.
1759 if (np->n_rucred == NULL)
1760 np->n_rucred = crhold(cnp->cn_cred);
1761 if (np->n_wucred == NULL)
1762 np->n_wucred = crhold(cnp->cn_cred);
1767 VTONFS(dvp)->n_flag |= NLMODIFIED;
1769 VTONFS(dvp)->n_attrstamp = 0;
1770 lwkt_reltoken(&nmp->nm_token);
1775 * nfs file remove call
1776 * To try and make nfs semantics closer to ufs semantics, a file that has
1777 * other processes using the vnode is renamed instead of removed and then
1778 * removed later on the last close.
1779 * - If v_sysref.refcnt > 1
1780 * If a rename is not already in the works
1781 * call nfs_sillyrename() to set it up
1785 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1786 * struct componentname *a_cnp)
1789 nfs_remove(struct vop_old_remove_args *ap)
1791 struct vnode *vp = ap->a_vp;
1792 struct vnode *dvp = ap->a_dvp;
1793 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1794 struct componentname *cnp = ap->a_cnp;
1795 struct nfsnode *np = VTONFS(vp);
1799 lwkt_gettoken(&nmp->nm_token);
1801 if (vp->v_sysref.refcnt < 1)
1802 panic("nfs_remove: bad v_sysref.refcnt");
1804 if (vp->v_type == VDIR) {
1806 } else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1807 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1809 * throw away biocache buffers, mainly to avoid
1810 * unnecessary delayed writes later.
1812 error = nfs_vinvalbuf(vp, 0, 1);
1815 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1816 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1818 * Kludge City: If the first reply to the remove rpc is lost..
1819 * the reply to the retransmitted request will be ENOENT
1820 * since the file was in fact removed
1821 * Therefore, we cheat and return success.
1823 if (error == ENOENT)
1825 } else if (!np->n_sillyrename) {
1826 error = nfs_sillyrename(dvp, vp, cnp);
1828 np->n_attrstamp = 0;
1829 lwkt_reltoken(&nmp->nm_token);
1835 * nfs file remove rpc called from nfs_inactive
1838 nfs_removeit(struct sillyrename *sp)
1840 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1845 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1848 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1849 struct ucred *cred, struct thread *td)
1851 int error = 0, wccflag = NFSV3_WCCRATTR;
1852 struct nfsm_info info;
1855 info.v3 = NFS_ISV3(dvp);
1857 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1858 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1859 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1860 ERROROUT(nfsm_fhtom(&info, dvp));
1861 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1862 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1864 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1869 VTONFS(dvp)->n_flag |= NLMODIFIED;
1871 VTONFS(dvp)->n_attrstamp = 0;
1876 * nfs file rename call
1878 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1879 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1880 * struct vnode *a_tvp, struct componentname *a_tcnp)
1883 nfs_rename(struct vop_old_rename_args *ap)
1885 struct vnode *fvp = ap->a_fvp;
1886 struct vnode *tvp = ap->a_tvp;
1887 struct vnode *fdvp = ap->a_fdvp;
1888 struct vnode *tdvp = ap->a_tdvp;
1889 struct componentname *tcnp = ap->a_tcnp;
1890 struct componentname *fcnp = ap->a_fcnp;
1891 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1894 lwkt_gettoken(&nmp->nm_token);
1896 /* Check for cross-device rename */
1897 if ((fvp->v_mount != tdvp->v_mount) ||
1898 (tvp && (fvp->v_mount != tvp->v_mount))) {
1904 * We shouldn't have to flush fvp on rename for most server-side
1905 * filesystems as the file handle should not change. Unfortunately
1906 * the inode for some filesystems (msdosfs) might be tied to the
1907 * file name or directory position so to be completely safe
1908 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1911 * We must flush tvp on rename because it might become stale on the
1912 * server after the rename.
1914 if (nfs_flush_on_rename)
1915 VOP_FSYNC(fvp, MNT_WAIT, 0);
1917 VOP_FSYNC(tvp, MNT_WAIT, 0);
1920 * If the tvp exists and is in use, sillyrename it before doing the
1921 * rename of the new file over it.
1923 * XXX Can't sillyrename a directory.
1925 * We do not attempt to do any namecache purges in this old API
1926 * routine. The new API compat functions have access to the actual
1927 * namecache structures and will do it for us.
1929 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1930 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1937 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1938 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1942 lwkt_reltoken(&nmp->nm_token);
1952 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1954 if (error == ENOENT)
1960 * nfs file rename rpc called from nfs_remove() above
1963 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1964 struct sillyrename *sp)
1966 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1967 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1971 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1974 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1975 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1976 struct ucred *cred, struct thread *td)
1978 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1979 struct nfsm_info info;
1982 info.v3 = NFS_ISV3(fdvp);
1984 nfsstats.rpccnt[NFSPROC_RENAME]++;
1985 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1986 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1987 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1988 ERROROUT(nfsm_fhtom(&info, fdvp));
1989 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1990 ERROROUT(nfsm_fhtom(&info, tdvp));
1991 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1992 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1994 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
1995 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2000 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2001 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2003 VTONFS(fdvp)->n_attrstamp = 0;
2005 VTONFS(tdvp)->n_attrstamp = 0;
2010 * nfs hard link create call
2012 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2013 * struct componentname *a_cnp)
2016 nfs_link(struct vop_old_link_args *ap)
2018 struct vnode *vp = ap->a_vp;
2019 struct vnode *tdvp = ap->a_tdvp;
2020 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2021 struct componentname *cnp = ap->a_cnp;
2022 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2023 struct nfsm_info info;
2025 if (vp->v_mount != tdvp->v_mount) {
2028 lwkt_gettoken(&nmp->nm_token);
2031 * The attribute cache may get out of sync with the server on link.
2032 * Pushing writes to the server before handle was inherited from
2033 * long long ago and it is unclear if we still need to do this.
2036 if (nfs_flush_on_hlink)
2037 VOP_FSYNC(vp, MNT_WAIT, 0);
2040 info.v3 = NFS_ISV3(vp);
2042 nfsstats.rpccnt[NFSPROC_LINK]++;
2043 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2044 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2045 nfsm_rndup(cnp->cn_namelen));
2046 ERROROUT(nfsm_fhtom(&info, vp));
2047 ERROROUT(nfsm_fhtom(&info, tdvp));
2048 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2050 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2051 cnp->cn_cred, &error));
2053 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2054 NFS_LATTR_NOSHRINK));
2055 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2060 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2062 VTONFS(vp)->n_attrstamp = 0;
2064 VTONFS(tdvp)->n_attrstamp = 0;
2066 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2068 if (error == EEXIST)
2070 lwkt_reltoken(&nmp->nm_token);
2075 * nfs symbolic link create call
2077 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2078 * struct componentname *a_cnp, struct vattr *a_vap,
2082 nfs_symlink(struct vop_old_symlink_args *ap)
2084 struct vnode *dvp = ap->a_dvp;
2085 struct vattr *vap = ap->a_vap;
2086 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2087 struct componentname *cnp = ap->a_cnp;
2088 struct nfsv2_sattr *sp;
2089 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2090 struct vnode *newvp = NULL;
2091 struct nfsm_info info;
2094 info.v3 = NFS_ISV3(dvp);
2095 lwkt_gettoken(&nmp->nm_token);
2097 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2098 slen = strlen(ap->a_target);
2099 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2100 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2101 nfsm_rndup(cnp->cn_namelen) +
2102 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2103 ERROROUT(nfsm_fhtom(&info, dvp));
2104 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2107 nfsm_v3attrbuild(&info, vap, FALSE);
2109 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2111 sp = nfsm_build(&info, NFSX_V2SATTR);
2112 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2113 sp->sa_uid = nfs_xdrneg1;
2114 sp->sa_gid = nfs_xdrneg1;
2115 sp->sa_size = nfs_xdrneg1;
2116 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2117 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2121 * Issue the NFS request and get the rpc response.
2123 * Only NFSv3 responses returning an error of 0 actually return
2124 * a file handle that can be converted into newvp without having
2125 * to do an extra lookup rpc.
2127 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2128 cnp->cn_cred, &error));
2131 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2133 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2137 * out code jumps -> here, mrep is also freed.
2145 * If we get an EEXIST error, silently convert it to no-error
2146 * in case of an NFS retry.
2148 if (error == EEXIST)
2152 * If we do not have (or no longer have) an error, and we could
2153 * not extract the newvp from the response due to the request being
2154 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2155 * to obtain a newvp to return.
2157 if (error == 0 && newvp == NULL) {
2158 struct nfsnode *np = NULL;
2160 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2161 cnp->cn_cred, cnp->cn_td, &np);
2171 VTONFS(dvp)->n_flag |= NLMODIFIED;
2173 VTONFS(dvp)->n_attrstamp = 0;
2174 lwkt_reltoken(&nmp->nm_token);
2182 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2183 * struct componentname *a_cnp, struct vattr *a_vap)
2186 nfs_mkdir(struct vop_old_mkdir_args *ap)
2188 struct vnode *dvp = ap->a_dvp;
2189 struct vattr *vap = ap->a_vap;
2190 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2191 struct componentname *cnp = ap->a_cnp;
2192 struct nfsv2_sattr *sp;
2193 struct nfsnode *np = NULL;
2194 struct vnode *newvp = NULL;
2196 int error = 0, wccflag = NFSV3_WCCRATTR;
2199 struct nfsm_info info;
2202 info.v3 = NFS_ISV3(dvp);
2203 lwkt_gettoken(&nmp->nm_token);
2205 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2206 lwkt_reltoken(&nmp->nm_token);
2209 len = cnp->cn_namelen;
2210 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2211 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2212 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2213 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2214 ERROROUT(nfsm_fhtom(&info, dvp));
2215 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2217 nfsm_v3attrbuild(&info, vap, FALSE);
2219 sp = nfsm_build(&info, NFSX_V2SATTR);
2220 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2221 sp->sa_uid = nfs_xdrneg1;
2222 sp->sa_gid = nfs_xdrneg1;
2223 sp->sa_size = nfs_xdrneg1;
2224 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2225 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2227 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2228 cnp->cn_cred, &error));
2230 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2233 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2238 VTONFS(dvp)->n_flag |= NLMODIFIED;
2240 VTONFS(dvp)->n_attrstamp = 0;
2242 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2243 * if we can succeed in looking up the directory.
2245 if (error == EEXIST || (!error && !gotvp)) {
2250 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2254 if (newvp->v_type != VDIR)
2264 lwkt_reltoken(&nmp->nm_token);
2269 * nfs remove directory call
2271 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2272 * struct componentname *a_cnp)
2275 nfs_rmdir(struct vop_old_rmdir_args *ap)
2277 struct vnode *vp = ap->a_vp;
2278 struct vnode *dvp = ap->a_dvp;
2279 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2280 struct componentname *cnp = ap->a_cnp;
2281 int error = 0, wccflag = NFSV3_WCCRATTR;
2282 struct nfsm_info info;
2285 info.v3 = NFS_ISV3(dvp);
2290 lwkt_gettoken(&nmp->nm_token);
2292 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2293 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2294 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2295 nfsm_rndup(cnp->cn_namelen));
2296 ERROROUT(nfsm_fhtom(&info, dvp));
2297 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2299 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2300 cnp->cn_cred, &error));
2302 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2307 VTONFS(dvp)->n_flag |= NLMODIFIED;
2309 VTONFS(dvp)->n_attrstamp = 0;
2311 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2313 if (error == ENOENT)
2315 lwkt_reltoken(&nmp->nm_token);
2323 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2326 nfs_readdir(struct vop_readdir_args *ap)
2328 struct vnode *vp = ap->a_vp;
2329 struct nfsnode *np = VTONFS(vp);
2330 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2331 struct uio *uio = ap->a_uio;
2335 if (vp->v_type != VDIR)
2338 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2341 lwkt_gettoken(&nmp->nm_token);
2344 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2345 * and then check that is still valid, or if this is an NQNFS mount
2346 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2347 * VOP_GETATTR() does not necessarily go to the wire.
2349 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2350 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2351 if (VOP_GETATTR(vp, &vattr) == 0 &&
2352 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2354 nfsstats.direofcache_hits++;
2360 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2361 * own cache coherency checks so we do not have to.
2363 tresid = uio->uio_resid;
2364 error = nfs_bioread(vp, uio, 0);
2366 if (!error && uio->uio_resid == tresid)
2367 nfsstats.direofcache_misses++;
2369 lwkt_reltoken(&nmp->nm_token);
2376 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2378 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2379 * offset/block and converts the nfs formatted directory entries for userland
2380 * consumption as well as deals with offsets into the middle of blocks.
2381 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2382 * be block-bounded. It must convert to cookies for the actual RPC.
2385 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2388 struct nfs_dirent *dp = NULL;
2393 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2394 struct nfsnode *dnp = VTONFS(vp);
2396 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2398 struct nfsm_info info;
2401 info.v3 = NFS_ISV3(vp);
2404 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2405 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2406 panic("nfs readdirrpc bad uio");
2410 * If there is no cookie, assume directory was stale.
2412 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2416 return (NFSERR_BAD_COOKIE);
2418 * Loop around doing readdir rpc's of size nm_readdirsize
2419 * truncated to a multiple of DIRBLKSIZ.
2420 * The stopping criteria is EOF or buffer full.
2422 while (more_dirs && bigenough) {
2423 nfsstats.rpccnt[NFSPROC_READDIR]++;
2424 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2425 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2426 ERROROUT(nfsm_fhtom(&info, vp));
2428 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2429 *tl++ = cookie.nfsuquad[0];
2430 *tl++ = cookie.nfsuquad[1];
2431 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2432 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2435 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2436 * WITH NFSv2!!! There's nothing I can really do
2437 * about it other than to hope the server supports
2440 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2441 *tl++ = cookie.nfsuquad[0];
2443 *tl = txdr_unsigned(nmp->nm_readdirsize);
2444 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2446 nfs_vpcred(vp, ND_READ), &error));
2448 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2449 NFS_LATTR_NOSHRINK));
2450 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2451 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2452 dnp->n_cookieverf.nfsuquad[1] = *tl;
2454 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2455 more_dirs = fxdr_unsigned(int, *tl);
2457 /* loop thru the dir entries, converting them to std form */
2458 while (more_dirs && bigenough) {
2460 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2461 fileno = fxdr_hyper(tl);
2462 len = fxdr_unsigned(int, *(tl + 2));
2464 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2465 fileno = fxdr_unsigned(u_quad_t, *tl++);
2466 len = fxdr_unsigned(int, *tl);
2468 if (len <= 0 || len > NFS_MAXNAMLEN) {
2476 * len is the number of bytes in the path element
2477 * name, not including the \0 termination.
2479 * tlen is the number of bytes w have to reserve for
2480 * the path element name.
2482 tlen = nfsm_rndup(len);
2484 tlen += 4; /* To ensure null termination */
2487 * If the entry would cross a DIRBLKSIZ boundary,
2488 * extend the previous nfs_dirent to cover the
2491 left = DIRBLKSIZ - blksiz;
2492 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2493 dp->nfs_reclen += left;
2494 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2495 uiop->uio_iov->iov_len -= left;
2496 uiop->uio_offset += left;
2497 uiop->uio_resid -= left;
2500 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2503 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2504 dp->nfs_ino = fileno;
2505 dp->nfs_namlen = len;
2506 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2507 dp->nfs_type = DT_UNKNOWN;
2508 blksiz += dp->nfs_reclen;
2509 if (blksiz == DIRBLKSIZ)
2511 uiop->uio_offset += sizeof(struct nfs_dirent);
2512 uiop->uio_resid -= sizeof(struct nfs_dirent);
2513 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2514 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2515 ERROROUT(nfsm_mtouio(&info, uiop, len));
2518 * The uiop has advanced by nfs_dirent + len
2519 * but really needs to advance by
2522 cp = uiop->uio_iov->iov_base;
2524 *cp = '\0'; /* null terminate */
2525 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2526 uiop->uio_iov->iov_len -= tlen;
2527 uiop->uio_offset += tlen;
2528 uiop->uio_resid -= tlen;
2531 * NFS strings must be rounded up (nfsm_myouio
2532 * handled that in the bigenough case).
2534 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2537 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2539 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2543 * If we were able to accomodate the last entry,
2544 * get the cookie for the next one. Otherwise
2545 * hold-over the cookie for the one we were not
2546 * able to accomodate.
2549 cookie.nfsuquad[0] = *tl++;
2551 cookie.nfsuquad[1] = *tl++;
2552 } else if (info.v3) {
2557 more_dirs = fxdr_unsigned(int, *tl);
2560 * If at end of rpc data, get the eof boolean
2563 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2564 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2570 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2571 * by increasing d_reclen for the last record.
2574 left = DIRBLKSIZ - blksiz;
2575 dp->nfs_reclen += left;
2576 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2577 uiop->uio_iov->iov_len -= left;
2578 uiop->uio_offset += left;
2579 uiop->uio_resid -= left;
2584 * We hit the end of the directory, update direofoffset.
2586 dnp->n_direofoffset = uiop->uio_offset;
2589 * There is more to go, insert the link cookie so the
2590 * next block can be read.
2592 if (uiop->uio_resid > 0)
2593 kprintf("EEK! readdirrpc resid > 0\n");
2594 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2602 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2605 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2608 struct nfs_dirent *dp;
2610 struct vnode *newvp;
2612 caddr_t dpossav1, dpossav2;
2614 struct mbuf *mdsav1, *mdsav2;
2616 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2617 struct nfsnode *dnp = VTONFS(vp), *np;
2620 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2621 int attrflag, fhsize;
2622 struct nchandle nch;
2623 struct nchandle dnch;
2624 struct nlcomponent nlc;
2625 struct nfsm_info info;
2634 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2635 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2636 panic("nfs readdirplusrpc bad uio");
2639 * Obtain the namecache record for the directory so we have something
2640 * to use as a basis for creating the entries. This function will
2641 * return a held (but not locked) ncp. The ncp may be disconnected
2642 * from the tree and cannot be used for upward traversals, and the
2643 * ncp may be unnamed. Note that other unrelated operations may
2644 * cause the ncp to be named at any time.
2646 * We have to lock the ncp to prevent a lock order reversal when
2647 * rdirplus does nlookups of the children, because the vnode is
2648 * locked and has to stay that way.
2650 cache_fromdvp(vp, NULL, 0, &dnch);
2651 bzero(&nlc, sizeof(nlc));
2655 * If there is no cookie, assume directory was stale.
2657 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2663 return (NFSERR_BAD_COOKIE);
2667 * Loop around doing readdir rpc's of size nm_readdirsize
2668 * truncated to a multiple of DIRBLKSIZ.
2669 * The stopping criteria is EOF or buffer full.
2671 while (more_dirs && bigenough) {
2672 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2673 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2674 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2675 ERROROUT(nfsm_fhtom(&info, vp));
2676 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2677 *tl++ = cookie.nfsuquad[0];
2678 *tl++ = cookie.nfsuquad[1];
2679 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2680 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2681 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2682 *tl = txdr_unsigned(nmp->nm_rsize);
2683 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2685 nfs_vpcred(vp, ND_READ), &error));
2686 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2687 NFS_LATTR_NOSHRINK));
2688 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2689 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2690 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2691 more_dirs = fxdr_unsigned(int, *tl);
2693 /* loop thru the dir entries, doctoring them to 4bsd form */
2694 while (more_dirs && bigenough) {
2695 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2696 fileno = fxdr_hyper(tl);
2697 len = fxdr_unsigned(int, *(tl + 2));
2698 if (len <= 0 || len > NFS_MAXNAMLEN) {
2704 tlen = nfsm_rndup(len);
2706 tlen += 4; /* To ensure null termination*/
2707 left = DIRBLKSIZ - blksiz;
2708 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2709 dp->nfs_reclen += left;
2710 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2711 uiop->uio_iov->iov_len -= left;
2712 uiop->uio_offset += left;
2713 uiop->uio_resid -= left;
2716 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2719 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2720 dp->nfs_ino = fileno;
2721 dp->nfs_namlen = len;
2722 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2723 dp->nfs_type = DT_UNKNOWN;
2724 blksiz += dp->nfs_reclen;
2725 if (blksiz == DIRBLKSIZ)
2727 uiop->uio_offset += sizeof(struct nfs_dirent);
2728 uiop->uio_resid -= sizeof(struct nfs_dirent);
2729 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2730 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2731 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2732 nlc.nlc_namelen = len;
2733 ERROROUT(nfsm_mtouio(&info, uiop, len));
2734 cp = uiop->uio_iov->iov_base;
2737 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2738 uiop->uio_iov->iov_len -= tlen;
2739 uiop->uio_offset += tlen;
2740 uiop->uio_resid -= tlen;
2742 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2744 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2746 cookie.nfsuquad[0] = *tl++;
2747 cookie.nfsuquad[1] = *tl++;
2753 * Since the attributes are before the file handle
2754 * (sigh), we must skip over the attributes and then
2755 * come back and get them.
2757 attrflag = fxdr_unsigned(int, *tl);
2759 dpossav1 = info.dpos;
2761 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2762 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2763 doit = fxdr_unsigned(int, *tl);
2765 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2767 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2768 !NFS_CMPFH(dnp, fhp, fhsize)
2772 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2773 nlc.nlc_namelen, nlc.nlc_namelen,
2777 * This is a bit hokey but there isn't
2778 * much we can do about it. We can't
2779 * hold the directory vp locked while
2780 * doing lookups and gets.
2782 nch = cache_nlookup_nonblock(&dnch, &nlc);
2783 if (nch.ncp == NULL)
2785 cache_setunresolved(&nch);
2786 error = nfs_nget_nonblock(vp->v_mount, fhp,
2794 dpossav2 = info.dpos;
2795 info.dpos = dpossav1;
2798 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2799 info.dpos = dpossav2;
2802 IFTODT(VTTOIF(np->n_vattr.va_type));
2803 nfs_cache_setvp(&nch, newvp,
2804 nfspos_cache_timeout);
2812 kprintf("Warning: NFS/rddirplus, "
2813 "UNABLE TO ENTER %*.*s\n",
2814 nlc.nlc_namelen, nlc.nlc_namelen,
2820 /* Just skip over the file handle */
2821 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2822 i = fxdr_unsigned(int, *tl);
2823 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2825 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2826 more_dirs = fxdr_unsigned(int, *tl);
2829 * If at end of rpc data, get the eof boolean
2832 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2833 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2839 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2840 * by increasing d_reclen for the last record.
2843 left = DIRBLKSIZ - blksiz;
2844 dp->nfs_reclen += left;
2845 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2846 uiop->uio_iov->iov_len -= left;
2847 uiop->uio_offset += left;
2848 uiop->uio_resid -= left;
2852 * We are now either at the end of the directory or have filled the
2856 dnp->n_direofoffset = uiop->uio_offset;
2858 if (uiop->uio_resid > 0)
2859 kprintf("EEK! readdirplusrpc resid > 0\n");
2860 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2864 if (newvp != NULLVP) {
2877 * Silly rename. To make the NFS filesystem that is stateless look a little
2878 * more like the "ufs" a remove of an active vnode is translated to a rename
2879 * to a funny looking filename that is removed by nfs_inactive on the
2880 * nfsnode. There is the potential for another process on a different client
2881 * to create the same funny name between the nfs_lookitup() fails and the
2882 * nfs_rename() completes, but...
2885 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2887 struct sillyrename *sp;
2892 * We previously purged dvp instead of vp. I don't know why, it
2893 * completely destroys performance. We can't do it anyway with the
2894 * new VFS API since we would be breaking the namecache topology.
2896 cache_purge(vp); /* XXX */
2899 if (vp->v_type == VDIR)
2900 panic("nfs: sillyrename dir");
2902 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2903 sp->s_cred = crdup(cnp->cn_cred);
2907 /* Fudge together a funny name */
2908 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2909 (int)(intptr_t)cnp->cn_td);
2911 /* Try lookitups until we get one that isn't there */
2912 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2913 cnp->cn_td, NULL) == 0) {
2915 if (sp->s_name[4] > 'z') {
2920 error = nfs_renameit(dvp, cnp, sp);
2923 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2925 np->n_sillyrename = sp;
2930 kfree((caddr_t)sp, M_NFSREQ);
2935 * Look up a file name and optionally either update the file handle or
2936 * allocate an nfsnode, depending on the value of npp.
2937 * npp == NULL --> just do the lookup
2938 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2940 * *npp != NULL --> update the file handle in the vnode
2943 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2944 struct thread *td, struct nfsnode **npp)
2946 struct vnode *newvp = NULL;
2947 struct nfsnode *np, *dnp = VTONFS(dvp);
2948 int error = 0, fhlen, attrflag;
2950 struct nfsm_info info;
2953 info.v3 = NFS_ISV3(dvp);
2955 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2956 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2957 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2958 ERROROUT(nfsm_fhtom(&info, dvp));
2959 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2960 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2961 if (npp && !error) {
2962 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2965 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2966 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2967 np->n_fhp = &np->n_fh;
2968 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2969 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2970 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2971 np->n_fhsize = fhlen;
2973 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2977 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2986 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2987 NFS_LATTR_NOSHRINK));
2988 if (!attrflag && *npp == NULL) {
2998 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3004 if (npp && *npp == NULL) {
3019 * Nfs Version 3 commit rpc
3021 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3025 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3027 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3028 int error = 0, wccflag = NFSV3_WCCRATTR;
3029 struct nfsm_info info;
3035 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3037 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3038 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3039 ERROROUT(nfsm_fhtom(&info, vp));
3040 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3041 txdr_hyper(offset, tl);
3043 *tl = txdr_unsigned(cnt);
3044 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3045 nfs_vpcred(vp, ND_WRITE), &error));
3046 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3048 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3049 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3050 NFSX_V3WRITEVERF)) {
3051 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3053 error = NFSERR_STALEWRITEVERF;
3064 * - make nfs_bmap() essentially a no-op that does no translation
3065 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3066 * (Maybe I could use the process's page mapping, but I was concerned that
3067 * Kernel Write might not be enabled and also figured copyout() would do
3068 * a lot more work than bcopy() and also it currently happens in the
3069 * context of the swapper process (2).
3071 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3072 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3075 nfs_bmap(struct vop_bmap_args *ap)
3077 /* no token lock required */
3078 if (ap->a_doffsetp != NULL)
3079 *ap->a_doffsetp = ap->a_loffset;
3080 if (ap->a_runp != NULL)
3082 if (ap->a_runb != NULL)
3091 nfs_strategy(struct vop_strategy_args *ap)
3093 struct bio *bio = ap->a_bio;
3095 struct buf *bp __debugvar = bio->bio_buf;
3096 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3100 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3101 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3102 KASSERT(BUF_REFCNT(bp) > 0,
3103 ("nfs_strategy: buffer %p not locked", bp));
3105 if (bio->bio_flags & BIO_SYNC)
3106 td = curthread; /* XXX */
3110 lwkt_gettoken(&nmp->nm_token);
3113 * We probably don't need to push an nbio any more since no
3114 * block conversion is required due to the use of 64 bit byte
3115 * offsets, but do it anyway.
3117 * NOTE: When NFS callers itself via this strategy routines and
3118 * sets up a synchronous I/O, it expects the I/O to run
3119 * synchronously (its bio_done routine just assumes it),
3120 * so for now we have to honor the bit.
3122 nbio = push_bio(bio);
3123 nbio->bio_offset = bio->bio_offset;
3124 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3127 * If the op is asynchronous and an i/o daemon is waiting
3128 * queue the request, wake it up and wait for completion
3129 * otherwise just do it ourselves.
3131 if (bio->bio_flags & BIO_SYNC) {
3132 error = nfs_doio(ap->a_vp, nbio, td);
3134 nfs_asyncio(ap->a_vp, nbio);
3137 lwkt_reltoken(&nmp->nm_token);
3145 * NB Currently unsupported.
3147 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3151 nfs_mmap(struct vop_mmap_args *ap)
3153 /* no token lock required */
3158 * fsync vnode op. Just call nfs_flush() with commit == 1.
3160 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3164 nfs_fsync(struct vop_fsync_args *ap)
3166 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3169 lwkt_gettoken(&nmp->nm_token);
3170 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3171 lwkt_reltoken(&nmp->nm_token);
3177 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3178 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3179 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3180 * set the buffer contains data that has already been written to the server
3181 * and which now needs a commit RPC.
3183 * If commit is 0 we only take one pass and only flush buffers containing new
3186 * If commit is 1 we take two passes, issuing a commit RPC in the second
3189 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3190 * to completely flush all pending data.
3192 * Note that the RB_SCAN code properly handles the case where the
3193 * callback might block and directly or indirectly (another thread) cause
3194 * the RB tree to change.
3197 #ifndef NFS_COMMITBVECSIZ
3198 #define NFS_COMMITBVECSIZ 16
3201 struct nfs_flush_info {
3202 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3209 struct buf *bvary[NFS_COMMITBVECSIZ];
3215 static int nfs_flush_bp(struct buf *bp, void *data);
3216 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3219 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3221 struct nfsnode *np = VTONFS(vp);
3222 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3223 struct nfs_flush_info info;
3226 bzero(&info, sizeof(info));
3229 info.waitfor = waitfor;
3230 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3232 lwkt_gettoken(&vp->v_token);
3238 info.mode = NFI_FLUSHNEW;
3239 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3240 nfs_flush_bp, &info);
3243 * Take a second pass if committing and no error occured.
3244 * Clean up any left over collection (whether an error
3247 if (commit && error == 0) {
3248 info.mode = NFI_COMMIT;
3249 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3250 nfs_flush_bp, &info);
3252 error = nfs_flush_docommit(&info, error);
3256 * Wait for pending I/O to complete before checking whether
3257 * any further dirty buffers exist.
3259 while (waitfor == MNT_WAIT &&
3260 bio_track_active(&vp->v_track_write)) {
3261 error = bio_track_wait(&vp->v_track_write,
3262 info.slpflag, info.slptimeo);
3265 * We have to be able to break out if this
3266 * is an 'intr' mount.
3268 if (nfs_sigintr(nmp, NULL, td)) {
3274 * Since we do not process pending signals,
3275 * once we get a PCATCH our tsleep() will no
3276 * longer sleep, switch to a fixed timeout
3279 if (info.slpflag == PCATCH) {
3281 info.slptimeo = 2 * hz;
3288 * Loop if we are flushing synchronous as well as committing,
3289 * and dirty buffers are still present. Otherwise we might livelock.
3291 } while (waitfor == MNT_WAIT && commit &&
3292 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3295 * The callbacks have to return a negative error to terminate the
3302 * Deal with any error collection
3304 if (np->n_flag & NWRITEERR) {
3305 error = np->n_error;
3306 np->n_flag &= ~NWRITEERR;
3308 lwkt_reltoken(&vp->v_token);
3314 nfs_flush_bp(struct buf *bp, void *data)
3316 struct nfs_flush_info *info = data;
3322 switch(info->mode) {
3324 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3325 if (error && info->loops && info->waitfor == MNT_WAIT) {
3326 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3328 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3329 if (info->slpflag & PCATCH)
3330 lkflags |= LK_PCATCH;
3331 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3337 * Ignore locking errors
3345 * The buffer may have changed out from under us, even if
3346 * we did not block (MPSAFE). Check again now that it is
3349 if (bp->b_vp == info->vp &&
3350 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3359 * Only process buffers in need of a commit which we can
3360 * immediately lock. This may prevent a buffer from being
3361 * committed, but the normal flush loop will block on the
3362 * same buffer so we shouldn't get into an endless loop.
3364 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3365 (B_DELWRI | B_NEEDCOMMIT)) {
3368 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3372 * We must recheck after successfully locking the buffer.
3374 if (bp->b_vp != info->vp ||
3375 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3376 (B_DELWRI | B_NEEDCOMMIT)) {
3382 * NOTE: storing the bp in the bvary[] basically sets
3383 * it up for a commit operation.
3385 * We must call vfs_busy_pages() now so the commit operation
3386 * is interlocked with user modifications to memory mapped
3387 * pages. The b_dirtyoff/b_dirtyend range is not correct
3388 * until after the pages have been busied.
3390 * Note: to avoid loopback deadlocks, we do not
3391 * assign b_runningbufspace.
3394 bp->b_cmd = BUF_CMD_WRITE;
3395 vfs_busy_pages(bp->b_vp, bp);
3396 info->bvary[info->bvsize] = bp;
3397 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3398 if (info->bvsize == 0 || toff < info->beg_off)
3399 info->beg_off = toff;
3400 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3401 if (info->bvsize == 0 || toff > info->end_off)
3402 info->end_off = toff;
3404 if (info->bvsize == NFS_COMMITBVECSIZ) {
3405 error = nfs_flush_docommit(info, 0);
3406 KKASSERT(info->bvsize == 0);
3414 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3424 if (info->bvsize > 0) {
3426 * Commit data on the server, as required. Note that
3427 * nfs_commit will use the vnode's cred for the commit.
3428 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3430 bytes = info->end_off - info->beg_off;
3431 if (bytes > 0x40000000)
3436 retv = nfs_commitrpc_uio(vp, info->beg_off,
3437 (int)bytes, info->td);
3438 if (retv == NFSERR_STALEWRITEVERF)
3439 nfs_clearcommit(vp->v_mount);
3443 * Now, either mark the blocks I/O done or mark the
3444 * blocks dirty, depending on whether the commit
3447 for (i = 0; i < info->bvsize; ++i) {
3448 bp = info->bvary[i];
3449 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3451 * Either an error or the original
3452 * vfs_busy_pages() cleared B_NEEDCOMMIT
3453 * due to finding new dirty VM pages in
3456 * Leave B_DELWRI intact.
3458 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3459 vfs_unbusy_pages(bp);
3460 bp->b_cmd = BUF_CMD_DONE;
3464 * Success, remove B_DELWRI ( bundirty() ).
3466 * b_dirtyoff/b_dirtyend seem to be NFS
3467 * specific. We should probably move that
3468 * into bundirty(). XXX
3470 * We are faking an I/O write, we have to
3471 * start the transaction in order to
3472 * immediately biodone() it.
3475 bp->b_flags &= ~B_ERROR;
3476 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3477 bp->b_dirtyoff = bp->b_dirtyend = 0;
3478 biodone(&bp->b_bio1);
3487 * NFS advisory byte-level locks.
3488 * Currently unsupported.
3490 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3494 nfs_advlock(struct vop_advlock_args *ap)
3496 struct nfsnode *np = VTONFS(ap->a_vp);
3498 /* no token lock currently required */
3500 * The following kludge is to allow diskless support to work
3501 * until a real NFS lockd is implemented. Basically, just pretend
3502 * that this is a local lock.
3504 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3508 * Print out the contents of an nfsnode.
3510 * nfs_print(struct vnode *a_vp)
3513 nfs_print(struct vop_print_args *ap)
3515 struct vnode *vp = ap->a_vp;
3516 struct nfsnode *np = VTONFS(vp);
3518 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3519 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3520 if (vp->v_type == VFIFO)
3527 * nfs special file access vnode op.
3529 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3532 nfs_laccess(struct vop_access_args *ap)
3534 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3538 lwkt_gettoken(&nmp->nm_token);
3539 error = VOP_GETATTR(ap->a_vp, &vattr);
3541 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3544 lwkt_reltoken(&nmp->nm_token);
3550 * Read wrapper for fifos.
3552 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3553 * struct ucred *a_cred)
3556 nfsfifo_read(struct vop_read_args *ap)
3558 struct nfsnode *np = VTONFS(ap->a_vp);
3560 /* no token access required */
3565 getnanotime(&np->n_atim);
3566 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3570 * Write wrapper for fifos.
3572 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3573 * struct ucred *a_cred)
3576 nfsfifo_write(struct vop_write_args *ap)
3578 struct nfsnode *np = VTONFS(ap->a_vp);
3580 /* no token access required */
3585 getnanotime(&np->n_mtim);
3586 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3590 * Close wrapper for fifos.
3592 * Update the times on the nfsnode then do fifo close.
3594 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3597 nfsfifo_close(struct vop_close_args *ap)
3599 struct vnode *vp = ap->a_vp;
3600 struct nfsnode *np = VTONFS(vp);
3604 /* no token access required */
3606 if (np->n_flag & (NACC | NUPD)) {
3608 if (np->n_flag & NACC)
3610 if (np->n_flag & NUPD)
3613 if (vp->v_sysref.refcnt == 1 &&
3614 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3616 if (np->n_flag & NACC)
3617 vattr.va_atime = np->n_atim;
3618 if (np->n_flag & NUPD)
3619 vattr.va_mtime = np->n_mtim;
3620 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3623 return (VOCALL(&fifo_vnode_vops, &ap->a_head));