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