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