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