1 /* $NetBSD: kernfs_vfsops.c,v 1.64 2004/05/29 14:28:41 tron 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.64 2004/05/29 14:28:41 tron 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 60 #include <miscfs/specfs/specdev.h> 61 #include <miscfs/kernfs/kernfs.h> 62 63 MALLOC_DEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures"); 64 65 dev_t rrootdev = NODEV; 66 67 void kernfs_init __P((void)); 68 void kernfs_reinit __P((void)); 69 void kernfs_done __P((void)); 70 void kernfs_get_rrootdev __P((void)); 71 int kernfs_mount __P((struct mount *, const char *, void *, 72 struct nameidata *, struct proc *)); 73 int kernfs_start __P((struct mount *, int, struct proc *)); 74 int kernfs_unmount __P((struct mount *, int, struct proc *)); 75 int kernfs_statvfs __P((struct mount *, struct statvfs *, struct proc *)); 76 int kernfs_quotactl __P((struct mount *, int, uid_t, void *, 77 struct proc *)); 78 int kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *)); 79 int kernfs_vget __P((struct mount *, ino_t, struct vnode **)); 80 int kernfs_fhtovp __P((struct mount *, struct fid *, struct vnode **)); 81 int kernfs_checkexp __P((struct mount *, struct mbuf *, int *, 82 struct ucred **)); 83 int kernfs_vptofh __P((struct vnode *, struct fid *)); 84 85 void 86 kernfs_init() 87 { 88 #ifdef _LKM 89 malloc_type_attach(M_KERNFSMNT); 90 #endif 91 kernfs_hashinit(); 92 } 93 94 void 95 kernfs_reinit() 96 { 97 kernfs_hashreinit(); 98 } 99 100 void 101 kernfs_done() 102 { 103 #ifdef _LKM 104 malloc_type_detach(M_KERNFSMNT); 105 #endif 106 kernfs_hashdone(); 107 } 108 109 void 110 kernfs_get_rrootdev() 111 { 112 static int tried = 0; 113 114 if (tried) { 115 /* Already did it once. */ 116 return; 117 } 118 tried = 1; 119 120 if (rootdev == NODEV) 121 return; 122 rrootdev = devsw_blk2chr(rootdev); 123 if (rrootdev != NODEV) 124 return; 125 rrootdev = NODEV; 126 printf("kernfs_get_rrootdev: no raw root device\n"); 127 } 128 129 /* 130 * Mount the Kernel params filesystem 131 */ 132 int 133 kernfs_mount(mp, path, data, ndp, p) 134 struct mount *mp; 135 const char *path; 136 void *data; 137 struct nameidata *ndp; 138 struct proc *p; 139 { 140 int error = 0; 141 struct kernfs_mount *fmp; 142 143 if (UIO_MX & (UIO_MX - 1)) { 144 log(LOG_ERR, "kernfs: invalid directory entry size"); 145 return (EINVAL); 146 } 147 148 if (mp->mnt_flag & MNT_GETARGS) 149 return 0; 150 /* 151 * Update is a no-op 152 */ 153 if (mp->mnt_flag & MNT_UPDATE) 154 return (EOPNOTSUPP); 155 156 MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount), 157 M_KERNFSMNT, M_WAITOK); 158 memset(fmp, 0, sizeof(*fmp)); 159 TAILQ_INIT(&fmp->nodelist); 160 161 mp->mnt_data = fmp; 162 mp->mnt_flag |= MNT_LOCAL; 163 vfs_getnewfsid(mp); 164 165 if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs", 166 UIO_SYSSPACE, mp, p)) != 0) { 167 free(fmp, M_KERNFSMNT); 168 return error; 169 } 170 171 kernfs_get_rrootdev(); 172 return 0; 173 } 174 175 int 176 kernfs_start(mp, flags, p) 177 struct mount *mp; 178 int flags; 179 struct proc *p; 180 { 181 182 return (0); 183 } 184 185 int 186 kernfs_unmount(mp, mntflags, p) 187 struct mount *mp; 188 int mntflags; 189 struct proc *p; 190 { 191 int error; 192 int flags = 0; 193 194 if (mntflags & MNT_FORCE) 195 flags |= FORCECLOSE; 196 197 if ((error = vflush(mp, 0, flags)) != 0) 198 return (error); 199 200 /* 201 * Finally, throw away the kernfs_mount structure 202 */ 203 free(mp->mnt_data, M_KERNFSMNT); 204 mp->mnt_data = NULL; 205 return (0); 206 } 207 208 int 209 kernfs_root(mp, vpp) 210 struct mount *mp; 211 struct vnode **vpp; 212 { 213 214 /* setup "." */ 215 return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0)); 216 } 217 218 int 219 kernfs_quotactl(mp, cmd, uid, arg, p) 220 struct mount *mp; 221 int cmd; 222 uid_t uid; 223 void *arg; 224 struct proc *p; 225 { 226 227 return (EOPNOTSUPP); 228 } 229 230 int 231 kernfs_statvfs(mp, sbp, p) 232 struct mount *mp; 233 struct statvfs *sbp; 234 struct proc *p; 235 { 236 237 sbp->f_bsize = DEV_BSIZE; 238 sbp->f_frsize = DEV_BSIZE; 239 sbp->f_iosize = DEV_BSIZE; 240 sbp->f_blocks = 2; /* 1K to keep df happy */ 241 sbp->f_bfree = 0; 242 sbp->f_bavail = 0; 243 sbp->f_bresvd = 0; 244 sbp->f_files = 1024; /* XXX lie */ 245 sbp->f_ffree = 128; /* XXX lie */ 246 sbp->f_favail = 128; /* XXX lie */ 247 sbp->f_fresvd = 0; 248 sbp->f_namemax = MAXNAMLEN; 249 copy_statvfs_info(sbp, mp); 250 return (0); 251 } 252 253 /*ARGSUSED*/ 254 int 255 kernfs_sync(mp, waitfor, uc, p) 256 struct mount *mp; 257 int waitfor; 258 struct ucred *uc; 259 struct proc *p; 260 { 261 262 return (0); 263 } 264 265 /* 266 * Kernfs flat namespace lookup. 267 * Currently unsupported. 268 */ 269 int 270 kernfs_vget(mp, ino, vpp) 271 struct mount *mp; 272 ino_t ino; 273 struct vnode **vpp; 274 { 275 276 return (EOPNOTSUPP); 277 } 278 279 /*ARGSUSED*/ 280 int 281 kernfs_fhtovp(mp, fhp, vpp) 282 struct mount *mp; 283 struct fid *fhp; 284 struct vnode **vpp; 285 { 286 287 return (EOPNOTSUPP); 288 } 289 290 /*ARGSUSED*/ 291 int 292 kernfs_checkexp(mp, mb, what, anon) 293 struct mount *mp; 294 struct mbuf *mb; 295 int *what; 296 struct ucred **anon; 297 { 298 299 return (EOPNOTSUPP); 300 } 301 302 /*ARGSUSED*/ 303 int 304 kernfs_vptofh(vp, fhp) 305 struct vnode *vp; 306 struct fid *fhp; 307 { 308 309 return (EOPNOTSUPP); 310 } 311 312 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup") 313 { 314 315 sysctl_createv(clog, 0, NULL, NULL, 316 CTLFLAG_PERMANENT, 317 CTLTYPE_NODE, "vfs", NULL, 318 NULL, 0, NULL, 0, 319 CTL_VFS, CTL_EOL); 320 sysctl_createv(clog, 0, NULL, NULL, 321 CTLFLAG_PERMANENT, 322 CTLTYPE_NODE, "kernfs", 323 SYSCTL_DESCR("/kern file system"), 324 NULL, 0, NULL, 0, 325 CTL_VFS, 11, CTL_EOL); 326 /* 327 * XXX the "11" above could be dynamic, thereby eliminating one 328 * more instance of the "number to vfs" mapping problem, but 329 * "11" is the order as taken from sys/mount.h 330 */ 331 } 332 333 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc; 334 335 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = { 336 &kernfs_vnodeop_opv_desc, 337 NULL, 338 }; 339 340 struct vfsops kernfs_vfsops = { 341 MOUNT_KERNFS, 342 kernfs_mount, 343 kernfs_start, 344 kernfs_unmount, 345 kernfs_root, 346 kernfs_quotactl, 347 kernfs_statvfs, 348 kernfs_sync, 349 kernfs_vget, 350 kernfs_fhtovp, 351 kernfs_vptofh, 352 kernfs_init, 353 kernfs_reinit, 354 kernfs_done, 355 NULL, 356 NULL, /* vfs_mountroot */ 357 kernfs_checkexp, 358 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 359 kernfs_vnodeopv_descs, 360 }; 361