nfs_vfsops.c revision 1.130 1 /* $NetBSD: nfs_vfsops.c,v 1.130 2003/06/29 18:43:37 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.130 2003/06/29 18:43:37 thorpej Exp $");
43
44 #if defined(_KERNEL_OPT)
45 #include "opt_compat_netbsd.h"
46 #include "opt_nfs.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/signal.h>
52 #include <sys/proc.h>
53 #include <sys/namei.h>
54 #include <sys/device.h>
55 #include <sys/vnode.h>
56 #include <sys/kernel.h>
57 #include <sys/mount.h>
58 #include <sys/buf.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/systm.h>
64
65 #include <net/if.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfsproto.h>
71 #include <nfs/nfsnode.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsmount.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfs/nfsm_subs.h>
76 #include <nfs/nfsdiskless.h>
77 #include <nfs/nqnfs.h>
78 #include <nfs/nfs_var.h>
79
80 extern struct nfsstats nfsstats;
81 extern int nfs_ticks;
82
83 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
84
85 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
86 struct lwp *));
87
88 /*
89 * nfs vfs operations.
90 */
91
92 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
93 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
94 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
95
96 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
97 &nfsv2_vnodeop_opv_desc,
98 &spec_nfsv2nodeop_opv_desc,
99 &fifo_nfsv2nodeop_opv_desc,
100 NULL,
101 };
102
103 struct vfsops nfs_vfsops = {
104 MOUNT_NFS,
105 nfs_mount,
106 nfs_start,
107 nfs_unmount,
108 nfs_root,
109 nfs_quotactl,
110 nfs_statfs,
111 nfs_sync,
112 nfs_vget,
113 nfs_fhtovp,
114 nfs_vptofh,
115 nfs_vfs_init,
116 nfs_vfs_reinit,
117 nfs_vfs_done,
118 nfs_sysctl,
119 nfs_mountroot,
120 nfs_checkexp,
121 nfs_vnodeopv_descs,
122 };
123
124 extern u_int32_t nfs_procids[NFS_NPROCS];
125 extern u_int32_t nfs_prog, nfs_vers;
126
127 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
128 struct mount **, struct vnode **, struct lwp *));
129
130 /*
131 * nfs statfs call
132 */
133 int
134 nfs_statfs(mp, sbp, l)
135 struct mount *mp;
136 struct statfs *sbp;
137 struct lwp *l;
138 {
139 struct vnode *vp;
140 struct nfs_statfs *sfp;
141 caddr_t cp;
142 u_int32_t *tl;
143 int32_t t1, t2;
144 caddr_t bpos, dpos, cp2;
145 struct nfsmount *nmp = VFSTONFS(mp);
146 int error = 0, retattr;
147 #ifdef NFS_V2_ONLY
148 const int v3 = 0;
149 #else
150 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
151 #endif
152 struct mbuf *mreq, *mrep = NULL, *md, *mb;
153 struct ucred *cred;
154 u_quad_t tquad;
155 struct nfsnode *np;
156
157 #ifndef nolint
158 sfp = (struct nfs_statfs *)0;
159 #endif
160 vp = nmp->nm_vnode;
161 np = VTONFS(vp);
162 cred = crget();
163 cred->cr_ngroups = 0;
164 #ifndef NFS_V2_ONLY
165 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
166 (void)nfs_fsinfo(nmp, vp, cred, l);
167 #endif
168 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
169 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
170 nfsm_fhtom(np, v3);
171 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
172 if (v3)
173 nfsm_postop_attr(vp, retattr, 0);
174 if (error) {
175 if (mrep != NULL)
176 m_free(mrep);
177 goto nfsmout;
178 }
179 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
180 #ifdef COMPAT_09
181 sbp->f_type = 2;
182 #else
183 sbp->f_type = 0;
184 #endif
185 sbp->f_flags = nmp->nm_flag;
186 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
187 if (v3) {
188 sbp->f_bsize = NFS_FABLKSIZE;
189 tquad = fxdr_hyper(&sfp->sf_tbytes);
190 sbp->f_blocks = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
191 tquad = fxdr_hyper(&sfp->sf_fbytes);
192 sbp->f_bfree = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
193 tquad = fxdr_hyper(&sfp->sf_abytes);
194 sbp->f_bavail = (long)((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
195 tquad = fxdr_hyper(&sfp->sf_tfiles);
196 sbp->f_files = (long)tquad;
197 tquad = fxdr_hyper(&sfp->sf_ffiles);
198 sbp->f_ffree = (long)tquad;
199 } else {
200 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
201 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
202 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
203 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
204 sbp->f_files = 0;
205 sbp->f_ffree = 0;
206 }
207 copy_statfs_info(sbp, mp);
208 nfsm_reqdone;
209 crfree(cred);
210 return (error);
211 }
212
213 #ifndef NFS_V2_ONLY
214 /*
215 * nfs version 3 fsinfo rpc call
216 */
217 int
218 nfs_fsinfo(nmp, vp, cred, l)
219 struct nfsmount *nmp;
220 struct vnode *vp;
221 struct ucred *cred;
222 struct lwp *l;
223 {
224 struct nfsv3_fsinfo *fsp;
225 caddr_t cp;
226 int32_t t1, t2;
227 u_int32_t *tl, pref, max;
228 caddr_t bpos, dpos, cp2;
229 int error = 0, retattr;
230 struct mbuf *mreq, *mrep, *md, *mb;
231 u_int64_t maxfsize;
232 struct nfsnode *np = VTONFS(vp);
233
234 nfsstats.rpccnt[NFSPROC_FSINFO]++;
235 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
236 nfsm_fhtom(np, 1);
237 nfsm_request(np, NFSPROC_FSINFO, l, cred);
238 nfsm_postop_attr(vp, retattr, 0);
239 if (!error) {
240 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
241 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
242 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
243 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
244 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
245 ~(NFS_FABLKSIZE - 1);
246 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
247 if (max < nmp->nm_wsize && max > 0) {
248 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
249 if (nmp->nm_wsize == 0)
250 nmp->nm_wsize = max;
251 }
252 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
253 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
254 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
255 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
256 ~(NFS_FABLKSIZE - 1);
257 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
258 if (max < nmp->nm_rsize && max > 0) {
259 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
260 if (nmp->nm_rsize == 0)
261 nmp->nm_rsize = max;
262 }
263 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
264 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
265 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
266 ~(NFS_DIRFRAGSIZ - 1);
267 if (max < nmp->nm_readdirsize && max > 0) {
268 nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
269 if (nmp->nm_readdirsize == 0)
270 nmp->nm_readdirsize = max;
271 }
272 /* XXX */
273 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
274 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
275 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
276 nmp->nm_maxfilesize = maxfsize;
277 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
278 }
279 nfsm_reqdone;
280 return (error);
281 }
282 #endif
283
284 /*
285 * Mount a remote root fs via. NFS. It goes like this:
286 * - Call nfs_boot_init() to fill in the nfs_diskless struct
287 * - build the rootfs mount point and call mountnfs() to do the rest.
288 */
289 int
290 nfs_mountroot()
291 {
292 struct nfs_diskless *nd;
293 struct vattr attr;
294 struct mount *mp;
295 struct vnode *vp;
296 struct lwp *l;
297 long n;
298 int error;
299
300 l = curlwp; /* XXX */
301
302 if (root_device->dv_class != DV_IFNET)
303 return (ENODEV);
304
305 /*
306 * XXX time must be non-zero when we init the interface or else
307 * the arp code will wedge. [Fixed now in if_ether.c]
308 * However, the NFS attribute cache gives false "hits" when
309 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
310 */
311 if (time.tv_sec < NFS_MAXATTRTIMO)
312 time.tv_sec = NFS_MAXATTRTIMO;
313
314 /*
315 * Call nfs_boot_init() to fill in the nfs_diskless struct.
316 * Side effect: Finds and configures a network interface.
317 */
318 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
319 memset((caddr_t)nd, 0, sizeof(*nd));
320 error = nfs_boot_init(nd, l);
321 if (error) {
322 free(nd, M_NFSMNT);
323 return (error);
324 }
325
326 /*
327 * Create the root mount point.
328 */
329 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
330 if (error)
331 goto out;
332 printf("root on %s\n", nd->nd_root.ndm_host);
333
334 /*
335 * Link it into the mount list.
336 */
337 simple_lock(&mountlist_slock);
338 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
339 simple_unlock(&mountlist_slock);
340 rootvp = vp;
341 mp->mnt_vnodecovered = NULLVP;
342 vfs_unbusy(mp);
343
344 /* Get root attributes (for the time). */
345 error = VOP_GETATTR(vp, &attr, l->l_proc->p_ucred, l);
346 if (error)
347 panic("nfs_mountroot: getattr for root");
348 n = attr.va_atime.tv_sec;
349 #ifdef DEBUG
350 printf("root time: 0x%lx\n", n);
351 #endif
352 inittodr(n);
353
354 out:
355 if (error)
356 nfs_boot_cleanup(nd, l);
357 free(nd, M_NFSMNT);
358 return (error);
359 }
360
361 /*
362 * Internal version of mount system call for diskless setup.
363 * Separate function because we used to call it twice.
364 * (once for root and once for swap)
365 */
366 static int
367 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, l)
368 struct nfs_dlmount *ndmntp;
369 const char *mntname; /* mount point name */
370 struct mount **mpp;
371 struct vnode **vpp;
372 struct lwp *l;
373 {
374 struct mount *mp;
375 struct mbuf *m;
376 int error;
377
378 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
379
380 mp->mnt_op = &nfs_vfsops;
381
382 /*
383 * Historical practice expects NFS root file systems to
384 * be initially mounted r/w.
385 */
386 mp->mnt_flag &= ~MNT_RDONLY;
387
388 /* Get mbuf for server sockaddr. */
389 m = m_get(M_WAIT, MT_SONAME);
390 if (m == NULL)
391 panic("nfs_mountroot: mget soname for %s", mntname);
392 MCLAIM(m, &nfs_mowner);
393 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
394 (m->m_len = ndmntp->ndm_args.addr->sa_len));
395
396 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
397 ndmntp->ndm_args.hostname, vpp, l);
398 if (error) {
399 mp->mnt_op->vfs_refcount--;
400 vfs_unbusy(mp);
401 printf("nfs_mountroot: mount %s failed: %d\n",
402 mntname, error);
403 free(mp, M_MOUNT);
404 } else
405 *mpp = mp;
406
407 return (error);
408 }
409
410 void
411 nfs_decode_args(nmp, argp)
412 struct nfsmount *nmp;
413 struct nfs_args *argp;
414 {
415 int s;
416 int adjsock;
417 int maxio;
418
419 s = splsoftnet();
420
421 /*
422 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
423 * no sense in that context.
424 */
425 if (argp->sotype == SOCK_STREAM)
426 argp->flags &= ~NFSMNT_NOCONN;
427
428 /*
429 * Cookie translation is not needed for v2, silently ignore it.
430 */
431 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
432 NFSMNT_XLATECOOKIE)
433 argp->flags &= ~NFSMNT_XLATECOOKIE;
434
435 /* Re-bind if rsrvd port requested and wasn't on one */
436 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
437 && (argp->flags & NFSMNT_RESVPORT);
438 /* Also re-bind if we're switching to/from a connected UDP socket */
439 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
440 (argp->flags & NFSMNT_NOCONN));
441
442 /* Update flags. */
443 nmp->nm_flag = argp->flags;
444 splx(s);
445
446 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
447 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
448 if (nmp->nm_timeo < NFS_MINTIMEO)
449 nmp->nm_timeo = NFS_MINTIMEO;
450 else if (nmp->nm_timeo > NFS_MAXTIMEO)
451 nmp->nm_timeo = NFS_MAXTIMEO;
452 }
453
454 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
455 nmp->nm_retry = argp->retrans;
456 if (nmp->nm_retry > NFS_MAXREXMIT)
457 nmp->nm_retry = NFS_MAXREXMIT;
458 }
459
460 #ifndef NFS_V2_ONLY
461 if (argp->flags & NFSMNT_NFSV3) {
462 if (argp->sotype == SOCK_DGRAM)
463 maxio = NFS_MAXDGRAMDATA;
464 else
465 maxio = NFS_MAXDATA;
466 } else
467 #endif
468 maxio = NFS_V2MAXDATA;
469
470 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
471 int osize = nmp->nm_wsize;
472 nmp->nm_wsize = argp->wsize;
473 /* Round down to multiple of blocksize */
474 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
475 if (nmp->nm_wsize <= 0)
476 nmp->nm_wsize = NFS_FABLKSIZE;
477 adjsock |= (nmp->nm_wsize != osize);
478 }
479 if (nmp->nm_wsize > maxio)
480 nmp->nm_wsize = maxio;
481 if (nmp->nm_wsize > MAXBSIZE)
482 nmp->nm_wsize = MAXBSIZE;
483
484 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
485 int osize = nmp->nm_rsize;
486 nmp->nm_rsize = argp->rsize;
487 /* Round down to multiple of blocksize */
488 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
489 if (nmp->nm_rsize <= 0)
490 nmp->nm_rsize = NFS_FABLKSIZE;
491 adjsock |= (nmp->nm_rsize != osize);
492 }
493 if (nmp->nm_rsize > maxio)
494 nmp->nm_rsize = maxio;
495 if (nmp->nm_rsize > MAXBSIZE)
496 nmp->nm_rsize = MAXBSIZE;
497
498 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
499 nmp->nm_readdirsize = argp->readdirsize;
500 /* Round down to multiple of minimum blocksize */
501 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
502 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
503 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
504 /* Bigger than buffer size makes no sense */
505 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
506 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
507 } else if (argp->flags & NFSMNT_RSIZE)
508 nmp->nm_readdirsize = nmp->nm_rsize;
509
510 if (nmp->nm_readdirsize > maxio)
511 nmp->nm_readdirsize = maxio;
512
513 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
514 argp->maxgrouplist <= NFS_MAXGRPS)
515 nmp->nm_numgrps = argp->maxgrouplist;
516 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
517 argp->readahead <= NFS_MAXRAHEAD)
518 nmp->nm_readahead = argp->readahead;
519 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
520 argp->leaseterm <= NQ_MAXLEASE)
521 nmp->nm_leaseterm = argp->leaseterm;
522 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
523 argp->deadthresh <= NQ_NEVERDEAD)
524 nmp->nm_deadthresh = argp->deadthresh;
525
526 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
527 (nmp->nm_soproto != argp->proto));
528 nmp->nm_sotype = argp->sotype;
529 nmp->nm_soproto = argp->proto;
530
531 if (nmp->nm_so && adjsock) {
532 nfs_safedisconnect(nmp);
533 if (nmp->nm_sotype == SOCK_DGRAM)
534 while (nfs_connect(nmp, (struct nfsreq *)0)) {
535 printf("nfs_args: retrying connect\n");
536 (void) tsleep((caddr_t)&lbolt,
537 PSOCK, "nfscn3", 0);
538 }
539 }
540 }
541
542 /*
543 * VFS Operations.
544 *
545 * mount system call
546 * It seems a bit dumb to copyinstr() the host and path here and then
547 * memcpy() them in mountnfs(), but I wanted to detect errors before
548 * doing the sockargs() call because sockargs() allocates an mbuf and
549 * an error after that means that I have to release the mbuf.
550 */
551 /* ARGSUSED */
552 int
553 nfs_mount(mp, path, data, ndp, l)
554 struct mount *mp;
555 const char *path;
556 void *data;
557 struct nameidata *ndp;
558 struct lwp *l;
559 {
560 int error;
561 struct nfs_args args;
562 struct mbuf *nam;
563 struct nfsmount *nmp = VFSTONFS(mp);
564 struct sockaddr *sa;
565 struct vnode *vp;
566 char *pth, *hst;
567 struct proc *p;
568 size_t len;
569 u_char *nfh;
570
571 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
572 if (error)
573 return (error);
574
575 p = l->l_proc;
576 if (mp->mnt_flag & MNT_GETARGS) {
577
578 if (nmp == NULL)
579 return (EIO);
580 if (args.addr != NULL) {
581 sa = mtod(nmp->nm_nam, struct sockaddr *);
582 error = copyout(sa, args.addr, sa->sa_len);
583 if (error)
584 return (error);
585 args.addrlen = sa->sa_len;
586 } else
587 args.addrlen = 0;
588
589 args.version = NFS_ARGSVERSION;
590 args.sotype = nmp->nm_sotype;
591 args.proto = nmp->nm_soproto;
592 args.fh = NULL;
593 args.fhsize = 0;
594 args.flags = nmp->nm_flag;
595 args.wsize = nmp->nm_wsize;
596 args.rsize = nmp->nm_rsize;
597 args.readdirsize = nmp->nm_readdirsize;
598 args.timeo = nmp->nm_timeo;
599 args.retrans = nmp->nm_retry;
600 args.maxgrouplist = nmp->nm_numgrps;
601 args.readahead = nmp->nm_readahead;
602 args.leaseterm = nmp->nm_leaseterm;
603 args.deadthresh = nmp->nm_deadthresh;
604 args.hostname = NULL;
605 return (copyout(&args, data, sizeof(args)));
606 }
607
608 if (args.version != NFS_ARGSVERSION)
609 return (EPROGMISMATCH);
610 #ifdef NFS_V2_ONLY
611 if (args.flags & NFSMNT_NQNFS)
612 return (EPROGUNAVAIL);
613 if (args.flags & NFSMNT_NFSV3)
614 return (EPROGMISMATCH);
615 #endif
616 if (mp->mnt_flag & MNT_UPDATE) {
617 if (nmp == NULL)
618 return (EIO);
619 /*
620 * When doing an update, we can't change from or to
621 * v3 and/or nqnfs, or change cookie translation
622 */
623 args.flags = (args.flags &
624 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
625 (nmp->nm_flag &
626 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
627 nfs_decode_args(nmp, &args);
628 return (0);
629 }
630 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
631 return (EINVAL);
632 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
633 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
634 if (error)
635 return (error);
636 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
637 error = copyinstr(path, pth, MNAMELEN - 1, &len);
638 if (error)
639 goto free_nfh;
640 memset(&pth[len], 0, MNAMELEN - len);
641 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
642 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
643 if (error)
644 goto free_pth;
645 memset(&hst[len], 0, MNAMELEN - len);
646 /* sockargs() call must be after above copyin() calls */
647 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
648 if (error)
649 goto free_hst;
650 MCLAIM(nam, &nfs_mowner);
651 args.fh = nfh;
652 error = mountnfs(&args, mp, nam, pth, hst, &vp, l);
653
654 free_hst:
655 FREE(hst, M_TEMP);
656 free_pth:
657 FREE(pth, M_TEMP);
658 free_nfh:
659 FREE(nfh, M_TEMP);
660
661 return (error);
662 }
663
664 /*
665 * Common code for mount and mountroot
666 */
667 int
668 mountnfs(argp, mp, nam, pth, hst, vpp, l)
669 struct nfs_args *argp;
670 struct mount *mp;
671 struct mbuf *nam;
672 const char *pth, *hst;
673 struct vnode **vpp;
674 struct lwp *l;
675 {
676 struct nfsmount *nmp;
677 struct nfsnode *np;
678 int error;
679 struct vattr *attrs;
680 struct ucred *cr;
681
682 /*
683 * If the number of nfs iothreads to use has never
684 * been set, create a reasonable number of them.
685 */
686
687 if (nfs_niothreads < 0) {
688 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
689 nfs_getset_niothreads(TRUE);
690 }
691
692 if (mp->mnt_flag & MNT_UPDATE) {
693 nmp = VFSTONFS(mp);
694 /* update paths, file handles, etc, here XXX */
695 m_freem(nam);
696 return (0);
697 } else {
698 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
699 M_NFSMNT, M_WAITOK);
700 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
701 mp->mnt_data = nmp;
702 TAILQ_INIT(&nmp->nm_uidlruhead);
703 TAILQ_INIT(&nmp->nm_bufq);
704 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
705 simple_lock_init(&nmp->nm_slock);
706 }
707 vfs_getnewfsid(mp);
708 nmp->nm_mountp = mp;
709
710 #ifndef NFS_V2_ONLY
711 if (argp->flags & NFSMNT_NQNFS)
712 /*
713 * We have to set mnt_maxsymlink to a non-zero value so
714 * that COMPAT_43 routines will know that we are setting
715 * the d_type field in directories (and can zero it for
716 * unsuspecting binaries).
717 */
718 mp->mnt_maxsymlinklen = 1;
719 #endif
720
721 #ifndef NFS_V2_ONLY
722 if ((argp->flags & NFSMNT_NFSV3) == 0)
723 #endif
724 /*
725 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
726 * will fill this in.
727 */
728 nmp->nm_maxfilesize = 0xffffffffLL;
729
730 nmp->nm_timeo = NFS_TIMEO;
731 nmp->nm_retry = NFS_RETRANS;
732 nmp->nm_wsize = NFS_WSIZE;
733 nmp->nm_rsize = NFS_RSIZE;
734 nmp->nm_readdirsize = NFS_READDIRSIZE;
735 nmp->nm_numgrps = NFS_MAXGRPS;
736 nmp->nm_readahead = NFS_DEFRAHEAD;
737 nmp->nm_leaseterm = NQ_DEFLEASE;
738 nmp->nm_deadthresh = NQ_DEADTHRESH;
739 CIRCLEQ_INIT(&nmp->nm_timerhead);
740 nmp->nm_inprog = NULLVP;
741 #ifdef COMPAT_09
742 mp->mnt_stat.f_type = 2;
743 #else
744 mp->mnt_stat.f_type = 0;
745 #endif
746 error = set_statfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, l);
747 if (error)
748 goto bad;
749 nmp->nm_nam = nam;
750
751 /* Set up the sockets and per-host congestion */
752 nmp->nm_sotype = argp->sotype;
753 nmp->nm_soproto = argp->proto;
754
755 nfs_decode_args(nmp, argp);
756
757 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
758 mp->mnt_dev_bshift = DEV_BSHIFT;
759
760 /*
761 * For Connection based sockets (TCP,...) defer the connect until
762 * the first request, in case the server is not responding.
763 */
764 if (nmp->nm_sotype == SOCK_DGRAM &&
765 (error = nfs_connect(nmp, (struct nfsreq *)0)))
766 goto bad;
767
768 /*
769 * This is silly, but it has to be set so that vinifod() works.
770 * We do not want to do an nfs_statfs() here since we can get
771 * stuck on a dead server and we are holding a lock on the mount
772 * point.
773 */
774 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
775 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np, l);
776 if (error)
777 goto bad;
778 *vpp = NFSTOV(np);
779 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
780 VOP_GETATTR(*vpp, attrs, l->l_proc->p_ucred, l);
781 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
782 cr = crget();
783 cr->cr_uid = attrs->va_uid;
784 cr->cr_gid = attrs->va_gid;
785 cr->cr_ngroups = 0;
786 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, l, cr);
787 crfree(cr);
788 }
789 FREE(attrs, M_TEMP);
790
791 /*
792 * A reference count is needed on the nfsnode representing the
793 * remote root. If this object is not persistent, then backward
794 * traversals of the mount point (i.e. "..") will not work if
795 * the nfsnode gets flushed out of the cache. Ufs does not have
796 * this problem, because one can identify root inodes by their
797 * number == ROOTINO (2). So, just unlock, but no rele.
798 */
799
800 nmp->nm_vnode = *vpp;
801 VOP_UNLOCK(*vpp, 0);
802
803 return (0);
804 bad:
805 nfs_disconnect(nmp);
806 free((caddr_t)nmp, M_NFSMNT);
807 m_freem(nam);
808 return (error);
809 }
810
811 /*
812 * unmount system call
813 */
814 int
815 nfs_unmount(mp, mntflags, l)
816 struct mount *mp;
817 int mntflags;
818 struct lwp *l;
819 {
820 struct nfsmount *nmp;
821 struct vnode *vp;
822 int error, flags = 0;
823
824 if (mntflags & MNT_FORCE)
825 flags |= FORCECLOSE;
826 nmp = VFSTONFS(mp);
827 /*
828 * Goes something like this..
829 * - Check for activity on the root vnode (other than ourselves).
830 * - Call vflush() to clear out vnodes for this file system,
831 * except for the root vnode.
832 * - Decrement reference on the vnode representing remote root.
833 * - Close the socket
834 * - Free up the data structures
835 */
836 /*
837 * We need to decrement the ref. count on the nfsnode representing
838 * the remote root. See comment in mountnfs(). The VFS unmount()
839 * has done vput on this vnode, otherwise we would get deadlock!
840 */
841 vp = nmp->nm_vnode;
842 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
843 if (error != 0)
844 return error;
845
846 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
847 vput(vp);
848 return (EBUSY);
849 }
850
851 /*
852 * Must handshake with nqnfs_clientd() if it is active.
853 */
854 nmp->nm_iflag |= NFSMNT_DISMINPROG;
855 while (nmp->nm_inprog != NULLVP)
856 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
857 error = vflush(mp, vp, flags);
858 if (error) {
859 vput(vp);
860 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
861 return (error);
862 }
863
864 /*
865 * We are now committed to the unmount; mark the mount structure
866 * as doomed so that any sleepers kicked awake by nfs_disconnect
867 * will go away cleanly.
868 */
869 nmp->nm_iflag |= NFSMNT_DISMNT;
870
871 /*
872 * There are two reference counts to get rid of here
873 * (see comment in mountnfs()).
874 */
875 vrele(vp);
876 vput(vp);
877 vgone(vp);
878 nfs_disconnect(nmp);
879 m_freem(nmp->nm_nam);
880
881 /*
882 * For NQNFS, let the server daemon free the nfsmount structure.
883 */
884 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
885 free((caddr_t)nmp, M_NFSMNT);
886 return (0);
887 }
888
889 /*
890 * Return root of a filesystem
891 */
892 int
893 nfs_root(mp, vpp)
894 struct mount *mp;
895 struct vnode **vpp;
896 {
897 struct vnode *vp;
898 struct nfsmount *nmp;
899 int error;
900
901 nmp = VFSTONFS(mp);
902 vp = nmp->nm_vnode;
903 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
904 if (error != 0)
905 return error;
906 if (vp->v_type == VNON)
907 vp->v_type = VDIR;
908 vp->v_flag = VROOT;
909 *vpp = vp;
910 return (0);
911 }
912
913 extern int syncprt;
914
915 /*
916 * Flush out the buffer cache
917 */
918 /* ARGSUSED */
919 int
920 nfs_sync(mp, waitfor, cred, l)
921 struct mount *mp;
922 int waitfor;
923 struct ucred *cred;
924 struct lwp *l;
925 {
926 struct vnode *vp;
927 int error, allerror = 0;
928
929 /*
930 * Force stale buffer cache information to be flushed.
931 */
932 loop:
933 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
934 /*
935 * If the vnode that we are about to sync is no longer
936 * associated with this mount point, start over.
937 */
938 if (vp->v_mount != mp)
939 goto loop;
940 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
941 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
942 vp->v_uobj.uo_npages == 0))
943 continue;
944 if (vget(vp, LK_EXCLUSIVE))
945 goto loop;
946 error = VOP_FSYNC(vp, cred,
947 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
948 if (error)
949 allerror = error;
950 vput(vp);
951 }
952 return (allerror);
953 }
954
955 /*
956 * NFS flat namespace lookup.
957 * Currently unsupported.
958 */
959 /* ARGSUSED */
960 int
961 nfs_vget(mp, ino, vpp)
962 struct mount *mp;
963 ino_t ino;
964 struct vnode **vpp;
965 {
966
967 return (EOPNOTSUPP);
968 }
969
970 /*
971 * Do that sysctl thang...
972 */
973 int
974 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, l)
975 int *name;
976 u_int namelen;
977 void *oldp;
978 size_t *oldlenp;
979 void *newp;
980 size_t newlen;
981 struct lwp *l;
982 {
983 int rv;
984
985 /*
986 * All names at this level are terminal.
987 */
988 if(namelen > 1)
989 return ENOTDIR; /* overloaded */
990
991 switch(name[0]) {
992 case NFS_NFSSTATS:
993 if(!oldp) {
994 *oldlenp = sizeof nfsstats;
995 return 0;
996 }
997
998 if(*oldlenp < sizeof nfsstats) {
999 *oldlenp = sizeof nfsstats;
1000 return ENOMEM;
1001 }
1002
1003 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
1004 if(rv) return rv;
1005
1006 if(newp && newlen != sizeof nfsstats)
1007 return EINVAL;
1008
1009 if(newp) {
1010 return copyin(newp, &nfsstats, sizeof nfsstats);
1011 }
1012 return 0;
1013
1014 case NFS_IOTHREADS:
1015 nfs_getset_niothreads(0);
1016
1017 rv = (sysctl_int(oldp, oldlenp, newp, newlen,
1018 &nfs_niothreads));
1019
1020 if (newp)
1021 nfs_getset_niothreads(1);
1022
1023 return rv;
1024
1025 default:
1026 return EOPNOTSUPP;
1027 }
1028 }
1029
1030
1031 /*
1032 * At this point, this should never happen
1033 */
1034 /* ARGSUSED */
1035 int
1036 nfs_fhtovp(mp, fhp, vpp)
1037 struct mount *mp;
1038 struct fid *fhp;
1039 struct vnode **vpp;
1040 {
1041
1042 return (EINVAL);
1043 }
1044
1045 /* ARGSUSED */
1046 int
1047 nfs_checkexp(mp, nam, exflagsp, credanonp)
1048 struct mount *mp;
1049 struct mbuf *nam;
1050 int *exflagsp;
1051 struct ucred **credanonp;
1052 {
1053
1054 return (EINVAL);
1055 }
1056
1057 /*
1058 * Vnode pointer to File handle, should never happen either
1059 */
1060 /* ARGSUSED */
1061 int
1062 nfs_vptofh(vp, fhp)
1063 struct vnode *vp;
1064 struct fid *fhp;
1065 {
1066
1067 return (EINVAL);
1068 }
1069
1070 /*
1071 * Vfs start routine, a no-op.
1072 */
1073 /* ARGSUSED */
1074 int
1075 nfs_start(mp, flags, l)
1076 struct mount *mp;
1077 int flags;
1078 struct lwp *l;
1079 {
1080
1081 return (0);
1082 }
1083
1084 /*
1085 * Do operations associated with quotas, not supported
1086 */
1087 /* ARGSUSED */
1088 int
1089 nfs_quotactl(mp, cmd, uid, arg, l)
1090 struct mount *mp;
1091 int cmd;
1092 uid_t uid;
1093 caddr_t arg;
1094 struct lwp *l;
1095 {
1096
1097 return (EOPNOTSUPP);
1098 }
1099