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