xref: /freebsd/sys/fs/devfs/devfs_devs.c (revision 3a51f88a2721f4cf7aded2a63f20925f8d700a7c)
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$
30  */
31 
32 #include "opt_devfs.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/dirent.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/sysctl.h>
43 #include <sys/vnode.h>
44 
45 #include <machine/atomic.h>
46 
47 #include <fs/devfs/devfs.h>
48 
49 static dev_t devfs_inot[NDEVFSINO];
50 static dev_t *devfs_overflow;
51 static int devfs_ref[NDEVFSINO];
52 static int *devfs_refoverflow;
53 static int devfs_nextino = 3;
54 static int devfs_numino;
55 static int devfs_topino;
56 static int devfs_noverflowwant = NDEVFSOVERFLOW;
57 static int devfs_noverflow;
58 static unsigned devfs_generation;
59 
60 static void devfs_attemptoverflow(int insist);
61 static struct devfs_dirent *devfs_find (struct devfs_dirent *dd, const char *name, int namelen);
62 
63 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
64 SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW,
65 	&devfs_noverflowwant, 0, "Size of DEVFS overflow table");
66 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
67 	&devfs_generation, 0, "DEVFS generation number");
68 SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD,
69 	&devfs_numino, 0, "DEVFS inodes");
70 SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD,
71 	&devfs_topino, 0, "DEVFS highest inode#");
72 
73 static int *
74 devfs_itor(int inode)
75 {
76 	if (inode < NDEVFSINO)
77 		return (&devfs_ref[inode]);
78 	else if (inode < NDEVFSINO + devfs_noverflow)
79 		return (&devfs_refoverflow[inode - NDEVFSINO]);
80 	else
81 		panic ("YRK!");
82 }
83 
84 static void
85 devfs_dropref(int inode)
86 {
87 	int *ip;
88 
89 	ip = devfs_itor(inode);
90 	atomic_add_int(ip, -1);
91 }
92 
93 static int
94 devfs_getref(int inode)
95 {
96 	int *ip, i, j;
97 	dev_t *dp;
98 
99 	ip = devfs_itor(inode);
100 	dp = devfs_itod(inode);
101 	for (;;) {
102 		i = *ip;
103 		j = i + 1;
104 		if (!atomic_cmpset_int(ip, i, j))
105 			continue;
106 		if (*dp != NULL)
107 			return (1);
108 		atomic_add_int(ip, -1);
109 		return(0);
110 	}
111 }
112 
113 struct devfs_dirent **
114 devfs_itode (struct devfs_mount *dm, int inode)
115 {
116 
117 	if (inode < NDEVFSINO)
118 		return (&dm->dm_dirent[inode]);
119 	if (devfs_overflow == NULL)
120 		return (NULL);
121 	if (inode < NDEVFSINO + devfs_noverflow)
122 		return (&dm->dm_overflow[inode - NDEVFSINO]);
123 	return (NULL);
124 }
125 
126 dev_t *
127 devfs_itod (int inode)
128 {
129 
130 	if (inode < NDEVFSINO)
131 		return (&devfs_inot[inode]);
132 	if (devfs_overflow == NULL)
133 		return (NULL);
134 	if (inode < NDEVFSINO + devfs_noverflow)
135 		return (&devfs_overflow[inode - NDEVFSINO]);
136 	return (NULL);
137 }
138 
139 static void
140 devfs_attemptoverflow(int insist)
141 {
142 	dev_t **ot;
143 	int *or;
144 	int n, nb;
145 
146 	/* Check if somebody beat us to it */
147 	if (devfs_overflow != NULL)
148 		return;
149 	ot = NULL;
150 	or = NULL;
151 	n = devfs_noverflowwant;
152 	nb = sizeof (struct dev_t *) * n;
153 	MALLOC(ot, dev_t **, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
154 	if (ot == NULL)
155 		goto bail;
156 	nb = sizeof (int) * n;
157 	MALLOC(or, int *, nb, M_DEVFS, (insist ? M_WAITOK : M_NOWAIT) | M_ZERO);
158 	if (or == NULL)
159 		goto bail;
160 	if (!atomic_cmpset_ptr(&devfs_overflow, NULL, ot))
161 		goto bail;
162 	devfs_refoverflow = or;
163 	devfs_noverflow = n;
164 	printf("DEVFS Overflow table with %d entries allocated when %d in use\n", n, devfs_numino);
165 	return;
166 
167 bail:
168 	/* Somebody beat us to it, or something went wrong. */
169 	if (ot != NULL)
170 		FREE(ot, M_DEVFS);
171 	if (or != NULL)
172 		FREE(or, M_DEVFS);
173 	return;
174 }
175 
176 static struct devfs_dirent *
177 devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
178 {
179 	struct devfs_dirent *de;
180 
181 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
182 		if (namelen != de->de_dirent->d_namlen)
183 			continue;
184 		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
185 			continue;
186 		break;
187 	}
188 	return (de);
189 }
190 
191 struct devfs_dirent *
192 devfs_newdirent(char *name, int namelen)
193 {
194 	int i;
195 	struct devfs_dirent *de;
196 	struct dirent d;
197 
198 	d.d_namlen = namelen;
199 	i = sizeof (*de) + GENERIC_DIRSIZ(&d);
200 	MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK | M_ZERO);
201 	de->de_dirent = (struct dirent *)(de + 1);
202 	de->de_dirent->d_namlen = namelen;
203 	de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
204 	bcopy(name, de->de_dirent->d_name, namelen);
205 	de->de_dirent->d_name[namelen] = '\0';
206 	getnanotime(&de->de_ctime);
207 	de->de_mtime = de->de_atime = de->de_ctime;
208 	de->de_links = 1;
209 	return (de);
210 }
211 
212 struct devfs_dirent *
213 devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot)
214 {
215 	struct devfs_dirent *dd;
216 	struct devfs_dirent *de;
217 
218 	dd = devfs_newdirent(name, namelen);
219 
220 	TAILQ_INIT(&dd->de_dlist);
221 
222 	dd->de_dirent->d_type = DT_DIR;
223 	dd->de_mode = 0755;
224 	dd->de_links = 2;
225 	dd->de_dir = dd;
226 
227 	de = devfs_newdirent(".", 1);
228 	de->de_dirent->d_type = DT_DIR;
229 	de->de_dir = dd;
230 	de->de_flags |= DE_DOT;
231 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
232 
233 	de = devfs_newdirent("..", 2);
234 	de->de_dirent->d_type = DT_DIR;
235 	if (dotdot == NULL)
236 		de->de_dir = dd;
237 	else
238 		de->de_dir = dotdot;
239 	de->de_flags |= DE_DOTDOT;
240 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
241 
242 	return (dd);
243 }
244 
245 static void
246 devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de)
247 {
248 
249 	if (de->de_symlink) {
250 		FREE(de->de_symlink, M_DEVFS);
251 		de->de_symlink = NULL;
252 	}
253 	if (de->de_vnode)
254 		de->de_vnode->v_data = NULL;
255 	TAILQ_REMOVE(&dd->de_dlist, de, de_list);
256 	FREE(de, M_DEVFS);
257 }
258 
259 void
260 devfs_purge(struct devfs_dirent *dd)
261 {
262 	struct devfs_dirent *de;
263 
264 	for (;;) {
265 		de = TAILQ_FIRST(&dd->de_dlist);
266 		if (de == NULL)
267 			break;
268 		devfs_delete(dd, de);
269 	}
270 	FREE(dd, M_DEVFS);
271 }
272 
273 
274 int
275 devfs_populate(struct devfs_mount *dm)
276 {
277 	int i, j;
278 	dev_t dev, pdev;
279 	struct devfs_dirent *dd;
280 	struct devfs_dirent *de, **dep;
281 	char *q, *s;
282 
283 	if (dm->dm_generation == devfs_generation)
284 		return (0);
285 	lockmgr(&dm->dm_lock, LK_UPGRADE, 0, curproc);
286 	if (devfs_noverflow && dm->dm_overflow == NULL) {
287 		i = devfs_noverflow * sizeof (struct devfs_dirent *);
288 		MALLOC(dm->dm_overflow, struct devfs_dirent **, i,
289 			M_DEVFS, M_WAITOK | M_ZERO);
290 	}
291 	while (dm->dm_generation != devfs_generation) {
292 		dm->dm_generation = devfs_generation;
293 		for (i = 0; i <= devfs_topino; i++) {
294 			dev = *devfs_itod(i);
295 			dep = devfs_itode(dm, i);
296 			de = *dep;
297 			if (dev == NULL && de == DE_DELETED) {
298 				*dep = NULL;
299 				continue;
300 			}
301 			if (dev == NULL && de != NULL) {
302 				dd = de->de_dir;
303 				*dep = NULL;
304 				TAILQ_REMOVE(&dd->de_dlist, de, de_list);
305 				if (de->de_vnode)
306 					de->de_vnode->v_data = NULL;
307 				FREE(de, M_DEVFS);
308 				devfs_dropref(i);
309 				continue;
310 			}
311 			if (dev == NULL)
312 				continue;
313 			if (de != NULL)
314 				continue;
315 			if (!devfs_getref(i))
316 				continue;
317 			dd = dm->dm_basedir;
318 			s = dev->si_name;
319 			for (;;) {
320 				for (q = s; *q != '/' && *q != '\0'; q++)
321 					continue;
322 				if (*q != '/')
323 					break;
324 				de = devfs_find(dd, s, q - s);
325 				if (de == NULL) {
326 					de = devfs_vmkdir(s, q - s, dd);
327 					de->de_inode = dm->dm_inode++;
328 					TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
329 					dd->de_links++;
330 				}
331 				s = q + 1;
332 				dd = de;
333 			}
334 			de = devfs_newdirent(s, q - s);
335 			if (dev->si_flags & SI_ALIAS) {
336 				de->de_inode = dm->dm_inode++;
337 				de->de_uid = 0;
338 				de->de_gid = 0;
339 				de->de_mode = 0666;
340 				de->de_dirent->d_type = DT_LNK;
341 				pdev = dev->si_drv1;
342 				j = strlen(pdev->si_name) + 1;
343 				MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK);
344 				bcopy(pdev->si_name, de->de_symlink, j);
345 			} else {
346 				de->de_inode = i;
347 				de->de_uid = dev->si_uid;
348 				de->de_gid = dev->si_gid;
349 				de->de_mode = dev->si_mode;
350 				de->de_dirent->d_type = DT_CHR;
351 			}
352 			*dep = de;
353 			de->de_dir = dd;
354 			TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
355 #if 0
356 			printf("Add ino%d %s\n", i, dev->si_name);
357 #endif
358 		}
359 	}
360 	lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, curproc);
361 	return (0);
362 }
363 
364 static void
365 devfs_create(dev_t dev)
366 {
367 	int ino, i, *ip;
368 	dev_t *dp;
369 
370 	for (;;) {
371 		/* Grab the next inode number */
372 		ino = devfs_nextino;
373 		i = ino + 1;
374 		/* wrap around when we reach the end */
375 		if (i >= NDEVFSINO + devfs_noverflow)
376 			i = 3;
377 		if (!atomic_cmpset_int(&devfs_nextino, ino, i))
378 			continue;
379 
380 		/* see if it was occupied */
381 		dp = devfs_itod(ino);
382 		if (dp == NULL)
383 			Debugger("dp == NULL\n");
384 		if (*dp != NULL)
385 			continue;
386 		ip = devfs_itor(ino);
387 		if (ip == NULL)
388 			Debugger("ip == NULL\n");
389 		if (*ip != 0)
390 			continue;
391 
392 		if (!atomic_cmpset_ptr(dp, NULL, dev))
393 			continue;
394 
395 		dev->si_inode = ino;
396 		for (;;) {
397 			i = devfs_topino;
398 			if (i >= ino)
399 				break;
400 			if (atomic_cmpset_int(&devfs_topino, i, ino))
401 				break;
402 			printf("failed topino %d %d\n", i, ino);
403 		}
404 		break;
405 	}
406 
407 	atomic_add_int(&devfs_numino, 1);
408 	atomic_add_int(&devfs_generation, 1);
409 	if (devfs_overflow == NULL && devfs_numino + 100 > NDEVFSINO)
410 		devfs_attemptoverflow(0);
411 }
412 
413 static void
414 devfs_destroy(dev_t dev)
415 {
416 	int ino, i;
417 
418 	ino = dev->si_inode;
419 	dev->si_inode = 0;
420 	if (ino == 0)
421 		return;
422 	if (atomic_cmpset_ptr(devfs_itod(ino), dev, NULL)) {
423 		atomic_add_int(&devfs_generation, 1);
424 		atomic_add_int(&devfs_numino, -1);
425 		i = devfs_nextino;
426 		if (ino < i)
427 			atomic_cmpset_int(&devfs_nextino, i, ino);
428 	}
429 }
430 
431 devfs_create_t *devfs_create_hook = devfs_create;
432 devfs_destroy_t *devfs_destroy_hook = devfs_destroy;
433 int devfs_present = 1;
434