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