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