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