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