nfs_vfsops.c revision 1.241.2.1 1 /* $NetBSD: nfs_vfsops.c,v 1.241.2.1 2021/04/03 22:29:02 thorpej 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.241.2.1 2021/04/03 22:29:02 thorpej 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 } else {
227 sbp->f_bsize = NFS_FABLKSIZE;
228 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
229 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
230 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
231 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
232 sbp->f_fresvd = 0;
233 sbp->f_files = 0;
234 sbp->f_ffree = 0;
235 sbp->f_favail = 0;
236 sbp->f_fresvd = 0;
237 }
238 copy_statvfs_info(sbp, mp);
239 nfsm_reqdone;
240 kauth_cred_free(cred);
241 return (error);
242 }
243
244 #ifndef NFS_V2_ONLY
245 /*
246 * nfs version 3 fsinfo rpc call
247 */
248 int
249 nfs_fsinfo(struct nfsmount *nmp, struct vnode *vp, kauth_cred_t cred, struct lwp *l)
250 {
251 struct nfsv3_fsinfo *fsp;
252 char *cp;
253 int32_t t1, t2;
254 u_int32_t *tl, pref, xmax;
255 char *bpos, *dpos, *cp2;
256 int error = 0, retattr;
257 struct mbuf *mreq, *mrep, *md, *mb;
258 u_int64_t maxfsize;
259 struct nfsnode *np = VTONFS(vp);
260
261 nfsstats.rpccnt[NFSPROC_FSINFO]++;
262 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
263 nfsm_fhtom(np, 1);
264 nfsm_request(np, NFSPROC_FSINFO, l, cred);
265 nfsm_postop_attr(vp, retattr, 0);
266 if (!error) {
267 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
268 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
269 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
270 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
271 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
272 ~(NFS_FABLKSIZE - 1);
273 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
274 if (xmax < nmp->nm_wsize && xmax > 0) {
275 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
276 if (nmp->nm_wsize == 0)
277 nmp->nm_wsize = xmax;
278 }
279 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
280 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
281 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
282 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
283 ~(NFS_FABLKSIZE - 1);
284 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
285 if (xmax < nmp->nm_rsize && xmax > 0) {
286 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
287 if (nmp->nm_rsize == 0)
288 nmp->nm_rsize = xmax;
289 }
290 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
291 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
292 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
293 ~(NFS_DIRFRAGSIZ - 1);
294 if (xmax < nmp->nm_readdirsize && xmax > 0) {
295 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
296 if (nmp->nm_readdirsize == 0)
297 nmp->nm_readdirsize = xmax;
298 }
299 /* XXX */
300 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
301 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
302 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
303 nmp->nm_maxfilesize = maxfsize;
304 nmp->nm_mountp->mnt_fs_bshift =
305 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
306 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
307 }
308 nfsm_reqdone;
309 return (error);
310 }
311 #endif
312
313 /*
314 * Mount a remote root fs via. NFS. It goes like this:
315 * - Call nfs_boot_init() to fill in the nfs_diskless struct
316 * - build the rootfs mount point and call mountnfs() to do the rest.
317 */
318 int
319 nfs_mountroot(void)
320 {
321 struct timespec ts;
322 struct nfs_diskless *nd;
323 struct vattr attr;
324 struct mount *mp;
325 struct vnode *vp;
326 struct lwp *l;
327 long n;
328 int error;
329
330 l = curlwp; /* XXX */
331
332 if (device_class(root_device) != DV_IFNET)
333 return (ENODEV);
334
335 /*
336 * XXX time must be non-zero when we init the interface or else
337 * the arp code will wedge. [Fixed now in if_ether.c]
338 * However, the NFS attribute cache gives false "hits" when the
339 * current time < nfs_attrtimeo(nmp, np) so keep this in for now.
340 */
341 if (time_second < NFS_MAXATTRTIMO) {
342 ts.tv_sec = NFS_MAXATTRTIMO;
343 ts.tv_nsec = 0;
344 tc_setclock(&ts);
345 }
346
347 /*
348 * Call nfs_boot_init() to fill in the nfs_diskless struct.
349 * Side effect: Finds and configures a network interface.
350 */
351 nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
352 error = nfs_boot_init(nd, l);
353 if (error) {
354 kmem_free(nd, sizeof(*nd));
355 return (error);
356 }
357
358 /*
359 * Create the root mount point.
360 */
361 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
362 if (error)
363 goto out;
364 printf("root on %s\n", nd->nd_root.ndm_host);
365
366 /*
367 * Link it into the mount list.
368 */
369 mountlist_append(mp);
370 rootvp = vp;
371 mp->mnt_vnodecovered = NULLVP;
372 vfs_unbusy(mp);
373
374 /* Get root attributes (for the time). */
375 vn_lock(vp, LK_SHARED | LK_RETRY);
376 error = VOP_GETATTR(vp, &attr, l->l_cred);
377 VOP_UNLOCK(vp);
378 if (error)
379 panic("nfs_mountroot: getattr for root");
380 n = attr.va_atime.tv_sec;
381 #ifdef DEBUG
382 printf("root time: 0x%lx\n", n);
383 #endif
384 setrootfstime(n);
385
386 out:
387 if (error)
388 nfs_boot_cleanup(nd, l);
389 kmem_free(nd, sizeof(*nd));
390 return (error);
391 }
392
393 /*
394 * Internal version of mount system call for diskless setup.
395 * Separate function because we used to call it twice.
396 * (once for root and once for swap)
397 */
398 static int
399 nfs_mount_diskless(struct nfs_dlmount *ndmntp, const char *mntname, struct mount **mpp, struct vnode **vpp, struct lwp *l)
400 /* mntname: mount point name */
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 vfs_unbusy(mp);
428 vfs_rele(mp);
429 printf("nfs_mountroot: mount %s failed: %d\n",
430 mntname, error);
431 } else
432 *mpp = mp;
433
434 return (error);
435 }
436
437 void
438 nfs_decode_args(struct nfsmount *nmp, struct nfs_args *argp, 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 size_t len;
585 u_char *nfh;
586
587 if (args == NULL)
588 return EINVAL;
589 if (*data_len < sizeof *args)
590 return EINVAL;
591
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 nfh = malloc(NFSX_V3FHMAX, M_TEMP, M_WAITOK);
648 error = copyin(args->fh, nfh, args->fhsize);
649 if (error)
650 goto free_nfh;
651 pth = malloc(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 hst = malloc(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, UIO_USERSPACE,
663 MT_SONAME);
664 if (error)
665 goto free_hst;
666 MCLAIM(nam, &nfs_mowner);
667 args->fh = nfh;
668 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
669
670 free_hst:
671 free(hst, M_TEMP);
672 free_pth:
673 free(pth, M_TEMP);
674 free_nfh:
675 free(nfh, M_TEMP);
676
677 return (error);
678 }
679
680 /*
681 * Common code for mount and mountroot
682 */
683 int
684 mountnfs(struct nfs_args *argp, struct mount *mp, struct mbuf *nam, const char *pth, const char *hst, struct vnode **vpp, struct lwp *l)
685 {
686 struct nfsmount *nmp;
687 struct nfsnode *np;
688 struct vnode *vp;
689 int error;
690 struct vattr *attrs;
691 kauth_cred_t cr;
692 char iosname[IOSTATNAMELEN];
693
694 /*
695 * If the number of nfs iothreads to use has never
696 * been set, create a reasonable number of them.
697 */
698
699 if (nfs_niothreads < 0) {
700 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
701 }
702
703 if (mp->mnt_flag & MNT_UPDATE) {
704 nmp = VFSTONFS(mp);
705 /* update paths, file handles, etc, here XXX */
706 m_freem(nam);
707 return 0;
708 }
709 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
710 TAILQ_INIT(&nmp->nm_uidlruhead);
711 TAILQ_INIT(&nmp->nm_bufq);
712 rw_init(&nmp->nm_writeverflock);
713 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
714 cv_init(&nmp->nm_rcvcv, "nfsrcv");
715 cv_init(&nmp->nm_sndcv, "nfssnd");
716 cv_init(&nmp->nm_aiocv, "nfsaio");
717 cv_init(&nmp->nm_disconcv, "nfsdis");
718
719 mp->mnt_data = nmp;
720 mp->mnt_stat.f_namemax = NFS_MAXNAMLEN;
721 vfs_getnewfsid(mp);
722 nmp->nm_mountp = mp;
723
724 #ifndef NFS_V2_ONLY
725 if ((argp->flags & NFSMNT_NFSV3) == 0)
726 #endif
727 {
728 if (argp->fhsize != NFSX_V2FH) {
729 return EINVAL;
730 }
731 }
732
733 /*
734 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
735 * will overwrite this.
736 */
737 nmp->nm_maxfilesize = 0xffffffffLL;
738
739 nmp->nm_timeo = NFS_TIMEO;
740 nmp->nm_retry = NFS_RETRANS;
741 nmp->nm_wsize = NFS_WSIZE;
742 nmp->nm_rsize = NFS_RSIZE;
743 nmp->nm_readdirsize = NFS_READDIRSIZE;
744 nmp->nm_numgrps = NFS_MAXGRPS;
745 nmp->nm_readahead = NFS_DEFRAHEAD;
746 nmp->nm_deadthresh = NFS_DEFDEADTHRESH;
747 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE,
748 mp->mnt_op->vfs_name, mp, l);
749 if (error)
750 goto bad;
751 nmp->nm_nam = nam;
752
753 /* Set up the sockets and per-host congestion */
754 nmp->nm_sotype = argp->sotype;
755 nmp->nm_soproto = argp->proto;
756
757 nfs_decode_args(nmp, argp, l);
758
759 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
760 mp->mnt_dev_bshift = DEV_BSHIFT;
761
762 /*
763 * For Connection based sockets (TCP,...) defer the connect until
764 * the first request, in case the server is not responding.
765 */
766 if (nmp->nm_sotype == SOCK_DGRAM &&
767 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
768 goto bad;
769
770 /*
771 * This is silly, but it has to be set so that vinifod() works.
772 * We do not want to do an nfs_statvfs() here since we can get
773 * stuck on a dead server and we are holding a lock on the mount
774 * point.
775 */
776 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
777 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
778 if (error)
779 goto bad;
780 vp = NFSTOV(np);
781 attrs = malloc(sizeof(struct vattr), M_TEMP, M_WAITOK);
782 VOP_GETATTR(vp, attrs, l->l_cred);
783 if ((nmp->nm_flag & NFSMNT_NFSV3) && (vp->v_type == VDIR)) {
784 cr = kauth_cred_alloc();
785 kauth_cred_setuid(cr, attrs->va_uid);
786 kauth_cred_seteuid(cr, attrs->va_uid);
787 kauth_cred_setsvuid(cr, attrs->va_uid);
788 kauth_cred_setgid(cr, attrs->va_gid);
789 kauth_cred_setegid(cr, attrs->va_gid);
790 kauth_cred_setsvgid(cr, attrs->va_gid);
791 nfs_cookieheuristic(vp, &nmp->nm_iflag, l, cr);
792 kauth_cred_free(cr);
793 }
794 free(attrs, M_TEMP);
795
796 /*
797 * A reference count is needed on the nfsnode representing the
798 * remote root. If this object is not persistent, then backward
799 * traversals of the mount point (i.e. "..") will not work if
800 * the nfsnode gets flushed out of the cache. Ufs does not have
801 * this problem, because one can identify root inodes by their
802 * number == UFS_ROOTINO (2). So, just unlock, but no rele.
803 */
804
805 nmp->nm_vnode = vp;
806 if (vp->v_type == VNON)
807 vp->v_type = VDIR;
808 vp->v_vflag |= VV_ROOT;
809 VOP_UNLOCK(vp);
810 *vpp = vp;
811
812 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
813 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
814
815 return (0);
816 bad:
817 nfs_disconnect(nmp);
818 rw_destroy(&nmp->nm_writeverflock);
819 mutex_destroy(&nmp->nm_lock);
820 cv_destroy(&nmp->nm_rcvcv);
821 cv_destroy(&nmp->nm_sndcv);
822 cv_destroy(&nmp->nm_aiocv);
823 cv_destroy(&nmp->nm_disconcv);
824 kmem_free(nmp, sizeof(*nmp));
825 m_freem(nam);
826 return (error);
827 }
828
829 /*
830 * unmount system call
831 */
832 int
833 nfs_unmount(struct mount *mp, int mntflags)
834 {
835 struct nfsmount *nmp = VFSTONFS(mp);
836 struct vnode *vp;
837 int error, flags = 0;
838
839 if (mntflags & MNT_FORCE) {
840 mutex_enter(&nmp->nm_lock);
841 flags |= FORCECLOSE;
842 nmp->nm_iflag |= NFSMNT_DISMNTFORCE;
843 mutex_exit(&nmp->nm_lock);
844
845 }
846
847 /*
848 * Goes something like this..
849 * - Check for activity on the root vnode (other than ourselves).
850 * - Call vflush() to clear out vnodes for this file system,
851 * except for the root vnode.
852 * - Decrement reference on the vnode representing remote root.
853 * - Close the socket
854 * - Free up the data structures
855 */
856 /*
857 * We need to decrement the ref. count on the nfsnode representing
858 * the remote root. See comment in mountnfs().
859 */
860 vp = nmp->nm_vnode;
861 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
862 if (error != 0)
863 goto err;
864
865 if ((mntflags & MNT_FORCE) == 0 && vrefcnt(vp) > 1) {
866 VOP_UNLOCK(vp);
867 error = EBUSY;
868 goto err;
869 }
870
871 error = vflush(mp, vp, flags);
872 if (error) {
873 VOP_UNLOCK(vp);
874 goto err;
875 }
876
877 /*
878 * We are now committed to the unmount; mark the mount structure
879 * as doomed so that any sleepers kicked awake by nfs_disconnect
880 * will go away cleanly.
881 */
882 nmp->nm_iflag |= NFSMNT_DISMNT;
883
884 /*
885 * No new async I/O will be added, but await for pending
886 * ones to drain.
887 */
888 while (nfs_iodbusy(nmp))
889 kpause("nfsumnt", false, hz, NULL);
890
891 /*
892 * Clean up the stats... note that we carefully avoid decrementing
893 * nfs_mount_count here for good reason - we may not be unmounting
894 * the last thing mounted.
895 */
896 iostat_free(nmp->nm_stats);
897
898 /*
899 * There is one reference count to get rid of here
900 * (see comment in mountnfs()).
901 */
902 VOP_UNLOCK(vp);
903 vgone(vp);
904 nfs_disconnect(nmp);
905 m_freem(nmp->nm_nam);
906
907 rw_destroy(&nmp->nm_writeverflock);
908 mutex_destroy(&nmp->nm_lock);
909 cv_destroy(&nmp->nm_rcvcv);
910 cv_destroy(&nmp->nm_sndcv);
911 cv_destroy(&nmp->nm_aiocv);
912 cv_destroy(&nmp->nm_disconcv);
913 kmem_free(nmp, sizeof(*nmp));
914 return (0);
915
916 err:
917 if (mntflags & MNT_FORCE) {
918 mutex_enter(&nmp->nm_lock);
919 nmp->nm_iflag &= ~NFSMNT_DISMNTFORCE;
920 mutex_exit(&nmp->nm_lock);
921 }
922
923 return error;
924 }
925
926 /*
927 * Return root of a filesystem
928 */
929 int
930 nfs_root(struct mount *mp, int lktype, struct vnode **vpp)
931 {
932 struct vnode *vp;
933 struct nfsmount *nmp;
934 int error;
935
936 nmp = VFSTONFS(mp);
937 vp = nmp->nm_vnode;
938 vref(vp);
939 error = vn_lock(vp, lktype | LK_RETRY);
940 if (error != 0) {
941 vrele(vp);
942 return error;
943 }
944 *vpp = vp;
945 return (0);
946 }
947
948 extern int syncprt;
949
950 static bool
951 nfs_sync_selector(void *cl, struct vnode *vp)
952 {
953
954 KASSERT(mutex_owned(vp->v_interlock));
955
956 return !LIST_EMPTY(&vp->v_dirtyblkhd) ||
957 (vp->v_iflag & VI_ONWORKLST) != 0;
958 }
959
960 /*
961 * Flush out the buffer cache
962 */
963 /* ARGSUSED */
964 int
965 nfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
966 {
967 struct vnode *vp;
968 struct vnode_iterator *marker;
969 int error, allerror = 0;
970
971 /*
972 * Force stale buffer cache information to be flushed.
973 */
974 vfs_vnode_iterator_init(mp, &marker);
975 while ((vp = vfs_vnode_iterator_next(marker, nfs_sync_selector,
976 NULL)))
977 {
978 error = vn_lock(vp, LK_EXCLUSIVE);
979 if (error) {
980 vrele(vp);
981 continue;
982 }
983 error = VOP_FSYNC(vp, cred,
984 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
985 if (error)
986 allerror = error;
987 vput(vp);
988 }
989 vfs_vnode_iterator_destroy(marker);
990 return allerror;
991 }
992
993 /*
994 * NFS flat namespace lookup.
995 * Currently unsupported.
996 */
997 /* ARGSUSED */
998 int
999 nfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
1000 {
1001
1002 return (EOPNOTSUPP);
1003 }
1004
1005 /*
1006 * Do that sysctl thang...
1007 */
1008 static int
1009 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1010 {
1011 struct sysctlnode node;
1012 int val;
1013 int error;
1014
1015 val = nfs_niothreads;
1016 node = *rnode;
1017 node.sysctl_data = &val;
1018 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1019 if (error || newp == NULL)
1020 return error;
1021
1022 return nfs_set_niothreads(val);
1023 }
1024
1025 SYSCTL_SETUP(nfs_sysctl_init, "nfs sysctl")
1026 {
1027
1028 sysctl_createv(clog, 0, NULL, NULL,
1029 CTLFLAG_PERMANENT,
1030 CTLTYPE_NODE, "nfs",
1031 SYSCTL_DESCR("NFS vfs options"),
1032 NULL, 0, NULL, 0,
1033 CTL_VFS, 2, CTL_EOL);
1034 /*
1035 * XXX the "2" above could be dynamic, thereby eliminating one
1036 * more instance of the "number to vfs" mapping problem, but
1037 * "2" is the order as taken from sys/mount.h
1038 */
1039
1040 sysctl_createv(clog, 0, NULL, NULL,
1041 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1042 CTLTYPE_STRUCT, "nfsstats",
1043 SYSCTL_DESCR("NFS operation statistics"),
1044 NULL, 0, &nfsstats, sizeof(nfsstats),
1045 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1046 sysctl_createv(clog, 0, NULL, NULL,
1047 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1048 CTLTYPE_INT, "iothreads",
1049 SYSCTL_DESCR("Number of NFS client processes desired"),
1050 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1051 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1052 }
1053
1054 /* ARGSUSED */
1055 int
1056 nfs_fhtovp(struct mount *mp, struct fid *fid, int lktype, struct vnode **vpp)
1057 {
1058 size_t fidsize;
1059 size_t fhsize;
1060 struct nfsnode *np;
1061 int error;
1062 struct vattr va;
1063
1064 fidsize = fid->fid_len;
1065 if (fidsize < sizeof(*fid)) {
1066 return EINVAL;
1067 }
1068 fhsize = fidsize - sizeof(*fid);
1069 if ((fhsize % NFSX_UNSIGNED) != 0) {
1070 return EINVAL;
1071 }
1072 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1073 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1074 return EINVAL;
1075 }
1076 } else {
1077 if (fhsize != NFSX_V2FH) {
1078 return EINVAL;
1079 }
1080 }
1081 /* XXX lktype ignored */
1082 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1083 if (error) {
1084 return error;
1085 }
1086 *vpp = NFSTOV(np);
1087 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1088 if (error != 0) {
1089 vput(*vpp);
1090 *vpp = NULLVP;
1091 }
1092 return error;
1093 }
1094
1095 /* ARGSUSED */
1096 int
1097 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1098 {
1099 struct nfsnode *np;
1100 struct fid *fid;
1101 size_t fidsize;
1102 int error = 0;
1103
1104 np = VTONFS(vp);
1105 fidsize = sizeof(*fid) + np->n_fhsize;
1106 if (*bufsize < fidsize) {
1107 error = E2BIG;
1108 }
1109 *bufsize = fidsize;
1110 if (error == 0) {
1111 struct fid fid_store;
1112
1113 fid = &fid_store;
1114 memset(fid, 0, sizeof(*fid));
1115 fid->fid_len = fidsize;
1116 memcpy(buf, fid, sizeof(*fid));
1117 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1118 }
1119 return error;
1120 }
1121
1122 /*
1123 * Vfs start routine, a no-op.
1124 */
1125 /* ARGSUSED */
1126 int
1127 nfs_start(struct mount *mp, int flags)
1128 {
1129
1130 return (0);
1131 }
1132
1133 /*
1134 * Called once at VFS init to initialize client-specific data structures.
1135 */
1136 void
1137 nfs_vfs_init(void)
1138 {
1139
1140 /* Initialize NFS server / client shared data. */
1141 nfs_init();
1142 nfs_node_init();
1143
1144 /* Initialize the kqueue structures */
1145 nfs_kqinit();
1146 /* Initialize the iod structures */
1147 nfs_iodinit();
1148
1149 nfs_commitsize = uvmexp.npages << (PAGE_SHIFT - 4);
1150 }
1151
1152 void
1153 nfs_vfs_done(void)
1154 {
1155
1156 nfs_node_done();
1157 nfs_kqfini();
1158 nfs_iodfini();
1159 nfs_fini();
1160 }
1161