nfs_vfsops.c revision 1.47 1 /* $NetBSD: nfs_vfsops.c,v 1.47 1996/05/23 22:47:29 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 /*
596 * When doing an update, we can't change from or to
597 * v3 and/or nqnfs.
598 */
599 args.flags = (args.flags & ~(NFSMNT_NFSV3|NFSMNT_NQNFS)) |
600 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_NQNFS));
601 nfs_decode_args(nmp, &args);
602 return (0);
603 }
604 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
605 if (error)
606 return (error);
607 error = copyinstr(path, pth, MNAMELEN-1, &len);
608 if (error)
609 return (error);
610 bzero(&pth[len], MNAMELEN - len);
611 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
612 if (error)
613 return (error);
614 bzero(&hst[len], MNAMELEN - len);
615 /* sockargs() call must be after above copyin() calls */
616 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
617 if (error)
618 return (error);
619 args.fh = nfh;
620 error = mountnfs(&args, mp, nam, pth, hst, &vp);
621 return (error);
622 }
623
624 /*
625 * Common code for mount and mountroot
626 */
627 int
628 mountnfs(argp, mp, nam, pth, hst, vpp)
629 register struct nfs_args *argp;
630 register struct mount *mp;
631 struct mbuf *nam;
632 char *pth, *hst;
633 struct vnode **vpp;
634 {
635 register struct nfsmount *nmp;
636 struct nfsnode *np;
637 int error;
638 struct vattr attrs;
639
640 if (mp->mnt_flag & MNT_UPDATE) {
641 nmp = VFSTONFS(mp);
642 /* update paths, file handles, etc, here XXX */
643 m_freem(nam);
644 return (0);
645 } else {
646 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
647 M_NFSMNT, M_WAITOK);
648 bzero((caddr_t)nmp, sizeof (struct nfsmount));
649 mp->mnt_data = (qaddr_t)nmp;
650 TAILQ_INIT(&nmp->nm_uidlruhead);
651 }
652 #ifdef Lite2_integrated
653 vfs_getnewfsid(mp, makefstype(MOUNT_NFS));
654 #else
655 getnewfsid(mp, makefstype(MOUNT_NFS));
656 #endif
657 nmp->nm_mountp = mp;
658 if (argp->flags & NFSMNT_NQNFS)
659 /*
660 * We have to set mnt_maxsymlink to a non-zero value so
661 * that COMPAT_43 routines will know that we are setting
662 * the d_type field in directories (and can zero it for
663 * unsuspecting binaries).
664 */
665 mp->mnt_maxsymlinklen = 1;
666 nmp->nm_timeo = NFS_TIMEO;
667 nmp->nm_retry = NFS_RETRANS;
668 nmp->nm_wsize = NFS_WSIZE;
669 nmp->nm_rsize = NFS_RSIZE;
670 nmp->nm_readdirsize = NFS_READDIRSIZE;
671 nmp->nm_numgrps = NFS_MAXGRPS;
672 nmp->nm_readahead = NFS_DEFRAHEAD;
673 nmp->nm_leaseterm = NQ_DEFLEASE;
674 nmp->nm_deadthresh = NQ_DEADTHRESH;
675 CIRCLEQ_INIT(&nmp->nm_timerhead);
676 nmp->nm_inprog = NULLVP;
677 nmp->nm_fhsize = argp->fhsize;
678 bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
679 #ifdef COMPAT_09
680 mp->mnt_stat.f_type = 2;
681 #else
682 mp->mnt_stat.f_type = 0;
683 #endif
684 strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
685 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
686 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
687 nmp->nm_nam = nam;
688 nfs_decode_args(nmp, argp);
689
690 /* Set up the sockets and per-host congestion */
691 nmp->nm_sotype = argp->sotype;
692 nmp->nm_soproto = argp->proto;
693
694 /*
695 * For Connection based sockets (TCP,...) defer the connect until
696 * the first request, in case the server is not responding.
697 */
698 if (nmp->nm_sotype == SOCK_DGRAM &&
699 (error = nfs_connect(nmp, (struct nfsreq *)0)))
700 goto bad;
701
702 /*
703 * This is silly, but it has to be set so that vinifod() works.
704 * We do not want to do an nfs_statfs() here since we can get
705 * stuck on a dead server and we are holding a lock on the mount
706 * point.
707 */
708 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
709 /*
710 * A reference count is needed on the nfsnode representing the
711 * remote root. If this object is not persistent, then backward
712 * traversals of the mount point (i.e. "..") will not work if
713 * the nfsnode gets flushed out of the cache. Ufs does not have
714 * this problem, because one can identify root inodes by their
715 * number == ROOTINO (2).
716 */
717 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
718 if (error)
719 goto bad;
720 *vpp = NFSTOV(np);
721 VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc); /* XXX */
722
723 return (0);
724 bad:
725 nfs_disconnect(nmp);
726 free((caddr_t)nmp, M_NFSMNT);
727 m_freem(nam);
728 return (error);
729 }
730
731 /*
732 * unmount system call
733 */
734 int
735 nfs_unmount(mp, mntflags, p)
736 struct mount *mp;
737 int mntflags;
738 struct proc *p;
739 {
740 register struct nfsmount *nmp;
741 struct nfsnode *np;
742 struct vnode *vp;
743 int error, flags = 0;
744
745 if (mntflags & MNT_FORCE)
746 flags |= FORCECLOSE;
747 nmp = VFSTONFS(mp);
748 /*
749 * Goes something like this..
750 * - Check for activity on the root vnode (other than ourselves).
751 * - Call vflush() to clear out vnodes for this file system,
752 * except for the root vnode.
753 * - Decrement reference on the vnode representing remote root.
754 * - Close the socket
755 * - Free up the data structures
756 */
757 /*
758 * We need to decrement the ref. count on the nfsnode representing
759 * the remote root. See comment in mountnfs(). The VFS unmount()
760 * has done vput on this vnode, otherwise we would get deadlock!
761 */
762 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
763 if (error)
764 return(error);
765 vp = NFSTOV(np);
766 if (vp->v_usecount > 2) {
767 vput(vp);
768 return (EBUSY);
769 }
770
771 /*
772 * Must handshake with nqnfs_clientd() if it is active.
773 */
774 nmp->nm_flag |= NFSMNT_DISMINPROG;
775 while (nmp->nm_inprog != NULLVP)
776 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
777 error = vflush(mp, vp, flags);
778 if (error) {
779 vput(vp);
780 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
781 return (error);
782 }
783
784 /*
785 * We are now committed to the unmount.
786 * For NQNFS, let the server daemon free the nfsmount structure.
787 */
788 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
789 nmp->nm_flag |= NFSMNT_DISMNT;
790
791 /*
792 * There are two reference counts to get rid of here.
793 */
794 vrele(vp);
795 vrele(vp);
796 vgone(vp);
797 nfs_disconnect(nmp);
798 m_freem(nmp->nm_nam);
799
800 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
801 free((caddr_t)nmp, M_NFSMNT);
802 return (0);
803 }
804
805 /*
806 * Return root of a filesystem
807 */
808 int
809 nfs_root(mp, vpp)
810 struct mount *mp;
811 struct vnode **vpp;
812 {
813 register struct vnode *vp;
814 struct nfsmount *nmp;
815 struct nfsnode *np;
816 int error;
817
818 nmp = VFSTONFS(mp);
819 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
820 if (error)
821 return (error);
822 vp = NFSTOV(np);
823 if (vp->v_type == VNON)
824 vp->v_type = VDIR;
825 vp->v_flag = VROOT;
826 *vpp = vp;
827 return (0);
828 }
829
830 extern int syncprt;
831
832 /*
833 * Flush out the buffer cache
834 */
835 /* ARGSUSED */
836 int
837 nfs_sync(mp, waitfor, cred, p)
838 struct mount *mp;
839 int waitfor;
840 struct ucred *cred;
841 struct proc *p;
842 {
843 register struct vnode *vp;
844 int error, allerror = 0;
845
846 /*
847 * Force stale buffer cache information to be flushed.
848 */
849 loop:
850 for (vp = mp->mnt_vnodelist.lh_first;
851 vp != NULL;
852 vp = vp->v_mntvnodes.le_next) {
853 /*
854 * If the vnode that we are about to sync is no longer
855 * associated with this mount point, start over.
856 */
857 if (vp->v_mount != mp)
858 goto loop;
859 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
860 continue;
861 #ifdef Lite2_integrated
862 if (vget(vp, LK_EXCLUSIVE, p))
863 #else
864 if (vget(vp, 1))
865 #endif
866 goto loop;
867 error = VOP_FSYNC(vp, cred, waitfor, p);
868 if (error)
869 allerror = error;
870 vput(vp);
871 }
872 return (allerror);
873 }
874
875 /*
876 * NFS flat namespace lookup.
877 * Currently unsupported.
878 */
879 /* ARGSUSED */
880 int
881 nfs_vget(mp, ino, vpp)
882 struct mount *mp;
883 ino_t ino;
884 struct vnode **vpp;
885 {
886
887 return (EOPNOTSUPP);
888 }
889
890 #ifdef notyet
891 /*
892 * Do that sysctl thang...
893 */
894 static int
895 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
896 size_t newlen, struct proc *p)
897 {
898 int rv;
899
900 /*
901 * All names at this level are terminal.
902 */
903 if(namelen > 1)
904 return ENOTDIR; /* overloaded */
905
906 switch(name[0]) {
907 case NFS_NFSSTATS:
908 if(!oldp) {
909 *oldlenp = sizeof nfsstats;
910 return 0;
911 }
912
913 if(*oldlenp < sizeof nfsstats) {
914 *oldlenp = sizeof nfsstats;
915 return ENOMEM;
916 }
917
918 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
919 if(rv) return rv;
920
921 if(newp && newlen != sizeof nfsstats)
922 return EINVAL;
923
924 if(newp) {
925 return copyin(newp, &nfsstats, sizeof nfsstats);
926 }
927 return 0;
928
929 default:
930 return EOPNOTSUPP;
931 }
932 }
933 #endif
934
935
936 /*
937 * At this point, this should never happen
938 */
939 /* ARGSUSED */
940 int
941 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
942 register struct mount *mp;
943 struct fid *fhp;
944 struct mbuf *nam;
945 struct vnode **vpp;
946 int *exflagsp;
947 struct ucred **credanonp;
948 {
949
950 return (EINVAL);
951 }
952
953 /*
954 * Vnode pointer to File handle, should never happen either
955 */
956 /* ARGSUSED */
957 int
958 nfs_vptofh(vp, fhp)
959 struct vnode *vp;
960 struct fid *fhp;
961 {
962
963 return (EINVAL);
964 }
965
966 /*
967 * Vfs start routine, a no-op.
968 */
969 /* ARGSUSED */
970 int
971 nfs_start(mp, flags, p)
972 struct mount *mp;
973 int flags;
974 struct proc *p;
975 {
976
977 return (0);
978 }
979
980 /*
981 * Do operations associated with quotas, not supported
982 */
983 /* ARGSUSED */
984 int
985 nfs_quotactl(mp, cmd, uid, arg, p)
986 struct mount *mp;
987 int cmd;
988 uid_t uid;
989 caddr_t arg;
990 struct proc *p;
991 {
992
993 return (EOPNOTSUPP);
994 }
995