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