xref: /openbsd/sys/nfs/nfs_vfsops.c (revision aaaa6048)
1 /*	$OpenBSD: nfs_vfsops.c,v 1.131 2024/05/12 09:09:39 jsg Exp $	*/
2 /*	$NetBSD: nfs_vfsops.c,v 1.46.4.1 1996/05/25 22:40:35 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/lock.h>
43 #include <sys/kernel.h>
44 #include <sys/mount.h>
45 #include <sys/swap.h>
46 #include <sys/mbuf.h>
47 #include <sys/dirent.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/systm.h>
51 #include <sys/sysctl.h>
52 #include <sys/queue.h>
53 
54 #include <netinet/in.h>
55 
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfsnode.h>
58 #include <nfs/nfs.h>
59 #include <nfs/nfsmount.h>
60 #include <nfs/xdr_subs.h>
61 #include <nfs/nfsdiskless.h>
62 #include <nfs/nfs_var.h>
63 #include <nfs/nfsm_subs.h>
64 
65 extern struct nfsstats nfsstats;
66 extern int nfs_ticks;
67 
68 int	nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
69 	    struct proc *);
70 int	nfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
71 struct	mount *nfs_mount_diskless(struct nfs_dlmount *, char *, int,
72 	    struct vnode **, struct proc *p);
73 int	mountnfs(struct nfs_args *, struct mount *, struct mbuf *,
74 	    const char *, char *, struct vnode **, struct proc *p);
75 int	nfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *);
76 int	nfs_root(struct mount *, struct vnode **);
77 int	nfs_start(struct mount *, int, struct proc *);
78 int	nfs_statfs(struct mount *, struct statfs *, struct proc *);
79 int	nfs_sync(struct mount *, int, int, struct ucred *, struct proc *);
80 int	nfs_unmount(struct mount *, int, struct proc *);
81 void	nfs_reaper(void *);
82 int	nfs_vget(struct mount *, ino_t, struct vnode **);
83 int	nfs_vptofh(struct vnode *, struct fid *);
84 int	nfs_mountroot(void);
85 void	nfs_decode_args(struct nfsmount *, struct nfs_args *,
86 	    struct nfs_args *);
87 int	nfs_fhtovp(struct mount *, struct fid *, struct vnode **);
88 
89 /*
90  * nfs vfs operations.
91  */
92 const struct vfsops nfs_vfsops = {
93 	.vfs_mount	= nfs_mount,
94 	.vfs_start	= nfs_start,
95 	.vfs_unmount	= nfs_unmount,
96 	.vfs_root	= nfs_root,
97 	.vfs_quotactl	= nfs_quotactl,
98 	.vfs_statfs	= nfs_statfs,
99 	.vfs_sync	= nfs_sync,
100 	.vfs_vget	= nfs_vget,
101 	.vfs_fhtovp	= nfs_fhtovp,
102 	.vfs_vptofh	= nfs_vptofh,
103 	.vfs_init	= nfs_vfs_init,
104 	.vfs_sysctl	= nfs_sysctl,
105 	.vfs_checkexp	= nfs_checkexp,
106 };
107 
108 /*
109  * nfs statfs call
110  */
111 int
nfs_statfs(struct mount * mp,struct statfs * sbp,struct proc * p)112 nfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
113 {
114 	struct vnode *vp;
115 	struct nfs_statfs *sfp = NULL;
116 	struct nfsm_info	info;
117 	struct nfsmount *nmp = VFSTONFS(mp);
118 	int error = 0, retattr;
119 	struct ucred *cred;
120 	u_quad_t tquad;
121 
122 	info.nmi_v3 = (nmp->nm_flag & NFSMNT_NFSV3);
123 	info.nmi_errorp = &error;
124 
125 	error = nfs_root(mp, &vp);
126 	if (error)
127 		return (error);
128 	cred = crget();
129 	cred->cr_ngroups = 0;
130 	if (info.nmi_v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
131 		(void)nfs_fsinfo(nmp, vp, cred, p);
132 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
133 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(info.nmi_v3));
134 	nfsm_fhtom(&info, vp, info.nmi_v3);
135 
136 	info.nmi_procp = p;
137 	info.nmi_cred = cred;
138 	error = nfs_request(vp, NFSPROC_FSSTAT, &info);
139 	if (info.nmi_v3) {
140 		if (nfsm_postop_attr(&info, &vp, &retattr) != 0)
141 			goto nfsmout;
142 	}
143 	if (error) {
144 		m_freem(info.nmi_mrep);
145 		goto nfsmout;
146 	}
147 
148 	sfp = (struct nfs_statfs *)
149 	    nfsm_dissect(&info, NFSX_STATFS(info.nmi_v3));
150 	if (sfp == NULL)
151 		goto nfsmout;
152 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
153 	if (info.nmi_v3) {
154 		sbp->f_bsize = NFS_FABLKSIZE;
155 		tquad = fxdr_hyper(&sfp->sf_tbytes);
156 		sbp->f_blocks = tquad / (u_quad_t)NFS_FABLKSIZE;
157 		tquad = fxdr_hyper(&sfp->sf_fbytes);
158 		sbp->f_bfree = tquad / (u_quad_t)NFS_FABLKSIZE;
159 		tquad = fxdr_hyper(&sfp->sf_abytes);
160 		sbp->f_bavail = (quad_t)tquad / (quad_t)NFS_FABLKSIZE;
161 
162 		tquad = fxdr_hyper(&sfp->sf_tfiles);
163 		sbp->f_files = tquad;
164 		tquad = fxdr_hyper(&sfp->sf_ffiles);
165 		sbp->f_ffree = tquad;
166 		sbp->f_favail = tquad;
167 	} else {
168 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
169 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
170 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
171 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
172 		sbp->f_files = 0;
173 		sbp->f_ffree = 0;
174 		sbp->f_favail = 0;
175 	}
176 	copy_statfs_info(sbp, mp);
177 	m_freem(info.nmi_mrep);
178 nfsmout:
179 	vput(vp);
180 	crfree(cred);
181 	return (error);
182 }
183 
184 /*
185  * nfs version 3 fsinfo rpc call
186  */
187 int
nfs_fsinfo(struct nfsmount * nmp,struct vnode * vp,struct ucred * cred,struct proc * p)188 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
189     struct proc *p)
190 {
191 	struct nfsv3_fsinfo *fsp;
192 	struct nfsm_info	info;
193 	u_int32_t pref, max;
194 	int error = 0, retattr;
195 
196 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
197 	info.nmi_mb = info.nmi_mreq = nfsm_reqhead(NFSX_FH(1));
198 	nfsm_fhtom(&info, vp, 1);
199 
200 	info.nmi_procp = p;
201 	info.nmi_cred = cred;
202 	info.nmi_errorp = &error;
203 	error = nfs_request(vp, NFSPROC_FSINFO, &info);
204 
205 	if (nfsm_postop_attr(&info, &vp, &retattr) != 0)
206 		goto nfsmout;
207 	if (error) {
208 		m_freem(info.nmi_mrep);
209 		goto nfsmout;
210 	}
211 
212 	fsp = (struct nfsv3_fsinfo *)nfsm_dissect(&info, NFSX_V3FSINFO);
213 	if (fsp == NULL)
214 		goto nfsmout;
215 	pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
216 	if (pref < nmp->nm_wsize)
217 		nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
218 			~(NFS_FABLKSIZE - 1);
219 	max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
220 	if (max < nmp->nm_wsize) {
221 		nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
222 		if (nmp->nm_wsize == 0)
223 			nmp->nm_wsize = max;
224 	}
225 	pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
226 	if (pref < nmp->nm_rsize)
227 		nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
228 			~(NFS_FABLKSIZE - 1);
229 	max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
230 	if (max < nmp->nm_rsize) {
231 		nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
232 		if (nmp->nm_rsize == 0)
233 			nmp->nm_rsize = max;
234 	}
235 	pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
236 	if (pref < nmp->nm_readdirsize)
237 		nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
238 			~(NFS_DIRBLKSIZ - 1);
239 	if (max < nmp->nm_readdirsize) {
240 		nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
241 		if (nmp->nm_readdirsize == 0)
242 			nmp->nm_readdirsize = max;
243 	}
244 	nmp->nm_flag |= NFSMNT_GOTFSINFO;
245 
246 	m_freem(info.nmi_mrep);
247 nfsmout:
248 	return (error);
249 }
250 
251 struct nfs_diskless nfs_diskless;
252 
253 /*
254  * Mount a remote root fs via. NFS.  It goes like this:
255  * - Call nfs_boot_init() to fill in the nfs_diskless struct
256  *   (using RARP, bootparam RPC, mountd RPC)
257  * - hand craft the swap nfs vnode hanging off a fake mount point
258  *	if swdevt[0].sw_dev == NODEV
259  * - build the rootfs mount point and call mountnfs() to do the rest.
260  */
261 int
nfs_mountroot(void)262 nfs_mountroot(void)
263 {
264 	struct vattr attr;
265 	struct mount *mp;
266 	struct vnode *vp;
267 	struct proc *procp;
268 	long n;
269 	int error;
270 
271 	procp = curproc; /* XXX */
272 
273 	/*
274 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
275 	 * Side effect:	 Finds and configures a network interface.
276 	 */
277 	nfs_boot_init(&nfs_diskless, procp);
278 
279 	/*
280 	 * Create the root mount point.
281 	 */
282 	if (nfs_boot_getfh(&nfs_diskless.nd_boot, "root", &nfs_diskless.nd_root, -1))
283 		panic("nfs_mountroot: root");
284 	mp = nfs_mount_diskless(&nfs_diskless.nd_root, "/", 0, &vp, procp);
285 	printf("root on %s\n", nfs_diskless.nd_root.ndm_host);
286 
287 	/*
288 	 * Link it into the mount list.
289 	 */
290 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
291 	rootvp = vp;
292 	vfs_unbusy(mp);
293 
294 	/* Get root attributes (for the time). */
295 	error = VOP_GETATTR(rootvp, &attr, procp->p_ucred, procp);
296 	if (error) panic("nfs_mountroot: getattr for root");
297 	n = attr.va_atime.tv_sec;
298 #ifdef	DEBUG
299 	printf("root time: 0x%lx\n", n);
300 #endif
301 	inittodr(n);
302 
303 #ifdef notyet
304 	/* Set up swap credentials. */
305 	proc0.p_ucred->cr_uid = ntohl(nfs_diskless.swap_ucred.cr_uid);
306 	proc0.p_ucred->cr_gid = ntohl(nfs_diskless.swap_ucred.cr_gid);
307 	if ((proc0.p_ucred->cr_ngroups = ntohs(nfs_diskless.swap_ucred.cr_ngroups)) >
308 		NGROUPS_MAX)
309 		proc0.p_ucred->cr_ngroups = NGROUPS_MAX;
310 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
311 	    proc0.p_ucred->cr_groups[i] = ntohl(nfs_diskless.swap_ucred.cr_groups[i]);
312 #endif
313 
314 	/*
315 	 * "Mount" the swap device.
316 	 *
317 	 * On a "dataless" configuration (swap on disk) we will have:
318 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
319 	 */
320 	if (swdevt[0].sw_dev != NODEV) {
321 		if (bdevvp(swapdev, &swapdev_vp))
322 			panic("nfs_mountroot: can't setup swap vp");
323 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
324 		return (0);
325 	}
326 
327 	/*
328 	 * If swapping to an nfs node:	(swdevt[0].sw_dev == NODEV)
329 	 * Create a fake mount point just for the swap vnode so that the
330 	 * swap file can be on a different server from the rootfs.
331 	 *
332 	 * Wait 5 retries, finally no swap is cool. -mickey
333 	 */
334 	error = nfs_boot_getfh(&nfs_diskless.nd_boot, "swap", &nfs_diskless.nd_swap, 5);
335 	if (!error) {
336 		mp = nfs_mount_diskless(&nfs_diskless.nd_swap, "/swap", 0, &vp,
337 		    procp);
338 		vfs_unbusy(mp);
339 
340 		/*
341 		 * Since the swap file is not the root dir of a file system,
342 		 * hack it to a regular file.
343 		 */
344 		vp->v_type = VREG;
345 		vp->v_flag = 0;
346 
347 		/*
348 		 * Next line is a hack to make swapmount() work on NFS
349 		 * swap files.
350 		 */
351 		swdevt[0].sw_dev = NETDEV;
352 		/* end hack */
353 		nfs_diskless.sw_vp = vp;
354 
355 		/*
356 		 * Find out how large the swap file is.
357 		 */
358 		error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
359 		if (error)
360 			printf("nfs_mountroot: getattr for swap\n");
361 		n = (long) (attr.va_size >> DEV_BSHIFT);
362 
363 		printf("swap on %s\n", nfs_diskless.nd_swap.ndm_host);
364 #ifdef	DEBUG
365 		printf("swap size: 0x%lx (blocks)\n", n);
366 #endif
367 		return (0);
368 	}
369 
370 	printf("WARNING: no swap\n");
371 	swdevt[0].sw_dev = NODEV;
372 	return (0);
373 }
374 
375 /*
376  * Internal version of mount system call for diskless setup.
377  */
378 struct mount *
nfs_mount_diskless(struct nfs_dlmount * ndmntp,char * mntname,int mntflag,struct vnode ** vpp,struct proc * p)379 nfs_mount_diskless(struct nfs_dlmount *ndmntp, char *mntname, int mntflag,
380     struct vnode **vpp, struct proc *p)
381 {
382 	struct mount *mp;
383 	struct mbuf *m;
384 	int error;
385 
386 	if (vfs_rootmountalloc("nfs", mntname, &mp))
387 		panic("nfs_mount_diskless: vfs_rootmountalloc failed");
388 	mp->mnt_flag |= mntflag;
389 
390 	/* Get mbuf for server sockaddr. */
391 	m = m_get(M_WAIT, MT_SONAME);
392 	bcopy(ndmntp->ndm_args.addr, mtod(m, caddr_t),
393 	    (m->m_len = ndmntp->ndm_args.addr->sa_len));
394 
395 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
396 	    ndmntp->ndm_args.hostname, vpp, p);
397 	if (error)
398 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
399 
400 	return (mp);
401 }
402 
403 void
nfs_decode_args(struct nfsmount * nmp,struct nfs_args * argp,struct nfs_args * nargp)404 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp,
405     struct nfs_args *nargp)
406 {
407 	int adjsock = 0;
408 	int maxio;
409 
410 #if 0
411 	/* Re-bind if rsrvd port requested and wasn't on one */
412 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
413 		  && (argp->flags & NFSMNT_RESVPORT);
414 #endif
415 	/* Also re-bind if we're switching to/from a connected UDP socket */
416 	adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
417 	    (argp->flags & NFSMNT_NOCONN));
418 
419 	nmp->nm_flag =
420 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
421 
422 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
423 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
424 		if (nmp->nm_timeo < NFS_MINTIMEO)
425 			nmp->nm_timeo = NFS_MINTIMEO;
426 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
427 			nmp->nm_timeo = NFS_MAXTIMEO;
428 	}
429 
430 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1)
431 		nmp->nm_retry = MIN(argp->retrans, NFS_MAXREXMIT);
432 	if (!(nmp->nm_flag & NFSMNT_SOFT))
433 		nmp->nm_retry = NFS_MAXREXMIT + 1; /* past clip limit */
434 
435 	if (argp->flags & NFSMNT_NFSV3) {
436 		if (argp->sotype == SOCK_DGRAM)
437 			maxio = NFS_MAXDGRAMDATA;
438 		else
439 			maxio = NFS_MAXDATA;
440 	} else
441 		maxio = NFS_V2MAXDATA;
442 
443 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
444 		int osize = nmp->nm_wsize;
445 		nmp->nm_wsize = argp->wsize;
446 		/* Round down to multiple of blocksize */
447 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
448 		if (nmp->nm_wsize <= 0)
449 			nmp->nm_wsize = NFS_FABLKSIZE;
450 		adjsock |= (nmp->nm_wsize != osize);
451 	}
452 	if (nmp->nm_wsize > maxio)
453 		nmp->nm_wsize = maxio;
454 	if (nmp->nm_wsize > MAXBSIZE)
455 		nmp->nm_wsize = MAXBSIZE;
456 
457 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
458 		int osize = nmp->nm_rsize;
459 		nmp->nm_rsize = argp->rsize;
460 		/* Round down to multiple of blocksize */
461 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
462 		if (nmp->nm_rsize <= 0)
463 			nmp->nm_rsize = NFS_FABLKSIZE;
464 		adjsock |= (nmp->nm_rsize != osize);
465 	}
466 	if (nmp->nm_rsize > maxio)
467 		nmp->nm_rsize = maxio;
468 	if (nmp->nm_rsize > MAXBSIZE)
469 		nmp->nm_rsize = MAXBSIZE;
470 
471 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
472 		nmp->nm_readdirsize = argp->readdirsize;
473 		/* Round down to multiple of blocksize */
474 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
475 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
476 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
477 	} else if (argp->flags & NFSMNT_RSIZE)
478 		nmp->nm_readdirsize = nmp->nm_rsize;
479 
480 	if (nmp->nm_readdirsize > maxio)
481 		nmp->nm_readdirsize = maxio;
482 
483 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
484 		argp->maxgrouplist <= NFS_MAXGRPS)
485 		nmp->nm_numgrps = argp->maxgrouplist;
486 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
487 		argp->readahead <= NFS_MAXRAHEAD)
488 		nmp->nm_readahead = argp->readahead;
489 	if (argp->flags & NFSMNT_ACREGMIN && argp->acregmin >= 0) {
490 		if (argp->acregmin > 0xffff)
491 			nmp->nm_acregmin = 0xffff;
492 		else
493 			nmp->nm_acregmin = argp->acregmin;
494 	}
495 	if (argp->flags & NFSMNT_ACREGMAX && argp->acregmax >= 0) {
496 		if (argp->acregmax > 0xffff)
497 			nmp->nm_acregmax = 0xffff;
498 		else
499 			nmp->nm_acregmax = argp->acregmax;
500 	}
501 	if (nmp->nm_acregmin > nmp->nm_acregmax)
502 	  nmp->nm_acregmin = nmp->nm_acregmax;
503 
504 	if (argp->flags & NFSMNT_ACDIRMIN && argp->acdirmin >= 0) {
505 		if (argp->acdirmin > 0xffff)
506 			nmp->nm_acdirmin = 0xffff;
507 		else
508 			nmp->nm_acdirmin = argp->acdirmin;
509 	}
510 	if (argp->flags & NFSMNT_ACDIRMAX && argp->acdirmax >= 0) {
511 		if (argp->acdirmax > 0xffff)
512 			nmp->nm_acdirmax = 0xffff;
513 		else
514 			nmp->nm_acdirmax = argp->acdirmax;
515 	}
516 	if (nmp->nm_acdirmin > nmp->nm_acdirmax)
517 	  nmp->nm_acdirmin = nmp->nm_acdirmax;
518 
519 	if (nmp->nm_so && adjsock) {
520 		nfs_disconnect(nmp);
521 		if (nmp->nm_sotype == SOCK_DGRAM)
522 			while (nfs_connect(nmp, NULL)) {
523 				printf("nfs_args: retrying connect\n");
524 				tsleep_nsec(&nowake, PSOCK, "nfscon",
525 				    SEC_TO_NSEC(1));
526 			}
527 	}
528 
529 	/* Update nargp based on nmp */
530 	nargp->wsize = nmp->nm_wsize;
531 	nargp->rsize = nmp->nm_rsize;
532 	nargp->readdirsize = nmp->nm_readdirsize;
533 	nargp->timeo = nmp->nm_timeo;
534 	nargp->retrans = nmp->nm_retry;
535 	nargp->maxgrouplist = nmp->nm_numgrps;
536 	nargp->readahead = nmp->nm_readahead;
537 	nargp->acregmin = nmp->nm_acregmin;
538 	nargp->acregmax = nmp->nm_acregmax;
539 	nargp->acdirmin = nmp->nm_acdirmin;
540 	nargp->acdirmax = nmp->nm_acdirmax;
541 }
542 
543 /*
544  * VFS Operations.
545  *
546  * mount system call
547  * It seems a bit dumb to copyinstr() the host here and then
548  * bcopy() it in mountnfs(), but I wanted to detect errors before
549  * doing the sockargs() call because sockargs() allocates an mbuf and
550  * an error after that means that I have to release the mbuf.
551  */
552 int
nfs_mount(struct mount * mp,const char * path,void * data,struct nameidata * ndp,struct proc * p)553 nfs_mount(struct mount *mp, const char *path, void *data,
554     struct nameidata *ndp, struct proc *p)
555 {
556 	int error;
557 	struct nfs_args *args = data;
558 	struct mbuf *nam;
559 	struct vnode *vp;
560 	char hst[MNAMELEN];
561 	size_t len;
562 	u_char nfh[NFSX_V3FHMAX];
563 
564 	if (args &&
565 	    (args->flags & (NFSMNT_NFSV3|NFSMNT_RDIRPLUS)) == NFSMNT_RDIRPLUS)
566 		return (EINVAL);
567 
568 	if (nfs_niothreads < 0) {
569 		nfs_niothreads = 4;
570 		nfs_getset_niothreads(1);
571 	}
572 
573 	if (mp->mnt_flag & MNT_UPDATE) {
574 		struct nfsmount *nmp = VFSTONFS(mp);
575 
576 		if (nmp == NULL)
577 			return (EIO);
578 		/*
579 		 * When doing an update, we can't change from or to
580 		 * v3.
581 		 */
582 		if (args) {
583 			args->flags = (args->flags & ~(NFSMNT_NFSV3)) |
584 			    (nmp->nm_flag & (NFSMNT_NFSV3));
585 			nfs_decode_args(nmp, args, &mp->mnt_stat.mount_info.nfs_args);
586 		}
587 		return (0);
588 	}
589 	if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
590 		return (EINVAL);
591 	error = copyin(args->fh, nfh, args->fhsize);
592 	if (error)
593 		return (error);
594 	error = copyinstr(args->hostname, hst, MNAMELEN-1, &len);
595 	if (error)
596 		return (error);
597 	memset(&hst[len], 0, MNAMELEN - len);
598 	/* sockargs() call must be after above copyin() calls */
599 	error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
600 	if (error)
601 		return (error);
602 	args->fh = nfh;
603 	error = mountnfs(args, mp, nam, path, hst, &vp, p);
604 	return (error);
605 }
606 
607 /*
608  * Common code for mount and mountroot
609  */
610 int
mountnfs(struct nfs_args * argp,struct mount * mp,struct mbuf * nam,const char * pth,char * hst,struct vnode ** vpp,struct proc * p)611 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam,
612     const char *pth, char *hst, struct vnode **vpp, struct proc *p)
613 {
614 	struct nfsmount *nmp;
615 	struct nfsnode *np;
616 	struct vnode *vp;
617 	struct vattr attr;
618 	int error;
619 
620 	if (mp->mnt_flag & MNT_UPDATE) {
621 		nmp = VFSTONFS(mp);
622 		/* update paths, file handles, etc, here	XXX */
623 		m_freem(nam);
624 		return (0);
625 	} else {
626 		nmp = malloc(sizeof(*nmp), M_NFSMNT,
627 		    M_WAITOK|M_ZERO);
628 		mp->mnt_data = nmp;
629 	}
630 
631 	vfs_getnewfsid(mp);
632 	nmp->nm_mountp = mp;
633 	nmp->nm_timeo = NFS_TIMEO;
634 	nmp->nm_retry = NFS_RETRANS;
635 	nmp->nm_wsize = NFS_WSIZE;
636 	nmp->nm_rsize = NFS_RSIZE;
637 	nmp->nm_readdirsize = NFS_READDIRSIZE;
638 	nmp->nm_numgrps = NFS_MAXGRPS;
639 	nmp->nm_readahead = NFS_DEFRAHEAD;
640 	nmp->nm_acregmin = NFS_MINATTRTIMO;
641 	nmp->nm_acregmax = NFS_MAXATTRTIMO;
642 	nmp->nm_acdirmin = NFS_MINATTRTIMO;
643 	nmp->nm_acdirmax = NFS_MAXATTRTIMO;
644 	mp->mnt_stat.f_namemax = MAXNAMLEN;
645 	memset(mp->mnt_stat.f_mntonname, 0, MNAMELEN);
646 	strlcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
647 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
648 	strlcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
649 	memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
650 	strlcpy(mp->mnt_stat.f_mntfromspec, hst, MNAMELEN);
651 	bcopy(argp, &mp->mnt_stat.mount_info.nfs_args, sizeof(*argp));
652 	nmp->nm_nam = nam;
653 	nfs_decode_args(nmp, argp, &mp->mnt_stat.mount_info.nfs_args);
654 
655 	nfs_ninit(nmp);
656 	TAILQ_INIT(&nmp->nm_reqsq);
657 	timeout_set_proc(&nmp->nm_rtimeout, nfs_timer, nmp);
658 
659 	/* Set up the sockets and per-host congestion */
660 	nmp->nm_sotype = argp->sotype;
661 	nmp->nm_soproto = argp->proto;
662 
663 	/*
664 	 * For Connection based sockets (TCP,...) defer the connect until
665 	 * the first request, in case the server is not responding.
666 	 */
667 	if (nmp->nm_sotype == SOCK_DGRAM &&
668 	    (error = nfs_connect(nmp, NULL)))
669 		goto bad;
670 
671 	/*
672 	 * This is silly, but it has to be set so that vinifod() works.
673 	 * We do not want to do an nfs_statfs() here since we can get
674 	 * stuck on a dead server and we are holding a lock on the mount
675 	 * point.
676 	 */
677 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
678 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
679 	if (error)
680 		goto bad;
681 	vp = NFSTOV(np);
682 	error = VOP_GETATTR(vp, &attr, p->p_ucred, p);
683 	if (error) {
684 		vput(vp);
685 		goto bad;
686 	}
687 
688 	/*
689 	 * A reference count is needed on the nfsnode representing the
690 	 * remote root.  If this object is not persistent, then backward
691 	 * traversals of the mount point (i.e. "..") will not work if
692 	 * the nfsnode gets flushed out of the cache. Ufs does not have
693 	 * this problem, because one can identify root inodes by their
694 	 * number == ROOTINO (2).  So, just unlock, but no rele.
695 	 */
696 	nmp->nm_vnode = vp;
697 	if (vp->v_type == VNON)
698 		vp->v_type = VDIR;
699 	vp->v_flag = VROOT;
700 	VOP_UNLOCK(vp);
701 	*vpp = vp;
702 
703 	return (0);
704 bad:
705 	nfs_disconnect(nmp);
706 	free(nmp, M_NFSMNT, sizeof(*nmp));
707 	m_freem(nam);
708 	return (error);
709 }
710 
711 /* unmount system call */
712 int
nfs_unmount(struct mount * mp,int mntflags,struct proc * p)713 nfs_unmount(struct mount *mp, int mntflags, struct proc *p)
714 {
715 	struct nfsmount *nmp;
716 	struct vnode *vp;
717 	int error, flags = 0;
718 
719 	nmp = VFSTONFS(mp);
720 	error = nfs_root(mp, &vp);
721 	if (error)
722 		return (error);
723 
724 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
725 		vput(vp);
726 		return (EBUSY);
727 	}
728 
729 	if (mntflags & MNT_FORCE)
730 		flags |= FORCECLOSE;
731 
732 	error = vflush(mp, vp, flags);
733 	if (error) {
734 		vput(vp);
735 		return (error);
736 	}
737 
738 	/*
739 	 * There are two references count to get rid of here: one
740 	 * from mountnfs() and one from nfs_root() above.
741 	 */
742 	vrele(vp);
743 	vput(vp);
744 	vgone(vp);
745 	nfs_disconnect(nmp);
746 	m_freem(nmp->nm_nam);
747 	timeout_del(&nmp->nm_rtimeout);
748 	timeout_set_proc(&nmp->nm_rtimeout, nfs_reaper, nmp);
749 	timeout_add(&nmp->nm_rtimeout, 0);
750 	mp->mnt_data = NULL;
751 	return (0);
752 }
753 
754 /*
755  * Delay nfs mount point free until pending or sleeping timeouts have finished.
756  */
757 void
nfs_reaper(void * arg)758 nfs_reaper(void *arg)
759 {
760 	struct nfsmount *nmp = arg;
761 
762 	free(nmp, M_NFSMNT, sizeof(*nmp));
763 }
764 
765 /*
766  * Return root of a filesystem
767  */
768 int
nfs_root(struct mount * mp,struct vnode ** vpp)769 nfs_root(struct mount *mp, struct vnode **vpp)
770 {
771 	struct vnode *vp;
772 	struct nfsmount *nmp;
773 	int error;
774 
775 	nmp = VFSTONFS(mp);
776 	vp = nmp->nm_vnode;
777 	vref(vp);
778 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
779 	if (error) {
780 		vrele(vp);
781 		return (error);
782 	}
783 	*vpp = vp;
784 	return (0);
785 }
786 
787 /*
788  * Flush out the buffer cache
789  */
790 int
nfs_sync(struct mount * mp,int waitfor,int stall,struct ucred * cred,struct proc * p)791 nfs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, struct proc *p)
792 {
793 	struct vnode *vp;
794 	int allerror = 0;
795 	int empty, error, s;
796 
797 	/*
798 	 * Don't traverse the vnode list if we want to skip all of them.
799 	 */
800 	if (waitfor == MNT_LAZY)
801 		return (allerror);
802 
803 	/*
804 	 * Force stale buffer cache information to be flushed.
805 	 */
806 loop:
807 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
808 		/*
809 		 * If the vnode that we are about to sync is no longer
810 		 * associated with this mount point, start over.
811 		 */
812 		if (vp->v_mount != mp)
813 			goto loop;
814 		if (VOP_ISLOCKED(vp))
815 			continue;
816 		s = splbio();
817 		empty = LIST_EMPTY(&vp->v_dirtyblkhd);
818 		splx(s);
819 		if (empty)
820 			continue;
821 		if (vget(vp, LK_EXCLUSIVE))
822 			goto loop;
823 		error = VOP_FSYNC(vp, cred, waitfor, p);
824 		if (error)
825 			allerror = error;
826 		vput(vp);
827 	}
828 
829 	return (allerror);
830 }
831 
832 /*
833  * NFS flat namespace lookup.
834  * Currently unsupported.
835  */
836 int
nfs_vget(struct mount * mp,ino_t ino,struct vnode ** vpp)837 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
838 {
839 
840 	return (EOPNOTSUPP);
841 }
842 
843 /*
844  * Do that sysctl thang...
845  */
846 int
nfs_sysctl(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen,struct proc * p)847 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
848     size_t newlen, struct proc *p)
849 {
850 	int rv;
851 
852 	/*
853 	 * All names at this level are terminal.
854 	 */
855 	if(namelen > 1)
856 		return ENOTDIR;	/* overloaded */
857 
858 	switch(name[0]) {
859 	case NFS_NFSSTATS:
860 		if(!oldp) {
861 			*oldlenp = sizeof nfsstats;
862 			return 0;
863 		}
864 
865 		if(*oldlenp < sizeof nfsstats) {
866 			*oldlenp = sizeof nfsstats;
867 			return ENOMEM;
868 		}
869 
870 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
871 		if(rv) return rv;
872 
873 		if(newp && newlen != sizeof nfsstats)
874 			return EINVAL;
875 
876 		if(newp) {
877 			return copyin(newp, &nfsstats, sizeof nfsstats);
878 		}
879 		return 0;
880 
881 	case NFS_NIOTHREADS:
882 		nfs_getset_niothreads(0);
883 
884 		rv = sysctl_int(oldp, oldlenp, newp, newlen, &nfs_niothreads);
885 		if (newp)
886 			nfs_getset_niothreads(1);
887 
888 		return rv;
889 
890 	default:
891 		return EOPNOTSUPP;
892 	}
893 }
894 
895 
896 /*
897  * At this point, this should never happen
898  */
899 int
nfs_fhtovp(struct mount * mp,struct fid * fhp,struct vnode ** vpp)900 nfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
901 {
902 	return (EINVAL);
903 }
904 
905 /*
906  * Vnode pointer to File handle, should never happen either
907  */
908 int
nfs_vptofh(struct vnode * vp,struct fid * fhp)909 nfs_vptofh(struct vnode *vp, struct fid *fhp)
910 {
911 	return (EINVAL);
912 }
913 
914 /*
915  * Vfs start routine, a no-op.
916  */
917 int
nfs_start(struct mount * mp,int flags,struct proc * p)918 nfs_start(struct mount *mp, int flags, struct proc *p)
919 {
920 	return (0);
921 }
922 
923 /*
924  * Do operations associated with quotas, not supported
925  */
926 int
nfs_quotactl(struct mount * mp,int cmd,uid_t uid,caddr_t arg,struct proc * p)927 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg, struct proc *p)
928 {
929 	return (EOPNOTSUPP);
930 }
931 
932 /*
933  * check export permission, not supported
934  */
935 /* ARGUSED */
936 int
nfs_checkexp(struct mount * mp,struct mbuf * nam,int * exflagsp,struct ucred ** credanonp)937 nfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
938     struct ucred **credanonp)
939 {
940 	return (EOPNOTSUPP);
941 }
942 
943