]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r98944,
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_vfsops.c
1 /*
2  * Copyright (c) 1989, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *      @(#)nfs_vfsops.c        8.12 (Berkeley) 5/20/95
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_bootp.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/module.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/vnode.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61 #include <vm/uma.h>
62
63 #include <net/if.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfsclient/nfsnode.h>
71 #include <nfsclient/nfsmount.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfsclient/nfsm_subs.h>
74 #include <nfsclient/nfsdiskless.h>
75
76 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
77 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
78 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
79 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
80
81 uma_zone_t nfsmount_zone;
82
83 struct nfsstats nfsstats;
84 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
85 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
86         &nfsstats, nfsstats, "S,nfsstats");
87 #ifdef NFS_DEBUG
88 int nfs_debug;
89 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
90 #endif
91
92 static int      nfs_iosize(struct nfsmount *nmp);
93 static void     nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp);
94 static int      mountnfs(struct nfs_args *, struct mount *,
95                     struct sockaddr *, char *, char *, struct vnode **,
96                     struct ucred *cred);
97 static int      nfs_mount(struct mount *mp, char *path, caddr_t data,
98                     struct nameidata *ndp, struct thread *td);
99 static int      nfs_unmount(struct mount *mp, int mntflags, struct thread *td);
100 static int      nfs_root(struct mount *mp, struct vnode **vpp);
101 static int      nfs_statfs(struct mount *mp, struct statfs *sbp,
102                     struct thread *td);
103 static int      nfs_sync(struct mount *mp, int waitfor, struct ucred *cred,
104                     struct thread *td);
105
106 /*
107  * nfs vfs operations.
108  */
109 static struct vfsops nfs_vfsops = {
110         nfs_mount,
111         vfs_stdstart,
112         nfs_unmount,
113         nfs_root,
114         vfs_stdquotactl,
115         nfs_statfs,
116         nfs_sync,
117         vfs_stdvget,
118         vfs_stdfhtovp,          /* shouldn't happen */
119         vfs_stdcheckexp,
120         vfs_stdvptofh,          /* shouldn't happen */
121         nfs_init,
122         nfs_uninit,
123         vfs_stdextattrctl,
124 };
125 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
126
127 /* So that loader and kldload(2) can find us, wherever we are.. */
128 MODULE_VERSION(nfs, 1);
129
130 /*
131  * This structure must be filled in by a primary bootstrap or bootstrap
132  * server for a diskless/dataless machine. It is initialized below just
133  * to ensure that it is allocated to initialized data (.data not .bss).
134  */
135 struct nfs_diskless nfs_diskless = { { { 0 } } };
136 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
137 int nfs_diskless_valid = 0;
138
139 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD,
140         &nfs_diskless_valid, 0, "");
141
142 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
143         nfsv3_diskless.root_hostnam, 0, "");
144
145 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
146         &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
147         "%Ssockaddr_in", "");
148
149 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
150         nfsv3_diskless.swap_hostnam, 0, "");
151
152 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
153         &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr,
154         "%Ssockaddr_in","");
155
156
157 void            nfsargs_ntoh(struct nfs_args *);
158 static int      nfs_mountdiskless(char *, char *, int,
159                     struct sockaddr_in *, struct nfs_args *,
160                     struct thread *, struct vnode **, struct mount **);
161 static void     nfs_convert_diskless(void);
162 static void     nfs_convert_oargs(struct nfs_args *args,
163                     struct onfs_args *oargs);
164
165 static int
166 nfs_iosize(struct nfsmount *nmp)
167 {
168         int iosize;
169
170         /*
171          * Calculate the size used for io buffers.  Use the larger
172          * of the two sizes to minimise nfs requests but make sure
173          * that it is at least one VM page to avoid wasting buffer
174          * space.
175          */
176         iosize = max(nmp->nm_rsize, nmp->nm_wsize);
177         if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
178         return iosize;
179 }
180
181 static void
182 nfs_convert_oargs(struct nfs_args *args, struct onfs_args *oargs)
183 {
184
185         args->version = NFS_ARGSVERSION;
186         args->addr = oargs->addr;
187         args->addrlen = oargs->addrlen;
188         args->sotype = oargs->sotype;
189         args->proto = oargs->proto;
190         args->fh = oargs->fh;
191         args->fhsize = oargs->fhsize;
192         args->flags = oargs->flags;
193         args->wsize = oargs->wsize;
194         args->rsize = oargs->rsize;
195         args->readdirsize = oargs->readdirsize;
196         args->timeo = oargs->timeo;
197         args->retrans = oargs->retrans;
198         args->maxgrouplist = oargs->maxgrouplist;
199         args->readahead = oargs->readahead;
200         args->deadthresh = oargs->deadthresh;
201         args->hostname = oargs->hostname;
202 }
203
204 static void
205 nfs_convert_diskless(void)
206 {
207
208         bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
209                 sizeof(struct ifaliasreq));
210         bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
211                 sizeof(struct sockaddr_in));
212         nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args);
213         nfsv3_diskless.swap_fhsize = NFSX_V2FH;
214         bcopy(nfs_diskless.swap_fh, nfsv3_diskless.swap_fh, NFSX_V2FH);
215         bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr,
216                 sizeof(struct sockaddr_in));
217         bcopy(nfs_diskless.swap_hostnam, nfsv3_diskless.swap_hostnam, MNAMELEN);
218         nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks;
219         bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred,
220                 sizeof(struct ucred));
221         nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
222         nfsv3_diskless.root_fhsize = NFSX_V2FH;
223         bcopy(nfs_diskless.root_fh, nfsv3_diskless.root_fh, NFSX_V2FH);
224         bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
225                 sizeof(struct sockaddr_in));
226         bcopy(nfs_diskless.root_hostnam, nfsv3_diskless.root_hostnam, MNAMELEN);
227         nfsv3_diskless.root_time = nfs_diskless.root_time;
228         bcopy(nfs_diskless.my_hostnam, nfsv3_diskless.my_hostnam,
229                 MAXHOSTNAMELEN);
230         nfs_diskless_valid = 3;
231 }
232
233 /*
234  * nfs statfs call
235  */
236 int
237 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
238 {
239         struct vnode *vp;
240         struct nfs_statfs *sfp;
241         caddr_t bpos, dpos;
242         struct nfsmount *nmp = VFSTONFS(mp);
243         int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
244         struct mbuf *mreq, *mrep, *md, *mb;
245         struct nfsnode *np;
246         u_quad_t tquad;
247
248 #ifndef nolint
249         sfp = (struct nfs_statfs *)0;
250 #endif
251         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
252         if (error)
253                 return (error);
254         vp = NFSTOV(np);
255         if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
256                 (void)nfs_fsinfo(nmp, vp, td->td_ucred, td);
257         nfsstats.rpccnt[NFSPROC_FSSTAT]++;
258         mreq = nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
259         mb = mreq;
260         bpos = mtod(mb, caddr_t);
261         nfsm_fhtom(vp, v3);
262         nfsm_request(vp, NFSPROC_FSSTAT, td, td->td_ucred);
263         if (v3)
264                 nfsm_postop_attr(vp, retattr);
265         if (error) {
266                 if (mrep != NULL)
267                         m_freem(mrep);
268                 goto nfsmout;
269         }
270         sfp = nfsm_dissect(struct nfs_statfs *, NFSX_STATFS(v3));
271         sbp->f_flags = nmp->nm_flag;
272         sbp->f_iosize = nfs_iosize(nmp);
273         if (v3) {
274                 sbp->f_bsize = NFS_FABLKSIZE;
275                 tquad = fxdr_hyper(&sfp->sf_tbytes);
276                 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
277                 tquad = fxdr_hyper(&sfp->sf_fbytes);
278                 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
279                 tquad = fxdr_hyper(&sfp->sf_abytes);
280                 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
281                 sbp->f_files = (fxdr_unsigned(int32_t,
282                     sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
283                 sbp->f_ffree = (fxdr_unsigned(int32_t,
284                     sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
285         } else {
286                 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
287                 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
288                 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
289                 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
290                 sbp->f_files = 0;
291                 sbp->f_ffree = 0;
292         }
293         if (sbp != &mp->mnt_stat) {
294                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
295                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
296                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
297         }
298         m_freem(mrep);
299 nfsmout:
300         vput(vp);
301         return (error);
302 }
303
304 /*
305  * nfs version 3 fsinfo rpc call
306  */
307 int
308 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, struct ucred *cred,
309     struct thread *td)
310 {
311         struct nfsv3_fsinfo *fsp;
312         u_int32_t pref, max;
313         caddr_t bpos, dpos;
314         int error = 0, retattr;
315         struct mbuf *mreq, *mrep, *md, *mb;
316         u_int64_t maxfsize;
317
318         nfsstats.rpccnt[NFSPROC_FSINFO]++;
319         mreq = nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
320         mb = mreq;
321         bpos = mtod(mb, caddr_t);
322         nfsm_fhtom(vp, 1);
323         nfsm_request(vp, NFSPROC_FSINFO, td, cred);
324         nfsm_postop_attr(vp, retattr);
325         if (!error) {
326                 fsp = nfsm_dissect(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
327                 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
328                 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
329                         nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
330                                 ~(NFS_FABLKSIZE - 1);
331                 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
332                 if (max < nmp->nm_wsize && max > 0) {
333                         nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
334                         if (nmp->nm_wsize == 0)
335                                 nmp->nm_wsize = max;
336                 }
337                 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
338                 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
339                         nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
340                                 ~(NFS_FABLKSIZE - 1);
341                 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
342                 if (max < nmp->nm_rsize && max > 0) {
343                         nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
344                         if (nmp->nm_rsize == 0)
345                                 nmp->nm_rsize = max;
346                 }
347                 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
348                 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
349                         nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
350                                 ~(NFS_DIRBLKSIZ - 1);
351                 if (max < nmp->nm_readdirsize && max > 0) {
352                         nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
353                         if (nmp->nm_readdirsize == 0)
354                                 nmp->nm_readdirsize = max;
355                 }
356                 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
357                 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
358                         nmp->nm_maxfilesize = maxfsize;
359                 nmp->nm_state |= NFSSTA_GOTFSINFO;
360         }
361         m_freem(mrep);
362 nfsmout:
363         return (error);
364 }
365
366 /*
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
369  * bootstrap.
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
374  *   a default gateway.
375  * - build the rootfs mount point and call mountnfs() to do the rest.
376  */
377 int
378 nfs_mountroot(struct mount *mp, struct thread *td)
379 {
380         struct mount  *swap_mp;
381         struct nfsv3_diskless *nd = &nfsv3_diskless;
382         struct socket *so;
383         struct vnode *vp;
384         int error, i;
385         u_long l;
386         char buf[128];
387
388 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
389         bootpc_init();          /* use bootp to get nfs_diskless filled in */
390 #endif
391
392         if (nfs_diskless_valid==1)
393                 nfs_convert_diskless();
394
395         /*
396          * XXX splnet, so networks will receive...
397          */
398         splnet();
399
400 #ifdef notyet
401         /* Set up swap credentials. */
402         proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
403         proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
404         if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
405                 NGROUPS)
406                 proc0.p_ucred->cr_ngroups = NGROUPS;
407         for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
408             proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
409 #endif
410
411         /*
412          * Do enough of ifconfig(8) so that the critical net interface can
413          * talk to the server.
414          */
415         error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0,
416             td->td_ucred, td);
417         if (error)
418                 panic("nfs_mountroot: socreate(%04x): %d",
419                         nd->myif.ifra_addr.sa_family, error);
420
421 #if 0 /* XXX Bad idea */
422         /*
423          * We might not have been told the right interface, so we pass
424          * over the first ten interfaces of the same kind, until we get
425          * one of them configured.
426          */
427
428         for (i = strlen(nd->myif.ifra_name) - 1;
429                 nd->myif.ifra_name[i] >= '0' &&
430                 nd->myif.ifra_name[i] <= '9';
431                 nd->myif.ifra_name[i] ++) {
432                 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
433                 if(!error)
434                         break;
435         }
436 #endif
437         error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
438         if (error)
439                 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
440         soclose(so);
441
442         /*
443          * If the gateway field is filled in, set it as the default route.
444          */
445         if (nd->mygateway.sin_len != 0) {
446                 struct sockaddr_in mask, sin;
447
448                 bzero((caddr_t)&mask, sizeof(mask));
449                 sin = mask;
450                 sin.sin_family = AF_INET;
451                 sin.sin_len = sizeof(sin);
452                 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
453                     (struct sockaddr *)&nd->mygateway,
454                     (struct sockaddr *)&mask,
455                     RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
456                 if (error)
457                         panic("nfs_mountroot: RTM_ADD: %d", error);
458         }
459
460         /*
461          * Create the rootfs mount point.
462          */
463         nd->root_args.fh = nd->root_fh;
464         nd->root_args.fhsize = nd->root_fhsize;
465         l = ntohl(nd->root_saddr.sin_addr.s_addr);
466         snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
467                 (l >> 24) & 0xff, (l >> 16) & 0xff,
468                 (l >>  8) & 0xff, (l >>  0) & 0xff, nd->root_hostnam);
469         printf("NFS ROOT: %s\n", buf);
470         if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
471             &nd->root_saddr, &nd->root_args, td, &vp, &mp)) != 0) {
472                 if (swap_mp) {
473                         mp->mnt_vfc->vfc_refcount--;
474                         free(swap_mp, M_MOUNT);
475                 }
476                 return (error);
477         }
478
479         swap_mp = NULL;
480         if (nd->swap_nblks) {
481
482                 /* Convert to DEV_BSIZE instead of Kilobyte */
483                 nd->swap_nblks *= 2;
484
485                 /*
486                  * Create a fake mount point just for the swap vnode so that the
487                  * swap file can be on a different server from the rootfs.
488                  */
489                 nd->swap_args.fh = nd->swap_fh;
490                 nd->swap_args.fhsize = nd->swap_fhsize;
491                 l = ntohl(nd->swap_saddr.sin_addr.s_addr);
492                 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
493                         (l >> 24) & 0xff, (l >> 16) & 0xff,
494                         (l >>  8) & 0xff, (l >>  0) & 0xff, nd->swap_hostnam);
495                 printf("NFS SWAP: %s\n", buf);
496                 if ((error = nfs_mountdiskless(buf, "/swap", 0,
497                     &nd->swap_saddr, &nd->swap_args, td, &vp, &swap_mp)) != 0)
498                         return (error);
499                 vfs_unbusy(swap_mp, td);
500
501                 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size =
502                                 nd->swap_nblks * DEV_BSIZE ;
503                 /*
504                  * Since the swap file is not the root dir of a filesystem,
505                  * hack it to a regular file.
506                  */
507                 vp->v_type = VREG;
508                 vp->v_flag = 0;
509                 VREF(vp);
510                 swaponvp(td, vp, NODEV, nd->swap_nblks);
511         }
512
513         mp->mnt_flag |= MNT_ROOTFS;
514         mp->mnt_vnodecovered = NULLVP;
515         rootvp = vp;
516         vfs_unbusy(mp, td);
517
518         /*
519          * This is not really an nfs issue, but it is much easier to
520          * set hostname here and then let the "/etc/rc.xxx" files
521          * mount the right /var based upon its preset value.
522          */
523         bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
524         hostname[MAXHOSTNAMELEN - 1] = '\0';
525         for (i = 0; i < MAXHOSTNAMELEN; i++)
526                 if (hostname[i] == '\0')
527                         break;
528         inittodr(ntohl(nd->root_time));
529         return (0);
530 }
531
532 /*
533  * Internal version of mount system call for diskless setup.
534  */
535 static int
536 nfs_mountdiskless(char *path, char *which, int mountflag,
537     struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
538     struct vnode **vpp, struct mount **mpp)
539 {
540         struct mount *mp;
541         struct sockaddr *nam;
542         int error;
543
544         mp = *mpp;
545
546         if (!mp && (error = vfs_rootmountalloc("nfs", path, &mp))) {
547                 printf("nfs_mountroot: NFS not configured");
548                 return (error);
549         }
550
551         mp->mnt_kern_flag = 0;
552         mp->mnt_flag = mountflag;
553         nam = dup_sockaddr((struct sockaddr *)sin, 1);
554         if ((error = mountnfs(args, mp, nam, which, path, vpp,
555             td->td_ucred)) != 0) {
556                 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
557                 mp->mnt_vfc->vfc_refcount--;
558                 vfs_unbusy(mp, td);
559                 free(mp, M_MOUNT);
560                 FREE(nam, M_SONAME);
561                 return (error);
562         }
563         (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
564         *mpp = mp;
565         return (0);
566 }
567
568 static void
569 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp)
570 {
571         int s;
572         int adjsock;
573         int maxio;
574
575         s = splnet();
576         /*
577          * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
578          * no sense in that context.
579          */
580         if (argp->sotype == SOCK_STREAM)
581                 nmp->nm_flag &= ~NFSMNT_NOCONN;
582
583         /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
584         if ((argp->flags & NFSMNT_NFSV3) == 0)
585                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
586
587         /* Re-bind if rsrvd port requested and wasn't on one */
588         adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
589                   && (argp->flags & NFSMNT_RESVPORT);
590         /* Also re-bind if we're switching to/from a connected UDP socket */
591         adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
592                     (argp->flags & NFSMNT_NOCONN));
593
594         /* Update flags atomically.  Don't change the lock bits. */
595         nmp->nm_flag = argp->flags | nmp->nm_flag;
596         splx(s);
597
598         if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
599                 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
600                 if (nmp->nm_timeo < NFS_MINTIMEO)
601                         nmp->nm_timeo = NFS_MINTIMEO;
602                 else if (nmp->nm_timeo > NFS_MAXTIMEO)
603                         nmp->nm_timeo = NFS_MAXTIMEO;
604         }
605
606         if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
607                 nmp->nm_retry = argp->retrans;
608                 if (nmp->nm_retry > NFS_MAXREXMIT)
609                         nmp->nm_retry = NFS_MAXREXMIT;
610         }
611
612         if (argp->flags & NFSMNT_NFSV3) {
613                 if (argp->sotype == SOCK_DGRAM)
614                         maxio = NFS_MAXDGRAMDATA;
615                 else
616                         maxio = NFS_MAXDATA;
617         } else
618                 maxio = NFS_V2MAXDATA;
619
620         if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
621                 nmp->nm_wsize = argp->wsize;
622                 /* Round down to multiple of blocksize */
623                 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
624                 if (nmp->nm_wsize <= 0)
625                         nmp->nm_wsize = NFS_FABLKSIZE;
626         }
627         if (nmp->nm_wsize > maxio)
628                 nmp->nm_wsize = maxio;
629         if (nmp->nm_wsize > MAXBSIZE)
630                 nmp->nm_wsize = MAXBSIZE;
631
632         if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
633                 nmp->nm_rsize = argp->rsize;
634                 /* Round down to multiple of blocksize */
635                 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
636                 if (nmp->nm_rsize <= 0)
637                         nmp->nm_rsize = NFS_FABLKSIZE;
638         }
639         if (nmp->nm_rsize > maxio)
640                 nmp->nm_rsize = maxio;
641         if (nmp->nm_rsize > MAXBSIZE)
642                 nmp->nm_rsize = MAXBSIZE;
643
644         if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
645                 nmp->nm_readdirsize = argp->readdirsize;
646         }
647         if (nmp->nm_readdirsize > maxio)
648                 nmp->nm_readdirsize = maxio;
649         if (nmp->nm_readdirsize > nmp->nm_rsize)
650                 nmp->nm_readdirsize = nmp->nm_rsize;
651
652         if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
653                 nmp->nm_acregmin = argp->acregmin;
654         else
655                 nmp->nm_acregmin = NFS_MINATTRTIMO;
656         if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
657                 nmp->nm_acregmax = argp->acregmax;
658         else
659                 nmp->nm_acregmax = NFS_MAXATTRTIMO;
660         if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
661                 nmp->nm_acdirmin = argp->acdirmin;
662         else
663                 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
664         if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
665                 nmp->nm_acdirmax = argp->acdirmax;
666         else
667                 nmp->nm_acdirmax = NFS_MAXDIRATTRTIMO;
668         if (nmp->nm_acdirmin > nmp->nm_acdirmax)
669                 nmp->nm_acdirmin = nmp->nm_acdirmax;
670         if (nmp->nm_acregmin > nmp->nm_acregmax)
671                 nmp->nm_acregmin = nmp->nm_acregmax;
672
673         if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
674                 if (argp->maxgrouplist <= NFS_MAXGRPS)
675                         nmp->nm_numgrps = argp->maxgrouplist;
676                 else
677                         nmp->nm_numgrps = NFS_MAXGRPS;
678         }
679         if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
680                 if (argp->readahead <= NFS_MAXRAHEAD)
681                         nmp->nm_readahead = argp->readahead;
682                 else
683                         nmp->nm_readahead = NFS_MAXRAHEAD;
684         }
685         if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 0) {
686                 if (argp->deadthresh <= NFS_MAXDEADTHRESH)
687                         nmp->nm_deadthresh = argp->deadthresh;
688                 else
689                         nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
690         }
691
692         adjsock |= ((nmp->nm_sotype != argp->sotype) ||
693                     (nmp->nm_soproto != argp->proto));
694         nmp->nm_sotype = argp->sotype;
695         nmp->nm_soproto = argp->proto;
696
697         if (nmp->nm_so && adjsock) {
698                 nfs_safedisconnect(nmp);
699                 if (nmp->nm_sotype == SOCK_DGRAM)
700                         while (nfs_connect(nmp, (struct nfsreq *)0)) {
701                                 printf("nfs_args: retrying connect\n");
702                                 (void) tsleep((caddr_t)&lbolt,
703                                               PSOCK, "nfscon", 0);
704                         }
705         }
706 }
707
708 /*
709  * VFS Operations.
710  *
711  * mount system call
712  * It seems a bit dumb to copyinstr() the host and path here and then
713  * bcopy() them in mountnfs(), but I wanted to detect errors before
714  * doing the sockargs() call because sockargs() allocates an mbuf and
715  * an error after that means that I have to release the mbuf.
716  */
717 /* ARGSUSED */
718 static int
719 nfs_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
720     struct thread *td)
721 {
722         int error;
723         struct nfs_args args;
724         struct sockaddr *nam;
725         struct vnode *vp;
726         char hst[MNAMELEN];
727         size_t len;
728         u_char nfh[NFSX_V3FHMAX];
729
730         if (path == NULL) {
731                 nfs_mountroot(mp, td);
732                 return (0);
733         }
734         error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
735         if (error)
736                 return (error);
737         if (args.version != NFS_ARGSVERSION) {
738 #ifdef COMPAT_PRELITE2
739                 /*
740                  * If the argument version is unknown, then assume the
741                  * caller is a pre-lite2 4.4BSD client and convert its
742                  * arguments.
743                  */
744                 struct onfs_args oargs;
745                 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
746                 if (error)
747                         return (error);
748                 nfs_convert_oargs(&args,&oargs);
749 #else /* !COMPAT_PRELITE2 */
750                 return (EPROGMISMATCH);
751 #endif /* COMPAT_PRELITE2 */
752         }
753         if (mp->mnt_flag & MNT_UPDATE) {
754                 struct nfsmount *nmp = VFSTONFS(mp);
755
756                 if (nmp == NULL)
757                         return (EIO);
758                 /*
759                  * When doing an update, we can't change from or to
760                  * v3, switch lockd strategies or change cookie translation
761                  */
762                 args.flags = (args.flags &
763                     ~(NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/)) |
764                     (nmp->nm_flag &
765                         (NFSMNT_NFSV3 | NFSMNT_NOLOCKD /*|NFSMNT_XLATECOOKIE*/));
766                 nfs_decode_args(nmp, &args);
767                 return (0);
768         }
769         if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
770                 return (EINVAL);
771         error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
772         if (error)
773                 return (error);
774         error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
775         if (error)
776                 return (error);
777         bzero(&hst[len], MNAMELEN - len);
778         /* sockargs() call must be after above copyin() calls */
779         error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
780         if (error)
781                 return (error);
782         args.fh = nfh;
783         error = mountnfs(&args, mp, nam, path, hst, &vp, td->td_ucred);
784         return (error);
785 }
786
787 /*
788  * Common code for mount and mountroot
789  */
790 static int
791 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
792     char *pth, char *hst, struct vnode **vpp, struct ucred *cred)
793 {
794         struct nfsmount *nmp;
795         struct nfsnode *np;
796         int error;
797         struct vattr attrs;
798
799         if (mp->mnt_flag & MNT_UPDATE) {
800                 nmp = VFSTONFS(mp);
801                 /* update paths, file handles, etc, here        XXX */
802                 FREE(nam, M_SONAME);
803                 return (0);
804         } else {
805                 nmp = uma_zalloc(nfsmount_zone, M_WAITOK);
806                 bzero((caddr_t)nmp, sizeof (struct nfsmount));
807                 TAILQ_INIT(&nmp->nm_bufq);
808                 mp->mnt_data = (qaddr_t)nmp;
809         }
810         vfs_getnewfsid(mp);
811         nmp->nm_mountp = mp;
812         nmp->nm_cred = crhold(cred);
813
814         /*
815          * V2 can only handle 32 bit filesizes.  A 4GB-1 limit may be too
816          * high, depending on whether we end up with negative offsets in
817          * the client or server somewhere.  2GB-1 may be safer.
818          *
819          * For V3, nfs_fsinfo will adjust this as necessary.  Assume maximum
820          * that we can handle until we find out otherwise.
821          * XXX Our "safe" limit on the client is what we can store in our
822          * buffer cache using signed(!) block numbers.
823          */
824         if ((argp->flags & NFSMNT_NFSV3) == 0)
825                 nmp->nm_maxfilesize = 0xffffffffLL;
826         else
827                 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
828
829         nmp->nm_timeo = NFS_TIMEO;
830         nmp->nm_retry = NFS_RETRANS;
831         nmp->nm_wsize = NFS_WSIZE;
832         nmp->nm_rsize = NFS_RSIZE;
833         nmp->nm_readdirsize = NFS_READDIRSIZE;
834         nmp->nm_numgrps = NFS_MAXGRPS;
835         nmp->nm_readahead = NFS_DEFRAHEAD;
836         nmp->nm_deadthresh = NFS_MAXDEADTHRESH;
837         nmp->nm_fhsize = argp->fhsize;
838         bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
839         bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
840         bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
841         nmp->nm_nam = nam;
842         /* Set up the sockets and per-host congestion */
843         nmp->nm_sotype = argp->sotype;
844         nmp->nm_soproto = argp->proto;
845
846         nfs_decode_args(nmp, argp);
847
848         /*
849          * For Connection based sockets (TCP,...) defer the connect until
850          * the first request, in case the server is not responding.
851          */
852         if (nmp->nm_sotype == SOCK_DGRAM &&
853                 (error = nfs_connect(nmp, (struct nfsreq *)0)))
854                 goto bad;
855
856         /*
857          * This is silly, but it has to be set so that vinifod() works.
858          * We do not want to do an nfs_statfs() here since we can get
859          * stuck on a dead server and we are holding a lock on the mount
860          * point.
861          */
862         mp->mnt_stat.f_iosize = nfs_iosize(nmp);
863         /*
864          * A reference count is needed on the nfsnode representing the
865          * remote root.  If this object is not persistent, then backward
866          * traversals of the mount point (i.e. "..") will not work if
867          * the nfsnode gets flushed out of the cache. Ufs does not have
868          * this problem, because one can identify root inodes by their
869          * number == ROOTINO (2).
870          */
871         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
872         if (error)
873                 goto bad;
874         *vpp = NFSTOV(np);
875
876         /*
877          * Get file attributes for the mountpoint.  This has the side
878          * effect of filling in (*vpp)->v_type with the correct value.
879          */
880         VOP_GETATTR(*vpp, &attrs, curthread->td_ucred, curthread);
881
882         /*
883          * Lose the lock but keep the ref.
884          */
885         VOP_UNLOCK(*vpp, 0, curthread);
886
887         return (0);
888 bad:
889         nfs_disconnect(nmp);
890         crfree(nmp->nm_cred);
891         uma_zfree(nfsmount_zone, nmp);
892         FREE(nam, M_SONAME);
893         return (error);
894 }
895
896 /*
897  * unmount system call
898  */
899 static int
900 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
901 {
902         struct nfsmount *nmp;
903         int error, flags = 0;
904
905         if (mntflags & MNT_FORCE)
906                 flags |= FORCECLOSE;
907         nmp = VFSTONFS(mp);
908         /*
909          * Goes something like this..
910          * - Call vflush() to clear out vnodes for this filesystem
911          * - Close the socket
912          * - Free up the data structures
913          */
914         /* In the forced case, cancel any outstanding requests. */
915         if (flags & FORCECLOSE) {
916                 error = nfs_nmcancelreqs(nmp);
917                 if (error)
918                         return (error);
919         }
920         /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
921         error = vflush(mp, 1, flags);
922         if (error)
923                 return (error);
924
925         /*
926          * We are now committed to the unmount.
927          */
928         nfs_disconnect(nmp);
929         FREE(nmp->nm_nam, M_SONAME);
930
931         crfree(nmp->nm_cred);
932         uma_zfree(nfsmount_zone, nmp);
933         return (0);
934 }
935
936 /*
937  * Return root of a filesystem
938  */
939 static int
940 nfs_root(struct mount *mp, struct vnode **vpp)
941 {
942         struct vnode *vp;
943         struct nfsmount *nmp;
944         struct nfsnode *np;
945         int error;
946
947         nmp = VFSTONFS(mp);
948         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
949         if (error)
950                 return (error);
951         vp = NFSTOV(np);
952         if (vp->v_type == VNON)
953             vp->v_type = VDIR;
954         vp->v_flag = VROOT;
955         *vpp = vp;
956         return (0);
957 }
958
959 /*
960  * Flush out the buffer cache
961  */
962 /* ARGSUSED */
963 static int
964 nfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
965 {
966         struct vnode *vp, *vnp;
967         int error, allerror = 0;
968
969         /*
970          * Force stale buffer cache information to be flushed.
971          */
972         mtx_lock(&mntvnode_mtx);
973 loop:
974         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
975              vp != NULL;
976              vp = vnp) {
977                 /*
978                  * If the vnode that we are about to sync is no longer
979                  * associated with this mount point, start over.
980                  */
981                 if (vp->v_mount != mp)
982                         goto loop;
983                 vnp = TAILQ_NEXT(vp, v_nmntvnodes);
984                 mtx_unlock(&mntvnode_mtx);
985                 mtx_lock(&vp->v_interlock);
986                 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
987                     waitfor == MNT_LAZY) {
988                         mtx_unlock(&vp->v_interlock);
989                         mtx_lock(&mntvnode_mtx);
990                         continue;
991                 }
992                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
993                         mtx_lock(&mntvnode_mtx);
994                         goto loop;
995                 }
996                 error = VOP_FSYNC(vp, cred, waitfor, td);
997                 if (error)
998                         allerror = error;
999                 vput(vp);
1000                 mtx_lock(&mntvnode_mtx);
1001         }
1002         mtx_unlock(&mntvnode_mtx);
1003         return (allerror);
1004 }