1 /* $NetBSD: mfs_vfsops.c,v 1.107 2014/04/16 18:55:19 maxv 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.107 2014/04/16 18:55:19 maxv 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 69 MODULE(MODULE_CLASS_VFS, mfs, "ffs"); 70 71 kmutex_t mfs_lock; /* global lock */ 72 73 /* used for building internal dev_t, minor == 0 reserved for miniroot */ 74 static int mfs_minor = 1; 75 static int mfs_initcnt; 76 77 extern int (**mfs_vnodeop_p)(void *); 78 79 static struct sysctllog *mfs_sysctl_log; 80 81 /* 82 * mfs vfs operations. 83 */ 84 85 extern const struct vnodeopv_desc mfs_vnodeop_opv_desc; 86 87 const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = { 88 &mfs_vnodeop_opv_desc, 89 NULL, 90 }; 91 92 struct 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 117 static int 118 mfs_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 */ 157 void 158 mfs_init(void) 159 { 160 161 if (mfs_initcnt++ == 0) { 162 mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE); 163 ffs_init(); 164 } 165 } 166 167 void 168 mfs_reinit(void) 169 { 170 171 ffs_reinit(); 172 } 173 174 void 175 mfs_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 188 int 189 mfs_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 */ 238 int 239 mfs_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 (args == NULL) 251 return EINVAL; 252 if (*data_len < sizeof *args) 253 return EINVAL; 254 255 p = l->l_proc; 256 if (mp->mnt_flag & MNT_GETARGS) { 257 struct vnode *vp; 258 259 ump = VFSTOUFS(mp); 260 if (ump == NULL) 261 return EIO; 262 263 vp = ump->um_devvp; 264 if (vp == NULL) 265 return EIO; 266 267 mfsp = VTOMFS(vp); 268 if (mfsp == NULL) 269 return EIO; 270 271 args->fspec = NULL; 272 args->base = mfsp->mfs_baseoff; 273 args->size = mfsp->mfs_size; 274 *data_len = sizeof *args; 275 return 0; 276 } 277 /* 278 * XXX turn off async to avoid hangs when writing lots of data. 279 * the problem is that MFS needs to allocate pages to clean pages, 280 * so if we wait until the last minute to clean pages then there 281 * may not be any pages available to do the cleaning. 282 * ... and since the default partially-synchronous mode turns out 283 * to not be sufficient under heavy load, make it full synchronous. 284 */ 285 mp->mnt_flag &= ~MNT_ASYNC; 286 mp->mnt_flag |= MNT_SYNCHRONOUS; 287 288 /* 289 * If updating, check whether changing from read-only to 290 * read/write; if there is no device name, that's all we do. 291 */ 292 if (mp->mnt_flag & MNT_UPDATE) { 293 ump = VFSTOUFS(mp); 294 fs = ump->um_fs; 295 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 296 flags = WRITECLOSE; 297 if (mp->mnt_flag & MNT_FORCE) 298 flags |= FORCECLOSE; 299 error = ffs_flushfiles(mp, flags, l); 300 if (error) 301 return (error); 302 } 303 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) 304 fs->fs_ronly = 0; 305 if (args->fspec == NULL) 306 return EINVAL; 307 return (0); 308 } 309 error = getnewvnode(VT_MFS, NULL, mfs_vnodeop_p, NULL, &devvp); 310 if (error) 311 return (error); 312 devvp->v_vflag |= VV_MPSAFE; 313 devvp->v_type = VBLK; 314 spec_node_init(devvp, makedev(255, mfs_minor)); 315 mfs_minor++; 316 mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP); 317 devvp->v_data = mfsp; 318 mfsp->mfs_baseoff = args->base; 319 mfsp->mfs_size = args->size; 320 mfsp->mfs_vnode = devvp; 321 mfsp->mfs_proc = p; 322 mfsp->mfs_shutdown = 0; 323 cv_init(&mfsp->mfs_cv, "mfsidl"); 324 mfsp->mfs_refcnt = 1; 325 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0); 326 if ((error = ffs_mountfs(devvp, mp, l)) != 0) { 327 mfsp->mfs_shutdown = 1; 328 vrele(devvp); 329 return (error); 330 } 331 ump = VFSTOUFS(mp); 332 fs = ump->um_fs; 333 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, 334 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 335 if (error) 336 return error; 337 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, 338 sizeof(fs->fs_fsmnt)); 339 fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0'; 340 /* XXX: cleanup on error */ 341 return 0; 342 } 343 344 /* 345 * Used to grab the process and keep it in the kernel to service 346 * memory filesystem I/O requests. 347 * 348 * Loop servicing I/O requests. 349 * Copy the requested data into or out of the memory filesystem 350 * address space. 351 */ 352 /* ARGSUSED */ 353 int 354 mfs_start(struct mount *mp, int flags) 355 { 356 struct vnode *vp; 357 struct mfsnode *mfsp; 358 struct proc *p; 359 struct buf *bp; 360 void *base; 361 int sleepreturn = 0, refcnt, error; 362 ksiginfoq_t kq; 363 364 /* 365 * Ensure that file system is still mounted when getting mfsnode. 366 * Add a reference to the mfsnode to prevent it disappearing in 367 * this routine. 368 */ 369 if ((error = vfs_busy(mp, NULL)) != 0) 370 return error; 371 vp = VFSTOUFS(mp)->um_devvp; 372 mfsp = VTOMFS(vp); 373 mutex_enter(&mfs_lock); 374 mfsp->mfs_refcnt++; 375 mutex_exit(&mfs_lock); 376 vfs_unbusy(mp, false, NULL); 377 378 base = mfsp->mfs_baseoff; 379 mutex_enter(&mfs_lock); 380 while (mfsp->mfs_shutdown != 1) { 381 while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) { 382 mutex_exit(&mfs_lock); 383 mfs_doio(bp, base); 384 mutex_enter(&mfs_lock); 385 } 386 /* 387 * If a non-ignored signal is received, try to unmount. 388 * If that fails, or the filesystem is already in the 389 * process of being unmounted, clear the signal (it has been 390 * "processed"), otherwise we will loop here, as tsleep 391 * will always return EINTR/ERESTART. 392 */ 393 if (sleepreturn != 0) { 394 mutex_exit(&mfs_lock); 395 if (dounmount(mp, 0, curlwp) != 0) { 396 p = curproc; 397 ksiginfo_queue_init(&kq); 398 mutex_enter(p->p_lock); 399 sigclearall(p, NULL, &kq); 400 mutex_exit(p->p_lock); 401 ksiginfo_queue_drain(&kq); 402 } 403 sleepreturn = 0; 404 mutex_enter(&mfs_lock); 405 continue; 406 } 407 408 sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock); 409 } 410 KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL); 411 refcnt = --mfsp->mfs_refcnt; 412 mutex_exit(&mfs_lock); 413 if (refcnt == 0) { 414 bufq_free(mfsp->mfs_buflist); 415 cv_destroy(&mfsp->mfs_cv); 416 kmem_free(mfsp, sizeof(*mfsp)); 417 } 418 return (sleepreturn); 419 } 420 421 /* 422 * Get file system statistics. 423 */ 424 int 425 mfs_statvfs(struct mount *mp, struct statvfs *sbp) 426 { 427 int error; 428 429 error = ffs_statvfs(mp, sbp); 430 if (error) 431 return error; 432 (void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, 433 sizeof(sbp->f_fstypename)); 434 sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0'; 435 return 0; 436 } 437