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