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