Shorten some paths in various Makefiles.
[dragonfly.git] / sys / vfs / nfs / nfs_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
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
30 * SUCH DAMAGE.
31 *
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 $
34 */
35
36
37/*
38 * vnode op calls for Sun NFS version 2 and 3
39 */
40
41#include "opt_inet.h"
42
43#include <sys/param.h>
44#include <sys/kernel.h>
45#include <sys/systm.h>
46#include <sys/resourcevar.h>
47#include <sys/proc.h>
48#include <sys/mount.h>
49#include <sys/buf.h>
50#include <sys/malloc.h>
51#include <sys/mbuf.h>
52#include <sys/namei.h>
fad57d0e 53#include <sys/nlookup.h>
984263bc
MD
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>
59#include <sys/stat.h>
60#include <sys/sysctl.h>
61#include <sys/conf.h>
62
63#include <vm/vm.h>
64#include <vm/vm_extern.h>
984263bc 65
3020e3be
MD
66#include <sys/buf2.h>
67
1f2de5d4 68#include <vfs/fifofs/fifo.h>
01f31ab3
JS
69#include <vfs/ufs/dir.h>
70
71#undef DIRBLKSIZ
984263bc 72
1f2de5d4
MD
73#include "rpcv2.h"
74#include "nfsproto.h"
75#include "nfs.h"
1f2de5d4 76#include "nfsmount.h"
c1cf1e59 77#include "nfsnode.h"
1f2de5d4
MD
78#include "xdr_subs.h"
79#include "nfsm_subs.h"
984263bc
MD
80
81#include <net/if.h>
82#include <netinet/in.h>
83#include <netinet/in_var.h>
84
85/* Defs */
86#define TRUE 1
87#define FALSE 0
88
a6ee311a
RG
89static int nfsfifo_read (struct vop_read_args *);
90static int nfsfifo_write (struct vop_write_args *);
a6ee311a 91static int nfsfifo_close (struct vop_close_args *);
a6ee311a 92static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
e62afb5f
MD
93static int nfs_lookup (struct vop_old_lookup_args *);
94static int nfs_create (struct vop_old_create_args *);
95static int nfs_mknod (struct vop_old_mknod_args *);
a6ee311a
RG
96static int nfs_open (struct vop_open_args *);
97static int nfs_close (struct vop_close_args *);
98static int nfs_access (struct vop_access_args *);
99static int nfs_getattr (struct vop_getattr_args *);
100static int nfs_setattr (struct vop_setattr_args *);
101static int nfs_read (struct vop_read_args *);
a6ee311a 102static int nfs_fsync (struct vop_fsync_args *);
e62afb5f
MD
103static int nfs_remove (struct vop_old_remove_args *);
104static int nfs_link (struct vop_old_link_args *);
105static int nfs_rename (struct vop_old_rename_args *);
106static int nfs_mkdir (struct vop_old_mkdir_args *);
107static int nfs_rmdir (struct vop_old_rmdir_args *);
108static int nfs_symlink (struct vop_old_symlink_args *);
a6ee311a
RG
109static int nfs_readdir (struct vop_readdir_args *);
110static int nfs_bmap (struct vop_bmap_args *);
111static int nfs_strategy (struct vop_strategy_args *);
112static int nfs_lookitup (struct vnode *, const char *, int,
113 struct ucred *, struct thread *, struct nfsnode **);
114static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
8be7edad 115static int nfs_laccess (struct vop_access_args *);
a6ee311a
RG
116static int nfs_readlink (struct vop_readlink_args *);
117static int nfs_print (struct vop_print_args *);
118static int nfs_advlock (struct vop_advlock_args *);
05c073d6 119static int nfs_kqfilter (struct vop_kqfilter_args *ap);
fad57d0e
MD
120
121static int nfs_nresolve (struct vop_nresolve_args *);
984263bc
MD
122/*
123 * Global vfs data structures for nfs
124 */
66a1ddf5
MD
125struct vop_ops nfsv2_vnode_vops = {
126 .vop_default = vop_defaultop,
127 .vop_access = nfs_access,
128 .vop_advlock = nfs_advlock,
129 .vop_bmap = nfs_bmap,
130 .vop_close = nfs_close,
131 .vop_old_create = nfs_create,
132 .vop_fsync = nfs_fsync,
133 .vop_getattr = nfs_getattr,
8452310f
MD
134 .vop_getpages = vop_stdgetpages,
135 .vop_putpages = vop_stdputpages,
66a1ddf5 136 .vop_inactive = nfs_inactive,
66a1ddf5 137 .vop_old_link = nfs_link,
66a1ddf5
MD
138 .vop_old_lookup = nfs_lookup,
139 .vop_old_mkdir = nfs_mkdir,
140 .vop_old_mknod = nfs_mknod,
66a1ddf5 141 .vop_open = nfs_open,
66a1ddf5
MD
142 .vop_print = nfs_print,
143 .vop_read = nfs_read,
144 .vop_readdir = nfs_readdir,
145 .vop_readlink = nfs_readlink,
146 .vop_reclaim = nfs_reclaim,
147 .vop_old_remove = nfs_remove,
148 .vop_old_rename = nfs_rename,
149 .vop_old_rmdir = nfs_rmdir,
150 .vop_setattr = nfs_setattr,
151 .vop_strategy = nfs_strategy,
152 .vop_old_symlink = nfs_symlink,
66a1ddf5 153 .vop_write = nfs_write,
05c073d6
MD
154 .vop_nresolve = nfs_nresolve,
155 .vop_kqfilter = nfs_kqfilter
984263bc 156};
984263bc
MD
157
158/*
159 * Special device vnode ops
160 */
66a1ddf5 161struct vop_ops nfsv2_spec_vops = {
8be7edad
MD
162 .vop_default = vop_defaultop,
163 .vop_access = nfs_laccess,
164 .vop_close = nfs_close,
66a1ddf5
MD
165 .vop_fsync = nfs_fsync,
166 .vop_getattr = nfs_getattr,
167 .vop_inactive = nfs_inactive,
66a1ddf5 168 .vop_print = nfs_print,
8be7edad 169 .vop_read = vop_stdnoread,
66a1ddf5
MD
170 .vop_reclaim = nfs_reclaim,
171 .vop_setattr = nfs_setattr,
8be7edad 172 .vop_write = vop_stdnowrite
984263bc 173};
984263bc 174
66a1ddf5
MD
175struct vop_ops nfsv2_fifo_vops = {
176 .vop_default = fifo_vnoperate,
8be7edad 177 .vop_access = nfs_laccess,
66a1ddf5
MD
178 .vop_close = nfsfifo_close,
179 .vop_fsync = nfs_fsync,
180 .vop_getattr = nfs_getattr,
181 .vop_inactive = nfs_inactive,
66a1ddf5
MD
182 .vop_print = nfs_print,
183 .vop_read = nfsfifo_read,
184 .vop_reclaim = nfs_reclaim,
185 .vop_setattr = nfs_setattr,
66a1ddf5 186 .vop_write = nfsfifo_write
984263bc 187};
984263bc 188
a6ee311a 189static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
984263bc 190 struct componentname *cnp,
a6ee311a
RG
191 struct vattr *vap);
192static int nfs_removerpc (struct vnode *dvp, const char *name,
984263bc 193 int namelen,
a6ee311a
RG
194 struct ucred *cred, struct thread *td);
195static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
984263bc
MD
196 int fnamelen, struct vnode *tdvp,
197 const char *tnameptr, int tnamelen,
a6ee311a
RG
198 struct ucred *cred, struct thread *td);
199static int nfs_renameit (struct vnode *sdvp,
984263bc 200 struct componentname *scnp,
a6ee311a 201 struct sillyrename *sp);
984263bc 202
984263bc
MD
203SYSCTL_DECL(_vfs_nfs);
204
a482a28a
MD
205static int nfs_flush_on_rename = 1;
206SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
207 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
bb7bf7b2
MD
208static int nfs_flush_on_hlink = 0;
209SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
210 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
a482a28a 211
97100839 212static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
984263bc
MD
213SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
214 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
215
4d17b298
MD
216static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
217SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
a15b4eef
MD
218 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
219
220static int nfspos_cache_timeout = NFS_MINATTRTIMO;
221SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
222 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
4d17b298 223
984263bc
MD
224static int nfsv3_commit_on_close = 0;
225SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
226 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
227#if 0
228SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
229 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
230
231SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
232 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
233#endif
234
235#define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
236 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
237 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
af5fde9c 238
05c073d6
MD
239static __inline
240void
241nfs_knote(struct vnode *vp, int flags)
242{
243 if (flags)
244 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
245}
246
af5fde9c
MD
247/*
248 * Returns whether a name component is a degenerate '.' or '..'.
249 */
250static __inline
251int
252nlcdegenerate(struct nlcomponent *nlc)
253{
254 if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
255 return(1);
256 if (nlc->nlc_namelen == 2 &&
257 nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
258 return(1);
259 return(0);
260}
261
984263bc 262static int
dadab5e9 263nfs3_access_otw(struct vnode *vp, int wmode,
e851b29e 264 struct thread *td, struct ucred *cred)
984263bc 265{
42edf14f
MD
266 struct nfsnode *np = VTONFS(vp);
267 int attrflag;
268 int error = 0;
984263bc 269 u_int32_t *tl;
984263bc 270 u_int32_t rmode;
42edf14f
MD
271 struct nfsm_info info;
272
273 info.mrep = NULL;
274 info.v3 = 1;
984263bc
MD
275
276 nfsstats.rpccnt[NFSPROC_ACCESS]++;
42edf14f
MD
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);
984263bc 281 *tl = txdr_unsigned(wmode);
42edf14f
MD
282 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
283 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
284 if (error == 0) {
285 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
286 rmode = fxdr_unsigned(u_int32_t, *tl);
287 np->n_mode = rmode;
288 np->n_modeuid = cred->cr_uid;
3a6f9faf 289 np->n_modestamp = mycpu->gd_time_seconds;
984263bc 290 }
42edf14f
MD
291 m_freem(info.mrep);
292 info.mrep = NULL;
6b08710e 293nfsmout:
984263bc
MD
294 return error;
295}
296
297/*
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.
e851b29e 302 *
b478fdce 303 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
984263bc
MD
304 */
305static int
e851b29e 306nfs_access(struct vop_access_args *ap)
984263bc 307{
40822939 308 struct ucred *cred;
40393ded 309 struct vnode *vp = ap->a_vp;
87de5057 310 thread_t td = curthread;
984263bc
MD
311 int error = 0;
312 u_int32_t mode, wmode;
984263bc 313 struct nfsnode *np = VTONFS(vp);
c6b43e93 314 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
42edf14f 315 int v3 = NFS_ISV3(vp);
984263bc 316
c6b43e93
MD
317 lwkt_gettoken(&nmp->nm_token);
318
984263bc
MD
319 /*
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.
323 */
324 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
325 switch (vp->v_type) {
326 case VREG:
327 case VDIR:
328 case VLNK:
c6b43e93 329 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
330 return (EROFS);
331 default:
332 break;
333 }
334 }
40822939
NT
335
336 /*
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.
7adf09fa
MD
341 *
342 * The crdup() here can cause a lot of ucred structures to build-up
343 * (up to maxvnodes), so do our best to avoid it.
40822939
NT
344 */
345 if (ap->a_flags & AT_EACCESS) {
346 cred = crhold(ap->a_cred);
347 } else {
7adf09fa
MD
348 cred = ap->a_cred;
349 if (cred->cr_uid == cred->cr_ruid &&
350 cred->cr_gid == cred->cr_rgid) {
351 cred = crhold(ap->a_cred);
352 } else {
353 cred = crdup(ap->a_cred);
354 cred->cr_uid = cred->cr_ruid;
355 cred->cr_gid = cred->cr_rgid;
356 }
40822939
NT
357 }
358
984263bc
MD
359 /*
360 * For nfs v3, check to see if we have done this recently, and if
361 * so return our cached result instead of making an ACCESS call.
362 * If not, do an access rpc, otherwise you are stuck emulating
363 * ufs_access() locally using the vattr. This may not be correct,
364 * since the server may apply other access criteria such as
365 * client uid-->server uid mapping that we do not know about.
366 */
367 if (v3) {
368 if (ap->a_mode & VREAD)
369 mode = NFSV3ACCESS_READ;
370 else
371 mode = 0;
372 if (vp->v_type != VDIR) {
373 if (ap->a_mode & VWRITE)
374 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
375 if (ap->a_mode & VEXEC)
376 mode |= NFSV3ACCESS_EXECUTE;
377 } else {
378 if (ap->a_mode & VWRITE)
379 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
380 NFSV3ACCESS_DELETE);
381 if (ap->a_mode & VEXEC)
382 mode |= NFSV3ACCESS_LOOKUP;
383 }
384 /* XXX safety belt, only make blanket request if caching */
385 if (nfsaccess_cache_timeout > 0) {
386 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
387 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
388 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
389 } else {
390 wmode = mode;
391 }
392
393 /*
394 * Does our cached result allow us to give a definite yes to
395 * this request?
396 */
fad57d0e
MD
397 if (np->n_modestamp &&
398 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
40822939 399 (cred->cr_uid == np->n_modeuid) &&
fad57d0e 400 ((np->n_mode & mode) == mode)) {
984263bc
MD
401 nfsstats.accesscache_hits++;
402 } else {
403 /*
404 * Either a no, or a don't know. Go to the wire.
405 */
406 nfsstats.accesscache_misses++;
40822939 407 error = nfs3_access_otw(vp, wmode, td, cred);
984263bc
MD
408 if (!error) {
409 if ((np->n_mode & mode) != mode) {
410 error = EACCES;
411 }
412 }
413 }
984263bc 414 } else {
40822939
NT
415 if ((error = nfs_laccess(ap)) != 0) {
416 crfree(cred);
c6b43e93 417 lwkt_reltoken(&nmp->nm_token);
984263bc 418 return (error);
40822939 419 }
984263bc
MD
420
421 /*
422 * Attempt to prevent a mapped root from accessing a file
423 * which it shouldn't. We try to read a byte from the file
424 * if the user is root and the file is not zero length.
8be7edad 425 * After calling nfs_laccess, we should have the correct
984263bc
MD
426 * file size cached.
427 */
40822939 428 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
984263bc
MD
429 && VTONFS(vp)->n_size > 0) {
430 struct iovec aiov;
431 struct uio auio;
432 char buf[1];
433
434 aiov.iov_base = buf;
435 aiov.iov_len = 1;
436 auio.uio_iov = &aiov;
437 auio.uio_iovcnt = 1;
438 auio.uio_offset = 0;
439 auio.uio_resid = 1;
440 auio.uio_segflg = UIO_SYSSPACE;
441 auio.uio_rw = UIO_READ;
87de5057 442 auio.uio_td = td;
984263bc 443
c1cf1e59 444 if (vp->v_type == VREG) {
edb90c22 445 error = nfs_readrpc_uio(vp, &auio);
c1cf1e59 446 } else if (vp->v_type == VDIR) {
984263bc 447 char* bp;
efda3bd0 448 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
984263bc
MD
449 aiov.iov_base = bp;
450 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
cc7d050e 451 error = nfs_readdirrpc_uio(vp, &auio);
efda3bd0 452 kfree(bp, M_TEMP);
c1cf1e59 453 } else if (vp->v_type == VLNK) {
cc7d050e 454 error = nfs_readlinkrpc_uio(vp, &auio);
c1cf1e59 455 } else {
984263bc 456 error = EACCES;
c1cf1e59 457 }
984263bc 458 }
984263bc 459 }
c1cf1e59
MD
460 /*
461 * [re]record creds for reading and/or writing if access
09b1ee9b
MD
462 * was granted. Assume the NFS server will grant read access
463 * for execute requests.
c1cf1e59
MD
464 */
465 if (error == 0) {
40822939
NT
466 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
467 crhold(cred);
c1cf1e59
MD
468 if (np->n_rucred)
469 crfree(np->n_rucred);
40822939 470 np->n_rucred = cred;
c1cf1e59 471 }
40822939
NT
472 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
473 crhold(cred);
c1cf1e59
MD
474 if (np->n_wucred)
475 crfree(np->n_wucred);
40822939 476 np->n_wucred = cred;
c1cf1e59
MD
477 }
478 }
c6b43e93 479 lwkt_reltoken(&nmp->nm_token);
40822939 480 crfree(cred);
7adf09fa 481
c1cf1e59 482 return(error);
984263bc
MD
483}
484
485/*
486 * nfs open vnode op
487 * Check to see if the type is ok
488 * and that deletion is not in progress.
489 * For paged in text files, you will need to flush the page cache
490 * if consistency is lost.
e851b29e 491 *
b478fdce
SW
492 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
493 * struct file *a_fp)
984263bc
MD
494 */
495/* ARGSUSED */
496static int
e851b29e 497nfs_open(struct vop_open_args *ap)
984263bc 498{
40393ded 499 struct vnode *vp = ap->a_vp;
984263bc 500 struct nfsnode *np = VTONFS(vp);
c6b43e93 501 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc
MD
502 struct vattr vattr;
503 int error;
504
c6b43e93
MD
505 lwkt_gettoken(&nmp->nm_token);
506
984263bc
MD
507 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
508#ifdef DIAGNOSTIC
086c1d7e 509 kprintf("open eacces vtyp=%d\n",vp->v_type);
984263bc 510#endif
c6b43e93 511 lwkt_reltoken(&nmp->nm_token);
ca3a2b2f 512 return (EOPNOTSUPP);
984263bc 513 }
5a9187cb 514
ed94161f
MD
515 /*
516 * Save valid creds for reading and writing for later RPCs.
517 */
518 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
519 crhold(ap->a_cred);
520 if (np->n_rucred)
521 crfree(np->n_rucred);
522 np->n_rucred = ap->a_cred;
523 }
524 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
525 crhold(ap->a_cred);
526 if (np->n_wucred)
527 crfree(np->n_wucred);
528 np->n_wucred = ap->a_cred;
529 }
530
984263bc 531 /*
5a9187cb
MD
532 * Clear the attribute cache only if opening with write access. It
533 * is unclear if we should do this at all here, but we certainly
534 * should not clear the cache unconditionally simply because a file
535 * is being opened.
984263bc 536 */
5a9187cb
MD
537 if (ap->a_mode & FWRITE)
538 np->n_attrstamp = 0;
539
e07fef60
MD
540 /*
541 * For normal NFS, reconcile changes made locally verses
542 * changes made remotely. Note that VOP_GETATTR only goes
543 * to the wire if the cached attribute has timed out or been
544 * cleared.
545 *
546 * If local modifications have been made clear the attribute
547 * cache to force an attribute and modified time check. If
548 * GETATTR detects that the file has been changed by someone
549 * other then us it will set NRMODIFIED.
550 *
551 * If we are opening a directory and local changes have been
552 * made we have to invalidate the cache in order to ensure
553 * that we get the most up-to-date information from the
554 * server. XXX
555 */
556 if (np->n_flag & NLMODIFIED) {
557 np->n_attrstamp = 0;
558 if (vp->v_type == VDIR) {
87de5057 559 error = nfs_vinvalbuf(vp, V_SAVE, 1);
1013402d
SW
560 if (error == EINTR) {
561 lwkt_reltoken(&nmp->nm_token);
5a9187cb 562 return (error);
1013402d 563 }
e07fef60 564 nfs_invaldir(vp);
5a9187cb 565 }
984263bc 566 }
87de5057 567 error = VOP_GETATTR(vp, &vattr);
c6b43e93
MD
568 if (error) {
569 lwkt_reltoken(&nmp->nm_token);
e07fef60 570 return (error);
c6b43e93 571 }
e07fef60
MD
572 if (np->n_flag & NRMODIFIED) {
573 if (vp->v_type == VDIR)
574 nfs_invaldir(vp);
87de5057 575 error = nfs_vinvalbuf(vp, V_SAVE, 1);
c6b43e93
MD
576 if (error == EINTR) {
577 lwkt_reltoken(&nmp->nm_token);
e07fef60 578 return (error);
c6b43e93 579 }
e07fef60
MD
580 np->n_flag &= ~NRMODIFIED;
581 }
c6b43e93
MD
582 error = vop_stdopen(ap);
583 lwkt_reltoken(&nmp->nm_token);
fad57d0e 584
c6b43e93 585 return error;
984263bc
MD
586}
587
588/*
589 * nfs close vnode op
590 * What an NFS client should do upon close after writing is a debatable issue.
591 * Most NFS clients push delayed writes to the server upon close, basically for
592 * two reasons:
593 * 1 - So that any write errors may be reported back to the client process
594 * doing the close system call. By far the two most likely errors are
595 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
596 * 2 - To put a worst case upper bound on cache inconsistency between
597 * multiple clients for the file.
598 * There is also a consistency problem for Version 2 of the protocol w.r.t.
599 * not being able to tell if other clients are writing a file concurrently,
600 * since there is no way of knowing if the changed modify time in the reply
601 * is only due to the write for this client.
602 * (NFS Version 3 provides weak cache consistency data in the reply that
603 * should be sufficient to detect and handle this case.)
604 *
605 * The current code does the following:
606 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
607 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
608 * or commit them (this satisfies 1 and 2 except for the
609 * case where the server crashes after this close but
610 * before the commit RPC, which is felt to be "good
611 * enough". Changing the last argument to nfs_flush() to
612 * a 1 would force a commit operation, if it is felt a
613 * commit is necessary now.
614 * for NQNFS - do nothing now, since 2 is dealt with via leases and
615 * 1 should be dealt with via an fsync() system call for
616 * cases where write errors are important.
e851b29e 617 *
b478fdce 618 * nfs_close(struct vnode *a_vp, int a_fflag)
984263bc
MD
619 */
620/* ARGSUSED */
621static int
e851b29e 622nfs_close(struct vop_close_args *ap)
984263bc 623{
40393ded
RG
624 struct vnode *vp = ap->a_vp;
625 struct nfsnode *np = VTONFS(vp);
c6b43e93 626 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc 627 int error = 0;
87de5057 628 thread_t td = curthread;
984263bc 629
12cdc371 630 vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
c6b43e93
MD
631 lwkt_gettoken(&nmp->nm_token);
632
984263bc 633 if (vp->v_type == VREG) {
e07fef60 634 if (np->n_flag & NLMODIFIED) {
984263bc
MD
635 if (NFS_ISV3(vp)) {
636 /*
637 * Under NFSv3 we have dirty buffers to dispose of. We
638 * must flush them to the NFS server. We have the option
639 * of waiting all the way through the commit rpc or just
640 * waiting for the initial write. The default is to only
641 * wait through the initial write so the data is in the
642 * server's cache, which is roughly similar to the state
643 * a standard disk subsystem leaves the file in on close().
644 *
5a9187cb 645 * We cannot clear the NLMODIFIED bit in np->n_flag due to
984263bc
MD
646 * potential races with other processes, and certainly
647 * cannot clear it if we don't commit.
648 */
649 int cm = nfsv3_commit_on_close ? 1 : 0;
87de5057 650 error = nfs_flush(vp, MNT_WAIT, td, cm);
5a9187cb 651 /* np->n_flag &= ~NLMODIFIED; */
984263bc 652 } else {
87de5057 653 error = nfs_vinvalbuf(vp, V_SAVE, 1);
984263bc
MD
654 }
655 np->n_attrstamp = 0;
656 }
657 if (np->n_flag & NWRITEERR) {
658 np->n_flag &= ~NWRITEERR;
659 error = np->n_error;
660 }
661 }
8ddc6004 662 vop_stdclose(ap);
c6b43e93
MD
663 lwkt_reltoken(&nmp->nm_token);
664
984263bc
MD
665 return (error);
666}
667
668/*
669 * nfs getattr call from vfs.
e851b29e 670 *
b478fdce 671 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
984263bc
MD
672 */
673static int
e851b29e 674nfs_getattr(struct vop_getattr_args *ap)
984263bc 675{
40393ded
RG
676 struct vnode *vp = ap->a_vp;
677 struct nfsnode *np = VTONFS(vp);
6a9301d9 678 struct nfsmount *nmp;
984263bc 679 int error = 0;
87de5057 680 thread_t td = curthread;
42edf14f
MD
681 struct nfsm_info info;
682
683 info.mrep = NULL;
684 info.v3 = NFS_ISV3(vp);
6a9301d9 685 nmp = VFSTONFS(vp->v_mount);
c6b43e93
MD
686
687 lwkt_gettoken(&nmp->nm_token);
984263bc
MD
688
689 /*
690 * Update local times for special files.
691 */
692 if (np->n_flag & (NACC | NUPD))
693 np->n_flag |= NCHG;
694 /*
695 * First look in the cache.
696 */
697 if (nfs_getattrcache(vp, ap->a_vap) == 0)
6a9301d9 698 goto done;
984263bc 699
42edf14f 700 if (info.v3 && nfsaccess_cache_timeout > 0) {
984263bc 701 nfsstats.accesscache_misses++;
87de5057 702 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
984263bc 703 if (nfs_getattrcache(vp, ap->a_vap) == 0)
6a9301d9 704 goto done;
984263bc
MD
705 }
706
707 nfsstats.rpccnt[NFSPROC_GETATTR]++;
42edf14f
MD
708 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
709 ERROROUT(nfsm_fhtom(&info, vp));
710 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
711 nfs_vpcred(vp, ND_CHECK), &error));
712 if (error == 0) {
713 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
984263bc 714 }
42edf14f
MD
715 m_freem(info.mrep);
716 info.mrep = NULL;
6a9301d9
MD
717done:
718 /*
719 * NFS doesn't support chflags flags. If the nfs mount was
720 * made -o cache set the UF_CACHE bit for swapcache.
721 */
722 if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
723 ap->a_vap->va_flags |= UF_CACHE;
6b08710e 724nfsmout:
c6b43e93 725 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
726 return (error);
727}
728
729/*
730 * nfs setattr call.
e851b29e 731 *
31bd717a 732 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
984263bc
MD
733 */
734static int
e851b29e 735nfs_setattr(struct vop_setattr_args *ap)
984263bc 736{
40393ded
RG
737 struct vnode *vp = ap->a_vp;
738 struct nfsnode *np = VTONFS(vp);
c6b43e93 739 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
40393ded 740 struct vattr *vap = ap->a_vap;
984263bc 741 int error = 0;
05c073d6 742 int kflags = 0;
a63246d1 743 off_t tsize;
87de5057 744 thread_t td = curthread;
984263bc
MD
745
746#ifndef nolint
a63246d1 747 tsize = (off_t)0;
984263bc 748#endif
984263bc
MD
749 /*
750 * Setting of flags is not supported.
751 */
752 if (vap->va_flags != VNOVAL)
753 return (EOPNOTSUPP);
754
755 /*
756 * Disallow write attempts if the filesystem is mounted read-only.
757 */
758 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
759 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
760 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
761 (vp->v_mount->mnt_flag & MNT_RDONLY))
762 return (EROFS);
a482a28a 763
c6b43e93
MD
764 lwkt_gettoken(&nmp->nm_token);
765
984263bc 766 if (vap->va_size != VNOVAL) {
a482a28a
MD
767 /*
768 * truncation requested
769 */
984263bc
MD
770 switch (vp->v_type) {
771 case VDIR:
c6b43e93 772 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
773 return (EISDIR);
774 case VCHR:
775 case VBLK:
776 case VSOCK:
777 case VFIFO:
778 if (vap->va_mtime.tv_sec == VNOVAL &&
779 vap->va_atime.tv_sec == VNOVAL &&
780 vap->va_mode == (mode_t)VNOVAL &&
781 vap->va_uid == (uid_t)VNOVAL &&
c6b43e93
MD
782 vap->va_gid == (gid_t)VNOVAL) {
783 lwkt_reltoken(&nmp->nm_token);
984263bc 784 return (0);
c6b43e93 785 }
984263bc
MD
786 vap->va_size = VNOVAL;
787 break;
788 default:
789 /*
790 * Disallow write attempts if the filesystem is
791 * mounted read-only.
792 */
c6b43e93
MD
793 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
794 lwkt_reltoken(&nmp->nm_token);
984263bc 795 return (EROFS);
c6b43e93 796 }
984263bc 797
984263bc 798 tsize = np->n_size;
a004bca6 799again:
8452310f 800 error = nfs_meta_setsize(vp, td, vap->va_size, 0);
984263bc 801
8452310f 802#if 0
5a9187cb 803 if (np->n_flag & NLMODIFIED) {
984263bc 804 if (vap->va_size == 0)
87de5057 805 error = nfs_vinvalbuf(vp, 0, 1);
984263bc 806 else
87de5057 807 error = nfs_vinvalbuf(vp, V_SAVE, 1);
984263bc 808 }
8452310f 809#endif
a004bca6
MD
810 /*
811 * note: this loop case almost always happens at
812 * least once per truncation.
b07fc55c 813 */
a004bca6
MD
814 if (error == 0 && np->n_size != vap->va_size)
815 goto again;
816 np->n_vattr.va_size = vap->va_size;
05c073d6
MD
817 kflags |= NOTE_WRITE;
818 if (tsize < vap->va_size)
819 kflags |= NOTE_EXTEND;
5a9187cb
MD
820 break;
821 }
a482a28a
MD
822 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
823 /*
824 * What to do. If we are modifying the mtime we lose
825 * mtime detection of changes made by the server or other
826 * clients. But programs like rsync/rdist/cpdup are going
827 * to call utimes a lot. We don't want to piecemeal sync.
828 *
829 * For now sync if any prior remote changes were detected,
830 * but allow us to lose track of remote changes made during
831 * the utimes operation.
832 */
833 if (np->n_flag & NRMODIFIED)
834 error = nfs_vinvalbuf(vp, V_SAVE, 1);
1013402d
SW
835 if (error == EINTR) {
836 lwkt_reltoken(&nmp->nm_token);
a482a28a 837 return (error);
1013402d 838 }
a482a28a
MD
839 if (error == 0) {
840 if (vap->va_mtime.tv_sec != VNOVAL) {
841 np->n_mtime = vap->va_mtime.tv_sec;
842 }
843 }
a004bca6 844 }
87de5057 845 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
05c073d6
MD
846 if (error == 0)
847 kflags |= NOTE_EXTEND;
a004bca6
MD
848
849 /*
850 * Sanity check if a truncation was issued. This should only occur
851 * if multiple processes are racing on the same file.
852 */
853 if (error == 0 && vap->va_size != VNOVAL &&
854 np->n_size != vap->va_size) {
973c11b9 855 kprintf("NFS ftruncate: server disagrees on the file size: "
a63246d1
MD
856 "%jd/%jd/%jd\n",
857 (intmax_t)tsize,
858 (intmax_t)vap->va_size,
859 (intmax_t)np->n_size);
a004bca6
MD
860 goto again;
861 }
984263bc
MD
862 if (error && vap->va_size != VNOVAL) {
863 np->n_size = np->n_vattr.va_size = tsize;
8452310f 864 nfs_meta_setsize(vp, td, np->n_size, 0);
984263bc 865 }
c6b43e93 866 lwkt_reltoken(&nmp->nm_token);
05c073d6 867 nfs_knote(vp, kflags);
c6b43e93 868
984263bc
MD
869 return (error);
870}
871
872/*
873 * Do an nfs setattr rpc.
874 */
875static int
dadab5e9 876nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
e851b29e 877 struct ucred *cred, struct thread *td)
984263bc 878{
40393ded 879 struct nfsv2_sattr *sp;
999914df 880 struct nfsnode *np = VTONFS(vp);
984263bc
MD
881 u_int32_t *tl;
882 int error = 0, wccflag = NFSV3_WCCRATTR;
42edf14f
MD
883 struct nfsm_info info;
884
885 info.mrep = NULL;
886 info.v3 = NFS_ISV3(vp);
984263bc
MD
887
888 nfsstats.rpccnt[NFSPROC_SETATTR]++;
42edf14f
MD
889 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
890 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
891 ERROROUT(nfsm_fhtom(&info, vp));
892 if (info.v3) {
893 nfsm_v3attrbuild(&info, vap, TRUE);
894 tl = nfsm_build(&info, NFSX_UNSIGNED);
984263bc
MD
895 *tl = nfs_false;
896 } else {
42edf14f 897 sp = nfsm_build(&info, NFSX_V2SATTR);
984263bc
MD
898 if (vap->va_mode == (mode_t)VNOVAL)
899 sp->sa_mode = nfs_xdrneg1;
900 else
901 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
902 if (vap->va_uid == (uid_t)VNOVAL)
903 sp->sa_uid = nfs_xdrneg1;
904 else
905 sp->sa_uid = txdr_unsigned(vap->va_uid);
906 if (vap->va_gid == (gid_t)VNOVAL)
907 sp->sa_gid = nfs_xdrneg1;
908 else
909 sp->sa_gid = txdr_unsigned(vap->va_gid);
910 sp->sa_size = txdr_unsigned(vap->va_size);
911 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
912 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
913 }
42edf14f
MD
914 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
915 if (info.v3) {
999914df 916 np->n_modestamp = 0;
42edf14f
MD
917 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
918 } else {
919 ERROROUT(nfsm_loadattr(&info, vp, NULL));
920 }
921 m_freem(info.mrep);
922 info.mrep = NULL;
6b08710e 923nfsmout:
984263bc
MD
924 return (error);
925}
926
a15b4eef
MD
927static
928void
929nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
930{
931 if (nctimeout == 0)
932 nctimeout = 1;
933 else
934 nctimeout *= hz;
935 cache_setvp(nch, vp);
936 cache_settimeout(nch, nctimeout);
937}
938
fad57d0e
MD
939/*
940 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
941 * nfs_lookup() until all remaining new api calls are implemented.
942 *
943 * Resolve a namecache entry. This function is passed a locked ncp and
a15b4eef 944 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
fad57d0e
MD
945 */
946static int
947nfs_nresolve(struct vop_nresolve_args *ap)
948{
949 struct thread *td = curthread;
950 struct namecache *ncp;
c6b43e93 951 struct nfsmount *nmp;
fad57d0e
MD
952 struct nfsnode *np;
953 struct vnode *dvp;
954 struct vnode *nvp;
955 nfsfh_t *fhp;
956 int attrflag;
957 int fhsize;
958 int error;
046b7e33 959 int tmp_error;
fad57d0e 960 int len;
42edf14f 961 struct nfsm_info info;
fad57d0e 962
dff430ab 963 dvp = ap->a_dvp;
c6b43e93 964 nmp = VFSTONFS(dvp->v_mount);
fad57d0e 965
c6b43e93
MD
966 lwkt_gettoken(&nmp->nm_token);
967
968 if ((error = vget(dvp, LK_SHARED)) != 0) {
969 lwkt_reltoken(&nmp->nm_token);
fad57d0e 970 return (error);
c6b43e93 971 }
fad57d0e 972
42edf14f
MD
973 info.mrep = NULL;
974 info.v3 = NFS_ISV3(dvp);
975
fad57d0e 976 nvp = NULL;
fad57d0e
MD
977 nfsstats.lookupcache_misses++;
978 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
dff430ab 979 ncp = ap->a_nch->ncp;
fad57d0e 980 len = ncp->nc_nlen;
42edf14f
MD
981 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
982 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
983 ERROROUT(nfsm_fhtom(&info, dvp));
984 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
985 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
986 ap->a_cred, &error));
fad57d0e
MD
987 if (error) {
988 /*
989 * Cache negatve lookups to reduce NFS traffic, but use
990 * a fast timeout. Otherwise use a timeout of 1 tick.
991 * XXX we should add a namecache flag for no-caching
992 * to uncache the negative hit as soon as possible, but
993 * we cannot simply destroy the entry because it is used
994 * as a placeholder by the caller.
046b7e33
MD
995 *
996 * The refactored nfs code will overwrite a non-zero error
997 * with 0 when we use ERROROUT(), so don't here.
fad57d0e 998 */
a15b4eef
MD
999 if (error == ENOENT)
1000 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
046b7e33
MD
1001 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1002 NFS_LATTR_NOSHRINK);
1003 if (tmp_error) {
1004 error = tmp_error;
1005 goto nfsmout;
1006 }
42edf14f
MD
1007 m_freem(info.mrep);
1008 info.mrep = NULL;
fad57d0e
MD
1009 goto nfsmout;
1010 }
1011
1012 /*
1013 * Success, get the file handle, do various checks, and load
1014 * post-operation data from the reply packet. Theoretically
1015 * we should never be looking up "." so, theoretically, we
1016 * should never get the same file handle as our directory. But
1017 * we check anyway. XXX
1018 *
1019 * Note that no timeout is set for the positive cache hit. We
1020 * assume, theoretically, that ESTALE returns will be dealt with
1021 * properly to handle NFS races and in anycase we cannot depend
1022 * on a timeout to deal with NFS open/create/excl issues so instead
1023 * of a bad hack here the rest of the NFS client code needs to do
1024 * the right thing.
1025 */
42edf14f 1026 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
fad57d0e
MD
1027
1028 np = VTONFS(dvp);
1029 if (NFS_CMPFH(np, fhp, fhsize)) {
1030 vref(dvp);
1031 nvp = dvp;
1032 } else {
c9682b51 1033 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
fad57d0e 1034 if (error) {
42edf14f
MD
1035 m_freem(info.mrep);
1036 info.mrep = NULL;
fad57d0e 1037 vput(dvp);
c6b43e93 1038 lwkt_reltoken(&nmp->nm_token);
fad57d0e
MD
1039 return (error);
1040 }
1041 nvp = NFSTOV(np);
1042 }
42edf14f
MD
1043 if (info.v3) {
1044 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1045 NFS_LATTR_NOSHRINK));
1046 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1047 NFS_LATTR_NOSHRINK));
fad57d0e 1048 } else {
42edf14f 1049 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
fad57d0e 1050 }
a15b4eef 1051 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
42edf14f
MD
1052 m_freem(info.mrep);
1053 info.mrep = NULL;
fad57d0e 1054nfsmout:
c6b43e93 1055 lwkt_reltoken(&nmp->nm_token);
fad57d0e
MD
1056 vput(dvp);
1057 if (nvp) {
1058 if (nvp == dvp)
1059 vrele(nvp);
1060 else
1061 vput(nvp);
1062 }
1063 return (error);
1064}
1065
984263bc 1066/*
4d17b298 1067 * 'cached' nfs directory lookup
e851b29e 1068 *
fad57d0e
MD
1069 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1070 *
31bd717a
MD
1071 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1072 * struct componentname *a_cnp)
984263bc
MD
1073 */
1074static int
e62afb5f 1075nfs_lookup(struct vop_old_lookup_args *ap)
984263bc
MD
1076{
1077 struct componentname *cnp = ap->a_cnp;
1078 struct vnode *dvp = ap->a_dvp;
1079 struct vnode **vpp = ap->a_vpp;
1080 int flags = cnp->cn_flags;
1081 struct vnode *newvp;
c9682b51 1082 struct vnode *notvp;
984263bc 1083 struct nfsmount *nmp;
984263bc
MD
1084 long len;
1085 nfsfh_t *fhp;
1086 struct nfsnode *np;
046b7e33
MD
1087 int lockparent, wantparent, attrflag, fhsize;
1088 int error;
1089 int tmp_error;
42edf14f
MD
1090 struct nfsm_info info;
1091
1092 info.mrep = NULL;
1093 info.v3 = NFS_ISV3(dvp);
046b7e33 1094 error = 0;
984263bc 1095
c9682b51
MD
1096 notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1097
4d17b298
MD
1098 /*
1099 * Read-only mount check and directory check.
1100 */
984263bc 1101 *vpp = NULLVP;
fad57d0e 1102 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
2b69e610 1103 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
984263bc 1104 return (EROFS);
4d17b298 1105
984263bc
MD
1106 if (dvp->v_type != VDIR)
1107 return (ENOTDIR);
4d17b298
MD
1108
1109 /*
1110 * Look it up in the cache. Note that ENOENT is only returned if we
1111 * previously entered a negative hit (see later on). The additional
1112 * nfsneg_cache_timeout check causes previously cached results to
1113 * be instantly ignored if the negative caching is turned off.
1114 */
2b69e610
MD
1115 lockparent = flags & CNP_LOCKPARENT;
1116 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
984263bc
MD
1117 nmp = VFSTONFS(dvp->v_mount);
1118 np = VTONFS(dvp);
984263bc 1119
c6b43e93
MD
1120 lwkt_gettoken(&nmp->nm_token);
1121
4d17b298 1122 /*
fad57d0e 1123 * Go to the wire.
4d17b298 1124 */
984263bc
MD
1125 error = 0;
1126 newvp = NULLVP;
1127 nfsstats.lookupcache_misses++;
1128 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1129 len = cnp->cn_namelen;
42edf14f
MD
1130 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1131 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1132 ERROROUT(nfsm_fhtom(&info, dvp));
1133 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1134 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1135 cnp->cn_cred, &error));
984263bc 1136 if (error) {
046b7e33
MD
1137 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1138 NFS_LATTR_NOSHRINK);
1139 if (tmp_error) {
1140 error = tmp_error;
1141 goto nfsmout;
1142 }
1143
42edf14f
MD
1144 m_freem(info.mrep);
1145 info.mrep = NULL;
984263bc
MD
1146 goto nfsmout;
1147 }
42edf14f 1148 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
984263bc
MD
1149
1150 /*
1151 * Handle RENAME case...
1152 */
fad57d0e 1153 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
984263bc 1154 if (NFS_CMPFH(np, fhp, fhsize)) {
42edf14f
MD
1155 m_freem(info.mrep);
1156 info.mrep = NULL;
c6b43e93 1157 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1158 return (EISDIR);
1159 }
c9682b51 1160 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
984263bc 1161 if (error) {
42edf14f
MD
1162 m_freem(info.mrep);
1163 info.mrep = NULL;
c6b43e93 1164 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1165 return (error);
1166 }
1167 newvp = NFSTOV(np);
42edf14f
MD
1168 if (info.v3) {
1169 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1170 NFS_LATTR_NOSHRINK));
1171 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1172 NFS_LATTR_NOSHRINK));
1173 } else {
1174 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1175 }
984263bc 1176 *vpp = newvp;
42edf14f
MD
1177 m_freem(info.mrep);
1178 info.mrep = NULL;
7ab77df6 1179 if (!lockparent) {
a11aaa81 1180 vn_unlock(dvp);
7ab77df6
MD
1181 cnp->cn_flags |= CNP_PDIRUNLOCK;
1182 }
c6b43e93 1183 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1184 return (0);
1185 }
1186
2b69e610 1187 if (flags & CNP_ISDOTDOT) {
a11aaa81 1188 vn_unlock(dvp);
7ab77df6 1189 cnp->cn_flags |= CNP_PDIRUNLOCK;
c9682b51 1190 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
984263bc 1191 if (error) {
ca466bae 1192 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
7ab77df6 1193 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
c6b43e93 1194 lwkt_reltoken(&nmp->nm_token);
7ab77df6 1195 return (error); /* NOTE: return error from nget */
984263bc
MD
1196 }
1197 newvp = NFSTOV(np);
fad57d0e 1198 if (lockparent) {
b458d1ab 1199 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
7ab77df6
MD
1200 if (error) {
1201 vput(newvp);
c6b43e93 1202 lwkt_reltoken(&nmp->nm_token);
7ab77df6
MD
1203 return (error);
1204 }
1205 cnp->cn_flags |= CNP_PDIRUNLOCK;
984263bc
MD
1206 }
1207 } else if (NFS_CMPFH(np, fhp, fhsize)) {
597aea93 1208 vref(dvp);
984263bc
MD
1209 newvp = dvp;
1210 } else {
c9682b51 1211 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
984263bc 1212 if (error) {
42edf14f
MD
1213 m_freem(info.mrep);
1214 info.mrep = NULL;
c6b43e93 1215 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1216 return (error);
1217 }
fad57d0e 1218 if (!lockparent) {
a11aaa81 1219 vn_unlock(dvp);
7ab77df6
MD
1220 cnp->cn_flags |= CNP_PDIRUNLOCK;
1221 }
984263bc
MD
1222 newvp = NFSTOV(np);
1223 }
42edf14f
MD
1224 if (info.v3) {
1225 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1226 NFS_LATTR_NOSHRINK));
1227 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1228 NFS_LATTR_NOSHRINK));
1229 } else {
1230 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1231 }
fad57d0e
MD
1232#if 0
1233 /* XXX MOVE TO nfs_nremove() */
2b69e610 1234 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
fad57d0e
MD
1235 cnp->cn_nameiop != NAMEI_DELETE) {
1236 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
984263bc 1237 }
fad57d0e 1238#endif
984263bc 1239 *vpp = newvp;
42edf14f
MD
1240 m_freem(info.mrep);
1241 info.mrep = NULL;
6b08710e 1242nfsmout:
984263bc
MD
1243 if (error) {
1244 if (newvp != NULLVP) {
1245 vrele(newvp);
1246 *vpp = NULLVP;
1247 }
fad57d0e
MD
1248 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1249 cnp->cn_nameiop == NAMEI_RENAME) &&
1250 error == ENOENT) {
7ab77df6 1251 if (!lockparent) {
a11aaa81 1252 vn_unlock(dvp);
7ab77df6
MD
1253 cnp->cn_flags |= CNP_PDIRUNLOCK;
1254 }
984263bc
MD
1255 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1256 error = EROFS;
1257 else
1258 error = EJUSTRETURN;
1259 }
984263bc 1260 }
c6b43e93 1261 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1262 return (error);
1263}
1264
1265/*
1266 * nfs read call.
1267 * Just call nfs_bioread() to do the work.
e851b29e
CP
1268 *
1269 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1270 * struct ucred *a_cred)
984263bc
MD
1271 */
1272static int
e851b29e 1273nfs_read(struct vop_read_args *ap)
984263bc 1274{
40393ded 1275 struct vnode *vp = ap->a_vp;
c6b43e93
MD
1276 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1277 int error;
1278
1279 lwkt_gettoken(&nmp->nm_token);
1280 error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1281 lwkt_reltoken(&nmp->nm_token);
984263bc 1282
c6b43e93 1283 return error;
984263bc
MD
1284}
1285
1286/*
1287 * nfs readlink call
e851b29e
CP
1288 *
1289 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
984263bc
MD
1290 */
1291static int
e851b29e 1292nfs_readlink(struct vop_readlink_args *ap)
984263bc 1293{
40393ded 1294 struct vnode *vp = ap->a_vp;
c6b43e93
MD
1295 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1296 int error;
984263bc
MD
1297
1298 if (vp->v_type != VLNK)
1299 return (EINVAL);
c6b43e93
MD
1300
1301 lwkt_gettoken(&nmp->nm_token);
1302 error = nfs_bioread(vp, ap->a_uio, 0);
1303 lwkt_reltoken(&nmp->nm_token);
1304
1305 return error;
984263bc
MD
1306}
1307
1308/*
1309 * Do a readlink rpc.
1310 * Called by nfs_doio() from below the buffer cache.
1311 */
1312int
cc7d050e 1313nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
984263bc 1314{
984263bc 1315 int error = 0, len, attrflag;
42edf14f
MD
1316 struct nfsm_info info;
1317
1318 info.mrep = NULL;
1319 info.v3 = NFS_ISV3(vp);
984263bc
MD
1320
1321 nfsstats.rpccnt[NFSPROC_READLINK]++;
42edf14f
MD
1322 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1323 ERROROUT(nfsm_fhtom(&info, vp));
1324 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1325 nfs_vpcred(vp, ND_CHECK), &error));
1326 if (info.v3) {
1327 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1328 NFS_LATTR_NOSHRINK));
1329 }
984263bc 1330 if (!error) {
42edf14f 1331 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
984263bc
MD
1332 if (len == NFS_MAXPATHLEN) {
1333 struct nfsnode *np = VTONFS(vp);
1334 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1335 len = np->n_size;
1336 }
42edf14f 1337 ERROROUT(nfsm_mtouio(&info, uiop, len));
984263bc 1338 }
42edf14f
MD
1339 m_freem(info.mrep);
1340 info.mrep = NULL;
6b08710e 1341nfsmout:
984263bc
MD
1342 return (error);
1343}
1344
1345/*
28953d39 1346 * nfs synchronous read rpc using UIO
984263bc
MD
1347 */
1348int
edb90c22 1349nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
984263bc 1350{
40393ded 1351 u_int32_t *tl;
984263bc
MD
1352 struct nfsmount *nmp;
1353 int error = 0, len, retlen, tsiz, eof, attrflag;
42edf14f 1354 struct nfsm_info info;
28953d39 1355 off_t tmp_off;
42edf14f
MD
1356
1357 info.mrep = NULL;
1358 info.v3 = NFS_ISV3(vp);
984263bc
MD
1359
1360#ifndef nolint
1361 eof = 0;
1362#endif
1363 nmp = VFSTONFS(vp->v_mount);
c6b43e93 1364
984263bc 1365 tsiz = uiop->uio_resid;
28953d39
MD
1366 tmp_off = uiop->uio_offset + tsiz;
1367 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
984263bc 1368 return (EFBIG);
28953d39 1369 tmp_off = uiop->uio_offset;
984263bc
MD
1370 while (tsiz > 0) {
1371 nfsstats.rpccnt[NFSPROC_READ]++;
1372 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
42edf14f
MD
1373 nfsm_reqhead(&info, vp, NFSPROC_READ,
1374 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1375 ERROROUT(nfsm_fhtom(&info, vp));
1376 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1377 if (info.v3) {
984263bc
MD
1378 txdr_hyper(uiop->uio_offset, tl);
1379 *(tl + 2) = txdr_unsigned(len);
1380 } else {
1381 *tl++ = txdr_unsigned(uiop->uio_offset);
1382 *tl++ = txdr_unsigned(len);
1383 *tl = 0;
1384 }
42edf14f
MD
1385 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1386 nfs_vpcred(vp, ND_READ), &error));
1387 if (info.v3) {
1388 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1389 NFS_LATTR_NOSHRINK));
1390 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
984263bc 1391 eof = fxdr_unsigned(int, *(tl + 1));
42edf14f
MD
1392 } else {
1393 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1394 }
28953d39 1395 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
42edf14f
MD
1396 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1397 m_freem(info.mrep);
1398 info.mrep = NULL;
28953d39
MD
1399
1400 /*
1401 * Handle short-read from server (NFSv3). If EOF is not
1402 * flagged (and no error occurred), but retlen is less
1403 * then the request size, we must zero-fill the remainder.
1404 */
1405 if (retlen < len && info.v3 && eof == 0) {
1406 ERROROUT(uiomovez(len - retlen, uiop));
1407 retlen = len;
1408 }
984263bc 1409 tsiz -= retlen;
28953d39
MD
1410
1411 /*
1412 * Terminate loop on EOF or zero-length read.
1413 *
1414 * For NFSv2 a short-read indicates EOF, not zero-fill,
1415 * and also terminates the loop.
1416 */
42edf14f 1417 if (info.v3) {
28953d39 1418 if (eof || retlen == 0)
984263bc 1419 tsiz = 0;
984263bc
MD
1420 } else if (retlen < len) {
1421 tsiz = 0;
1422 }
1423 }
1424nfsmout:
1425 return (error);
1426}
1427
1428/*
1429 * nfs write call
1430 */
1431int
cc7d050e
MD
1432nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1433 int *iomode, int *must_commit)
984263bc 1434{
40393ded 1435 u_int32_t *tl;
42edf14f 1436 int32_t backup;
984263bc
MD
1437 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1438 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
42edf14f
MD
1439 int committed = NFSV3WRITE_FILESYNC;
1440 struct nfsm_info info;
1441
1442 info.mrep = NULL;
1443 info.v3 = NFS_ISV3(vp);
984263bc
MD
1444
1445#ifndef DIAGNOSTIC
1446 if (uiop->uio_iovcnt != 1)
1447 panic("nfs: writerpc iovcnt > 1");
1448#endif
1449 *must_commit = 0;
1450 tsiz = uiop->uio_resid;
1451 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1452 return (EFBIG);
1453 while (tsiz > 0) {
1454 nfsstats.rpccnt[NFSPROC_WRITE]++;
1455 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
42edf14f 1456 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
5e6f1ca5
MD
1457 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1458 nfsm_rndup(len));
42edf14f
MD
1459 ERROROUT(nfsm_fhtom(&info, vp));
1460 if (info.v3) {
1461 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
984263bc
MD
1462 txdr_hyper(uiop->uio_offset, tl);
1463 tl += 2;
1464 *tl++ = txdr_unsigned(len);
1465 *tl++ = txdr_unsigned(*iomode);
1466 *tl = txdr_unsigned(len);
1467 } else {
40393ded 1468 u_int32_t x;
984263bc 1469
42edf14f 1470 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
984263bc
MD
1471 /* Set both "begin" and "current" to non-garbage. */
1472 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1473 *tl++ = x; /* "begin offset" */
1474 *tl++ = x; /* "current offset" */
1475 x = txdr_unsigned(len);
1476 *tl++ = x; /* total to this offset */
1477 *tl = x; /* size of this write */
1478 }
42edf14f
MD
1479 ERROROUT(nfsm_uiotom(&info, uiop, len));
1480 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1481 nfs_vpcred(vp, ND_WRITE), &error));
1482 if (info.v3) {
5a9187cb
MD
1483 /*
1484 * The write RPC returns a before and after mtime. The
1485 * nfsm_wcc_data() macro checks the before n_mtime
1486 * against the before time and stores the after time
1487 * in the nfsnode's cached vattr and n_mtime field.
1488 * The NRMODIFIED bit will be set if the before
1489 * time did not match the original mtime.
1490 */
984263bc 1491 wccflag = NFSV3_WCCCHK;
42edf14f
MD
1492 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1493 if (error == 0) {
1494 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
984263bc
MD
1495 rlen = fxdr_unsigned(int, *tl++);
1496 if (rlen == 0) {
1497 error = NFSERR_IO;
42edf14f
MD
1498 m_freem(info.mrep);
1499 info.mrep = NULL;
984263bc
MD
1500 break;
1501 } else if (rlen < len) {
1502 backup = len - rlen;
656849c6 1503 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
984263bc
MD
1504 uiop->uio_iov->iov_len += backup;
1505 uiop->uio_offset -= backup;
1506 uiop->uio_resid += backup;
1507 len = rlen;
1508 }
1509 commit = fxdr_unsigned(int, *tl++);
1510
1511 /*
1512 * Return the lowest committment level
1513 * obtained by any of the RPCs.
1514 */
1515 if (committed == NFSV3WRITE_FILESYNC)
1516 committed = commit;
1517 else if (committed == NFSV3WRITE_DATASYNC &&
1518 commit == NFSV3WRITE_UNSTABLE)
1519 committed = commit;
1520 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1521 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1522 NFSX_V3WRITEVERF);
1523 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1524 } else if (bcmp((caddr_t)tl,
1525 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1526 *must_commit = 1;
1527 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1528 NFSX_V3WRITEVERF);
1529 }
1530 }
5a9187cb 1531 } else {
42edf14f 1532 ERROROUT(nfsm_loadattr(&info, vp, NULL));
5a9187cb 1533 }
42edf14f
MD
1534 m_freem(info.mrep);
1535 info.mrep = NULL;
984263bc
MD
1536 if (error)
1537 break;
1538 tsiz -= len;
1539 }
1540nfsmout:
1541 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1542 committed = NFSV3WRITE_FILESYNC;
1543 *iomode = committed;
1544 if (error)
1545 uiop->uio_resid = tsiz;
1546 return (error);
1547}
1548
1549/*
1550 * nfs mknod rpc
1551 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1552 * mode set to specify the file type and the size field for rdev.
1553 */
1554static int
e851b29e
CP
1555nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1556 struct vattr *vap)
984263bc 1557{
40393ded
RG
1558 struct nfsv2_sattr *sp;
1559 u_int32_t *tl;
60233e58
SW
1560 struct vnode *newvp = NULL;
1561 struct nfsnode *np = NULL;
984263bc 1562 struct vattr vattr;
984263bc 1563 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
0e9b9130 1564 int rmajor, rminor;
42edf14f
MD
1565 struct nfsm_info info;
1566
1567 info.mrep = NULL;
1568 info.v3 = NFS_ISV3(dvp);
984263bc 1569
0e9b9130
MD
1570 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1571 rmajor = txdr_unsigned(vap->va_rmajor);
1572 rminor = txdr_unsigned(vap->va_rminor);
1573 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1574 rmajor = nfs_xdrneg1;
1575 rminor = nfs_xdrneg1;
1576 } else {
984263bc
MD
1577 return (EOPNOTSUPP);
1578 }
87de5057 1579 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
984263bc
MD
1580 return (error);
1581 }
1582 nfsstats.rpccnt[NFSPROC_MKNOD]++;
42edf14f
MD
1583 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1584 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1585 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1586 ERROROUT(nfsm_fhtom(&info, dvp));
1587 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1588 NFS_MAXNAMLEN));
1589 if (info.v3) {
1590 tl = nfsm_build(&info, NFSX_UNSIGNED);
984263bc 1591 *tl++ = vtonfsv3_type(vap->va_type);
42edf14f 1592 nfsm_v3attrbuild(&info, vap, FALSE);
984263bc 1593 if (vap->va_type == VCHR || vap->va_type == VBLK) {
42edf14f 1594 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
0e9b9130
MD
1595 *tl++ = txdr_unsigned(vap->va_rmajor);
1596 *tl = txdr_unsigned(vap->va_rminor);
984263bc
MD
1597 }
1598 } else {
42edf14f 1599 sp = nfsm_build(&info, NFSX_V2SATTR);
984263bc
MD
1600 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1601 sp->sa_uid = nfs_xdrneg1;
1602 sp->sa_gid = nfs_xdrneg1;
0e9b9130 1603 sp->sa_size = makeudev(rmajor, rminor);
984263bc
MD
1604 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1605 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1606 }
42edf14f
MD
1607 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1608 cnp->cn_cred, &error));
984263bc 1609 if (!error) {
42edf14f 1610 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
984263bc
MD
1611 if (!gotvp) {
1612 if (newvp) {
1613 vput(newvp);
60233e58 1614 newvp = NULL;
984263bc
MD
1615 }
1616 error = nfs_lookitup(dvp, cnp->cn_nameptr,
dadab5e9 1617 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1618 if (!error)
1619 newvp = NFSTOV(np);
1620 }
1621 }
42edf14f
MD
1622 if (info.v3) {
1623 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1624 }
1625 m_freem(info.mrep);
1626 info.mrep = NULL;
6b08710e 1627nfsmout:
984263bc
MD
1628 if (error) {
1629 if (newvp)
1630 vput(newvp);
1631 } else {
984263bc
MD
1632 *vpp = newvp;
1633 }
5a9187cb 1634 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
1635 if (!wccflag)
1636 VTONFS(dvp)->n_attrstamp = 0;
1637 return (error);
1638}
1639
1640/*
1641 * nfs mknod vop
1642 * just call nfs_mknodrpc() to do the work.
e851b29e
CP
1643 *
1644 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1645 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1646 */
1647/* ARGSUSED */
1648static int
e62afb5f 1649nfs_mknod(struct vop_old_mknod_args *ap)
984263bc 1650{
c6b43e93
MD
1651 struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1652 int error;
1653
1654 lwkt_gettoken(&nmp->nm_token);
1655 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1656 lwkt_reltoken(&nmp->nm_token);
05c073d6
MD
1657 if (error == 0)
1658 nfs_knote(ap->a_dvp, NOTE_WRITE);
c6b43e93
MD
1659
1660 return error;
984263bc
MD
1661}
1662
1663static u_long create_verf;
1664/*
1665 * nfs file create call
e851b29e
CP
1666 *
1667 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1668 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1669 */
1670static int
e62afb5f 1671nfs_create(struct vop_old_create_args *ap)
984263bc 1672{
40393ded
RG
1673 struct vnode *dvp = ap->a_dvp;
1674 struct vattr *vap = ap->a_vap;
c6b43e93 1675 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
40393ded
RG
1676 struct componentname *cnp = ap->a_cnp;
1677 struct nfsv2_sattr *sp;
1678 u_int32_t *tl;
60233e58
SW
1679 struct nfsnode *np = NULL;
1680 struct vnode *newvp = NULL;
984263bc 1681 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
984263bc 1682 struct vattr vattr;
42edf14f
MD
1683 struct nfsm_info info;
1684
1685 info.mrep = NULL;
1686 info.v3 = NFS_ISV3(dvp);
c6b43e93 1687 lwkt_gettoken(&nmp->nm_token);
984263bc
MD
1688
1689 /*
1690 * Oops, not for me..
1691 */
c6b43e93
MD
1692 if (vap->va_type == VSOCK) {
1693 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1694 lwkt_reltoken(&nmp->nm_token);
1695 return error;
1696 }
984263bc 1697
87de5057 1698 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
c6b43e93 1699 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1700 return (error);
1701 }
1702 if (vap->va_vaflags & VA_EXCLUSIVE)
1703 fmode |= O_EXCL;
1704again:
1705 nfsstats.rpccnt[NFSPROC_CREATE]++;
42edf14f
MD
1706 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1707 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1708 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1709 ERROROUT(nfsm_fhtom(&info, dvp));
1710 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1711 NFS_MAXNAMLEN));
1712 if (info.v3) {
1713 tl = nfsm_build(&info, NFSX_UNSIGNED);
984263bc
MD
1714 if (fmode & O_EXCL) {
1715 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
42edf14f 1716 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
984263bc 1717#ifdef INET
1b562c24
SZ
1718 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1719 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
984263bc
MD
1720 else
1721#endif
1722 *tl++ = create_verf;
1723 *tl = ++create_verf;
1724 } else {
1725 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
42edf14f 1726 nfsm_v3attrbuild(&info, vap, FALSE);
984263bc
MD
1727 }
1728 } else {
42edf14f 1729 sp = nfsm_build(&info, NFSX_V2SATTR);
984263bc
MD
1730 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1731 sp->sa_uid = nfs_xdrneg1;
1732 sp->sa_gid = nfs_xdrneg1;
1733 sp->sa_size = 0;
1734 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1735 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1736 }
42edf14f
MD
1737 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1738 cnp->cn_cred, &error));
1739 if (error == 0) {
1740 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
984263bc
MD
1741 if (!gotvp) {
1742 if (newvp) {
1743 vput(newvp);
60233e58 1744 newvp = NULL;
984263bc
MD
1745 }
1746 error = nfs_lookitup(dvp, cnp->cn_nameptr,
dadab5e9 1747 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1748 if (!error)
1749 newvp = NFSTOV(np);
1750 }
1751 }
42edf14f 1752 if (info.v3) {
d64fd6d3
MD
1753 if (error == 0)
1754 error = nfsm_wcc_data(&info, dvp, &wccflag);
1755 else
1756 (void)nfsm_wcc_data(&info, dvp, &wccflag);
42edf14f
MD
1757 }
1758 m_freem(info.mrep);
1759 info.mrep = NULL;
6b08710e 1760nfsmout:
984263bc 1761 if (error) {
42edf14f 1762 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
c5c4cc5d 1763 KKASSERT(newvp == NULL);
984263bc
MD
1764 fmode &= ~O_EXCL;
1765 goto again;
1766 }
42edf14f 1767 } else if (info.v3 && (fmode & O_EXCL)) {
984263bc
MD
1768 /*
1769 * We are normally called with only a partially initialized
1770 * VAP. Since the NFSv3 spec says that server may use the
1771 * file attributes to store the verifier, the spec requires
1772 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1773 * in atime, but we can't really assume that all servers will
1774 * so we ensure that our SETATTR sets both atime and mtime.
1775 */
1776 if (vap->va_mtime.tv_sec == VNOVAL)
1777 vfs_timestamp(&vap->va_mtime);
1778 if (vap->va_atime.tv_sec == VNOVAL)
1779 vap->va_atime = vap->va_mtime;
dadab5e9 1780 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
984263bc 1781 }
c5c4cc5d 1782 if (error == 0) {
c1cf1e59
MD
1783 /*
1784 * The new np may have enough info for access
1785 * checks, make sure rucred and wucred are
1786 * initialized for read and write rpc's.
1787 */
1788 np = VTONFS(newvp);
1789 if (np->n_rucred == NULL)
1790 np->n_rucred = crhold(cnp->cn_cred);
1791 if (np->n_wucred == NULL)
1792 np->n_wucred = crhold(cnp->cn_cred);
984263bc 1793 *ap->a_vpp = newvp;
05c073d6 1794 nfs_knote(dvp, NOTE_WRITE);
c5c4cc5d
MD
1795 } else if (newvp) {
1796 vput(newvp);
984263bc 1797 }
5a9187cb 1798 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
1799 if (!wccflag)
1800 VTONFS(dvp)->n_attrstamp = 0;
c6b43e93 1801 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
1802 return (error);
1803}
1804
1805/*
1806 * nfs file remove call
1807 * To try and make nfs semantics closer to ufs semantics, a file that has
1808 * other processes using the vnode is renamed instead of removed and then
1809 * removed later on the last close.
ee173d09 1810 * - If v_refcnt > 1
984263bc
MD
1811 * If a rename is not already in the works
1812 * call nfs_sillyrename() to set it up
1813 * else
1814 * do the remove rpc
e851b29e 1815 *
31bd717a
MD
1816 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1817 * struct componentname *a_cnp)
984263bc
MD
1818 */
1819static int
e62afb5f 1820nfs_remove(struct vop_old_remove_args *ap)
984263bc 1821{
40393ded
RG
1822 struct vnode *vp = ap->a_vp;
1823 struct vnode *dvp = ap->a_dvp;
c6b43e93 1824 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
40393ded
RG
1825 struct componentname *cnp = ap->a_cnp;
1826 struct nfsnode *np = VTONFS(vp);
984263bc
MD
1827 int error = 0;
1828 struct vattr vattr;
1829
c6b43e93 1830 lwkt_gettoken(&nmp->nm_token);
984263bc 1831#ifndef DIAGNOSTIC
ee173d09
SW
1832 if (VREFCNT(vp) < 1)
1833 panic("nfs_remove: bad v_refcnt");
984263bc 1834#endif
c6b43e93 1835 if (vp->v_type == VDIR) {
984263bc 1836 error = EPERM;
ee173d09 1837 } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
c6b43e93 1838 VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
984263bc 1839 /*
7adf09fa
MD
1840 * Force finalization so the VOP_INACTIVE() call is not delayed.
1841 * This prevents cred structures from building up in nfsnodes
1842 * for deleted files.
1843 */
1844 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1845 np->n_flag |= NREMOVED;
1846
1847 /*
1848 * Throw away biocache buffers, mainly to avoid
984263bc
MD
1849 * unnecessary delayed writes later.
1850 */
87de5057 1851 error = nfs_vinvalbuf(vp, 0, 1);
984263bc 1852 /* Do the rpc */
7adf09fa 1853 if (error != EINTR) {
984263bc 1854 error = nfs_removerpc(dvp, cnp->cn_nameptr,
7adf09fa
MD
1855 cnp->cn_namelen,
1856 cnp->cn_cred, cnp->cn_td);
1857 }
1858
984263bc
MD
1859 /*
1860 * Kludge City: If the first reply to the remove rpc is lost..
1861 * the reply to the retransmitted request will be ENOENT
1862 * since the file was in fact removed
1863 * Therefore, we cheat and return success.
1864 */
1865 if (error == ENOENT)
1866 error = 0;
fad57d0e 1867 } else if (!np->n_sillyrename) {
984263bc 1868 error = nfs_sillyrename(dvp, vp, cnp);
fad57d0e 1869 }
984263bc 1870 np->n_attrstamp = 0;
c6b43e93 1871 lwkt_reltoken(&nmp->nm_token);
05c073d6
MD
1872 if (error == 0) {
1873 nfs_knote(vp, NOTE_DELETE);
1874 nfs_knote(dvp, NOTE_WRITE);
1875 }
c6b43e93 1876
984263bc
MD
1877 return (error);
1878}
1879
1880/*
1881 * nfs file remove rpc called from nfs_inactive
966299f6
MD
1882 *
1883 * NOTE: s_dvp can be VBAD during a forced unmount.
984263bc
MD
1884 */
1885int
dadab5e9 1886nfs_removeit(struct sillyrename *sp)
984263bc 1887{
966299f6
MD
1888 if (sp->s_dvp->v_type == VBAD)
1889 return(0);
dadab5e9
MD
1890 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1891 sp->s_cred, NULL));
984263bc
MD
1892}
1893
1894/*
1895 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1896 */
1897static int
e851b29e
CP
1898nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1899 struct ucred *cred, struct thread *td)
984263bc 1900{
984263bc 1901 int error = 0, wccflag = NFSV3_WCCRATTR;
42edf14f
MD
1902 struct nfsm_info info;
1903
1904 info.mrep = NULL;
1905 info.v3 = NFS_ISV3(dvp);
984263bc
MD
1906
1907 nfsstats.rpccnt[NFSPROC_REMOVE]++;
42edf14f
MD
1908 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1909 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1910 ERROROUT(nfsm_fhtom(&info, dvp));
1911 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1912 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1913 if (info.v3) {
1914 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1915 }
1916 m_freem(info.mrep);
1917 info.mrep = NULL;
6b08710e 1918nfsmout:
5a9187cb 1919 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
1920 if (!wccflag)
1921 VTONFS(dvp)->n_attrstamp = 0;
1922 return (error);
1923}
1924
1925/*
1926 * nfs file rename call
e851b29e
CP
1927 *
1928 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1929 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1930 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
1931 */
1932static int
e62afb5f 1933nfs_rename(struct vop_old_rename_args *ap)
984263bc 1934{
40393ded
RG
1935 struct vnode *fvp = ap->a_fvp;
1936 struct vnode *tvp = ap->a_tvp;
1937 struct vnode *fdvp = ap->a_fdvp;
1938 struct vnode *tdvp = ap->a_tdvp;
1939 struct componentname *tcnp = ap->a_tcnp;
1940 struct componentname *fcnp = ap->a_fcnp;
c6b43e93 1941 struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
984263bc
MD
1942 int error;
1943
c6b43e93
MD
1944 lwkt_gettoken(&nmp->nm_token);
1945
7adf09fa
MD
1946 /*
1947 * Force finalization so the VOP_INACTIVE() call is not delayed.
1948 * This prevents cred structures from building up in nfsnodes
1949 * for deleted files.
1950 */
1951 if (tvp) {
1952 atomic_set_int(&tvp->v_refcnt, VREF_FINALIZE);
1953 if (VTONFS(tvp))
1954 VTONFS(tvp)->n_flag |= NREMOVED;
1955 }
1956
984263bc
MD
1957 /* Check for cross-device rename */
1958 if ((fvp->v_mount != tdvp->v_mount) ||
1959 (tvp && (fvp->v_mount != tvp->v_mount))) {
1960 error = EXDEV;
1961 goto out;
1962 }
1963
1964 /*
bb7bf7b2
MD
1965 * We shouldn't have to flush fvp on rename for most server-side
1966 * filesystems as the file handle should not change. Unfortunately
1967 * the inode for some filesystems (msdosfs) might be tied to the
1968 * file name or directory position so to be completely safe
1969 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1970 * performance.
a482a28a
MD
1971 *
1972 * We must flush tvp on rename because it might become stale on the
1973 * server after the rename.
984263bc 1974 */
a482a28a 1975 if (nfs_flush_on_rename)
52174f71 1976 VOP_FSYNC(fvp, MNT_WAIT, 0);
984263bc 1977 if (tvp)
52174f71 1978 VOP_FSYNC(tvp, MNT_WAIT, 0);
984263bc
MD
1979
1980 /*
1981 * If the tvp exists and is in use, sillyrename it before doing the
1982 * rename of the new file over it.
fad57d0e 1983 *
984263bc 1984 * XXX Can't sillyrename a directory.
5fd012e0 1985 *
fad57d0e
MD
1986 * We do not attempt to do any namecache purges in this old API
1987 * routine. The new API compat functions have access to the actual
1988 * namecache structures and will do it for us.
984263bc 1989 */
ee173d09 1990 if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
984263bc 1991 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
05c073d6 1992 nfs_knote(tvp, NOTE_DELETE);
984263bc
MD
1993 vput(tvp);
1994 tvp = NULL;
5fd012e0 1995 } else if (tvp) {
05c073d6 1996 nfs_knote(tvp, NOTE_DELETE);
984263bc
MD
1997 }
1998
1999 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2000 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
dadab5e9 2001 tcnp->cn_td);
984263bc 2002
984263bc 2003out:
05c073d6
MD
2004 if (error == 0) {
2005 nfs_knote(fdvp, NOTE_WRITE);
2006 nfs_knote(tdvp, NOTE_WRITE);
2007 nfs_knote(fvp, NOTE_RENAME);
2008 }
c6b43e93 2009 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
2010 if (tdvp == tvp)
2011 vrele(tdvp);
2012 else
2013 vput(tdvp);
2014 if (tvp)
2015 vput(tvp);
2016 vrele(fdvp);
2017 vrele(fvp);
2018 /*
2019 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2020 */
2021 if (error == ENOENT)
2022 error = 0;
2023 return (error);
2024}
2025
2026/*
2027 * nfs file rename rpc called from nfs_remove() above
2028 */
2029static int
e851b29e
CP
2030nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2031 struct sillyrename *sp)
984263bc
MD
2032{
2033 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
dadab5e9 2034 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
984263bc
MD
2035}
2036
2037/*
2038 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2039 */
2040static int
e851b29e
CP
2041nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2042 struct vnode *tdvp, const char *tnameptr, int tnamelen,
2043 struct ucred *cred, struct thread *td)
984263bc 2044{
984263bc 2045 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
42edf14f
MD
2046 struct nfsm_info info;
2047
2048 info.mrep = NULL;
2049 info.v3 = NFS_ISV3(fdvp);
984263bc
MD
2050
2051 nfsstats.rpccnt[NFSPROC_RENAME]++;
42edf14f
MD
2052 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2053 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2054 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2055 ERROROUT(nfsm_fhtom(&info, fdvp));
2056 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2057 ERROROUT(nfsm_fhtom(&info, tdvp));
2058 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2059 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2060 if (info.v3) {
2061 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2062 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2063 }
2064 m_freem(info.mrep);
2065 info.mrep = NULL;
6b08710e 2066nfsmout:
5a9187cb
MD
2067 VTONFS(fdvp)->n_flag |= NLMODIFIED;
2068 VTONFS(tdvp)->n_flag |= NLMODIFIED;
984263bc
MD
2069 if (!fwccflag)
2070 VTONFS(fdvp)->n_attrstamp = 0;
2071 if (!twccflag)
2072 VTONFS(tdvp)->n_attrstamp = 0;
2073 return (error);
2074}
2075
2076/*
2077 * nfs hard link create call
e851b29e
CP
2078 *
2079 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2080 * struct componentname *a_cnp)
984263bc
MD
2081 */
2082static int
e62afb5f 2083nfs_link(struct vop_old_link_args *ap)
984263bc 2084{
40393ded
RG
2085 struct vnode *vp = ap->a_vp;
2086 struct vnode *tdvp = ap->a_tdvp;
c6b43e93 2087 struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
40393ded 2088 struct componentname *cnp = ap->a_cnp;
984263bc 2089 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
42edf14f 2090 struct nfsm_info info;
984263bc
MD
2091
2092 if (vp->v_mount != tdvp->v_mount) {
2093 return (EXDEV);
2094 }
c6b43e93 2095 lwkt_gettoken(&nmp->nm_token);
984263bc
MD
2096
2097 /*
bb7bf7b2
MD
2098 * The attribute cache may get out of sync with the server on link.
2099 * Pushing writes to the server before handle was inherited from
2100 * long long ago and it is unclear if we still need to do this.
2101 * Defaults to off.
984263bc 2102 */
bb7bf7b2 2103 if (nfs_flush_on_hlink)
52174f71 2104 VOP_FSYNC(vp, MNT_WAIT, 0);
984263bc 2105
42edf14f
MD
2106 info.mrep = NULL;
2107 info.v3 = NFS_ISV3(vp);
2108
984263bc 2109 nfsstats.rpccnt[NFSPROC_LINK]++;
42edf14f
MD
2110 nfsm_reqhead(&info, vp, NFSPROC_LINK,
2111 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2112 nfsm_rndup(cnp->cn_namelen));
2113 ERROROUT(nfsm_fhtom(&info, vp));
2114 ERROROUT(nfsm_fhtom(&info, tdvp));
2115 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2116 NFS_MAXNAMLEN));
2117 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2118 cnp->cn_cred, &error));
2119 if (info.v3) {
2120 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2121 NFS_LATTR_NOSHRINK));
2122 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2123 }
2124 m_freem(info.mrep);
2125 info.mrep = NULL;
6b08710e 2126nfsmout:
5a9187cb 2127 VTONFS(tdvp)->n_flag |= NLMODIFIED;
984263bc
MD
2128 if (!attrflag)
2129 VTONFS(vp)->n_attrstamp = 0;
2130 if (!wccflag)
2131 VTONFS(tdvp)->n_attrstamp = 0;
2132 /*
2133 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2134 */
2135 if (error == EEXIST)
2136 error = 0;
c6b43e93 2137 lwkt_reltoken(&nmp->nm_token);
05c073d6
MD
2138 if (error == 0) {
2139 nfs_knote(vp, NOTE_LINK);
2140 nfs_knote(tdvp, NOTE_WRITE);
2141 }
2142
984263bc
MD
2143 return (error);
2144}
2145
2146/*
2147 * nfs symbolic link create call
e851b29e
CP
2148 *
2149 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2150 * struct componentname *a_cnp, struct vattr *a_vap,
2151 * char *a_target)
984263bc
MD
2152 */
2153static int
e62afb5f 2154nfs_symlink(struct vop_old_symlink_args *ap)
984263bc 2155{
40393ded
RG
2156 struct vnode *dvp = ap->a_dvp;
2157 struct vattr *vap = ap->a_vap;
c6b43e93 2158 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
40393ded
RG
2159 struct componentname *cnp = ap->a_cnp;
2160 struct nfsv2_sattr *sp;
984263bc 2161 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
60233e58 2162 struct vnode *newvp = NULL;
42edf14f
MD
2163 struct nfsm_info info;
2164
2165 info.mrep = NULL;
2166 info.v3 = NFS_ISV3(dvp);
c6b43e93 2167 lwkt_gettoken(&nmp->nm_token);
984263bc
MD
2168
2169 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2170 slen = strlen(ap->a_target);
42edf14f
MD
2171 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2172 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2173 nfsm_rndup(cnp->cn_namelen) +
2174 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2175 ERROROUT(nfsm_fhtom(&info, dvp));
2176 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2177 NFS_MAXNAMLEN));
2178 if (info.v3) {
2179 nfsm_v3attrbuild(&info, vap, FALSE);
2180 }
2181 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2182 if (info.v3 == 0) {
2183 sp = nfsm_build(&info, NFSX_V2SATTR);
984263bc
MD
2184 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2185 sp->sa_uid = nfs_xdrneg1;
2186 sp->sa_gid = nfs_xdrneg1;
2187 sp->sa_size = nfs_xdrneg1;
2188 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2189 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2190 }
2191
2192 /*
2193 * Issue the NFS request and get the rpc response.
2194 *
2195 * Only NFSv3 responses returning an error of 0 actually return
2196 * a file handle that can be converted into newvp without having
2197 * to do an extra lookup rpc.
2198 */
42edf14f
MD
2199 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2200 cnp->cn_cred, &error));
2201 if (info.v3) {
2202 if (error == 0) {
2203 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2204 }
2205 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
984263bc
MD
2206 }
2207
2208 /*
2209 * out code jumps -> here, mrep is also freed.
2210 */
2211
42edf14f
MD
2212 m_freem(info.mrep);
2213 info.mrep = NULL;
6b08710e 2214nfsmout:
984263bc
MD
2215
2216 /*
2217 * If we get an EEXIST error, silently convert it to no-error
2218 * in case of an NFS retry.
2219 */
2220 if (error == EEXIST)
2221 error = 0;
2222
2223 /*
2224 * If we do not have (or no longer have) an error, and we could
2225 * not extract the newvp from the response due to the request being
2226 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2227 * to obtain a newvp to return.
2228 */
2229 if (error == 0 && newvp == NULL) {
2230 struct nfsnode *np = NULL;
2231
2232 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
c6b43e93 2233 cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
2234 if (!error)
2235 newvp = NFSTOV(np);
2236 }
2237 if (error) {
2238 if (newvp)
2239 vput(newvp);
2240 } else {
2241 *ap->a_vpp = newvp;
2242 }
5a9187cb 2243 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
2244 if (!wccflag)
2245 VTONFS(dvp)->n_attrstamp = 0;
05c073d6
MD
2246 if (error == 0 && *ap->a_vpp)
2247 nfs_knote(*ap->a_vpp, NOTE_WRITE);
c6b43e93
MD
2248 lwkt_reltoken(&nmp->nm_token);
2249
984263bc
MD
2250 return (error);
2251}
2252
2253/*
2254 * nfs make dir call
e851b29e
CP
2255 *
2256 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2257 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
2258 */
2259static int
e62afb5f 2260nfs_mkdir(struct vop_old_mkdir_args *ap)
984263bc 2261{
40393ded
RG
2262 struct vnode *dvp = ap->a_dvp;
2263 struct vattr *vap = ap->a_vap;
c6b43e93 2264 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
40393ded
RG
2265 struct componentname *cnp = ap->a_cnp;
2266 struct nfsv2_sattr *sp;
60233e58
SW
2267 struct nfsnode *np = NULL;
2268 struct vnode *newvp = NULL;
42edf14f 2269 struct vattr vattr;
984263bc
MD
2270 int error = 0, wccflag = NFSV3_WCCRATTR;
2271 int gotvp = 0;
42edf14f
MD
2272 int len;
2273 struct nfsm_info info;
2274
2275 info.mrep = NULL;
2276 info.v3 = NFS_ISV3(dvp);
c6b43e93 2277 lwkt_gettoken(&nmp->nm_token);
984263bc 2278
87de5057 2279 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
c6b43e93 2280 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
2281 return (error);
2282 }
2283 len = cnp->cn_namelen;
2284 nfsstats.rpccnt[NFSPROC_MKDIR]++;
42edf14f
MD
2285 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2286 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2287 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2288 ERROROUT(nfsm_fhtom(&info, dvp));
2289 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2290 if (info.v3) {
2291 nfsm_v3attrbuild(&info, vap, FALSE);
984263bc 2292 } else {
42edf14f 2293 sp = nfsm_build(&info, NFSX_V2SATTR);
984263bc
MD
2294 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2295 sp->sa_uid = nfs_xdrneg1;
2296 sp->sa_gid = nfs_xdrneg1;
2297 sp->sa_size = nfs_xdrneg1;
2298 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2299 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2300 }
42edf14f
MD
2301 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2302 cnp->cn_cred, &error));
2303 if (error == 0) {
2304 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2305 }
2306 if (info.v3) {
2307 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2308 }
2309 m_freem(info.mrep);
2310 info.mrep = NULL;
6b08710e 2311nfsmout:
5a9187cb 2312 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
2313 if (!wccflag)
2314 VTONFS(dvp)->n_attrstamp = 0;
2315 /*
2316 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2317 * if we can succeed in looking up the directory.
2318 */
2319 if (error == EEXIST || (!error && !gotvp)) {
2320 if (newvp) {
2321 vrele(newvp);
60233e58 2322 newvp = NULL;
984263bc
MD
2323 }
2324 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
dadab5e9 2325 cnp->cn_td, &np);
984263bc
MD
2326 if (!error) {
2327 newvp = NFSTOV(np);
2328 if (newvp->v_type != VDIR)
2329 error = EEXIST;
2330 }
2331 }
2332 if (error) {
2333 if (newvp)
2334 vrele(newvp);
c6b43e93 2335 } else {
05c073d6 2336 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
984263bc 2337 *ap->a_vpp = newvp;
c6b43e93
MD
2338 }
2339 lwkt_reltoken(&nmp->nm_token);
984263bc
MD
2340 return (error);
2341}
2342
2343/*
2344 * nfs remove directory call
e851b29e
CP
2345 *
2346 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2347 * struct componentname *a_cnp)
984263bc
MD
2348 */
2349static int
e62afb5f 2350nfs_rmdir(struct vop_old_rmdir_args *ap)
984263bc 2351{
40393ded
RG
2352 struct vnode *vp = ap->a_vp;
2353 struct vnode *dvp = ap->a_dvp;
c6b43e93 2354 struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
40393ded 2355 struct componentname *cnp = ap->a_cnp;
984263bc 2356 int error = 0, wccflag = NFSV3_WCCRATTR;
42edf14f
MD
2357 struct nfsm_info info;
2358
2359 info.mrep = NULL;
2360 info.v3 = NFS_ISV3(dvp);
984263bc
MD
2361
2362 if (dvp == vp)
2363 return (EINVAL);
c6b43e93
MD
2364
2365 lwkt_gettoken(&nmp->nm_token);
2366
984263bc 2367 nfsstats.rpccnt[NFSPROC_RMDIR]++;
42edf14f
MD
2368 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2369 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2370 nfsm_rndup(cnp->cn_namelen));
2371 ERROROUT(nfsm_fhtom(&info, dvp));
2372 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2373 NFS_MAXNAMLEN));
2374 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2375 cnp->cn_cred, &error));
2376 if (info.v3) {
2377 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2378 }
2379 m_freem(info.mrep);
2380 info.mrep = NULL;
6b08710e 2381nfsmout:
5a9187cb 2382 VTONFS(dvp)->n_flag |= NLMODIFIED;
984263bc
MD
2383 if (!wccflag)
2384 VTONFS(dvp)->n_attrstamp = 0;
984263bc
MD
2385 /*
2386 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2387 */
2388 if (error == ENOENT)
2389 error = 0;
05c073d6
MD
2390 else
2391 nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
c6b43e93
MD
2392 lwkt_reltoken(&nmp->nm_token);
2393
984263bc
MD
2394 return (error);
2395}
2396
2397/*
2398 * nfs readdir call
e851b29e
CP
2399 *
2400 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
984263bc
MD
2401 */
2402static int
e851b29e 2403nfs_readdir(struct vop_readdir_args *ap)
984263bc 2404{
40393ded
RG
2405 struct vnode *vp = ap->a_vp;
2406 struct nfsnode *np = VTONFS(vp);
c6b43e93 2407 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
40393ded 2408 struct uio *uio = ap->a_uio;
984263bc
MD
2409 int tresid, error;
2410 struct vattr vattr;
2411
2412 if (vp->v_type != VDIR)
2413 return (EPERM);
5a9187cb 2414
b458d1ab
MD
2415 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2416 if (error)
885ecb13
MD
2417 return (error);
2418
c6b43e93
MD
2419 lwkt_gettoken(&nmp->nm_token);
2420
984263bc 2421 /*
5a9187cb
MD
2422 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2423 * and then check that is still valid, or if this is an NQNFS mount
2424 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2425 * VOP_GETATTR() does not necessarily go to the wire.
984263bc
MD
2426 */
2427 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
5a9187cb 2428 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
87de5057 2429 if (VOP_GETATTR(vp, &vattr) == 0 &&
e07fef60 2430 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
5a9187cb 2431 ) {
984263bc 2432 nfsstats.direofcache_hits++;
885ecb13 2433 goto done;
984263bc
MD
2434 }
2435 }
2436
2437 /*
5a9187cb
MD
2438 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2439 * own cache coherency checks so we do not have to.
984263bc
MD
2440 */
2441 tresid = uio->uio_resid;
3b568787 2442 error = nfs_bioread(vp, uio, 0);
984263bc
MD
2443
2444 if (!error && uio->uio_resid == tresid)
2445 nfsstats.direofcache_misses++;
885ecb13 2446done:
c6b43e93 2447 lwkt_reltoken(&nmp->nm_token);
885ecb13 2448 vn_unlock(vp);
c6b43e93 2449
984263bc
MD
2450 return (error);
2451}
2452
2453/*
7d877edf
MD
2454 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2455 *
2456 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2457 * offset/block and converts the nfs formatted directory entries for userland
2458 * consumption as well as deals with offsets into the middle of blocks.
2459 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2460 * be block-bounded. It must convert to cookies for the actual RPC.
984263bc
MD
2461 */
2462int
cc7d050e 2463nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
984263bc 2464{
40393ded 2465 int len, left;
01f31ab3 2466 struct nfs_dirent *dp = NULL;
40393ded 2467 u_int32_t *tl;
40393ded 2468 nfsuint64 *cookiep;
42edf14f 2469 caddr_t cp;
984263bc
MD
2470 nfsuint64 cookie;
2471 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2472 struct nfsnode *dnp = VTONFS(vp);
2473 u_quad_t fileno;
2474 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2475 int attrflag;
42edf14f
MD
2476 struct nfsm_info info;
2477
2478 info.mrep = NULL;
2479 info.v3 = NFS_ISV3(vp);
984263bc
MD
2480
2481#ifndef DIAGNOSTIC
2482 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2483 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2484 panic("nfs readdirrpc bad uio");
2485#endif
2486
2487 /*
2488 * If there is no cookie, assume directory was stale.
2489 */
2490 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2491 if (cookiep)
2492 cookie = *cookiep;
2493 else
2494 return (NFSERR_BAD_COOKIE);
2495 /*
2496 * Loop around doing readdir rpc's of size nm_readdirsize
2497 * truncated to a multiple of DIRBLKSIZ.
2498 * The stopping criteria is EOF or buffer full.
2499 */
2500 while (more_dirs && bigenough) {
2501 nfsstats.rpccnt[NFSPROC_READDIR]++;
42edf14f
MD
2502 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2503 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2504 ERROROUT(nfsm_fhtom(&info, vp));
2505 if (info.v3) {
2506 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
984263bc
MD
2507 *tl++ = cookie.nfsuquad[0];
2508 *tl++ = cookie.nfsuquad[1];
2509 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2510 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2511 } else {
5e6f1ca5
MD
2512 /*
2513 * WARNING! HAMMER DIRECTORIES WILL NOT WORK WELL
2514 * WITH NFSv2!!! There's nothing I can really do
2515 * about it other than to hope the server supports
2516 * rdirplus w/NFSv2.
2517 */
42edf14f 2518 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
984263bc
MD
2519 *tl++ = cookie.nfsuquad[0];
2520 }
2521 *tl = txdr_unsigned(nmp->nm_readdirsize);
42edf14f
MD
2522 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2523 uiop->uio_td,
2524 nfs_vpcred(vp, ND_READ), &error));
2525 if (info.v3) {
2526 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2527 NFS_LATTR_NOSHRINK));
2528 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2529 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2530 dnp->n_cookieverf.nfsuquad[1] = *tl;
984263bc 2531 }
42edf14f 2532 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
2533 more_dirs = fxdr_unsigned(int, *tl);
2534
7d877edf 2535 /* loop thru the dir entries, converting them to std form */
984263bc 2536 while (more_dirs && bigenough) {
42edf14f
MD
2537 if (info.v3) {
2538 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2539 fileno = fxdr_hyper(tl);
2540 len = fxdr_unsigned(int, *(tl + 2));
2541 } else {
42edf14f 2542 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
984263bc
MD
2543 fileno = fxdr_unsigned(u_quad_t, *tl++);
2544 len = fxdr_unsigned(int, *tl);
2545 }
2546 if (len <= 0 || len > NFS_MAXNAMLEN) {
2547 error = EBADRPC;
42edf14f
MD
2548 m_freem(info.mrep);
2549 info.mrep = NULL;
984263bc
MD
2550 goto nfsmout;
2551 }
7d877edf
MD
2552
2553 /*
2554 * len is the number of bytes in the path element
2555 * name, not including the \0 termination.
2556 *
2557 * tlen is the number of bytes w have to reserve for
2558 * the path element name.
2559 */
984263bc
MD
2560 tlen = nfsm_rndup(len);
2561 if (tlen == len)
2562 tlen += 4; /* To ensure null termination */
7d877edf
MD
2563
2564 /*
2565 * If the entry would cross a DIRBLKSIZ boundary,
2566 * extend the previous nfs_dirent to cover the
2567 * remaining space.
2568 */
984263bc 2569 left = DIRBLKSIZ - blksiz;
01f31ab3
JS
2570 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2571 dp->nfs_reclen += left;
656849c6 2572 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
984263bc
MD
2573 uiop->uio_iov->iov_len -= left;
2574 uiop->uio_offset += left;
2575 uiop->uio_resid -= left;
2576 blksiz = 0;
2577 }
01f31ab3 2578 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
984263bc
MD
2579 bigenough = 0;
2580 if (bigenough) {
01f31ab3
JS
2581 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2582 dp->nfs_ino = fileno;
2583 dp->nfs_namlen = len;
2584 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2585 dp->nfs_type = DT_UNKNOWN;
2586 blksiz += dp->nfs_reclen;
984263bc
MD
2587 if (blksiz == DIRBLKSIZ)
2588 blksiz = 0;
01f31ab3
JS
2589 uiop->uio_offset += sizeof(struct nfs_dirent);
2590 uiop->uio_resid -= sizeof(struct nfs_dirent);
656849c6 2591 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
01f31ab3 2592 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
42edf14f 2593 ERROROUT(nfsm_mtouio(&info, uiop, len));
7d877edf
MD
2594
2595 /*
2596 * The uiop has advanced by nfs_dirent + len
2597 * but really needs to advance by
2598 * nfs_dirent + tlen
2599 */
984263bc
MD
2600 cp = uiop->uio_iov->iov_base;
2601 tlen -= len;
2602 *cp = '\0'; /* null terminate */
656849c6 2603 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
984263bc
MD
2604 uiop->uio_iov->iov_len -= tlen;
2605 uiop->uio_offset += tlen;
2606 uiop->uio_resid -= tlen;
7d877edf
MD
2607 } else {
2608 /*
2609 * NFS strings must be rounded up (nfsm_myouio
2610 * handled that in the bigenough case).
2611 */
42edf14f 2612 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
7d877edf 2613 }
42edf14f
MD
2614 if (info.v3) {
2615 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc 2616 } else {
42edf14f 2617 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
984263bc 2618 }
7d877edf
MD
2619
2620 /*
2621 * If we were able to accomodate the last entry,
2622 * get the cookie for the next one. Otherwise
2623 * hold-over the cookie for the one we were not
2624 * able to accomodate.
2625 */
984263bc
MD
2626 if (bigenough) {
2627 cookie.nfsuquad[0] = *tl++;
42edf14f 2628 if (info.v3)
984263bc 2629 cookie.nfsuquad[1] = *tl++;
42edf14f 2630 } else if (info.v3) {
984263bc 2631 tl += 2;
7d877edf 2632 } else {
984263bc 2633 tl++;
7d877edf 2634 }
984263bc
MD
2635 more_dirs = fxdr_unsigned(int, *tl);
2636 }
2637 /*
2638 * If at end of rpc data, get the eof boolean
2639 */
2640 if (!more_dirs) {
42edf14f 2641 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
2642 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2643 }
42edf14f
MD
2644 m_freem(info.mrep);
2645 info.mrep = NULL;
984263bc
MD
2646 }
2647 /*
2648 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2649 * by increasing d_reclen for the last record.
2650 */
2651 if (blksiz > 0) {
2652 left = DIRBLKSIZ - blksiz;
01f31ab3 2653 dp->nfs_reclen += left;
656849c6 2654 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
984263bc
MD
2655 uiop->uio_iov->iov_len -= left;
2656 uiop->uio_offset += left;
2657 uiop->uio_resid -= left;
2658 }
2659
7d877edf
MD
2660 if (bigenough) {
2661 /*
2662 * We hit the end of the directory, update direofoffset.
2663 */
984263bc 2664 dnp->n_direofoffset = uiop->uio_offset;
7d877edf
MD
2665 } else {
2666 /*
2667 * There is more to go, insert the link cookie so the
2668 * next block can be read.
2669 */
984263bc 2670 if (uiop->uio_resid > 0)
086c1d7e 2671 kprintf("EEK! readdirrpc resid > 0\n");
984263bc
MD
2672 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2673 *cookiep = cookie;
2674 }
2675nfsmout:
2676 return (error);
2677}
2678
2679/*
2680 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2681 */
2682int
cc7d050e 2683nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
984263bc 2684{
40393ded 2685 int len, left;
01f31ab3 2686 struct nfs_dirent *dp;
40393ded 2687 u_int32_t *tl;
40393ded
RG
2688 struct vnode *newvp;
2689 nfsuint64 *cookiep;
42edf14f
MD
2690 caddr_t dpossav1, dpossav2;
2691 caddr_t cp;
2692 struct mbuf *mdsav1, *mdsav2;
984263bc
MD
2693 nfsuint64 cookie;
2694 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2695 struct nfsnode *dnp = VTONFS(vp), *np;
2696 nfsfh_t *fhp;
2697 u_quad_t fileno;
2698 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2699 int attrflag, fhsize;
28623bf9
MD
2700 struct nchandle nch;
2701 struct nchandle dnch;
fad57d0e 2702 struct nlcomponent nlc;
42edf14f
MD
2703 struct nfsm_info info;
2704
2705 info.mrep = NULL;
2706 info.v3 = 1;
984263bc
MD
2707
2708#ifndef nolint
01f31ab3 2709 dp = NULL;
984263bc
MD
2710#endif
2711#ifndef DIAGNOSTIC
2712 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2713 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2714 panic("nfs readdirplusrpc bad uio");
2715#endif
fad57d0e
MD
2716 /*
2717 * Obtain the namecache record for the directory so we have something
2718 * to use as a basis for creating the entries. This function will
2719 * return a held (but not locked) ncp. The ncp may be disconnected
2720 * from the tree and cannot be used for upward traversals, and the
2721 * ncp may be unnamed. Note that other unrelated operations may
2722 * cause the ncp to be named at any time.
94f2e6f2
MD
2723 *
2724 * We have to lock the ncp to prevent a lock order reversal when
2725 * rdirplus does nlookups of the children, because the vnode is
2726 * locked and has to stay that way.
fad57d0e 2727 */
28623bf9 2728 cache_fromdvp(vp, NULL, 0, &dnch);
fad57d0e 2729 bzero(&nlc, sizeof(nlc));
984263bc
MD
2730 newvp = NULLVP;
2731
2732 /*
2733 * If there is no cookie, assume directory was stale.
2734 */
2735 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
af5fde9c 2736 if (cookiep) {
984263bc 2737 cookie = *cookiep;
af5fde9c
MD
2738 } else {
2739 if (dnch.ncp)
2740 cache_drop(&dnch);
984263bc 2741 return (NFSERR_BAD_COOKIE);
af5fde9c
MD
2742 }
2743
984263bc
MD
2744 /*
2745 * Loop around doing readdir rpc's of size nm_readdirsize
2746 * truncated to a multiple of DIRBLKSIZ.
2747 * The stopping criteria is EOF or buffer full.
2748 */
2749 while (more_dirs && bigenough) {
2750 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
42edf14f 2751 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
226341c1 2752 NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
42edf14f
MD
2753 ERROROUT(nfsm_fhtom(&info, vp));
2754 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
984263bc
MD
2755 *tl++ = cookie.nfsuquad[0];
2756 *tl++ = cookie.nfsuquad[1];
2757 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2758 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2759 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2760 *tl = txdr_unsigned(nmp->nm_rsize);
42edf14f
MD
2761 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2762 uiop->uio_td,
2763 nfs_vpcred(vp, ND_READ), &error));
2764 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2765 NFS_LATTR_NOSHRINK));
2766 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2767 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2768 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2769 more_dirs = fxdr_unsigned(int, *tl);
2770
2771 /* loop thru the dir entries, doctoring them to 4bsd form */
2772 while (more_dirs && bigenough) {
42edf14f 2773 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2774 fileno = fxdr_hyper(tl);
2775 len = fxdr_unsigned(int, *(tl + 2));
2776 if (len <= 0 || len > NFS_MAXNAMLEN) {
2777 error = EBADRPC;
42edf14f
MD
2778 m_freem(info.mrep);
2779 info.mrep = NULL;
984263bc
MD
2780 goto nfsmout;
2781 }
2782 tlen = nfsm_rndup(len);
2783 if (tlen == len)
2784 tlen += 4; /* To ensure null termination*/
2785 left = DIRBLKSIZ - blksiz;
01f31ab3
JS
2786 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2787 dp->nfs_reclen += left;
656849c6 2788 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
984263bc
MD
2789 uiop->uio_iov->iov_len -= left;
2790 uiop->uio_offset += left;
2791 uiop->uio_resid -= left;
2792 blksiz = 0;
2793 }
01f31ab3 2794 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
984263bc
MD
2795 bigenough = 0;
2796 if (bigenough) {
01f31ab3
JS
2797 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2798 dp->nfs_ino = fileno;
2799 dp->nfs_namlen = len;
2800 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2801 dp->nfs_type = DT_UNKNOWN;
2802 blksiz += dp->nfs_reclen;
984263bc
MD
2803 if (blksiz == DIRBLKSIZ)
2804 blksiz = 0;
01f31ab3
JS
2805 uiop->uio_offset += sizeof(struct nfs_dirent);
2806 uiop->uio_resid -= sizeof(struct nfs_dirent);
656849c6 2807 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
01f31ab3 2808 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
fad57d0e
MD
2809 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2810 nlc.nlc_namelen = len;
42edf14f 2811 ERROROUT(nfsm_mtouio(&info, uiop, len));
984263bc
MD
2812 cp = uiop->uio_iov->iov_base;
2813 tlen -= len;
2814 *cp = '\0';
656849c6 2815 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
984263bc
MD
2816 uiop->uio_iov->iov_len -= tlen;
2817 uiop->uio_offset += tlen;
2818 uiop->uio_resid -= tlen;
42edf14f
MD
2819 } else {
2820 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2821 }
2822 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
2823 if (bigenough) {
2824 cookie.nfsuquad[0] = *tl++;
2825 cookie.nfsuquad[1] = *tl++;
af5fde9c 2826 } else {
984263bc 2827 tl += 2;
af5fde9c 2828 }
984263bc
MD
2829
2830 /*
2831 * Since the attributes are before the file handle
2832 * (sigh), we must skip over the attributes and then
2833 * come back and get them.
2834 */
2835 attrflag = fxdr_unsigned(int, *tl);
2836 if (attrflag) {
42edf14f
MD
2837 dpossav1 = info.dpos;
2838 mdsav1 = info.md;
2839 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2840 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
2841 doit = fxdr_unsigned(int, *tl);
2842 if (doit) {
42edf14f 2843 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
984263bc 2844 }
af5fde9c
MD
2845 if (doit && bigenough && !nlcdegenerate(&nlc) &&
2846 !NFS_CMPFH(dnp, fhp, fhsize)
2847 ) {
28623bf9 2848 if (dnch.ncp) {
269a08e4 2849#if 0
086c1d7e 2850 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
fad57d0e
MD
2851 nlc.nlc_namelen, nlc.nlc_namelen,
2852 nlc.nlc_nameptr);
269a08e4 2853#endif
94f2e6f2
MD
2854 /*
2855 * This is a bit hokey but there isn't
2856 * much we can do about it. We can't
2857 * hold the directory vp locked while
2858 * doing lookups and gets.
2859 */
668b43c5
MD
2860 nch = cache_nlookup_nonblock(&dnch, &nlc);
2861 if (nch.ncp == NULL)
2862 goto rdfail;
28623bf9 2863 cache_setunresolved(&nch);
668b43c5 2864 error = nfs_nget_nonblock(vp->v_mount, fhp,
c9682b51
MD
2865 fhsize, &np,
2866 NULL);
668b43c5
MD
2867 if (error) {
2868 cache_put(&nch);
2869 goto rdfail;
2595d6be 2870 }
668b43c5
MD
2871 newvp = NFSTOV(np);
2872 dpossav2 = info.dpos;
2873 info.dpos = dpossav1;
2874 mdsav2 = info.md;
2875 info.md = mdsav1;
2876 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2877 info.dpos = dpossav2;
2878 info.md = mdsav2;
2879 dp->nfs_type =
2880 IFTODT(VTTOIF(np->n_vattr.va_type));
2881 nfs_cache_setvp(&nch, newvp,
2882 nfspos_cache_timeout);
2883 vput(newvp);
2884 newvp = NULLVP;
af5fde9c 2885 cache_put(&nch);
fad57d0e 2886 } else {
668b43c5 2887rdfail:
09bd4fbc
MD
2888 ;
2889#if 0
269a08e4
MD
2890 kprintf("Warning: NFS/rddirplus, "
2891 "UNABLE TO ENTER %*.*s\n",
fad57d0e
MD
2892 nlc.nlc_namelen, nlc.nlc_namelen,
2893 nlc.nlc_nameptr);
09bd4fbc 2894#endif
fad57d0e 2895 }
984263bc
MD
2896 }
2897 } else {
2898 /* Just skip over the file handle */
42edf14f 2899 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc 2900 i = fxdr_unsigned(int, *tl);
42edf14f 2901 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
984263bc 2902 }
42edf14f 2903 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
2904 more_dirs = fxdr_unsigned(int, *tl);
2905 }
2906 /*
2907 * If at end of rpc data, get the eof boolean
2908 */
2909 if (!more_dirs) {
42edf14f 2910 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
984263bc
MD
2911 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2912 }
42edf14f
MD
2913 m_freem(info.mrep);
2914 info.mrep = NULL;
984263bc
MD
2915 }
2916 /*
2917 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2918 * by increasing d_reclen for the last record.
2919 */
2920 if (blksiz > 0) {
2921 left = DIRBLKSIZ - blksiz;
01f31ab3 2922 dp->nfs_reclen += left;
656849c6 2923 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
984263bc
MD
2924 uiop->uio_iov->iov_len -= left;
2925 uiop->uio_offset += left;
2926 uiop->uio_resid -= left;
2927 }
2928
2929 /*
2930 * We are now either at the end of the directory or have filled the
2931 * block.
2932 */
af5fde9c 2933 if (bigenough) {
984263bc 2934 dnp->n_direofoffset = uiop->uio_offset;
af5fde9c 2935 } else {
984263bc 2936 if (uiop->uio_resid > 0)
086c1d7e 2937 kprintf("EEK! readdirplusrpc resid > 0\n");
984263bc
MD
2938 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2939 *cookiep = cookie;
2940 }
2941nfsmout:
2942 if (newvp != NULLVP) {
2943 if (newvp == vp)
2944 vrele(newvp);
2945 else
2946 vput(newvp);
2947 newvp = NULLVP;
2948 }
28623bf9
MD
2949 if (dnch.ncp)
2950 cache_drop(&dnch);
984263bc
MD
2951 return (error);
2952}
2953
2954/*
2955 * Silly rename. To make the NFS filesystem that is stateless look a little
2956 * more like the "ufs" a remove of an active vnode is translated to a rename
2957 * to a funny looking filename that is removed by nfs_inactive on the
2958 * nfsnode. There is the potential for another process on a different client
2959 * to create the same funny name between the nfs_lookitup() fails and the
2960 * nfs_rename() completes, but...
2961 */
2962static int
e851b29e 2963nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
984263bc 2964{
40393ded 2965 struct sillyrename *sp;
984263bc
MD
2966 struct nfsnode *np;
2967 int error;
984263bc 2968
85e11dad
MD
2969 /*
2970 * Force finalization so the VOP_INACTIVE() call is not delayed.
7adf09fa
MD
2971 * This prevents cred structures from building up in nfsnodes
2972 * for deleted files.
85e11dad
MD
2973 */
2974 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
7adf09fa
MD
2975 np = VTONFS(vp);
2976 np->n_flag |= NREMOVED;
85e11dad 2977
8c361dda
MD
2978 /*
2979 * We previously purged dvp instead of vp. I don't know why, it
2980 * completely destroys performance. We can't do it anyway with the
2981 * new VFS API since we would be breaking the namecache topology.
2982 */
fad57d0e 2983 cache_purge(vp); /* XXX */
984263bc
MD
2984#ifndef DIAGNOSTIC
2985 if (vp->v_type == VDIR)
2986 panic("nfs: sillyrename dir");
2987#endif
884717e1 2988 sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
984263bc
MD
2989 sp->s_cred = crdup(cnp->cn_cred);
2990 sp->s_dvp = dvp;
597aea93 2991 vref(dvp);
984263bc
MD
2992
2993 /* Fudge together a funny name */
973c11b9
MD
2994 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2995 (int)(intptr_t)cnp->cn_td);
984263bc
MD
2996
2997 /* Try lookitups until we get one that isn't there */
2998 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
60233e58 2999 cnp->cn_td, NULL) == 0) {
984263bc
MD
3000 sp->s_name[4]++;
3001 if (sp->s_name[4] > 'z') {
3002 error = EINVAL;
3003 goto bad;
3004 }
3005 }
3006 error = nfs_renameit(dvp, cnp, sp);
3007 if (error)
3008 goto bad;
3009 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
dadab5e9 3010 cnp->cn_td, &np);
984263bc
MD
3011 np->n_sillyrename = sp;
3012 return (0);
3013bad:
3014 vrele(sp->s_dvp);
3015 crfree(sp->s_cred);
efda3bd0 3016 kfree((caddr_t)sp, M_NFSREQ);
7adf09fa 3017
984263bc
MD
3018 return (error);
3019}
3020
3021/*
3022 * Look up a file name and optionally either update the file handle or
3023 * allocate an nfsnode, depending on the value of npp.
3024 * npp == NULL --> just do the lookup
3025 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3026 * handled too
3027 * *npp != NULL --> update the file handle in the vnode
3028 */
3029static int
e851b29e
CP
3030nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
3031 struct thread *td, struct nfsnode **npp)
984263bc 3032{
60233e58 3033 struct vnode *newvp = NULL;
984263bc 3034 struct nfsnode *np, *dnp = VTONFS(dvp);
984263bc 3035 int error = 0, fhlen, attrflag;
984263bc 3036 nfsfh_t *nfhp;
42edf14f
MD
3037 struct nfsm_info info;
3038
3039 info.mrep = NULL;
3040 info.v3 = NFS_ISV3(dvp);
984263bc
MD
3041
3042 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
42edf14f
MD
3043 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3044 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3045 ERROROUT(nfsm_fhtom(&info, dvp));
3046 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3047 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
984263bc 3048 if (npp && !error) {
42edf14f 3049 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
984263bc
MD
3050 if (*npp) {
3051 np = *npp;
3052 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
efda3bd0 3053 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
984263bc
MD
3054 np->n_fhp = &np->n_fh;
3055 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
efda3bd0 3056 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
984263bc
MD
3057 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3058 np->n_fhsize = fhlen;
3059 newvp = NFSTOV(np);
3060 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
597aea93 3061 vref(dvp);
984263bc
MD
3062 newvp = dvp;
3063 } else {
c9682b51 3064 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
984263bc 3065 if (error) {
42edf14f
MD
3066 m_freem(info.mrep);
3067 info.mrep = NULL;
984263bc
MD
3068 return (error);
3069 }
3070 newvp = NFSTOV(np);
3071 }
42edf14f
MD
3072 if (info.v3) {
3073 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3074 NFS_LATTR_NOSHRINK));
984263bc 3075 if (!attrflag && *npp == NULL) {
42edf14f
MD
3076 m_freem(info.mrep);
3077 info.mrep = NULL;
984263bc
MD
3078 if (newvp == dvp)
3079 vrele(newvp);
3080 else
3081 vput(newvp);
3082 return (ENOENT);
3083 }
42edf14f 3084 } else {
fc6058ba 3085 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
42edf14f 3086 }
984263bc 3087 }
42edf14f
MD
3088 m_freem(info.mrep);
3089 info.mrep = NULL;
6b08710e 3090nfsmout:
984263bc
MD
3091 if (npp && *npp == NULL) {
3092 if (error) {
3093 if (newvp) {
3094 if (newvp == dvp)
3095 vrele(newvp);
3096 else
3097 vput(newvp);
3098 }
3099 } else
3100 *npp = np;
3101 }
3102 return (error);
3103}
3104
3105/*
3106 * Nfs Version 3 commit rpc
cc7d050e
MD
3107 *
3108 * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3109 * involved.
984263bc
MD
3110 */
3111int
cc7d050e 3112nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
984263bc 3113{
40393ded 3114 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc 3115 int error = 0, wccflag = NFSV3_WCCRATTR;
42edf14f
MD
3116 struct nfsm_info info;
3117 u_int32_t *tl;
3118
3119 info.mrep = NULL;
3120 info.v3 = 1;
984263bc
MD
3121
3122 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3123 return (0);
3124 nfsstats.rpccnt[NFSPROC_COMMIT]++;
42edf14f
MD
3125 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3126 ERROROUT(nfsm_fhtom(&info, vp));
3127 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
984263bc
MD
3128 txdr_hyper(offset, tl);
3129 tl += 2;
3130 *tl = txdr_unsigned(cnt);
42edf14f
MD
3131 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3132 nfs_vpcred(vp, ND_WRITE), &error));
3133 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
984263bc 3134 if (!error) {
42edf14f 3135 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
984263bc
MD
3136 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3137 NFSX_V3WRITEVERF)) {
3138 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3139 NFSX_V3WRITEVERF);
3140 error = NFSERR_STALEWRITEVERF;
3141 }
3142 }
42edf14f
MD
3143 m_freem(info.mrep);
3144 info.mrep = NULL;
6b08710e 3145nfsmout:
984263bc
MD
3146 return (error);
3147}
3148
3149/*
3150 * Kludge City..
3151 * - make nfs_bmap() essentially a no-op that does no translation
3152 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3153 * (Maybe I could use the process's page mapping, but I was concerned that
3154 * Kernel Write might not be enabled and also figured copyout() would do
3155 * a lot more work than bcopy() and also it currently happens in the
3156 * context of the swapper process (2).
e851b29e 3157 *
08daea96 3158 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
54078292 3159 * off_t *a_doffsetp, int *a_runp, int *a_runb)
984263bc
MD
3160 */
3161static int
e851b29e 3162nfs_bmap(struct vop_bmap_args *ap)
984263bc 3163{
c6b43e93 3164 /* no token lock required */
54078292
MD
3165 if (ap->a_doffsetp != NULL)
3166 *ap->a_doffsetp = ap->a_loffset;
984263bc
MD
3167 if (ap->a_runp != NULL)
3168 *ap->a_runp = 0;
3169 if (ap->a_runb != NULL)
3170 *ap->a_runb = 0;
3171 return (0);
3172}
3173
3174/*
3175 * Strategy routine.
984263bc
MD
3176 */
3177static int
e851b29e 3178nfs_strategy(struct vop_strategy_args *ap)
984263bc 3179{
81b5c339
MD
3180 struct bio *bio = ap->a_bio;
3181 struct bio *nbio;
f64b567c 3182 struct buf *bp __debugvar = bio->bio_buf;
c6b43e93 3183 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
dadab5e9 3184 struct thread *td;
cc7d050e 3185 int error;
984263bc 3186
10f3fee5
MD
3187 KASSERT(bp->b_cmd != BUF_CMD_DONE,
3188 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3b6a19b2 3189 KASSERT(BUF_LOCKINUSE(bp),
81b5c339 3190 ("nfs_strategy: buffer %p not locked", bp));
984263bc 3191
ae8e83e6 3192 if (bio->bio_flags & BIO_SYNC)
dadab5e9 3193 td = curthread; /* XXX */
ae8e83e6
MD
3194 else
3195 td = NULL;
984263bc 3196
c6b43e93
MD
3197 lwkt_gettoken(&nmp->nm_token);
3198
81b5c339 3199 /*
54078292
MD
3200 * We probably don't need to push an nbio any more since no
3201 * block conversion is required due to the use of 64 bit byte
3202 * offsets, but do it anyway.
8bfa5617
MD
3203 *
3204 * NOTE: When NFS callers itself via this strategy routines and
3205 * sets up a synchronous I/O, it expects the I/O to run
3206 * synchronously (its bio_done routine just assumes it),
3207 * so for now we have to honor the bit.
81b5c339
MD
3208 */
3209 nbio = push_bio(bio);
54078292 3210 nbio->bio_offset = bio->bio_offset;
8bfa5617 3211 nbio->bio_flags = bio->bio_flags & BIO_SYNC;
81b5c339 3212
984263bc
MD
3213 /*
3214 * If the op is asynchronous and an i/o daemon is waiting
3215 * queue the request, wake it up and wait for completion
3216 * otherwise just do it ourselves.
3217 */
edb90c22 3218 if (bio->bio_flags & BIO_SYNC) {
cc7d050e 3219 error = nfs_doio(ap->a_vp, nbio, td);
edb90c22
MD
3220 } else {
3221 nfs_asyncio(ap->a_vp, nbio);
cc7d050e 3222 error = 0;
edb90c22 3223 }
c6b43e93
MD
3224 lwkt_reltoken(&nmp->nm_token);
3225
cc7d050e 3226 return (error);
984263bc
MD
3227}
3228
984263bc
MD
3229/*
3230 * fsync vnode op. Just call nfs_flush() with commit == 1.
e851b29e 3231 *
b478fdce 3232 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
984263bc
MD
3233 */
3234/* ARGSUSED */
3235static int
e851b29e 3236nfs_fsync(struct vop_fsync_args *ap)
984263bc 3237{
c6b43e93
MD
3238 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3239 int error;
3240
3241 lwkt_gettoken(&nmp->nm_token);
fd2da346
MD
3242
3243 /*
3244 * NOTE: Because attributes are set synchronously we currently
3245 * do not have to implement vsetisdirty()/vclrisdirty().
3246 */
c6b43e93 3247 error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
fd2da346 3248
c6b43e93
MD
3249 lwkt_reltoken(&nmp->nm_token);
3250
3251 return error;
984263bc
MD
3252}
3253
3254/*
6bae6177
MD
3255 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
3256 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
3257 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
3258 * set the buffer contains data that has already been written to the server
3259 * and which now needs a commit RPC.
3260 *
3261 * If commit is 0 we only take one pass and only flush buffers containing new
3262 * dirty data.
3263 *
3264 * If commit is 1 we take two passes, issuing a commit RPC in the second
3265 * pass.
3266 *
3267 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3268 * to completely flush all pending data.
3269 *
3270 * Note that the RB_SCAN code properly handles the case where the
3271 * callback might block and directly or indirectly (another thread) cause
3272 * the RB tree to change.
984263bc 3273 */
6bae6177
MD
3274
3275#ifndef NFS_COMMITBVECSIZ
3276#define NFS_COMMITBVECSIZ 16
3277#endif
3278
3279struct nfs_flush_info {
3280 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3281 struct thread *td;
3282 struct vnode *vp;
3283 int waitfor;
3284 int slpflag;
3285 int slptimeo;
3286 int loops;
3287 struct buf *bvary[NFS_COMMITBVECSIZ];
3288 int bvsize;
3289 off_t beg_off;
3290 off_t end_off;
3291};