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