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