nfs_vfsops.c revision 1.148.2.10 1 /* $NetBSD: nfs_vfsops.c,v 1.148.2.10 2008/02/15 10:40:08 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.148.2.10 2008/02/15 10:40:08 yamt Exp $");
39
40 #if defined(_KERNEL_OPT)
41 #include "opt_compat_netbsd.h"
42 #include "opt_nfs.h"
43 #endif
44
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47 #include <sys/signal.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/device.h>
51 #include <sys/vnode.h>
52 #include <sys/kernel.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/mbuf.h>
56 #include <sys/dirent.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/timetc.h>
62 #include <sys/kauth.h>
63
64 #include <net/if.h>
65 #include <net/route.h>
66 #include <netinet/in.h>
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfsnode.h>
71 #include <nfs/nfs.h>
72 #include <nfs/nfsmount.h>
73 #include <nfs/xdr_subs.h>
74 #include <nfs/nfsm_subs.h>
75 #include <nfs/nfsdiskless.h>
76 #include <nfs/nfs_var.h>
77
78 extern struct nfsstats nfsstats;
79 extern int nfs_ticks;
80
81 /*
82 * keep a count of the nfs mounts to generate ficticious drive names
83 * for the per drive stats.
84 */
85 unsigned int nfs_mount_count = 0;
86
87 /*
88 * nfs vfs operations.
89 */
90
91 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
92 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
93 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
94
95 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
96 &nfsv2_vnodeop_opv_desc,
97 &spec_nfsv2nodeop_opv_desc,
98 &fifo_nfsv2nodeop_opv_desc,
99 NULL,
100 };
101
102 struct vfsops nfs_vfsops = {
103 MOUNT_NFS,
104 sizeof (struct nfs_args),
105 nfs_mount,
106 nfs_start,
107 nfs_unmount,
108 nfs_root,
109 (void *)eopnotsupp, /* vfs_quotactl */
110 nfs_statvfs,
111 nfs_sync,
112 nfs_vget,
113 nfs_fhtovp,
114 nfs_vptofh,
115 nfs_vfs_init,
116 nfs_vfs_reinit,
117 nfs_vfs_done,
118 nfs_mountroot,
119 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
120 vfs_stdextattrctl,
121 (void *)eopnotsupp, /* vfs_suspendctl */
122 genfs_renamelock_enter,
123 genfs_renamelock_exit,
124 nfs_vnodeopv_descs,
125 0,
126 { NULL, NULL },
127 };
128 VFS_ATTACH(nfs_vfsops);
129
130 extern u_int32_t nfs_procids[NFS_NPROCS];
131 extern u_int32_t nfs_prog, nfs_vers;
132
133 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
134 struct mount **, struct vnode **, struct lwp *));
135
136 /*
137 * nfs statvfs call
138 */
139 int
140 nfs_statvfs(mp, sbp)
141 struct mount *mp;
142 struct statvfs *sbp;
143 {
144 struct lwp *l = curlwp;
145 struct vnode *vp;
146 struct nfs_statfs *sfp;
147 char *cp;
148 u_int32_t *tl;
149 int32_t t1, t2;
150 char *bpos, *dpos, *cp2;
151 struct nfsmount *nmp = VFSTONFS(mp);
152 int error = 0, retattr;
153 #ifdef NFS_V2_ONLY
154 const int v3 = 0;
155 #else
156 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
157 #endif
158 struct mbuf *mreq, *mrep = NULL, *md, *mb;
159 kauth_cred_t cred;
160 u_quad_t tquad;
161 struct nfsnode *np;
162
163 #ifndef nolint
164 sfp = (struct nfs_statfs *)0;
165 #endif
166 vp = nmp->nm_vnode;
167 np = VTONFS(vp);
168 cred = kauth_cred_alloc();
169 #ifndef NFS_V2_ONLY
170 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
171 (void)nfs_fsinfo(nmp, vp, cred, l);
172 #endif
173 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
174 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
175 nfsm_fhtom(np, v3);
176 nfsm_request(np, NFSPROC_FSSTAT, l, cred);
177 if (v3)
178 nfsm_postop_attr(vp, retattr, 0);
179 if (error) {
180 if (mrep != NULL) {
181 if (mrep->m_next != NULL)
182 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
183 m_freem(mrep);
184 }
185 goto nfsmout;
186 }
187 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
188 sbp->f_flag = nmp->nm_flag;
189 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
190 if (v3) {
191 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
192 tquad = fxdr_hyper(&sfp->sf_tbytes);
193 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
194 tquad = fxdr_hyper(&sfp->sf_fbytes);
195 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
196 tquad = fxdr_hyper(&sfp->sf_abytes);
197 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
198 sbp->f_bresvd = sbp->f_bfree - tquad;
199 sbp->f_bavail = tquad;
200 #ifdef COMPAT_20
201 /* Handle older NFS servers returning negative values */
202 if ((quad_t)sbp->f_bavail < 0)
203 sbp->f_bavail = 0;
204 #endif
205 tquad = fxdr_hyper(&sfp->sf_tfiles);
206 sbp->f_files = tquad;
207 tquad = fxdr_hyper(&sfp->sf_ffiles);
208 sbp->f_ffree = tquad;
209 sbp->f_favail = tquad;
210 sbp->f_fresvd = 0;
211 sbp->f_namemax = MAXNAMLEN;
212 } else {
213 sbp->f_bsize = NFS_FABLKSIZE;
214 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
215 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
216 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
217 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
218 sbp->f_fresvd = 0;
219 sbp->f_files = 0;
220 sbp->f_ffree = 0;
221 sbp->f_favail = 0;
222 sbp->f_fresvd = 0;
223 sbp->f_namemax = MAXNAMLEN;
224 }
225 copy_statvfs_info(sbp, mp);
226 nfsm_reqdone;
227 kauth_cred_free(cred);
228 return (error);
229 }
230
231 #ifndef NFS_V2_ONLY
232 /*
233 * nfs version 3 fsinfo rpc call
234 */
235 int
236 nfs_fsinfo(nmp, vp, cred, l)
237 struct nfsmount *nmp;
238 struct vnode *vp;
239 kauth_cred_t cred;
240 struct lwp *l;
241 {
242 struct nfsv3_fsinfo *fsp;
243 char *cp;
244 int32_t t1, t2;
245 u_int32_t *tl, pref, xmax;
246 char *bpos, *dpos, *cp2;
247 int error = 0, retattr;
248 struct mbuf *mreq, *mrep, *md, *mb;
249 u_int64_t maxfsize;
250 struct nfsnode *np = VTONFS(vp);
251
252 nfsstats.rpccnt[NFSPROC_FSINFO]++;
253 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
254 nfsm_fhtom(np, 1);
255 nfsm_request(np, NFSPROC_FSINFO, l, cred);
256 nfsm_postop_attr(vp, retattr, 0);
257 if (!error) {
258 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
259 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
260 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
261 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
262 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
263 ~(NFS_FABLKSIZE - 1);
264 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
265 if (xmax < nmp->nm_wsize && xmax > 0) {
266 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
267 if (nmp->nm_wsize == 0)
268 nmp->nm_wsize = xmax;
269 }
270 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
271 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
272 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
273 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
274 ~(NFS_FABLKSIZE - 1);
275 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
276 if (xmax < nmp->nm_rsize && xmax > 0) {
277 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
278 if (nmp->nm_rsize == 0)
279 nmp->nm_rsize = xmax;
280 }
281 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
282 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
283 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
284 ~(NFS_DIRFRAGSIZ - 1);
285 if (xmax < nmp->nm_readdirsize && xmax > 0) {
286 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
287 if (nmp->nm_readdirsize == 0)
288 nmp->nm_readdirsize = xmax;
289 }
290 /* XXX */
291 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
292 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
293 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
294 nmp->nm_maxfilesize = maxfsize;
295 nmp->nm_mountp->mnt_fs_bshift =
296 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
297 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
298 }
299 nfsm_reqdone;
300 return (error);
301 }
302 #endif
303
304 /*
305 * Mount a remote root fs via. NFS. It goes like this:
306 * - Call nfs_boot_init() to fill in the nfs_diskless struct
307 * - build the rootfs mount point and call mountnfs() to do the rest.
308 */
309 int
310 nfs_mountroot()
311 {
312 struct timespec ts;
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 ts.tv_sec = NFS_MAXATTRTIMO;
334 ts.tv_nsec = 0;
335 tc_setclock(&ts);
336 }
337
338 /*
339 * Call nfs_boot_init() to fill in the nfs_diskless struct.
340 * Side effect: Finds and configures a network interface.
341 */
342 nd = kmem_alloc(sizeof(*nd), KM_SLEEP);
343 memset(nd, 0, sizeof(*nd));
344 error = nfs_boot_init(nd, l);
345 if (error) {
346 kmem_free(nd, sizeof(*nd));
347 return (error);
348 }
349
350 /*
351 * Create the root mount point.
352 */
353 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, l);
354 if (error)
355 goto out;
356 printf("root on %s\n", nd->nd_root.ndm_host);
357
358 /*
359 * Link it into the mount list.
360 */
361 mutex_enter(&mountlist_lock);
362 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
363 mutex_exit(&mountlist_lock);
364 rootvp = vp;
365 mp->mnt_vnodecovered = NULLVP;
366 vfs_unbusy(mp, false);
367
368 /* Get root attributes (for the time). */
369 error = VOP_GETATTR(vp, &attr, l->l_cred);
370 if (error)
371 panic("nfs_mountroot: getattr for root");
372 n = attr.va_atime.tv_sec;
373 #ifdef DEBUG
374 printf("root time: 0x%lx\n", n);
375 #endif
376 setrootfstime(n);
377
378 out:
379 if (error)
380 nfs_boot_cleanup(nd, l);
381 kmem_free(nd, sizeof(*nd));
382 return (error);
383 }
384
385 /*
386 * Internal version of mount system call for diskless setup.
387 * Separate function because we used to call it twice.
388 * (once for root and once for swap)
389 */
390 static int
391 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, l)
392 struct nfs_dlmount *ndmntp;
393 const char *mntname; /* mount point name */
394 struct mount **mpp;
395 struct vnode **vpp;
396 struct lwp *l;
397 {
398 struct mount *mp;
399 struct mbuf *m;
400 int error;
401
402 vfs_rootmountalloc(MOUNT_NFS, mntname, &mp);
403
404 mp->mnt_op = &nfs_vfsops;
405
406 /*
407 * Historical practice expects NFS root file systems to
408 * be initially mounted r/w.
409 */
410 mp->mnt_flag &= ~MNT_RDONLY;
411
412 /* Get mbuf for server sockaddr. */
413 m = m_get(M_WAIT, MT_SONAME);
414 if (m == NULL)
415 panic("nfs_mountroot: mget soname for %s", mntname);
416 MCLAIM(m, &nfs_mowner);
417 memcpy(mtod(m, void *), (void *)ndmntp->ndm_args.addr,
418 (m->m_len = ndmntp->ndm_args.addr->sa_len));
419
420 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
421 ndmntp->ndm_args.hostname, vpp, l);
422 if (error) {
423 vfs_unbusy(mp, false);
424 vfs_destroy(mp);
425 printf("nfs_mountroot: mount %s failed: %d\n",
426 mntname, error);
427 } else
428 *mpp = mp;
429
430 return (error);
431 }
432
433 void
434 nfs_decode_args(nmp, argp, l)
435 struct nfsmount *nmp;
436 struct nfs_args *argp;
437 struct lwp *l;
438 {
439 int adjsock;
440 int maxio;
441
442 /*
443 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
444 * no sense in that context.
445 */
446 if (argp->sotype == SOCK_STREAM)
447 argp->flags &= ~NFSMNT_NOCONN;
448
449 /*
450 * Cookie translation is not needed for v2, silently ignore it.
451 */
452 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
453 NFSMNT_XLATECOOKIE)
454 argp->flags &= ~NFSMNT_XLATECOOKIE;
455
456 /* Re-bind if rsrvd port requested and wasn't on one */
457 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
458 && (argp->flags & NFSMNT_RESVPORT);
459 /* Also re-bind if we're switching to/from a connected UDP socket */
460 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
461 (argp->flags & NFSMNT_NOCONN));
462
463 /* Update flags. */
464 nmp->nm_flag = argp->flags;
465
466 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
467 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
468 if (nmp->nm_timeo < NFS_MINTIMEO)
469 nmp->nm_timeo = NFS_MINTIMEO;
470 else if (nmp->nm_timeo > NFS_MAXTIMEO)
471 nmp->nm_timeo = NFS_MAXTIMEO;
472 }
473
474 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
475 nmp->nm_retry = argp->retrans;
476 if (nmp->nm_retry > NFS_MAXREXMIT)
477 nmp->nm_retry = NFS_MAXREXMIT;
478 }
479
480 #ifndef NFS_V2_ONLY
481 if (argp->flags & NFSMNT_NFSV3) {
482 if (argp->sotype == SOCK_DGRAM)
483 maxio = NFS_MAXDGRAMDATA;
484 else
485 maxio = NFS_MAXDATA;
486 } else
487 #endif
488 maxio = NFS_V2MAXDATA;
489
490 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
491 int osize = nmp->nm_wsize;
492 nmp->nm_wsize = argp->wsize;
493 /* Round down to multiple of blocksize */
494 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
495 if (nmp->nm_wsize <= 0)
496 nmp->nm_wsize = NFS_FABLKSIZE;
497 adjsock |= (nmp->nm_wsize != osize);
498 }
499 if (nmp->nm_wsize > maxio)
500 nmp->nm_wsize = maxio;
501 if (nmp->nm_wsize > MAXBSIZE)
502 nmp->nm_wsize = MAXBSIZE;
503
504 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
505 int osize = nmp->nm_rsize;
506 nmp->nm_rsize = argp->rsize;
507 /* Round down to multiple of blocksize */
508 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
509 if (nmp->nm_rsize <= 0)
510 nmp->nm_rsize = NFS_FABLKSIZE;
511 adjsock |= (nmp->nm_rsize != osize);
512 }
513 if (nmp->nm_rsize > maxio)
514 nmp->nm_rsize = maxio;
515 if (nmp->nm_rsize > MAXBSIZE)
516 nmp->nm_rsize = MAXBSIZE;
517
518 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
519 nmp->nm_readdirsize = argp->readdirsize;
520 /* Round down to multiple of minimum blocksize */
521 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
522 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
523 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
524 /* Bigger than buffer size makes no sense */
525 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
526 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
527 } else if (argp->flags & NFSMNT_RSIZE)
528 nmp->nm_readdirsize = nmp->nm_rsize;
529
530 if (nmp->nm_readdirsize > maxio)
531 nmp->nm_readdirsize = maxio;
532
533 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
534 argp->maxgrouplist <= NFS_MAXGRPS)
535 nmp->nm_numgrps = argp->maxgrouplist;
536 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
537 argp->readahead <= NFS_MAXRAHEAD)
538 nmp->nm_readahead = argp->readahead;
539 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
540 argp->deadthresh <= NFS_NEVERDEAD)
541 nmp->nm_deadthresh = argp->deadthresh;
542
543 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
544 (nmp->nm_soproto != argp->proto));
545 nmp->nm_sotype = argp->sotype;
546 nmp->nm_soproto = argp->proto;
547
548 if (nmp->nm_so && adjsock) {
549 nfs_safedisconnect(nmp);
550 if (nmp->nm_sotype == SOCK_DGRAM)
551 while (nfs_connect(nmp, (struct nfsreq *)0, l)) {
552 printf("nfs_args: retrying connect\n");
553 kpause("nfscn3", false, hz, NULL);
554 }
555 }
556 }
557
558 /*
559 * VFS Operations.
560 *
561 * mount system call
562 * It seems a bit dumb to copyinstr() the host and path here and then
563 * memcpy() them in mountnfs(), but I wanted to detect errors before
564 * doing the sockargs() call because sockargs() allocates an mbuf and
565 * an error after that means that I have to release the mbuf.
566 */
567 /* ARGSUSED */
568 int
569 nfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
570 {
571 struct lwp *l = curlwp;
572 int error;
573 struct nfs_args *args = data;
574 struct mbuf *nam;
575 struct nfsmount *nmp = VFSTONFS(mp);
576 struct sockaddr *sa;
577 struct vnode *vp;
578 char *pth, *hst;
579 struct proc *p;
580 size_t len;
581 u_char *nfh;
582
583 if (*data_len < sizeof *args)
584 return EINVAL;
585
586 p = l->l_proc;
587 if (mp->mnt_flag & MNT_GETARGS) {
588
589 if (nmp == NULL)
590 return (EIO);
591 if (args->addr != NULL) {
592 sa = mtod(nmp->nm_nam, struct sockaddr *);
593 error = copyout(sa, args->addr, sa->sa_len);
594 if (error)
595 return (error);
596 args->addrlen = sa->sa_len;
597 } else
598 args->addrlen = 0;
599
600 args->version = NFS_ARGSVERSION;
601 args->sotype = nmp->nm_sotype;
602 args->proto = nmp->nm_soproto;
603 args->fh = NULL;
604 args->fhsize = 0;
605 args->flags = nmp->nm_flag;
606 args->wsize = nmp->nm_wsize;
607 args->rsize = nmp->nm_rsize;
608 args->readdirsize = nmp->nm_readdirsize;
609 args->timeo = nmp->nm_timeo;
610 args->retrans = nmp->nm_retry;
611 args->maxgrouplist = nmp->nm_numgrps;
612 args->readahead = nmp->nm_readahead;
613 args->leaseterm = 0; /* dummy */
614 args->deadthresh = nmp->nm_deadthresh;
615 args->hostname = NULL;
616 *data_len = sizeof *args;
617 return 0;
618 }
619
620 if (args->version != NFS_ARGSVERSION)
621 return (EPROGMISMATCH);
622 if (args->flags & (NFSMNT_NQNFS|NFSMNT_KERB))
623 return (EPROGUNAVAIL);
624 #ifdef NFS_V2_ONLY
625 if (args->flags & NFSMNT_NFSV3)
626 return (EPROGMISMATCH);
627 #endif
628 if (mp->mnt_flag & MNT_UPDATE) {
629 #if 0
630 if (nmp == NULL)
631 return (EIO);
632 /*
633 * When doing an update, we can't change from or to
634 * v3, or change cookie translation
635 */
636 args->flags = (args->flags & ~(NFSMNT_NFSV3|NFSMNT_XLATECOOKIE)) |
637 (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_XLATECOOKIE));
638 nfs_decode_args(nmp, args, l);
639 return (0);
640 #endif
641 return EOPNOTSUPP;
642 }
643 if (args->fhsize < 0 || args->fhsize > NFSX_V3FHMAX)
644 return (EINVAL);
645 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
646 error = copyin(args->fh, nfh, args->fhsize);
647 if (error)
648 goto free_nfh;
649 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
650 error = copyinstr(path, pth, MNAMELEN - 1, &len);
651 if (error)
652 goto free_pth;
653 memset(&pth[len], 0, MNAMELEN - len);
654 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
655 error = copyinstr(args->hostname, hst, MNAMELEN - 1, &len);
656 if (error)
657 goto free_hst;
658 memset(&hst[len], 0, MNAMELEN - len);
659 /* sockargs() call must be after above copyin() calls */
660 error = sockargs(&nam, args->addr, args->addrlen, MT_SONAME);
661 if (error)
662 goto free_hst;
663 MCLAIM(nam, &nfs_mowner);
664 args->fh = nfh;
665 error = mountnfs(args, mp, nam, pth, hst, &vp, l);
666
667 free_hst:
668 FREE(hst, M_TEMP);
669 free_pth:
670 FREE(pth, M_TEMP);
671 free_nfh:
672 FREE(nfh, M_TEMP);
673
674 return (error);
675 }
676
677 /*
678 * Common code for mount and mountroot
679 */
680 int
681 mountnfs(argp, mp, nam, pth, hst, vpp, l)
682 struct nfs_args *argp;
683 struct mount *mp;
684 struct mbuf *nam;
685 const char *pth, *hst;
686 struct vnode **vpp;
687 struct lwp *l;
688 {
689 struct nfsmount *nmp;
690 struct nfsnode *np;
691 int error;
692 struct vattr *attrs;
693 kauth_cred_t cr;
694 char iosname[IOSTATNAMELEN];
695
696 /*
697 * If the number of nfs iothreads to use has never
698 * been set, create a reasonable number of them.
699 */
700
701 if (nfs_niothreads < 0) {
702 nfs_set_niothreads(NFS_DEFAULT_NIOTHREADS);
703 }
704
705 if (mp->mnt_flag & MNT_UPDATE) {
706 nmp = VFSTONFS(mp);
707 /* update paths, file handles, etc, here XXX */
708 m_freem(nam);
709 return (0);
710 } else {
711 nmp = kmem_zalloc(sizeof(*nmp), KM_SLEEP);
712 mp->mnt_data = nmp;
713 TAILQ_INIT(&nmp->nm_uidlruhead);
714 TAILQ_INIT(&nmp->nm_bufq);
715 rw_init(&nmp->nm_writeverflock);
716 mutex_init(&nmp->nm_lock, MUTEX_DEFAULT, IPL_NONE);
717 cv_init(&nmp->nm_rcvcv, "nfsrcv");
718 cv_init(&nmp->nm_sndcv, "nfssnd");
719 cv_init(&nmp->nm_aiocv, "nfsaio");
720 cv_init(&nmp->nm_disconcv, "nfsdis");
721 }
722 vfs_getnewfsid(mp);
723 nmp->nm_mountp = mp;
724
725 #ifndef NFS_V2_ONLY
726 if ((argp->flags & NFSMNT_NFSV3) == 0)
727 #endif
728 {
729 /*
730 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
731 * will fill this in.
732 */
733 nmp->nm_maxfilesize = 0xffffffffLL;
734 if (argp->fhsize != NFSX_V2FH) {
735 return EINVAL;
736 }
737 }
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 *vpp = NFSTOV(np);
781 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
782 VOP_GETATTR(*vpp, attrs, l->l_cred);
783 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->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(*vpp, &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 == ROOTINO (2). So, just unlock, but no rele.
803 */
804
805 (*vpp)->v_vflag |= VV_ROOT;
806 nmp->nm_vnode = *vpp;
807 VOP_UNLOCK(*vpp, 0);
808
809 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
810 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
811
812 return (0);
813 bad:
814 nfs_disconnect(nmp);
815 rw_destroy(&nmp->nm_writeverflock);
816 mutex_destroy(&nmp->nm_lock);
817 cv_destroy(&nmp->nm_rcvcv);
818 cv_destroy(&nmp->nm_sndcv);
819 cv_destroy(&nmp->nm_aiocv);
820 cv_destroy(&nmp->nm_disconcv);
821 kmem_free(nmp, sizeof(*nmp));
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)
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 vput(vp);
888 vgone(vp);
889 nfs_disconnect(nmp);
890 m_freem(nmp->nm_nam);
891
892 rw_destroy(&nmp->nm_writeverflock);
893 mutex_destroy(&nmp->nm_lock);
894 cv_destroy(&nmp->nm_rcvcv);
895 cv_destroy(&nmp->nm_sndcv);
896 cv_destroy(&nmp->nm_aiocv);
897 cv_destroy(&nmp->nm_disconcv);
898 kmem_free(nmp, sizeof(*nmp));
899 return (0);
900 }
901
902 /*
903 * Return root of a filesystem
904 */
905 int
906 nfs_root(mp, vpp)
907 struct mount *mp;
908 struct vnode **vpp;
909 {
910 struct vnode *vp;
911 struct nfsmount *nmp;
912 int error;
913
914 nmp = VFSTONFS(mp);
915 vp = nmp->nm_vnode;
916 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
917 if (error != 0)
918 return error;
919 if (vp->v_type == VNON)
920 vp->v_type = VDIR;
921 *vpp = vp;
922 return (0);
923 }
924
925 extern int syncprt;
926
927 /*
928 * Flush out the buffer cache
929 */
930 /* ARGSUSED */
931 int
932 nfs_sync(mp, waitfor, cred)
933 struct mount *mp;
934 int waitfor;
935 kauth_cred_t cred;
936 {
937 struct vnode *vp, *mvp;
938 int error, allerror = 0;
939
940 /*
941 * Force stale buffer cache information to be flushed.
942 */
943 if ((mvp = vnalloc(mp)) == NULL)
944 return (ENOMEM);
945 loop:
946 /*
947 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
948 * and vclean() can be called indirectly
949 */
950 mutex_enter(&mntvnode_lock);
951 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
952 vmark(mvp, vp);
953 if (vp->v_mount != mp || vismarker(vp))
954 continue;
955 mutex_enter(&vp->v_interlock);
956 /* XXX MNT_LAZY cannot be right? */
957 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
958 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
959 UVM_OBJ_IS_CLEAN(&vp->v_uobj))) {
960 mutex_exit(&vp->v_interlock);
961 continue;
962 }
963 mutex_exit(&mntvnode_lock);
964 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
965 (void)vunmark(mvp);
966 goto loop;
967 }
968 error = VOP_FSYNC(vp, cred,
969 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
970 if (error)
971 allerror = error;
972 vput(vp);
973 mutex_enter(&mntvnode_lock);
974 }
975 mutex_exit(&mntvnode_lock);
976 vnfree(mvp);
977 return (allerror);
978 }
979
980 /*
981 * NFS flat namespace lookup.
982 * Currently unsupported.
983 */
984 /* ARGSUSED */
985 int
986 nfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
987 {
988
989 return (EOPNOTSUPP);
990 }
991
992 /*
993 * Do that sysctl thang...
994 */
995 static int
996 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
997 {
998 struct sysctlnode node;
999 int val;
1000 int error;
1001
1002 val = nfs_niothreads;
1003 node = *rnode;
1004 node.sysctl_data = &val;
1005 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1006 if (error || newp == NULL)
1007 return error;
1008
1009 return nfs_set_niothreads(val);
1010 }
1011
1012 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
1013 {
1014
1015 sysctl_createv(clog, 0, NULL, NULL,
1016 CTLFLAG_PERMANENT,
1017 CTLTYPE_NODE, "vfs", NULL,
1018 NULL, 0, NULL, 0,
1019 CTL_VFS, CTL_EOL);
1020 sysctl_createv(clog, 0, NULL, NULL,
1021 CTLFLAG_PERMANENT,
1022 CTLTYPE_NODE, "nfs",
1023 SYSCTL_DESCR("NFS vfs options"),
1024 NULL, 0, NULL, 0,
1025 CTL_VFS, 2, CTL_EOL);
1026 /*
1027 * XXX the "2" above could be dynamic, thereby eliminating one
1028 * more instance of the "number to vfs" mapping problem, but
1029 * "2" is the order as taken from sys/mount.h
1030 */
1031
1032 sysctl_createv(clog, 0, NULL, NULL,
1033 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1034 CTLTYPE_STRUCT, "nfsstats",
1035 SYSCTL_DESCR("NFS operation statistics"),
1036 NULL, 0, &nfsstats, sizeof(nfsstats),
1037 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1038 sysctl_createv(clog, 0, NULL, NULL,
1039 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1040 CTLTYPE_INT, "iothreads",
1041 SYSCTL_DESCR("Number of NFS client processes desired"),
1042 sysctl_vfs_nfs_iothreads, 0, NULL, 0,
1043 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1044 }
1045
1046 /* ARGSUSED */
1047 int
1048 nfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
1049 {
1050 size_t fidsize;
1051 size_t fhsize;
1052 struct nfsnode *np;
1053 int error;
1054 struct vattr va;
1055
1056 fidsize = fid->fid_len;
1057 if (fidsize < sizeof(*fid)) {
1058 return EINVAL;
1059 }
1060 fhsize = fidsize - sizeof(*fid);
1061 if ((fhsize % NFSX_UNSIGNED) != 0) {
1062 return EINVAL;
1063 }
1064 if ((VFSTONFS(mp)->nm_flag & NFSMNT_NFSV3) != 0) {
1065 if (fhsize > NFSX_V3FHMAX || fhsize == 0) {
1066 return EINVAL;
1067 }
1068 } else {
1069 if (fhsize != NFSX_V2FH) {
1070 return EINVAL;
1071 }
1072 }
1073 error = nfs_nget(mp, (void *)fid->fid_data, fhsize, &np);
1074 if (error) {
1075 return error;
1076 }
1077 *vpp = NFSTOV(np);
1078 error = VOP_GETATTR(*vpp, &va, kauth_cred_get());
1079 if (error != 0) {
1080 vput(*vpp);
1081 }
1082 return error;
1083 }
1084
1085 /* ARGSUSED */
1086 int
1087 nfs_vptofh(struct vnode *vp, struct fid *buf, size_t *bufsize)
1088 {
1089 struct nfsnode *np;
1090 struct fid *fid;
1091 size_t fidsize;
1092 int error = 0;
1093
1094 np = VTONFS(vp);
1095 fidsize = sizeof(*fid) + np->n_fhsize;
1096 if (*bufsize < fidsize) {
1097 error = E2BIG;
1098 }
1099 *bufsize = fidsize;
1100 if (error == 0) {
1101 struct fid fid_store;
1102
1103 fid = &fid_store;
1104 memset(fid, 0, sizeof(*fid));
1105 fid->fid_len = fidsize;
1106 memcpy(buf, fid, sizeof(*fid));
1107 memcpy(buf->fid_data, np->n_fhp, np->n_fhsize);
1108 }
1109 return error;
1110 }
1111
1112 /*
1113 * Vfs start routine, a no-op.
1114 */
1115 /* ARGSUSED */
1116 int
1117 nfs_start(struct mount *mp, int flags)
1118 {
1119
1120 return (0);
1121 }
1122