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