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