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