Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 | /* * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright 1997,1998 Julian Elischer. All rights reserved. * julian@freebsd.org * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * devfs_tree.c */ /* * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. */ /* * HISTORY * Dieter Siegmund (dieter@apple.com) Thu Apr 8 14:08:19 PDT 1999 * - removed mounting of "hidden" mountpoint * - fixed problem in which devnode->dn_vn pointer was not * updated with the vnode returned from checkalias() * - replaced devfs_vntodn() with a macro VTODN() * - rewrote dev_finddir() to not use recursion * - added locking to avoid data structure corruption (DEVFS_(UN)LOCK()) * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999 * - fixed problem with devfs_dntovn() checking the v_id against the * value cached in the device node; a union mount on top of us causes * the v_id to get incremented thus, we would end up returning a new * vnode instead of the existing one that has the mounted_here * field filled in; the net effect was that the filesystem mounted * on top of us would never show up * - added devfs_stats to store how many data structures are actually * allocated */ /* SPLIT_DEVS means each devfs uses a different devnode for the same device */ /* Otherwise the same device always ends up at the same vnode even if */ /* reached througgh a different devfs instance. The practical difference */ /* is that with the same vnode, chmods and chowns show up on all instances of */ /* a device. (etc) */ #define SPLIT_DEVS 1 /* maybe make this an option */ /*#define SPLIT_DEVS 1*/ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/conf.h> #include <sys/malloc.h> #include <sys/mount_internal.h> #include <sys/proc.h> #include <sys/vnode_internal.h> #include <stdarg.h> #include <libkern/OSAtomic.h> #define BSD_KERNEL_PRIVATE 1 /* devfs_make_link() prototype */ #include "devfs.h" #include "devfsdefs.h" #if CONFIG_MACF #include <security/mac_framework.h> #endif static void devfs_release_busy(devnode_t *); static void dev_free_hier(devdirent_t *); static int devfs_propogate(devdirent_t *, devdirent_t *); static int dev_finddir(const char *, devnode_t *, int, devnode_t **); static int dev_dup_entry(devnode_t *, devdirent_t *, devdirent_t **, struct devfsmount *); lck_grp_t * devfs_lck_grp; lck_grp_attr_t * devfs_lck_grp_attr; lck_attr_t * devfs_lck_attr; lck_mtx_t devfs_mutex; devdirent_t * dev_root = NULL; /* root of backing tree */ struct devfs_stats devfs_stats; /* hold stats */ #ifdef HIDDEN_MOUNTPOINT static struct mount *devfs_hidden_mount; #endif /* HIDDEN_MOINTPOINT */ static int devfs_ready = 0; #define DEVFS_NOCREATE FALSE #define DEVFS_CREATE TRUE /* * Set up the root directory node in the backing plane * This is happenning before the vfs system has been * set up yet, so be careful about what we reference.. * Notice that the ops are by indirection.. as they haven't * been set up yet! * DEVFS has a hidden mountpoint that is used as the anchor point * for the internal 'blueprint' version of the dev filesystem tree. */ /*proto*/ int devfs_sinit(void) { int error; devfs_lck_grp_attr = lck_grp_attr_alloc_init(); devfs_lck_grp = lck_grp_alloc_init("devfs_lock", devfs_lck_grp_attr); devfs_lck_attr = lck_attr_alloc_init(); lck_mtx_init(&devfs_mutex, devfs_lck_grp, devfs_lck_attr); DEVFS_LOCK(); error = dev_add_entry("root", NULL, DEV_DIR, NULL, NULL, NULL, &dev_root); DEVFS_UNLOCK(); if (error) { printf("devfs_sinit: dev_add_entry failed "); return (ENOTSUP); } #ifdef HIDDEN_MOUNTPOINT MALLOC(devfs_hidden_mount, struct mount *, sizeof(struct mount), M_MOUNT, M_WAITOK); bzero(devfs_hidden_mount,sizeof(struct mount)); mount_lock_init(devfs_hidden_mount); TAILQ_INIT(&devfs_hidden_mount->mnt_vnodelist); TAILQ_INIT(&devfs_hidden_mount->mnt_workerqueue); TAILQ_INIT(&devfs_hidden_mount->mnt_newvnodes); #if CONFIG_MACF mac_mount_label_init(devfs_hidden_mount); mac_mount_label_associate(vfs_context_kernel(), devfs_hidden_mount); #endif /* Initialize the default IO constraints */ mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS; mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32; mp->mnt_ioflags = 0; mp->mnt_realrootvp = NULLVP; mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL; devfs_mount(devfs_hidden_mount,"dummy",NULL,NULL,NULL); dev_root->de_dnp->dn_dvm = (struct devfsmount *)devfs_hidden_mount->mnt_data; #endif /* HIDDEN_MOUNTPOINT */ #if CONFIG_MACF mac_devfs_label_associate_directory("/", strlen("/"), dev_root->de_dnp, "/"); #endif devfs_ready = 1; return (0); } /***********************************************************************\ ************************************************************************* * Routines used to find our way to a point in the tree * ************************************************************************* \***********************************************************************/ /*************************************************************** * Search down the linked list off a dir to find "name" * return the devnode_t * for that node. * * called with DEVFS_LOCK held ***************************************************************/ devdirent_t * dev_findname(devnode_t * dir, const char *name) { devdirent_t * newfp; if (dir->dn_type != DEV_DIR) return 0;/*XXX*/ /* printf?*/ if (name[0] == '.') { if(name[1] == 0) { return dir->dn_typeinfo.Dir.myname; } if((name[1] == '.') && (name[2] == 0)) { /* for root, .. == . */ return dir->dn_typeinfo.Dir.parent->dn_typeinfo.Dir.myname; } } newfp = dir->dn_typeinfo.Dir.dirlist; while(newfp) { if(!(strncmp(name, newfp->de_name, sizeof(newfp->de_name)))) return newfp; newfp = newfp->de_next; } return NULL; } /*********************************************************************** * Given a starting node (0 for root) and a pathname, return the node * for the end item on the path. It MUST BE A DIRECTORY. If the 'DEVFS_CREATE' * option is true, then create any missing nodes in the path and create * and return the final node as well. * This is used to set up a directory, before making nodes in it.. * * called with DEVFS_LOCK held ***********************************************************************/ static int dev_finddir(const char * path, devnode_t * dirnode, int create, devnode_t * * dn_pp) { devnode_t * dnp = NULL; int error = 0; const char * scan; #if CONFIG_MACF char fullpath[DEVMAXPATHSIZE]; #endif if (!dirnode) /* dirnode == NULL means start at root */ dirnode = dev_root->de_dnp; if (dirnode->dn_type != DEV_DIR) return ENOTDIR; if (strlen(path) > (DEVMAXPATHSIZE - 1)) return ENAMETOOLONG; #if CONFIG_MACF strlcpy (fullpath, path, DEVMAXPATHSIZE); #endif scan = path; while (*scan == '/') scan++; *dn_pp = NULL; while (1) { char component[DEVMAXPATHSIZE]; devdirent_t * dirent_p; const char * start; if (*scan == 0) { /* we hit the end of the string, we're done */ *dn_pp = dirnode; break; } start = scan; while (*scan != '/' && *scan) scan++; strlcpy(component, start, scan - start); if (*scan == '/') scan++; dirent_p = dev_findname(dirnode, component); if (dirent_p) { dnp = dirent_p->de_dnp; if (dnp->dn_type != DEV_DIR) { error = ENOTDIR; break; } } else { if (!create) { error = ENOENT; break; } error = dev_add_entry(component, dirnode, DEV_DIR, NULL, NULL, NULL, &dirent_p); if (error) break; dnp = dirent_p->de_dnp; #if CONFIG_MACF mac_devfs_label_associate_directory( dirnode->dn_typeinfo.Dir.myname->de_name, strlen(dirnode->dn_typeinfo.Dir.myname->de_name), dnp, fullpath); #endif devfs_propogate(dirnode->dn_typeinfo.Dir.myname, dirent_p); } dirnode = dnp; /* continue relative to this directory */ } return (error); } /*********************************************************************** * Add a new NAME element to the devfs * If we're creating a root node, then dirname is NULL * Basically this creates a new namespace entry for the device node * * Creates a name node, and links it to the supplied node * * called with DEVFS_LOCK held ***********************************************************************/ int dev_add_name(const char * name, devnode_t * dirnode, __unused devdirent_t * back, devnode_t * dnp, devdirent_t * *dirent_pp) { devdirent_t * dirent_p = NULL; if(dirnode != NULL ) { if(dirnode->dn_type != DEV_DIR) return(ENOTDIR); if( dev_findname(dirnode,name)) return(EEXIST); } /* * make sure the name is legal * slightly misleading in the case of NULL */ if (!name || (strlen(name) > (DEVMAXNAMESIZE - 1))) return (ENAMETOOLONG); /* * Allocate and fill out a new directory entry */ MALLOC(dirent_p, devdirent_t *, sizeof(devdirent_t), M_DEVFSNAME, M_WAITOK); if (!dirent_p) { return ENOMEM; } bzero(dirent_p,sizeof(devdirent_t)); /* inherrit our parent's mount info */ /*XXX*/ /* a kludge but.... */ if(dirnode && ( dnp->dn_dvm == NULL)) { dnp->dn_dvm = dirnode->dn_dvm; /* if(!dnp->dn_dvm) printf("parent had null dvm "); */ } /* * Link the two together * include the implicit link in the count of links to the devnode.. * this stops it from being accidentally freed later. */ dirent_p->de_dnp = dnp; dnp->dn_links++ ; /* implicit from our own name-node */ /* * Make sure that we can find all the links that reference a node * so that we can get them all if we need to zap the node. */ if(dnp->dn_linklist) { dirent_p->de_nextlink = dnp->dn_linklist; dirent_p->de_prevlinkp = dirent_p->de_nextlink->de_prevlinkp; dirent_p->de_nextlink->de_prevlinkp = &(dirent_p->de_nextlink); *dirent_p->de_prevlinkp = dirent_p; } else { dirent_p->de_nextlink = dirent_p; dirent_p->de_prevlinkp = &(dirent_p->de_nextlink); } dnp->dn_linklist = dirent_p; /* * If the node is a directory, then we need to handle the * creation of the .. link. * A NULL dirnode indicates a root node, so point to ourself. */ if(dnp->dn_type == DEV_DIR) { dnp->dn_typeinfo.Dir.myname = dirent_p; /* * If we are unlinking from an old dir, decrement its links * as we point our '..' elsewhere * Note: it's up to the calling code to remove the * us from the original directory's list */ if(dnp->dn_typeinfo.Dir.parent) { dnp->dn_typeinfo.Dir.parent->dn_links--; } if(dirnode) { dnp->dn_typeinfo.Dir.parent = dirnode; } else { dnp->dn_typeinfo.Dir.parent = dnp; } dnp->dn_typeinfo.Dir.parent->dn_links++; /* account for the new '..' */ } /* * put the name into the directory entry. */ strlcpy(dirent_p->de_name, name, DEVMAXNAMESIZE); /* * Check if we are not making a root node.. * (i.e. have parent) */ if(dirnode) { /* * Put it on the END of the linked list of directory entries */ dirent_p->de_parent = dirnode; /* null for root */ dirent_p->de_prevp = dirnode->dn_typeinfo.Dir.dirlast; dirent_p->de_next = *(dirent_p->de_prevp); /* should be NULL */ /*right?*/ *(dirent_p->de_prevp) = dirent_p; dirnode->dn_typeinfo.Dir.dirlast = &(dirent_p->de_next); dirnode->dn_typeinfo.Dir.entrycount++; dirnode->dn_len += strlen(name) + 8;/*ok, ok?*/ } *dirent_pp = dirent_p; DEVFS_INCR_ENTRIES(); return 0 ; } /*********************************************************************** * Add a new element to the devfs plane. * * Creates a new dev_node to go with it if the prototype should not be * reused. (Is a DIR, or we select SPLIT_DEVS at compile time) * typeinfo gives us info to make our node if we don't have a prototype. * If typeinfo is null and proto exists, then the typeinfo field of * the proto is used intead in the DEVFS_CREATE case. * note the 'links' count is 0 (except if a dir) * but it is only cleared on a transition * so this is ok till we link it to something * Even in SPLIT_DEVS mode, * if the node already exists on the wanted plane, just return it * * called with DEVFS_LOCK held ***********************************************************************/ int dev_add_node(int entrytype, devnode_type_t * typeinfo, devnode_t * proto, devnode_t * *dn_pp, struct devfsmount *dvm) { devnode_t * dnp = NULL; #if defined SPLIT_DEVS /* * If we have a prototype, then check if there is already a sibling * on the mount plane we are looking at, if so, just return it. */ if (proto) { dnp = proto->dn_nextsibling; while( dnp != proto) { if (dnp->dn_dvm == dvm) { *dn_pp = dnp; return (0); } dnp = dnp->dn_nextsibling; } if (typeinfo == NULL) typeinfo = &(proto->dn_typeinfo); } #else /* SPLIT_DEVS */ if ( proto ) { switch (proto->type) { case DEV_BDEV: case DEV_CDEV: *dn_pp = proto; return 0; } } #endif /* SPLIT_DEVS */ MALLOC(dnp, devnode_t *, sizeof(devnode_t), M_DEVFSNODE, M_WAITOK); if (!dnp) { return ENOMEM; } /* * If we have a proto, that means that we are duplicating some * other device, which can only happen if we are not at the back plane */ if (proto) { bcopy(proto, dnp, sizeof(devnode_t)); dnp->dn_links = 0; dnp->dn_linklist = NULL; dnp->dn_vn = NULL; dnp->dn_len = 0; /* add to END of siblings list */ dnp->dn_prevsiblingp = proto->dn_prevsiblingp; *(dnp->dn_prevsiblingp) = dnp; dnp->dn_nextsibling = proto; proto->dn_prevsiblingp = &(dnp->dn_nextsibling); #if CONFIG_MACF mac_devfs_label_init(dnp); mac_devfs_label_copy(proto->dn_label, dnp->dn_label); #endif } else { struct timeval tv; /* * We have no prototype, so start off with a clean slate */ microtime(&tv); bzero(dnp, sizeof(devnode_t)); dnp->dn_type = entrytype; dnp->dn_nextsibling = dnp; dnp->dn_prevsiblingp = &(dnp->dn_nextsibling); dnp->dn_atime.tv_sec = tv.tv_sec; dnp->dn_mtime.tv_sec = tv.tv_sec; dnp->dn_ctime.tv_sec = tv.tv_sec; #if CONFIG_MACF mac_devfs_label_init(dnp); #endif } dnp->dn_dvm = dvm; /* * fill out the dev node according to type */ switch(entrytype) { case DEV_DIR: /* * As it's a directory, make sure * it has a null entries list */ dnp->dn_typeinfo.Dir.dirlast = &(dnp->dn_typeinfo.Dir.dirlist); dnp->dn_typeinfo.Dir.dirlist = (devdirent_t *)0; dnp->dn_typeinfo.Dir.entrycount = 0; /* until we know better, it has a null parent pointer*/ dnp->dn_typeinfo.Dir.parent = NULL; dnp->dn_links++; /* for .*/ dnp->dn_typeinfo.Dir.myname = NULL; /* * make sure that the ops associated with it are the ops * that we use (by default) for directories */ dnp->dn_ops = &devfs_vnodeop_p; dnp->dn_mode |= 0555; /* default perms */ break; case DEV_SLNK: /* * As it's a symlink allocate and store the link info * Symlinks should only ever be created by the user, * so they are not on the back plane and should not be * propogated forward.. a bit like directories in that way.. * A symlink only exists on one plane and has its own * node.. therefore we might be on any random plane. */ MALLOC(dnp->dn_typeinfo.Slnk.name, char *, typeinfo->Slnk.namelen+1, M_DEVFSNODE, M_WAITOK); if (!dnp->dn_typeinfo.Slnk.name) { FREE(dnp,M_DEVFSNODE); return ENOMEM; } strlcpy(dnp->dn_typeinfo.Slnk.name, typeinfo->Slnk.name, typeinfo->Slnk.namelen + 1); dnp->dn_typeinfo.Slnk.namelen = typeinfo->Slnk.namelen; DEVFS_INCR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1); dnp->dn_ops = &devfs_vnodeop_p; dnp->dn_mode |= 0555; /* default perms */ break; case DEV_CDEV: case DEV_BDEV: /* * Make sure it has DEVICE type ops * and device specific fields are correct */ dnp->dn_ops = &devfs_spec_vnodeop_p; dnp->dn_typeinfo.dev = typeinfo->dev; break; default: return EINVAL; } *dn_pp = dnp; DEVFS_INCR_NODES(); return 0 ; } /*********************************************************************** * called with DEVFS_LOCK held **********************************************************************/ void devnode_free(devnode_t * dnp) { if (dnp->dn_lflags & DN_BUSY) { dnp->dn_lflags |= DN_DELETE; return; } #if CONFIG_MACF mac_devfs_label_destroy(dnp); #endif if (dnp->dn_type == DEV_SLNK) { DEVFS_DECR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1); FREE(dnp->dn_typeinfo.Slnk.name, M_DEVFSNODE); } DEVFS_DECR_NODES(); FREE(dnp, M_DEVFSNODE); } /*********************************************************************** * called with DEVFS_LOCK held **********************************************************************/ static void devfs_dn_free(devnode_t * dnp) { if(--dnp->dn_links <= 0 ) /* can be -1 for initial free, on error */ { /*probably need to do other cleanups XXX */ if (dnp->dn_nextsibling != dnp) { devnode_t * * prevp = dnp->dn_prevsiblingp; *prevp = dnp->dn_nextsibling; dnp->dn_nextsibling->dn_prevsiblingp = prevp; } if (dnp->dn_vn == NULL) { devnode_free(dnp); /* no accesses/references */ } else { dnp->dn_delete = TRUE; } } } /***********************************************************************\ * Front Node Operations * * Add or delete a chain of front nodes * \***********************************************************************/ /*********************************************************************** * Given a directory backing node, and a child backing node, add the * appropriate front nodes to the front nodes of the directory to * represent the child node to the user * * on failure, front nodes will either be correct or not exist for each * front dir, however dirs completed will not be stripped of completed * frontnodes on failure of a later frontnode * * This allows a new node to be propogated through all mounted planes * * called with DEVFS_LOCK held ***********************************************************************/ static int devfs_propogate(devdirent_t * parent,devdirent_t * child) { int error; devdirent_t * newnmp; devnode_t * dnp = child->de_dnp; devnode_t * pdnp = parent->de_dnp; devnode_t * adnp = parent->de_dnp; int type = child->de_dnp->dn_type; /*********************************************** * Find the other instances of the parent node ***********************************************/ for (adnp = pdnp->dn_nextsibling; adnp != pdnp; adnp = adnp->dn_nextsibling) { /* * Make the node, using the original as a prototype) * if the node already exists on that plane it won't be * re-made.. */ if ((error = dev_add_entry(child->de_name, adnp, type, NULL, dnp, adnp->dn_dvm, &newnmp)) != 0) { printf("duplicating %s failed\n",child->de_name); } } return 0; /* for now always succeed */ } /*********************************************************************** * remove all instances of this devicename [for backing nodes..] * note.. if there is another link to the node (non dir nodes only) * then the devfs_node will still exist as the ref count will be non-0 * removing a directory node will remove all sup-nodes on all planes (ZAP) * * Used by device drivers to remove nodes that are no longer relevant * The argument is the 'cookie' they were given when they created the node * this function is exported.. see devfs.h ***********************************************************************/ void devfs_remove(void *dirent_p) { devnode_t * dnp = ((devdirent_t *)dirent_p)->de_dnp; devnode_t * dnp2; boolean_t lastlink; DEVFS_LOCK(); if (!devfs_ready) { printf("devfs_remove: not ready for devices!\n"); goto out; } /* keep removing the next sibling till only we exist. */ while ((dnp2 = dnp->dn_nextsibling) != dnp) { /* * Keep removing the next front node till no more exist */ dnp->dn_nextsibling = dnp2->dn_nextsibling; dnp->dn_nextsibling->dn_prevsiblingp = &(dnp->dn_nextsibling); dnp2->dn_nextsibling = dnp2; dnp2->dn_prevsiblingp = &(dnp2->dn_nextsibling); if (dnp2->dn_linklist) { do { lastlink = (1 == dnp2->dn_links); dev_free_name(dnp2->dn_linklist); } while (!lastlink); } } /* * then free the main node * If we are not running in SPLIT_DEVS mode, then * THIS is what gets rid of the propogated nodes. */ if (dnp->dn_linklist) { do { lastlink = (1 == dnp->dn_links); dev_free_name(dnp->dn_linklist); } while (!lastlink); } out: DEVFS_UNLOCK(); return ; } /*************************************************************** * duplicate the backing tree into a tree of nodes hung off the * mount point given as the argument. Do this by * calling dev_dup_entry which recurses all the way * up the tree.. * * called with DEVFS_LOCK held **************************************************************/ int dev_dup_plane(struct devfsmount *devfs_mp_p) { devdirent_t * new; int error = 0; if ((error = dev_dup_entry(NULL, dev_root, &new, devfs_mp_p))) return error; devfs_mp_p->plane_root = new; return error; } /*************************************************************** * Free a whole plane * * called with DEVFS_LOCK held ***************************************************************/ void devfs_free_plane(struct devfsmount *devfs_mp_p) { devdirent_t * dirent_p; dirent_p = devfs_mp_p->plane_root; if (dirent_p) { dev_free_hier(dirent_p); dev_free_name(dirent_p); } devfs_mp_p->plane_root = NULL; } /*************************************************************** * Create and link in a new front element.. * Parent can be 0 for a root node * Not presently usable to make a symlink XXX * (Ok, symlinks don't propogate) * recursively will create subnodes corresponding to equivalent * child nodes in the base level * * called with DEVFS_LOCK held ***************************************************************/ static int dev_dup_entry(devnode_t * parent, devdirent_t * back, devdirent_t * *dnm_pp, struct devfsmount *dvm) { devdirent_t * entry_p; devdirent_t * newback; devdirent_t * newfront; int error; devnode_t * dnp = back->de_dnp; int type = dnp->dn_type; /* * go get the node made (if we need to) * use the back one as a prototype */ if ((error = dev_add_entry(back->de_name, parent, type, NULL, dnp, parent?parent->dn_dvm:dvm, &entry_p)) != 0) { printf("duplicating %s failed\n",back->de_name); } /* * If we have just made the root, then insert the pointer to the * mount information */ if(dvm) { entry_p->de_dnp->dn_dvm = dvm; } /* * If it is a directory, then recurse down all the other * subnodes in it.... * note that this time we don't pass on the mount info.. */ if (type == DEV_DIR) { for(newback = back->de_dnp->dn_typeinfo.Dir.dirlist; newback; newback = newback->de_next) { if((error = dev_dup_entry(entry_p->de_dnp, newback, &newfront, NULL)) != 0) { break; /* back out with an error */ } } } *dnm_pp = entry_p; return error; } /*************************************************************** * Free a name node * remember that if there are other names pointing to the * dev_node then it may not get freed yet * can handle if there is no dnp * * called with DEVFS_LOCK held ***************************************************************/ int dev_free_name(devdirent_t * dirent_p) { devnode_t * parent = dirent_p->de_parent; devnode_t * dnp = dirent_p->de_dnp; if(dnp) { if(dnp->dn_type == DEV_DIR) { devnode_t * p; if(dnp->dn_typeinfo.Dir.dirlist) return (ENOTEMPTY); p = dnp->dn_typeinfo.Dir.parent; devfs_dn_free(dnp); /* account for '.' */ devfs_dn_free(p); /* '..' */ } /* * unlink us from the list of links for this node * If we are the only link, it's easy! * if we are a DIR of course there should not be any * other links. */ if(dirent_p->de_nextlink == dirent_p) { dnp->dn_linklist = NULL; } else { if(dnp->dn_linklist == dirent_p) { dnp->dn_linklist = dirent_p->de_nextlink; } dirent_p->de_nextlink->de_prevlinkp = dirent_p->de_prevlinkp; *dirent_p->de_prevlinkp = dirent_p->de_nextlink; } devfs_dn_free(dnp); } /* * unlink ourselves from the directory on this plane */ if(parent) /* if not fs root */ { if( (*dirent_p->de_prevp = dirent_p->de_next) )/* yes, assign */ { dirent_p->de_next->de_prevp = dirent_p->de_prevp; } else { parent->dn_typeinfo.Dir.dirlast = dirent_p->de_prevp; } parent->dn_typeinfo.Dir.entrycount--; parent->dn_len -= strlen(dirent_p->de_name) + 8; } DEVFS_DECR_ENTRIES(); FREE(dirent_p, M_DEVFSNAME); return 0; } /*************************************************************** * Free a hierarchy starting at a directory node name * remember that if there are other names pointing to the * dev_node then it may not get freed yet * can handle if there is no dnp * leave the node itself allocated. * * called with DEVFS_LOCK held ***************************************************************/ static void dev_free_hier(devdirent_t * dirent_p) { devnode_t * dnp = dirent_p->de_dnp; if(dnp) { if(dnp->dn_type == DEV_DIR) { while(dnp->dn_typeinfo.Dir.dirlist) { dev_free_hier(dnp->dn_typeinfo.Dir.dirlist); dev_free_name(dnp->dn_typeinfo.Dir.dirlist); } } } } /*************************************************************** * given a dev_node, find the appropriate vnode if one is already * associated, or get a new one and associate it with the dev_node * * called with DEVFS_LOCK held ***************************************************************/ int devfs_dntovn(devnode_t * dnp, struct vnode **vn_pp, __unused struct proc * p) { struct vnode *vn_p; struct vnode ** vnptr; int error = 0; struct vnode_fsparam vfsp; enum vtype vtype = 0; int markroot = 0; int n_minor = DEVFS_CLONE_ALLOC; /* new minor number for clone device */ retry: *vn_pp = NULL; vn_p = dnp->dn_vn; dnp->dn_lflags |= DN_BUSY; if (vn_p) { /* already has a vnode */ uint32_t vid; vid = vnode_vid(vn_p); DEVFS_UNLOCK(); error = vnode_getwithvid(vn_p, vid); DEVFS_LOCK(); if (dnp->dn_lflags & DN_DELETE) { /* * our BUSY node got marked for * deletion while the DEVFS lock * was dropped... */ if (error == 0) { /* * vnode_getwithvid returned a valid ref * which we need to drop */ vnode_put(vn_p); } /* * set the error to EAGAIN * which will cause devfs_lookup * to retry this node */ error = EAGAIN; } if ( !error) *vn_pp = vn_p; devfs_release_busy(dnp); return error; } if (dnp->dn_lflags & DN_CREATE) { dnp->dn_lflags |= DN_CREATEWAIT; msleep(&dnp->dn_lflags, &devfs_mutex, PRIBIO, 0 , 0); goto retry; } dnp->dn_lflags |= DN_CREATE; switch (dnp->dn_type) { case DEV_SLNK: vtype = VLNK; break; case DEV_DIR: if (dnp->dn_typeinfo.Dir.parent == dnp) { markroot = 1; } vtype = VDIR; break; case DEV_BDEV: case DEV_CDEV: vtype = (dnp->dn_type == DEV_BDEV) ? VBLK : VCHR; break; } vfsp.vnfs_mp = dnp->dn_dvm->mount; vfsp.vnfs_vtype = vtype; vfsp.vnfs_str = "devfs"; vfsp.vnfs_dvp = 0; vfsp.vnfs_fsnode = dnp; vfsp.vnfs_cnp = 0; vfsp.vnfs_vops = *(dnp->dn_ops); if (vtype == VBLK || vtype == VCHR) { /* * Ask the clone minor number function for a new minor number * to use for the next device instance. If an administative * limit has been reached, this function will return -1. */ if (dnp->dn_clone != NULL) { int n_major = major(dnp->dn_typeinfo.dev); n_minor = (*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_ALLOC); if (n_minor == -1) { devfs_release_busy(dnp); return ENOMEM; } vfsp.vnfs_rdev = makedev(n_major, n_minor);; } else { vfsp.vnfs_rdev = dnp->dn_typeinfo.dev; } } else { vfsp.vnfs_rdev = 0; } vfsp.vnfs_filesize = 0; vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE; /* Tag system files */ vfsp.vnfs_marksystem = 0; vfsp.vnfs_markroot = markroot; DEVFS_UNLOCK(); if (dnp->dn_clone == NULL) vnptr = &dnp->dn_vn; else vnptr = &vn_p; error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, vnptr); DEVFS_LOCK(); if (error == 0) { if ((dnp->dn_clone != NULL) && (dnp->dn_vn != NULLVP) ) panic("devnode already has a vnode?"); /* * Don't cache the vnode for the next open, if the * device is a cloning device (each open gets it's * own per-device instance vnode). */ if (dnp->dn_clone == NULL) { *vn_pp = dnp->dn_vn; } else { *vn_pp = vn_p; } } else if (n_minor != DEVFS_CLONE_ALLOC) { /* * If we failed the create, we need to release the cloned minor * back to the free list. In general, this is only useful if * the clone function results in a state change in the cloned * device for which the minor number was obtained. If we get * past this point withouth falling into this case, it's * assumed that any state to be released will be released when * the vnode is dropped, instead. */ (void)(*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_FREE); } dnp->dn_lflags &= ~DN_CREATE; if (dnp->dn_lflags & DN_CREATEWAIT) { dnp->dn_lflags &= ~DN_CREATEWAIT; wakeup(&dnp->dn_lflags); } devfs_release_busy(dnp); return error; } /*********************************************************************** * called with DEVFS_LOCK held ***********************************************************************/ static void devfs_release_busy(devnode_t *dnp) { dnp->dn_lflags &= ~DN_BUSY; if (dnp->dn_lflags & DN_DELETE) devnode_free(dnp); } /*********************************************************************** * add a whole device, with no prototype.. make name element and node * Used for adding the original device entries * * called with DEVFS_LOCK held ***********************************************************************/ int dev_add_entry(const char *name, devnode_t * parent, int type, devnode_type_t * typeinfo, devnode_t * proto, struct devfsmount *dvm, devdirent_t * *nm_pp) { devnode_t * dnp; int error = 0; if ((error = dev_add_node(type, typeinfo, proto, &dnp, (parent?parent->dn_dvm:dvm))) != 0) { printf("devfs: %s: base node allocation failed (Errno=%d)\n", name,error); return error; } if ((error = dev_add_name(name ,parent ,NULL, dnp, nm_pp)) != 0) { devfs_dn_free(dnp); /* 1->0 for dir, 0->(-1) for other */ printf("devfs: %s: name slot allocation failed (Errno=%d)\n", name,error); } return error; } /* * Function: devfs_make_node * * Purpose * Create a device node with the given pathname in the devfs namespace. * * Parameters: * dev - the dev_t value to associate * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK) * uid, gid - ownership * perms - permissions * clone - minor number cloning function * fmt, ... - path format string with printf args to format the path name * Returns: * A handle to a device node if successful, NULL otherwise. */ void * devfs_make_node_clone(dev_t dev, int chrblk, uid_t uid, gid_t gid, int perms, int (*clone)(dev_t dev, int action), const char *fmt, ...) { devdirent_t * new_dev = NULL; devnode_t * dnp; /* devnode for parent directory */ devnode_type_t typeinfo; char *name, buf[256]; /* XXX */ const char *path; int i; va_list ap; DEVFS_LOCK(); if (!devfs_ready) { printf("devfs_make_node: not ready for devices!\n"); goto out; } if (chrblk != DEVFS_CHAR && chrblk != DEVFS_BLOCK) goto out; DEVFS_UNLOCK(); va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); name = NULL; for(i=strlen(buf); i>0; i--) if(buf[i] == '/') { name=&buf[i]; buf[i]=0; break; } if (name) { *name++ = '\0'; path = buf; } else { name = buf; path = "/"; } DEVFS_LOCK(); /* find/create directory path ie. mkdir -p */ if (dev_finddir(path, NULL, DEVFS_CREATE, &dnp) == 0) { typeinfo.dev = dev; if (dev_add_entry(name, dnp, (chrblk == DEVFS_CHAR) ? DEV_CDEV : DEV_BDEV, &typeinfo, NULL, NULL, &new_dev) == 0) { new_dev->de_dnp->dn_gid = gid; new_dev->de_dnp->dn_uid = uid; new_dev->de_dnp->dn_mode |= perms; new_dev->de_dnp->dn_clone = clone; devfs_propogate(dnp->dn_typeinfo.Dir.myname, new_dev); } } out: DEVFS_UNLOCK(); return new_dev; } /* * Function: devfs_make_node * * Purpose * Create a device node with the given pathname in the devfs namespace. * * Parameters: * dev - the dev_t value to associate * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK) * uid, gid - ownership * perms - permissions * fmt, ... - path format string with printf args to format the path name * Returns: * A handle to a device node if successful, NULL otherwise. */ void * devfs_make_node(dev_t dev, int chrblk, uid_t uid, gid_t gid, int perms, const char *fmt, ...) { devdirent_t * new_dev = NULL; devnode_t * dnp; /* devnode for parent directory */ devnode_type_t typeinfo; char *name, buf[256]; /* XXX */ const char *path; #if CONFIG_MACF char buff[sizeof(buf)]; #endif int i; va_list ap; DEVFS_LOCK(); if (!devfs_ready) { printf("devfs_make_node: not ready for devices!\n"); goto out; } if (chrblk != DEVFS_CHAR && chrblk != DEVFS_BLOCK) goto out; DEVFS_UNLOCK(); va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); #if CONFIG_MACF bcopy(buf, buff, sizeof(buff)); buff[sizeof(buff)-1] = 0; #endif name = NULL; for(i=strlen(buf); i>0; i--) if(buf[i] == '/') { name=&buf[i]; buf[i]=0; break; } if (name) { *name++ = '\0'; path = buf; } else { name = buf; path = "/"; } DEVFS_LOCK(); /* find/create directory path ie. mkdir -p */ if (dev_finddir(path, NULL, DEVFS_CREATE, &dnp) == 0) { typeinfo.dev = dev; if (dev_add_entry(name, dnp, (chrblk == DEVFS_CHAR) ? DEV_CDEV : DEV_BDEV, &typeinfo, NULL, NULL, &new_dev) == 0) { new_dev->de_dnp->dn_gid = gid; new_dev->de_dnp->dn_uid = uid; new_dev->de_dnp->dn_mode |= perms; new_dev->de_dnp->dn_clone = NULL; #if CONFIG_MACF mac_devfs_label_associate_device(dev, new_dev->de_dnp, buff); #endif devfs_propogate(dnp->dn_typeinfo.Dir.myname, new_dev); } } out: DEVFS_UNLOCK(); return new_dev; } /* * Function: devfs_make_link * * Purpose: * Create a link to a previously created device node. * * Returns: * 0 if successful, -1 if failed */ int devfs_make_link(void *original, char *fmt, ...) { devdirent_t * new_dev = NULL; devdirent_t * orig = (devdirent_t *) original; devnode_t * dirnode; /* devnode for parent directory */ va_list ap; char *p, buf[256]; /* XXX */ int i; DEVFS_LOCK(); if (!devfs_ready) { printf("devfs_make_link: not ready for devices!\n"); goto out; } DEVFS_UNLOCK(); va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); p = NULL; for(i=strlen(buf); i>0; i--) { if(buf[i] == '/') { p=&buf[i]; buf[i]=0; break; } } DEVFS_LOCK(); if (p) { *p++ = '\0'; if (dev_finddir(buf, NULL, DEVFS_CREATE, &dirnode) || dev_add_name(p, dirnode, NULL, orig->de_dnp, &new_dev)) goto fail; } else { if (dev_finddir("", NULL, DEVFS_CREATE, &dirnode) || dev_add_name(buf, dirnode, NULL, orig->de_dnp, &new_dev)) goto fail; } devfs_propogate(dirnode->dn_typeinfo.Dir.myname, new_dev); fail: out: DEVFS_UNLOCK(); return ((new_dev != NULL) ? 0 : -1); } |