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