Add first stab at Faroese locale.
[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 $
3446c007 38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.28 2004/08/28 19:02:20 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);
4d17b298
MD
860 error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
861 if (error != 0) {
984263bc
MD
862 struct vattr vattr;
863 int vpid;
864
4d17b298
MD
865 if (error == ENOENT && nfsneg_cache_timeout) {
866 *vpp = NULLVP;
867 return (error);
868 }
dadab5e9 869 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
984263bc
MD
870 *vpp = NULLVP;
871 return (error);
872 }
873
874 newvp = *vpp;
875 vpid = newvp->v_id;
876 /*
877 * See the comment starting `Step through' in ufs/ufs_lookup.c
878 * for an explanation of the locking protocol
879 */
880 if (dvp == newvp) {
597aea93 881 vref(newvp);
984263bc 882 error = 0;
2b69e610 883 } else if (flags & CNP_ISDOTDOT) {
41a01a4d
MD
884 VOP_UNLOCK(dvp, NULL, 0, td);
885 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
2b69e610 886 if (!error && lockparent && (flags & CNP_ISLASTCN))
41a01a4d 887 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
984263bc 888 } else {
41a01a4d 889 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
2b69e610 890 if (!lockparent || error || !(flags & CNP_ISLASTCN))
41a01a4d 891 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
892 }
893 if (!error) {
894 if (vpid == newvp->v_id) {
3b568787 895 if (!VOP_GETATTR(newvp, &vattr, td)
984263bc
MD
896 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
897 nfsstats.lookupcache_hits++;
2b69e610
MD
898 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
899 (flags & CNP_ISLASTCN))
900 cnp->cn_flags |= CNP_SAVENAME;
984263bc
MD
901 return (0);
902 }
903 cache_purge(newvp);
904 }
905 vput(newvp);
2b69e610 906 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
41a01a4d 907 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc 908 }
41a01a4d 909 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
984263bc
MD
910 *vpp = NULLVP;
911 if (error)
912 return (error);
913 }
4d17b298
MD
914
915 /*
916 * Cache miss, go the wire.
917 */
984263bc
MD
918 error = 0;
919 newvp = NULLVP;
920 nfsstats.lookupcache_misses++;
921 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
922 len = cnp->cn_namelen;
923 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
924 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
925 nfsm_fhtom(dvp, v3);
926 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
dadab5e9 927 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
984263bc 928 if (error) {
4d17b298
MD
929 /*
930 * Cache negatve lookups to reduce NFS traffic, but use
931 * a fast timeout.
932 */
933 if (error == ENOENT &&
934 (cnp->cn_flags & CNP_MAKEENTRY) &&
935 cnp->cn_nameiop == NAMEI_LOOKUP &&
936 nfsneg_cache_timeout) {
937 int toval = nfsneg_cache_timeout * hz;
938 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
939 if (cnp->cn_timeout > toval)
940 cnp->cn_timeout = toval;
941 } else {
942 cnp->cn_flags |= CNP_CACHETIMEOUT;
943 cnp->cn_timeout = toval;
944 }
945 cache_enter(dvp, NCPNULL, NULL, cnp);
946 }
984263bc
MD
947 nfsm_postop_attr(dvp, attrflag);
948 m_freem(mrep);
949 goto nfsmout;
950 }
951 nfsm_getfh(fhp, fhsize, v3);
952
953 /*
954 * Handle RENAME case...
955 */
2b69e610 956 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
984263bc
MD
957 if (NFS_CMPFH(np, fhp, fhsize)) {
958 m_freem(mrep);
959 return (EISDIR);
960 }
961 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
962 if (error) {
963 m_freem(mrep);
964 return (error);
965 }
966 newvp = NFSTOV(np);
967 if (v3) {
968 nfsm_postop_attr(newvp, attrflag);
969 nfsm_postop_attr(dvp, attrflag);
970 } else
971 nfsm_loadattr(newvp, (struct vattr *)0);
972 *vpp = newvp;
973 m_freem(mrep);
2b69e610 974 cnp->cn_flags |= CNP_SAVENAME;
984263bc 975 if (!lockparent)
41a01a4d 976 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
977 return (0);
978 }
979
2b69e610 980 if (flags & CNP_ISDOTDOT) {
41a01a4d 981 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
982 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
983 if (error) {
41a01a4d 984 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
985 return (error);
986 }
987 newvp = NFSTOV(np);
2b69e610 988 if (lockparent && (flags & CNP_ISLASTCN) &&
41a01a4d 989 (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
984263bc
MD
990 vput(newvp);
991 return (error);
992 }
993 } else if (NFS_CMPFH(np, fhp, fhsize)) {
597aea93 994 vref(dvp);
984263bc
MD
995 newvp = dvp;
996 } else {
997 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
998 if (error) {
999 m_freem(mrep);
1000 return (error);
1001 }
2b69e610 1002 if (!lockparent || !(flags & CNP_ISLASTCN))
41a01a4d 1003 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
1004 newvp = NFSTOV(np);
1005 }
1006 if (v3) {
1007 nfsm_postop_attr(newvp, attrflag);
1008 nfsm_postop_attr(dvp, attrflag);
1009 } else
1010 nfsm_loadattr(newvp, (struct vattr *)0);
2b69e610
MD
1011 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1012 cnp->cn_flags |= CNP_SAVENAME;
1013 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1014 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
984263bc 1015 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
bc0c094e 1016 cache_enter(dvp, NCPNULL, newvp, cnp);
984263bc
MD
1017 }
1018 *vpp = newvp;
6b08710e
MD
1019 m_freem(mrep);
1020nfsmout:
984263bc
MD
1021 if (error) {
1022 if (newvp != NULLVP) {
1023 vrele(newvp);
1024 *vpp = NULLVP;
1025 }
2b69e610
MD
1026 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1027 (flags & CNP_ISLASTCN) && error == ENOENT) {
984263bc 1028 if (!lockparent)
41a01a4d 1029 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
1030 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1031 error = EROFS;
1032 else
1033 error = EJUSTRETURN;
1034 }
2b69e610
MD
1035 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1036 cnp->cn_flags |= CNP_SAVENAME;
984263bc
MD
1037 }
1038 return (error);
1039}
1040
1041/*
1042 * nfs read call.
1043 * Just call nfs_bioread() to do the work.
e851b29e
CP
1044 *
1045 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1046 * struct ucred *a_cred)
984263bc
MD
1047 */
1048static int
e851b29e 1049nfs_read(struct vop_read_args *ap)
984263bc 1050{
40393ded 1051 struct vnode *vp = ap->a_vp;
984263bc 1052
3b568787 1053 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
ca3a2b2f
HP
1054 switch (vp->v_type) {
1055 case VREG:
1056 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1057 case VDIR:
1058 return (EISDIR);
1059 default:
1060 return EOPNOTSUPP;
1061 }
984263bc
MD
1062}
1063
1064/*
1065 * nfs readlink call
e851b29e
CP
1066 *
1067 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
984263bc
MD
1068 */
1069static int
e851b29e 1070nfs_readlink(struct vop_readlink_args *ap)
984263bc 1071{
40393ded 1072 struct vnode *vp = ap->a_vp;
984263bc
MD
1073
1074 if (vp->v_type != VLNK)
1075 return (EINVAL);
3b568787 1076 return (nfs_bioread(vp, ap->a_uio, 0));
984263bc
MD
1077}
1078
1079/*
1080 * Do a readlink rpc.
1081 * Called by nfs_doio() from below the buffer cache.
1082 */
1083int
3b568787 1084nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
984263bc 1085{
40393ded
RG
1086 u_int32_t *tl;
1087 caddr_t cp;
1088 int32_t t1, t2;
984263bc
MD
1089 caddr_t bpos, dpos, cp2;
1090 int error = 0, len, attrflag;
1091 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1092 int v3 = NFS_ISV3(vp);
1093
1094 nfsstats.rpccnt[NFSPROC_READLINK]++;
1095 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1096 nfsm_fhtom(vp, v3);
c1cf1e59 1097 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
984263bc
MD
1098 if (v3)
1099 nfsm_postop_attr(vp, attrflag);
1100 if (!error) {
1101 nfsm_strsiz(len, NFS_MAXPATHLEN);
1102 if (len == NFS_MAXPATHLEN) {
1103 struct nfsnode *np = VTONFS(vp);
1104 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1105 len = np->n_size;
1106 }
1107 nfsm_mtouio(uiop, len);
1108 }
6b08710e
MD
1109 m_freem(mrep);
1110nfsmout:
984263bc
MD
1111 return (error);
1112}
1113
1114/*
1115 * nfs read rpc call
1116 * Ditto above
1117 */
1118int
3b568787 1119nfs_readrpc(struct vnode *vp, struct uio *uiop)
984263bc 1120{
40393ded
RG
1121 u_int32_t *tl;
1122 caddr_t cp;
1123 int32_t t1, t2;
984263bc
MD
1124 caddr_t bpos, dpos, cp2;
1125 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1126 struct nfsmount *nmp;
1127 int error = 0, len, retlen, tsiz, eof, attrflag;
1128 int v3 = NFS_ISV3(vp);
1129
1130#ifndef nolint
1131 eof = 0;
1132#endif
1133 nmp = VFSTONFS(vp->v_mount);
1134 tsiz = uiop->uio_resid;
1135 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1136 return (EFBIG);
1137 while (tsiz > 0) {
1138 nfsstats.rpccnt[NFSPROC_READ]++;
1139 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1140 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1141 nfsm_fhtom(vp, v3);
1142 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1143 if (v3) {
1144 txdr_hyper(uiop->uio_offset, tl);
1145 *(tl + 2) = txdr_unsigned(len);
1146 } else {
1147 *tl++ = txdr_unsigned(uiop->uio_offset);
1148 *tl++ = txdr_unsigned(len);
1149 *tl = 0;
1150 }
c1cf1e59 1151 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
984263bc
MD
1152 if (v3) {
1153 nfsm_postop_attr(vp, attrflag);
1154 if (error) {
1155 m_freem(mrep);
1156 goto nfsmout;
1157 }
1158 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1159 eof = fxdr_unsigned(int, *(tl + 1));
1160 } else
1161 nfsm_loadattr(vp, (struct vattr *)0);
1162 nfsm_strsiz(retlen, nmp->nm_rsize);
1163 nfsm_mtouio(uiop, retlen);
1164 m_freem(mrep);
1165 tsiz -= retlen;
1166 if (v3) {
1167 if (eof || retlen == 0) {
1168 tsiz = 0;
1169 }
1170 } else if (retlen < len) {
1171 tsiz = 0;
1172 }
1173 }
1174nfsmout:
1175 return (error);
1176}
1177
1178/*
1179 * nfs write call
1180 */
1181int
e851b29e 1182nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
984263bc 1183{
40393ded
RG
1184 u_int32_t *tl;
1185 caddr_t cp;
1186 int32_t t1, t2, backup;
984263bc
MD
1187 caddr_t bpos, dpos, cp2;
1188 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1189 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1190 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1191 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1192
1193#ifndef DIAGNOSTIC
1194 if (uiop->uio_iovcnt != 1)
1195 panic("nfs: writerpc iovcnt > 1");
1196#endif
1197 *must_commit = 0;
1198 tsiz = uiop->uio_resid;
1199 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1200 return (EFBIG);
1201 while (tsiz > 0) {
1202 nfsstats.rpccnt[NFSPROC_WRITE]++;
1203 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1204 nfsm_reqhead(vp, NFSPROC_WRITE,
1205 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1206 nfsm_fhtom(vp, v3);
1207 if (v3) {
1208 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1209 txdr_hyper(uiop->uio_offset, tl);
1210 tl += 2;
1211 *tl++ = txdr_unsigned(len);
1212 *tl++ = txdr_unsigned(*iomode);
1213 *tl = txdr_unsigned(len);
1214 } else {
40393ded 1215 u_int32_t x;
984263bc
MD
1216
1217 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1218 /* Set both "begin" and "current" to non-garbage. */
1219 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1220 *tl++ = x; /* "begin offset" */
1221 *tl++ = x; /* "current offset" */
1222 x = txdr_unsigned(len);
1223 *tl++ = x; /* total to this offset */
1224 *tl = x; /* size of this write */
1225 }
1226 nfsm_uiotom(uiop, len);
c1cf1e59 1227 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
984263bc
MD
1228 if (v3) {
1229 wccflag = NFSV3_WCCCHK;
1230 nfsm_wcc_data(vp, wccflag);
1231 if (!error) {
1232 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1233 + NFSX_V3WRITEVERF);
1234 rlen = fxdr_unsigned(int, *tl++);
1235 if (rlen == 0) {
1236 error = NFSERR_IO;
1237 m_freem(mrep);
1238 break;
1239 } else if (rlen < len) {
1240 backup = len - rlen;
1241 uiop->uio_iov->iov_base -= backup;
1242 uiop->uio_iov->iov_len += backup;
1243 uiop->uio_offset -= backup;
1244 uiop->uio_resid += backup;
1245 len = rlen;
1246 }
1247 commit = fxdr_unsigned(int, *tl++);
1248
1249 /*
1250 * Return the lowest committment level
1251 * obtained by any of the RPCs.
1252 */
1253 if (committed == NFSV3WRITE_FILESYNC)
1254 committed = commit;
1255 else if (committed == NFSV3WRITE_DATASYNC &&
1256 commit == NFSV3WRITE_UNSTABLE)
1257 committed = commit;
1258 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1259 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1260 NFSX_V3WRITEVERF);
1261 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1262 } else if (bcmp((caddr_t)tl,
1263 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1264 *must_commit = 1;
1265 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1266 NFSX_V3WRITEVERF);
1267 }
1268 }
1269 } else
1270 nfsm_loadattr(vp, (struct vattr *)0);
1271 if (wccflag)
1272 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1273 m_freem(mrep);
1274 if (error)
1275 break;
1276 tsiz -= len;
1277 }
1278nfsmout:
1279 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1280 committed = NFSV3WRITE_FILESYNC;
1281 *iomode = committed;
1282 if (error)
1283 uiop->uio_resid = tsiz;
1284 return (error);
1285}
1286
1287/*
1288 * nfs mknod rpc
1289 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1290 * mode set to specify the file type and the size field for rdev.
1291 */
1292static int
e851b29e
CP
1293nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1294 struct vattr *vap)
984263bc 1295{
40393ded
RG
1296 struct nfsv2_sattr *sp;
1297 u_int32_t *tl;
1298 caddr_t cp;
1299 int32_t t1, t2;
984263bc
MD
1300 struct vnode *newvp = (struct vnode *)0;
1301 struct nfsnode *np = (struct nfsnode *)0;
1302 struct vattr vattr;
1303 char *cp2;
1304 caddr_t bpos, dpos;
1305 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1306 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1307 u_int32_t rdev;
1308 int v3 = NFS_ISV3(dvp);
1309
1310 if (vap->va_type == VCHR || vap->va_type == VBLK)
1311 rdev = txdr_unsigned(vap->va_rdev);
1312 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1313 rdev = nfs_xdrneg1;
1314 else {
1315 return (EOPNOTSUPP);
1316 }
3b568787 1317 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
984263bc
MD
1318 return (error);
1319 }
1320 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1321 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1322 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1323 nfsm_fhtom(dvp, v3);
1324 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1325 if (v3) {
1326 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1327 *tl++ = vtonfsv3_type(vap->va_type);
1328 nfsm_v3attrbuild(vap, FALSE);
1329 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1330 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1331 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1332 *tl = txdr_unsigned(uminor(vap->va_rdev));
1333 }
1334 } else {
1335 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1336 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1337 sp->sa_uid = nfs_xdrneg1;
1338 sp->sa_gid = nfs_xdrneg1;
1339 sp->sa_size = rdev;
1340 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1341 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1342 }
dadab5e9 1343 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1344 if (!error) {
1345 nfsm_mtofh(dvp, newvp, v3, gotvp);
1346 if (!gotvp) {
1347 if (newvp) {
1348 vput(newvp);
1349 newvp = (struct vnode *)0;
1350 }
1351 error = nfs_lookitup(dvp, cnp->cn_nameptr,
dadab5e9 1352 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1353 if (!error)
1354 newvp = NFSTOV(np);
1355 }
1356 }
1357 if (v3)
1358 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
1359 m_freem(mrep);
1360nfsmout:
984263bc
MD
1361 if (error) {
1362 if (newvp)
1363 vput(newvp);
1364 } else {
2b69e610 1365 if (cnp->cn_flags & CNP_MAKEENTRY)
bc0c094e 1366 cache_enter(dvp, NCPNULL, newvp, cnp);
984263bc
MD
1367 *vpp = newvp;
1368 }
1369 VTONFS(dvp)->n_flag |= NMODIFIED;
1370 if (!wccflag)
1371 VTONFS(dvp)->n_attrstamp = 0;
1372 return (error);
1373}
1374
1375/*
1376 * nfs mknod vop
1377 * just call nfs_mknodrpc() to do the work.
e851b29e
CP
1378 *
1379 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1380 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1381 */
1382/* ARGSUSED */
1383static int
e851b29e 1384nfs_mknod(struct vop_mknod_args *ap)
984263bc
MD
1385{
1386 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1387}
1388
1389static u_long create_verf;
1390/*
1391 * nfs file create call
e851b29e
CP
1392 *
1393 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1394 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1395 */
1396static int
e851b29e 1397nfs_create(struct vop_create_args *ap)
984263bc 1398{
40393ded
RG
1399 struct vnode *dvp = ap->a_dvp;
1400 struct vattr *vap = ap->a_vap;
1401 struct componentname *cnp = ap->a_cnp;
1402 struct nfsv2_sattr *sp;
1403 u_int32_t *tl;
1404 caddr_t cp;
1405 int32_t t1, t2;
984263bc
MD
1406 struct nfsnode *np = (struct nfsnode *)0;
1407 struct vnode *newvp = (struct vnode *)0;
1408 caddr_t bpos, dpos, cp2;
1409 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1410 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1411 struct vattr vattr;
1412 int v3 = NFS_ISV3(dvp);
1413
1414 /*
1415 * Oops, not for me..
1416 */
1417 if (vap->va_type == VSOCK)
1418 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1419
3b568787 1420 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
984263bc
MD
1421 return (error);
1422 }
1423 if (vap->va_vaflags & VA_EXCLUSIVE)
1424 fmode |= O_EXCL;
1425again:
1426 nfsstats.rpccnt[NFSPROC_CREATE]++;
1427 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1428 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1429 nfsm_fhtom(dvp, v3);
1430 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1431 if (v3) {
1432 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1433 if (fmode & O_EXCL) {
1434 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1435 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1436#ifdef INET
1437 if (!TAILQ_EMPTY(&in_ifaddrhead))
ecd80f47 1438 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
984263bc
MD
1439 else
1440#endif
1441 *tl++ = create_verf;
1442 *tl = ++create_verf;
1443 } else {
1444 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1445 nfsm_v3attrbuild(vap, FALSE);
1446 }
1447 } else {
1448 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1449 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1450 sp->sa_uid = nfs_xdrneg1;
1451 sp->sa_gid = nfs_xdrneg1;
1452 sp->sa_size = 0;
1453 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1454 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1455 }
dadab5e9 1456 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1457 if (!error) {
1458 nfsm_mtofh(dvp, newvp, v3, gotvp);
1459 if (!gotvp) {
1460 if (newvp) {
1461 vput(newvp);
1462 newvp = (struct vnode *)0;
1463 }
1464 error = nfs_lookitup(dvp, cnp->cn_nameptr,
dadab5e9 1465 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1466 if (!error)
1467 newvp = NFSTOV(np);
1468 }
1469 }
1470 if (v3)
1471 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
1472 m_freem(mrep);
1473nfsmout:
984263bc
MD
1474 if (error) {
1475 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1476 fmode &= ~O_EXCL;
1477 goto again;
1478 }
1479 if (newvp)
1480 vput(newvp);
1481 } else if (v3 && (fmode & O_EXCL)) {
1482 /*
1483 * We are normally called with only a partially initialized
1484 * VAP. Since the NFSv3 spec says that server may use the
1485 * file attributes to store the verifier, the spec requires
1486 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1487 * in atime, but we can't really assume that all servers will
1488 * so we ensure that our SETATTR sets both atime and mtime.
1489 */
1490 if (vap->va_mtime.tv_sec == VNOVAL)
1491 vfs_timestamp(&vap->va_mtime);
1492 if (vap->va_atime.tv_sec == VNOVAL)
1493 vap->va_atime = vap->va_mtime;
dadab5e9 1494 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
984263bc
MD
1495 }
1496 if (!error) {
2b69e610 1497 if (cnp->cn_flags & CNP_MAKEENTRY)
bc0c094e 1498 cache_enter(dvp, NCPNULL, newvp, cnp);
c1cf1e59
MD
1499 /*
1500 * The new np may have enough info for access
1501 * checks, make sure rucred and wucred are
1502 * initialized for read and write rpc's.
1503 */
1504 np = VTONFS(newvp);
1505 if (np->n_rucred == NULL)
1506 np->n_rucred = crhold(cnp->cn_cred);
1507 if (np->n_wucred == NULL)
1508 np->n_wucred = crhold(cnp->cn_cred);
984263bc
MD
1509 *ap->a_vpp = newvp;
1510 }
1511 VTONFS(dvp)->n_flag |= NMODIFIED;
1512 if (!wccflag)
1513 VTONFS(dvp)->n_attrstamp = 0;
1514 return (error);
1515}
1516
1517/*
1518 * nfs file remove call
1519 * To try and make nfs semantics closer to ufs semantics, a file that has
1520 * other processes using the vnode is renamed instead of removed and then
1521 * removed later on the last close.
1522 * - If v_usecount > 1
1523 * If a rename is not already in the works
1524 * call nfs_sillyrename() to set it up
1525 * else
1526 * do the remove rpc
e851b29e
CP
1527 *
1528 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1529 * struct vnode *a_vp, struct componentname *a_cnp)
984263bc
MD
1530 */
1531static int
e851b29e 1532nfs_remove(struct vop_remove_args *ap)
984263bc 1533{
40393ded
RG
1534 struct vnode *vp = ap->a_vp;
1535 struct vnode *dvp = ap->a_dvp;
1536 struct componentname *cnp = ap->a_cnp;
1537 struct nfsnode *np = VTONFS(vp);
984263bc
MD
1538 int error = 0;
1539 struct vattr vattr;
1540
1541#ifndef DIAGNOSTIC
2b69e610 1542 if ((cnp->cn_flags & CNP_HASBUF) == 0)
984263bc
MD
1543 panic("nfs_remove: no name");
1544 if (vp->v_usecount < 1)
1545 panic("nfs_remove: bad v_usecount");
1546#endif
1547 if (vp->v_type == VDIR)
1548 error = EPERM;
1549 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
3b568787 1550 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
984263bc
MD
1551 vattr.va_nlink > 1)) {
1552 /*
1553 * Purge the name cache so that the chance of a lookup for
1554 * the name succeeding while the remove is in progress is
1555 * minimized. Without node locking it can still happen, such
1556 * that an I/O op returns ESTALE, but since you get this if
1557 * another host removes the file..
1558 */
1559 cache_purge(vp);
1560 /*
1561 * throw away biocache buffers, mainly to avoid
1562 * unnecessary delayed writes later.
1563 */
3b568787 1564 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
984263bc
MD
1565 /* Do the rpc */
1566 if (error != EINTR)
1567 error = nfs_removerpc(dvp, cnp->cn_nameptr,
dadab5e9 1568 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
984263bc
MD
1569 /*
1570 * Kludge City: If the first reply to the remove rpc is lost..
1571 * the reply to the retransmitted request will be ENOENT
1572 * since the file was in fact removed
1573 * Therefore, we cheat and return success.
1574 */
1575 if (error == ENOENT)
1576 error = 0;
1577 } else if (!np->n_sillyrename)
1578 error = nfs_sillyrename(dvp, vp, cnp);
1579 np->n_attrstamp = 0;
1580 return (error);
1581}
1582
1583/*
1584 * nfs file remove rpc called from nfs_inactive
1585 */
1586int
dadab5e9 1587nfs_removeit(struct sillyrename *sp)
984263bc 1588{
dadab5e9
MD
1589 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1590 sp->s_cred, NULL));
984263bc
MD
1591}
1592
1593/*
1594 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1595 */
1596static int
e851b29e
CP
1597nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1598 struct ucred *cred, struct thread *td)
984263bc 1599{
40393ded
RG
1600 u_int32_t *tl;
1601 caddr_t cp;
1602 int32_t t1, t2;
984263bc
MD
1603 caddr_t bpos, dpos, cp2;
1604 int error = 0, wccflag = NFSV3_WCCRATTR;
1605 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1606 int v3 = NFS_ISV3(dvp);
1607
1608 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1609 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1610 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1611 nfsm_fhtom(dvp, v3);
1612 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
dadab5e9 1613 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
984263bc
MD
1614 if (v3)
1615 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
1616 m_freem(mrep);
1617nfsmout:
984263bc
MD
1618 VTONFS(dvp)->n_flag |= NMODIFIED;
1619 if (!wccflag)
1620 VTONFS(dvp)->n_attrstamp = 0;
1621 return (error);
1622}
1623
1624/*
1625 * nfs file rename call
e851b29e
CP
1626 *
1627 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1628 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1629 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
1630 */
1631static int
e851b29e 1632nfs_rename(struct vop_rename_args *ap)
984263bc 1633{
40393ded
RG
1634 struct vnode *fvp = ap->a_fvp;
1635 struct vnode *tvp = ap->a_tvp;
1636 struct vnode *fdvp = ap->a_fdvp;
1637 struct vnode *tdvp = ap->a_tdvp;
1638 struct componentname *tcnp = ap->a_tcnp;
1639 struct componentname *fcnp = ap->a_fcnp;
984263bc
MD
1640 int error;
1641
1642#ifndef DIAGNOSTIC
2b69e610
MD
1643 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1644 (fcnp->cn_flags & CNP_HASBUF) == 0)
984263bc
MD
1645 panic("nfs_rename: no name");
1646#endif
1647 /* Check for cross-device rename */
1648 if ((fvp->v_mount != tdvp->v_mount) ||
1649 (tvp && (fvp->v_mount != tvp->v_mount))) {
1650 error = EXDEV;
1651 goto out;
1652 }
1653
1654 /*
1655 * We have to flush B_DELWRI data prior to renaming
1656 * the file. If we don't, the delayed-write buffers
1657 * can be flushed out later after the file has gone stale
1658 * under NFSV3. NFSV2 does not have this problem because
1659 * ( as far as I can tell ) it flushes dirty buffers more
1660 * often.
1661 */
1662
3b568787 1663 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
984263bc 1664 if (tvp)
3b568787 1665 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
984263bc
MD
1666
1667 /*
1668 * If the tvp exists and is in use, sillyrename it before doing the
1669 * rename of the new file over it.
1670 * XXX Can't sillyrename a directory.
1671 */
1672 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1673 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1674 vput(tvp);
1675 tvp = NULL;
1676 }
1677
1678 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1679 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
dadab5e9 1680 tcnp->cn_td);
984263bc
MD
1681
1682 if (fvp->v_type == VDIR) {
1683 if (tvp != NULL && tvp->v_type == VDIR)
1684 cache_purge(tdvp);
1685 cache_purge(fdvp);
1686 }
1687
1688out:
1689 if (tdvp == tvp)
1690 vrele(tdvp);
1691 else
1692 vput(tdvp);
1693 if (tvp)
1694 vput(tvp);
1695 vrele(fdvp);
1696 vrele(fvp);
1697 /*
1698 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1699 */
1700 if (error == ENOENT)
1701 error = 0;
1702 return (error);
1703}
1704
1705/*
1706 * nfs file rename rpc called from nfs_remove() above
1707 */
1708static int
e851b29e
CP
1709nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1710 struct sillyrename *sp)
984263bc
MD
1711{
1712 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
dadab5e9 1713 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
984263bc
MD
1714}
1715
1716/*
1717 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1718 */
1719static int
e851b29e
CP
1720nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1721 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1722 struct ucred *cred, struct thread *td)
984263bc 1723{
40393ded
RG
1724 u_int32_t *tl;
1725 caddr_t cp;
1726 int32_t t1, t2;
984263bc
MD
1727 caddr_t bpos, dpos, cp2;
1728 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1729 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1730 int v3 = NFS_ISV3(fdvp);
1731
1732 nfsstats.rpccnt[NFSPROC_RENAME]++;
1733 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1734 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1735 nfsm_rndup(tnamelen));
1736 nfsm_fhtom(fdvp, v3);
1737 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1738 nfsm_fhtom(tdvp, v3);
1739 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
dadab5e9 1740 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
984263bc
MD
1741 if (v3) {
1742 nfsm_wcc_data(fdvp, fwccflag);
1743 nfsm_wcc_data(tdvp, twccflag);
1744 }
6b08710e
MD
1745 m_freem(mrep);
1746nfsmout:
984263bc
MD
1747 VTONFS(fdvp)->n_flag |= NMODIFIED;
1748 VTONFS(tdvp)->n_flag |= NMODIFIED;
1749 if (!fwccflag)
1750 VTONFS(fdvp)->n_attrstamp = 0;
1751 if (!twccflag)
1752 VTONFS(tdvp)->n_attrstamp = 0;
1753 return (error);
1754}
1755
1756/*
1757 * nfs hard link create call
e851b29e
CP
1758 *
1759 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1760 * struct componentname *a_cnp)
984263bc
MD
1761 */
1762static int
e851b29e 1763nfs_link(struct vop_link_args *ap)
984263bc 1764{
40393ded
RG
1765 struct vnode *vp = ap->a_vp;
1766 struct vnode *tdvp = ap->a_tdvp;
1767 struct componentname *cnp = ap->a_cnp;
1768 u_int32_t *tl;
1769 caddr_t cp;
1770 int32_t t1, t2;
984263bc
MD
1771 caddr_t bpos, dpos, cp2;
1772 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1773 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1774 int v3;
1775
1776 if (vp->v_mount != tdvp->v_mount) {
1777 return (EXDEV);
1778 }
1779
1780 /*
1781 * Push all writes to the server, so that the attribute cache
1782 * doesn't get "out of sync" with the server.
1783 * XXX There should be a better way!
1784 */
3b568787 1785 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
984263bc
MD
1786
1787 v3 = NFS_ISV3(vp);
1788 nfsstats.rpccnt[NFSPROC_LINK]++;
1789 nfsm_reqhead(vp, NFSPROC_LINK,
1790 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1791 nfsm_fhtom(vp, v3);
1792 nfsm_fhtom(tdvp, v3);
1793 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
dadab5e9 1794 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1795 if (v3) {
1796 nfsm_postop_attr(vp, attrflag);
1797 nfsm_wcc_data(tdvp, wccflag);
1798 }
6b08710e
MD
1799 m_freem(mrep);
1800nfsmout:
984263bc
MD
1801 VTONFS(tdvp)->n_flag |= NMODIFIED;
1802 if (!attrflag)
1803 VTONFS(vp)->n_attrstamp = 0;
1804 if (!wccflag)
1805 VTONFS(tdvp)->n_attrstamp = 0;
1806 /*
1807 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1808 */
1809 if (error == EEXIST)
1810 error = 0;
1811 return (error);
1812}
1813
1814/*
1815 * nfs symbolic link create call
e851b29e
CP
1816 *
1817 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1818 * struct componentname *a_cnp, struct vattr *a_vap,
1819 * char *a_target)
984263bc
MD
1820 */
1821static int
e851b29e 1822nfs_symlink(struct vop_symlink_args *ap)
984263bc 1823{
40393ded
RG
1824 struct vnode *dvp = ap->a_dvp;
1825 struct vattr *vap = ap->a_vap;
1826 struct componentname *cnp = ap->a_cnp;
1827 struct nfsv2_sattr *sp;
1828 u_int32_t *tl;
1829 caddr_t cp;
1830 int32_t t1, t2;
984263bc
MD
1831 caddr_t bpos, dpos, cp2;
1832 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1833 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1834 struct vnode *newvp = (struct vnode *)0;
1835 int v3 = NFS_ISV3(dvp);
1836
1837 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1838 slen = strlen(ap->a_target);
1839 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1840 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1841 nfsm_fhtom(dvp, v3);
1842 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1843 if (v3) {
1844 nfsm_v3attrbuild(vap, FALSE);
1845 }
1846 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1847 if (!v3) {
1848 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1849 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1850 sp->sa_uid = nfs_xdrneg1;
1851 sp->sa_gid = nfs_xdrneg1;
1852 sp->sa_size = nfs_xdrneg1;
1853 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1854 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1855 }
1856
1857 /*
1858 * Issue the NFS request and get the rpc response.
1859 *
1860 * Only NFSv3 responses returning an error of 0 actually return
1861 * a file handle that can be converted into newvp without having
1862 * to do an extra lookup rpc.
1863 */
dadab5e9 1864 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1865 if (v3) {
1866 if (error == 0)
1867 nfsm_mtofh(dvp, newvp, v3, gotvp);
1868 nfsm_wcc_data(dvp, wccflag);
1869 }
1870
1871 /*
1872 * out code jumps -> here, mrep is also freed.
1873 */
1874
6b08710e
MD
1875 m_freem(mrep);
1876nfsmout:
984263bc
MD
1877
1878 /*
1879 * If we get an EEXIST error, silently convert it to no-error
1880 * in case of an NFS retry.
1881 */
1882 if (error == EEXIST)
1883 error = 0;
1884
1885 /*
1886 * If we do not have (or no longer have) an error, and we could
1887 * not extract the newvp from the response due to the request being
1888 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1889 * to obtain a newvp to return.
1890 */
1891 if (error == 0 && newvp == NULL) {
1892 struct nfsnode *np = NULL;
1893
1894 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
dadab5e9 1895 cnp->cn_cred, cnp->cn_td, &np);
984263bc
MD
1896 if (!error)
1897 newvp = NFSTOV(np);
1898 }
1899 if (error) {
1900 if (newvp)
1901 vput(newvp);
1902 } else {
1903 *ap->a_vpp = newvp;
1904 }
1905 VTONFS(dvp)->n_flag |= NMODIFIED;
1906 if (!wccflag)
1907 VTONFS(dvp)->n_attrstamp = 0;
1908 return (error);
1909}
1910
1911/*
1912 * nfs make dir call
e851b29e
CP
1913 *
1914 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1915 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
1916 */
1917static int
e851b29e 1918nfs_mkdir(struct vop_mkdir_args *ap)
984263bc 1919{
40393ded
RG
1920 struct vnode *dvp = ap->a_dvp;
1921 struct vattr *vap = ap->a_vap;
1922 struct componentname *cnp = ap->a_cnp;
1923 struct nfsv2_sattr *sp;
1924 u_int32_t *tl;
1925 caddr_t cp;
1926 int32_t t1, t2;
1927 int len;
984263bc
MD
1928 struct nfsnode *np = (struct nfsnode *)0;
1929 struct vnode *newvp = (struct vnode *)0;
1930 caddr_t bpos, dpos, cp2;
1931 int error = 0, wccflag = NFSV3_WCCRATTR;
1932 int gotvp = 0;
1933 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1934 struct vattr vattr;
1935 int v3 = NFS_ISV3(dvp);
1936
3b568787 1937 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
984263bc
MD
1938 return (error);
1939 }
1940 len = cnp->cn_namelen;
1941 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1942 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1943 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1944 nfsm_fhtom(dvp, v3);
1945 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1946 if (v3) {
1947 nfsm_v3attrbuild(vap, FALSE);
1948 } else {
1949 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1950 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1951 sp->sa_uid = nfs_xdrneg1;
1952 sp->sa_gid = nfs_xdrneg1;
1953 sp->sa_size = nfs_xdrneg1;
1954 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1955 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1956 }
dadab5e9 1957 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
984263bc
MD
1958 if (!error)
1959 nfsm_mtofh(dvp, newvp, v3, gotvp);
1960 if (v3)
1961 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
1962 m_freem(mrep);
1963nfsmout:
984263bc
MD
1964 VTONFS(dvp)->n_flag |= NMODIFIED;
1965 if (!wccflag)
1966 VTONFS(dvp)->n_attrstamp = 0;
1967 /*
1968 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1969 * if we can succeed in looking up the directory.
1970 */
1971 if (error == EEXIST || (!error && !gotvp)) {
1972 if (newvp) {
1973 vrele(newvp);
1974 newvp = (struct vnode *)0;
1975 }
1976 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
dadab5e9 1977 cnp->cn_td, &np);
984263bc
MD
1978 if (!error) {
1979 newvp = NFSTOV(np);
1980 if (newvp->v_type != VDIR)
1981 error = EEXIST;
1982 }
1983 }
1984 if (error) {
1985 if (newvp)
1986 vrele(newvp);
1987 } else
1988 *ap->a_vpp = newvp;
1989 return (error);
1990}
1991
1992/*
1993 * nfs remove directory call
e851b29e
CP
1994 *
1995 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1996 * struct componentname *a_cnp)
984263bc
MD
1997 */
1998static int
e851b29e 1999nfs_rmdir(struct vop_rmdir_args *ap)
984263bc 2000{
40393ded
RG
2001 struct vnode *vp = ap->a_vp;
2002 struct vnode *dvp = ap->a_dvp;
2003 struct componentname *cnp = ap->a_cnp;
2004 u_int32_t *tl;
2005 caddr_t cp;
2006 int32_t t1, t2;
984263bc
MD
2007 caddr_t bpos, dpos, cp2;
2008 int error = 0, wccflag = NFSV3_WCCRATTR;
2009 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2010 int v3 = NFS_ISV3(dvp);
2011
2012 if (dvp == vp)
2013 return (EINVAL);
2014 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2015 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2016 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2017 nfsm_fhtom(dvp, v3);
2018 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
dadab5e9 2019 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
984263bc
MD
2020 if (v3)
2021 nfsm_wcc_data(dvp, wccflag);
6b08710e
MD
2022 m_freem(mrep);
2023nfsmout:
984263bc
MD
2024 VTONFS(dvp)->n_flag |= NMODIFIED;
2025 if (!wccflag)
2026 VTONFS(dvp)->n_attrstamp = 0;
2027 cache_purge(dvp);
2028 cache_purge(vp);
2029 /*
2030 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2031 */
2032 if (error == ENOENT)
2033 error = 0;
2034 return (error);
2035}
2036
2037/*
2038 * nfs readdir call
e851b29e
CP
2039 *
2040 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
984263bc
MD
2041 */
2042static int
e851b29e 2043nfs_readdir(struct vop_readdir_args *ap)
984263bc 2044{
40393ded
RG
2045 struct vnode *vp = ap->a_vp;
2046 struct nfsnode *np = VTONFS(vp);
2047 struct uio *uio = ap->a_uio;
984263bc
MD
2048 int tresid, error;
2049 struct vattr vattr;
2050
2051 if (vp->v_type != VDIR)
2052 return (EPERM);
2053 /*
2054 * First, check for hit on the EOF offset cache
2055 */
2056 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2057 (np->n_flag & NMODIFIED) == 0) {
2058 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2059 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2060 nfsstats.direofcache_hits++;
2061 return (0);
2062 }
3b568787 2063 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
984263bc
MD
2064 np->n_mtime == vattr.va_mtime.tv_sec) {
2065 nfsstats.direofcache_hits++;
2066 return (0);
2067 }
2068 }
2069
2070 /*
2071 * Call nfs_bioread() to do the real work.
2072 */
2073 tresid = uio->uio_resid;
3b568787 2074 error = nfs_bioread(vp, uio, 0);
984263bc
MD
2075
2076 if (!error && uio->uio_resid == tresid)
2077 nfsstats.direofcache_misses++;
2078 return (error);
2079}
2080
2081/*
2082 * Readdir rpc call.
2083 * Called from below the buffer cache by nfs_doio().
2084 */
2085int
3b568787 2086nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
984263bc 2087{
40393ded
RG
2088 int len, left;
2089 struct dirent *dp = NULL;
2090 u_int32_t *tl;
2091 caddr_t cp;
2092 int32_t t1, t2;
2093 nfsuint64 *cookiep;
984263bc
MD
2094 caddr_t bpos, dpos, cp2;
2095 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2096 nfsuint64 cookie;
2097 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2098 struct nfsnode *dnp = VTONFS(vp);
2099 u_quad_t fileno;
2100 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2101 int attrflag;
2102 int v3 = NFS_ISV3(vp);
2103
2104#ifndef DIAGNOSTIC
2105 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2106 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2107 panic("nfs readdirrpc bad uio");
2108#endif
2109
2110 /*
2111 * If there is no cookie, assume directory was stale.
2112 */
2113 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2114 if (cookiep)
2115 cookie = *cookiep;
2116 else
2117 return (NFSERR_BAD_COOKIE);
2118 /*
2119 * Loop around doing readdir rpc's of size nm_readdirsize
2120 * truncated to a multiple of DIRBLKSIZ.
2121 * The stopping criteria is EOF or buffer full.
2122 */
2123 while (more_dirs && bigenough) {
2124 nfsstats.rpccnt[NFSPROC_READDIR]++;
2125 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2126 NFSX_READDIR(v3));
2127 nfsm_fhtom(vp, v3);
2128 if (v3) {
2129 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2130 *tl++ = cookie.nfsuquad[0];
2131 *tl++ = cookie.nfsuquad[1];
2132 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2133 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2134 } else {
2135 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2136 *tl++ = cookie.nfsuquad[0];
2137 }
2138 *tl = txdr_unsigned(nmp->nm_readdirsize);
c1cf1e59 2139 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
984263bc
MD
2140 if (v3) {
2141 nfsm_postop_attr(vp, attrflag);
2142 if (!error) {
2143 nfsm_dissect(tl, u_int32_t *,
2144 2 * NFSX_UNSIGNED);
2145 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2146 dnp->n_cookieverf.nfsuquad[1] = *tl;
2147 } else {
2148 m_freem(mrep);
2149 goto nfsmout;
2150 }
2151 }
2152 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2153 more_dirs = fxdr_unsigned(int, *tl);
2154
2155 /* loop thru the dir entries, doctoring them to 4bsd form */
2156 while (more_dirs && bigenough) {
2157 if (v3) {
2158 nfsm_dissect(tl, u_int32_t *,
2159 3 * NFSX_UNSIGNED);
2160 fileno = fxdr_hyper(tl);
2161 len = fxdr_unsigned(int, *(tl + 2));
2162 } else {
2163 nfsm_dissect(tl, u_int32_t *,
2164 2 * NFSX_UNSIGNED);
2165 fileno = fxdr_unsigned(u_quad_t, *tl++);
2166 len = fxdr_unsigned(int, *tl);
2167 }
2168 if (len <= 0 || len > NFS_MAXNAMLEN) {
2169 error = EBADRPC;
2170 m_freem(mrep);
2171 goto nfsmout;
2172 }
2173 tlen = nfsm_rndup(len);
2174 if (tlen == len)
2175 tlen += 4; /* To ensure null termination */
2176 left = DIRBLKSIZ - blksiz;
2177 if ((tlen + DIRHDSIZ) > left) {
2178 dp->d_reclen += left;
2179 uiop->uio_iov->iov_base += left;
2180 uiop->uio_iov->iov_len -= left;
2181 uiop->uio_offset += left;
2182 uiop->uio_resid -= left;
2183 blksiz = 0;
2184 }
2185 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2186 bigenough = 0;
2187 if (bigenough) {
2188 dp = (struct dirent *)uiop->uio_iov->iov_base;
2189 dp->d_fileno = (int)fileno;
2190 dp->d_namlen = len;
2191 dp->d_reclen = tlen + DIRHDSIZ;
2192 dp->d_type = DT_UNKNOWN;
2193 blksiz += dp->d_reclen;
2194 if (blksiz == DIRBLKSIZ)
2195 blksiz = 0;
2196 uiop->uio_offset += DIRHDSIZ;
2197 uiop->uio_resid -= DIRHDSIZ;
2198 uiop->uio_iov->iov_base += DIRHDSIZ;
2199 uiop->uio_iov->iov_len -= DIRHDSIZ;
2200 nfsm_mtouio(uiop, len);
2201 cp = uiop->uio_iov->iov_base;
2202 tlen -= len;
2203 *cp = '\0'; /* null terminate */
2204 uiop->uio_iov->iov_base += tlen;
2205 uiop->uio_iov->iov_len -= tlen;
2206 uiop->uio_offset += tlen;
2207 uiop->uio_resid -= tlen;
2208 } else
2209 nfsm_adv(nfsm_rndup(len));
2210 if (v3) {
2211 nfsm_dissect(tl, u_int32_t *,
2212 3 * NFSX_UNSIGNED);
2213 } else {
2214 nfsm_dissect(tl, u_int32_t *,
2215 2 * NFSX_UNSIGNED);
2216 }
2217 if (bigenough) {
2218 cookie.nfsuquad[0] = *tl++;
2219 if (v3)
2220 cookie.nfsuquad[1] = *tl++;
2221 } else if (v3)
2222 tl += 2;
2223 else
2224 tl++;
2225 more_dirs = fxdr_unsigned(int, *tl);
2226 }
2227 /*
2228 * If at end of rpc data, get the eof boolean
2229 */
2230 if (!more_dirs) {
2231 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2232 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2233 }
2234 m_freem(mrep);
2235 }
2236 /*
2237 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2238 * by increasing d_reclen for the last record.
2239 */
2240 if (blksiz > 0) {
2241 left = DIRBLKSIZ - blksiz;
2242 dp->d_reclen += left;
2243 uiop->uio_iov->iov_base += left;
2244 uiop->uio_iov->iov_len -= left;
2245 uiop->uio_offset += left;
2246 uiop->uio_resid -= left;
2247 }
2248
2249 /*
2250 * We are now either at the end of the directory or have filled the
2251 * block.
2252 */
2253 if (bigenough)
2254 dnp->n_direofoffset = uiop->uio_offset;
2255 else {
2256 if (uiop->uio_resid > 0)
2257 printf("EEK! readdirrpc resid > 0\n");
2258 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2259 *cookiep = cookie;
2260 }
2261nfsmout:
2262 return (error);
2263}
2264
2265/*
2266 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2267 */
2268int
3b568787 2269nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
984263bc 2270{
40393ded
RG
2271 int len, left;
2272 struct dirent *dp;
2273 u_int32_t *tl;
2274 caddr_t cp;
2275 int32_t t1, t2;
2276 struct vnode *newvp;
2277 nfsuint64 *cookiep;
984263bc
MD
2278 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2279 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2280 struct nameidata nami, *ndp = &nami;
2281 struct componentname *cnp = &ndp->ni_cnd;
2282 nfsuint64 cookie;
2283 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2284 struct nfsnode *dnp = VTONFS(vp), *np;
2285 nfsfh_t *fhp;
2286 u_quad_t fileno;
2287 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2288 int attrflag, fhsize;
2289
2290#ifndef nolint
2291 dp = (struct dirent *)0;
2292#endif
2293#ifndef DIAGNOSTIC
2294 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2295 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2296 panic("nfs readdirplusrpc bad uio");
2297#endif
2298 ndp->ni_dvp = vp;
2299 newvp = NULLVP;
2300
2301 /*
2302 * If there is no cookie, assume directory was stale.
2303 */
2304 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2305 if (cookiep)
2306 cookie = *cookiep;
2307 else
2308 return (NFSERR_BAD_COOKIE);
2309 /*
2310 * Loop around doing readdir rpc's of size nm_readdirsize
2311 * truncated to a multiple of DIRBLKSIZ.
2312 * The stopping criteria is EOF or buffer full.
2313 */
2314 while (more_dirs && bigenough) {
2315 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2316 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2317 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2318 nfsm_fhtom(vp, 1);
2319 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2320 *tl++ = cookie.nfsuquad[0];
2321 *tl++ = cookie.nfsuquad[1];
2322 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2323 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2324 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2325 *tl = txdr_unsigned(nmp->nm_rsize);
c1cf1e59 2326 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
984263bc
MD
2327 nfsm_postop_attr(vp, attrflag);
2328 if (error) {
2329 m_freem(mrep);
2330 goto nfsmout;
2331 }
2332 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2333 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2334 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2335 more_dirs = fxdr_unsigned(int, *tl);
2336
2337 /* loop thru the dir entries, doctoring them to 4bsd form */
2338 while (more_dirs && bigenough) {
2339 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2340 fileno = fxdr_hyper(tl);
2341 len = fxdr_unsigned(int, *(tl + 2));
2342 if (len <= 0 || len > NFS_MAXNAMLEN) {
2343 error = EBADRPC;
2344 m_freem(mrep);
2345 goto nfsmout;
2346 }
2347 tlen = nfsm_rndup(len);
2348 if (tlen == len)
2349 tlen += 4; /* To ensure null termination*/
2350 left = DIRBLKSIZ - blksiz;
2351 if ((tlen + DIRHDSIZ) > left) {
2352 dp->d_reclen += left;
2353 uiop->uio_iov->iov_base += left;
2354 uiop->uio_iov->iov_len -= left;
2355 uiop->uio_offset += left;
2356 uiop->uio_resid -= left;
2357 blksiz = 0;
2358 }
2359 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2360 bigenough = 0;
2361 if (bigenough) {
2362 dp = (struct dirent *)uiop->uio_iov->iov_base;
2363 dp->d_fileno = (int)fileno;
2364 dp->d_namlen = len;
2365 dp->d_reclen = tlen + DIRHDSIZ;
2366 dp->d_type = DT_UNKNOWN;
2367 blksiz += dp->d_reclen;
2368 if (blksiz == DIRBLKSIZ)
2369 blksiz = 0;
2370 uiop->uio_offset += DIRHDSIZ;
2371 uiop->uio_resid -= DIRHDSIZ;
2372 uiop->uio_iov->iov_base += DIRHDSIZ;
2373 uiop->uio_iov->iov_len -= DIRHDSIZ;
2374 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2375 cnp->cn_namelen = len;
2376 nfsm_mtouio(uiop, len);
2377 cp = uiop->uio_iov->iov_base;
2378 tlen -= len;
2379 *cp = '\0';
2380 uiop->uio_iov->iov_base += tlen;
2381 uiop->uio_iov->iov_len -= tlen;
2382 uiop->uio_offset += tlen;
2383 uiop->uio_resid -= tlen;
2384 } else
2385 nfsm_adv(nfsm_rndup(len));
2386 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2387 if (bigenough) {
2388 cookie.nfsuquad[0] = *tl++;
2389 cookie.nfsuquad[1] = *tl++;
2390 } else
2391 tl += 2;
2392
2393 /*
2394 * Since the attributes are before the file handle
2395 * (sigh), we must skip over the attributes and then
2396 * come back and get them.
2397 */
2398 attrflag = fxdr_unsigned(int, *tl);
2399 if (attrflag) {
2400 dpossav1 = dpos;
2401 mdsav1 = md;
2402 nfsm_adv(NFSX_V3FATTR);
2403 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2404 doit = fxdr_unsigned(int, *tl);
2405 if (doit) {
2406 nfsm_getfh(fhp, fhsize, 1);
2407 if (NFS_CMPFH(dnp, fhp, fhsize)) {
597aea93 2408 vref(vp);
984263bc
MD
2409 newvp = vp;
2410 np = dnp;
2411 } else {
2412 error = nfs_nget(vp->v_mount, fhp,
2413 fhsize, &np);
2414 if (error)
2415 doit = 0;
2416 else
2417 newvp = NFSTOV(np);
2418 }
2419 }
2420 if (doit && bigenough) {
2421 dpossav2 = dpos;
2422 dpos = dpossav1;
2423 mdsav2 = md;
2424 md = mdsav1;
2425 nfsm_loadattr(newvp, (struct vattr *)0);
2426 dpos = dpossav2;
2427 md = mdsav2;
2428 dp->d_type =
2429 IFTODT(VTTOIF(np->n_vattr.va_type));
2430 ndp->ni_vp = newvp;
bc0c094e 2431 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
984263bc
MD
2432 }
2433 } else {
2434 /* Just skip over the file handle */
2435 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2436 i = fxdr_unsigned(int, *tl);
2437 nfsm_adv(nfsm_rndup(i));
2438 }
2439 if (newvp != NULLVP) {
2440 if (newvp == vp)
2441 vrele(newvp);
2442 else
2443 vput(newvp);
2444 newvp = NULLVP;
2445 }
2446 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2447 more_dirs = fxdr_unsigned(int, *tl);
2448 }
2449 /*
2450 * If at end of rpc data, get the eof boolean
2451 */
2452 if (!more_dirs) {
2453 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2454 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2455 }
2456 m_freem(mrep);
2457 }
2458 /*
2459 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2460 * by increasing d_reclen for the last record.
2461 */
2462 if (blksiz > 0) {
2463 left = DIRBLKSIZ - blksiz;
2464 dp->d_reclen += left;
2465 uiop->uio_iov->iov_base += left;
2466 uiop->uio_iov->iov_len -= left;
2467 uiop->uio_offset += left;
2468 uiop->uio_resid -= left;
2469 }
2470
2471 /*
2472 * We are now either at the end of the directory or have filled the
2473 * block.
2474 */
2475 if (bigenough)
2476 dnp->n_direofoffset = uiop->uio_offset;
2477 else {
2478 if (uiop->uio_resid > 0)
2479 printf("EEK! readdirplusrpc resid > 0\n");
2480 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2481 *cookiep = cookie;
2482 }
2483nfsmout:
2484 if (newvp != NULLVP) {
2485 if (newvp == vp)
2486 vrele(newvp);
2487 else
2488 vput(newvp);
2489 newvp = NULLVP;
2490 }
2491 return (error);
2492}
2493
2494/*
2495 * Silly rename. To make the NFS filesystem that is stateless look a little
2496 * more like the "ufs" a remove of an active vnode is translated to a rename
2497 * to a funny looking filename that is removed by nfs_inactive on the
2498 * nfsnode. There is the potential for another process on a different client
2499 * to create the same funny name between the nfs_lookitup() fails and the
2500 * nfs_rename() completes, but...
2501 */
2502static int
e851b29e 2503nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
984263bc 2504{
40393ded 2505 struct sillyrename *sp;
984263bc
MD
2506 struct nfsnode *np;
2507 int error;
984263bc
MD
2508
2509 cache_purge(dvp);
2510 np = VTONFS(vp);
2511#ifndef DIAGNOSTIC
2512 if (vp->v_type == VDIR)
2513 panic("nfs: sillyrename dir");
2514#endif
2515 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2516 M_NFSREQ, M_WAITOK);
2517 sp->s_cred = crdup(cnp->cn_cred);
2518 sp->s_dvp = dvp;
597aea93 2519 vref(dvp);
984263bc
MD
2520
2521 /* Fudge together a funny name */
dadab5e9 2522 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
984263bc
MD
2523
2524 /* Try lookitups until we get one that isn't there */
2525 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
dadab5e9 2526 cnp->cn_td, (struct nfsnode **)0) == 0) {
984263bc
MD
2527 sp->s_name[4]++;
2528 if (sp->s_name[4] > 'z') {
2529 error = EINVAL;
2530 goto bad;
2531 }
2532 }
2533 error = nfs_renameit(dvp, cnp, sp);
2534 if (error)
2535 goto bad;
2536 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
dadab5e9 2537 cnp->cn_td, &np);
984263bc
MD
2538 np->n_sillyrename = sp;
2539 return (0);
2540bad:
2541 vrele(sp->s_dvp);
2542 crfree(sp->s_cred);
2543 free((caddr_t)sp, M_NFSREQ);
2544 return (error);
2545}
2546
2547/*
2548 * Look up a file name and optionally either update the file handle or
2549 * allocate an nfsnode, depending on the value of npp.
2550 * npp == NULL --> just do the lookup
2551 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2552 * handled too
2553 * *npp != NULL --> update the file handle in the vnode
2554 */
2555static int
e851b29e
CP
2556nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2557 struct thread *td, struct nfsnode **npp)
984263bc 2558{
40393ded
RG
2559 u_int32_t *tl;
2560 caddr_t cp;
2561 int32_t t1, t2;
984263bc
MD
2562 struct vnode *newvp = (struct vnode *)0;
2563 struct nfsnode *np, *dnp = VTONFS(dvp);
2564 caddr_t bpos, dpos, cp2;
2565 int error = 0, fhlen, attrflag;
2566 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2567 nfsfh_t *nfhp;
2568 int v3 = NFS_ISV3(dvp);
2569
2570 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2571 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2572 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2573 nfsm_fhtom(dvp, v3);
2574 nfsm_strtom(name, len, NFS_MAXNAMLEN);
dadab5e9 2575 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
984263bc
MD
2576 if (npp && !error) {
2577 nfsm_getfh(nfhp, fhlen, v3);
2578 if (*npp) {
2579 np = *npp;
2580 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2581 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2582 np->n_fhp = &np->n_fh;
2583 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2584 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2585 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2586 np->n_fhsize = fhlen;
2587 newvp = NFSTOV(np);
2588 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
597aea93 2589 vref(dvp);
984263bc
MD
2590 newvp = dvp;
2591 } else {
2592 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2593 if (error) {
2594 m_freem(mrep);
2595 return (error);
2596 }
2597 newvp = NFSTOV(np);
2598 }
2599 if (v3) {
2600 nfsm_postop_attr(newvp, attrflag);
2601 if (!attrflag && *npp == NULL) {
2602 m_freem(mrep);
2603 if (newvp == dvp)
2604 vrele(newvp);
2605 else
2606 vput(newvp);
2607 return (ENOENT);
2608 }
2609 } else
2610 nfsm_loadattr(newvp, (struct vattr *)0);
2611 }
6b08710e
MD
2612 m_freem(mrep);
2613nfsmout:
984263bc
MD
2614 if (npp && *npp == NULL) {
2615 if (error) {
2616 if (newvp) {
2617 if (newvp == dvp)
2618 vrele(newvp);
2619 else
2620 vput(newvp);
2621 }
2622 } else
2623 *npp = np;
2624 }
2625 return (error);
2626}
2627
2628/*
2629 * Nfs Version 3 commit rpc
2630 */
2631int
3b568787 2632nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
984263bc 2633{
40393ded
RG
2634 caddr_t cp;
2635 u_int32_t *tl;
2636 int32_t t1, t2;
2637 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc
MD
2638 caddr_t bpos, dpos, cp2;
2639 int error = 0, wccflag = NFSV3_WCCRATTR;
2640 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2641
2642 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2643 return (0);
2644 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2645 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2646 nfsm_fhtom(vp, 1);
2647 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2648 txdr_hyper(offset, tl);
2649 tl += 2;
2650 *tl = txdr_unsigned(cnt);
c1cf1e59 2651 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
984263bc
MD
2652 nfsm_wcc_data(vp, wccflag);
2653 if (!error) {
2654 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2655 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2656 NFSX_V3WRITEVERF)) {
2657 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2658 NFSX_V3WRITEVERF);
2659 error = NFSERR_STALEWRITEVERF;
2660 }
2661 }
6b08710e
MD
2662 m_freem(mrep);
2663nfsmout:
984263bc
MD
2664 return (error);
2665}
2666
2667/*
2668 * Kludge City..
2669 * - make nfs_bmap() essentially a no-op that does no translation
2670 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2671 * (Maybe I could use the process's page mapping, but I was concerned that
2672 * Kernel Write might not be enabled and also figured copyout() would do
2673 * a lot more work than bcopy() and also it currently happens in the
2674 * context of the swapper process (2).
e851b29e
CP
2675 *
2676 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2677 * daddr_t *a_bnp, int *a_runp, int *a_runb)
984263bc
MD
2678 */
2679static int
e851b29e 2680nfs_bmap(struct vop_bmap_args *ap)
984263bc 2681{
40393ded 2682 struct vnode *vp = ap->a_vp;
984263bc
MD
2683
2684 if (ap->a_vpp != NULL)
2685 *ap->a_vpp = vp;
2686 if (ap->a_bnp != NULL)
2687 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2688 if (ap->a_runp != NULL)
2689 *ap->a_runp = 0;
2690 if (ap->a_runb != NULL)
2691 *ap->a_runb = 0;
2692 return (0);
2693}
2694
2695/*
2696 * Strategy routine.
2697 * For async requests when nfsiod(s) are running, queue the request by
2698 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2699 * request.
2700 */
2701static int
e851b29e 2702nfs_strategy(struct vop_strategy_args *ap)
984263bc 2703{
40393ded 2704 struct buf *bp = ap->a_bp;
dadab5e9 2705 struct thread *td;
984263bc
MD
2706 int error = 0;
2707
2708 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2709 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2710
2711 if (bp->b_flags & B_PHYS)
2712 panic("nfs physio");
2713
2714 if (bp->b_flags & B_ASYNC)
dadab5e9 2715 td = NULL;
984263bc 2716 else
dadab5e9 2717 td = curthread; /* XXX */
984263bc 2718
984263bc
MD
2719 /*
2720 * If the op is asynchronous and an i/o daemon is waiting
2721 * queue the request, wake it up and wait for completion
2722 * otherwise just do it ourselves.
2723 */
2724 if ((bp->b_flags & B_ASYNC) == 0 ||
3b568787
MD
2725 nfs_asyncio(bp, td))
2726 error = nfs_doio(bp, td);
984263bc
MD
2727 return (error);
2728}
2729
2730/*
2731 * Mmap a file
2732 *
2733 * NB Currently unsupported.
e851b29e
CP
2734 *
2735 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2736 * struct thread *a_td)
984263bc
MD
2737 */
2738/* ARGSUSED */
2739static int
e851b29e 2740nfs_mmap(struct vop_mmap_args *ap)
984263bc 2741{
984263bc
MD
2742 return (EINVAL);
2743}
2744
2745/*
2746 * fsync vnode op. Just call nfs_flush() with commit == 1.
e851b29e
CP
2747 *
2748 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2749 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
984263bc
MD
2750 */
2751/* ARGSUSED */
2752static int
e851b29e 2753nfs_fsync(struct vop_fsync_args *ap)
984263bc 2754{
3b568787 2755 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
984263bc
MD
2756}
2757
2758/*
2759 * Flush all the blocks associated with a vnode.
2760 * Walk through the buffer pool and push any dirty pages
2761 * associated with the vnode.
2762 */
2763static int
e851b29e 2764nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
984263bc 2765{
40393ded
RG
2766 struct nfsnode *np = VTONFS(vp);
2767 struct buf *bp;
2768 int i;
984263bc
MD
2769 struct buf *nbp;
2770 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2771 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2772 int passone = 1;
2773 u_quad_t off, endoff, toff;
984263bc
MD
2774 struct buf **bvec = NULL;
2775#ifndef NFS_COMMITBVECSIZ
2776#define NFS_COMMITBVECSIZ 20
2777#endif
2778 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2779 int bvecsize = 0, bveccount;
2780
2781 if (nmp->nm_flag & NFSMNT_INT)
2782 slpflag = PCATCH;
2783 if (!commit)
2784 passone = 0;
2785 /*
2786 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2787 * server, but nas not been committed to stable storage on the server
2788 * yet. On the first pass, the byte range is worked out and the commit
2789 * rpc is done. On the second pass, nfs_writebp() is called to do the
2790 * job.
2791 */
2792again:
2793 off = (u_quad_t)-1;
2794 endoff = 0;
2795 bvecpos = 0;
2796 if (NFS_ISV3(vp) && commit) {
2797 s = splbio();
2798 /*
2799 * Count up how many buffers waiting for a commit.
2800 */
2801 bveccount = 0;
2802 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2803 nbp = TAILQ_NEXT(bp, b_vnbufs);
2804 if (BUF_REFCNT(bp) == 0 &&
2805 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2806 == (B_DELWRI | B_NEEDCOMMIT))
2807 bveccount++;
2808 }
2809 /*
2810 * Allocate space to remember the list of bufs to commit. It is
2811 * important to use M_NOWAIT here to avoid a race with nfs_write.
2812 * If we can't get memory (for whatever reason), we will end up
2813 * committing the buffers one-by-one in the loop below.
2814 */
2815 if (bvec != NULL && bvec != bvec_on_stack)
2816 free(bvec, M_TEMP);
2817 if (bveccount > NFS_COMMITBVECSIZ) {
2818 bvec = (struct buf **)
2819 malloc(bveccount * sizeof(struct buf *),
2820 M_TEMP, M_NOWAIT);
2821 if (bvec == NULL) {
2822 bvec = bvec_on_stack;
2823 bvecsize = NFS_COMMITBVECSIZ;
2824 } else
2825 bvecsize = bveccount;
2826 } else {
2827 bvec = bvec_on_stack;
2828 bvecsize = NFS_COMMITBVECSIZ;
2829 }
2830 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2831 nbp = TAILQ_NEXT(bp, b_vnbufs);
2832 if (bvecpos >= bvecsize)
2833 break;
2834 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2835 (B_DELWRI | B_NEEDCOMMIT) ||
2836 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2837 continue;
2838 bremfree(bp);
2839 /*
984263bc
MD
2840 * NOTE: we are not clearing B_DONE here, so we have
2841 * to do it later on in this routine if we intend to
2842 * initiate I/O on the bp.
2843 *
2844 * Note: to avoid loopback deadlocks, we do not
2845 * assign b_runningbufspace.
2846 */
984263bc
MD
2847 bp->b_flags |= B_WRITEINPROG;
2848 vfs_busy_pages(bp, 1);
2849
2850 /*
2851 * bp is protected by being locked, but nbp is not
2852 * and vfs_busy_pages() may sleep. We have to
2853 * recalculate nbp.
2854 */
2855 nbp = TAILQ_NEXT(bp, b_vnbufs);
2856
2857 /*
2858 * A list of these buffers is kept so that the
2859 * second loop knows which buffers have actually
2860 * been committed. This is necessary, since there
2861 * may be a race between the commit rpc and new
2862 * uncommitted writes on the file.
2863 */
2864 bvec[bvecpos++] = bp;
2865 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2866 bp->b_dirtyoff;
2867 if (toff < off)
2868 off = toff;
2869 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2870 if (toff > endoff)
2871 endoff = toff;
2872 }
2873 splx(s);
2874 }
2875 if (bvecpos > 0) {
2876 /*
3b568787
MD
2877 * Commit data on the server, as required. Note that
2878 * nfs_commit will use the vnode's cred for the commit.
984263bc 2879 */
3b568787 2880 retv = nfs_commit(vp, off, (int)(endoff - off), td);
984263bc
MD
2881
2882 if (retv == NFSERR_STALEWRITEVERF)
2883 nfs_clearcommit(vp->v_mount);
2884
2885 /*
2886 * Now, either mark the blocks I/O done or mark the
2887 * blocks dirty, depending on whether the commit
2888 * succeeded.
2889 */
2890 for (i = 0; i < bvecpos; i++) {
2891 bp = bvec[i];
2892 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2893 if (retv) {
2894 /*
2895 * Error, leave B_DELWRI intact
2896 */
2897 vfs_unbusy_pages(bp);
2898 brelse(bp);
2899 } else {
2900 /*
2901 * Success, remove B_DELWRI ( bundirty() ).
2902 *
2903 * b_dirtyoff/b_dirtyend seem to be NFS
2904 * specific. We should probably move that
2905 * into bundirty(). XXX
2906 */
2907 s = splbio();
2908 vp->v_numoutput++;
2909 bp->b_flags |= B_ASYNC;
2910 bundirty(bp);
2911 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2912 bp->b_dirtyoff = bp->b_dirtyend = 0;
2913 splx(s);
2914 biodone(bp);
2915 }
2916 }
2917 }
2918
2919 /*
2920 * Start/do any write(s) that are required.
2921 */
2922loop:
2923 s = splbio();
2924 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2925 nbp = TAILQ_NEXT(bp, b_vnbufs);
2926 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2927 if (waitfor != MNT_WAIT || passone)
2928 continue;
2929 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2930 "nfsfsync", slpflag, slptimeo);
2931 splx(s);
2932 if (error == 0)
2933 panic("nfs_fsync: inconsistent lock");
2934 if (error == ENOLCK)
2935 goto loop;
dadab5e9 2936 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
984263bc
MD
2937 error = EINTR;
2938 goto done;
2939 }
2940 if (slpflag == PCATCH) {
2941 slpflag = 0;
2942 slptimeo = 2 * hz;
2943 }
2944 goto loop;
2945 }
2946 if ((bp->b_flags & B_DELWRI) == 0)
2947 panic("nfs_fsync: not dirty");
2948 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2949 BUF_UNLOCK(bp);
2950 continue;
2951 }
2952 bremfree(bp);
2953 if (passone || !commit)
2954 bp->b_flags |= B_ASYNC;
2955 else
2956 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2957 splx(s);
2958 VOP_BWRITE(bp->b_vp, bp);
2959 goto loop;
2960 }
2961 splx(s);
2962 if (passone) {
2963 passone = 0;
2964 goto again;
2965 }
2966 if (waitfor == MNT_WAIT) {
2967 while (vp->v_numoutput) {
2968 vp->v_flag |= VBWAIT;
2969 error = tsleep((caddr_t)&vp->v_numoutput,
377d4740 2970 slpflag, "nfsfsync", slptimeo);
984263bc 2971 if (error) {
dadab5e9 2972 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
984263bc
MD
2973 error = EINTR;
2974 goto done;
2975 }
2976 if (slpflag == PCATCH) {
2977 slpflag = 0;
2978 slptimeo = 2 * hz;
2979 }
2980 }
2981 }
2982 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2983 goto loop;
2984 }
2985 }
2986 if (np->n_flag & NWRITEERR) {
2987 error = np->n_error;
2988 np->n_flag &= ~NWRITEERR;
2989 }
2990done:
2991 if (bvec != NULL && bvec != bvec_on_stack)
2992 free(bvec, M_TEMP);
2993 return (error);
2994}
2995
2996/*
2997 * NFS advisory byte-level locks.
2998 * Currently unsupported.
e851b29e
CP
2999 *
3000 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3001 * int a_flags)
984263bc
MD
3002 */
3003static int
e851b29e 3004nfs_advlock(struct vop_advlock_args *ap)
984263bc 3005{
40393ded 3006 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3007
3008 /*
3009 * The following kludge is to allow diskless support to work
3010 * until a real NFS lockd is implemented. Basically, just pretend
3011 * that this is a local lock.
3012 */
3013 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3014}
3015
3016/*
3017 * Print out the contents of an nfsnode.
e851b29e
CP
3018 *
3019 * nfs_print(struct vnode *a_vp)
984263bc
MD
3020 */
3021static int
e851b29e 3022nfs_print(struct vop_print_args *ap)
984263bc 3023{
40393ded
RG
3024 struct vnode *vp = ap->a_vp;
3025 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3026
3027 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3028 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3029 if (vp->v_type == VFIFO)
3030 fifo_printinfo(vp);
3031 printf("\n");
3032 return (0);
3033}
3034
3035/*
3036 * Just call nfs_writebp() with the force argument set to 1.
3037 *
3038 * NOTE: B_DONE may or may not be set in a_bp on call.
e851b29e
CP
3039 *
3040 * nfs_bwrite(struct vnode *a_bp)
984263bc
MD
3041 */
3042static int
e851b29e 3043nfs_bwrite(struct vop_bwrite_args *ap)
984263bc 3044{
dadab5e9 3045 return (nfs_writebp(ap->a_bp, 1, curthread));
984263bc
MD
3046}
3047
3048/*
3049 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3050 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3051 * B_CACHE if this is a VMIO buffer.
3052 */
3053int
e851b29e 3054nfs_writebp(struct buf *bp, int force, struct thread *td)
984263bc
MD
3055{
3056 int s;
3057 int oldflags = bp->b_flags;
3058#if 0
3059 int retv = 1;
3060 off_t off;
3061#endif
3062
3063 if (BUF_REFCNT(bp) == 0)
3064 panic("bwrite: buffer is not locked???");
3065
3066 if (bp->b_flags & B_INVAL) {
3067 brelse(bp);
3068 return(0);
3069 }
3070
3071 bp->b_flags |= B_CACHE;
3072
3073 /*
3074 * Undirty the bp. We will redirty it later if the I/O fails.
3075 */
3076
3077 s = splbio();
3078 bundirty(bp);
3079 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3080
3081 bp->b_vp->v_numoutput++;
984263bc
MD
3082 splx(s);
3083
3084 /*
3085 * Note: to avoid loopback deadlocks, we do not
3086 * assign b_runningbufspace.
3087 */
3088 vfs_busy_pages(bp, 1);
3089
3090 if (force)
3091 bp->b_flags |= B_WRITEINPROG;
3092 BUF_KERNPROC(bp);
3093 VOP_STRATEGY(bp->b_vp, bp);
3094
3095 if( (oldflags & B_ASYNC) == 0) {
3096 int rtval = biowait(bp);
3097
3098 if (oldflags & B_DELWRI) {
3099 s = splbio();
3100 reassignbuf(bp, bp->b_vp);
3101 splx(s);
3102 }
3103
3104 brelse(bp);
3105 return (rtval);
3106 }
3107
3108 return (0);
3109}
3110
3111/*
3112 * nfs special file access vnode op.
3113 * Essentially just get vattr and then imitate iaccess() since the device is
3114 * local to the client.
e851b29e
CP
3115 *
3116 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3117 * struct thread *a_td)
984263bc
MD
3118 */
3119static int
e851b29e 3120nfsspec_access(struct vop_access_args *ap)
984263bc 3121{
40393ded
RG
3122 struct vattr *vap;
3123 gid_t *gp;
3124 struct ucred *cred = ap->a_cred;
984263bc
MD
3125 struct vnode *vp = ap->a_vp;
3126 mode_t mode = ap->a_mode;
3127 struct vattr vattr;
40393ded 3128 int i;
984263bc
MD
3129 int error;
3130
3131 /*
3132 * Disallow write attempts on filesystems mounted read-only;
3133 * unless the file is a socket, fifo, or a block or character
3134 * device resident on the filesystem.
3135 */
3136 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3137 switch (vp->v_type) {
3138 case VREG:
3139 case VDIR:
3140 case VLNK:
3141 return (EROFS);
3142 default:
3143 break;
3144 }
3145 }
3146 /*
3147 * If you're the super-user,
3148 * you always get access.
3149 */
3150 if (cred->cr_uid == 0)
3151 return (0);
3152 vap = &vattr;
3b568787 3153 error = VOP_GETATTR(vp, vap, ap->a_td);
984263bc
MD
3154 if (error)
3155 return (error);
3156 /*
3157 * Access check is based on only one of owner, group, public.
3158 * If not owner, then check group. If not a member of the
3159 * group, then check public access.
3160 */
3161 if (cred->cr_uid != vap->va_uid) {
3162 mode >>= 3;
3163 gp = cred->cr_groups;
3164 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3165 if (vap->va_gid == *gp)
3166 goto found;
3167 mode >>= 3;
3168found:
3169 ;
3170 }
3171 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3172 return (error);
3173}
3174
3175/*
3176 * Read wrapper for special devices.
e851b29e
CP
3177 *
3178 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3179 * struct ucred *a_cred)
984263bc
MD
3180 */
3181static int
e851b29e 3182nfsspec_read(struct vop_read_args *ap)
984263bc 3183{
40393ded 3184 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3185
3186 /*
3187 * Set access flag.
3188 */
3189 np->n_flag |= NACC;
3190 getnanotime(&np->n_atim);
0961aa92 3191 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3192}
3193
3194/*
3195 * Write wrapper for special devices.
e851b29e
CP
3196 *
3197 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3198 * struct ucred *a_cred)
984263bc
MD
3199 */
3200static int
e851b29e 3201nfsspec_write(struct vop_write_args *ap)
984263bc 3202{
40393ded 3203 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3204
3205 /*
3206 * Set update flag.
3207 */
3208 np->n_flag |= NUPD;
3209 getnanotime(&np->n_mtim);
0961aa92 3210 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3211}
3212
3213/*
3214 * Close wrapper for special devices.
3215 *
3216 * Update the times on the nfsnode then do device close.
e851b29e
CP
3217 *
3218 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3219 * struct thread *a_td)
984263bc
MD
3220 */
3221static int
e851b29e 3222nfsspec_close(struct vop_close_args *ap)
984263bc 3223{
40393ded
RG
3224 struct vnode *vp = ap->a_vp;
3225 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3226 struct vattr vattr;
3227
3228 if (np->n_flag & (NACC | NUPD)) {
3229 np->n_flag |= NCHG;
3230 if (vp->v_usecount == 1 &&
3231 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3232 VATTR_NULL(&vattr);
3233 if (np->n_flag & NACC)
3234 vattr.va_atime = np->n_atim;
3235 if (np->n_flag & NUPD)
3236 vattr.va_mtime = np->n_mtim;
c1cf1e59 3237 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
984263bc
MD
3238 }
3239 }
0961aa92 3240 return (VOCALL(spec_vnode_vops, &ap->a_head));
984263bc
MD
3241}
3242
3243/*
3244 * Read wrapper for fifos.
e851b29e
CP
3245 *
3246 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3247 * struct ucred *a_cred)
984263bc
MD
3248 */
3249static int
e851b29e 3250nfsfifo_read(struct vop_read_args *ap)
984263bc 3251{
40393ded 3252 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3253
3254 /*
3255 * Set access flag.
3256 */
3257 np->n_flag |= NACC;
3258 getnanotime(&np->n_atim);
0961aa92 3259 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3260}
3261
3262/*
3263 * Write wrapper for fifos.
e851b29e
CP
3264 *
3265 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3266 * struct ucred *a_cred)
984263bc
MD
3267 */
3268static int
e851b29e 3269nfsfifo_write(struct vop_write_args *ap)
984263bc 3270{
40393ded 3271 struct nfsnode *np = VTONFS(ap->a_vp);
984263bc
MD
3272
3273 /*
3274 * Set update flag.
3275 */
3276 np->n_flag |= NUPD;
3277 getnanotime(&np->n_mtim);
0961aa92 3278 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3279}
3280
3281/*
3282 * Close wrapper for fifos.
3283 *
3284 * Update the times on the nfsnode then do fifo close.
e851b29e
CP
3285 *
3286 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
984263bc
MD
3287 */
3288static int
e851b29e 3289nfsfifo_close(struct vop_close_args *ap)
984263bc 3290{
40393ded
RG
3291 struct vnode *vp = ap->a_vp;
3292 struct nfsnode *np = VTONFS(vp);
984263bc
MD
3293 struct vattr vattr;
3294 struct timespec ts;
3295
3296 if (np->n_flag & (NACC | NUPD)) {
3297 getnanotime(&ts);
3298 if (np->n_flag & NACC)
3299 np->n_atim = ts;
3300 if (np->n_flag & NUPD)
3301 np->n_mtim = ts;
3302 np->n_flag |= NCHG;
3303 if (vp->v_usecount == 1 &&
3304 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3305 VATTR_NULL(&vattr);
3306 if (np->n_flag & NACC)
3307 vattr.va_atime = np->n_atim;
3308 if (np->n_flag & NUPD)
3309 vattr.va_mtime = np->n_mtim;
c1cf1e59 3310 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
984263bc
MD
3311 }
3312 }
0961aa92 3313 return (VOCALL(fifo_vnode_vops, &ap->a_head));
984263bc
MD
3314}
3315