Deleted Added
sdiff udiff text old ( 104113 ) new ( 107698 )
full compact
1#define DEBUG 1
2/*
3 * Copyright (c) 2000
4 * Poul-Henning Kamp. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Neither the name of the University nor the names of its contributors
12 * may be used to endorse or promote products derived from this software
13 * without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
28 *
29 * $FreeBSD: head/sys/fs/devfs/devfs_devs.c 104113 2002-09-28 21:21:01Z phk $
30 */
31
32#include "opt_devfs.h"
33#include "opt_mac.h"
34#ifndef NODEVFS
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
39#include <sys/dirent.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/mac.h>
43#include <sys/malloc.h>
44#include <sys/proc.h>
45#include <sys/sysctl.h>
46#include <sys/vnode.h>
47
48#include <machine/atomic.h>
49
50#include <fs/devfs/devfs.h>
51
52static dev_t devfs_inot[NDEVFSINO];
53static dev_t *devfs_overflow;
54static int devfs_ref[NDEVFSINO];
55static int *devfs_refoverflow;
56static int devfs_nextino = 3;
57static int devfs_numino;
58static int devfs_topino;
59static int devfs_noverflowwant = NDEVFSOVERFLOW;
60static int devfs_noverflow;
61static unsigned devfs_generation;
62
63static void devfs_attemptoverflow(int insist);
64static struct devfs_dirent *devfs_find (struct devfs_dirent *dd, const char *name, int namelen);
65
66SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
67SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW,
68 &devfs_noverflowwant, 0, "Size of DEVFS overflow table");
69SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
70 &devfs_generation, 0, "DEVFS generation number");
71SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD,
72 &devfs_numino, 0, "DEVFS inodes");
73SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD,
74 &devfs_topino, 0, "DEVFS highest inode#");
75
76static int *
77devfs_itor(int inode)
78{
79 if (inode < NDEVFSINO)
80 return (&devfs_ref[inode]);
81 else if (inode < NDEVFSINO + devfs_noverflow)
82 return (&devfs_refoverflow[inode - NDEVFSINO]);
83 else
84 panic ("YRK!");
85}
86
87static void
88devfs_dropref(int inode)
89{
90 int *ip;
91
92 ip = devfs_itor(inode);
93 atomic_add_int(ip, -1);
94}
95
96static int
97devfs_getref(int inode)
98{
99 int *ip, i, j;
100 dev_t *dp;
101
102 ip = devfs_itor(inode);
103 dp = devfs_itod(inode);
104 for (;;) {
105 i = *ip;
106 j = i + 1;
107 if (!atomic_cmpset_int(ip, i, j))
108 continue;
109 if (*dp != NULL)
110 return (1);
111 atomic_add_int(ip, -1);
112 return(0);
113 }
114}
115
116struct devfs_dirent **
117devfs_itode (struct devfs_mount *dm, int inode)
118{
119
120 if (inode < NDEVFSINO)
121 return (&dm->dm_dirent[inode]);
122 if (devfs_overflow == NULL)
123 return (NULL);
124 if (inode < NDEVFSINO + devfs_noverflow)
125 return (&dm->dm_overflow[inode - NDEVFSINO]);
126 return (NULL);
127}
128
129dev_t *
130devfs_itod (int inode)
131{
132
133 if (inode < NDEVFSINO)
134 return (&devfs_inot[inode]);
135 if (devfs_overflow == NULL)
136 return (NULL);
137 if (inode < NDEVFSINO + devfs_noverflow)
138 return (&devfs_overflow[inode - NDEVFSINO]);
139 return (NULL);
140}
141
142static void
143devfs_attemptoverflow(int insist)
144{
145 dev_t **ot;
146 int *or;
147 int n, nb;
148
149 /* Check if somebody beat us to it */
150 if (devfs_overflow != NULL)
151 return;
152 ot = NULL;
153 or = NULL;
154 n = devfs_noverflowwant;
155 nb = sizeof (dev_t *) * n;
156 MALLOC(ot, dev_t **, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
157 if (ot == NULL)
158 goto bail;
159 nb = sizeof (int) * n;
160 MALLOC(or, int *, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
161 if (or == NULL)
162 goto bail;
163 if (!atomic_cmpset_ptr(&devfs_overflow, NULL, ot))
164 goto bail;
165 devfs_refoverflow = or;
166 devfs_noverflow = n;
167 printf("DEVFS Overflow table with %d entries allocated when %d in use\n", n, devfs_numino);
168 return;
169
170bail:
171 /* Somebody beat us to it, or something went wrong. */
172 if (ot != NULL)
173 FREE(ot, M_DEVFS);
174 if (or != NULL)
175 FREE(or, M_DEVFS);
176 return;
177}
178
179static struct devfs_dirent *
180devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
181{
182 struct devfs_dirent *de;
183
184 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
185 if (namelen != de->de_dirent->d_namlen)
186 continue;
187 if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
188 continue;
189 break;
190 }
191 return (de);
192}
193
194struct devfs_dirent *
195devfs_newdirent(char *name, int namelen)
196{
197 int i;
198 struct devfs_dirent *de;
199 struct dirent d;
200
201 d.d_namlen = namelen;
202 i = sizeof (*de) + GENERIC_DIRSIZ(&d);
203 MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK | M_ZERO);
204 de->de_dirent = (struct dirent *)(de + 1);
205 de->de_dirent->d_namlen = namelen;
206 de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
207 bcopy(name, de->de_dirent->d_name, namelen);
208 de->de_dirent->d_name[namelen] = '\0';
209 vfs_timestamp(&de->de_ctime);
210 de->de_mtime = de->de_atime = de->de_ctime;
211 de->de_links = 1;
212#ifdef MAC
213 mac_init_devfsdirent(de);
214#endif
215 return (de);
216}
217
218struct devfs_dirent *
219devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot)
220{
221 struct devfs_dirent *dd;
222 struct devfs_dirent *de;
223
224 dd = devfs_newdirent(name, namelen);
225
226 TAILQ_INIT(&dd->de_dlist);
227
228 dd->de_dirent->d_type = DT_DIR;
229 dd->de_mode = 0555;
230 dd->de_links = 2;
231 dd->de_dir = dd;
232
233 de = devfs_newdirent(".", 1);
234 de->de_dirent->d_type = DT_DIR;
235 de->de_dir = dd;
236 de->de_flags |= DE_DOT;
237 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
238
239 de = devfs_newdirent("..", 2);
240 de->de_dirent->d_type = DT_DIR;
241 if (dotdot == NULL)
242 de->de_dir = dd;
243 else
244 de->de_dir = dotdot;
245 de->de_flags |= DE_DOTDOT;
246 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
247
248 return (dd);
249}
250
251static void
252devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de)
253{
254
255 if (de->de_symlink) {
256 FREE(de->de_symlink, M_DEVFS);
257 de->de_symlink = NULL;
258 }
259 if (de->de_vnode)
260 de->de_vnode->v_data = NULL;
261 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
262#ifdef MAC
263 mac_destroy_devfsdirent(de);
264#endif
265 FREE(de, M_DEVFS);
266}
267
268void
269devfs_purge(struct devfs_dirent *dd)
270{
271 struct devfs_dirent *de;
272
273 for (;;) {
274 de = TAILQ_FIRST(&dd->de_dlist);
275 if (de == NULL)
276 break;
277 devfs_delete(dd, de);
278 }
279 FREE(dd, M_DEVFS);
280}
281
282
283int
284devfs_populate(struct devfs_mount *dm)
285{
286 int i, j;
287 dev_t dev, pdev;
288 struct devfs_dirent *dd;
289 struct devfs_dirent *de, **dep;
290 char *q, *s;
291
292 if (dm->dm_generation == devfs_generation)
293 return (0);
294 lockmgr(&dm->dm_lock, LK_UPGRADE, 0, curthread);
295 if (devfs_noverflow && dm->dm_overflow == NULL) {
296 i = devfs_noverflow * sizeof (struct devfs_dirent *);
297 MALLOC(dm->dm_overflow, struct devfs_dirent **, i,
298 M_DEVFS, M_WAITOK | M_ZERO);
299 }
300 while (dm->dm_generation != devfs_generation) {
301 dm->dm_generation = devfs_generation;
302 for (i = 0; i <= devfs_topino; i++) {
303 dev = *devfs_itod(i);
304 dep = devfs_itode(dm, i);
305 de = *dep;
306 if (dev == NULL && de == DE_DELETED) {
307 *dep = NULL;
308 continue;
309 }
310 if (dev == NULL && de != NULL) {
311 dd = de->de_dir;
312 *dep = NULL;
313 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
314 if (de->de_vnode)
315 de->de_vnode->v_data = NULL;
316 FREE(de, M_DEVFS);
317 devfs_dropref(i);
318 continue;
319 }
320 if (dev == NULL)
321 continue;
322 if (de != NULL)
323 continue;
324 if (!devfs_getref(i))
325 continue;
326 dd = dm->dm_basedir;
327 s = dev->si_name;
328 for (;;) {
329 for (q = s; *q != '/' && *q != '\0'; q++)
330 continue;
331 if (*q != '/')
332 break;
333 de = devfs_find(dd, s, q - s);
334 if (de == NULL) {
335 de = devfs_vmkdir(s, q - s, dd);
336#ifdef MAC
337 mac_create_devfs_directory(s, q - s,
338 de);
339#endif
340 de->de_inode = dm->dm_inode++;
341 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
342 dd->de_links++;
343 }
344 s = q + 1;
345 dd = de;
346 }
347 de = devfs_newdirent(s, q - s);
348 if (dev->si_flags & SI_ALIAS) {
349 de->de_inode = dm->dm_inode++;
350 de->de_uid = 0;
351 de->de_gid = 0;
352 de->de_mode = 0755;
353 de->de_dirent->d_type = DT_LNK;
354 pdev = dev->si_parent;
355 j = strlen(pdev->si_name) + 1;
356 MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK);
357 bcopy(pdev->si_name, de->de_symlink, j);
358 } else {
359 de->de_inode = i;
360 de->de_uid = dev->si_uid;
361 de->de_gid = dev->si_gid;
362 de->de_mode = dev->si_mode;
363 de->de_dirent->d_type = DT_CHR;
364 }
365#ifdef MAC
366 mac_create_devfs_device(dev, de);
367#endif
368 *dep = de;
369 de->de_dir = dd;
370 devfs_rules_apply(dm, de);
371 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
372#if 0
373 printf("Add ino%d %s\n", i, dev->si_name);
374#endif
375 }
376 }
377 lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, curthread);
378 return (0);
379}
380
381static void
382devfs_create(dev_t dev)
383{
384 int ino, i, *ip;
385 dev_t *dp;
386
387 for (;;) {
388 /* Grab the next inode number */
389 ino = devfs_nextino;
390 i = ino + 1;
391 /* wrap around when we reach the end */
392 if (i >= NDEVFSINO + devfs_noverflow)
393 i = 3;
394 if (!atomic_cmpset_int(&devfs_nextino, ino, i))
395 continue;
396
397 /* see if it was occupied */
398 dp = devfs_itod(ino);
399 if (dp == NULL)
400 Debugger("dp == NULL\n");
401 if (*dp != NULL)
402 continue;
403 ip = devfs_itor(ino);
404 if (ip == NULL)
405 Debugger("ip == NULL\n");
406 if (*ip != 0)
407 continue;
408
409 if (!atomic_cmpset_ptr(dp, NULL, dev))
410 continue;
411
412 dev->si_inode = ino;
413 for (;;) {
414 i = devfs_topino;
415 if (i >= ino)
416 break;
417 if (atomic_cmpset_int(&devfs_topino, i, ino))
418 break;
419 printf("failed topino %d %d\n", i, ino);
420 }
421 break;
422 }
423
424 atomic_add_int(&devfs_numino, 1);
425 atomic_add_int(&devfs_generation, 1);
426 if (devfs_overflow == NULL && devfs_numino + 100 > NDEVFSINO)
427 devfs_attemptoverflow(0);
428}
429
430static void
431devfs_destroy(dev_t dev)
432{
433 int ino, i;
434
435 ino = dev->si_inode;
436 dev->si_inode = 0;
437 if (ino == 0)
438 return;
439 if (atomic_cmpset_ptr(devfs_itod(ino), dev, NULL)) {
440 atomic_add_int(&devfs_generation, 1);
441 atomic_add_int(&devfs_numino, -1);
442 i = devfs_nextino;
443 if (ino < i)
444 atomic_cmpset_int(&devfs_nextino, i, ino);
445 }
446}
447
448static void
449devfs_init(void *junk)
450{
451 devfs_create_hook = devfs_create;
452 devfs_destroy_hook = devfs_destroy;
453 devfs_present = 1;
454 devfs_rules_init();
455}
456
457SYSINIT(devfs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_init, NULL);
458
459#endif