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