mfs_vfsops.c revision 1.106
1/*	$NetBSD: mfs_vfsops.c,v 1.106 2014/03/23 15:21:17 hannken 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.106 2014/03/23 15:21:17 hannken 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/kmem.h>
53#include <sys/module.h>
54
55#include <miscfs/genfs/genfs.h>
56#include <miscfs/specfs/specdev.h>
57
58#include <ufs/ufs/quota.h>
59#include <ufs/ufs/inode.h>
60#include <ufs/ufs/ufsmount.h>
61#include <ufs/ufs/ufs_extern.h>
62
63#include <ufs/ffs/fs.h>
64#include <ufs/ffs/ffs_extern.h>
65
66#include <ufs/mfs/mfsnode.h>
67#include <ufs/mfs/mfs_extern.h>
68
69MODULE(MODULE_CLASS_VFS, mfs, "ffs");
70
71kmutex_t mfs_lock;	/* global lock */
72
73/* used for building internal dev_t, minor == 0 reserved for miniroot */
74static int mfs_minor = 1;
75static int mfs_initcnt;
76
77extern int (**mfs_vnodeop_p)(void *);
78
79static struct sysctllog *mfs_sysctl_log;
80
81/*
82 * mfs vfs operations.
83 */
84
85extern const struct vnodeopv_desc mfs_vnodeop_opv_desc;
86
87const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = {
88	&mfs_vnodeop_opv_desc,
89	NULL,
90};
91
92struct vfsops mfs_vfsops = {
93	.vfs_name = MOUNT_MFS,
94	.vfs_min_mount_data = sizeof (struct mfs_args),
95	.vfs_mount = mfs_mount,
96	.vfs_start = mfs_start,
97	.vfs_unmount = ffs_unmount,
98	.vfs_root = ufs_root,
99	.vfs_quotactl = ufs_quotactl,
100	.vfs_statvfs = mfs_statvfs,
101	.vfs_sync = ffs_sync,
102	.vfs_vget = ffs_vget,
103	.vfs_fhtovp = ffs_fhtovp,
104	.vfs_vptofh = ffs_vptofh,
105	.vfs_init = mfs_init,
106	.vfs_reinit = mfs_reinit,
107	.vfs_done = mfs_done,
108	.vfs_snapshot = (void *)eopnotsupp,
109	.vfs_extattrctl = vfs_stdextattrctl,
110	.vfs_suspendctl = (void *)eopnotsupp,
111	.vfs_renamelock_enter = genfs_renamelock_enter,
112	.vfs_renamelock_exit = genfs_renamelock_exit,
113	.vfs_fsync = (void *)eopnotsupp,
114	.vfs_opv_descs = mfs_vnodeopv_descs
115};
116
117static int
118mfs_modcmd(modcmd_t cmd, void *arg)
119{
120	int error;
121
122	switch (cmd) {
123	case MODULE_CMD_INIT:
124		error = vfs_attach(&mfs_vfsops);
125		if (error != 0)
126			break;
127		sysctl_createv(&mfs_sysctl_log, 0, NULL, NULL,
128			       CTLFLAG_PERMANENT|CTLFLAG_ALIAS,
129			       CTLTYPE_NODE, "mfs",
130			       SYSCTL_DESCR("Memory based file system"),
131			       NULL, 1, NULL, 0,
132			       CTL_VFS, 3, CTL_EOL);
133		/*
134		 * XXX the "1" and the "3" above could be dynamic, thereby
135		 * eliminating one more instance of the "number to vfs"
136		 * mapping problem, but they are in order as taken from
137		 * sys/mount.h
138		 */
139		break;
140	case MODULE_CMD_FINI:
141		error = vfs_detach(&mfs_vfsops);
142		if (error != 0)
143			break;
144		sysctl_teardown(&mfs_sysctl_log);
145		break;
146	default:
147		error = ENOTTY;
148		break;
149	}
150
151	return (error);
152}
153
154/*
155 * Memory based filesystem initialization.
156 */
157void
158mfs_init(void)
159{
160
161	if (mfs_initcnt++ == 0) {
162		mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE);
163		ffs_init();
164	}
165}
166
167void
168mfs_reinit(void)
169{
170
171	ffs_reinit();
172}
173
174void
175mfs_done(void)
176{
177
178	if (--mfs_initcnt == 0) {
179		ffs_done();
180		mutex_destroy(&mfs_lock);
181	}
182}
183
184/*
185 * Called by main() when mfs is going to be mounted as root.
186 */
187
188int
189mfs_mountroot(void)
190{
191	struct fs *fs;
192	struct mount *mp;
193	struct lwp *l = curlwp;		/* XXX */
194	struct ufsmount *ump;
195	struct mfsnode *mfsp;
196	int error = 0;
197
198	if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) {
199		vrele(rootvp);
200		return (error);
201	}
202
203	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
204	rootvp->v_data = mfsp;
205	rootvp->v_op = mfs_vnodeop_p;
206	rootvp->v_tag = VT_MFS;
207	mfsp->mfs_baseoff = mfs_rootbase;
208	mfsp->mfs_size = mfs_rootsize;
209	mfsp->mfs_vnode = rootvp;
210	mfsp->mfs_proc = NULL;		/* indicate kernel space */
211	mfsp->mfs_shutdown = 0;
212	cv_init(&mfsp->mfs_cv, "mfs");
213	mfsp->mfs_refcnt = 1;
214	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
215	if ((error = ffs_mountfs(rootvp, mp, l)) != 0) {
216		vfs_unbusy(mp, false, NULL);
217		bufq_free(mfsp->mfs_buflist);
218		vfs_destroy(mp);
219		kmem_free(mfsp, sizeof(*mfsp));
220		return (error);
221	}
222	mountlist_append(mp);
223	mp->mnt_vnodecovered = NULLVP;
224	ump = VFSTOUFS(mp);
225	fs = ump->um_fs;
226	(void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
227	(void)ffs_statvfs(mp, &mp->mnt_stat);
228	vfs_unbusy(mp, false, NULL);
229	return (0);
230}
231
232/*
233 * VFS Operations.
234 *
235 * mount system call
236 */
237/* ARGSUSED */
238int
239mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
240{
241	struct lwp *l = curlwp;
242	struct vnode *devvp;
243	struct mfs_args *args = data;
244	struct ufsmount *ump;
245	struct fs *fs;
246	struct mfsnode *mfsp;
247	struct proc *p;
248	int flags, error = 0;
249
250	if (*data_len < sizeof *args)
251		return EINVAL;
252
253	p = l->l_proc;
254	if (mp->mnt_flag & MNT_GETARGS) {
255		struct vnode *vp;
256
257		ump = VFSTOUFS(mp);
258		if (ump == NULL)
259			return EIO;
260
261		vp = ump->um_devvp;
262		if (vp == NULL)
263			return EIO;
264
265		mfsp = VTOMFS(vp);
266		if (mfsp == NULL)
267			return EIO;
268
269		args->fspec = NULL;
270		args->base = mfsp->mfs_baseoff;
271		args->size = mfsp->mfs_size;
272		*data_len = sizeof *args;
273		return 0;
274	}
275	/*
276	 * XXX turn off async to avoid hangs when writing lots of data.
277	 * the problem is that MFS needs to allocate pages to clean pages,
278	 * so if we wait until the last minute to clean pages then there
279	 * may not be any pages available to do the cleaning.
280	 * ... and since the default partially-synchronous mode turns out
281	 * to not be sufficient under heavy load, make it full synchronous.
282	 */
283	mp->mnt_flag &= ~MNT_ASYNC;
284	mp->mnt_flag |= MNT_SYNCHRONOUS;
285
286	/*
287	 * If updating, check whether changing from read-only to
288	 * read/write; if there is no device name, that's all we do.
289	 */
290	if (mp->mnt_flag & MNT_UPDATE) {
291		ump = VFSTOUFS(mp);
292		fs = ump->um_fs;
293		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
294			flags = WRITECLOSE;
295			if (mp->mnt_flag & MNT_FORCE)
296				flags |= FORCECLOSE;
297			error = ffs_flushfiles(mp, flags, l);
298			if (error)
299				return (error);
300		}
301		if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR))
302			fs->fs_ronly = 0;
303		if (args->fspec == NULL)
304			return EINVAL;
305		return (0);
306	}
307	error = getnewvnode(VT_MFS, NULL, mfs_vnodeop_p, NULL, &devvp);
308	if (error)
309		return (error);
310	devvp->v_vflag |= VV_MPSAFE;
311	devvp->v_type = VBLK;
312	spec_node_init(devvp, makedev(255, mfs_minor));
313	mfs_minor++;
314	mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP);
315	devvp->v_data = mfsp;
316	mfsp->mfs_baseoff = args->base;
317	mfsp->mfs_size = args->size;
318	mfsp->mfs_vnode = devvp;
319	mfsp->mfs_proc = p;
320	mfsp->mfs_shutdown = 0;
321	cv_init(&mfsp->mfs_cv, "mfsidl");
322	mfsp->mfs_refcnt = 1;
323	bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0);
324	if ((error = ffs_mountfs(devvp, mp, l)) != 0) {
325		mfsp->mfs_shutdown = 1;
326		vrele(devvp);
327		return (error);
328	}
329	ump = VFSTOUFS(mp);
330	fs = ump->um_fs;
331	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
332	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
333	if (error)
334		return error;
335	(void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname,
336		sizeof(fs->fs_fsmnt));
337	fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0';
338	/* XXX: cleanup on error */
339	return 0;
340}
341
342/*
343 * Used to grab the process and keep it in the kernel to service
344 * memory filesystem I/O requests.
345 *
346 * Loop servicing I/O requests.
347 * Copy the requested data into or out of the memory filesystem
348 * address space.
349 */
350/* ARGSUSED */
351int
352mfs_start(struct mount *mp, int flags)
353{
354	struct vnode *vp;
355	struct mfsnode *mfsp;
356	struct proc *p;
357	struct buf *bp;
358	void *base;
359	int sleepreturn = 0, refcnt, error;
360	ksiginfoq_t kq;
361
362	/*
363	 * Ensure that file system is still mounted when getting mfsnode.
364	 * Add a reference to the mfsnode to prevent it disappearing in
365	 * this routine.
366	 */
367	if ((error = vfs_busy(mp, NULL)) != 0)
368		return error;
369	vp = VFSTOUFS(mp)->um_devvp;
370	mfsp = VTOMFS(vp);
371	mutex_enter(&mfs_lock);
372	mfsp->mfs_refcnt++;
373	mutex_exit(&mfs_lock);
374	vfs_unbusy(mp, false, NULL);
375
376	base = mfsp->mfs_baseoff;
377	mutex_enter(&mfs_lock);
378	while (mfsp->mfs_shutdown != 1) {
379		while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
380			mutex_exit(&mfs_lock);
381			mfs_doio(bp, base);
382			mutex_enter(&mfs_lock);
383		}
384		/*
385		 * If a non-ignored signal is received, try to unmount.
386		 * If that fails, or the filesystem is already in the
387		 * process of being unmounted, clear the signal (it has been
388		 * "processed"), otherwise we will loop here, as tsleep
389		 * will always return EINTR/ERESTART.
390		 */
391		if (sleepreturn != 0) {
392			mutex_exit(&mfs_lock);
393			if (dounmount(mp, 0, curlwp) != 0) {
394				p = curproc;
395				ksiginfo_queue_init(&kq);
396				mutex_enter(p->p_lock);
397				sigclearall(p, NULL, &kq);
398				mutex_exit(p->p_lock);
399				ksiginfo_queue_drain(&kq);
400			}
401			sleepreturn = 0;
402			mutex_enter(&mfs_lock);
403			continue;
404		}
405
406		sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock);
407	}
408	KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL);
409	refcnt = --mfsp->mfs_refcnt;
410	mutex_exit(&mfs_lock);
411	if (refcnt == 0) {
412		bufq_free(mfsp->mfs_buflist);
413		cv_destroy(&mfsp->mfs_cv);
414		kmem_free(mfsp, sizeof(*mfsp));
415	}
416	return (sleepreturn);
417}
418
419/*
420 * Get file system statistics.
421 */
422int
423mfs_statvfs(struct mount *mp, struct statvfs *sbp)
424{
425	int error;
426
427	error = ffs_statvfs(mp, sbp);
428	if (error)
429		return error;
430	(void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name,
431	    sizeof(sbp->f_fstypename));
432	sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0';
433	return 0;
434}
435