devfs_devs.c revision 130585
1/* 2 * Copyright (c) 2000 3 * Poul-Henning Kamp. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Neither the name of the University nor the names of its contributors 11 * may be used to endorse or promote products derived from this software 12 * without specific prior written permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36 27 * 28 * $FreeBSD: head/sys/fs/devfs/devfs_devs.c 130585 2004-06-16 09:47:26Z phk $ 29 */ 30 31#include "opt_devfs.h" 32#include "opt_mac.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/conf.h> 37#include <sys/dirent.h> 38#include <sys/kernel.h> 39#include <sys/lock.h> 40#include <sys/mac.h> 41#include <sys/malloc.h> 42#include <sys/proc.h> 43#include <sys/sysctl.h> 44#include <sys/vnode.h> 45 46#include <machine/atomic.h> 47 48#include <fs/devfs/devfs.h> 49 50static struct cdev *devfs_inot[NDEVFSINO]; 51static struct cdev **devfs_overflow; 52static int devfs_ref[NDEVFSINO]; 53static int *devfs_refoverflow; 54static int devfs_nextino = 3; 55static int devfs_numino; 56static int devfs_topino; 57static int devfs_noverflowwant = NDEVFSOVERFLOW; 58static int devfs_noverflow; 59static unsigned devfs_generation; 60 61static void devfs_attemptoverflow(int insist); 62static struct devfs_dirent *devfs_find (struct devfs_dirent *dd, const char *name, int namelen); 63 64SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem"); 65SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW, 66 &devfs_noverflowwant, 0, "Size of DEVFS overflow table"); 67SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD, 68 &devfs_generation, 0, "DEVFS generation number"); 69SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD, 70 &devfs_numino, 0, "DEVFS inodes"); 71SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD, 72 &devfs_topino, 0, "DEVFS highest inode#"); 73 74static int * 75devfs_itor(int inode) 76{ 77 if (inode < NDEVFSINO) 78 return (&devfs_ref[inode]); 79 else if (inode < NDEVFSINO + devfs_noverflow) 80 return (&devfs_refoverflow[inode - NDEVFSINO]); 81 else 82 panic ("YRK!"); 83} 84 85static void 86devfs_dropref(int inode) 87{ 88 int *ip; 89 90 ip = devfs_itor(inode); 91 atomic_add_int(ip, -1); 92} 93 94static int 95devfs_getref(int inode) 96{ 97 int *ip, i, j; 98 struct cdev **dp; 99 100 ip = devfs_itor(inode); 101 dp = devfs_itod(inode); 102 for (;;) { 103 i = *ip; 104 j = i + 1; 105 if (!atomic_cmpset_int(ip, i, j)) 106 continue; 107 if (*dp != NULL) 108 return (1); 109 atomic_add_int(ip, -1); 110 return(0); 111 } 112} 113 114struct devfs_dirent ** 115devfs_itode (struct devfs_mount *dm, int inode) 116{ 117 118 if (inode < NDEVFSINO) 119 return (&dm->dm_dirent[inode]); 120 if (devfs_overflow == NULL) 121 return (NULL); 122 if (inode < NDEVFSINO + devfs_noverflow) 123 return (&dm->dm_overflow[inode - NDEVFSINO]); 124 return (NULL); 125} 126 127struct cdev ** 128devfs_itod (int inode) 129{ 130 131 if (inode < NDEVFSINO) 132 return (&devfs_inot[inode]); 133 if (devfs_overflow == NULL) 134 return (NULL); 135 if (inode < NDEVFSINO + devfs_noverflow) 136 return (&devfs_overflow[inode - NDEVFSINO]); 137 return (NULL); 138} 139 140static void 141devfs_attemptoverflow(int insist) 142{ 143 struct cdev ***ot; 144 int *or; 145 int n, nb; 146 147 /* Check if somebody beat us to it */ 148 if (devfs_overflow != NULL) 149 return; 150 ot = NULL; 151 or = NULL; 152 n = devfs_noverflowwant; 153 nb = sizeof (struct cdev **) * n; 154 MALLOC(ot, struct cdev ***, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO); 155 if (ot == NULL) 156 goto bail; 157 nb = sizeof (int) * n; 158 MALLOC(or, int *, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO); 159 if (or == NULL) 160 goto bail; 161 if (!atomic_cmpset_ptr(&devfs_overflow, NULL, ot)) 162 goto bail; 163 devfs_refoverflow = or; 164 devfs_noverflow = n; 165 printf("DEVFS Overflow table with %d entries allocated when %d in use\n", n, devfs_numino); 166 return; 167 168bail: 169 /* Somebody beat us to it, or something went wrong. */ 170 if (ot != NULL) 171 FREE(ot, M_DEVFS); 172 if (or != NULL) 173 FREE(or, M_DEVFS); 174 return; 175} 176 177static struct devfs_dirent * 178devfs_find(struct devfs_dirent *dd, const char *name, int namelen) 179{ 180 struct devfs_dirent *de; 181 182 TAILQ_FOREACH(de, &dd->de_dlist, de_list) { 183 if (namelen != de->de_dirent->d_namlen) 184 continue; 185 if (bcmp(name, de->de_dirent->d_name, namelen) != 0) 186 continue; 187 break; 188 } 189 return (de); 190} 191 192struct devfs_dirent * 193devfs_newdirent(char *name, int namelen) 194{ 195 int i; 196 struct devfs_dirent *de; 197 struct dirent d; 198 199 d.d_namlen = namelen; 200 i = sizeof (*de) + GENERIC_DIRSIZ(&d); 201 MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK | M_ZERO); 202 de->de_dirent = (struct dirent *)(de + 1); 203 de->de_dirent->d_namlen = namelen; 204 de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d); 205 bcopy(name, de->de_dirent->d_name, namelen); 206 de->de_dirent->d_name[namelen] = '\0'; 207 vfs_timestamp(&de->de_ctime); 208 de->de_mtime = de->de_atime = de->de_ctime; 209 de->de_links = 1; 210#ifdef MAC 211 mac_init_devfsdirent(de); 212#endif 213 return (de); 214} 215 216struct devfs_dirent * 217devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot) 218{ 219 struct devfs_dirent *dd; 220 struct devfs_dirent *de; 221 222 dd = devfs_newdirent(name, namelen); 223 224 TAILQ_INIT(&dd->de_dlist); 225 226 dd->de_dirent->d_type = DT_DIR; 227 dd->de_mode = 0555; 228 dd->de_links = 2; 229 dd->de_dir = dd; 230 231 de = devfs_newdirent(".", 1); 232 de->de_dirent->d_type = DT_DIR; 233 de->de_dir = dd; 234 de->de_flags |= DE_DOT; 235 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 236 237 de = devfs_newdirent("..", 2); 238 de->de_dirent->d_type = DT_DIR; 239 if (dotdot == NULL) 240 de->de_dir = dd; 241 else 242 de->de_dir = dotdot; 243 de->de_flags |= DE_DOTDOT; 244 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 245 246 return (dd); 247} 248 249static void 250devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de) 251{ 252 253 if (de->de_symlink) { 254 FREE(de->de_symlink, M_DEVFS); 255 de->de_symlink = NULL; 256 } 257 if (de->de_vnode) 258 de->de_vnode->v_data = NULL; 259 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 260#ifdef MAC 261 mac_destroy_devfsdirent(de); 262#endif 263 FREE(de, M_DEVFS); 264} 265 266void 267devfs_purge(struct devfs_dirent *dd) 268{ 269 struct devfs_dirent *de; 270 271 for (;;) { 272 de = TAILQ_FIRST(&dd->de_dlist); 273 if (de == NULL) 274 break; 275 devfs_delete(dd, de); 276 } 277 FREE(dd, M_DEVFS); 278} 279 280 281int 282devfs_populate(struct devfs_mount *dm) 283{ 284 int i, j; 285 struct cdev *dev, *pdev; 286 struct devfs_dirent *dd; 287 struct devfs_dirent *de, **dep; 288 char *q, *s; 289 290 if (dm->dm_generation == devfs_generation) 291 return (0); 292 lockmgr(&dm->dm_lock, LK_UPGRADE, 0, curthread); 293 if (devfs_noverflow && dm->dm_overflow == NULL) { 294 i = devfs_noverflow * sizeof (struct devfs_dirent *); 295 MALLOC(dm->dm_overflow, struct devfs_dirent **, i, 296 M_DEVFS, M_WAITOK | M_ZERO); 297 } 298 while (dm->dm_generation != devfs_generation) { 299 dm->dm_generation = devfs_generation; 300 for (i = 0; i <= devfs_topino; i++) { 301 dev = *devfs_itod(i); 302 dep = devfs_itode(dm, i); 303 de = *dep; 304 if (dev == NULL && de == DE_DELETED) { 305 *dep = NULL; 306 continue; 307 } 308 if (dev == NULL && de != NULL) { 309 dd = de->de_dir; 310 *dep = NULL; 311 TAILQ_REMOVE(&dd->de_dlist, de, de_list); 312 if (de->de_vnode) 313 de->de_vnode->v_data = NULL; 314 FREE(de, M_DEVFS); 315 devfs_dropref(i); 316 continue; 317 } 318 if (dev == NULL) 319 continue; 320 if (de != NULL) 321 continue; 322 if (!devfs_getref(i)) 323 continue; 324 dd = dm->dm_basedir; 325 s = dev->si_name; 326 for (;;) { 327 for (q = s; *q != '/' && *q != '\0'; q++) 328 continue; 329 if (*q != '/') 330 break; 331 de = devfs_find(dd, s, q - s); 332 if (de == NULL) { 333 de = devfs_vmkdir(s, q - s, dd); 334#ifdef MAC 335 mac_create_devfs_directory( 336 dm->dm_mount, s, q - s, de); 337#endif 338 de->de_inode = dm->dm_inode++; 339 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 340 dd->de_links++; 341 } 342 s = q + 1; 343 dd = de; 344 } 345 de = devfs_newdirent(s, q - s); 346 if (dev->si_flags & SI_ALIAS) { 347 de->de_inode = dm->dm_inode++; 348 de->de_uid = 0; 349 de->de_gid = 0; 350 de->de_mode = 0755; 351 de->de_dirent->d_type = DT_LNK; 352 pdev = dev->si_parent; 353 j = strlen(pdev->si_name) + 1; 354 MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK); 355 bcopy(pdev->si_name, de->de_symlink, j); 356 } else { 357 de->de_inode = i; 358 de->de_uid = dev->si_uid; 359 de->de_gid = dev->si_gid; 360 de->de_mode = dev->si_mode; 361 de->de_dirent->d_type = DT_CHR; 362 } 363#ifdef MAC 364 mac_create_devfs_device(dm->dm_mount, dev, de); 365#endif 366 *dep = de; 367 de->de_dir = dd; 368 devfs_rules_apply(dm, de); 369 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list); 370#if 0 371 printf("Add ino%d %s\n", i, dev->si_name); 372#endif 373 } 374 } 375 lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, curthread); 376 return (0); 377} 378 379void 380devfs_create(struct cdev *dev) 381{ 382 int ino, i, *ip; 383 struct cdev **dp; 384 385 for (;;) { 386 /* Grab the next inode number */ 387 ino = devfs_nextino; 388 i = ino + 1; 389 /* wrap around when we reach the end */ 390 if (i >= NDEVFSINO + devfs_noverflow) 391 i = 3; 392 if (!atomic_cmpset_int(&devfs_nextino, ino, i)) 393 continue; 394 395 /* see if it was occupied */ 396 dp = devfs_itod(ino); 397 if (dp == NULL) 398 Debugger("dp == NULL\n"); 399 if (*dp != NULL) 400 continue; 401 ip = devfs_itor(ino); 402 if (ip == NULL) 403 Debugger("ip == NULL\n"); 404 if (*ip != 0) 405 continue; 406 407 if (!atomic_cmpset_ptr(dp, NULL, dev)) 408 continue; 409 410 dev->si_inode = ino; 411 for (;;) { 412 i = devfs_topino; 413 if (i >= ino) 414 break; 415 if (atomic_cmpset_int(&devfs_topino, i, ino)) 416 break; 417 printf("failed topino %d %d\n", i, ino); 418 } 419 break; 420 } 421 422 atomic_add_int(&devfs_numino, 1); 423 atomic_add_int(&devfs_generation, 1); 424 if (devfs_overflow == NULL && devfs_numino + 100 > NDEVFSINO) 425 devfs_attemptoverflow(0); 426} 427 428void 429devfs_destroy(struct cdev *dev) 430{ 431 int ino, i; 432 433 ino = dev->si_inode; 434 dev->si_inode = 0; 435 if (ino == 0) 436 return; 437 if (atomic_cmpset_ptr(devfs_itod(ino), dev, NULL)) { 438 atomic_add_int(&devfs_generation, 1); 439 atomic_add_int(&devfs_numino, -1); 440 i = devfs_nextino; 441 if (ino < i) 442 atomic_cmpset_int(&devfs_nextino, i, ino); 443 } 444} 445