]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfs/nfs_vfsops.c
Fix an omission of a line from the previous commit to this file. The
[FreeBSD/FreeBSD.git] / sys / 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  * $Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
38  */
39
40 #include <sys/param.h>
41 #include <sys/sockio.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/systm.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55
56 #include <net/if.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfs/nfs.h>
63 #include <nfs/nfsnode.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/xdr_subs.h>
66 #include <nfs/nfsm_subs.h>
67 #include <nfs/nfsdiskless.h>
68 #include <nfs/nqnfs.h>
69
70 extern int      nfs_mountroot __P((struct mount *mp));
71
72 extern int      nfs_ticks;
73
74 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
75 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
76 MALLOC_DEFINE(M_NFSBIGFH, "NFSV3 bigfh", "NFS version 3 file handle");
77 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
78 MALLOC_DEFINE(M_NFSDIROFF, "NFSV3 diroff", "NFS directory offset data");
79 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
80 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
81 MALLOC_DEFINE(M_NQLEASE, "NQNFS Lease", "Nqnfs lease");
82
83 struct nfsstats nfsstats;
84 SYSCTL_NODE(_vfs, MOUNT_NFS, nfs, CTLFLAG_RW, 0, "NFS filesystem");
85 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
86         &nfsstats, 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 __P((struct nfsmount *nmp));
93 static int      mountnfs __P((struct nfs_args *,struct mount *,
94                         struct sockaddr *,char *,char *,struct vnode **));
95 static int      nfs_mount __P(( struct mount *mp, char *path, caddr_t data,
96                         struct nameidata *ndp, struct proc *p));
97 static int      nfs_start __P(( struct mount *mp, int flags,
98                         struct proc *p));
99 static int      nfs_unmount __P(( struct mount *mp, int mntflags,
100                         struct proc *p));
101 static int      nfs_root __P(( struct mount *mp, struct vnode **vpp));
102 static int      nfs_quotactl __P(( struct mount *mp, int cmds, uid_t uid,
103                         caddr_t arg, struct proc *p));
104 static int      nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
105                         struct proc *p));
106 static int      nfs_sync __P(( struct mount *mp, int waitfor,
107                         struct ucred *cred, struct proc *p));
108 static int      nfs_vptofh __P(( struct vnode *vp, struct fid *fhp));
109 static int      nfs_fhtovp __P((struct mount *mp, struct fid *fhp,
110                         struct sockaddr *nam, struct vnode **vpp,
111                         int *exflagsp, struct ucred **credanonp));
112 static int      nfs_vget __P((struct mount *, ino_t, struct vnode **));
113
114
115 /*
116  * nfs vfs operations.
117  */
118 static struct vfsops nfs_vfsops = {
119         nfs_mount,
120         nfs_start,
121         nfs_unmount,
122         nfs_root,
123         nfs_quotactl,
124         nfs_statfs,
125         nfs_sync,
126         nfs_vget,
127         nfs_fhtovp,
128         nfs_vptofh,
129         nfs_init
130 };
131 VFS_SET(nfs_vfsops, nfs, MOUNT_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 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 nfs_iosize(nmp)
170         struct nfsmount* nmp;
171 {
172         int iosize;
173
174         /*
175          * Calculate the size used for io buffers.  Use the larger
176          * of the two sizes to minimise nfs requests but make sure
177          * that it is at least one VM page to avoid wasting buffer
178          * space.
179          */
180         iosize = max(nmp->nm_rsize, nmp->nm_wsize);
181         if (iosize < PAGE_SIZE) iosize = PAGE_SIZE;
182         return iosize;
183 }
184
185 static void nfs_convert_oargs(args,oargs)
186      struct nfs_args *args;
187      struct onfs_args *oargs;
188 {
189   args->version = NFS_ARGSVERSION;
190   args->addr = oargs->addr;
191   args->addrlen = oargs->addrlen;
192   args->sotype = oargs->sotype;
193   args->proto = oargs->proto;
194   args->fh = oargs->fh;
195   args->fhsize = oargs->fhsize;
196   args->flags = oargs->flags;
197   args->wsize = oargs->wsize;
198   args->rsize = oargs->rsize;
199   args->readdirsize = oargs->readdirsize;
200   args->timeo = oargs->timeo;
201   args->retrans = oargs->retrans;
202   args->maxgrouplist = oargs->maxgrouplist;
203   args->readahead = oargs->readahead;
204   args->leaseterm = oargs->leaseterm;
205   args->deadthresh = oargs->deadthresh;
206   args->hostname = oargs->hostname;
207 }
208
209 void nfs_convert_diskless()
210 {
211   bcopy(&nfs_diskless.myif, &nfsv3_diskless.myif,
212         sizeof(struct ifaliasreq));
213   bcopy(&nfs_diskless.mygateway, &nfsv3_diskless.mygateway,
214         sizeof(struct sockaddr_in));
215   nfs_convert_oargs(&nfsv3_diskless.swap_args,&nfs_diskless.swap_args);
216   nfsv3_diskless.swap_fhsize = NFSX_V2FH;
217   bcopy(nfs_diskless.swap_fh,nfsv3_diskless.swap_fh,NFSX_V2FH);
218   bcopy(&nfs_diskless.swap_saddr,&nfsv3_diskless.swap_saddr,
219         sizeof(struct sockaddr_in));
220   bcopy(nfs_diskless.swap_hostnam,nfsv3_diskless.swap_hostnam,
221         MNAMELEN);
222   nfsv3_diskless.swap_nblks = nfs_diskless.swap_nblks;
223   bcopy(&nfs_diskless.swap_ucred, &nfsv3_diskless.swap_ucred,
224         sizeof(struct ucred));
225   nfs_convert_oargs(&nfsv3_diskless.root_args,&nfs_diskless.root_args);
226   nfsv3_diskless.root_fhsize = NFSX_V2FH;
227   bcopy(nfs_diskless.root_fh,nfsv3_diskless.root_fh,NFSX_V2FH);
228   bcopy(&nfs_diskless.root_saddr,&nfsv3_diskless.root_saddr,
229         sizeof(struct sockaddr_in));
230   bcopy(nfs_diskless.root_hostnam,nfsv3_diskless.root_hostnam,
231         MNAMELEN);
232   nfsv3_diskless.root_time = nfs_diskless.root_time;
233   bcopy(nfs_diskless.my_hostnam,nfsv3_diskless.my_hostnam,
234         MAXHOSTNAMELEN);
235   nfs_diskless_valid = 3;
236 }
237
238 /*
239  * nfs statfs call
240  */
241 int
242 nfs_statfs(mp, sbp, p)
243         struct mount *mp;
244         register struct statfs *sbp;
245         struct proc *p;
246 {
247         register struct vnode *vp;
248         register struct nfs_statfs *sfp;
249         register caddr_t cp;
250         register u_long *tl;
251         register long t1, t2;
252         caddr_t bpos, dpos, cp2;
253         struct nfsmount *nmp = VFSTONFS(mp);
254         int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
255         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
256         struct ucred *cred;
257         struct nfsnode *np;
258         u_quad_t tquad;
259
260 #ifndef nolint
261         sfp = (struct nfs_statfs *)0;
262 #endif
263         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
264         if (error)
265                 return (error);
266         vp = NFSTOV(np);
267         cred = crget();
268         cred->cr_ngroups = 1;
269         if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
270                 (void)nfs_fsinfo(nmp, vp, cred, p);
271         nfsstats.rpccnt[NFSPROC_FSSTAT]++;
272         nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
273         nfsm_fhtom(vp, v3);
274         nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
275         if (v3)
276                 nfsm_postop_attr(vp, retattr);
277         if (!error) {
278                 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
279         } else
280                 goto nfsmout;
281
282         sbp->f_type = MOUNT_NFS;
283         sbp->f_flags = nmp->nm_flag;
284         sbp->f_iosize = nfs_iosize(nmp);
285         if (v3) {
286                 sbp->f_bsize = NFS_FABLKSIZE;
287                 fxdr_hyper(&sfp->sf_tbytes, &tquad);
288                 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
289                 fxdr_hyper(&sfp->sf_fbytes, &tquad);
290                 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
291                 fxdr_hyper(&sfp->sf_abytes, &tquad);
292                 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
293                 sbp->f_files = (fxdr_unsigned(long, sfp->sf_tfiles.nfsuquad[1])
294                         & 0x7fffffff);
295                 sbp->f_ffree = (fxdr_unsigned(long, sfp->sf_ffiles.nfsuquad[1])
296                         & 0x7fffffff);
297         } else {
298                 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
299                 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
300                 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
301                 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
302                 sbp->f_files = 0;
303                 sbp->f_ffree = 0;
304         }
305         if (sbp != &mp->mnt_stat) {
306                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
307                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
308         }
309         nfsm_reqdone;
310         vput(vp);
311         crfree(cred);
312         return (error);
313 }
314
315 /*
316  * nfs version 3 fsinfo rpc call
317  */
318 int
319 nfs_fsinfo(nmp, vp, cred, p)
320         register struct nfsmount *nmp;
321         register struct vnode *vp;
322         struct ucred *cred;
323         struct proc *p;
324 {
325         register struct nfsv3_fsinfo *fsp;
326         register caddr_t cp;
327         register long t1, t2;
328         register u_long *tl, pref, max;
329         caddr_t bpos, dpos, cp2;
330         int error = 0, retattr;
331         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
332
333         nfsstats.rpccnt[NFSPROC_FSINFO]++;
334         nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
335         nfsm_fhtom(vp, 1);
336         nfsm_request(vp, NFSPROC_FSINFO, p, cred);
337         nfsm_postop_attr(vp, retattr);
338         if (!error) {
339                 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
340                 pref = fxdr_unsigned(u_long, fsp->fs_wtpref);
341                 if (pref < nmp->nm_wsize)
342                         nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
343                                 ~(NFS_FABLKSIZE - 1);
344                 max = fxdr_unsigned(u_long, fsp->fs_wtmax);
345                 if (max < nmp->nm_wsize) {
346                         nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
347                         if (nmp->nm_wsize == 0)
348                                 nmp->nm_wsize = max;
349                 }
350                 pref = fxdr_unsigned(u_long, fsp->fs_rtpref);
351                 if (pref < nmp->nm_rsize)
352                         nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
353                                 ~(NFS_FABLKSIZE - 1);
354                 max = fxdr_unsigned(u_long, fsp->fs_rtmax);
355                 if (max < nmp->nm_rsize) {
356                         nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
357                         if (nmp->nm_rsize == 0)
358                                 nmp->nm_rsize = max;
359                 }
360                 pref = fxdr_unsigned(u_long, fsp->fs_dtpref);
361                 if (pref < nmp->nm_readdirsize)
362                         nmp->nm_readdirsize = pref;
363                 if (max < nmp->nm_readdirsize) {
364                         nmp->nm_readdirsize = max;
365                 }
366                 nmp->nm_flag |= NFSMNT_GOTFSINFO;
367         }
368         nfsm_reqdone;
369         return (error);
370 }
371
372 /*
373  * Mount a remote root fs via. nfs. This depends on the info in the
374  * nfs_diskless structure that has been filled in properly by some primary
375  * bootstrap.
376  * It goes something like this:
377  * - do enough of "ifconfig" by calling ifioctl() so that the system
378  *   can talk to the server
379  * - If nfs_diskless.mygateway is filled in, use that address as
380  *   a default gateway.
381  * - build the rootfs mount point and call mountnfs() to do the rest.
382  */
383 int
384 nfs_mountroot(mp)
385         struct mount *mp;
386 {
387         struct mount  *swap_mp;
388         struct nfsv3_diskless *nd = &nfsv3_diskless;
389         struct socket *so;
390         struct vnode *vp;
391         struct proc *p = curproc;               /* XXX */
392         int error, i;
393         u_long l;
394         char buf[128];
395
396         /*
397          * XXX time must be non-zero when we init the interface or else
398          * the arp code will wedge...
399          */
400         if (time.tv_sec == 0)
401                 time.tv_sec = 1;
402
403         if (nfs_diskless_valid==1) 
404           nfs_convert_diskless();
405
406         /*
407          * XXX splnet, so networks will receive...
408          */
409         splnet();
410
411 #ifdef notyet
412         /* Set up swap credentials. */
413         proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
414         proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
415         if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
416                 NGROUPS)
417                 proc0.p_ucred->cr_ngroups = NGROUPS;
418         for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
419             proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
420 #endif
421
422         /*
423          * Do enough of ifconfig(8) so that the critical net interface can
424          * talk to the server.
425          */
426         error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0, p);
427         if (error)
428                 panic("nfs_mountroot: socreate(%04x): %d",
429                         nd->myif.ifra_addr.sa_family, error);
430
431         /*
432          * We might not have been told the right interface, so we pass
433          * over the first ten interfaces of the same kind, until we get
434          * one of them configured.
435          */
436
437         for (i = strlen(nd->myif.ifra_name) - 1;
438                 nd->myif.ifra_name[i] >= '0' &&
439                 nd->myif.ifra_name[i] <= '9';
440                 nd->myif.ifra_name[i] ++) {
441                 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p);
442                 if(!error)
443                         break;
444         }
445         if (error)
446                 panic("nfs_mountroot: SIOCAIFADDR: %d", error);
447         soclose(so);
448
449         /*
450          * If the gateway field is filled in, set it as the default route.
451          */
452         if (nd->mygateway.sin_len != 0) {
453                 struct sockaddr_in mask, sin;
454
455                 bzero((caddr_t)&mask, sizeof(mask));
456                 sin = mask;
457                 sin.sin_family = AF_INET;
458                 sin.sin_len = sizeof(sin);
459                 error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
460                     (struct sockaddr *)&nd->mygateway,
461                     (struct sockaddr *)&mask,
462                     RTF_UP | RTF_GATEWAY, (struct rtentry **)0);
463                 if (error)
464                         panic("nfs_mountroot: RTM_ADD: %d", error);
465         }
466
467         /*
468          * Create the rootfs mount point.
469          */
470         nd->root_args.fh = nd->root_fh;
471         nd->root_args.fhsize = nd->root_fhsize;
472         l = ntohl(nd->root_saddr.sin_addr.s_addr);
473         sprintf(buf,"%ld.%ld.%ld.%ld:%s",
474                 (l >> 24) & 0xff, (l >> 16) & 0xff,
475                 (l >>  8) & 0xff, (l >>  0) & 0xff,nd->root_hostnam);
476         printf("NFS ROOT: %s\n",buf);
477         if (error = nfs_mountdiskless(buf, "/", MNT_RDONLY,
478             &nd->root_saddr, &nd->root_args, p, &vp, &mp)) {
479                 if (swap_mp) {
480                         mp->mnt_vfc->vfc_refcount--;
481                         free(swap_mp, M_MOUNT);
482                 }
483                 return (error);
484         }
485
486         swap_mp = NULL;
487         if (nd->swap_nblks) {
488
489                 /* Convert to DEV_BSIZE instead of Kilobyte */
490                 nd->swap_nblks *= 2;
491
492                 /*
493                  * Create a fake mount point just for the swap vnode so that the
494                  * swap file can be on a different server from the rootfs.
495                  */
496                 nd->swap_args.fh = nd->swap_fh;
497                 nd->swap_args.fhsize = nd->swap_fhsize;
498                 l = ntohl(nd->swap_saddr.sin_addr.s_addr);
499                 sprintf(buf,"%ld.%ld.%ld.%ld:%s",
500                         (l >> 24) & 0xff, (l >> 16) & 0xff,
501                         (l >>  8) & 0xff, (l >>  0) & 0xff,nd->swap_hostnam);
502                 printf("NFS SWAP: %s\n",buf);
503                 if (error = nfs_mountdiskless(buf, "/swap", 0,
504                     &nd->swap_saddr, &nd->swap_args, p, &vp, &swap_mp))
505                         return (error);
506                 vfs_unbusy(swap_mp, p);
507
508                 VTONFS(vp)->n_size = VTONFS(vp)->n_vattr.va_size = 
509                                 nd->swap_nblks * DEV_BSIZE ;
510                 
511                 /*
512                  * Since the swap file is not the root dir of a file system,
513                  * hack it to a regular file.
514                  */
515                 vp->v_type = VREG;
516                 vp->v_flag = 0;
517                 VREF(vp);
518                 swaponvp(p, vp, NODEV, nd->swap_nblks);
519         }
520
521         mp->mnt_flag |= MNT_ROOTFS;
522         mp->mnt_vnodecovered = NULLVP;
523         rootvp = vp;
524         vfs_unbusy(mp, p);
525
526         /*
527          * This is not really an nfs issue, but it is much easier to
528          * set hostname here and then let the "/etc/rc.xxx" files
529          * mount the right /var based upon its preset value.
530          */
531         bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
532         hostname[MAXHOSTNAMELEN - 1] = '\0';
533         for (i = 0; i < MAXHOSTNAMELEN; i++)
534                 if (hostname[i] == '\0')
535                         break;
536         inittodr(ntohl(nd->root_time));
537         return (0);
538 }
539
540 /*
541  * Internal version of mount system call for diskless setup.
542  */
543 static int
544 nfs_mountdiskless(path, which, mountflag, sin, args, p, vpp, mpp)
545         char *path;
546         char *which;
547         int mountflag;
548         struct sockaddr_in *sin;
549         struct nfs_args *args;
550         struct proc *p;
551         struct vnode **vpp;
552         struct mount **mpp;
553 {
554         struct mount *mp;
555         struct sockaddr *nam;
556         int error;
557
558         mp = *mpp;
559
560         if (!mp && (error = vfs_rootmountalloc("nfs", path, &mp))) {
561                 printf("nfs_mountroot: NFS not configured");
562                 return (error);
563         }
564
565         mp->mnt_kern_flag = 0;
566         mp->mnt_flag = mountflag;
567         nam = dup_sockaddr((struct sockaddr *)sin, 1);
568         if (error = mountnfs(args, mp, nam, which, path, vpp)) {
569                 printf("nfs_mountroot: mount %s on %s: %d", path, which, error);
570                 mp->mnt_vfc->vfc_refcount--;
571                 vfs_unbusy(mp, p);
572                 free(mp, M_MOUNT);
573                 FREE(nam, M_SONAME);
574                 return (error);
575         }
576         (void) copystr(which, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
577         *mpp = mp;
578         return (0);
579 }
580
581 /*
582  * VFS Operations.
583  *
584  * mount system call
585  * It seems a bit dumb to copyinstr() the host and path here and then
586  * bcopy() them in mountnfs(), but I wanted to detect errors before
587  * doing the sockargs() call because sockargs() allocates an mbuf and
588  * an error after that means that I have to release the mbuf.
589  */
590 /* ARGSUSED */
591 static int
592 nfs_mount(mp, path, data, ndp, p)
593         struct mount *mp;
594         char *path;
595         caddr_t data;
596         struct nameidata *ndp;
597         struct proc *p;
598 {
599         int error;
600         struct nfs_args args;
601         struct sockaddr *nam;
602         struct vnode *vp;
603         char pth[MNAMELEN], hst[MNAMELEN];
604         u_int len;
605         u_char nfh[NFSX_V3FHMAX];
606
607         if (path == NULL) {
608                 nfs_mountroot(mp);
609                 return (0);
610         }
611         error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
612         if (error)
613                 return (error);
614         if (args.version != NFS_ARGSVERSION) {
615 #ifndef NO_COMPAT_PRELITE2
616                 /*
617                  * If the argument version is unknown, then assume the
618                  * caller is a pre-lite2 4.4BSD client and convert its
619                  * arguments.
620                  */
621                 struct onfs_args oargs;
622                 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
623                 if (error)
624                         return (error);
625                 nfs_convert_oargs(&args,&oargs);
626 #else /* NO_COMPAT_PRELITE2 */
627                 return (EPROGMISMATCH);
628 #endif /* !NO_COMPAT_PRELITE2 */
629         }
630         error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
631         if (error)
632                 return (error);
633         error = copyinstr(path, pth, MNAMELEN-1, &len);
634         if (error)
635                 return (error);
636         bzero(&pth[len], MNAMELEN - len);
637         error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
638         if (error)
639                 return (error);
640         bzero(&hst[len], MNAMELEN - len);
641         /* sockargs() call must be after above copyin() calls */
642         error = getsockaddr(&nam, (caddr_t)args.addr, args.addrlen);
643         if (error)
644                 return (error);
645         args.fh = nfh;
646         error = mountnfs(&args, mp, nam, pth, hst, &vp);
647         return (error);
648 }
649
650 /*
651  * Common code for mount and mountroot
652  */
653 static int
654 mountnfs(argp, mp, nam, pth, hst, vpp)
655         register struct nfs_args *argp;
656         register struct mount *mp;
657         struct sockaddr *nam;
658         char *pth, *hst;
659         struct vnode **vpp;
660 {
661         register struct nfsmount *nmp;
662         struct nfsnode *np;
663         int error, maxio;
664         struct vattr attrs;
665
666         if (mp->mnt_flag & MNT_UPDATE) {
667                 nmp = VFSTONFS(mp);
668                 /* update paths, file handles, etc, here        XXX */
669                 FREE(nam, M_SONAME);
670                 return (0);
671         } else {
672                 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
673                     M_NFSMNT, M_WAITOK);
674                 bzero((caddr_t)nmp, sizeof (struct nfsmount));
675                 TAILQ_INIT(&nmp->nm_uidlruhead);
676                 TAILQ_INIT(&nmp->nm_bufq);
677                 mp->mnt_data = (qaddr_t)nmp;
678         }
679         vfs_getnewfsid(mp);
680         nmp->nm_mountp = mp;
681         nmp->nm_flag = argp->flags;
682         if (nmp->nm_flag & NFSMNT_NQNFS)
683                 /*
684                  * We have to set mnt_maxsymlink to a non-zero value so
685                  * that COMPAT_43 routines will know that we are setting
686                  * the d_type field in directories (and can zero it for
687                  * unsuspecting binaries).
688                  */
689                 mp->mnt_maxsymlinklen = 1;
690         nmp->nm_timeo = NFS_TIMEO;
691         nmp->nm_retry = NFS_RETRANS;
692         nmp->nm_wsize = NFS_WSIZE;
693         nmp->nm_rsize = NFS_RSIZE;
694         nmp->nm_readdirsize = NFS_READDIRSIZE;
695         nmp->nm_numgrps = NFS_MAXGRPS;
696         nmp->nm_readahead = NFS_DEFRAHEAD;
697         nmp->nm_leaseterm = NQ_DEFLEASE;
698         nmp->nm_deadthresh = NQ_DEADTHRESH;
699         CIRCLEQ_INIT(&nmp->nm_timerhead);
700         nmp->nm_inprog = NULLVP;
701         nmp->nm_fhsize = argp->fhsize;
702         bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
703         bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
704         bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
705         nmp->nm_nam = nam;
706
707         /*
708          * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
709          * no sense in that context.
710          */
711         if (argp->sotype == SOCK_STREAM)
712                 argp->flags &= ~NFSMNT_NOCONN;
713
714         if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
715                 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
716                 if (nmp->nm_timeo < NFS_MINTIMEO)
717                         nmp->nm_timeo = NFS_MINTIMEO;
718                 else if (nmp->nm_timeo > NFS_MAXTIMEO)
719                         nmp->nm_timeo = NFS_MAXTIMEO;
720         }
721
722         if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
723                 nmp->nm_retry = argp->retrans;
724                 if (nmp->nm_retry > NFS_MAXREXMIT)
725                         nmp->nm_retry = NFS_MAXREXMIT;
726         }
727
728         if (argp->flags & NFSMNT_NFSV3) {
729                 if (argp->sotype == SOCK_DGRAM)
730                         maxio = NFS_MAXDGRAMDATA;
731                 else
732                         maxio = NFS_MAXDATA;
733         } else
734                 maxio = NFS_V2MAXDATA;
735
736         if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
737                 nmp->nm_wsize = argp->wsize;
738                 /* Round down to multiple of blocksize */
739                 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
740                 if (nmp->nm_wsize <= 0)
741                         nmp->nm_wsize = NFS_FABLKSIZE;
742         }
743         if (nmp->nm_wsize > maxio)
744                 nmp->nm_wsize = maxio;
745         if (nmp->nm_wsize > MAXBSIZE)
746                 nmp->nm_wsize = MAXBSIZE;
747
748         if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
749                 nmp->nm_rsize = argp->rsize;
750                 /* Round down to multiple of blocksize */
751                 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
752                 if (nmp->nm_rsize <= 0)
753                         nmp->nm_rsize = NFS_FABLKSIZE;
754         }
755         if (nmp->nm_rsize > maxio)
756                 nmp->nm_rsize = maxio;
757         if (nmp->nm_rsize > MAXBSIZE)
758                 nmp->nm_rsize = MAXBSIZE;
759
760         if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
761                 nmp->nm_readdirsize = argp->readdirsize;
762         }
763         if (nmp->nm_readdirsize > maxio)
764                 nmp->nm_readdirsize = maxio;
765         if (nmp->nm_readdirsize > nmp->nm_rsize)
766                 nmp->nm_readdirsize = nmp->nm_rsize;
767
768         if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
769                 argp->maxgrouplist <= NFS_MAXGRPS)
770                 nmp->nm_numgrps = argp->maxgrouplist;
771         if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
772                 argp->readahead <= NFS_MAXRAHEAD)
773                 nmp->nm_readahead = argp->readahead;
774         if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
775                 argp->leaseterm <= NQ_MAXLEASE)
776                 nmp->nm_leaseterm = argp->leaseterm;
777         if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
778                 argp->deadthresh <= NQ_NEVERDEAD)
779                 nmp->nm_deadthresh = argp->deadthresh;
780         /* Set up the sockets and per-host congestion */
781         nmp->nm_sotype = argp->sotype;
782         nmp->nm_soproto = argp->proto;
783
784         /*
785          * For Connection based sockets (TCP,...) defer the connect until
786          * the first request, in case the server is not responding.
787          */
788         if (nmp->nm_sotype == SOCK_DGRAM &&
789                 (error = nfs_connect(nmp, (struct nfsreq *)0)))
790                 goto bad;
791
792         /*
793          * This is silly, but it has to be set so that vinifod() works.
794          * We do not want to do an nfs_statfs() here since we can get
795          * stuck on a dead server and we are holding a lock on the mount
796          * point.
797          */
798         mp->mnt_stat.f_iosize = nfs_iosize(nmp);
799         /*
800          * A reference count is needed on the nfsnode representing the
801          * remote root.  If this object is not persistent, then backward
802          * traversals of the mount point (i.e. "..") will not work if
803          * the nfsnode gets flushed out of the cache. Ufs does not have
804          * this problem, because one can identify root inodes by their
805          * number == ROOTINO (2).
806          */
807         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
808         if (error)
809                 goto bad;
810         *vpp = NFSTOV(np);
811
812         /*
813          * Get file attributes for the mountpoint.  This has the side
814          * effect of filling in (*vpp)->v_type with the correct value.
815          */
816         VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc);
817
818         /*
819          * Lose the lock but keep the ref.
820          */
821         VOP_UNLOCK(*vpp, 0, curproc);
822
823         return (0);
824 bad:
825         nfs_disconnect(nmp);
826         free((caddr_t)nmp, M_NFSMNT);
827         FREE(nam, M_SONAME);
828         return (error);
829 }
830
831 /*
832  * unmount system call
833  */
834 static int
835 nfs_unmount(mp, mntflags, p)
836         struct mount *mp;
837         int mntflags;
838         struct proc *p;
839 {
840         register struct nfsmount *nmp;
841         struct nfsnode *np;
842         struct vnode *vp;
843         int error, flags = 0;
844
845         if (mntflags & MNT_FORCE)
846                 flags |= FORCECLOSE;
847         nmp = VFSTONFS(mp);
848         /*
849          * Goes something like this..
850          * - Check for activity on the root vnode (other than ourselves).
851          * - Call vflush() to clear out vnodes for this file system,
852          *   except for the root vnode.
853          * - Decrement reference on the vnode representing remote root.
854          * - Close the socket
855          * - Free up the data structures
856          */
857         /*
858          * We need to decrement the ref. count on the nfsnode representing
859          * the remote root.  See comment in mountnfs().  The VFS unmount()
860          * has done vput on this vnode, otherwise we would get deadlock!
861          */
862         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
863         if (error)
864                 return(error);
865         vp = NFSTOV(np);
866         if (vp->v_usecount > 2) {
867                 vput(vp);
868                 return (EBUSY);
869         }
870
871         /*
872          * Must handshake with nqnfs_clientd() if it is active.
873          */
874         nmp->nm_flag |= NFSMNT_DISMINPROG;
875         while (nmp->nm_inprog != NULLVP)
876                 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
877         error = vflush(mp, vp, flags);
878         if (error) {
879                 vput(vp);
880                 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
881                 return (error);
882         }
883
884         /*
885          * We are now committed to the unmount.
886          * For NQNFS, let the server daemon free the nfsmount structure.
887          */
888         if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
889                 nmp->nm_flag |= NFSMNT_DISMNT;
890
891         /*
892          * There are two reference counts and one lock to get rid of here.
893          */
894         vput(vp);
895         vrele(vp);
896         vgone(vp);
897         nfs_disconnect(nmp);
898         FREE(nmp->nm_nam, M_SONAME);
899
900         if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
901                 free((caddr_t)nmp, M_NFSMNT);
902         return (0);
903 }
904
905 /*
906  * Return root of a filesystem
907  */
908 static int
909 nfs_root(mp, vpp)
910         struct mount *mp;
911         struct vnode **vpp;
912 {
913         register struct vnode *vp;
914         struct nfsmount *nmp;
915         struct nfsnode *np;
916         int error;
917
918         nmp = VFSTONFS(mp);
919         error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
920         if (error)
921                 return (error);
922         vp = NFSTOV(np);
923         if (vp->v_type == VNON)
924             vp->v_type = VDIR;
925         vp->v_flag = VROOT;
926         *vpp = vp;
927         return (0);
928 }
929
930 extern int syncprt;
931
932 /*
933  * Flush out the buffer cache
934  */
935 /* ARGSUSED */
936 static int
937 nfs_sync(mp, waitfor, cred, p)
938         struct mount *mp;
939         int waitfor;
940         struct ucred *cred;
941         struct proc *p;
942 {
943         register struct vnode *vp;
944         int error, allerror = 0;
945
946         /*
947          * Force stale buffer cache information to be flushed.
948          */
949 loop:
950         for (vp = mp->mnt_vnodelist.lh_first;
951              vp != NULL;
952              vp = vp->v_mntvnodes.le_next) {
953                 /*
954                  * If the vnode that we are about to sync is no longer
955                  * associated with this mount point, start over.
956                  */
957                 if (vp->v_mount != mp)
958                         goto loop;
959                 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL ||
960                     waitfor == MNT_LAZY)
961                         continue;
962                 if (vget(vp, LK_EXCLUSIVE, p))
963                         goto loop;
964                 error = VOP_FSYNC(vp, cred, waitfor, p);
965                 if (error)
966                         allerror = error;
967                 vput(vp);
968         }
969         return (allerror);
970 }
971
972 /*
973  * NFS flat namespace lookup.
974  * Currently unsupported.
975  */
976 /* ARGSUSED */
977 static int
978 nfs_vget(mp, ino, vpp)
979         struct mount *mp;
980         ino_t ino;
981         struct vnode **vpp;
982 {
983
984         return (EOPNOTSUPP);
985 }
986
987 /*
988  * At this point, this should never happen
989  */
990 /* ARGSUSED */
991 static int
992 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
993         register struct mount *mp;
994         struct fid *fhp;
995         struct sockaddr *nam;
996         struct vnode **vpp;
997         int *exflagsp;
998         struct ucred **credanonp;
999 {
1000
1001         return (EINVAL);
1002 }
1003
1004 /*
1005  * Vnode pointer to File handle, should never happen either
1006  */
1007 /* ARGSUSED */
1008 static int
1009 nfs_vptofh(vp, fhp)
1010         struct vnode *vp;
1011         struct fid *fhp;
1012 {
1013
1014         return (EINVAL);
1015 }
1016
1017 /*
1018  * Vfs start routine, a no-op.
1019  */
1020 /* ARGSUSED */
1021 static int
1022 nfs_start(mp, flags, p)
1023         struct mount *mp;
1024         int flags;
1025         struct proc *p;
1026 {
1027
1028         return (0);
1029 }
1030
1031 /*
1032  * Do operations associated with quotas, not supported
1033  */
1034 /* ARGSUSED */
1035 static int
1036 nfs_quotactl(mp, cmd, uid, arg, p)
1037         struct mount *mp;
1038         int cmd;
1039         uid_t uid;
1040         caddr_t arg;
1041         struct proc *p;
1042 {
1043
1044         return (EOPNOTSUPP);
1045 }