xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision 055aefb1bcd6c54859c45274c8e03f03b3f5e681)
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 <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/dirent.h>
41 #include <sys/conf.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/malloc.h>
46 #include <sys/eventhandler.h>
47 
48 #define DEVFS_INTERN
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 proc *p));
55 static int	devfs_unmount __P((struct mount *mp, int mntflags,
56 				  struct proc *p));
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 proc *p));
60 
61 /*
62  * Mount the filesystem
63  */
64 static int
65 devfs_mount(mp, path, data, ndp, p)
66 	struct mount *mp;
67 	char *path;
68 	caddr_t data;
69 	struct nameidata *ndp;
70 	struct proc *p;
71 {
72 	int error = 0;
73 	u_int size;
74 	struct devfs_mount *fmp;
75 	struct vnode *rvp;
76 
77 	/*
78 	 * Update is a no-op
79 	 */
80 	if (mp->mnt_flag & MNT_UPDATE)
81 		return (EOPNOTSUPP);
82 
83 	MALLOC(fmp, struct devfs_mount *, sizeof(struct devfs_mount), M_DEVFS, M_WAITOK);
84 
85 	bzero(fmp, sizeof(*fmp));
86 
87 	error = getnewvnode(VT_DEVFS, mp, devfs_vnodeop_p, &rvp);
88 	if (error) {
89 		FREE(fmp, M_DEVFS);
90 		return (error);
91 	}
92 
93 	vhold(rvp);
94 	rvp->v_type = VDIR;
95 	rvp->v_flag |= VROOT;
96 	mp->mnt_flag |= MNT_LOCAL;
97 	mp->mnt_data = (qaddr_t) fmp;
98 	vfs_getnewfsid(mp);
99 
100 	fmp->dm_inode = NDEVINO;
101 	fmp->dm_root = rvp;
102 	fmp->dm_rootdir = devfs_vmkdir();
103 	rvp->v_data = fmp->dm_rootdir;
104 	TAILQ_FIRST(&fmp->dm_rootdir->dd_list)->de_vnode = rvp;
105 	TAILQ_FIRST(&fmp->dm_rootdir->dd_list)->de_inode = 2;
106 
107 #ifdef DEVFS_DEVBASE
108 	{
109 	struct devfs_dirent *de;
110 
111 	fmp->dm_basedir = devfs_vmkdir();
112 	de = devfs_newdirent("dev", 3);
113 	de->de_inode = fmp->dm_inode++;
114 	de->de_dir = fmp->dm_basedir;
115 	TAILQ_FIRST(&de->de_dir->dd_list)->de_inode = de->de_inode;
116 	de->de_uid = 0;
117 	de->de_gid = 0;
118 	de->de_mode = 0755;
119 	de->de_dirent->d_type = DT_DIR;
120 	TAILQ_INSERT_TAIL(&fmp->dm_rootdir->dd_list, de, de_list);
121 	}
122 #else
123 	fmp->dm_basedir = fmp->dm_rootdir;
124 #endif
125 
126 	if (path != NULL) {
127 		(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
128 	} else {
129 		strcpy(mp->mnt_stat.f_mntonname, "/");
130 		size = 1;
131 	}
132 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
133 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
134 	bcopy("devfs", mp->mnt_stat.f_mntfromname, sizeof("devfs"));
135 	(void)devfs_statfs(mp, &mp->mnt_stat, p);
136 	devfs_populate(fmp);
137 
138 	return (0);
139 }
140 
141 static int
142 devfs_unmount(mp, mntflags, p)
143 	struct mount *mp;
144 	int mntflags;
145 	struct proc *p;
146 {
147 	int error;
148 	int flags = 0;
149 	struct vnode *rootvp = VFSTODEVFS(mp)->dm_root;
150 	struct devfs_mount *fmp;
151 
152 	fmp = (struct devfs_mount*) mp->mnt_data;
153 	if (mntflags & MNT_FORCE)
154 		flags |= FORCECLOSE;
155 
156 	/*
157 	 * Clear out buffer cache.  I don't think we
158 	 * ever get anything cached at this level at the
159 	 * moment, but who knows...
160 	 */
161 	if (rootvp->v_usecount > 2)
162 		return (EBUSY);
163 	devfs_purge(fmp->dm_rootdir);
164 	error = vflush(mp, rootvp, flags);
165 	if (error)
166 		return (error);
167 
168 	/*
169 	 * Release reference on underlying root vnode
170 	 */
171 	vrele(rootvp);
172 	/*
173 	 * And blow it away for future re-use
174 	 */
175 	vgone(rootvp);
176 	/*
177 	 * Finally, throw away the devfs_mount structure
178 	 */
179 	free(mp->mnt_data, M_DEVFS);
180 	mp->mnt_data = 0;
181 	return 0;
182 }
183 
184 static int
185 devfs_root(mp, vpp)
186 	struct mount *mp;
187 	struct vnode **vpp;
188 {
189 	struct proc *p = curproc;	/* XXX */
190 	struct vnode *vp;
191 
192 	/*
193 	 * Return locked reference to root.
194 	 */
195 	vp = VFSTODEVFS(mp)->dm_root;
196 	VREF(vp);
197 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
198 	*vpp = vp;
199 	return (0);
200 }
201 
202 static int
203 devfs_statfs(mp, sbp, p)
204 	struct mount *mp;
205 	struct statfs *sbp;
206 	struct proc *p;
207 {
208 
209 	sbp->f_flags = 0;
210 	sbp->f_bsize = DEV_BSIZE;
211 	sbp->f_iosize = DEV_BSIZE;
212 	sbp->f_blocks = 2;		/* 1K to keep df happy */
213 	sbp->f_bfree = 0;
214 	sbp->f_bavail = 0;
215 	sbp->f_files = 0;
216 	sbp->f_ffree = 0;
217 	if (sbp != &mp->mnt_stat) {
218 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
219 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
220 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
221 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
222 	}
223 	return (0);
224 }
225 
226 static struct vfsops devfs_vfsops = {
227 	devfs_mount,
228 	vfs_stdstart,
229 	devfs_unmount,
230 	devfs_root,
231 	vfs_stdquotactl,
232 	devfs_statfs,
233 	vfs_stdsync,
234 	vfs_stdvget,
235 	vfs_stdfhtovp,
236 	vfs_stdcheckexp,
237 	vfs_stdvptofh,
238 	vfs_stdinit,
239 	vfs_stduninit,
240 	vfs_stdextattrctl,
241 };
242 
243 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
244