nfs_vfsops.c revision 1.61 1 /* $NetBSD: nfs_vfsops.c,v 1.61 1997/07/17 23:54:31 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/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 bzero((caddr_t) &nd, sizeof(nd));
296 error = nfs_boot_init(&nd, procp);
297 if (error)
298 return (error);
299
300 /*
301 * Create the root mount point.
302 */
303 error = nfs_boot_getfh(&nd.nd_root);
304 if (error)
305 return (error);
306 error = nfs_mount_diskless(&nd.nd_root, "/", &mp, &vp);
307 if (error)
308 return (error);
309 printf("root on %s\n", nd.nd_root.ndm_host);
310
311 /*
312 * Link it into the mount list.
313 */
314 #ifdef Lite2_integrated
315 simple_lock(&mountlist_slock);
316 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
317 simple_unlock(&mountlist_slock);
318 rootvp = vp;
319 vfs_unbusy(mp, procp);
320 #else
321 if (vfs_lock(mp))
322 panic("nfs_mountroot: vfs_lock");
323 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
324 mp->mnt_vnodecovered = NULLVP;
325 vfs_unlock(mp);
326 rootvp = vp;
327 #endif
328
329 /* Get root attributes (for the time). */
330 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
331 if (error)
332 panic("nfs_mountroot: getattr for root");
333 n = attr.va_mtime.tv_sec;
334 #ifdef DEBUG
335 printf("root time: 0x%lx\n", n);
336 #endif
337 inittodr(n);
338
339 #if 0
340 /*
341 * XXX splnet, so networks will receive...
342 * XXX What system needed this hack?
343 * XXX Should already be at spl0.
344 */
345 splnet();
346 #endif
347
348 #ifdef notyet
349 /* Set up swap credentials. */
350 proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
351 proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
352 if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
353 NGROUPS)
354 proc0.p_ucred->cr_ngroups = NGROUPS;
355 for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
356 proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
357 #endif
358
359 /* swap from outside now */
360 #if 0
361 * "Mount" the swap device.
362 *
363 * On a "dataless" configuration (swap on disk) we will have:
364 * (swdevt[0].sw_dev != NODEV) identifying the swap device.
365 */
366 if (bdevvp(swapdev, &swapdev_vp))
367 panic("nfs_mountroot: can't setup swap vp");
368 if (swdevt[0].sw_dev != NODEV) {
369 printf("swap on device 0x%x\n", swdevt[0].sw_dev);
370 return (0);
371 }
372
373 /*
374 * If swapping to an nfs node: (swdevt[0].sw_dev == NODEV)
375 * Create a fake mount point just for the swap vnode so that the
376 * swap file can be on a different server from the rootfs.
377 */
378 if ((error = nfs_boot_getfh(&nd.nd_swap)) != 0) {
379 printf("nfs_boot: warning: getfh(swap), error=%d\n", error);
380 return (0);
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 return (0);
386 }
387 #ifdef Lite2_integrated
388 vfs_unbusy(mp, procp);
389 #endif
390 printf("swap on %s\n", nd.nd_swap.ndm_host);
391
392 /*
393 * Since the swap file is not the root dir of a file system,
394 * hack it to a regular file.
395 */
396 vp->v_type = VREG;
397 vp->v_flag = 0;
398 swdevt[0].sw_vp = vp;
399
400 /*
401 * Find out how large the swap file is.
402 */
403 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
404 if (error)
405 panic("nfs_mountroot: getattr for swap");
406 n = (long) (attr.va_size >> DEV_BSHIFT);
407 #ifdef DEBUG
408 printf("swap size: 0x%lx (blocks)\n", n);
409 #endif
410 swdevt[0].sw_nblks = n;
411 #endif
412
413 return (0);
414 }
415
416 /*
417 * Internal version of mount system call for diskless setup.
418 */
419 static int
420 nfs_mount_diskless(ndmntp, mntname, mpp, vpp)
421 struct nfs_dlmount *ndmntp;
422 const char *mntname; /* mount point name */
423 struct mount **mpp;
424 struct vnode **vpp;
425 {
426 struct mount *mp;
427 struct mbuf *m;
428 int error;
429
430 #ifdef Lite2_integrated
431 vfs_rootmountalloc("nfs", mntname, &mp);
432 #else
433 /* Create the mount point. */
434 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
435 M_MOUNT, M_WAITOK);
436 bzero((char *)mp, (u_long)sizeof(struct mount));
437 #endif
438 mp->mnt_op = &nfs_vfsops;
439 /* mp->mnt_flag = 0 */
440
441 /* Get mbuf for server sockaddr. */
442 m = m_get(M_WAIT, MT_SONAME);
443 if (m == NULL)
444 panic("nfs_mountroot: mget soname for %s", mntname);
445 bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t),
446 (m->m_len = ndmntp->ndm_args.addr->sa_len));
447
448 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
449 ndmntp->ndm_args.hostname, vpp);
450 if (error) {
451 printf("nfs_mountroot: mount %s failed: %d\n",
452 mntname, error);
453 free(mp, M_MOUNT);
454 } else
455 *mpp = mp;
456
457 return (error);
458 }
459
460 void
461 nfs_decode_args(nmp, argp)
462 struct nfsmount *nmp;
463 struct nfs_args *argp;
464 {
465 int s;
466 int adjsock;
467 int maxio;
468
469 s = splsoftnet();
470
471 /*
472 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
473 * no sense in that context.
474 */
475 if (argp->sotype == SOCK_STREAM)
476 argp->flags &= ~NFSMNT_NOCONN;
477
478 /* Re-bind if rsrvd port requested and wasn't on one */
479 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
480 && (argp->flags & NFSMNT_RESVPORT);
481 /* Also re-bind if we're switching to/from a connected UDP socket */
482 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
483 (argp->flags & NFSMNT_NOCONN));
484
485 /* Update flags atomically. Don't change the lock bits. */
486 nmp->nm_flag =
487 (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
488 splx(s);
489
490 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
491 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
492 if (nmp->nm_timeo < NFS_MINTIMEO)
493 nmp->nm_timeo = NFS_MINTIMEO;
494 else if (nmp->nm_timeo > NFS_MAXTIMEO)
495 nmp->nm_timeo = NFS_MAXTIMEO;
496 }
497
498 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
499 nmp->nm_retry = argp->retrans;
500 if (nmp->nm_retry > NFS_MAXREXMIT)
501 nmp->nm_retry = NFS_MAXREXMIT;
502 }
503
504 if (argp->flags & NFSMNT_NFSV3) {
505 if (argp->sotype == SOCK_DGRAM)
506 maxio = NFS_MAXDGRAMDATA;
507 else
508 maxio = NFS_MAXDATA;
509 } else
510 maxio = NFS_V2MAXDATA;
511
512 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
513 int osize = nmp->nm_wsize;
514 nmp->nm_wsize = argp->wsize;
515 /* Round down to multiple of blocksize */
516 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
517 if (nmp->nm_wsize <= 0)
518 nmp->nm_wsize = NFS_FABLKSIZE;
519 adjsock |= (nmp->nm_wsize != osize);
520 }
521 if (nmp->nm_wsize > maxio)
522 nmp->nm_wsize = maxio;
523 if (nmp->nm_wsize > MAXBSIZE)
524 nmp->nm_wsize = MAXBSIZE;
525
526 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
527 int osize = nmp->nm_rsize;
528 nmp->nm_rsize = argp->rsize;
529 /* Round down to multiple of blocksize */
530 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
531 if (nmp->nm_rsize <= 0)
532 nmp->nm_rsize = NFS_FABLKSIZE;
533 adjsock |= (nmp->nm_rsize != osize);
534 }
535 if (nmp->nm_rsize > maxio)
536 nmp->nm_rsize = maxio;
537 if (nmp->nm_rsize > MAXBSIZE)
538 nmp->nm_rsize = MAXBSIZE;
539
540 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
541 nmp->nm_readdirsize = argp->readdirsize;
542 /* Round down to multiple of blocksize */
543 nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
544 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
545 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
546 } else if (argp->flags & NFSMNT_RSIZE)
547 nmp->nm_readdirsize = nmp->nm_rsize;
548
549 if (nmp->nm_readdirsize > maxio)
550 nmp->nm_readdirsize = maxio;
551
552 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
553 argp->maxgrouplist <= NFS_MAXGRPS)
554 nmp->nm_numgrps = argp->maxgrouplist;
555 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
556 argp->readahead <= NFS_MAXRAHEAD)
557 nmp->nm_readahead = argp->readahead;
558 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
559 argp->leaseterm <= NQ_MAXLEASE)
560 nmp->nm_leaseterm = argp->leaseterm;
561 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
562 argp->deadthresh <= NQ_NEVERDEAD)
563 nmp->nm_deadthresh = argp->deadthresh;
564
565 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
566 (nmp->nm_soproto != argp->proto));
567 nmp->nm_sotype = argp->sotype;
568 nmp->nm_soproto = argp->proto;
569
570 if (nmp->nm_so && adjsock) {
571 nfs_disconnect(nmp);
572 if (nmp->nm_sotype == SOCK_DGRAM)
573 while (nfs_connect(nmp, (struct nfsreq *)0)) {
574 printf("nfs_args: retrying connect\n");
575 (void) tsleep((caddr_t)&lbolt,
576 PSOCK, "nfscon", 0);
577 }
578 }
579 }
580
581 /*
582 * VFS Operations.
583 *
584 * mount system call
585 * It seems a bit dumb to copyinstr() the host and path here and then
586 * bcopy() them in mountnfs(), but I wanted to detect errors before
587 * doing the sockargs() call because sockargs() allocates an mbuf and
588 * an error after that means that I have to release the mbuf.
589 */
590 /* ARGSUSED */
591 int
592 nfs_mount(mp, path, data, ndp, p)
593 struct mount *mp;
594 const char *path;
595 void *data;
596 struct nameidata *ndp;
597 struct proc *p;
598 {
599 int error;
600 struct nfs_args args;
601 struct mbuf *nam;
602 struct vnode *vp;
603 char pth[MNAMELEN], hst[MNAMELEN];
604 size_t len;
605 u_char nfh[NFSX_V3FHMAX];
606
607 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
608 if (error)
609 return (error);
610 if (args.version != NFS_ARGSVERSION)
611 return (EPROGMISMATCH);
612 if (mp->mnt_flag & MNT_UPDATE) {
613 register struct nfsmount *nmp = VFSTONFS(mp);
614
615 if (nmp == NULL)
616 return (EIO);
617 /*
618 * When doing an update, we can't change from or to
619 * v3 and/or nqnfs.
620 */
621 args.flags = (args.flags & ~(NFSMNT_NFSV3|NFSMNT_NQNFS)) |
622 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_NQNFS));
623 nfs_decode_args(nmp, &args);
624 return (0);
625 }
626 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
627 if (error)
628 return (error);
629 error = copyinstr(path, pth, MNAMELEN-1, &len);
630 if (error)
631 return (error);
632 bzero(&pth[len], MNAMELEN - len);
633 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
634 if (error)
635 return (error);
636 bzero(&hst[len], MNAMELEN - len);
637 /* sockargs() call must be after above copyin() calls */
638 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
639 if (error)
640 return (error);
641 args.fh = nfh;
642 error = mountnfs(&args, mp, nam, pth, hst, &vp);
643 return (error);
644 }
645
646 /*
647 * Common code for mount and mountroot
648 */
649 int
650 mountnfs(argp, mp, nam, pth, hst, vpp)
651 register struct nfs_args *argp;
652 register struct mount *mp;
653 struct mbuf *nam;
654 const char *pth, *hst;
655 struct vnode **vpp;
656 {
657 register struct nfsmount *nmp;
658 struct nfsnode *np;
659 int error;
660 struct vattr attrs;
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)
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, curproc->p_ucred, curproc); /* XXX */
755
756 return (0);
757 bad:
758 nfs_disconnect(nmp);
759 free((caddr_t)nmp, M_NFSMNT);
760 m_freem(nam);
761 return (error);
762 }
763
764 /*
765 * unmount system call
766 */
767 int
768 nfs_unmount(mp, mntflags, p)
769 struct mount *mp;
770 int mntflags;
771 struct proc *p;
772 {
773 register struct nfsmount *nmp;
774 struct nfsnode *np;
775 struct vnode *vp;
776 int error, flags = 0;
777
778 if (mntflags & MNT_FORCE)
779 flags |= FORCECLOSE;
780 nmp = VFSTONFS(mp);
781 /*
782 * Goes something like this..
783 * - Check for activity on the root vnode (other than ourselves).
784 * - Call vflush() to clear out vnodes for this file system,
785 * except for the root vnode.
786 * - Decrement reference on the vnode representing remote root.
787 * - Close the socket
788 * - Free up the data structures
789 */
790 /*
791 * We need to decrement the ref. count on the nfsnode representing
792 * the remote root. See comment in mountnfs(). The VFS unmount()
793 * has done vput on this vnode, otherwise we would get deadlock!
794 */
795 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
796 if (error)
797 return(error);
798 vp = NFSTOV(np);
799 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
800 vput(vp);
801 return (EBUSY);
802 }
803
804 /*
805 * Must handshake with nqnfs_clientd() if it is active.
806 */
807 nmp->nm_flag |= NFSMNT_DISMINPROG;
808 while (nmp->nm_inprog != NULLVP)
809 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
810 error = vflush(mp, vp, flags);
811 if (error) {
812 vput(vp);
813 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
814 return (error);
815 }
816
817 /*
818 * We are now committed to the unmount.
819 * For NQNFS, let the server daemon free the nfsmount structure.
820 */
821 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
822 nmp->nm_flag |= NFSMNT_DISMNT;
823
824 /*
825 * There are two reference counts to get rid of here.
826 */
827 vrele(vp);
828 vrele(vp);
829 vgone(vp);
830 nfs_disconnect(nmp);
831 m_freem(nmp->nm_nam);
832
833 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
834 free((caddr_t)nmp, M_NFSMNT);
835 return (0);
836 }
837
838 /*
839 * Return root of a filesystem
840 */
841 int
842 nfs_root(mp, vpp)
843 struct mount *mp;
844 struct vnode **vpp;
845 {
846 register struct vnode *vp;
847 struct nfsmount *nmp;
848 struct nfsnode *np;
849 int error;
850
851 nmp = VFSTONFS(mp);
852 error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
853 if (error)
854 return (error);
855 vp = NFSTOV(np);
856 if (vp->v_type == VNON)
857 vp->v_type = VDIR;
858 vp->v_flag = VROOT;
859 *vpp = vp;
860 return (0);
861 }
862
863 extern int syncprt;
864
865 /*
866 * Flush out the buffer cache
867 */
868 /* ARGSUSED */
869 int
870 nfs_sync(mp, waitfor, cred, p)
871 struct mount *mp;
872 int waitfor;
873 struct ucred *cred;
874 struct proc *p;
875 {
876 register struct vnode *vp;
877 int error, allerror = 0;
878
879 /*
880 * Force stale buffer cache information to be flushed.
881 */
882 loop:
883 for (vp = mp->mnt_vnodelist.lh_first;
884 vp != NULL;
885 vp = vp->v_mntvnodes.le_next) {
886 /*
887 * If the vnode that we are about to sync is no longer
888 * associated with this mount point, start over.
889 */
890 if (vp->v_mount != mp)
891 goto loop;
892 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
893 continue;
894 #ifdef Lite2_integrated
895 if (vget(vp, LK_EXCLUSIVE, p))
896 #else
897 if (vget(vp, 1))
898 #endif
899 goto loop;
900 error = VOP_FSYNC(vp, cred, waitfor, p);
901 if (error)
902 allerror = error;
903 vput(vp);
904 }
905 return (allerror);
906 }
907
908 /*
909 * NFS flat namespace lookup.
910 * Currently unsupported.
911 */
912 /* ARGSUSED */
913 int
914 nfs_vget(mp, ino, vpp)
915 struct mount *mp;
916 ino_t ino;
917 struct vnode **vpp;
918 {
919
920 return (EOPNOTSUPP);
921 }
922
923 #ifdef notyet
924 /*
925 * Do that sysctl thang...
926 */
927 static int
928 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
929 size_t newlen, struct proc *p)
930 {
931 int rv;
932
933 /*
934 * All names at this level are terminal.
935 */
936 if(namelen > 1)
937 return ENOTDIR; /* overloaded */
938
939 switch(name[0]) {
940 case NFS_NFSSTATS:
941 if(!oldp) {
942 *oldlenp = sizeof nfsstats;
943 return 0;
944 }
945
946 if(*oldlenp < sizeof nfsstats) {
947 *oldlenp = sizeof nfsstats;
948 return ENOMEM;
949 }
950
951 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
952 if(rv) return rv;
953
954 if(newp && newlen != sizeof nfsstats)
955 return EINVAL;
956
957 if(newp) {
958 return copyin(newp, &nfsstats, sizeof nfsstats);
959 }
960 return 0;
961
962 default:
963 return EOPNOTSUPP;
964 }
965 }
966 #endif
967
968
969 /*
970 * At this point, this should never happen
971 */
972 /* ARGSUSED */
973 int
974 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
975 register struct mount *mp;
976 struct fid *fhp;
977 struct mbuf *nam;
978 struct vnode **vpp;
979 int *exflagsp;
980 struct ucred **credanonp;
981 {
982
983 return (EINVAL);
984 }
985
986 /*
987 * Vnode pointer to File handle, should never happen either
988 */
989 /* ARGSUSED */
990 int
991 nfs_vptofh(vp, fhp)
992 struct vnode *vp;
993 struct fid *fhp;
994 {
995
996 return (EINVAL);
997 }
998
999 /*
1000 * Vfs start routine, a no-op.
1001 */
1002 /* ARGSUSED */
1003 int
1004 nfs_start(mp, flags, p)
1005 struct mount *mp;
1006 int flags;
1007 struct proc *p;
1008 {
1009
1010 return (0);
1011 }
1012
1013 /*
1014 * Do operations associated with quotas, not supported
1015 */
1016 /* ARGSUSED */
1017 int
1018 nfs_quotactl(mp, cmd, uid, arg, p)
1019 struct mount *mp;
1020 int cmd;
1021 uid_t uid;
1022 caddr_t arg;
1023 struct proc *p;
1024 {
1025
1026 return (EOPNOTSUPP);
1027 }
1028