nfs_vfsops.c revision 1.196.6.2 1 /* $NetBSD: nfs_vfsops.c,v 1.196.6.2 2008/10/05 20:11:33 mjf 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_vfsops.c,v 1.196.6.2 2008/10/05 20:11:33 mjf Exp $");
39
40 #if defined(_KERNEL_OPT)
41 #include "opt_compat_netbsd.h"
42 #include "opt_nfs.h"
43 #endif
44
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/signal.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/device.h>
51 #include <sys/vnode.h>
52 #include <sys/kernel.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/mbuf.h>
56 #include <sys/dirent.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/timetc.h>
62 #include <sys/kauth.h>
63 #include <sys/module.h>
64
65 #include <net/if.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfsproto.h>
71 #include <nfs/nfsnode.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsmount.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfs/nfsm_subs.h>
76 #include <nfs/nfsdiskless.h>
77 #include <nfs/nfs_var.h>
78
79 MODULE(MODULE_CLASS_VFS, nfs, NULL);
80
81 extern struct nfsstats nfsstats;
82 extern int nfs_ticks;
83
84 /*
85 * keep a count of the nfs mounts to generate ficticious drive names
86 * for the per drive stats.
87 */
88 unsigned int nfs_mount_count = 0;
89
90 /*
91 * nfs vfs operations.
92 */
93
94 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
95 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
96 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
97
98 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
99 &nfsv2_vnodeop_opv_desc,
100 &spec_nfsv2nodeop_opv_desc,
101 &fifo_nfsv2nodeop_opv_desc,
102 NULL,
103 };
104
105 struct vfsops nfs_vfsops = {
106 MOUNT_NFS,
107 sizeof (struct nfs_args),
108 nfs_mount,
109 nfs_start,
110 nfs_unmount,
111 nfs_root,
112 (void *)eopnotsupp, /* vfs_quotactl */
113 nfs_statvfs,
114 nfs_sync,
115 nfs_vget,
116 nfs_fhtovp,
117 nfs_vptofh,
118 nfs_vfs_init,
119 nfs_vfs_reinit,
120 nfs_vfs_done,
121 nfs_mountroot,
122 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
123 vfs_stdextattrctl,
124 (void *)eopnotsupp, /* vfs_suspendctl */
125 genfs_renamelock_enter,
126 genfs_renamelock_exit,
127 (void *)eopnotsupp,
128 nfs_vnodeopv_descs,
129 0,
130 { NULL, NULL },
131 };
132
133 static int
134 nfs_modcmd(modcmd_t cmd, void *arg)
135 {
136
137 switch (cmd) {
138 case MODULE_CMD_INIT:
139 return vfs_attach(&nfs_vfsops);
140 case MODULE_CMD_FINI:
141 return vfs_detach(&nfs_vfsops);
142 default:
143 return ENOTTY;
144 }
145 }
146
147 extern u_int32_t nfs_procids[NFS_NPROCS];
148 extern u_int32_t nfs_prog, nfs_vers;
149
150 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
151 struct mount **, struct vnode **, struct lwp *));
152
153 /*
154 * nfs statvfs call
155 */
156 int
157 nfs_statvfs(mp, sbp)
158 struct mount *mp;
159 struct statvfs *sbp;
160 {
161 struct lwp *l = curlwp;
162 struct vnode *vp;
163 struct nfs_statfs *sfp;
164 char *cp;
165 u_int32_t *tl;
166 int32_t t1, t2;
167 char *bpos, *dpos, *cp2;
168 struct nfsmount *nmp = VFSTONFS(mp);
169 int error = 0, retattr;
170 #ifdef NFS_V2_ONLY
171 const int v3 = 0;
172 #else
173 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
174 #endif
175 struct mbuf *mreq, *mrep = NULL, *md, *mb;
176 kauth_cred_t cred;
177 u_quad_t tquad;
178 struct nfsnode *np;
179
180 #ifndef nolint
181 sfp = (struct nfs_statfs *)0;
182 #endif
183 vp = nmp->nm_vnode;
184 np = VTONFS(vp);
185 cred = kauth_cred_alloc();
186 #ifndef NFS_V2_ONLY
187 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
188 (void)nfs_fsinfo(nmp, vp, cred, l);
189 #endif
190 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
191 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
192 nfsm_fhtom(np, v3);
193 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
194 if (v3)
195 nfsm_postop_attr(vp, retattr, 0);
196 if (error) {
197 if (mrep != NULL) {
198 if (mrep->m_next != NULL)
199 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
200 m_freem(mrep);
201 }
202 goto nfsmout;
203 }
204 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
205 sbp->f_flag = nmp->nm_flag;
206 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
207 if (v3) {
208 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
209 tquad = fxdr_hyper(&sfp->sf_tbytes);
210 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
211 tquad = fxdr_hyper(&sfp->sf_fbytes);
212 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
213 tquad = fxdr_hyper(&sfp->sf_abytes);
214 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
215 sbp->f_bresvd = sbp->f_bfree - tquad;
216 sbp->f_bavail = tquad;
217 #ifdef COMPAT_20
218 /* Handle older NFS servers returning negative values */
219 if ((quad_t)sbp->f_bavail < 0)
220 sbp->f_bavail = 0;
221 #endif
222 tquad = fxdr_hyper(&sfp->sf_tfiles);
223 sbp->f_files = tquad;
224 tquad = fxdr_hyper(&sfp->sf_ffiles);
225 sbp->f_ffree = tquad;
226 sbp->f_favail = tquad;
227 sbp->f_fresvd = 0;
228 sbp->f_namemax = MAXNAMLEN;
229 } else {
230 sbp->f_bsize = NFS_FABLKSIZE;
231 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
232 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
233 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
234 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
235 sbp->f_fresvd = 0;
236 sbp->f_files = 0;
237 sbp->f_ffree = 0;
238 sbp->f_favail = 0;
239 sbp->f_fresvd = 0;
240 sbp->f_namemax = MAXNAMLEN;
241 }
242 copy_statvfs_info(sbp, mp);
243 nfsm_reqdone;
244 kauth_cred_free(cred);
245 return (error);
246 }
247
248 #ifndef NFS_V2_ONLY
249 /*
250 * nfs version 3 fsinfo rpc call
251 */
252 int
253 nfs_fsinfo(nmp, vp, cred, l)
254 struct nfsmount *nmp;
255 struct vnode *vp;
256 kauth_cred_t cred;
257 struct lwp *l;
258 {
259 struct nfsv3_fsinfo *fsp;
260 char *cp;
261 int32_t t1, t2;
262 u_int32_t *tl, pref, xmax;
263 char *bpos, *dpos, *cp2;
264 int error = 0, retattr;
265 struct mbuf *mreq, *mrep, *md, *mb;
266 u_int64_t maxfsize;
267 struct nfsnode *np = VTONFS(vp);
268
269 nfsstats.rpccnt[NFSPROC_FSINFO]++;
270 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
271 nfsm_fhtom(np, 1);
272 nfsm_request(np, NFSPROC_FSINFO, l, cred);
273 nfsm_postop_attr(vp, retattr, 0);
274 if (!error) {
275 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
276 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
277 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
278 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
279 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
280 ~(NFS_FABLKSIZE - 1);
281 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
282 if (xmax < nmp->nm_wsize && xmax > 0) {
283 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
284 if (nmp->nm_wsize == 0)
285 nmp->nm_wsize = xmax;
286 }
287 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
288 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
289 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
290 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
291 ~(NFS_FABLKSIZE - 1);
292 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
293 if (xmax < nmp->nm_rsize && xmax > 0) {
294 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
295 if (nmp->nm_rsize == 0)
296 nmp->nm_rsize = xmax;
297 }
298 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
299 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
300 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
301 ~(NFS_DIRFRAGSIZ - 1);
302 if (xmax < nmp->nm_readdirsize && xmax > 0) {
303 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
304 if (nmp->nm_readdirsize == 0)
305 nmp->nm_readdirsize = xmax;
306 }
307 /* XXX */
308 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
309 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
310 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
311 nmp->nm_maxfilesize = maxfsize;
312 nmp->nm_mountp->mnt_fs_bshift =
313 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
314 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
315 }
316 nfsm_reqdone;
317 return (error);
318 }
319 #endif
320
321 /*
322 * Mount a remote root fs via. NFS. It goes like this:
323 * - Call nfs_boot_init() to fill in the nfs_diskless struct
324 * - build the rootfs mount point and call mountnfs() to do the rest.
325 */
326 int
327 nfs_mountroot()
328 {
329 struct timespec ts;
330 struct nfs_diskless *nd;
331 struct vattr attr;
332 struct mount *mp;
333 struct vnode *vp;
334 struct lwp *l;
335 long n;
336 int error;
337
338 l = curlwp; /* XXX */
339
340 if (device_class(root_device) != DV_IFNET)
341 return (ENODEV);
342
343 /*
344 * XXX time must be non-zero when we init the interface or else
345 * the arp code will wedge. [Fixed now in if_ether.c]
346 * However, the NFS attribute cache gives false "hits" when the
347 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
348 */
349 if (time_second < NFS_MAXATTRTIMO) {
350 ts.tv_sec = NFS_MAXATTRTIMO;
351 ts.tv_nsec = 0;
352 tc_setclock(&ts);
353 }
354
355 /*
356 * Call nfs_boot_init() to fill in the nfs_diskless struct.
357 * Side effect: Finds and configures a network interface.
358 */
359 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
360 error = nfs_boot_init(nd, l);
361 if (error) {
362 kmem_free(nd, sizeof(*nd));
363 return (error);
364 }
365
366 /*
367 * Create the root mount point.
368 */
369 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
370 if (error)
371 goto out;
372 printf("root on %s\n", nd->nd_root.ndm_host);
373
374 /*
375 * Link it into the mount list.
376 */
377 mutex_enter(&mountlist_lock);
378 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
379 mutex_exit(&mountlist_lock);
380 rootvp = vp;
381 mp->mnt_vnodecovered = NULLVP;
382 vfs_unbusy(mp, false, NULL);
383
384 /* Get root attributes (for the time). */
385 error = VOP_GETATTR(vp, &attr, l->l_cred);
386 if (error)
387 panic("nfs_mountroot: getattr for root");
388 n = attr.va_atime.tv_sec;
389 #ifdef DEBUG
390 printf("root time: 0x%lx\n", n);
391 #endif
392 setrootfstime(n);
393
394 out:
395 if (error)
396 nfs_boot_cleanup(nd, l);
397 kmem_free(nd, sizeof(*nd));
398 return (error);
399 }
400
401 /*
402 * Internal version of mount system call for diskless setup.
403 * Separate function because we used to call it twice.
404 * (once for root and once for swap)
405 */
406 static int
407 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, l)
408 struct nfs_dlmount *ndmntp;
409 const char *mntname; /* mount point name */
410 struct mount **mpp;
411 struct vnode **vpp;
412 struct lwp *l;
413 {
414 struct mount *mp;
415 struct mbuf *m;
416 int error;
417
418 vfs_rootmountalloc(MOUNT_NFS, mntname, &mp);
419
420 mp->mnt_op = &nfs_vfsops;
421
422 /*
423 * Historical practice expects NFS root file systems to
424 * be initially mounted r/w.
425 */
426 mp->mnt_flag &= ~MNT_RDONLY;
427
428 /* Get mbuf for server sockaddr. */
429 m = m_get(M_WAIT, MT_SONAME);
430 if (m == NULL)
431 panic("nfs_mountroot: mget soname for %s", mntname);
432 MCLAIM(m, &nfs_mowner);
433 memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr,
434 (m->m_len = ndmntp->ndm_args.addr->sa_len));
435
436 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
437 ndmntp->ndm_args.hostname, vpp, l);
438 if (error) {
439 vfs_unbusy(mp, false, NULL);
440 vfs_destroy(mp);
441 printf("nfs_mountroot: mount %s failed: %d\n",
442 mntname, error);
443 } else
444 *mpp = mp;
445
446 return (error);
447 }
448
449 void
450 nfs_decode_args(nmp, argp, l)
451 struct nfsmount *nmp;
452 struct nfs_args *argp;
453 struct lwp *l;
454 {
455 int s;
456 int adjsock;
457 int maxio;
458
459 s = splsoftnet();
460
461 /*
462 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
463 * no sense in that context.
464 */
465 if (argp->sotype == SOCK_STREAM)
466 argp->flags &= ~NFSMNT_NOCONN;
467
468 /*
469 * Cookie translation is not needed for v2, silently ignore it.
470 */
471 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
472 NFSMNT_XLATECOOKIE)
473 argp->flags &= ~NFSMNT_XLATECOOKIE;
474
475 /* Re-bind if rsrvd port requested and wasn't on one */
476 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
477 && (argp->flags & NFSMNT_RESVPORT);
478 /* Also re-bind if we're switching to/from a connected UDP socket */
479 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
480 (argp->flags & NFSMNT_NOCONN));
481
482 /* Update flags. */
483 nmp->nm_flag = argp->flags;
484 splx(s);
485
486 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
487 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
488 if (nmp->nm_timeo < NFS_MINTIMEO)
489 nmp->nm_timeo = NFS_MINTIMEO;
490 else if (nmp->nm_timeo > NFS_MAXTIMEO)
491 nmp->nm_timeo = NFS_MAXTIMEO;
492 }
493
494 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
495 nmp->nm_retry = argp->retrans;
496 if (nmp->nm_retry > NFS_MAXREXMIT)
497 nmp->nm_retry = NFS_MAXREXMIT;
498 }
499
500 #ifndef NFS_V2_ONLY
501 if (argp->flags & NFSMNT_NFSV3) {
502 if (argp->sotype == SOCK_DGRAM)
503 maxio = NFS_MAXDGRAMDATA;
504 else
505 maxio = NFS_MAXDATA;
506 } else
507 #endif
508 maxio = NFS_V2MAXDATA;
509
510 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
511 int osize = nmp->nm_wsize;
512 nmp->nm_wsize = argp->wsize;
513 /* Round down to multiple of blocksize */
514 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
515 if (nmp->nm_wsize <= 0)
516 nmp->nm_wsize = NFS_FABLKSIZE;
517 adjsock |= (nmp->nm_wsize != osize);
518 }
519 if (nmp->nm_wsize > maxio)
520 nmp->nm_wsize = maxio;
521 if (nmp->nm_wsize > MAXBSIZE)
522 nmp->nm_wsize = MAXBSIZE;
523
524 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
525 int osize = nmp->nm_rsize;
526 nmp->nm_rsize = argp->rsize;
527 /* Round down to multiple of blocksize */
528 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
529 if (nmp->nm_rsize <= 0)
530 nmp->nm_rsize = NFS_FABLKSIZE;
531 adjsock |= (nmp->nm_rsize != osize);
532 }
533 if (nmp->nm_rsize > maxio)
534 nmp->nm_rsize = maxio;
535 if (nmp->nm_rsize > MAXBSIZE)
536 nmp->nm_rsize = MAXBSIZE;
537
538 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
539 nmp->nm_readdirsize = argp->readdirsize;
540 /* Round down to multiple of minimum blocksize */
541 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
542 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
543 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
544 /* Bigger than buffer size makes no sense */
545 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
546 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
547 } else if (argp->flags & NFSMNT_RSIZE)
548 nmp->nm_readdirsize = nmp->nm_rsize;
549
550 if (nmp->nm_readdirsize > maxio)
551 nmp->nm_readdirsize = maxio;
552
553 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
554 argp->maxgrouplist <= NFS_MAXGRPS)
555 nmp->nm_numgrps = argp->maxgrouplist;
556 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
557 argp->readahead <= NFS_MAXRAHEAD)
558 nmp->nm_readahead = argp->readahead;
559 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
560 argp->deadthresh <= NFS_NEVERDEAD)
561 nmp->nm_deadthresh = argp->deadthresh;
562
563 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
564 (nmp->nm_soproto != argp->proto));
565 nmp->nm_sotype = argp->sotype;
566 nmp->nm_soproto = argp->proto;
567
568 if (nmp->nm_so && adjsock) {
569 nfs_safedisconnect(nmp);
570 if (nmp->nm_sotype == SOCK_DGRAM)
571 while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
572 printf("nfs_args: retrying connect\n");
573 kpause("nfscn3", false, hz, NULL);
574 }
575 }
576 }
577
578 /*
579 * VFS Operations.
580 *
581 * mount system call
582 * It seems a bit dumb to copyinstr() the host and path here and then
583 * memcpy() them in mountnfs(), but I wanted to detect errors before
584 * doing the sockargs() call because sockargs() allocates an mbuf and
585 * an error after that means that I have to release the mbuf.
586 */
587 /* ARGSUSED */
588 int
589 nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
590 {
591 struct lwp *l = curlwp;
592 int error;
593 struct nfs_args *args = data;
594 struct mbuf *nam;
595 struct nfsmount *nmp = VFSTONFS(mp);
596 struct sockaddr *sa;
597 struct vnode *vp;
598 char *pth, *hst;
599 struct proc *p;
600 size_t len;
601 u_char *nfh;
602
603 if (*data_len < sizeof *args)
604 return EINVAL;
605
606 p = l->l_proc;
607 if (mp->mnt_flag & MNT_GETARGS) {
608
609 if (nmp == NULL)
610 return (EIO);
611 if (args->addr != NULL) {
612 sa = mtod(nmp->nm_nam, struct sockaddr *);
613 error = copyout(sa, args->addr, sa->sa_len);
614 if (error)
615 return (error);
616 args->addrlen = sa->sa_len;
617 } else
618 args->addrlen = 0;
619
620 args->version = NFS_ARGSVERSION;
621 args->sotype = nmp->nm_sotype;
622 args->proto = nmp->nm_soproto;
623 args->fh = NULL;
624 args->fhsize = 0;
625 args->flags = nmp->nm_flag;
626 args->wsize = nmp->nm_wsize;
627 args->rsize = nmp->nm_rsize;
628 args->readdirsize = nmp->nm_readdirsize;
629 args->timeo = nmp->nm_timeo;
630 args->retrans = nmp->nm_retry;
631 args->maxgrouplist = nmp->nm_numgrps;
632 args->readahead = nmp->nm_readahead;
633 args->leaseterm = 0; /* dummy */
634 args->deadthresh = nmp->nm_deadthresh;
635 args->hostname = NULL;
636 *data_len = sizeof *args;
637 return 0;
638 }
639
640 if (args->version != NFS_ARGSVERSION)
641 return (EPROGMISMATCH);
642 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
643 return (EPROGUNAVAIL);
644 #ifdef NFS_V2_ONLY
645 if (args->flags & NFSMNT_NFSV3)
646 return (EPROGMISMATCH);
647 #endif
648 if (mp->mnt_flag & MNT_UPDATE) {
649 if (nmp == NULL)
650 return (EIO);
651 /*
652 * When doing an update, we can't change from or to
653 * v3, or change cookie translation
654 */
655 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
656 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
657 nfs_decode_args(nmp, args, l);
658 return (0);
659 }
660 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
661 return (EINVAL);
662 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
663 error = copyin(args->fh, nfh, args->fhsize);
664 if (error)
665 goto free_nfh;
666 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
667 error = copyinstr(path, pth, MNAMELEN - 1, &len);
668 if (error)
669 goto free_pth;
670 memset(&pth[len], 0, MNAMELEN - len);
671 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
672 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
673 if (error)
674 goto free_hst;
675 memset(&hst[len], 0, MNAMELEN - len);
676 /* sockargs() call must be after above copyin() calls */
677 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
678 if (error)
679 goto free_hst;
680 MCLAIM(nam, &nfs_mowner);
681 args->fh = nfh;
682 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
683
684 free_hst:
685 FREE(hst, M_TEMP);
686 free_pth:
687 FREE(pth, M_TEMP);
688 free_nfh:
689 FREE(nfh, M_TEMP);
690
691 return (error);
692 }
693
694 /*
695 * Common code for mount and mountroot
696 */
697 int
698 mountnfs(argp, mp, nam, pth, hst, vpp, l)
699 struct nfs_args *argp;
700 struct mount *mp;
701 struct mbuf *nam;
702 const char *pth, *hst;
703 struct vnode **vpp;
704 struct lwp *l;
705 {
706 struct nfsmount *nmp;
707 struct nfsnode *np;
708 struct vnode *vp;
709 int error;
710 struct vattr *attrs;
711 kauth_cred_t cr;
712 char iosname[IOSTATNAMELEN];
713
714 /*
715 * If the number of nfs iothreads to use has never
716 * been set, create a reasonable number of them.
717 */
718
719 if (nfs_niothreads < 0) {
720 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
721 }
722
723 if (mp->mnt_flag & MNT_UPDATE) {
724 nmp = VFSTONFS(mp);
725 /* update paths, file handles, etc, here XXX */
726 m_freem(nam);
727 return (0);
728 } else {
729 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
730 mp->mnt_data = nmp;
731 TAILQ_INIT(&nmp->nm_uidlruhead);
732 TAILQ_INIT(&nmp->nm_bufq);
733 rw_init(&nmp->nm_writeverflock);
734 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
735 cv_init(&nmp->nm_rcvcv, "nfsrcv");
736 cv_init(&nmp->nm_sndcv, "nfssnd");
737 cv_init(&nmp->nm_aiocv, "nfsaio");
738 cv_init(&nmp->nm_disconcv, "nfsdis");
739 }
740 vfs_getnewfsid(mp);
741 nmp->nm_mountp = mp;
742
743 #ifndef NFS_V2_ONLY
744 if ((argp->flags & NFSMNT_NFSV3) == 0)
745 #endif
746 {
747 if (argp->fhsize != NFSX_V2FH) {
748 return EINVAL;
749 }
750 }
751
752 /*
753 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
754 * will overwrite this.
755 */
756 nmp->nm_maxfilesize = 0xffffffffLL;
757
758 nmp->nm_timeo = NFS_TIMEO;
759 nmp->nm_retry = NFS_RETRANS;
760 nmp->nm_wsize = NFS_WSIZE;
761 nmp->nm_rsize = NFS_RSIZE;
762 nmp->nm_readdirsize = NFS_READDIRSIZE;
763 nmp->nm_numgrps = NFS_MAXGRPS;
764 nmp->nm_readahead = NFS_DEFRAHEAD;
765 nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
766 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
767 mp->mnt_op->vfs_name, mp, l);
768 if (error)
769 goto bad;
770 nmp->nm_nam = nam;
771
772 /* Set up the sockets and per-host congestion */
773 nmp->nm_sotype = argp->sotype;
774 nmp->nm_soproto = argp->proto;
775
776 nfs_decode_args(nmp, argp, l);
777
778 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
779 mp->mnt_dev_bshift = DEV_BSHIFT;
780
781 /*
782 * For Connection based sockets (TCP,...) defer the connect until
783 * the first request, in case the server is not responding.
784 */
785 if (nmp->nm_sotype == SOCK_DGRAM &&
786 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
787 goto bad;
788
789 /*
790 * This is silly, but it has to be set so that vinifod() works.
791 * We do not want to do an nfs_statvfs() here since we can get
792 * stuck on a dead server and we are holding a lock on the mount
793 * point.
794 */
795 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
796 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
797 if (error)
798 goto bad;
799 vp = NFSTOV(np);
800 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
801 VOP_GETATTR(vp, attrs, l->l_cred);
802 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
803 cr = kauth_cred_alloc();
804 kauth_cred_setuid(cr, attrs->va_uid);
805 kauth_cred_seteuid(cr, attrs->va_uid);
806 kauth_cred_setsvuid(cr, attrs->va_uid);
807 kauth_cred_setgid(cr, attrs->va_gid);
808 kauth_cred_setegid(cr, attrs->va_gid);
809 kauth_cred_setsvgid(cr, attrs->va_gid);
810 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
811 kauth_cred_free(cr);
812 }
813 FREE(attrs, M_TEMP);
814
815 /*
816 * A reference count is needed on the nfsnode representing the
817 * remote root. If this object is not persistent, then backward
818 * traversals of the mount point (i.e. "..") will not work if
819 * the nfsnode gets flushed out of the cache. Ufs does not have
820 * this problem, because one can identify root inodes by their
821 * number == ROOTINO (2). So, just unlock, but no rele.
822 */
823
824 nmp->nm_vnode = vp;
825 if (vp->v_type == VNON)
826 vp->v_type = VDIR;
827 vp->v_vflag |= VV_ROOT;
828 VOP_UNLOCK(vp, 0);
829 *vpp = vp;
830
831 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
832 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
833
834 return (0);
835 bad:
836 nfs_disconnect(nmp);
837 rw_destroy(&nmp->nm_writeverflock);
838 mutex_destroy(&nmp->nm_lock);
839 cv_destroy(&nmp->nm_rcvcv);
840 cv_destroy(&nmp->nm_sndcv);
841 cv_destroy(&nmp->nm_aiocv);
842 cv_destroy(&nmp->nm_disconcv);
843 kmem_free(nmp, sizeof(*nmp));
844 m_freem(nam);
845 return (error);
846 }
847
848 /*
849 * unmount system call
850 */
851 int
852 nfs_unmount(struct mount *mp, int mntflags)
853 {
854 struct nfsmount *nmp;
855 struct vnode *vp;
856 int error, flags = 0;
857
858 if (mntflags & MNT_FORCE)
859 flags |= FORCECLOSE;
860 nmp = VFSTONFS(mp);
861 /*
862 * Goes something like this..
863 * - Check for activity on the root vnode (other than ourselves).
864 * - Call vflush() to clear out vnodes for this file system,
865 * except for the root vnode.
866 * - Decrement reference on the vnode representing remote root.
867 * - Close the socket
868 * - Free up the data structures
869 */
870 /*
871 * We need to decrement the ref. count on the nfsnode representing
872 * the remote root. See comment in mountnfs(). The VFS unmount()
873 * has done vput on this vnode, otherwise we would get deadlock!
874 */
875 vp = nmp->nm_vnode;
876 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
877 if (error != 0)
878 return error;
879
880 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
881 vput(vp);
882 return (EBUSY);
883 }
884
885 error = vflush(mp, vp, flags);
886 if (error) {
887 vput(vp);
888 return (error);
889 }
890
891 /*
892 * We are now committed to the unmount; mark the mount structure
893 * as doomed so that any sleepers kicked awake by nfs_disconnect
894 * will go away cleanly.
895 */
896 nmp->nm_iflag |= NFSMNT_DISMNT;
897
898 /*
899 * Clean up the stats... note that we carefully avoid decrementing
900 * nfs_mount_count here for good reason - we may not be unmounting
901 * the last thing mounted.
902 */
903 iostat_free(nmp->nm_stats);
904
905 /*
906 * There are two reference counts to get rid of here
907 * (see comment in mountnfs()).
908 */
909 vput(vp);
910 vgone(vp);
911 nfs_disconnect(nmp);
912 m_freem(nmp->nm_nam);
913
914 rw_destroy(&nmp->nm_writeverflock);
915 mutex_destroy(&nmp->nm_lock);
916 cv_destroy(&nmp->nm_rcvcv);
917 cv_destroy(&nmp->nm_sndcv);
918 cv_destroy(&nmp->nm_aiocv);
919 cv_destroy(&nmp->nm_disconcv);
920 kmem_free(nmp, sizeof(*nmp));
921 return (0);
922 }
923
924 /*
925 * Return root of a filesystem
926 */
927 int
928 nfs_root(mp, vpp)
929 struct mount *mp;
930 struct vnode **vpp;
931 {
932 struct vnode *vp;
933 struct nfsmount *nmp;
934 int error;
935
936 nmp = VFSTONFS(mp);
937 vp = nmp->nm_vnode;
938 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
939 if (error != 0)
940 return error;
941 *vpp = vp;
942 return (0);
943 }
944
945 extern int syncprt;
946
947 /*
948 * Flush out the buffer cache
949 */
950 /* ARGSUSED */
951 int
952 nfs_sync(mp, waitfor, cred)
953 struct mount *mp;
954 int waitfor;
955 kauth_cred_t cred;
956 {
957 struct vnode *vp, *mvp;
958 int error, allerror = 0;
959
960 /*
961 * Force stale buffer cache information to be flushed.
962 */
963 if ((mvp = vnalloc(mp)) == NULL)
964 return (ENOMEM);
965 loop:
966 /*
967 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
968 * and vclean() can be called indirectly
969 */
970 mutex_enter(&mntvnode_lock);
971 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
972 vmark(mvp, vp);
973 if (vp->v_mount != mp || vismarker(vp))
974 continue;
975 mutex_enter(&vp->v_interlock);
976 /* XXX MNT_LAZY cannot be right? */
977 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
978 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
979 UVM_OBJ_IS_CLEAN(&vp->v_uobj))) {
980 mutex_exit(&vp->v_interlock);
981 continue;
982 }
983 mutex_exit(&mntvnode_lock);
984 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
985 (void)vunmark(mvp);
986 goto loop;
987 }
988 error = VOP_FSYNC(vp, cred,
989 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
990 if (error)
991 allerror = error;
992 vput(vp);
993 mutex_enter(&mntvnode_lock);
994 }
995 mutex_exit(&mntvnode_lock);
996 vnfree(mvp);
997 return (allerror);
998 }
999
1000 /*
1001 * NFS flat namespace lookup.
1002 * Currently unsupported.
1003 */
1004 /* ARGSUSED */
1005 int
1006 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1007 {
1008
1009 return (EOPNOTSUPP);
1010 }
1011
1012 /*
1013 * Do that sysctl thang...
1014 */
1015 static int
1016 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1017 {
1018 struct sysctlnode node;
1019 int val;
1020 int error;
1021
1022 val = nfs_niothreads;
1023 node = *rnode;
1024 node.sysctl_data = &val;
1025 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1026 if (error || newp == NULL)
1027 return error;
1028
1029 return nfs_set_niothreads(val);
1030 }
1031
1032 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
1033 {
1034
1035 sysctl_createv(clog, 0, NULL, NULL,
1036 CTLFLAG_PERMANENT,
1037 CTLTYPE_NODE, "vfs", NULL,
1038 NULL, 0, NULL, 0,
1039 CTL_VFS, CTL_EOL);
1040 sysctl_createv(clog, 0, NULL, NULL,
1041 CTLFLAG_PERMANENT,
1042 CTLTYPE_NODE, "nfs",
1043 SYSCTL_DESCR("NFS vfs options"),
1044 NULL, 0, NULL, 0,
1045 CTL_VFS, 2, CTL_EOL);
1046 /*
1047 * XXX the "2" above could be dynamic, thereby eliminating one
1048 * more instance of the "number to vfs" mapping problem, but
1049 * "2" is the order as taken from sys/mount.h
1050 */
1051
1052 sysctl_createv(clog, 0, NULL, NULL,
1053 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1054 CTLTYPE_STRUCT, "nfsstats",
1055 SYSCTL_DESCR("NFS operation statistics"),
1056 NULL, 0, &nfsstats, sizeof(nfsstats),
1057 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1058 sysctl_createv(clog, 0, NULL, NULL,
1059 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1060 CTLTYPE_INT, "iothreads",
1061 SYSCTL_DESCR("Number of NFS client processes desired"),
1062 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1063 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1064 }
1065
1066 /* ARGSUSED */
1067 int
1068 nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1069 {
1070 size_t fidsize;
1071 size_t fhsize;
1072 struct nfsnode *np;
1073 int error;
1074 struct vattr va;
1075
1076 fidsize = fid->fid_len;
1077 if (fidsize < sizeof(*fid)) {
1078 return EINVAL;
1079 }
1080 fhsize = fidsize - sizeof(*fid);
1081 if ((fhsize % NFSX_UNSIGNED) != 0) {
1082 return EINVAL;
1083 }
1084 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1085 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1086 return EINVAL;
1087 }
1088 } else {
1089 if (fhsize != NFSX_V2FH) {
1090 return EINVAL;
1091 }
1092 }
1093 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1094 if (error) {
1095 return error;
1096 }
1097 *vpp = NFSTOV(np);
1098 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1099 if (error != 0) {
1100 vput(*vpp);
1101 }
1102 return error;
1103 }
1104
1105 /* ARGSUSED */
1106 int
1107 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1108 {
1109 struct nfsnode *np;
1110 struct fid *fid;
1111 size_t fidsize;
1112 int error = 0;
1113
1114 np = VTONFS(vp);
1115 fidsize = sizeof(*fid) + np->n_fhsize;
1116 if (*bufsize < fidsize) {
1117 error = E2BIG;
1118 }
1119 *bufsize = fidsize;
1120 if (error == 0) {
1121 struct fid fid_store;
1122
1123 fid = &fid_store;
1124 memset(fid, 0, sizeof(*fid));
1125 fid->fid_len = fidsize;
1126 memcpy(buf, fid, sizeof(*fid));
1127 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1128 }
1129 return error;
1130 }
1131
1132 /*
1133 * Vfs start routine, a no-op.
1134 */
1135 /* ARGSUSED */
1136 int
1137 nfs_start(struct mount *mp, int flags)
1138 {
1139
1140 return (0);
1141 }
1142