]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/devfs/devfs_tree.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / miscfs / devfs / devfs_tree.c
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("root", NULL, DEV_DIR, NULL, NULL, NULL, &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("/", (int) strlen("/"),
216 dev_root->de_dnp, "/");
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(name, newfp->de_name, 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(path) > (DEVMAXPATHSIZE - 1)) {
296 return ENAMETOOLONG;
297 }
298
299 #if CONFIG_MACF
300 strlcpy(fullpath, 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(component, start, (scan - start) + 1);
326 if (*scan == '/') {
327 scan++;
328 }
329
330 dirent_p = dev_findname(dirnode, 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(component, dirnode,
343 DEV_DIR, NULL, NULL, NULL, &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 dirnode->dn_typeinfo.Dir.myname->de_name,
351 (int) strlen(dirnode->dn_typeinfo.Dir.myname->de_name),
352 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(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(name) > (DEVMAXNAMESIZE - 1))) {
391 return ENAMETOOLONG;
392 }
393
394 /*
395 * Allocate and fill out a new directory entry
396 */
397 MALLOC(dirent_p, devdirent_t *, sizeof(devdirent_t),
398 M_DEVFSNAME, M_WAITOK);
399 if (!dirent_p) {
400 return ENOMEM;
401 }
402 bzero(dirent_p, sizeof(devdirent_t));
403
404 /* inherrit our parent's mount info */ /*XXX*/
405 /* a kludge but.... */
406 if (dirnode && (dnp->dn_dvm == NULL)) {
407 dnp->dn_dvm = dirnode->dn_dvm;
408 /* if(!dnp->dn_dvm) printf("parent had null dvm "); */
409 }
410
411 /*
412 * Link the two together
413 * include the implicit link in the count of links to the devnode..
414 * this stops it from being accidentally freed later.
415 */
416 dirent_p->de_dnp = dnp;
417 dnp->dn_links++; /* implicit from our own name-node */
418
419 /*
420 * Make sure that we can find all the links that reference a node
421 * so that we can get them all if we need to zap the node.
422 */
423 if (dnp->dn_linklist) {
424 dirent_p->de_nextlink = dnp->dn_linklist;
425 dirent_p->de_prevlinkp = dirent_p->de_nextlink->de_prevlinkp;
426 dirent_p->de_nextlink->de_prevlinkp = &(dirent_p->de_nextlink);
427 *dirent_p->de_prevlinkp = dirent_p;
428 } else {
429 dirent_p->de_nextlink = dirent_p;
430 dirent_p->de_prevlinkp = &(dirent_p->de_nextlink);
431 }
432 dnp->dn_linklist = dirent_p;
433
434 /*
435 * If the node is a directory, then we need to handle the
436 * creation of the .. link.
437 * A NULL dirnode indicates a root node, so point to ourself.
438 */
439 if (dnp->dn_type == DEV_DIR) {
440 dnp->dn_typeinfo.Dir.myname = dirent_p;
441 /*
442 * If we are unlinking from an old dir, decrement its links
443 * as we point our '..' elsewhere
444 * Note: it's up to the calling code to remove the
445 * us from the original directory's list
446 */
447 if (dnp->dn_typeinfo.Dir.parent) {
448 dnp->dn_typeinfo.Dir.parent->dn_links--;
449 }
450 if (dirnode) {
451 dnp->dn_typeinfo.Dir.parent = dirnode;
452 } else {
453 dnp->dn_typeinfo.Dir.parent = dnp;
454 }
455 dnp->dn_typeinfo.Dir.parent->dn_links++; /* account for the new '..' */
456 }
457
458 /*
459 * put the name into the directory entry.
460 */
461 strlcpy(dirent_p->de_name, name, DEVMAXNAMESIZE);
462
463
464 /*
465 * Check if we are not making a root node..
466 * (i.e. have parent)
467 */
468 if (dirnode) {
469 /*
470 * Put it on the END of the linked list of directory entries
471 */
472 dirent_p->de_parent = dirnode; /* null for root */
473 dirent_p->de_prevp = dirnode->dn_typeinfo.Dir.dirlast;
474 dirent_p->de_next = *(dirent_p->de_prevp); /* should be NULL */
475 /*right?*/
476 *(dirent_p->de_prevp) = dirent_p;
477 dirnode->dn_typeinfo.Dir.dirlast = &(dirent_p->de_next);
478 dirnode->dn_typeinfo.Dir.entrycount++;
479 dirnode->dn_len += strlen(name) + 8;/*ok, ok?*/
480 }
481
482 *dirent_pp = dirent_p;
483 DEVFS_INCR_ENTRIES();
484 return 0;
485 }
486
487
488 /***********************************************************************
489 * Add a new element to the devfs plane.
490 *
491 * Creates a new dev_node to go with it if the prototype should not be
492 * reused. (Is a DIR, or we select SPLIT_DEVS at compile time)
493 * typeinfo gives us info to make our node if we don't have a prototype.
494 * If typeinfo is null and proto exists, then the typeinfo field of
495 * the proto is used intead in the DEVFS_CREATE case.
496 * note the 'links' count is 0 (except if a dir)
497 * but it is only cleared on a transition
498 * so this is ok till we link it to something
499 * Even in SPLIT_DEVS mode,
500 * if the node already exists on the wanted plane, just return it
501 *
502 * called with DEVFS_LOCK held
503 ***********************************************************************/
504 int
505 dev_add_node(int entrytype, devnode_type_t * typeinfo, devnode_t * proto,
506 devnode_t * *dn_pp, struct devfsmount *dvm)
507 {
508 devnode_t * dnp = NULL;
509 int error = 0;
510
511 #if defined SPLIT_DEVS
512 /*
513 * If we have a prototype, then check if there is already a sibling
514 * on the mount plane we are looking at, if so, just return it.
515 */
516 if (proto) {
517 dnp = proto->dn_nextsibling;
518 while (dnp != proto) {
519 if (dnp->dn_dvm == dvm) {
520 *dn_pp = dnp;
521 return 0;
522 }
523 dnp = dnp->dn_nextsibling;
524 }
525 if (typeinfo == NULL) {
526 typeinfo = &(proto->dn_typeinfo);
527 }
528 }
529 #else /* SPLIT_DEVS */
530 if (proto) {
531 switch (proto->type) {
532 case DEV_BDEV:
533 case DEV_CDEV:
534 *dn_pp = proto;
535 return 0;
536 }
537 }
538 #endif /* SPLIT_DEVS */
539 MALLOC(dnp, devnode_t *, sizeof(devnode_t), M_DEVFSNODE, M_WAITOK);
540 if (!dnp) {
541 return ENOMEM;
542 }
543
544 /*
545 * If we have a proto, that means that we are duplicating some
546 * other device, which can only happen if we are not at the back plane
547 */
548 if (proto) {
549 bcopy(proto, dnp, sizeof(devnode_t));
550 dnp->dn_links = 0;
551 dnp->dn_linklist = NULL;
552 dnp->dn_vn = NULL;
553 dnp->dn_len = 0;
554 /* add to END of siblings list */
555 dnp->dn_prevsiblingp = proto->dn_prevsiblingp;
556 *(dnp->dn_prevsiblingp) = dnp;
557 dnp->dn_nextsibling = proto;
558 proto->dn_prevsiblingp = &(dnp->dn_nextsibling);
559 #if CONFIG_MACF
560 mac_devfs_label_init(dnp);
561 mac_devfs_label_copy(proto->dn_label, dnp->dn_label);
562 #endif
563 } else {
564 struct timeval tv;
565
566 /*
567 * We have no prototype, so start off with a clean slate
568 */
569 microtime(&tv);
570 bzero(dnp, sizeof(devnode_t));
571 dnp->dn_type = entrytype;
572 dnp->dn_nextsibling = dnp;
573 dnp->dn_prevsiblingp = &(dnp->dn_nextsibling);
574 dnp->dn_atime.tv_sec = tv.tv_sec;
575 dnp->dn_mtime.tv_sec = tv.tv_sec;
576 dnp->dn_ctime.tv_sec = tv.tv_sec;
577 #if CONFIG_MACF
578 mac_devfs_label_init(dnp);
579 #endif
580 }
581 dnp->dn_dvm = dvm;
582
583 /* Note: this inits the reference count to 1, this is considered unreferenced */
584 os_ref_init_raw(&dnp->dn_refcount, &devfs_refgrp);
585 dnp->dn_ino = devfs_unique_fileno;
586 devfs_unique_fileno++;
587
588 /*
589 * fill out the dev node according to type
590 */
591 switch (entrytype) {
592 case DEV_DIR:
593 /*
594 * As it's a directory, make sure
595 * it has a null entries list
596 */
597 dnp->dn_typeinfo.Dir.dirlast = &(dnp->dn_typeinfo.Dir.dirlist);
598 dnp->dn_typeinfo.Dir.dirlist = (devdirent_t *)0;
599 dnp->dn_typeinfo.Dir.entrycount = 0;
600 /* until we know better, it has a null parent pointer*/
601 dnp->dn_typeinfo.Dir.parent = NULL;
602 dnp->dn_links++; /* for .*/
603 dnp->dn_typeinfo.Dir.myname = NULL;
604 /*
605 * make sure that the ops associated with it are the ops
606 * that we use (by default) for directories
607 */
608 dnp->dn_ops = &devfs_vnodeop_p;
609 dnp->dn_mode |= 0555; /* default perms */
610 break;
611 case DEV_SLNK:
612 /*
613 * As it's a symlink allocate and store the link info
614 * Symlinks should only ever be created by the user,
615 * so they are not on the back plane and should not be
616 * propogated forward.. a bit like directories in that way..
617 * A symlink only exists on one plane and has its own
618 * node.. therefore we might be on any random plane.
619 */
620 MALLOC(dnp->dn_typeinfo.Slnk.name, char *,
621 typeinfo->Slnk.namelen + 1,
622 M_DEVFSNODE, M_WAITOK);
623 if (!dnp->dn_typeinfo.Slnk.name) {
624 error = ENOMEM;
625 break;
626 }
627 strlcpy(dnp->dn_typeinfo.Slnk.name, typeinfo->Slnk.name,
628 typeinfo->Slnk.namelen + 1);
629 dnp->dn_typeinfo.Slnk.namelen = typeinfo->Slnk.namelen;
630 DEVFS_INCR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1);
631 dnp->dn_ops = &devfs_vnodeop_p;
632 dnp->dn_mode |= 0555; /* default perms */
633 break;
634 case DEV_CDEV:
635 case DEV_BDEV:
636 /*
637 * Make sure it has DEVICE type ops
638 * and device specific fields are correct
639 */
640 dnp->dn_ops = &devfs_spec_vnodeop_p;
641 dnp->dn_typeinfo.dev = typeinfo->dev;
642 break;
643
644 #if FDESC
645 /* /dev/fd is special */
646 case DEV_DEVFD:
647 dnp->dn_ops = &devfs_devfd_vnodeop_p;
648 dnp->dn_mode |= 0555; /* default perms */
649 break;
650
651 #endif /* FDESC */
652 default:
653 error = EINVAL;
654 }
655
656 if (error) {
657 FREE(dnp, M_DEVFSNODE);
658 } else {
659 *dn_pp = dnp;
660 DEVFS_INCR_NODES();
661 }
662
663 return error;
664 }
665
666
667 /***********************************************************************
668 * called with DEVFS_LOCK held
669 **********************************************************************/
670 void
671 devnode_free(devnode_t * dnp)
672 {
673 #if CONFIG_MACF
674 mac_devfs_label_destroy(dnp);
675 #endif
676 if (dnp->dn_type == DEV_SLNK) {
677 DEVFS_DECR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1);
678 FREE(dnp->dn_typeinfo.Slnk.name, M_DEVFSNODE);
679 }
680 DEVFS_DECR_NODES();
681 FREE(dnp, M_DEVFSNODE);
682 }
683
684
685 /***********************************************************************
686 * called with DEVFS_LOCK held
687 **********************************************************************/
688 static void
689 devfs_dn_free(devnode_t * dnp)
690 {
691 if (--dnp->dn_links <= 0) { /* can be -1 for initial free, on error */
692 /*probably need to do other cleanups XXX */
693 if (dnp->dn_nextsibling != dnp) {
694 devnode_t * * prevp = dnp->dn_prevsiblingp;
695 *prevp = dnp->dn_nextsibling;
696 dnp->dn_nextsibling->dn_prevsiblingp = prevp;
697 }
698
699 /* Can only free if there are no references; otherwise, wait for last vnode to be reclaimed */
700 os_ref_count_t rc = os_ref_get_count_raw(&dnp->dn_refcount);
701 if (rc == 1) {
702 /* release final reference from dev_add_node */
703 (void) os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
704 devnode_free(dnp);
705 } else {
706 dnp->dn_lflags |= DN_DELETE;
707 }
708 }
709 }
710
711 /***********************************************************************\
712 * Front Node Operations *
713 * Add or delete a chain of front nodes *
714 \***********************************************************************/
715
716
717 /***********************************************************************
718 * Given a directory backing node, and a child backing node, add the
719 * appropriate front nodes to the front nodes of the directory to
720 * represent the child node to the user
721 *
722 * on failure, front nodes will either be correct or not exist for each
723 * front dir, however dirs completed will not be stripped of completed
724 * frontnodes on failure of a later frontnode
725 *
726 * This allows a new node to be propogated through all mounted planes
727 *
728 * called with DEVFS_LOCK held
729 ***********************************************************************/
730 static int
731 devfs_propogate(devdirent_t * parent, devdirent_t * child, devfs_event_log_t delp)
732 {
733 int error;
734 devdirent_t * newnmp;
735 devnode_t * dnp = child->de_dnp;
736 devnode_t * pdnp = parent->de_dnp;
737 devnode_t * adnp = parent->de_dnp;
738 int type = child->de_dnp->dn_type;
739 uint32_t events;
740
741 events = (dnp->dn_type == DEV_DIR ? VNODE_EVENT_DIR_CREATED : VNODE_EVENT_FILE_CREATED);
742 if (delp != NULL) {
743 devfs_record_event(delp, pdnp, events);
744 }
745
746 /***********************************************
747 * Find the other instances of the parent node
748 ***********************************************/
749 for (adnp = pdnp->dn_nextsibling;
750 adnp != pdnp;
751 adnp = adnp->dn_nextsibling) {
752 /*
753 * Make the node, using the original as a prototype)
754 * if the node already exists on that plane it won't be
755 * re-made..
756 */
757 if ((error = dev_add_entry(child->de_name, adnp, type,
758 NULL, dnp, adnp->dn_dvm,
759 &newnmp)) != 0) {
760 printf("duplicating %s failed\n", child->de_name);
761 } else {
762 if (delp != NULL) {
763 devfs_record_event(delp, adnp, events);
764
765 /*
766 * Slightly subtle. We're guaranteed that there will
767 * only be a vnode hooked into this devnode if we're creating
768 * a new link to an existing node; otherwise, the devnode is new
769 * and no one can have looked it up yet. If we're making a link,
770 * then the buffer is large enough for two nodes in each
771 * plane; otherwise, there's no vnode and this call will
772 * do nothing.
773 */
774 devfs_record_event(delp, newnmp->de_dnp, VNODE_EVENT_LINK);
775 }
776 }
777 }
778 return 0; /* for now always succeed */
779 }
780
781 static uint32_t
782 remove_notify_count(devnode_t *dnp)
783 {
784 uint32_t notify_count = 0;
785 devnode_t *dnp2;
786
787 /*
788 * Could need to notify for one removed node on each mount and
789 * one parent for each such node.
790 */
791 notify_count = devfs_nmountplanes;
792 notify_count += dnp->dn_links;
793 for (dnp2 = dnp->dn_nextsibling; dnp2 != dnp; dnp2 = dnp2->dn_nextsibling) {
794 notify_count += dnp2->dn_links;
795 }
796
797 return notify_count;
798 }
799
800 /***********************************************************************
801 * remove all instances of this devicename [for backing nodes..]
802 * note.. if there is another link to the node (non dir nodes only)
803 * then the devfs_node will still exist as the ref count will be non-0
804 * removing a directory node will remove all sup-nodes on all planes (ZAP)
805 *
806 * Used by device drivers to remove nodes that are no longer relevant
807 * The argument is the 'cookie' they were given when they created the node
808 * this function is exported.. see devfs.h
809 ***********************************************************************/
810 void
811 devfs_remove(void *dirent_p)
812 {
813 devnode_t * dnp = ((devdirent_t *)dirent_p)->de_dnp;
814 devnode_t * dnp2;
815 boolean_t lastlink;
816 struct devfs_event_log event_log;
817 uint32_t log_count = 0;
818 int do_notify = 0;
819 int need_free = 0;
820 struct devfs_vnode_event stackbuf[NUM_STACK_ENTRIES];
821
822 DEVFS_LOCK();
823
824 if (!devfs_ready) {
825 printf("devfs_remove: not ready for devices!\n");
826 goto out;
827 }
828
829 log_count = remove_notify_count(dnp);
830
831 if (log_count > NUM_STACK_ENTRIES) {
832 uint32_t new_count;
833 wrongsize:
834 DEVFS_UNLOCK();
835 if (devfs_init_event_log(&event_log, log_count, NULL) == 0) {
836 do_notify = 1;
837 need_free = 1;
838 }
839 DEVFS_LOCK();
840
841 new_count = remove_notify_count(dnp);
842 if (need_free && (new_count > log_count)) {
843 devfs_release_event_log(&event_log, 1);
844 need_free = 0;
845 do_notify = 0;
846 log_count = log_count * 2;
847 goto wrongsize;
848 }
849 } else {
850 if (devfs_init_event_log(&event_log, NUM_STACK_ENTRIES, &stackbuf[0]) == 0) {
851 do_notify = 1;
852 }
853 }
854
855 /* This file has been deleted */
856 if (do_notify != 0) {
857 devfs_record_event(&event_log, dnp, VNODE_EVENT_DELETE);
858 }
859
860 /* keep removing the next sibling till only we exist. */
861 while ((dnp2 = dnp->dn_nextsibling) != dnp) {
862 /*
863 * Keep removing the next front node till no more exist
864 */
865 dnp->dn_nextsibling = dnp2->dn_nextsibling;
866 dnp->dn_nextsibling->dn_prevsiblingp = &(dnp->dn_nextsibling);
867 dnp2->dn_nextsibling = dnp2;
868 dnp2->dn_prevsiblingp = &(dnp2->dn_nextsibling);
869
870 /* This file has been deleted in this plane */
871 if (do_notify != 0) {
872 devfs_record_event(&event_log, dnp2, VNODE_EVENT_DELETE);
873 }
874
875 if (dnp2->dn_linklist) {
876 do {
877 lastlink = (1 == dnp2->dn_links);
878 /* Each parent of a link to this file has lost a child in this plane */
879 if (do_notify != 0) {
880 devfs_record_event(&event_log, dnp2->dn_linklist->de_parent, VNODE_EVENT_FILE_REMOVED);
881 }
882 dev_free_name(dnp2->dn_linklist);
883 } while (!lastlink);
884 }
885 }
886
887 /*
888 * then free the main node
889 * If we are not running in SPLIT_DEVS mode, then
890 * THIS is what gets rid of the propogated nodes.
891 */
892 if (dnp->dn_linklist) {
893 do {
894 lastlink = (1 == dnp->dn_links);
895 /* Each parent of a link to this file has lost a child */
896 if (do_notify != 0) {
897 devfs_record_event(&event_log, dnp->dn_linklist->de_parent, VNODE_EVENT_FILE_REMOVED);
898 }
899 dev_free_name(dnp->dn_linklist);
900 } while (!lastlink);
901 }
902 out:
903 DEVFS_UNLOCK();
904 if (do_notify != 0) {
905 devfs_bulk_notify(&event_log);
906 devfs_release_event_log(&event_log, need_free);
907 }
908
909 return;
910 }
911
912
913
914 /***************************************************************
915 * duplicate the backing tree into a tree of nodes hung off the
916 * mount point given as the argument. Do this by
917 * calling dev_dup_entry which recurses all the way
918 * up the tree..
919 *
920 * called with DEVFS_LOCK held
921 **************************************************************/
922 int
923 dev_dup_plane(struct devfsmount *devfs_mp_p)
924 {
925 devdirent_t * new;
926 int error = 0;
927
928 if ((error = dev_dup_entry(NULL, dev_root, &new, devfs_mp_p))) {
929 return error;
930 }
931 devfs_mp_p->plane_root = new;
932 devfs_nmountplanes++;
933 return error;
934 }
935
936
937
938 /***************************************************************
939 * Free a whole plane
940 *
941 * called with DEVFS_LOCK held
942 ***************************************************************/
943 void
944 devfs_free_plane(struct devfsmount *devfs_mp_p)
945 {
946 devdirent_t * dirent_p;
947
948 dirent_p = devfs_mp_p->plane_root;
949 if (dirent_p) {
950 dev_free_hier(dirent_p);
951 dev_free_name(dirent_p);
952 }
953 devfs_mp_p->plane_root = NULL;
954 devfs_nmountplanes--;
955
956 if (devfs_nmountplanes > (devfs_nmountplanes + 1)) {
957 panic("plane count wrapped around.\n");
958 }
959 }
960
961
962 /***************************************************************
963 * Create and link in a new front element..
964 * Parent can be 0 for a root node
965 * Not presently usable to make a symlink XXX
966 * (Ok, symlinks don't propogate)
967 * recursively will create subnodes corresponding to equivalent
968 * child nodes in the base level
969 *
970 * called with DEVFS_LOCK held
971 ***************************************************************/
972 static int
973 dev_dup_entry(devnode_t * parent, devdirent_t * back, devdirent_t * *dnm_pp,
974 struct devfsmount *dvm)
975 {
976 devdirent_t * entry_p = NULL;
977 devdirent_t * newback;
978 devdirent_t * newfront;
979 int error;
980 devnode_t * dnp = back->de_dnp;
981 int type = dnp->dn_type;
982
983 /*
984 * go get the node made (if we need to)
985 * use the back one as a prototype
986 */
987 error = dev_add_entry(back->de_name, parent, type, NULL, dnp,
988 parent?parent->dn_dvm:dvm, &entry_p);
989 if (!error && (entry_p == NULL)) {
990 error = ENOMEM; /* Really can't happen, but make static analyzer happy */
991 }
992 if (error != 0) {
993 printf("duplicating %s failed\n", back->de_name);
994 goto out;
995 }
996
997 /*
998 * If we have just made the root, then insert the pointer to the
999 * mount information
1000 */
1001 if (dvm) {
1002 entry_p->de_dnp->dn_dvm = dvm;
1003 }
1004
1005 /*
1006 * If it is a directory, then recurse down all the other
1007 * subnodes in it....
1008 * note that this time we don't pass on the mount info..
1009 */
1010 if (type == DEV_DIR) {
1011 for (newback = back->de_dnp->dn_typeinfo.Dir.dirlist;
1012 newback; newback = newback->de_next) {
1013 if ((error = dev_dup_entry(entry_p->de_dnp,
1014 newback, &newfront, NULL)) != 0) {
1015 break; /* back out with an error */
1016 }
1017 }
1018 }
1019 out:
1020 *dnm_pp = entry_p;
1021 return error;
1022 }
1023
1024
1025 /***************************************************************
1026 * Free a name node
1027 * remember that if there are other names pointing to the
1028 * dev_node then it may not get freed yet
1029 * can handle if there is no dnp
1030 *
1031 * called with DEVFS_LOCK held
1032 ***************************************************************/
1033
1034 int
1035 dev_free_name(devdirent_t * dirent_p)
1036 {
1037 devnode_t * parent = dirent_p->de_parent;
1038 devnode_t * dnp = dirent_p->de_dnp;
1039
1040 if (dnp) {
1041 if (dnp->dn_type == DEV_DIR) {
1042 devnode_t * p;
1043
1044 if (dnp->dn_typeinfo.Dir.dirlist) {
1045 return ENOTEMPTY;
1046 }
1047 p = dnp->dn_typeinfo.Dir.parent;
1048 devfs_dn_free(dnp); /* account for '.' */
1049 devfs_dn_free(p); /* '..' */
1050 }
1051 /*
1052 * unlink us from the list of links for this node
1053 * If we are the only link, it's easy!
1054 * if we are a DIR of course there should not be any
1055 * other links.
1056 */
1057 if (dirent_p->de_nextlink == dirent_p) {
1058 dnp->dn_linklist = NULL;
1059 } else {
1060 if (dnp->dn_linklist == dirent_p) {
1061 dnp->dn_linklist = dirent_p->de_nextlink;
1062 }
1063 }
1064 devfs_dn_free(dnp);
1065 }
1066
1067 dirent_p->de_nextlink->de_prevlinkp = dirent_p->de_prevlinkp;
1068 *(dirent_p->de_prevlinkp) = dirent_p->de_nextlink;
1069
1070 /*
1071 * unlink ourselves from the directory on this plane
1072 */
1073 if (parent) { /* if not fs root */
1074 if ((*dirent_p->de_prevp = dirent_p->de_next)) {/* yes, assign */
1075 dirent_p->de_next->de_prevp = dirent_p->de_prevp;
1076 } else {
1077 parent->dn_typeinfo.Dir.dirlast
1078 = dirent_p->de_prevp;
1079 }
1080 parent->dn_typeinfo.Dir.entrycount--;
1081 parent->dn_len -= strlen(dirent_p->de_name) + 8;
1082 }
1083
1084 DEVFS_DECR_ENTRIES();
1085 FREE(dirent_p, M_DEVFSNAME);
1086 return 0;
1087 }
1088
1089
1090 /***************************************************************
1091 * Free a hierarchy starting at a directory node name
1092 * remember that if there are other names pointing to the
1093 * dev_node then it may not get freed yet
1094 * can handle if there is no dnp
1095 * leave the node itself allocated.
1096 *
1097 * called with DEVFS_LOCK held
1098 ***************************************************************/
1099
1100 static void
1101 dev_free_hier(devdirent_t * dirent_p)
1102 {
1103 devnode_t * dnp = dirent_p->de_dnp;
1104
1105 if (dnp) {
1106 if (dnp->dn_type == DEV_DIR) {
1107 while (dnp->dn_typeinfo.Dir.dirlist) {
1108 dev_free_hier(dnp->dn_typeinfo.Dir.dirlist);
1109 dev_free_name(dnp->dn_typeinfo.Dir.dirlist);
1110 }
1111 }
1112 }
1113 }
1114
1115
1116 /***************************************************************
1117 * given a dev_node, find the appropriate vnode if one is already
1118 * associated, or get a new one and associate it with the dev_node
1119 *
1120 * called with DEVFS_LOCK held
1121 *
1122 * If an error is returned, then the dnp may have been freed (we
1123 * raced with a delete and lost). A devnode should not be accessed
1124 * after devfs_dntovn() fails.
1125 ****************************************************************/
1126 int
1127 devfs_dntovn(devnode_t * dnp, struct vnode **vn_pp, __unused struct proc * p)
1128 {
1129 struct vnode *vn_p;
1130 int error = 0;
1131 struct vnode_fsparam vfsp;
1132 enum vtype vtype = 0;
1133 int markroot = 0;
1134 int nretries = 0;
1135 int n_minor = DEVFS_CLONE_ALLOC; /* new minor number for clone device */
1136
1137 /*
1138 * We should never come in and find that our devnode has been marked for delete.
1139 * The lookup should have held the lock from entry until now; it should not have
1140 * been able to find a removed entry. Any other pathway would have just created
1141 * the devnode and come here without dropping the devfs lock, so no one would
1142 * have a chance to delete.
1143 */
1144 if (dnp->dn_lflags & DN_DELETE) {
1145 panic("devfs_dntovn: DN_DELETE set on a devnode upon entry.");
1146 }
1147
1148 devfs_ref_node(dnp);
1149
1150 retry:
1151 *vn_pp = NULL;
1152 vn_p = dnp->dn_vn;
1153
1154 if (vn_p) { /* already has a vnode */
1155 uint32_t vid;
1156
1157 vid = vnode_vid(vn_p);
1158
1159 DEVFS_UNLOCK();
1160
1161 /*
1162 * We want to use the drainok variant of vnode_getwithvid
1163 * because we _don't_ want to get an iocount if the vnode is
1164 * is blocked in vnode_drain as it can cause infinite
1165 * loops in vn_open_auth. While in use vnodes are typically
1166 * only reclaimed on forced unmounts, In use devfs tty vnodes
1167 * can be quite frequently reclaimed by revoke(2) or by the
1168 * exit of a controlling process.
1169 */
1170 error = vnode_getwithvid_drainok(vn_p, vid);
1171
1172 DEVFS_LOCK();
1173
1174 if (dnp->dn_lflags & DN_DELETE) {
1175 /*
1176 * our BUSY node got marked for
1177 * deletion while the DEVFS lock
1178 * was dropped...
1179 */
1180 if (error == 0) {
1181 /*
1182 * vnode_getwithvid returned a valid ref
1183 * which we need to drop
1184 */
1185 vnode_put(vn_p);
1186 }
1187
1188 /*
1189 * This entry is no longer in the namespace. This is only
1190 * possible for lookup: no other path would not find an existing
1191 * vnode. Therefore, ENOENT is a valid result.
1192 */
1193 error = ENOENT;
1194 } else if (error == ENODEV) {
1195 /*
1196 * The Filesystem is getting unmounted.
1197 */
1198 error = ENOENT;
1199 } else if (error && (nretries < DEV_MAX_VNODE_RETRY)) {
1200 /*
1201 * If we got an error from vnode_getwithvid, it means
1202 * we raced with a recycle and lost i.e. we asked for
1203 * an iocount only after vnode_drain had been entered
1204 * for the vnode and returned with an error only after
1205 * devfs_reclaim was called on the vnode. devfs_reclaim
1206 * sets dn_vn to NULL but while we were waiting to
1207 * reacquire DEVFS_LOCK, another vnode might have gotten
1208 * associated with the dnp. In either case, we need to
1209 * retry otherwise we will end up returning an ENOENT
1210 * for this lookup but the next lookup will succeed
1211 * because it creates a new vnode (or a racing lookup
1212 * created a new vnode already).
1213 */
1214 error = 0;
1215 nretries++;
1216 goto retry;
1217 }
1218 if (!error) {
1219 *vn_pp = vn_p;
1220 }
1221
1222 goto out;
1223 }
1224
1225 /*
1226 * If we get here, then we've beaten any deletes;
1227 * if someone sets DN_DELETE during a subsequent drop
1228 * of the devfs lock, we'll still vend a vnode.
1229 */
1230
1231 if (dnp->dn_lflags & DN_CREATE) {
1232 dnp->dn_lflags |= DN_CREATEWAIT;
1233 msleep(&dnp->dn_lflags, &devfs_mutex, PRIBIO, 0, 0);
1234 goto retry;
1235 }
1236
1237 dnp->dn_lflags |= DN_CREATE;
1238
1239 switch (dnp->dn_type) {
1240 case DEV_SLNK:
1241 vtype = VLNK;
1242 break;
1243 case DEV_DIR:
1244 if (dnp->dn_typeinfo.Dir.parent == dnp) {
1245 markroot = 1;
1246 }
1247 vtype = VDIR;
1248 break;
1249 case DEV_BDEV:
1250 case DEV_CDEV:
1251 vtype = (dnp->dn_type == DEV_BDEV) ? VBLK : VCHR;
1252 break;
1253 #if FDESC
1254 case DEV_DEVFD:
1255 vtype = VDIR;
1256 break;
1257 #endif /* FDESC */
1258 }
1259 vfsp.vnfs_mp = dnp->dn_dvm->mount;
1260 vfsp.vnfs_vtype = vtype;
1261 vfsp.vnfs_str = "devfs";
1262 vfsp.vnfs_dvp = 0;
1263 vfsp.vnfs_fsnode = dnp;
1264 vfsp.vnfs_cnp = 0;
1265 vfsp.vnfs_vops = *(dnp->dn_ops);
1266
1267 if (vtype == VBLK || vtype == VCHR) {
1268 /*
1269 * Ask the clone minor number function for a new minor number
1270 * to use for the next device instance. If an administative
1271 * limit has been reached, this function will return -1.
1272 */
1273 if (dnp->dn_clone != NULL) {
1274 int n_major = major(dnp->dn_typeinfo.dev);
1275
1276 n_minor = (*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_ALLOC);
1277 if (n_minor == -1) {
1278 error = ENOMEM;
1279 goto out;
1280 }
1281
1282 vfsp.vnfs_rdev = makedev(n_major, n_minor);;
1283 } else {
1284 vfsp.vnfs_rdev = dnp->dn_typeinfo.dev;
1285 }
1286 } else {
1287 vfsp.vnfs_rdev = 0;
1288 }
1289 vfsp.vnfs_filesize = 0;
1290 vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
1291 /* Tag system files */
1292 vfsp.vnfs_marksystem = 0;
1293 vfsp.vnfs_markroot = markroot;
1294
1295 DEVFS_UNLOCK();
1296
1297 error = vnode_create(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &vn_p);
1298
1299 /* Do this before grabbing the lock */
1300 if (error == 0) {
1301 vnode_setneedinactive(vn_p);
1302 }
1303
1304 DEVFS_LOCK();
1305
1306 if (error == 0) {
1307 vnode_settag(vn_p, VT_DEVFS);
1308
1309 if ((dnp->dn_clone != NULL) && (dnp->dn_vn != NULLVP)) {
1310 panic("devfs_dntovn: cloning device with a vnode?\n");
1311 }
1312
1313 *vn_pp = vn_p;
1314
1315 /*
1316 * Another vnode that has this devnode as its v_data.
1317 * This reference, unlike the one taken at the start
1318 * of the function, persists until a VNOP_RECLAIM
1319 * comes through for this vnode.
1320 */
1321 devfs_ref_node(dnp);
1322
1323 /*
1324 * A cloned vnode is not hooked into the devnode; every lookup
1325 * gets a new vnode.
1326 */
1327 if (dnp->dn_clone == NULL) {
1328 dnp->dn_vn = vn_p;
1329 }
1330 } else if (n_minor != DEVFS_CLONE_ALLOC) {
1331 /*
1332 * If we failed the create, we need to release the cloned minor
1333 * back to the free list. In general, this is only useful if
1334 * the clone function results in a state change in the cloned
1335 * device for which the minor number was obtained. If we get
1336 * past this point withouth falling into this case, it's
1337 * assumed that any state to be released will be released when
1338 * the vnode is dropped, instead.
1339 */
1340 (void)(*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_FREE);
1341 }
1342
1343 dnp->dn_lflags &= ~DN_CREATE;
1344 if (dnp->dn_lflags & DN_CREATEWAIT) {
1345 dnp->dn_lflags &= ~DN_CREATEWAIT;
1346 wakeup(&dnp->dn_lflags);
1347 }
1348
1349 out:
1350 /*
1351 * Release the reference we took to prevent deletion while we weren't holding the lock.
1352 * If not returning success, then dropping this reference could delete the devnode;
1353 * no one should access a devnode after a call to devfs_dntovn fails.
1354 */
1355 devfs_rele_node(dnp);
1356
1357 return error;
1358 }
1359
1360 /*
1361 * Increment refcount on a devnode; prevents free of the node
1362 * while the devfs lock is not held.
1363 */
1364 void
1365 devfs_ref_node(devnode_t *dnp)
1366 {
1367 os_ref_retain_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1368 }
1369
1370 /*
1371 * Release a reference on a devnode. If the devnode is marked for
1372 * free and the refcount is dropped to one, do the free.
1373 */
1374 void
1375 devfs_rele_node(devnode_t *dnp)
1376 {
1377 os_ref_count_t rc = os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1378 if (rc < 1) {
1379 panic("devfs_rele_node: devnode without a refcount!\n");
1380 } else if ((rc == 1) && (dnp->dn_lflags & DN_DELETE)) {
1381 /* release final reference from dev_add_node */
1382 (void) os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1383 devnode_free(dnp);
1384 }
1385 }
1386
1387 /***********************************************************************
1388 * add a whole device, with no prototype.. make name element and node
1389 * Used for adding the original device entries
1390 *
1391 * called with DEVFS_LOCK held
1392 ***********************************************************************/
1393 int
1394 dev_add_entry(const char *name, devnode_t * parent, int type, devnode_type_t * typeinfo,
1395 devnode_t * proto, struct devfsmount *dvm, devdirent_t * *nm_pp)
1396 {
1397 devnode_t * dnp;
1398 int error = 0;
1399
1400 if ((error = dev_add_node(type, typeinfo, proto, &dnp,
1401 (parent?parent->dn_dvm:dvm))) != 0) {
1402 printf("devfs: %s: base node allocation failed (Errno=%d)\n",
1403 name, error);
1404 return error;
1405 }
1406 if ((error = dev_add_name(name, parent, NULL, dnp, nm_pp)) != 0) {
1407 devfs_dn_free(dnp); /* 1->0 for dir, 0->(-1) for other */
1408 printf("devfs: %s: name slot allocation failed (Errno=%d)\n",
1409 name, error);
1410 }
1411 return error;
1412 }
1413
1414 static void
1415 devfs_bulk_notify(devfs_event_log_t delp)
1416 {
1417 uint32_t i;
1418 for (i = 0; i < delp->del_used; i++) {
1419 devfs_vnode_event_t dvep = &delp->del_entries[i];
1420 if (vnode_getwithvid(dvep->dve_vp, dvep->dve_vid) == 0) {
1421 vnode_notify(dvep->dve_vp, dvep->dve_events, NULL);
1422 vnode_put(dvep->dve_vp);
1423 }
1424 }
1425 }
1426
1427 static void
1428 devfs_record_event(devfs_event_log_t delp, devnode_t *dnp, uint32_t events)
1429 {
1430 if (delp->del_used >= delp->del_max) {
1431 panic("devfs event log overflowed.\n");
1432 }
1433
1434 /* Can only notify for nodes that have an associated vnode */
1435 if (dnp->dn_vn != NULLVP && vnode_ismonitored(dnp->dn_vn)) {
1436 devfs_vnode_event_t dvep = &delp->del_entries[delp->del_used];
1437 dvep->dve_vp = dnp->dn_vn;
1438 dvep->dve_vid = vnode_vid(dnp->dn_vn);
1439 dvep->dve_events = events;
1440 delp->del_used++;
1441 }
1442 }
1443
1444 static int
1445 devfs_init_event_log(devfs_event_log_t delp, uint32_t count, devfs_vnode_event_t buf)
1446 {
1447 devfs_vnode_event_t dvearr;
1448
1449 if (buf == NULL) {
1450 MALLOC(dvearr, devfs_vnode_event_t, count * sizeof(struct devfs_vnode_event), M_TEMP, M_WAITOK | M_ZERO);
1451 if (dvearr == NULL) {
1452 return ENOMEM;
1453 }
1454 } else {
1455 dvearr = buf;
1456 }
1457
1458 delp->del_max = count;
1459 delp->del_used = 0;
1460 delp->del_entries = dvearr;
1461 return 0;
1462 }
1463
1464 static void
1465 devfs_release_event_log(devfs_event_log_t delp, int need_free)
1466 {
1467 if (delp->del_entries == NULL) {
1468 panic("Free of devfs notify info that has not been intialized.\n");
1469 }
1470
1471 if (need_free) {
1472 FREE(delp->del_entries, M_TEMP);
1473 }
1474
1475 delp->del_entries = NULL;
1476 }
1477
1478 /*
1479 * Function: devfs_make_node
1480 *
1481 * Purpose
1482 * Create a device node with the given pathname in the devfs namespace.
1483 *
1484 * Parameters:
1485 * dev - the dev_t value to associate
1486 * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1487 * uid, gid - ownership
1488 * perms - permissions
1489 * clone - minor number cloning function
1490 * fmt, ... - path format string with printf args to format the path name
1491 * Returns:
1492 * A handle to a device node if successful, NULL otherwise.
1493 */
1494 void *
1495 devfs_make_node_clone(dev_t dev, int chrblk, uid_t uid,
1496 gid_t gid, int perms, int (*clone)(dev_t dev, int action),
1497 const char *fmt, ...)
1498 {
1499 devdirent_t * new_dev = NULL;
1500 devfstype_t type;
1501 va_list ap;
1502
1503 switch (chrblk) {
1504 case DEVFS_CHAR:
1505 type = DEV_CDEV;
1506 break;
1507 case DEVFS_BLOCK:
1508 type = DEV_BDEV;
1509 break;
1510 default:
1511 goto out;
1512 }
1513
1514 va_start(ap, fmt);
1515 new_dev = devfs_make_node_internal(dev, type, uid, gid, perms, clone, fmt, ap);
1516 va_end(ap);
1517 out:
1518 return new_dev;
1519 }
1520
1521
1522 /*
1523 * Function: devfs_make_node
1524 *
1525 * Purpose
1526 * Create a device node with the given pathname in the devfs namespace.
1527 *
1528 * Parameters:
1529 * dev - the dev_t value to associate
1530 * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1531 * uid, gid - ownership
1532 * perms - permissions
1533 * fmt, ... - path format string with printf args to format the path name
1534 * Returns:
1535 * A handle to a device node if successful, NULL otherwise.
1536 */
1537 void *
1538 devfs_make_node(dev_t dev, int chrblk, uid_t uid,
1539 gid_t gid, int perms, const char *fmt, ...)
1540 {
1541 devdirent_t * new_dev = NULL;
1542 devfstype_t type;
1543 va_list ap;
1544
1545 if (chrblk != DEVFS_CHAR && chrblk != DEVFS_BLOCK) {
1546 goto out;
1547 }
1548
1549 type = (chrblk == DEVFS_BLOCK ? DEV_BDEV : DEV_CDEV);
1550
1551 va_start(ap, fmt);
1552 new_dev = devfs_make_node_internal(dev, type, uid, gid, perms, NULL, fmt, ap);
1553 va_end(ap);
1554
1555 out:
1556 return new_dev;
1557 }
1558
1559 static devdirent_t *
1560 devfs_make_node_internal(dev_t dev, devfstype_t type, uid_t uid,
1561 gid_t gid, int perms, int (*clone)(dev_t dev, int action), const char *fmt, va_list ap)
1562 {
1563 devdirent_t * new_dev = NULL;
1564 devnode_t * dnp;
1565 devnode_type_t typeinfo;
1566
1567 char *name, buf[256]; /* XXX */
1568 const char *path;
1569 #if CONFIG_MACF
1570 char buff[sizeof(buf)];
1571 #endif
1572 size_t i;
1573 uint32_t log_count;
1574 struct devfs_event_log event_log;
1575 struct devfs_vnode_event stackbuf[NUM_STACK_ENTRIES];
1576 int need_free = 0;
1577
1578 vsnprintf(buf, sizeof(buf), fmt, ap);
1579
1580 #if CONFIG_MACF
1581 bcopy(buf, buff, sizeof(buff));
1582 buff[sizeof(buff) - 1] = 0;
1583 #endif
1584 name = NULL;
1585
1586 for (i = strlen(buf); i > 0; i--) {
1587 if (buf[i] == '/') {
1588 name = &buf[i];
1589 buf[i] = 0;
1590 break;
1591 }
1592 }
1593
1594 if (name) {
1595 *name++ = '\0';
1596 path = buf;
1597 } else {
1598 name = buf;
1599 path = "/";
1600 }
1601
1602 log_count = devfs_nmountplanes;
1603 if (log_count > NUM_STACK_ENTRIES) {
1604 wrongsize:
1605 need_free = 1;
1606 if (devfs_init_event_log(&event_log, log_count, NULL) != 0) {
1607 return NULL;
1608 }
1609 } else {
1610 need_free = 0;
1611 log_count = NUM_STACK_ENTRIES;
1612 if (devfs_init_event_log(&event_log, log_count, &stackbuf[0]) != 0) {
1613 return NULL;
1614 }
1615 }
1616
1617 DEVFS_LOCK();
1618 if (log_count < devfs_nmountplanes) {
1619 DEVFS_UNLOCK();
1620 devfs_release_event_log(&event_log, need_free);
1621 log_count = log_count * 2;
1622 goto wrongsize;
1623 }
1624
1625 if (!devfs_ready) {
1626 printf("devfs_make_node: not ready for devices!\n");
1627 goto out;
1628 }
1629
1630 /* find/create directory path ie. mkdir -p */
1631 if (dev_finddir(path, NULL, DEVFS_CREATE, &dnp, &event_log) == 0) {
1632 typeinfo.dev = dev;
1633 if (dev_add_entry(name, dnp, type, &typeinfo, NULL, NULL, &new_dev) == 0) {
1634 new_dev->de_dnp->dn_gid = gid;
1635 new_dev->de_dnp->dn_uid = uid;
1636 new_dev->de_dnp->dn_mode |= perms;
1637 new_dev->de_dnp->dn_clone = clone;
1638 #if CONFIG_MACF
1639 mac_devfs_label_associate_device(dev, new_dev->de_dnp, buff);
1640 #endif
1641 devfs_propogate(dnp->dn_typeinfo.Dir.myname, new_dev, &event_log);
1642 }
1643 }
1644
1645 out:
1646 DEVFS_UNLOCK();
1647
1648 devfs_bulk_notify(&event_log);
1649 devfs_release_event_log(&event_log, need_free);
1650 return new_dev;
1651 }
1652
1653 /*
1654 * Function: devfs_make_link
1655 *
1656 * Purpose:
1657 * Create a link to a previously created device node.
1658 *
1659 * Returns:
1660 * 0 if successful, -1 if failed
1661 */
1662 int
1663 devfs_make_link(void *original, char *fmt, ...)
1664 {
1665 devdirent_t * new_dev = NULL;
1666 devdirent_t * orig = (devdirent_t *) original;
1667 devnode_t * dirnode; /* devnode for parent directory */
1668 struct devfs_event_log event_log;
1669 uint32_t log_count;
1670
1671 va_list ap;
1672 char *p, buf[256]; /* XXX */
1673 size_t i;
1674
1675 DEVFS_LOCK();
1676
1677 if (!devfs_ready) {
1678 DEVFS_UNLOCK();
1679 printf("devfs_make_link: not ready for devices!\n");
1680 return -1;
1681 }
1682 DEVFS_UNLOCK();
1683
1684 va_start(ap, fmt);
1685 vsnprintf(buf, sizeof(buf), fmt, ap);
1686 va_end(ap);
1687
1688 p = NULL;
1689
1690 for (i = strlen(buf); i > 0; i--) {
1691 if (buf[i] == '/') {
1692 p = &buf[i];
1693 buf[i] = 0;
1694 break;
1695 }
1696 }
1697
1698 /*
1699 * One slot for each directory, one for each devnode
1700 * whose link count changes
1701 */
1702 log_count = devfs_nmountplanes * 2;
1703 wrongsize:
1704 if (devfs_init_event_log(&event_log, log_count, NULL) != 0) {
1705 /* No lock held, no allocations done, can just return */
1706 return -1;
1707 }
1708
1709 DEVFS_LOCK();
1710
1711 if (log_count < devfs_nmountplanes) {
1712 DEVFS_UNLOCK();
1713 devfs_release_event_log(&event_log, 1);
1714 log_count = log_count * 2;
1715 goto wrongsize;
1716 }
1717
1718 if (p) {
1719 *p++ = '\0';
1720
1721 if (dev_finddir(buf, NULL, DEVFS_CREATE, &dirnode, &event_log)
1722 || dev_add_name(p, dirnode, NULL, orig->de_dnp, &new_dev)) {
1723 goto fail;
1724 }
1725 } else {
1726 if (dev_finddir("", NULL, DEVFS_CREATE, &dirnode, &event_log)
1727 || dev_add_name(buf, dirnode, NULL, orig->de_dnp, &new_dev)) {
1728 goto fail;
1729 }
1730 }
1731 devfs_propogate(dirnode->dn_typeinfo.Dir.myname, new_dev, &event_log);
1732 fail:
1733 DEVFS_UNLOCK();
1734 devfs_bulk_notify(&event_log);
1735 devfs_release_event_log(&event_log, 1);
1736
1737 return (new_dev != NULL) ? 0 : -1;
1738 }