xref: /openbsd-src/sys/ufs/mfs/mfs_vfsops.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: mfs_vfsops.c,v 1.52 2016/09/08 16:57:29 tedu Exp $	*/
2 /*	$NetBSD: mfs_vfsops.c,v 1.10 1996/02/09 22:31:28 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1990, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)mfs_vfsops.c	8.4 (Berkeley) 4/16/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/time.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/mount.h>
41 #include <sys/signalvar.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
44 #include <sys/kthread.h>
45 
46 #include <ufs/ufs/quota.h>
47 #include <ufs/ufs/inode.h>
48 #include <ufs/ufs/ufsmount.h>
49 #include <ufs/ufs/ufs_extern.h>
50 
51 #include <ufs/ffs/fs.h>
52 #include <ufs/ffs/ffs_extern.h>
53 
54 #include <ufs/mfs/mfsnode.h>
55 #include <ufs/mfs/mfs_extern.h>
56 
57 static	int mfs_minor;	/* used for building internal dev_t */
58 
59 /*
60  * mfs vfs operations.
61  */
62 const struct vfsops mfs_vfsops = {
63 	mfs_mount,
64 	mfs_start,
65 	ffs_unmount,
66 	ufs_root,
67 	ufs_quotactl,
68 	ffs_statfs,
69 	ffs_sync,
70 	ffs_vget,
71 	ffs_fhtovp,
72 	ffs_vptofh,
73 	mfs_init,
74 	ffs_sysctl,
75 	mfs_checkexp
76 };
77 
78 /*
79  * VFS Operations.
80  *
81  * mount system call
82  */
83 int
84 mfs_mount(struct mount *mp, const char *path, void *data,
85     struct nameidata *ndp, struct proc *p)
86 {
87 	struct vnode *devvp;
88 	struct mfs_args args;
89 	struct ufsmount *ump;
90 	struct fs *fs;
91 	struct mfsnode *mfsp;
92 	char fspec[MNAMELEN];
93 	int flags, error;
94 
95 	error = copyin(data, &args, sizeof(struct mfs_args));
96 	if (error)
97 		return (error);
98 
99 	/*
100 	 * If updating, check whether changing from read-only to
101 	 * read/write; if there is no device name, that's all we do.
102 	 */
103 	if (mp->mnt_flag & MNT_UPDATE) {
104 		ump = VFSTOUFS(mp);
105 		fs = ump->um_fs;
106 		if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
107 			flags = WRITECLOSE;
108 			if (mp->mnt_flag & MNT_FORCE)
109 				flags |= FORCECLOSE;
110 			error = ffs_flushfiles(mp, flags, p);
111 			if (error)
112 				return (error);
113 		}
114 		if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR))
115 			fs->fs_ronly = 0;
116 #ifdef EXPORTMFS
117 		if (args.fspec == NULL)
118 			return (vfs_export(mp, &ump->um_export,
119 			    &args.export_info));
120 #endif
121 		return (0);
122 	}
123 	error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
124 	if (error)
125 		return (error);
126 	error = getnewvnode(VT_MFS, NULL, &mfs_vops, &devvp);
127 	if (error)
128 		return (error);
129 	devvp->v_type = VBLK;
130 	if (checkalias(devvp, makedev(255, mfs_minor), NULL))
131 		panic("mfs_mount: dup dev");
132 	mfs_minor++;
133 	mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK | M_ZERO);
134 	devvp->v_data = mfsp;
135 	mfsp->mfs_baseoff = args.base;
136 	mfsp->mfs_size = args.size;
137 	mfsp->mfs_vnode = devvp;
138 	mfsp->mfs_pid = p->p_pid;
139 	bufq_init(&mfsp->mfs_bufq, BUFQ_FIFO);
140 	if ((error = ffs_mountfs(devvp, mp, p)) != 0) {
141 		mfsp->mfs_shutdown = 1;
142 		vrele(devvp);
143 		return (error);
144 	}
145 	ump = VFSTOUFS(mp);
146 	fs = ump->um_fs;
147 
148 	memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
149 	strlcpy(fs->fs_fsmnt, path, sizeof(fs->fs_fsmnt));
150 	memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
151 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
152 	strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
153 	memset(mp->mnt_stat.f_mntfromspec, 0, MNAMELEN);
154 	strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
155 	memcpy(&mp->mnt_stat.mount_info.mfs_args, &args, sizeof(args));
156 
157 	return (0);
158 }
159 
160 /*
161  * Used to grab the process and keep it in the kernel to service
162  * memory filesystem I/O requests.
163  *
164  * Loop servicing I/O requests.
165  * Copy the requested data into or out of the memory filesystem
166  * address space.
167  */
168 int
169 mfs_start(struct mount *mp, int flags, struct proc *p)
170 {
171 	struct vnode *vp = VFSTOUFS(mp)->um_devvp;
172 	struct mfsnode *mfsp = VTOMFS(vp);
173 	struct buf *bp;
174 	int sleepreturn = 0;
175 
176 	while (1) {
177 		while (1) {
178 			if (mfsp->mfs_shutdown == 1)
179 				break;
180 			bp = bufq_dequeue(&mfsp->mfs_bufq);
181 			if (bp == NULL)
182 				break;
183 			mfs_doio(mfsp, bp);
184 			wakeup(bp);
185 		}
186 		if (mfsp->mfs_shutdown == 1)
187 			break;
188 
189 		/*
190 		 * If a non-ignored signal is received, try to unmount.
191 		 * If that fails, clear the signal (it has been "processed"),
192 		 * otherwise we will loop here, as tsleep will always return
193 		 * EINTR/ERESTART.
194 		 */
195 		if (sleepreturn != 0) {
196 			if (vfs_busy(mp, VB_WRITE|VB_NOWAIT) ||
197 			    dounmount(mp,
198 			    (CURSIG(p) == SIGKILL) ? MNT_FORCE : 0, p, NULL))
199 				CLRSIG(p, CURSIG(p));
200 			sleepreturn = 0;
201 			continue;
202 		}
203 		sleepreturn = tsleep(vp, PWAIT | PCATCH, "mfsidl", 0);
204 	}
205 	return (0);
206 }
207 
208 /*
209  * check export permission, not supported
210  */
211 int
212 mfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
213     struct ucred **credanonp)
214 {
215 	return (EOPNOTSUPP);
216 }
217 
218 /*
219  * Memory based filesystem initialization.
220  */
221 int
222 mfs_init(struct vfsconf *vfsp)
223 {
224 	return (ffs_init(vfsp));
225 }
226