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