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