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