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