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