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#include <linux/err.h>
33#include <linux/list.h>
34#include <linux/sched.h>
35#include <linux/math64.h>
36#include <linux/module.h>
37#include <linux/mutex.h>
38#include <linux/mtd/ubi.h>
39#include <linux/mtd/mtd.h>
40#include "ubi-media.h"
41
42#define err_msg(fmt, ...) \
43 printk(KERN_DEBUG "gluebi (pid %d): %s: " fmt "\n", \
44 current->pid, __func__, ##__VA_ARGS__)
45
46
47
48
49
50
51
52
53
54
55struct gluebi_device {
56 struct mtd_info mtd;
57 int refcnt;
58 struct ubi_volume_desc *desc;
59 int ubi_num;
60 int vol_id;
61 struct list_head list;
62};
63
64
65static LIST_HEAD(gluebi_devices);
66static DEFINE_MUTEX(devices_mutex);
67
68
69
70
71
72
73
74
75
76
77
78static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
79{
80 struct gluebi_device *gluebi;
81
82 list_for_each_entry(gluebi, &gluebi_devices, list)
83 if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
84 return gluebi;
85 return NULL;
86}
87
88
89
90
91
92
93
94
95
96static int gluebi_get_device(struct mtd_info *mtd)
97{
98 struct gluebi_device *gluebi;
99 int ubi_mode = UBI_READONLY;
100
101 if (!try_module_get(THIS_MODULE))
102 return -ENODEV;
103
104 if (mtd->flags & MTD_WRITEABLE)
105 ubi_mode = UBI_READWRITE;
106
107 gluebi = container_of(mtd, struct gluebi_device, mtd);
108 mutex_lock(&devices_mutex);
109 if (gluebi->refcnt > 0) {
110
111
112
113
114
115
116
117
118 gluebi->refcnt += 1;
119 mutex_unlock(&devices_mutex);
120 return 0;
121 }
122
123
124
125
126
127 gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
128 ubi_mode);
129 if (IS_ERR(gluebi->desc)) {
130 mutex_unlock(&devices_mutex);
131 module_put(THIS_MODULE);
132 return PTR_ERR(gluebi->desc);
133 }
134 gluebi->refcnt += 1;
135 mutex_unlock(&devices_mutex);
136 return 0;
137}
138
139
140
141
142
143
144
145
146static void gluebi_put_device(struct mtd_info *mtd)
147{
148 struct gluebi_device *gluebi;
149
150 gluebi = container_of(mtd, struct gluebi_device, mtd);
151 mutex_lock(&devices_mutex);
152 gluebi->refcnt -= 1;
153 if (gluebi->refcnt == 0)
154 ubi_close_volume(gluebi->desc);
155 module_put(THIS_MODULE);
156 mutex_unlock(&devices_mutex);
157}
158
159
160
161
162
163
164
165
166
167
168
169
170static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
171 size_t *retlen, unsigned char *buf)
172{
173 int err = 0, lnum, offs, total_read;
174 struct gluebi_device *gluebi;
175
176 if (len < 0 || from < 0 || from + len > mtd->size)
177 return -EINVAL;
178
179 gluebi = container_of(mtd, struct gluebi_device, mtd);
180
181 lnum = div_u64_rem(from, mtd->erasesize, &offs);
182 total_read = len;
183 while (total_read) {
184 size_t to_read = mtd->erasesize - offs;
185
186 if (to_read > total_read)
187 to_read = total_read;
188
189 err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
190 if (err)
191 break;
192
193 lnum += 1;
194 offs = 0;
195 total_read -= to_read;
196 buf += to_read;
197 }
198
199 *retlen = len - total_read;
200 return err;
201}
202
203
204
205
206
207
208
209
210
211
212
213
214static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
215 size_t *retlen, const u_char *buf)
216{
217 int err = 0, lnum, offs, total_written;
218 struct gluebi_device *gluebi;
219
220 if (len < 0 || to < 0 || len + to > mtd->size)
221 return -EINVAL;
222
223 gluebi = container_of(mtd, struct gluebi_device, mtd);
224
225 if (!(mtd->flags & MTD_WRITEABLE))
226 return -EROFS;
227
228 lnum = div_u64_rem(to, mtd->erasesize, &offs);
229
230 if (len % mtd->writesize || offs % mtd->writesize)
231 return -EINVAL;
232
233 total_written = len;
234 while (total_written) {
235 size_t to_write = mtd->erasesize - offs;
236
237 if (to_write > total_written)
238 to_write = total_written;
239
240 err = ubi_write(gluebi->desc, lnum, buf, offs, to_write);
241 if (err)
242 break;
243
244 lnum += 1;
245 offs = 0;
246 total_written -= to_write;
247 buf += to_write;
248 }
249
250 *retlen = len - total_written;
251 return err;
252}
253
254
255
256
257
258
259
260
261
262static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
263{
264 int err, i, lnum, count;
265 struct gluebi_device *gluebi;
266
267 if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
268 return -EINVAL;
269 if (instr->len < 0 || instr->addr + instr->len > mtd->size)
270 return -EINVAL;
271 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
272 return -EINVAL;
273
274 lnum = mtd_div_by_eb(instr->addr, mtd);
275 count = mtd_div_by_eb(instr->len, mtd);
276
277 gluebi = container_of(mtd, struct gluebi_device, mtd);
278
279 if (!(mtd->flags & MTD_WRITEABLE))
280 return -EROFS;
281
282 for (i = 0; i < count - 1; i++) {
283 err = ubi_leb_unmap(gluebi->desc, lnum + i);
284 if (err)
285 goto out_err;
286 }
287
288
289
290
291
292
293
294 err = ubi_leb_erase(gluebi->desc, lnum + i);
295 if (err)
296 goto out_err;
297
298 instr->state = MTD_ERASE_DONE;
299 mtd_erase_callback(instr);
300 return 0;
301
302out_err:
303 instr->state = MTD_ERASE_FAILED;
304 instr->fail_addr = (long long)lnum * mtd->erasesize;
305 return err;
306}
307
308
309
310
311
312
313
314
315
316
317static int gluebi_create(struct ubi_device_info *di,
318 struct ubi_volume_info *vi)
319{
320 struct gluebi_device *gluebi, *g;
321 struct mtd_info *mtd;
322
323 gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
324 if (!gluebi)
325 return -ENOMEM;
326
327 mtd = &gluebi->mtd;
328 mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
329 if (!mtd->name) {
330 kfree(gluebi);
331 return -ENOMEM;
332 }
333
334 gluebi->vol_id = vi->vol_id;
335 gluebi->ubi_num = vi->ubi_num;
336 mtd->type = MTD_UBIVOLUME;
337 if (!di->ro_mode)
338 mtd->flags = MTD_WRITEABLE;
339 mtd->owner = THIS_MODULE;
340 mtd->writesize = di->min_io_size;
341 mtd->erasesize = vi->usable_leb_size;
342 mtd->read = gluebi_read;
343 mtd->write = gluebi_write;
344 mtd->erase = gluebi_erase;
345 mtd->get_device = gluebi_get_device;
346 mtd->put_device = gluebi_put_device;
347
348
349
350
351
352
353 if (vi->vol_type == UBI_DYNAMIC_VOLUME)
354 mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
355 else
356 mtd->size = vi->used_bytes;
357
358
359 mutex_lock(&devices_mutex);
360 g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
361 if (g)
362 err_msg("gluebi MTD device %d form UBI device %d volume %d "
363 "already exists", g->mtd.index, vi->ubi_num,
364 vi->vol_id);
365 mutex_unlock(&devices_mutex);
366
367 if (add_mtd_device(mtd)) {
368 err_msg("cannot add MTD device");
369 kfree(mtd->name);
370 kfree(gluebi);
371 return -ENFILE;
372 }
373
374 mutex_lock(&devices_mutex);
375 list_add_tail(&gluebi->list, &gluebi_devices);
376 mutex_unlock(&devices_mutex);
377 return 0;
378}
379
380
381
382
383
384
385
386
387
388static int gluebi_remove(struct ubi_volume_info *vi)
389{
390 int err = 0;
391 struct mtd_info *mtd;
392 struct gluebi_device *gluebi;
393
394 mutex_lock(&devices_mutex);
395 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
396 if (!gluebi) {
397 err_msg("got remove notification for unknown UBI device %d "
398 "volume %d", vi->ubi_num, vi->vol_id);
399 err = -ENOENT;
400 } else if (gluebi->refcnt)
401 err = -EBUSY;
402 else
403 list_del(&gluebi->list);
404 mutex_unlock(&devices_mutex);
405 if (err)
406 return err;
407
408 mtd = &gluebi->mtd;
409 err = del_mtd_device(mtd);
410 if (err) {
411 err_msg("cannot remove fake MTD device %d, UBI device %d, "
412 "volume %d, error %d", mtd->index, gluebi->ubi_num,
413 gluebi->vol_id, err);
414 mutex_lock(&devices_mutex);
415 list_add_tail(&gluebi->list, &gluebi_devices);
416 mutex_unlock(&devices_mutex);
417 return err;
418 }
419
420 kfree(mtd->name);
421 kfree(gluebi);
422 return 0;
423}
424
425
426
427
428
429
430
431
432
433
434
435static int gluebi_updated(struct ubi_volume_info *vi)
436{
437 struct gluebi_device *gluebi;
438
439 mutex_lock(&devices_mutex);
440 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
441 if (!gluebi) {
442 mutex_unlock(&devices_mutex);
443 err_msg("got update notification for unknown UBI device %d "
444 "volume %d", vi->ubi_num, vi->vol_id);
445 return -ENOENT;
446 }
447
448 if (vi->vol_type == UBI_STATIC_VOLUME)
449 gluebi->mtd.size = vi->used_bytes;
450 mutex_unlock(&devices_mutex);
451 return 0;
452}
453
454
455
456
457
458
459
460
461
462static int gluebi_resized(struct ubi_volume_info *vi)
463{
464 struct gluebi_device *gluebi;
465
466 mutex_lock(&devices_mutex);
467 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
468 if (!gluebi) {
469 mutex_unlock(&devices_mutex);
470 err_msg("got update notification for unknown UBI device %d "
471 "volume %d", vi->ubi_num, vi->vol_id);
472 return -ENOENT;
473 }
474 gluebi->mtd.size = vi->used_bytes;
475 mutex_unlock(&devices_mutex);
476 return 0;
477}
478
479
480
481
482
483
484
485static int gluebi_notify(struct notifier_block *nb, unsigned long l,
486 void *ns_ptr)
487{
488 struct ubi_notification *nt = ns_ptr;
489
490 switch (l) {
491 case UBI_VOLUME_ADDED:
492 gluebi_create(&nt->di, &nt->vi);
493 break;
494 case UBI_VOLUME_REMOVED:
495 gluebi_remove(&nt->vi);
496 break;
497 case UBI_VOLUME_RESIZED:
498 gluebi_resized(&nt->vi);
499 break;
500 case UBI_VOLUME_UPDATED:
501 gluebi_updated(&nt->vi);
502 break;
503 default:
504 break;
505 }
506 return NOTIFY_OK;
507}
508
509static struct notifier_block gluebi_notifier = {
510 .notifier_call = gluebi_notify,
511};
512
513static int __init ubi_gluebi_init(void)
514{
515 return ubi_register_volume_notifier(&gluebi_notifier, 0);
516}
517
518static void __exit ubi_gluebi_exit(void)
519{
520 struct gluebi_device *gluebi, *g;
521
522 list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
523 int err;
524 struct mtd_info *mtd = &gluebi->mtd;
525
526 err = del_mtd_device(mtd);
527 if (err)
528 err_msg("error %d while removing gluebi MTD device %d, "
529 "UBI device %d, volume %d - ignoring", err,
530 mtd->index, gluebi->ubi_num, gluebi->vol_id);
531 kfree(mtd->name);
532 kfree(gluebi);
533 }
534 ubi_unregister_volume_notifier(&gluebi_notifier);
535}
536
537module_init(ubi_gluebi_init);
538module_exit(ubi_gluebi_exit);
539MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
540MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
541MODULE_LICENSE("GPL");
542