1 /* $OpenBSD: mfs_vfsops.c,v 1.42 2010/12/21 20:14:44 thib 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/kernel.h> 39 #include <sys/proc.h> 40 #include <sys/buf.h> 41 #include <sys/mount.h> 42 #include <sys/signalvar.h> 43 #include <sys/vnode.h> 44 #include <sys/malloc.h> 45 #include <sys/kthread.h> 46 47 #include <ufs/ufs/quota.h> 48 #include <ufs/ufs/inode.h> 49 #include <ufs/ufs/ufsmount.h> 50 #include <ufs/ufs/ufs_extern.h> 51 52 #include <ufs/ffs/fs.h> 53 #include <ufs/ffs/ffs_extern.h> 54 55 #include <ufs/mfs/mfsnode.h> 56 #include <ufs/mfs/mfs_extern.h> 57 58 static int mfs_minor; /* used for building internal dev_t */ 59 60 /* 61 * mfs vfs operations. 62 */ 63 const struct vfsops mfs_vfsops = { 64 mfs_mount, 65 mfs_start, 66 ffs_unmount, 67 ufs_root, 68 ufs_quotactl, 69 mfs_statfs, 70 ffs_sync, 71 ffs_vget, 72 ffs_fhtovp, 73 ffs_vptofh, 74 mfs_init, 75 ffs_sysctl, 76 mfs_checkexp 77 }; 78 79 /* 80 * VFS Operations. 81 * 82 * mount system call 83 */ 84 /* ARGSUSED */ 85 int 86 mfs_mount(struct mount *mp, const char *path, void *data, 87 struct nameidata *ndp, struct proc *p) 88 { 89 struct vnode *devvp; 90 struct mfs_args args; 91 struct ufsmount *ump; 92 struct fs *fs; 93 struct mfsnode *mfsp; 94 size_t size; 95 int flags, error; 96 97 error = copyin(data, (caddr_t)&args, sizeof (struct mfs_args)); 98 if (error) 99 return (error); 100 101 /* 102 * If updating, check whether changing from read-only to 103 * read/write; if there is no device name, that's all we do. 104 */ 105 if (mp->mnt_flag & MNT_UPDATE) { 106 ump = VFSTOUFS(mp); 107 fs = ump->um_fs; 108 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) { 109 flags = WRITECLOSE; 110 if (mp->mnt_flag & MNT_FORCE) 111 flags |= FORCECLOSE; 112 error = ffs_flushfiles(mp, flags, p); 113 if (error) 114 return (error); 115 } 116 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) 117 fs->fs_ronly = 0; 118 #ifdef EXPORTMFS 119 if (args.fspec == 0) 120 return (vfs_export(mp, &ump->um_export, 121 &args.export_info)); 122 #endif 123 return (0); 124 } 125 error = getnewvnode(VT_MFS, NULL, &mfs_vops, &devvp); 126 if (error) 127 return (error); 128 devvp->v_type = VBLK; 129 if (checkalias(devvp, makedev(255, mfs_minor), (struct mount *)0)) 130 panic("mfs_mount: dup dev"); 131 mfs_minor++; 132 mfsp = malloc(sizeof *mfsp, M_MFSNODE, M_WAITOK); 133 devvp->v_data = mfsp; 134 mfsp->mfs_baseoff = args.base; 135 mfsp->mfs_size = args.size; 136 mfsp->mfs_vnode = devvp; 137 mfsp->mfs_pid = p->p_pid; 138 mfsp->mfs_buflist = (struct buf *)0; 139 if ((error = ffs_mountfs(devvp, mp, p)) != 0) { 140 mfsp->mfs_buflist = (struct buf *)-1; 141 vrele(devvp); 142 return (error); 143 } 144 ump = VFSTOUFS(mp); 145 fs = ump->um_fs; 146 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size); 147 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size); 148 bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN); 149 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 150 &size); 151 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 152 bcopy(&args, &mp->mnt_stat.mount_info.mfs_args, sizeof(args)); 153 return (0); 154 } 155 156 int mfs_pri = PWAIT | PCATCH; /* XXX prob. temp */ 157 158 /* 159 * Used to grab the process and keep it in the kernel to service 160 * memory filesystem I/O requests. 161 * 162 * Loop servicing I/O requests. 163 * Copy the requested data into or out of the memory filesystem 164 * address space. 165 */ 166 /* ARGSUSED */ 167 int 168 mfs_start(struct mount *mp, int flags, struct proc *p) 169 { 170 struct vnode *vp = VFSTOUFS(mp)->um_devvp; 171 struct mfsnode *mfsp = VTOMFS(vp); 172 struct buf *bp; 173 int sleepreturn = 0, s; 174 175 while (1) { 176 while (1) { 177 s = splbio(); 178 bp = mfsp->mfs_buflist; 179 if (bp == NULL || bp == (struct buf *)-1) { 180 splx(s); 181 break; 182 } 183 mfsp->mfs_buflist = bp->b_actf; 184 splx(s); 185 mfs_doio(mfsp, bp); 186 wakeup((caddr_t)bp); 187 } 188 if (bp == (struct buf *)-1) 189 break; 190 /* 191 * If a non-ignored signal is received, try to unmount. 192 * If that fails, clear the signal (it has been "processed"), 193 * otherwise we will loop here, as tsleep will always return 194 * EINTR/ERESTART. 195 */ 196 if (sleepreturn != 0) { 197 if (vfs_busy(mp, VB_WRITE|VB_NOWAIT) || 198 dounmount(mp, 199 (CURSIG(p) == SIGKILL) ? MNT_FORCE : 0, p, NULL)) 200 CLRSIG(p, CURSIG(p)); 201 sleepreturn = 0; 202 continue; 203 } 204 sleepreturn = tsleep((caddr_t)vp, mfs_pri, "mfsidl", 0); 205 } 206 return (0); 207 } 208 209 /* 210 * Get file system statistics. 211 */ 212 int 213 mfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 214 { 215 int error; 216 217 error = ffs_statfs(mp, sbp, p); 218 strncpy(&sbp->f_fstypename[0], mp->mnt_vfc->vfc_name, MFSNAMELEN); 219 if (sbp != &mp->mnt_stat) 220 bcopy(&mp->mnt_stat.mount_info.mfs_args, 221 &sbp->mount_info.mfs_args, sizeof(struct mfs_args)); 222 return (error); 223 } 224 225 /* 226 * check export permission, not supported 227 */ 228 /* ARGUSED */ 229 int 230 mfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp, 231 struct ucred **credanonp) 232 { 233 return (EOPNOTSUPP); 234 } 235 236 /* 237 * Memory based filesystem initialization. 238 */ 239 int 240 mfs_init(struct vfsconf *vfsp) 241 { 242 return (ffs_init(vfsp)); 243 } 244