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