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