]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vfsops.c
cda27c84d51ec9646c05562fc079bb1c6d2042ab
[apple/xnu.git] / bsd / nfs / nfs_vfsops.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1989, 1993, 1995
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)nfs_vfsops.c 8.12 (Berkeley) 5/20/95
62 * FreeBSD-Id: nfs_vfsops.c,v 1.52 1997/11/12 05:42:21 julian Exp $
63 */
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/conf.h>
68 #include <sys/ioctl.h>
69 #include <sys/signal.h>
70 #include <sys/proc.h>
71 #include <sys/namei.h>
72 #include <sys/vnode.h>
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/mount.h>
77 #include <sys/buf.h>
78 #include <sys/mbuf.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81
82 #include <sys/vm.h>
83 #include <sys/vmparam.h>
84
85 #if !defined(NO_MOUNT_PRIVATE)
86 #include <sys/filedesc.h>
87 #endif /* NO_MOUNT_PRIVATE */
88
89 #include <net/if.h>
90 #include <net/route.h>
91 #include <netinet/in.h>
92
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
95 #include <nfs/nfs.h>
96 #include <nfs/nfsnode.h>
97 #include <nfs/nfsmount.h>
98 #include <nfs/xdr_subs.h>
99 #include <nfs/nfsm_subs.h>
100 #include <nfs/nfsdiskless.h>
101 #include <nfs/nqnfs.h>
102
103 extern int nfs_mountroot __P((void));
104
105 extern int nfs_ticks;
106
107 struct nfsstats nfsstats;
108 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
109 struct proc *);
110 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
111 #ifdef notyet
112 SYSCTL_NODE(_vfs, MOUNT_NFS, nfs, CTLFLAG_RW, 0, "NFS filesystem");
113 SYSCTL_STRUCT(_vfs_nfs, NFS_NFSSTATS, nfsstats, CTLFLAG_RD,
114 &nfsstats, nfsstats, "");
115 #endif
116 #if NFSDIAG
117 int nfs_debug;
118 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
119 #ifdef notyet
120 SYSCTL_INT(_vfs_nfs, OID_AUTO, debug, CTLFLAG_RW, &nfs_debug, 0, "");
121 #endif
122 #endif
123
124 static int nfs_iosize __P((struct nfsmount *nmp));
125 static int mountnfs __P((struct nfs_args *,struct mount *,
126 struct mbuf *,char *,char *,struct vnode **));
127 static int nfs_mount __P(( struct mount *mp, char *path, caddr_t data,
128 struct nameidata *ndp, struct proc *p));
129 static int nfs_start __P(( struct mount *mp, int flags,
130 struct proc *p));
131 static int nfs_unmount __P(( struct mount *mp, int mntflags,
132 struct proc *p));
133 static int nfs_root __P(( struct mount *mp, struct vnode **vpp));
134 static int nfs_quotactl __P(( struct mount *mp, int cmds, uid_t uid,
135 caddr_t arg, struct proc *p));
136 static int nfs_statfs __P(( struct mount *mp, struct statfs *sbp,
137 struct proc *p));
138 static int nfs_sync __P(( struct mount *mp, int waitfor,
139 struct ucred *cred, struct proc *p));
140 static int nfs_vptofh __P(( struct vnode *vp, struct fid *fhp));
141 static int nfs_fhtovp __P((struct mount *mp, struct fid *fhp,
142 struct mbuf *nam, struct vnode **vpp,
143 int *exflagsp, struct ucred **credanonp));
144 static int nfs_vget __P((struct mount *, ino_t, struct vnode **));
145
146
147 /*
148 * nfs vfs operations.
149 */
150 struct vfsops nfs_vfsops = {
151 nfs_mount,
152 nfs_start,
153 nfs_unmount,
154 nfs_root,
155 nfs_quotactl,
156 nfs_statfs,
157 nfs_sync,
158 nfs_vget,
159 nfs_fhtovp,
160 nfs_vptofh,
161 nfs_init,
162 nfs_sysctl
163 };
164 /* XXX CSM 11/25/97 Mysterious kernel.h ld crud */
165 #ifdef notyet
166 VFS_SET(nfs_vfsops, nfs, MOUNT_NFS, VFCF_NETWORK);
167 #endif
168
169
170 void nfsargs_ntoh __P((struct nfs_args *));
171 static int
172 nfs_mount_diskless __P((struct nfs_dlmount *, char *, int, struct vnode **,
173 struct mount **));
174 #if !defined(NO_MOUNT_PRIVATE)
175 static int
176 nfs_mount_diskless_private __P((struct nfs_dlmount *, char *, int,
177 struct vnode **, struct mount **));
178 #endif /* NO_MOUNT_PRIVATE */
179 static void nfs_convert_oargs __P((struct nfs_args *args,
180 struct onfs_args *oargs));
181 #if NFSDIAG
182 int nfsreqqusers = 0;
183 extern int nfsbtlen, nfsbtcpu, nfsbtthread, nfsbt[32];
184 #endif
185
186 static int nfs_iosize(nmp)
187 struct nfsmount* nmp;
188 {
189 int iosize;
190
191 /*
192 * Calculate the size used for io buffers. Use the larger
193 * of the two sizes to minimise nfs requests but make sure
194 * that it is at least one VM page to avoid wasting buffer
195 * space.
196 */
197 iosize = max(nmp->nm_rsize, nmp->nm_wsize);
198 if (iosize < PAGE_SIZE)
199 iosize = PAGE_SIZE;
200 return (trunc_page_32(iosize));
201 }
202
203 static void nfs_convert_oargs(args,oargs)
204 struct nfs_args *args;
205 struct onfs_args *oargs;
206 {
207 args->version = NFS_ARGSVERSION;
208 args->addr = oargs->addr;
209 args->addrlen = oargs->addrlen;
210 args->sotype = oargs->sotype;
211 args->proto = oargs->proto;
212 args->fh = oargs->fh;
213 args->fhsize = oargs->fhsize;
214 args->flags = oargs->flags;
215 args->wsize = oargs->wsize;
216 args->rsize = oargs->rsize;
217 args->readdirsize = oargs->readdirsize;
218 args->timeo = oargs->timeo;
219 args->retrans = oargs->retrans;
220 args->maxgrouplist = oargs->maxgrouplist;
221 args->readahead = oargs->readahead;
222 args->leaseterm = oargs->leaseterm;
223 args->deadthresh = oargs->deadthresh;
224 args->hostname = oargs->hostname;
225 }
226
227 /*
228 * nfs statfs call
229 */
230 int
231 nfs_statfs(mp, sbp, p)
232 struct mount *mp;
233 register struct statfs *sbp;
234 struct proc *p;
235 {
236 register struct vnode *vp;
237 register struct nfs_statfs *sfp;
238 register caddr_t cp;
239 register u_long *tl;
240 register long t1, t2;
241 caddr_t bpos, dpos, cp2;
242 struct nfsmount *nmp = VFSTONFS(mp);
243 int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
244 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
245 struct ucred *cred;
246 u_quad_t tquad;
247 extern int nfs_mount_type;
248 u_int64_t xid;
249
250 #ifndef nolint
251 sfp = (struct nfs_statfs *)0;
252 #endif
253 vp = nmp->nm_dvp;
254 if (error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p))
255 return(error);
256 cred = crget();
257 cred->cr_ngroups = 1;
258 if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
259 (void)nfs_fsinfo(nmp, vp, cred, p);
260 nfsstats.rpccnt[NFSPROC_FSSTAT]++;
261 nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
262 nfsm_fhtom(vp, v3);
263 nfsm_request(vp, NFSPROC_FSSTAT, p, cred, &xid);
264 if (v3)
265 nfsm_postop_attr(vp, retattr, &xid);
266 nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
267
268 /* XXX CSM 12/2/97 Cleanup when/if we integrate FreeBSD mount.h */
269 #ifdef notyet
270 sbp->f_type = MOUNT_NFS;
271 #else
272 sbp->f_type = nfs_mount_type;
273 #endif
274 sbp->f_flags = nmp->nm_flag;
275 sbp->f_iosize = nfs_iosize(nmp);
276 if (v3) {
277 sbp->f_bsize = NFS_FABLKSIZE;
278 fxdr_hyper(&sfp->sf_tbytes, &tquad);
279 sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
280 fxdr_hyper(&sfp->sf_fbytes, &tquad);
281 sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
282 fxdr_hyper(&sfp->sf_abytes, &tquad);
283 sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
284 sbp->f_files = (fxdr_unsigned(long, sfp->sf_tfiles.nfsuquad[1])
285 & 0x7fffffff);
286 sbp->f_ffree = (fxdr_unsigned(long, sfp->sf_ffiles.nfsuquad[1])
287 & 0x7fffffff);
288 } else {
289 sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
290 sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
291 sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
292 sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
293 sbp->f_files = 0;
294 sbp->f_ffree = 0;
295 }
296 if (sbp != &mp->mnt_stat) {
297 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
298 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
299 }
300 nfsm_reqdone;
301 VOP_UNLOCK(vp, 0, p);
302 crfree(cred);
303 return (error);
304 }
305
306 /*
307 * nfs version 3 fsinfo rpc call
308 */
309 int
310 nfs_fsinfo(nmp, vp, cred, p)
311 register struct nfsmount *nmp;
312 register struct vnode *vp;
313 struct ucred *cred;
314 struct proc *p;
315 {
316 register struct nfsv3_fsinfo *fsp;
317 register caddr_t cp;
318 register long t1, t2;
319 register u_long *tl, pref, max;
320 caddr_t bpos, dpos, cp2;
321 int error = 0, retattr;
322 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
323 u_int64_t xid;
324
325 nfsstats.rpccnt[NFSPROC_FSINFO]++;
326 nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
327 nfsm_fhtom(vp, 1);
328 nfsm_request(vp, NFSPROC_FSINFO, p, cred, &xid);
329 nfsm_postop_attr(vp, retattr, &xid);
330 if (!error) {
331 nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
332 pref = fxdr_unsigned(u_long, fsp->fs_wtpref);
333 if (pref < nmp->nm_wsize)
334 nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
335 ~(NFS_FABLKSIZE - 1);
336 max = fxdr_unsigned(u_long, fsp->fs_wtmax);
337 if (max < nmp->nm_wsize) {
338 nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
339 if (nmp->nm_wsize == 0)
340 nmp->nm_wsize = max;
341 }
342 pref = fxdr_unsigned(u_long, fsp->fs_rtpref);
343 if (pref < nmp->nm_rsize)
344 nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
345 ~(NFS_FABLKSIZE - 1);
346 max = fxdr_unsigned(u_long, fsp->fs_rtmax);
347 if (max < nmp->nm_rsize) {
348 nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
349 if (nmp->nm_rsize == 0)
350 nmp->nm_rsize = max;
351 }
352 pref = fxdr_unsigned(u_long, fsp->fs_dtpref);
353 if (pref < nmp->nm_readdirsize)
354 nmp->nm_readdirsize = pref;
355 if (max < nmp->nm_readdirsize) {
356 nmp->nm_readdirsize = max;
357 }
358 nmp->nm_flag |= NFSMNT_GOTFSINFO;
359 }
360 nfsm_reqdone;
361 return (error);
362 }
363
364 /*
365 * Mount a remote root fs via. nfs. This depends on the info in the
366 * nfs_diskless structure that has been filled in properly by some primary
367 * bootstrap.
368 * It goes something like this:
369 * - do enough of "ifconfig" by calling ifioctl() so that the system
370 * can talk to the server
371 * - If nfs_diskless.mygateway is filled in, use that address as
372 * a default gateway.
373 * - hand craft the swap nfs vnode hanging off a fake mount point
374 * if swdevt[0].sw_dev == NODEV
375 * - build the rootfs mount point and call mountnfs() to do the rest.
376 */
377 int
378 nfs_mountroot()
379 {
380 struct nfs_diskless nd;
381 struct vattr attr;
382 struct mount *mp;
383 struct vnode *vp;
384 struct proc *procp;
385 long n;
386 int error;
387 #if !defined(NO_MOUNT_PRIVATE)
388 struct mount *mppriv;
389 struct vnode *vppriv;
390 #endif /* NO_MOUNT_PRIVATE */
391 int v3;
392
393 procp = current_proc(); /* XXX */
394
395 /*
396 * Call nfs_boot_init() to fill in the nfs_diskless struct.
397 * Note: networking must already have been configured before
398 * we're called.
399 */
400 bzero((caddr_t) &nd, sizeof(nd));
401 error = nfs_boot_init(&nd, procp);
402 if (error) {
403 panic("nfs_boot_init failed with %d\n", error);
404 }
405
406 /* try NFSv3 first, if that fails then try NFSv2 */
407 v3 = 1;
408
409 tryagain:
410 error = nfs_boot_getfh(&nd, procp, v3);
411 if (error) {
412 if (v3) {
413 printf("nfs_boot_getfh(v3) failed with %d, trying v2...\n", error);
414 v3 = 0;
415 goto tryagain;
416 }
417 panic("nfs_boot_getfh(v2) failed with %d\n", error);
418 }
419
420 /*
421 * Create the root mount point.
422 */
423 #if !defined(NO_MOUNT_PRIVATE)
424 if ((error = nfs_mount_diskless(&nd.nd_root, "/", MNT_RDONLY, &vp, &mp))) {
425 #else
426 if (error = nfs_mount_diskless(&nd.nd_root, "/", NULL, &vp, &mp)) {
427 #endif /* NO_MOUNT_PRIVATE */
428 if (v3) {
429 printf("nfs_mount_diskless(v3) failed with %d, trying v2...\n", error);
430 v3 = 0;
431 goto tryagain;
432 }
433 panic("nfs_mount_diskless root failed with %d\n", error);
434 }
435 printf("root on %s\n", (char *)&nd.nd_root.ndm_host);
436
437 simple_lock(&mountlist_slock);
438 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
439 simple_unlock(&mountlist_slock);
440 vfs_unbusy(mp, procp);
441 rootvp = vp;
442
443 #if !defined(NO_MOUNT_PRIVATE)
444 if (nd.nd_private.ndm_saddr.sin_addr.s_addr) {
445 error = nfs_mount_diskless_private(&nd.nd_private, "/private",
446 NULL, &vppriv, &mppriv);
447 if (error) {
448 panic("nfs_mount_diskless private failed with %d\n", error);
449 }
450 printf("private on %s\n", (char *)&nd.nd_private.ndm_host);
451
452 simple_lock(&mountlist_slock);
453 CIRCLEQ_INSERT_TAIL(&mountlist, mppriv, mnt_list);
454 simple_unlock(&mountlist_slock);
455 vfs_unbusy(mppriv, procp);
456 }
457
458 #endif /* NO_MOUNT_PRIVATE */
459
460 if (nd.nd_root.ndm_path)
461 FREE_ZONE(nd.nd_root.ndm_path, MAXPATHLEN, M_NAMEI);
462 if (nd.nd_private.ndm_path)
463 FREE_ZONE(nd.nd_private.ndm_path, MAXPATHLEN, M_NAMEI);
464
465 /* Get root attributes (for the time). */
466 error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
467 if (error) panic("nfs_mountroot: getattr for root");
468 n = attr.va_mtime.tv_sec;
469 inittodr(n);
470 return (0);
471 }
472
473 /*
474 * Internal version of mount system call for diskless setup.
475 */
476 static int
477 nfs_mount_diskless(ndmntp, mntname, mntflag, vpp, mpp)
478 struct nfs_dlmount *ndmntp;
479 char *mntname;
480 int mntflag;
481 struct vnode **vpp;
482 struct mount **mpp;
483 {
484 struct nfs_args args;
485 struct mount *mp;
486 struct mbuf *m;
487 int error;
488 struct proc *procp;
489
490 procp = current_proc(); /* XXX */
491
492 if ((error = vfs_rootmountalloc("nfs", ndmntp->ndm_host, &mp))) {
493 printf("nfs_mountroot: NFS not configured");
494 return (error);
495 }
496 mp->mnt_flag = mntflag;
497
498 /* Initialize mount args. */
499 bzero((caddr_t) &args, sizeof(args));
500 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
501 args.addrlen = args.addr->sa_len;
502 args.sotype = SOCK_DGRAM;
503 args.fh = ndmntp->ndm_fh;
504 args.fhsize = ndmntp->ndm_fhlen;
505 args.hostname = ndmntp->ndm_host;
506 args.flags = NFSMNT_RESVPORT;
507 if (ndmntp->ndm_nfsv3)
508 args.flags |= NFSMNT_NFSV3;
509
510 MGET(m, M_DONTWAIT, MT_SONAME);
511 bcopy((caddr_t)args.addr, mtod(m, caddr_t),
512 (m->m_len = args.addr->sa_len));
513 if ((error = mountnfs(&args, mp, m, mntname, args.hostname, vpp))) {
514 printf("nfs_mountroot: mount %s failed: %d", mntname, error);
515 mp->mnt_vfc->vfc_refcount--;
516 vfs_unbusy(mp, procp);
517 _FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
518 return (error);
519 }
520 #if 0 /* Causes incorrect reporting of "mounted on" */
521 (void) copystr(args.hostname, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
522 #endif /* 0 */
523 *mpp = mp;
524 return (0);
525 }
526
527 #if !defined(NO_MOUNT_PRIVATE)
528 /*
529 * Internal version of mount system call to mount "/private"
530 * separately in diskless setup
531 */
532 static int
533 nfs_mount_diskless_private(ndmntp, mntname, mntflag, vpp, mpp)
534 struct nfs_dlmount *ndmntp;
535 char *mntname;
536 int mntflag;
537 struct vnode **vpp;
538 struct mount **mpp;
539 {
540 struct nfs_args args;
541 struct mount *mp;
542 struct mbuf *m;
543 int error;
544 struct proc *procp;
545 struct vfsconf *vfsp;
546 struct nameidata nd;
547 struct vnode *vp;
548
549 procp = current_proc(); /* XXX */
550
551 {
552 /*
553 * mimic main()!. Temporarily set up rootvnode and other stuff so
554 * that namei works. Need to undo this because main() does it, too
555 */
556 struct filedesc *fdp; /* pointer to file descriptor state */
557 fdp = procp->p_fd;
558 mountlist.cqh_first->mnt_flag |= MNT_ROOTFS;
559
560 /* Get the vnode for '/'. Set fdp->fd_cdir to reference it. */
561 if (VFS_ROOT(mountlist.cqh_first, &rootvnode))
562 panic("cannot find root vnode");
563 VREF(rootvnode);
564 fdp->fd_cdir = rootvnode;
565 VOP_UNLOCK(rootvnode, 0, procp);
566 fdp->fd_rdir = NULL;
567 }
568
569 /*
570 * Get vnode to be covered
571 */
572 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
573 mntname, procp);
574 if ((error = namei(&nd))) {
575 printf("nfs_mountroot: private namei failed!");
576 return (error);
577 }
578 {
579 /* undo VREF in mimic main()! */
580 vrele(rootvnode);
581 }
582 vp = nd.ni_vp;
583 if ((error = vinvalbuf(vp, V_SAVE, procp->p_ucred, procp, 0, 0))) {
584 vput(vp);
585 return (error);
586 }
587 if (vp->v_type != VDIR) {
588 vput(vp);
589 return (ENOTDIR);
590 }
591 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
592 if (!strcmp(vfsp->vfc_name, "nfs"))
593 break;
594 if (vfsp == NULL) {
595 printf("nfs_mountroot: private NFS not configured");
596 vput(vp);
597 return (ENODEV);
598 }
599 if (vp->v_mountedhere != NULL) {
600 vput(vp);
601 return (EBUSY);
602 }
603
604 /*
605 * Allocate and initialize the filesystem.
606 */
607 mp = _MALLOC_ZONE((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
608 bzero((char *)mp, (u_long)sizeof(struct mount));
609
610 /* Initialize the default IO constraints */
611 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
612 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
613
614 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
615 (void)vfs_busy(mp, LK_NOWAIT, 0, procp);
616 LIST_INIT(&mp->mnt_vnodelist);
617 mp->mnt_op = vfsp->vfc_vfsops;
618 mp->mnt_vfc = vfsp;
619 vfsp->vfc_refcount++;
620 mp->mnt_stat.f_type = vfsp->vfc_typenum;
621 mp->mnt_flag = mntflag;
622 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
623 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
624 vp->v_mountedhere = mp;
625 mp->mnt_vnodecovered = vp;
626 mp->mnt_stat.f_owner = procp->p_ucred->cr_uid;
627 (void) copystr(mntname, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
628 (void) copystr(ndmntp->ndm_host, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
629
630 /* Initialize mount args. */
631 bzero((caddr_t) &args, sizeof(args));
632 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
633 args.addrlen = args.addr->sa_len;
634 args.sotype = SOCK_DGRAM;
635 args.fh = ndmntp->ndm_fh;
636 args.fhsize = ndmntp->ndm_fhlen;
637 args.hostname = ndmntp->ndm_host;
638 args.flags = NFSMNT_RESVPORT;
639 if (ndmntp->ndm_nfsv3)
640 args.flags |= NFSMNT_NFSV3;
641
642 MGET(m, M_DONTWAIT, MT_SONAME);
643 bcopy((caddr_t)args.addr, mtod(m, caddr_t),
644 (m->m_len = args.addr->sa_len));
645 if ((error = mountnfs(&args, mp, m, mntname, args.hostname, &vp))) {
646 printf("nfs_mountroot: mount %s failed: %d", mntname, error);
647 mp->mnt_vfc->vfc_refcount--;
648 vfs_unbusy(mp, procp);
649 _FREE_ZONE(mp, sizeof (struct mount), M_MOUNT);
650 return (error);
651 }
652
653 *mpp = mp;
654 *vpp = vp;
655 return (0);
656 }
657 #endif /* NO_MOUNT_PRIVATE */
658
659 /*
660 * VFS Operations.
661 *
662 * mount system call
663 * It seems a bit dumb to copyinstr() the host and path here and then
664 * bcopy() them in mountnfs(), but I wanted to detect errors before
665 * doing the sockargs() call because sockargs() allocates an mbuf and
666 * an error after that means that I have to release the mbuf.
667 */
668 /* ARGSUSED */
669 static int
670 nfs_mount(mp, path, data, ndp, p)
671 struct mount *mp;
672 char *path;
673 caddr_t data;
674 struct nameidata *ndp;
675 struct proc *p;
676 {
677 int error;
678 struct nfs_args args;
679 struct mbuf *nam;
680 struct vnode *vp;
681 char pth[MNAMELEN], hst[MNAMELEN];
682 u_int len;
683 u_char nfh[NFSX_V3FHMAX];
684
685 error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
686 if (error)
687 return (error);
688 if (args.version != NFS_ARGSVERSION) {
689 #ifndef NO_COMPAT_PRELITE2
690 /*
691 * If the argument version is unknown, then assume the
692 * caller is a pre-lite2 4.4BSD client and convert its
693 * arguments.
694 */
695 struct onfs_args oargs;
696 error = copyin(data, (caddr_t)&oargs, sizeof (struct onfs_args));
697 if (error)
698 return (error);
699 nfs_convert_oargs(&args,&oargs);
700 #else /* NO_COMPAT_PRELITE2 */
701 return (EPROGMISMATCH);
702 #endif /* !NO_COMPAT_PRELITE2 */
703 }
704 if (args.fhsize > NFSX_V3FHMAX)
705 return (EINVAL);
706 error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
707 if (error)
708 return (error);
709 error = copyinstr(path, pth, MNAMELEN-1, &len);
710 if (error)
711 return (error);
712 bzero(&pth[len], MNAMELEN - len);
713 error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
714 if (error)
715 return (error);
716 bzero(&hst[len], MNAMELEN - len);
717 /* sockargs() call must be after above copyin() calls */
718 error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
719 if (error)
720 return (error);
721 args.fh = nfh;
722 error = mountnfs(&args, mp, nam, pth, hst, &vp);
723 return (error);
724 }
725
726 /*
727 * Common code for mount and mountroot
728 */
729 static int
730 mountnfs(argp, mp, nam, pth, hst, vpp)
731 register struct nfs_args *argp;
732 register struct mount *mp;
733 struct mbuf *nam;
734 char *pth, *hst;
735 struct vnode **vpp;
736 {
737 register struct nfsmount *nmp;
738 struct nfsnode *np;
739 int error, maxio;
740 struct vattr attrs;
741 struct proc *curproc;
742
743 /*
744 * turning off NQNFS until we have further testing
745 * with UBC changes, in particular, nfs_pagein and nfs_pageout.
746 * Those have NQNFS defined out in conjunction with this
747 * returning an error. Remove when fully tested.
748 */
749 if (argp->flags & NFSMNT_NQNFS) {
750 error = NFSERR_NOTSUPP;
751 goto bad2;
752 }
753
754 if (mp->mnt_flag & MNT_UPDATE) {
755 nmp = VFSTONFS(mp);
756 /* update paths, file handles, etc, here XXX */
757 m_freem(nam);
758 return (0);
759 } else {
760 MALLOC_ZONE(nmp, struct nfsmount *,
761 sizeof (struct nfsmount), M_NFSMNT, M_WAITOK);
762 bzero((caddr_t)nmp, sizeof (struct nfsmount));
763 TAILQ_INIT(&nmp->nm_uidlruhead);
764 TAILQ_INIT(&nmp->nm_bufq);
765 mp->mnt_data = (qaddr_t)nmp;
766 }
767 vfs_getnewfsid(mp);
768 nmp->nm_mountp = mp;
769 nmp->nm_flag = argp->flags;
770 if (nmp->nm_flag & NFSMNT_NQNFS)
771 /*
772 * We have to set mnt_maxsymlink to a non-zero value so
773 * that COMPAT_43 routines will know that we are setting
774 * the d_type field in directories (and can zero it for
775 * unsuspecting binaries).
776 */
777 mp->mnt_maxsymlinklen = 1;
778 nmp->nm_timeo = NFS_TIMEO;
779 nmp->nm_retry = NFS_RETRANS;
780 nmp->nm_wsize = NFS_WSIZE;
781 nmp->nm_rsize = NFS_RSIZE;
782 nmp->nm_readdirsize = NFS_READDIRSIZE;
783 nmp->nm_numgrps = NFS_MAXGRPS;
784 nmp->nm_readahead = NFS_DEFRAHEAD;
785 nmp->nm_leaseterm = NQ_DEFLEASE;
786 nmp->nm_deadthresh = NQ_DEADTHRESH;
787 CIRCLEQ_INIT(&nmp->nm_timerhead);
788 nmp->nm_inprog = NULLVP;
789 bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
790 bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
791 nmp->nm_nam = nam;
792
793 /*
794 * Silently clear NFSMNT_NOCONN if it's a TCP mount, it makes
795 * no sense in that context.
796 */
797 if (argp->sotype == SOCK_STREAM)
798 argp->flags &= ~NFSMNT_NOCONN;
799
800 if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
801 nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
802 if (nmp->nm_timeo < NFS_MINTIMEO)
803 nmp->nm_timeo = NFS_MINTIMEO;
804 else if (nmp->nm_timeo > NFS_MAXTIMEO)
805 nmp->nm_timeo = NFS_MAXTIMEO;
806 }
807
808 if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
809 nmp->nm_retry = argp->retrans;
810 if (nmp->nm_retry > NFS_MAXREXMIT)
811 nmp->nm_retry = NFS_MAXREXMIT;
812 }
813
814 if (argp->flags & NFSMNT_NFSV3) {
815 if (argp->sotype == SOCK_DGRAM)
816 maxio = NFS_MAXDGRAMDATA;
817 else
818 maxio = NFS_MAXDATA;
819 } else
820 maxio = NFS_V2MAXDATA;
821
822 if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
823 nmp->nm_wsize = argp->wsize;
824 /* Round down to multiple of blocksize */
825 nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
826 if (nmp->nm_wsize <= 0)
827 nmp->nm_wsize = NFS_FABLKSIZE;
828 }
829 if (nmp->nm_wsize > maxio)
830 nmp->nm_wsize = maxio;
831 if (nmp->nm_wsize > MAXBSIZE)
832 nmp->nm_wsize = MAXBSIZE;
833
834 if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
835 nmp->nm_rsize = argp->rsize;
836 /* Round down to multiple of blocksize */
837 nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
838 if (nmp->nm_rsize <= 0)
839 nmp->nm_rsize = NFS_FABLKSIZE;
840 }
841 if (nmp->nm_rsize > maxio)
842 nmp->nm_rsize = maxio;
843 if (nmp->nm_rsize > MAXBSIZE)
844 nmp->nm_rsize = MAXBSIZE;
845
846 if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
847 nmp->nm_readdirsize = argp->readdirsize;
848 }
849 if (nmp->nm_readdirsize > maxio)
850 nmp->nm_readdirsize = maxio;
851 if (nmp->nm_readdirsize > nmp->nm_rsize)
852 nmp->nm_readdirsize = nmp->nm_rsize;
853
854 if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
855 argp->maxgrouplist <= NFS_MAXGRPS)
856 nmp->nm_numgrps = argp->maxgrouplist;
857 if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
858 argp->readahead <= NFS_MAXRAHEAD)
859 nmp->nm_readahead = argp->readahead;
860 if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
861 argp->leaseterm <= NQ_MAXLEASE)
862 nmp->nm_leaseterm = argp->leaseterm;
863 if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
864 argp->deadthresh <= NQ_NEVERDEAD)
865 nmp->nm_deadthresh = argp->deadthresh;
866 /* Set up the sockets and per-host congestion */
867 nmp->nm_sotype = argp->sotype;
868 nmp->nm_soproto = argp->proto;
869
870 /*
871 * For Connection based sockets (TCP,...) defer the connect until
872 * the first request, in case the server is not responding.
873 */
874 if (nmp->nm_sotype == SOCK_DGRAM &&
875 (error = nfs_connect(nmp, (struct nfsreq *)0)))
876 goto bad;
877
878 /*
879 * This is silly, but it has to be set so that vinifod() works.
880 * We do not want to do an nfs_statfs() here since we can get
881 * stuck on a dead server and we are holding a lock on the mount
882 * point.
883 */
884 mp->mnt_stat.f_iosize = nfs_iosize(nmp);
885 /*
886 * A reference count is needed on the nfsnode representing the
887 * remote root. If this object is not persistent, then backward
888 * traversals of the mount point (i.e. "..") will not work if
889 * the nfsnode gets flushed out of the cache. UFS does not have
890 * this problem, because one can identify root inodes by their
891 * number == ROOTINO (2).
892 */
893 error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
894 if (error)
895 goto bad;
896
897 /*
898 * save this vnode pointer. That way nfs_unmount()
899 * does not need to call nfs_net() just get it to drop
900 * this vnode reference.
901 */
902 nmp->nm_dvp = *vpp = NFSTOV(np);
903
904 /*
905 * Get file attributes for the mountpoint. This has the side
906 * effect of filling in (*vpp)->v_type with the correct value.
907 */
908 curproc = current_proc();
909 VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc);
910
911 /*
912 * Lose the lock but keep the ref.
913 */
914 VOP_UNLOCK(*vpp, 0, curproc);
915
916 return (0);
917 bad:
918 nfs_disconnect(nmp);
919 _FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
920 bad2:
921 m_freem(nam);
922 return (error);
923 }
924
925
926 /*
927 * unmount system call
928 */
929 static int
930 nfs_unmount(mp, mntflags, p)
931 struct mount *mp;
932 int mntflags;
933 struct proc *p;
934 {
935 register struct nfsmount *nmp;
936 struct vnode *vp;
937 int error, flags = 0;
938
939 if (mntflags & MNT_FORCE)
940 flags |= FORCECLOSE;
941 nmp = VFSTONFS(mp);
942 /*
943 * Goes something like this..
944 * - Call vflush() to clear out vnodes for this file system,
945 * except for the swap files. Deal with them in 2nd pass.
946 * It will do vgone making the vnode VBAD at that time.
947 * - Decrement reference on the vnode representing remote root.
948 * - Close the socket
949 * - Free up the data structures
950 */
951 vp = nmp->nm_dvp;
952
953 /*
954 * Must handshake with nqnfs_clientd() if it is active.
955 */
956 nmp->nm_flag |= NFSMNT_DISMINPROG;
957 while (nmp->nm_inprog != NULLVP)
958 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
959 /*
960 * vflush will check for busy vnodes on mountpoint.
961 * Will do the right thing for MNT_FORCE. That is, we should
962 * not get EBUSY back.
963 */
964 error = vflush(mp, vp, SKIPSWAP | flags);
965 if (mntflags & MNT_FORCE)
966 error = vflush(mp, NULLVP, flags); /* locks vp in the process */
967 else {
968 if (vp->v_usecount > 1) {
969 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
970 return (EBUSY);
971 }
972 error = vflush(mp, vp, flags);
973 }
974
975 if (error) {
976 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
977 return (error);
978 }
979
980 /*
981 * We are now committed to the unmount.
982 * For NQNFS, let the server daemon free the nfsmount structure.
983 */
984 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
985 nmp->nm_flag |= NFSMNT_DISMNT;
986
987 /*
988 * Release the root vnode reference held by mountnfs()
989 * vflush did the vgone for us when we didn't skip over
990 * it in the MNT_FORCE case. (Thus vp can't be locked when
991 * called vflush in non-skip vp case.)
992 */
993 vrele(vp);
994 if (!(mntflags & MNT_FORCE))
995 vgone(vp);
996 mp->mnt_data = 0; /* don't want to end up using stale vp */
997 nfs_disconnect(nmp);
998 m_freem(nmp->nm_nam);
999
1000 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) {
1001 register struct nfsreq *rp;
1002 /*
1003 * Loop through outstanding request list and remove dangling
1004 * references to defunct nfsmount struct
1005 */
1006 #if NFSDIAG && 0
1007 if (hw_atomic_add(&nfsreqqusers, 1) != 1)
1008 nfsatompanic("unmount add");
1009 nfsbtlen = backtrace(&nfsbt, sizeof(nfsbt));
1010 nfsbtcpu = cpu_number();
1011 nfsbtthread = (int)(current_thread());
1012 #endif
1013
1014 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
1015 if (rp->r_nmp == nmp)
1016 rp->r_nmp = (struct nfsmount *)0;
1017 #if NFSDIAG && 0
1018 if (hw_atomic_sub(&nfsreqqusers, 1) != 0)
1019 nfsatompanic("unmount sub");
1020 #endif
1021 _FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1022 }
1023 return (0);
1024 }
1025
1026 /*
1027 * Return root of a filesystem
1028 */
1029 static int
1030 nfs_root(mp, vpp)
1031 struct mount *mp;
1032 struct vnode **vpp;
1033 {
1034 register struct vnode *vp;
1035 struct nfsmount *nmp;
1036 int error;
1037
1038 nmp = VFSTONFS(mp);
1039 vp = nmp->nm_dvp;
1040 error = vget(vp, LK_EXCLUSIVE, current_proc());
1041 if (error)
1042 return (error);
1043 if (vp->v_type == VNON)
1044 vp->v_type = VDIR;
1045 vp->v_flag |= VROOT;
1046 *vpp = vp;
1047 return (0);
1048 }
1049
1050 extern int syncprt;
1051
1052 /*
1053 * Flush out the buffer cache
1054 */
1055 /* ARGSUSED */
1056 static int
1057 nfs_sync(mp, waitfor, cred, p)
1058 struct mount *mp;
1059 int waitfor;
1060 struct ucred *cred;
1061 struct proc *p;
1062 {
1063 register struct vnode *vp;
1064 int error, allerror = 0;
1065
1066 /*
1067 * Force stale buffer cache information to be flushed.
1068 */
1069 loop:
1070 for (vp = mp->mnt_vnodelist.lh_first;
1071 vp != NULL;
1072 vp = vp->v_mntvnodes.le_next) {
1073 int didhold = 0;
1074 /*
1075 * If the vnode that we are about to sync is no longer
1076 * associated with this mount point, start over.
1077 */
1078 if (vp->v_mount != mp)
1079 goto loop;
1080 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
1081 continue;
1082 if (vget(vp, LK_EXCLUSIVE, p))
1083 goto loop;
1084 didhold = ubc_hold(vp);
1085 error = VOP_FSYNC(vp, cred, waitfor, p);
1086 if (error)
1087 allerror = error;
1088 VOP_UNLOCK(vp, 0, p);
1089 if (didhold)
1090 ubc_rele(vp);
1091 vrele(vp);
1092 }
1093 return (allerror);
1094 }
1095
1096 /*
1097 * NFS flat namespace lookup.
1098 * Currently unsupported.
1099 */
1100 /* ARGSUSED */
1101 static int
1102 nfs_vget(mp, ino, vpp)
1103 struct mount *mp;
1104 ino_t ino;
1105 struct vnode **vpp;
1106 {
1107
1108 return (EOPNOTSUPP);
1109 }
1110
1111 /*
1112 * At this point, this should never happen
1113 */
1114 /* ARGSUSED */
1115 static int
1116 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1117 register struct mount *mp;
1118 struct fid *fhp;
1119 struct mbuf *nam;
1120 struct vnode **vpp;
1121 int *exflagsp;
1122 struct ucred **credanonp;
1123 {
1124
1125 return (EINVAL);
1126 }
1127
1128 /*
1129 * Vnode pointer to File handle, should never happen either
1130 */
1131 /* ARGSUSED */
1132 static int
1133 nfs_vptofh(vp, fhp)
1134 struct vnode *vp;
1135 struct fid *fhp;
1136 {
1137
1138 return (EINVAL);
1139 }
1140
1141 /*
1142 * Vfs start routine, a no-op.
1143 */
1144 /* ARGSUSED */
1145 static int
1146 nfs_start(mp, flags, p)
1147 struct mount *mp;
1148 int flags;
1149 struct proc *p;
1150 {
1151
1152 return (0);
1153 }
1154
1155 /*
1156 * Do operations associated with quotas, not supported
1157 */
1158 /* ARGSUSED */
1159 static int
1160 nfs_quotactl(mp, cmd, uid, arg, p)
1161 struct mount *mp;
1162 int cmd;
1163 uid_t uid;
1164 caddr_t arg;
1165 struct proc *p;
1166 {
1167
1168 return (EOPNOTSUPP);
1169 }
1170
1171 /*
1172 * Do that sysctl thang...
1173 */
1174 static int
1175 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1176 size_t newlen, struct proc *p)
1177 {
1178 int rv;
1179
1180 /*
1181 * All names at this level are terminal.
1182 */
1183 if(namelen > 1)
1184 return ENOTDIR; /* overloaded */
1185
1186 switch(name[0]) {
1187 case NFS_NFSSTATS:
1188 if(!oldp) {
1189 *oldlenp = sizeof nfsstats;
1190 return 0;
1191 }
1192
1193 if(*oldlenp < sizeof nfsstats) {
1194 *oldlenp = sizeof nfsstats;
1195 return ENOMEM;
1196 }
1197
1198 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
1199 if(rv) return rv;
1200
1201 if(newp && newlen != sizeof nfsstats)
1202 return EINVAL;
1203
1204 if(newp) {
1205 return copyin(newp, &nfsstats, sizeof nfsstats);
1206 }
1207 return 0;
1208
1209 default:
1210 return EOPNOTSUPP;
1211 }
1212 }
1213