2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_bootp.h"
39 #include "opt_nfsroot.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mount.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
60 #include <vm/vm_extern.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
67 #include <rpc/rpcclnt.h>
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfsclient/nfsm_subs.h>
76 #include <nfsclient/nfsdiskless.h>
78 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
79 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
80 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
81 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
82 MALLOC_DEFINE(M_NFSDIRECTIO, "NFS DirectIO", "NFS Direct IO async write state");
84 uma_zone_t nfsmount_zone;
86 struct nfsstats nfsstats;
87 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
88 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RW,
89 &nfsstats, nfsstats, "S,nfsstats");
90 static int nfs_ip_paranoia = 1;
91 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW,
92 &nfs_ip_paranoia, 0, "");
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
97 static int nfs_tprintf_initial_delay = NFS_TPRINTF_INITIAL_DELAY;
98 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_INITIAL_DELAY,
99 downdelayinitial, CTLFLAG_RW, &nfs_tprintf_initial_delay, 0, "");
100 /* how long between console messages "nfs server foo not responding" */
101 static int nfs_tprintf_delay = NFS_TPRINTF_DELAY;
102 SYSCTL_INT(_vfs_nfs, NFS_TPRINTF_DELAY,
103 downdelayinterval, CTLFLAG_RW, &nfs_tprintf_delay, 0, "");
105 static int nfs_iosize(struct nfsmount *nmp);
106 static void nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp);
107 static int mountnfs(struct nfs_args *, struct mount *,
108 struct sockaddr *, char *, struct vnode **,
110 static vfs_mount_t nfs_mount;
111 static vfs_cmount_t nfs_cmount;
112 static vfs_unmount_t nfs_unmount;
113 static vfs_root_t nfs_root;
114 static vfs_statfs_t nfs_statfs;
115 static vfs_sync_t nfs_sync;
116 static vfs_sysctl_t nfs_sysctl;
119 * nfs vfs operations.
121 static struct vfsops nfs_vfsops = {
122 .vfs_init = nfs_init,
123 .vfs_mount = nfs_mount,
124 .vfs_cmount = nfs_cmount,
125 .vfs_root = nfs_root,
126 .vfs_statfs = nfs_statfs,
127 .vfs_sync = nfs_sync,
128 .vfs_uninit = nfs_uninit,
129 .vfs_unmount = nfs_unmount,
130 .vfs_sysctl = nfs_sysctl,
132 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
134 /* So that loader and kldload(2) can find us, wherever we are.. */
135 MODULE_VERSION(nfs, 1);
137 static struct nfs_rpcops nfs_rpcops = {
147 * This structure must be filled in by a primary bootstrap or bootstrap
148 * server for a diskless/dataless machine. It is initialized below just
149 * to ensure that it is allocated to initialized data (.data not .bss).
151 struct nfs_diskless nfs_diskless = { { { 0 } } };
152 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
153 int nfs_diskless_valid = 0;
155 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
156 &nfs_diskless_valid, 0, "");
158 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
159 nfsv3_diskless.root_hostnam, 0, "");
161 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
162 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
163 "%Ssockaddr_in", "");
166 void nfsargs_ntoh(struct nfs_args *);
167 static int nfs_mountdiskless(char *, int,
168 struct sockaddr_in *, struct nfs_args *,
169 struct thread *, struct vnode **, struct mount *);
170 static void nfs_convert_diskless(void);
171 static void nfs_convert_oargs(struct nfs_args *args,
172 struct onfs_args *oargs);
175 nfs_iosize(struct nfsmount *nmp)
180 * Calculate the size used for io buffers. Use the larger
181 * of the two sizes to minimise nfs requests but make sure
182 * that it is at least one VM page to avoid wasting buffer
185 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
186 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
191 nfs_convert_oargs(struct nfs_args *args, struct onfs_args *oargs)
194 args->version = NFS_ARGSVERSION;
195 args->addr = oargs->addr;
196 args->addrlen = oargs->addrlen;
197 args->sotype = oargs->sotype;
198 args->proto = oargs->proto;
199 args->fh = oargs->fh;
200 args->fhsize = oargs->fhsize;
201 args->flags = oargs->flags;
202 args->wsize = oargs->wsize;
203 args->rsize = oargs->rsize;
204 args->readdirsize = oargs->readdirsize;
205 args->timeo = oargs->timeo;
206 args->retrans = oargs->retrans;
207 args->maxgrouplist = oargs->maxgrouplist;
208 args->readahead = oargs->readahead;
209 args->deadthresh = oargs->deadthresh;
210 args->hostname = oargs->hostname;
214 nfs_convert_diskless(void)
217 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
218 sizeof(struct ifaliasreq));
219 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
220 sizeof(struct sockaddr_in));
221 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
222 if (nfsv3_diskless.root_args.flags & NFSMNT_NFSV3) {
223 nfsv3_diskless.root_fhsize = NFSX_V3FH;
224 bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V3FH);
226 nfsv3_diskless.root_fhsize = NFSX_V2FH;
227 bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V2FH);
229 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
230 sizeof(struct sockaddr_in));
231 bcopy(nfs_diskless.root_hostnam, nfsv3_diskless.root_hostnam, MNAMELEN);
232 nfsv3_diskless.root_time = nfs_diskless.root_time;
233 bcopy(nfs_diskless.my_hostnam, nfsv3_diskless.my_hostnam,
235 nfs_diskless_valid = 3;
242 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
245 struct nfs_statfs *sfp;
247 struct nfsmount *nmp = VFSTONFS(mp);
248 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
249 struct mbuf *mreq, *mrep, *md, *mb;
256 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np, LK_EXCLUSIVE);
260 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
261 (void)nfs_fsinfo(nmp, vp, td->td_ucred, td);
262 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
263 mreq = nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
265 bpos = mtod(mb, caddr_t);
267 nfsm_request(vp, NFSPROC_FSSTAT, td, td->td_ucred);
269 nfsm_postop_attr(vp, retattr);
275 sfp = nfsm_dissect(struct nfs_statfs *, NFSX_STATFS(v3));
276 sbp->f_iosize = nfs_iosize(nmp);
278 sbp->f_bsize = NFS_FABLKSIZE;
279 tquad = fxdr_hyper(&sfp->sf_tbytes);
280 sbp->f_blocks = tquad / NFS_FABLKSIZE;
281 tquad = fxdr_hyper(&sfp->sf_fbytes);
282 sbp->f_bfree = tquad / NFS_FABLKSIZE;
283 tquad = fxdr_hyper(&sfp->sf_abytes);
284 sbp->f_bavail = tquad / NFS_FABLKSIZE;
285 sbp->f_files = (fxdr_unsigned(int32_t,
286 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
287 sbp->f_ffree = (fxdr_unsigned(int32_t,
288 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
290 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
291 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
292 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
293 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
304 * nfs version 3 fsinfo rpc call
307 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
310 struct nfsv3_fsinfo *fsp;
313 int error = 0, retattr;
314 struct mbuf *mreq, *mrep, *md, *mb;
317 nfsstats.rpccnt[NFSPROC_FSINFO]++;
318 mreq = nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
320 bpos = mtod(mb, caddr_t);
322 nfsm_request(vp, NFSPROC_FSINFO, td, cred);
323 nfsm_postop_attr(vp, retattr);
325 fsp = nfsm_dissect(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
326 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
327 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
328 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
329 ~(NFS_FABLKSIZE - 1);
330 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
331 if (max < nmp->nm_wsize && max > 0) {
332 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
333 if (nmp->nm_wsize == 0)
336 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
337 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
338 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
339 ~(NFS_FABLKSIZE - 1);
340 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
341 if (max < nmp->nm_rsize && max > 0) {
342 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
343 if (nmp->nm_rsize == 0)
346 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
347 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
348 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
349 ~(NFS_DIRBLKSIZ - 1);
350 if (max < nmp->nm_readdirsize && max > 0) {
351 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
352 if (nmp->nm_readdirsize == 0)
353 nmp->nm_readdirsize = max;
355 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
356 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
357 nmp->nm_maxfilesize = maxfsize;
358 nmp->nm_mountp->mnt_stat.f_iosize = nfs_iosize(nmp);
359 nmp->nm_state |= NFSSTA_GOTFSINFO;
367 * Mount a remote root fs via. nfs. This depends on the info in the
368 * nfs_diskless structure that has been filled in properly by some primary
370 * It goes something like this:
371 * - do enough of "ifconfig" by calling ifioctl() so that the system
372 * can talk to the server
373 * - If nfs_diskless.mygateway is filled in, use that address as
375 * - build the rootfs mount point and call mountnfs() to do the rest.
377 * It is assumed to be safe to read, modify, and write the nfsv3_diskless
378 * structure, as well as other global NFS client variables here, as
379 * nfs_mountroot() will be called once in the boot before any other NFS
380 * client activity occurs.
383 nfs_mountroot(struct mount *mp, struct thread *td)
385 struct nfsv3_diskless *nd = &nfsv3_diskless;
396 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
397 bootpc_init(); /* use bootp to get nfs_diskless filled in */
398 #elif defined(NFS_ROOT)
399 nfs_setup_diskless();
402 if (nfs_diskless_valid == 0)
404 if (nfs_diskless_valid == 1)
405 nfs_convert_diskless();
408 * XXX splnet, so networks will receive...
413 * Do enough of ifconfig(8) so that the critical net interface can
414 * talk to the server.
416 error = socreate(nd->myif.ifra_addr.sa_family, &so, nd->root_args.sotype, 0,
419 panic("nfs_mountroot: socreate(%04x): %d",
420 nd->myif.ifra_addr.sa_family, error);
422 #if 0 /* XXX Bad idea */
424 * We might not have been told the right interface, so we pass
425 * over the first ten interfaces of the same kind, until we get
426 * one of them configured.
429 for (i = strlen(nd->myif.ifra_name) - 1;
430 nd->myif.ifra_name[i] >= '0' &&
431 nd->myif.ifra_name[i] <= '9';
432 nd->myif.ifra_name[i] ++) {
433 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
438 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
440 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
441 if ((cp = getenv("boot.netif.mtu")) != NULL) {
442 ir.ifr_mtu = strtol(cp, NULL, 10);
443 bcopy(nd->myif.ifra_name, ir.ifr_name, IFNAMSIZ);
445 error = ifioctl(so, SIOCSIFMTU, (caddr_t)&ir, td);
447 printf("nfs_mountroot: SIOCSIFMTU: %d", error);
452 * If the gateway field is filled in, set it as the default route.
453 * Note that pxeboot will set a default route of 0 if the route
454 * is not set by the DHCP server. Check also for a value of 0
455 * to avoid panicking inappropriately in that situation.
457 if (nd->mygateway.sin_len != 0 &&
458 nd->mygateway.sin_addr.s_addr != 0) {
459 struct sockaddr_in mask, sin;
461 bzero((caddr_t)&mask, sizeof(mask));
463 sin.sin_family = AF_INET;
464 sin.sin_len = sizeof(sin);
465 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
466 (struct sockaddr *)&nd->mygateway,
467 (struct sockaddr *)&mask,
468 RTF_UP | RTF_GATEWAY, NULL);
470 panic("nfs_mountroot: RTM_ADD: %d", error);
474 * Create the rootfs mount point.
476 nd->root_args.fh = nd->root_fh;
477 nd->root_args.fhsize = nd->root_fhsize;
478 l = ntohl(nd->root_saddr.sin_addr.s_addr);
479 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
480 (l >> 24) & 0xff, (l >> 16) & 0xff,
481 (l >> 8) & 0xff, (l >> 0) & 0xff, nd->root_hostnam);
482 printf("NFS ROOT: %s\n", buf);
483 if ((error = nfs_mountdiskless(buf, MNT_RDONLY,
484 &nd->root_saddr, &nd->root_args, td, &vp, mp)) != 0) {
489 * This is not really an nfs issue, but it is much easier to
490 * set hostname here and then let the "/etc/rc.xxx" files
491 * mount the right /var based upon its preset value.
493 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
494 hostname[MAXHOSTNAMELEN - 1] = '\0';
495 for (i = 0; i < MAXHOSTNAMELEN; i++)
496 if (hostname[i] == '\0')
498 inittodr(ntohl(nd->root_time));
503 * Internal version of mount system call for diskless setup.
506 nfs_mountdiskless(char *path, int mountflag,
507 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
508 struct vnode **vpp, struct mount *mp)
510 struct sockaddr *nam;
514 mp->mnt_kern_flag = 0;
515 mp->mnt_flag = mountflag;
517 nam = sodupsockaddr((struct sockaddr *)sin, M_WAITOK);
518 if ((error = mountnfs(args, mp, nam, path, vpp,
519 td->td_ucred)) != 0) {
520 printf("nfs_mountroot: mount %s on /: %d\n", path, error);
527 nfs_decode_args(struct mount *mp, struct nfsmount *nmp, struct nfs_args *argp)
536 * Set read-only flag if requested; otherwise, clear it if this is
537 * an update. If this is not an update, then either the read-only
538 * flag is already clear, or this is a root mount and it was set
539 * intentionally at some previous point.
541 if (vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL) == 0) {
543 mp->mnt_flag |= MNT_RDONLY;
545 } else if (mp->mnt_flag & MNT_UPDATE) {
547 mp->mnt_flag &= ~MNT_RDONLY;
552 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
553 * no sense in that context. Also, set up appropriate retransmit
554 * and soft timeout behavior.
556 if (argp->sotype == SOCK_STREAM) {
557 nmp->nm_flag &= ~NFSMNT_NOCONN;
558 nmp->nm_flag |= NFSMNT_DUMBTIMR;
559 nmp->nm_timeo = NFS_MAXTIMEO;
560 nmp->nm_retry = NFS_RETRANS_TCP;
563 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
564 if ((argp->flags & NFSMNT_NFSV3) == 0)
565 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
567 /* Re-bind if rsrvd port requested and wasn't on one */
568 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
569 && (argp->flags & NFSMNT_RESVPORT);
570 /* Also re-bind if we're switching to/from a connected UDP socket */
571 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
572 (argp->flags & NFSMNT_NOCONN));
574 /* Update flags atomically. Don't change the lock bits. */
575 nmp->nm_flag = argp->flags | nmp->nm_flag;
578 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
579 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
580 if (nmp->nm_timeo < NFS_MINTIMEO)
581 nmp->nm_timeo = NFS_MINTIMEO;
582 else if (nmp->nm_timeo > NFS_MAXTIMEO)
583 nmp->nm_timeo = NFS_MAXTIMEO;
586 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
587 nmp->nm_retry = argp->retrans;
588 if (nmp->nm_retry > NFS_MAXREXMIT)
589 nmp->nm_retry = NFS_MAXREXMIT;
592 if (argp->flags & NFSMNT_NFSV3) {
593 if (argp->sotype == SOCK_DGRAM)
594 maxio = NFS_MAXDGRAMDATA;
598 maxio = NFS_V2MAXDATA;
600 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
601 nmp->nm_wsize = argp->wsize;
602 /* Round down to multiple of blocksize */
603 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
604 if (nmp->nm_wsize <= 0)
605 nmp->nm_wsize = NFS_FABLKSIZE;
607 if (nmp->nm_wsize > maxio)
608 nmp->nm_wsize = maxio;
609 if (nmp->nm_wsize > MAXBSIZE)
610 nmp->nm_wsize = MAXBSIZE;
612 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
613 nmp->nm_rsize = argp->rsize;
614 /* Round down to multiple of blocksize */
615 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
616 if (nmp->nm_rsize <= 0)
617 nmp->nm_rsize = NFS_FABLKSIZE;
619 if (nmp->nm_rsize > maxio)
620 nmp->nm_rsize = maxio;
621 if (nmp->nm_rsize > MAXBSIZE)
622 nmp->nm_rsize = MAXBSIZE;
624 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
625 nmp->nm_readdirsize = argp->readdirsize;
627 if (nmp->nm_readdirsize > maxio)
628 nmp->nm_readdirsize = maxio;
629 if (nmp->nm_readdirsize > nmp->nm_rsize)
630 nmp->nm_readdirsize = nmp->nm_rsize;
632 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
633 nmp->nm_acregmin = argp->acregmin;
635 nmp->nm_acregmin = NFS_MINATTRTIMO;
636 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
637 nmp->nm_acregmax = argp->acregmax;
639 nmp->nm_acregmax = NFS_MAXATTRTIMO;
640 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
641 nmp->nm_acdirmin = argp->acdirmin;
643 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
644 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
645 nmp->nm_acdirmax = argp->acdirmax;
647 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
648 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
649 nmp->nm_acdirmin = nmp->nm_acdirmax;
650 if (nmp->nm_acregmin > nmp->nm_acregmax)
651 nmp->nm_acregmin = nmp->nm_acregmax;
653 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
654 if (argp->maxgrouplist <= NFS_MAXGRPS)
655 nmp->nm_numgrps = argp->maxgrouplist;
657 nmp->nm_numgrps = NFS_MAXGRPS;
659 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
660 if (argp->readahead <= NFS_MAXRAHEAD)
661 nmp->nm_readahead = argp->readahead;
663 nmp->nm_readahead = NFS_MAXRAHEAD;
665 if ((argp->flags & NFSMNT_WCOMMITSIZE) && argp->wcommitsize >= 0) {
666 if (argp->wcommitsize < nmp->nm_wsize)
667 nmp->nm_wcommitsize = nmp->nm_wsize;
669 nmp->nm_wcommitsize = argp->wcommitsize;
671 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
672 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
673 nmp->nm_deadthresh = argp->deadthresh;
675 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
678 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
679 (nmp->nm_soproto != argp->proto));
680 nmp->nm_sotype = argp->sotype;
681 nmp->nm_soproto = argp->proto;
683 if (nmp->nm_so && adjsock) {
684 nfs_safedisconnect(nmp);
685 if (nmp->nm_sotype == SOCK_DGRAM)
686 while (nfs_connect(nmp, NULL)) {
687 printf("nfs_args: retrying connect\n");
688 (void) tsleep((caddr_t)&lbolt,
694 static const char *nfs_opts[] = { "from", "nfs_args", NULL };
700 * It seems a bit dumb to copyinstr() the host and path here and then
701 * bcopy() them in mountnfs(), but I wanted to detect errors before
702 * doing the sockargs() call because sockargs() allocates an mbuf and
703 * an error after that means that I have to release the mbuf.
707 nfs_mount(struct mount *mp, struct thread *td)
710 struct nfs_args args;
711 struct sockaddr *nam;
715 u_char nfh[NFSX_V3FHMAX];
717 if (vfs_filteropt(mp->mnt_optnew, nfs_opts))
720 if (mp->mnt_flag & MNT_ROOTFS)
721 return (nfs_mountroot(mp, td));
723 error = vfs_copyopt(mp->mnt_optnew, "nfs_args", &args, sizeof args);
727 if (args.version != NFS_ARGSVERSION) {
728 return (EPROGMISMATCH);
730 if (mp->mnt_flag & MNT_UPDATE) {
731 struct nfsmount *nmp = VFSTONFS(mp);
736 * When doing an update, we can't change from or to
737 * v3, switch lockd strategies or change cookie translation
739 args.flags = (args.flags &
740 ~(NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/)) |
742 (NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/));
743 nfs_decode_args(mp, nmp, &args);
748 * Make the nfs_ip_paranoia sysctl serve as the default connection
749 * or no-connection mode for those protocols that support
750 * no-connection mode (the flag will be cleared later for protocols
751 * that do not support no-connection mode). This will allow a client
752 * to receive replies from a different IP then the request was
753 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid),
756 if (nfs_ip_paranoia == 0)
757 args.flags |= NFSMNT_NOCONN;
758 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
760 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
763 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
766 bzero(&hst[len], MNAMELEN - len);
767 /* sockargs() call must be after above copyin() calls */
768 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
772 error = mountnfs(&args, mp, nam, hst, &vp, td->td_ucred);
776 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
788 * It seems a bit dumb to copyinstr() the host and path here and then
789 * bcopy() them in mountnfs(), but I wanted to detect errors before
790 * doing the sockargs() call because sockargs() allocates an mbuf and
791 * an error after that means that I have to release the mbuf.
795 nfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
798 struct nfs_args args;
800 error = copyin(data, &args, sizeof (struct nfs_args));
804 ma = mount_arg(ma, "nfs_args", &args, sizeof args);
806 error = kernel_mount(ma, flags);
812 * Common code for mount and mountroot
815 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
816 char *hst, struct vnode **vpp, struct ucred *cred)
818 struct nfsmount *nmp;
823 if (mp->mnt_flag & MNT_UPDATE) {
825 /* update paths, file handles, etc, here XXX */
829 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
830 bzero((caddr_t)nmp, sizeof (struct nfsmount));
831 TAILQ_INIT(&nmp->nm_bufq);
832 mp->mnt_data = (qaddr_t)nmp;
838 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
839 * high, depending on whether we end up with negative offsets in
840 * the client or server somewhere. 2GB-1 may be safer.
842 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
843 * that we can handle until we find out otherwise.
844 * XXX Our "safe" limit on the client is what we can store in our
845 * buffer cache using signed(!) block numbers.
847 if ((argp->flags & NFSMNT_NFSV3) == 0)
848 nmp->nm_maxfilesize = 0xffffffffLL;
850 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
852 nmp->nm_timeo = NFS_TIMEO;
853 nmp->nm_retry = NFS_RETRANS;
854 if ((argp->flags & NFSMNT_NFSV3) && argp->sotype == SOCK_STREAM) {
855 nmp->nm_wsize = nmp->nm_rsize = NFS_MAXDATA;
857 nmp->nm_wsize = NFS_WSIZE;
858 nmp->nm_rsize = NFS_RSIZE;
860 nmp->nm_wcommitsize = hibufspace / (desiredvnodes / 1000);
861 nmp->nm_readdirsize = NFS_READDIRSIZE;
862 nmp->nm_numgrps = NFS_MAXGRPS;
863 nmp->nm_readahead = NFS_DEFRAHEAD;
864 nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
865 nmp->nm_tprintf_delay = nfs_tprintf_delay;
866 if (nmp->nm_tprintf_delay < 0)
867 nmp->nm_tprintf_delay = 0;
868 nmp->nm_tprintf_initial_delay = nfs_tprintf_initial_delay;
869 if (nmp->nm_tprintf_initial_delay < 0)
870 nmp->nm_tprintf_initial_delay = 0;
871 nmp->nm_fhsize = argp->fhsize;
872 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
873 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
875 /* Set up the sockets and per-host congestion */
876 nmp->nm_sotype = argp->sotype;
877 nmp->nm_soproto = argp->proto;
878 nmp->nm_rpcops = &nfs_rpcops;
880 nfs_decode_args(mp, nmp, argp);
882 if (nmp->nm_sotype == SOCK_STREAM)
883 mtx_init(&nmp->nm_nfstcpstate.mtx, "NFS/TCP state lock",
887 * For Connection based sockets (TCP,...) defer the connect until
888 * the first request, in case the server is not responding.
890 if (nmp->nm_sotype == SOCK_DGRAM &&
891 (error = nfs_connect(nmp, NULL)))
895 * This is silly, but it has to be set so that vinifod() works.
896 * We do not want to do an nfs_statfs() here since we can get
897 * stuck on a dead server and we are holding a lock on the mount
900 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
902 * A reference count is needed on the nfsnode representing the
903 * remote root. If this object is not persistent, then backward
904 * traversals of the mount point (i.e. "..") will not work if
905 * the nfsnode gets flushed out of the cache. Ufs does not have
906 * this problem, because one can identify root inodes by their
907 * number == ROOTINO (2).
909 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np, LK_EXCLUSIVE);
915 * Get file attributes and transfer parameters for the
916 * mountpoint. This has the side effect of filling in
917 * (*vpp)->v_type with the correct value.
919 if (argp->flags & NFSMNT_NFSV3)
920 nfs_fsinfo(nmp, *vpp, curthread->td_ucred, curthread);
922 VOP_GETATTR(*vpp, &attrs, curthread->td_ucred, curthread);
925 * Lose the lock but keep the ref.
927 VOP_UNLOCK(*vpp, 0, curthread);
931 if (nmp->nm_sotype == SOCK_STREAM)
932 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
934 uma_zfree(nfsmount_zone, nmp);
940 * unmount system call
943 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
945 struct nfsmount *nmp;
946 int error, flags = 0;
948 if (mntflags & MNT_FORCE)
952 * Goes something like this..
953 * - Call vflush() to clear out vnodes for this filesystem
955 * - Free up the data structures
957 /* In the forced case, cancel any outstanding requests. */
958 if (flags & FORCECLOSE) {
959 error = nfs_nmcancelreqs(nmp);
963 /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
964 error = vflush(mp, 1, flags, td);
969 * We are now committed to the unmount.
972 FREE(nmp->nm_nam, M_SONAME);
974 if (nmp->nm_sotype == SOCK_STREAM)
975 mtx_destroy(&nmp->nm_nfstcpstate.mtx);
977 uma_zfree(nfsmount_zone, nmp);
982 * Return root of a filesystem
985 nfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
988 struct nfsmount *nmp;
993 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np, flags);
998 * Get transfer parameters and attributes for root vnode once.
1000 if ((nmp->nm_state & NFSSTA_GOTFSINFO) == 0 &&
1001 (nmp->nm_flag & NFSMNT_NFSV3)) {
1002 nfs_fsinfo(nmp, vp, curthread->td_ucred, curthread);
1004 if (vp->v_type == VNON)
1006 vp->v_vflag |= VV_ROOT;
1012 * Flush out the buffer cache
1016 nfs_sync(struct mount *mp, int waitfor, struct thread *td)
1018 struct vnode *vp, *mvp;
1019 int error, allerror = 0;
1022 * Force stale buffer cache information to be flushed.
1026 MNT_VNODE_FOREACH(vp, mp, mvp) {
1029 if (VOP_ISLOCKED(vp, NULL) ||
1030 vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1031 waitfor == MNT_LAZY) {
1036 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
1038 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
1041 error = VOP_FSYNC(vp, waitfor, td);
1044 VOP_UNLOCK(vp, 0, td);
1054 nfs_sysctl(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
1056 struct nfsmount *nmp = VFSTONFS(mp);
1060 bzero(&vq, sizeof(vq));
1063 case VFS_CTL_NOLOCKS:
1064 val = (nmp->nm_flag & NFSMNT_NOLOCKS) ? 1 : 0;
1065 if (req->oldptr != NULL) {
1066 error = SYSCTL_OUT(req, &val, sizeof(val));
1070 if (req->newptr != NULL) {
1071 error = SYSCTL_IN(req, &val, sizeof(val));
1075 nmp->nm_flag |= NFSMNT_NOLOCKS;
1077 nmp->nm_flag &= ~NFSMNT_NOLOCKS;
1082 if (nmp->nm_state & NFSSTA_TIMEO)
1083 vq.vq_flags |= VQ_NOTRESP;
1085 if (!(nmp->nm_flag & NFSMNT_NOLOCKS) &&
1086 (nmp->nm_state & NFSSTA_LOCKTIMEO))
1087 vq.vq_flags |= VQ_NOTRESPLOCK;
1089 error = SYSCTL_OUT(req, &vq, sizeof(vq));
1092 if (req->oldptr != NULL) {
1093 error = SYSCTL_OUT(req, &nmp->nm_tprintf_initial_delay,
1094 sizeof(nmp->nm_tprintf_initial_delay));
1098 if (req->newptr != NULL) {
1099 error = vfs_suser(mp, req->td);
1102 error = SYSCTL_IN(req, &nmp->nm_tprintf_initial_delay,
1103 sizeof(nmp->nm_tprintf_initial_delay));
1106 if (nmp->nm_tprintf_initial_delay < 0)
1107 nmp->nm_tprintf_initial_delay = 0;