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