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