nfs_vfsops.c revision 1.140 1 /* $NetBSD: nfs_vfsops.c,v 1.140 2004/05/25 14:54:58 hannken 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.140 2004/05/25 14:54:58 hannken 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 nfs_vnodeopv_descs,
117 };
118
119 extern u_int32_t nfs_procids[NFS_NPROCS];
120 extern u_int32_t nfs_prog, nfs_vers;
121
122 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
123 struct mount **, struct vnode **, struct proc *));
124
125 /*
126 * nfs statvfs call
127 */
128 int
129 nfs_statvfs(mp, sbp, p)
130 struct mount *mp;
131 struct statvfs *sbp;
132 struct proc *p;
133 {
134 struct vnode *vp;
135 struct nfs_statfs *sfp;
136 caddr_t cp;
137 u_int32_t *tl;
138 int32_t t1, t2;
139 caddr_t bpos, dpos, cp2;
140 struct nfsmount *nmp = VFSTONFS(mp);
141 int error = 0, retattr;
142 #ifdef NFS_V2_ONLY
143 const int v3 = 0;
144 #else
145 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
146 #endif
147 struct mbuf *mreq, *mrep = NULL, *md, *mb;
148 struct ucred *cred;
149 u_quad_t tquad;
150 struct nfsnode *np;
151
152 #ifndef nolint
153 sfp = (struct nfs_statfs *)0;
154 #endif
155 vp = nmp->nm_vnode;
156 np = VTONFS(vp);
157 cred = crget();
158 cred->cr_ngroups = 0;
159 #ifndef NFS_V2_ONLY
160 if (v3 && (nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
161 (void)nfs_fsinfo(nmp, vp, cred, p);
162 #endif
163 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
164 nfsm_reqhead(np, NFSPROC_FSSTAT, NFSX_FH(v3));
165 nfsm_fhtom(np, v3);
166 nfsm_request(np, NFSPROC_FSSTAT, p, cred);
167 if (v3)
168 nfsm_postop_attr(vp, retattr, 0);
169 if (error) {
170 if (mrep != NULL) {
171 if (mrep->m_next != NULL)
172 printf("nfs_vfsops: nfs_statvfs would lose buffers\n");
173 m_freem(mrep);
174 }
175 goto nfsmout;
176 }
177 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
178 sbp->f_flag = nmp->nm_flag;
179 sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
180 if (v3) {
181 sbp->f_frsize = sbp->f_bsize = NFS_FABLKSIZE;
182 tquad = fxdr_hyper(&sfp->sf_tbytes);
183 sbp->f_blocks = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
184 tquad = fxdr_hyper(&sfp->sf_fbytes);
185 sbp->f_bfree = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
186 tquad = fxdr_hyper(&sfp->sf_abytes);
187 tquad = ((quad_t)tquad / (quad_t)NFS_FABLKSIZE);
188 sbp->f_bresvd = sbp->f_bfree - tquad;
189 sbp->f_bavail = tquad;
190 #ifdef COMPAT_20
191 /* Handle older NFS servers returning negative values */
192 if ((quad_t)sbp->f_bavail < 0)
193 sbp->f_bavail = 0;
194 #endif
195 tquad = fxdr_hyper(&sfp->sf_tfiles);
196 sbp->f_files = tquad;
197 tquad = fxdr_hyper(&sfp->sf_ffiles);
198 sbp->f_ffree = tquad;
199 sbp->f_favail = tquad;
200 sbp->f_fresvd = 0;
201 sbp->f_namemax = MAXNAMLEN;
202 } else {
203 sbp->f_bsize = NFS_FABLKSIZE;
204 sbp->f_frsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
205 sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
206 sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
207 sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
208 sbp->f_fresvd = 0;
209 sbp->f_files = 0;
210 sbp->f_ffree = 0;
211 sbp->f_favail = 0;
212 sbp->f_fresvd = 0;
213 sbp->f_namemax = MAXNAMLEN;
214 }
215 copy_statvfs_info(sbp, mp);
216 nfsm_reqdone;
217 crfree(cred);
218 return (error);
219 }
220
221 #ifndef NFS_V2_ONLY
222 /*
223 * nfs version 3 fsinfo rpc call
224 */
225 int
226 nfs_fsinfo(nmp, vp, cred, p)
227 struct nfsmount *nmp;
228 struct vnode *vp;
229 struct ucred *cred;
230 struct proc *p;
231 {
232 struct nfsv3_fsinfo *fsp;
233 caddr_t cp;
234 int32_t t1, t2;
235 u_int32_t *tl, pref, max;
236 caddr_t bpos, dpos, cp2;
237 int error = 0, retattr;
238 struct mbuf *mreq, *mrep, *md, *mb;
239 u_int64_t maxfsize;
240 struct nfsnode *np = VTONFS(vp);
241
242 nfsstats.rpccnt[NFSPROC_FSINFO]++;
243 nfsm_reqhead(np, NFSPROC_FSINFO, NFSX_FH(1));
244 nfsm_fhtom(np, 1);
245 nfsm_request(np, NFSPROC_FSINFO, p, cred);
246 nfsm_postop_attr(vp, retattr, 0);
247 if (!error) {
248 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
249 pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
250 if ((nmp->nm_flag & NFSMNT_WSIZE) == 0 &&
251 pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
252 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
253 ~(NFS_FABLKSIZE - 1);
254 max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
255 if (max < nmp->nm_wsize && max > 0) {
256 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
257 if (nmp->nm_wsize == 0)
258 nmp->nm_wsize = max;
259 }
260 pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
261 if ((nmp->nm_flag & NFSMNT_RSIZE) == 0 &&
262 pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
263 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
264 ~(NFS_FABLKSIZE - 1);
265 max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
266 if (max < nmp->nm_rsize && max > 0) {
267 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
268 if (nmp->nm_rsize == 0)
269 nmp->nm_rsize = max;
270 }
271 pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
272 if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
273 nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
274 ~(NFS_DIRFRAGSIZ - 1);
275 if (max < nmp->nm_readdirsize && max > 0) {
276 nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
277 if (nmp->nm_readdirsize == 0)
278 nmp->nm_readdirsize = max;
279 }
280 /* XXX */
281 nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
282 maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
283 if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
284 nmp->nm_maxfilesize = maxfsize;
285 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
286 }
287 nfsm_reqdone;
288 return (error);
289 }
290 #endif
291
292 /*
293 * Mount a remote root fs via. NFS. It goes like this:
294 * - Call nfs_boot_init() to fill in the nfs_diskless struct
295 * - build the rootfs mount point and call mountnfs() to do the rest.
296 */
297 int
298 nfs_mountroot()
299 {
300 struct nfs_diskless *nd;
301 struct vattr attr;
302 struct mount *mp;
303 struct vnode *vp;
304 struct proc *procp;
305 long n;
306 int error;
307
308 procp = curproc; /* XXX */
309
310 if (root_device->dv_class != DV_IFNET)
311 return (ENODEV);
312
313 /*
314 * XXX time must be non-zero when we init the interface or else
315 * the arp code will wedge. [Fixed now in if_ether.c]
316 * However, the NFS attribute cache gives false "hits" when
317 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
318 */
319 if (time.tv_sec < NFS_MAXATTRTIMO)
320 time.tv_sec = NFS_MAXATTRTIMO;
321
322 /*
323 * Call nfs_boot_init() to fill in the nfs_diskless struct.
324 * Side effect: Finds and configures a network interface.
325 */
326 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
327 memset((caddr_t)nd, 0, sizeof(*nd));
328 error = nfs_boot_init(nd, procp);
329 if (error) {
330 free(nd, M_NFSMNT);
331 return (error);
332 }
333
334 /*
335 * Create the root mount point.
336 */
337 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
338 if (error)
339 goto out;
340 printf("root on %s\n", nd->nd_root.ndm_host);
341
342 /*
343 * Link it into the mount list.
344 */
345 simple_lock(&mountlist_slock);
346 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
347 simple_unlock(&mountlist_slock);
348 rootvp = vp;
349 mp->mnt_vnodecovered = NULLVP;
350 vfs_unbusy(mp);
351
352 /* Get root attributes (for the time). */
353 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
354 if (error)
355 panic("nfs_mountroot: getattr for root");
356 n = attr.va_atime.tv_sec;
357 #ifdef DEBUG
358 printf("root time: 0x%lx\n", n);
359 #endif
360 inittodr(n);
361
362 out:
363 if (error)
364 nfs_boot_cleanup(nd, procp);
365 free(nd, M_NFSMNT);
366 return (error);
367 }
368
369 /*
370 * Internal version of mount system call for diskless setup.
371 * Separate function because we used to call it twice.
372 * (once for root and once for swap)
373 */
374 static int
375 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
376 struct nfs_dlmount *ndmntp;
377 const char *mntname; /* mount point name */
378 struct mount **mpp;
379 struct vnode **vpp;
380 struct proc *p;
381 {
382 struct mount *mp;
383 struct mbuf *m;
384 int error;
385
386 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
387
388 mp->mnt_op = &nfs_vfsops;
389
390 /*
391 * Historical practice expects NFS root file systems to
392 * be initially mounted r/w.
393 */
394 mp->mnt_flag &= ~MNT_RDONLY;
395
396 /* Get mbuf for server sockaddr. */
397 m = m_get(M_WAIT, MT_SONAME);
398 if (m == NULL)
399 panic("nfs_mountroot: mget soname for %s", mntname);
400 MCLAIM(m, &nfs_mowner);
401 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
402 (m->m_len = ndmntp->ndm_args.addr->sa_len));
403
404 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
405 ndmntp->ndm_args.hostname, vpp, p);
406 if (error) {
407 mp->mnt_op->vfs_refcount--;
408 vfs_unbusy(mp);
409 printf("nfs_mountroot: mount %s failed: %d\n",
410 mntname, error);
411 free(mp, M_MOUNT);
412 } else
413 *mpp = mp;
414
415 return (error);
416 }
417
418 void
419 nfs_decode_args(nmp, argp, p)
420 struct nfsmount *nmp;
421 struct nfs_args *argp;
422 struct proc *p;
423 {
424 int s;
425 int adjsock;
426 int maxio;
427
428 s = splsoftnet();
429
430 /*
431 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
432 * no sense in that context.
433 */
434 if (argp->sotype == SOCK_STREAM)
435 argp->flags &= ~NFSMNT_NOCONN;
436
437 /*
438 * Cookie translation is not needed for v2, silently ignore it.
439 */
440 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
441 NFSMNT_XLATECOOKIE)
442 argp->flags &= ~NFSMNT_XLATECOOKIE;
443
444 /* Re-bind if rsrvd port requested and wasn't on one */
445 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
446 && (argp->flags & NFSMNT_RESVPORT);
447 /* Also re-bind if we're switching to/from a connected UDP socket */
448 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
449 (argp->flags & NFSMNT_NOCONN));
450
451 /* Update flags. */
452 nmp->nm_flag = argp->flags;
453 splx(s);
454
455 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
456 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
457 if (nmp->nm_timeo < NFS_MINTIMEO)
458 nmp->nm_timeo = NFS_MINTIMEO;
459 else if (nmp->nm_timeo > NFS_MAXTIMEO)
460 nmp->nm_timeo = NFS_MAXTIMEO;
461 }
462
463 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
464 nmp->nm_retry = argp->retrans;
465 if (nmp->nm_retry > NFS_MAXREXMIT)
466 nmp->nm_retry = NFS_MAXREXMIT;
467 }
468
469 #ifndef NFS_V2_ONLY
470 if (argp->flags & NFSMNT_NFSV3) {
471 if (argp->sotype == SOCK_DGRAM)
472 maxio = NFS_MAXDGRAMDATA;
473 else
474 maxio = NFS_MAXDATA;
475 } else
476 #endif
477 maxio = NFS_V2MAXDATA;
478
479 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
480 int osize = nmp->nm_wsize;
481 nmp->nm_wsize = argp->wsize;
482 /* Round down to multiple of blocksize */
483 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
484 if (nmp->nm_wsize <= 0)
485 nmp->nm_wsize = NFS_FABLKSIZE;
486 adjsock |= (nmp->nm_wsize != osize);
487 }
488 if (nmp->nm_wsize > maxio)
489 nmp->nm_wsize = maxio;
490 if (nmp->nm_wsize > MAXBSIZE)
491 nmp->nm_wsize = MAXBSIZE;
492
493 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
494 int osize = nmp->nm_rsize;
495 nmp->nm_rsize = argp->rsize;
496 /* Round down to multiple of blocksize */
497 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
498 if (nmp->nm_rsize <= 0)
499 nmp->nm_rsize = NFS_FABLKSIZE;
500 adjsock |= (nmp->nm_rsize != osize);
501 }
502 if (nmp->nm_rsize > maxio)
503 nmp->nm_rsize = maxio;
504 if (nmp->nm_rsize > MAXBSIZE)
505 nmp->nm_rsize = MAXBSIZE;
506
507 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
508 nmp->nm_readdirsize = argp->readdirsize;
509 /* Round down to multiple of minimum blocksize */
510 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
511 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
512 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
513 /* Bigger than buffer size makes no sense */
514 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
515 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
516 } else if (argp->flags & NFSMNT_RSIZE)
517 nmp->nm_readdirsize = nmp->nm_rsize;
518
519 if (nmp->nm_readdirsize > maxio)
520 nmp->nm_readdirsize = maxio;
521
522 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
523 argp->maxgrouplist <= NFS_MAXGRPS)
524 nmp->nm_numgrps = argp->maxgrouplist;
525 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
526 argp->readahead <= NFS_MAXRAHEAD)
527 nmp->nm_readahead = argp->readahead;
528 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
529 argp->leaseterm <= NQ_MAXLEASE)
530 nmp->nm_leaseterm = argp->leaseterm;
531 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
532 argp->deadthresh <= NQ_NEVERDEAD)
533 nmp->nm_deadthresh = argp->deadthresh;
534
535 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
536 (nmp->nm_soproto != argp->proto));
537 nmp->nm_sotype = argp->sotype;
538 nmp->nm_soproto = argp->proto;
539
540 if (nmp->nm_so && adjsock) {
541 nfs_safedisconnect(nmp);
542 if (nmp->nm_sotype == SOCK_DGRAM)
543 while (nfs_connect(nmp, (struct nfsreq *)0, p)) {
544 printf("nfs_args: retrying connect\n");
545 (void) tsleep((caddr_t)&lbolt,
546 PSOCK, "nfscn3", 0);
547 }
548 }
549 }
550
551 /*
552 * VFS Operations.
553 *
554 * mount system call
555 * It seems a bit dumb to copyinstr() the host and path here and then
556 * memcpy() them in mountnfs(), but I wanted to detect errors before
557 * doing the sockargs() call because sockargs() allocates an mbuf and
558 * an error after that means that I have to release the mbuf.
559 */
560 /* ARGSUSED */
561 int
562 nfs_mount(mp, path, data, ndp, p)
563 struct mount *mp;
564 const char *path;
565 void *data;
566 struct nameidata *ndp;
567 struct proc *p;
568 {
569 int error;
570 struct nfs_args args;
571 struct mbuf *nam;
572 struct nfsmount *nmp = VFSTONFS(mp);
573 struct sockaddr *sa;
574 struct vnode *vp;
575 char *pth, *hst;
576 size_t len;
577 u_char *nfh;
578
579 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
580 if (error)
581 return (error);
582
583 if (mp->mnt_flag & MNT_GETARGS) {
584
585 if (nmp == NULL)
586 return (EIO);
587 if (args.addr != NULL) {
588 sa = mtod(nmp->nm_nam, struct sockaddr *);
589 error = copyout(sa, args.addr, sa->sa_len);
590 if (error)
591 return (error);
592 args.addrlen = sa->sa_len;
593 } else
594 args.addrlen = 0;
595
596 args.version = NFS_ARGSVERSION;
597 args.sotype = nmp->nm_sotype;
598 args.proto = nmp->nm_soproto;
599 args.fh = NULL;
600 args.fhsize = 0;
601 args.flags = nmp->nm_flag;
602 args.wsize = nmp->nm_wsize;
603 args.rsize = nmp->nm_rsize;
604 args.readdirsize = nmp->nm_readdirsize;
605 args.timeo = nmp->nm_timeo;
606 args.retrans = nmp->nm_retry;
607 args.maxgrouplist = nmp->nm_numgrps;
608 args.readahead = nmp->nm_readahead;
609 args.leaseterm = nmp->nm_leaseterm;
610 args.deadthresh = nmp->nm_deadthresh;
611 args.hostname = NULL;
612 return (copyout(&args, data, sizeof(args)));
613 }
614
615 if (args.version != NFS_ARGSVERSION)
616 return (EPROGMISMATCH);
617 #ifdef NFS_V2_ONLY
618 if (args.flags & NFSMNT_NQNFS)
619 return (EPROGUNAVAIL);
620 if (args.flags & NFSMNT_NFSV3)
621 return (EPROGMISMATCH);
622 #endif
623 if (mp->mnt_flag & MNT_UPDATE) {
624 if (nmp == NULL)
625 return (EIO);
626 /*
627 * When doing an update, we can't change from or to
628 * v3 and/or nqnfs, or change cookie translation
629 */
630 args.flags = (args.flags &
631 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
632 (nmp->nm_flag &
633 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
634 nfs_decode_args(nmp, &args, p);
635 return (0);
636 }
637 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
638 return (EINVAL);
639 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
640 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
641 if (error)
642 return (error);
643 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
644 error = copyinstr(path, pth, MNAMELEN - 1, &len);
645 if (error)
646 goto free_nfh;
647 memset(&pth[len], 0, MNAMELEN - len);
648 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
649 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
650 if (error)
651 goto free_pth;
652 memset(&hst[len], 0, MNAMELEN - len);
653 /* sockargs() call must be after above copyin() calls */
654 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
655 if (error)
656 goto free_hst;
657 MCLAIM(nam, &nfs_mowner);
658 args.fh = nfh;
659 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
660
661 free_hst:
662 FREE(hst, M_TEMP);
663 free_pth:
664 FREE(pth, M_TEMP);
665 free_nfh:
666 FREE(nfh, M_TEMP);
667
668 return (error);
669 }
670
671 /*
672 * Common code for mount and mountroot
673 */
674 int
675 mountnfs(argp, mp, nam, pth, hst, vpp, p)
676 struct nfs_args *argp;
677 struct mount *mp;
678 struct mbuf *nam;
679 const char *pth, *hst;
680 struct vnode **vpp;
681 struct proc *p;
682 {
683 struct nfsmount *nmp;
684 struct nfsnode *np;
685 int error;
686 struct vattr *attrs;
687 struct ucred *cr;
688
689 /*
690 * If the number of nfs iothreads to use has never
691 * been set, create a reasonable number of them.
692 */
693
694 if (nfs_niothreads < 0) {
695 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
696 nfs_getset_niothreads(TRUE);
697 }
698
699 if (mp->mnt_flag & MNT_UPDATE) {
700 nmp = VFSTONFS(mp);
701 /* update paths, file handles, etc, here XXX */
702 m_freem(nam);
703 return (0);
704 } else {
705 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
706 M_NFSMNT, M_WAITOK);
707 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
708 mp->mnt_data = nmp;
709 TAILQ_INIT(&nmp->nm_uidlruhead);
710 TAILQ_INIT(&nmp->nm_bufq);
711 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
712 simple_lock_init(&nmp->nm_slock);
713 }
714 vfs_getnewfsid(mp);
715 nmp->nm_mountp = mp;
716
717 #ifndef NFS_V2_ONLY
718 if (argp->flags & NFSMNT_NQNFS)
719 /*
720 * We have to set mnt_maxsymlink to a non-zero value so
721 * that COMPAT_43 routines will know that we are setting
722 * the d_type field in directories (and can zero it for
723 * unsuspecting binaries).
724 */
725 mp->mnt_maxsymlinklen = 1;
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, p);
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, p);
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, p)))
768 goto bad;
769
770 /*
771 * This is silly, but it has to be set so that vinifod() works.
772 * We do not want to do an nfs_statvfs() here since we can get
773 * stuck on a dead server and we are holding a lock on the mount
774 * point.
775 */
776 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
777 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
778 if (error)
779 goto bad;
780 *vpp = NFSTOV(np);
781 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
782 VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
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, p, 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, p)
818 struct mount *mp;
819 int mntflags;
820 struct proc *p;
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, p)
923 struct mount *mp;
924 int waitfor;
925 struct ucred *cred;
926 struct proc *p;
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, p);
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, p)
1068 struct mount *mp;
1069 int flags;
1070 struct proc *p;
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, p)
1082 struct mount *mp;
1083 int cmd;
1084 uid_t uid;
1085 void *arg;
1086 struct proc *p;
1087 {
1088
1089 return (EOPNOTSUPP);
1090 }
1091