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