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