]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_vfsops.c
This commit was generated by cvs2svn to compensate for changes in r58650,
[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  * $FreeBSD$
38  */
39
40 #include "opt_bootp.h"
41
42 #include <sys/param.h>
43 #include <sys/sockio.h>
44 #include <sys/proc.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>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/systm.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_zone.h>
58
59 #include <net/if.h>
60 #include <net/route.h>
61 #include <netinet/in.h>
62
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/nfs.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>
72
73 extern int      nfs_mountroot __P((struct mount *mp));
74
75 extern int      nfs_ticks;
76
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");
85
86 vm_zone_t nfsmount_zone;
87
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, "");
92 #ifdef NFS_DEBUG
93 int nfs_debug;
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
95 #endif
96
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,
105                         struct proc *p));
106 static int      nfs_root __P(( struct mount *mp, struct vnode **vpp));
107 static int      nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
108                         struct proc *p));
109 static int      nfs_sync __P(( struct mount *mp, int waitfor,
110                         struct ucred *cred, struct proc *p));
111
112 /*
113  * nfs vfs operations.
114  */
115 static struct vfsops nfs_vfsops = {
116         nfs_mount,
117         vfs_stdstart,
118         nfs_unmount,
119         nfs_root,
120         vfs_stdquotactl,
121         nfs_statfs,
122         nfs_sync,
123         vfs_stdvget,
124         vfs_stdfhtovp,          /* shouldn't happen */
125         vfs_stdcheckexp,
126         vfs_stdvptofh,          /* shouldn't happen */
127         nfs_init,
128         nfs_uninit,
129         vfs_stdextattrctl,
130 };
131 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
132
133 /*
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).
137  */
138 struct nfs_diskless nfs_diskless = { { { 0 } } };
139 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
140 int nfs_diskless_valid = 0;
141
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD, 
143         &nfs_diskless_valid, 0, "");
144
145 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
146         nfsv3_diskless.root_hostnam, 0, "");
147
148 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
149         &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
150         "%Ssockaddr_in", "");
151
152 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
153         nfsv3_diskless.swap_hostnam, 0, "");
154
155 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
156         &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr, 
157         "%Ssockaddr_in","");
158
159
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 **,
164                                   struct mount **));
165 static void nfs_convert_diskless __P((void));
166 static void nfs_convert_oargs __P((struct nfs_args *args,
167                                    struct onfs_args *oargs));
168
169 static int
170 nfs_iosize(nmp)
171         struct nfsmount* nmp;
172 {
173         int iosize;
174
175         /*
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
179          * space.
180          */
181         iosize = max(nmp->nm_rsize, nmp->nm_wsize);
182         if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
183         return iosize;
184 }
185
186 static void
187 nfs_convert_oargs(args, oargs)
188         struct nfs_args *args;
189         struct onfs_args *oargs;
190 {
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;
209 }
210
211 static void
212 nfs_convert_diskless()
213 {
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,
235                 MAXHOSTNAMELEN);
236         nfs_diskless_valid = 3;
237 }
238
239 /*
240  * nfs statfs call
241  */
242 int
243 nfs_statfs(mp, sbp, p)
244         struct mount *mp;
245         register struct statfs *sbp;
246         struct proc *p;
247 {
248         register struct vnode *vp;
249         register struct nfs_statfs *sfp;
250         register caddr_t cp;
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;
257         struct ucred *cred;
258         struct nfsnode *np;
259         u_quad_t tquad;
260
261 #ifndef nolint
262         sfp = (struct nfs_statfs *)0;
263 #endif
264         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
265         if (error)
266                 return (error);
267         vp = NFSTOV(np);
268         cred = crget();
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));
274         nfsm_fhtom(vp, v3);
275         nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
276         if (v3)
277                 nfsm_postop_attr(vp, retattr);
278         if (error) {
279                 if (mrep != NULL)
280                         m_freem(mrep);
281                 goto nfsmout;
282         }
283         nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
284         sbp->f_flags = nmp->nm_flag;
285         sbp->f_iosize = nfs_iosize(nmp);
286         if (v3) {
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);
298         } else {
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);
303                 sbp->f_files = 0;
304                 sbp->f_ffree = 0;
305         }
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);
310         }
311         nfsm_reqdone;
312         vput(vp);
313         crfree(cred);
314         return (error);
315 }
316
317 /*
318  * nfs version 3 fsinfo rpc call
319  */
320 int
321 nfs_fsinfo(nmp, vp, cred, p)
322         register struct nfsmount *nmp;
323         register struct vnode *vp;
324         struct ucred *cred;
325         struct proc *p;
326 {
327         register struct nfsv3_fsinfo *fsp;
328         register caddr_t cp;
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;
334         u_int64_t maxfsize;
335
336         nfsstats.rpccnt[NFSPROC_FSINFO]++;
337         nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
338         nfsm_fhtom(vp, 1);
339         nfsm_request(vp, NFSPROC_FSINFO, p, cred);
340         nfsm_postop_attr(vp, retattr);
341         if (!error) {
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)
351                                 nmp->nm_wsize = max;
352                 }
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)
361                                 nmp->nm_rsize = max;
362                 }
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;
371                 }
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;
376         }
377         nfsm_reqdone;
378         return (error);
379 }
380
381 /*
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
384  * bootstrap.
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
389  *   a default gateway.
390  * - build the rootfs mount point and call mountnfs() to do the rest.
391  */
392 int
393 nfs_mountroot(mp)
394         struct mount *mp;
395 {
396         struct mount  *swap_mp;
397         struct nfsv3_diskless *nd = &nfsv3_diskless;
398         struct socket *so;
399         struct vnode *vp;
400         struct proc *p = curproc;               /* XXX */
401         int error, i;
402         u_long l;
403         char buf[128];
404
405 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
406         bootpc_init();          /* use bootp to get nfs_diskless filled in */
407 #endif
408
409         /*
410          * XXX time must be non-zero when we init the interface or else
411          * the arp code will wedge...
412          */
413         while (time_second == 0)
414                 tsleep(&time_second, PZERO+8, "arpkludge", 10);
415
416         if (nfs_diskless_valid==1) 
417           nfs_convert_diskless();
418
419         /*
420          * XXX splnet, so networks will receive...
421          */
422         splnet();
423
424 #ifdef notyet
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)) >
429                 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]);
433 #endif
434
435         /*
436          * Do enough of ifconfig(8) so that the critical net interface can
437          * talk to the server.
438          */
439         error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0, p);
440         if (error)
441                 panic("nfs_mountroot: socreate(%04x): %d",
442                         nd->myif.ifra_addr.sa_family, error);
443
444         /*
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.
448          */
449
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);
455                 if(!error)
456                         break;
457         }
458         if (error)
459                 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
460         soclose(so);
461
462         /*
463          * If the gateway field is filled in, set it as the default route.
464          */
465         if (nd->mygateway.sin_len != 0) {
466                 struct sockaddr_in mask, sin;
467
468                 bzero((caddr_t)&mask, sizeof(mask));
469                 sin = 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);
476                 if (error)
477                         panic("nfs_mountroot: RTM_ADD: %d", error);
478         }
479
480         /*
481          * Create the rootfs mount point.
482          */
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) {
492                 if (swap_mp) {
493                         mp->mnt_vfc->vfc_refcount--;
494                         free(swap_mp, M_MOUNT);
495                 }
496                 return (error);
497         }
498
499         swap_mp = NULL;
500         if (nd->swap_nblks) {
501
502                 /* Convert to DEV_BSIZE instead of Kilobyte */
503                 nd->swap_nblks *= 2;
504
505                 /*
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.
508                  */
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)
518                         return (error);
519                 vfs_unbusy(swap_mp, p);
520
521                 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size = 
522                                 nd->swap_nblks * DEV_BSIZE ;
523                 
524                 /*
525                  * Since the swap file is not the root dir of a file system,
526                  * hack it to a regular file.
527                  */
528                 vp->v_type = VREG;
529                 vp->v_flag = 0;
530                 VREF(vp);
531                 swaponvp(p, vp, NODEV, nd->swap_nblks);
532         }
533
534         mp->mnt_flag |= MNT_ROOTFS;
535         mp->mnt_vnodecovered = NULLVP;
536         rootvp = vp;
537         vfs_unbusy(mp, p);
538
539         /*
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.
543          */
544         bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
545         hostname[MAXHOSTNAMELEN - 1] = '\0';
546         for (i = 0; i < MAXHOSTNAMELEN; i++)
547                 if (hostname[i] == '\0')
548                         break;
549         inittodr(ntohl(nd->root_time));
550         return (0);
551 }
552
553 /*
554  * Internal version of mount system call for diskless setup.
555  */
556 static int
557 nfs_mountdiskless(path, which, mountflag, sin, args, p, vpp, mpp)
558         char *path;
559         char *which;
560         int mountflag;
561         struct sockaddr_in *sin;
562         struct nfs_args *args;
563         struct proc *p;
564         struct vnode **vpp;
565         struct mount **mpp;
566 {
567         struct mount *mp;
568         struct sockaddr *nam;
569         int error;
570
571         mp = *mpp;
572
573         if (!mp && (error = vfs_rootmountalloc("nfs", path, &mp))) {
574                 printf("nfs_mountroot: NFS not configured");
575                 return (error);
576         }
577
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--;
584                 vfs_unbusy(mp, p);
585                 free(mp, M_MOUNT);
586                 FREE(nam, M_SONAME);
587                 return (error);
588         }
589         (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
590         *mpp = mp;
591         return (0);
592 }
593
594 static void
595 nfs_decode_args(nmp, argp)
596         struct nfsmount *nmp;
597         struct nfs_args *argp;
598 {
599         int s;
600         int adjsock;
601         int maxio;
602
603         s = splnet();
604         /*
605          * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
606          * no sense in that context.
607          */
608         if (argp->sotype == SOCK_STREAM)
609                 nmp->nm_flag &= ~NFSMNT_NOCONN;
610
611         /* Also clear RDIRPLUS if not NFSv3, it crashes some servers */
612         if ((argp->flags & NFSMNT_NFSV3) == 0)
613                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
614
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));
621
622         /* Update flags atomically.  Don't change the lock bits. */
623         nmp->nm_flag = argp->flags | nmp->nm_flag;
624         splx(s);
625
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;
632         }
633
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;
638         }
639
640         if (argp->flags & NFSMNT_NFSV3) {
641                 if (argp->sotype == SOCK_DGRAM)
642                         maxio = NFS_MAXDGRAMDATA;
643                 else
644                         maxio = NFS_MAXDATA;
645         } else
646                 maxio = NFS_V2MAXDATA;
647
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;
654         }
655         if (nmp->nm_wsize > maxio)
656                 nmp->nm_wsize = maxio;
657         if (nmp->nm_wsize > MAXBSIZE)
658                 nmp->nm_wsize = MAXBSIZE;
659
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;
666         }
667         if (nmp->nm_rsize > maxio)
668                 nmp->nm_rsize = maxio;
669         if (nmp->nm_rsize > MAXBSIZE)
670                 nmp->nm_rsize = MAXBSIZE;
671
672         if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
673                 nmp->nm_readdirsize = argp->readdirsize;
674         }
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;
679
680         if ((argp->flags & NFSMNT_ACREGMIN) && argp->acregmin >= 0)
681                 nmp->nm_acregmin = argp->acregmin;
682         else
683                 nmp->nm_acregmin = NFS_MINATTRTIMO;
684         if ((argp->flags & NFSMNT_ACREGMAX) && argp->acregmax >= 0)
685                 nmp->nm_acregmax = argp->acregmax;
686         else
687                 nmp->nm_acregmax = NFS_MAXATTRTIMO;
688         if ((argp->flags & NFSMNT_ACDIRMIN) && argp->acdirmin >= 0)
689                 nmp->nm_acdirmin = argp->acdirmin;
690         else
691                 nmp->nm_acdirmin = NFS_MINDIRATTRTIMO;
692         if ((argp->flags & NFSMNT_ACDIRMAX) && argp->acdirmax >= 0)
693                 nmp->nm_acdirmax = argp->acdirmax;
694         else
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;
700
701         if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0) {
702                 if (argp->maxgrouplist <= NFS_MAXGRPS)
703                         nmp->nm_numgrps = argp->maxgrouplist;
704                 else
705                         nmp->nm_numgrps = NFS_MAXGRPS;
706         }
707         if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0) {
708                 if (argp->readahead <= NFS_MAXRAHEAD)
709                         nmp->nm_readahead = argp->readahead;
710                 else
711                         nmp->nm_readahead = NFS_MAXRAHEAD;
712         }
713         if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2) {
714                 if (argp->leaseterm <= NQ_MAXLEASE)
715                         nmp->nm_leaseterm = argp->leaseterm;
716                 else
717                         nmp->nm_leaseterm = NQ_MAXLEASE;
718         }
719         if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1) {
720                 if (argp->deadthresh <= NQ_NEVERDEAD)
721                         nmp->nm_deadthresh = argp->deadthresh;
722                 else
723                         nmp->nm_deadthresh = NQ_NEVERDEAD;
724         }
725
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;
730
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,
737                                               PSOCK, "nfscon", 0);
738                         }
739         }
740 }
741
742 /*
743  * VFS Operations.
744  *
745  * mount system call
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.
750  */
751 /* ARGSUSED */
752 static int
753 nfs_mount(mp, path, data, ndp, p)
754         struct mount *mp;
755         char *path;
756         caddr_t data;
757         struct nameidata *ndp;
758         struct proc *p;
759 {
760         int error;
761         struct nfs_args args;
762         struct sockaddr *nam;
763         struct vnode *vp;
764         char pth[MNAMELEN], hst[MNAMELEN];
765         size_t len;
766         u_char nfh[NFSX_V3FHMAX];
767
768         if (path == NULL) {
769                 nfs_mountroot(mp);
770                 return (0);
771         }
772         error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
773         if (error)
774                 return (error);
775         if (args.version != NFS_ARGSVERSION) {
776 #ifdef COMPAT_PRELITE2
777                 /*
778                  * If the argument version is unknown, then assume the
779                  * caller is a pre-lite2 4.4BSD client and convert its
780                  * arguments.
781                  */
782                 struct onfs_args oargs;
783                 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
784                 if (error)
785                         return (error);
786                 nfs_convert_oargs(&args,&oargs);
787 #else /* !COMPAT_PRELITE2 */
788                 return (EPROGMISMATCH);
789 #endif /* COMPAT_PRELITE2 */
790         }
791         if (mp->mnt_flag & MNT_UPDATE) {
792                 register struct nfsmount *nmp = VFSTONFS(mp);
793
794                 if (nmp == NULL)
795                         return (EIO);
796                 /*
797                  * When doing an update, we can't change from or to
798                  * v3 and/or nqnfs, or change cookie translation
799                  */
800                 args.flags = (args.flags &
801                     ~(NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)) |
802                     (nmp->nm_flag &
803                         (NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/));
804                 nfs_decode_args(nmp, &args);
805                 return (0);
806         }
807         error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
808         if (error)
809                 return (error);
810         error = copyinstr(path, pth, MNAMELEN-1, &len);
811         if (error)
812                 return (error);
813         bzero(&pth[len], MNAMELEN - len);
814         error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
815         if (error)
816                 return (error);
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);
820         if (error)
821                 return (error);
822         args.fh = nfh;
823         error = mountnfs(&args, mp, nam, pth, hst, &vp);
824         return (error);
825 }
826
827 /*
828  * Common code for mount and mountroot
829  */
830 static int
831 mountnfs(argp, mp, nam, pth, hst, vpp)
832         register struct nfs_args *argp;
833         register struct mount *mp;
834         struct sockaddr *nam;
835         char *pth, *hst;
836         struct vnode **vpp;
837 {
838         register struct nfsmount *nmp;
839         struct nfsnode *np;
840         int error;
841         struct vattr attrs;
842
843         if (mp->mnt_flag & MNT_UPDATE) {
844                 nmp = VFSTONFS(mp);
845                 /* update paths, file handles, etc, here        XXX */
846                 FREE(nam, M_SONAME);
847                 return (0);
848         } else {
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;
854         }
855         vfs_getnewfsid(mp);
856         nmp->nm_mountp = mp;
857         if (argp->flags & NFSMNT_NQNFS)
858                 /*
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).
863                  */
864                 mp->mnt_maxsymlinklen = 1;
865
866         /*
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.
870          *
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.
875          */
876         if ((argp->flags & NFSMNT_NFSV3) == 0)
877                 nmp->nm_maxfilesize = 0xffffffffLL;
878         else
879                 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
880
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);
896         nmp->nm_nam = nam;
897         /* Set up the sockets and per-host congestion */
898         nmp->nm_sotype = argp->sotype;
899         nmp->nm_soproto = argp->proto;
900
901         nfs_decode_args(nmp, argp);
902
903         /*
904          * For Connection based sockets (TCP,...) defer the connect until
905          * the first request, in case the server is not responding.
906          */
907         if (nmp->nm_sotype == SOCK_DGRAM &&
908                 (error = nfs_connect(nmp, (struct nfsreq *)0)))
909                 goto bad;
910
911         /*
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
915          * point.
916          */
917         mp->mnt_stat.f_iosize = nfs_iosize(nmp);
918         /*
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).
925          */
926         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
927         if (error)
928                 goto bad;
929         *vpp = NFSTOV(np);
930
931         /*
932          * Get file attributes for the mountpoint.  This has the side
933          * effect of filling in (*vpp)->v_type with the correct value.
934          */
935         VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc);
936
937         /*
938          * Lose the lock but keep the ref.
939          */
940         VOP_UNLOCK(*vpp, 0, curproc);
941
942         return (0);
943 bad:
944         nfs_disconnect(nmp);
945         zfree(nfsmount_zone, nmp);
946         FREE(nam, M_SONAME);
947         return (error);
948 }
949
950 /*
951  * unmount system call
952  */
953 static int
954 nfs_unmount(mp, mntflags, p)
955         struct mount *mp;
956         int mntflags;
957         struct proc *p;
958 {
959         register struct nfsmount *nmp;
960         struct nfsnode *np;
961         struct vnode *vp;
962         int error, flags = 0;
963
964         if (mntflags & MNT_FORCE)
965                 flags |= FORCECLOSE;
966         nmp = VFSTONFS(mp);
967         /*
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.
973          * - Close the socket
974          * - Free up the data structures
975          */
976         /*
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!
980          */
981         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
982         if (error)
983                 return(error);
984         vp = NFSTOV(np);
985         if (vp->v_usecount > 2) {
986                 vput(vp);
987                 return (EBUSY);
988         }
989
990         /*
991          * Must handshake with nqnfs_clientd() if it is active.
992          */
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);
997         if (error) {
998                 vput(vp);
999                 nmp->nm_state &= ~NFSSTA_DISMINPROG;
1000                 return (error);
1001         }
1002
1003         /*
1004          * We are now committed to the unmount.
1005          * For NQNFS, let the server daemon free the nfsmount structure.
1006          */
1007         if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
1008                 nmp->nm_state |= NFSSTA_DISMNT;
1009
1010         /*
1011          * There are two reference counts and one lock to get rid of here.
1012          */
1013         vput(vp);
1014         vrele(vp);
1015         vgone(vp);
1016         nfs_disconnect(nmp);
1017         FREE(nmp->nm_nam, M_SONAME);
1018
1019         if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
1020                 zfree(nfsmount_zone, nmp);
1021         return (0);
1022 }
1023
1024 /*
1025  * Return root of a filesystem
1026  */
1027 static int
1028 nfs_root(mp, vpp)
1029         struct mount *mp;
1030         struct vnode **vpp;
1031 {
1032         register struct vnode *vp;
1033         struct nfsmount *nmp;
1034         struct nfsnode *np;
1035         int error;
1036
1037         nmp = VFSTONFS(mp);
1038         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
1039         if (error)
1040                 return (error);
1041         vp = NFSTOV(np);
1042         if (vp->v_type == VNON)
1043             vp->v_type = VDIR;
1044         vp->v_flag = VROOT;
1045         *vpp = vp;
1046         return (0);
1047 }
1048
1049 extern int syncprt;
1050
1051 /*
1052  * Flush out the buffer cache
1053  */
1054 /* ARGSUSED */
1055 static int
1056 nfs_sync(mp, waitfor, cred, p)
1057         struct mount *mp;
1058         int waitfor;
1059         struct ucred *cred;
1060         struct proc *p;
1061 {
1062         register struct vnode *vp;
1063         int error, allerror = 0;
1064
1065         /*
1066          * Force stale buffer cache information to be flushed.
1067          */
1068 loop:
1069         for (vp = mp->mnt_vnodelist.lh_first;
1070              vp != NULL;
1071              vp = vp->v_mntvnodes.le_next) {
1072                 /*
1073                  * If the vnode that we are about to sync is no longer
1074                  * associated with this mount point, start over.
1075                  */
1076                 if (vp->v_mount != mp)
1077                         goto loop;
1078                 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1079                     waitfor == MNT_LAZY)
1080                         continue;
1081                 if (vget(vp, LK_EXCLUSIVE, p))
1082                         goto loop;
1083                 error = VOP_FSYNC(vp, cred, waitfor, p);
1084                 if (error)
1085                         allerror = error;
1086                 vput(vp);
1087         }
1088         return (allerror);
1089 }
1090