nfs_vfsops.c revision 1.160 1 /* $NetBSD: nfs_vfsops.c,v 1.160 2006/08/23 17:19:32 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.160 2006/08/23 17:19:32 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 #include <sys/timetc.h>
62 #include <sys/kauth.h>
63
64 #include <net/if.h>
65 #include <net/route.h>
66 #include <netinet/in.h>
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfsnode.h>
71 #include <nfs/nfs.h>
72 #include <nfs/nfsmount.h>
73 #include <nfs/xdr_subs.h>
74 #include <nfs/nfsm_subs.h>
75 #include <nfs/nfsdiskless.h>
76 #include <nfs/nqnfs.h>
77 #include <nfs/nfs_var.h>
78
79 extern struct nfsstats nfsstats;
80 extern int nfs_ticks;
81
82 /*
83 * keep a count of the nfs mounts to generate ficticious drive names
84 * for the per drive stats.
85 */
86 unsigned int nfs_mount_count = 0;
87
88 MALLOC_DEFINE(M_NFSMNT, "NFS mount", "NFS mount structure");
89
90 /*
91 * nfs vfs operations.
92 */
93
94 extern const struct vnodeopv_desc nfsv2_vnodeop_opv_desc;
95 extern const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc;
96 extern const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc;
97
98 const struct vnodeopv_desc * const nfs_vnodeopv_descs[] = {
99 &nfsv2_vnodeop_opv_desc,
100 &spec_nfsv2nodeop_opv_desc,
101 &fifo_nfsv2nodeop_opv_desc,
102 NULL,
103 };
104
105 struct vfsops nfs_vfsops = {
106 MOUNT_NFS,
107 nfs_mount,
108 nfs_start,
109 nfs_unmount,
110 nfs_root,
111 nfs_quotactl,
112 nfs_statvfs,
113 nfs_sync,
114 nfs_vget,
115 nfs_fhtovp,
116 nfs_vptofh,
117 nfs_vfs_init,
118 nfs_vfs_reinit,
119 nfs_vfs_done,
120 nfs_mountroot,
121 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
122 vfs_stdextattrctl,
123 nfs_vnodeopv_descs,
124 };
125 VFS_ATTACH(nfs_vfsops);
126
127 extern u_int32_t nfs_procids[NFS_NPROCS];
128 extern u_int32_t nfs_prog, nfs_vers;
129
130 static int nfs_mount_diskless __P((struct nfs_dlmount *, const char *,
131 struct mount **, struct vnode **, struct lwp *));
132
133 /*
134 * nfs statvfs call
135 */
136 int
137 nfs_statvfs(mp, sbp, l)
138 struct mount *mp;
139 struct statvfs *sbp;
140 struct lwp *l;
141 {
142 struct vnode *vp;
143 struct nfs_statfs *sfp;
144 caddr_t cp;
145 u_int32_t *tl;
146 int32_t t1, t2;
147 caddr_t bpos, dpos, cp2;
148 struct nfsmount *nmp = VFSTONFS(mp);
149 int error = 0, retattr;
150 #ifdef NFS_V2_ONLY
151 const int v3 = 0;
152 #else
153 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
154 #endif
155 struct mbuf *mreq, *mrep = NULL, *md, *mb;
156 kauth_cred_t cred;
157 u_quad_t tquad;
158 struct nfsnode *np;
159
160 #ifndef nolint
161 sfp = (struct nfs_statfs *)0;
162 #endif
163 vp = nmp->nm_vnode;
164 np = VTONFS(vp);
165 cred = kauth_cred_alloc();
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 kauth_cred_free(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 kauth_cred_t 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_cred, 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 kauth_cred_t cr;
709 char iosname[IOSTATNAMELEN];
710
711 /*
712 * If the number of nfs iothreads to use has never
713 * been set, create a reasonable number of them.
714 */
715
716 if (nfs_niothreads < 0) {
717 nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
718 nfs_getset_niothreads(TRUE);
719 }
720
721 if (mp->mnt_flag & MNT_UPDATE) {
722 nmp = VFSTONFS(mp);
723 /* update paths, file handles, etc, here XXX */
724 m_freem(nam);
725 return (0);
726 } else {
727 MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
728 M_NFSMNT, M_WAITOK);
729 memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
730 mp->mnt_data = nmp;
731 TAILQ_INIT(&nmp->nm_uidlruhead);
732 TAILQ_INIT(&nmp->nm_bufq);
733 lockinit(&nmp->nm_writeverflock, PRIBIO, "nfswverf", 0, 0);
734 simple_lock_init(&nmp->nm_slock);
735 }
736 vfs_getnewfsid(mp);
737 nmp->nm_mountp = mp;
738
739 #ifndef NFS_V2_ONLY
740 if (argp->flags & NFSMNT_NQNFS)
741 mp->mnt_iflag |= IMNT_DTYPE;
742 #endif
743
744 #ifndef NFS_V2_ONLY
745 if ((argp->flags & NFSMNT_NFSV3) == 0)
746 #endif
747 {
748 /*
749 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
750 * will fill this in.
751 */
752 nmp->nm_maxfilesize = 0xffffffffLL;
753 if (argp->fhsize != NFSX_V2FH) {
754 return EINVAL;
755 }
756 }
757
758 nmp->nm_timeo = NFS_TIMEO;
759 nmp->nm_retry = NFS_RETRANS;
760 nmp->nm_wsize = NFS_WSIZE;
761 nmp->nm_rsize = NFS_RSIZE;
762 nmp->nm_readdirsize = NFS_READDIRSIZE;
763 nmp->nm_numgrps = NFS_MAXGRPS;
764 nmp->nm_readahead = NFS_DEFRAHEAD;
765 nmp->nm_leaseterm = NQ_DEFLEASE;
766 nmp->nm_deadthresh = NQ_DEADTHRESH;
767 CIRCLEQ_INIT(&nmp->nm_timerhead);
768 nmp->nm_inprog = NULLVP;
769 error = set_statvfs_info(pth, UIO_SYSSPACE, hst, UIO_SYSSPACE, mp, l);
770 if (error)
771 goto bad;
772 nmp->nm_nam = nam;
773
774 /* Set up the sockets and per-host congestion */
775 nmp->nm_sotype = argp->sotype;
776 nmp->nm_soproto = argp->proto;
777
778 nfs_decode_args(nmp, argp, l);
779
780 mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
781 mp->mnt_dev_bshift = DEV_BSHIFT;
782
783 /*
784 * For Connection based sockets (TCP,...) defer the connect until
785 * the first request, in case the server is not responding.
786 */
787 if (nmp->nm_sotype == SOCK_DGRAM &&
788 (error = nfs_connect(nmp, (struct nfsreq *)0, l)))
789 goto bad;
790
791 /*
792 * This is silly, but it has to be set so that vinifod() works.
793 * We do not want to do an nfs_statvfs() here since we can get
794 * stuck on a dead server and we are holding a lock on the mount
795 * point.
796 */
797 mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
798 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
799 if (error)
800 goto bad;
801 *vpp = NFSTOV(np);
802 MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
803 VOP_GETATTR(*vpp, attrs, l->l_cred, l);
804 if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
805 cr = kauth_cred_alloc();
806 kauth_cred_setuid(cr, attrs->va_uid);
807 kauth_cred_seteuid(cr, attrs->va_uid);
808 kauth_cred_setsvuid(cr, attrs->va_uid);
809 kauth_cred_setgid(cr, attrs->va_gid);
810 kauth_cred_setegid(cr, attrs->va_gid);
811 kauth_cred_setsvgid(cr, attrs->va_gid);
812 nfs_cookieheuristic(*vpp, &nmp->nm_iflag, l, cr);
813 kauth_cred_free(cr);
814 }
815 FREE(attrs, M_TEMP);
816
817 /*
818 * A reference count is needed on the nfsnode representing the
819 * remote root. If this object is not persistent, then backward
820 * traversals of the mount point (i.e. "..") will not work if
821 * the nfsnode gets flushed out of the cache. Ufs does not have
822 * this problem, because one can identify root inodes by their
823 * number == ROOTINO (2). So, just unlock, but no rele.
824 */
825
826 nmp->nm_vnode = *vpp;
827 VOP_UNLOCK(*vpp, 0);
828
829 snprintf(iosname, sizeof(iosname), "nfs%u", nfs_mount_count++);
830 nmp->nm_stats = iostat_alloc(IOSTAT_NFS, nmp, iosname);
831
832 return (0);
833 bad:
834 nfs_disconnect(nmp);
835 free((caddr_t)nmp, M_NFSMNT);
836 m_freem(nam);
837 return (error);
838 }
839
840 /*
841 * unmount system call
842 */
843 int
844 nfs_unmount(mp, mntflags, l)
845 struct mount *mp;
846 int mntflags;
847 struct lwp *l;
848 {
849 struct nfsmount *nmp;
850 struct vnode *vp;
851 int error, flags = 0;
852
853 if (mntflags & MNT_FORCE)
854 flags |= FORCECLOSE;
855 nmp = VFSTONFS(mp);
856 /*
857 * Goes something like this..
858 * - Check for activity on the root vnode (other than ourselves).
859 * - Call vflush() to clear out vnodes for this file system,
860 * except for the root vnode.
861 * - Decrement reference on the vnode representing remote root.
862 * - Close the socket
863 * - Free up the data structures
864 */
865 /*
866 * We need to decrement the ref. count on the nfsnode representing
867 * the remote root. See comment in mountnfs(). The VFS unmount()
868 * has done vput on this vnode, otherwise we would get deadlock!
869 */
870 vp = nmp->nm_vnode;
871 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
872 if (error != 0)
873 return error;
874
875 if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
876 vput(vp);
877 return (EBUSY);
878 }
879
880 /*
881 * Must handshake with nqnfs_clientd() if it is active.
882 */
883 nmp->nm_iflag |= NFSMNT_DISMINPROG;
884 while (nmp->nm_inprog != NULLVP)
885 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
886 error = vflush(mp, vp, flags);
887 if (error) {
888 vput(vp);
889 nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
890 return (error);
891 }
892
893 /*
894 * We are now committed to the unmount; mark the mount structure
895 * as doomed so that any sleepers kicked awake by nfs_disconnect
896 * will go away cleanly.
897 */
898 nmp->nm_iflag |= NFSMNT_DISMNT;
899
900 /*
901 * Clean up the stats... note that we carefully avoid decrementing
902 * nfs_mount_count here for good reason - we may not be unmounting
903 * the last thing mounted.
904 */
905 FREE(nmp->nm_stats->io_name, M_NFSMNT);
906 iostat_free(nmp->nm_stats);
907
908 /*
909 * There are two reference counts to get rid of here
910 * (see comment in mountnfs()).
911 */
912 vrele(vp);
913 vput(vp);
914 vgone(vp);
915 nfs_disconnect(nmp);
916 m_freem(nmp->nm_nam);
917
918 /*
919 * For NQNFS, let the server daemon free the nfsmount structure.
920 */
921 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
922 free((caddr_t)nmp, M_NFSMNT);
923 return (0);
924 }
925
926 /*
927 * Return root of a filesystem
928 */
929 int
930 nfs_root(mp, vpp)
931 struct mount *mp;
932 struct vnode **vpp;
933 {
934 struct vnode *vp;
935 struct nfsmount *nmp;
936 int error;
937
938 nmp = VFSTONFS(mp);
939 vp = nmp->nm_vnode;
940 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
941 if (error != 0)
942 return error;
943 if (vp->v_type == VNON)
944 vp->v_type = VDIR;
945 vp->v_flag = VROOT;
946 *vpp = vp;
947 return (0);
948 }
949
950 extern int syncprt;
951
952 /*
953 * Flush out the buffer cache
954 */
955 /* ARGSUSED */
956 int
957 nfs_sync(mp, waitfor, cred, l)
958 struct mount *mp;
959 int waitfor;
960 kauth_cred_t cred;
961 struct lwp *l;
962 {
963 struct vnode *vp;
964 int error, allerror = 0;
965
966 /*
967 * Force stale buffer cache information to be flushed.
968 */
969 loop:
970 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
971 /*
972 * If the vnode that we are about to sync is no longer
973 * associated with this mount point, start over.
974 */
975 if (vp->v_mount != mp)
976 goto loop;
977 if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
978 (LIST_EMPTY(&vp->v_dirtyblkhd) &&
979 vp->v_uobj.uo_npages == 0))
980 continue;
981 if (vget(vp, LK_EXCLUSIVE))
982 goto loop;
983 error = VOP_FSYNC(vp, cred,
984 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l);
985 if (error)
986 allerror = error;
987 vput(vp);
988 }
989 return (allerror);
990 }
991
992 /*
993 * NFS flat namespace lookup.
994 * Currently unsupported.
995 */
996 /* ARGSUSED */
997 int
998 nfs_vget(mp, ino, vpp)
999 struct mount *mp;
1000 ino_t ino;
1001 struct vnode **vpp;
1002 {
1003
1004 return (EOPNOTSUPP);
1005 }
1006
1007 /*
1008 * Do that sysctl thang...
1009 */
1010 static int
1011 sysctl_vfs_nfs_iothreads(SYSCTLFN_ARGS)
1012 {
1013 int error;
1014
1015 nfs_getset_niothreads(0);
1016 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1017 if (error || newp == NULL)
1018 return (error);
1019 nfs_getset_niothreads(1);
1020
1021 return (0);
1022 }
1023
1024 SYSCTL_SETUP(sysctl_vfs_nfs_setup, "sysctl vfs.nfs subtree setup")
1025 {
1026
1027 sysctl_createv(clog, 0, NULL, NULL,
1028 CTLFLAG_PERMANENT,
1029 CTLTYPE_NODE, "vfs", NULL,
1030 NULL, 0, NULL, 0,
1031 CTL_VFS, CTL_EOL);
1032 sysctl_createv(clog, 0, NULL, NULL,
1033 CTLFLAG_PERMANENT,
1034 CTLTYPE_NODE, "nfs",
1035 SYSCTL_DESCR("NFS vfs options"),
1036 NULL, 0, NULL, 0,
1037 CTL_VFS, 2, CTL_EOL);
1038 /*
1039 * XXX the "2" above could be dynamic, thereby eliminating one
1040 * more instance of the "number to vfs" mapping problem, but
1041 * "2" is the order as taken from sys/mount.h
1042 */
1043
1044 sysctl_createv(clog, 0, NULL, NULL,
1045 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1046 CTLTYPE_STRUCT, "nfsstats",
1047 SYSCTL_DESCR("NFS operation statistics"),
1048 NULL, 0, &nfsstats, sizeof(nfsstats),
1049 CTL_VFS, 2, NFS_NFSSTATS, CTL_EOL);
1050 sysctl_createv(clog, 0, NULL, NULL,
1051 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1052 CTLTYPE_INT, "iothreads",
1053 SYSCTL_DESCR("Number of NFS client processes desired"),
1054 sysctl_vfs_nfs_iothreads, 0, &nfs_niothreads, 0,
1055 CTL_VFS, 2, NFS_IOTHREADS, CTL_EOL);
1056 }
1057
1058 /*
1059 * At this point, this should never happen
1060 */
1061 /* ARGSUSED */
1062 int
1063 nfs_fhtovp(mp, fhp, vpp)
1064 struct mount *mp;
1065 struct fid *fhp;
1066 struct vnode **vpp;
1067 {
1068
1069 return (EINVAL);
1070 }
1071
1072 /*
1073 * Vnode pointer to File handle, should never happen either
1074 */
1075 /* ARGSUSED */
1076 int
1077 nfs_vptofh(vp, fhp, fh_size)
1078 struct vnode *vp;
1079 struct fid *fhp;
1080 size_t *fh_size;
1081 {
1082
1083 return (EINVAL);
1084 }
1085
1086 /*
1087 * Vfs start routine, a no-op.
1088 */
1089 /* ARGSUSED */
1090 int
1091 nfs_start(mp, flags, l)
1092 struct mount *mp;
1093 int flags;
1094 struct lwp *l;
1095 {
1096
1097 return (0);
1098 }
1099
1100 /*
1101 * Do operations associated with quotas, not supported
1102 */
1103 /* ARGSUSED */
1104 int
1105 nfs_quotactl(mp, cmd, uid, arg, l)
1106 struct mount *mp;
1107 int cmd;
1108 uid_t uid;
1109 void *arg;
1110 struct lwp *l;
1111 {
1112
1113 return (EOPNOTSUPP);
1114 }
1115