Style(9):
[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 $
5fd012e0 38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.35 2004/10/12 19:21:01 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) {
5fd012e0 901 VOP_UNLOCK(dvp, 0, td);
7ab77df6 902 cnp->cn_flags |= CNP_PDIRUNLOCK;
5fd012e0 903 error = vget(newvp, LK_EXCLUSIVE, td);
67773eb3 904 vrele(newvp); /* get rid of ref from lookup */
7ab77df6 905 if (!error && lockparent && (flags & CNP_ISLASTCN)) {
5fd012e0 906 error = vn_lock(dvp, LK_EXCLUSIVE, td);
7ab77df6
MD
907 if (error == 0)
908 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
909 }
984263bc 910 } else {
5fd012e0 911 error = vget(newvp, LK_EXCLUSIVE, td);
67773eb3 912 vrele(newvp); /* get rid of ref from lookup */
7ab77df6 913 if (!lockparent || error || !(flags & CNP_ISLASTCN)) {
5fd012e0 914 VOP_UNLOCK(dvp, 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)) {
5fd012e0 941 VOP_UNLOCK(dvp, 0, td);
7ab77df6
MD
942 cnp->cn_flags |= CNP_PDIRUNLOCK;
943 }
984263bc 944 }
5fd012e0 945 error = vn_lock(dvp, 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) {
5fd012e0 1015 VOP_UNLOCK(dvp, 0, td);
7ab77df6
MD
1016 cnp->cn_flags |= CNP_PDIRUNLOCK;
1017 }
984263bc
MD
1018 return (0);
1019 }
1020
2b69e610 1021 if (flags & CNP_ISDOTDOT) {
5fd012e0 1022 VOP_UNLOCK(dvp, 0, td);
7ab77df6 1023 cnp->cn_flags |= CNP_PDIRUNLOCK;
984263bc
MD
1024 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1025 if (error) {
5fd012e0 1026 vn_lock(dvp, 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 1031 if (lockparent && (flags & CNP_ISLASTCN)) {
5fd012e0 1032 error = vn_lock(dvp, LK_EXCLUSIVE, td);
7ab77df6
MD
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)) {
5fd012e0 1049 VOP_UNLOCK(dvp, 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) {
5fd012e0 1077 VOP_UNLOCK(dvp, 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.
5fd012e0
MD
1721 *
1722 * We must purge tvp from the cache (old API) or further accesses
1723 * will see the old version of the file and return ESTALE.
984263bc
MD
1724 */
1725 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1726 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
8e005a45 1727 cache_purge(tvp);
984263bc
MD
1728 vput(tvp);
1729 tvp = NULL;
5fd012e0
MD
1730 } else if (tvp) {
1731 cache_purge(tvp);
984263bc
MD
1732 }
1733
1734 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1735 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
dadab5e9 1736 tcnp->cn_td);
984263bc 1737
8e005a45
MD
1738 cache_purge(fvp);
1739#if 0
984263bc
MD
1740 if (fvp->v_type == VDIR) {
1741 if (tvp != NULL && tvp->v_type == VDIR)
1742 cache_purge(tdvp);
1743 cache_purge(fdvp);
1744 }
8e005a45 1745#endif
984263bc
MD
1746
1747out:
1748 if (tdvp == tvp)
1749 vrele(tdvp);
1750 else
1751 vput(tdvp);
1752 if (tvp)
1753 vput(tvp);
1754 vrele(fdvp);
1755 vrele(fvp);
1756 /*
1757 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1758 */
1759 if (error == ENOENT)
1760 error = 0;
1761 return (error);
1762}
1763
1764/*
1765 * nfs file rename rpc called from nfs_remove() above
1766 */
1767static int
e851b29e
CP
1768nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1769 struct sillyrename *sp)
984263bc
MD
1770{
1771 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
dadab5e9 1772 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
984263bc
MD
1773}
1774
1775/*
1776 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1777 */
1778static int
e851b29e
CP
1779nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1780 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1781 struct ucred *cred, struct thread *td)
984263bc 1782{
40393ded
RG
1783 u_int32_t *tl;
1784 caddr_t cp;
1785 int32_t t1, t2;
984263bc
MD
1786 caddr_t bpos, dpos, cp2;
1787 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1788 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1789 int v3 = NFS_ISV3(fdvp);
1790
1791 nfsstats.rpccnt[NFSPROC_RENAME]++;
1792 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1793 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1794 nfsm_rndup(tnamelen));
1795 nfsm_fhtom(fdvp, v3);
1796 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1797 nfsm_fhtom(tdvp, v3);
1798 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
dadab5e9 1799 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
984263bc
MD
1800 if (v3) {
1801 nfsm_wcc_data(fdvp, fwccflag);
1802 nfsm_wcc_data(tdvp, twccflag);
1803 }
6b08710e
MD
1804 m_freem(mrep);
1805nfsmout:
984263bc
MD
1806 VTONFS(fdvp)->n_flag |= NMODIFIED;
1807 VTONFS(tdvp)->n_flag |= NMODIFIED;
1808 if (!fwccflag)
1809 VTONFS(fdvp)->n_attrstamp = 0;
1810 if (!twccflag)
1811 VTONFS(tdvp)->n_attrstamp = 0;
1812 return (error);
1813}
1814
1815/*
1816 * nfs hard link create call
e851b29e
CP
1817 *
1818 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1819 * struct componentname *a_cnp)
984263bc
MD
1820 */
1821static int
e851b29e 1822nfs_link(struct vop_link_args *ap)
984263bc 1823{
40393ded
RG
1824 struct vnode *vp = ap->a_vp;
1825 struct vnode *tdvp = ap->a_tdvp;
1826 struct componentname *cnp = ap->a_cnp;
1827 u_int32_t *tl;
1828 caddr_t cp;
1829 int32_t t1, t2;
984263bc
MD
1830 caddr_t bpos, dpos, cp2;
1831 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1832 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1833 int v3;
1834
1835 if (vp->v_mount != tdvp->v_mount) {
1836 return (EXDEV);
1837 }
1838
1839 /*
1840 * Push all writes to the server, so that the attribute cache
1841 * doesn't get "out of sync" with the server.
1842 * XXX There should be a better way!
1843 */
3b568787 1844 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
984263bc
MD
1845
1846 v3 = NFS_ISV3(vp);
1847 nfsstats.rpccnt[NFSPROC_LINK]++;
1848 nfsm_reqhead(vp, NFSPROC_LINK,
1849 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1850 nfsm_fhtom(vp, v3);
1851 nfsm_fhtom(tdvp, v3);
1852 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
dadab5e9 1853 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1854 if (v3) {
1855 nfsm_postop_attr(vp, attrflag);
1856 nfsm_wcc_data(tdvp, wccflag);
1857 }
6b08710e
MD
1858 m_freem(mrep);
1859nfsmout:
984263bc
MD
1860 VTONFS(tdvp)->n_flag |= NMODIFIED;
1861 if (!attrflag)
1862 VTONFS(vp)->n_attrstamp = 0;
1863 if (!wccflag)
1864 VTONFS(tdvp)->n_attrstamp = 0;
1865 /*
1866 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1867 */
1868 if (error == EEXIST)
1869 error = 0;
1870 return (error);
1871}
1872
1873/*
1874 * nfs symbolic link create call
e851b29e
CP
1875 *
1876 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1877 * struct componentname *a_cnp, struct vattr *a_vap,
1878 * char *a_target)
984263bc
MD
1879 */
1880static int
e851b29e 1881nfs_symlink(struct vop_symlink_args *ap)
984263bc 1882{
40393ded
RG
1883 struct vnode *dvp = ap->a_dvp;
1884 struct vattr *vap = ap->a_vap;
1885 struct componentname *cnp = ap->a_cnp;
1886 struct nfsv2_sattr *sp;
1887 u_int32_t *tl;
1888 caddr_t cp;
1889 int32_t t1, t2;
984263bc
MD
1890 caddr_t bpos, dpos, cp2;
1891 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1892 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1893 struct vnode *newvp = (struct vnode *)0;
1894 int v3 = NFS_ISV3(dvp);
1895
1896 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1897 slen = strlen(ap->a_target);
1898 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1899 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1900 nfsm_fhtom(dvp, v3);
1901 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1902 if (v3) {
1903 nfsm_v3attrbuild(vap, FALSE);
1904 }
1905 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1906 if (!v3) {
1907 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1908 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1909 sp->sa_uid = nfs_xdrneg1;
1910 sp->sa_gid = nfs_xdrneg1;
1911 sp->sa_size = nfs_xdrneg1;
1912 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1913 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1914 }
1915
1916 /*
1917 * Issue the NFS request and get the rpc response.
1918 *
1919 * Only NFSv3 responses returning an error of 0 actually return
1920 * a file handle that can be converted into newvp without having
1921 * to do an extra lookup rpc.
1922 */
dadab5e9 1923 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1924 if (v3) {
1925 if (error == 0)
1926 nfsm_mtofh(dvp, newvp, v3, gotvp);
1927 nfsm_wcc_data(dvp, wccflag);
1928 }
1929
1930 /*
1931 * out code jumps -> here, mrep is also freed.
1932 */
1933
6b08710e
MD
1934 m_freem(mrep);
1935nfsmout:
984263bc
MD
1936
1937 /*
1938 * If we get an EEXIST error, silently convert it to no-error
1939 * in case of an NFS retry.
1940 */
1941 if (error == EEXIST)
1942 error = 0;
1943
1944 /*
1945 * If we do not have (or no longer have) an error, and we could
1946 * not extract the newvp from the response due to the request being
1947 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1948 * to obtain a newvp to return.
1949 */
1950 if (error == 0 && newvp == NULL) {
1951 struct nfsnode *np = NULL;
1952
1953 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
dadab5e9 1954 cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1955 if (!error)
1956 newvp = NFSTOV(np);
1957 }
1958 if (error) {
1959 if (newvp)
1960 vput(newvp);
1961 } else {
1962 *ap->a_vpp = newvp;
1963 }
1964 VTONFS(dvp)->n_flag |= NMODIFIED;
1965 if (!wccflag)
1966 VTONFS(dvp)->n_attrstamp = 0;
1967 return (error);
1968}
1969
1970/*
1971 * nfs make dir call
e851b29e
CP
1972 *
1973 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1974 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1975 */
1976static int
e851b29e 1977nfs_mkdir(struct vop_mkdir_args *ap)
984263bc 1978{
40393ded
RG
1979 struct vnode *dvp = ap->a_dvp;
1980 struct vattr *vap = ap->a_vap;
1981 struct componentname *cnp = ap->a_cnp;
1982 struct nfsv2_sattr *sp;
1983 u_int32_t *tl;
1984 caddr_t cp;
1985 int32_t t1, t2;
1986 int len;
984263bc
MD
1987 struct nfsnode *np = (struct nfsnode *)0;
1988 struct vnode *newvp = (struct vnode *)0;
1989 caddr_t bpos, dpos, cp2;
1990 int error = 0, wccflag = NFSV3_WCCRATTR;
1991 int gotvp = 0;
1992 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1993 struct vattr vattr;
1994 int v3 = NFS_ISV3(dvp);
1995
3b568787 1996 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
984263bc
MD
1997 return (error);
1998 }
1999 len = cnp->cn_namelen;
2000 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2001 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2002 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2003 nfsm_fhtom(dvp, v3);
2004 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2005 if (v3) {
2006 nfsm_v3attrbuild(vap, FALSE);
2007 } else {
2008 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2009 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2010 sp->sa_uid = nfs_xdrneg1;
2011 sp->sa_gid = nfs_xdrneg1;
2012 sp->sa_size = nfs_xdrneg1;
2013 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2014 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2015 }
dadab5e9 2016 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
984263bc
MD
2017 if (!error)
2018 nfsm_mtofh(dvp, newvp, v3, gotvp);
2019 if (v3)
2020 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
2021 m_freem(mrep);
2022nfsmout:
984263bc
MD
2023 VTONFS(dvp)->n_flag |= NMODIFIED;
2024 if (!wccflag)
2025 VTONFS(dvp)->n_attrstamp = 0;
2026 /*
2027 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2028 * if we can succeed in looking up the directory.
2029 */
2030 if (error == EEXIST || (!error && !gotvp)) {
2031 if (newvp) {
2032 vrele(newvp);
2033 newvp = (struct vnode *)0;
2034 }
2035 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
dadab5e9 2036 cnp->cn_td, &np);
984263bc
MD
2037 if (!error) {
2038 newvp = NFSTOV(np);
2039 if (newvp->v_type != VDIR)
2040 error = EEXIST;
2041 }
2042 }
2043 if (error) {
2044 if (newvp)
2045 vrele(newvp);
2046 } else
2047 *ap->a_vpp = newvp;
2048 return (error);
2049}
2050
2051/*
2052 * nfs remove directory call
e851b29e
CP
2053 *
2054 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2055 * struct componentname *a_cnp)
984263bc
MD
2056 */
2057static int
e851b29e 2058nfs_rmdir(struct vop_rmdir_args *ap)
984263bc 2059{
40393ded
RG
2060 struct vnode *vp = ap->a_vp;
2061 struct vnode *dvp = ap->a_dvp;
2062 struct componentname *cnp = ap->a_cnp;
2063 u_int32_t *tl;
2064 caddr_t cp;
2065 int32_t t1, t2;
984263bc
MD
2066 caddr_t bpos, dpos, cp2;
2067 int error = 0, wccflag = NFSV3_WCCRATTR;
2068 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2069 int v3 = NFS_ISV3(dvp);
2070
2071 if (dvp == vp)
2072 return (EINVAL);
2073 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2074 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2075 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2076 nfsm_fhtom(dvp, v3);
2077 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
dadab5e9 2078 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
984263bc
MD
2079 if (v3)
2080 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
2081 m_freem(mrep);
2082nfsmout:
984263bc
MD
2083 VTONFS(dvp)->n_flag |= NMODIFIED;
2084 if (!wccflag)
2085 VTONFS(dvp)->n_attrstamp = 0;
8e005a45 2086#if 0
984263bc 2087 cache_purge(dvp);
8e005a45 2088#endif
984263bc
MD
2089 cache_purge(vp);
2090 /*
2091 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2092 */
2093 if (error == ENOENT)
2094 error = 0;
2095 return (error);
2096}
2097
2098/*
2099 * nfs readdir call
e851b29e
CP
2100 *
2101 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
984263bc
MD
2102 */
2103static int
e851b29e 2104nfs_readdir(struct vop_readdir_args *ap)
984263bc 2105{
40393ded
RG
2106 struct vnode *vp = ap->a_vp;
2107 struct nfsnode *np = VTONFS(vp);
2108 struct uio *uio = ap->a_uio;
984263bc
MD
2109 int tresid, error;
2110 struct vattr vattr;
2111
2112 if (vp->v_type != VDIR)
2113 return (EPERM);
2114 /*
2115 * First, check for hit on the EOF offset cache
2116 */
2117 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2118 (np->n_flag & NMODIFIED) == 0) {
2119 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2120 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2121 nfsstats.direofcache_hits++;
2122 return (0);
2123 }
3b568787 2124 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
984263bc
MD
2125 np->n_mtime == vattr.va_mtime.tv_sec) {
2126 nfsstats.direofcache_hits++;
2127 return (0);
2128 }
2129 }
2130
2131 /*
2132 * Call nfs_bioread() to do the real work.
2133 */
2134 tresid = uio->uio_resid;
3b568787 2135 error = nfs_bioread(vp, uio, 0);
984263bc
MD
2136
2137 if (!error && uio->uio_resid == tresid)
2138 nfsstats.direofcache_misses++;
2139 return (error);
2140}
2141
2142/*
2143 * Readdir rpc call.
2144 * Called from below the buffer cache by nfs_doio().
2145 */
2146int
3b568787 2147nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
984263bc 2148{
40393ded
RG
2149 int len, left;
2150 struct dirent *dp = NULL;
2151 u_int32_t *tl;
2152 caddr_t cp;
2153 int32_t t1, t2;
2154 nfsuint64 *cookiep;
984263bc
MD
2155 caddr_t bpos, dpos, cp2;
2156 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2157 nfsuint64 cookie;
2158 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2159 struct nfsnode *dnp = VTONFS(vp);
2160 u_quad_t fileno;
2161 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2162 int attrflag;
2163 int v3 = NFS_ISV3(vp);
2164
2165#ifndef DIAGNOSTIC
2166 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2167 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2168 panic("nfs readdirrpc bad uio");
2169#endif
2170
2171 /*
2172 * If there is no cookie, assume directory was stale.
2173 */
2174 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2175 if (cookiep)
2176 cookie = *cookiep;
2177 else
2178 return (NFSERR_BAD_COOKIE);
2179 /*
2180 * Loop around doing readdir rpc's of size nm_readdirsize
2181 * truncated to a multiple of DIRBLKSIZ.
2182 * The stopping criteria is EOF or buffer full.
2183 */
2184 while (more_dirs && bigenough) {
2185 nfsstats.rpccnt[NFSPROC_READDIR]++;
2186 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2187 NFSX_READDIR(v3));
2188 nfsm_fhtom(vp, v3);
2189 if (v3) {
2190 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2191 *tl++ = cookie.nfsuquad[0];
2192 *tl++ = cookie.nfsuquad[1];
2193 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2194 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2195 } else {
2196 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2197 *tl++ = cookie.nfsuquad[0];
2198 }
2199 *tl = txdr_unsigned(nmp->nm_readdirsize);
c1cf1e59 2200 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
984263bc
MD
2201 if (v3) {
2202 nfsm_postop_attr(vp, attrflag);
2203 if (!error) {
2204 nfsm_dissect(tl, u_int32_t *,
2205 2 * NFSX_UNSIGNED);
2206 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2207 dnp->n_cookieverf.nfsuquad[1] = *tl;
2208 } else {
2209 m_freem(mrep);
2210 goto nfsmout;
2211 }
2212 }
2213 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2214 more_dirs = fxdr_unsigned(int, *tl);
2215
2216 /* loop thru the dir entries, doctoring them to 4bsd form */
2217 while (more_dirs && bigenough) {
2218 if (v3) {
2219 nfsm_dissect(tl, u_int32_t *,
2220 3 * NFSX_UNSIGNED);
2221 fileno = fxdr_hyper(tl);
2222 len = fxdr_unsigned(int, *(tl + 2));
2223 } else {
2224 nfsm_dissect(tl, u_int32_t *,
2225 2 * NFSX_UNSIGNED);
2226 fileno = fxdr_unsigned(u_quad_t, *tl++);
2227 len = fxdr_unsigned(int, *tl);
2228 }
2229 if (len <= 0 || len > NFS_MAXNAMLEN) {
2230 error = EBADRPC;
2231 m_freem(mrep);
2232 goto nfsmout;
2233 }
2234 tlen = nfsm_rndup(len);
2235 if (tlen == len)
2236 tlen += 4; /* To ensure null termination */
2237 left = DIRBLKSIZ - blksiz;
2238 if ((tlen + DIRHDSIZ) > left) {
2239 dp->d_reclen += left;
2240 uiop->uio_iov->iov_base += left;
2241 uiop->uio_iov->iov_len -= left;
2242 uiop->uio_offset += left;
2243 uiop->uio_resid -= left;
2244 blksiz = 0;
2245 }
2246 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2247 bigenough = 0;
2248 if (bigenough) {
2249 dp = (struct dirent *)uiop->uio_iov->iov_base;
2250 dp->d_fileno = (int)fileno;
2251 dp->d_namlen = len;
2252 dp->d_reclen = tlen + DIRHDSIZ;
2253 dp->d_type = DT_UNKNOWN;
2254 blksiz += dp->d_reclen;
2255 if (blksiz == DIRBLKSIZ)
2256 blksiz = 0;
2257 uiop->uio_offset += DIRHDSIZ;
2258 uiop->uio_resid -= DIRHDSIZ;
2259 uiop->uio_iov->iov_base += DIRHDSIZ;
2260 uiop->uio_iov->iov_len -= DIRHDSIZ;
2261 nfsm_mtouio(uiop, len);
2262 cp = uiop->uio_iov->iov_base;
2263 tlen -= len;
2264 *cp = '\0'; /* null terminate */
2265 uiop->uio_iov->iov_base += tlen;
2266 uiop->uio_iov->iov_len -= tlen;
2267 uiop->uio_offset += tlen;
2268 uiop->uio_resid -= tlen;
2269 } else
2270 nfsm_adv(nfsm_rndup(len));
2271 if (v3) {
2272 nfsm_dissect(tl, u_int32_t *,
2273 3 * NFSX_UNSIGNED);
2274 } else {
2275 nfsm_dissect(tl, u_int32_t *,
2276 2 * NFSX_UNSIGNED);
2277 }
2278 if (bigenough) {
2279 cookie.nfsuquad[0] = *tl++;
2280 if (v3)
2281 cookie.nfsuquad[1] = *tl++;
2282 } else if (v3)
2283 tl += 2;
2284 else
2285 tl++;
2286 more_dirs = fxdr_unsigned(int, *tl);
2287 }
2288 /*
2289 * If at end of rpc data, get the eof boolean
2290 */
2291 if (!more_dirs) {
2292 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2293 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2294 }
2295 m_freem(mrep);
2296 }
2297 /*
2298 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2299 * by increasing d_reclen for the last record.
2300 */
2301 if (blksiz > 0) {
2302 left = DIRBLKSIZ - blksiz;
2303 dp->d_reclen += left;
2304 uiop->uio_iov->iov_base += left;
2305 uiop->uio_iov->iov_len -= left;
2306 uiop->uio_offset += left;
2307 uiop->uio_resid -= left;
2308 }
2309
2310 /*
2311 * We are now either at the end of the directory or have filled the
2312 * block.
2313 */
2314 if (bigenough)
2315 dnp->n_direofoffset = uiop->uio_offset;
2316 else {
2317 if (uiop->uio_resid > 0)
2318 printf("EEK! readdirrpc resid > 0\n");
2319 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2320 *cookiep = cookie;
2321 }
2322nfsmout:
2323 return (error);
2324}
2325
2326/*
2327 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2328 */
2329int
3b568787 2330nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
984263bc 2331{
40393ded
RG
2332 int len, left;
2333 struct dirent *dp;
2334 u_int32_t *tl;
2335 caddr_t cp;
2336 int32_t t1, t2;
2337 struct vnode *newvp;
2338 nfsuint64 *cookiep;
984263bc
MD
2339 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2340 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2341 struct nameidata nami, *ndp = &nami;
2342 struct componentname *cnp = &ndp->ni_cnd;
2343 nfsuint64 cookie;
2344 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2345 struct nfsnode *dnp = VTONFS(vp), *np;
2346 nfsfh_t *fhp;
2347 u_quad_t fileno;
2348 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2349 int attrflag, fhsize;
2350
2351#ifndef nolint
2352 dp = (struct dirent *)0;
2353#endif
2354#ifndef DIAGNOSTIC
2355 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2356 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2357 panic("nfs readdirplusrpc bad uio");
2358#endif
2359 ndp->ni_dvp = vp;
2360 newvp = NULLVP;
2361
2362 /*
2363 * If there is no cookie, assume directory was stale.
2364 */
2365 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2366 if (cookiep)
2367 cookie = *cookiep;
2368 else
2369 return (NFSERR_BAD_COOKIE);
2370 /*
2371 * Loop around doing readdir rpc's of size nm_readdirsize
2372 * truncated to a multiple of DIRBLKSIZ.
2373 * The stopping criteria is EOF or buffer full.
2374 */
2375 while (more_dirs && bigenough) {
2376 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2377 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2378 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2379 nfsm_fhtom(vp, 1);
2380 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2381 *tl++ = cookie.nfsuquad[0];
2382 *tl++ = cookie.nfsuquad[1];
2383 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2384 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2385 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2386 *tl = txdr_unsigned(nmp->nm_rsize);
c1cf1e59 2387 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
984263bc
MD
2388 nfsm_postop_attr(vp, attrflag);
2389 if (error) {
2390 m_freem(mrep);
2391 goto nfsmout;
2392 }
2393 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2394 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2395 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2396 more_dirs = fxdr_unsigned(int, *tl);
2397
2398 /* loop thru the dir entries, doctoring them to 4bsd form */
2399 while (more_dirs && bigenough) {
2400 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2401 fileno = fxdr_hyper(tl);
2402 len = fxdr_unsigned(int, *(tl + 2));
2403 if (len <= 0 || len > NFS_MAXNAMLEN) {
2404 error = EBADRPC;
2405 m_freem(mrep);
2406 goto nfsmout;
2407 }
2408 tlen = nfsm_rndup(len);
2409 if (tlen == len)
2410 tlen += 4; /* To ensure null termination*/
2411 left = DIRBLKSIZ - blksiz;
2412 if ((tlen + DIRHDSIZ) > left) {
2413 dp->d_reclen += left;
2414 uiop->uio_iov->iov_base += left;
2415 uiop->uio_iov->iov_len -= left;
2416 uiop->uio_offset += left;
2417 uiop->uio_resid -= left;
2418 blksiz = 0;
2419 }
2420 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2421 bigenough = 0;
2422 if (bigenough) {
2423 dp = (struct dirent *)uiop->uio_iov->iov_base;
2424 dp->d_fileno = (int)fileno;
2425 dp->d_namlen = len;
2426 dp->d_reclen = tlen + DIRHDSIZ;
2427 dp->d_type = DT_UNKNOWN;
2428 blksiz += dp->d_reclen;
2429 if (blksiz == DIRBLKSIZ)
2430 blksiz = 0;
2431 uiop->uio_offset += DIRHDSIZ;
2432 uiop->uio_resid -= DIRHDSIZ;
2433 uiop->uio_iov->iov_base += DIRHDSIZ;
2434 uiop->uio_iov->iov_len -= DIRHDSIZ;
2435 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2436 cnp->cn_namelen = len;
2437 nfsm_mtouio(uiop, len);
2438 cp = uiop->uio_iov->iov_base;
2439 tlen -= len;
2440 *cp = '\0';
2441 uiop->uio_iov->iov_base += tlen;
2442 uiop->uio_iov->iov_len -= tlen;
2443 uiop->uio_offset += tlen;
2444 uiop->uio_resid -= tlen;
2445 } else
2446 nfsm_adv(nfsm_rndup(len));
2447 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2448 if (bigenough) {
2449 cookie.nfsuquad[0] = *tl++;
2450 cookie.nfsuquad[1] = *tl++;
2451 } else
2452 tl += 2;
2453
2454 /*
2455 * Since the attributes are before the file handle
2456 * (sigh), we must skip over the attributes and then
2457 * come back and get them.
2458 */
2459 attrflag = fxdr_unsigned(int, *tl);
2460 if (attrflag) {
2461 dpossav1 = dpos;
2462 mdsav1 = md;
2463 nfsm_adv(NFSX_V3FATTR);
2464 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2465 doit = fxdr_unsigned(int, *tl);
2466 if (doit) {
2467 nfsm_getfh(fhp, fhsize, 1);
2468 if (NFS_CMPFH(dnp, fhp, fhsize)) {
597aea93 2469 vref(vp);
984263bc
MD
2470 newvp = vp;
2471 np = dnp;
2472 } else {
2473 error = nfs_nget(vp->v_mount, fhp,
2474 fhsize, &np);
2475 if (error)
2476 doit = 0;
2477 else
2478 newvp = NFSTOV(np);
2479 }
2480 }
2481 if (doit && bigenough) {
2482 dpossav2 = dpos;
2483 dpos = dpossav1;
2484 mdsav2 = md;
2485 md = mdsav1;
2486 nfsm_loadattr(newvp, (struct vattr *)0);
2487 dpos = dpossav2;
2488 md = mdsav2;
2489 dp->d_type =
2490 IFTODT(VTTOIF(np->n_vattr.va_type));
2491 ndp->ni_vp = newvp;
21739618 2492 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
984263bc
MD
2493 }
2494 } else {
2495 /* Just skip over the file handle */
2496 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2497 i = fxdr_unsigned(int, *tl);
2498 nfsm_adv(nfsm_rndup(i));
2499 }
2500 if (newvp != NULLVP) {
2501 if (newvp == vp)
2502 vrele(newvp);
2503 else
2504 vput(newvp);
2505 newvp = NULLVP;
2506 }
2507 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2508 more_dirs = fxdr_unsigned(int, *tl);
2509 }
2510 /*
2511 * If at end of rpc data, get the eof boolean
2512 */
2513 if (!more_dirs) {
2514 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2515 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2516 }
2517 m_freem(mrep);
2518 }
2519 /*
2520 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2521 * by increasing d_reclen for the last record.
2522 */
2523 if (blksiz > 0) {
2524 left = DIRBLKSIZ - blksiz;
2525 dp->d_reclen += left;
2526 uiop->uio_iov->iov_base += left;
2527 uiop->uio_iov->iov_len -= left;
2528 uiop->uio_offset += left;
2529 uiop->uio_resid -= left;
2530 }
2531
2532 /*
2533 * We are now either at the end of the directory or have filled the
2534 * block.
2535 */
2536 if (bigenough)
2537 dnp->n_direofoffset = uiop->uio_offset;
2538 else {
2539 if (uiop->uio_resid > 0)
2540 printf("EEK! readdirplusrpc resid > 0\n");
2541 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2542 *cookiep = cookie;
2543 }
2544nfsmout:
2545 if (newvp != NULLVP) {
2546 if (newvp == vp)
2547 vrele(newvp);
2548 else
2549 vput(newvp);
2550 newvp = NULLVP;
2551 }
2552 return (error);
2553}
2554
2555/*
2556 * Silly rename. To make the NFS filesystem that is stateless look a little
2557 * more like the "ufs" a remove of an active vnode is translated to a rename
2558 * to a funny looking filename that is removed by nfs_inactive on the
2559 * nfsnode. There is the potential for another process on a different client
2560 * to create the same funny name between the nfs_lookitup() fails and the
2561 * nfs_rename() completes, but...
2562 */
2563static int
e851b29e 2564nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
984263bc 2565{
40393ded 2566 struct sillyrename *sp;
984263bc
MD
2567 struct nfsnode *np;
2568 int error;
984263bc 2569
8c361dda
MD
2570 /*
2571 * We previously purged dvp instead of vp. I don't know why, it
2572 * completely destroys performance. We can't do it anyway with the
2573 * new VFS API since we would be breaking the namecache topology.
2574 */
2575 cache_purge(vp);
984263bc
MD
2576 np = VTONFS(vp);
2577#ifndef DIAGNOSTIC
2578 if (vp->v_type == VDIR)
2579 panic("nfs: sillyrename dir");
2580#endif
2581 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2582 M_NFSREQ, M_WAITOK);
2583 sp->s_cred = crdup(cnp->cn_cred);
2584 sp->s_dvp = dvp;
597aea93 2585 vref(dvp);
984263bc
MD
2586
2587 /* Fudge together a funny name */
dadab5e9 2588 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
984263bc
MD
2589
2590 /* Try lookitups until we get one that isn't there */
2591 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
dadab5e9 2592 cnp->cn_td, (struct nfsnode **)0) == 0) {
984263bc
MD
2593 sp->s_name[4]++;
2594 if (sp->s_name[4] > 'z') {
2595 error = EINVAL;
2596 goto bad;
2597 }
2598 }
2599 error = nfs_renameit(dvp, cnp, sp);
2600 if (error)
2601 goto bad;
2602 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
dadab5e9 2603 cnp->cn_td, &np);
984263bc
MD
2604 np->n_sillyrename = sp;
2605 return (0);
2606bad:
2607 vrele(sp->s_dvp);
2608 crfree(sp->s_cred);
2609 free((caddr_t)sp, M_NFSREQ);
2610 return (error);
2611}
2612
2613/*
2614 * Look up a file name and optionally either update the file handle or
2615 * allocate an nfsnode, depending on the value of npp.
2616 * npp == NULL --> just do the lookup
2617 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2618 * handled too
2619 * *npp != NULL --> update the file handle in the vnode
2620 */
2621static int
e851b29e
CP
2622nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2623 struct thread *td, struct nfsnode **npp)
984263bc 2624{
40393ded
RG
2625 u_int32_t *tl;
2626 caddr_t cp;
2627 int32_t t1, t2;
984263bc
MD
2628 struct vnode *newvp = (struct vnode *)0;
2629 struct nfsnode *np, *dnp = VTONFS(dvp);
2630 caddr_t bpos, dpos, cp2;
2631 int error = 0, fhlen, attrflag;
2632 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2633 nfsfh_t *nfhp;
2634 int v3 = NFS_ISV3(dvp);
2635
2636 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2637 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2638 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2639 nfsm_fhtom(dvp, v3);
2640 nfsm_strtom(name, len, NFS_MAXNAMLEN);
dadab5e9 2641 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
984263bc
MD
2642 if (npp && !error) {
2643 nfsm_getfh(nfhp, fhlen, v3);
2644 if (*npp) {
2645 np = *npp;
2646 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2647 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2648 np->n_fhp = &np->n_fh;
2649 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2650 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2651 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2652 np->n_fhsize = fhlen;
2653 newvp = NFSTOV(np);
2654 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
597aea93 2655 vref(dvp);
984263bc
MD
2656 newvp = dvp;
2657 } else {
2658 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2659 if (error) {
2660 m_freem(mrep);
2661 return (error);
2662 }
2663 newvp = NFSTOV(np);
2664 }
2665 if (v3) {
2666 nfsm_postop_attr(newvp, attrflag);
2667 if (!attrflag && *npp == NULL) {
2668 m_freem(mrep);
2669 if (newvp == dvp)
2670 vrele(newvp);
2671 else
2672 vput(newvp);
2673 return (ENOENT);
2674 }
2675 } else
2676 nfsm_loadattr(newvp, (struct vattr *)0);
2677 }
6b08710e
MD
2678 m_freem(mrep);
2679nfsmout:
984263bc
MD
2680 if (npp && *npp == NULL) {
2681 if (error) {
2682 if (newvp) {
2683 if (newvp == dvp)
2684 vrele(newvp);
2685 else
2686 vput(newvp);
2687 }
2688 } else
2689 *npp = np;
2690 }
2691 return (error);
2692}
2693
2694/*
2695 * Nfs Version 3 commit rpc
2696 */
2697int
3b568787 2698nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
984263bc 2699{
40393ded
RG
2700 caddr_t cp;
2701 u_int32_t *tl;
2702 int32_t t1, t2;
2703 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc
MD
2704 caddr_t bpos, dpos, cp2;
2705 int error = 0, wccflag = NFSV3_WCCRATTR;
2706 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2707
2708 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2709 return (0);
2710 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2711 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2712 nfsm_fhtom(vp, 1);
2713 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2714 txdr_hyper(offset, tl);
2715 tl += 2;
2716 *tl = txdr_unsigned(cnt);
c1cf1e59 2717 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
984263bc
MD
2718 nfsm_wcc_data(vp, wccflag);
2719 if (!error) {
2720 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2721 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2722 NFSX_V3WRITEVERF)) {
2723 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2724 NFSX_V3WRITEVERF);
2725 error = NFSERR_STALEWRITEVERF;
2726 }
2727 }
6b08710e
MD
2728 m_freem(mrep);
2729nfsmout:
984263bc
MD
2730 return (error);
2731}
2732
2733/*
2734 * Kludge City..
2735 * - make nfs_bmap() essentially a no-op that does no translation
2736 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2737 * (Maybe I could use the process's page mapping, but I was concerned that
2738 * Kernel Write might not be enabled and also figured copyout() would do
2739 * a lot more work than bcopy() and also it currently happens in the
2740 * context of the swapper process (2).
e851b29e
CP
2741 *
2742 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2743 * daddr_t *a_bnp, int *a_runp, int *a_runb)
984263bc
MD
2744 */
2745static int
e851b29e 2746nfs_bmap(struct vop_bmap_args *ap)
984263bc 2747{
40393ded 2748 struct vnode *vp = ap->a_vp;
984263bc
MD
2749
2750 if (ap->a_vpp != NULL)
2751 *ap->a_vpp = vp;
2752 if (ap->a_bnp != NULL)
2753 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2754 if (ap->a_runp != NULL)
2755 *ap->a_runp = 0;
2756 if (ap->a_runb != NULL)
2757 *ap->a_runb = 0;
2758 return (0);
2759}
2760
2761/*
2762 * Strategy routine.
2763 * For async requests when nfsiod(s) are running, queue the request by
2764 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2765 * request.
2766 */
2767static int
e851b29e 2768nfs_strategy(struct vop_strategy_args *ap)
984263bc 2769{
40393ded 2770 struct buf *bp = ap->a_bp;
dadab5e9 2771 struct thread *td;
984263bc
MD
2772 int error = 0;
2773
2774 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2775 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2776
2777 if (bp->b_flags & B_PHYS)
2778 panic("nfs physio");
2779
2780 if (bp->b_flags & B_ASYNC)
dadab5e9 2781 td = NULL;
984263bc 2782 else
dadab5e9 2783 td = curthread; /* XXX */
984263bc 2784
984263bc
MD
2785 /*
2786 * If the op is asynchronous and an i/o daemon is waiting
2787 * queue the request, wake it up and wait for completion
2788 * otherwise just do it ourselves.
2789 */
2790 if ((bp->b_flags & B_ASYNC) == 0 ||
3b568787
MD
2791 nfs_asyncio(bp, td))
2792 error = nfs_doio(bp, td);
984263bc
MD
2793 return (error);
2794}
2795
2796/*
2797 * Mmap a file
2798 *
2799 * NB Currently unsupported.
e851b29e
CP
2800 *
2801 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2802 * struct thread *a_td)
984263bc
MD
2803 */
2804/* ARGSUSED */
2805static int
e851b29e 2806nfs_mmap(struct vop_mmap_args *ap)
984263bc 2807{
984263bc
MD
2808 return (EINVAL);
2809}
2810
2811/*
2812 * fsync vnode op. Just call nfs_flush() with commit == 1.
e851b29e
CP
2813 *
2814 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2815 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
984263bc
MD
2816 */
2817/* ARGSUSED */
2818static int
e851b29e 2819nfs_fsync(struct vop_fsync_args *ap)
984263bc 2820{
3b568787 2821 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
984263bc
MD
2822}
2823
2824/*
2825 * Flush all the blocks associated with a vnode.
2826 * Walk through the buffer pool and push any dirty pages
2827 * associated with the vnode.
2828 */
2829static int
e851b29e 2830nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
984263bc 2831{
40393ded
RG
2832 struct nfsnode *np = VTONFS(vp);
2833 struct buf *bp;
2834 int i;
984263bc
MD
2835 struct buf *nbp;
2836 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2837 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2838 int passone = 1;
2839 u_quad_t off, endoff, toff;
984263bc
MD
2840 struct buf **bvec = NULL;
2841#ifndef NFS_COMMITBVECSIZ
2842#define NFS_COMMITBVECSIZ 20
2843#endif
2844 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2845 int bvecsize = 0, bveccount;
2846
2847 if (nmp->nm_flag & NFSMNT_INT)
2848 slpflag = PCATCH;
2849 if (!commit)
2850 passone = 0;
2851 /*
2852 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2853 * server, but nas not been committed to stable storage on the server
2854 * yet. On the first pass, the byte range is worked out and the commit
2855 * rpc is done. On the second pass, nfs_writebp() is called to do the
2856 * job.
2857 */
2858again:
2859 off = (u_quad_t)-1;
2860 endoff = 0;
2861 bvecpos = 0;
2862 if (NFS_ISV3(vp) && commit) {
2863 s = splbio();
2864 /*
2865 * Count up how many buffers waiting for a commit.
2866 */
2867 bveccount = 0;
2868 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2869 nbp = TAILQ_NEXT(bp, b_vnbufs);
2870 if (BUF_REFCNT(bp) == 0 &&
2871 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2872 == (B_DELWRI | B_NEEDCOMMIT))
2873 bveccount++;
2874 }
2875 /*
2876 * Allocate space to remember the list of bufs to commit. It is
2877 * important to use M_NOWAIT here to avoid a race with nfs_write.
2878 * If we can't get memory (for whatever reason), we will end up
2879 * committing the buffers one-by-one in the loop below.
2880 */
2881 if (bvec != NULL && bvec != bvec_on_stack)
2882 free(bvec, M_TEMP);
2883 if (bveccount > NFS_COMMITBVECSIZ) {
2884 bvec = (struct buf **)
2885 malloc(bveccount * sizeof(struct buf *),
2886 M_TEMP, M_NOWAIT);
2887 if (bvec == NULL) {
2888 bvec = bvec_on_stack;
2889 bvecsize = NFS_COMMITBVECSIZ;
2890 } else
2891 bvecsize = bveccount;
2892 } else {
2893 bvec = bvec_on_stack;
2894 bvecsize = NFS_COMMITBVECSIZ;
2895 }
2896 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2897 nbp = TAILQ_NEXT(bp, b_vnbufs);
2898 if (bvecpos >= bvecsize)
2899 break;
2900 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2901 (B_DELWRI | B_NEEDCOMMIT) ||
2902 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2903 continue;
2904 bremfree(bp);
2905 /*
984263bc
MD
2906 * NOTE: we are not clearing B_DONE here, so we have
2907 * to do it later on in this routine if we intend to
2908 * initiate I/O on the bp.
2909 *
2910 * Note: to avoid loopback deadlocks, we do not
2911 * assign b_runningbufspace.
2912 */
984263bc
MD
2913 bp->b_flags |= B_WRITEINPROG;
2914 vfs_busy_pages(bp, 1);
2915
2916 /*
2917 * bp is protected by being locked, but nbp is not
2918 * and vfs_busy_pages() may sleep. We have to
2919 * recalculate nbp.
2920 */
2921 nbp = TAILQ_NEXT(bp, b_vnbufs);
2922
2923 /*
2924 * A list of these buffers is kept so that the
2925 * second loop knows which buffers have actually
2926 * been committed. This is necessary, since there
2927 * may be a race between the commit rpc and new
2928 * uncommitted writes on the file.
2929 */
2930 bvec[bvecpos++] = bp;
2931 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2932 bp->b_dirtyoff;
2933 if (toff < off)
2934 off = toff;
2935 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2936 if (toff > endoff)
2937 endoff = toff;
2938 }
2939 splx(s);
2940 }
2941 if (bvecpos > 0) {
2942 /*
3b568787
MD
2943 * Commit data on the server, as required. Note that
2944 * nfs_commit will use the vnode's cred for the commit.
984263bc 2945 */
3b568787 2946 retv = nfs_commit(vp, off, (int)(endoff - off), td);
984263bc
MD
2947
2948 if (retv == NFSERR_STALEWRITEVERF)
2949 nfs_clearcommit(vp->v_mount);
2950
2951 /*
2952 * Now, either mark the blocks I/O done or mark the
2953 * blocks dirty, depending on whether the commit
2954 * succeeded.
2955 */
2956 for (i = 0; i < bvecpos; i++) {
2957 bp = bvec[i];
2958 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2959 if (retv) {
2960 /*
2961 * Error, leave B_DELWRI intact
2962 */
2963 vfs_unbusy_pages(bp);
2964 brelse(bp);
2965 } else {
2966 /*
2967 * Success, remove B_DELWRI ( bundirty() ).
2968 *
2969 * b_dirtyoff/b_dirtyend seem to be NFS
2970 * specific. We should probably move that
2971 * into bundirty(). XXX
2972 */
2973 s = splbio();
2974 vp->v_numoutput++;
2975 bp->b_flags |= B_ASYNC;
2976 bundirty(bp);
2977 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2978 bp->b_dirtyoff = bp->b_dirtyend = 0;
2979 splx(s);
2980 biodone(bp);
2981 }
2982 }
2983 }
2984
2985 /*
2986 * Start/do any write(s) that are required.
2987 */
2988loop:
2989 s = splbio();
2990 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2991 nbp = TAILQ_NEXT(bp, b_vnbufs);
2992 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2993 if (waitfor != MNT_WAIT || passone)
2994 continue;
2995 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2996 "nfsfsync", slpflag, slptimeo);
2997 splx(s);
2998 if (error == 0)
2999 panic("nfs_fsync: inconsistent lock");
3000 if (error == ENOLCK)
3001 goto loop;
dadab5e9 3002 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
984263bc
MD
3003 error = EINTR;
3004 goto done;
3005 }
3006 if (slpflag == PCATCH) {
3007 slpflag = 0;
3008 slptimeo = 2 * hz;
3009 }
3010 goto loop;
3011 }
3012 if ((bp->b_flags & B_DELWRI) == 0)
3013 panic("nfs_fsync: not dirty");
3014 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3015 BUF_UNLOCK(bp);
3016 continue;
3017 }
3018 bremfree(bp);
3019 if (passone || !commit)
3020 bp->b_flags |= B_ASYNC;
3021 else
3022 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3023 splx(s);
3024 VOP_BWRITE(bp->b_vp, bp);
3025 goto loop;
3026 }
3027 splx(s);
3028 if (passone) {
3029 passone = 0;
3030 goto again;
3031 }
3032 if (waitfor == MNT_WAIT) {
3033 while (vp->v_numoutput) {
3034 vp->v_flag |= VBWAIT;
3035 error = tsleep((caddr_t)&vp->v_numoutput,
377d4740 3036 slpflag, "nfsfsync", slptimeo);
984263bc 3037 if (error) {
dadab5e9 3038 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
984263bc
MD
3039 error = EINTR;
3040 goto done;
3041 }
3042 if (slpflag == PCATCH) {
3043 slpflag = 0;
3044 slptimeo = 2 * hz;
3045 }
3046 }
3047 }
3048 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3049 goto loop;
3050 }
3051 }
3052 if (np->n_flag & NWRITEERR) {
3053 error = np->n_error;
3054 np->n_flag &= ~NWRITEERR;
3055 }
3056done:
3057 if (bvec != NULL && bvec != bvec_on_stack)
3058 free(bvec, M_TEMP);
3059 return (error);
3060}
3061
3062/*
3063 * NFS advisory byte-level locks.
3064 * Currently unsupported.
e851b29e
CP
3065 *
3066 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3067 * int a_flags)
984263bc
MD
3068 */
3069static int
e851b29e 3070nfs_advlock(struct vop_advlock_args *ap)
984263bc 3071{
40393ded 3072 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3073
3074 /*
3075 * The following kludge is to allow diskless support to work
3076 * until a real NFS lockd is implemented. Basically, just pretend
3077 * that this is a local lock.
3078 */
3079 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3080}
3081
3082/*
3083 * Print out the contents of an nfsnode.
e851b29e
CP
3084 *
3085 * nfs_print(struct vnode *a_vp)
984263bc
MD
3086 */
3087static int
e851b29e 3088nfs_print(struct vop_print_args *ap)
984263bc 3089{
40393ded
RG
3090 struct vnode *vp = ap->a_vp;
3091 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3092
3093 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3094 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3095 if (vp->v_type == VFIFO)
3096 fifo_printinfo(vp);
3097 printf("\n");
3098 return (0);
3099}
3100
3101/*
3102 * Just call nfs_writebp() with the force argument set to 1.
3103 *
3104 * NOTE: B_DONE may or may not be set in a_bp on call.
e851b29e
CP
3105 *
3106 * nfs_bwrite(struct vnode *a_bp)
984263bc
MD
3107 */
3108static int
e851b29e 3109nfs_bwrite(struct vop_bwrite_args *ap)
984263bc 3110{
dadab5e9 3111 return (nfs_writebp(ap->a_bp, 1, curthread));
984263bc
MD
3112}
3113
3114/*
3115 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3116 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3117 * B_CACHE if this is a VMIO buffer.
3118 */
3119int
e851b29e 3120nfs_writebp(struct buf *bp, int force, struct thread *td)
984263bc
MD
3121{
3122 int s;
3123 int oldflags = bp->b_flags;
3124#if 0
3125 int retv = 1;
3126 off_t off;
3127#endif
3128
3129 if (BUF_REFCNT(bp) == 0)
3130 panic("bwrite: buffer is not locked???");
3131
3132 if (bp->b_flags & B_INVAL) {
3133 brelse(bp);
3134 return(0);
3135 }
3136
3137 bp->b_flags |= B_CACHE;
3138
3139 /*
3140 * Undirty the bp. We will redirty it later if the I/O fails.
3141 */
3142
3143 s = splbio();
3144 bundirty(bp);
3145 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3146
3147 bp->b_vp->v_numoutput++;
984263bc
MD
3148 splx(s);
3149
3150 /*
3151 * Note: to avoid loopback deadlocks, we do not
3152 * assign b_runningbufspace.
3153 */
3154 vfs_busy_pages(bp, 1);
3155
3156 if (force)
3157 bp->b_flags |= B_WRITEINPROG;
3158 BUF_KERNPROC(bp);
3159 VOP_STRATEGY(bp->b_vp, bp);
3160
3161 if( (oldflags & B_ASYNC) == 0) {
3162 int rtval = biowait(bp);
3163
3164 if (oldflags & B_DELWRI) {
3165 s = splbio();
3166 reassignbuf(bp, bp->b_vp);
3167 splx(s);
3168 }
3169
3170 brelse(bp);
3171 return (rtval);
3172 }
3173
3174 return (0);
3175}
3176
3177/*
3178 * nfs special file access vnode op.
3179 * Essentially just get vattr and then imitate iaccess() since the device is
3180 * local to the client.
e851b29e
CP
3181 *
3182 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3183 * struct thread *a_td)
984263bc
MD
3184 */
3185static int
e851b29e 3186nfsspec_access(struct vop_access_args *ap)
984263bc 3187{
40393ded
RG
3188 struct vattr *vap;
3189 gid_t *gp;
3190 struct ucred *cred = ap->a_cred;
984263bc
MD
3191 struct vnode *vp = ap->a_vp;
3192 mode_t mode = ap->a_mode;
3193 struct vattr vattr;
40393ded 3194 int i;
984263bc
MD
3195 int error;
3196
3197 /*
3198 * Disallow write attempts on filesystems mounted read-only;
3199 * unless the file is a socket, fifo, or a block or character
3200 * device resident on the filesystem.
3201 */
3202 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3203 switch (vp->v_type) {
3204 case VREG:
3205 case VDIR:
3206 case VLNK:
3207 return (EROFS);
3208 default:
3209 break;
3210 }
3211 }
3212 /*
3213 * If you're the super-user,
3214 * you always get access.
3215 */
3216 if (cred->cr_uid == 0)
3217 return (0);
3218 vap = &vattr;
3b568787 3219 error = VOP_GETATTR(vp, vap, ap->a_td);
984263bc
MD
3220 if (error)
3221 return (error);
3222 /*
3223 * Access check is based on only one of owner, group, public.
3224 * If not owner, then check group. If not a member of the
3225 * group, then check public access.
3226 */
3227 if (cred->cr_uid != vap->va_uid) {
3228 mode >>= 3;
3229 gp = cred->cr_groups;
3230 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3231 if (vap->va_gid == *gp)
3232 goto found;
3233 mode >>= 3;
3234found:
3235 ;
3236 }
3237 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3238 return (error);
3239}
3240
3241/*
3242 * Read wrapper for special devices.
e851b29e
CP
3243 *
3244 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3245 * struct ucred *a_cred)
984263bc
MD
3246 */
3247static int
e851b29e 3248nfsspec_read(struct vop_read_args *ap)
984263bc 3249{
40393ded 3250 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3251
3252 /*
3253 * Set access flag.
3254 */
3255 np->n_flag |= NACC;
3256 getnanotime(&np->n_atim);
0961aa92 3257 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3258}
3259
3260/*
3261 * Write wrapper for special devices.
e851b29e
CP
3262 *
3263 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3264 * struct ucred *a_cred)
984263bc
MD
3265 */
3266static int
e851b29e 3267nfsspec_write(struct vop_write_args *ap)
984263bc 3268{
40393ded 3269 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3270
3271 /*
3272 * Set update flag.
3273 */
3274 np->n_flag |= NUPD;
3275 getnanotime(&np->n_mtim);
0961aa92 3276 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3277}
3278
3279/*
3280 * Close wrapper for special devices.
3281 *
3282 * Update the times on the nfsnode then do device close.
e851b29e
CP
3283 *
3284 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3285 * struct thread *a_td)
984263bc
MD
3286 */
3287static int
e851b29e 3288nfsspec_close(struct vop_close_args *ap)
984263bc 3289{
40393ded
RG
3290 struct vnode *vp = ap->a_vp;
3291 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3292 struct vattr vattr;
3293
3294 if (np->n_flag & (NACC | NUPD)) {
3295 np->n_flag |= NCHG;
3296 if (vp->v_usecount == 1 &&
3297 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3298 VATTR_NULL(&vattr);
3299 if (np->n_flag & NACC)
3300 vattr.va_atime = np->n_atim;
3301 if (np->n_flag & NUPD)
3302 vattr.va_mtime = np->n_mtim;
c1cf1e59 3303 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
984263bc
MD
3304 }
3305 }
0961aa92 3306 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3307}
3308
3309/*
3310 * Read wrapper for fifos.
e851b29e
CP
3311 *
3312 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3313 * struct ucred *a_cred)
984263bc
MD
3314 */
3315static int
e851b29e 3316nfsfifo_read(struct vop_read_args *ap)
984263bc 3317{
40393ded 3318 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3319
3320 /*
3321 * Set access flag.
3322 */
3323 np->n_flag |= NACC;
3324 getnanotime(&np->n_atim);
0961aa92 3325 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3326}
3327
3328/*
3329 * Write wrapper for fifos.
e851b29e
CP
3330 *
3331 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3332 * struct ucred *a_cred)
984263bc
MD
3333 */
3334static int
e851b29e 3335nfsfifo_write(struct vop_write_args *ap)
984263bc 3336{
40393ded 3337 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3338
3339 /*
3340 * Set update flag.
3341 */
3342 np->n_flag |= NUPD;
3343 getnanotime(&np->n_mtim);
0961aa92 3344 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3345}
3346
3347/*
3348 * Close wrapper for fifos.
3349 *
3350 * Update the times on the nfsnode then do fifo close.
e851b29e
CP
3351 *
3352 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
984263bc
MD
3353 */
3354static int
e851b29e 3355nfsfifo_close(struct vop_close_args *ap)
984263bc 3356{
40393ded
RG
3357 struct vnode *vp = ap->a_vp;
3358 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3359 struct vattr vattr;
3360 struct timespec ts;
3361
3362 if (np->n_flag & (NACC | NUPD)) {
3363 getnanotime(&ts);
3364 if (np->n_flag & NACC)
3365 np->n_atim = ts;
3366 if (np->n_flag & NUPD)
3367 np->n_mtim = ts;
3368 np->n_flag |= NCHG;
3369 if (vp->v_usecount == 1 &&
3370 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3371 VATTR_NULL(&vattr);
3372 if (np->n_flag & NACC)
3373 vattr.va_atime = np->n_atim;
3374 if (np->n_flag & NUPD)
3375 vattr.va_mtime = np->n_mtim;
c1cf1e59 3376 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
984263bc
MD
3377 }
3378 }
0961aa92 3379 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3380}
3381