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