1 /* $NetBSD: kernfs_vfsops.c,v 1.85 2008/05/10 02:26:09 rumble Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kernfs_vfsops.c 8.10 (Berkeley) 5/14/95 35 */ 36 37 /* 38 * Kernel params Filesystem 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.85 2008/05/10 02:26:09 rumble Exp $"); 43 44 #ifdef _KERNEL_OPT 45 #include "opt_compat_netbsd.h" 46 #endif 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/sysctl.h> 51 #include <sys/conf.h> 52 #include <sys/proc.h> 53 #include <sys/vnode.h> 54 #include <sys/mount.h> 55 #include <sys/namei.h> 56 #include <sys/dirent.h> 57 #include <sys/malloc.h> 58 #include <sys/syslog.h> 59 #include <sys/kauth.h> 60 #include <sys/module.h> 61 62 #include <miscfs/genfs/genfs.h> 63 #include <miscfs/specfs/specdev.h> 64 #include <miscfs/kernfs/kernfs.h> 65 66 MODULE(MODULE_CLASS_VFS, kernfs, NULL); 67 68 MALLOC_JUSTDEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures"); 69 70 dev_t rrootdev = NODEV; 71 72 VFS_PROTOS(kernfs); 73 74 void kernfs_get_rrootdev(void); 75 76 void 77 kernfs_init() 78 { 79 80 malloc_type_attach(M_KERNFSMNT); 81 kernfs_hashinit(); 82 } 83 84 void 85 kernfs_reinit() 86 { 87 kernfs_hashreinit(); 88 } 89 90 void 91 kernfs_done() 92 { 93 94 kernfs_hashdone(); 95 malloc_type_detach(M_KERNFSMNT); 96 } 97 98 void 99 kernfs_get_rrootdev() 100 { 101 static int tried = 0; 102 103 if (tried) { 104 /* Already did it once. */ 105 return; 106 } 107 tried = 1; 108 109 if (rootdev == NODEV) 110 return; 111 rrootdev = devsw_blk2chr(rootdev); 112 if (rrootdev != NODEV) 113 return; 114 rrootdev = NODEV; 115 printf("kernfs_get_rrootdev: no raw root device\n"); 116 } 117 118 /* 119 * Mount the Kernel params filesystem 120 */ 121 int 122 kernfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 123 { 124 struct lwp *l = curlwp; 125 int error = 0; 126 struct kernfs_mount *fmp; 127 128 if (UIO_MX & (UIO_MX - 1)) { 129 log(LOG_ERR, "kernfs: invalid directory entry size"); 130 return (EINVAL); 131 } 132 133 if (mp->mnt_flag & MNT_GETARGS) { 134 *data_len = 0; 135 return 0; 136 } 137 /* 138 * Update is a no-op 139 */ 140 if (mp->mnt_flag & MNT_UPDATE) 141 return (EOPNOTSUPP); 142 143 MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount), 144 M_KERNFSMNT, M_WAITOK); 145 memset(fmp, 0, sizeof(*fmp)); 146 TAILQ_INIT(&fmp->nodelist); 147 148 mp->mnt_stat.f_namemax = MAXNAMLEN; 149 mp->mnt_flag |= MNT_LOCAL; 150 mp->mnt_data = fmp; 151 vfs_getnewfsid(mp); 152 153 if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs", 154 UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) { 155 free(fmp, M_KERNFSMNT); 156 return error; 157 } 158 159 kernfs_get_rrootdev(); 160 return 0; 161 } 162 163 int 164 kernfs_start(struct mount *mp, int flags) 165 { 166 167 return (0); 168 } 169 170 int 171 kernfs_unmount(struct mount *mp, int mntflags) 172 { 173 int error; 174 int flags = 0; 175 176 if (mntflags & MNT_FORCE) 177 flags |= FORCECLOSE; 178 179 if ((error = vflush(mp, 0, flags)) != 0) 180 return (error); 181 182 /* 183 * Finally, throw away the kernfs_mount structure 184 */ 185 free(mp->mnt_data, M_KERNFSMNT); 186 mp->mnt_data = NULL; 187 return (0); 188 } 189 190 int 191 kernfs_root(mp, vpp) 192 struct mount *mp; 193 struct vnode **vpp; 194 { 195 196 /* setup "." */ 197 return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0)); 198 } 199 200 int 201 kernfs_statvfs(struct mount *mp, struct statvfs *sbp) 202 { 203 204 sbp->f_bsize = DEV_BSIZE; 205 sbp->f_frsize = DEV_BSIZE; 206 sbp->f_iosize = DEV_BSIZE; 207 sbp->f_blocks = 2; /* 1K to keep df happy */ 208 sbp->f_bfree = 0; 209 sbp->f_bavail = 0; 210 sbp->f_bresvd = 0; 211 sbp->f_files = 1024; /* XXX lie */ 212 sbp->f_ffree = 128; /* XXX lie */ 213 sbp->f_favail = 128; /* XXX lie */ 214 sbp->f_fresvd = 0; 215 copy_statvfs_info(sbp, mp); 216 return (0); 217 } 218 219 /*ARGSUSED*/ 220 int 221 kernfs_sync(struct mount *mp, int waitfor, 222 kauth_cred_t uc) 223 { 224 225 return (0); 226 } 227 228 /* 229 * Kernfs flat namespace lookup. 230 * Currently unsupported. 231 */ 232 int 233 kernfs_vget(struct mount *mp, ino_t ino, 234 struct vnode **vpp) 235 { 236 237 return (EOPNOTSUPP); 238 } 239 240 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup") 241 { 242 243 sysctl_createv(clog, 0, NULL, NULL, 244 CTLFLAG_PERMANENT, 245 CTLTYPE_NODE, "vfs", NULL, 246 NULL, 0, NULL, 0, 247 CTL_VFS, CTL_EOL); 248 sysctl_createv(clog, 0, NULL, NULL, 249 CTLFLAG_PERMANENT, 250 CTLTYPE_NODE, "kernfs", 251 SYSCTL_DESCR("/kern file system"), 252 NULL, 0, NULL, 0, 253 CTL_VFS, 11, CTL_EOL); 254 /* 255 * XXX the "11" above could be dynamic, thereby eliminating one 256 * more instance of the "number to vfs" mapping problem, but 257 * "11" is the order as taken from sys/mount.h 258 */ 259 } 260 261 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc; 262 263 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = { 264 &kernfs_vnodeop_opv_desc, 265 NULL, 266 }; 267 268 struct vfsops kernfs_vfsops = { 269 MOUNT_KERNFS, 270 0, 271 kernfs_mount, 272 kernfs_start, 273 kernfs_unmount, 274 kernfs_root, 275 (void *)eopnotsupp, /* vfs_quotactl */ 276 kernfs_statvfs, 277 kernfs_sync, 278 kernfs_vget, 279 (void *)eopnotsupp, /* vfs_fhtovp */ 280 (void *)eopnotsupp, /* vfs_vptofh */ 281 kernfs_init, 282 kernfs_reinit, 283 kernfs_done, 284 NULL, /* vfs_mountroot */ 285 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 286 vfs_stdextattrctl, 287 (void *)eopnotsupp, /* vfs_suspendctl */ 288 genfs_renamelock_enter, 289 genfs_renamelock_exit, 290 (void *)eopnotsupp, 291 kernfs_vnodeopv_descs, 292 0, 293 { NULL, NULL }, 294 }; 295 296 static int 297 kernfs_modcmd(modcmd_t cmd, void *arg) 298 { 299 300 switch (cmd) { 301 case MODULE_CMD_INIT: 302 return vfs_attach(&kernfs_vfsops); 303 case MODULE_CMD_FINI: 304 return vfs_detach(&kernfs_vfsops); 305 default: 306 return ENOTTY; 307 } 308 } 309