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