blob: c1d38234d725c858f2b986f4e4d5e37a75449e5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/base/map.c
3 *
4 * (C) Copyright Al Viro 2002,2003
5 * Released under GPL v2.
6 *
7 * NOTE: data structure needs to be changed. It works, but for large dev_t
8 * it will be too slow. It is isolated, though, so these changes will be
9 * local to that file.
10 */
11
12#include <linux/module.h>
13#include <linux/slab.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kdev_t.h>
16#include <linux/kobject.h>
17#include <linux/kobj_map.h>
18
19struct kobj_map {
20 struct probe {
21 struct probe *next;
22 dev_t dev;
23 unsigned long range;
24 struct module *owner;
25 kobj_probe_t *get;
26 int (*lock)(dev_t, void *);
27 void *data;
28 } *probes[255];
Jes Sorensen58383af2006-02-06 14:12:43 -080029 struct mutex *lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
33 struct module *module, kobj_probe_t *probe,
34 int (*lock)(dev_t, void *), void *data)
35{
36 unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
37 unsigned index = MAJOR(dev);
38 unsigned i;
39 struct probe *p;
40
41 if (n > 255)
42 n = 255;
43
Andrei Poenaru2e4fabe2015-03-08 12:29:02 +020044 p = kmalloc_array(n, sizeof(struct probe), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (p == NULL)
46 return -ENOMEM;
47
48 for (i = 0; i < n; i++, p++) {
49 p->owner = module;
50 p->get = probe;
51 p->lock = lock;
52 p->dev = dev;
53 p->range = range;
54 p->data = data;
55 }
Jes Sorensen58383af2006-02-06 14:12:43 -080056 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 for (i = 0, p -= n; i < n; i++, p++, index++) {
58 struct probe **s = &domain->probes[index % 255];
59 while (*s && (*s)->range < range)
60 s = &(*s)->next;
61 p->next = *s;
62 *s = p;
63 }
Jes Sorensen58383af2006-02-06 14:12:43 -080064 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
68void kobj_unmap(struct kobj_map *domain, dev_t dev, unsigned long range)
69{
70 unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
71 unsigned index = MAJOR(dev);
72 unsigned i;
73 struct probe *found = NULL;
74
75 if (n > 255)
76 n = 255;
77
Jes Sorensen58383af2006-02-06 14:12:43 -080078 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 for (i = 0; i < n; i++, index++) {
80 struct probe **s;
81 for (s = &domain->probes[index % 255]; *s; s = &(*s)->next) {
82 struct probe *p = *s;
83 if (p->dev == dev && p->range == range) {
84 *s = p->next;
85 if (!found)
86 found = p;
87 break;
88 }
89 }
90 }
Jes Sorensen58383af2006-02-06 14:12:43 -080091 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 kfree(found);
93}
94
95struct kobject *kobj_lookup(struct kobj_map *domain, dev_t dev, int *index)
96{
97 struct kobject *kobj;
98 struct probe *p;
99 unsigned long best = ~0UL;
100
101retry:
Jes Sorensen58383af2006-02-06 14:12:43 -0800102 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 for (p = domain->probes[MAJOR(dev) % 255]; p; p = p->next) {
104 struct kobject *(*probe)(dev_t, int *, void *);
105 struct module *owner;
106 void *data;
107
108 if (p->dev > dev || p->dev + p->range - 1 < dev)
109 continue;
110 if (p->range - 1 >= best)
111 break;
112 if (!try_module_get(p->owner))
113 continue;
114 owner = p->owner;
115 data = p->data;
116 probe = p->get;
117 best = p->range - 1;
118 *index = dev - p->dev;
119 if (p->lock && p->lock(dev, data) < 0) {
120 module_put(owner);
121 continue;
122 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800123 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 kobj = probe(dev, index, data);
125 /* Currently ->owner protects _only_ ->probe() itself. */
126 module_put(owner);
127 if (kobj)
128 return kobj;
129 goto retry;
130 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800131 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return NULL;
133}
134
Jes Sorensen58383af2006-02-06 14:12:43 -0800135struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct mutex *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct kobj_map *p = kmalloc(sizeof(struct kobj_map), GFP_KERNEL);
Jiri Slaby4aed0642005-09-13 01:25:01 -0700138 struct probe *base = kzalloc(sizeof(*base), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int i;
140
141 if ((p == NULL) || (base == NULL)) {
142 kfree(p);
143 kfree(base);
144 return NULL;
145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 base->dev = 1;
148 base->range = ~0;
149 base->get = base_probe;
150 for (i = 0; i < 255; i++)
151 p->probes[i] = base;
Jes Sorensen58383af2006-02-06 14:12:43 -0800152 p->lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return p;
154}