1/* $NetBSD: mfs_vfsops.c,v 1.116 2022/03/19 13:53:33 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.116 2022/03/19 13:53:33 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 devminor_t mfs_minor = 1; 75static int mfs_initcnt; 76 77extern int (**mfs_vnodeop_p)(void *); 78 79/* 80 * mfs vfs operations. 81 */ 82 83extern const struct vnodeopv_desc mfs_vnodeop_opv_desc; 84 85const struct vnodeopv_desc * const mfs_vnodeopv_descs[] = { 86 &mfs_vnodeop_opv_desc, 87 NULL, 88}; 89 90struct vfsops mfs_vfsops = { 91 .vfs_name = MOUNT_MFS, 92 .vfs_min_mount_data = sizeof (struct mfs_args), 93 .vfs_mount = mfs_mount, 94 .vfs_start = mfs_start, 95 .vfs_unmount = ffs_unmount, 96 .vfs_root = ufs_root, 97 .vfs_quotactl = ufs_quotactl, 98 .vfs_statvfs = mfs_statvfs, 99 .vfs_sync = ffs_sync, 100 .vfs_vget = ufs_vget, 101 .vfs_loadvnode = ffs_loadvnode, 102 .vfs_newvnode = ffs_newvnode, 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 = genfs_suspendctl, 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 117SYSCTL_SETUP(mfs_sysctl_setup, "mfs sysctl") 118{ 119 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 134static int 135mfs_modcmd(modcmd_t cmd, void *arg) 136{ 137 int error; 138 139 switch (cmd) { 140 case MODULE_CMD_INIT: 141 error = vfs_attach(&mfs_vfsops); 142 if (error != 0) 143 break; 144 break; 145 case MODULE_CMD_FINI: 146 error = vfs_detach(&mfs_vfsops); 147 if (error != 0) 148 break; 149 break; 150 default: 151 error = ENOTTY; 152 break; 153 } 154 155 return (error); 156} 157 158/* 159 * Memory based filesystem initialization. 160 */ 161void 162mfs_init(void) 163{ 164 165 if (mfs_initcnt++ == 0) { 166 mutex_init(&mfs_lock, MUTEX_DEFAULT, IPL_NONE); 167 ffs_init(); 168 } 169} 170 171void 172mfs_reinit(void) 173{ 174 175 ffs_reinit(); 176} 177 178void 179mfs_done(void) 180{ 181 182 if (--mfs_initcnt == 0) { 183 ffs_done(); 184 mutex_destroy(&mfs_lock); 185 } 186} 187 188/* 189 * Called by main() when mfs is going to be mounted as root. 190 */ 191 192int 193mfs_mountroot(void) 194{ 195 struct fs *fs; 196 struct mount *mp; 197 struct lwp *l = curlwp; /* XXX */ 198 struct ufsmount *ump; 199 struct mfsnode *mfsp; 200 int error = 0; 201 202 if ((error = vfs_rootmountalloc(MOUNT_MFS, "mfs_root", &mp))) { 203 vrele(rootvp); 204 return (error); 205 } 206 207 mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP); 208 rootvp->v_data = mfsp; 209 rootvp->v_op = mfs_vnodeop_p; 210 rootvp->v_tag = VT_MFS; 211 mfsp->mfs_baseoff = mfs_rootbase; 212 mfsp->mfs_size = mfs_rootsize; 213 mfsp->mfs_vnode = rootvp; 214 mfsp->mfs_proc = NULL; /* indicate kernel space */ 215 mfsp->mfs_shutdown = 0; 216 cv_init(&mfsp->mfs_cv, "mfs"); 217 mfsp->mfs_refcnt = 1; 218 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0); 219 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) { 220 vfs_unbusy(mp); 221 bufq_free(mfsp->mfs_buflist); 222 vfs_rele(mp); 223 kmem_free(mfsp, sizeof(*mfsp)); 224 return (error); 225 } 226 mountlist_append(mp); 227 mp->mnt_vnodecovered = NULLVP; 228 ump = VFSTOUFS(mp); 229 fs = ump->um_fs; 230 (void) copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0); 231 (void)ffs_statvfs(mp, &mp->mnt_stat); 232 vfs_unbusy(mp); 233 return (0); 234} 235 236/* 237 * VFS Operations. 238 * 239 * mount system call 240 */ 241/* ARGSUSED */ 242int 243mfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 244{ 245 struct lwp *l = curlwp; 246 struct vnode *devvp; 247 struct mfs_args *args = data; 248 struct ufsmount *ump; 249 struct fs *fs; 250 struct mfsnode *mfsp; 251 struct proc *p; 252 devminor_t minor; 253 int flags, error = 0; 254 255 if (args == NULL) 256 return EINVAL; 257 if (*data_len < sizeof *args) 258 return EINVAL; 259 260 p = l->l_proc; 261 if (mp->mnt_flag & MNT_GETARGS) { 262 struct vnode *vp; 263 264 ump = VFSTOUFS(mp); 265 if (ump == NULL) 266 return EIO; 267 268 vp = ump->um_devvp; 269 if (vp == NULL) 270 return EIO; 271 272 mfsp = VTOMFS(vp); 273 if (mfsp == NULL) 274 return EIO; 275 276 args->fspec = NULL; 277 args->base = mfsp->mfs_baseoff; 278 args->size = mfsp->mfs_size; 279 *data_len = sizeof *args; 280 return 0; 281 } 282 /* 283 * XXX turn off async to avoid hangs when writing lots of data. 284 * the problem is that MFS needs to allocate pages to clean pages, 285 * so if we wait until the last minute to clean pages then there 286 * may not be any pages available to do the cleaning. 287 * ... and since the default partially-synchronous mode turns out 288 * to not be sufficient under heavy load, make it full synchronous. 289 */ 290 mp->mnt_flag &= ~MNT_ASYNC; 291 mp->mnt_flag |= MNT_SYNCHRONOUS; 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 mutex_enter(&mfs_lock); 315 minor = mfs_minor++; 316 mutex_exit(&mfs_lock); 317 error = bdevvp(makedev(255, minor), &devvp); 318 if (error) 319 return (error); 320 mfsp = kmem_alloc(sizeof(*mfsp), KM_SLEEP); 321 /* 322 * Changing v_op and v_data here is safe as we are 323 * the exclusive owner of this device node. 324 */ 325 KASSERT(devvp->v_op == spec_vnodeop_p); 326 KASSERT(devvp->v_data == NULL); 327 devvp->v_op = mfs_vnodeop_p; 328 devvp->v_data = mfsp; 329 mfsp->mfs_baseoff = args->base; 330 mfsp->mfs_size = args->size; 331 mfsp->mfs_vnode = devvp; 332 mfsp->mfs_proc = p; 333 mfsp->mfs_shutdown = 0; 334 cv_init(&mfsp->mfs_cv, "mfsidl"); 335 mfsp->mfs_refcnt = 1; 336 bufq_alloc(&mfsp->mfs_buflist, "fcfs", 0); 337 if ((error = ffs_mountfs(devvp, mp, l)) != 0) { 338 mfsp->mfs_shutdown = 1; 339 vrele(devvp); 340 return (error); 341 } 342 ump = VFSTOUFS(mp); 343 fs = ump->um_fs; 344 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, 345 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 346 if (error) 347 return error; 348 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, 349 sizeof(fs->fs_fsmnt)); 350 fs->fs_fsmnt[sizeof(fs->fs_fsmnt) - 1] = '\0'; 351 /* XXX: cleanup on error */ 352 return 0; 353} 354 355/* 356 * Used to grab the process and keep it in the kernel to service 357 * memory filesystem I/O requests. 358 * 359 * Loop servicing I/O requests. 360 * Copy the requested data into or out of the memory filesystem 361 * address space. 362 */ 363/* ARGSUSED */ 364int 365mfs_start(struct mount *mp, int flags) 366{ 367 struct vnode *vp; 368 struct mfsnode *mfsp; 369 struct proc *p; 370 struct buf *bp; 371 void *base; 372 int sleepreturn = 0, refcnt, error; 373 ksiginfoq_t kq; 374 375 /* 376 * Ensure that file system is still mounted when getting mfsnode. 377 * Add a reference to the mfsnode to prevent it disappearing in 378 * this routine. 379 */ 380 if ((error = vfs_busy(mp)) != 0) 381 return error; 382 vp = VFSTOUFS(mp)->um_devvp; 383 mfsp = VTOMFS(vp); 384 mutex_enter(&mfs_lock); 385 mfsp->mfs_refcnt++; 386 mutex_exit(&mfs_lock); 387 vfs_unbusy(mp); 388 389 base = mfsp->mfs_baseoff; 390 mutex_enter(&mfs_lock); 391 while (mfsp->mfs_shutdown != 1) { 392 while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) { 393 mutex_exit(&mfs_lock); 394 mfs_doio(bp, base); 395 mutex_enter(&mfs_lock); 396 } 397 /* 398 * If a non-ignored signal is received, try to unmount. 399 * If that fails, or the filesystem is already in the 400 * process of being unmounted, clear the signal (it has been 401 * "processed"), otherwise we will loop here, as tsleep 402 * will always return EINTR/ERESTART. 403 */ 404 if (sleepreturn != 0) { 405 mutex_exit(&mfs_lock); 406 if (dounmount(mp, 0, curlwp) != 0) { 407 p = curproc; 408 ksiginfo_queue_init(&kq); 409 mutex_enter(p->p_lock); 410 sigclearall(p, NULL, &kq); 411 mutex_exit(p->p_lock); 412 ksiginfo_queue_drain(&kq); 413 } 414 sleepreturn = 0; 415 mutex_enter(&mfs_lock); 416 continue; 417 } 418 419 sleepreturn = cv_wait_sig(&mfsp->mfs_cv, &mfs_lock); 420 } 421 KASSERT(bufq_peek(mfsp->mfs_buflist) == NULL); 422 refcnt = --mfsp->mfs_refcnt; 423 mutex_exit(&mfs_lock); 424 if (refcnt == 0) { 425 bufq_free(mfsp->mfs_buflist); 426 cv_destroy(&mfsp->mfs_cv); 427 kmem_free(mfsp, sizeof(*mfsp)); 428 } 429 return (sleepreturn); 430} 431 432/* 433 * Get file system statistics. 434 */ 435int 436mfs_statvfs(struct mount *mp, struct statvfs *sbp) 437{ 438 int error; 439 440 error = ffs_statvfs(mp, sbp); 441 if (error) 442 return error; 443 (void)strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, 444 sizeof(sbp->f_fstypename)); 445 sbp->f_fstypename[sizeof(sbp->f_fstypename) - 1] = '\0'; 446 return 0; 447} 448