diff options
Diffstat (limited to 'litmus/fdso.c')
-rw-r--r-- | litmus/fdso.c | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/litmus/fdso.c b/litmus/fdso.c new file mode 100644 index 000000000000..0ff54e41839c --- /dev/null +++ b/litmus/fdso.c | |||
@@ -0,0 +1,308 @@ | |||
1 | /* fdso.c - file descriptor attached shared objects | ||
2 | * | ||
3 | * (c) 2007 B. Brandenburg, LITMUS^RT project | ||
4 | * | ||
5 | * Notes: | ||
6 | * - objects descriptor (OD) tables are not cloned during a fork. | ||
7 | * - objects are created on-demand, and freed after the last reference | ||
8 | * is dropped. | ||
9 | * - for now, object types are hard coded. | ||
10 | * - As long as we have live objects, we keep a reference to the inode. | ||
11 | */ | ||
12 | |||
13 | #include <linux/errno.h> | ||
14 | #include <linux/sched.h> | ||
15 | #include <linux/mutex.h> | ||
16 | #include <linux/file.h> | ||
17 | #include <asm/uaccess.h> | ||
18 | |||
19 | #include <litmus/fdso.h> | ||
20 | |||
21 | extern struct fdso_ops generic_lock_ops; | ||
22 | |||
23 | static const struct fdso_ops* fdso_ops[] = { | ||
24 | &generic_lock_ops, /* FMLP_SEM */ | ||
25 | &generic_lock_ops, /* SRP_SEM */ | ||
26 | &generic_lock_ops, /* MPCP_SEM */ | ||
27 | &generic_lock_ops, /* MPCP_VS_SEM */ | ||
28 | &generic_lock_ops, /* DPCP_SEM */ | ||
29 | &generic_lock_ops, /* PCP_SEM */ | ||
30 | &generic_lock_ops, /* DFLP_SEM */ | ||
31 | }; | ||
32 | |||
33 | static int fdso_create(void** obj_ref, obj_type_t type, void* __user config) | ||
34 | { | ||
35 | BUILD_BUG_ON(ARRAY_SIZE(fdso_ops) != MAX_OBJ_TYPE + 1); | ||
36 | |||
37 | if (fdso_ops[type]->create) | ||
38 | return fdso_ops[type]->create(obj_ref, type, config); | ||
39 | else | ||
40 | return -EINVAL; | ||
41 | } | ||
42 | |||
43 | static void fdso_destroy(obj_type_t type, void* obj) | ||
44 | { | ||
45 | fdso_ops[type]->destroy(type, obj); | ||
46 | } | ||
47 | |||
48 | static int fdso_open(struct od_table_entry* entry, void* __user config) | ||
49 | { | ||
50 | if (fdso_ops[entry->obj->type]->open) | ||
51 | return fdso_ops[entry->obj->type]->open(entry, config); | ||
52 | else | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | static int fdso_close(struct od_table_entry* entry) | ||
57 | { | ||
58 | if (fdso_ops[entry->obj->type]->close) | ||
59 | return fdso_ops[entry->obj->type]->close(entry); | ||
60 | else | ||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | /* inode must be locked already */ | ||
65 | static int alloc_inode_obj(struct inode_obj_id** obj_ref, | ||
66 | struct inode* inode, | ||
67 | obj_type_t type, | ||
68 | unsigned int id, | ||
69 | void* __user config) | ||
70 | { | ||
71 | struct inode_obj_id* obj; | ||
72 | void* raw_obj; | ||
73 | int err; | ||
74 | |||
75 | obj = kmalloc(sizeof(*obj), GFP_KERNEL); | ||
76 | if (!obj) { | ||
77 | return -ENOMEM; | ||
78 | } | ||
79 | |||
80 | err = fdso_create(&raw_obj, type, config); | ||
81 | if (err != 0) { | ||
82 | kfree(obj); | ||
83 | return err; | ||
84 | } | ||
85 | |||
86 | INIT_LIST_HEAD(&obj->list); | ||
87 | atomic_set(&obj->count, 1); | ||
88 | obj->type = type; | ||
89 | obj->id = id; | ||
90 | obj->obj = raw_obj; | ||
91 | obj->inode = inode; | ||
92 | |||
93 | list_add(&obj->list, &inode->i_obj_list); | ||
94 | atomic_inc(&inode->i_count); | ||
95 | |||
96 | printk(KERN_DEBUG "alloc_inode_obj(%p, %d, %d): object created\n", inode, type, id); | ||
97 | |||
98 | *obj_ref = obj; | ||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | /* inode must be locked already */ | ||
103 | static struct inode_obj_id* get_inode_obj(struct inode* inode, | ||
104 | obj_type_t type, | ||
105 | unsigned int id) | ||
106 | { | ||
107 | struct list_head* pos; | ||
108 | struct inode_obj_id* obj = NULL; | ||
109 | |||
110 | list_for_each(pos, &inode->i_obj_list) { | ||
111 | obj = list_entry(pos, struct inode_obj_id, list); | ||
112 | if (obj->id == id && obj->type == type) { | ||
113 | atomic_inc(&obj->count); | ||
114 | return obj; | ||
115 | } | ||
116 | } | ||
117 | printk(KERN_DEBUG "get_inode_obj(%p, %d, %d): couldn't find object\n", inode, type, id); | ||
118 | return NULL; | ||
119 | } | ||
120 | |||
121 | |||
122 | static void put_inode_obj(struct inode_obj_id* obj) | ||
123 | { | ||
124 | struct inode* inode; | ||
125 | int let_go = 0; | ||
126 | |||
127 | inode = obj->inode; | ||
128 | if (atomic_dec_and_test(&obj->count)) { | ||
129 | |||
130 | mutex_lock(&inode->i_obj_mutex); | ||
131 | /* no new references can be obtained */ | ||
132 | if (!atomic_read(&obj->count)) { | ||
133 | list_del(&obj->list); | ||
134 | fdso_destroy(obj->type, obj->obj); | ||
135 | kfree(obj); | ||
136 | let_go = 1; | ||
137 | } | ||
138 | mutex_unlock(&inode->i_obj_mutex); | ||
139 | if (let_go) | ||
140 | iput(inode); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | static struct od_table_entry* get_od_entry(struct task_struct* t) | ||
145 | { | ||
146 | struct od_table_entry* table; | ||
147 | int i; | ||
148 | |||
149 | |||
150 | table = t->od_table; | ||
151 | if (!table) { | ||
152 | table = kzalloc(sizeof(*table) * MAX_OBJECT_DESCRIPTORS, | ||
153 | GFP_KERNEL); | ||
154 | t->od_table = table; | ||
155 | } | ||
156 | |||
157 | for (i = 0; table && i < MAX_OBJECT_DESCRIPTORS; i++) | ||
158 | if (!table[i].used) { | ||
159 | table[i].used = 1; | ||
160 | return table + i; | ||
161 | } | ||
162 | return NULL; | ||
163 | } | ||
164 | |||
165 | static int put_od_entry(struct od_table_entry* od) | ||
166 | { | ||
167 | put_inode_obj(od->obj); | ||
168 | od->used = 0; | ||
169 | return 0; | ||
170 | } | ||
171 | |||
172 | static long close_od_entry(struct od_table_entry *od) | ||
173 | { | ||
174 | long ret; | ||
175 | |||
176 | /* Give the class a chance to reject the close. */ | ||
177 | ret = fdso_close(od); | ||
178 | if (ret == 0) | ||
179 | ret = put_od_entry(od); | ||
180 | |||
181 | return ret; | ||
182 | } | ||
183 | |||
184 | void exit_od_table(struct task_struct* t) | ||
185 | { | ||
186 | int i; | ||
187 | |||
188 | if (t->od_table) { | ||
189 | for (i = 0; i < MAX_OBJECT_DESCRIPTORS; i++) | ||
190 | if (t->od_table[i].used) | ||
191 | close_od_entry(t->od_table + i); | ||
192 | kfree(t->od_table); | ||
193 | t->od_table = NULL; | ||
194 | } | ||
195 | } | ||
196 | |||
197 | static int do_sys_od_open(struct file* file, obj_type_t type, int id, | ||
198 | void* __user config) | ||
199 | { | ||
200 | int idx = 0, err = 0; | ||
201 | struct inode* inode; | ||
202 | struct inode_obj_id* obj = NULL; | ||
203 | struct od_table_entry* entry; | ||
204 | |||
205 | inode = file_inode(file); | ||
206 | |||
207 | entry = get_od_entry(current); | ||
208 | if (!entry) | ||
209 | return -ENOMEM; | ||
210 | |||
211 | mutex_lock(&inode->i_obj_mutex); | ||
212 | obj = get_inode_obj(inode, type, id); | ||
213 | if (!obj) | ||
214 | err = alloc_inode_obj(&obj, inode, type, id, config); | ||
215 | if (err != 0) { | ||
216 | obj = NULL; | ||
217 | idx = err; | ||
218 | entry->used = 0; | ||
219 | } else { | ||
220 | entry->obj = obj; | ||
221 | entry->class = fdso_ops[type]; | ||
222 | idx = entry - current->od_table; | ||
223 | } | ||
224 | |||
225 | mutex_unlock(&inode->i_obj_mutex); | ||
226 | |||
227 | /* open only if creation succeeded */ | ||
228 | if (!err) | ||
229 | err = fdso_open(entry, config); | ||
230 | if (err < 0) { | ||
231 | /* The class rejected the open call. | ||
232 | * We need to clean up and tell user space. | ||
233 | */ | ||
234 | if (obj) | ||
235 | put_od_entry(entry); | ||
236 | idx = err; | ||
237 | } | ||
238 | |||
239 | return idx; | ||
240 | } | ||
241 | |||
242 | |||
243 | struct od_table_entry* get_entry_for_od(int od) | ||
244 | { | ||
245 | struct task_struct *t = current; | ||
246 | |||
247 | if (!t->od_table) | ||
248 | return NULL; | ||
249 | if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS) | ||
250 | return NULL; | ||
251 | if (!t->od_table[od].used) | ||
252 | return NULL; | ||
253 | return t->od_table + od; | ||
254 | } | ||
255 | |||
256 | |||
257 | asmlinkage long sys_od_open(int fd, int type, int obj_id, void* __user config) | ||
258 | { | ||
259 | int ret = 0; | ||
260 | struct file* file; | ||
261 | |||
262 | /* | ||
263 | 1) get file from fd, get inode from file | ||
264 | 2) lock inode | ||
265 | 3) try to lookup object | ||
266 | 4) if not present create and enqueue object, inc inode refcnt | ||
267 | 5) increment refcnt of object | ||
268 | 6) alloc od_table_entry, setup ptrs | ||
269 | 7) unlock inode | ||
270 | 8) return offset in od_table as OD | ||
271 | */ | ||
272 | |||
273 | if (type < MIN_OBJ_TYPE || type > MAX_OBJ_TYPE) { | ||
274 | ret = -EINVAL; | ||
275 | goto out; | ||
276 | } | ||
277 | |||
278 | file = fget(fd); | ||
279 | if (!file) { | ||
280 | ret = -EBADF; | ||
281 | goto out; | ||
282 | } | ||
283 | |||
284 | ret = do_sys_od_open(file, type, obj_id, config); | ||
285 | |||
286 | fput(file); | ||
287 | |||
288 | out: | ||
289 | return ret; | ||
290 | } | ||
291 | |||
292 | |||
293 | asmlinkage long sys_od_close(int od) | ||
294 | { | ||
295 | int ret = -EINVAL; | ||
296 | struct task_struct *t = current; | ||
297 | |||
298 | if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS) | ||
299 | return ret; | ||
300 | |||
301 | if (!t->od_table || !t->od_table[od].used) | ||
302 | return ret; | ||
303 | |||
304 | |||
305 | ret = close_od_entry(t->od_table + od); | ||
306 | |||
307 | return ret; | ||
308 | } | ||