nfs_vfsops.c revision 1.101.2.7 1 /* $NetBSD: nfs_vfsops.c,v 1.101.2.7 2002/01/08 00:34:34 nathanw 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.101.2.7 2002/01/08 00:34:34 nathanw 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/lwp.h>
53 #include <sys/proc.h>
54 #include <sys/namei.h>
55 #include <sys/device.h>
56 #include <sys/vnode.h>
57 #include <sys/kernel.h>
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/mbuf.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
65
66 #include <net/if.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfs/nfsnode.h>
73 #include <nfs/nfs.h>
74 #include <nfs/nfsmount.h>
75 #include <nfs/xdr_subs.h>
76 #include <nfs/nfsm_subs.h>
77 #include <nfs/nfsdiskless.h>
78 #include <nfs/nqnfs.h>
79 #include <nfs/nfs_var.h>
80
81 extern struct nfsstats nfsstats;
82 extern int nfs_ticks;
83
84 int nfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
85 struct proc *));
86
87 /*
88 * nfs vfs operations.
89 */
90
91 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
92 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
93 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
94
95 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
96 &nfsv2_vnodeop_opv_desc,
97 &spec_nfsv2nodeop_opv_desc,
98 &fifo_nfsv2nodeop_opv_desc,
99 NULL,
100 };
101
102 struct vfsops nfs_vfsops = {
103 MOUNT_NFS,
104 nfs_mount,
105 nfs_start,
106 nfs_unmount,
107 nfs_root,
108 nfs_quotactl,
109 nfs_statfs,
110 nfs_sync,
111 nfs_vget,
112 nfs_fhtovp,
113 nfs_vptofh,
114 nfs_vfs_init,
115 nfs_vfs_reinit,
116 nfs_vfs_done,
117 nfs_sysctl,
118 nfs_mountroot,
119 nfs_checkexp,
120 nfs_vnodeopv_descs,
121 };
122
123 extern u_int32_t nfs_procids[NFS_NPROCS];
124 extern u_int32_t nfs_prog, nfs_vers;
125
126 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
127 struct mount **, struct vnode **, struct proc *));
128
129 #define TRUE 1
130 #define FALSE 0
131
132 /*
133 * nfs statfs call
134 */
135 int
136 nfs_statfs(mp, sbp, p)
137 struct mount *mp;
138 struct statfs *sbp;
139 struct proc *p;
140 {
141 struct vnode *vp;
142 struct nfs_statfs *sfp;
143 caddr_t cp;
144 u_int32_t *tl;
145 int32_t t1, t2;
146 caddr_t bpos, dpos, cp2;
147 struct nfsmount *nmp = VFSTONFS(mp);
148 int error = 0, retattr;
149 #ifdef NFS_V2_ONLY
150 const int v3 = 0;
151 #else
152 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
153 #endif
154 struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
155 struct ucred *cred;
156 u_quad_t tquad;
157
158 #ifndef nolint
159 sfp = (struct nfs_statfs *)0;
160 #endif
161 vp = nmp->nm_vnode;
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, p);
167 #endif
168 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
169 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
170 nfsm_fhtom(vp, v3);
171 nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
172 if (v3)
173 nfsm_postop_attr(vp, retattr);
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 if (sbp != &mp->mnt_stat) {
208 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
209 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
210 }
211 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
212 nfsm_reqdone;
213 crfree(cred);
214 return (error);
215 }
216
217 #ifndef NFS_V2_ONLY
218 /*
219 * nfs version 3 fsinfo rpc call
220 */
221 int
222 nfs_fsinfo(nmp, vp, cred, p)
223 struct nfsmount *nmp;
224 struct vnode *vp;
225 struct ucred *cred;
226 struct proc *p;
227 {
228 struct nfsv3_fsinfo *fsp;
229 caddr_t cp;
230 int32_t t1, t2;
231 u_int32_t *tl, pref, max;
232 caddr_t bpos, dpos, cp2;
233 int error = 0, retattr;
234 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
235 u_int64_t maxfsize;
236
237 nfsstats.rpccnt[NFSPROC_FSINFO]++;
238 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
239 nfsm_fhtom(vp, 1);
240 nfsm_request(vp, NFSPROC_FSINFO, p, cred);
241 nfsm_postop_attr(vp, retattr);
242 if (!error) {
243 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
244 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
245 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
246 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
247 ~(NFS_FABLKSIZE - 1);
248 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
249 if (max < nmp->nm_wsize && max > 0) {
250 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
251 if (nmp->nm_wsize == 0)
252 nmp->nm_wsize = max;
253 }
254 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
255 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
256 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
257 ~(NFS_FABLKSIZE - 1);
258 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
259 if (max < nmp->nm_rsize && max > 0) {
260 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
261 if (nmp->nm_rsize == 0)
262 nmp->nm_rsize = max;
263 }
264 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
265 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
266 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
267 ~(NFS_DIRFRAGSIZ - 1);
268 if (max < nmp->nm_readdirsize && max > 0) {
269 nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
270 if (nmp->nm_readdirsize == 0)
271 nmp->nm_readdirsize = max;
272 }
273 /* XXX */
274 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
275 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
276 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
277 nmp->nm_maxfilesize = maxfsize;
278 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
279 }
280 nfsm_reqdone;
281 return (error);
282 }
283 #endif
284
285 /*
286 * Mount a remote root fs via. NFS. It goes like this:
287 * - Call nfs_boot_init() to fill in the nfs_diskless struct
288 * - build the rootfs mount point and call mountnfs() to do the rest.
289 */
290 int
291 nfs_mountroot()
292 {
293 struct nfs_diskless *nd;
294 struct vattr attr;
295 struct mount *mp;
296 struct vnode *vp;
297 struct proc *procp;
298 long n;
299 int error;
300
301 procp = curproc->l_proc; /* XXX */
302
303 if (root_device->dv_class != DV_IFNET)
304 return (ENODEV);
305
306 /*
307 * XXX time must be non-zero when we init the interface or else
308 * the arp code will wedge. [Fixed now in if_ether.c]
309 * However, the NFS attribute cache gives false "hits" when
310 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
311 */
312 if (time.tv_sec < NFS_MAXATTRTIMO)
313 time.tv_sec = NFS_MAXATTRTIMO;
314
315 /*
316 * Call nfs_boot_init() to fill in the nfs_diskless struct.
317 * Side effect: Finds and configures a network interface.
318 */
319 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
320 memset((caddr_t)nd, 0, sizeof(*nd));
321 error = nfs_boot_init(nd, procp);
322 if (error) {
323 free(nd, M_NFSMNT);
324 return (error);
325 }
326
327 /*
328 * Create the root mount point.
329 */
330 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
331 if (error)
332 goto out;
333 printf("root on %s\n", nd->nd_root.ndm_host);
334
335 /*
336 * Link it into the mount list.
337 */
338 simple_lock(&mountlist_slock);
339 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
340 simple_unlock(&mountlist_slock);
341 rootvp = vp;
342 mp->mnt_vnodecovered = NULLVP;
343 vfs_unbusy(mp);
344
345 /* Get root attributes (for the time). */
346 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
347 if (error)
348 panic("nfs_mountroot: getattr for root");
349 n = attr.va_atime.tv_sec;
350 #ifdef DEBUG
351 printf("root time: 0x%lx\n", n);
352 #endif
353 inittodr(n);
354
355 out:
356 if (error)
357 nfs_boot_cleanup(nd, procp);
358 free(nd, M_NFSMNT);
359 return (error);
360 }
361
362 /*
363 * Internal version of mount system call for diskless setup.
364 * Separate function because we used to call it twice.
365 * (once for root and once for swap)
366 */
367 static int
368 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
369 struct nfs_dlmount *ndmntp;
370 const char *mntname; /* mount point name */
371 struct mount **mpp;
372 struct vnode **vpp;
373 struct proc *p;
374 {
375 struct mount *mp;
376 struct mbuf *m;
377 int error;
378
379 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
380
381 mp->mnt_op = &nfs_vfsops;
382
383 /*
384 * Historical practice expects NFS root file systems to
385 * be initially mounted r/w.
386 */
387 mp->mnt_flag &= ~MNT_RDONLY;
388
389 /* Get mbuf for server sockaddr. */
390 m = m_get(M_WAIT, MT_SONAME);
391 if (m == NULL)
392 panic("nfs_mountroot: mget soname for %s", mntname);
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, p);
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, p)
554 struct mount *mp;
555 const char *path;
556 void *data;
557 struct nameidata *ndp;
558 struct proc *p;
559 {
560 int error;
561 struct nfs_args args;
562 struct mbuf *nam;
563 struct vnode *vp;
564 char pth[MNAMELEN], hst[MNAMELEN];
565 size_t len;
566 u_char nfh[NFSX_V3FHMAX];
567
568 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
569 if (error)
570 return (error);
571 if (args.version != NFS_ARGSVERSION)
572 return (EPROGMISMATCH);
573 #ifdef NFS_V2_ONLY
574 if (args.flags & NFSMNT_NQNFS)
575 return (EPROGUNAVAIL);
576 if (args.flags & NFSMNT_NFSV3)
577 return (EPROGMISMATCH);
578 #endif
579 if (mp->mnt_flag & MNT_UPDATE) {
580 struct nfsmount *nmp = VFSTONFS(mp);
581
582 if (nmp == NULL)
583 return (EIO);
584 /*
585 * When doing an update, we can't change from or to
586 * v3 and/or nqnfs, or change cookie translation
587 */
588 args.flags = (args.flags &
589 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
590 (nmp->nm_flag &
591 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
592 nfs_decode_args(nmp, &args);
593 return (0);
594 }
595 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
596 return (EINVAL);
597 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
598 if (error)
599 return (error);
600 error = copyinstr(path, pth, MNAMELEN-1, &len);
601 if (error)
602 return (error);
603 memset(&pth[len], 0, MNAMELEN - len);
604 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
605 if (error)
606 return (error);
607 memset(&hst[len], 0, MNAMELEN - len);
608 /* sockargs() call must be after above copyin() calls */
609 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
610 if (error)
611 return (error);
612 args.fh = nfh;
613 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
614 return (error);
615 }
616
617 /*
618 * Common code for mount and mountroot
619 */
620 int
621 mountnfs(argp, mp, nam, pth, hst, vpp, p)
622 struct nfs_args *argp;
623 struct mount *mp;
624 struct mbuf *nam;
625 const char *pth, *hst;
626 struct vnode **vpp;
627 struct proc *p;
628 {
629 struct nfsmount *nmp;
630 struct nfsnode *np;
631 int error;
632 struct vattr attrs;
633 struct ucred *cr;
634
635 /*
636 * If the number of nfs iothreads to use has never
637 * been set, create a reasonable number of them.
638 */
639
640 if (nfs_niothreads < 0) {
641 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
642 nfs_getset_niothreads(TRUE);
643 }
644
645 if (mp->mnt_flag & MNT_UPDATE) {
646 nmp = VFSTONFS(mp);
647 /* update paths, file handles, etc, here XXX */
648 m_freem(nam);
649 return (0);
650 } else {
651 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
652 M_NFSMNT, M_WAITOK);
653 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
654 mp->mnt_data = (qaddr_t)nmp;
655 TAILQ_INIT(&nmp->nm_uidlruhead);
656 TAILQ_INIT(&nmp->nm_bufq);
657 }
658 vfs_getnewfsid(mp);
659 nmp->nm_mountp = mp;
660
661 #ifndef NFS_V2_ONLY
662 if (argp->flags & NFSMNT_NQNFS)
663 /*
664 * We have to set mnt_maxsymlink to a non-zero value so
665 * that COMPAT_43 routines will know that we are setting
666 * the d_type field in directories (and can zero it for
667 * unsuspecting binaries).
668 */
669 mp->mnt_maxsymlinklen = 1;
670 #endif
671
672 #ifndef NFS_V2_ONLY
673 if ((argp->flags & NFSMNT_NFSV3) == 0)
674 #endif
675 /*
676 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
677 * will fill this in.
678 */
679 nmp->nm_maxfilesize = 0xffffffffLL;
680
681 nmp->nm_timeo = NFS_TIMEO;
682 nmp->nm_retry = NFS_RETRANS;
683 nmp->nm_wsize = NFS_WSIZE;
684 nmp->nm_rsize = NFS_RSIZE;
685 nmp->nm_readdirsize = NFS_READDIRSIZE;
686 nmp->nm_numgrps = NFS_MAXGRPS;
687 nmp->nm_readahead = NFS_DEFRAHEAD;
688 nmp->nm_leaseterm = NQ_DEFLEASE;
689 nmp->nm_deadthresh = NQ_DEADTHRESH;
690 CIRCLEQ_INIT(&nmp->nm_timerhead);
691 nmp->nm_inprog = NULLVP;
692 #ifdef COMPAT_09
693 mp->mnt_stat.f_type = 2;
694 #else
695 mp->mnt_stat.f_type = 0;
696 #endif
697 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name,
698 MFSNAMELEN);
699 memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
700 memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
701 nmp->nm_nam = nam;
702
703 /* Set up the sockets and per-host congestion */
704 nmp->nm_sotype = argp->sotype;
705 nmp->nm_soproto = argp->proto;
706
707 nfs_decode_args(nmp, argp);
708
709 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
710 mp->mnt_dev_bshift = DEV_BSHIFT;
711
712 /*
713 * For Connection based sockets (TCP,...) defer the connect until
714 * the first request, in case the server is not responding.
715 */
716 if (nmp->nm_sotype == SOCK_DGRAM &&
717 (error = nfs_connect(nmp, (struct nfsreq *)0)))
718 goto bad;
719
720 /*
721 * This is silly, but it has to be set so that vinifod() works.
722 * We do not want to do an nfs_statfs() here since we can get
723 * stuck on a dead server and we are holding a lock on the mount
724 * point.
725 */
726 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
727 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
728 if (error)
729 goto bad;
730 *vpp = NFSTOV(np);
731 VOP_GETATTR(*vpp, &attrs, p->p_ucred, p);
732 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
733 cr = crget();
734 cr->cr_uid = attrs.va_uid;
735 cr->cr_gid = attrs.va_gid;
736 cr->cr_ngroups = 0;
737 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
738 crfree(cr);
739 }
740
741 /*
742 * A reference count is needed on the nfsnode representing the
743 * remote root. If this object is not persistent, then backward
744 * traversals of the mount point (i.e. "..") will not work if
745 * the nfsnode gets flushed out of the cache. Ufs does not have
746 * this problem, because one can identify root inodes by their
747 * number == ROOTINO (2). So, just unlock, but no rele.
748 */
749
750 nmp->nm_vnode = *vpp;
751 VOP_UNLOCK(*vpp, 0);
752
753 return (0);
754 bad:
755 nfs_disconnect(nmp);
756 free((caddr_t)nmp, M_NFSMNT);
757 m_freem(nam);
758 return (error);
759 }
760
761 /*
762 * unmount system call
763 */
764 int
765 nfs_unmount(mp, mntflags, p)
766 struct mount *mp;
767 int mntflags;
768 struct proc *p;
769 {
770 struct nfsmount *nmp;
771 struct vnode *vp;
772 int error, flags = 0;
773
774 if (mntflags & MNT_FORCE)
775 flags |= FORCECLOSE;
776 nmp = VFSTONFS(mp);
777 /*
778 * Goes something like this..
779 * - Check for activity on the root vnode (other than ourselves).
780 * - Call vflush() to clear out vnodes for this file system,
781 * except for the root vnode.
782 * - Decrement reference on the vnode representing remote root.
783 * - Close the socket
784 * - Free up the data structures
785 */
786 /*
787 * We need to decrement the ref. count on the nfsnode representing
788 * the remote root. See comment in mountnfs(). The VFS unmount()
789 * has done vput on this vnode, otherwise we would get deadlock!
790 */
791 vp = nmp->nm_vnode;
792 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
793 if (error != 0)
794 return error;
795
796 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
797 vput(vp);
798 return (EBUSY);
799 }
800
801 /*
802 * Must handshake with nqnfs_clientd() if it is active.
803 */
804 nmp->nm_iflag |= NFSMNT_DISMINPROG;
805 while (nmp->nm_inprog != NULLVP)
806 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
807 error = vflush(mp, vp, flags);
808 if (error) {
809 vput(vp);
810 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
811 return (error);
812 }
813
814 /*
815 * We are now committed to the unmount; mark the mount structure
816 * as doomed so that any sleepers kicked awake by nfs_disconnect
817 * will go away cleanly.
818 */
819 nmp->nm_iflag |= NFSMNT_DISMNT;
820
821 /*
822 * There are two reference counts to get rid of here
823 * (see comment in mountnfs()).
824 */
825 vrele(vp);
826 vput(vp);
827 vgone(vp);
828 nfs_disconnect(nmp);
829 m_freem(nmp->nm_nam);
830
831 /*
832 * For NQNFS, let the server daemon free the nfsmount structure.
833 */
834 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
835 free((caddr_t)nmp, M_NFSMNT);
836 return (0);
837 }
838
839 /*
840 * Return root of a filesystem
841 */
842 int
843 nfs_root(mp, vpp)
844 struct mount *mp;
845 struct vnode **vpp;
846 {
847 struct vnode *vp;
848 struct nfsmount *nmp;
849 int error;
850
851 nmp = VFSTONFS(mp);
852 vp = nmp->nm_vnode;
853 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
854 if (error != 0)
855 return error;
856 if (vp->v_type == VNON)
857 vp->v_type = VDIR;
858 vp->v_flag = VROOT;
859 *vpp = vp;
860 return (0);
861 }
862
863 extern int syncprt;
864
865 /*
866 * Flush out the buffer cache
867 */
868 /* ARGSUSED */
869 int
870 nfs_sync(mp, waitfor, cred, p)
871 struct mount *mp;
872 int waitfor;
873 struct ucred *cred;
874 struct proc *p;
875 {
876 struct vnode *vp;
877 int error, allerror = 0;
878
879 /*
880 * Force stale buffer cache information to be flushed.
881 */
882 loop:
883 for (vp = mp->mnt_vnodelist.lh_first;
884 vp != NULL;
885 vp = vp->v_mntvnodes.le_next) {
886 /*
887 * If the vnode that we are about to sync is no longer
888 * associated with this mount point, start over.
889 */
890 if (vp->v_mount != mp)
891 goto loop;
892 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
893 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
894 vp->v_uobj.uo_npages == 0))
895 continue;
896 if (vget(vp, LK_EXCLUSIVE))
897 goto loop;
898 error = VOP_FSYNC(vp, cred,
899 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
900 if (error)
901 allerror = error;
902 vput(vp);
903 }
904 return (allerror);
905 }
906
907 /*
908 * NFS flat namespace lookup.
909 * Currently unsupported.
910 */
911 /* ARGSUSED */
912 int
913 nfs_vget(mp, ino, vpp)
914 struct mount *mp;
915 ino_t ino;
916 struct vnode **vpp;
917 {
918
919 return (EOPNOTSUPP);
920 }
921
922 /*
923 * Do that sysctl thang...
924 */
925 int
926 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
927 int *name;
928 u_int namelen;
929 void *oldp;
930 size_t *oldlenp;
931 void *newp;
932 size_t newlen;
933 struct proc *p;
934 {
935 int rv;
936
937 /*
938 * All names at this level are terminal.
939 */
940 if(namelen > 1)
941 return ENOTDIR; /* overloaded */
942
943 switch(name[0]) {
944 case NFS_NFSSTATS:
945 if(!oldp) {
946 *oldlenp = sizeof nfsstats;
947 return 0;
948 }
949
950 if(*oldlenp < sizeof nfsstats) {
951 *oldlenp = sizeof nfsstats;
952 return ENOMEM;
953 }
954
955 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
956 if(rv) return rv;
957
958 if(newp && newlen != sizeof nfsstats)
959 return EINVAL;
960
961 if(newp) {
962 return copyin(newp, &nfsstats, sizeof nfsstats);
963 }
964 return 0;
965
966 case NFS_IOTHREADS:
967 nfs_getset_niothreads(0);
968
969 rv = (sysctl_int(oldp, oldlenp, newp, newlen,
970 &nfs_niothreads));
971
972 if (newp)
973 nfs_getset_niothreads(1);
974
975 return rv;
976
977 default:
978 return EOPNOTSUPP;
979 }
980 }
981
982
983 /*
984 * At this point, this should never happen
985 */
986 /* ARGSUSED */
987 int
988 nfs_fhtovp(mp, fhp, vpp)
989 struct mount *mp;
990 struct fid *fhp;
991 struct vnode **vpp;
992 {
993
994 return (EINVAL);
995 }
996
997 /* ARGSUSED */
998 int
999 nfs_checkexp(mp, nam, exflagsp, credanonp)
1000 struct mount *mp;
1001 struct mbuf *nam;
1002 int *exflagsp;
1003 struct ucred **credanonp;
1004 {
1005
1006 return (EINVAL);
1007 }
1008
1009 /*
1010 * Vnode pointer to File handle, should never happen either
1011 */
1012 /* ARGSUSED */
1013 int
1014 nfs_vptofh(vp, fhp)
1015 struct vnode *vp;
1016 struct fid *fhp;
1017 {
1018
1019 return (EINVAL);
1020 }
1021
1022 /*
1023 * Vfs start routine, a no-op.
1024 */
1025 /* ARGSUSED */
1026 int
1027 nfs_start(mp, flags, p)
1028 struct mount *mp;
1029 int flags;
1030 struct proc *p;
1031 {
1032
1033 return (0);
1034 }
1035
1036 /*
1037 * Do operations associated with quotas, not supported
1038 */
1039 /* ARGSUSED */
1040 int
1041 nfs_quotactl(mp, cmd, uid, arg, p)
1042 struct mount *mp;
1043 int cmd;
1044 uid_t uid;
1045 caddr_t arg;
1046 struct proc *p;
1047 {
1048
1049 return (EOPNOTSUPP);
1050 }
1051