nfs_vfsops.c revision 1.8 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.8 1993/12/07 23:37:43 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 int s = splnet();
280
281 /* Don't touch the lock flags */
282 nmp->nm_flag = (argp->flags & ~(NFSMNT_LOCKBITS)) |
283 (nmp->nm_flag & NFSMNT_LOCKBITS);
284 splx(s);
285
286 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
287 nmp->nm_rto = argp->timeo;
288 /* NFS timeouts are specified in 1/10 sec. */
289 nmp->nm_rto = (nmp->nm_rto * 10) / NFS_HZ;
290 if (nmp->nm_rto < NFS_MINTIMEO)
291 nmp->nm_rto = NFS_MINTIMEO;
292 else if (nmp->nm_rto > NFS_MAXTIMEO)
293 nmp->nm_rto = NFS_MAXTIMEO;
294 nmp->nm_rttvar = nmp->nm_rto << 1;
295 }
296
297 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
298 nmp->nm_retry = argp->retrans;
299 if (nmp->nm_retry > NFS_MAXREXMIT)
300 nmp->nm_retry = NFS_MAXREXMIT;
301 }
302
303 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
304 nmp->nm_wsize = argp->wsize;
305 /* Round down to multiple of blocksize */
306 nmp->nm_wsize &= ~0x1ff;
307 if (nmp->nm_wsize <= 0)
308 nmp->nm_wsize = 512;
309 else if (nmp->nm_wsize > NFS_MAXDATA)
310 nmp->nm_wsize = NFS_MAXDATA;
311 }
312 if (nmp->nm_wsize > MAXBSIZE)
313 nmp->nm_wsize = MAXBSIZE;
314
315 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
316 nmp->nm_rsize = argp->rsize;
317 /* Round down to multiple of blocksize */
318 nmp->nm_rsize &= ~0x1ff;
319 if (nmp->nm_rsize <= 0)
320 nmp->nm_rsize = 512;
321 else if (nmp->nm_rsize > NFS_MAXDATA)
322 nmp->nm_rsize = NFS_MAXDATA;
323 }
324 if (nmp->nm_rsize > MAXBSIZE)
325 nmp->nm_rsize = MAXBSIZE;
326 }
327
328 /*
329 * VFS Operations.
330 *
331 * mount system call
332 * It seems a bit dumb to copyinstr() the host and path here and then
333 * bcopy() them in mountnfs(), but I wanted to detect errors before
334 * doing the sockargs() call because sockargs() allocates an mbuf and
335 * an error after that means that I have to release the mbuf.
336 */
337 /* ARGSUSED */
338 nfs_mount(mp, path, data, ndp, p)
339 struct mount *mp;
340 char *path;
341 caddr_t data;
342 struct nameidata *ndp;
343 struct proc *p;
344 {
345 int error;
346 struct nfs_args args;
347 struct mbuf *nam;
348 struct vnode *vp;
349 char pth[MNAMELEN], hst[MNAMELEN];
350 u_int len;
351 nfsv2fh_t nfh;
352
353 if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)))
354 return (error);
355 if (mp->mnt_flag & MNT_UPDATE) {
356 register struct nfsmount *nmp = VFSTONFS(mp);
357
358 if (nmp == NULL)
359 return EIO;
360 nfs_decode_flags(&args, nmp);
361 return (0);
362 }
363 if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t)))
364 return (error);
365 if (error = copyinstr(path, pth, MNAMELEN-1, &len))
366 return (error);
367 bzero(&pth[len], MNAMELEN - len);
368 if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len))
369 return (error);
370 bzero(&hst[len], MNAMELEN - len);
371 /* sockargs() call must be after above copyin() calls */
372 if (error = sockargs(&nam, (caddr_t)args.addr,
373 sizeof (struct sockaddr), MT_SONAME))
374 return (error);
375 args.fh = &nfh;
376 error = mountnfs(&args, mp, nam, pth, hst, &vp);
377 return (error);
378 }
379
380 /*
381 * Common code for mount and mountroot
382 */
383 mountnfs(argp, mp, nam, pth, hst, vpp)
384 register struct nfs_args *argp;
385 register struct mount *mp;
386 struct mbuf *nam;
387 char *pth, *hst;
388 struct vnode **vpp;
389 {
390 register struct nfsmount *nmp;
391 struct proc *p = curproc; /* XXX */
392 struct nfsnode *np;
393 int error;
394 fsid_t tfsid;
395
396 MALLOC(nmp, struct nfsmount *, sizeof *nmp, M_NFSMNT, M_WAITOK);
397 bzero((caddr_t)nmp, sizeof *nmp);
398 mp->mnt_data = (qaddr_t)nmp;
399
400 getnewfsid(mp, MOUNT_NFS);
401 nmp->nm_mountp = mp;
402 nmp->nm_rto = NFS_TIMEO;
403 nmp->nm_rtt = -1;
404 nmp->nm_rttvar = nmp->nm_rto << 1;
405 nmp->nm_retry = NFS_RETRANS;
406 nmp->nm_wsize = NFS_WSIZE;
407 nmp->nm_rsize = NFS_RSIZE;
408 bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t));
409 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
410 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
411 nmp->nm_nam = nam;
412 nfs_decode_flags(argp, nmp);
413
414 /* Set up the sockets and per-host congestion */
415 nmp->nm_sotype = argp->sotype;
416 nmp->nm_soproto = argp->proto;
417 if (error = nfs_connect(nmp))
418 goto bad;
419
420 if (error = nfs_statfs(mp, &mp->mnt_stat, p))
421 goto bad;
422 /*
423 * A reference count is needed on the nfsnode representing the
424 * remote root. If this object is not persistent, then backward
425 * traversals of the mount point (i.e. "..") will not work if
426 * the nfsnode gets flushed out of the cache. Ufs does not have
427 * this problem, because one can identify root inodes by their
428 * number == ROOTINO (2).
429 */
430 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
431 goto bad;
432 /*
433 * Unlock it, but keep the reference count.
434 */
435 nfs_unlock(NFSTOV(np));
436 *vpp = NFSTOV(np);
437
438 return (0);
439 bad:
440 nfs_disconnect(nmp);
441 FREE(nmp, M_NFSMNT);
442 m_freem(nam);
443 return (error);
444 }
445
446 /*
447 * unmount system call
448 */
449 nfs_unmount(mp, mntflags, p)
450 struct mount *mp;
451 int mntflags;
452 struct proc *p;
453 {
454 register struct nfsmount *nmp;
455 struct nfsnode *np;
456 struct vnode *vp;
457 int error, flags = 0;
458 extern int doforce;
459
460 if (mntflags & MNT_FORCE) {
461 if (!doforce || mp == rootfs)
462 return (EINVAL);
463 flags |= FORCECLOSE;
464 }
465 nmp = VFSTONFS(mp);
466 /*
467 * Clear out the buffer cache
468 */
469 mntflushbuf(mp, 0);
470 if (mntinvalbuf(mp))
471 return (EBUSY);
472 /*
473 * Goes something like this..
474 * - Check for activity on the root vnode (other than ourselves).
475 * - Call vflush() to clear out vnodes for this file system,
476 * except for the root vnode.
477 * - Decrement reference on the vnode representing remote root.
478 * - Close the socket
479 * - Free up the data structures
480 */
481 /*
482 * We need to decrement the ref. count on the nfsnode representing
483 * the remote root. See comment in mountnfs(). The VFS unmount()
484 * has done vput on this vnode, otherwise we would get deadlock!
485 */
486 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
487 return(error);
488 vp = NFSTOV(np);
489 if (vp->v_usecount > 2) {
490 vput(vp);
491 return (EBUSY);
492 }
493 if (error = vflush(mp, vp, flags)) {
494 vput(vp);
495 return (error);
496 }
497 /*
498 * Get rid of two reference counts, and unlock it on the second.
499 */
500 vrele(vp);
501 vput(vp);
502 nfs_disconnect(nmp);
503 m_freem(nmp->nm_nam);
504 free((caddr_t)nmp, M_NFSMNT);
505 return (0);
506 }
507
508 /*
509 * Return root of a filesystem
510 */
511 nfs_root(mp, vpp)
512 struct mount *mp;
513 struct vnode **vpp;
514 {
515 register struct vnode *vp;
516 struct nfsmount *nmp;
517 struct nfsnode *np;
518 int error;
519
520 nmp = VFSTONFS(mp);
521 if (error = nfs_nget(mp, &nmp->nm_fh, &np))
522 return (error);
523 vp = NFSTOV(np);
524 vp->v_type = VDIR;
525 vp->v_flag = VROOT;
526 *vpp = vp;
527 return (0);
528 }
529
530 extern int syncprt;
531
532 /*
533 * Flush out the buffer cache
534 */
535 /* ARGSUSED */
536 nfs_sync(mp, waitfor)
537 struct mount *mp;
538 int waitfor;
539 {
540 if (syncprt)
541 bufstats();
542 /*
543 * Force stale buffer cache information to be flushed.
544 */
545 mntflushbuf(mp, waitfor == MNT_WAIT ? B_SYNC : 0);
546 return (0);
547 }
548
549 /*
550 * At this point, this should never happen
551 */
552 /* ARGSUSED */
553 nfs_fhtovp(mp, fhp, vpp)
554 struct mount *mp;
555 struct fid *fhp;
556 struct vnode **vpp;
557 {
558
559 return (EINVAL);
560 }
561
562 /*
563 * Vnode pointer to File handle, should never happen either
564 */
565 /* ARGSUSED */
566 nfs_vptofh(vp, fhp)
567 struct vnode *vp;
568 struct fid *fhp;
569 {
570
571 return (EINVAL);
572 }
573
574 /*
575 * Vfs start routine, a no-op.
576 */
577 /* ARGSUSED */
578 nfs_start(mp, flags, p)
579 struct mount *mp;
580 int flags;
581 struct proc *p;
582 {
583
584 return (0);
585 }
586
587 /*
588 * Do operations associated with quotas, not supported
589 */
590 nfs_quotactl(mp, cmd, uid, arg, p)
591 struct mount *mp;
592 int cmd;
593 uid_t uid;
594 caddr_t arg;
595 struct proc *p;
596 {
597 #ifdef lint
598 mp = mp; cmd = cmd; uid = uid; arg = arg;
599 #endif /* lint */
600 return (EOPNOTSUPP);
601 }
602