nfs_vfsops.c revision 1.220.14.1 1 /* $NetBSD: nfs_vfsops.c,v 1.220.14.1 2014/04/21 10:15:36 bouyer 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.220.14.1 2014/04/21 10:15:36 bouyer Exp $");
39
40 #if defined(_KERNEL_OPT)
41 #include "opt_nfs.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/signal.h>
47 #include <sys/proc.h>
48 #include <sys/namei.h>
49 #include <sys/device.h>
50 #include <sys/vnode.h>
51 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/buf.h>
54 #include <sys/mbuf.h>
55 #include <sys/dirent.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/timetc.h>
61 #include <sys/kauth.h>
62 #include <sys/module.h>
63
64 #include <net/if.h>
65 #include <net/route.h>
66 #include <netinet/in.h>
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfsnode.h>
71 #include <nfs/nfs.h>
72 #include <nfs/nfsmount.h>
73 #include <nfs/xdr_subs.h>
74 #include <nfs/nfsm_subs.h>
75 #include <nfs/nfsdiskless.h>
76 #include <nfs/nfs_var.h>
77
78 MODULE(MODULE_CLASS_VFS, nfs, NULL);
79
80 extern struct nfsstats nfsstats;
81 extern int nfs_ticks;
82
83 /*
84 * keep a count of the nfs mounts to generate ficticious drive names
85 * for the per drive stats.
86 */
87 unsigned int nfs_mount_count = 0;
88
89 int nfs_commitsize;
90
91 /*
92 * nfs vfs operations.
93 */
94
95 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
96 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
97 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
98
99 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
100 &nfsv2_vnodeop_opv_desc,
101 &spec_nfsv2nodeop_opv_desc,
102 &fifo_nfsv2nodeop_opv_desc,
103 NULL,
104 };
105
106 struct vfsops nfs_vfsops = {
107 MOUNT_NFS,
108 sizeof (struct nfs_args),
109 nfs_mount,
110 nfs_start,
111 nfs_unmount,
112 nfs_root,
113 (void *)eopnotsupp, /* vfs_quotactl */
114 nfs_statvfs,
115 nfs_sync,
116 nfs_vget,
117 nfs_fhtovp,
118 nfs_vptofh,
119 nfs_vfs_init,
120 NULL,
121 nfs_vfs_done,
122 nfs_mountroot,
123 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
124 vfs_stdextattrctl,
125 (void *)eopnotsupp, /* vfs_suspendctl */
126 genfs_renamelock_enter,
127 genfs_renamelock_exit,
128 (void *)eopnotsupp,
129 nfs_vnodeopv_descs,
130 0,
131 { NULL, NULL },
132 };
133
134 extern u_int32_t nfs_procids[NFS_NPROCS];
135 extern u_int32_t nfs_prog, nfs_vers;
136 static struct sysctllog *nfs_clog;
137
138 static int nfs_mount_diskless(struct nfs_dlmount *, const char *,
139 struct mount **, struct vnode **, struct lwp *);
140 static void nfs_sysctl_init(void);
141 static void nfs_sysctl_fini(void);
142
143 static int
144 nfs_modcmd(modcmd_t cmd, void *arg)
145 {
146 int error;
147
148 switch (cmd) {
149 case MODULE_CMD_INIT:
150 error = vfs_attach(&nfs_vfsops);
151 if (error == 0) {
152 nfs_sysctl_init();
153 }
154 return error;
155 case MODULE_CMD_FINI:
156 error = vfs_detach(&nfs_vfsops);
157 if (error == 0) {
158 nfs_sysctl_fini();
159 }
160 return error;
161 default:
162 return ENOTTY;
163 }
164 }
165
166 /*
167 * nfs statvfs call
168 */
169 int
170 nfs_statvfs(struct mount *mp, struct statvfs *sbp)
171 {
172 struct lwp *l = curlwp;
173 struct vnode *vp;
174 struct nfs_statfs *sfp;
175 char *cp;
176 u_int32_t *tl;
177 int32_t t1, t2;
178 char *bpos, *dpos, *cp2;
179 struct nfsmount *nmp = VFSTONFS(mp);
180 int error = 0, retattr;
181 #ifdef NFS_V2_ONLY
182 const int v3 = 0;
183 #else
184 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
185 #endif
186 struct mbuf *mreq, *mrep = NULL, *md, *mb;
187 kauth_cred_t cred;
188 u_quad_t tquad;
189 struct nfsnode *np;
190
191 #ifndef nolint
192 sfp = (struct nfs_statfs *)0;
193 #endif
194 vp = nmp->nm_vnode;
195 np = VTONFS(vp);
196 cred = kauth_cred_alloc();
197 #ifndef NFS_V2_ONLY
198 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
199 (void)nfs_fsinfo(nmp, vp, cred, l);
200 #endif
201 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
202 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
203 nfsm_fhtom(np, v3);
204 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
205 if (v3)
206 nfsm_postop_attr(vp, retattr, 0);
207 if (error) {
208 if (mrep != NULL) {
209 if (mrep->m_next != NULL)
210 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
211 m_freem(mrep);
212 }
213 goto nfsmout;
214 }
215 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
216 sbp->f_flag = nmp->nm_flag;
217 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
218 if (v3) {
219 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
220 tquad = fxdr_hyper(&sfp->sf_tbytes);
221 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
222 tquad = fxdr_hyper(&sfp->sf_fbytes);
223 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
224 tquad = fxdr_hyper(&sfp->sf_abytes);
225 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
226 sbp->f_bresvd = sbp->f_bfree - tquad;
227 sbp->f_bavail = tquad;
228 /* Handle older NFS servers returning negative values */
229 if ((quad_t)sbp->f_bavail < 0)
230 sbp->f_bavail = 0;
231 tquad = fxdr_hyper(&sfp->sf_tfiles);
232 sbp->f_files = tquad;
233 tquad = fxdr_hyper(&sfp->sf_ffiles);
234 sbp->f_ffree = tquad;
235 sbp->f_favail = tquad;
236 sbp->f_fresvd = 0;
237 sbp->f_namemax = NFS_MAXNAMLEN;
238 } else {
239 sbp->f_bsize = NFS_FABLKSIZE;
240 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
241 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
242 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
243 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
244 sbp->f_fresvd = 0;
245 sbp->f_files = 0;
246 sbp->f_ffree = 0;
247 sbp->f_favail = 0;
248 sbp->f_fresvd = 0;
249 sbp->f_namemax = NFS_MAXNAMLEN;
250 }
251 copy_statvfs_info(sbp, mp);
252 nfsm_reqdone;
253 kauth_cred_free(cred);
254 return (error);
255 }
256
257 #ifndef NFS_V2_ONLY
258 /*
259 * nfs version 3 fsinfo rpc call
260 */
261 int
262 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, kauth_cred_t cred, struct lwp *l)
263 {
264 struct nfsv3_fsinfo *fsp;
265 char *cp;
266 int32_t t1, t2;
267 u_int32_t *tl, pref, xmax;
268 char *bpos, *dpos, *cp2;
269 int error = 0, retattr;
270 struct mbuf *mreq, *mrep, *md, *mb;
271 u_int64_t maxfsize;
272 struct nfsnode *np = VTONFS(vp);
273
274 nfsstats.rpccnt[NFSPROC_FSINFO]++;
275 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
276 nfsm_fhtom(np, 1);
277 nfsm_request(np, NFSPROC_FSINFO, l, cred);
278 nfsm_postop_attr(vp, retattr, 0);
279 if (!error) {
280 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
281 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
282 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
283 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
284 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
285 ~(NFS_FABLKSIZE - 1);
286 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
287 if (xmax < nmp->nm_wsize && xmax > 0) {
288 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
289 if (nmp->nm_wsize == 0)
290 nmp->nm_wsize = xmax;
291 }
292 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
293 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
294 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
295 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
296 ~(NFS_FABLKSIZE - 1);
297 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
298 if (xmax < nmp->nm_rsize && xmax > 0) {
299 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
300 if (nmp->nm_rsize == 0)
301 nmp->nm_rsize = xmax;
302 }
303 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
304 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
305 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
306 ~(NFS_DIRFRAGSIZ - 1);
307 if (xmax < nmp->nm_readdirsize && xmax > 0) {
308 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
309 if (nmp->nm_readdirsize == 0)
310 nmp->nm_readdirsize = xmax;
311 }
312 /* XXX */
313 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
314 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
315 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
316 nmp->nm_maxfilesize = maxfsize;
317 nmp->nm_mountp->mnt_fs_bshift =
318 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
319 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
320 }
321 nfsm_reqdone;
322 return (error);
323 }
324 #endif
325
326 /*
327 * Mount a remote root fs via. NFS. It goes like this:
328 * - Call nfs_boot_init() to fill in the nfs_diskless struct
329 * - build the rootfs mount point and call mountnfs() to do the rest.
330 */
331 int
332 nfs_mountroot(void)
333 {
334 struct timespec ts;
335 struct nfs_diskless *nd;
336 struct vattr attr;
337 struct mount *mp;
338 struct vnode *vp;
339 struct lwp *l;
340 long n;
341 int error;
342
343 l = curlwp; /* XXX */
344
345 if (device_class(root_device) != DV_IFNET)
346 return (ENODEV);
347
348 /*
349 * XXX time must be non-zero when we init the interface or else
350 * the arp code will wedge. [Fixed now in if_ether.c]
351 * However, the NFS attribute cache gives false "hits" when the
352 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
353 */
354 if (time_second < NFS_MAXATTRTIMO) {
355 ts.tv_sec = NFS_MAXATTRTIMO;
356 ts.tv_nsec = 0;
357 tc_setclock(&ts);
358 }
359
360 /*
361 * Call nfs_boot_init() to fill in the nfs_diskless struct.
362 * Side effect: Finds and configures a network interface.
363 */
364 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
365 error = nfs_boot_init(nd, l);
366 if (error) {
367 kmem_free(nd, sizeof(*nd));
368 return (error);
369 }
370
371 /*
372 * Create the root mount point.
373 */
374 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
375 if (error)
376 goto out;
377 printf("root on %s\n", nd->nd_root.ndm_host);
378
379 /*
380 * Link it into the mount list.
381 */
382 mutex_enter(&mountlist_lock);
383 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
384 mutex_exit(&mountlist_lock);
385 rootvp = vp;
386 mp->mnt_vnodecovered = NULLVP;
387 vfs_unbusy(mp, false, NULL);
388
389 /* Get root attributes (for the time). */
390 vn_lock(vp, LK_SHARED | LK_RETRY);
391 error = VOP_GETATTR(vp, &attr, l->l_cred);
392 VOP_UNLOCK(vp);
393 if (error)
394 panic("nfs_mountroot: getattr for root");
395 n = attr.va_atime.tv_sec;
396 #ifdef DEBUG
397 printf("root time: 0x%lx\n", n);
398 #endif
399 setrootfstime(n);
400
401 out:
402 if (error)
403 nfs_boot_cleanup(nd, l);
404 kmem_free(nd, sizeof(*nd));
405 return (error);
406 }
407
408 /*
409 * Internal version of mount system call for diskless setup.
410 * Separate function because we used to call it twice.
411 * (once for root and once for swap)
412 */
413 static int
414 nfs_mount_diskless(struct nfs_dlmount *ndmntp, const char *mntname, struct mount **mpp, struct vnode **vpp, struct lwp *l)
415 /* mntname: mount point name */
416 {
417 struct mount *mp;
418 struct mbuf *m;
419 int error;
420
421 vfs_rootmountalloc(MOUNT_NFS, mntname, &mp);
422
423 mp->mnt_op = &nfs_vfsops;
424
425 /*
426 * Historical practice expects NFS root file systems to
427 * be initially mounted r/w.
428 */
429 mp->mnt_flag &= ~MNT_RDONLY;
430
431 /* Get mbuf for server sockaddr. */
432 m = m_get(M_WAIT, MT_SONAME);
433 if (m == NULL)
434 panic("nfs_mountroot: mget soname for %s", mntname);
435 MCLAIM(m, &nfs_mowner);
436 memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr,
437 (m->m_len = ndmntp->ndm_args.addr->sa_len));
438
439 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
440 ndmntp->ndm_args.hostname, vpp, l);
441 if (error) {
442 vfs_unbusy(mp, false, NULL);
443 vfs_destroy(mp);
444 printf("nfs_mountroot: mount %s failed: %d\n",
445 mntname, error);
446 } else
447 *mpp = mp;
448
449 return (error);
450 }
451
452 void
453 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, 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 nfh = malloc(NFSX_V3FHMAX, M_TEMP, M_WAITOK);
665 error = copyin(args->fh, nfh, args->fhsize);
666 if (error)
667 goto free_nfh;
668 pth = malloc(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 hst = malloc(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(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, const char *pth, const char *hst, struct vnode **vpp, struct lwp *l)
701 {
702 struct nfsmount *nmp;
703 struct nfsnode *np;
704 struct vnode *vp;
705 int error;
706 struct vattr *attrs;
707 kauth_cred_t cr;
708 char iosname[IOSTATNAMELEN];
709
710 /*
711 * If the number of nfs iothreads to use has never
712 * been set, create a reasonable number of them.
713 */
714
715 if (nfs_niothreads < 0) {
716 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
717 }
718
719 if (mp->mnt_flag & MNT_UPDATE) {
720 nmp = VFSTONFS(mp);
721 /* update paths, file handles, etc, here XXX */
722 m_freem(nam);
723 return (0);
724 } else {
725 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
726 mp->mnt_data = nmp;
727 TAILQ_INIT(&nmp->nm_uidlruhead);
728 TAILQ_INIT(&nmp->nm_bufq);
729 rw_init(&nmp->nm_writeverflock);
730 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
731 rw_init(&nmp->nm_rbtlock);
732 cv_init(&nmp->nm_rcvcv, "nfsrcv");
733 cv_init(&nmp->nm_sndcv, "nfssnd");
734 cv_init(&nmp->nm_aiocv, "nfsaio");
735 cv_init(&nmp->nm_disconcv, "nfsdis");
736 nfs_rbtinit(nmp);
737 }
738 vfs_getnewfsid(mp);
739 nmp->nm_mountp = mp;
740
741 #ifndef NFS_V2_ONLY
742 if ((argp->flags & NFSMNT_NFSV3) == 0)
743 #endif
744 {
745 if (argp->fhsize != NFSX_V2FH) {
746 return EINVAL;
747 }
748 }
749
750 /*
751 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
752 * will overwrite this.
753 */
754 nmp->nm_maxfilesize = 0xffffffffLL;
755
756 nmp->nm_timeo = NFS_TIMEO;
757 nmp->nm_retry = NFS_RETRANS;
758 nmp->nm_wsize = NFS_WSIZE;
759 nmp->nm_rsize = NFS_RSIZE;
760 nmp->nm_readdirsize = NFS_READDIRSIZE;
761 nmp->nm_numgrps = NFS_MAXGRPS;
762 nmp->nm_readahead = NFS_DEFRAHEAD;
763 nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
764 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
765 mp->mnt_op->vfs_name, mp, l);
766 if (error)
767 goto bad;
768 nmp->nm_nam = nam;
769
770 /* Set up the sockets and per-host congestion */
771 nmp->nm_sotype = argp->sotype;
772 nmp->nm_soproto = argp->proto;
773
774 nfs_decode_args(nmp, argp, l);
775
776 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
777 mp->mnt_dev_bshift = DEV_BSHIFT;
778
779 /*
780 * For Connection based sockets (TCP,...) defer the connect until
781 * the first request, in case the server is not responding.
782 */
783 if (nmp->nm_sotype == SOCK_DGRAM &&
784 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
785 goto bad;
786
787 /*
788 * This is silly, but it has to be set so that vinifod() works.
789 * We do not want to do an nfs_statvfs() here since we can get
790 * stuck on a dead server and we are holding a lock on the mount
791 * point.
792 */
793 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
794 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
795 if (error)
796 goto bad;
797 vp = NFSTOV(np);
798 attrs = malloc(sizeof(struct vattr), M_TEMP, M_WAITOK);
799 VOP_GETATTR(vp, attrs, l->l_cred);
800 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
801 cr = kauth_cred_alloc();
802 kauth_cred_setuid(cr, attrs->va_uid);
803 kauth_cred_seteuid(cr, attrs->va_uid);
804 kauth_cred_setsvuid(cr, attrs->va_uid);
805 kauth_cred_setgid(cr, attrs->va_gid);
806 kauth_cred_setegid(cr, attrs->va_gid);
807 kauth_cred_setsvgid(cr, attrs->va_gid);
808 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
809 kauth_cred_free(cr);
810 }
811 free(attrs, M_TEMP);
812
813 /*
814 * A reference count is needed on the nfsnode representing the
815 * remote root. If this object is not persistent, then backward
816 * traversals of the mount point (i.e. "..") will not work if
817 * the nfsnode gets flushed out of the cache. Ufs does not have
818 * this problem, because one can identify root inodes by their
819 * number == ROOTINO (2). So, just unlock, but no rele.
820 */
821
822 nmp->nm_vnode = vp;
823 if (vp->v_type == VNON)
824 vp->v_type = VDIR;
825 vp->v_vflag |= VV_ROOT;
826 VOP_UNLOCK(vp);
827 *vpp = vp;
828
829 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
830 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
831
832 return (0);
833 bad:
834 nfs_disconnect(nmp);
835 rw_destroy(&nmp->nm_writeverflock);
836 rw_destroy(&nmp->nm_rbtlock);
837 mutex_destroy(&nmp->nm_lock);
838 cv_destroy(&nmp->nm_rcvcv);
839 cv_destroy(&nmp->nm_sndcv);
840 cv_destroy(&nmp->nm_aiocv);
841 cv_destroy(&nmp->nm_disconcv);
842 kmem_free(nmp, sizeof(*nmp));
843 m_freem(nam);
844 return (error);
845 }
846
847 /*
848 * unmount system call
849 */
850 int
851 nfs_unmount(struct mount *mp, int mntflags)
852 {
853 struct nfsmount *nmp;
854 struct vnode *vp;
855 int error, flags = 0;
856
857 if (mntflags & MNT_FORCE)
858 flags |= FORCECLOSE;
859 nmp = VFSTONFS(mp);
860 /*
861 * Goes something like this..
862 * - Check for activity on the root vnode (other than ourselves).
863 * - Call vflush() to clear out vnodes for this file system,
864 * except for the root vnode.
865 * - Decrement reference on the vnode representing remote root.
866 * - Close the socket
867 * - Free up the data structures
868 */
869 /*
870 * We need to decrement the ref. count on the nfsnode representing
871 * the remote root. See comment in mountnfs().
872 */
873 vp = nmp->nm_vnode;
874 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
875 if (error != 0)
876 return error;
877
878 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 1) {
879 VOP_UNLOCK(vp);
880 return (EBUSY);
881 }
882
883 error = vflush(mp, vp, flags);
884 if (error) {
885 VOP_UNLOCK(vp);
886 return (error);
887 }
888
889 /*
890 * We are now committed to the unmount; mark the mount structure
891 * as doomed so that any sleepers kicked awake by nfs_disconnect
892 * will go away cleanly.
893 */
894 nmp->nm_iflag |= NFSMNT_DISMNT;
895
896 /*
897 * Clean up the stats... note that we carefully avoid decrementing
898 * nfs_mount_count here for good reason - we may not be unmounting
899 * the last thing mounted.
900 */
901 iostat_free(nmp->nm_stats);
902
903 /*
904 * There is one reference count to get rid of here
905 * (see comment in mountnfs()).
906 */
907 VOP_UNLOCK(vp);
908 vgone(vp);
909 nfs_disconnect(nmp);
910 m_freem(nmp->nm_nam);
911
912 rw_destroy(&nmp->nm_writeverflock);
913 rw_destroy(&nmp->nm_rbtlock);
914 mutex_destroy(&nmp->nm_lock);
915 cv_destroy(&nmp->nm_rcvcv);
916 cv_destroy(&nmp->nm_sndcv);
917 cv_destroy(&nmp->nm_aiocv);
918 cv_destroy(&nmp->nm_disconcv);
919 kmem_free(nmp, sizeof(*nmp));
920 return (0);
921 }
922
923 /*
924 * Return root of a filesystem
925 */
926 int
927 nfs_root(struct mount *mp, struct vnode **vpp)
928 {
929 struct vnode *vp;
930 struct nfsmount *nmp;
931 int error;
932
933 nmp = VFSTONFS(mp);
934 vp = nmp->nm_vnode;
935 vref(vp);
936 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
937 if (error != 0) {
938 vrele(vp);
939 return error;
940 }
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(struct mount *mp, int waitfor, kauth_cred_t cred)
953 {
954 struct vnode *vp, *mvp;
955 int error, allerror = 0;
956
957 /*
958 * Force stale buffer cache information to be flushed.
959 */
960 mvp = vnalloc(mp);
961 loop:
962 /*
963 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
964 * and vclean() can be called indirectly
965 */
966 mutex_enter(&mntvnode_lock);
967 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
968 vmark(mvp, vp);
969 if (vp->v_mount != mp || vismarker(vp))
970 continue;
971 mutex_enter(vp->v_interlock);
972 /* XXX MNT_LAZY cannot be right? */
973 if (waitfor == MNT_LAZY ||
974 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
975 UVM_OBJ_IS_CLEAN(&vp->v_uobj))) {
976 mutex_exit(vp->v_interlock);
977 continue;
978 }
979 mutex_exit(&mntvnode_lock);
980 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
981 if (error != 0) {
982 if (error != ENOENT) {
983 mutex_enter(&mntvnode_lock);
984 continue;
985 }
986 (void)vunmark(mvp);
987 goto loop;
988 }
989 error = VOP_FSYNC(vp, cred,
990 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
991 if (error)
992 allerror = error;
993 vput(vp);
994 mutex_enter(&mntvnode_lock);
995 }
996 mutex_exit(&mntvnode_lock);
997 vnfree(mvp);
998 return (allerror);
999 }
1000
1001 /*
1002 * NFS flat namespace lookup.
1003 * Currently unsupported.
1004 */
1005 /* ARGSUSED */
1006 int
1007 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1008 {
1009
1010 return (EOPNOTSUPP);
1011 }
1012
1013 /*
1014 * Do that sysctl thang...
1015 */
1016 static int
1017 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1018 {
1019 struct sysctlnode node;
1020 int val;
1021 int error;
1022
1023 val = nfs_niothreads;
1024 node = *rnode;
1025 node.sysctl_data = &val;
1026 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1027 if (error || newp == NULL)
1028 return error;
1029
1030 return nfs_set_niothreads(val);
1031 }
1032
1033 static void
1034 nfs_sysctl_init(void)
1035 {
1036
1037 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1038 CTLFLAG_PERMANENT,
1039 CTLTYPE_NODE, "vfs", NULL,
1040 NULL, 0, NULL, 0,
1041 CTL_VFS, CTL_EOL);
1042 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1043 CTLFLAG_PERMANENT,
1044 CTLTYPE_NODE, "nfs",
1045 SYSCTL_DESCR("NFS vfs options"),
1046 NULL, 0, NULL, 0,
1047 CTL_VFS, 2, CTL_EOL);
1048 /*
1049 * XXX the "2" above could be dynamic, thereby eliminating one
1050 * more instance of the "number to vfs" mapping problem, but
1051 * "2" is the order as taken from sys/mount.h
1052 */
1053
1054 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1055 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1056 CTLTYPE_STRUCT, "nfsstats",
1057 SYSCTL_DESCR("NFS operation statistics"),
1058 NULL, 0, &nfsstats, sizeof(nfsstats),
1059 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1060 sysctl_createv(&nfs_clog, 0, NULL, NULL,
1061 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1062 CTLTYPE_INT, "iothreads",
1063 SYSCTL_DESCR("Number of NFS client processes desired"),
1064 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1065 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1066 }
1067
1068 static void
1069 nfs_sysctl_fini(void)
1070 {
1071
1072 sysctl_teardown(&nfs_clog);
1073 }
1074
1075 /* ARGSUSED */
1076 int
1077 nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1078 {
1079 size_t fidsize;
1080 size_t fhsize;
1081 struct nfsnode *np;
1082 int error;
1083 struct vattr va;
1084
1085 fidsize = fid->fid_len;
1086 if (fidsize < sizeof(*fid)) {
1087 return EINVAL;
1088 }
1089 fhsize = fidsize - sizeof(*fid);
1090 if ((fhsize % NFSX_UNSIGNED) != 0) {
1091 return EINVAL;
1092 }
1093 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1094 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1095 return EINVAL;
1096 }
1097 } else {
1098 if (fhsize != NFSX_V2FH) {
1099 return EINVAL;
1100 }
1101 }
1102 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1103 if (error) {
1104 return error;
1105 }
1106 *vpp = NFSTOV(np);
1107 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1108 if (error != 0) {
1109 vput(*vpp);
1110 *vpp = NULLVP;
1111 }
1112 return error;
1113 }
1114
1115 /* ARGSUSED */
1116 int
1117 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1118 {
1119 struct nfsnode *np;
1120 struct fid *fid;
1121 size_t fidsize;
1122 int error = 0;
1123
1124 np = VTONFS(vp);
1125 fidsize = sizeof(*fid) + np->n_fhsize;
1126 if (*bufsize < fidsize) {
1127 error = E2BIG;
1128 }
1129 *bufsize = fidsize;
1130 if (error == 0) {
1131 struct fid fid_store;
1132
1133 fid = &fid_store;
1134 memset(fid, 0, sizeof(*fid));
1135 fid->fid_len = fidsize;
1136 memcpy(buf, fid, sizeof(*fid));
1137 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1138 }
1139 return error;
1140 }
1141
1142 /*
1143 * Vfs start routine, a no-op.
1144 */
1145 /* ARGSUSED */
1146 int
1147 nfs_start(struct mount *mp, int flags)
1148 {
1149
1150 return (0);
1151 }
1152
1153 /*
1154 * Called once at VFS init to initialize client-specific data structures.
1155 */
1156 void
1157 nfs_vfs_init(void)
1158 {
1159
1160 /* Initialize NFS server / client shared data. */
1161 nfs_init();
1162 nfs_node_init();
1163
1164 /* Initialize the kqueue structures */
1165 nfs_kqinit();
1166 /* Initialize the iod structures */
1167 nfs_iodinit();
1168
1169 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
1170 }
1171
1172 void
1173 nfs_vfs_done(void)
1174 {
1175
1176 nfs_node_done();
1177 nfs_kqfini();
1178 nfs_iodfini();
1179 }
1180