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