devfs_devs.c revision 66877
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 66877 2000-10-09 14:18:07Z phk $
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/proc.h>
40#include <sys/malloc.h>
41#include <sys/sysctl.h>
42#include <sys/kernel.h>
43#include <sys/lock.h>
44
45#include <machine/atomic.h>
46
47#include <fs/devfs/devfs.h>
48
49static dev_t devfs_inot[NDEVFSINO];
50static dev_t *devfs_overflow;
51static int devfs_ref[NDEVFSINO];
52static int *devfs_refoverflow;
53static int devfs_nextino = 3;
54static int devfs_numino;
55static int devfs_topino;
56static int devfs_noverflowwant = NDEVFSOVERFLOW;
57static int devfs_noverflow;
58static unsigned devfs_generation;
59
60SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
61SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW,
62	&devfs_noverflowwant, 0, "Size of DEVFS overflow table");
63SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
64	&devfs_generation, 0, "DEVFS generation number");
65SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD,
66	&devfs_numino, 0, "DEVFS inodes");
67SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD,
68	&devfs_topino, 0, "DEVFS highest inode#");
69
70static int *
71devfs_itor(int inode)
72{
73	if (inode < NDEVFSINO)
74		return (&devfs_ref[inode]);
75	else if (inode < NDEVFSINO + devfs_noverflow)
76		return (&devfs_refoverflow[inode - NDEVFSINO]);
77	else
78		panic ("YRK!");
79}
80
81static void
82devfs_dropref(int inode)
83{
84	int *ip;
85
86	ip = devfs_itor(inode);
87	atomic_add_int(ip, -1);
88}
89
90static int
91devfs_getref(int inode)
92{
93	int *ip, i, j;
94	dev_t *dp;
95
96	ip = devfs_itor(inode);
97	dp = devfs_itod(inode);
98	for (;;) {
99		i = *ip;
100		j = i + 1;
101		if (!atomic_cmpset_int(ip, i, j))
102			continue;
103		if (*dp != NULL)
104			return (1);
105		atomic_add_int(ip, -1);
106		return(0);
107	}
108}
109
110struct devfs_dirent **
111devfs_itode (struct devfs_mount *dm, int inode)
112{
113
114	if (inode < NDEVFSINO)
115		return (&dm->dm_dirent[inode]);
116	if (devfs_overflow == NULL)
117		return (NULL);
118	if (inode < NDEVFSINO + devfs_noverflow)
119		return (&dm->dm_overflow[inode - NDEVFSINO]);
120	return (NULL);
121}
122
123dev_t *
124devfs_itod (int inode)
125{
126
127	if (inode < NDEVFSINO)
128		return (&devfs_inot[inode]);
129	if (devfs_overflow == NULL)
130		return (NULL);
131	if (inode < NDEVFSINO + devfs_noverflow)
132		return (&devfs_overflow[inode - NDEVFSINO]);
133	return (NULL);
134}
135
136void
137devfs_attemptoverflow(int insist)
138{
139	dev_t **ot;
140	int *or;
141	int n, nb;
142
143	/* Check if somebody beat us to it */
144	if (devfs_overflow != NULL)
145		return;
146	ot = NULL;
147	or = NULL;
148	n = devfs_noverflowwant;
149	nb = sizeof (struct dev_t *) * n;
150	MALLOC(ot, dev_t **, nb, M_DEVFS, insist ? M_WAITOK : M_NOWAIT);
151	if (ot == NULL)
152		goto bail;
153	bzero(ot, nb);
154	nb = sizeof (int) * n;
155	MALLOC(or, int *, nb, M_DEVFS, insist ? M_WAITOK : M_NOWAIT);
156	if (or == NULL)
157		goto bail;
158	bzero(or, nb);
159	if (!atomic_cmpset_ptr(&devfs_overflow, NULL, ot))
160		goto bail;
161	devfs_refoverflow = or;
162	devfs_noverflow = n;
163	printf("DEVFS Overflow table with %d entries allocated when %d in use\n", n, devfs_numino);
164	return;
165
166bail:
167	/* Somebody beat us to it, or something went wrong. */
168	if (ot != NULL)
169		FREE(ot, M_DEVFS);
170	if (or != NULL)
171		FREE(or, M_DEVFS);
172	return;
173}
174
175struct devfs_dirent *
176devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
177{
178	struct devfs_dirent *de;
179
180	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
181		if (namelen != de->de_dirent->d_namlen)
182			continue;
183		if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
184			continue;
185		break;
186	}
187	return (de);
188}
189
190struct devfs_dirent *
191devfs_newdirent(char *name, int namelen)
192{
193	int i;
194	struct devfs_dirent *de;
195	struct dirent d;
196
197	d.d_namlen = namelen;
198	i = sizeof (*de) + GENERIC_DIRSIZ(&d);
199	MALLOC(de, struct devfs_dirent *, i, M_DEVFS, M_WAITOK);
200	bzero(de, i);
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 + 1);
205	nanotime(&de->de_ctime);
206	de->de_mtime = de->de_atime = de->de_ctime;
207	de->de_links = 1;
208	return (de);
209}
210
211struct devfs_dirent *
212devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot)
213{
214	struct devfs_dirent *dd;
215	struct devfs_dirent *de;
216
217	dd = devfs_newdirent(name, namelen);
218
219	TAILQ_INIT(&dd->de_dlist);
220
221	dd->de_dirent->d_type = DT_DIR;
222	dd->de_mode = 0755;
223	dd->de_links = 2;
224	dd->de_dir = dd;
225
226	de = devfs_newdirent(".", 1);
227	de->de_dirent->d_type = DT_DIR;
228	de->de_dir = dd;
229	de->de_flags |= DE_DOT;
230	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
231
232	de = devfs_newdirent("..", 2);
233	de->de_dirent->d_type = DT_DIR;
234	if (dotdot == NULL)
235		de->de_dir = dd;
236	else
237		de->de_dir = dotdot;
238	de->de_flags |= DE_DOTDOT;
239	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
240
241	return (dd);
242}
243
244static void
245devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de)
246{
247
248	if (de->de_symlink) {
249		FREE(de->de_symlink, M_DEVFS);
250		de->de_symlink = NULL;
251	}
252	if (de->de_vnode)
253		de->de_vnode->v_data = NULL;
254	TAILQ_REMOVE(&dd->de_dlist, de, de_list);
255	FREE(de, M_DEVFS);
256}
257
258void
259devfs_purge(struct devfs_dirent *dd)
260{
261	struct devfs_dirent *de;
262
263	for (;;) {
264		de = TAILQ_FIRST(&dd->de_dlist);
265		if (de == NULL)
266			break;
267		devfs_delete(dd, de);
268	}
269	FREE(dd, M_DEVFS);
270}
271
272
273int
274devfs_populate(struct devfs_mount *dm)
275{
276	int i, j;
277	dev_t dev, pdev;
278	struct devfs_dirent *dd;
279	struct devfs_dirent *de, **dep;
280	char *q, *s;
281
282	if (dm->dm_generation == devfs_generation)
283		return (0);
284	lockmgr(&dm->dm_lock, LK_UPGRADE, 0, curproc);
285	if (devfs_noverflow && dm->dm_overflow == NULL) {
286		i = devfs_noverflow * sizeof (struct devfs_dirent *);
287		MALLOC(dm->dm_overflow, struct devfs_dirent **, i,
288			M_DEVFS, M_WAITOK);
289		bzero(dm->dm_overflow, i);
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
364static void
365devfs_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
413static void
414devfs_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
431devfs_create_t *devfs_create_hook = devfs_create;
432devfs_destroy_t *devfs_destroy_hook = devfs_destroy;
433int devfs_present = 1;
434