1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include "drmP.h"
37
38
39
40
41
42
43
44
45
46
47
48static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
49{
50 struct drm_file *retval = NULL;
51 struct drm_magic_entry *pt;
52 struct drm_hash_item *hash;
53 struct drm_device *dev = master->minor->dev;
54
55 mutex_lock(&dev->struct_mutex);
56 if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
57 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
58 retval = pt->priv;
59 }
60 mutex_unlock(&dev->struct_mutex);
61 return retval;
62}
63
64
65
66
67
68
69
70
71
72
73
74
75static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
76 drm_magic_t magic)
77{
78 struct drm_magic_entry *entry;
79 struct drm_device *dev = master->minor->dev;
80 DRM_DEBUG("%d\n", magic);
81
82 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
83 if (!entry)
84 return -ENOMEM;
85 entry->priv = priv;
86 entry->hash_item.key = (unsigned long)magic;
87 mutex_lock(&dev->struct_mutex);
88 drm_ht_insert_item(&master->magiclist, &entry->hash_item);
89 list_add_tail(&entry->head, &master->magicfree);
90 mutex_unlock(&dev->struct_mutex);
91
92 return 0;
93}
94
95
96
97
98
99
100
101
102
103
104static int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
105{
106 struct drm_magic_entry *pt;
107 struct drm_hash_item *hash;
108 struct drm_device *dev = master->minor->dev;
109
110 DRM_DEBUG("%d\n", magic);
111
112 mutex_lock(&dev->struct_mutex);
113 if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
114 mutex_unlock(&dev->struct_mutex);
115 return -EINVAL;
116 }
117 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
118 drm_ht_remove_item(&master->magiclist, hash);
119 list_del(&pt->head);
120 mutex_unlock(&dev->struct_mutex);
121
122 kfree(pt);
123
124 return 0;
125}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
141{
142 static drm_magic_t sequence = 0;
143 static DEFINE_SPINLOCK(lock);
144 struct drm_auth *auth = data;
145
146
147 if (file_priv->magic) {
148 auth->magic = file_priv->magic;
149 } else {
150 do {
151 spin_lock(&lock);
152 if (!sequence)
153 ++sequence;
154 auth->magic = sequence++;
155 spin_unlock(&lock);
156 } while (drm_find_file(file_priv->master, auth->magic));
157 file_priv->magic = auth->magic;
158 drm_add_magic(file_priv->master, file_priv, auth->magic);
159 }
160
161 DRM_DEBUG("%u\n", auth->magic);
162
163 return 0;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177int drm_authmagic(struct drm_device *dev, void *data,
178 struct drm_file *file_priv)
179{
180 struct drm_auth *auth = data;
181 struct drm_file *file;
182
183 DRM_DEBUG("%u\n", auth->magic);
184 if ((file = drm_find_file(file_priv->master, auth->magic))) {
185 file->authenticated = 1;
186 drm_remove_magic(file_priv->master, auth->magic);
187 return 0;
188 }
189 return -EINVAL;
190}
191