xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2000
5  *	Poul-Henning Kamp.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
32  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
33  *
34  * $FreeBSD$
35  */
36 
37 #include "opt_devfs.h"
38 #ifndef NODEVFS
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 
49 #include <fs/devfs/devfs.h>
50 
51 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data");
52 
53 static int	devfs_mount __P((struct mount *mp, char *path, caddr_t data,
54 				  struct nameidata *ndp, struct thread *td));
55 static int	devfs_unmount __P((struct mount *mp, int mntflags,
56 				  struct thread *td));
57 static int	devfs_root __P((struct mount *mp, struct vnode **vpp));
58 static int	devfs_statfs __P((struct mount *mp, struct statfs *sbp,
59 				   struct thread *td));
60 
61 /*
62  * Mount the filesystem
63  */
64 static int
65 devfs_mount(mp, path, data, ndp, td)
66 	struct mount *mp;
67 	char *path;
68 	caddr_t data;
69 	struct nameidata *ndp;
70 	struct thread *td;
71 {
72 	int error;
73 	struct devfs_mount *fmp;
74 	struct vnode *rvp;
75 
76 	error = 0;
77 	/*
78 	 * XXX: flag changes.
79 	 */
80 	if (mp->mnt_flag & MNT_UPDATE)
81 		return (EOPNOTSUPP);
82 
83 	MALLOC(fmp, struct devfs_mount *, sizeof(struct devfs_mount),
84 	    M_DEVFS, M_WAITOK | M_ZERO);
85 	lockinit(&fmp->dm_lock, PVFS, "devfs", 0, LK_NOPAUSE);
86 
87 	mp->mnt_flag |= MNT_LOCAL;
88 	mp->mnt_data = (qaddr_t) fmp;
89 	vfs_getnewfsid(mp);
90 
91 	fmp->dm_inode = DEVFSINOMOUNT;
92 
93 	fmp->dm_rootdir = devfs_vmkdir("(root)", 6, NULL);
94 	fmp->dm_rootdir->de_inode = 2;
95 	fmp->dm_basedir = fmp->dm_rootdir;
96 
97 	error = devfs_root(mp, &rvp);
98 	if (error) {
99 		lockdestroy(&fmp->dm_lock);
100 		FREE(fmp, M_DEVFS);
101 		return (error);
102 	}
103 	VOP_UNLOCK(rvp, 0, td);
104 
105 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
106 	bcopy("devfs", mp->mnt_stat.f_mntfromname, sizeof("devfs"));
107 	(void)devfs_statfs(mp, &mp->mnt_stat, td);
108 
109 	return (0);
110 }
111 
112 static int
113 devfs_unmount(mp, mntflags, td)
114 	struct mount *mp;
115 	int mntflags;
116 	struct thread *td;
117 {
118 	int error;
119 	int flags = 0;
120 	struct devfs_mount *fmp;
121 
122 	fmp = VFSTODEVFS(mp);
123 	if (mntflags & MNT_FORCE)
124 		flags |= FORCECLOSE;
125 	/* There is 1 extra root vnode reference from devfs_mount(). */
126 	error = vflush(mp, 1, flags);
127 	if (error)
128 		return (error);
129 	devfs_purge(fmp->dm_rootdir);
130 	mp->mnt_data = 0;
131 	lockdestroy(&fmp->dm_lock);
132 	free(fmp, M_DEVFS);
133 	return 0;
134 }
135 
136 /* Return locked reference to root.  */
137 
138 static int
139 devfs_root(mp, vpp)
140 	struct mount *mp;
141 	struct vnode **vpp;
142 {
143 	int error;
144 	struct thread *td;
145 	struct vnode *vp;
146 	struct devfs_mount *dmp;
147 
148 	td = curthread;					/* XXX */
149 	dmp = VFSTODEVFS(mp);
150 	error = devfs_allocv(dmp->dm_rootdir, mp, &vp, td);
151 	if (error)
152 		return (error);
153 	vp->v_flag |= VROOT;
154 	*vpp = vp;
155 	return (0);
156 }
157 
158 static int
159 devfs_statfs(mp, sbp, td)
160 	struct mount *mp;
161 	struct statfs *sbp;
162 	struct thread *td;
163 {
164 
165 	sbp->f_flags = 0;
166 	sbp->f_bsize = DEV_BSIZE;
167 	sbp->f_iosize = DEV_BSIZE;
168 	sbp->f_blocks = 2;		/* 1K to keep df happy */
169 	sbp->f_bfree = 0;
170 	sbp->f_bavail = 0;
171 	sbp->f_files = 0;
172 	sbp->f_ffree = 0;
173 	if (sbp != &mp->mnt_stat) {
174 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
175 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
176 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
177 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
178 	}
179 	return (0);
180 }
181 
182 static struct vfsops devfs_vfsops = {
183 	devfs_mount,
184 	vfs_stdstart,
185 	devfs_unmount,
186 	devfs_root,
187 	vfs_stdquotactl,
188 	devfs_statfs,
189 	vfs_stdsync,
190 	vfs_stdvget,
191 	vfs_stdfhtovp,
192 	vfs_stdcheckexp,
193 	vfs_stdvptofh,
194 	vfs_stdinit,
195 	vfs_stduninit,
196 	vfs_stdextattrctl,
197 };
198 
199 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
200 #endif
201