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