proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / vfs / nfs / 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: src/sys/nfs/nfs_vfsops.c,v 1.91.2.7 2003/01/27 20:04:08 dillon Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_vfsops.c,v 1.3 2003/06/25 03:56:07 dillon Exp $
39  */
40
41 #include "opt_bootp.h"
42
43 #include <sys/param.h>
44 #include <sys/sockio.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_zone.h>
59
60 #include <net/if.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfs.h>
67 #include <nfs/nfsnode.h>
68 #include <nfs/nfsmount.h>
69 #include <nfs/xdr_subs.h>
70 #include <nfs/nfsm_subs.h>
71 #include <nfs/nfsdiskless.h>
72 #include <nfs/nqnfs.h>
73
74 extern int      nfs_mountroot __P((struct mount *mp));
75
76 extern int      nfs_ticks;
77
78 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
79 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
80 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
81 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
82 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
83 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
84 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease");
85 MALLOC_DEFINE(M_NFSHASH, "NFS hash", "NFS hash tables");
86
87 vm_zone_t nfsmount_zone;
88
89 struct nfsstats nfsstats;
90 SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
91 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
92         &nfsstats, nfsstats, "");
93 static int nfs_ip_paranoia = 1;
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_ip_paranoia, CTLFLAG_RW,
95         &nfs_ip_paranoia, 0, "");
96 #ifdef NFS_DEBUG
97 int nfs_debug;
98 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
99 #endif
100
101 static int      nfs_iosize __P((struct nfsmount *nmp));
102 static void     nfs_decode_args __P((struct nfsmount *nmp,
103                         struct nfs_args *argp));
104 static int      mountnfs __P((struct nfs_args *,struct mount *,
105                         struct sockaddr *,char *,char *,struct vnode **));
106 static int      nfs_mount __P(( struct mount *mp, char *path, caddr_t data,
107                         struct nameidata *ndp, struct thread *td));
108 static int      nfs_unmount __P(( struct mount *mp, int mntflags,
109                         struct thread *td));
110 static int      nfs_root __P(( struct mount *mp, struct vnode **vpp));
111 static int      nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
112                         struct thread *td));
113 static int      nfs_sync __P(( struct mount *mp, int waitfor,
114                         struct ucred *cred, struct thread *td));
115
116 /*
117  * nfs vfs operations.
118  */
119 static struct vfsops nfs_vfsops = {
120         nfs_mount,
121         vfs_stdstart,
122         nfs_unmount,
123         nfs_root,
124         vfs_stdquotactl,
125         nfs_statfs,
126         nfs_sync,
127         vfs_stdvget,
128         vfs_stdfhtovp,          /* shouldn't happen */
129         vfs_stdcheckexp,
130         vfs_stdvptofh,          /* shouldn't happen */
131         nfs_init,
132         nfs_uninit,
133         vfs_stdextattrctl,
134 };
135 VFS_SET(nfs_vfsops, nfs, VFCF_NETWORK);
136
137 /*
138  * This structure must be filled in by a primary bootstrap or bootstrap
139  * server for a diskless/dataless machine. It is initialized below just
140  * to ensure that it is allocated to initialized data (.data not .bss).
141  */
142 struct nfs_diskless nfs_diskless = { { { 0 } } };
143 struct nfsv3_diskless nfsv3_diskless = { { { 0 } } };
144 int nfs_diskless_valid = 0;
145
146 SYSCTL_INT(_vfs_nfs, OID_AUTO, diskless_valid, CTLFLAG_RD, 
147         &nfs_diskless_valid, 0, "");
148
149 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_rootpath, CTLFLAG_RD,
150         nfsv3_diskless.root_hostnam, 0, "");
151
152 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_rootaddr, CTLFLAG_RD,
153         &nfsv3_diskless.root_saddr, sizeof nfsv3_diskless.root_saddr,
154         "%Ssockaddr_in", "");
155
156 SYSCTL_STRING(_vfs_nfs, OID_AUTO, diskless_swappath, CTLFLAG_RD,
157         nfsv3_diskless.swap_hostnam, 0, "");
158
159 SYSCTL_OPAQUE(_vfs_nfs, OID_AUTO, diskless_swapaddr, CTLFLAG_RD,
160         &nfsv3_diskless.swap_saddr, sizeof nfsv3_diskless.swap_saddr, 
161         "%Ssockaddr_in","");
162
163
164 void nfsargs_ntoh __P((struct nfs_args *));
165 static int nfs_mountdiskless __P((char *, char *, int,
166                                   struct sockaddr_in *, struct nfs_args *,
167                                   struct thread *, struct vnode **,
168                                   struct mount **));
169 static void nfs_convert_diskless __P((void));
170 static void nfs_convert_oargs __P((struct nfs_args *args,
171                                    struct onfs_args *oargs));
172
173 static int
174 nfs_iosize(nmp)
175         struct nfsmount* nmp;
176 {
177         int iosize;
178
179         /*
180          * Calculate the size used for io buffers.  Use the larger
181          * of the two sizes to minimise nfs requests but make sure
182          * that it is at least one VM page to avoid wasting buffer
183          * space.
184          */
185         iosize = max(nmp->nm_rsize, nmp->nm_wsize);
186         if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
187         return iosize;
188 }
189
190 static void
191 nfs_convert_oargs(args, oargs)
192         struct nfs_args *args;
193         struct onfs_args *oargs;
194 {
195         args->version = NFS_ARGSVERSION;
196         args->addr = oargs->addr;
197         args->addrlen = oargs->addrlen;
198         args->sotype = oargs->sotype;
199         args->proto = oargs->proto;
200         args->fh = oargs->fh;
201         args->fhsize = oargs->fhsize;
202         args->flags = oargs->flags;
203         args->wsize = oargs->wsize;
204         args->rsize = oargs->rsize;
205         args->readdirsize = oargs->readdirsize;
206         args->timeo = oargs->timeo;
207         args->retrans = oargs->retrans;
208         args->maxgrouplist = oargs->maxgrouplist;
209         args->readahead = oargs->readahead;
210         args->leaseterm = oargs->leaseterm;
211         args->deadthresh = oargs->deadthresh;
212         args->hostname = oargs->hostname;
213 }
214
215 static void
216 nfs_convert_diskless()
217 {
218         bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
219                 sizeof(struct ifaliasreq));
220         bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
221                 sizeof(struct sockaddr_in));
222         nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args);
223         nfsv3_diskless.swap_fhsize = NFSX_V2FH;
224         bcopy(nfs_diskless.swap_fh,nfsv3_diskless.swap_fh,NFSX_V2FH);
225         bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr,
226                 sizeof(struct sockaddr_in));
227         bcopy(nfs_diskless.swap_hostnam,nfsv3_diskless.swap_hostnam, MNAMELEN);
228         nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks;
229         bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred,
230                 sizeof(struct ucred));
231         nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
232         nfsv3_diskless.root_fhsize = NFSX_V2FH;
233         bcopy(nfs_diskless.root_fh,nfsv3_diskless.root_fh,NFSX_V2FH);
234         bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
235                 sizeof(struct sockaddr_in));
236         bcopy(nfs_diskless.root_hostnam,nfsv3_diskless.root_hostnam, MNAMELEN);
237         nfsv3_diskless.root_time = nfs_diskless.root_time;
238         bcopy(nfs_diskless.my_hostnam,nfsv3_diskless.my_hostnam,
239                 MAXHOSTNAMELEN);
240         nfs_diskless_valid = 3;
241 }
242
243 /*
244  * nfs statfs call
245  */
246 int
247 nfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
248 {
249         register struct vnode *vp;
250         register struct nfs_statfs *sfp;
251         register caddr_t cp;
252         register u_int32_t *tl;
253         register int32_t t1, t2;
254         caddr_t bpos, dpos, cp2;
255         struct nfsmount *nmp = VFSTONFS(mp);
256         int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
257         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
258         struct ucred *cred;
259         struct nfsnode *np;
260         u_quad_t tquad;
261
262 #ifndef nolint
263         sfp = (struct nfs_statfs *)0;
264 #endif
265         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
266         if (error)
267                 return (error);
268         vp = NFSTOV(np);
269         cred = crget();
270         cred->cr_ngroups = 1;
271         if (v3 && (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
272                 (void)nfs_fsinfo(nmp, vp, cred, td);
273         nfsstats.rpccnt[NFSPROC_FSSTAT]++;
274         nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
275         nfsm_fhtom(vp, v3);
276         nfsm_request(vp, NFSPROC_FSSTAT, td, cred);
277         if (v3)
278                 nfsm_postop_attr(vp, retattr);
279         if (error) {
280                 if (mrep != NULL)
281                         m_freem(mrep);
282                 goto nfsmout;
283         }
284         nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
285         sbp->f_flags = nmp->nm_flag;
286         sbp->f_iosize = nfs_iosize(nmp);
287         if (v3) {
288                 sbp->f_bsize = NFS_FABLKSIZE;
289                 tquad = fxdr_hyper(&sfp->sf_tbytes);
290                 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
291                 tquad = fxdr_hyper(&sfp->sf_fbytes);
292                 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
293                 tquad = fxdr_hyper(&sfp->sf_abytes);
294                 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
295                 sbp->f_files = (fxdr_unsigned(int32_t,
296                     sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
297                 sbp->f_ffree = (fxdr_unsigned(int32_t,
298                     sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
299         } else {
300                 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
301                 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
302                 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
303                 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
304                 sbp->f_files = 0;
305                 sbp->f_ffree = 0;
306         }
307         if (sbp != &mp->mnt_stat) {
308                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
309                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
310                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
311         }
312         nfsm_reqdone;
313         vput(vp);
314         crfree(cred);
315         return (error);
316 }
317
318 /*
319  * nfs version 3 fsinfo rpc call
320  */
321 int
322 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, 
323         struct ucred *cred, struct thread *td)
324 {
325         register struct nfsv3_fsinfo *fsp;
326         register caddr_t cp;
327         register int32_t t1, t2;
328         register u_int32_t *tl, pref, max;
329         caddr_t bpos, dpos, cp2;
330         int error = 0, retattr;
331         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
332         u_int64_t maxfsize;
333
334         nfsstats.rpccnt[NFSPROC_FSINFO]++;
335         nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
336         nfsm_fhtom(vp, 1);
337         nfsm_request(vp, NFSPROC_FSINFO, td, cred);
338         nfsm_postop_attr(vp, retattr);
339         if (!error) {
340                 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
341                 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
342                 if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
343                         nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
344                                 ~(NFS_FABLKSIZE - 1);
345                 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
346                 if (max < nmp->nm_wsize && max > 0) {
347                         nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
348                         if (nmp->nm_wsize == 0)
349                                 nmp->nm_wsize = max;
350                 }
351                 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
352                 if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
353                         nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
354                                 ~(NFS_FABLKSIZE - 1);
355                 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
356                 if (max < nmp->nm_rsize && max > 0) {
357                         nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
358                         if (nmp->nm_rsize == 0)
359                                 nmp->nm_rsize = max;
360                 }
361                 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
362                 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRBLKSIZ)
363                         nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
364                                 ~(NFS_DIRBLKSIZ - 1);
365                 if (max < nmp->nm_readdirsize && max > 0) {
366                         nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
367                         if (nmp->nm_readdirsize == 0)
368                                 nmp->nm_readdirsize = max;
369                 }
370                 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
371                 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
372                         nmp->nm_maxfilesize = maxfsize;
373                 nmp->nm_state |= NFSSTA_GOTFSINFO;
374         }
375         nfsm_reqdone;
376         return (error);
377 }
378
379 /*
380  * Mount a remote root fs via. nfs. This depends on the info in the
381  * nfs_diskless structure that has been filled in properly by some primary
382  * bootstrap.
383  * It goes something like this:
384  * - do enough of "ifconfig" by calling ifioctl() so that the system
385  *   can talk to the server
386  * - If nfs_diskless.mygateway is filled in, use that address as
387  *   a default gateway.
388  * - build the rootfs mount point and call mountnfs() to do the rest.
389  */
390 int
391 nfs_mountroot(mp)
392         struct mount *mp;
393 {
394         struct mount  *swap_mp;
395         struct nfsv3_diskless *nd = &nfsv3_diskless;
396         struct socket *so;
397         struct vnode *vp;
398         struct thread *td = curthread;          /* XXX */
399         int error, i;
400         u_long l;
401         char buf[128];
402
403 #if defined(BOOTP_NFSROOT) && defined(BOOTP)
404         bootpc_init();          /* use bootp to get nfs_diskless filled in */
405 #endif
406
407         /*
408          * XXX time must be non-zero when we init the interface or else
409          * the arp code will wedge...
410          */
411         while (time_second == 0)
412                 tsleep(&time_second, PZERO+8, "arpkludge", 10);
413
414         if (nfs_diskless_valid==1) 
415           nfs_convert_diskless();
416
417         /*
418          * XXX splnet, so networks will receive...
419          */
420         splnet();
421
422 #ifdef notyet
423         /* Set up swap credentials. */
424         proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
425         proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
426         if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
427                 NGROUPS)
428                 proc0.p_ucred->cr_ngroups = NGROUPS;
429         for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
430             proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
431 #endif
432
433         /*
434          * Do enough of ifconfig(8) so that the critical net interface can
435          * talk to the server.
436          */
437         error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0, td);
438         if (error)
439                 panic("nfs_mountroot: socreate(%04x): %d",
440                         nd->myif.ifra_addr.sa_family, error);
441
442 #if 0 /* XXX Bad idea */
443         /*
444          * We might not have been told the right interface, so we pass
445          * over the first ten interfaces of the same kind, until we get
446          * one of them configured.
447          */
448
449         for (i = strlen(nd->myif.ifra_name) - 1;
450                 nd->myif.ifra_name[i] >= '0' &&
451                 nd->myif.ifra_name[i] <= '9';
452                 nd->myif.ifra_name[i] ++) {
453                 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
454                 if(!error)
455                         break;
456         }
457 #endif
458         error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, td);
459         if (error)
460                 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
461         soclose(so);
462
463         /*
464          * If the gateway field is filled in, set it as the default route.
465          */
466         if (nd->mygateway.sin_len != 0) {
467                 struct sockaddr_in mask, sin;
468
469                 bzero((caddr_t)&mask, sizeof(mask));
470                 sin = mask;
471                 sin.sin_family = AF_INET;
472                 sin.sin_len = sizeof(sin);
473                 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
474                     (struct sockaddr *)&nd->mygateway,
475                     (struct sockaddr *)&mask,
476                     RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
477                 if (error)
478                         panic("nfs_mountroot: RTM_ADD: %d", error);
479         }
480
481         /*
482          * Create the rootfs mount point.
483          */
484         nd->root_args.fh = nd->root_fh;
485         nd->root_args.fhsize = nd->root_fhsize;
486         l = ntohl(nd->root_saddr.sin_addr.s_addr);
487         snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
488                 (l >> 24) & 0xff, (l >> 16) & 0xff,
489                 (l >>  8) & 0xff, (l >>  0) & 0xff,nd->root_hostnam);
490         printf("NFS ROOT: %s\n",buf);
491         if ((error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
492             &nd->root_saddr, &nd->root_args, td, &vp, &mp)) != 0) {
493                 if (swap_mp) {
494                         mp->mnt_vfc->vfc_refcount--;
495                         free(swap_mp, M_MOUNT);
496                 }
497                 return (error);
498         }
499
500         swap_mp = NULL;
501         if (nd->swap_nblks) {
502
503                 /* Convert to DEV_BSIZE instead of Kilobyte */
504                 nd->swap_nblks *= 2;
505
506                 /*
507                  * Create a fake mount point just for the swap vnode so that the
508                  * swap file can be on a different server from the rootfs.
509                  */
510                 nd->swap_args.fh = nd->swap_fh;
511                 nd->swap_args.fhsize = nd->swap_fhsize;
512                 l = ntohl(nd->swap_saddr.sin_addr.s_addr);
513                 snprintf(buf, sizeof(buf), "%ld.%ld.%ld.%ld:%s",
514                         (l >> 24) & 0xff, (l >> 16) & 0xff,
515                         (l >>  8) & 0xff, (l >>  0) & 0xff,nd->swap_hostnam);
516                 printf("NFS SWAP: %s\n",buf);
517                 if ((error = nfs_mountdiskless(buf, "/swap", 0,
518                     &nd->swap_saddr, &nd->swap_args, td, &vp, &swap_mp)) != 0)
519                         return (error);
520                 vfs_unbusy(swap_mp, td);
521
522                 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size = 
523                                 nd->swap_nblks * DEV_BSIZE ;
524                 
525                 /*
526                  * Since the swap file is not the root dir of a file system,
527                  * hack it to a regular file.
528                  */
529                 vp->v_type = VREG;
530                 vp->v_flag = 0;
531                 VREF(vp);
532                 swaponvp(td, vp, NODEV, nd->swap_nblks);
533         }
534
535         mp->mnt_flag |= MNT_ROOTFS;
536         mp->mnt_vnodecovered = NULLVP;
537         rootvp = vp;
538         vfs_unbusy(mp, td);
539
540         /*
541          * This is not really an nfs issue, but it is much easier to
542          * set hostname here and then let the "/etc/rc.xxx" files
543          * mount the right /var based upon its preset value.
544          */
545         bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
546         hostname[MAXHOSTNAMELEN - 1] = '\0';
547         for (i = 0; i < MAXHOSTNAMELEN; i++)
548                 if (hostname[i] == '\0')
549                         break;
550         inittodr(ntohl(nd->root_time));
551         return (0);
552 }
553
554 /*
555  * Internal version of mount system call for diskless setup.
556  */
557 static int
558 nfs_mountdiskless(char *path, char *which, int mountflag,
559         struct sockaddr_in *sin, struct nfs_args *args, struct thread *td,
560         struct vnode **vpp, struct mount **mpp)
561 {
562         struct mount *mp;
563         struct sockaddr *nam;
564         int error;
565         int didalloc = 0;
566
567         mp = *mpp;
568
569         if (mp == NULL) {
570                 if ((error = vfs_rootmountalloc("nfs", path, &mp)) != 0) {
571                         printf("nfs_mountroot: NFS not configured");
572                         return (error);
573                 }
574                 didalloc = 1;
575         }
576
577         mp->mnt_kern_flag = 0;
578         mp->mnt_flag = mountflag;
579         nam = dup_sockaddr((struct sockaddr *)sin, 1);
580         if ((error = mountnfs(args, mp, nam, which, path, vpp)) != 0) {
581                 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
582                 mp->mnt_vfc->vfc_refcount--;
583                 vfs_unbusy(mp, td);
584                 if (didalloc)
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(struct mount *mp, char *path, caddr_t data,
754         struct nameidata *ndp, struct thread *td)
755 {
756         int error;
757         struct nfs_args args;
758         struct sockaddr *nam;
759         struct vnode *vp;
760         char pth[MNAMELEN], hst[MNAMELEN];
761         size_t len;
762         u_char nfh[NFSX_V3FHMAX];
763
764         if (path == NULL) {
765                 nfs_mountroot(mp);
766                 return (0);
767         }
768         error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
769         if (error)
770                 return (error);
771         if (args.version != NFS_ARGSVERSION) {
772 #ifdef COMPAT_PRELITE2
773                 /*
774                  * If the argument version is unknown, then assume the
775                  * caller is a pre-lite2 4.4BSD client and convert its
776                  * arguments.
777                  */
778                 struct onfs_args oargs;
779                 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
780                 if (error)
781                         return (error);
782                 nfs_convert_oargs(&args,&oargs);
783 #else /* !COMPAT_PRELITE2 */
784                 return (EPROGMISMATCH);
785 #endif /* COMPAT_PRELITE2 */
786         }
787         if (mp->mnt_flag & MNT_UPDATE) {
788                 register struct nfsmount *nmp = VFSTONFS(mp);
789
790                 if (nmp == NULL)
791                         return (EIO);
792                 /*
793                  * When doing an update, we can't change from or to
794                  * v3 and/or nqnfs, or change cookie translation
795                  */
796                 args.flags = (args.flags &
797                     ~(NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/)) |
798                     (nmp->nm_flag &
799                         (NFSMNT_NFSV3|NFSMNT_NQNFS /*|NFSMNT_XLATECOOKIE*/));
800                 nfs_decode_args(nmp, &args);
801                 return (0);
802         }
803
804         /*
805          * Make the nfs_ip_paranoia sysctl serve as the default connection
806          * or no-connection mode for those protocols that support 
807          * no-connection mode (the flag will be cleared later for protocols
808          * that do not support no-connection mode).  This will allow a client
809          * to receive replies from a different IP then the request was
810          * sent to.  Note: default value for nfs_ip_paranoia is 1 (paranoid),
811          * not 0.
812          */
813         if (nfs_ip_paranoia == 0)
814                 args.flags |= NFSMNT_NOCONN;
815         if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
816                 return (EINVAL);
817         error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
818         if (error)
819                 return (error);
820         error = copyinstr(path, pth, MNAMELEN-1, &len);
821         if (error)
822                 return (error);
823         bzero(&pth[len], MNAMELEN - len);
824         error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
825         if (error)
826                 return (error);
827         bzero(&hst[len], MNAMELEN - len);
828         /* sockargs() call must be after above copyin() calls */
829         error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
830         if (error)
831                 return (error);
832         args.fh = nfh;
833         error = mountnfs(&args, mp, nam, pth, hst, &vp);
834         return (error);
835 }
836
837 /*
838  * Common code for mount and mountroot
839  */
840 static int
841 mountnfs(struct nfs_args *argp, struct mount *mp, struct sockaddr *nam,
842         char *pth, char *hst, struct vnode **vpp)
843 {
844         register struct nfsmount *nmp;
845         struct nfsnode *np;
846         int error;
847         struct vattr attrs;
848
849         if (mp->mnt_flag & MNT_UPDATE) {
850                 nmp = VFSTONFS(mp);
851                 /* update paths, file handles, etc, here        XXX */
852                 FREE(nam, M_SONAME);
853                 return (0);
854         } else {
855                 nmp = zalloc(nfsmount_zone);
856                 bzero((caddr_t)nmp, sizeof (struct nfsmount));
857                 TAILQ_INIT(&nmp->nm_uidlruhead);
858                 TAILQ_INIT(&nmp->nm_bufq);
859                 mp->mnt_data = (qaddr_t)nmp;
860         }
861         vfs_getnewfsid(mp);
862         nmp->nm_mountp = mp;
863         if (argp->flags & NFSMNT_NQNFS)
864                 /*
865                  * We have to set mnt_maxsymlink to a non-zero value so
866                  * that COMPAT_43 routines will know that we are setting
867                  * the d_type field in directories (and can zero it for
868                  * unsuspecting binaries).
869                  */
870                 mp->mnt_maxsymlinklen = 1;
871
872         /*
873          * V2 can only handle 32 bit filesizes.  A 4GB-1 limit may be too
874          * high, depending on whether we end up with negative offsets in
875          * the client or server somewhere.  2GB-1 may be safer.
876          *
877          * For V3, nfs_fsinfo will adjust this as necessary.  Assume maximum
878          * that we can handle until we find out otherwise.
879          * XXX Our "safe" limit on the client is what we can store in our
880          * buffer cache using signed(!) block numbers.
881          */
882         if ((argp->flags & NFSMNT_NFSV3) == 0)
883                 nmp->nm_maxfilesize = 0xffffffffLL;
884         else
885                 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
886
887         nmp->nm_timeo = NFS_TIMEO;
888         nmp->nm_retry = NFS_RETRANS;
889         nmp->nm_wsize = NFS_WSIZE;
890         nmp->nm_rsize = NFS_RSIZE;
891         nmp->nm_readdirsize = NFS_READDIRSIZE;
892         nmp->nm_numgrps = NFS_MAXGRPS;
893         nmp->nm_readahead = NFS_DEFRAHEAD;
894         nmp->nm_leaseterm = NQ_DEFLEASE;
895         nmp->nm_deadthresh = NQ_DEADTHRESH;
896         CIRCLEQ_INIT(&nmp->nm_timerhead);
897         nmp->nm_inprog = NULLVP;
898         nmp->nm_fhsize = argp->fhsize;
899         bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
900         bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
901         bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
902         nmp->nm_nam = nam;
903         /* Set up the sockets and per-host congestion */
904         nmp->nm_sotype = argp->sotype;
905         nmp->nm_soproto = argp->proto;
906
907         nfs_decode_args(nmp, argp);
908
909         /*
910          * For Connection based sockets (TCP,...) defer the connect until
911          * the first request, in case the server is not responding.
912          */
913         if (nmp->nm_sotype == SOCK_DGRAM &&
914                 (error = nfs_connect(nmp, (struct nfsreq *)0)))
915                 goto bad;
916
917         /*
918          * This is silly, but it has to be set so that vinifod() works.
919          * We do not want to do an nfs_statfs() here since we can get
920          * stuck on a dead server and we are holding a lock on the mount
921          * point.
922          */
923         mp->mnt_stat.f_iosize = nfs_iosize(nmp);
924         /*
925          * A reference count is needed on the nfsnode representing the
926          * remote root.  If this object is not persistent, then backward
927          * traversals of the mount point (i.e. "..") will not work if
928          * the nfsnode gets flushed out of the cache. Ufs does not have
929          * this problem, because one can identify root inodes by their
930          * number == ROOTINO (2).
931          */
932         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
933         if (error)
934                 goto bad;
935         *vpp = NFSTOV(np);
936
937         /*
938          * Get file attributes for the mountpoint.  This has the side
939          * effect of filling in (*vpp)->v_type with the correct value.
940          */
941         VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curthread);
942
943         /*
944          * Lose the lock but keep the ref.
945          */
946         VOP_UNLOCK(*vpp, 0, curthread);
947
948         return (0);
949 bad:
950         nfs_disconnect(nmp);
951         zfree(nfsmount_zone, nmp);
952         FREE(nam, M_SONAME);
953         return (error);
954 }
955
956 /*
957  * unmount system call
958  */
959 static int
960 nfs_unmount(struct mount *mp, int mntflags, struct thread *td)
961 {
962         register struct nfsmount *nmp;
963         int error, flags = 0;
964
965         if (mntflags & MNT_FORCE)
966                 flags |= FORCECLOSE;
967         nmp = VFSTONFS(mp);
968         /*
969          * Goes something like this..
970          * - Call vflush() to clear out vnodes for this file system
971          * - Close the socket
972          * - Free up the data structures
973          */
974         /* In the forced case, cancel any outstanding requests. */
975         if (flags & FORCECLOSE) {
976                 error = nfs_nmcancelreqs(nmp);
977                 if (error)
978                         return (error);
979         }
980         /*
981          * Must handshake with nqnfs_clientd() if it is active.
982          */
983         nmp->nm_state |= NFSSTA_DISMINPROG;
984         while (nmp->nm_inprog != NULLVP)
985                 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
986
987         /* We hold 1 extra ref on the root vnode; see comment in mountnfs(). */
988         error = vflush(mp, 1, flags);
989         if (error) {
990                 nmp->nm_state &= ~NFSSTA_DISMINPROG;
991                 return (error);
992         }
993
994         /*
995          * We are now committed to the unmount.
996          * For NQNFS, let the server daemon free the nfsmount structure.
997          */
998         if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
999                 nmp->nm_state |= NFSSTA_DISMNT;
1000
1001         nfs_disconnect(nmp);
1002         FREE(nmp->nm_nam, M_SONAME);
1003
1004         if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
1005                 zfree(nfsmount_zone, nmp);
1006         return (0);
1007 }
1008
1009 /*
1010  * Return root of a filesystem
1011  */
1012 static int
1013 nfs_root(mp, vpp)
1014         struct mount *mp;
1015         struct vnode **vpp;
1016 {
1017         register struct vnode *vp;
1018         struct nfsmount *nmp;
1019         struct nfsnode *np;
1020         int error;
1021
1022         nmp = VFSTONFS(mp);
1023         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
1024         if (error)
1025                 return (error);
1026         vp = NFSTOV(np);
1027         if (vp->v_type == VNON)
1028             vp->v_type = VDIR;
1029         vp->v_flag = VROOT;
1030         *vpp = vp;
1031         return (0);
1032 }
1033
1034 extern int syncprt;
1035
1036 /*
1037  * Flush out the buffer cache
1038  */
1039 /* ARGSUSED */
1040 static int
1041 nfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td)
1042 {
1043         struct vnode *vp;
1044         int error, allerror = 0;
1045
1046         /*
1047          * Force stale buffer cache information to be flushed.
1048          */
1049 loop:
1050         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1051              vp != NULL;
1052              vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
1053                 /*
1054                  * If the vnode that we are about to sync is no longer
1055                  * associated with this mount point, start over.
1056                  */
1057                 if (vp->v_mount != mp)
1058                         goto loop;
1059                 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
1060                     waitfor == MNT_LAZY)
1061                         continue;
1062                 if (vget(vp, LK_EXCLUSIVE, td))
1063                         goto loop;
1064                 error = VOP_FSYNC(vp, cred, waitfor, td);
1065                 if (error)
1066                         allerror = error;
1067                 vput(vp);
1068         }
1069         return (allerror);
1070 }
1071