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 *);
122 static int nfs_kqfilter (struct vop_kqfilter_args *ap);
124 static int nfs_nresolve (struct vop_nresolve_args *);
126 * Global vfs data structures for nfs
128 struct vop_ops nfsv2_vnode_vops = {
129 .vop_default = vop_defaultop,
130 .vop_access = nfs_access,
131 .vop_advlock = nfs_advlock,
132 .vop_bmap = nfs_bmap,
133 .vop_close = nfs_close,
134 .vop_old_create = nfs_create,
135 .vop_fsync = nfs_fsync,
136 .vop_getattr = nfs_getattr,
137 .vop_getpages = vop_stdgetpages,
138 .vop_putpages = vop_stdputpages,
139 .vop_inactive = nfs_inactive,
140 .vop_old_link = nfs_link,
141 .vop_old_lookup = nfs_lookup,
142 .vop_old_mkdir = nfs_mkdir,
143 .vop_old_mknod = nfs_mknod,
144 .vop_mmap = nfs_mmap,
145 .vop_open = nfs_open,
146 .vop_print = nfs_print,
147 .vop_read = nfs_read,
148 .vop_readdir = nfs_readdir,
149 .vop_readlink = nfs_readlink,
150 .vop_reclaim = nfs_reclaim,
151 .vop_old_remove = nfs_remove,
152 .vop_old_rename = nfs_rename,
153 .vop_old_rmdir = nfs_rmdir,
154 .vop_setattr = nfs_setattr,
155 .vop_strategy = nfs_strategy,
156 .vop_old_symlink = nfs_symlink,
157 .vop_write = nfs_write,
158 .vop_nresolve = nfs_nresolve,
159 .vop_kqfilter = nfs_kqfilter
163 * Special device vnode ops
165 struct vop_ops nfsv2_spec_vops = {
166 .vop_default = vop_defaultop,
167 .vop_access = nfs_laccess,
168 .vop_close = nfs_close,
169 .vop_fsync = nfs_fsync,
170 .vop_getattr = nfs_getattr,
171 .vop_inactive = nfs_inactive,
172 .vop_print = nfs_print,
173 .vop_read = vop_stdnoread,
174 .vop_reclaim = nfs_reclaim,
175 .vop_setattr = nfs_setattr,
176 .vop_write = vop_stdnowrite
179 struct vop_ops nfsv2_fifo_vops = {
180 .vop_default = fifo_vnoperate,
181 .vop_access = nfs_laccess,
182 .vop_close = nfsfifo_close,
183 .vop_fsync = nfs_fsync,
184 .vop_getattr = nfs_getattr,
185 .vop_inactive = nfs_inactive,
186 .vop_print = nfs_print,
187 .vop_read = nfsfifo_read,
188 .vop_reclaim = nfs_reclaim,
189 .vop_setattr = nfs_setattr,
190 .vop_write = nfsfifo_write
193 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
194 struct componentname *cnp,
196 static int nfs_removerpc (struct vnode *dvp, const char *name,
198 struct ucred *cred, struct thread *td);
199 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
200 int fnamelen, struct vnode *tdvp,
201 const char *tnameptr, int tnamelen,
202 struct ucred *cred, struct thread *td);
203 static int nfs_renameit (struct vnode *sdvp,
204 struct componentname *scnp,
205 struct sillyrename *sp);
207 SYSCTL_DECL(_vfs_nfs);
209 static int nfs_flush_on_rename = 1;
210 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
211 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
212 static int nfs_flush_on_hlink = 0;
213 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
214 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
216 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
217 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
218 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
220 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
221 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
222 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
224 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
226 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
228 static int nfsv3_commit_on_close = 0;
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
230 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
232 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
233 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
235 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
236 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
239 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
240 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
241 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
245 nfs_knote(struct vnode *vp, int flags)
248 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
252 * Returns whether a name component is a degenerate '.' or '..'.
256 nlcdegenerate(struct nlcomponent *nlc)
258 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
260 if (nlc->nlc_namelen == 2 &&
261 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
267 nfs3_access_otw(struct vnode *vp, int wmode,
268 struct thread *td, struct ucred *cred)
270 struct nfsnode *np = VTONFS(vp);
275 struct nfsm_info info;
280 nfsstats.rpccnt[NFSPROC_ACCESS]++;
281 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
282 NFSX_FH(info.v3) + NFSX_UNSIGNED);
283 ERROROUT(nfsm_fhtom(&info, vp));
284 tl = nfsm_build(&info, NFSX_UNSIGNED);
285 *tl = txdr_unsigned(wmode);
286 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
287 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
289 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
290 rmode = fxdr_unsigned(u_int32_t, *tl);
292 np->n_modeuid = cred->cr_uid;
293 np->n_modestamp = mycpu->gd_time_seconds;
302 * nfs access vnode op.
303 * For nfs version 2, just return ok. File accesses may fail later.
304 * For nfs version 3, use the access rpc to check accessibility. If file modes
305 * are changed on the server, accesses might still fail later.
307 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
310 nfs_access(struct vop_access_args *ap)
313 struct vnode *vp = ap->a_vp;
314 thread_t td = curthread;
316 u_int32_t mode, wmode;
317 struct nfsnode *np = VTONFS(vp);
318 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
319 int v3 = NFS_ISV3(vp);
321 lwkt_gettoken(&nmp->nm_token);
324 * Disallow write attempts on filesystems mounted read-only;
325 * unless the file is a socket, fifo, or a block or character
326 * device resident on the filesystem.
328 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
329 switch (vp->v_type) {
333 lwkt_reltoken(&nmp->nm_token);
341 * The NFS protocol passes only the effective uid/gid over the wire but
342 * we need to check access against real ids if AT_EACCESS not set.
343 * Handle this case by cloning the credentials and setting the
344 * effective ids to the real ones.
346 if (ap->a_flags & AT_EACCESS) {
347 cred = crhold(ap->a_cred);
349 cred = crdup(ap->a_cred);
350 cred->cr_uid = cred->cr_ruid;
351 cred->cr_gid = cred->cr_rgid;
355 * For nfs v3, check to see if we have done this recently, and if
356 * so return our cached result instead of making an ACCESS call.
357 * If not, do an access rpc, otherwise you are stuck emulating
358 * ufs_access() locally using the vattr. This may not be correct,
359 * since the server may apply other access criteria such as
360 * client uid-->server uid mapping that we do not know about.
363 if (ap->a_mode & VREAD)
364 mode = NFSV3ACCESS_READ;
367 if (vp->v_type != VDIR) {
368 if (ap->a_mode & VWRITE)
369 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
370 if (ap->a_mode & VEXEC)
371 mode |= NFSV3ACCESS_EXECUTE;
373 if (ap->a_mode & VWRITE)
374 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
376 if (ap->a_mode & VEXEC)
377 mode |= NFSV3ACCESS_LOOKUP;
379 /* XXX safety belt, only make blanket request if caching */
380 if (nfsaccess_cache_timeout > 0) {
381 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
382 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
383 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
389 * Does our cached result allow us to give a definite yes to
392 if (np->n_modestamp &&
393 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
394 (cred->cr_uid == np->n_modeuid) &&
395 ((np->n_mode & mode) == mode)) {
396 nfsstats.accesscache_hits++;
399 * Either a no, or a don't know. Go to the wire.
401 nfsstats.accesscache_misses++;
402 error = nfs3_access_otw(vp, wmode, td, cred);
404 if ((np->n_mode & mode) != mode) {
410 if ((error = nfs_laccess(ap)) != 0) {
412 lwkt_reltoken(&nmp->nm_token);
417 * Attempt to prevent a mapped root from accessing a file
418 * which it shouldn't. We try to read a byte from the file
419 * if the user is root and the file is not zero length.
420 * After calling nfs_laccess, we should have the correct
423 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
424 && VTONFS(vp)->n_size > 0) {
431 auio.uio_iov = &aiov;
435 auio.uio_segflg = UIO_SYSSPACE;
436 auio.uio_rw = UIO_READ;
439 if (vp->v_type == VREG) {
440 error = nfs_readrpc_uio(vp, &auio);
441 } else if (vp->v_type == VDIR) {
443 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
445 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
446 error = nfs_readdirrpc_uio(vp, &auio);
448 } else if (vp->v_type == VLNK) {
449 error = nfs_readlinkrpc_uio(vp, &auio);
456 * [re]record creds for reading and/or writing if access
457 * was granted. Assume the NFS server will grant read access
458 * for execute requests.
461 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
464 crfree(np->n_rucred);
467 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
470 crfree(np->n_wucred);
474 lwkt_reltoken(&nmp->nm_token);
481 * Check to see if the type is ok
482 * and that deletion is not in progress.
483 * For paged in text files, you will need to flush the page cache
484 * if consistency is lost.
486 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
491 nfs_open(struct vop_open_args *ap)
493 struct vnode *vp = ap->a_vp;
494 struct nfsnode *np = VTONFS(vp);
495 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
499 lwkt_gettoken(&nmp->nm_token);
501 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
503 kprintf("open eacces vtyp=%d\n",vp->v_type);
505 lwkt_reltoken(&nmp->nm_token);
510 * Save valid creds for reading and writing for later RPCs.
512 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
515 crfree(np->n_rucred);
516 np->n_rucred = ap->a_cred;
518 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
521 crfree(np->n_wucred);
522 np->n_wucred = ap->a_cred;
526 * Clear the attribute cache only if opening with write access. It
527 * is unclear if we should do this at all here, but we certainly
528 * should not clear the cache unconditionally simply because a file
531 if (ap->a_mode & FWRITE)
535 * For normal NFS, reconcile changes made locally verses
536 * changes made remotely. Note that VOP_GETATTR only goes
537 * to the wire if the cached attribute has timed out or been
540 * If local modifications have been made clear the attribute
541 * cache to force an attribute and modified time check. If
542 * GETATTR detects that the file has been changed by someone
543 * other then us it will set NRMODIFIED.
545 * If we are opening a directory and local changes have been
546 * made we have to invalidate the cache in order to ensure
547 * that we get the most up-to-date information from the
550 if (np->n_flag & NLMODIFIED) {
552 if (vp->v_type == VDIR) {
553 error = nfs_vinvalbuf(vp, V_SAVE, 1);
554 if (error == EINTR) {
555 lwkt_reltoken(&nmp->nm_token);
561 error = VOP_GETATTR(vp, &vattr);
563 lwkt_reltoken(&nmp->nm_token);
566 if (np->n_flag & NRMODIFIED) {
567 if (vp->v_type == VDIR)
569 error = nfs_vinvalbuf(vp, V_SAVE, 1);
570 if (error == EINTR) {
571 lwkt_reltoken(&nmp->nm_token);
574 np->n_flag &= ~NRMODIFIED;
576 error = vop_stdopen(ap);
577 lwkt_reltoken(&nmp->nm_token);
584 * What an NFS client should do upon close after writing is a debatable issue.
585 * Most NFS clients push delayed writes to the server upon close, basically for
587 * 1 - So that any write errors may be reported back to the client process
588 * doing the close system call. By far the two most likely errors are
589 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
590 * 2 - To put a worst case upper bound on cache inconsistency between
591 * multiple clients for the file.
592 * There is also a consistency problem for Version 2 of the protocol w.r.t.
593 * not being able to tell if other clients are writing a file concurrently,
594 * since there is no way of knowing if the changed modify time in the reply
595 * is only due to the write for this client.
596 * (NFS Version 3 provides weak cache consistency data in the reply that
597 * should be sufficient to detect and handle this case.)
599 * The current code does the following:
600 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
601 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
602 * or commit them (this satisfies 1 and 2 except for the
603 * case where the server crashes after this close but
604 * before the commit RPC, which is felt to be "good
605 * enough". Changing the last argument to nfs_flush() to
606 * a 1 would force a commit operation, if it is felt a
607 * commit is necessary now.
608 * for NQNFS - do nothing now, since 2 is dealt with via leases and
609 * 1 should be dealt with via an fsync() system call for
610 * cases where write errors are important.
612 * nfs_close(struct vnode *a_vp, int a_fflag)
616 nfs_close(struct vop_close_args *ap)
618 struct vnode *vp = ap->a_vp;
619 struct nfsnode *np = VTONFS(vp);
620 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
622 thread_t td = curthread;
624 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
625 lwkt_gettoken(&nmp->nm_token);
627 if (vp->v_type == VREG) {
628 if (np->n_flag & NLMODIFIED) {
631 * Under NFSv3 we have dirty buffers to dispose of. We
632 * must flush them to the NFS server. We have the option
633 * of waiting all the way through the commit rpc or just
634 * waiting for the initial write. The default is to only
635 * wait through the initial write so the data is in the
636 * server's cache, which is roughly similar to the state
637 * a standard disk subsystem leaves the file in on close().
639 * We cannot clear the NLMODIFIED bit in np->n_flag due to
640 * potential races with other processes, and certainly
641 * cannot clear it if we don't commit.
643 int cm = nfsv3_commit_on_close ? 1 : 0;
644 error = nfs_flush(vp, MNT_WAIT, td, cm);
645 /* np->n_flag &= ~NLMODIFIED; */
647 error = nfs_vinvalbuf(vp, V_SAVE, 1);
651 if (np->n_flag & NWRITEERR) {
652 np->n_flag &= ~NWRITEERR;
657 lwkt_reltoken(&nmp->nm_token);
663 * nfs getattr call from vfs.
665 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
668 nfs_getattr(struct vop_getattr_args *ap)
670 struct vnode *vp = ap->a_vp;
671 struct nfsnode *np = VTONFS(vp);
672 struct nfsmount *nmp;
674 thread_t td = curthread;
675 struct nfsm_info info;
678 info.v3 = NFS_ISV3(vp);
679 nmp = VFSTONFS(vp->v_mount);
681 lwkt_gettoken(&nmp->nm_token);
684 * Update local times for special files.
686 if (np->n_flag & (NACC | NUPD))
689 * First look in the cache.
691 if (nfs_getattrcache(vp, ap->a_vap) == 0)
694 if (info.v3 && nfsaccess_cache_timeout > 0) {
695 nfsstats.accesscache_misses++;
696 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
697 if (nfs_getattrcache(vp, ap->a_vap) == 0)
701 nfsstats.rpccnt[NFSPROC_GETATTR]++;
702 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
703 ERROROUT(nfsm_fhtom(&info, vp));
704 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
705 nfs_vpcred(vp, ND_CHECK), &error));
707 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
713 * NFS doesn't support chflags flags. If the nfs mount was
714 * made -o cache set the UF_CACHE bit for swapcache.
716 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
717 ap->a_vap->va_flags |= UF_CACHE;
719 lwkt_reltoken(&nmp->nm_token);
726 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
729 nfs_setattr(struct vop_setattr_args *ap)
731 struct vnode *vp = ap->a_vp;
732 struct nfsnode *np = VTONFS(vp);
733 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
734 struct vattr *vap = ap->a_vap;
738 thread_t td = curthread;
744 * Setting of flags is not supported.
746 if (vap->va_flags != VNOVAL)
750 * Disallow write attempts if the filesystem is mounted read-only.
752 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
753 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
754 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
755 (vp->v_mount->mnt_flag & MNT_RDONLY))
758 lwkt_gettoken(&nmp->nm_token);
760 if (vap->va_size != VNOVAL) {
762 * truncation requested
764 switch (vp->v_type) {
766 lwkt_reltoken(&nmp->nm_token);
772 if (vap->va_mtime.tv_sec == VNOVAL &&
773 vap->va_atime.tv_sec == VNOVAL &&
774 vap->va_mode == (mode_t)VNOVAL &&
775 vap->va_uid == (uid_t)VNOVAL &&
776 vap->va_gid == (gid_t)VNOVAL) {
777 lwkt_reltoken(&nmp->nm_token);
780 vap->va_size = VNOVAL;
784 * Disallow write attempts if the filesystem is
787 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
788 lwkt_reltoken(&nmp->nm_token);
794 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
797 if (np->n_flag & NLMODIFIED) {
798 if (vap->va_size == 0)
799 error = nfs_vinvalbuf(vp, 0, 1);
801 error = nfs_vinvalbuf(vp, V_SAVE, 1);
805 * note: this loop case almost always happens at
806 * least once per truncation.
808 if (error == 0 && np->n_size != vap->va_size)
810 np->n_vattr.va_size = vap->va_size;
811 kflags |= NOTE_WRITE;
812 if (tsize < vap->va_size)
813 kflags |= NOTE_EXTEND;
816 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
818 * What to do. If we are modifying the mtime we lose
819 * mtime detection of changes made by the server or other
820 * clients. But programs like rsync/rdist/cpdup are going
821 * to call utimes a lot. We don't want to piecemeal sync.
823 * For now sync if any prior remote changes were detected,
824 * but allow us to lose track of remote changes made during
825 * the utimes operation.
827 if (np->n_flag & NRMODIFIED)
828 error = nfs_vinvalbuf(vp, V_SAVE, 1);
829 if (error == EINTR) {
830 lwkt_reltoken(&nmp->nm_token);
834 if (vap->va_mtime.tv_sec != VNOVAL) {
835 np->n_mtime = vap->va_mtime.tv_sec;
839 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
841 kflags |= NOTE_EXTEND;
844 * Sanity check if a truncation was issued. This should only occur
845 * if multiple processes are racing on the same file.
847 if (error == 0 && vap->va_size != VNOVAL &&
848 np->n_size != vap->va_size) {
849 kprintf("NFS ftruncate: server disagrees on the file size: "
852 (intmax_t)vap->va_size,
853 (intmax_t)np->n_size);
856 if (error && vap->va_size != VNOVAL) {
857 np->n_size = np->n_vattr.va_size = tsize;
858 nfs_meta_setsize(vp, td, np->n_size, 0);
860 lwkt_reltoken(&nmp->nm_token);
861 nfs_knote(vp, kflags);
867 * Do an nfs setattr rpc.
870 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
871 struct ucred *cred, struct thread *td)
873 struct nfsv2_sattr *sp;
874 struct nfsnode *np = VTONFS(vp);
876 int error = 0, wccflag = NFSV3_WCCRATTR;
877 struct nfsm_info info;
880 info.v3 = NFS_ISV3(vp);
882 nfsstats.rpccnt[NFSPROC_SETATTR]++;
883 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
884 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
885 ERROROUT(nfsm_fhtom(&info, vp));
887 nfsm_v3attrbuild(&info, vap, TRUE);
888 tl = nfsm_build(&info, NFSX_UNSIGNED);
891 sp = nfsm_build(&info, NFSX_V2SATTR);
892 if (vap->va_mode == (mode_t)VNOVAL)
893 sp->sa_mode = nfs_xdrneg1;
895 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
896 if (vap->va_uid == (uid_t)VNOVAL)
897 sp->sa_uid = nfs_xdrneg1;
899 sp->sa_uid = txdr_unsigned(vap->va_uid);
900 if (vap->va_gid == (gid_t)VNOVAL)
901 sp->sa_gid = nfs_xdrneg1;
903 sp->sa_gid = txdr_unsigned(vap->va_gid);
904 sp->sa_size = txdr_unsigned(vap->va_size);
905 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
906 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
908 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
911 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
913 ERROROUT(nfsm_loadattr(&info, vp, NULL));
923 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
929 cache_setvp(nch, vp);
930 cache_settimeout(nch, nctimeout);
934 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
935 * nfs_lookup() until all remaining new api calls are implemented.
937 * Resolve a namecache entry. This function is passed a locked ncp and
938 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
941 nfs_nresolve(struct vop_nresolve_args *ap)
943 struct thread *td = curthread;
944 struct namecache *ncp;
945 struct nfsmount *nmp;
955 struct nfsm_info info;
958 nmp = VFSTONFS(dvp->v_mount);
960 lwkt_gettoken(&nmp->nm_token);
962 if ((error = vget(dvp, LK_SHARED)) != 0) {
963 lwkt_reltoken(&nmp->nm_token);
968 info.v3 = NFS_ISV3(dvp);
971 nfsstats.lookupcache_misses++;
972 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
973 ncp = ap->a_nch->ncp;
975 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
976 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
977 ERROROUT(nfsm_fhtom(&info, dvp));
978 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
979 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
980 ap->a_cred, &error));
983 * Cache negatve lookups to reduce NFS traffic, but use
984 * a fast timeout. Otherwise use a timeout of 1 tick.
985 * XXX we should add a namecache flag for no-caching
986 * to uncache the negative hit as soon as possible, but
987 * we cannot simply destroy the entry because it is used
988 * as a placeholder by the caller.
990 * The refactored nfs code will overwrite a non-zero error
991 * with 0 when we use ERROROUT(), so don't here.
994 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
995 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1007 * Success, get the file handle, do various checks, and load
1008 * post-operation data from the reply packet. Theoretically
1009 * we should never be looking up "." so, theoretically, we
1010 * should never get the same file handle as our directory. But
1011 * we check anyway. XXX
1013 * Note that no timeout is set for the positive cache hit. We
1014 * assume, theoretically, that ESTALE returns will be dealt with
1015 * properly to handle NFS races and in anycase we cannot depend
1016 * on a timeout to deal with NFS open/create/excl issues so instead
1017 * of a bad hack here the rest of the NFS client code needs to do
1020 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1023 if (NFS_CMPFH(np, fhp, fhsize)) {
1027 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1032 lwkt_reltoken(&nmp->nm_token);
1038 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1039 NFS_LATTR_NOSHRINK));
1040 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1041 NFS_LATTR_NOSHRINK));
1043 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1045 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1049 lwkt_reltoken(&nmp->nm_token);
1061 * 'cached' nfs directory lookup
1063 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1065 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1066 * struct componentname *a_cnp)
1069 nfs_lookup(struct vop_old_lookup_args *ap)
1071 struct componentname *cnp = ap->a_cnp;
1072 struct vnode *dvp = ap->a_dvp;
1073 struct vnode **vpp = ap->a_vpp;
1074 int flags = cnp->cn_flags;
1075 struct vnode *newvp;
1076 struct vnode *notvp;
1077 struct nfsmount *nmp;
1081 int lockparent, wantparent, attrflag, fhsize;
1084 struct nfsm_info info;
1087 info.v3 = NFS_ISV3(dvp);
1090 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1093 * Read-only mount check and directory check.
1096 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1097 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1100 if (dvp->v_type != VDIR)
1104 * Look it up in the cache. Note that ENOENT is only returned if we
1105 * previously entered a negative hit (see later on). The additional
1106 * nfsneg_cache_timeout check causes previously cached results to
1107 * be instantly ignored if the negative caching is turned off.
1109 lockparent = flags & CNP_LOCKPARENT;
1110 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1111 nmp = VFSTONFS(dvp->v_mount);
1114 lwkt_gettoken(&nmp->nm_token);
1121 nfsstats.lookupcache_misses++;
1122 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1123 len = cnp->cn_namelen;
1124 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1125 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1126 ERROROUT(nfsm_fhtom(&info, dvp));
1127 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1128 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1129 cnp->cn_cred, &error));
1131 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1132 NFS_LATTR_NOSHRINK);
1142 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1145 * Handle RENAME case...
1147 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1148 if (NFS_CMPFH(np, fhp, fhsize)) {
1151 lwkt_reltoken(&nmp->nm_token);
1154 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1158 lwkt_reltoken(&nmp->nm_token);
1163 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1164 NFS_LATTR_NOSHRINK));
1165 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1166 NFS_LATTR_NOSHRINK));
1168 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1175 cnp->cn_flags |= CNP_PDIRUNLOCK;
1177 lwkt_reltoken(&nmp->nm_token);
1181 if (flags & CNP_ISDOTDOT) {
1183 cnp->cn_flags |= CNP_PDIRUNLOCK;
1184 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1186 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1187 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1188 lwkt_reltoken(&nmp->nm_token);
1189 return (error); /* NOTE: return error from nget */
1193 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1196 lwkt_reltoken(&nmp->nm_token);
1199 cnp->cn_flags |= CNP_PDIRUNLOCK;
1201 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1205 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1209 lwkt_reltoken(&nmp->nm_token);
1214 cnp->cn_flags |= CNP_PDIRUNLOCK;
1219 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1220 NFS_LATTR_NOSHRINK));
1221 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1222 NFS_LATTR_NOSHRINK));
1224 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1227 /* XXX MOVE TO nfs_nremove() */
1228 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1229 cnp->cn_nameiop != NAMEI_DELETE) {
1230 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1238 if (newvp != NULLVP) {
1242 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1243 cnp->cn_nameiop == NAMEI_RENAME) &&
1247 cnp->cn_flags |= CNP_PDIRUNLOCK;
1249 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1252 error = EJUSTRETURN;
1255 lwkt_reltoken(&nmp->nm_token);
1261 * Just call nfs_bioread() to do the work.
1263 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1264 * struct ucred *a_cred)
1267 nfs_read(struct vop_read_args *ap)
1269 struct vnode *vp = ap->a_vp;
1270 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1273 lwkt_gettoken(&nmp->nm_token);
1274 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1275 lwkt_reltoken(&nmp->nm_token);
1283 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1286 nfs_readlink(struct vop_readlink_args *ap)
1288 struct vnode *vp = ap->a_vp;
1289 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1292 if (vp->v_type != VLNK)
1295 lwkt_gettoken(&nmp->nm_token);
1296 error = nfs_bioread(vp, ap->a_uio, 0);
1297 lwkt_reltoken(&nmp->nm_token);
1303 * Do a readlink rpc.
1304 * Called by nfs_doio() from below the buffer cache.
1307 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1309 int error = 0, len, attrflag;
1310 struct nfsm_info info;
1313 info.v3 = NFS_ISV3(vp);
1315 nfsstats.rpccnt[NFSPROC_READLINK]++;
1316 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1317 ERROROUT(nfsm_fhtom(&info, vp));
1318 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1319 nfs_vpcred(vp, ND_CHECK), &error));
1321 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1322 NFS_LATTR_NOSHRINK));
1325 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1326 if (len == NFS_MAXPATHLEN) {
1327 struct nfsnode *np = VTONFS(vp);
1328 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1331 ERROROUT(nfsm_mtouio(&info, uiop, len));
1340 * nfs synchronous read rpc using UIO
1343 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1346 struct nfsmount *nmp;
1347 int error = 0, len, retlen, tsiz, eof, attrflag;
1348 struct nfsm_info info;
1352 info.v3 = NFS_ISV3(vp);
1357 nmp = VFSTONFS(vp->v_mount);
1359 tsiz = uiop->uio_resid;
1360 tmp_off = uiop->uio_offset + tsiz;
1361 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1363 tmp_off = uiop->uio_offset;
1365 nfsstats.rpccnt[NFSPROC_READ]++;
1366 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1367 nfsm_reqhead(&info, vp, NFSPROC_READ,
1368 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1369 ERROROUT(nfsm_fhtom(&info, vp));
1370 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1372 txdr_hyper(uiop->uio_offset, tl);
1373 *(tl + 2) = txdr_unsigned(len);
1375 *tl++ = txdr_unsigned(uiop->uio_offset);
1376 *tl++ = txdr_unsigned(len);
1379 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1380 nfs_vpcred(vp, ND_READ), &error));
1382 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1383 NFS_LATTR_NOSHRINK));
1384 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1385 eof = fxdr_unsigned(int, *(tl + 1));
1387 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1389 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1390 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1395 * Handle short-read from server (NFSv3). If EOF is not
1396 * flagged (and no error occurred), but retlen is less
1397 * then the request size, we must zero-fill the remainder.
1399 if (retlen < len && info.v3 && eof == 0) {
1400 ERROROUT(uiomovez(len - retlen, uiop));
1406 * Terminate loop on EOF or zero-length read.
1408 * For NFSv2 a short-read indicates EOF, not zero-fill,
1409 * and also terminates the loop.
1412 if (eof || retlen == 0)
1414 } else if (retlen < len) {
1426 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1427 int *iomode, int *must_commit)
1431 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1432 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1433 int committed = NFSV3WRITE_FILESYNC;
1434 struct nfsm_info info;
1437 info.v3 = NFS_ISV3(vp);
1440 if (uiop->uio_iovcnt != 1)
1441 panic("nfs: writerpc iovcnt > 1");
1444 tsiz = uiop->uio_resid;
1445 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1448 nfsstats.rpccnt[NFSPROC_WRITE]++;
1449 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1450 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1451 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1453 ERROROUT(nfsm_fhtom(&info, vp));
1455 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1456 txdr_hyper(uiop->uio_offset, tl);
1458 *tl++ = txdr_unsigned(len);
1459 *tl++ = txdr_unsigned(*iomode);
1460 *tl = txdr_unsigned(len);
1464 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1465 /* Set both "begin" and "current" to non-garbage. */
1466 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1467 *tl++ = x; /* "begin offset" */
1468 *tl++ = x; /* "current offset" */
1469 x = txdr_unsigned(len);
1470 *tl++ = x; /* total to this offset */
1471 *tl = x; /* size of this write */
1473 ERROROUT(nfsm_uiotom(&info, uiop, len));
1474 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1475 nfs_vpcred(vp, ND_WRITE), &error));
1478 * The write RPC returns a before and after mtime. The
1479 * nfsm_wcc_data() macro checks the before n_mtime
1480 * against the before time and stores the after time
1481 * in the nfsnode's cached vattr and n_mtime field.
1482 * The NRMODIFIED bit will be set if the before
1483 * time did not match the original mtime.
1485 wccflag = NFSV3_WCCCHK;
1486 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1488 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1489 rlen = fxdr_unsigned(int, *tl++);
1495 } else if (rlen < len) {
1496 backup = len - rlen;
1497 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1498 uiop->uio_iov->iov_len += backup;
1499 uiop->uio_offset -= backup;
1500 uiop->uio_resid += backup;
1503 commit = fxdr_unsigned(int, *tl++);
1506 * Return the lowest committment level
1507 * obtained by any of the RPCs.
1509 if (committed == NFSV3WRITE_FILESYNC)
1511 else if (committed == NFSV3WRITE_DATASYNC &&
1512 commit == NFSV3WRITE_UNSTABLE)
1514 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1515 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1517 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1518 } else if (bcmp((caddr_t)tl,
1519 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1521 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1526 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1535 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1536 committed = NFSV3WRITE_FILESYNC;
1537 *iomode = committed;
1539 uiop->uio_resid = tsiz;
1545 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1546 * mode set to specify the file type and the size field for rdev.
1549 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1552 struct nfsv2_sattr *sp;
1554 struct vnode *newvp = NULL;
1555 struct nfsnode *np = NULL;
1557 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1559 struct nfsm_info info;
1562 info.v3 = NFS_ISV3(dvp);
1564 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1565 rmajor = txdr_unsigned(vap->va_rmajor);
1566 rminor = txdr_unsigned(vap->va_rminor);
1567 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1568 rmajor = nfs_xdrneg1;
1569 rminor = nfs_xdrneg1;
1571 return (EOPNOTSUPP);
1573 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1576 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1577 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1578 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1579 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1580 ERROROUT(nfsm_fhtom(&info, dvp));
1581 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1584 tl = nfsm_build(&info, NFSX_UNSIGNED);
1585 *tl++ = vtonfsv3_type(vap->va_type);
1586 nfsm_v3attrbuild(&info, vap, FALSE);
1587 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1588 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1589 *tl++ = txdr_unsigned(vap->va_rmajor);
1590 *tl = txdr_unsigned(vap->va_rminor);
1593 sp = nfsm_build(&info, NFSX_V2SATTR);
1594 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1595 sp->sa_uid = nfs_xdrneg1;
1596 sp->sa_gid = nfs_xdrneg1;
1597 sp->sa_size = makeudev(rmajor, rminor);
1598 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1599 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1601 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1602 cnp->cn_cred, &error));
1604 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1610 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1611 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1617 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1628 VTONFS(dvp)->n_flag |= NLMODIFIED;
1630 VTONFS(dvp)->n_attrstamp = 0;
1636 * just call nfs_mknodrpc() to do the work.
1638 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1639 * struct componentname *a_cnp, struct vattr *a_vap)
1643 nfs_mknod(struct vop_old_mknod_args *ap)
1645 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1648 lwkt_gettoken(&nmp->nm_token);
1649 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1650 lwkt_reltoken(&nmp->nm_token);
1652 nfs_knote(ap->a_dvp, NOTE_WRITE);
1657 static u_long create_verf;
1659 * nfs file create call
1661 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1662 * struct componentname *a_cnp, struct vattr *a_vap)
1665 nfs_create(struct vop_old_create_args *ap)
1667 struct vnode *dvp = ap->a_dvp;
1668 struct vattr *vap = ap->a_vap;
1669 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1670 struct componentname *cnp = ap->a_cnp;
1671 struct nfsv2_sattr *sp;
1673 struct nfsnode *np = NULL;
1674 struct vnode *newvp = NULL;
1675 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1677 struct nfsm_info info;
1680 info.v3 = NFS_ISV3(dvp);
1681 lwkt_gettoken(&nmp->nm_token);
1684 * Oops, not for me..
1686 if (vap->va_type == VSOCK) {
1687 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1688 lwkt_reltoken(&nmp->nm_token);
1692 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1693 lwkt_reltoken(&nmp->nm_token);
1696 if (vap->va_vaflags & VA_EXCLUSIVE)
1699 nfsstats.rpccnt[NFSPROC_CREATE]++;
1700 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1701 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1702 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1703 ERROROUT(nfsm_fhtom(&info, dvp));
1704 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1707 tl = nfsm_build(&info, NFSX_UNSIGNED);
1708 if (fmode & O_EXCL) {
1709 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1710 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1712 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1713 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1716 *tl++ = create_verf;
1717 *tl = ++create_verf;
1719 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1720 nfsm_v3attrbuild(&info, vap, FALSE);
1723 sp = nfsm_build(&info, NFSX_V2SATTR);
1724 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1725 sp->sa_uid = nfs_xdrneg1;
1726 sp->sa_gid = nfs_xdrneg1;
1728 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1729 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1731 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1732 cnp->cn_cred, &error));
1734 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1740 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1741 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1748 error = nfsm_wcc_data(&info, dvp, &wccflag);
1750 (void)nfsm_wcc_data(&info, dvp, &wccflag);
1756 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1757 KKASSERT(newvp == NULL);
1761 } else if (info.v3 && (fmode & O_EXCL)) {
1763 * We are normally called with only a partially initialized
1764 * VAP. Since the NFSv3 spec says that server may use the
1765 * file attributes to store the verifier, the spec requires
1766 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1767 * in atime, but we can't really assume that all servers will
1768 * so we ensure that our SETATTR sets both atime and mtime.
1770 if (vap->va_mtime.tv_sec == VNOVAL)
1771 vfs_timestamp(&vap->va_mtime);
1772 if (vap->va_atime.tv_sec == VNOVAL)
1773 vap->va_atime = vap->va_mtime;
1774 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1778 * The new np may have enough info for access
1779 * checks, make sure rucred and wucred are
1780 * initialized for read and write rpc's.
1783 if (np->n_rucred == NULL)
1784 np->n_rucred = crhold(cnp->cn_cred);
1785 if (np->n_wucred == NULL)
1786 np->n_wucred = crhold(cnp->cn_cred);
1788 nfs_knote(dvp, NOTE_WRITE);
1792 VTONFS(dvp)->n_flag |= NLMODIFIED;
1794 VTONFS(dvp)->n_attrstamp = 0;
1795 lwkt_reltoken(&nmp->nm_token);
1800 * nfs file remove call
1801 * To try and make nfs semantics closer to ufs semantics, a file that has
1802 * other processes using the vnode is renamed instead of removed and then
1803 * removed later on the last close.
1805 * If a rename is not already in the works
1806 * call nfs_sillyrename() to set it up
1810 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1811 * struct componentname *a_cnp)
1814 nfs_remove(struct vop_old_remove_args *ap)
1816 struct vnode *vp = ap->a_vp;
1817 struct vnode *dvp = ap->a_dvp;
1818 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1819 struct componentname *cnp = ap->a_cnp;
1820 struct nfsnode *np = VTONFS(vp);
1824 lwkt_gettoken(&nmp->nm_token);
1826 if (VREFCNT(vp) < 1)
1827 panic("nfs_remove: bad v_refcnt");
1829 if (vp->v_type == VDIR) {
1831 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1832 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1834 * throw away biocache buffers, mainly to avoid
1835 * unnecessary delayed writes later.
1837 error = nfs_vinvalbuf(vp, 0, 1);
1840 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1841 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1843 * Kludge City: If the first reply to the remove rpc is lost..
1844 * the reply to the retransmitted request will be ENOENT
1845 * since the file was in fact removed
1846 * Therefore, we cheat and return success.
1848 if (error == ENOENT)
1850 } else if (!np->n_sillyrename) {
1851 error = nfs_sillyrename(dvp, vp, cnp);
1853 np->n_attrstamp = 0;
1854 lwkt_reltoken(&nmp->nm_token);
1856 nfs_knote(vp, NOTE_DELETE);
1857 nfs_knote(dvp, NOTE_WRITE);
1864 * nfs file remove rpc called from nfs_inactive
1866 * NOTE: s_dvp can be VBAD during a forced unmount.
1869 nfs_removeit(struct sillyrename *sp)
1871 if (sp->s_dvp->v_type == VBAD)
1873 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1878 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1881 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1882 struct ucred *cred, struct thread *td)
1884 int error = 0, wccflag = NFSV3_WCCRATTR;
1885 struct nfsm_info info;
1888 info.v3 = NFS_ISV3(dvp);
1890 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1891 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1892 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1893 ERROROUT(nfsm_fhtom(&info, dvp));
1894 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1895 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1897 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1902 VTONFS(dvp)->n_flag |= NLMODIFIED;
1904 VTONFS(dvp)->n_attrstamp = 0;
1909 * nfs file rename call
1911 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1912 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1913 * struct vnode *a_tvp, struct componentname *a_tcnp)
1916 nfs_rename(struct vop_old_rename_args *ap)
1918 struct vnode *fvp = ap->a_fvp;
1919 struct vnode *tvp = ap->a_tvp;
1920 struct vnode *fdvp = ap->a_fdvp;
1921 struct vnode *tdvp = ap->a_tdvp;
1922 struct componentname *tcnp = ap->a_tcnp;
1923 struct componentname *fcnp = ap->a_fcnp;
1924 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1927 lwkt_gettoken(&nmp->nm_token);
1929 /* Check for cross-device rename */
1930 if ((fvp->v_mount != tdvp->v_mount) ||
1931 (tvp && (fvp->v_mount != tvp->v_mount))) {
1937 * We shouldn't have to flush fvp on rename for most server-side
1938 * filesystems as the file handle should not change. Unfortunately
1939 * the inode for some filesystems (msdosfs) might be tied to the
1940 * file name or directory position so to be completely safe
1941 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1944 * We must flush tvp on rename because it might become stale on the
1945 * server after the rename.
1947 if (nfs_flush_on_rename)
1948 VOP_FSYNC(fvp, MNT_WAIT, 0);
1950 VOP_FSYNC(tvp, MNT_WAIT, 0);
1953 * If the tvp exists and is in use, sillyrename it before doing the
1954 * rename of the new file over it.
1956 * XXX Can't sillyrename a directory.
1958 * We do not attempt to do any namecache purges in this old API
1959 * routine. The new API compat functions have access to the actual
1960 * namecache structures and will do it for us.
1962 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1963 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1964 nfs_knote(tvp, NOTE_DELETE);
1968 nfs_knote(tvp, NOTE_DELETE);
1971 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1972 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1977 nfs_knote(fdvp, NOTE_WRITE);
1978 nfs_knote(tdvp, NOTE_WRITE);
1979 nfs_knote(fvp, NOTE_RENAME);
1981 lwkt_reltoken(&nmp->nm_token);
1991 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1993 if (error == ENOENT)
1999 * nfs file rename rpc called from nfs_remove() above
2002 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2003 struct sillyrename *sp)
2005 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2006 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
2010 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2013 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2014 struct vnode *tdvp, const char *tnameptr, int tnamelen,
2015 struct ucred *cred, struct thread *td)
2017 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2018 struct nfsm_info info;
2021 info.v3 = NFS_ISV3(fdvp);
2023 nfsstats.rpccnt[NFSPROC_RENAME]++;
2024 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2025 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2026 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2027 ERROROUT(nfsm_fhtom(&info, fdvp));
2028 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2029 ERROROUT(nfsm_fhtom(&info, tdvp));
2030 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2031 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2033 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2034 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2039 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2040 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2042 VTONFS(fdvp)->n_attrstamp = 0;
2044 VTONFS(tdvp)->n_attrstamp = 0;
2049 * nfs hard link create call
2051 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2052 * struct componentname *a_cnp)
2055 nfs_link(struct vop_old_link_args *ap)
2057 struct vnode *vp = ap->a_vp;
2058 struct vnode *tdvp = ap->a_tdvp;
2059 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2060 struct componentname *cnp = ap->a_cnp;
2061 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2062 struct nfsm_info info;
2064 if (vp->v_mount != tdvp->v_mount) {
2067 lwkt_gettoken(&nmp->nm_token);
2070 * The attribute cache may get out of sync with the server on link.
2071 * Pushing writes to the server before handle was inherited from
2072 * long long ago and it is unclear if we still need to do this.
2075 if (nfs_flush_on_hlink)
2076 VOP_FSYNC(vp, MNT_WAIT, 0);
2079 info.v3 = NFS_ISV3(vp);
2081 nfsstats.rpccnt[NFSPROC_LINK]++;
2082 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2083 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2084 nfsm_rndup(cnp->cn_namelen));
2085 ERROROUT(nfsm_fhtom(&info, vp));
2086 ERROROUT(nfsm_fhtom(&info, tdvp));
2087 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2089 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2090 cnp->cn_cred, &error));
2092 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2093 NFS_LATTR_NOSHRINK));
2094 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2099 VTONFS(tdvp)->n_flag |= NLMODIFIED;
2101 VTONFS(vp)->n_attrstamp = 0;
2103 VTONFS(tdvp)->n_attrstamp = 0;
2105 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2107 if (error == EEXIST)
2109 lwkt_reltoken(&nmp->nm_token);
2111 nfs_knote(vp, NOTE_LINK);
2112 nfs_knote(tdvp, NOTE_WRITE);
2119 * nfs symbolic link create call
2121 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2122 * struct componentname *a_cnp, struct vattr *a_vap,
2126 nfs_symlink(struct vop_old_symlink_args *ap)
2128 struct vnode *dvp = ap->a_dvp;
2129 struct vattr *vap = ap->a_vap;
2130 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2131 struct componentname *cnp = ap->a_cnp;
2132 struct nfsv2_sattr *sp;
2133 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2134 struct vnode *newvp = NULL;
2135 struct nfsm_info info;
2138 info.v3 = NFS_ISV3(dvp);
2139 lwkt_gettoken(&nmp->nm_token);
2141 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2142 slen = strlen(ap->a_target);
2143 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2144 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2145 nfsm_rndup(cnp->cn_namelen) +
2146 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2147 ERROROUT(nfsm_fhtom(&info, dvp));
2148 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2151 nfsm_v3attrbuild(&info, vap, FALSE);
2153 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2155 sp = nfsm_build(&info, NFSX_V2SATTR);
2156 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2157 sp->sa_uid = nfs_xdrneg1;
2158 sp->sa_gid = nfs_xdrneg1;
2159 sp->sa_size = nfs_xdrneg1;
2160 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2161 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2165 * Issue the NFS request and get the rpc response.
2167 * Only NFSv3 responses returning an error of 0 actually return
2168 * a file handle that can be converted into newvp without having
2169 * to do an extra lookup rpc.
2171 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2172 cnp->cn_cred, &error));
2175 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2177 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2181 * out code jumps -> here, mrep is also freed.
2189 * If we get an EEXIST error, silently convert it to no-error
2190 * in case of an NFS retry.
2192 if (error == EEXIST)
2196 * If we do not have (or no longer have) an error, and we could
2197 * not extract the newvp from the response due to the request being
2198 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2199 * to obtain a newvp to return.
2201 if (error == 0 && newvp == NULL) {
2202 struct nfsnode *np = NULL;
2204 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2205 cnp->cn_cred, cnp->cn_td, &np);
2215 VTONFS(dvp)->n_flag |= NLMODIFIED;
2217 VTONFS(dvp)->n_attrstamp = 0;
2218 if (error == 0 && *ap->a_vpp)
2219 nfs_knote(*ap->a_vpp, NOTE_WRITE);
2220 lwkt_reltoken(&nmp->nm_token);
2228 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2229 * struct componentname *a_cnp, struct vattr *a_vap)
2232 nfs_mkdir(struct vop_old_mkdir_args *ap)
2234 struct vnode *dvp = ap->a_dvp;
2235 struct vattr *vap = ap->a_vap;
2236 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2237 struct componentname *cnp = ap->a_cnp;
2238 struct nfsv2_sattr *sp;
2239 struct nfsnode *np = NULL;
2240 struct vnode *newvp = NULL;
2242 int error = 0, wccflag = NFSV3_WCCRATTR;
2245 struct nfsm_info info;
2248 info.v3 = NFS_ISV3(dvp);
2249 lwkt_gettoken(&nmp->nm_token);
2251 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2252 lwkt_reltoken(&nmp->nm_token);
2255 len = cnp->cn_namelen;
2256 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2257 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2258 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2259 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2260 ERROROUT(nfsm_fhtom(&info, dvp));
2261 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2263 nfsm_v3attrbuild(&info, vap, FALSE);
2265 sp = nfsm_build(&info, NFSX_V2SATTR);
2266 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2267 sp->sa_uid = nfs_xdrneg1;
2268 sp->sa_gid = nfs_xdrneg1;
2269 sp->sa_size = nfs_xdrneg1;
2270 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2271 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2273 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2274 cnp->cn_cred, &error));
2276 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2279 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2284 VTONFS(dvp)->n_flag |= NLMODIFIED;
2286 VTONFS(dvp)->n_attrstamp = 0;
2288 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2289 * if we can succeed in looking up the directory.
2291 if (error == EEXIST || (!error && !gotvp)) {
2296 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2300 if (newvp->v_type != VDIR)
2308 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2311 lwkt_reltoken(&nmp->nm_token);
2316 * nfs remove directory call
2318 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2319 * struct componentname *a_cnp)
2322 nfs_rmdir(struct vop_old_rmdir_args *ap)
2324 struct vnode *vp = ap->a_vp;
2325 struct vnode *dvp = ap->a_dvp;
2326 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2327 struct componentname *cnp = ap->a_cnp;
2328 int error = 0, wccflag = NFSV3_WCCRATTR;
2329 struct nfsm_info info;
2332 info.v3 = NFS_ISV3(dvp);
2337 lwkt_gettoken(&nmp->nm_token);
2339 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2340 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2341 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2342 nfsm_rndup(cnp->cn_namelen));
2343 ERROROUT(nfsm_fhtom(&info, dvp));
2344 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2346 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2347 cnp->cn_cred, &error));
2349 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2354 VTONFS(dvp)->n_flag |= NLMODIFIED;
2356 VTONFS(dvp)->n_attrstamp = 0;
2358 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2360 if (error == ENOENT)
2363 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2364 lwkt_reltoken(&nmp->nm_token);
2372 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2375 nfs_readdir(struct vop_readdir_args *ap)
2377 struct vnode *vp = ap->a_vp;
2378 struct nfsnode *np = VTONFS(vp);
2379 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2380 struct uio *uio = ap->a_uio;
2384 if (vp->v_type != VDIR)
2387 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2391 lwkt_gettoken(&nmp->nm_token);
2394 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2395 * and then check that is still valid, or if this is an NQNFS mount
2396 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2397 * VOP_GETATTR() does not necessarily go to the wire.
2399 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2400 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2401 if (VOP_GETATTR(vp, &vattr) == 0 &&
2402 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2404 nfsstats.direofcache_hits++;
2410 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2411 * own cache coherency checks so we do not have to.
2413 tresid = uio->uio_resid;
2414 error = nfs_bioread(vp, uio, 0);
2416 if (!error && uio->uio_resid == tresid)
2417 nfsstats.direofcache_misses++;
2419 lwkt_reltoken(&nmp->nm_token);
2426 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2428 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2429 * offset/block and converts the nfs formatted directory entries for userland
2430 * consumption as well as deals with offsets into the middle of blocks.
2431 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2432 * be block-bounded. It must convert to cookies for the actual RPC.
2435 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2438 struct nfs_dirent *dp = NULL;
2443 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2444 struct nfsnode *dnp = VTONFS(vp);
2446 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2448 struct nfsm_info info;
2451 info.v3 = NFS_ISV3(vp);
2454 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2455 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2456 panic("nfs readdirrpc bad uio");
2460 * If there is no cookie, assume directory was stale.
2462 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2466 return (NFSERR_BAD_COOKIE);
2468 * Loop around doing readdir rpc's of size nm_readdirsize
2469 * truncated to a multiple of DIRBLKSIZ.
2470 * The stopping criteria is EOF or buffer full.
2472 while (more_dirs && bigenough) {
2473 nfsstats.rpccnt[NFSPROC_READDIR]++;
2474 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2475 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2476 ERROROUT(nfsm_fhtom(&info, vp));
2478 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2479 *tl++ = cookie.nfsuquad[0];
2480 *tl++ = cookie.nfsuquad[1];
2481 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2482 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2485 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2486 * WITH NFSv2!!! There's nothing I can really do
2487 * about it other than to hope the server supports
2490 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2491 *tl++ = cookie.nfsuquad[0];
2493 *tl = txdr_unsigned(nmp->nm_readdirsize);
2494 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2496 nfs_vpcred(vp, ND_READ), &error));
2498 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2499 NFS_LATTR_NOSHRINK));
2500 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2501 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2502 dnp->n_cookieverf.nfsuquad[1] = *tl;
2504 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2505 more_dirs = fxdr_unsigned(int, *tl);
2507 /* loop thru the dir entries, converting them to std form */
2508 while (more_dirs && bigenough) {
2510 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2511 fileno = fxdr_hyper(tl);
2512 len = fxdr_unsigned(int, *(tl + 2));
2514 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2515 fileno = fxdr_unsigned(u_quad_t, *tl++);
2516 len = fxdr_unsigned(int, *tl);
2518 if (len <= 0 || len > NFS_MAXNAMLEN) {
2526 * len is the number of bytes in the path element
2527 * name, not including the \0 termination.
2529 * tlen is the number of bytes w have to reserve for
2530 * the path element name.
2532 tlen = nfsm_rndup(len);
2534 tlen += 4; /* To ensure null termination */
2537 * If the entry would cross a DIRBLKSIZ boundary,
2538 * extend the previous nfs_dirent to cover the
2541 left = DIRBLKSIZ - blksiz;
2542 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2543 dp->nfs_reclen += left;
2544 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2545 uiop->uio_iov->iov_len -= left;
2546 uiop->uio_offset += left;
2547 uiop->uio_resid -= left;
2550 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2553 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2554 dp->nfs_ino = fileno;
2555 dp->nfs_namlen = len;
2556 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2557 dp->nfs_type = DT_UNKNOWN;
2558 blksiz += dp->nfs_reclen;
2559 if (blksiz == DIRBLKSIZ)
2561 uiop->uio_offset += sizeof(struct nfs_dirent);
2562 uiop->uio_resid -= sizeof(struct nfs_dirent);
2563 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2564 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2565 ERROROUT(nfsm_mtouio(&info, uiop, len));
2568 * The uiop has advanced by nfs_dirent + len
2569 * but really needs to advance by
2572 cp = uiop->uio_iov->iov_base;
2574 *cp = '\0'; /* null terminate */
2575 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2576 uiop->uio_iov->iov_len -= tlen;
2577 uiop->uio_offset += tlen;
2578 uiop->uio_resid -= tlen;
2581 * NFS strings must be rounded up (nfsm_myouio
2582 * handled that in the bigenough case).
2584 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2587 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2589 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2593 * If we were able to accomodate the last entry,
2594 * get the cookie for the next one. Otherwise
2595 * hold-over the cookie for the one we were not
2596 * able to accomodate.
2599 cookie.nfsuquad[0] = *tl++;
2601 cookie.nfsuquad[1] = *tl++;
2602 } else if (info.v3) {
2607 more_dirs = fxdr_unsigned(int, *tl);
2610 * If at end of rpc data, get the eof boolean
2613 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2614 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2620 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2621 * by increasing d_reclen for the last record.
2624 left = DIRBLKSIZ - blksiz;
2625 dp->nfs_reclen += left;
2626 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2627 uiop->uio_iov->iov_len -= left;
2628 uiop->uio_offset += left;
2629 uiop->uio_resid -= left;
2634 * We hit the end of the directory, update direofoffset.
2636 dnp->n_direofoffset = uiop->uio_offset;
2639 * There is more to go, insert the link cookie so the
2640 * next block can be read.
2642 if (uiop->uio_resid > 0)
2643 kprintf("EEK! readdirrpc resid > 0\n");
2644 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2652 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2655 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2658 struct nfs_dirent *dp;
2660 struct vnode *newvp;
2662 caddr_t dpossav1, dpossav2;
2664 struct mbuf *mdsav1, *mdsav2;
2666 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2667 struct nfsnode *dnp = VTONFS(vp), *np;
2670 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2671 int attrflag, fhsize;
2672 struct nchandle nch;
2673 struct nchandle dnch;
2674 struct nlcomponent nlc;
2675 struct nfsm_info info;
2684 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2685 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2686 panic("nfs readdirplusrpc bad uio");
2689 * Obtain the namecache record for the directory so we have something
2690 * to use as a basis for creating the entries. This function will
2691 * return a held (but not locked) ncp. The ncp may be disconnected
2692 * from the tree and cannot be used for upward traversals, and the
2693 * ncp may be unnamed. Note that other unrelated operations may
2694 * cause the ncp to be named at any time.
2696 * We have to lock the ncp to prevent a lock order reversal when
2697 * rdirplus does nlookups of the children, because the vnode is
2698 * locked and has to stay that way.
2700 cache_fromdvp(vp, NULL, 0, &dnch);
2701 bzero(&nlc, sizeof(nlc));
2705 * If there is no cookie, assume directory was stale.
2707 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2713 return (NFSERR_BAD_COOKIE);
2717 * Loop around doing readdir rpc's of size nm_readdirsize
2718 * truncated to a multiple of DIRBLKSIZ.
2719 * The stopping criteria is EOF or buffer full.
2721 while (more_dirs && bigenough) {
2722 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2723 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2724 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2725 ERROROUT(nfsm_fhtom(&info, vp));
2726 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2727 *tl++ = cookie.nfsuquad[0];
2728 *tl++ = cookie.nfsuquad[1];
2729 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2730 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2731 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2732 *tl = txdr_unsigned(nmp->nm_rsize);
2733 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2735 nfs_vpcred(vp, ND_READ), &error));
2736 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2737 NFS_LATTR_NOSHRINK));
2738 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2739 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2740 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2741 more_dirs = fxdr_unsigned(int, *tl);
2743 /* loop thru the dir entries, doctoring them to 4bsd form */
2744 while (more_dirs && bigenough) {
2745 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2746 fileno = fxdr_hyper(tl);
2747 len = fxdr_unsigned(int, *(tl + 2));
2748 if (len <= 0 || len > NFS_MAXNAMLEN) {
2754 tlen = nfsm_rndup(len);
2756 tlen += 4; /* To ensure null termination*/
2757 left = DIRBLKSIZ - blksiz;
2758 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2759 dp->nfs_reclen += left;
2760 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2761 uiop->uio_iov->iov_len -= left;
2762 uiop->uio_offset += left;
2763 uiop->uio_resid -= left;
2766 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2769 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2770 dp->nfs_ino = fileno;
2771 dp->nfs_namlen = len;
2772 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2773 dp->nfs_type = DT_UNKNOWN;
2774 blksiz += dp->nfs_reclen;
2775 if (blksiz == DIRBLKSIZ)
2777 uiop->uio_offset += sizeof(struct nfs_dirent);
2778 uiop->uio_resid -= sizeof(struct nfs_dirent);
2779 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2780 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2781 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2782 nlc.nlc_namelen = len;
2783 ERROROUT(nfsm_mtouio(&info, uiop, len));
2784 cp = uiop->uio_iov->iov_base;
2787 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2788 uiop->uio_iov->iov_len -= tlen;
2789 uiop->uio_offset += tlen;
2790 uiop->uio_resid -= tlen;
2792 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2794 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2796 cookie.nfsuquad[0] = *tl++;
2797 cookie.nfsuquad[1] = *tl++;
2803 * Since the attributes are before the file handle
2804 * (sigh), we must skip over the attributes and then
2805 * come back and get them.
2807 attrflag = fxdr_unsigned(int, *tl);
2809 dpossav1 = info.dpos;
2811 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2812 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2813 doit = fxdr_unsigned(int, *tl);
2815 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2817 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2818 !NFS_CMPFH(dnp, fhp, fhsize)
2822 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2823 nlc.nlc_namelen, nlc.nlc_namelen,
2827 * This is a bit hokey but there isn't
2828 * much we can do about it. We can't
2829 * hold the directory vp locked while
2830 * doing lookups and gets.
2832 nch = cache_nlookup_nonblock(&dnch, &nlc);
2833 if (nch.ncp == NULL)
2835 cache_setunresolved(&nch);
2836 error = nfs_nget_nonblock(vp->v_mount, fhp,
2844 dpossav2 = info.dpos;
2845 info.dpos = dpossav1;
2848 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2849 info.dpos = dpossav2;
2852 IFTODT(VTTOIF(np->n_vattr.va_type));
2853 nfs_cache_setvp(&nch, newvp,
2854 nfspos_cache_timeout);
2862 kprintf("Warning: NFS/rddirplus, "
2863 "UNABLE TO ENTER %*.*s\n",
2864 nlc.nlc_namelen, nlc.nlc_namelen,
2870 /* Just skip over the file handle */
2871 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2872 i = fxdr_unsigned(int, *tl);
2873 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2875 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2876 more_dirs = fxdr_unsigned(int, *tl);
2879 * If at end of rpc data, get the eof boolean
2882 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2883 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2889 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2890 * by increasing d_reclen for the last record.
2893 left = DIRBLKSIZ - blksiz;
2894 dp->nfs_reclen += left;
2895 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2896 uiop->uio_iov->iov_len -= left;
2897 uiop->uio_offset += left;
2898 uiop->uio_resid -= left;
2902 * We are now either at the end of the directory or have filled the
2906 dnp->n_direofoffset = uiop->uio_offset;
2908 if (uiop->uio_resid > 0)
2909 kprintf("EEK! readdirplusrpc resid > 0\n");
2910 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2914 if (newvp != NULLVP) {
2927 * Silly rename. To make the NFS filesystem that is stateless look a little
2928 * more like the "ufs" a remove of an active vnode is translated to a rename
2929 * to a funny looking filename that is removed by nfs_inactive on the
2930 * nfsnode. There is the potential for another process on a different client
2931 * to create the same funny name between the nfs_lookitup() fails and the
2932 * nfs_rename() completes, but...
2935 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2937 struct sillyrename *sp;
2942 * Force finalization so the VOP_INACTIVE() call is not delayed.
2944 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
2947 * We previously purged dvp instead of vp. I don't know why, it
2948 * completely destroys performance. We can't do it anyway with the
2949 * new VFS API since we would be breaking the namecache topology.
2951 cache_purge(vp); /* XXX */
2954 if (vp->v_type == VDIR)
2955 panic("nfs: sillyrename dir");
2957 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2958 sp->s_cred = crdup(cnp->cn_cred);
2962 /* Fudge together a funny name */
2963 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2964 (int)(intptr_t)cnp->cn_td);
2966 /* Try lookitups until we get one that isn't there */
2967 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2968 cnp->cn_td, NULL) == 0) {
2970 if (sp->s_name[4] > 'z') {
2975 error = nfs_renameit(dvp, cnp, sp);
2978 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2980 np->n_sillyrename = sp;
2985 kfree((caddr_t)sp, M_NFSREQ);
2990 * Look up a file name and optionally either update the file handle or
2991 * allocate an nfsnode, depending on the value of npp.
2992 * npp == NULL --> just do the lookup
2993 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2995 * *npp != NULL --> update the file handle in the vnode
2998 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2999 struct thread *td, struct nfsnode **npp)
3001 struct vnode *newvp = NULL;
3002 struct nfsnode *np, *dnp = VTONFS(dvp);
3003 int error = 0, fhlen, attrflag;
3005 struct nfsm_info info;
3008 info.v3 = NFS_ISV3(dvp);
3010 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3011 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3012 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3013 ERROROUT(nfsm_fhtom(&info, dvp));
3014 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3015 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
3016 if (npp && !error) {
3017 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
3020 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3021 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
3022 np->n_fhp = &np->n_fh;
3023 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3024 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
3025 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3026 np->n_fhsize = fhlen;
3028 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3032 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
3041 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3042 NFS_LATTR_NOSHRINK));
3043 if (!attrflag && *npp == NULL) {
3053 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3059 if (npp && *npp == NULL) {
3074 * Nfs Version 3 commit rpc
3076 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3080 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3082 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3083 int error = 0, wccflag = NFSV3_WCCRATTR;
3084 struct nfsm_info info;
3090 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3092 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3093 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3094 ERROROUT(nfsm_fhtom(&info, vp));
3095 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3096 txdr_hyper(offset, tl);
3098 *tl = txdr_unsigned(cnt);
3099 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3100 nfs_vpcred(vp, ND_WRITE), &error));
3101 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3103 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3104 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3105 NFSX_V3WRITEVERF)) {
3106 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3108 error = NFSERR_STALEWRITEVERF;
3119 * - make nfs_bmap() essentially a no-op that does no translation
3120 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3121 * (Maybe I could use the process's page mapping, but I was concerned that
3122 * Kernel Write might not be enabled and also figured copyout() would do
3123 * a lot more work than bcopy() and also it currently happens in the
3124 * context of the swapper process (2).
3126 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3127 * off_t *a_doffsetp, int *a_runp, int *a_runb)
3130 nfs_bmap(struct vop_bmap_args *ap)
3132 /* no token lock required */
3133 if (ap->a_doffsetp != NULL)
3134 *ap->a_doffsetp = ap->a_loffset;
3135 if (ap->a_runp != NULL)
3137 if (ap->a_runb != NULL)
3146 nfs_strategy(struct vop_strategy_args *ap)
3148 struct bio *bio = ap->a_bio;
3150 struct buf *bp __debugvar = bio->bio_buf;
3151 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3155 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3156 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3157 KASSERT(BUF_REFCNT(bp) > 0,
3158 ("nfs_strategy: buffer %p not locked", bp));
3160 if (bio->bio_flags & BIO_SYNC)
3161 td = curthread; /* XXX */
3165 lwkt_gettoken(&nmp->nm_token);
3168 * We probably don't need to push an nbio any more since no
3169 * block conversion is required due to the use of 64 bit byte
3170 * offsets, but do it anyway.
3172 * NOTE: When NFS callers itself via this strategy routines and
3173 * sets up a synchronous I/O, it expects the I/O to run
3174 * synchronously (its bio_done routine just assumes it),
3175 * so for now we have to honor the bit.
3177 nbio = push_bio(bio);
3178 nbio->bio_offset = bio->bio_offset;
3179 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3182 * If the op is asynchronous and an i/o daemon is waiting
3183 * queue the request, wake it up and wait for completion
3184 * otherwise just do it ourselves.
3186 if (bio->bio_flags & BIO_SYNC) {
3187 error = nfs_doio(ap->a_vp, nbio, td);
3189 nfs_asyncio(ap->a_vp, nbio);
3192 lwkt_reltoken(&nmp->nm_token);
3200 * NB Currently unsupported.
3202 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3206 nfs_mmap(struct vop_mmap_args *ap)
3208 /* no token lock required */
3213 * fsync vnode op. Just call nfs_flush() with commit == 1.
3215 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3219 nfs_fsync(struct vop_fsync_args *ap)
3221 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3224 lwkt_gettoken(&nmp->nm_token);
3227 * NOTE: Because attributes are set synchronously we currently
3228 * do not have to implement vsetisdirty()/vclrisdirty().
3230 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3232 lwkt_reltoken(&nmp->nm_token);
3238 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3239 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3240 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3241 * set the buffer contains data that has already been written to the server
3242 * and which now needs a commit RPC.
3244 * If commit is 0 we only take one pass and only flush buffers containing new
3247 * If commit is 1 we take two passes, issuing a commit RPC in the second
3250 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3251 * to completely flush all pending data.
3253 * Note that the RB_SCAN code properly handles the case where the
3254 * callback might block and directly or indirectly (another thread) cause
3255 * the RB tree to change.
3258 #ifndef NFS_COMMITBVECSIZ
3259 #define NFS_COMMITBVECSIZ 16
3262 struct nfs_flush_info {
3263 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3270 struct buf *bvary[NFS_COMMITBVECSIZ];
3276 static int nfs_flush_bp(struct buf *bp, void *data);
3277 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3280 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3282 struct nfsnode *np = VTONFS(vp);
3283 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3284 struct nfs_flush_info info;
3287 bzero(&info, sizeof(info));
3290 info.waitfor = waitfor;
3291 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3293 lwkt_gettoken(&vp->v_token);
3299 info.mode = NFI_FLUSHNEW;
3300 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3301 nfs_flush_bp, &info);
3304 * Take a second pass if committing and no error occured.
3305 * Clean up any left over collection (whether an error
3308 if (commit && error == 0) {
3309 info.mode = NFI_COMMIT;
3310 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3311 nfs_flush_bp, &info);
3313 error = nfs_flush_docommit(&info, error);
3317 * Wait for pending I/O to complete before checking whether
3318 * any further dirty buffers exist.
3320 while (waitfor == MNT_WAIT &&
3321 bio_track_active(&vp->v_track_write)) {
3322 error = bio_track_wait(&vp->v_track_write,
3323 info.slpflag, info.slptimeo);
3326 * We have to be able to break out if this
3327 * is an 'intr' mount.
3329 if (nfs_sigintr(nmp, NULL, td)) {
3335 * Since we do not process pending signals,
3336 * once we get a PCATCH our tsleep() will no
3337 * longer sleep, switch to a fixed timeout
3340 if (info.slpflag == PCATCH) {
3342 info.slptimeo = 2 * hz;
3349 * Loop if we are flushing synchronous as well as committing,
3350 * and dirty buffers are still present. Otherwise we might livelock.
3352 } while (waitfor == MNT_WAIT && commit &&
3353 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3356 * The callbacks have to return a negative error to terminate the
3363 * Deal with any error collection
3365 if (np->n_flag & NWRITEERR) {
3366 error = np->n_error;
3367 np->n_flag &= ~NWRITEERR;
3369 lwkt_reltoken(&vp->v_token);
3375 nfs_flush_bp(struct buf *bp, void *data)
3377 struct nfs_flush_info *info = data;
3383 switch(info->mode) {
3385 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3386 if (error && info->loops && info->waitfor == MNT_WAIT) {
3387 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3389 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3390 if (info->slpflag & PCATCH)
3391 lkflags |= LK_PCATCH;
3392 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3398 * Ignore locking errors
3406 * The buffer may have changed out from under us, even if
3407 * we did not block (MPSAFE). Check again now that it is
3410 if (bp->b_vp == info->vp &&
3411 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3420 * Only process buffers in need of a commit which we can
3421 * immediately lock. This may prevent a buffer from being
3422 * committed, but the normal flush loop will block on the
3423 * same buffer so we shouldn't get into an endless loop.
3425 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3426 (B_DELWRI | B_NEEDCOMMIT)) {
3429 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3433 * We must recheck after successfully locking the buffer.
3435 if (bp->b_vp != info->vp ||
3436 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3437 (B_DELWRI | B_NEEDCOMMIT)) {
3443 * NOTE: storing the bp in the bvary[] basically sets
3444 * it up for a commit operation.
3446 * We must call vfs_busy_pages() now so the commit operation
3447 * is interlocked with user modifications to memory mapped
3448 * pages. The b_dirtyoff/b_dirtyend range is not correct
3449 * until after the pages have been busied.
3451 * Note: to avoid loopback deadlocks, we do not
3452 * assign b_runningbufspace.
3455 bp->b_cmd = BUF_CMD_WRITE;
3456 vfs_busy_pages(bp->b_vp, bp);
3457 info->bvary[info->bvsize] = bp;
3458 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3459 if (info->bvsize == 0 || toff < info->beg_off)
3460 info->beg_off = toff;
3461 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3462 if (info->bvsize == 0 || toff > info->end_off)
3463 info->end_off = toff;
3465 if (info->bvsize == NFS_COMMITBVECSIZ) {
3466 error = nfs_flush_docommit(info, 0);
3467 KKASSERT(info->bvsize == 0);
3475 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3485 if (info->bvsize > 0) {
3487 * Commit data on the server, as required. Note that
3488 * nfs_commit will use the vnode's cred for the commit.
3489 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3491 bytes = info->end_off - info->beg_off;
3492 if (bytes > 0x40000000)
3497 retv = nfs_commitrpc_uio(vp, info->beg_off,
3498 (int)bytes, info->td);
3499 if (retv == NFSERR_STALEWRITEVERF)
3500 nfs_clearcommit(vp->v_mount);
3504 * Now, either mark the blocks I/O done or mark the
3505 * blocks dirty, depending on whether the commit
3508 for (i = 0; i < info->bvsize; ++i) {
3509 bp = info->bvary[i];
3510 if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3512 * Either an error or the original
3513 * vfs_busy_pages() cleared B_NEEDCOMMIT
3514 * due to finding new dirty VM pages in
3517 * Leave B_DELWRI intact.
3519 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3520 vfs_unbusy_pages(bp);
3521 bp->b_cmd = BUF_CMD_DONE;
3525 * Success, remove B_DELWRI ( bundirty() ).
3527 * b_dirtyoff/b_dirtyend seem to be NFS
3528 * specific. We should probably move that
3529 * into bundirty(). XXX
3531 * We are faking an I/O write, we have to
3532 * start the transaction in order to
3533 * immediately biodone() it.
3536 bp->b_flags &= ~B_ERROR;
3537 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3538 bp->b_dirtyoff = bp->b_dirtyend = 0;
3539 biodone(&bp->b_bio1);
3548 * NFS advisory byte-level locks.
3549 * Currently unsupported.
3551 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3555 nfs_advlock(struct vop_advlock_args *ap)
3557 struct nfsnode *np = VTONFS(ap->a_vp);
3559 /* no token lock currently required */
3561 * The following kludge is to allow diskless support to work
3562 * until a real NFS lockd is implemented. Basically, just pretend
3563 * that this is a local lock.
3565 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3569 * Print out the contents of an nfsnode.
3571 * nfs_print(struct vnode *a_vp)
3574 nfs_print(struct vop_print_args *ap)
3576 struct vnode *vp = ap->a_vp;
3577 struct nfsnode *np = VTONFS(vp);
3579 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3580 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3581 if (vp->v_type == VFIFO)
3588 * nfs special file access vnode op.
3590 * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3593 nfs_laccess(struct vop_access_args *ap)
3595 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3599 lwkt_gettoken(&nmp->nm_token);
3600 error = VOP_GETATTR(ap->a_vp, &vattr);
3602 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3605 lwkt_reltoken(&nmp->nm_token);
3611 * Read wrapper for fifos.
3613 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3614 * struct ucred *a_cred)
3617 nfsfifo_read(struct vop_read_args *ap)
3619 struct nfsnode *np = VTONFS(ap->a_vp);
3621 /* no token access required */
3626 getnanotime(&np->n_atim);
3627 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3631 * Write wrapper for fifos.
3633 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3634 * struct ucred *a_cred)
3637 nfsfifo_write(struct vop_write_args *ap)
3639 struct nfsnode *np = VTONFS(ap->a_vp);
3641 /* no token access required */
3646 getnanotime(&np->n_mtim);
3647 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3651 * Close wrapper for fifos.
3653 * Update the times on the nfsnode then do fifo close.
3655 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3658 nfsfifo_close(struct vop_close_args *ap)
3660 struct vnode *vp = ap->a_vp;
3661 struct nfsnode *np = VTONFS(vp);
3665 /* no token access required */
3667 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3668 if (np->n_flag & (NACC | NUPD)) {
3670 if (np->n_flag & NACC)
3672 if (np->n_flag & NUPD)
3675 if (VREFCNT(vp) == 1 &&
3676 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3678 if (np->n_flag & NACC)
3679 vattr.va_atime = np->n_atim;
3680 if (np->n_flag & NUPD)
3681 vattr.va_mtime = np->n_mtim;
3682 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3685 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3688 /************************************************************************
3690 ************************************************************************/
3692 static void filt_nfsdetach(struct knote *kn);
3693 static int filt_nfsread(struct knote *kn, long hint);
3694 static int filt_nfswrite(struct knote *kn, long hint);
3695 static int filt_nfsvnode(struct knote *kn, long hint);
3697 static struct filterops nfsread_filtops =
3698 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3699 NULL, filt_nfsdetach, filt_nfsread };
3700 static struct filterops nfswrite_filtops =
3701 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3702 NULL, filt_nfsdetach, filt_nfswrite };
3703 static struct filterops nfsvnode_filtops =
3704 { FILTEROP_ISFD | FILTEROP_MPSAFE,
3705 NULL, filt_nfsdetach, filt_nfsvnode };
3708 nfs_kqfilter (struct vop_kqfilter_args *ap)
3710 struct vnode *vp = ap->a_vp;
3711 struct knote *kn = ap->a_kn;
3713 switch (kn->kn_filter) {
3715 kn->kn_fop = &nfsread_filtops;
3718 kn->kn_fop = &nfswrite_filtops;
3721 kn->kn_fop = &nfsvnode_filtops;
3724 return (EOPNOTSUPP);
3727 kn->kn_hook = (caddr_t)vp;
3729 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3735 filt_nfsdetach(struct knote *kn)
3737 struct vnode *vp = (void *)kn->kn_hook;
3739 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3743 filt_nfsread(struct knote *kn, long hint)
3745 struct vnode *vp = (void *)kn->kn_hook;
3746 struct nfsnode *node = VTONFS(vp);
3749 if (hint == NOTE_REVOKE) {
3750 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3755 * Interlock against MP races when performing this function. XXX
3757 /* TMPFS_NODE_LOCK_SH(node); */
3758 off = node->n_size - kn->kn_fp->f_offset;
3759 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3760 if (kn->kn_sfflags & NOTE_OLDAPI) {
3761 /* TMPFS_NODE_UNLOCK(node); */
3764 if (kn->kn_data == 0) {
3765 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3767 /* TMPFS_NODE_UNLOCK(node); */
3768 return (kn->kn_data != 0);
3772 filt_nfswrite(struct knote *kn, long hint)
3774 if (hint == NOTE_REVOKE)
3775 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3781 filt_nfsvnode(struct knote *kn, long hint)
3783 if (kn->kn_sfflags & hint)
3784 kn->kn_fflags |= hint;
3785 if (hint == NOTE_REVOKE) {
3786 kn->kn_flags |= (EV_EOF | EV_NODATA);
3789 return (kn->kn_fflags != 0);