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