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