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