xref: /freebsd/sys/fs/devfs/devfs_vfsops.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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 #include "opt_mac.h"
39 #ifndef NODEVFS
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 
51 #include <fs/devfs/devfs.h>
52 
53 MALLOC_DEFINE(M_DEVFS, "DEVFS", "DEVFS data");
54 
55 static vfs_nmount_t	devfs_nmount;
56 static vfs_unmount_t	devfs_unmount;
57 static vfs_root_t	devfs_root;
58 static vfs_statfs_t	devfs_statfs;
59 
60 /*
61  * Mount the filesystem
62  */
63 static int
64 devfs_nmount(mp, ndp, td)
65 	struct mount *mp;
66 	struct nameidata *ndp;
67 	struct thread *td;
68 {
69 	int error;
70 	struct devfs_mount *fmp;
71 	struct vnode *rvp;
72 
73 	error = 0;
74 	/*
75 	 * XXX: flag changes.
76 	 */
77 	if (mp->mnt_flag & MNT_UPDATE)
78 		return (EOPNOTSUPP);
79 
80 	MALLOC(fmp, struct devfs_mount *, sizeof(struct devfs_mount),
81 	    M_DEVFS, M_WAITOK | M_ZERO);
82 	lockinit(&fmp->dm_lock, PVFS, "devfs", 0, LK_NOPAUSE);
83 
84 	mp->mnt_flag |= MNT_LOCAL;
85 #ifdef MAC
86 	mp->mnt_flag |= MNT_MULTILABEL;
87 #endif
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 #ifdef MAC
96 	mac_create_devfs_directory("", 0, fmp->dm_rootdir);
97 #endif
98 	fmp->dm_basedir = fmp->dm_rootdir;
99 	devfs_rules_newmount(fmp, td);
100 
101 	error = devfs_root(mp, &rvp);
102 	if (error) {
103 		lockdestroy(&fmp->dm_lock);
104 		FREE(fmp, M_DEVFS);
105 		return (error);
106 	}
107 	VOP_UNLOCK(rvp, 0, td);
108 
109 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
110 	bcopy("devfs", mp->mnt_stat.f_mntfromname, sizeof("devfs"));
111 	(void)devfs_statfs(mp, &mp->mnt_stat, td);
112 
113 	return (0);
114 }
115 
116 static int
117 devfs_unmount(mp, mntflags, td)
118 	struct mount *mp;
119 	int mntflags;
120 	struct thread *td;
121 {
122 	int error;
123 	int flags = 0;
124 	struct devfs_mount *fmp;
125 
126 	fmp = VFSTODEVFS(mp);
127 	if (mntflags & MNT_FORCE)
128 		flags |= FORCECLOSE;
129 	/* There is 1 extra root vnode reference from devfs_mount(). */
130 	error = vflush(mp, 1, flags);
131 	if (error)
132 		return (error);
133 	devfs_purge(fmp->dm_rootdir);
134 	mp->mnt_data = 0;
135 	lockdestroy(&fmp->dm_lock);
136 	free(fmp, M_DEVFS);
137 	return 0;
138 }
139 
140 /* Return locked reference to root.  */
141 
142 static int
143 devfs_root(mp, vpp)
144 	struct mount *mp;
145 	struct vnode **vpp;
146 {
147 	int error;
148 	struct thread *td;
149 	struct vnode *vp;
150 	struct devfs_mount *dmp;
151 
152 	td = curthread;					/* XXX */
153 	dmp = VFSTODEVFS(mp);
154 	error = devfs_allocv(dmp->dm_rootdir, mp, &vp, td);
155 	if (error)
156 		return (error);
157 	vp->v_vflag |= VV_ROOT;
158 	*vpp = vp;
159 	return (0);
160 }
161 
162 static int
163 devfs_statfs(mp, sbp, td)
164 	struct mount *mp;
165 	struct statfs *sbp;
166 	struct thread *td;
167 {
168 
169 	sbp->f_flags = 0;
170 	sbp->f_bsize = DEV_BSIZE;
171 	sbp->f_iosize = DEV_BSIZE;
172 	sbp->f_blocks = 2;		/* 1K to keep df happy */
173 	sbp->f_bfree = 0;
174 	sbp->f_bavail = 0;
175 	sbp->f_files = 0;
176 	sbp->f_ffree = 0;
177 	if (sbp != &mp->mnt_stat) {
178 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
179 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
180 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
181 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
182 	}
183 	return (0);
184 }
185 
186 static struct vfsops devfs_vfsops = {
187 	NULL,
188 	vfs_stdstart,
189 	devfs_unmount,
190 	devfs_root,
191 	vfs_stdquotactl,
192 	devfs_statfs,
193 	vfs_stdsync,
194 	vfs_stdvget,
195 	vfs_stdfhtovp,
196 	vfs_stdcheckexp,
197 	vfs_stdvptofh,
198 	vfs_stdinit,
199 	vfs_stduninit,
200 	vfs_stdextattrctl,
201 	devfs_nmount,
202 };
203 
204 VFS_SET(devfs_vfsops, devfs, VFCF_SYNTHETIC);
205 #endif
206