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