nfs_vfsops.c revision 1.17 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Rick Macklem at The University of Guelph.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.2 cgd * from: @(#)nfs_vfsops.c 7.31 (Berkeley) 5/6/91
37 1.17 cgd * $Id: nfs_vfsops.c,v 1.17 1994/04/23 07:55:13 cgd Exp $
38 1.1 cgd */
39 1.1 cgd
40 1.9 mycroft #include <sys/param.h>
41 1.9 mycroft #include <sys/conf.h>
42 1.9 mycroft #include <sys/ioctl.h>
43 1.9 mycroft #include <sys/signal.h>
44 1.9 mycroft #include <sys/proc.h>
45 1.9 mycroft #include <sys/namei.h>
46 1.9 mycroft #include <sys/vnode.h>
47 1.9 mycroft #include <sys/mount.h>
48 1.9 mycroft #include <sys/buf.h>
49 1.9 mycroft #include <sys/mbuf.h>
50 1.9 mycroft #include <sys/socket.h>
51 1.9 mycroft #include <sys/systm.h>
52 1.1 cgd
53 1.9 mycroft #include <net/if.h>
54 1.9 mycroft #include <net/route.h>
55 1.1 cgd
56 1.9 mycroft #include <netinet/in.h>
57 1.9 mycroft
58 1.9 mycroft #include <nfs/nfsv2.h>
59 1.9 mycroft #include <nfs/nfsnode.h>
60 1.9 mycroft #include <nfs/nfsmount.h>
61 1.9 mycroft #include <nfs/nfs.h>
62 1.9 mycroft #include <nfs/xdr_subs.h>
63 1.9 mycroft #include <nfs/nfsm_subs.h>
64 1.9 mycroft #include <nfs/nfsdiskless.h>
65 1.1 cgd
66 1.1 cgd /*
67 1.1 cgd * nfs vfs operations.
68 1.1 cgd */
69 1.1 cgd struct vfsops nfs_vfsops = {
70 1.14 cgd MOUNT_NFS,
71 1.1 cgd nfs_mount,
72 1.1 cgd nfs_start,
73 1.1 cgd nfs_unmount,
74 1.1 cgd nfs_root,
75 1.1 cgd nfs_quotactl,
76 1.1 cgd nfs_statfs,
77 1.1 cgd nfs_sync,
78 1.1 cgd nfs_fhtovp,
79 1.1 cgd nfs_vptofh,
80 1.1 cgd nfs_init,
81 1.1 cgd };
82 1.1 cgd
83 1.1 cgd static u_char nfs_mntid;
84 1.1 cgd extern u_long nfs_procids[NFS_NPROCS];
85 1.1 cgd extern u_long nfs_prog, nfs_vers;
86 1.1 cgd void nfs_disconnect();
87 1.1 cgd
88 1.1 cgd #define TRUE 1
89 1.1 cgd #define FALSE 0
90 1.1 cgd
91 1.1 cgd /*
92 1.1 cgd * nfs statfs call
93 1.1 cgd */
94 1.1 cgd nfs_statfs(mp, sbp, p)
95 1.1 cgd struct mount *mp;
96 1.1 cgd register struct statfs *sbp;
97 1.1 cgd struct proc *p;
98 1.1 cgd {
99 1.1 cgd register struct vnode *vp;
100 1.1 cgd register struct nfsv2_statfs *sfp;
101 1.1 cgd register caddr_t cp;
102 1.1 cgd register long t1;
103 1.1 cgd caddr_t bpos, dpos, cp2;
104 1.1 cgd u_long xid;
105 1.1 cgd int error = 0;
106 1.1 cgd struct mbuf *mreq, *mrep, *md, *mb, *mb2;
107 1.1 cgd struct nfsmount *nmp;
108 1.1 cgd struct ucred *cred;
109 1.1 cgd struct nfsnode *np;
110 1.1 cgd
111 1.1 cgd nmp = VFSTONFS(mp);
112 1.1 cgd if (error = nfs_nget(mp, &nmp->nm_fh, &np))
113 1.1 cgd return (error);
114 1.1 cgd vp = NFSTOV(np);
115 1.1 cgd nfsstats.rpccnt[NFSPROC_STATFS]++;
116 1.1 cgd cred = crget();
117 1.1 cgd cred->cr_ngroups = 1;
118 1.1 cgd nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH);
119 1.1 cgd nfsm_fhtom(vp);
120 1.1 cgd nfsm_request(vp, NFSPROC_STATFS, p, 0);
121 1.1 cgd nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS);
122 1.14 cgd #ifdef COMPAT_09
123 1.14 cgd sbp->f_type = 2;
124 1.14 cgd #else
125 1.14 cgd sbp->f_type = 0;
126 1.14 cgd #endif
127 1.1 cgd sbp->f_flags = nmp->nm_flag;
128 1.16 cgd sbp->f_iosize = fxdr_unsigned(long, sfp->sf_tsize);
129 1.16 cgd sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
130 1.1 cgd sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
131 1.1 cgd sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
132 1.1 cgd sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
133 1.1 cgd sbp->f_files = 0;
134 1.1 cgd sbp->f_ffree = 0;
135 1.1 cgd if (sbp != &mp->mnt_stat) {
136 1.1 cgd bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
137 1.1 cgd bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
138 1.1 cgd }
139 1.14 cgd strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
140 1.14 cgd sbp->f_fstypename[MFSNAMELEN] = '\0';
141 1.1 cgd nfsm_reqdone;
142 1.1 cgd nfs_nput(vp);
143 1.1 cgd crfree(cred);
144 1.1 cgd return (error);
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd /*
148 1.15 glass * Mount a remote root fs via. nfs.
149 1.15 glass *
150 1.15 glass * Configure up an interface
151 1.15 glass * Initialize a nfs_diskless struct with:
152 1.15 glass * ip addr
153 1.15 glass * broadcast addr
154 1.15 glass * netmask
155 1.15 glass * as acquired and implied by RARP.
156 1.15 glass *
157 1.15 glass * Then call nfs_boot() to fill in the rest of the structure with enough
158 1.15 glass * information to mount root and swap.
159 1.1 cgd */
160 1.1 cgd nfs_mountroot()
161 1.1 cgd {
162 1.1 cgd register struct mount *mp;
163 1.1 cgd register struct mbuf *m;
164 1.1 cgd struct socket *so;
165 1.1 cgd struct vnode *vp;
166 1.15 glass struct ifnet *ifp;
167 1.15 glass struct sockaddr_in *sin;
168 1.15 glass struct in_addr myip;
169 1.15 glass struct ifreq ireq;
170 1.15 glass struct nfs_diskless diskless;
171 1.15 glass int error, len;
172 1.1 cgd
173 1.1 cgd /*
174 1.15 glass * Find an interface, rarp for its ip address, stuff it, the
175 1.15 glass * implied broadcast addr, and netmask into a nfs_diskless struct.
176 1.1 cgd */
177 1.15 glass
178 1.15 glass for (ifp = ifnet; ifp; ifp = ifp->if_next)
179 1.15 glass if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
180 1.15 glass break;
181 1.15 glass if (ifp == NULL)
182 1.15 glass return ENETUNREACH;
183 1.15 glass strcpy(ireq.ifr_name, ifp->if_name);
184 1.15 glass len = strlen(ireq.ifr_name);
185 1.15 glass ireq.ifr_name[len] = '0' + ifp->if_unit; /* XXX */
186 1.15 glass ireq.ifr_name[len+1] = '\0';
187 1.15 glass ireq.ifr_flags = IFF_UP;
188 1.15 glass if (error = ifioctl(NULL, SIOCSIFFLAGS, &ireq, curproc))
189 1.15 glass panic("nfs_mountroot, bringing interface %s up",
190 1.15 glass ireq.ifr_name);
191 1.15 glass bzero((caddr_t) &diskless, sizeof(diskless));
192 1.15 glass strcpy(diskless.myif.ifra_name, ireq.ifr_name);
193 1.15 glass if (revarp_whoami(&myip, ifp))
194 1.15 glass panic("revarp failed");
195 1.15 glass sin = (struct sockaddr_in *) &ireq.ifr_addr;
196 1.15 glass bzero((caddr_t) sin, sizeof(struct sockaddr_in));
197 1.15 glass sin->sin_len = sizeof(struct sockaddr_in);
198 1.15 glass sin->sin_family = AF_INET;
199 1.15 glass sin->sin_addr = myip;
200 1.15 glass
201 1.15 glass /*
202 1.15 glass * Do enough of ifconfig(8) so that the chosen interface can
203 1.15 glass * talk to the server(s).
204 1.15 glass */
205 1.15 glass if (socreate(AF_INET, &so, SOCK_DGRAM, 0))
206 1.1 cgd panic("nfs ifconf");
207 1.15 glass if (ifioctl(so, SIOCSIFADDR, &ireq, curproc))
208 1.15 glass panic("nfs_mountroot: setting interface address\n");
209 1.15 glass bcopy((caddr_t) sin, (caddr_t) &diskless.myif.ifra_addr,
210 1.15 glass sizeof(struct sockaddr));
211 1.15 glass if (ifioctl(so, SIOCGIFBRDADDR, &ireq, curproc))
212 1.15 glass panic("nfs baddr");
213 1.15 glass bcopy((caddr_t) &ireq.ifr_broadaddr,
214 1.15 glass (caddr_t) &diskless.myif.ifra_broadaddr,
215 1.15 glass sizeof (ireq.ifr_broadaddr));
216 1.15 glass if (ifioctl(so, SIOCGIFNETMASK, &ireq, curproc))
217 1.15 glass panic("nfs get netmask");
218 1.1 cgd soclose(so);
219 1.1 cgd
220 1.15 glass if (error = nfs_boot(&diskless))
221 1.15 glass return error;
222 1.15 glass
223 1.1 cgd /*
224 1.1 cgd * If the gateway field is filled in, set it as the default route.
225 1.1 cgd */
226 1.1 cgd #ifdef COMPAT_43
227 1.15 glass if (diskless.mygateway.sa_family == AF_INET) {
228 1.1 cgd struct ortentry rt;
229 1.1 cgd struct sockaddr_in *sin;
230 1.1 cgd
231 1.1 cgd sin = (struct sockaddr_in *) &rt.rt_dst;
232 1.1 cgd sin->sin_len = sizeof (struct sockaddr_in);
233 1.1 cgd sin->sin_family = AF_INET;
234 1.1 cgd sin->sin_addr.s_addr = 0; /* default */
235 1.15 glass bcopy((caddr_t)&diskless.mygateway, (caddr_t)&rt.rt_gateway,
236 1.1 cgd sizeof (struct sockaddr_in));
237 1.1 cgd rt.rt_flags = (RTF_UP | RTF_GATEWAY);
238 1.3 cgd if (rtioctl(SIOCADDRT, (caddr_t)&rt, curproc))
239 1.1 cgd panic("nfs root route");
240 1.1 cgd }
241 1.1 cgd #endif /* COMPAT_43 */
242 1.1 cgd
243 1.1 cgd /*
244 1.1 cgd * If swapping to an nfs node (indicated by swdevt[0].sw_dev == NODEV):
245 1.1 cgd * Create a fake mount point just for the swap vnode so that the
246 1.1 cgd * swap file can be on a different server from the rootfs.
247 1.1 cgd */
248 1.1 cgd if (swdevt[0].sw_dev == NODEV) {
249 1.1 cgd mp = (struct mount *)malloc((u_long)sizeof(struct mount),
250 1.1 cgd M_MOUNT, M_NOWAIT);
251 1.1 cgd if (mp == NULL)
252 1.1 cgd panic("nfs root mount");
253 1.1 cgd mp->mnt_op = &nfs_vfsops;
254 1.1 cgd mp->mnt_flag = 0;
255 1.1 cgd mp->mnt_exroot = 0;
256 1.16 cgd LIST_INIT(&mp->mnt_vnodelist);
257 1.1 cgd
258 1.1 cgd /*
259 1.1 cgd * Set up the diskless nfs_args for the swap mount point
260 1.1 cgd * and then call mountnfs() to mount it.
261 1.1 cgd * Since the swap file is not the root dir of a file system,
262 1.1 cgd * hack it to a regular file.
263 1.1 cgd */
264 1.15 glass diskless.swap_args.fh = (nfsv2fh_t *)diskless.swap_fh;
265 1.1 cgd MGET(m, MT_SONAME, M_DONTWAIT);
266 1.1 cgd if (m == NULL)
267 1.1 cgd panic("nfs root mbuf");
268 1.15 glass bcopy((caddr_t)&diskless.swap_saddr, mtod(m, caddr_t),
269 1.15 glass diskless.swap_saddr.sa_len);
270 1.15 glass m->m_len = diskless.swap_saddr.sa_len;
271 1.15 glass if (mountnfs(&diskless.swap_args, mp, m, "/swap",
272 1.15 glass diskless.swap_hostnam, &vp))
273 1.1 cgd panic("nfs swap");
274 1.1 cgd vp->v_type = VREG;
275 1.1 cgd vp->v_flag = 0;
276 1.1 cgd swapdev_vp = vp;
277 1.1 cgd VREF(vp);
278 1.1 cgd swdevt[0].sw_vp = vp;
279 1.3 cgd {
280 1.3 cgd struct vattr attr;
281 1.3 cgd
282 1.4 cgd if (nfs_dogetattr(vp,&attr,NOCRED,0,0)) {
283 1.12 glass panic("nfs swap getattr");
284 1.3 cgd }
285 1.3 cgd swdevt[0].sw_nblks = attr.va_size / DEV_BSIZE;
286 1.3 cgd }
287 1.1 cgd }
288 1.1 cgd
289 1.1 cgd /*
290 1.1 cgd * Create the rootfs mount point.
291 1.1 cgd */
292 1.1 cgd mp = (struct mount *)malloc((u_long)sizeof(struct mount),
293 1.1 cgd M_MOUNT, M_NOWAIT);
294 1.1 cgd if (mp == NULL)
295 1.1 cgd panic("nfs root mount2");
296 1.1 cgd mp->mnt_op = &nfs_vfsops;
297 1.1 cgd mp->mnt_flag = MNT_RDONLY;
298 1.1 cgd mp->mnt_exroot = 0;
299 1.16 cgd LIST_INIT(&mp->mnt_vnodelist);
300 1.1 cgd
301 1.1 cgd /*
302 1.1 cgd * Set up the root fs args and call mountnfs() to do the rest.
303 1.1 cgd */
304 1.15 glass diskless.root_args.fh = (nfsv2fh_t *)diskless.root_fh;
305 1.1 cgd MGET(m, MT_SONAME, M_DONTWAIT);
306 1.1 cgd if (m == NULL)
307 1.1 cgd panic("nfs root mbuf2");
308 1.15 glass bcopy((caddr_t)&diskless.root_saddr, mtod(m, caddr_t),
309 1.15 glass diskless.root_saddr.sa_len);
310 1.15 glass m->m_len = diskless.root_saddr.sa_len;
311 1.15 glass if (mountnfs(&diskless.root_args, mp, m, "/",
312 1.15 glass diskless.root_hostnam, &vp))
313 1.1 cgd panic("nfs root");
314 1.1 cgd if (vfs_lock(mp))
315 1.1 cgd panic("nfs root2");
316 1.1 cgd rootfs = mp;
317 1.16 cgd TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
318 1.1 cgd mp->mnt_vnodecovered = NULLVP;
319 1.1 cgd vfs_unlock(mp);
320 1.1 cgd rootvp = vp;
321 1.1 cgd inittodr((time_t)0); /* There is no time in the nfs fsstat so ?? */
322 1.1 cgd return (0);
323 1.1 cgd }
324 1.1 cgd
325 1.6 pk static void
326 1.6 pk nfs_decode_flags(argp, nmp)
327 1.6 pk struct nfs_args *argp;
328 1.6 pk struct nfsmount *nmp;
329 1.6 pk {
330 1.8 pk int s = splnet();
331 1.8 pk
332 1.7 pk /* Don't touch the lock flags */
333 1.7 pk nmp->nm_flag = (argp->flags & ~(NFSMNT_LOCKBITS)) |
334 1.7 pk (nmp->nm_flag & NFSMNT_LOCKBITS);
335 1.8 pk splx(s);
336 1.6 pk
337 1.6 pk if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
338 1.6 pk nmp->nm_rto = argp->timeo;
339 1.6 pk /* NFS timeouts are specified in 1/10 sec. */
340 1.6 pk nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ;
341 1.6 pk if (nmp->nm_rto < NFS_MINTIMEO)
342 1.6 pk nmp->nm_rto = NFS_MINTIMEO;
343 1.6 pk else if (nmp->nm_rto > NFS_MAXTIMEO)
344 1.6 pk nmp->nm_rto = NFS_MAXTIMEO;
345 1.6 pk nmp->nm_rttvar = nmp->nm_rto << 1;
346 1.6 pk }
347 1.6 pk
348 1.6 pk if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
349 1.6 pk nmp->nm_retry = argp->retrans;
350 1.6 pk if (nmp->nm_retry > NFS_MAXREXMIT)
351 1.6 pk nmp->nm_retry = NFS_MAXREXMIT;
352 1.6 pk }
353 1.6 pk
354 1.6 pk if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
355 1.6 pk nmp->nm_wsize = argp->wsize;
356 1.6 pk /* Round down to multiple of blocksize */
357 1.6 pk nmp->nm_wsize &= ~0x1ff;
358 1.6 pk if (nmp->nm_wsize <= 0)
359 1.6 pk nmp->nm_wsize = 512;
360 1.6 pk else if (nmp->nm_wsize > NFS_MAXDATA)
361 1.6 pk nmp->nm_wsize = NFS_MAXDATA;
362 1.6 pk }
363 1.6 pk if (nmp->nm_wsize > MAXBSIZE)
364 1.6 pk nmp->nm_wsize = MAXBSIZE;
365 1.6 pk
366 1.6 pk if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
367 1.6 pk nmp->nm_rsize = argp->rsize;
368 1.6 pk /* Round down to multiple of blocksize */
369 1.6 pk nmp->nm_rsize &= ~0x1ff;
370 1.6 pk if (nmp->nm_rsize <= 0)
371 1.6 pk nmp->nm_rsize = 512;
372 1.6 pk else if (nmp->nm_rsize > NFS_MAXDATA)
373 1.6 pk nmp->nm_rsize = NFS_MAXDATA;
374 1.6 pk }
375 1.6 pk if (nmp->nm_rsize > MAXBSIZE)
376 1.6 pk nmp->nm_rsize = MAXBSIZE;
377 1.6 pk }
378 1.6 pk
379 1.1 cgd /*
380 1.1 cgd * VFS Operations.
381 1.1 cgd *
382 1.1 cgd * mount system call
383 1.1 cgd * It seems a bit dumb to copyinstr() the host and path here and then
384 1.1 cgd * bcopy() them in mountnfs(), but I wanted to detect errors before
385 1.1 cgd * doing the sockargs() call because sockargs() allocates an mbuf and
386 1.1 cgd * an error after that means that I have to release the mbuf.
387 1.1 cgd */
388 1.1 cgd /* ARGSUSED */
389 1.1 cgd nfs_mount(mp, path, data, ndp, p)
390 1.1 cgd struct mount *mp;
391 1.1 cgd char *path;
392 1.1 cgd caddr_t data;
393 1.1 cgd struct nameidata *ndp;
394 1.1 cgd struct proc *p;
395 1.1 cgd {
396 1.1 cgd int error;
397 1.1 cgd struct nfs_args args;
398 1.1 cgd struct mbuf *nam;
399 1.1 cgd struct vnode *vp;
400 1.1 cgd char pth[MNAMELEN], hst[MNAMELEN];
401 1.1 cgd u_int len;
402 1.1 cgd nfsv2fh_t nfh;
403 1.1 cgd
404 1.1 cgd if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)))
405 1.1 cgd return (error);
406 1.6 pk if (mp->mnt_flag & MNT_UPDATE) {
407 1.6 pk register struct nfsmount *nmp = VFSTONFS(mp);
408 1.6 pk
409 1.6 pk if (nmp == NULL)
410 1.6 pk return EIO;
411 1.6 pk nfs_decode_flags(&args, nmp);
412 1.6 pk return (0);
413 1.6 pk }
414 1.1 cgd if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t)))
415 1.1 cgd return (error);
416 1.1 cgd if (error = copyinstr(path, pth, MNAMELEN-1, &len))
417 1.1 cgd return (error);
418 1.1 cgd bzero(&pth[len], MNAMELEN - len);
419 1.1 cgd if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len))
420 1.1 cgd return (error);
421 1.1 cgd bzero(&hst[len], MNAMELEN - len);
422 1.1 cgd /* sockargs() call must be after above copyin() calls */
423 1.1 cgd if (error = sockargs(&nam, (caddr_t)args.addr,
424 1.1 cgd sizeof (struct sockaddr), MT_SONAME))
425 1.1 cgd return (error);
426 1.1 cgd args.fh = &nfh;
427 1.1 cgd error = mountnfs(&args, mp, nam, pth, hst, &vp);
428 1.1 cgd return (error);
429 1.1 cgd }
430 1.1 cgd
431 1.1 cgd /*
432 1.1 cgd * Common code for mount and mountroot
433 1.1 cgd */
434 1.1 cgd mountnfs(argp, mp, nam, pth, hst, vpp)
435 1.1 cgd register struct nfs_args *argp;
436 1.1 cgd register struct mount *mp;
437 1.1 cgd struct mbuf *nam;
438 1.1 cgd char *pth, *hst;
439 1.1 cgd struct vnode **vpp;
440 1.1 cgd {
441 1.1 cgd register struct nfsmount *nmp;
442 1.1 cgd struct proc *p = curproc; /* XXX */
443 1.1 cgd struct nfsnode *np;
444 1.1 cgd int error;
445 1.1 cgd fsid_t tfsid;
446 1.1 cgd
447 1.1 cgd MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK);
448 1.1 cgd bzero((caddr_t)nmp, sizeof *nmp);
449 1.1 cgd mp->mnt_data = (qaddr_t)nmp;
450 1.5 cgd
451 1.17 cgd getnewfsid(mp, makefstype(MOUNT_NFS));
452 1.1 cgd nmp->nm_mountp = mp;
453 1.1 cgd nmp->nm_rto = NFS_TIMEO;
454 1.1 cgd nmp->nm_rtt = -1;
455 1.1 cgd nmp->nm_rttvar = nmp->nm_rto << 1;
456 1.1 cgd nmp->nm_retry = NFS_RETRANS;
457 1.1 cgd nmp->nm_wsize = NFS_WSIZE;
458 1.1 cgd nmp->nm_rsize = NFS_RSIZE;
459 1.1 cgd bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t));
460 1.1 cgd bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
461 1.1 cgd bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
462 1.1 cgd nmp->nm_nam = nam;
463 1.6 pk nfs_decode_flags(argp, nmp);
464 1.1 cgd
465 1.1 cgd /* Set up the sockets and per-host congestion */
466 1.1 cgd nmp->nm_sotype = argp->sotype;
467 1.1 cgd nmp->nm_soproto = argp->proto;
468 1.1 cgd if (error = nfs_connect(nmp))
469 1.1 cgd goto bad;
470 1.1 cgd
471 1.1 cgd if (error = nfs_statfs(mp, &mp->mnt_stat, p))
472 1.1 cgd goto bad;
473 1.1 cgd /*
474 1.1 cgd * A reference count is needed on the nfsnode representing the
475 1.1 cgd * remote root. If this object is not persistent, then backward
476 1.1 cgd * traversals of the mount point (i.e. "..") will not work if
477 1.1 cgd * the nfsnode gets flushed out of the cache. Ufs does not have
478 1.1 cgd * this problem, because one can identify root inodes by their
479 1.1 cgd * number == ROOTINO (2).
480 1.1 cgd */
481 1.1 cgd if (error = nfs_nget(mp, &nmp->nm_fh, &np))
482 1.1 cgd goto bad;
483 1.1 cgd /*
484 1.1 cgd * Unlock it, but keep the reference count.
485 1.1 cgd */
486 1.1 cgd nfs_unlock(NFSTOV(np));
487 1.1 cgd *vpp = NFSTOV(np);
488 1.1 cgd
489 1.1 cgd return (0);
490 1.1 cgd bad:
491 1.1 cgd nfs_disconnect(nmp);
492 1.1 cgd FREE(nmp, M_NFSMNT);
493 1.1 cgd m_freem(nam);
494 1.1 cgd return (error);
495 1.1 cgd }
496 1.1 cgd
497 1.1 cgd /*
498 1.1 cgd * unmount system call
499 1.1 cgd */
500 1.1 cgd nfs_unmount(mp, mntflags, p)
501 1.1 cgd struct mount *mp;
502 1.1 cgd int mntflags;
503 1.1 cgd struct proc *p;
504 1.1 cgd {
505 1.1 cgd register struct nfsmount *nmp;
506 1.1 cgd struct nfsnode *np;
507 1.1 cgd struct vnode *vp;
508 1.1 cgd int error, flags = 0;
509 1.1 cgd extern int doforce;
510 1.1 cgd
511 1.1 cgd if (mntflags & MNT_FORCE) {
512 1.1 cgd if (!doforce || mp == rootfs)
513 1.1 cgd return (EINVAL);
514 1.1 cgd flags |= FORCECLOSE;
515 1.1 cgd }
516 1.1 cgd nmp = VFSTONFS(mp);
517 1.1 cgd /*
518 1.1 cgd * Clear out the buffer cache
519 1.1 cgd */
520 1.1 cgd mntflushbuf(mp, 0);
521 1.1 cgd if (mntinvalbuf(mp))
522 1.1 cgd return (EBUSY);
523 1.1 cgd /*
524 1.1 cgd * Goes something like this..
525 1.1 cgd * - Check for activity on the root vnode (other than ourselves).
526 1.1 cgd * - Call vflush() to clear out vnodes for this file system,
527 1.1 cgd * except for the root vnode.
528 1.1 cgd * - Decrement reference on the vnode representing remote root.
529 1.1 cgd * - Close the socket
530 1.1 cgd * - Free up the data structures
531 1.1 cgd */
532 1.1 cgd /*
533 1.1 cgd * We need to decrement the ref. count on the nfsnode representing
534 1.1 cgd * the remote root. See comment in mountnfs(). The VFS unmount()
535 1.1 cgd * has done vput on this vnode, otherwise we would get deadlock!
536 1.1 cgd */
537 1.1 cgd if (error = nfs_nget(mp, &nmp->nm_fh, &np))
538 1.1 cgd return(error);
539 1.1 cgd vp = NFSTOV(np);
540 1.1 cgd if (vp->v_usecount > 2) {
541 1.1 cgd vput(vp);
542 1.1 cgd return (EBUSY);
543 1.1 cgd }
544 1.1 cgd if (error = vflush(mp, vp, flags)) {
545 1.1 cgd vput(vp);
546 1.1 cgd return (error);
547 1.1 cgd }
548 1.1 cgd /*
549 1.1 cgd * Get rid of two reference counts, and unlock it on the second.
550 1.1 cgd */
551 1.1 cgd vrele(vp);
552 1.1 cgd vput(vp);
553 1.13 cgd vgone(vp);
554 1.1 cgd nfs_disconnect(nmp);
555 1.1 cgd m_freem(nmp->nm_nam);
556 1.1 cgd free((caddr_t)nmp, M_NFSMNT);
557 1.1 cgd return (0);
558 1.1 cgd }
559 1.1 cgd
560 1.1 cgd /*
561 1.1 cgd * Return root of a filesystem
562 1.1 cgd */
563 1.1 cgd nfs_root(mp, vpp)
564 1.1 cgd struct mount *mp;
565 1.1 cgd struct vnode **vpp;
566 1.1 cgd {
567 1.1 cgd register struct vnode *vp;
568 1.1 cgd struct nfsmount *nmp;
569 1.1 cgd struct nfsnode *np;
570 1.1 cgd int error;
571 1.11 cgd struct vattr va;
572 1.11 cgd struct proc *p = curproc /* XXX */;
573 1.1 cgd
574 1.1 cgd nmp = VFSTONFS(mp);
575 1.1 cgd if (error = nfs_nget(mp, &nmp->nm_fh, &np))
576 1.1 cgd return (error);
577 1.1 cgd vp = NFSTOV(np);
578 1.10 cgd if (error = nfs_getattr(vp, &va, p->p_ucred, p))
579 1.10 cgd return (error);
580 1.10 cgd vp->v_type = va.va_type;
581 1.1 cgd vp->v_flag = VROOT;
582 1.1 cgd *vpp = vp;
583 1.1 cgd return (0);
584 1.1 cgd }
585 1.1 cgd
586 1.1 cgd extern int syncprt;
587 1.1 cgd
588 1.1 cgd /*
589 1.1 cgd * Flush out the buffer cache
590 1.1 cgd */
591 1.1 cgd /* ARGSUSED */
592 1.1 cgd nfs_sync(mp, waitfor)
593 1.1 cgd struct mount *mp;
594 1.1 cgd int waitfor;
595 1.1 cgd {
596 1.1 cgd if (syncprt)
597 1.1 cgd bufstats();
598 1.1 cgd /*
599 1.1 cgd * Force stale buffer cache information to be flushed.
600 1.1 cgd */
601 1.1 cgd mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0);
602 1.1 cgd return (0);
603 1.1 cgd }
604 1.1 cgd
605 1.1 cgd /*
606 1.1 cgd * At this point, this should never happen
607 1.1 cgd */
608 1.1 cgd /* ARGSUSED */
609 1.1 cgd nfs_fhtovp(mp, fhp, vpp)
610 1.1 cgd struct mount *mp;
611 1.1 cgd struct fid *fhp;
612 1.1 cgd struct vnode **vpp;
613 1.1 cgd {
614 1.1 cgd
615 1.1 cgd return (EINVAL);
616 1.1 cgd }
617 1.1 cgd
618 1.1 cgd /*
619 1.1 cgd * Vnode pointer to File handle, should never happen either
620 1.1 cgd */
621 1.1 cgd /* ARGSUSED */
622 1.1 cgd nfs_vptofh(vp, fhp)
623 1.1 cgd struct vnode *vp;
624 1.1 cgd struct fid *fhp;
625 1.1 cgd {
626 1.1 cgd
627 1.1 cgd return (EINVAL);
628 1.1 cgd }
629 1.1 cgd
630 1.1 cgd /*
631 1.1 cgd * Vfs start routine, a no-op.
632 1.1 cgd */
633 1.1 cgd /* ARGSUSED */
634 1.1 cgd nfs_start(mp, flags, p)
635 1.1 cgd struct mount *mp;
636 1.1 cgd int flags;
637 1.1 cgd struct proc *p;
638 1.1 cgd {
639 1.1 cgd
640 1.1 cgd return (0);
641 1.1 cgd }
642 1.1 cgd
643 1.1 cgd /*
644 1.1 cgd * Do operations associated with quotas, not supported
645 1.1 cgd */
646 1.1 cgd nfs_quotactl(mp, cmd, uid, arg, p)
647 1.1 cgd struct mount *mp;
648 1.1 cgd int cmd;
649 1.1 cgd uid_t uid;
650 1.1 cgd caddr_t arg;
651 1.1 cgd struct proc *p;
652 1.1 cgd {
653 1.1 cgd #ifdef lint
654 1.1 cgd mp = mp; cmd = cmd; uid = uid; arg = arg;
655 1.1 cgd #endif /* lint */
656 1.1 cgd return (EOPNOTSUPP);
657 1.1 cgd }
658