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