nfs_vfsops.c revision 1.6 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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 * from: @(#)nfs_vfsops.c 7.31 (Berkeley) 5/6/91
37 * $Id: nfs_vfsops.c,v 1.6 1993/12/06 23:38:14 pk Exp $
38 */
39
40 #include "param.h"
41 #include "conf.h"
42 #include "ioctl.h"
43 #include "signal.h"
44 #include "proc.h"
45 #include "namei.h"
46 #include "vnode.h"
47 #include "mount.h"
48 #include "buf.h"
49 #include "mbuf.h"
50 #include "socket.h"
51 #include "systm.h"
52
53 #include "../net/if.h"
54 #include "../net/route.h"
55 #include "../netinet/in.h"
56
57 #include "nfsv2.h"
58 #include "nfsnode.h"
59 #include "nfsmount.h"
60 #include "nfs.h"
61 #include "xdr_subs.h"
62 #include "nfsm_subs.h"
63 #include "nfsdiskless.h"
64
65 /*
66 * nfs vfs operations.
67 */
68 struct vfsops nfs_vfsops = {
69 nfs_mount,
70 nfs_start,
71 nfs_unmount,
72 nfs_root,
73 nfs_quotactl,
74 nfs_statfs,
75 nfs_sync,
76 nfs_fhtovp,
77 nfs_vptofh,
78 nfs_init,
79 };
80
81 static u_char nfs_mntid;
82 extern u_long nfs_procids[NFS_NPROCS];
83 extern u_long nfs_prog, nfs_vers;
84 struct nfs_diskless nfs_diskless;
85 void nfs_disconnect();
86
87 #define TRUE 1
88 #define FALSE 0
89
90 /*
91 * nfs statfs call
92 */
93 nfs_statfs(mp, sbp, p)
94 struct mount *mp;
95 register struct statfs *sbp;
96 struct proc *p;
97 {
98 register struct vnode *vp;
99 register struct nfsv2_statfs *sfp;
100 register caddr_t cp;
101 register long t1;
102 caddr_t bpos, dpos, cp2;
103 u_long xid;
104 int error = 0;
105 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
106 struct nfsmount *nmp;
107 struct ucred *cred;
108 struct nfsnode *np;
109
110 nmp = VFSTONFS(mp);
111 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
112 return (error);
113 vp = NFSTOV(np);
114 nfsstats.rpccnt[NFSPROC_STATFS]++;
115 cred = crget();
116 cred->cr_ngroups = 1;
117 nfsm_reqhead(nfs_procids[NFSPROC_STATFS], cred, NFSX_FH);
118 nfsm_fhtom(vp);
119 nfsm_request(vp, NFSPROC_STATFS, p, 0);
120 nfsm_disect(sfp, struct nfsv2_statfs *, NFSX_STATFS);
121 sbp->f_type = MOUNT_NFS;
122 sbp->f_flags = nmp->nm_flag;
123 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_tsize);
124 sbp->f_fsize = fxdr_unsigned(long, sfp->sf_bsize);
125 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
126 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
127 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
128 sbp->f_files = 0;
129 sbp->f_ffree = 0;
130 if (sbp != &mp->mnt_stat) {
131 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
132 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
133 }
134 nfsm_reqdone;
135 nfs_nput(vp);
136 crfree(cred);
137 return (error);
138 }
139
140 /*
141 * Mount a remote root fs via. nfs. This depends on the info in the
142 * nfs_diskless structure that has been filled in properly by some primary
143 * bootstrap.
144 * It goes something like this:
145 * - do enough of "ifconfig" by calling ifioctl() so that the system
146 * can talk to the server
147 * - If nfs_diskless.mygateway is filled in, use that address as
148 * a default gateway.
149 * (This is done the 4.3 way with rtioctl() and should be changed)
150 * - hand craft the swap nfs vnode hanging off a fake mount point
151 * - build the rootfs mount point and call mountnfs() to do the rest.
152 */
153 nfs_mountroot()
154 {
155 register struct mount *mp;
156 register struct mbuf *m;
157 struct socket *so;
158 struct vnode *vp;
159 int error;
160
161 /*
162 * Do enough of ifconfig(8) so that critical net interface can
163 * talk to the server.
164 */
165 if (socreate(nfs_diskless.myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0))
166 panic("nfs ifconf");
167 if (ifioctl(so, SIOCAIFADDR, &nfs_diskless.myif))
168 panic("nfs ifconf2");
169 soclose(so);
170
171 /*
172 * If the gateway field is filled in, set it as the default route.
173 */
174 #ifdef COMPAT_43
175 if (nfs_diskless.mygateway.sa_family == AF_INET) {
176 struct ortentry rt;
177 struct sockaddr_in *sin;
178
179 sin = (struct sockaddr_in *) &rt.rt_dst;
180 sin->sin_len = sizeof (struct sockaddr_in);
181 sin->sin_family = AF_INET;
182 sin->sin_addr.s_addr = 0; /* default */
183 bcopy((caddr_t)&nfs_diskless.mygateway, (caddr_t)&rt.rt_gateway,
184 sizeof (struct sockaddr_in));
185 rt.rt_flags = (RTF_UP | RTF_GATEWAY);
186 if (rtioctl(SIOCADDRT, (caddr_t)&rt, curproc))
187 panic("nfs root route");
188 }
189 #endif /* COMPAT_43 */
190
191 /*
192 * If swapping to an nfs node (indicated by swdevt[0].sw_dev == NODEV):
193 * Create a fake mount point just for the swap vnode so that the
194 * swap file can be on a different server from the rootfs.
195 */
196 if (swdevt[0].sw_dev == NODEV) {
197 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
198 M_MOUNT, M_NOWAIT);
199 if (mp == NULL)
200 panic("nfs root mount");
201 mp->mnt_op = &nfs_vfsops;
202 mp->mnt_flag = 0;
203 mp->mnt_exroot = 0;
204 mp->mnt_mounth = NULLVP;
205
206 /*
207 * Set up the diskless nfs_args for the swap mount point
208 * and then call mountnfs() to mount it.
209 * Since the swap file is not the root dir of a file system,
210 * hack it to a regular file.
211 */
212 nfs_diskless.swap_args.fh = (nfsv2fh_t *)nfs_diskless.swap_fh;
213 MGET(m, MT_SONAME, M_DONTWAIT);
214 if (m == NULL)
215 panic("nfs root mbuf");
216 bcopy((caddr_t)&nfs_diskless.swap_saddr, mtod(m, caddr_t),
217 nfs_diskless.swap_saddr.sa_len);
218 m->m_len = nfs_diskless.swap_saddr.sa_len;
219 if (mountnfs(&nfs_diskless.swap_args, mp, m, "/swap",
220 nfs_diskless.swap_hostnam, &vp))
221 panic("nfs swap");
222 vp->v_type = VREG;
223 vp->v_flag = 0;
224 swapdev_vp = vp;
225 VREF(vp);
226 swdevt[0].sw_vp = vp;
227 {
228 struct vattr attr;
229
230 if (nfs_dogetattr(vp,&attr,NOCRED,0,0)) {
231 panic("nfs swap");
232 }
233 swdevt[0].sw_nblks = attr.va_size / DEV_BSIZE;
234 }
235 }
236
237 /*
238 * Create the rootfs mount point.
239 */
240 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
241 M_MOUNT, M_NOWAIT);
242 if (mp == NULL)
243 panic("nfs root mount2");
244 mp->mnt_op = &nfs_vfsops;
245 mp->mnt_flag = MNT_RDONLY;
246 mp->mnt_exroot = 0;
247 mp->mnt_mounth = NULLVP;
248
249 /*
250 * Set up the root fs args and call mountnfs() to do the rest.
251 */
252 nfs_diskless.root_args.fh = (nfsv2fh_t *)nfs_diskless.root_fh;
253 MGET(m, MT_SONAME, M_DONTWAIT);
254 if (m == NULL)
255 panic("nfs root mbuf2");
256 bcopy((caddr_t)&nfs_diskless.root_saddr, mtod(m, caddr_t),
257 nfs_diskless.root_saddr.sa_len);
258 m->m_len = nfs_diskless.root_saddr.sa_len;
259 if (mountnfs(&nfs_diskless.root_args, mp, m, "/",
260 nfs_diskless.root_hostnam, &vp))
261 panic("nfs root");
262 if (vfs_lock(mp))
263 panic("nfs root2");
264 rootfs = mp;
265 mp->mnt_next = mp;
266 mp->mnt_prev = mp;
267 mp->mnt_vnodecovered = NULLVP;
268 vfs_unlock(mp);
269 rootvp = vp;
270 inittodr((time_t)0); /* There is no time in the nfs fsstat so ?? */
271 return (0);
272 }
273
274 static void
275 nfs_decode_flags(argp, nmp)
276 struct nfs_args *argp;
277 struct nfsmount *nmp;
278 {
279
280 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
281 nmp->nm_rto = argp->timeo;
282 /* NFS timeouts are specified in 1/10 sec. */
283 nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ;
284 if (nmp->nm_rto < NFS_MINTIMEO)
285 nmp->nm_rto = NFS_MINTIMEO;
286 else if (nmp->nm_rto > NFS_MAXTIMEO)
287 nmp->nm_rto = NFS_MAXTIMEO;
288 nmp->nm_rttvar = nmp->nm_rto << 1;
289 }
290
291 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
292 nmp->nm_retry = argp->retrans;
293 if (nmp->nm_retry > NFS_MAXREXMIT)
294 nmp->nm_retry = NFS_MAXREXMIT;
295 }
296
297 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
298 nmp->nm_wsize = argp->wsize;
299 /* Round down to multiple of blocksize */
300 nmp->nm_wsize &= ~0x1ff;
301 if (nmp->nm_wsize <= 0)
302 nmp->nm_wsize = 512;
303 else if (nmp->nm_wsize > NFS_MAXDATA)
304 nmp->nm_wsize = NFS_MAXDATA;
305 }
306 if (nmp->nm_wsize > MAXBSIZE)
307 nmp->nm_wsize = MAXBSIZE;
308
309 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
310 nmp->nm_rsize = argp->rsize;
311 /* Round down to multiple of blocksize */
312 nmp->nm_rsize &= ~0x1ff;
313 if (nmp->nm_rsize <= 0)
314 nmp->nm_rsize = 512;
315 else if (nmp->nm_rsize > NFS_MAXDATA)
316 nmp->nm_rsize = NFS_MAXDATA;
317 }
318 if (nmp->nm_rsize > MAXBSIZE)
319 nmp->nm_rsize = MAXBSIZE;
320 }
321
322 /*
323 * VFS Operations.
324 *
325 * mount system call
326 * It seems a bit dumb to copyinstr() the host and path here and then
327 * bcopy() them in mountnfs(), but I wanted to detect errors before
328 * doing the sockargs() call because sockargs() allocates an mbuf and
329 * an error after that means that I have to release the mbuf.
330 */
331 /* ARGSUSED */
332 nfs_mount(mp, path, data, ndp, p)
333 struct mount *mp;
334 char *path;
335 caddr_t data;
336 struct nameidata *ndp;
337 struct proc *p;
338 {
339 int error;
340 struct nfs_args args;
341 struct mbuf *nam;
342 struct vnode *vp;
343 char pth[MNAMELEN], hst[MNAMELEN];
344 u_int len;
345 nfsv2fh_t nfh;
346
347 if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)))
348 return (error);
349 if (mp->mnt_flag & MNT_UPDATE) {
350 register struct nfsmount *nmp = VFSTONFS(mp);
351
352 if (nmp == NULL)
353 return EIO;
354 nfs_decode_flags(&args, nmp);
355 return (0);
356 }
357 if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t)))
358 return (error);
359 if (error = copyinstr(path, pth, MNAMELEN-1, &len))
360 return (error);
361 bzero(&pth[len], MNAMELEN - len);
362 if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len))
363 return (error);
364 bzero(&hst[len], MNAMELEN - len);
365 /* sockargs() call must be after above copyin() calls */
366 if (error = sockargs(&nam, (caddr_t)args.addr,
367 sizeof (struct sockaddr), MT_SONAME))
368 return (error);
369 args.fh = &nfh;
370 error = mountnfs(&args, mp, nam, pth, hst, &vp);
371 return (error);
372 }
373
374 /*
375 * Common code for mount and mountroot
376 */
377 mountnfs(argp, mp, nam, pth, hst, vpp)
378 register struct nfs_args *argp;
379 register struct mount *mp;
380 struct mbuf *nam;
381 char *pth, *hst;
382 struct vnode **vpp;
383 {
384 register struct nfsmount *nmp;
385 struct proc *p = curproc; /* XXX */
386 struct nfsnode *np;
387 int error;
388 fsid_t tfsid;
389
390 MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK);
391 bzero((caddr_t)nmp, sizeof *nmp);
392 mp->mnt_data = (qaddr_t)nmp;
393
394 getnewfsid(mp, MOUNT_NFS);
395 nmp->nm_mountp = mp;
396 nmp->nm_flag = argp->flags;
397 nmp->nm_rto = NFS_TIMEO;
398 nmp->nm_rtt = -1;
399 nmp->nm_rttvar = nmp->nm_rto << 1;
400 nmp->nm_retry = NFS_RETRANS;
401 nmp->nm_wsize = NFS_WSIZE;
402 nmp->nm_rsize = NFS_RSIZE;
403 bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t));
404 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
405 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
406 nmp->nm_nam = nam;
407
408 nfs_decode_flags(argp, nmp);
409
410 /* Set up the sockets and per-host congestion */
411 nmp->nm_sotype = argp->sotype;
412 nmp->nm_soproto = argp->proto;
413 if (error = nfs_connect(nmp))
414 goto bad;
415
416 if (error = nfs_statfs(mp, &mp->mnt_stat, p))
417 goto bad;
418 /*
419 * A reference count is needed on the nfsnode representing the
420 * remote root. If this object is not persistent, then backward
421 * traversals of the mount point (i.e. "..") will not work if
422 * the nfsnode gets flushed out of the cache. Ufs does not have
423 * this problem, because one can identify root inodes by their
424 * number == ROOTINO (2).
425 */
426 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
427 goto bad;
428 /*
429 * Unlock it, but keep the reference count.
430 */
431 nfs_unlock(NFSTOV(np));
432 *vpp = NFSTOV(np);
433
434 return (0);
435 bad:
436 nfs_disconnect(nmp);
437 FREE(nmp, M_NFSMNT);
438 m_freem(nam);
439 return (error);
440 }
441
442 /*
443 * unmount system call
444 */
445 nfs_unmount(mp, mntflags, p)
446 struct mount *mp;
447 int mntflags;
448 struct proc *p;
449 {
450 register struct nfsmount *nmp;
451 struct nfsnode *np;
452 struct vnode *vp;
453 int error, flags = 0;
454 extern int doforce;
455
456 if (mntflags & MNT_FORCE) {
457 if (!doforce || mp == rootfs)
458 return (EINVAL);
459 flags |= FORCECLOSE;
460 }
461 nmp = VFSTONFS(mp);
462 /*
463 * Clear out the buffer cache
464 */
465 mntflushbuf(mp, 0);
466 if (mntinvalbuf(mp))
467 return (EBUSY);
468 /*
469 * Goes something like this..
470 * - Check for activity on the root vnode (other than ourselves).
471 * - Call vflush() to clear out vnodes for this file system,
472 * except for the root vnode.
473 * - Decrement reference on the vnode representing remote root.
474 * - Close the socket
475 * - Free up the data structures
476 */
477 /*
478 * We need to decrement the ref. count on the nfsnode representing
479 * the remote root. See comment in mountnfs(). The VFS unmount()
480 * has done vput on this vnode, otherwise we would get deadlock!
481 */
482 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
483 return(error);
484 vp = NFSTOV(np);
485 if (vp->v_usecount > 2) {
486 vput(vp);
487 return (EBUSY);
488 }
489 if (error = vflush(mp, vp, flags)) {
490 vput(vp);
491 return (error);
492 }
493 /*
494 * Get rid of two reference counts, and unlock it on the second.
495 */
496 vrele(vp);
497 vput(vp);
498 nfs_disconnect(nmp);
499 m_freem(nmp->nm_nam);
500 free((caddr_t)nmp, M_NFSMNT);
501 return (0);
502 }
503
504 /*
505 * Return root of a filesystem
506 */
507 nfs_root(mp, vpp)
508 struct mount *mp;
509 struct vnode **vpp;
510 {
511 register struct vnode *vp;
512 struct nfsmount *nmp;
513 struct nfsnode *np;
514 int error;
515
516 nmp = VFSTONFS(mp);
517 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
518 return (error);
519 vp = NFSTOV(np);
520 vp->v_type = VDIR;
521 vp->v_flag = VROOT;
522 *vpp = vp;
523 return (0);
524 }
525
526 extern int syncprt;
527
528 /*
529 * Flush out the buffer cache
530 */
531 /* ARGSUSED */
532 nfs_sync(mp, waitfor)
533 struct mount *mp;
534 int waitfor;
535 {
536 if (syncprt)
537 bufstats();
538 /*
539 * Force stale buffer cache information to be flushed.
540 */
541 mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0);
542 return (0);
543 }
544
545 /*
546 * At this point, this should never happen
547 */
548 /* ARGSUSED */
549 nfs_fhtovp(mp, fhp, vpp)
550 struct mount *mp;
551 struct fid *fhp;
552 struct vnode **vpp;
553 {
554
555 return (EINVAL);
556 }
557
558 /*
559 * Vnode pointer to File handle, should never happen either
560 */
561 /* ARGSUSED */
562 nfs_vptofh(vp, fhp)
563 struct vnode *vp;
564 struct fid *fhp;
565 {
566
567 return (EINVAL);
568 }
569
570 /*
571 * Vfs start routine, a no-op.
572 */
573 /* ARGSUSED */
574 nfs_start(mp, flags, p)
575 struct mount *mp;
576 int flags;
577 struct proc *p;
578 {
579
580 return (0);
581 }
582
583 /*
584 * Do operations associated with quotas, not supported
585 */
586 nfs_quotactl(mp, cmd, uid, arg, p)
587 struct mount *mp;
588 int cmd;
589 uid_t uid;
590 caddr_t arg;
591 struct proc *p;
592 {
593 #ifdef lint
594 mp = mp; cmd = cmd; uid = uid; arg = arg;
595 #endif /* lint */
596 return (EOPNOTSUPP);
597 }
598