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