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