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