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