xref: /dragonfly/sys/kern/vfs_vfsops.c (revision 678e8cc6)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Implement mp->mnt_ops function call wrappers.
36  *
37  * These wrappers are responsible for handling all MPSAFE issues related to
38  * a mount.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf.h>
44 #include <sys/conf.h>
45 #include <sys/dirent.h>
46 #include <sys/domain.h>
47 #include <sys/eventhandler.h>
48 #include <sys/fcntl.h>
49 #include <sys/kernel.h>
50 #include <sys/kthread.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/namei.h>
56 #include <sys/reboot.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
61 #include <sys/vmmeter.h>
62 #include <sys/vnode.h>
63 #include <sys/vfsops.h>
64 #include <sys/sysmsg.h>
65 
66 #include <machine/limits.h>
67 
68 #include <vm/vm.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_kern.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vnode_pager.h>
77 #include <vm/vm_zone.h>
78 
79 #include <sys/buf2.h>
80 #include <sys/thread2.h>
81 #include <sys/mplock2.h>
82 
83 /*
84  * MPSAFE
85  */
86 int
87 vfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
88 {
89 	VFS_MPLOCK_DECLARE;
90 	int error;
91 
92 	VFS_MPLOCK1(mp);
93 	error = (mp->mnt_op->vfs_mount)(mp, path, data, cred);
94 	if (error == 0) {
95 		/* Create per-filesystem syncer threads if requested */
96 		if ((mp->mnt_flag & MNT_UPDATE) == 0 &&
97 		    (mp->mnt_kern_flag & MNTK_THR_SYNC))
98 			vn_syncer_thr_create(mp);
99 	}
100 	VFS_MPUNLOCK(mp);
101 	return (error);
102 }
103 
104 /*
105  * MPSAFE
106  */
107 int
108 vfs_start(struct mount *mp, int flags)
109 {
110 	VFS_MPLOCK_DECLARE;
111 	int error;
112 
113 	VFS_MPLOCK_FLAG(mp, MNTK_ST_MPSAFE);
114 	error = (mp->mnt_op->vfs_start)(mp, flags);
115 	if (error == 0)
116 		/* do not call vfs_acinit on mount updates */
117 		if ((mp->mnt_flag & MNT_UPDATE) == 0)
118 			error = (mp->mnt_op->vfs_acinit)(mp);
119 	VFS_MPUNLOCK(mp);
120 	if (error == EMOUNTEXIT)
121 		error = 0;
122 	return (error);
123 }
124 
125 /*
126  * MPSAFE
127  */
128 int
129 vfs_unmount(struct mount *mp, int mntflags)
130 {
131 	VFS_MPLOCK_DECLARE;
132 	int error;
133 
134 	VFS_MPLOCK1(mp);
135 	if (mp->mnt_kern_flag & MNTK_THR_SYNC)
136 		vn_syncer_thr_stop(mp);
137 	error = (mp->mnt_op->vfs_acdone)(mp);
138 	if (error == 0)
139 		error = (mp->mnt_op->vfs_unmount)(mp, mntflags);
140 	VFS_MPUNLOCK(mp);
141 	return (error);
142 }
143 
144 /*
145  * MPSAFE
146  */
147 int
148 vfs_root(struct mount *mp, struct vnode **vpp)
149 {
150 	VFS_MPLOCK_DECLARE;
151 	int error;
152 
153 	VFS_MPLOCK1(mp);
154 	error = (mp->mnt_op->vfs_root)(mp, vpp);
155 	VFS_MPUNLOCK(mp);
156 	return (error);
157 }
158 
159 /*
160  * MPSAFE
161  */
162 int
163 vfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
164 	     struct ucred *cred)
165 {
166 	VFS_MPLOCK_DECLARE;
167 	int error;
168 
169 	VFS_MPLOCK1(mp);
170 	error = (mp->mnt_op->vfs_quotactl)(mp, cmds, uid, arg, cred);
171 	VFS_MPUNLOCK(mp);
172 	return (error);
173 }
174 
175 /*
176  * MPSAFE
177  */
178 int
179 vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
180 {
181 	VFS_MPLOCK_DECLARE;
182 	int error;
183 
184 	VFS_MPLOCK1(mp);
185 	error = (mp->mnt_op->vfs_statfs)(mp, sbp, cred);
186 	VFS_MPUNLOCK(mp);
187 	return (error);
188 }
189 
190 int
191 vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
192 {
193 	VFS_MPLOCK_DECLARE;
194 	int error;
195 
196 	VFS_MPLOCK1(mp);
197 	error = (mp->mnt_op->vfs_statvfs)(mp, sbp, cred);
198 	VFS_MPUNLOCK(mp);
199 	return (error);
200 }
201 
202 /*
203  * MPSAFE
204  */
205 int
206 vfs_sync(struct mount *mp, int waitfor)
207 {
208 	VFS_MPLOCK_DECLARE;
209 	int error;
210 
211 	VFS_MPLOCK1(mp);
212 	error = (mp->mnt_op->vfs_sync)(mp, waitfor);
213 	VFS_MPUNLOCK(mp);
214 	return (error);
215 }
216 
217 /*
218  * MPSAFE
219  */
220 int
221 vfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
222 {
223 	VFS_MPLOCK_DECLARE;
224 	int error;
225 
226 	VFS_MPLOCK1(mp);
227 	error = (mp->mnt_op->vfs_vget)(mp, dvp, ino, vpp);
228 	VFS_MPUNLOCK(mp);
229 	return (error);
230 }
231 
232 /*
233  * MPSAFE
234  */
235 int
236 vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
237 	   struct fid *fhp, struct vnode **vpp)
238 {
239 	VFS_MPLOCK_DECLARE;
240 	int error;
241 
242 	VFS_MPLOCK1(mp);
243 	error = (mp->mnt_op->vfs_fhtovp)(mp, rootvp, fhp, vpp);
244 	VFS_MPUNLOCK(mp);
245 	return (error);
246 }
247 
248 /*
249  * MPSAFE
250  */
251 int
252 vfs_checkexp(struct mount *mp, struct sockaddr *nam,
253 	     int *extflagsp, struct ucred **credanonp)
254 {
255 	VFS_MPLOCK_DECLARE;
256 	int error;
257 
258 	VFS_MPLOCK1(mp);
259 	error = (mp->mnt_op->vfs_checkexp)(mp, nam, extflagsp, credanonp);
260 	VFS_MPUNLOCK(mp);
261 	return (error);
262 }
263 
264 /*
265  * MPSAFE
266  */
267 int
268 vfs_vptofh(struct vnode *vp, struct fid *fhp)
269 {
270 	VFS_MPLOCK_DECLARE;
271 	int error;
272 
273 	VFS_MPLOCK1(vp->v_mount);
274 	error = (vp->v_mount->mnt_op->vfs_vptofh)(vp, fhp);
275 	VFS_MPUNLOCK(vp->v_mount);
276 	return (error);
277 }
278 
279 /*
280  * MPSAFE
281  */
282 int
283 vfs_init(struct vfsconf *vfc)
284 {
285 	int error;
286 
287 	get_mplock();
288 	error = (vfc->vfc_vfsops->vfs_init)(vfc);
289 	rel_mplock();
290 
291 	return (error);
292 }
293 
294 /*
295  * MPSAFE
296  */
297 int
298 vfs_uninit(struct vfsconf *vfc, struct vfsconf *vfsp)
299 {
300 	int error;
301 
302 	get_mplock();
303 	error = (vfc->vfc_vfsops->vfs_uninit)(vfsp);
304 	rel_mplock();
305 
306 	return (error);
307 }
308 
309 /*
310  * MPSAFE
311  */
312 int
313 vfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
314 		 int attrnamespace, const char *attrname,
315 		 struct ucred *cred)
316 {
317 	VFS_MPLOCK_DECLARE;
318 	int error;
319 
320 	VFS_MPLOCK1(mp);
321 	error = (mp->mnt_op->vfs_extattrctl)(mp, cmd, vp,
322 					     attrnamespace, attrname,
323 					     cred);
324 	VFS_MPUNLOCK(mp);
325 	return (error);
326 }
327