1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef __UBOOT__
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/stringify.h>
24#include <linux/namei.h>
25#include <linux/stat.h>
26#include <linux/miscdevice.h>
27#include <linux/log2.h>
28#include <linux/kthread.h>
29#include <linux/kernel.h>
30#include <linux/slab.h>
31#include <linux/major.h>
32#else
33#include <linux/bug.h>
34#include <linux/log2.h>
35#endif
36#include <linux/err.h>
37#include <ubi_uboot.h>
38#include <linux/mtd/partitions.h>
39
40#include "ubi.h"
41
42
43#define MTD_PARAM_LEN_MAX 64
44
45
46#define MTD_PARAM_MAX_COUNT 4
47
48
49#define MAX_MTD_UBI_BEB_LIMIT 768
50
51#ifdef CONFIG_MTD_UBI_MODULE
52#define ubi_is_module() 1
53#else
54#define ubi_is_module() 0
55#endif
56
57#if (CONFIG_SYS_MALLOC_LEN < (512 << 10))
58#error Malloc area too small for UBI, increase CONFIG_SYS_MALLOC_LEN to >= 512k
59#endif
60
61
62
63
64
65
66
67
68struct mtd_dev_param {
69 char name[MTD_PARAM_LEN_MAX];
70 int ubi_num;
71 int vid_hdr_offs;
72 int max_beb_per1024;
73};
74
75
76static int __initdata mtd_devs;
77
78
79static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
80#ifndef __UBOOT__
81#ifdef CONFIG_MTD_UBI_FASTMAP
82
83static bool fm_autoconvert;
84static bool fm_debug;
85#endif
86#else
87#ifdef CONFIG_MTD_UBI_FASTMAP
88#if !defined(CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT)
89#define CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT 0
90#endif
91static bool fm_autoconvert = CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT;
92#if !defined(CONFIG_MTD_UBI_FM_DEBUG)
93#define CONFIG_MTD_UBI_FM_DEBUG 0
94#endif
95static bool fm_debug = CONFIG_MTD_UBI_FM_DEBUG;
96#endif
97#endif
98
99
100struct kmem_cache *ubi_wl_entry_slab;
101
102#ifndef __UBOOT__
103
104static struct miscdevice ubi_ctrl_cdev = {
105 .minor = MISC_DYNAMIC_MINOR,
106 .name = "ubi_ctrl",
107 .fops = &ubi_ctrl_cdev_operations,
108};
109#endif
110
111
112#ifndef __UBOOT__
113static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
114#else
115struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
116#endif
117
118#ifndef __UBOOT__
119
120DEFINE_MUTEX(ubi_devices_mutex);
121
122
123static DEFINE_SPINLOCK(ubi_devices_lock);
124
125
126static ssize_t ubi_version_show(struct class *class,
127 struct class_attribute *attr, char *buf)
128{
129 return sprintf(buf, "%d\n", UBI_VERSION);
130}
131
132
133static struct class_attribute ubi_class_attrs[] = {
134 __ATTR(version, S_IRUGO, ubi_version_show, NULL),
135 __ATTR_NULL
136};
137
138
139struct class ubi_class = {
140 .name = UBI_NAME_STR,
141 .owner = THIS_MODULE,
142 .class_attrs = ubi_class_attrs,
143};
144
145static ssize_t dev_attribute_show(struct device *dev,
146 struct device_attribute *attr, char *buf);
147
148
149static struct device_attribute dev_eraseblock_size =
150 __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
151static struct device_attribute dev_avail_eraseblocks =
152 __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
153static struct device_attribute dev_total_eraseblocks =
154 __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
155static struct device_attribute dev_volumes_count =
156 __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
157static struct device_attribute dev_max_ec =
158 __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
159static struct device_attribute dev_reserved_for_bad =
160 __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
161static struct device_attribute dev_bad_peb_count =
162 __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
163static struct device_attribute dev_max_vol_count =
164 __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
165static struct device_attribute dev_min_io_size =
166 __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
167static struct device_attribute dev_bgt_enabled =
168 __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
169static struct device_attribute dev_mtd_num =
170 __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
171#endif
172
173
174
175
176
177
178
179
180
181
182
183int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
184{
185 int ret;
186 struct ubi_notification nt;
187
188 ubi_do_get_device_info(ubi, &nt.di);
189 ubi_do_get_volume_info(ubi, vol, &nt.vi);
190
191 switch (ntype) {
192 case UBI_VOLUME_ADDED:
193 case UBI_VOLUME_REMOVED:
194 case UBI_VOLUME_RESIZED:
195 case UBI_VOLUME_RENAMED:
196 ret = ubi_update_fastmap(ubi);
197 if (ret)
198 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
199 }
200
201 return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
202}
203
204
205
206
207
208
209
210
211
212
213
214
215int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
216{
217 struct ubi_notification nt;
218 int i, count = 0;
219#ifndef __UBOOT__
220 int ret;
221#endif
222
223 ubi_do_get_device_info(ubi, &nt.di);
224
225 mutex_lock(&ubi->device_mutex);
226 for (i = 0; i < ubi->vtbl_slots; i++) {
227
228
229
230
231
232 if (!ubi->volumes[i])
233 continue;
234
235 ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
236#ifndef __UBOOT__
237 if (nb)
238 nb->notifier_call(nb, ntype, &nt);
239 else
240 ret = blocking_notifier_call_chain(&ubi_notifiers, ntype,
241 &nt);
242#endif
243 count += 1;
244 }
245 mutex_unlock(&ubi->device_mutex);
246
247 return count;
248}
249
250
251
252
253
254
255
256
257
258
259int ubi_enumerate_volumes(struct notifier_block *nb)
260{
261 int i, count = 0;
262
263
264
265
266
267 for (i = 0; i < UBI_MAX_DEVICES; i++) {
268 struct ubi_device *ubi = ubi_devices[i];
269
270 if (!ubi)
271 continue;
272 count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
273 }
274
275 return count;
276}
277
278
279
280
281
282
283
284
285
286
287struct ubi_device *ubi_get_device(int ubi_num)
288{
289 struct ubi_device *ubi;
290
291 spin_lock(&ubi_devices_lock);
292 ubi = ubi_devices[ubi_num];
293 if (ubi) {
294 ubi_assert(ubi->ref_count >= 0);
295 ubi->ref_count += 1;
296 get_device(&ubi->dev);
297 }
298 spin_unlock(&ubi_devices_lock);
299
300 return ubi;
301}
302
303
304
305
306
307void ubi_put_device(struct ubi_device *ubi)
308{
309 spin_lock(&ubi_devices_lock);
310 ubi->ref_count -= 1;
311 put_device(&ubi->dev);
312 spin_unlock(&ubi_devices_lock);
313}
314
315
316
317
318
319
320
321
322struct ubi_device *ubi_get_by_major(int major)
323{
324 int i;
325 struct ubi_device *ubi;
326
327 spin_lock(&ubi_devices_lock);
328 for (i = 0; i < UBI_MAX_DEVICES; i++) {
329 ubi = ubi_devices[i];
330 if (ubi && MAJOR(ubi->cdev.dev) == major) {
331 ubi_assert(ubi->ref_count >= 0);
332 ubi->ref_count += 1;
333 get_device(&ubi->dev);
334 spin_unlock(&ubi_devices_lock);
335 return ubi;
336 }
337 }
338 spin_unlock(&ubi_devices_lock);
339
340 return NULL;
341}
342
343
344
345
346
347
348
349
350
351int ubi_major2num(int major)
352{
353 int i, ubi_num = -ENODEV;
354
355 spin_lock(&ubi_devices_lock);
356 for (i = 0; i < UBI_MAX_DEVICES; i++) {
357 struct ubi_device *ubi = ubi_devices[i];
358
359 if (ubi && MAJOR(ubi->cdev.dev) == major) {
360 ubi_num = ubi->ubi_num;
361 break;
362 }
363 }
364 spin_unlock(&ubi_devices_lock);
365
366 return ubi_num;
367}
368
369#ifndef __UBOOT__
370
371static ssize_t dev_attribute_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
374 ssize_t ret;
375 struct ubi_device *ubi;
376
377
378
379
380
381
382
383
384
385
386
387 ubi = container_of(dev, struct ubi_device, dev);
388 ubi = ubi_get_device(ubi->ubi_num);
389 if (!ubi)
390 return -ENODEV;
391
392 if (attr == &dev_eraseblock_size)
393 ret = sprintf(buf, "%d\n", ubi->leb_size);
394 else if (attr == &dev_avail_eraseblocks)
395 ret = sprintf(buf, "%d\n", ubi->avail_pebs);
396 else if (attr == &dev_total_eraseblocks)
397 ret = sprintf(buf, "%d\n", ubi->good_peb_count);
398 else if (attr == &dev_volumes_count)
399 ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
400 else if (attr == &dev_max_ec)
401 ret = sprintf(buf, "%d\n", ubi->max_ec);
402 else if (attr == &dev_reserved_for_bad)
403 ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
404 else if (attr == &dev_bad_peb_count)
405 ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
406 else if (attr == &dev_max_vol_count)
407 ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
408 else if (attr == &dev_min_io_size)
409 ret = sprintf(buf, "%d\n", ubi->min_io_size);
410 else if (attr == &dev_bgt_enabled)
411 ret = sprintf(buf, "%d\n", ubi->thread_enabled);
412 else if (attr == &dev_mtd_num)
413 ret = sprintf(buf, "%d\n", ubi->mtd->index);
414 else
415 ret = -EINVAL;
416
417 ubi_put_device(ubi);
418 return ret;
419}
420
421static struct attribute *ubi_dev_attrs[] = {
422 &dev_eraseblock_size.attr,
423 &dev_avail_eraseblocks.attr,
424 &dev_total_eraseblocks.attr,
425 &dev_volumes_count.attr,
426 &dev_max_ec.attr,
427 &dev_reserved_for_bad.attr,
428 &dev_bad_peb_count.attr,
429 &dev_max_vol_count.attr,
430 &dev_min_io_size.attr,
431 &dev_bgt_enabled.attr,
432 &dev_mtd_num.attr,
433 NULL
434};
435ATTRIBUTE_GROUPS(ubi_dev);
436
437static void dev_release(struct device *dev)
438{
439 struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
440
441 kfree(ubi);
442}
443
444
445
446
447
448
449
450
451
452
453static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
454{
455 int err;
456
457 ubi->dev.release = dev_release;
458 ubi->dev.devt = ubi->cdev.dev;
459 ubi->dev.class = &ubi_class;
460 ubi->dev.groups = ubi_dev_groups;
461 dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
462 err = device_register(&ubi->dev);
463 if (err)
464 return err;
465
466 *ref = 1;
467 return 0;
468}
469
470
471
472
473
474static void ubi_sysfs_close(struct ubi_device *ubi)
475{
476 device_unregister(&ubi->dev);
477}
478#endif
479
480
481
482
483
484static void kill_volumes(struct ubi_device *ubi)
485{
486 int i;
487
488 for (i = 0; i < ubi->vtbl_slots; i++)
489 if (ubi->volumes[i])
490 ubi_free_volume(ubi, ubi->volumes[i]);
491}
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511static int uif_init(struct ubi_device *ubi, int *ref)
512{
513 int i, err;
514#ifndef __UBOOT__
515 dev_t dev;
516#endif
517
518 *ref = 0;
519 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
520
521
522
523
524
525
526
527
528
529 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
530 if (err) {
531 ubi_err(ubi, "cannot register UBI character devices");
532 return err;
533 }
534
535 ubi_assert(MINOR(dev) == 0);
536 cdev_init(&ubi->cdev, &ubi_cdev_operations);
537 dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
538 ubi->cdev.owner = THIS_MODULE;
539
540 err = cdev_add(&ubi->cdev, dev, 1);
541 if (err) {
542 ubi_err(ubi, "cannot add character device");
543 goto out_unreg;
544 }
545
546 err = ubi_sysfs_init(ubi, ref);
547 if (err)
548 goto out_sysfs;
549
550 for (i = 0; i < ubi->vtbl_slots; i++)
551 if (ubi->volumes[i]) {
552 err = ubi_add_volume(ubi, ubi->volumes[i]);
553 if (err) {
554 ubi_err(ubi, "cannot add volume %d", i);
555 goto out_volumes;
556 }
557 }
558
559 return 0;
560
561out_volumes:
562 kill_volumes(ubi);
563out_sysfs:
564 if (*ref)
565 get_device(&ubi->dev);
566 ubi_sysfs_close(ubi);
567 cdev_del(&ubi->cdev);
568out_unreg:
569 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
570 ubi_err(ubi, "cannot initialize UBI %s, error %d",
571 ubi->ubi_name, err);
572 return err;
573}
574
575
576
577
578
579
580
581
582
583static void uif_close(struct ubi_device *ubi)
584{
585 kill_volumes(ubi);
586 ubi_sysfs_close(ubi);
587 cdev_del(&ubi->cdev);
588 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
589}
590
591
592
593
594
595void ubi_free_internal_volumes(struct ubi_device *ubi)
596{
597 int i;
598
599 for (i = ubi->vtbl_slots;
600 i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
601 kfree(ubi->volumes[i]->eba_tbl);
602 kfree(ubi->volumes[i]);
603 }
604}
605
606static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
607{
608 int limit, device_pebs;
609 uint64_t device_size;
610
611 if (!max_beb_per1024)
612 return 0;
613
614
615
616
617
618
619
620
621
622
623 device_size = mtd_get_device_size(ubi->mtd);
624 device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
625 limit = mult_frac(device_pebs, max_beb_per1024, 1024);
626
627
628 if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
629 limit += 1;
630
631 return limit;
632}
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650static int io_init(struct ubi_device *ubi, int max_beb_per1024)
651{
652 dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
653 dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
654
655 if (ubi->mtd->numeraseregions != 0) {
656
657
658
659
660
661
662
663
664
665 ubi_err(ubi, "multiple regions, not implemented");
666 return -EINVAL;
667 }
668
669 if (ubi->vid_hdr_offset < 0)
670 return -EINVAL;
671
672
673
674
675
676
677 ubi->peb_size = ubi->mtd->erasesize;
678 ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
679 ubi->flash_size = ubi->mtd->size;
680
681 if (mtd_can_have_bb(ubi->mtd)) {
682 ubi->bad_allowed = 1;
683 ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
684 }
685
686 if (ubi->mtd->type == MTD_NORFLASH) {
687 ubi_assert(ubi->mtd->writesize == 1);
688 ubi->nor_flash = 1;
689 }
690
691 ubi->min_io_size = ubi->mtd->writesize;
692 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
693
694
695
696
697
698
699 if (!is_power_of_2(ubi->min_io_size)) {
700 ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
701 ubi->min_io_size);
702 return -EINVAL;
703 }
704
705 ubi_assert(ubi->hdrs_min_io_size > 0);
706 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
707 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
708
709 ubi->max_write_size = ubi->mtd->writebufsize;
710
711
712
713
714 if (ubi->max_write_size < ubi->min_io_size ||
715 ubi->max_write_size % ubi->min_io_size ||
716 !is_power_of_2(ubi->max_write_size)) {
717 ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
718 ubi->max_write_size, ubi->min_io_size);
719 return -EINVAL;
720 }
721
722
723 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
724 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
725
726 dbg_gen("min_io_size %d", ubi->min_io_size);
727 dbg_gen("max_write_size %d", ubi->max_write_size);
728 dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
729 dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
730 dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
731
732 if (ubi->vid_hdr_offset == 0)
733
734 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
735 ubi->ec_hdr_alsize;
736 else {
737 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
738 ~(ubi->hdrs_min_io_size - 1);
739 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
740 ubi->vid_hdr_aloffset;
741 }
742
743
744 ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
745 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
746
747 dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
748 dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
749 dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
750 dbg_gen("leb_start %d", ubi->leb_start);
751
752
753 if (ubi->vid_hdr_shift % 4) {
754 ubi_err(ubi, "unaligned VID header shift %d",
755 ubi->vid_hdr_shift);
756 return -EINVAL;
757 }
758
759
760 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
761 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
762 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
763 ubi->leb_start & (ubi->min_io_size - 1)) {
764 ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
765 ubi->vid_hdr_offset, ubi->leb_start);
766 return -EINVAL;
767 }
768
769
770
771
772
773 ubi->max_erroneous = ubi->peb_count / 10;
774 if (ubi->max_erroneous < 16)
775 ubi->max_erroneous = 16;
776 dbg_gen("max_erroneous %d", ubi->max_erroneous);
777
778
779
780
781
782
783 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
784 ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
785 ubi->ro_mode = 1;
786 }
787
788 ubi->leb_size = ubi->peb_size - ubi->leb_start;
789
790 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
791 ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
792 ubi->mtd->index);
793 ubi->ro_mode = 1;
794 }
795
796
797
798
799
800
801
802
803
804 return 0;
805}
806
807
808
809
810
811
812
813
814
815
816
817static int autoresize(struct ubi_device *ubi, int vol_id)
818{
819 struct ubi_volume_desc desc;
820 struct ubi_volume *vol = ubi->volumes[vol_id];
821 int err, old_reserved_pebs = vol->reserved_pebs;
822
823 if (ubi->ro_mode) {
824 ubi_warn(ubi, "skip auto-resize because of R/O mode");
825 return 0;
826 }
827
828
829
830
831
832
833 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
834
835 if (ubi->avail_pebs == 0) {
836 struct ubi_vtbl_record vtbl_rec;
837
838
839
840
841
842 vtbl_rec = ubi->vtbl[vol_id];
843 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
844 if (err)
845 ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
846 vol_id);
847 } else {
848 desc.vol = vol;
849 err = ubi_resize_volume(&desc,
850 old_reserved_pebs + ubi->avail_pebs);
851 if (err)
852 ubi_err(ubi, "cannot auto-resize volume %d",
853 vol_id);
854 }
855
856 if (err)
857 return err;
858
859 ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
860 vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
861 return 0;
862}
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
881 int vid_hdr_offset, int max_beb_per1024)
882{
883 struct ubi_device *ubi;
884 int i, err, ref = 0;
885
886 if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
887 return -EINVAL;
888
889 if (!max_beb_per1024)
890 max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
891
892
893
894
895
896
897
898 for (i = 0; i < UBI_MAX_DEVICES; i++) {
899 ubi = ubi_devices[i];
900 if (ubi && mtd->index == ubi->mtd->index) {
901 ubi_err(ubi, "mtd%d is already attached to ubi%d",
902 mtd->index, i);
903 return -EEXIST;
904 }
905 }
906
907
908
909
910
911
912
913
914
915 if (mtd->type == MTD_UBIVOLUME) {
916 ubi_err(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
917 mtd->index);
918 return -EINVAL;
919 }
920
921 if (ubi_num == UBI_DEV_NUM_AUTO) {
922
923 for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
924 if (!ubi_devices[ubi_num])
925 break;
926 if (ubi_num == UBI_MAX_DEVICES) {
927 ubi_err(ubi, "only %d UBI devices may be created",
928 UBI_MAX_DEVICES);
929 return -ENFILE;
930 }
931 } else {
932 if (ubi_num >= UBI_MAX_DEVICES)
933 return -EINVAL;
934
935
936 if (ubi_devices[ubi_num]) {
937 ubi_err(ubi, "already exists");
938 return -EEXIST;
939 }
940 }
941
942 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
943 if (!ubi)
944 return -ENOMEM;
945
946 ubi->mtd = mtd;
947 ubi->ubi_num = ubi_num;
948 ubi->vid_hdr_offset = vid_hdr_offset;
949 ubi->autoresize_vol_id = -1;
950
951#ifdef CONFIG_MTD_UBI_FASTMAP
952 ubi->fm_pool.used = ubi->fm_pool.size = 0;
953 ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
954
955
956
957
958
959 ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
960 ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
961 ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
962 UBI_FM_MIN_POOL_SIZE);
963
964 ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
965 ubi->fm_disabled = !fm_autoconvert;
966 if (fm_debug)
967 ubi_enable_dbg_chk_fastmap(ubi);
968
969 if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
970 <= UBI_FM_MAX_START) {
971 ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
972 UBI_FM_MAX_START);
973 ubi->fm_disabled = 1;
974 }
975
976 ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
977 ubi_msg(ubi, "default fastmap WL pool size: %d",
978 ubi->fm_wl_pool.max_size);
979#else
980 ubi->fm_disabled = 1;
981#endif
982 mutex_init(&ubi->buf_mutex);
983 mutex_init(&ubi->ckvol_mutex);
984 mutex_init(&ubi->device_mutex);
985 spin_lock_init(&ubi->volumes_lock);
986 init_rwsem(&ubi->fm_protect);
987 init_rwsem(&ubi->fm_eba_sem);
988
989 ubi_msg(ubi, "attaching mtd%d", mtd->index);
990
991 err = io_init(ubi, max_beb_per1024);
992 if (err)
993 goto out_free;
994
995 err = -ENOMEM;
996 ubi->peb_buf = vmalloc(ubi->peb_size);
997 if (!ubi->peb_buf)
998 goto out_free;
999
1000#ifdef CONFIG_MTD_UBI_FASTMAP
1001 ubi->fm_size = ubi_calc_fm_size(ubi);
1002 ubi->fm_buf = vzalloc(ubi->fm_size);
1003 if (!ubi->fm_buf)
1004 goto out_free;
1005#endif
1006 err = ubi_attach(ubi, 0);
1007 if (err) {
1008 ubi_err(ubi, "failed to attach mtd%d, error %d",
1009 mtd->index, err);
1010 goto out_free;
1011 }
1012
1013 if (ubi->autoresize_vol_id != -1) {
1014 err = autoresize(ubi, ubi->autoresize_vol_id);
1015 if (err)
1016 goto out_detach;
1017 }
1018
1019 err = uif_init(ubi, &ref);
1020 if (err)
1021 goto out_detach;
1022
1023 err = ubi_debugfs_init_dev(ubi);
1024 if (err)
1025 goto out_uif;
1026
1027 ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
1028 if (IS_ERR(ubi->bgt_thread)) {
1029 err = PTR_ERR(ubi->bgt_thread);
1030 ubi_err(ubi, "cannot spawn \"%s\", error %d",
1031 ubi->bgt_name, err);
1032 goto out_debugfs;
1033 }
1034
1035 ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
1036 mtd->index, mtd->name, ubi->flash_size >> 20);
1037 ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
1038 ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
1039 ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
1040 ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
1041 ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
1042 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
1043 ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
1044 ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
1045 ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
1046 ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
1047 ubi->vtbl_slots);
1048 ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
1049 ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
1050 ubi->image_seq);
1051 ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
1052 ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
1053
1054
1055
1056
1057
1058 spin_lock(&ubi->wl_lock);
1059 ubi->thread_enabled = 1;
1060#ifndef __UBOOT__
1061 wake_up_process(ubi->bgt_thread);
1062#else
1063
1064
1065
1066
1067 err = do_work(ubi);
1068 if (err) {
1069 ubi_err(ubi, "%s: work failed with error code %d",
1070 ubi->bgt_name, err);
1071 }
1072#endif
1073
1074 spin_unlock(&ubi->wl_lock);
1075
1076 ubi_devices[ubi_num] = ubi;
1077 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
1078 return ubi_num;
1079
1080out_debugfs:
1081 ubi_debugfs_exit_dev(ubi);
1082out_uif:
1083 get_device(&ubi->dev);
1084 ubi_assert(ref);
1085 uif_close(ubi);
1086out_detach:
1087 ubi_wl_close(ubi);
1088 ubi_free_internal_volumes(ubi);
1089 vfree(ubi->vtbl);
1090out_free:
1091 vfree(ubi->peb_buf);
1092 vfree(ubi->fm_buf);
1093 if (ref)
1094 put_device(&ubi->dev);
1095 else
1096 kfree(ubi);
1097 return err;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113int ubi_detach_mtd_dev(int ubi_num, int anyway)
1114{
1115 struct ubi_device *ubi;
1116
1117 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1118 return -EINVAL;
1119
1120 ubi = ubi_get_device(ubi_num);
1121 if (!ubi)
1122 return -EINVAL;
1123
1124 spin_lock(&ubi_devices_lock);
1125 put_device(&ubi->dev);
1126 ubi->ref_count -= 1;
1127 if (ubi->ref_count) {
1128 if (!anyway) {
1129 spin_unlock(&ubi_devices_lock);
1130 return -EBUSY;
1131 }
1132
1133 ubi_err(ubi, "%s reference count %d, destroy anyway",
1134 ubi->ubi_name, ubi->ref_count);
1135 }
1136 ubi_devices[ubi_num] = NULL;
1137 spin_unlock(&ubi_devices_lock);
1138
1139 ubi_assert(ubi_num == ubi->ubi_num);
1140 ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
1141 ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
1142#ifdef CONFIG_MTD_UBI_FASTMAP
1143
1144
1145
1146
1147 if (!ubi_dbg_chk_fastmap(ubi))
1148 ubi_update_fastmap(ubi);
1149#endif
1150
1151
1152
1153
1154 if (ubi->bgt_thread)
1155 kthread_stop(ubi->bgt_thread);
1156
1157
1158
1159
1160
1161 get_device(&ubi->dev);
1162
1163 ubi_debugfs_exit_dev(ubi);
1164 uif_close(ubi);
1165
1166 ubi_wl_close(ubi);
1167 ubi_free_internal_volumes(ubi);
1168 vfree(ubi->vtbl);
1169 put_mtd_device(ubi->mtd);
1170 vfree(ubi->peb_buf);
1171 vfree(ubi->fm_buf);
1172 ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
1173 put_device(&ubi->dev);
1174 return 0;
1175}
1176
1177#ifndef __UBOOT__
1178
1179
1180
1181
1182
1183
1184
1185
1186static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1187{
1188 int err, major, minor, mode;
1189 struct path path;
1190
1191
1192 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1193 if (err)
1194 return ERR_PTR(err);
1195
1196
1197 major = imajor(d_backing_inode(path.dentry));
1198 minor = iminor(d_backing_inode(path.dentry));
1199 mode = d_backing_inode(path.dentry)->i_mode;
1200 path_put(&path);
1201 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1202 return ERR_PTR(-EINVAL);
1203
1204 if (minor & 1)
1205
1206
1207
1208
1209 return ERR_PTR(-EINVAL);
1210
1211 return get_mtd_device(NULL, minor / 2);
1212}
1213#endif
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1226{
1227 struct mtd_info *mtd;
1228 int mtd_num;
1229 char *endp;
1230
1231 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1232 if (*endp != '\0' || mtd_dev == endp) {
1233
1234
1235
1236
1237 mtd = get_mtd_device_nm(mtd_dev);
1238#ifndef __UBOOT__
1239 if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1240
1241 mtd = open_mtd_by_chdev(mtd_dev);
1242#endif
1243 } else
1244 mtd = get_mtd_device(NULL, mtd_num);
1245
1246 return mtd;
1247}
1248
1249#ifndef __UBOOT__
1250static int __init ubi_init(void)
1251#else
1252int ubi_init(void)
1253#endif
1254{
1255 int err, i, k;
1256
1257
1258 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
1259 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
1260
1261 if (mtd_devs > UBI_MAX_DEVICES) {
1262 pr_err("UBI error: too many MTD devices, maximum is %d",
1263 UBI_MAX_DEVICES);
1264 return -EINVAL;
1265 }
1266
1267
1268 err = class_register(&ubi_class);
1269 if (err < 0)
1270 return err;
1271
1272 err = misc_register(&ubi_ctrl_cdev);
1273 if (err) {
1274 pr_err("UBI error: cannot register device");
1275 goto out;
1276 }
1277
1278 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
1279 sizeof(struct ubi_wl_entry),
1280 0, 0, NULL);
1281 if (!ubi_wl_entry_slab) {
1282 err = -ENOMEM;
1283 goto out_dev_unreg;
1284 }
1285
1286 err = ubi_debugfs_init();
1287 if (err)
1288 goto out_slab;
1289
1290
1291
1292 for (i = 0; i < mtd_devs; i++) {
1293 struct mtd_dev_param *p = &mtd_dev_param[i];
1294 struct mtd_info *mtd;
1295
1296 cond_resched();
1297
1298 mtd = open_mtd_device(p->name);
1299 if (IS_ERR(mtd)) {
1300 err = PTR_ERR(mtd);
1301 pr_err("UBI error: cannot open mtd %s, error %d",
1302 p->name, err);
1303
1304 if (ubi_is_module())
1305 goto out_detach;
1306 continue;
1307 }
1308
1309 mutex_lock(&ubi_devices_mutex);
1310 err = ubi_attach_mtd_dev(mtd, p->ubi_num,
1311 p->vid_hdr_offs, p->max_beb_per1024);
1312 mutex_unlock(&ubi_devices_mutex);
1313 if (err < 0) {
1314 pr_err("UBI error: cannot attach mtd%d",
1315 mtd->index);
1316 put_mtd_device(mtd);
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331 if (ubi_is_module())
1332 goto out_detach;
1333 }
1334 }
1335
1336 err = ubiblock_init();
1337 if (err) {
1338 pr_err("UBI error: block: cannot initialize, error %d", err);
1339
1340
1341 if (ubi_is_module())
1342 goto out_detach;
1343 }
1344
1345 return 0;
1346
1347out_detach:
1348 for (k = 0; k < i; k++)
1349 if (ubi_devices[k]) {
1350 mutex_lock(&ubi_devices_mutex);
1351 ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
1352 mutex_unlock(&ubi_devices_mutex);
1353 }
1354 ubi_debugfs_exit();
1355out_slab:
1356 kmem_cache_destroy(ubi_wl_entry_slab);
1357out_dev_unreg:
1358 misc_deregister(&ubi_ctrl_cdev);
1359out:
1360#ifdef __UBOOT__
1361
1362 mtd_devs = 0;
1363#endif
1364 class_unregister(&ubi_class);
1365 pr_err("UBI error: cannot initialize UBI, error %d", err);
1366 return err;
1367}
1368late_initcall(ubi_init);
1369
1370#ifndef __UBOOT__
1371static void __exit ubi_exit(void)
1372#else
1373void ubi_exit(void)
1374#endif
1375{
1376 int i;
1377
1378 ubiblock_exit();
1379
1380 for (i = 0; i < UBI_MAX_DEVICES; i++)
1381 if (ubi_devices[i]) {
1382 mutex_lock(&ubi_devices_mutex);
1383 ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
1384 mutex_unlock(&ubi_devices_mutex);
1385 }
1386 ubi_debugfs_exit();
1387 kmem_cache_destroy(ubi_wl_entry_slab);
1388 misc_deregister(&ubi_ctrl_cdev);
1389 class_unregister(&ubi_class);
1390#ifdef __UBOOT__
1391
1392 mtd_devs = 0;
1393#endif
1394}
1395module_exit(ubi_exit);
1396
1397
1398
1399
1400
1401
1402
1403
1404static int __init bytes_str_to_int(const char *str)
1405{
1406 char *endp;
1407 unsigned long result;
1408
1409 result = simple_strtoul(str, &endp, 0);
1410 if (str == endp || result >= INT_MAX) {
1411 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1412 return -EINVAL;
1413 }
1414
1415 switch (*endp) {
1416 case 'G':
1417 result *= 1024;
1418 case 'M':
1419 result *= 1024;
1420 case 'K':
1421 result *= 1024;
1422 if (endp[1] == 'i' && endp[2] == 'B')
1423 endp += 2;
1424 case '\0':
1425 break;
1426 default:
1427 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1428 return -EINVAL;
1429 }
1430
1431 return result;
1432}
1433
1434int kstrtoint(const char *s, unsigned int base, int *res)
1435{
1436 unsigned long long tmp;
1437
1438 tmp = simple_strtoull(s, NULL, base);
1439 if (tmp != (unsigned long long)(int)tmp)
1440 return -ERANGE;
1441
1442 return (int)tmp;
1443}
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453#ifndef __UBOOT__
1454static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1455#else
1456int ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1457#endif
1458{
1459 int i, len;
1460 struct mtd_dev_param *p;
1461 char buf[MTD_PARAM_LEN_MAX];
1462 char *pbuf = &buf[0];
1463 char *tokens[MTD_PARAM_MAX_COUNT], *token;
1464
1465 if (!val)
1466 return -EINVAL;
1467
1468 if (mtd_devs == UBI_MAX_DEVICES) {
1469 pr_err("UBI error: too many parameters, max. is %d\n",
1470 UBI_MAX_DEVICES);
1471 return -EINVAL;
1472 }
1473
1474 len = strnlen(val, MTD_PARAM_LEN_MAX);
1475 if (len == MTD_PARAM_LEN_MAX) {
1476 pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1477 val, MTD_PARAM_LEN_MAX);
1478 return -EINVAL;
1479 }
1480
1481 if (len == 0) {
1482 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
1483 return 0;
1484 }
1485
1486 strcpy(buf, val);
1487
1488
1489 if (buf[len - 1] == '\n')
1490 buf[len - 1] = '\0';
1491
1492 for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
1493 tokens[i] = strsep(&pbuf, ",");
1494
1495 if (pbuf) {
1496 pr_err("UBI error: too many arguments at \"%s\"\n", val);
1497 return -EINVAL;
1498 }
1499
1500 p = &mtd_dev_param[mtd_devs];
1501 strcpy(&p->name[0], tokens[0]);
1502
1503 token = tokens[1];
1504 if (token) {
1505 p->vid_hdr_offs = bytes_str_to_int(token);
1506
1507 if (p->vid_hdr_offs < 0)
1508 return p->vid_hdr_offs;
1509 }
1510
1511 token = tokens[2];
1512 if (token) {
1513 int err = kstrtoint(token, 10, &p->max_beb_per1024);
1514
1515 if (err) {
1516 pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1517 token);
1518 return -EINVAL;
1519 }
1520 }
1521
1522 token = tokens[3];
1523 if (token) {
1524 int err = kstrtoint(token, 10, &p->ubi_num);
1525
1526 if (err) {
1527 pr_err("UBI error: bad value for ubi_num parameter: %s",
1528 token);
1529 return -EINVAL;
1530 }
1531 } else
1532 p->ubi_num = UBI_DEV_NUM_AUTO;
1533
1534 mtd_devs += 1;
1535 return 0;
1536}
1537
1538module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
1539MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
1540 "Multiple \"mtd\" parameters may be specified.\n"
1541 "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1542 "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1543 "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1544 __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
1545 "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
1546 "\n"
1547 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1548 "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
1549 "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
1550 "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
1551 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1552#ifdef CONFIG_MTD_UBI_FASTMAP
1553module_param(fm_autoconvert, bool, 0644);
1554MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
1555module_param(fm_debug, bool, 0);
1556MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
1557#endif
1558MODULE_VERSION(__stringify(UBI_VERSION));
1559MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1560MODULE_AUTHOR("Artem Bityutskiy");
1561MODULE_LICENSE("GPL");
1562