nfs_vfsops.c revision 1.142 1 /* $NetBSD: nfs_vfsops.c,v 1.142 2004/07/12 12:40:30 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.142 2004/07/12 12:40:30 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
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_mountp->mnt_fs_bshift =
286 ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
287 nmp->nm_iflag |= NFSMNT_GOTFSINFO;
288 }
289 nfsm_reqdone;
290 return (error);
291 }
292 #endif
293
294 /*
295 * Mount a remote root fs via. NFS. It goes like this:
296 * - Call nfs_boot_init() to fill in the nfs_diskless struct
297 * - build the rootfs mount point and call mountnfs() to do the rest.
298 */
299 int
300 nfs_mountroot()
301 {
302 struct nfs_diskless *nd;
303 struct vattr attr;
304 struct mount *mp;
305 struct vnode *vp;
306 struct proc *procp;
307 long n;
308 int error;
309
310 procp = curproc; /* XXX */
311
312 if (root_device->dv_class != DV_IFNET)
313 return (ENODEV);
314
315 /*
316 * XXX time must be non-zero when we init the interface or else
317 * the arp code will wedge. [Fixed now in if_ether.c]
318 * However, the NFS attribute cache gives false "hits" when
319 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
320 */
321 if (time.tv_sec < NFS_MAXATTRTIMO)
322 time.tv_sec = NFS_MAXATTRTIMO;
323
324 /*
325 * Call nfs_boot_init() to fill in the nfs_diskless struct.
326 * Side effect: Finds and configures a network interface.
327 */
328 nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
329 memset((caddr_t)nd, 0, sizeof(*nd));
330 error = nfs_boot_init(nd, procp);
331 if (error) {
332 free(nd, M_NFSMNT);
333 return (error);
334 }
335
336 /*
337 * Create the root mount point.
338 */
339 error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
340 if (error)
341 goto out;
342 printf("root on %s\n", nd->nd_root.ndm_host);
343
344 /*
345 * Link it into the mount list.
346 */
347 simple_lock(&mountlist_slock);
348 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
349 simple_unlock(&mountlist_slock);
350 rootvp = vp;
351 mp->mnt_vnodecovered = NULLVP;
352 vfs_unbusy(mp);
353
354 /* Get root attributes (for the time). */
355 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
356 if (error)
357 panic("nfs_mountroot: getattr for root");
358 n = attr.va_atime.tv_sec;
359 #ifdef DEBUG
360 printf("root time: 0x%lx\n", n);
361 #endif
362 setrootfstime(n);
363
364 out:
365 if (error)
366 nfs_boot_cleanup(nd, procp);
367 free(nd, M_NFSMNT);
368 return (error);
369 }
370
371 /*
372 * Internal version of mount system call for diskless setup.
373 * Separate function because we used to call it twice.
374 * (once for root and once for swap)
375 */
376 static int
377 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
378 struct nfs_dlmount *ndmntp;
379 const char *mntname; /* mount point name */
380 struct mount **mpp;
381 struct vnode **vpp;
382 struct proc *p;
383 {
384 struct mount *mp;
385 struct mbuf *m;
386 int error;
387
388 vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
389
390 mp->mnt_op = &nfs_vfsops;
391
392 /*
393 * Historical practice expects NFS root file systems to
394 * be initially mounted r/w.
395 */
396 mp->mnt_flag &= ~MNT_RDONLY;
397
398 /* Get mbuf for server sockaddr. */
399 m = m_get(M_WAIT, MT_SONAME);
400 if (m == NULL)
401 panic("nfs_mountroot: mget soname for %s", mntname);
402 MCLAIM(m, &nfs_mowner);
403 memcpy(mtod(m, caddr_t), (caddr_t)ndmntp->ndm_args.addr,
404 (m->m_len = ndmntp->ndm_args.addr->sa_len));
405
406 error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
407 ndmntp->ndm_args.hostname, vpp, p);
408 if (error) {
409 mp->mnt_op->vfs_refcount--;
410 vfs_unbusy(mp);
411 printf("nfs_mountroot: mount %s failed: %d\n",
412 mntname, error);
413 free(mp, M_MOUNT);
414 } else
415 *mpp = mp;
416
417 return (error);
418 }
419
420 void
421 nfs_decode_args(nmp, argp, p)
422 struct nfsmount *nmp;
423 struct nfs_args *argp;
424 struct proc *p;
425 {
426 int s;
427 int adjsock;
428 int maxio;
429
430 s = splsoftnet();
431
432 /*
433 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
434 * no sense in that context.
435 */
436 if (argp->sotype == SOCK_STREAM)
437 argp->flags &= ~NFSMNT_NOCONN;
438
439 /*
440 * Cookie translation is not needed for v2, silently ignore it.
441 */
442 if ((argp->flags & (NFSMNT_XLATECOOKIE|NFSMNT_NFSV3)) ==
443 NFSMNT_XLATECOOKIE)
444 argp->flags &= ~NFSMNT_XLATECOOKIE;
445
446 /* Re-bind if rsrvd port requested and wasn't on one */
447 adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
448 && (argp->flags & NFSMNT_RESVPORT);
449 /* Also re-bind if we're switching to/from a connected UDP socket */
450 adjsock |= ((nmp->nm_flag & NFSMNT_NOCONN) !=
451 (argp->flags & NFSMNT_NOCONN));
452
453 /* Update flags. */
454 nmp->nm_flag = argp->flags;
455 splx(s);
456
457 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
458 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
459 if (nmp->nm_timeo < NFS_MINTIMEO)
460 nmp->nm_timeo = NFS_MINTIMEO;
461 else if (nmp->nm_timeo > NFS_MAXTIMEO)
462 nmp->nm_timeo = NFS_MAXTIMEO;
463 }
464
465 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
466 nmp->nm_retry = argp->retrans;
467 if (nmp->nm_retry > NFS_MAXREXMIT)
468 nmp->nm_retry = NFS_MAXREXMIT;
469 }
470
471 #ifndef NFS_V2_ONLY
472 if (argp->flags & NFSMNT_NFSV3) {
473 if (argp->sotype == SOCK_DGRAM)
474 maxio = NFS_MAXDGRAMDATA;
475 else
476 maxio = NFS_MAXDATA;
477 } else
478 #endif
479 maxio = NFS_V2MAXDATA;
480
481 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
482 int osize = nmp->nm_wsize;
483 nmp->nm_wsize = argp->wsize;
484 /* Round down to multiple of blocksize */
485 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
486 if (nmp->nm_wsize <= 0)
487 nmp->nm_wsize = NFS_FABLKSIZE;
488 adjsock |= (nmp->nm_wsize != osize);
489 }
490 if (nmp->nm_wsize > maxio)
491 nmp->nm_wsize = maxio;
492 if (nmp->nm_wsize > MAXBSIZE)
493 nmp->nm_wsize = MAXBSIZE;
494
495 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
496 int osize = nmp->nm_rsize;
497 nmp->nm_rsize = argp->rsize;
498 /* Round down to multiple of blocksize */
499 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
500 if (nmp->nm_rsize <= 0)
501 nmp->nm_rsize = NFS_FABLKSIZE;
502 adjsock |= (nmp->nm_rsize != osize);
503 }
504 if (nmp->nm_rsize > maxio)
505 nmp->nm_rsize = maxio;
506 if (nmp->nm_rsize > MAXBSIZE)
507 nmp->nm_rsize = MAXBSIZE;
508
509 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
510 nmp->nm_readdirsize = argp->readdirsize;
511 /* Round down to multiple of minimum blocksize */
512 nmp->nm_readdirsize &= ~(NFS_DIRFRAGSIZ - 1);
513 if (nmp->nm_readdirsize < NFS_DIRFRAGSIZ)
514 nmp->nm_readdirsize = NFS_DIRFRAGSIZ;
515 /* Bigger than buffer size makes no sense */
516 if (nmp->nm_readdirsize > NFS_DIRBLKSIZ)
517 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
518 } else if (argp->flags & NFSMNT_RSIZE)
519 nmp->nm_readdirsize = nmp->nm_rsize;
520
521 if (nmp->nm_readdirsize > maxio)
522 nmp->nm_readdirsize = maxio;
523
524 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
525 argp->maxgrouplist <= NFS_MAXGRPS)
526 nmp->nm_numgrps = argp->maxgrouplist;
527 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
528 argp->readahead <= NFS_MAXRAHEAD)
529 nmp->nm_readahead = argp->readahead;
530 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
531 argp->leaseterm <= NQ_MAXLEASE)
532 nmp->nm_leaseterm = argp->leaseterm;
533 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
534 argp->deadthresh <= NQ_NEVERDEAD)
535 nmp->nm_deadthresh = argp->deadthresh;
536
537 adjsock |= ((nmp->nm_sotype != argp->sotype) ||
538 (nmp->nm_soproto != argp->proto));
539 nmp->nm_sotype = argp->sotype;
540 nmp->nm_soproto = argp->proto;
541
542 if (nmp->nm_so && adjsock) {
543 nfs_safedisconnect(nmp);
544 if (nmp->nm_sotype == SOCK_DGRAM)
545 while (nfs_connect(nmp, (struct nfsreq *)0, p)) {
546 printf("nfs_args: retrying connect\n");
547 (void) tsleep((caddr_t)&lbolt,
548 PSOCK, "nfscn3", 0);
549 }
550 }
551 }
552
553 /*
554 * VFS Operations.
555 *
556 * mount system call
557 * It seems a bit dumb to copyinstr() the host and path here and then
558 * memcpy() them in mountnfs(), but I wanted to detect errors before
559 * doing the sockargs() call because sockargs() allocates an mbuf and
560 * an error after that means that I have to release the mbuf.
561 */
562 /* ARGSUSED */
563 int
564 nfs_mount(mp, path, data, ndp, p)
565 struct mount *mp;
566 const char *path;
567 void *data;
568 struct nameidata *ndp;
569 struct proc *p;
570 {
571 int error;
572 struct nfs_args args;
573 struct mbuf *nam;
574 struct nfsmount *nmp = VFSTONFS(mp);
575 struct sockaddr *sa;
576 struct vnode *vp;
577 char *pth, *hst;
578 size_t len;
579 u_char *nfh;
580
581 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
582 if (error)
583 return (error);
584
585 if (mp->mnt_flag & MNT_GETARGS) {
586
587 if (nmp == NULL)
588 return (EIO);
589 if (args.addr != NULL) {
590 sa = mtod(nmp->nm_nam, struct sockaddr *);
591 error = copyout(sa, args.addr, sa->sa_len);
592 if (error)
593 return (error);
594 args.addrlen = sa->sa_len;
595 } else
596 args.addrlen = 0;
597
598 args.version = NFS_ARGSVERSION;
599 args.sotype = nmp->nm_sotype;
600 args.proto = nmp->nm_soproto;
601 args.fh = NULL;
602 args.fhsize = 0;
603 args.flags = nmp->nm_flag;
604 args.wsize = nmp->nm_wsize;
605 args.rsize = nmp->nm_rsize;
606 args.readdirsize = nmp->nm_readdirsize;
607 args.timeo = nmp->nm_timeo;
608 args.retrans = nmp->nm_retry;
609 args.maxgrouplist = nmp->nm_numgrps;
610 args.readahead = nmp->nm_readahead;
611 args.leaseterm = nmp->nm_leaseterm;
612 args.deadthresh = nmp->nm_deadthresh;
613 args.hostname = NULL;
614 return (copyout(&args, data, sizeof(args)));
615 }
616
617 if (args.version != NFS_ARGSVERSION)
618 return (EPROGMISMATCH);
619 #ifdef NFS_V2_ONLY
620 if (args.flags & NFSMNT_NQNFS)
621 return (EPROGUNAVAIL);
622 if (args.flags & NFSMNT_NFSV3)
623 return (EPROGMISMATCH);
624 #endif
625 if (mp->mnt_flag & MNT_UPDATE) {
626 if (nmp == NULL)
627 return (EIO);
628 /*
629 * When doing an update, we can't change from or to
630 * v3 and/or nqnfs, or change cookie translation
631 */
632 args.flags = (args.flags &
633 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
634 (nmp->nm_flag &
635 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
636 nfs_decode_args(nmp, &args, p);
637 return (0);
638 }
639 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
640 return (EINVAL);
641 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
642 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
643 if (error)
644 return (error);
645 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
646 error = copyinstr(path, pth, MNAMELEN - 1, &len);
647 if (error)
648 goto free_nfh;
649 memset(&pth[len], 0, MNAMELEN - len);
650 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
651 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
652 if (error)
653 goto free_pth;
654 memset(&hst[len], 0, MNAMELEN - len);
655 /* sockargs() call must be after above copyin() calls */
656 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
657 if (error)
658 goto free_hst;
659 MCLAIM(nam, &nfs_mowner);
660 args.fh = nfh;
661 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
662
663 free_hst:
664 FREE(hst, M_TEMP);
665 free_pth:
666 FREE(pth, M_TEMP);
667 free_nfh:
668 FREE(nfh, M_TEMP);
669
670 return (error);
671 }
672
673 /*
674 * Common code for mount and mountroot
675 */
676 int
677 mountnfs(argp, mp, nam, pth, hst, vpp, p)
678 struct nfs_args *argp;
679 struct mount *mp;
680 struct mbuf *nam;
681 const char *pth, *hst;
682 struct vnode **vpp;
683 struct proc *p;
684 {
685 struct nfsmount *nmp;
686 struct nfsnode *np;
687 int error;
688 struct vattr *attrs;
689 struct ucred *cr;
690
691 /*
692 * If the number of nfs iothreads to use has never
693 * been set, create a reasonable number of them.
694 */
695
696 if (nfs_niothreads < 0) {
697 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
698 nfs_getset_niothreads(TRUE);
699 }
700
701 if (mp->mnt_flag & MNT_UPDATE) {
702 nmp = VFSTONFS(mp);
703 /* update paths, file handles, etc, here XXX */
704 m_freem(nam);
705 return (0);
706 } else {
707 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
708 M_NFSMNT, M_WAITOK);
709 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
710 mp->mnt_data = nmp;
711 TAILQ_INIT(&nmp->nm_uidlruhead);
712 TAILQ_INIT(&nmp->nm_bufq);
713 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
714 simple_lock_init(&nmp->nm_slock);
715 }
716 vfs_getnewfsid(mp);
717 nmp->nm_mountp = mp;
718
719 #ifndef NFS_V2_ONLY
720 if (argp->flags & NFSMNT_NQNFS)
721 /*
722 * We have to set mnt_maxsymlink to a non-zero value so
723 * that COMPAT_43 routines will know that we are setting
724 * the d_type field in directories (and can zero it for
725 * unsuspecting binaries).
726 */
727 mp->mnt_maxsymlinklen = 1;
728 #endif
729
730 #ifndef NFS_V2_ONLY
731 if ((argp->flags & NFSMNT_NFSV3) == 0)
732 #endif
733 /*
734 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
735 * will fill this in.
736 */
737 nmp->nm_maxfilesize = 0xffffffffLL;
738
739 nmp->nm_timeo = NFS_TIMEO;
740 nmp->nm_retry = NFS_RETRANS;
741 nmp->nm_wsize = NFS_WSIZE;
742 nmp->nm_rsize = NFS_RSIZE;
743 nmp->nm_readdirsize = NFS_READDIRSIZE;
744 nmp->nm_numgrps = NFS_MAXGRPS;
745 nmp->nm_readahead = NFS_DEFRAHEAD;
746 nmp->nm_leaseterm = NQ_DEFLEASE;
747 nmp->nm_deadthresh = NQ_DEADTHRESH;
748 CIRCLEQ_INIT(&nmp->nm_timerhead);
749 nmp->nm_inprog = NULLVP;
750 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p);
751 if (error)
752 goto bad;
753 nmp->nm_nam = nam;
754
755 /* Set up the sockets and per-host congestion */
756 nmp->nm_sotype = argp->sotype;
757 nmp->nm_soproto = argp->proto;
758
759 nfs_decode_args(nmp, argp, p);
760
761 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
762 mp->mnt_dev_bshift = DEV_BSHIFT;
763
764 /*
765 * For Connection based sockets (TCP,...) defer the connect until
766 * the first request, in case the server is not responding.
767 */
768 if (nmp->nm_sotype == SOCK_DGRAM &&
769 (error = nfs_connect(nmp, (struct nfsreq *)0, p)))
770 goto bad;
771
772 /*
773 * This is silly, but it has to be set so that vinifod() works.
774 * We do not want to do an nfs_statvfs() here since we can get
775 * stuck on a dead server and we are holding a lock on the mount
776 * point.
777 */
778 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
779 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
780 if (error)
781 goto bad;
782 *vpp = NFSTOV(np);
783 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
784 VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
785 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
786 cr = crget();
787 cr->cr_uid = attrs->va_uid;
788 cr->cr_gid = attrs->va_gid;
789 cr->cr_ngroups = 0;
790 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
791 crfree(cr);
792 }
793 FREE(attrs, M_TEMP);
794
795 /*
796 * A reference count is needed on the nfsnode representing the
797 * remote root. If this object is not persistent, then backward
798 * traversals of the mount point (i.e. "..") will not work if
799 * the nfsnode gets flushed out of the cache. Ufs does not have
800 * this problem, because one can identify root inodes by their
801 * number == ROOTINO (2). So, just unlock, but no rele.
802 */
803
804 nmp->nm_vnode = *vpp;
805 VOP_UNLOCK(*vpp, 0);
806
807 return (0);
808 bad:
809 nfs_disconnect(nmp);
810 free((caddr_t)nmp, M_NFSMNT);
811 m_freem(nam);
812 return (error);
813 }
814
815 /*
816 * unmount system call
817 */
818 int
819 nfs_unmount(mp, mntflags, p)
820 struct mount *mp;
821 int mntflags;
822 struct proc *p;
823 {
824 struct nfsmount *nmp;
825 struct vnode *vp;
826 int error, flags = 0;
827
828 if (mntflags & MNT_FORCE)
829 flags |= FORCECLOSE;
830 nmp = VFSTONFS(mp);
831 /*
832 * Goes something like this..
833 * - Check for activity on the root vnode (other than ourselves).
834 * - Call vflush() to clear out vnodes for this file system,
835 * except for the root vnode.
836 * - Decrement reference on the vnode representing remote root.
837 * - Close the socket
838 * - Free up the data structures
839 */
840 /*
841 * We need to decrement the ref. count on the nfsnode representing
842 * the remote root. See comment in mountnfs(). The VFS unmount()
843 * has done vput on this vnode, otherwise we would get deadlock!
844 */
845 vp = nmp->nm_vnode;
846 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
847 if (error != 0)
848 return error;
849
850 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
851 vput(vp);
852 return (EBUSY);
853 }
854
855 /*
856 * Must handshake with nqnfs_clientd() if it is active.
857 */
858 nmp->nm_iflag |= NFSMNT_DISMINPROG;
859 while (nmp->nm_inprog != NULLVP)
860 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
861 error = vflush(mp, vp, flags);
862 if (error) {
863 vput(vp);
864 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
865 return (error);
866 }
867
868 /*
869 * We are now committed to the unmount; mark the mount structure
870 * as doomed so that any sleepers kicked awake by nfs_disconnect
871 * will go away cleanly.
872 */
873 nmp->nm_iflag |= NFSMNT_DISMNT;
874
875 /*
876 * There are two reference counts to get rid of here
877 * (see comment in mountnfs()).
878 */
879 vrele(vp);
880 vput(vp);
881 vgone(vp);
882 nfs_disconnect(nmp);
883 m_freem(nmp->nm_nam);
884
885 /*
886 * For NQNFS, let the server daemon free the nfsmount structure.
887 */
888 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
889 free((caddr_t)nmp, M_NFSMNT);
890 return (0);
891 }
892
893 /*
894 * Return root of a filesystem
895 */
896 int
897 nfs_root(mp, vpp)
898 struct mount *mp;
899 struct vnode **vpp;
900 {
901 struct vnode *vp;
902 struct nfsmount *nmp;
903 int error;
904
905 nmp = VFSTONFS(mp);
906 vp = nmp->nm_vnode;
907 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
908 if (error != 0)
909 return error;
910 if (vp->v_type == VNON)
911 vp->v_type = VDIR;
912 vp->v_flag = VROOT;
913 *vpp = vp;
914 return (0);
915 }
916
917 extern int syncprt;
918
919 /*
920 * Flush out the buffer cache
921 */
922 /* ARGSUSED */
923 int
924 nfs_sync(mp, waitfor, cred, p)
925 struct mount *mp;
926 int waitfor;
927 struct ucred *cred;
928 struct proc *p;
929 {
930 struct vnode *vp;
931 int error, allerror = 0;
932
933 /*
934 * Force stale buffer cache information to be flushed.
935 */
936 loop:
937 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
938 /*
939 * If the vnode that we are about to sync is no longer
940 * associated with this mount point, start over.
941 */
942 if (vp->v_mount != mp)
943 goto loop;
944 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
945 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
946 vp->v_uobj.uo_npages == 0))
947 continue;
948 if (vget(vp, LK_EXCLUSIVE))
949 goto loop;
950 error = VOP_FSYNC(vp, cred,
951 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
952 if (error)
953 allerror = error;
954 vput(vp);
955 }
956 return (allerror);
957 }
958
959 /*
960 * NFS flat namespace lookup.
961 * Currently unsupported.
962 */
963 /* ARGSUSED */
964 int
965 nfs_vget(mp, ino, vpp)
966 struct mount *mp;
967 ino_t ino;
968 struct vnode **vpp;
969 {
970
971 return (EOPNOTSUPP);
972 }
973
974 /*
975 * Do that sysctl thang...
976 */
977 static int
978 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
979 {
980 int error;
981
982 nfs_getset_niothreads(0);
983 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
984 if (error || newp == NULL)
985 return (error);
986 nfs_getset_niothreads(1);
987
988 return (0);
989 }
990
991 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
992 {
993
994 sysctl_createv(clog, 0, NULL, NULL,
995 CTLFLAG_PERMANENT,
996 CTLTYPE_NODE, "vfs", NULL,
997 NULL, 0, NULL, 0,
998 CTL_VFS, CTL_EOL);
999 sysctl_createv(clog, 0, NULL, NULL,
1000 CTLFLAG_PERMANENT,
1001 CTLTYPE_NODE, "nfs",
1002 SYSCTL_DESCR("NFS vfs options"),
1003 NULL, 0, NULL, 0,
1004 CTL_VFS, 2, CTL_EOL);
1005 /*
1006 * XXX the "2" above could be dynamic, thereby eliminating one
1007 * more instance of the "number to vfs" mapping problem, but
1008 * "2" is the order as taken from sys/mount.h
1009 */
1010
1011 sysctl_createv(clog, 0, NULL, NULL,
1012 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1013 CTLTYPE_STRUCT, "nfsstats",
1014 SYSCTL_DESCR("NFS operation statistics"),
1015 NULL, 0, &nfsstats, sizeof(nfsstats),
1016 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1017 sysctl_createv(clog, 0, NULL, NULL,
1018 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1019 CTLTYPE_INT, "iothreads",
1020 SYSCTL_DESCR("Number of NFS client processes desired"),
1021 sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1022 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1023 }
1024
1025 /*
1026 * At this point, this should never happen
1027 */
1028 /* ARGSUSED */
1029 int
1030 nfs_fhtovp(mp, fhp, vpp)
1031 struct mount *mp;
1032 struct fid *fhp;
1033 struct vnode **vpp;
1034 {
1035
1036 return (EINVAL);
1037 }
1038
1039 /* ARGSUSED */
1040 int
1041 nfs_checkexp(mp, nam, exflagsp, credanonp)
1042 struct mount *mp;
1043 struct mbuf *nam;
1044 int *exflagsp;
1045 struct ucred **credanonp;
1046 {
1047
1048 return (EINVAL);
1049 }
1050
1051 /*
1052 * Vnode pointer to File handle, should never happen either
1053 */
1054 /* ARGSUSED */
1055 int
1056 nfs_vptofh(vp, fhp)
1057 struct vnode *vp;
1058 struct fid *fhp;
1059 {
1060
1061 return (EINVAL);
1062 }
1063
1064 /*
1065 * Vfs start routine, a no-op.
1066 */
1067 /* ARGSUSED */
1068 int
1069 nfs_start(mp, flags, p)
1070 struct mount *mp;
1071 int flags;
1072 struct proc *p;
1073 {
1074
1075 return (0);
1076 }
1077
1078 /*
1079 * Do operations associated with quotas, not supported
1080 */
1081 /* ARGSUSED */
1082 int
1083 nfs_quotactl(mp, cmd, uid, arg, p)
1084 struct mount *mp;
1085 int cmd;
1086 uid_t uid;
1087 void *arg;
1088 struct proc *p;
1089 {
1090
1091 return (EOPNOTSUPP);
1092 }
1093