nfs_vfsops.c revision 1.203.4.1 1 /* $NetBSD: nfs_vfsops.c,v 1.203.4.1 2014/04/25 15:43:51 sborrill 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.203.4.1 2014/04/25 15:43:51 sborrill 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 NULL,
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 (args == NULL)
604 return EINVAL;
605 if (*data_len < sizeof *args)
606 return EINVAL;
607
608 p = l->l_proc;
609 if (mp->mnt_flag & MNT_GETARGS) {
610
611 if (nmp == NULL)
612 return (EIO);
613 if (args->addr != NULL) {
614 sa = mtod(nmp->nm_nam, struct sockaddr *);
615 error = copyout(sa, args->addr, sa->sa_len);
616 if (error)
617 return (error);
618 args->addrlen = sa->sa_len;
619 } else
620 args->addrlen = 0;
621
622 args->version = NFS_ARGSVERSION;
623 args->sotype = nmp->nm_sotype;
624 args->proto = nmp->nm_soproto;
625 args->fh = NULL;
626 args->fhsize = 0;
627 args->flags = nmp->nm_flag;
628 args->wsize = nmp->nm_wsize;
629 args->rsize = nmp->nm_rsize;
630 args->readdirsize = nmp->nm_readdirsize;
631 args->timeo = nmp->nm_timeo;
632 args->retrans = nmp->nm_retry;
633 args->maxgrouplist = nmp->nm_numgrps;
634 args->readahead = nmp->nm_readahead;
635 args->leaseterm = 0; /* dummy */
636 args->deadthresh = nmp->nm_deadthresh;
637 args->hostname = NULL;
638 *data_len = sizeof *args;
639 return 0;
640 }
641
642 if (args->version != NFS_ARGSVERSION)
643 return (EPROGMISMATCH);
644 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
645 return (EPROGUNAVAIL);
646 #ifdef NFS_V2_ONLY
647 if (args->flags & NFSMNT_NFSV3)
648 return (EPROGMISMATCH);
649 #endif
650 if (mp->mnt_flag & MNT_UPDATE) {
651 if (nmp == NULL)
652 return (EIO);
653 /*
654 * When doing an update, we can't change from or to
655 * v3, or change cookie translation
656 */
657 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
658 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
659 nfs_decode_args(nmp, args, l);
660 return (0);
661 }
662 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
663 return (EINVAL);
664 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
665 error = copyin(args->fh, nfh, args->fhsize);
666 if (error)
667 goto free_nfh;
668 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
669 error = copyinstr(path, pth, MNAMELEN - 1, &len);
670 if (error)
671 goto free_pth;
672 memset(&pth[len], 0, MNAMELEN - len);
673 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
674 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
675 if (error)
676 goto free_hst;
677 memset(&hst[len], 0, MNAMELEN - len);
678 /* sockargs() call must be after above copyin() calls */
679 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
680 if (error)
681 goto free_hst;
682 MCLAIM(nam, &nfs_mowner);
683 args->fh = nfh;
684 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
685
686 free_hst:
687 FREE(hst, M_TEMP);
688 free_pth:
689 FREE(pth, M_TEMP);
690 free_nfh:
691 FREE(nfh, M_TEMP);
692
693 return (error);
694 }
695
696 /*
697 * Common code for mount and mountroot
698 */
699 int
700 mountnfs(argp, mp, nam, pth, hst, vpp, l)
701 struct nfs_args *argp;
702 struct mount *mp;
703 struct mbuf *nam;
704 const char *pth, *hst;
705 struct vnode **vpp;
706 struct lwp *l;
707 {
708 struct nfsmount *nmp;
709 struct nfsnode *np;
710 struct vnode *vp;
711 int error;
712 struct vattr *attrs;
713 kauth_cred_t cr;
714 char iosname[IOSTATNAMELEN];
715
716 /*
717 * If the number of nfs iothreads to use has never
718 * been set, create a reasonable number of them.
719 */
720
721 if (nfs_niothreads < 0) {
722 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
723 }
724
725 if (mp->mnt_flag & MNT_UPDATE) {
726 nmp = VFSTONFS(mp);
727 /* update paths, file handles, etc, here XXX */
728 m_freem(nam);
729 return (0);
730 } else {
731 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
732 mp->mnt_data = nmp;
733 TAILQ_INIT(&nmp->nm_uidlruhead);
734 TAILQ_INIT(&nmp->nm_bufq);
735 rw_init(&nmp->nm_writeverflock);
736 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
737 rw_init(&nmp->nm_rbtlock);
738 cv_init(&nmp->nm_rcvcv, "nfsrcv");
739 cv_init(&nmp->nm_sndcv, "nfssnd");
740 cv_init(&nmp->nm_aiocv, "nfsaio");
741 cv_init(&nmp->nm_disconcv, "nfsdis");
742 nfs_rbtinit(nmp);
743 }
744 vfs_getnewfsid(mp);
745 nmp->nm_mountp = mp;
746
747 #ifndef NFS_V2_ONLY
748 if ((argp->flags & NFSMNT_NFSV3) == 0)
749 #endif
750 {
751 if (argp->fhsize != NFSX_V2FH) {
752 return EINVAL;
753 }
754 }
755
756 /*
757 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
758 * will overwrite this.
759 */
760 nmp->nm_maxfilesize = 0xffffffffLL;
761
762 nmp->nm_timeo = NFS_TIMEO;
763 nmp->nm_retry = NFS_RETRANS;
764 nmp->nm_wsize = NFS_WSIZE;
765 nmp->nm_rsize = NFS_RSIZE;
766 nmp->nm_readdirsize = NFS_READDIRSIZE;
767 nmp->nm_numgrps = NFS_MAXGRPS;
768 nmp->nm_readahead = NFS_DEFRAHEAD;
769 nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
770 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
771 mp->mnt_op->vfs_name, mp, l);
772 if (error)
773 goto bad;
774 nmp->nm_nam = nam;
775
776 /* Set up the sockets and per-host congestion */
777 nmp->nm_sotype = argp->sotype;
778 nmp->nm_soproto = argp->proto;
779
780 nfs_decode_args(nmp, argp, l);
781
782 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
783 mp->mnt_dev_bshift = DEV_BSHIFT;
784
785 /*
786 * For Connection based sockets (TCP,...) defer the connect until
787 * the first request, in case the server is not responding.
788 */
789 if (nmp->nm_sotype == SOCK_DGRAM &&
790 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
791 goto bad;
792
793 /*
794 * This is silly, but it has to be set so that vinifod() works.
795 * We do not want to do an nfs_statvfs() here since we can get
796 * stuck on a dead server and we are holding a lock on the mount
797 * point.
798 */
799 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
800 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
801 if (error)
802 goto bad;
803 vp = NFSTOV(np);
804 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
805 VOP_GETATTR(vp, attrs, l->l_cred);
806 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
807 cr = kauth_cred_alloc();
808 kauth_cred_setuid(cr, attrs->va_uid);
809 kauth_cred_seteuid(cr, attrs->va_uid);
810 kauth_cred_setsvuid(cr, attrs->va_uid);
811 kauth_cred_setgid(cr, attrs->va_gid);
812 kauth_cred_setegid(cr, attrs->va_gid);
813 kauth_cred_setsvgid(cr, attrs->va_gid);
814 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
815 kauth_cred_free(cr);
816 }
817 FREE(attrs, M_TEMP);
818
819 /*
820 * A reference count is needed on the nfsnode representing the
821 * remote root. If this object is not persistent, then backward
822 * traversals of the mount point (i.e. "..") will not work if
823 * the nfsnode gets flushed out of the cache. Ufs does not have
824 * this problem, because one can identify root inodes by their
825 * number == ROOTINO (2). So, just unlock, but no rele.
826 */
827
828 nmp->nm_vnode = vp;
829 if (vp->v_type == VNON)
830 vp->v_type = VDIR;
831 vp->v_vflag |= VV_ROOT;
832 VOP_UNLOCK(vp, 0);
833 *vpp = vp;
834
835 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
836 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
837
838 return (0);
839 bad:
840 nfs_disconnect(nmp);
841 rw_destroy(&nmp->nm_writeverflock);
842 rw_destroy(&nmp->nm_rbtlock);
843 mutex_destroy(&nmp->nm_lock);
844 cv_destroy(&nmp->nm_rcvcv);
845 cv_destroy(&nmp->nm_sndcv);
846 cv_destroy(&nmp->nm_aiocv);
847 cv_destroy(&nmp->nm_disconcv);
848 kmem_free(nmp, sizeof(*nmp));
849 m_freem(nam);
850 return (error);
851 }
852
853 /*
854 * unmount system call
855 */
856 int
857 nfs_unmount(struct mount *mp, int mntflags)
858 {
859 struct nfsmount *nmp;
860 struct vnode *vp;
861 int error, flags = 0;
862
863 if (mntflags & MNT_FORCE)
864 flags |= FORCECLOSE;
865 nmp = VFSTONFS(mp);
866 /*
867 * Goes something like this..
868 * - Check for activity on the root vnode (other than ourselves).
869 * - Call vflush() to clear out vnodes for this file system,
870 * except for the root vnode.
871 * - Decrement reference on the vnode representing remote root.
872 * - Close the socket
873 * - Free up the data structures
874 */
875 /*
876 * We need to decrement the ref. count on the nfsnode representing
877 * the remote root. See comment in mountnfs(). The VFS unmount()
878 * has done vput on this vnode, otherwise we would get deadlock!
879 */
880 vp = nmp->nm_vnode;
881 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
882 if (error != 0)
883 return error;
884
885 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
886 vput(vp);
887 return (EBUSY);
888 }
889
890 error = vflush(mp, vp, flags);
891 if (error) {
892 vput(vp);
893 return (error);
894 }
895
896 /*
897 * We are now committed to the unmount; mark the mount structure
898 * as doomed so that any sleepers kicked awake by nfs_disconnect
899 * will go away cleanly.
900 */
901 nmp->nm_iflag |= NFSMNT_DISMNT;
902
903 /*
904 * Clean up the stats... note that we carefully avoid decrementing
905 * nfs_mount_count here for good reason - we may not be unmounting
906 * the last thing mounted.
907 */
908 iostat_free(nmp->nm_stats);
909
910 /*
911 * There are two reference counts to get rid of here
912 * (see comment in mountnfs()).
913 */
914 vput(vp);
915 vgone(vp);
916 nfs_disconnect(nmp);
917 m_freem(nmp->nm_nam);
918
919 rw_destroy(&nmp->nm_writeverflock);
920 rw_destroy(&nmp->nm_rbtlock);
921 mutex_destroy(&nmp->nm_lock);
922 cv_destroy(&nmp->nm_rcvcv);
923 cv_destroy(&nmp->nm_sndcv);
924 cv_destroy(&nmp->nm_aiocv);
925 cv_destroy(&nmp->nm_disconcv);
926 kmem_free(nmp, sizeof(*nmp));
927 return (0);
928 }
929
930 /*
931 * Return root of a filesystem
932 */
933 int
934 nfs_root(mp, vpp)
935 struct mount *mp;
936 struct vnode **vpp;
937 {
938 struct vnode *vp;
939 struct nfsmount *nmp;
940 int error;
941
942 nmp = VFSTONFS(mp);
943 vp = nmp->nm_vnode;
944 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
945 if (error != 0)
946 return error;
947 *vpp = vp;
948 return (0);
949 }
950
951 extern int syncprt;
952
953 /*
954 * Flush out the buffer cache
955 */
956 /* ARGSUSED */
957 int
958 nfs_sync(mp, waitfor, cred)
959 struct mount *mp;
960 int waitfor;
961 kauth_cred_t cred;
962 {
963 struct vnode *vp, *mvp;
964 int error, allerror = 0;
965
966 /*
967 * Force stale buffer cache information to be flushed.
968 */
969 if ((mvp = vnalloc(mp)) == NULL)
970 return (ENOMEM);
971 loop:
972 /*
973 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
974 * and vclean() can be called indirectly
975 */
976 mutex_enter(&mntvnode_lock);
977 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
978 vmark(mvp, vp);
979 if (vp->v_mount != mp || vismarker(vp))
980 continue;
981 mutex_enter(&vp->v_interlock);
982 /* XXX MNT_LAZY cannot be right? */
983 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
984 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
985 UVM_OBJ_IS_CLEAN(&vp->v_uobj))) {
986 mutex_exit(&vp->v_interlock);
987 continue;
988 }
989 mutex_exit(&mntvnode_lock);
990 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
991 (void)vunmark(mvp);
992 goto loop;
993 }
994 error = VOP_FSYNC(vp, cred,
995 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
996 if (error)
997 allerror = error;
998 vput(vp);
999 mutex_enter(&mntvnode_lock);
1000 }
1001 mutex_exit(&mntvnode_lock);
1002 vnfree(mvp);
1003 return (allerror);
1004 }
1005
1006 /*
1007 * NFS flat namespace lookup.
1008 * Currently unsupported.
1009 */
1010 /* ARGSUSED */
1011 int
1012 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1013 {
1014
1015 return (EOPNOTSUPP);
1016 }
1017
1018 /*
1019 * Do that sysctl thang...
1020 */
1021 static int
1022 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1023 {
1024 struct sysctlnode node;
1025 int val;
1026 int error;
1027
1028 val = nfs_niothreads;
1029 node = *rnode;
1030 node.sysctl_data = &val;
1031 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1032 if (error || newp == NULL)
1033 return error;
1034
1035 return nfs_set_niothreads(val);
1036 }
1037
1038 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
1039 {
1040
1041 sysctl_createv(clog, 0, NULL, NULL,
1042 CTLFLAG_PERMANENT,
1043 CTLTYPE_NODE, "vfs", NULL,
1044 NULL, 0, NULL, 0,
1045 CTL_VFS, CTL_EOL);
1046 sysctl_createv(clog, 0, NULL, NULL,
1047 CTLFLAG_PERMANENT,
1048 CTLTYPE_NODE, "nfs",
1049 SYSCTL_DESCR("NFS vfs options"),
1050 NULL, 0, NULL, 0,
1051 CTL_VFS, 2, CTL_EOL);
1052 /*
1053 * XXX the "2" above could be dynamic, thereby eliminating one
1054 * more instance of the "number to vfs" mapping problem, but
1055 * "2" is the order as taken from sys/mount.h
1056 */
1057
1058 sysctl_createv(clog, 0, NULL, NULL,
1059 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1060 CTLTYPE_STRUCT, "nfsstats",
1061 SYSCTL_DESCR("NFS operation statistics"),
1062 NULL, 0, &nfsstats, sizeof(nfsstats),
1063 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1064 sysctl_createv(clog, 0, NULL, NULL,
1065 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1066 CTLTYPE_INT, "iothreads",
1067 SYSCTL_DESCR("Number of NFS client processes desired"),
1068 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1069 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1070 }
1071
1072 /* ARGSUSED */
1073 int
1074 nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1075 {
1076 size_t fidsize;
1077 size_t fhsize;
1078 struct nfsnode *np;
1079 int error;
1080 struct vattr va;
1081
1082 fidsize = fid->fid_len;
1083 if (fidsize < sizeof(*fid)) {
1084 return EINVAL;
1085 }
1086 fhsize = fidsize - sizeof(*fid);
1087 if ((fhsize % NFSX_UNSIGNED) != 0) {
1088 return EINVAL;
1089 }
1090 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1091 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1092 return EINVAL;
1093 }
1094 } else {
1095 if (fhsize != NFSX_V2FH) {
1096 return EINVAL;
1097 }
1098 }
1099 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1100 if (error) {
1101 return error;
1102 }
1103 *vpp = NFSTOV(np);
1104 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1105 if (error != 0) {
1106 vput(*vpp);
1107 }
1108 return error;
1109 }
1110
1111 /* ARGSUSED */
1112 int
1113 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1114 {
1115 struct nfsnode *np;
1116 struct fid *fid;
1117 size_t fidsize;
1118 int error = 0;
1119
1120 np = VTONFS(vp);
1121 fidsize = sizeof(*fid) + np->n_fhsize;
1122 if (*bufsize < fidsize) {
1123 error = E2BIG;
1124 }
1125 *bufsize = fidsize;
1126 if (error == 0) {
1127 struct fid fid_store;
1128
1129 fid = &fid_store;
1130 memset(fid, 0, sizeof(*fid));
1131 fid->fid_len = fidsize;
1132 memcpy(buf, fid, sizeof(*fid));
1133 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1134 }
1135 return error;
1136 }
1137
1138 /*
1139 * Vfs start routine, a no-op.
1140 */
1141 /* ARGSUSED */
1142 int
1143 nfs_start(struct mount *mp, int flags)
1144 {
1145
1146 return (0);
1147 }
1148