nfs_vfsops.c revision 1.170 1 /* $NetBSD: nfs_vfsops.c,v 1.170 2006/12/27 12:51:22 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.170 2006/12/27 12:51:22 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 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
88
89 /*
90 * nfs vfs operations.
91 */
92
93 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
94 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
95 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
96
97 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
98 &nfsv2_vnodeop_opv_desc,
99 &spec_nfsv2nodeop_opv_desc,
100 &fifo_nfsv2nodeop_opv_desc,
101 NULL,
102 };
103
104 struct vfsops nfs_vfsops = {
105 MOUNT_NFS,
106 nfs_mount,
107 nfs_start,
108 nfs_unmount,
109 nfs_root,
110 nfs_quotactl,
111 nfs_statvfs,
112 nfs_sync,
113 nfs_vget,
114 nfs_fhtovp,
115 nfs_vptofh,
116 nfs_vfs_init,
117 nfs_vfs_reinit,
118 nfs_vfs_done,
119 nfs_mountroot,
120 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
121 vfs_stdextattrctl,
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, l)
139 struct mount *mp;
140 struct statvfs *sbp;
141 struct lwp *l;
142 {
143 struct vnode *vp;
144 struct nfs_statfs *sfp;
145 caddr_t cp;
146 u_int32_t *tl;
147 int32_t t1, t2;
148 caddr_t 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 caddr_t cp;
242 int32_t t1, t2;
243 u_int32_t *tl, pref, xmax;
244 caddr_t 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 = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
347 memset((caddr_t)nd, 0, sizeof(*nd));
348 error = nfs_boot_init(nd, l);
349 if (error) {
350 free(nd, M_NFSMNT);
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 simple_lock(&mountlist_slock);
366 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
367 simple_unlock(&mountlist_slock);
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, l);
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 free(nd, M_NFSMNT);
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, caddr_t), (caddr_t)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 (void) tsleep((caddr_t)&lbolt,
563 PSOCK, "nfscn3", 0);
564 }
565 }
566 }
567
568 /*
569 * VFS Operations.
570 *
571 * mount system call
572 * It seems a bit dumb to copyinstr() the host and path here and then
573 * memcpy() them in mountnfs(), but I wanted to detect errors before
574 * doing the sockargs() call because sockargs() allocates an mbuf and
575 * an error after that means that I have to release the mbuf.
576 */
577 /* ARGSUSED */
578 int
579 nfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp,
580 struct lwp *l)
581 {
582 int error;
583 struct nfs_args args;
584 struct mbuf *nam;
585 struct nfsmount *nmp = VFSTONFS(mp);
586 struct sockaddr *sa;
587 struct vnode *vp;
588 char *pth, *hst;
589 struct proc *p;
590 size_t len;
591 u_char *nfh;
592
593 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
594 if (error)
595 return (error);
596
597 p = l->l_proc;
598 if (mp->mnt_flag & MNT_GETARGS) {
599
600 if (nmp == NULL)
601 return (EIO);
602 if (args.addr != NULL) {
603 sa = mtod(nmp->nm_nam, struct sockaddr *);
604 error = copyout(sa, args.addr, sa->sa_len);
605 if (error)
606 return (error);
607 args.addrlen = sa->sa_len;
608 } else
609 args.addrlen = 0;
610
611 args.version = NFS_ARGSVERSION;
612 args.sotype = nmp->nm_sotype;
613 args.proto = nmp->nm_soproto;
614 args.fh = NULL;
615 args.fhsize = 0;
616 args.flags = nmp->nm_flag;
617 args.wsize = nmp->nm_wsize;
618 args.rsize = nmp->nm_rsize;
619 args.readdirsize = nmp->nm_readdirsize;
620 args.timeo = nmp->nm_timeo;
621 args.retrans = nmp->nm_retry;
622 args.maxgrouplist = nmp->nm_numgrps;
623 args.readahead = nmp->nm_readahead;
624 args.leaseterm = 0; /* dummy */
625 args.deadthresh = nmp->nm_deadthresh;
626 args.hostname = NULL;
627 return (copyout(&args, data, sizeof(args)));
628 }
629
630 if (args.version != NFS_ARGSVERSION)
631 return (EPROGMISMATCH);
632 if (args.flags & (NFSMNT_NQNFS|NFSMNT_KERB))
633 return (EPROGUNAVAIL);
634 #ifdef NFS_V2_ONLY
635 if (args.flags & NFSMNT_NFSV3)
636 return (EPROGMISMATCH);
637 #endif
638 if (mp->mnt_flag & MNT_UPDATE) {
639 if (nmp == NULL)
640 return (EIO);
641 /*
642 * When doing an update, we can't change from or to
643 * v3, or change cookie translation
644 */
645 args.flags = (args.flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
646 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
647 nfs_decode_args(nmp, &args, l);
648 return (0);
649 }
650 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
651 return (EINVAL);
652 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
653 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
654 if (error)
655 return (error);
656 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
657 error = copyinstr(path, pth, MNAMELEN - 1, &len);
658 if (error)
659 goto free_nfh;
660 memset(&pth[len], 0, MNAMELEN - len);
661 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
662 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
663 if (error)
664 goto free_pth;
665 memset(&hst[len], 0, MNAMELEN - len);
666 /* sockargs() call must be after above copyin() calls */
667 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
668 if (error)
669 goto free_hst;
670 MCLAIM(nam, &nfs_mowner);
671 args.fh = nfh;
672 error = mountnfs(&args, mp, nam, pth, hst, &vp, l);
673
674 free_hst:
675 FREE(hst, M_TEMP);
676 free_pth:
677 FREE(pth, M_TEMP);
678 free_nfh:
679 FREE(nfh, M_TEMP);
680
681 return (error);
682 }
683
684 /*
685 * Common code for mount and mountroot
686 */
687 int
688 mountnfs(argp, mp, nam, pth, hst, vpp, l)
689 struct nfs_args *argp;
690 struct mount *mp;
691 struct mbuf *nam;
692 const char *pth, *hst;
693 struct vnode **vpp;
694 struct lwp *l;
695 {
696 struct nfsmount *nmp;
697 struct nfsnode *np;
698 int error;
699 struct vattr *attrs;
700 kauth_cred_t cr;
701 char iosname[IOSTATNAMELEN];
702
703 /*
704 * If the number of nfs iothreads to use has never
705 * been set, create a reasonable number of them.
706 */
707
708 if (nfs_niothreads < 0) {
709 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
710 nfs_getset_niothreads(TRUE);
711 }
712
713 if (mp->mnt_flag & MNT_UPDATE) {
714 nmp = VFSTONFS(mp);
715 /* update paths, file handles, etc, here XXX */
716 m_freem(nam);
717 return (0);
718 } else {
719 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
720 M_NFSMNT, M_WAITOK);
721 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
722 mp->mnt_data = nmp;
723 TAILQ_INIT(&nmp->nm_uidlruhead);
724 TAILQ_INIT(&nmp->nm_bufq);
725 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
726 simple_lock_init(&nmp->nm_slock);
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, mp, l);
754 if (error)
755 goto bad;
756 nmp->nm_nam = nam;
757
758 /* Set up the sockets and per-host congestion */
759 nmp->nm_sotype = argp->sotype;
760 nmp->nm_soproto = argp->proto;
761
762 nfs_decode_args(nmp, argp, l);
763
764 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
765 mp->mnt_dev_bshift = DEV_BSHIFT;
766
767 /*
768 * For Connection based sockets (TCP,...) defer the connect until
769 * the first request, in case the server is not responding.
770 */
771 if (nmp->nm_sotype == SOCK_DGRAM &&
772 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
773 goto bad;
774
775 /*
776 * This is silly, but it has to be set so that vinifod() works.
777 * We do not want to do an nfs_statvfs() here since we can get
778 * stuck on a dead server and we are holding a lock on the mount
779 * point.
780 */
781 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
782 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
783 if (error)
784 goto bad;
785 *vpp = NFSTOV(np);
786 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
787 VOP_GETATTR(*vpp, attrs, l->l_cred, l);
788 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
789 cr = kauth_cred_alloc();
790 kauth_cred_setuid(cr, attrs->va_uid);
791 kauth_cred_seteuid(cr, attrs->va_uid);
792 kauth_cred_setsvuid(cr, attrs->va_uid);
793 kauth_cred_setgid(cr, attrs->va_gid);
794 kauth_cred_setegid(cr, attrs->va_gid);
795 kauth_cred_setsvgid(cr, attrs->va_gid);
796 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, l, cr);
797 kauth_cred_free(cr);
798 }
799 FREE(attrs, M_TEMP);
800
801 /*
802 * A reference count is needed on the nfsnode representing the
803 * remote root. If this object is not persistent, then backward
804 * traversals of the mount point (i.e. "..") will not work if
805 * the nfsnode gets flushed out of the cache. Ufs does not have
806 * this problem, because one can identify root inodes by their
807 * number == ROOTINO (2). So, just unlock, but no rele.
808 */
809
810 nmp->nm_vnode = *vpp;
811 VOP_UNLOCK(*vpp, 0);
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 free((caddr_t)nmp, M_NFSMNT);
820 m_freem(nam);
821 return (error);
822 }
823
824 /*
825 * unmount system call
826 */
827 int
828 nfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
829 {
830 struct nfsmount *nmp;
831 struct vnode *vp;
832 int error, flags = 0;
833
834 if (mntflags & MNT_FORCE)
835 flags |= FORCECLOSE;
836 nmp = VFSTONFS(mp);
837 /*
838 * Goes something like this..
839 * - Check for activity on the root vnode (other than ourselves).
840 * - Call vflush() to clear out vnodes for this file system,
841 * except for the root vnode.
842 * - Decrement reference on the vnode representing remote root.
843 * - Close the socket
844 * - Free up the data structures
845 */
846 /*
847 * We need to decrement the ref. count on the nfsnode representing
848 * the remote root. See comment in mountnfs(). The VFS unmount()
849 * has done vput on this vnode, otherwise we would get deadlock!
850 */
851 vp = nmp->nm_vnode;
852 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
853 if (error != 0)
854 return error;
855
856 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
857 vput(vp);
858 return (EBUSY);
859 }
860
861 error = vflush(mp, vp, flags);
862 if (error) {
863 vput(vp);
864 return (error);
865 }
866
867 /*
868 * We are now committed to the unmount; mark the mount structure
869 * as doomed so that any sleepers kicked awake by nfs_disconnect
870 * will go away cleanly.
871 */
872 nmp->nm_iflag |= NFSMNT_DISMNT;
873
874 /*
875 * Clean up the stats... note that we carefully avoid decrementing
876 * nfs_mount_count here for good reason - we may not be unmounting
877 * the last thing mounted.
878 */
879 iostat_free(nmp->nm_stats);
880
881 /*
882 * There are two reference counts to get rid of here
883 * (see comment in mountnfs()).
884 */
885 vrele(vp);
886 vput(vp);
887 vgone(vp);
888 nfs_disconnect(nmp);
889 m_freem(nmp->nm_nam);
890
891 free(nmp, M_NFSMNT);
892 return (0);
893 }
894
895 /*
896 * Return root of a filesystem
897 */
898 int
899 nfs_root(mp, vpp)
900 struct mount *mp;
901 struct vnode **vpp;
902 {
903 struct vnode *vp;
904 struct nfsmount *nmp;
905 int error;
906
907 nmp = VFSTONFS(mp);
908 vp = nmp->nm_vnode;
909 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
910 if (error != 0)
911 return error;
912 if (vp->v_type == VNON)
913 vp->v_type = VDIR;
914 vp->v_flag = VROOT;
915 *vpp = vp;
916 return (0);
917 }
918
919 extern int syncprt;
920
921 /*
922 * Flush out the buffer cache
923 */
924 /* ARGSUSED */
925 int
926 nfs_sync(mp, waitfor, cred, l)
927 struct mount *mp;
928 int waitfor;
929 kauth_cred_t cred;
930 struct lwp *l;
931 {
932 struct vnode *vp, *nvp;
933 int error, allerror = 0;
934
935 /*
936 * Force stale buffer cache information to be flushed.
937 */
938 loop:
939 /*
940 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
941 * and vclean() can be called indirectly
942 */
943 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
944 /*
945 * If the vnode that we are about to sync is no longer
946 * associated with this mount point, start over.
947 */
948 if (vp->v_mount != mp)
949 goto loop;
950 nvp = TAILQ_NEXT(vp, v_mntvnodes);
951 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
952 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
953 vp->v_uobj.uo_npages == 0))
954 continue;
955 if (vget(vp, LK_EXCLUSIVE))
956 goto loop;
957 error = VOP_FSYNC(vp, cred,
958 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
959 if (error)
960 allerror = error;
961 vput(vp);
962 }
963 return (allerror);
964 }
965
966 /*
967 * NFS flat namespace lookup.
968 * Currently unsupported.
969 */
970 /* ARGSUSED */
971 int
972 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
973 {
974
975 return (EOPNOTSUPP);
976 }
977
978 /*
979 * Do that sysctl thang...
980 */
981 static int
982 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
983 {
984 int error;
985
986 nfs_getset_niothreads(0);
987 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
988 if (error || newp == NULL)
989 return (error);
990 nfs_getset_niothreads(1);
991
992 return (0);
993 }
994
995 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
996 {
997
998 sysctl_createv(clog, 0, NULL, NULL,
999 CTLFLAG_PERMANENT,
1000 CTLTYPE_NODE, "vfs", NULL,
1001 NULL, 0, NULL, 0,
1002 CTL_VFS, CTL_EOL);
1003 sysctl_createv(clog, 0, NULL, NULL,
1004 CTLFLAG_PERMANENT,
1005 CTLTYPE_NODE, "nfs",
1006 SYSCTL_DESCR("NFS vfs options"),
1007 NULL, 0, NULL, 0,
1008 CTL_VFS, 2, CTL_EOL);
1009 /*
1010 * XXX the "2" above could be dynamic, thereby eliminating one
1011 * more instance of the "number to vfs" mapping problem, but
1012 * "2" is the order as taken from sys/mount.h
1013 */
1014
1015 sysctl_createv(clog, 0, NULL, NULL,
1016 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1017 CTLTYPE_STRUCT, "nfsstats",
1018 SYSCTL_DESCR("NFS operation statistics"),
1019 NULL, 0, &nfsstats, sizeof(nfsstats),
1020 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1021 sysctl_createv(clog, 0, NULL, NULL,
1022 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1023 CTLTYPE_INT, "iothreads",
1024 SYSCTL_DESCR("Number of NFS client processes desired"),
1025 sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1026 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1027 }
1028
1029 /* ARGSUSED */
1030 int
1031 nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1032 {
1033 size_t fidsize;
1034 size_t fhsize;
1035 struct nfsnode *np;
1036 int error;
1037 struct vattr va;
1038
1039 fidsize = fid->fid_len;
1040 if (fidsize < sizeof(*fid)) {
1041 return EINVAL;
1042 }
1043 fhsize = fidsize - sizeof(*fid);
1044 if ((fhsize % NFSX_UNSIGNED) != 0) {
1045 return EINVAL;
1046 }
1047 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1048 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1049 return EINVAL;
1050 }
1051 } else {
1052 if (fhsize != NFSX_V2FH) {
1053 return EINVAL;
1054 }
1055 }
1056 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1057 if (error) {
1058 return error;
1059 }
1060 *vpp = NFSTOV(np);
1061 error = VOP_GETATTR(*vpp, &va, kauth_cred_get(), curlwp);
1062 if (error != 0) {
1063 vput(*vpp);
1064 }
1065 return error;
1066 }
1067
1068 /* ARGSUSED */
1069 int
1070 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1071 {
1072 struct nfsnode *np;
1073 struct fid *fid;
1074 size_t fidsize;
1075 int error = 0;
1076
1077 np = VTONFS(vp);
1078 fidsize = sizeof(*fid) + np->n_fhsize;
1079 if (*bufsize < fidsize) {
1080 error = E2BIG;
1081 }
1082 *bufsize = fidsize;
1083 if (error == 0) {
1084 struct fid fid_store;
1085
1086 fid = &fid_store;
1087 memset(fid, 0, sizeof(*fid));
1088 fid->fid_len = fidsize;
1089 memcpy(buf, fid, sizeof(*fid));
1090 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1091 }
1092 return error;
1093 }
1094
1095 /*
1096 * Vfs start routine, a no-op.
1097 */
1098 /* ARGSUSED */
1099 int
1100 nfs_start(struct mount *mp, int flags, struct lwp *l)
1101 {
1102
1103 return (0);
1104 }
1105
1106 /*
1107 * Do operations associated with quotas, not supported
1108 */
1109 /* ARGSUSED */
1110 int
1111 nfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
1112 {
1113
1114 return (EOPNOTSUPP);
1115 }
1116