]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_vfsops.c
xnu-201.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
612 /* Initialize the default IO constraints */
613 mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
614 mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
615
616 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
617 (void)vfs_busy(mp, LK_NOWAIT, 0, procp);
618 LIST_INIT(&mp->mnt_vnodelist);
619 mp->mnt_op = vfsp->vfc_vfsops;
620 mp->mnt_vfc = vfsp;
621 vfsp->vfc_refcount++;
622 mp->mnt_stat.f_type = vfsp->vfc_typenum;
623 mp->mnt_flag = mntflag;
624 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
625 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
626 vp->v_mountedhere = mp;
627 mp->mnt_vnodecovered = vp;
628 mp->mnt_stat.f_owner = procp->p_ucred->cr_uid;
629 (void) copystr(mntname, mp->mnt_stat.f_mntonname, MNAMELEN - 1, 0);
630 (void) copystr(ndmntp->ndm_host, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
631
632 /* Initialize mount args. */
633 bzero((caddr_t) &args, sizeof(args));
634 args.addr = (struct sockaddr *)&ndmntp->ndm_saddr;
635 args.addrlen = args.addr->sa_len;
636 args.sotype = SOCK_DGRAM;
637 args.fh = ndmntp->ndm_fh;
638 args.fhsize = NFSX_V2FH;
639 args.hostname = ndmntp->ndm_host;
640 args.flags = NFSMNT_RESVPORT;
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 * We need to decrement the ref. count on the nfsnode representing
952 * the remote root. See comment in mountnfs(). The VFS unmount()
953 * has done vput on this vnode, otherwise we would get deadlock!
954 */
955 vp = nmp->nm_dvp;
956 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
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 /*
965 * vflush will check for busy vnodes on mountpoint.
966 * Will do the right thing for MNT_FORCE. That is, we should
967 * not get EBUSY back.
968 */
969 error = vflush(mp, vp, SKIPSWAP | flags);
970 if (mntflags & MNT_FORCE)
971 error = vflush(mp, NULLVP, flags);
972 else {
973 if (vp->v_usecount > 1) {
974 VOP_UNLOCK(vp, 0, p);
975 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
976 return (EBUSY);
977 }
978 error = vflush(mp, vp, flags);
979 }
980
981 if (error) {
982 VOP_UNLOCK(vp, 0, p);
983 nmp->nm_flag &= ~NFSMNT_DISMINPROG;
984 return (error);
985 }
986
987 /*
988 * We are now committed to the unmount.
989 * For NQNFS, let the server daemon free the nfsmount structure.
990 */
991 if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
992 nmp->nm_flag |= NFSMNT_DISMNT;
993
994 /*
995 * Release the root vnode reference held by mountnfs()
996 * Note: vflush would have done the vgone for us if we
997 * didn't skip over it due to mount reference held.
998 */
999 vput(vp);
1000 if (!(mntflags & MNT_FORCE))
1001 vgone(vp);
1002 mp->mnt_data = 0; /* don't want to end up using stale vp */
1003 nfs_disconnect(nmp);
1004 m_freem(nmp->nm_nam);
1005
1006 if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0) {
1007 register struct nfsreq *rp;
1008 /*
1009 * Loop through outstanding request list and remove dangling
1010 * references to defunct nfsmount struct
1011 */
1012 #if NFSDIAG && 0
1013 if (hw_atomic_add(&nfsreqqusers, 1) != 1)
1014 nfsatompanic("unmount add");
1015 nfsbtlen = backtrace(&nfsbt, sizeof(nfsbt));
1016 nfsbtcpu = cpu_number();
1017 nfsbtthread = (int)(current_thread());
1018 #endif
1019
1020 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
1021 if (rp->r_nmp == nmp)
1022 rp->r_nmp = (struct nfsmount *)0;
1023 #if NFSDIAG && 0
1024 if (hw_atomic_sub(&nfsreqqusers, 1) != 0)
1025 nfsatompanic("unmount sub");
1026 #endif
1027 _FREE_ZONE((caddr_t)nmp, sizeof (struct nfsmount), M_NFSMNT);
1028 }
1029 return (0);
1030 }
1031
1032 /*
1033 * Return root of a filesystem
1034 */
1035 static int
1036 nfs_root(mp, vpp)
1037 struct mount *mp;
1038 struct vnode **vpp;
1039 {
1040 register struct vnode *vp;
1041 struct nfsmount *nmp;
1042 int error;
1043
1044 nmp = VFSTONFS(mp);
1045 vp = nmp->nm_dvp;
1046 error = vget(vp, LK_EXCLUSIVE, current_proc());
1047 if (error)
1048 return (error);
1049 if (vp->v_type == VNON)
1050 vp->v_type = VDIR;
1051 vp->v_flag |= VROOT;
1052 *vpp = vp;
1053 return (0);
1054 }
1055
1056 extern int syncprt;
1057
1058 /*
1059 * Flush out the buffer cache
1060 */
1061 /* ARGSUSED */
1062 static int
1063 nfs_sync(mp, waitfor, cred, p)
1064 struct mount *mp;
1065 int waitfor;
1066 struct ucred *cred;
1067 struct proc *p;
1068 {
1069 register struct vnode *vp;
1070 int error, allerror = 0;
1071
1072 /*
1073 * Force stale buffer cache information to be flushed.
1074 */
1075 loop:
1076 for (vp = mp->mnt_vnodelist.lh_first;
1077 vp != NULL;
1078 vp = vp->v_mntvnodes.le_next) {
1079 /*
1080 * If the vnode that we are about to sync is no longer
1081 * associated with this mount point, start over.
1082 */
1083 if (vp->v_mount != mp)
1084 goto loop;
1085 if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
1086 continue;
1087 if (vget(vp, LK_EXCLUSIVE, p))
1088 goto loop;
1089 error = VOP_FSYNC(vp, cred, waitfor, p);
1090 if (error)
1091 allerror = error;
1092 vput(vp);
1093 }
1094 return (allerror);
1095 }
1096
1097 /*
1098 * NFS flat namespace lookup.
1099 * Currently unsupported.
1100 */
1101 /* ARGSUSED */
1102 static int
1103 nfs_vget(mp, ino, vpp)
1104 struct mount *mp;
1105 ino_t ino;
1106 struct vnode **vpp;
1107 {
1108
1109 return (EOPNOTSUPP);
1110 }
1111
1112 /*
1113 * At this point, this should never happen
1114 */
1115 /* ARGSUSED */
1116 static int
1117 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1118 register struct mount *mp;
1119 struct fid *fhp;
1120 struct mbuf *nam;
1121 struct vnode **vpp;
1122 int *exflagsp;
1123 struct ucred **credanonp;
1124 {
1125
1126 return (EINVAL);
1127 }
1128
1129 /*
1130 * Vnode pointer to File handle, should never happen either
1131 */
1132 /* ARGSUSED */
1133 static int
1134 nfs_vptofh(vp, fhp)
1135 struct vnode *vp;
1136 struct fid *fhp;
1137 {
1138
1139 return (EINVAL);
1140 }
1141
1142 /*
1143 * Vfs start routine, a no-op.
1144 */
1145 /* ARGSUSED */
1146 static int
1147 nfs_start(mp, flags, p)
1148 struct mount *mp;
1149 int flags;
1150 struct proc *p;
1151 {
1152
1153 return (0);
1154 }
1155
1156 /*
1157 * Do operations associated with quotas, not supported
1158 */
1159 /* ARGSUSED */
1160 static int
1161 nfs_quotactl(mp, cmd, uid, arg, p)
1162 struct mount *mp;
1163 int cmd;
1164 uid_t uid;
1165 caddr_t arg;
1166 struct proc *p;
1167 {
1168
1169 return (EOPNOTSUPP);
1170 }
1171
1172 /*
1173 * Do that sysctl thang...
1174 */
1175 static int
1176 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1177 size_t newlen, struct proc *p)
1178 {
1179 int rv;
1180
1181 /*
1182 * All names at this level are terminal.
1183 */
1184 if(namelen > 1)
1185 return ENOTDIR; /* overloaded */
1186
1187 switch(name[0]) {
1188 case NFS_NFSSTATS:
1189 if(!oldp) {
1190 *oldlenp = sizeof nfsstats;
1191 return 0;
1192 }
1193
1194 if(*oldlenp < sizeof nfsstats) {
1195 *oldlenp = sizeof nfsstats;
1196 return ENOMEM;
1197 }
1198
1199 rv = copyout(&nfsstats, oldp, sizeof nfsstats);
1200 if(rv) return rv;
1201
1202 if(newp && newlen != sizeof nfsstats)
1203 return EINVAL;
1204
1205 if(newp) {
1206 return copyin(newp, &nfsstats, sizeof nfsstats);
1207 }
1208 return 0;
1209
1210 default:
1211 return EOPNOTSUPP;
1212 }
1213 }
1214