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