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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
37 * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.80 2008/10/18 01:13:54 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
65 #include <sys/sysctl.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
74 #include <vfs/fifofs/fifo.h>
75 #include <vfs/ufs/dir.h>
85 #include "nfsm_subs.h"
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
91 #include <sys/thread2.h>
97 static int nfsfifo_read (struct vop_read_args *);
98 static int nfsfifo_write (struct vop_write_args *);
99 static int nfsfifo_close (struct vop_close_args *);
100 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
101 static int nfs_lookup (struct vop_old_lookup_args *);
102 static int nfs_create (struct vop_old_create_args *);
103 static int nfs_mknod (struct vop_old_mknod_args *);
104 static int nfs_open (struct vop_open_args *);
105 static int nfs_close (struct vop_close_args *);
106 static int nfs_access (struct vop_access_args *);
107 static int nfs_getattr (struct vop_getattr_args *);
108 static int nfs_setattr (struct vop_setattr_args *);
109 static int nfs_read (struct vop_read_args *);
110 static int nfs_mmap (struct vop_mmap_args *);
111 static int nfs_fsync (struct vop_fsync_args *);
112 static int nfs_remove (struct vop_old_remove_args *);
113 static int nfs_link (struct vop_old_link_args *);
114 static int nfs_rename (struct vop_old_rename_args *);
115 static int nfs_mkdir (struct vop_old_mkdir_args *);
116 static int nfs_rmdir (struct vop_old_rmdir_args *);
117 static int nfs_symlink (struct vop_old_symlink_args *);
118 static int nfs_readdir (struct vop_readdir_args *);
119 static int nfs_bmap (struct vop_bmap_args *);
120 static int nfs_strategy (struct vop_strategy_args *);
121 static int nfs_lookitup (struct vnode *, const char *, int,
122 struct ucred *, struct thread *, struct nfsnode **);
123 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
124 static int nfs_laccess (struct vop_access_args *);
125 static int nfs_readlink (struct vop_readlink_args *);
126 static int nfs_print (struct vop_print_args *);
127 static int nfs_advlock (struct vop_advlock_args *);
129 static int nfs_nresolve (struct vop_nresolve_args *);
131 * Global vfs data structures for nfs
133 struct vop_ops nfsv2_vnode_vops = {
134 .vop_default = vop_defaultop,
135 .vop_access = nfs_access,
136 .vop_advlock = nfs_advlock,
137 .vop_bmap = nfs_bmap,
138 .vop_close = nfs_close,
139 .vop_old_create = nfs_create,
140 .vop_fsync = nfs_fsync,
141 .vop_getattr = nfs_getattr,
142 .vop_getpages = vop_stdgetpages,
143 .vop_putpages = vop_stdputpages,
144 .vop_inactive = nfs_inactive,
145 .vop_old_link = nfs_link,
146 .vop_old_lookup = nfs_lookup,
147 .vop_old_mkdir = nfs_mkdir,
148 .vop_old_mknod = nfs_mknod,
149 .vop_mmap = nfs_mmap,
150 .vop_open = nfs_open,
151 .vop_print = nfs_print,
152 .vop_read = nfs_read,
153 .vop_readdir = nfs_readdir,
154 .vop_readlink = nfs_readlink,
155 .vop_reclaim = nfs_reclaim,
156 .vop_old_remove = nfs_remove,
157 .vop_old_rename = nfs_rename,
158 .vop_old_rmdir = nfs_rmdir,
159 .vop_setattr = nfs_setattr,
160 .vop_strategy = nfs_strategy,
161 .vop_old_symlink = nfs_symlink,
162 .vop_write = nfs_write,
163 .vop_nresolve = nfs_nresolve
167 * Special device vnode ops
169 struct vop_ops nfsv2_spec_vops = {
170 .vop_default = vop_defaultop,
171 .vop_access = nfs_laccess,
172 .vop_close = nfs_close,
173 .vop_fsync = nfs_fsync,
174 .vop_getattr = nfs_getattr,
175 .vop_inactive = nfs_inactive,
176 .vop_print = nfs_print,
177 .vop_read = vop_stdnoread,
178 .vop_reclaim = nfs_reclaim,
179 .vop_setattr = nfs_setattr,
180 .vop_write = vop_stdnowrite
183 struct vop_ops nfsv2_fifo_vops = {
184 .vop_default = fifo_vnoperate,
185 .vop_access = nfs_laccess,
186 .vop_close = nfsfifo_close,
187 .vop_fsync = nfs_fsync,
188 .vop_getattr = nfs_getattr,
189 .vop_inactive = nfs_inactive,
190 .vop_print = nfs_print,
191 .vop_read = nfsfifo_read,
192 .vop_reclaim = nfs_reclaim,
193 .vop_setattr = nfs_setattr,
194 .vop_write = nfsfifo_write
197 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
198 struct componentname *cnp,
200 static int nfs_removerpc (struct vnode *dvp, const char *name,
202 struct ucred *cred, struct thread *td);
203 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
204 int fnamelen, struct vnode *tdvp,
205 const char *tnameptr, int tnamelen,
206 struct ucred *cred, struct thread *td);
207 static int nfs_renameit (struct vnode *sdvp,
208 struct componentname *scnp,
209 struct sillyrename *sp);
211 SYSCTL_DECL(_vfs_nfs);
213 static int nfs_flush_on_rename = 1;
214 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
215 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
216 static int nfs_flush_on_hlink = 0;
217 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
218 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
220 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
221 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
222 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
224 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
226 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
228 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
230 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
232 static int nfsv3_commit_on_close = 0;
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
234 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
236 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
237 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
239 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
240 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
243 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
244 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
245 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
248 * Returns whether a name component is a degenerate '.' or '..'.
252 nlcdegenerate(struct nlcomponent *nlc)
254 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
256 if (nlc->nlc_namelen == 2 &&
257 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
263 nfs3_access_otw(struct vnode *vp, int wmode,
264 struct thread *td, struct ucred *cred)
266 struct nfsnode *np = VTONFS(vp);
271 struct nfsm_info info;
276 nfsstats.rpccnt[NFSPROC_ACCESS]++;
277 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
278 NFSX_FH(info.v3) + NFSX_UNSIGNED);
279 ERROROUT(nfsm_fhtom(&info, vp));
280 tl = nfsm_build(&info, NFSX_UNSIGNED);
281 *tl = txdr_unsigned(wmode);
282 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
283 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
285 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
286 rmode = fxdr_unsigned(u_int32_t, *tl);
288 np->n_modeuid = cred->cr_uid;
289 np->n_modestamp = mycpu->gd_time_seconds;
298 * nfs access vnode op.
299 * For nfs version 2, just return ok. File accesses may fail later.
300 * For nfs version 3, use the access rpc to check accessibility. If file modes
301 * are changed on the server, accesses might still fail later.
303 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
306 nfs_access(struct vop_access_args *ap)
309 struct vnode *vp = ap->a_vp;
310 thread_t td = curthread;
312 u_int32_t mode, wmode;
313 struct nfsnode *np = VTONFS(vp);
314 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
315 int v3 = NFS_ISV3(vp);
317 lwkt_gettoken(&nmp->nm_token);
320 * Disallow write attempts on filesystems mounted read-only;
321 * unless the file is a socket, fifo, or a block or character
322 * device resident on the filesystem.
324 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
325 switch (vp->v_type) {
329 lwkt_reltoken(&nmp->nm_token);
337 * The NFS protocol passes only the effective uid/gid over the wire but
338 * we need to check access against real ids if AT_EACCESS not set.
339 * Handle this case by cloning the credentials and setting the
340 * effective ids to the real ones.
342 if (ap->a_flags & AT_EACCESS) {
343 cred = crhold(ap->a_cred);
345 cred = crdup(ap->a_cred);
346 cred->cr_uid = cred->cr_ruid;
347 cred->cr_gid = cred->cr_rgid;
351 * For nfs v3, check to see if we have done this recently, and if
352 * so return our cached result instead of making an ACCESS call.
353 * If not, do an access rpc, otherwise you are stuck emulating
354 * ufs_access() locally using the vattr. This may not be correct,
355 * since the server may apply other access criteria such as
356 * client uid-->server uid mapping that we do not know about.
359 if (ap->a_mode & VREAD)
360 mode = NFSV3ACCESS_READ;
363 if (vp->v_type != VDIR) {
364 if (ap->a_mode & VWRITE)
365 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
366 if (ap->a_mode & VEXEC)
367 mode |= NFSV3ACCESS_EXECUTE;
369 if (ap->a_mode & VWRITE)
370 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
372 if (ap->a_mode & VEXEC)
373 mode |= NFSV3ACCESS_LOOKUP;
375 /* XXX safety belt, only make blanket request if caching */
376 if (nfsaccess_cache_timeout > 0) {
377 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
378 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
379 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
385 * Does our cached result allow us to give a definite yes to
388 if (np->n_modestamp &&
389 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
390 (cred->cr_uid == np->n_modeuid) &&
391 ((np->n_mode & mode) == mode)) {
392 nfsstats.accesscache_hits++;
395 * Either a no, or a don't know. Go to the wire.
397 nfsstats.accesscache_misses++;
398 error = nfs3_access_otw(vp, wmode, td, cred);
400 if ((np->n_mode & mode) != mode) {
406 if ((error = nfs_laccess(ap)) != 0) {
408 lwkt_reltoken(&nmp->nm_token);
413 * Attempt to prevent a mapped root from accessing a file
414 * which it shouldn't. We try to read a byte from the file
415 * if the user is root and the file is not zero length.
416 * After calling nfs_laccess, we should have the correct
419 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
420 && VTONFS(vp)->n_size > 0) {
427 auio.uio_iov = &aiov;
431 auio.uio_segflg = UIO_SYSSPACE;
432 auio.uio_rw = UIO_READ;
435 if (vp->v_type == VREG) {
436 error = nfs_readrpc_uio(vp, &auio);
437 } else if (vp->v_type == VDIR) {
439 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
441 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
442 error = nfs_readdirrpc_uio(vp, &auio);
444 } else if (vp->v_type == VLNK) {
445 error = nfs_readlinkrpc_uio(vp, &auio);
452 * [re]record creds for reading and/or writing if access
453 * was granted. Assume the NFS server will grant read access
454 * for execute requests.
457 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
460 crfree(np->n_rucred);
463 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
466 crfree(np->n_wucred);
470 lwkt_reltoken(&nmp->nm_token);
477 * Check to see if the type is ok
478 * and that deletion is not in progress.
479 * For paged in text files, you will need to flush the page cache
480 * if consistency is lost.
482 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
487 nfs_open(struct vop_open_args *ap)
489 struct vnode *vp = ap->a_vp;
490 struct nfsnode *np = VTONFS(vp);
491 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
495 lwkt_gettoken(&nmp->nm_token);
497 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
499 kprintf("open eacces vtyp=%d\n",vp->v_type);
501 lwkt_reltoken(&nmp->nm_token);
506 * Save valid creds for reading and writing for later RPCs.
508 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
511 crfree(np->n_rucred);
512 np->n_rucred = ap->a_cred;
514 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
517 crfree(np->n_wucred);
518 np->n_wucred = ap->a_cred;
522 * Clear the attribute cache only if opening with write access. It
523 * is unclear if we should do this at all here, but we certainly
524 * should not clear the cache unconditionally simply because a file
527 if (ap->a_mode & FWRITE)
531 * For normal NFS, reconcile changes made locally verses
532 * changes made remotely. Note that VOP_GETATTR only goes
533 * to the wire if the cached attribute has timed out or been
536 * If local modifications have been made clear the attribute
537 * cache to force an attribute and modified time check. If
538 * GETATTR detects that the file has been changed by someone
539 * other then us it will set NRMODIFIED.
541 * If we are opening a directory and local changes have been
542 * made we have to invalidate the cache in order to ensure
543 * that we get the most up-to-date information from the
546 if (np->n_flag & NLMODIFIED) {
548 if (vp->v_type == VDIR) {
549 error = nfs_vinvalbuf(vp, V_SAVE, 1);
555 error = VOP_GETATTR(vp, &vattr);
557 lwkt_reltoken(&nmp->nm_token);
560 if (np->n_flag & NRMODIFIED) {
561 if (vp->v_type == VDIR)
563 error = nfs_vinvalbuf(vp, V_SAVE, 1);
564 if (error == EINTR) {
565 lwkt_reltoken(&nmp->nm_token);
568 np->n_flag &= ~NRMODIFIED;
570 error = vop_stdopen(ap);
571 lwkt_reltoken(&nmp->nm_token);
578 * What an NFS client should do upon close after writing is a debatable issue.
579 * Most NFS clients push delayed writes to the server upon close, basically for
581 * 1 - So that any write errors may be reported back to the client process
582 * doing the close system call. By far the two most likely errors are
583 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
584 * 2 - To put a worst case upper bound on cache inconsistency between
585 * multiple clients for the file.
586 * There is also a consistency problem for Version 2 of the protocol w.r.t.
587 * not being able to tell if other clients are writing a file concurrently,
588 * since there is no way of knowing if the changed modify time in the reply
589 * is only due to the write for this client.
590 * (NFS Version 3 provides weak cache consistency data in the reply that
591 * should be sufficient to detect and handle this case.)
593 * The current code does the following:
594 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
595 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
596 * or commit them (this satisfies 1 and 2 except for the
597 * case where the server crashes after this close but
598 * before the commit RPC, which is felt to be "good
599 * enough". Changing the last argument to nfs_flush() to
600 * a 1 would force a commit operation, if it is felt a
601 * commit is necessary now.
602 * for NQNFS - do nothing now, since 2 is dealt with via leases and
603 * 1 should be dealt with via an fsync() system call for
604 * cases where write errors are important.
606 * nfs_close(struct vnode *a_vp, int a_fflag)
610 nfs_close(struct vop_close_args *ap)
612 struct vnode *vp = ap->a_vp;
613 struct nfsnode *np = VTONFS(vp);
614 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
616 thread_t td = curthread;
618 lwkt_gettoken(&nmp->nm_token);
620 if (vp->v_type == VREG) {
621 if (np->n_flag & NLMODIFIED) {
624 * Under NFSv3 we have dirty buffers to dispose of. We
625 * must flush them to the NFS server. We have the option
626 * of waiting all the way through the commit rpc or just
627 * waiting for the initial write. The default is to only
628 * wait through the initial write so the data is in the
629 * server's cache, which is roughly similar to the state
630 * a standard disk subsystem leaves the file in on close().
632 * We cannot clear the NLMODIFIED bit in np->n_flag due to
633 * potential races with other processes, and certainly
634 * cannot clear it if we don't commit.
636 int cm = nfsv3_commit_on_close ? 1 : 0;
637 error = nfs_flush(vp, MNT_WAIT, td, cm);
638 /* np->n_flag &= ~NLMODIFIED; */
640 error = nfs_vinvalbuf(vp, V_SAVE, 1);
644 if (np->n_flag & NWRITEERR) {
645 np->n_flag &= ~NWRITEERR;
650 lwkt_reltoken(&nmp->nm_token);
656 * nfs getattr call from vfs.
658 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
661 nfs_getattr(struct vop_getattr_args *ap)
663 struct vnode *vp = ap->a_vp;
664 struct nfsnode *np = VTONFS(vp);
665 struct nfsmount *nmp;
667 thread_t td = curthread;
668 struct nfsm_info info;
671 info.v3 = NFS_ISV3(vp);
672 nmp = VFSTONFS(vp->v_mount);
674 lwkt_gettoken(&nmp->nm_token);
677 * Update local times for special files.
679 if (np->n_flag & (NACC | NUPD))
682 * First look in the cache.
684 if (nfs_getattrcache(vp, ap->a_vap) == 0)
687 if (info.v3 && nfsaccess_cache_timeout > 0) {
688 nfsstats.accesscache_misses++;
689 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
690 if (nfs_getattrcache(vp, ap->a_vap) == 0)
694 nfsstats.rpccnt[NFSPROC_GETATTR]++;
695 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
696 ERROROUT(nfsm_fhtom(&info, vp));
697 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
698 nfs_vpcred(vp, ND_CHECK), &error));
700 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
706 * NFS doesn't support chflags flags. If the nfs mount was
707 * made -o cache set the UF_CACHE bit for swapcache.
709 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
710 ap->a_vap->va_flags |= UF_CACHE;
712 lwkt_reltoken(&nmp->nm_token);
719 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
722 nfs_setattr(struct vop_setattr_args *ap)
724 struct vnode *vp = ap->a_vp;
725 struct nfsnode *np = VTONFS(vp);
726 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
727 struct vattr *vap = ap->a_vap;
728 int biosize = vp->v_mount->mnt_stat.f_iosize;
732 thread_t td = curthread;
738 * Setting of flags is not supported.
740 if (vap->va_flags != VNOVAL)
744 * Disallow write attempts if the filesystem is mounted read-only.
746 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
747 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
748 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
749 (vp->v_mount->mnt_flag & MNT_RDONLY))
752 lwkt_gettoken(&nmp->nm_token);
754 if (vap->va_size != VNOVAL) {
756 * truncation requested
758 switch (vp->v_type) {
760 lwkt_reltoken(&nmp->nm_token);
766 if (vap->va_mtime.tv_sec == VNOVAL &&
767 vap->va_atime.tv_sec == VNOVAL &&
768 vap->va_mode == (mode_t)VNOVAL &&
769 vap->va_uid == (uid_t)VNOVAL &&
770 vap->va_gid == (gid_t)VNOVAL) {
771 lwkt_reltoken(&nmp->nm_token);
774 vap->va_size = VNOVAL;
778 * Disallow write attempts if the filesystem is
781 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
782 lwkt_reltoken(&nmp->nm_token);
788 boff = (int)vap->va_size & (biosize - 1);
789 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
792 if (np->n_flag & NLMODIFIED) {
793 if (vap->va_size == 0)
794 error = nfs_vinvalbuf(vp, 0, 1);
796 error = nfs_vinvalbuf(vp, V_SAVE, 1);
800 * note: this loop case almost always happens at
801 * least once per truncation.
803 if (error == 0 && np->n_size != vap->va_size)
805 np->n_vattr.va_size = vap->va_size;
808 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
810 * What to do. If we are modifying the mtime we lose
811 * mtime detection of changes made by the server or other
812 * clients. But programs like rsync/rdist/cpdup are going
813 * to call utimes a lot. We don't want to piecemeal sync.
815 * For now sync if any prior remote changes were detected,
816 * but allow us to lose track of remote changes made during
817 * the utimes operation.
819 if (np->n_flag & NRMODIFIED)
820 error = nfs_vinvalbuf(vp, V_SAVE, 1);
824 if (vap->va_mtime.tv_sec != VNOVAL) {
825 np->n_mtime = vap->va_mtime.tv_sec;
829 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
832 * Sanity check if a truncation was issued. This should only occur
833 * if multiple processes are racing on the same file.
835 if (error == 0 && vap->va_size != VNOVAL &&
836 np->n_size != vap->va_size) {
837 kprintf("NFS ftruncate: server disagrees on the file size: "
840 (intmax_t)vap->va_size,
841 (intmax_t)np->n_size);
844 if (error && vap->va_size != VNOVAL) {
845 np->n_size = np->n_vattr.va_size = tsize;
846 nfs_meta_setsize(vp, td, np->n_size, 0);
848 lwkt_reltoken(&nmp->nm_token);
854 * Do an nfs setattr rpc.
857 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
858 struct ucred *cred, struct thread *td)
860 struct nfsv2_sattr *sp;
861 struct nfsnode *np = VTONFS(vp);
863 int error = 0, wccflag = NFSV3_WCCRATTR;
864 struct nfsm_info info;
867 info.v3 = NFS_ISV3(vp);
869 nfsstats.rpccnt[NFSPROC_SETATTR]++;
870 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
871 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
872 ERROROUT(nfsm_fhtom(&info, vp));
874 nfsm_v3attrbuild(&info, vap, TRUE);
875 tl = nfsm_build(&info, NFSX_UNSIGNED);
878 sp = nfsm_build(&info, NFSX_V2SATTR);
879 if (vap->va_mode == (mode_t)VNOVAL)
880 sp->sa_mode = nfs_xdrneg1;
882 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
883 if (vap->va_uid == (uid_t)VNOVAL)
884 sp->sa_uid = nfs_xdrneg1;
886 sp->sa_uid = txdr_unsigned(vap->va_uid);
887 if (vap->va_gid == (gid_t)VNOVAL)
888 sp->sa_gid = nfs_xdrneg1;
890 sp->sa_gid = txdr_unsigned(vap->va_gid);
891 sp->sa_size = txdr_unsigned(vap->va_size);
892 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
893 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
895 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
898 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
900 ERROROUT(nfsm_loadattr(&info, vp, NULL));
910 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
916 cache_setvp(nch, vp);
917 cache_settimeout(nch, nctimeout);
921 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
922 * nfs_lookup() until all remaining new api calls are implemented.
924 * Resolve a namecache entry. This function is passed a locked ncp and
925 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
928 nfs_nresolve(struct vop_nresolve_args *ap)
930 struct thread *td = curthread;
931 struct namecache *ncp;
932 struct nfsmount *nmp;
943 struct nfsm_info info;
947 nmp = VFSTONFS(dvp->v_mount);
949 lwkt_gettoken(&nmp->nm_token);
951 if ((error = vget(dvp, LK_SHARED)) != 0) {
952 lwkt_reltoken(&nmp->nm_token);
957 info.v3 = NFS_ISV3(dvp);
960 nfsstats.lookupcache_misses++;
961 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
962 ncp = ap->a_nch->ncp;
964 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
965 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
966 ERROROUT(nfsm_fhtom(&info, dvp));
967 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
968 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
969 ap->a_cred, &error));
972 * Cache negatve lookups to reduce NFS traffic, but use
973 * a fast timeout. Otherwise use a timeout of 1 tick.
974 * XXX we should add a namecache flag for no-caching
975 * to uncache the negative hit as soon as possible, but
976 * we cannot simply destroy the entry because it is used
977 * as a placeholder by the caller.
979 * The refactored nfs code will overwrite a non-zero error
980 * with 0 when we use ERROROUT(), so don't here.
983 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
984 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
996 * Success, get the file handle, do various checks, and load
997 * post-operation data from the reply packet. Theoretically
998 * we should never be looking up "." so, theoretically, we
999 * should never get the same file handle as our directory. But
1000 * we check anyway. XXX
1002 * Note that no timeout is set for the positive cache hit. We
1003 * assume, theoretically, that ESTALE returns will be dealt with
1004 * properly to handle NFS races and in anycase we cannot depend
1005 * on a timeout to deal with NFS open/create/excl issues so instead
1006 * of a bad hack here the rest of the NFS client code needs to do
1009 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1012 if (NFS_CMPFH(np, fhp, fhsize)) {
1016 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1021 lwkt_reltoken(&nmp->nm_token);
1027 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1028 NFS_LATTR_NOSHRINK));
1029 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1030 NFS_LATTR_NOSHRINK));
1032 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1034 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1038 lwkt_reltoken(&nmp->nm_token);
1050 * 'cached' nfs directory lookup
1052 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1054 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1055 * struct componentname *a_cnp)
1058 nfs_lookup(struct vop_old_lookup_args *ap)
1060 struct componentname *cnp = ap->a_cnp;
1061 struct vnode *dvp = ap->a_dvp;
1062 struct vnode **vpp = ap->a_vpp;
1063 int flags = cnp->cn_flags;
1064 struct vnode *newvp;
1065 struct nfsmount *nmp;
1069 int lockparent, wantparent, attrflag, fhsize;
1072 struct nfsm_info info;
1075 info.v3 = NFS_ISV3(dvp);
1079 * Read-only mount check and directory check.
1082 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1083 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1086 if (dvp->v_type != VDIR)
1090 * Look it up in the cache. Note that ENOENT is only returned if we
1091 * previously entered a negative hit (see later on). The additional
1092 * nfsneg_cache_timeout check causes previously cached results to
1093 * be instantly ignored if the negative caching is turned off.
1095 lockparent = flags & CNP_LOCKPARENT;
1096 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1097 nmp = VFSTONFS(dvp->v_mount);
1100 lwkt_gettoken(&nmp->nm_token);
1107 nfsstats.lookupcache_misses++;
1108 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1109 len = cnp->cn_namelen;
1110 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1111 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1112 ERROROUT(nfsm_fhtom(&info, dvp));
1113 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1114 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1115 cnp->cn_cred, &error));
1117 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1118 NFS_LATTR_NOSHRINK);
1128 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1131 * Handle RENAME case...
1133 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1134 if (NFS_CMPFH(np, fhp, fhsize)) {
1137 lwkt_reltoken(&nmp->nm_token);
1140 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1144 lwkt_reltoken(&nmp->nm_token);
1149 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1150 NFS_LATTR_NOSHRINK));
1151 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1152 NFS_LATTR_NOSHRINK));
1154 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1161 cnp->cn_flags |= CNP_PDIRUNLOCK;
1163 lwkt_reltoken(&nmp->nm_token);
1167 if (flags & CNP_ISDOTDOT) {
1169 cnp->cn_flags |= CNP_PDIRUNLOCK;
1170 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1172 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1173 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1174 lwkt_reltoken(&nmp->nm_token);
1175 return (error); /* NOTE: return error from nget */
1179 error = vn_lock(dvp, LK_EXCLUSIVE);
1182 lwkt_reltoken(&nmp->nm_token);
1185 cnp->cn_flags |= CNP_PDIRUNLOCK;
1187 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1191 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1195 lwkt_reltoken(&nmp->nm_token);
1200 cnp->cn_flags |= CNP_PDIRUNLOCK;
1205 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1206 NFS_LATTR_NOSHRINK));
1207 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1208 NFS_LATTR_NOSHRINK));
1210 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1213 /* XXX MOVE TO nfs_nremove() */
1214 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1215 cnp->cn_nameiop != NAMEI_DELETE) {
1216 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1224 if (newvp != NULLVP) {
1228 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1229 cnp->cn_nameiop == NAMEI_RENAME) &&
1233 cnp->cn_flags |= CNP_PDIRUNLOCK;
1235 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1238 error = EJUSTRETURN;
1241 lwkt_reltoken(&nmp->nm_token);
1247 * Just call nfs_bioread() to do the work.
1249 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1250 * struct ucred *a_cred)
1253 nfs_read(struct vop_read_args *ap)
1255 struct vnode *vp = ap->a_vp;
1256 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1259 lwkt_gettoken(&nmp->nm_token);
1260 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1261 lwkt_reltoken(&nmp->nm_token);
1269 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1272 nfs_readlink(struct vop_readlink_args *ap)
1274 struct vnode *vp = ap->a_vp;
1275 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1278 if (vp->v_type != VLNK)
1281 lwkt_gettoken(&nmp->nm_token);
1282 error = nfs_bioread(vp, ap->a_uio, 0);
1283 lwkt_reltoken(&nmp->nm_token);
1289 * Do a readlink rpc.
1290 * Called by nfs_doio() from below the buffer cache.
1293 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1295 int error = 0, len, attrflag;
1296 struct nfsm_info info;
1299 info.v3 = NFS_ISV3(vp);
1301 nfsstats.rpccnt[NFSPROC_READLINK]++;
1302 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1303 ERROROUT(nfsm_fhtom(&info, vp));
1304 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1305 nfs_vpcred(vp, ND_CHECK), &error));
1307 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1308 NFS_LATTR_NOSHRINK));
1311 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1312 if (len == NFS_MAXPATHLEN) {
1313 struct nfsnode *np = VTONFS(vp);
1314 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1317 ERROROUT(nfsm_mtouio(&info, uiop, len));
1326 * nfs synchronous read rpc using UIO
1329 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1332 struct nfsmount *nmp;
1333 int error = 0, len, retlen, tsiz, eof, attrflag;
1334 struct nfsm_info info;
1338 info.v3 = NFS_ISV3(vp);
1343 nmp = VFSTONFS(vp->v_mount);
1345 tsiz = uiop->uio_resid;
1346 tmp_off = uiop->uio_offset + tsiz;
1347 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1349 tmp_off = uiop->uio_offset;
1351 nfsstats.rpccnt[NFSPROC_READ]++;
1352 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1353 nfsm_reqhead(&info, vp, NFSPROC_READ,
1354 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1355 ERROROUT(nfsm_fhtom(&info, vp));
1356 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1358 txdr_hyper(uiop->uio_offset, tl);
1359 *(tl + 2) = txdr_unsigned(len);
1361 *tl++ = txdr_unsigned(uiop->uio_offset);
1362 *tl++ = txdr_unsigned(len);
1365 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1366 nfs_vpcred(vp, ND_READ), &error));
1368 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1369 NFS_LATTR_NOSHRINK));
1370 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1371 eof = fxdr_unsigned(int, *(tl + 1));
1373 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1375 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1376 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1381 * Handle short-read from server (NFSv3). If EOF is not
1382 * flagged (and no error occurred), but retlen is less
1383 * then the request size, we must zero-fill the remainder.
1385 if (retlen < len && info.v3 && eof == 0) {
1386 ERROROUT(uiomovez(len - retlen, uiop));
1392 * Terminate loop on EOF or zero-length read.
1394 * For NFSv2 a short-read indicates EOF, not zero-fill,
1395 * and also terminates the loop.
1398 if (eof || retlen == 0)
1400 } else if (retlen < len) {
1412 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1413 int *iomode, int *must_commit)
1417 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1418 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1419 int committed = NFSV3WRITE_FILESYNC;
1420 struct nfsm_info info;
1423 info.v3 = NFS_ISV3(vp);
1426 if (uiop->uio_iovcnt != 1)
1427 panic("nfs: writerpc iovcnt > 1");
1430 tsiz = uiop->uio_resid;
1431 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1434 nfsstats.rpccnt[NFSPROC_WRITE]++;
1435 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1436 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1437 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1438 ERROROUT(nfsm_fhtom(&info, vp));
1440 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1441 txdr_hyper(uiop->uio_offset, tl);
1443 *tl++ = txdr_unsigned(len);
1444 *tl++ = txdr_unsigned(*iomode);
1445 *tl = txdr_unsigned(len);
1449 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1450 /* Set both "begin" and "current" to non-garbage. */
1451 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1452 *tl++ = x; /* "begin offset" */
1453 *tl++ = x; /* "current offset" */
1454 x = txdr_unsigned(len);
1455 *tl++ = x; /* total to this offset */
1456 *tl = x; /* size of this write */
1458 ERROROUT(nfsm_uiotom(&info, uiop, len));
1459 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1460 nfs_vpcred(vp, ND_WRITE), &error));
1463 * The write RPC returns a before and after mtime. The
1464 * nfsm_wcc_data() macro checks the before n_mtime
1465 * against the before time and stores the after time
1466 * in the nfsnode's cached vattr and n_mtime field.
1467 * The NRMODIFIED bit will be set if the before
1468 * time did not match the original mtime.
1470 wccflag = NFSV3_WCCCHK;
1471 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1473 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1474 rlen = fxdr_unsigned(int, *tl++);
1480 } else if (rlen < len) {
1481 backup = len - rlen;
1482 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1483 uiop->uio_iov->iov_len += backup;
1484 uiop->uio_offset -= backup;
1485 uiop->uio_resid += backup;
1488 commit = fxdr_unsigned(int, *tl++);
1491 * Return the lowest committment level
1492 * obtained by any of the RPCs.
1494 if (committed == NFSV3WRITE_FILESYNC)
1496 else if (committed == NFSV3WRITE_DATASYNC &&
1497 commit == NFSV3WRITE_UNSTABLE)
1499 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1500 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1502 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1503 } else if (bcmp((caddr_t)tl,
1504 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1506 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1511 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1520 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1521 committed = NFSV3WRITE_FILESYNC;
1522 *iomode = committed;
1524 uiop->uio_resid = tsiz;
1530 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1531 * mode set to specify the file type and the size field for rdev.
1534 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1537 struct nfsv2_sattr *sp;
1539 struct vnode *newvp = NULL;
1540 struct nfsnode *np = NULL;
1542 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1544 struct nfsm_info info;
1547 info.v3 = NFS_ISV3(dvp);
1549 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1550 rmajor = txdr_unsigned(vap->va_rmajor);
1551 rminor = txdr_unsigned(vap->va_rminor);
1552 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1553 rmajor = nfs_xdrneg1;
1554 rminor = nfs_xdrneg1;
1556 return (EOPNOTSUPP);
1558 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1561 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1562 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1563 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1564 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1565 ERROROUT(nfsm_fhtom(&info, dvp));
1566 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1569 tl = nfsm_build(&info, NFSX_UNSIGNED);
1570 *tl++ = vtonfsv3_type(vap->va_type);
1571 nfsm_v3attrbuild(&info, vap, FALSE);
1572 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1573 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1574 *tl++ = txdr_unsigned(vap->va_rmajor);
1575 *tl = txdr_unsigned(vap->va_rminor);
1578 sp = nfsm_build(&info, NFSX_V2SATTR);
1579 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1580 sp->sa_uid = nfs_xdrneg1;
1581 sp->sa_gid = nfs_xdrneg1;
1582 sp->sa_size = makeudev(rmajor, rminor);
1583 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1584 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1586 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1587 cnp->cn_cred, &error));
1589 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1595 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1596 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1602 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1613 VTONFS(dvp)->n_flag |= NLMODIFIED;
1615 VTONFS(dvp)->n_attrstamp = 0;
1621 * just call nfs_mknodrpc() to do the work.
1623 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1624 * struct componentname *a_cnp, struct vattr *a_vap)
1628 nfs_mknod(struct vop_old_mknod_args *ap)
1630 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1633 lwkt_gettoken(&nmp->nm_token);
1634 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1635 lwkt_reltoken(&nmp->nm_token);
1640 static u_long create_verf;
1642 * nfs file create call
1644 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1645 * struct componentname *a_cnp, struct vattr *a_vap)
1648 nfs_create(struct vop_old_create_args *ap)
1650 struct vnode *dvp = ap->a_dvp;
1651 struct vattr *vap = ap->a_vap;
1652 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1653 struct componentname *cnp = ap->a_cnp;
1654 struct nfsv2_sattr *sp;
1656 struct nfsnode *np = NULL;
1657 struct vnode *newvp = NULL;
1658 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1660 struct nfsm_info info;
1663 info.v3 = NFS_ISV3(dvp);
1664 lwkt_gettoken(&nmp->nm_token);
1667 * Oops, not for me..
1669 if (vap->va_type == VSOCK) {
1670 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1671 lwkt_reltoken(&nmp->nm_token);
1675 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1676 lwkt_reltoken(&nmp->nm_token);
1679 if (vap->va_vaflags & VA_EXCLUSIVE)
1682 nfsstats.rpccnt[NFSPROC_CREATE]++;
1683 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1684 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1685 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1686 ERROROUT(nfsm_fhtom(&info, dvp));
1687 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1690 tl = nfsm_build(&info, NFSX_UNSIGNED);
1691 if (fmode & O_EXCL) {
1692 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1693 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1695 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1696 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1699 *tl++ = create_verf;
1700 *tl = ++create_verf;
1702 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1703 nfsm_v3attrbuild(&info, vap, FALSE);
1706 sp = nfsm_build(&info, NFSX_V2SATTR);
1707 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1708 sp->sa_uid = nfs_xdrneg1;
1709 sp->sa_gid = nfs_xdrneg1;
1711 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1712 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1714 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1715 cnp->cn_cred, &error));
1717 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1723 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1724 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1731 error = nfsm_wcc_data(&info, dvp, &wccflag);
1733 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1739 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1740 KKASSERT(newvp == NULL);
1744 } else if (info.v3 && (fmode & O_EXCL)) {
1746 * We are normally called with only a partially initialized
1747 * VAP. Since the NFSv3 spec says that server may use the
1748 * file attributes to store the verifier, the spec requires
1749 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1750 * in atime, but we can't really assume that all servers will
1751 * so we ensure that our SETATTR sets both atime and mtime.
1753 if (vap->va_mtime.tv_sec == VNOVAL)
1754 vfs_timestamp(&vap->va_mtime);
1755 if (vap->va_atime.tv_sec == VNOVAL)
1756 vap->va_atime = vap->va_mtime;
1757 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1761 * The new np may have enough info for access
1762 * checks, make sure rucred and wucred are
1763 * initialized for read and write rpc's.
1766 if (np->n_rucred == NULL)
1767 np->n_rucred = crhold(cnp->cn_cred);
1768 if (np->n_wucred == NULL)
1769 np->n_wucred = crhold(cnp->cn_cred);
1774 VTONFS(dvp)->n_flag |= NLMODIFIED;
1776 VTONFS(dvp)->n_attrstamp = 0;
1777 lwkt_reltoken(&nmp->nm_token);
1782 * nfs file remove call
1783 * To try and make nfs semantics closer to ufs semantics, a file that has
1784 * other processes using the vnode is renamed instead of removed and then
1785 * removed later on the last close.
1786 * - If v_sysref.refcnt > 1
1787 * If a rename is not already in the works
1788 * call nfs_sillyrename() to set it up
1792 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1793 * struct componentname *a_cnp)
1796 nfs_remove(struct vop_old_remove_args *ap)
1798 struct vnode *vp = ap->a_vp;
1799 struct vnode *dvp = ap->a_dvp;
1800 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1801 struct componentname *cnp = ap->a_cnp;
1802 struct nfsnode *np = VTONFS(vp);
1806 lwkt_gettoken(&nmp->nm_token);
1808 if (vp->v_sysref.refcnt < 1)
1809 panic("nfs_remove: bad v_sysref.refcnt");
1811 if (vp->v_type == VDIR) {
1813 } else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1814 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1816 * throw away biocache buffers, mainly to avoid
1817 * unnecessary delayed writes later.
1819 error = nfs_vinvalbuf(vp, 0, 1);
1822 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1823 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1825 * Kludge City: If the first reply to the remove rpc is lost..
1826 * the reply to the retransmitted request will be ENOENT
1827 * since the file was in fact removed
1828 * Therefore, we cheat and return success.
1830 if (error == ENOENT)
1832 } else if (!np->n_sillyrename) {
1833 error = nfs_sillyrename(dvp, vp, cnp);
1835 np->n_attrstamp = 0;
1836 lwkt_reltoken(&nmp->nm_token);
1842 * nfs file remove rpc called from nfs_inactive
1845 nfs_removeit(struct sillyrename *sp)
1847 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1852 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1855 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1856 struct ucred *cred, struct thread *td)
1858 int error = 0, wccflag = NFSV3_WCCRATTR;
1859 struct nfsm_info info;
1862 info.v3 = NFS_ISV3(dvp);
1864 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1865 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1866 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1867 ERROROUT(nfsm_fhtom(&info, dvp));
1868 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1869 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1871 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1876 VTONFS(dvp)->n_flag |= NLMODIFIED;
1878 VTONFS(dvp)->n_attrstamp = 0;
1883 * nfs file rename call
1885 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1886 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1887 * struct vnode *a_tvp, struct componentname *a_tcnp)
1890 nfs_rename(struct vop_old_rename_args *ap)
1892 struct vnode *fvp = ap->a_fvp;
1893 struct vnode *tvp = ap->a_tvp;
1894 struct vnode *fdvp = ap->a_fdvp;
1895 struct vnode *tdvp = ap->a_tdvp;
1896 struct componentname *tcnp = ap->a_tcnp;
1897 struct componentname *fcnp = ap->a_fcnp;
1898 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1901 lwkt_gettoken(&nmp->nm_token);
1903 /* Check for cross-device rename */
1904 if ((fvp->v_mount != tdvp->v_mount) ||
1905 (tvp && (fvp->v_mount != tvp->v_mount))) {
1911 * We shouldn't have to flush fvp on rename for most server-side
1912 * filesystems as the file handle should not change. Unfortunately
1913 * the inode for some filesystems (msdosfs) might be tied to the
1914 * file name or directory position so to be completely safe
1915 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1918 * We must flush tvp on rename because it might become stale on the
1919 * server after the rename.
1921 if (nfs_flush_on_rename)
1922 VOP_FSYNC(fvp, MNT_WAIT, 0);
1924 VOP_FSYNC(tvp, MNT_WAIT, 0);
1927 * If the tvp exists and is in use, sillyrename it before doing the
1928 * rename of the new file over it.
1930 * XXX Can't sillyrename a directory.
1932 * We do not attempt to do any namecache purges in this old API
1933 * routine. The new API compat functions have access to the actual
1934 * namecache structures and will do it for us.
1936 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1937 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1944 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1945 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1949 lwkt_reltoken(&nmp->nm_token);
1959 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1961 if (error == ENOENT)
1967 * nfs file rename rpc called from nfs_remove() above
1970 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1971 struct sillyrename *sp)
1973 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1974 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1978 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1981 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1982 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1983 struct ucred *cred, struct thread *td)
1985 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1986 struct nfsm_info info;
1989 info.v3 = NFS_ISV3(fdvp);
1991 nfsstats.rpccnt[NFSPROC_RENAME]++;
1992 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1993 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1994 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1995 ERROROUT(nfsm_fhtom(&info, fdvp));
1996 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1997 ERROROUT(nfsm_fhtom(&info, tdvp));
1998 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1999 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2001 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2002 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2007 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2008 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2010 VTONFS(fdvp)->n_attrstamp = 0;
2012 VTONFS(tdvp)->n_attrstamp = 0;
2017 * nfs hard link create call
2019 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2020 * struct componentname *a_cnp)
2023 nfs_link(struct vop_old_link_args *ap)
2025 struct vnode *vp = ap->a_vp;
2026 struct vnode *tdvp = ap->a_tdvp;
2027 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2028 struct componentname *cnp = ap->a_cnp;
2029 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2030 struct nfsm_info info;
2032 if (vp->v_mount != tdvp->v_mount) {
2035 lwkt_gettoken(&nmp->nm_token);
2038 * The attribute cache may get out of sync with the server on link.
2039 * Pushing writes to the server before handle was inherited from
2040 * long long ago and it is unclear if we still need to do this.
2043 if (nfs_flush_on_hlink)
2044 VOP_FSYNC(vp, MNT_WAIT, 0);
2047 info.v3 = NFS_ISV3(vp);
2049 nfsstats.rpccnt[NFSPROC_LINK]++;
2050 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2051 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2052 nfsm_rndup(cnp->cn_namelen));
2053 ERROROUT(nfsm_fhtom(&info, vp));
2054 ERROROUT(nfsm_fhtom(&info, tdvp));
2055 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2057 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2058 cnp->cn_cred, &error));
2060 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2061 NFS_LATTR_NOSHRINK));
2062 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2067 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2069 VTONFS(vp)->n_attrstamp = 0;
2071 VTONFS(tdvp)->n_attrstamp = 0;
2073 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2075 if (error == EEXIST)
2077 lwkt_reltoken(&nmp->nm_token);
2082 * nfs symbolic link create call
2084 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2085 * struct componentname *a_cnp, struct vattr *a_vap,
2089 nfs_symlink(struct vop_old_symlink_args *ap)
2091 struct vnode *dvp = ap->a_dvp;
2092 struct vattr *vap = ap->a_vap;
2093 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2094 struct componentname *cnp = ap->a_cnp;
2095 struct nfsv2_sattr *sp;
2096 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2097 struct vnode *newvp = NULL;
2098 struct nfsm_info info;
2101 info.v3 = NFS_ISV3(dvp);
2102 lwkt_gettoken(&nmp->nm_token);
2104 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2105 slen = strlen(ap->a_target);
2106 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2107 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2108 nfsm_rndup(cnp->cn_namelen) +
2109 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2110 ERROROUT(nfsm_fhtom(&info, dvp));
2111 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2114 nfsm_v3attrbuild(&info, vap, FALSE);
2116 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2118 sp = nfsm_build(&info, NFSX_V2SATTR);
2119 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2120 sp->sa_uid = nfs_xdrneg1;
2121 sp->sa_gid = nfs_xdrneg1;
2122 sp->sa_size = nfs_xdrneg1;
2123 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2124 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2128 * Issue the NFS request and get the rpc response.
2130 * Only NFSv3 responses returning an error of 0 actually return
2131 * a file handle that can be converted into newvp without having
2132 * to do an extra lookup rpc.
2134 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2135 cnp->cn_cred, &error));
2138 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2140 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2144 * out code jumps -> here, mrep is also freed.
2152 * If we get an EEXIST error, silently convert it to no-error
2153 * in case of an NFS retry.
2155 if (error == EEXIST)
2159 * If we do not have (or no longer have) an error, and we could
2160 * not extract the newvp from the response due to the request being
2161 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2162 * to obtain a newvp to return.
2164 if (error == 0 && newvp == NULL) {
2165 struct nfsnode *np = NULL;
2167 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2168 cnp->cn_cred, cnp->cn_td, &np);
2178 VTONFS(dvp)->n_flag |= NLMODIFIED;
2180 VTONFS(dvp)->n_attrstamp = 0;
2181 lwkt_reltoken(&nmp->nm_token);
2189 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2190 * struct componentname *a_cnp, struct vattr *a_vap)
2193 nfs_mkdir(struct vop_old_mkdir_args *ap)
2195 struct vnode *dvp = ap->a_dvp;
2196 struct vattr *vap = ap->a_vap;
2197 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2198 struct componentname *cnp = ap->a_cnp;
2199 struct nfsv2_sattr *sp;
2200 struct nfsnode *np = NULL;
2201 struct vnode *newvp = NULL;
2203 int error = 0, wccflag = NFSV3_WCCRATTR;
2206 struct nfsm_info info;
2209 info.v3 = NFS_ISV3(dvp);
2210 lwkt_gettoken(&nmp->nm_token);
2212 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2213 lwkt_reltoken(&nmp->nm_token);
2216 len = cnp->cn_namelen;
2217 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2218 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2219 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2220 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2221 ERROROUT(nfsm_fhtom(&info, dvp));
2222 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2224 nfsm_v3attrbuild(&info, vap, FALSE);
2226 sp = nfsm_build(&info, NFSX_V2SATTR);
2227 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2228 sp->sa_uid = nfs_xdrneg1;
2229 sp->sa_gid = nfs_xdrneg1;
2230 sp->sa_size = nfs_xdrneg1;
2231 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2232 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2234 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2235 cnp->cn_cred, &error));
2237 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2240 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2245 VTONFS(dvp)->n_flag |= NLMODIFIED;
2247 VTONFS(dvp)->n_attrstamp = 0;
2249 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2250 * if we can succeed in looking up the directory.
2252 if (error == EEXIST || (!error && !gotvp)) {
2257 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2261 if (newvp->v_type != VDIR)
2271 lwkt_reltoken(&nmp->nm_token);
2276 * nfs remove directory call
2278 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2279 * struct componentname *a_cnp)
2282 nfs_rmdir(struct vop_old_rmdir_args *ap)
2284 struct vnode *vp = ap->a_vp;
2285 struct vnode *dvp = ap->a_dvp;
2286 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2287 struct componentname *cnp = ap->a_cnp;
2288 int error = 0, wccflag = NFSV3_WCCRATTR;
2289 struct nfsm_info info;
2292 info.v3 = NFS_ISV3(dvp);
2297 lwkt_gettoken(&nmp->nm_token);
2299 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2300 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2301 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2302 nfsm_rndup(cnp->cn_namelen));
2303 ERROROUT(nfsm_fhtom(&info, dvp));
2304 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2306 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2307 cnp->cn_cred, &error));
2309 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2314 VTONFS(dvp)->n_flag |= NLMODIFIED;
2316 VTONFS(dvp)->n_attrstamp = 0;
2318 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2320 if (error == ENOENT)
2322 lwkt_reltoken(&nmp->nm_token);
2330 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2333 nfs_readdir(struct vop_readdir_args *ap)
2335 struct vnode *vp = ap->a_vp;
2336 struct nfsnode *np = VTONFS(vp);
2337 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2338 struct uio *uio = ap->a_uio;
2342 if (vp->v_type != VDIR)
2345 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2348 lwkt_gettoken(&nmp->nm_token);
2351 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2352 * and then check that is still valid, or if this is an NQNFS mount
2353 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2354 * VOP_GETATTR() does not necessarily go to the wire.
2356 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2357 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2358 if (VOP_GETATTR(vp, &vattr) == 0 &&
2359 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2361 nfsstats.direofcache_hits++;
2367 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2368 * own cache coherency checks so we do not have to.
2370 tresid = uio->uio_resid;
2371 error = nfs_bioread(vp, uio, 0);
2373 if (!error && uio->uio_resid == tresid)
2374 nfsstats.direofcache_misses++;
2376 lwkt_reltoken(&nmp->nm_token);
2383 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2385 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2386 * offset/block and converts the nfs formatted directory entries for userland
2387 * consumption as well as deals with offsets into the middle of blocks.
2388 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2389 * be block-bounded. It must convert to cookies for the actual RPC.
2392 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2395 struct nfs_dirent *dp = NULL;
2400 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2401 struct nfsnode *dnp = VTONFS(vp);
2403 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2405 struct nfsm_info info;
2408 info.v3 = NFS_ISV3(vp);
2411 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2412 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2413 panic("nfs readdirrpc bad uio");
2417 * If there is no cookie, assume directory was stale.
2419 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2423 return (NFSERR_BAD_COOKIE);
2425 * Loop around doing readdir rpc's of size nm_readdirsize
2426 * truncated to a multiple of DIRBLKSIZ.
2427 * The stopping criteria is EOF or buffer full.
2429 while (more_dirs && bigenough) {
2430 nfsstats.rpccnt[NFSPROC_READDIR]++;
2431 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2432 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2433 ERROROUT(nfsm_fhtom(&info, vp));
2435 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2436 *tl++ = cookie.nfsuquad[0];
2437 *tl++ = cookie.nfsuquad[1];
2438 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2439 *tl++ = dnp->n_cookieverf.nfsuquad[1];
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(1) + 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,
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 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2903 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);
2987 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2988 NFS_LATTR_NOSHRINK));
2989 if (!attrflag && *npp == NULL) {
2999 ERROROUT(error = 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);
3171 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3172 lwkt_reltoken(&nmp->nm_token);
3178 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3179 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3180 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3181 * set the buffer contains data that has already been written to the server
3182 * and which now needs a commit RPC.
3184 * If commit is 0 we only take one pass and only flush buffers containing new
3187 * If commit is 1 we take two passes, issuing a commit RPC in the second
3190 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3191 * to completely flush all pending data.
3193 * Note that the RB_SCAN code properly handles the case where the
3194 * callback might block and directly or indirectly (another thread) cause
3195 * the RB tree to change.
3198 #ifndef NFS_COMMITBVECSIZ
3199 #define NFS_COMMITBVECSIZ 16
3202 struct nfs_flush_info {
3203 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3210 struct buf *bvary[NFS_COMMITBVECSIZ];
3216 static int nfs_flush_bp(struct buf *bp, void *data);
3217 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3220 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3222 struct nfsnode *np = VTONFS(vp);
3223 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3224 struct nfs_flush_info info;
3227 bzero(&info, sizeof(info));
3230 info.waitfor = waitfor;
3231 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3233 lwkt_gettoken(&vp->v_token);
3239 info.mode = NFI_FLUSHNEW;
3240 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3241 nfs_flush_bp, &info);
3244 * Take a second pass if committing and no error occured.
3245 * Clean up any left over collection (whether an error
3248 if (commit && error == 0) {
3249 info.mode = NFI_COMMIT;
3250 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3251 nfs_flush_bp, &info);
3253 error = nfs_flush_docommit(&info, error);
3257 * Wait for pending I/O to complete before checking whether
3258 * any further dirty buffers exist.
3260 while (waitfor == MNT_WAIT &&
3261 bio_track_active(&vp->v_track_write)) {
3262 error = bio_track_wait(&vp->v_track_write,
3263 info.slpflag, info.slptimeo);
3266 * We have to be able to break out if this
3267 * is an 'intr' mount.
3269 if (nfs_sigintr(nmp, NULL, td)) {
3275 * Since we do not process pending signals,
3276 * once we get a PCATCH our tsleep() will no
3277 * longer sleep, switch to a fixed timeout
3280 if (info.slpflag == PCATCH) {
3282 info.slptimeo = 2 * hz;
3289 * Loop if we are flushing synchronous as well as committing,
3290 * and dirty buffers are still present. Otherwise we might livelock.
3292 } while (waitfor == MNT_WAIT && commit &&
3293 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3296 * The callbacks have to return a negative error to terminate the
3303 * Deal with any error collection
3305 if (np->n_flag & NWRITEERR) {
3306 error = np->n_error;
3307 np->n_flag &= ~NWRITEERR;
3309 lwkt_reltoken(&vp->v_token);
3315 nfs_flush_bp(struct buf *bp, void *data)
3317 struct nfs_flush_info *info = data;
3323 switch(info->mode) {
3325 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3326 if (error && info->loops && info->waitfor == MNT_WAIT) {
3327 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3329 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3330 if (info->slpflag & PCATCH)
3331 lkflags |= LK_PCATCH;
3332 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3338 * Ignore locking errors
3346 * The buffer may have changed out from under us, even if
3347 * we did not block (MPSAFE). Check again now that it is
3350 if (bp->b_vp == info->vp &&
3351 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3360 * Only process buffers in need of a commit which we can
3361 * immediately lock. This may prevent a buffer from being
3362 * committed, but the normal flush loop will block on the
3363 * same buffer so we shouldn't get into an endless loop.
3365 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3366 (B_DELWRI | B_NEEDCOMMIT)) {
3369 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3373 * We must recheck after successfully locking the buffer.
3375 if (bp->b_vp != info->vp ||
3376 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3377 (B_DELWRI | B_NEEDCOMMIT)) {
3383 * NOTE: storing the bp in the bvary[] basically sets
3384 * it up for a commit operation.
3386 * We must call vfs_busy_pages() now so the commit operation
3387 * is interlocked with user modifications to memory mapped
3388 * pages. The b_dirtyoff/b_dirtyend range is not correct
3389 * until after the pages have been busied.
3391 * Note: to avoid loopback deadlocks, we do not
3392 * assign b_runningbufspace.
3395 bp->b_cmd = BUF_CMD_WRITE;
3396 vfs_busy_pages(bp->b_vp, bp);
3397 info->bvary[info->bvsize] = bp;
3398 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3399 if (info->bvsize == 0 || toff < info->beg_off)
3400 info->beg_off = toff;
3401 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3402 if (info->bvsize == 0 || toff > info->end_off)
3403 info->end_off = toff;
3405 if (info->bvsize == NFS_COMMITBVECSIZ) {
3406 error = nfs_flush_docommit(info, 0);
3407 KKASSERT(info->bvsize == 0);
3415 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3425 if (info->bvsize > 0) {
3427 * Commit data on the server, as required. Note that
3428 * nfs_commit will use the vnode's cred for the commit.
3429 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3431 bytes = info->end_off - info->beg_off;
3432 if (bytes > 0x40000000)
3437 retv = nfs_commitrpc_uio(vp, info->beg_off,
3438 (int)bytes, info->td);
3439 if (retv == NFSERR_STALEWRITEVERF)
3440 nfs_clearcommit(vp->v_mount);
3444 * Now, either mark the blocks I/O done or mark the
3445 * blocks dirty, depending on whether the commit
3448 for (i = 0; i < info->bvsize; ++i) {
3449 bp = info->bvary[i];
3450 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3452 * Either an error or the original
3453 * vfs_busy_pages() cleared B_NEEDCOMMIT
3454 * due to finding new dirty VM pages in
3457 * Leave B_DELWRI intact.
3459 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3460 vfs_unbusy_pages(bp);
3461 bp->b_cmd = BUF_CMD_DONE;
3465 * Success, remove B_DELWRI ( bundirty() ).
3467 * b_dirtyoff/b_dirtyend seem to be NFS
3468 * specific. We should probably move that
3469 * into bundirty(). XXX
3471 * We are faking an I/O write, we have to
3472 * start the transaction in order to
3473 * immediately biodone() it.
3476 bp->b_flags &= ~B_ERROR;
3477 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3478 bp->b_dirtyoff = bp->b_dirtyend = 0;
3479 biodone(&bp->b_bio1);
3488 * NFS advisory byte-level locks.
3489 * Currently unsupported.
3491 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3495 nfs_advlock(struct vop_advlock_args *ap)
3497 struct nfsnode *np = VTONFS(ap->a_vp);
3499 /* no token lock currently required */
3501 * The following kludge is to allow diskless support to work
3502 * until a real NFS lockd is implemented. Basically, just pretend
3503 * that this is a local lock.
3505 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3509 * Print out the contents of an nfsnode.
3511 * nfs_print(struct vnode *a_vp)
3514 nfs_print(struct vop_print_args *ap)
3516 struct vnode *vp = ap->a_vp;
3517 struct nfsnode *np = VTONFS(vp);
3519 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3520 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3521 if (vp->v_type == VFIFO)
3528 * nfs special file access vnode op.
3530 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3533 nfs_laccess(struct vop_access_args *ap)
3535 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3539 lwkt_gettoken(&nmp->nm_token);
3540 error = VOP_GETATTR(ap->a_vp, &vattr);
3542 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3545 lwkt_reltoken(&nmp->nm_token);
3551 * Read wrapper for fifos.
3553 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3554 * struct ucred *a_cred)
3557 nfsfifo_read(struct vop_read_args *ap)
3559 struct nfsnode *np = VTONFS(ap->a_vp);
3561 /* no token access required */
3566 getnanotime(&np->n_atim);
3567 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3571 * Write wrapper for fifos.
3573 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3574 * struct ucred *a_cred)
3577 nfsfifo_write(struct vop_write_args *ap)
3579 struct nfsnode *np = VTONFS(ap->a_vp);
3581 /* no token access required */
3586 getnanotime(&np->n_mtim);
3587 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3591 * Close wrapper for fifos.
3593 * Update the times on the nfsnode then do fifo close.
3595 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3598 nfsfifo_close(struct vop_close_args *ap)
3600 struct vnode *vp = ap->a_vp;
3601 struct nfsnode *np = VTONFS(vp);
3605 /* no token access required */
3607 if (np->n_flag & (NACC | NUPD)) {
3609 if (np->n_flag & NACC)
3611 if (np->n_flag & NUPD)
3614 if (vp->v_sysref.refcnt == 1 &&
3615 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3617 if (np->n_flag & NACC)
3618 vattr.va_atime = np->n_atim;
3619 if (np->n_flag & NUPD)
3620 vattr.va_mtime = np->n_mtim;
3621 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3624 return (VOCALL(&fifo_vnode_vops, &ap->a_head));