mfs_vfsops.c revision 1.87 1 /* $NetBSD: mfs_vfsops.c,v 1.87 2008/01/25 10:30:20 pooka Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. 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 * @(#)mfs_vfsops.c 8.11 (Berkeley) 6/19/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: mfs_vfsops.c,v 1.87 2008/01/25 10:30:20 pooka Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/time.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/buf.h>
48 #include <sys/bufq.h>
49 #include <sys/mount.h>
50 #include <sys/signalvar.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53
54 #include <miscfs/specfs/specdev.h>
55 #include <miscfs/syncfs/syncfs.h>
56
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/ufs_extern.h>
61
62 #include <ufs/ffs/fs.h>
63 #include <ufs/ffs/ffs_extern.h>
64
65 #include <ufs/mfs/mfsnode.h>
66 #include <ufs/mfs/mfs_extern.h>
67
68 void * mfs_rootbase; /* address of mini-root in kernel virtual memory */
69 u_long mfs_rootsize; /* size of mini-root in bytes */
70
71 static int mfs_minor; /* used for building internal dev_t */
72
73 extern int (**mfs_vnodeop_p)(void *);
74
75 MALLOC_JUSTDEFINE(M_MFSNODE, "MFS node", "MFS vnode private part");
76
77 /*
78 * mfs vfs operations.
79 */
80
81 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
82
83 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
84 &mfs_vnodeop_opv_desc,
85 NULL,
86 };
87
88 struct vfsops mfs_vfsops = {
89 MOUNT_MFS,
90 sizeof (struct mfs_args),
91 mfs_mount,
92 mfs_start,
93 ffs_unmount,
94 ufs_root,
95 ufs_quotactl,
96 mfs_statvfs,
97 ffs_sync,
98 ffs_vget,
99 ffs_fhtovp,
100 ffs_vptofh,
101 mfs_init,
102 mfs_reinit,
103 mfs_done,
104 NULL,
105 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
106 vfs_stdextattrctl,
107 (void *)eopnotsupp, /* vfs_suspendctl */
108 mfs_vnodeopv_descs,
109 0,
110 { NULL, NULL },
111 };
112 VFS_ATTACH(mfs_vfsops);
113
114 SYSCTL_SETUP(sysctl_vfs_mfs_setup, "sysctl vfs.mfs subtree setup")
115 {
116
117 sysctl_createv(clog, 0, NULL, NULL,
118 CTLFLAG_PERMANENT,
119 CTLTYPE_NODE, "vfs", NULL,
120 NULL, 0, NULL, 0,
121 CTL_VFS, CTL_EOL);
122 sysctl_createv(clog, 0, NULL, NULL,
123 CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
124 CTLTYPE_NODE, "mfs",
125 SYSCTL_DESCR("Memory based file system"),
126 NULL, 1, NULL, 0,
127 CTL_VFS, 3, CTL_EOL);
128 /*
129 * XXX the "1" and the "3" above could be dynamic, thereby
130 * eliminating one more instance of the "number to vfs"
131 * mapping problem, but they are in order as taken from
132 * sys/mount.h
133 */
134 }
135
136 /*
137 * Memory based filesystem initialization.
138 */
139 void
140 mfs_init(void)
141 {
142
143 malloc_type_attach(M_MFSNODE);
144 /*
145 * ffs_init() ensures to initialize necessary resources
146 * only once.
147 */
148 ffs_init();
149 }
150
151 void
152 mfs_reinit(void)
153 {
154 ffs_reinit();
155 }
156
157 void
158 mfs_done(void)
159 {
160 /*
161 * ffs_done() ensures to free necessary resources
162 * only once, when it's no more needed.
163 */
164 ffs_done();
165 malloc_type_detach(M_MFSNODE);
166 }
167
168 /*
169 * Called by main() when mfs is going to be mounted as root.
170 */
171
172 int
173 mfs_mountroot(void)
174 {
175 struct fs *fs;
176 struct mount *mp;
177 struct lwp *l = curlwp; /* XXX */
178 struct ufsmount *ump;
179 struct mfsnode *mfsp;
180 int error = 0;
181
182 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
183 vrele(rootvp);
184 return (error);
185 }
186
187 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
188 rootvp->v_data = mfsp;
189 rootvp->v_op = mfs_vnodeop_p;
190 rootvp->v_tag = VT_MFS;
191 mfsp->mfs_baseoff = mfs_rootbase;
192 mfsp->mfs_size = mfs_rootsize;
193 mfsp->mfs_vnode = rootvp;
194 mfsp->mfs_proc = NULL; /* indicate kernel space */
195 mfsp->mfs_shutdown = 0;
196 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
197 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
198 mp->mnt_op->vfs_refcount--;
199 vfs_unbusy(mp);
200 bufq_free(mfsp->mfs_buflist);
201 vfs_destroy(mp);
202 free(mfsp, M_MFSNODE);
203 return (error);
204 }
205 mutex_enter(&mountlist_lock);
206 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
207 mutex_exit(&mountlist_lock);
208 mp->mnt_vnodecovered = NULLVP;
209 ump = VFSTOUFS(mp);
210 fs = ump->um_fs;
211 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
212 (void)ffs_statvfs(mp, &mp->mnt_stat);
213 vfs_unbusy(mp);
214 return (0);
215 }
216
217 /*
218 * This is called early in boot to set the base address and size
219 * of the mini-root.
220 */
221 int
222 mfs_initminiroot(void *base)
223 {
224 struct fs *fs = (struct fs *)((char *)base + SBLOCK_UFS1);
225
226 /* check for valid super block */
227 if (fs->fs_magic != FS_UFS1_MAGIC || fs->fs_bsize > MAXBSIZE ||
228 fs->fs_bsize < sizeof(struct fs))
229 return (0);
230 mountroot = mfs_mountroot;
231 mfs_rootbase = base;
232 mfs_rootsize = fs->fs_fsize * fs->fs_size;
233 rootdev = makedev(255, mfs_minor);
234 mfs_minor++;
235 return (mfs_rootsize);
236 }
237
238 /*
239 * VFS Operations.
240 *
241 * mount system call
242 */
243 /* ARGSUSED */
244 int
245 mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
246 {
247 struct lwp *l = curlwp;
248 struct vnode *devvp;
249 struct mfs_args *args = data;
250 struct ufsmount *ump;
251 struct fs *fs;
252 struct mfsnode *mfsp;
253 struct proc *p;
254 int flags, error = 0;
255
256 if (*data_len < sizeof *args)
257 return EINVAL;
258
259 p = l->l_proc;
260 if (mp->mnt_flag & MNT_GETARGS) {
261 struct vnode *vp;
262
263 ump = VFSTOUFS(mp);
264 if (ump == NULL)
265 return EIO;
266
267 vp = ump->um_devvp;
268 if (vp == NULL)
269 return EIO;
270
271 mfsp = VTOMFS(vp);
272 if (mfsp == NULL)
273 return EIO;
274
275 args->fspec = NULL;
276 args->base = mfsp->mfs_baseoff;
277 args->size = mfsp->mfs_size;
278 *data_len = sizeof *args;
279 return 0;
280 }
281 /*
282 * XXX turn off async to avoid hangs when writing lots of data.
283 * the problem is that MFS needs to allocate pages to clean pages,
284 * so if we wait until the last minute to clean pages then there
285 * may not be any pages available to do the cleaning.
286 * ... and since the default partially-synchronous mode turns out
287 * to not be sufficient under heavy load, make it full synchronous.
288 */
289 mp->mnt_flag &= ~MNT_ASYNC;
290 mp->mnt_flag |= MNT_SYNCHRONOUS;
291
292 /*
293 * If updating, check whether changing from read-only to
294 * read/write; if there is no device name, that's all we do.
295 */
296 if (mp->mnt_flag & MNT_UPDATE) {
297 ump = VFSTOUFS(mp);
298 fs = ump->um_fs;
299 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
300 flags = WRITECLOSE;
301 if (mp->mnt_flag & MNT_FORCE)
302 flags |= FORCECLOSE;
303 error = ffs_flushfiles(mp, flags, l);
304 if (error)
305 return (error);
306 }
307 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
308 fs->fs_ronly = 0;
309 if (args->fspec == NULL)
310 return EINVAL;
311 return (0);
312 }
313 error = getnewvnode(VT_MFS, (struct mount *)0, mfs_vnodeop_p, &devvp);
314 if (error)
315 return (error);
316 devvp->v_type = VBLK;
317 spec_node_init(devvp, makedev(255, mfs_minor));
318 mfs_minor++;
319 mfsp = (struct mfsnode *)malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK);
320 devvp->v_data = mfsp;
321 mfsp->mfs_baseoff = args->base;
322 mfsp->mfs_size = args->size;
323 mfsp->mfs_vnode = devvp;
324 mfsp->mfs_proc = p;
325 mfsp->mfs_shutdown = 0;
326 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
327 if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
328 mfsp->mfs_shutdown = 1;
329 vrele(devvp);
330 return (error);
331 }
332 ump = VFSTOUFS(mp);
333 fs = ump->um_fs;
334 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
335 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
336 if (error)
337 return error;
338 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
339 sizeof(fs->fs_fsmnt));
340 fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
341 /* XXX: cleanup on error */
342 return 0;
343 }
344
345 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */
346
347 /*
348 * Used to grab the process and keep it in the kernel to service
349 * memory filesystem I/O requests.
350 *
351 * Loop servicing I/O requests.
352 * Copy the requested data into or out of the memory filesystem
353 * address space.
354 */
355 /* ARGSUSED */
356 int
357 mfs_start(struct mount *mp, int flags)
358 {
359 struct lwp *l = curlwp;
360 struct vnode *vp = VFSTOUFS(mp)->um_devvp;
361 struct mfsnode *mfsp = VTOMFS(vp);
362 struct proc *p;
363 struct buf *bp;
364 void *base;
365 int sleepreturn = 0;
366 ksiginfoq_t kq;
367
368 base = mfsp->mfs_baseoff;
369 while (mfsp->mfs_shutdown != 1) {
370 while ((bp = BUFQ_GET(mfsp->mfs_buflist)) != NULL) {
371 mfs_doio(bp, base);
372 wakeup((void *)bp);
373 }
374 /*
375 * If a non-ignored signal is received, try to unmount.
376 * If that fails, or the filesystem is already in the
377 * process of being unmounted, clear the signal (it has been
378 * "processed"), otherwise we will loop here, as tsleep
379 * will always return EINTR/ERESTART.
380 */
381 if (sleepreturn != 0) {
382 /*
383 * XXX Freeze syncer. Must do this before locking
384 * the mount point. See dounmount() for details.
385 */
386 mutex_enter(&syncer_mutex);
387 if (vfs_busy(mp, LK_NOWAIT, 0) != 0)
388 mutex_exit(&syncer_mutex);
389 else if (dounmount(mp, 0, l) != 0) {
390 p = l->l_proc;
391 ksiginfo_queue_init(&kq);
392 mutex_enter(&p->p_smutex);
393 sigclearall(p, NULL, &kq);
394 mutex_exit(&p->p_smutex);
395 ksiginfo_queue_drain(&kq);
396 }
397 sleepreturn = 0;
398 continue;
399 }
400
401 sleepreturn = tsleep(vp, mfs_pri, "mfsidl", 0);
402 }
403 KASSERT(BUFQ_PEEK(mfsp->mfs_buflist) == NULL);
404 bufq_free(mfsp->mfs_buflist);
405 return (sleepreturn);
406 }
407
408 /*
409 * Get file system statistics.
410 */
411 int
412 mfs_statvfs(struct mount *mp, struct statvfs *sbp)
413 {
414 int error;
415
416 error = ffs_statvfs(mp, sbp);
417 if (error)
418 return error;
419 (void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
420 sizeof(sbp->f_fstypename));
421 sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
422 return 0;
423 }
424