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