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