nfs_vfsops.c revision 1.44 1 /* $NetBSD: nfs_vfsops.c,v 1.44 1996/03/13 00:38:55 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
39 */
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/ioctl.h>
44 #include <sys/signal.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/buf.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/systm.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/nfsnode.h>
63 #include <nfs/nfs.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 #include <nfs/nfs_var.h>
70
71 struct nfsstats nfsstats;
72 extern int nfs_ticks;
73
74 #ifdef notyet
75 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
76 struct proc *);
77 #endif
78
79 /*
80 * nfs vfs operations.
81 */
82 struct vfsops nfs_vfsops = {
83 MOUNT_NFS,
84 nfs_mount,
85 nfs_start,
86 nfs_unmount,
87 nfs_root,
88 nfs_quotactl,
89 nfs_statfs,
90 nfs_sync,
91 nfs_vget,
92 nfs_fhtovp,
93 nfs_vptofh,
94 nfs_init,
95 #ifdef notyet
96 nfs_sysctl
97 #endif
98 };
99
100 extern u_int32_t nfs_procids[NFS_NPROCS];
101 extern u_int32_t nfs_prog, nfs_vers;
102
103 static struct mount *
104 nfs_mount_diskless __P((struct nfs_dlmount *, char *, int, struct vnode **));
105
106 #define TRUE 1
107 #define FALSE 0
108
109 /*
110 * nfs statfs call
111 */
112 int
113 nfs_statfs(mp, sbp, p)
114 struct mount *mp;
115 register struct statfs *sbp;
116 struct proc *p;
117 {
118 register struct vnode *vp;
119 register struct nfs_statfs *sfp;
120 register caddr_t cp;
121 register u_int32_t *tl;
122 register int32_t t1, t2;
123 caddr_t bpos, dpos, cp2;
124 struct nfsmount *nmp = VFSTONFS(mp);
125 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
126 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
127 struct ucred *cred;
128 struct nfsnode *np;
129 u_quad_t tquad;
130
131 #ifndef nolint
132 sfp = (struct nfs_statfs *)0;
133 #endif
134 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
135 if (error)
136 return (error);
137 vp = NFSTOV(np);
138 cred = crget();
139 cred->cr_ngroups = 0;
140 if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
141 (void)nfs_fsinfo(nmp, vp, cred, p);
142 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
143 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
144 nfsm_fhtom(vp, v3);
145 nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
146 if (v3)
147 nfsm_postop_attr(vp, retattr);
148 if (!error)
149 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
150 #ifdef COMPAT_09
151 sbp->f_type = 2;
152 #else
153 sbp->f_type = 0;
154 #endif
155 sbp->f_flags = nmp->nm_flag;
156 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
157 if (v3) {
158 sbp->f_bsize = NFS_FABLKSIZE;
159 fxdr_hyper(&sfp->sf_tbytes, &tquad);
160 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
161 fxdr_hyper(&sfp->sf_fbytes, &tquad);
162 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
163 fxdr_hyper(&sfp->sf_abytes, &tquad);
164 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
165 sbp->f_files = (fxdr_unsigned(int32_t,
166 sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
167 sbp->f_ffree = (fxdr_unsigned(int32_t,
168 sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
169 } else {
170 sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
171 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
172 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
173 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
174 sbp->f_files = 0;
175 sbp->f_ffree = 0;
176 }
177 if (sbp != &mp->mnt_stat) {
178 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
179 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
180 }
181 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
182 nfsm_reqdone;
183 vrele(vp);
184 crfree(cred);
185 return (error);
186 }
187
188 /*
189 * nfs version 3 fsinfo rpc call
190 */
191 int
192 nfs_fsinfo(nmp, vp, cred, p)
193 register struct nfsmount *nmp;
194 register struct vnode *vp;
195 struct ucred *cred;
196 struct proc *p;
197 {
198 register struct nfsv3_fsinfo *fsp;
199 register caddr_t cp;
200 register int32_t t1, t2;
201 register u_int32_t *tl, pref, max;
202 caddr_t bpos, dpos, cp2;
203 int error = 0, retattr;
204 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
205
206 nfsstats.rpccnt[NFSPROC_FSINFO]++;
207 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
208 nfsm_fhtom(vp, 1);
209 nfsm_request(vp, NFSPROC_FSINFO, p, cred);
210 nfsm_postop_attr(vp, retattr);
211 if (!error) {
212 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
213 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
214 if (pref < nmp->nm_wsize)
215 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
216 ~(NFS_FABLKSIZE - 1);
217 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
218 if (max < nmp->nm_wsize) {
219 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
220 if (nmp->nm_wsize == 0)
221 nmp->nm_wsize = max;
222 }
223 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
224 if (pref < nmp->nm_rsize)
225 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
226 ~(NFS_FABLKSIZE - 1);
227 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
228 if (max < nmp->nm_rsize) {
229 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
230 if (nmp->nm_rsize == 0)
231 nmp->nm_rsize = max;
232 }
233 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
234 if (pref < nmp->nm_readdirsize)
235 nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
236 ~(NFS_DIRBLKSIZ - 1);
237 if (max < nmp->nm_readdirsize) {
238 nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
239 if (nmp->nm_readdirsize == 0)
240 nmp->nm_readdirsize = max;
241 }
242 nmp->nm_flag |= NFSMNT_GOTFSINFO;
243 }
244 nfsm_reqdone;
245 return (error);
246 }
247
248 /*
249 * Mount a remote root fs via. NFS. It goes like this:
250 * - Call nfs_boot_init() to fill in the nfs_diskless struct
251 * (using RARP, bootparam RPC, mountd RPC)
252 * - hand craft the swap nfs vnode hanging off a fake mount point
253 * if swdevt[0].sw_dev == NODEV
254 * - build the rootfs mount point and call mountnfs() to do the rest.
255 */
256 int
257 nfs_mountroot()
258 {
259 struct nfs_diskless nd;
260 struct vattr attr;
261 struct mount *mp;
262 struct vnode *vp;
263 struct proc *procp;
264 long n;
265 int error;
266
267 procp = curproc; /* XXX */
268
269 /*
270 * XXX time must be non-zero when we init the interface or else
271 * the arp code will wedge. [Fixed now in if_ether.c]
272 * However, the NFS attribute cache gives false "hits" when
273 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
274 */
275 if (time.tv_sec < NFS_MAXATTRTIMO)
276 time.tv_sec = NFS_MAXATTRTIMO;
277
278 /*
279 * Call nfs_boot_init() to fill in the nfs_diskless struct.
280 * Side effect: Finds and configures a network interface.
281 */
282 bzero((caddr_t) &nd, sizeof(nd));
283 nfs_boot_init(&nd, procp);
284
285 /*
286 * Create the root mount point.
287 */
288 nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root);
289 mp = nfs_mount_diskless(&nd.nd_root, "/", 0, &vp);
290 printf("root on %s\n", nd.nd_root.ndm_host);
291
292 /*
293 * Link it into the mount list.
294 */
295 #ifdef Lite2_integrated
296 simple_lock(&mountlist_slock);
297 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
298 simple_unlock(&mountlist_slock);
299 rootvp = vp;
300 vfs_unbusy(mp, procp);
301 #else
302 if (vfs_lock(mp))
303 panic("nfs_mountroot: vfs_lock");
304 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
305 mp->mnt_vnodecovered = NULLVP;
306 vfs_unlock(mp);
307 rootvp = vp;
308 #endif
309
310 /* Get root attributes (for the time). */
311 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
312 if (error) panic("nfs_mountroot: getattr for root");
313 n = attr.va_mtime.tv_sec;
314 #ifdef DEBUG
315 printf("root time: 0x%x\n", n);
316 #endif
317 inittodr(n);
318
319 /*
320 * XXX splnet, so networks will receive...
321 */
322 splnet();
323
324 #ifdef notyet
325 /* Set up swap credentials. */
326 proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
327 proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
328 if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
329 NGROUPS)
330 proc0.p_ucred->cr_ngroups = NGROUPS;
331 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
332 proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
333 #endif
334
335 /*
336 * "Mount" the swap device.
337 *
338 * On a "dataless" configuration (swap on disk) we will have:
339 * (swdevt[0].sw_dev != NODEV) identifying the swap device.
340 */
341 if (bdevvp(swapdev, &swapdev_vp))
342 panic("nfs_mountroot: can't setup swap vp");
343 if (swdevt[0].sw_dev != NODEV) {
344 printf("swap on device 0x%x\n", swdevt[0].sw_dev);
345 return (0);
346 }
347
348 /*
349 * If swapping to an nfs node: (swdevt[0].sw_dev == NODEV)
350 * Create a fake mount point just for the swap vnode so that the
351 * swap file can be on a different server from the rootfs.
352 */
353 nfs_boot_getfh(&nd.nd_boot, "swap", &nd.nd_swap);
354 mp = nfs_mount_diskless(&nd.nd_swap, "/swap", 0, &vp);
355 #ifdef Lite2_integrated
356 vfs_unbusy(mp, procp);
357 #endif
358 printf("swap on %s\n", nd.nd_swap.ndm_host);
359
360 /*
361 * Since the swap file is not the root dir of a file system,
362 * hack it to a regular file.
363 */
364 vp->v_type = VREG;
365 vp->v_flag = 0;
366 swdevt[0].sw_vp = vp;
367
368 /*
369 * Find out how large the swap file is.
370 */
371 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
372 if (error)
373 panic("nfs_mountroot: getattr for swap");
374 n = (long) (attr.va_size >> DEV_BSHIFT);
375 #ifdef DEBUG
376 printf("swap size: 0x%x (blocks)\n", n);
377 #endif
378 swdevt[0].sw_nblks = n;
379
380 return (0);
381 }
382
383 /*
384 * Internal version of mount system call for diskless setup.
385 */
386 static struct mount *
387 nfs_mount_diskless(ndmntp, mntname, mntflag, vpp)
388 struct nfs_dlmount *ndmntp;
389 char *mntname;
390 int mntflag;
391 struct vnode **vpp;
392 {
393 struct nfs_args args;
394 struct mount *mp;
395 struct mbuf *m;
396 int error;
397
398 #ifdef Lite2_integrated
399 vfs_rootmountalloc("nfs", mntname, &mp);
400 #else
401 /* Create the mount point. */
402 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
403 M_MOUNT, M_WAITOK);
404 if (mp == NULL)
405 panic("nfs_mountroot: malloc mount for %s", mntname);
406 bzero((char *)mp, (u_long)sizeof(struct mount));
407 #endif
408 mp->mnt_op = &nfs_vfsops;
409 mp->mnt_flag = mntflag;
410
411 /* Initialize mount args. */
412 bzero((caddr_t) &args, sizeof(args));
413 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
414 args.addrlen = args.addr->sa_len;
415 args.sotype = SOCK_DGRAM;
416 args.fh = ndmntp->ndm_fh;
417 args.fhsize = NFSX_V2FH;
418 args.hostname = ndmntp->ndm_host;
419 args.flags = NFSMNT_RESVPORT;
420
421 #ifdef NFS_BOOT_OPTIONS
422 args.flags |= NFS_BOOT_OPTIONS;
423 #endif
424 #ifdef NFS_BOOT_RWSIZE
425 /*
426 * Reduce rsize,wsize for interfaces that consistently
427 * drop fragments of long UDP messages. (i.e. wd8003).
428 * You can always change these later via remount.
429 */
430 args.flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
431 args.wsize = NFS_BOOT_RWSIZE;
432 args.rsize = NFS_BOOT_RWSIZE;
433 #endif
434
435 /* Get mbuf for server sockaddr. */
436 m = m_get(M_WAIT, MT_SONAME);
437 if (m == NULL)
438 panic("nfs_mountroot: mget soname for %s", mntname);
439 bcopy((caddr_t)args.addr, mtod(m, caddr_t),
440 (m->m_len = args.addr->sa_len));
441
442 error = mountnfs(&args, mp, m, mntname, args.hostname, vpp);
443 if (error)
444 panic("nfs_mountroot: mount %s failed: %d", mntname);
445
446 return (mp);
447 }
448
449 void
450 nfs_decode_args(nmp, argp)
451 struct nfsmount *nmp;
452 struct nfs_args *argp;
453 {
454 int s;
455 int adjsock;
456 int maxio;
457
458 s = splsoftnet();
459
460 /* Re-bind if rsrvd port requested and wasn't on one */
461 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
462 && (argp->flags & NFSMNT_RESVPORT);
463
464 /* Update flags atomically. Don't change the lock bits. */
465 nmp->nm_flag =
466 (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
467 splx(s);
468
469 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
470 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
471 if (nmp->nm_timeo < NFS_MINTIMEO)
472 nmp->nm_timeo = NFS_MINTIMEO;
473 else if (nmp->nm_timeo > NFS_MAXTIMEO)
474 nmp->nm_timeo = NFS_MAXTIMEO;
475 }
476
477 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
478 nmp->nm_retry = argp->retrans;
479 if (nmp->nm_retry > NFS_MAXREXMIT)
480 nmp->nm_retry = NFS_MAXREXMIT;
481 }
482
483 if (argp->flags & NFSMNT_NFSV3) {
484 if (argp->sotype == SOCK_DGRAM)
485 maxio = NFS_MAXDGRAMDATA;
486 else
487 maxio = NFS_MAXDATA;
488 } else
489 maxio = NFS_V2MAXDATA;
490
491 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
492 int osize = nmp->nm_wsize;
493 nmp->nm_wsize = argp->wsize;
494 /* Round down to multiple of blocksize */
495 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
496 if (nmp->nm_wsize <= 0)
497 nmp->nm_wsize = NFS_FABLKSIZE;
498 adjsock |= (nmp->nm_wsize != osize);
499 }
500 if (nmp->nm_wsize > maxio)
501 nmp->nm_wsize = maxio;
502 if (nmp->nm_wsize > MAXBSIZE)
503 nmp->nm_wsize = MAXBSIZE;
504
505 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
506 int osize = nmp->nm_rsize;
507 nmp->nm_rsize = argp->rsize;
508 /* Round down to multiple of blocksize */
509 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
510 if (nmp->nm_rsize <= 0)
511 nmp->nm_rsize = NFS_FABLKSIZE;
512 adjsock |= (nmp->nm_rsize != osize);
513 }
514 if (nmp->nm_rsize > maxio)
515 nmp->nm_rsize = maxio;
516 if (nmp->nm_rsize > MAXBSIZE)
517 nmp->nm_rsize = MAXBSIZE;
518
519 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
520 nmp->nm_readdirsize = argp->readdirsize;
521 /* Round down to multiple of blocksize */
522 nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
523 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
524 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
525 } else if (argp->flags & NFSMNT_RSIZE)
526 nmp->nm_readdirsize = nmp->nm_rsize;
527
528 if (nmp->nm_readdirsize > maxio)
529 nmp->nm_readdirsize = maxio;
530
531 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
532 argp->maxgrouplist <= NFS_MAXGRPS)
533 nmp->nm_numgrps = argp->maxgrouplist;
534 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
535 argp->readahead <= NFS_MAXRAHEAD)
536 nmp->nm_readahead = argp->readahead;
537 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
538 argp->leaseterm <= NQ_MAXLEASE)
539 nmp->nm_leaseterm = argp->leaseterm;
540 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
541 argp->deadthresh <= NQ_NEVERDEAD)
542 nmp->nm_deadthresh = argp->deadthresh;
543
544 if (nmp->nm_so && adjsock) {
545 nfs_disconnect(nmp);
546 if (nmp->nm_sotype == SOCK_DGRAM)
547 while (nfs_connect(nmp, (struct nfsreq *)0)) {
548 printf("nfs_args: retrying connect\n");
549 (void) tsleep((caddr_t)&lbolt,
550 PSOCK, "nfscon", 0);
551 }
552 }
553 }
554
555 /*
556 * VFS Operations.
557 *
558 * mount system call
559 * It seems a bit dumb to copyinstr() the host and path here and then
560 * bcopy() them in mountnfs(), but I wanted to detect errors before
561 * doing the sockargs() call because sockargs() allocates an mbuf and
562 * an error after that means that I have to release the mbuf.
563 */
564 /* ARGSUSED */
565 int
566 nfs_mount(mp, path, data, ndp, p)
567 struct mount *mp;
568 char *path;
569 caddr_t data;
570 struct nameidata *ndp;
571 struct proc *p;
572 {
573 int error;
574 struct nfs_args args;
575 struct mbuf *nam;
576 struct vnode *vp;
577 char pth[MNAMELEN], hst[MNAMELEN];
578 size_t len;
579 u_char nfh[NFSX_V3FHMAX];
580
581 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
582 if (error)
583 return (error);
584 if (args.version != NFS_ARGSVERSION)
585 return (EPROGMISMATCH);
586 if (mp->mnt_flag & MNT_UPDATE) {
587 register struct nfsmount *nmp = VFSTONFS(mp);
588
589 if (nmp == NULL)
590 return (EIO);
591 nfs_decode_args(nmp, &args);
592 return (0);
593 }
594 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
595 if (error)
596 return (error);
597 error = copyinstr(path, pth, MNAMELEN-1, &len);
598 if (error)
599 return (error);
600 bzero(&pth[len], MNAMELEN - len);
601 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
602 if (error)
603 return (error);
604 bzero(&hst[len], MNAMELEN - len);
605 /* sockargs() call must be after above copyin() calls */
606 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
607 if (error)
608 return (error);
609 args.fh = nfh;
610 error = mountnfs(&args, mp, nam, pth, hst, &vp);
611 return (error);
612 }
613
614 /*
615 * Common code for mount and mountroot
616 */
617 int
618 mountnfs(argp, mp, nam, pth, hst, vpp)
619 register struct nfs_args *argp;
620 register struct mount *mp;
621 struct mbuf *nam;
622 char *pth, *hst;
623 struct vnode **vpp;
624 {
625 register struct nfsmount *nmp;
626 struct nfsnode *np;
627 int error;
628
629 if (mp->mnt_flag & MNT_UPDATE) {
630 nmp = VFSTONFS(mp);
631 /* update paths, file handles, etc, here XXX */
632 m_freem(nam);
633 return (0);
634 } else {
635 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
636 M_NFSMNT, M_WAITOK);
637 bzero((caddr_t)nmp, sizeof (struct nfsmount));
638 mp->mnt_data = (qaddr_t)nmp;
639 TAILQ_INIT(&nmp->nm_uidlruhead);
640 }
641 #ifdef Lite2_integrated
642 vfs_getnewfsid(mp, makefstype(MOUNT_NFS));
643 #else
644 getnewfsid(mp, makefstype(MOUNT_NFS));
645 #endif
646 nmp->nm_mountp = mp;
647 if (argp->flags & NFSMNT_NQNFS)
648 /*
649 * We have to set mnt_maxsymlink to a non-zero value so
650 * that COMPAT_43 routines will know that we are setting
651 * the d_type field in directories (and can zero it for
652 * unsuspecting binaries).
653 */
654 mp->mnt_maxsymlinklen = 1;
655 nmp->nm_timeo = NFS_TIMEO;
656 nmp->nm_retry = NFS_RETRANS;
657 nmp->nm_wsize = NFS_WSIZE;
658 nmp->nm_rsize = NFS_RSIZE;
659 nmp->nm_readdirsize = NFS_READDIRSIZE;
660 nmp->nm_numgrps = NFS_MAXGRPS;
661 nmp->nm_readahead = NFS_DEFRAHEAD;
662 nmp->nm_leaseterm = NQ_DEFLEASE;
663 nmp->nm_deadthresh = NQ_DEADTHRESH;
664 CIRCLEQ_INIT(&nmp->nm_timerhead);
665 nmp->nm_inprog = NULLVP;
666 nmp->nm_fhsize = argp->fhsize;
667 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
668 #ifdef COMPAT_09
669 mp->mnt_stat.f_type = 2;
670 #else
671 mp->mnt_stat.f_type = 0;
672 #endif
673 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
674 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
675 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
676 nmp->nm_nam = nam;
677 nfs_decode_args(nmp, argp);
678
679 /* Set up the sockets and per-host congestion */
680 nmp->nm_sotype = argp->sotype;
681 nmp->nm_soproto = argp->proto;
682
683 /*
684 * For Connection based sockets (TCP,...) defer the connect until
685 * the first request, in case the server is not responding.
686 */
687 if (nmp->nm_sotype == SOCK_DGRAM &&
688 (error = nfs_connect(nmp, (struct nfsreq *)0)))
689 goto bad;
690
691 /*
692 * This is silly, but it has to be set so that vinifod() works.
693 * We do not want to do an nfs_statfs() here since we can get
694 * stuck on a dead server and we are holding a lock on the mount
695 * point.
696 */
697 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
698 /*
699 * A reference count is needed on the nfsnode representing the
700 * remote root. If this object is not persistent, then backward
701 * traversals of the mount point (i.e. "..") will not work if
702 * the nfsnode gets flushed out of the cache. Ufs does not have
703 * this problem, because one can identify root inodes by their
704 * number == ROOTINO (2).
705 */
706 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
707 if (error)
708 goto bad;
709 *vpp = NFSTOV(np);
710
711 return (0);
712 bad:
713 nfs_disconnect(nmp);
714 free((caddr_t)nmp, M_NFSMNT);
715 m_freem(nam);
716 return (error);
717 }
718
719 /*
720 * unmount system call
721 */
722 int
723 nfs_unmount(mp, mntflags, p)
724 struct mount *mp;
725 int mntflags;
726 struct proc *p;
727 {
728 register struct nfsmount *nmp;
729 struct nfsnode *np;
730 struct vnode *vp;
731 int error, flags = 0;
732
733 if (mntflags & MNT_FORCE)
734 flags |= FORCECLOSE;
735 nmp = VFSTONFS(mp);
736 /*
737 * Goes something like this..
738 * - Check for activity on the root vnode (other than ourselves).
739 * - Call vflush() to clear out vnodes for this file system,
740 * except for the root vnode.
741 * - Decrement reference on the vnode representing remote root.
742 * - Close the socket
743 * - Free up the data structures
744 */
745 /*
746 * We need to decrement the ref. count on the nfsnode representing
747 * the remote root. See comment in mountnfs(). The VFS unmount()
748 * has done vput on this vnode, otherwise we would get deadlock!
749 */
750 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
751 if (error)
752 return(error);
753 vp = NFSTOV(np);
754 if (vp->v_usecount > 2) {
755 vput(vp);
756 return (EBUSY);
757 }
758
759 /*
760 * Must handshake with nqnfs_clientd() if it is active.
761 */
762 nmp->nm_flag |= NFSMNT_DISMINPROG;
763 while (nmp->nm_inprog != NULLVP)
764 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
765 error = vflush(mp, vp, flags);
766 if (error) {
767 vput(vp);
768 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
769 return (error);
770 }
771
772 /*
773 * We are now committed to the unmount.
774 * For NQNFS, let the server daemon free the nfsmount structure.
775 */
776 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
777 nmp->nm_flag |= NFSMNT_DISMNT;
778
779 /*
780 * There are two reference counts to get rid of here.
781 */
782 vrele(vp);
783 vrele(vp);
784 vgone(vp);
785 nfs_disconnect(nmp);
786 m_freem(nmp->nm_nam);
787
788 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
789 free((caddr_t)nmp, M_NFSMNT);
790 return (0);
791 }
792
793 /*
794 * Return root of a filesystem
795 */
796 int
797 nfs_root(mp, vpp)
798 struct mount *mp;
799 struct vnode **vpp;
800 {
801 register struct vnode *vp;
802 struct nfsmount *nmp;
803 struct nfsnode *np;
804 int error;
805
806 nmp = VFSTONFS(mp);
807 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
808 if (error)
809 return (error);
810 vp = NFSTOV(np);
811 vp->v_type = VDIR;
812 vp->v_flag = VROOT;
813 *vpp = vp;
814 return (0);
815 }
816
817 extern int syncprt;
818
819 /*
820 * Flush out the buffer cache
821 */
822 /* ARGSUSED */
823 int
824 nfs_sync(mp, waitfor, cred, p)
825 struct mount *mp;
826 int waitfor;
827 struct ucred *cred;
828 struct proc *p;
829 {
830 register struct vnode *vp;
831 int error, allerror = 0;
832
833 /*
834 * Force stale buffer cache information to be flushed.
835 */
836 loop:
837 for (vp = mp->mnt_vnodelist.lh_first;
838 vp != NULL;
839 vp = vp->v_mntvnodes.le_next) {
840 /*
841 * If the vnode that we are about to sync is no longer
842 * associated with this mount point, start over.
843 */
844 if (vp->v_mount != mp)
845 goto loop;
846 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
847 continue;
848 #ifdef Lite2_integrated
849 if (vget(vp, LK_EXCLUSIVE, p))
850 #else
851 if (vget(vp, 1))
852 #endif
853 goto loop;
854 error = VOP_FSYNC(vp, cred, waitfor, p);
855 if (error)
856 allerror = error;
857 vput(vp);
858 }
859 return (allerror);
860 }
861
862 /*
863 * NFS flat namespace lookup.
864 * Currently unsupported.
865 */
866 /* ARGSUSED */
867 int
868 nfs_vget(mp, ino, vpp)
869 struct mount *mp;
870 ino_t ino;
871 struct vnode **vpp;
872 {
873
874 return (EOPNOTSUPP);
875 }
876
877 #ifdef notyet
878 /*
879 * Do that sysctl thang...
880 */
881 static int
882 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
883 size_t newlen, struct proc *p)
884 {
885 int rv;
886
887 /*
888 * All names at this level are terminal.
889 */
890 if(namelen > 1)
891 return ENOTDIR; /* overloaded */
892
893 switch(name[0]) {
894 case NFS_NFSSTATS:
895 if(!oldp) {
896 *oldlenp = sizeof nfsstats;
897 return 0;
898 }
899
900 if(*oldlenp < sizeof nfsstats) {
901 *oldlenp = sizeof nfsstats;
902 return ENOMEM;
903 }
904
905 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
906 if(rv) return rv;
907
908 if(newp && newlen != sizeof nfsstats)
909 return EINVAL;
910
911 if(newp) {
912 return copyin(newp, &nfsstats, sizeof nfsstats);
913 }
914 return 0;
915
916 default:
917 return EOPNOTSUPP;
918 }
919 }
920 #endif
921
922
923 /*
924 * At this point, this should never happen
925 */
926 /* ARGSUSED */
927 int
928 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
929 register struct mount *mp;
930 struct fid *fhp;
931 struct mbuf *nam;
932 struct vnode **vpp;
933 int *exflagsp;
934 struct ucred **credanonp;
935 {
936
937 return (EINVAL);
938 }
939
940 /*
941 * Vnode pointer to File handle, should never happen either
942 */
943 /* ARGSUSED */
944 int
945 nfs_vptofh(vp, fhp)
946 struct vnode *vp;
947 struct fid *fhp;
948 {
949
950 return (EINVAL);
951 }
952
953 /*
954 * Vfs start routine, a no-op.
955 */
956 /* ARGSUSED */
957 int
958 nfs_start(mp, flags, p)
959 struct mount *mp;
960 int flags;
961 struct proc *p;
962 {
963
964 return (0);
965 }
966
967 /*
968 * Do operations associated with quotas, not supported
969 */
970 /* ARGSUSED */
971 int
972 nfs_quotactl(mp, cmd, uid, arg, p)
973 struct mount *mp;
974 int cmd;
975 uid_t uid;
976 caddr_t arg;
977 struct proc *p;
978 {
979
980 return (EOPNOTSUPP);
981 }
982