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