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