nfs_vfsops.c revision 1.193 1 /* $NetBSD: nfs_vfsops.c,v 1.193 2008/01/28 14:31:20 dholland 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.193 2008/01/28 14:31:20 dholland 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 nfs_vnodeopv_descs,
125 0,
126 { NULL, NULL },
127 };
128 VFS_ATTACH(nfs_vfsops);
129
130 extern u_int32_t nfs_procids[NFS_NPROCS];
131 extern u_int32_t nfs_prog, nfs_vers;
132
133 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
134 struct mount **, struct vnode **, struct lwp *));
135
136 /*
137 * nfs statvfs call
138 */
139 int
140 nfs_statvfs(mp, sbp)
141 struct mount *mp;
142 struct statvfs *sbp;
143 {
144 struct lwp *l = curlwp;
145 struct vnode *vp;
146 struct nfs_statfs *sfp;
147 char *cp;
148 u_int32_t *tl;
149 int32_t t1, t2;
150 char *bpos, *dpos, *cp2;
151 struct nfsmount *nmp = VFSTONFS(mp);
152 int error = 0, retattr;
153 #ifdef NFS_V2_ONLY
154 const int v3 = 0;
155 #else
156 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
157 #endif
158 struct mbuf *mreq, *mrep = NULL, *md, *mb;
159 kauth_cred_t cred;
160 u_quad_t tquad;
161 struct nfsnode *np;
162
163 #ifndef nolint
164 sfp = (struct nfs_statfs *)0;
165 #endif
166 vp = nmp->nm_vnode;
167 np = VTONFS(vp);
168 cred = kauth_cred_alloc();
169 #ifndef NFS_V2_ONLY
170 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
171 (void)nfs_fsinfo(nmp, vp, cred, l);
172 #endif
173 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
174 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
175 nfsm_fhtom(np, v3);
176 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
177 if (v3)
178 nfsm_postop_attr(vp, retattr, 0);
179 if (error) {
180 if (mrep != NULL) {
181 if (mrep->m_next != NULL)
182 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
183 m_freem(mrep);
184 }
185 goto nfsmout;
186 }
187 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
188 sbp->f_flag = nmp->nm_flag;
189 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
190 if (v3) {
191 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
192 tquad = fxdr_hyper(&sfp->sf_tbytes);
193 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
194 tquad = fxdr_hyper(&sfp->sf_fbytes);
195 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
196 tquad = fxdr_hyper(&sfp->sf_abytes);
197 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
198 sbp->f_bresvd = sbp->f_bfree - tquad;
199 sbp->f_bavail = tquad;
200 #ifdef COMPAT_20
201 /* Handle older NFS servers returning negative values */
202 if ((quad_t)sbp->f_bavail < 0)
203 sbp->f_bavail = 0;
204 #endif
205 tquad = fxdr_hyper(&sfp->sf_tfiles);
206 sbp->f_files = tquad;
207 tquad = fxdr_hyper(&sfp->sf_ffiles);
208 sbp->f_ffree = tquad;
209 sbp->f_favail = tquad;
210 sbp->f_fresvd = 0;
211 sbp->f_namemax = MAXNAMLEN;
212 } else {
213 sbp->f_bsize = NFS_FABLKSIZE;
214 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
215 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
216 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
217 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
218 sbp->f_fresvd = 0;
219 sbp->f_files = 0;
220 sbp->f_ffree = 0;
221 sbp->f_favail = 0;
222 sbp->f_fresvd = 0;
223 sbp->f_namemax = MAXNAMLEN;
224 }
225 copy_statvfs_info(sbp, mp);
226 nfsm_reqdone;
227 kauth_cred_free(cred);
228 return (error);
229 }
230
231 #ifndef NFS_V2_ONLY
232 /*
233 * nfs version 3 fsinfo rpc call
234 */
235 int
236 nfs_fsinfo(nmp, vp, cred, l)
237 struct nfsmount *nmp;
238 struct vnode *vp;
239 kauth_cred_t cred;
240 struct lwp *l;
241 {
242 struct nfsv3_fsinfo *fsp;
243 char *cp;
244 int32_t t1, t2;
245 u_int32_t *tl, pref, xmax;
246 char *bpos, *dpos, *cp2;
247 int error = 0, retattr;
248 struct mbuf *mreq, *mrep, *md, *mb;
249 u_int64_t maxfsize;
250 struct nfsnode *np = VTONFS(vp);
251
252 nfsstats.rpccnt[NFSPROC_FSINFO]++;
253 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
254 nfsm_fhtom(np, 1);
255 nfsm_request(np, NFSPROC_FSINFO, l, cred);
256 nfsm_postop_attr(vp, retattr, 0);
257 if (!error) {
258 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
259 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
260 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
261 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
262 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
263 ~(NFS_FABLKSIZE - 1);
264 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
265 if (xmax < nmp->nm_wsize && xmax > 0) {
266 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
267 if (nmp->nm_wsize == 0)
268 nmp->nm_wsize = xmax;
269 }
270 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
271 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
272 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
273 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
274 ~(NFS_FABLKSIZE - 1);
275 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
276 if (xmax < nmp->nm_rsize && xmax > 0) {
277 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
278 if (nmp->nm_rsize == 0)
279 nmp->nm_rsize = xmax;
280 }
281 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
282 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
283 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
284 ~(NFS_DIRFRAGSIZ - 1);
285 if (xmax < nmp->nm_readdirsize && xmax > 0) {
286 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
287 if (nmp->nm_readdirsize == 0)
288 nmp->nm_readdirsize = xmax;
289 }
290 /* XXX */
291 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
292 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
293 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
294 nmp->nm_maxfilesize = maxfsize;
295 nmp->nm_mountp->mnt_fs_bshift =
296 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
297 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
298 }
299 nfsm_reqdone;
300 return (error);
301 }
302 #endif
303
304 /*
305 * Mount a remote root fs via. NFS. It goes like this:
306 * - Call nfs_boot_init() to fill in the nfs_diskless struct
307 * - build the rootfs mount point and call mountnfs() to do the rest.
308 */
309 int
310 nfs_mountroot()
311 {
312 struct timespec ts;
313 struct nfs_diskless *nd;
314 struct vattr attr;
315 struct mount *mp;
316 struct vnode *vp;
317 struct lwp *l;
318 long n;
319 int error;
320
321 l = curlwp; /* XXX */
322
323 if (device_class(root_device) != DV_IFNET)
324 return (ENODEV);
325
326 /*
327 * XXX time must be non-zero when we init the interface or else
328 * the arp code will wedge. [Fixed now in if_ether.c]
329 * However, the NFS attribute cache gives false "hits" when the
330 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
331 */
332 if (time_second < NFS_MAXATTRTIMO) {
333 ts.tv_sec = NFS_MAXATTRTIMO;
334 ts.tv_nsec = 0;
335 tc_setclock(&ts);
336 }
337
338 /*
339 * Call nfs_boot_init() to fill in the nfs_diskless struct.
340 * Side effect: Finds and configures a network interface.
341 */
342 nd = kmem_alloc(sizeof(*nd), KM_SLEEP);
343 memset(nd, 0, sizeof(*nd));
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);
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 mp->mnt_op->vfs_refcount--;
424 vfs_unbusy(mp);
425 printf("nfs_mountroot: mount %s failed: %d\n",
426 mntname, error);
427 free(mp, M_MOUNT);
428 } else
429 *mpp = mp;
430
431 return (error);
432 }
433
434 void
435 nfs_decode_args(nmp, argp, l)
436 struct nfsmount *nmp;
437 struct nfs_args *argp;
438 struct lwp *l;
439 {
440 int s;
441 int adjsock;
442 int maxio;
443
444 s = splsoftnet();
445
446 /*
447 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
448 * no sense in that context.
449 */
450 if (argp->sotype == SOCK_STREAM)
451 argp->flags &= ~NFSMNT_NOCONN;
452
453 /*
454 * Cookie translation is not needed for v2, silently ignore it.
455 */
456 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
457 NFSMNT_XLATECOOKIE)
458 argp->flags &= ~NFSMNT_XLATECOOKIE;
459
460 /* Re-bind if rsrvd port requested and wasn't on one */
461 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
462 && (argp->flags & NFSMNT_RESVPORT);
463 /* Also re-bind if we're switching to/from a connected UDP socket */
464 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
465 (argp->flags & NFSMNT_NOCONN));
466
467 /* Update flags. */
468 nmp->nm_flag = argp->flags;
469 splx(s);
470
471 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
472 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
473 if (nmp->nm_timeo < NFS_MINTIMEO)
474 nmp->nm_timeo = NFS_MINTIMEO;
475 else if (nmp->nm_timeo > NFS_MAXTIMEO)
476 nmp->nm_timeo = NFS_MAXTIMEO;
477 }
478
479 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
480 nmp->nm_retry = argp->retrans;
481 if (nmp->nm_retry > NFS_MAXREXMIT)
482 nmp->nm_retry = NFS_MAXREXMIT;
483 }
484
485 #ifndef NFS_V2_ONLY
486 if (argp->flags & NFSMNT_NFSV3) {
487 if (argp->sotype == SOCK_DGRAM)
488 maxio = NFS_MAXDGRAMDATA;
489 else
490 maxio = NFS_MAXDATA;
491 } else
492 #endif
493 maxio = NFS_V2MAXDATA;
494
495 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
496 int osize = nmp->nm_wsize;
497 nmp->nm_wsize = argp->wsize;
498 /* Round down to multiple of blocksize */
499 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
500 if (nmp->nm_wsize <= 0)
501 nmp->nm_wsize = NFS_FABLKSIZE;
502 adjsock |= (nmp->nm_wsize != osize);
503 }
504 if (nmp->nm_wsize > maxio)
505 nmp->nm_wsize = maxio;
506 if (nmp->nm_wsize > MAXBSIZE)
507 nmp->nm_wsize = MAXBSIZE;
508
509 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
510 int osize = nmp->nm_rsize;
511 nmp->nm_rsize = argp->rsize;
512 /* Round down to multiple of blocksize */
513 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
514 if (nmp->nm_rsize <= 0)
515 nmp->nm_rsize = NFS_FABLKSIZE;
516 adjsock |= (nmp->nm_rsize != osize);
517 }
518 if (nmp->nm_rsize > maxio)
519 nmp->nm_rsize = maxio;
520 if (nmp->nm_rsize > MAXBSIZE)
521 nmp->nm_rsize = MAXBSIZE;
522
523 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
524 nmp->nm_readdirsize = argp->readdirsize;
525 /* Round down to multiple of minimum blocksize */
526 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
527 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
528 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
529 /* Bigger than buffer size makes no sense */
530 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
531 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
532 } else if (argp->flags & NFSMNT_RSIZE)
533 nmp->nm_readdirsize = nmp->nm_rsize;
534
535 if (nmp->nm_readdirsize > maxio)
536 nmp->nm_readdirsize = maxio;
537
538 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
539 argp->maxgrouplist <= NFS_MAXGRPS)
540 nmp->nm_numgrps = argp->maxgrouplist;
541 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
542 argp->readahead <= NFS_MAXRAHEAD)
543 nmp->nm_readahead = argp->readahead;
544 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
545 argp->deadthresh <= NFS_NEVERDEAD)
546 nmp->nm_deadthresh = argp->deadthresh;
547
548 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
549 (nmp->nm_soproto != argp->proto));
550 nmp->nm_sotype = argp->sotype;
551 nmp->nm_soproto = argp->proto;
552
553 if (nmp->nm_so && adjsock) {
554 nfs_safedisconnect(nmp);
555 if (nmp->nm_sotype == SOCK_DGRAM)
556 while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
557 printf("nfs_args: retrying connect\n");
558 kpause("nfscn3", false, hz, NULL);
559 }
560 }
561 }
562
563 /*
564 * VFS Operations.
565 *
566 * mount system call
567 * It seems a bit dumb to copyinstr() the host and path here and then
568 * memcpy() them in mountnfs(), but I wanted to detect errors before
569 * doing the sockargs() call because sockargs() allocates an mbuf and
570 * an error after that means that I have to release the mbuf.
571 */
572 /* ARGSUSED */
573 int
574 nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
575 {
576 struct lwp *l = curlwp;
577 int error;
578 struct nfs_args *args = data;
579 struct mbuf *nam;
580 struct nfsmount *nmp = VFSTONFS(mp);
581 struct sockaddr *sa;
582 struct vnode *vp;
583 char *pth, *hst;
584 struct proc *p;
585 size_t len;
586 u_char *nfh;
587
588 if (*data_len < sizeof *args)
589 return EINVAL;
590
591 p = l->l_proc;
592 if (mp->mnt_flag & MNT_GETARGS) {
593
594 if (nmp == NULL)
595 return (EIO);
596 if (args->addr != NULL) {
597 sa = mtod(nmp->nm_nam, struct sockaddr *);
598 error = copyout(sa, args->addr, sa->sa_len);
599 if (error)
600 return (error);
601 args->addrlen = sa->sa_len;
602 } else
603 args->addrlen = 0;
604
605 args->version = NFS_ARGSVERSION;
606 args->sotype = nmp->nm_sotype;
607 args->proto = nmp->nm_soproto;
608 args->fh = NULL;
609 args->fhsize = 0;
610 args->flags = nmp->nm_flag;
611 args->wsize = nmp->nm_wsize;
612 args->rsize = nmp->nm_rsize;
613 args->readdirsize = nmp->nm_readdirsize;
614 args->timeo = nmp->nm_timeo;
615 args->retrans = nmp->nm_retry;
616 args->maxgrouplist = nmp->nm_numgrps;
617 args->readahead = nmp->nm_readahead;
618 args->leaseterm = 0; /* dummy */
619 args->deadthresh = nmp->nm_deadthresh;
620 args->hostname = NULL;
621 *data_len = sizeof *args;
622 return 0;
623 }
624
625 if (args->version != NFS_ARGSVERSION)
626 return (EPROGMISMATCH);
627 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
628 return (EPROGUNAVAIL);
629 #ifdef NFS_V2_ONLY
630 if (args->flags & NFSMNT_NFSV3)
631 return (EPROGMISMATCH);
632 #endif
633 if (mp->mnt_flag & MNT_UPDATE) {
634 if (nmp == NULL)
635 return (EIO);
636 /*
637 * When doing an update, we can't change from or to
638 * v3, or change cookie translation
639 */
640 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
641 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
642 nfs_decode_args(nmp, args, l);
643 return (0);
644 }
645 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
646 return (EINVAL);
647 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
648 error = copyin(args->fh, nfh, args->fhsize);
649 if (error)
650 goto free_nfh;
651 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
652 error = copyinstr(path, pth, MNAMELEN - 1, &len);
653 if (error)
654 goto free_pth;
655 memset(&pth[len], 0, MNAMELEN - len);
656 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
657 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
658 if (error)
659 goto free_hst;
660 memset(&hst[len], 0, MNAMELEN - len);
661 /* sockargs() call must be after above copyin() calls */
662 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
663 if (error)
664 goto free_hst;
665 MCLAIM(nam, &nfs_mowner);
666 args->fh = nfh;
667 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
668
669 free_hst:
670 FREE(hst, M_TEMP);
671 free_pth:
672 FREE(pth, M_TEMP);
673 free_nfh:
674 FREE(nfh, M_TEMP);
675
676 return (error);
677 }
678
679 /*
680 * Common code for mount and mountroot
681 */
682 int
683 mountnfs(argp, mp, nam, pth, hst, vpp, l)
684 struct nfs_args *argp;
685 struct mount *mp;
686 struct mbuf *nam;
687 const char *pth, *hst;
688 struct vnode **vpp;
689 struct lwp *l;
690 {
691 struct nfsmount *nmp;
692 struct nfsnode *np;
693 int error;
694 struct vattr *attrs;
695 kauth_cred_t cr;
696 char iosname[IOSTATNAMELEN];
697
698 /*
699 * If the number of nfs iothreads to use has never
700 * been set, create a reasonable number of them.
701 */
702
703 if (nfs_niothreads < 0) {
704 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
705 }
706
707 if (mp->mnt_flag & MNT_UPDATE) {
708 nmp = VFSTONFS(mp);
709 /* update paths, file handles, etc, here XXX */
710 m_freem(nam);
711 return (0);
712 } else {
713 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
714 mp->mnt_data = nmp;
715 TAILQ_INIT(&nmp->nm_uidlruhead);
716 TAILQ_INIT(&nmp->nm_bufq);
717 rw_init(&nmp->nm_writeverflock);
718 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
719 cv_init(&nmp->nm_rcvcv, "nfsrcv");
720 cv_init(&nmp->nm_sndcv, "nfssnd");
721 cv_init(&nmp->nm_aiocv, "nfsaio");
722 cv_init(&nmp->nm_disconcv, "nfsdis");
723 }
724 vfs_getnewfsid(mp);
725 nmp->nm_mountp = mp;
726
727 #ifndef NFS_V2_ONLY
728 if ((argp->flags & NFSMNT_NFSV3) == 0)
729 #endif
730 {
731 /*
732 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
733 * will fill this in.
734 */
735 nmp->nm_maxfilesize = 0xffffffffLL;
736 if (argp->fhsize != NFSX_V2FH) {
737 return EINVAL;
738 }
739 }
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