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