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
40 #include "opt_bootp.h"
42 #include <sys/param.h>
43 #include <sys/sockio.h>
45 #include <sys/vnode.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/systm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
60 #include <net/route.h>
61 #include <netinet/in.h>
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
66 #include <nfs/nfsnode.h>
67 #include <nfs/nfsmount.h>
68 #include <nfs/xdr_subs.h>
69 #include <nfs/nfsm_subs.h>
70 #include <nfs/nfsdiskless.h>
71 #include <nfs/nqnfs.h>
73 extern int nfs_mountroot __P((struct mount *mp));
77 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
78 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
79 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
80 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
81 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
82 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
83 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease");
84 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
86 vm_zone_t nfsmount_zone;
88 struct nfsstats nfsstats;
89 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
90 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
91 &nfsstats, nfsstats, "");
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
97 static int nfs_iosize __P((struct nfsmount *nmp));
98 static void nfs_decode_args __P((struct nfsmount *nmp,
99 struct nfs_args *argp));
100 static int mountnfs __P((struct nfs_args *,struct mount *,
101 struct sockaddr *,char *,char *,struct vnode **));
102 static int nfs_mount __P(( struct mount *mp, char *path, caddr_t data,
103 struct nameidata *ndp, struct proc *p));
104 static int nfs_unmount __P(( struct mount *mp, int mntflags,
106 static int nfs_root __P(( struct mount *mp, struct vnode **vpp));
107 static int nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
109 static int nfs_sync __P(( struct mount *mp, int waitfor,
110 struct ucred *cred, struct proc *p));
113 * nfs vfs operations.
115 static struct vfsops nfs_vfsops = {
124 vfs_stdfhtovp, /* shouldn't happen */
126 vfs_stdvptofh, /* shouldn't happen */
131 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
134 * This structure must be filled in by a primary bootstrap or bootstrap
135 * server for a diskless/dataless machine. It is initialized below just
136 * to ensure that it is allocated to initialized data (.data not .bss).
138 struct nfs_diskless nfs_diskless = { { { 0 } } };
139 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
140 int nfs_diskless_valid = 0;
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
143 &nfs_diskless_valid, 0, "");
145 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
146 nfsv3_diskless.root_hostnam, 0, "");
148 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
149 &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
150 "%Ssockaddr_in", "");
152 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
153 nfsv3_diskless.swap_hostnam, 0, "");
155 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
156 &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr,
160 void nfsargs_ntoh __P((struct nfs_args *));
161 static int nfs_mountdiskless __P((char *, char *, int,
162 struct sockaddr_in *, struct nfs_args *,
163 struct proc *, struct vnode **,
165 static void nfs_convert_diskless __P((void));
166 static void nfs_convert_oargs __P((struct nfs_args *args,
167 struct onfs_args *oargs));
171 struct nfsmount* nmp;
176 * Calculate the size used for io buffers. Use the larger
177 * of the two sizes to minimise nfs requests but make sure
178 * that it is at least one VM page to avoid wasting buffer
181 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
182 if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
187 nfs_convert_oargs(args, oargs)
188 struct nfs_args *args;
189 struct onfs_args *oargs;
191 args->version = NFS_ARGSVERSION;
192 args->addr = oargs->addr;
193 args->addrlen = oargs->addrlen;
194 args->sotype = oargs->sotype;
195 args->proto = oargs->proto;
196 args->fh = oargs->fh;
197 args->fhsize = oargs->fhsize;
198 args->flags = oargs->flags;
199 args->wsize = oargs->wsize;
200 args->rsize = oargs->rsize;
201 args->readdirsize = oargs->readdirsize;
202 args->timeo = oargs->timeo;
203 args->retrans = oargs->retrans;
204 args->maxgrouplist = oargs->maxgrouplist;
205 args->readahead = oargs->readahead;
206 args->leaseterm = oargs->leaseterm;
207 args->deadthresh = oargs->deadthresh;
208 args->hostname = oargs->hostname;
212 nfs_convert_diskless()
214 bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
215 sizeof(struct ifaliasreq));
216 bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
217 sizeof(struct sockaddr_in));
218 nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args);
219 nfsv3_diskless.swap_fhsize = NFSX_V2FH;
220 bcopy(nfs_diskless.swap_fh,nfsv3_diskless.swap_fh,NFSX_V2FH);
221 bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr,
222 sizeof(struct sockaddr_in));
223 bcopy(nfs_diskless.swap_hostnam,nfsv3_diskless.swap_hostnam, MNAMELEN);
224 nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks;
225 bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred,
226 sizeof(struct ucred));
227 nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
228 nfsv3_diskless.root_fhsize = NFSX_V2FH;
229 bcopy(nfs_diskless.root_fh,nfsv3_diskless.root_fh,NFSX_V2FH);
230 bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
231 sizeof(struct sockaddr_in));
232 bcopy(nfs_diskless.root_hostnam,nfsv3_diskless.root_hostnam, MNAMELEN);
233 nfsv3_diskless.root_time = nfs_diskless.root_time;
234 bcopy(nfs_diskless.my_hostnam,nfsv3_diskless.my_hostnam,
236 nfs_diskless_valid = 3;
243 nfs_statfs(mp, sbp, p)
245 register struct statfs *sbp;
248 register struct vnode *vp;
249 register struct nfs_statfs *sfp;
251 register u_int32_t *tl;
252 register int32_t t1, t2;
253 caddr_t bpos, dpos, cp2;
254 struct nfsmount *nmp = VFSTONFS(mp);
255 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
256 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
262 sfp = (struct nfs_statfs *)0;
264 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
269 cred->cr_ngroups = 1;
270 if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
271 (void)nfs_fsinfo(nmp, vp, cred, p);
272 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
273 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
275 nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
277 nfsm_postop_attr(vp, retattr);
283 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
284 sbp->f_flags = nmp->nm_flag;
285 sbp->f_iosize = nfs_iosize(nmp);
287 sbp->f_bsize = NFS_FABLKSIZE;
288 tquad = fxdr_hyper(&sfp->sf_tbytes);
289 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
290 tquad = fxdr_hyper(&sfp->sf_fbytes);
291 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
292 tquad = fxdr_hyper(&sfp->sf_abytes);
293 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
294 sbp->f_files = (fxdr_unsigned(int32_t,
295 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
296 sbp->f_ffree = (fxdr_unsigned(int32_t,
297 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
299 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
300 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
301 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
302 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
306 if (sbp != &mp->mnt_stat) {
307 sbp->f_type = mp->mnt_vfc->vfc_typenum;
308 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
309 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
318 * nfs version 3 fsinfo rpc call
321 nfs_fsinfo(nmp, vp, cred, p)
322 register struct nfsmount *nmp;
323 register struct vnode *vp;
327 register struct nfsv3_fsinfo *fsp;
329 register int32_t t1, t2;
330 register u_int32_t *tl, pref, max;
331 caddr_t bpos, dpos, cp2;
332 int error = 0, retattr;
333 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
336 nfsstats.rpccnt[NFSPROC_FSINFO]++;
337 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
339 nfsm_request(vp, NFSPROC_FSINFO, p, cred);
340 nfsm_postop_attr(vp, retattr);
342 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
343 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
344 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
345 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
346 ~(NFS_FABLKSIZE - 1);
347 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
348 if (max < nmp->nm_wsize && max > 0) {
349 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
350 if (nmp->nm_wsize == 0)
353 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
354 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
355 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
356 ~(NFS_FABLKSIZE - 1);
357 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
358 if (max < nmp->nm_rsize && max > 0) {
359 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
360 if (nmp->nm_rsize == 0)
363 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
364 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
365 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
366 ~(NFS_DIRBLKSIZ - 1);
367 if (max < nmp->nm_readdirsize && max > 0) {
368 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
369 if (nmp->nm_readdirsize == 0)
370 nmp->nm_readdirsize = max;
372 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
373 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
374 nmp->nm_maxfilesize = maxfsize;
375 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 proc *p = curproc; /* 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 (time_second == 0)
414 tsleep(&time_second, PZERO+8, "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, p);
441 panic("nfs_mountroot: socreate(%04x): %d",
442 nd->myif.ifra_addr.sa_family, error);
445 * We might not have been told the right interface, so we pass
446 * over the first ten interfaces of the same kind, until we get
447 * one of them configured.
450 for (i = strlen(nd->myif.ifra_name) - 1;
451 nd->myif.ifra_name[i] >= '0' &&
452 nd->myif.ifra_name[i] <= '9';
453 nd->myif.ifra_name[i] ++) {
454 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p);
459 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
463 * If the gateway field is filled in, set it as the default route.
465 if (nd->mygateway.sin_len != 0) {
466 struct sockaddr_in mask, sin;
468 bzero((caddr_t)&mask, sizeof(mask));
470 sin.sin_family = AF_INET;
471 sin.sin_len = sizeof(sin);
472 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
473 (struct sockaddr *)&nd->mygateway,
474 (struct sockaddr *)&mask,
475 RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
477 panic("nfs_mountroot: RTM_ADD: %d", error);
481 * Create the rootfs mount point.
483 nd->root_args.fh = nd->root_fh;
484 nd->root_args.fhsize = nd->root_fhsize;
485 l = ntohl(nd->root_saddr.sin_addr.s_addr);
486 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
487 (l >> 24) & 0xff, (l >> 16) & 0xff,
488 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->root_hostnam);
489 printf("NFS ROOT: %s\n",buf);
490 if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
491 &nd->root_saddr, &nd->root_args, p, &vp, &mp)) != 0) {
493 mp->mnt_vfc->vfc_refcount--;
494 free(swap_mp, M_MOUNT);
500 if (nd->swap_nblks) {
502 /* Convert to DEV_BSIZE instead of Kilobyte */
506 * Create a fake mount point just for the swap vnode so that the
507 * swap file can be on a different server from the rootfs.
509 nd->swap_args.fh = nd->swap_fh;
510 nd->swap_args.fhsize = nd->swap_fhsize;
511 l = ntohl(nd->swap_saddr.sin_addr.s_addr);
512 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
513 (l >> 24) & 0xff, (l >> 16) & 0xff,
514 (l >> 8) & 0xff, (l >> 0) & 0xff,nd->swap_hostnam);
515 printf("NFS SWAP: %s\n",buf);
516 if ((error = nfs_mountdiskless(buf, "/swap", 0,
517 &nd->swap_saddr, &nd->swap_args, p, &vp, &swap_mp)) != 0)
519 vfs_unbusy(swap_mp, p);
521 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size =
522 nd->swap_nblks * DEV_BSIZE ;
525 * Since the swap file is not the root dir of a file system,
526 * hack it to a regular file.
531 swaponvp(p, vp, NODEV, nd->swap_nblks);
534 mp->mnt_flag |= MNT_ROOTFS;
535 mp->mnt_vnodecovered = NULLVP;
540 * This is not really an nfs issue, but it is much easier to
541 * set hostname here and then let the "/etc/rc.xxx" files
542 * mount the right /var based upon its preset value.
544 bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
545 hostname[MAXHOSTNAMELEN - 1] = '\0';
546 for (i = 0; i < MAXHOSTNAMELEN; i++)
547 if (hostname[i] == '\0')
549 inittodr(ntohl(nd->root_time));
554 * Internal version of mount system call for diskless setup.
557 nfs_mountdiskless(path, which, mountflag, sin, args, p, vpp, mpp)
561 struct sockaddr_in *sin;
562 struct nfs_args *args;
568 struct sockaddr *nam;
573 if (!mp && (error = vfs_rootmountalloc("nfs", path, &mp))) {
574 printf("nfs_mountroot: NFS not configured");
578 mp->mnt_kern_flag = 0;
579 mp->mnt_flag = mountflag;
580 nam = dup_sockaddr((struct sockaddr *)sin, 1);
581 if ((error = mountnfs(args, mp, nam, which, path, vpp)) != 0) {
582 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
583 mp->mnt_vfc->vfc_refcount--;
589 (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
595 nfs_decode_args(nmp, argp)
596 struct nfsmount *nmp;
597 struct nfs_args *argp;
605 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
606 * no sense in that context.
608 if (argp->sotype == SOCK_STREAM)
609 nmp->nm_flag &= ~NFSMNT_NOCONN;
611 /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
612 if ((argp->flags & NFSMNT_NFSV3) == 0)
613 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
615 /* Re-bind if rsrvd port requested and wasn't on one */
616 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
617 && (argp->flags & NFSMNT_RESVPORT);
618 /* Also re-bind if we're switching to/from a connected UDP socket */
619 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
620 (argp->flags & NFSMNT_NOCONN));
622 /* Update flags atomically. Don't change the lock bits. */
623 nmp->nm_flag = argp->flags | nmp->nm_flag;
626 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
627 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
628 if (nmp->nm_timeo < NFS_MINTIMEO)
629 nmp->nm_timeo = NFS_MINTIMEO;
630 else if (nmp->nm_timeo > NFS_MAXTIMEO)
631 nmp->nm_timeo = NFS_MAXTIMEO;
634 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
635 nmp->nm_retry = argp->retrans;
636 if (nmp->nm_retry > NFS_MAXREXMIT)
637 nmp->nm_retry = NFS_MAXREXMIT;
640 if (argp->flags & NFSMNT_NFSV3) {
641 if (argp->sotype == SOCK_DGRAM)
642 maxio = NFS_MAXDGRAMDATA;
646 maxio = NFS_V2MAXDATA;
648 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
649 nmp->nm_wsize = argp->wsize;
650 /* Round down to multiple of blocksize */
651 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
652 if (nmp->nm_wsize <= 0)
653 nmp->nm_wsize = NFS_FABLKSIZE;
655 if (nmp->nm_wsize > maxio)
656 nmp->nm_wsize = maxio;
657 if (nmp->nm_wsize > MAXBSIZE)
658 nmp->nm_wsize = MAXBSIZE;
660 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
661 nmp->nm_rsize = argp->rsize;
662 /* Round down to multiple of blocksize */
663 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
664 if (nmp->nm_rsize <= 0)
665 nmp->nm_rsize = NFS_FABLKSIZE;
667 if (nmp->nm_rsize > maxio)
668 nmp->nm_rsize = maxio;
669 if (nmp->nm_rsize > MAXBSIZE)
670 nmp->nm_rsize = MAXBSIZE;
672 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
673 nmp->nm_readdirsize = argp->readdirsize;
675 if (nmp->nm_readdirsize > maxio)
676 nmp->nm_readdirsize = maxio;
677 if (nmp->nm_readdirsize > nmp->nm_rsize)
678 nmp->nm_readdirsize = nmp->nm_rsize;
680 if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
681 nmp->nm_acregmin = argp->acregmin;
683 nmp->nm_acregmin = NFS_MINATTRTIMO;
684 if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
685 nmp->nm_acregmax = argp->acregmax;
687 nmp->nm_acregmax = NFS_MAXATTRTIMO;
688 if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
689 nmp->nm_acdirmin = argp->acdirmin;
691 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
692 if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
693 nmp->nm_acdirmax = argp->acdirmax;
695 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
696 if (nmp->nm_acdirmin > nmp->nm_acdirmax)
697 nmp->nm_acdirmin = nmp->nm_acdirmax;
698 if (nmp->nm_acregmin > nmp->nm_acregmax)
699 nmp->nm_acregmin = nmp->nm_acregmax;
701 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
702 if (argp->maxgrouplist <= NFS_MAXGRPS)
703 nmp->nm_numgrps = argp->maxgrouplist;
705 nmp->nm_numgrps = NFS_MAXGRPS;
707 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
708 if (argp->readahead <= NFS_MAXRAHEAD)
709 nmp->nm_readahead = argp->readahead;
711 nmp->nm_readahead = NFS_MAXRAHEAD;
713 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2) {
714 if (argp->leaseterm <= NQ_MAXLEASE)
715 nmp->nm_leaseterm = argp->leaseterm;
717 nmp->nm_leaseterm = NQ_MAXLEASE;
719 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1) {
720 if (argp->deadthresh <= NQ_NEVERDEAD)
721 nmp->nm_deadthresh = argp->deadthresh;
723 nmp->nm_deadthresh = NQ_NEVERDEAD;
726 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
727 (nmp->nm_soproto != argp->proto));
728 nmp->nm_sotype = argp->sotype;
729 nmp->nm_soproto = argp->proto;
731 if (nmp->nm_so && adjsock) {
732 nfs_safedisconnect(nmp);
733 if (nmp->nm_sotype == SOCK_DGRAM)
734 while (nfs_connect(nmp, (struct nfsreq *)0)) {
735 printf("nfs_args: retrying connect\n");
736 (void) tsleep((caddr_t)&lbolt,
746 * It seems a bit dumb to copyinstr() the host and path here and then
747 * bcopy() them in mountnfs(), but I wanted to detect errors before
748 * doing the sockargs() call because sockargs() allocates an mbuf and
749 * an error after that means that I have to release the mbuf.
753 nfs_mount(mp, path, data, ndp, p)
757 struct nameidata *ndp;
761 struct nfs_args args;
762 struct sockaddr *nam;
764 char pth[MNAMELEN], hst[MNAMELEN];
766 u_char nfh[NFSX_V3FHMAX];
772 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
775 if (args.version != NFS_ARGSVERSION) {
776 #ifdef COMPAT_PRELITE2
778 * If the argument version is unknown, then assume the
779 * caller is a pre-lite2 4.4BSD client and convert its
782 struct onfs_args oargs;
783 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
786 nfs_convert_oargs(&args,&oargs);
787 #else /* !COMPAT_PRELITE2 */
788 return (EPROGMISMATCH);
789 #endif /* COMPAT_PRELITE2 */
791 if (mp->mnt_flag & MNT_UPDATE) {
792 register struct nfsmount *nmp = VFSTONFS(mp);
797 * When doing an update, we can't change from or to
798 * v3 and/or nqnfs, or change cookie translation
800 args.flags = (args.flags &
801 ~(NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)) |
803 (NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/));
804 nfs_decode_args(nmp, &args);
807 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
810 error = copyinstr(path, pth, MNAMELEN-1, &len);
813 bzero(&pth[len], MNAMELEN - len);
814 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
817 bzero(&hst[len], MNAMELEN - len);
818 /* sockargs() call must be after above copyin() calls */
819 error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
823 error = mountnfs(&args, mp, nam, pth, hst, &vp);
828 * Common code for mount and mountroot
831 mountnfs(argp, mp, nam, pth, hst, vpp)
832 register struct nfs_args *argp;
833 register struct mount *mp;
834 struct sockaddr *nam;
838 register struct nfsmount *nmp;
843 if (mp->mnt_flag & MNT_UPDATE) {
845 /* update paths, file handles, etc, here XXX */
849 nmp = zalloc(nfsmount_zone);
850 bzero((caddr_t)nmp, sizeof (struct nfsmount));
851 TAILQ_INIT(&nmp->nm_uidlruhead);
852 TAILQ_INIT(&nmp->nm_bufq);
853 mp->mnt_data = (qaddr_t)nmp;
857 if (argp->flags & NFSMNT_NQNFS)
859 * We have to set mnt_maxsymlink to a non-zero value so
860 * that COMPAT_43 routines will know that we are setting
861 * the d_type field in directories (and can zero it for
862 * unsuspecting binaries).
864 mp->mnt_maxsymlinklen = 1;
867 * V2 can only handle 32 bit filesizes. A 4GB-1 limit may be too
868 * high, depending on whether we end up with negative offsets in
869 * the client or server somewhere. 2GB-1 may be safer.
871 * For V3, nfs_fsinfo will adjust this as necessary. Assume maximum
872 * that we can handle until we find out otherwise.
873 * XXX Our "safe" limit on the client is what we can store in our
874 * buffer cache using signed(!) block numbers.
876 if ((argp->flags & NFSMNT_NFSV3) == 0)
877 nmp->nm_maxfilesize = 0xffffffffLL;
879 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
881 nmp->nm_timeo = NFS_TIMEO;
882 nmp->nm_retry = NFS_RETRANS;
883 nmp->nm_wsize = NFS_WSIZE;
884 nmp->nm_rsize = NFS_RSIZE;
885 nmp->nm_readdirsize = NFS_READDIRSIZE;
886 nmp->nm_numgrps = NFS_MAXGRPS;
887 nmp->nm_readahead = NFS_DEFRAHEAD;
888 nmp->nm_leaseterm = NQ_DEFLEASE;
889 nmp->nm_deadthresh = NQ_DEADTHRESH;
890 CIRCLEQ_INIT(&nmp->nm_timerhead);
891 nmp->nm_inprog = NULLVP;
892 nmp->nm_fhsize = argp->fhsize;
893 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
894 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
895 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
897 /* Set up the sockets and per-host congestion */
898 nmp->nm_sotype = argp->sotype;
899 nmp->nm_soproto = argp->proto;
901 nfs_decode_args(nmp, argp);
904 * For Connection based sockets (TCP,...) defer the connect until
905 * the first request, in case the server is not responding.
907 if (nmp->nm_sotype == SOCK_DGRAM &&
908 (error = nfs_connect(nmp, (struct nfsreq *)0)))
912 * This is silly, but it has to be set so that vinifod() works.
913 * We do not want to do an nfs_statfs() here since we can get
914 * stuck on a dead server and we are holding a lock on the mount
917 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
919 * A reference count is needed on the nfsnode representing the
920 * remote root. If this object is not persistent, then backward
921 * traversals of the mount point (i.e. "..") will not work if
922 * the nfsnode gets flushed out of the cache. Ufs does not have
923 * this problem, because one can identify root inodes by their
924 * number == ROOTINO (2).
926 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
932 * Get file attributes for the mountpoint. This has the side
933 * effect of filling in (*vpp)->v_type with the correct value.
935 VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc);
938 * Lose the lock but keep the ref.
940 VOP_UNLOCK(*vpp, 0, curproc);
945 zfree(nfsmount_zone, nmp);
951 * unmount system call
954 nfs_unmount(mp, mntflags, p)
959 register struct nfsmount *nmp;
962 int error, flags = 0;
964 if (mntflags & MNT_FORCE)
968 * Goes something like this..
969 * - Check for activity on the root vnode (other than ourselves).
970 * - Call vflush() to clear out vnodes for this file system,
971 * except for the root vnode.
972 * - Decrement reference on the vnode representing remote root.
974 * - Free up the data structures
977 * We need to decrement the ref. count on the nfsnode representing
978 * the remote root. See comment in mountnfs(). The VFS unmount()
979 * has done vput on this vnode, otherwise we would get deadlock!
981 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
985 if (vp->v_usecount > 2) {
991 * Must handshake with nqnfs_clientd() if it is active.
993 nmp->nm_state |= NFSSTA_DISMINPROG;
994 while (nmp->nm_inprog != NULLVP)
995 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
996 error = vflush(mp, vp, flags);
999 nmp->nm_state &= ~NFSSTA_DISMINPROG;
1004 * We are now committed to the unmount.
1005 * For NQNFS, let the server daemon free the nfsmount structure.
1007 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
1008 nmp->nm_state |= NFSSTA_DISMNT;
1011 * There are two reference counts and one lock to get rid of here.
1016 nfs_disconnect(nmp);
1017 FREE(nmp->nm_nam, M_SONAME);
1019 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
1020 zfree(nfsmount_zone, nmp);
1025 * Return root of a filesystem
1032 register struct vnode *vp;
1033 struct nfsmount *nmp;
1038 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
1042 if (vp->v_type == VNON)
1052 * Flush out the buffer cache
1056 nfs_sync(mp, waitfor, cred, p)
1062 register struct vnode *vp;
1063 int error, allerror = 0;
1066 * Force stale buffer cache information to be flushed.
1069 for (vp = mp->mnt_vnodelist.lh_first;
1071 vp = vp->v_mntvnodes.le_next) {
1073 * If the vnode that we are about to sync is no longer
1074 * associated with this mount point, start over.
1076 if (vp->v_mount != mp)
1078 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1079 waitfor == MNT_LAZY)
1081 if (vget(vp, LK_EXCLUSIVE, p))
1083 error = VOP_FSYNC(vp, cred, waitfor, p);