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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
37 * $FreeBSD: src/sys/nfs/nfs_vfsops.c,v 1.91.2.7 2003/01/27 20:04:08 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_vfsops.c,v 1.13 2004/03/01 06:33:21 dillon Exp $
41 #include "opt_bootp.h"
43 #include <sys/param.h>
44 #include <sys/sockio.h>
46 #include <sys/vnode.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_zone.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
70 #include "nfsm_subs.h"
71 #include "nfsdiskless.h"
74 extern int nfs_mountroot(struct mount *mp);
75 extern void bootpc_init(void);
79 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
80 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
81 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
82 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
83 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
84 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
85 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease");
86 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
88 vm_zone_t nfsmount_zone;
90 struct nfsstats nfsstats;
91 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
92 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
93 &nfsstats, nfsstats, "");
94 static int nfs_ip_paranoia = 1;
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW,
96 &nfs_ip_paranoia, 0, "");
99 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
102 static int nfs_iosize (struct nfsmount *nmp);
103 static void nfs_decode_args (struct nfsmount *nmp,
104 struct nfs_args *argp);
105 static int mountnfs (struct nfs_args *,struct mount *,
106 struct sockaddr *,char *,char *,struct vnode **);
107 static int nfs_mount ( struct mount *mp, char *path, caddr_t data,
108 struct nameidata *ndp, struct thread *td);
109 static int nfs_unmount ( struct mount *mp, int mntflags,
111 static int nfs_root ( struct mount *mp, struct vnode **vpp);
112 static int nfs_statfs ( struct mount *mp, struct statfs *sbp,
114 static int nfs_sync ( struct mount *mp, int waitfor,
118 * nfs vfs operations.
120 static struct vfsops nfs_vfsops = {
129 vfs_stdfhtovp, /* shouldn't happen */
131 vfs_stdvptofh, /* shouldn't happen */
136 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
139 * This structure must be filled in by a primary bootstrap or bootstrap
140 * server for a diskless/dataless machine. It is initialized below just
141 * to ensure that it is allocated to initialized data (.data not .bss).
143 struct nfs_diskless nfs_diskless = { { { 0 } } };
144 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
145 int nfs_diskless_valid = 0;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
148 &nfs_diskless_valid, 0, "");
150 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
151 nfsv3_diskless.root_hostnam, 0, "");
153 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
154 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
155 "%Ssockaddr_in", "");
157 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
158 nfsv3_diskless.swap_hostnam, 0, "");
160 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
161 &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr,
165 void nfsargs_ntoh (struct nfs_args *);
166 static int nfs_mountdiskless (char *, char *, int,
167 struct sockaddr_in *, struct nfs_args *,
168 struct thread *, struct vnode **,
170 static void nfs_convert_diskless (void);
171 static void nfs_convert_oargs (struct nfs_args *args,
172 struct onfs_args *oargs);
176 struct nfsmount* nmp;
181 * Calculate the size used for io buffers. Use the larger
182 * of the two sizes to minimise nfs requests but make sure
183 * that it is at least one VM page to avoid wasting buffer
186 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
187 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
192 nfs_convert_oargs(args, oargs)
193 struct nfs_args *args;
194 struct onfs_args *oargs;
196 args->version = NFS_ARGSVERSION;
197 args->addr = oargs->addr;
198 args->addrlen = oargs->addrlen;
199 args->sotype = oargs->sotype;
200 args->proto = oargs->proto;
201 args->fh = oargs->fh;
202 args->fhsize = oargs->fhsize;
203 args->flags = oargs->flags;
204 args->wsize = oargs->wsize;
205 args->rsize = oargs->rsize;
206 args->readdirsize = oargs->readdirsize;
207 args->timeo = oargs->timeo;
208 args->retrans = oargs->retrans;
209 args->maxgrouplist = oargs->maxgrouplist;
210 args->readahead = oargs->readahead;
211 args->leaseterm = oargs->leaseterm;
212 args->deadthresh = oargs->deadthresh;
213 args->hostname = oargs->hostname;
217 nfs_convert_diskless()
219 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
220 sizeof(struct ifaliasreq));
221 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
222 sizeof(struct sockaddr_in));
223 nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args);
224 nfsv3_diskless.swap_fhsize = NFSX_V2FH;
225 bcopy(nfs_diskless.swap_fh,nfsv3_diskless.swap_fh,NFSX_V2FH);
226 bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr,
227 sizeof(struct sockaddr_in));
228 bcopy(nfs_diskless.swap_hostnam,nfsv3_diskless.swap_hostnam, MNAMELEN);
229 nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks;
230 bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred,
231 sizeof(struct ucred));
232 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
233 nfsv3_diskless.root_fhsize = NFSX_V2FH;
234 bcopy(nfs_diskless.root_fh,nfsv3_diskless.root_fh,NFSX_V2FH);
235 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
236 sizeof(struct sockaddr_in));
237 bcopy(nfs_diskless.root_hostnam,nfsv3_diskless.root_hostnam, MNAMELEN);
238 nfsv3_diskless.root_time = nfs_diskless.root_time;
239 bcopy(nfs_diskless.my_hostnam,nfsv3_diskless.my_hostnam,
241 nfs_diskless_valid = 3;
248 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
251 struct nfs_statfs *sfp;
255 caddr_t bpos, dpos, cp2;
256 struct nfsmount *nmp = VFSTONFS(mp);
257 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
258 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
264 sfp = (struct nfs_statfs *)0;
266 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
271 cred->cr_ngroups = 1;
272 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
273 (void)nfs_fsinfo(nmp, vp, td);
274 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
275 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
277 nfsm_request(vp, NFSPROC_FSSTAT, td, cred);
279 nfsm_postop_attr(vp, retattr);
285 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
286 sbp->f_flags = nmp->nm_flag;
287 sbp->f_iosize = nfs_iosize(nmp);
289 sbp->f_bsize = NFS_FABLKSIZE;
290 tquad = fxdr_hyper(&sfp->sf_tbytes);
291 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
292 tquad = fxdr_hyper(&sfp->sf_fbytes);
293 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
294 tquad = fxdr_hyper(&sfp->sf_abytes);
295 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
296 sbp->f_files = (fxdr_unsigned(int32_t,
297 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
298 sbp->f_ffree = (fxdr_unsigned(int32_t,
299 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
301 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
302 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
303 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
304 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
308 if (sbp != &mp->mnt_stat) {
309 sbp->f_type = mp->mnt_vfc->vfc_typenum;
310 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
311 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
321 * nfs version 3 fsinfo rpc call
324 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct thread *td)
326 struct nfsv3_fsinfo *fsp;
329 u_int32_t *tl, pref, max;
330 caddr_t bpos, dpos, cp2;
331 int error = 0, retattr;
332 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
335 nfsstats.rpccnt[NFSPROC_FSINFO]++;
336 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
338 nfsm_request(vp, NFSPROC_FSINFO, td, nfs_vpcred(vp, ND_READ));
339 nfsm_postop_attr(vp, retattr);
341 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
342 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
343 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
344 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
345 ~(NFS_FABLKSIZE - 1);
346 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
347 if (max < nmp->nm_wsize && max > 0) {
348 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
349 if (nmp->nm_wsize == 0)
352 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
353 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
354 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
355 ~(NFS_FABLKSIZE - 1);
356 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
357 if (max < nmp->nm_rsize && max > 0) {
358 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
359 if (nmp->nm_rsize == 0)
362 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
363 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
364 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
365 ~(NFS_DIRBLKSIZ - 1);
366 if (max < nmp->nm_readdirsize && max > 0) {
367 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
368 if (nmp->nm_readdirsize == 0)
369 nmp->nm_readdirsize = max;
371 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
372 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
373 nmp->nm_maxfilesize = maxfsize;
374 nmp->nm_state |= NFSSTA_GOTFSINFO;
382 * Mount a remote root fs via. nfs. This depends on the info in the
383 * nfs_diskless structure that has been filled in properly by some primary
385 * It goes something like this:
386 * - do enough of "ifconfig" by calling ifioctl() so that the system
387 * can talk to the server
388 * - If nfs_diskless.mygateway is filled in, use that address as
390 * - build the rootfs mount point and call mountnfs() to do the rest.
396 struct mount *swap_mp;
397 struct nfsv3_diskless *nd = &nfsv3_diskless;
400 struct thread *td = curthread; /* XXX */
405 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
406 bootpc_init(); /* use bootp to get nfs_diskless filled in */
410 * XXX time must be non-zero when we init the interface or else
411 * the arp code will wedge...
413 while (mycpu->gd_time_seconds == 0)
414 tsleep(mycpu, 0, "arpkludge", 10);
416 if (nfs_diskless_valid==1)
417 nfs_convert_diskless();
420 * XXX splnet, so networks will receive...
425 /* Set up swap credentials. */
426 proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
427 proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
428 if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
430 proc0.p_ucred->cr_ngroups = NGROUPS;
431 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
432 proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
436 * Do enough of ifconfig(8) so that the critical net interface can
437 * talk to the server.
439 error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0, td);
441 panic("nfs_mountroot: socreate(%04x): %d",
442 nd->myif.ifra_addr.sa_family, error);
444 #if 0 /* XXX Bad idea */
446 * We might not have been told the right interface, so we pass
447 * over the first ten interfaces of the same kind, until we get
448 * one of them configured.
451 for (i = strlen(nd->myif.ifra_name) - 1;
452 nd->myif.ifra_name[i] >= '0' &&
453 nd->myif.ifra_name[i] <= '9';
454 nd->myif.ifra_name[i] ++) {
455 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
460 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
462 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
466 * If the gateway field is filled in, set it as the default route.
468 if (nd->mygateway.sin_len != 0) {
469 struct sockaddr_in mask, sin;
471 bzero((caddr_t)&mask, sizeof(mask));
473 sin.sin_family = AF_INET;
474 sin.sin_len = sizeof(sin);
475 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
476 (struct sockaddr *)&nd->mygateway,
477 (struct sockaddr *)&mask,
478 RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
480 panic("nfs_mountroot: RTM_ADD: %d", error);
484 * Create the rootfs mount point.
486 nd->root_args.fh = nd->root_fh;
487 nd->root_args.fhsize = nd->root_fhsize;
488 l = ntohl(nd->root_saddr.sin_addr.s_addr);
489 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
490 (l >> 24) & 0xff, (l >> 16) & 0xff,
491 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->root_hostnam);
492 printf("NFS ROOT: %s\n",buf);
493 if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
494 &nd->root_saddr, &nd->root_args, td, &vp, &mp)) != 0) {
496 mp->mnt_vfc->vfc_refcount--;
497 free(swap_mp, M_MOUNT);
503 if (nd->swap_nblks) {
505 /* Convert to DEV_BSIZE instead of Kilobyte */
509 * Create a fake mount point just for the swap vnode so that the
510 * swap file can be on a different server from the rootfs.
512 nd->swap_args.fh = nd->swap_fh;
513 nd->swap_args.fhsize = nd->swap_fhsize;
514 l = ntohl(nd->swap_saddr.sin_addr.s_addr);
515 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
516 (l >> 24) & 0xff, (l >> 16) & 0xff,
517 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->swap_hostnam);
518 printf("NFS SWAP: %s\n",buf);
519 if ((error = nfs_mountdiskless(buf, "/swap", 0,
520 &nd->swap_saddr, &nd->swap_args, td, &vp, &swap_mp)) != 0)
522 vfs_unbusy(swap_mp, td);
524 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size =
525 nd->swap_nblks * DEV_BSIZE ;
528 * Since the swap file is not the root dir of a file system,
529 * hack it to a regular file.
534 swaponvp(td, vp, NODEV, nd->swap_nblks);
537 mp->mnt_flag |= MNT_ROOTFS;
538 mp->mnt_vnodecovered = NULLVP;
543 * This is not really an nfs issue, but it is much easier to
544 * set hostname here and then let the "/etc/rc.xxx" files
545 * mount the right /var based upon its preset value.
547 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
548 hostname[MAXHOSTNAMELEN - 1] = '\0';
549 for (i = 0; i < MAXHOSTNAMELEN; i++)
550 if (hostname[i] == '\0')
552 inittodr(ntohl(nd->root_time));
557 * Internal version of mount system call for diskless setup.
560 nfs_mountdiskless(char *path, char *which, int mountflag,
561 struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
562 struct vnode **vpp, struct mount **mpp)
565 struct sockaddr *nam;
572 if ((error = vfs_rootmountalloc("nfs", path, &mp)) != 0) {
573 printf("nfs_mountroot: NFS not configured");
579 mp->mnt_kern_flag = 0;
580 mp->mnt_flag = mountflag;
581 nam = dup_sockaddr((struct sockaddr *)sin, 1);
582 if ((error = mountnfs(args, mp, nam, which, path, vpp)) != 0) {
583 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
584 mp->mnt_vfc->vfc_refcount--;
591 (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
597 nfs_decode_args(nmp, argp)
598 struct nfsmount *nmp;
599 struct nfs_args *argp;
607 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
608 * no sense in that context.
610 if (argp->sotype == SOCK_STREAM)
611 nmp->nm_flag &= ~NFSMNT_NOCONN;
613 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
614 if ((argp->flags & NFSMNT_NFSV3) == 0)
615 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
617 /* Re-bind if rsrvd port requested and wasn't on one */
618 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
619 && (argp->flags & NFSMNT_RESVPORT);
620 /* Also re-bind if we're switching to/from a connected UDP socket */
621 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
622 (argp->flags & NFSMNT_NOCONN));
624 /* Update flags atomically. Don't change the lock bits. */
625 nmp->nm_flag = argp->flags | nmp->nm_flag;
628 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
629 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
630 if (nmp->nm_timeo < NFS_MINTIMEO)
631 nmp->nm_timeo = NFS_MINTIMEO;
632 else if (nmp->nm_timeo > NFS_MAXTIMEO)
633 nmp->nm_timeo = NFS_MAXTIMEO;
636 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
637 nmp->nm_retry = argp->retrans;
638 if (nmp->nm_retry > NFS_MAXREXMIT)
639 nmp->nm_retry = NFS_MAXREXMIT;
642 if (argp->flags & NFSMNT_NFSV3) {
643 if (argp->sotype == SOCK_DGRAM)
644 maxio = NFS_MAXDGRAMDATA;
648 maxio = NFS_V2MAXDATA;
650 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
651 nmp->nm_wsize = argp->wsize;
652 /* Round down to multiple of blocksize */
653 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
654 if (nmp->nm_wsize <= 0)
655 nmp->nm_wsize = NFS_FABLKSIZE;
657 if (nmp->nm_wsize > maxio)
658 nmp->nm_wsize = maxio;
659 if (nmp->nm_wsize > MAXBSIZE)
660 nmp->nm_wsize = MAXBSIZE;
662 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
663 nmp->nm_rsize = argp->rsize;
664 /* Round down to multiple of blocksize */
665 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
666 if (nmp->nm_rsize <= 0)
667 nmp->nm_rsize = NFS_FABLKSIZE;
669 if (nmp->nm_rsize > maxio)
670 nmp->nm_rsize = maxio;
671 if (nmp->nm_rsize > MAXBSIZE)
672 nmp->nm_rsize = MAXBSIZE;
674 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
675 nmp->nm_readdirsize = argp->readdirsize;
677 if (nmp->nm_readdirsize > maxio)
678 nmp->nm_readdirsize = maxio;
679 if (nmp->nm_readdirsize > nmp->nm_rsize)
680 nmp->nm_readdirsize = nmp->nm_rsize;
682 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
683 nmp->nm_acregmin = argp->acregmin;
685 nmp->nm_acregmin = NFS_MINATTRTIMO;
686 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
687 nmp->nm_acregmax = argp->acregmax;
689 nmp->nm_acregmax = NFS_MAXATTRTIMO;
690 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
691 nmp->nm_acdirmin = argp->acdirmin;
693 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
694 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
695 nmp->nm_acdirmax = argp->acdirmax;
697 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
698 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
699 nmp->nm_acdirmin = nmp->nm_acdirmax;
700 if (nmp->nm_acregmin > nmp->nm_acregmax)
701 nmp->nm_acregmin = nmp->nm_acregmax;
703 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
704 if (argp->maxgrouplist <= NFS_MAXGRPS)
705 nmp->nm_numgrps = argp->maxgrouplist;
707 nmp->nm_numgrps = NFS_MAXGRPS;
709 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
710 if (argp->readahead <= NFS_MAXRAHEAD)
711 nmp->nm_readahead = argp->readahead;
713 nmp->nm_readahead = NFS_MAXRAHEAD;
715 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2) {
716 if (argp->leaseterm <= NQ_MAXLEASE)
717 nmp->nm_leaseterm = argp->leaseterm;
719 nmp->nm_leaseterm = NQ_MAXLEASE;
721 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1) {
722 if (argp->deadthresh <= NQ_NEVERDEAD)
723 nmp->nm_deadthresh = argp->deadthresh;
725 nmp->nm_deadthresh = NQ_NEVERDEAD;
728 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
729 (nmp->nm_soproto != argp->proto));
730 nmp->nm_sotype = argp->sotype;
731 nmp->nm_soproto = argp->proto;
733 if (nmp->nm_so && adjsock) {
734 nfs_safedisconnect(nmp);
735 if (nmp->nm_sotype == SOCK_DGRAM)
736 while (nfs_connect(nmp, (struct nfsreq *)0)) {
737 printf("nfs_args: retrying connect\n");
738 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
747 * It seems a bit dumb to copyinstr() the host and path here and then
748 * bcopy() them in mountnfs(), but I wanted to detect errors before
749 * doing the sockargs() call because sockargs() allocates an mbuf and
750 * an error after that means that I have to release the mbuf.
754 nfs_mount(struct mount *mp, char *path, caddr_t data,
755 struct nameidata *ndp, struct thread *td)
758 struct nfs_args args;
759 struct sockaddr *nam;
761 char pth[MNAMELEN], hst[MNAMELEN];
763 u_char nfh[NFSX_V3FHMAX];
769 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
772 if (args.version != NFS_ARGSVERSION) {
773 #ifdef COMPAT_PRELITE2
775 * If the argument version is unknown, then assume the
776 * caller is a pre-lite2 4.4BSD client and convert its
779 struct onfs_args oargs;
780 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
783 nfs_convert_oargs(&args,&oargs);
784 #else /* !COMPAT_PRELITE2 */
785 return (EPROGMISMATCH);
786 #endif /* COMPAT_PRELITE2 */
788 if (mp->mnt_flag & MNT_UPDATE) {
789 struct nfsmount *nmp = VFSTONFS(mp);
794 * When doing an update, we can't change from or to
795 * v3 and/or nqnfs, or change cookie translation
797 args.flags = (args.flags &
798 ~(NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)) |
800 (NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/));
801 nfs_decode_args(nmp, &args);
806 * Make the nfs_ip_paranoia sysctl serve as the default connection
807 * or no-connection mode for those protocols that support
808 * no-connection mode (the flag will be cleared later for protocols
809 * that do not support no-connection mode). This will allow a client
810 * to receive replies from a different IP then the request was
811 * sent to. Note: default value for nfs_ip_paranoia is 1 (paranoid),
814 if (nfs_ip_paranoia == 0)
815 args.flags |= NFSMNT_NOCONN;
816 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
818 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
821 error = copyinstr(path, pth, MNAMELEN-1, &len);
824 bzero(&pth[len], MNAMELEN - len);
825 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
828 bzero(&hst[len], MNAMELEN - len);
829 /* sockargs() call must be after above copyin() calls */
830 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
834 error = mountnfs(&args, mp, nam, pth, hst, &vp);
839 * Common code for mount and mountroot
842 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
843 char *pth, char *hst, struct vnode **vpp)
845 struct nfsmount *nmp;
850 if (mp->mnt_flag & MNT_UPDATE) {
852 /* update paths, file handles, etc, here XXX */
856 nmp = zalloc(nfsmount_zone);
857 bzero((caddr_t)nmp, sizeof (struct nfsmount));
858 TAILQ_INIT(&nmp->nm_uidlruhead);
859 TAILQ_INIT(&nmp->nm_bufq);
860 mp->mnt_data = (qaddr_t)nmp;
864 if (argp->flags & NFSMNT_NQNFS)
866 * We have to set mnt_maxsymlink to a non-zero value so
867 * that COMPAT_43 routines will know that we are setting
868 * the d_type field in directories (and can zero it for
869 * unsuspecting binaries).
871 mp->mnt_maxsymlinklen = 1;
874 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
875 * high, depending on whether we end up with negative offsets in
876 * the client or server somewhere. 2GB-1 may be safer.
878 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
879 * that we can handle until we find out otherwise.
880 * XXX Our "safe" limit on the client is what we can store in our
881 * buffer cache using signed(!) block numbers.
883 if ((argp->flags & NFSMNT_NFSV3) == 0)
884 nmp->nm_maxfilesize = 0xffffffffLL;
886 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
888 nmp->nm_timeo = NFS_TIMEO;
889 nmp->nm_retry = NFS_RETRANS;
890 nmp->nm_wsize = NFS_WSIZE;
891 nmp->nm_rsize = NFS_RSIZE;
892 nmp->nm_readdirsize = NFS_READDIRSIZE;
893 nmp->nm_numgrps = NFS_MAXGRPS;
894 nmp->nm_readahead = NFS_DEFRAHEAD;
895 nmp->nm_leaseterm = NQ_DEFLEASE;
896 nmp->nm_deadthresh = NQ_DEADTHRESH;
897 CIRCLEQ_INIT(&nmp->nm_timerhead);
898 nmp->nm_inprog = NULLVP;
899 nmp->nm_fhsize = argp->fhsize;
900 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
901 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
902 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
904 /* Set up the sockets and per-host congestion */
905 nmp->nm_sotype = argp->sotype;
906 nmp->nm_soproto = argp->proto;
907 nmp->nm_cred = crhold(proc0.p_ucred);
909 nfs_decode_args(nmp, argp);
912 * For Connection based sockets (TCP,...) defer the connect until
913 * the first request, in case the server is not responding.
915 if (nmp->nm_sotype == SOCK_DGRAM &&
916 (error = nfs_connect(nmp, (struct nfsreq *)0)))
920 * This is silly, but it has to be set so that vinifod() works.
921 * We do not want to do an nfs_statfs() here since we can get
922 * stuck on a dead server and we are holding a lock on the mount
925 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
927 * A reference count is needed on the nfsnode representing the
928 * remote root. If this object is not persistent, then backward
929 * traversals of the mount point (i.e. "..") will not work if
930 * the nfsnode gets flushed out of the cache. Ufs does not have
931 * this problem, because one can identify root inodes by their
932 * number == ROOTINO (2).
934 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
940 * Get file attributes for the mountpoint. This has the side
941 * effect of filling in (*vpp)->v_type with the correct value.
943 VOP_GETATTR(*vpp, &attrs, curthread);
946 * Lose the lock but keep the ref.
948 VOP_UNLOCK(*vpp, NULL, 0, curthread);
959 * unmount system call
962 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
964 struct nfsmount *nmp;
965 int error, flags = 0;
967 if (mntflags & MNT_FORCE)
971 * Goes something like this..
972 * - Call vflush() to clear out vnodes for this file system
974 * - Free up the data structures
976 /* In the forced case, cancel any outstanding requests. */
977 if (flags & FORCECLOSE) {
978 error = nfs_nmcancelreqs(nmp);
983 * Must handshake with nqnfs_clientd() if it is active.
985 nmp->nm_state |= NFSSTA_DISMINPROG;
986 while (nmp->nm_inprog != NULLVP)
987 (void) tsleep((caddr_t)&lbolt, 0, "nfsdism", 0);
989 /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
990 error = vflush(mp, 1, flags);
992 nmp->nm_state &= ~NFSSTA_DISMINPROG;
997 * We are now committed to the unmount.
998 * For NQNFS, let the server daemon free the nfsmount structure.
1000 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
1001 nmp->nm_state |= NFSSTA_DISMNT;
1003 nfs_disconnect(nmp);
1004 FREE(nmp->nm_nam, M_SONAME);
1006 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
1007 nfs_free_mount(nmp);
1012 nfs_free_mount(struct nfsmount *nmp)
1015 crfree(nmp->nm_cred);
1016 nmp->nm_cred = NULL;
1018 zfree(nfsmount_zone, nmp);
1022 * Return root of a filesystem
1030 struct nfsmount *nmp;
1035 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
1039 if (vp->v_type == VNON)
1049 * Flush out the buffer cache
1053 nfs_sync(struct mount *mp, int waitfor, struct thread *td)
1056 int error, allerror = 0;
1059 * Force stale buffer cache information to be flushed.
1062 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1064 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
1065 if (vp->v_flag & VPLACEMARKER) /* ZZZ */
1068 * If the vnode that we are about to sync is no longer
1069 * associated with this mount point, start over.
1071 if (vp->v_mount != mp)
1073 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1074 waitfor == MNT_LAZY)
1076 if (vget(vp, NULL, LK_EXCLUSIVE, td))
1078 error = VOP_FSYNC(vp, waitfor, td);