nfs_vfsops.c revision 1.149 1 /* $NetBSD: nfs_vfsops.c,v 1.149 2005/09/19 00:49:52 christos 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.149 2005/09/19 00:49:52 christos 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 proc *));
126
127 /*
128 * nfs statvfs call
129 */
130 int
131 nfs_statvfs(mp, sbp, p)
132 struct mount *mp;
133 struct statvfs *sbp;
134 struct proc *p;
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, p);
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, p, 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, p)
229 struct nfsmount *nmp;
230 struct vnode *vp;
231 struct ucred *cred;
232 struct proc *p;
233 {
234 struct nfsv3_fsinfo *fsp;
235 caddr_t cp;
236 int32_t t1, t2;
237 u_int32_t *tl, pref, xmax;
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, p, 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 xmax = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
257 if (xmax < nmp->nm_wsize && xmax > 0) {
258 nmp->nm_wsize = xmax & ~(NFS_FABLKSIZE - 1);
259 if (nmp->nm_wsize == 0)
260 nmp->nm_wsize = xmax;
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 xmax = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
268 if (xmax < nmp->nm_rsize && xmax > 0) {
269 nmp->nm_rsize = xmax & ~(NFS_FABLKSIZE - 1);
270 if (nmp->nm_rsize == 0)
271 nmp->nm_rsize = xmax;
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 (xmax < nmp->nm_readdirsize && xmax > 0) {
278 nmp->nm_readdirsize = xmax & ~(NFS_DIRFRAGSIZ - 1);
279 if (nmp->nm_readdirsize == 0)
280 nmp->nm_readdirsize = xmax;
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 proc *procp;
309 long n;
310 int error;
311
312 procp = curproc; /* 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(nmp, 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, procp);
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, procp);
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, procp->p_ucred, procp);
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, procp);
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, p)
380 struct nfs_dlmount *ndmntp;
381 const char *mntname; /* mount point name */
382 struct mount **mpp;
383 struct vnode **vpp;
384 struct proc *p;
385 {
386 struct mount *mp;
387 struct mbuf *m;
388 int error;
389
390 vfs_rootmountalloc(MOUNT_NFS, 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, p);
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, p)
424 struct nfsmount *nmp;
425 struct nfs_args *argp;
426 struct proc *p;
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, p)) {
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, p)
567 struct mount *mp;
568 const char *path;
569 void *data;
570 struct nameidata *ndp;
571 struct proc *p;
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 size_t len;
581 u_char *nfh;
582
583 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
584 if (error)
585 return (error);
586
587 if (mp->mnt_flag & MNT_GETARGS) {
588
589 if (nmp == NULL)
590 return (EIO);
591 if (args.addr != NULL) {
592 sa = mtod(nmp->nm_nam, struct sockaddr *);
593 error = copyout(sa, args.addr, sa->sa_len);
594 if (error)
595 return (error);
596 args.addrlen = sa->sa_len;
597 } else
598 args.addrlen = 0;
599
600 args.version = NFS_ARGSVERSION;
601 args.sotype = nmp->nm_sotype;
602 args.proto = nmp->nm_soproto;
603 args.fh = NULL;
604 args.fhsize = 0;
605 args.flags = nmp->nm_flag;
606 args.wsize = nmp->nm_wsize;
607 args.rsize = nmp->nm_rsize;
608 args.readdirsize = nmp->nm_readdirsize;
609 args.timeo = nmp->nm_timeo;
610 args.retrans = nmp->nm_retry;
611 args.maxgrouplist = nmp->nm_numgrps;
612 args.readahead = nmp->nm_readahead;
613 args.leaseterm = nmp->nm_leaseterm;
614 args.deadthresh = nmp->nm_deadthresh;
615 args.hostname = NULL;
616 return (copyout(&args, data, sizeof(args)));
617 }
618
619 if (args.version != NFS_ARGSVERSION)
620 return (EPROGMISMATCH);
621 #ifdef NFS_V2_ONLY
622 if (args.flags & NFSMNT_NQNFS)
623 return (EPROGUNAVAIL);
624 if (args.flags & NFSMNT_NFSV3)
625 return (EPROGMISMATCH);
626 #endif
627 if (mp->mnt_flag & MNT_UPDATE) {
628 if (nmp == NULL)
629 return (EIO);
630 /*
631 * When doing an update, we can't change from or to
632 * v3 and/or nqnfs, or change cookie translation
633 */
634 args.flags = (args.flags &
635 ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
636 (nmp->nm_flag &
637 (NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
638 nfs_decode_args(nmp, &args, p);
639 return (0);
640 }
641 if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
642 return (EINVAL);
643 MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
644 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
645 if (error)
646 return (error);
647 MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
648 error = copyinstr(path, pth, MNAMELEN - 1, &len);
649 if (error)
650 goto free_nfh;
651 memset(&pth[len], 0, MNAMELEN - len);
652 MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
653 error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
654 if (error)
655 goto free_pth;
656 memset(&hst[len], 0, MNAMELEN - len);
657 /* sockargs() call must be after above copyin() calls */
658 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
659 if (error)
660 goto free_hst;
661 MCLAIM(nam, &nfs_mowner);
662 args.fh = nfh;
663 error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
664
665 free_hst:
666 FREE(hst, M_TEMP);
667 free_pth:
668 FREE(pth, M_TEMP);
669 free_nfh:
670 FREE(nfh, M_TEMP);
671
672 return (error);
673 }
674
675 /*
676 * Common code for mount and mountroot
677 */
678 int
679 mountnfs(argp, mp, nam, pth, hst, vpp, p)
680 struct nfs_args *argp;
681 struct mount *mp;
682 struct mbuf *nam;
683 const char *pth, *hst;
684 struct vnode **vpp;
685 struct proc *p;
686 {
687 struct nfsmount *nmp;
688 struct nfsnode *np;
689 int error;
690 struct vattr *attrs;
691 struct ucred *cr;
692
693 /*
694 * If the number of nfs iothreads to use has never
695 * been set, create a reasonable number of them.
696 */
697
698 if (nfs_niothreads < 0) {
699 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
700 nfs_getset_niothreads(TRUE);
701 }
702
703 if (mp->mnt_flag & MNT_UPDATE) {
704 nmp = VFSTONFS(mp);
705 /* update paths, file handles, etc, here XXX */
706 m_freem(nam);
707 return (0);
708 } else {
709 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
710 M_NFSMNT, M_WAITOK);
711 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
712 mp->mnt_data = nmp;
713 TAILQ_INIT(&nmp->nm_uidlruhead);
714 TAILQ_INIT(&nmp->nm_bufq);
715 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
716 simple_lock_init(&nmp->nm_slock);
717 }
718 vfs_getnewfsid(mp);
719 nmp->nm_mountp = mp;
720
721 #ifndef NFS_V2_ONLY
722 if (argp->flags & NFSMNT_NQNFS)
723 mp->mnt_iflag |= IMNT_DTYPE;
724 #endif
725
726 #ifndef NFS_V2_ONLY
727 if ((argp->flags & NFSMNT_NFSV3) == 0)
728 #endif
729 /*
730 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
731 * will fill this in.
732 */
733 nmp->nm_maxfilesize = 0xffffffffLL;
734
735 nmp->nm_timeo = NFS_TIMEO;
736 nmp->nm_retry = NFS_RETRANS;
737 nmp->nm_wsize = NFS_WSIZE;
738 nmp->nm_rsize = NFS_RSIZE;
739 nmp->nm_readdirsize = NFS_READDIRSIZE;
740 nmp->nm_numgrps = NFS_MAXGRPS;
741 nmp->nm_readahead = NFS_DEFRAHEAD;
742 nmp->nm_leaseterm = NQ_DEFLEASE;
743 nmp->nm_deadthresh = NQ_DEADTHRESH;
744 CIRCLEQ_INIT(&nmp->nm_timerhead);
745 nmp->nm_inprog = NULLVP;
746 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, p);
747 if (error)
748 goto bad;
749 nmp->nm_nam = nam;
750
751 /* Set up the sockets and per-host congestion */
752 nmp->nm_sotype = argp->sotype;
753 nmp->nm_soproto = argp->proto;
754
755 nfs_decode_args(nmp, argp, p);
756
757 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
758 mp->mnt_dev_bshift = DEV_BSHIFT;
759
760 /*
761 * For Connection based sockets (TCP,...) defer the connect until
762 * the first request, in case the server is not responding.
763 */
764 if (nmp->nm_sotype == SOCK_DGRAM &&
765 (error = nfs_connect(nmp, (struct nfsreq *)0, p)))
766 goto bad;
767
768 /*
769 * This is silly, but it has to be set so that vinifod() works.
770 * We do not want to do an nfs_statvfs() here since we can get
771 * stuck on a dead server and we are holding a lock on the mount
772 * point.
773 */
774 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
775 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
776 if (error)
777 goto bad;
778 *vpp = NFSTOV(np);
779 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
780 VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
781 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
782 cr = crget();
783 cr->cr_uid = attrs->va_uid;
784 cr->cr_gid = attrs->va_gid;
785 cr->cr_ngroups = 0;
786 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
787 crfree(cr);
788 }
789 FREE(attrs, M_TEMP);
790
791 /*
792 * A reference count is needed on the nfsnode representing the
793 * remote root. If this object is not persistent, then backward
794 * traversals of the mount point (i.e. "..") will not work if
795 * the nfsnode gets flushed out of the cache. Ufs does not have
796 * this problem, because one can identify root inodes by their
797 * number == ROOTINO (2). So, just unlock, but no rele.
798 */
799
800 nmp->nm_vnode = *vpp;
801 VOP_UNLOCK(*vpp, 0);
802
803 return (0);
804 bad:
805 nfs_disconnect(nmp);
806 free((caddr_t)nmp, M_NFSMNT);
807 m_freem(nam);
808 return (error);
809 }
810
811 /*
812 * unmount system call
813 */
814 int
815 nfs_unmount(mp, mntflags, p)
816 struct mount *mp;
817 int mntflags;
818 struct proc *p;
819 {
820 struct nfsmount *nmp;
821 struct vnode *vp;
822 int error, flags = 0;
823
824 if (mntflags & MNT_FORCE)
825 flags |= FORCECLOSE;
826 nmp = VFSTONFS(mp);
827 /*
828 * Goes something like this..
829 * - Check for activity on the root vnode (other than ourselves).
830 * - Call vflush() to clear out vnodes for this file system,
831 * except for the root vnode.
832 * - Decrement reference on the vnode representing remote root.
833 * - Close the socket
834 * - Free up the data structures
835 */
836 /*
837 * We need to decrement the ref. count on the nfsnode representing
838 * the remote root. See comment in mountnfs(). The VFS unmount()
839 * has done vput on this vnode, otherwise we would get deadlock!
840 */
841 vp = nmp->nm_vnode;
842 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
843 if (error != 0)
844 return error;
845
846 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
847 vput(vp);
848 return (EBUSY);
849 }
850
851 /*
852 * Must handshake with nqnfs_clientd() if it is active.
853 */
854 nmp->nm_iflag |= NFSMNT_DISMINPROG;
855 while (nmp->nm_inprog != NULLVP)
856 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
857 error = vflush(mp, vp, flags);
858 if (error) {
859 vput(vp);
860 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
861 return (error);
862 }
863
864 /*
865 * We are now committed to the unmount; mark the mount structure
866 * as doomed so that any sleepers kicked awake by nfs_disconnect
867 * will go away cleanly.
868 */
869 nmp->nm_iflag |= NFSMNT_DISMNT;
870
871 /*
872 * There are two reference counts to get rid of here
873 * (see comment in mountnfs()).
874 */
875 vrele(vp);
876 vput(vp);
877 vgone(vp);
878 nfs_disconnect(nmp);
879 m_freem(nmp->nm_nam);
880
881 /*
882 * For NQNFS, let the server daemon free the nfsmount structure.
883 */
884 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
885 free((caddr_t)nmp, M_NFSMNT);
886 return (0);
887 }
888
889 /*
890 * Return root of a filesystem
891 */
892 int
893 nfs_root(mp, vpp)
894 struct mount *mp;
895 struct vnode **vpp;
896 {
897 struct vnode *vp;
898 struct nfsmount *nmp;
899 int error;
900
901 nmp = VFSTONFS(mp);
902 vp = nmp->nm_vnode;
903 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
904 if (error != 0)
905 return error;
906 if (vp->v_type == VNON)
907 vp->v_type = VDIR;
908 vp->v_flag = VROOT;
909 *vpp = vp;
910 return (0);
911 }
912
913 extern int syncprt;
914
915 /*
916 * Flush out the buffer cache
917 */
918 /* ARGSUSED */
919 int
920 nfs_sync(mp, waitfor, cred, p)
921 struct mount *mp;
922 int waitfor;
923 struct ucred *cred;
924 struct proc *p;
925 {
926 struct vnode *vp;
927 int error, allerror = 0;
928
929 /*
930 * Force stale buffer cache information to be flushed.
931 */
932 loop:
933 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
934 /*
935 * If the vnode that we are about to sync is no longer
936 * associated with this mount point, start over.
937 */
938 if (vp->v_mount != mp)
939 goto loop;
940 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
941 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
942 vp->v_uobj.uo_npages == 0))
943 continue;
944 if (vget(vp, LK_EXCLUSIVE))
945 goto loop;
946 error = VOP_FSYNC(vp, cred,
947 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
948 if (error)
949 allerror = error;
950 vput(vp);
951 }
952 return (allerror);
953 }
954
955 /*
956 * NFS flat namespace lookup.
957 * Currently unsupported.
958 */
959 /* ARGSUSED */
960 int
961 nfs_vget(mp, ino, vpp)
962 struct mount *mp;
963 ino_t ino;
964 struct vnode **vpp;
965 {
966
967 return (EOPNOTSUPP);
968 }
969
970 /*
971 * Do that sysctl thang...
972 */
973 static int
974 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
975 {
976 int error;
977
978 nfs_getset_niothreads(0);
979 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
980 if (error || newp == NULL)
981 return (error);
982 nfs_getset_niothreads(1);
983
984 return (0);
985 }
986
987 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
988 {
989
990 sysctl_createv(clog, 0, NULL, NULL,
991 CTLFLAG_PERMANENT,
992 CTLTYPE_NODE, "vfs", NULL,
993 NULL, 0, NULL, 0,
994 CTL_VFS, CTL_EOL);
995 sysctl_createv(clog, 0, NULL, NULL,
996 CTLFLAG_PERMANENT,
997 CTLTYPE_NODE, "nfs",
998 SYSCTL_DESCR("NFS vfs options"),
999 NULL, 0, NULL, 0,
1000 CTL_VFS, 2, CTL_EOL);
1001 /*
1002 * XXX the "2" above could be dynamic, thereby eliminating one
1003 * more instance of the "number to vfs" mapping problem, but
1004 * "2" is the order as taken from sys/mount.h
1005 */
1006
1007 sysctl_createv(clog, 0, NULL, NULL,
1008 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1009 CTLTYPE_STRUCT, "nfsstats",
1010 SYSCTL_DESCR("NFS operation statistics"),
1011 NULL, 0, &nfsstats, sizeof(nfsstats),
1012 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1013 sysctl_createv(clog, 0, NULL, NULL,
1014 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1015 CTLTYPE_INT, "iothreads",
1016 SYSCTL_DESCR("Number of NFS client processes desired"),
1017 sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1018 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1019 }
1020
1021 /*
1022 * At this point, this should never happen
1023 */
1024 /* ARGSUSED */
1025 int
1026 nfs_fhtovp(mp, fhp, vpp)
1027 struct mount *mp;
1028 struct fid *fhp;
1029 struct vnode **vpp;
1030 {
1031
1032 return (EINVAL);
1033 }
1034
1035 /* ARGSUSED */
1036 int
1037 nfs_checkexp(mp, nam, exflagsp, credanonp)
1038 struct mount *mp;
1039 struct mbuf *nam;
1040 int *exflagsp;
1041 struct ucred **credanonp;
1042 {
1043
1044 return (EINVAL);
1045 }
1046
1047 /*
1048 * Vnode pointer to File handle, should never happen either
1049 */
1050 /* ARGSUSED */
1051 int
1052 nfs_vptofh(vp, fhp)
1053 struct vnode *vp;
1054 struct fid *fhp;
1055 {
1056
1057 return (EINVAL);
1058 }
1059
1060 /*
1061 * Vfs start routine, a no-op.
1062 */
1063 /* ARGSUSED */
1064 int
1065 nfs_start(mp, flags, p)
1066 struct mount *mp;
1067 int flags;
1068 struct proc *p;
1069 {
1070
1071 return (0);
1072 }
1073
1074 /*
1075 * Do operations associated with quotas, not supported
1076 */
1077 /* ARGSUSED */
1078 int
1079 nfs_quotactl(mp, cmd, uid, arg, p)
1080 struct mount *mp;
1081 int cmd;
1082 uid_t uid;
1083 void *arg;
1084 struct proc *p;
1085 {
1086
1087 return (EOPNOTSUPP);
1088 }
1089