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