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