UBI: tweak volumes locking
[linux-block.git] / drivers / mtd / ubi / build.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  * Copyright (c) Nokia Corporation, 2007
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Artem Bityutskiy (Битюцкий Артём),
20  *         Frank Haverkamp
21  */
22
23 /*
24  * This file includes UBI initialization and building of UBI devices. At the
25  * moment UBI devices may only be added while UBI is initialized, but dynamic
26  * device add/remove functionality is planned. Also, at the moment we only
27  * attach UBI devices by scanning, which will become a bottleneck when flashes
28  * reach certain large size. Then one may improve UBI and add other methods.
29  */
30
31 #include <linux/err.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/stringify.h>
35 #include <linux/stat.h>
36 #include <linux/log2.h>
37 #include "ubi.h"
38
39 /* Maximum length of the 'mtd=' parameter */
40 #define MTD_PARAM_LEN_MAX 64
41
42 /**
43  * struct mtd_dev_param - MTD device parameter description data structure.
44  * @name: MTD device name or number string
45  * @vid_hdr_offs: VID header offset
46  * @data_offs: data offset
47  */
48 struct mtd_dev_param
49 {
50         char name[MTD_PARAM_LEN_MAX];
51         int vid_hdr_offs;
52         int data_offs;
53 };
54
55 /* Numbers of elements set in the @mtd_dev_param array */
56 static int mtd_devs = 0;
57
58 /* MTD devices specification parameters */
59 static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
60
61 /* All UBI devices in system */
62 struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
63
64 /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
65 struct class *ubi_class;
66
67 /* Slab cache for lock-tree entries */
68 struct kmem_cache *ubi_ltree_slab;
69
70 /* Slab cache for wear-leveling entries */
71 struct kmem_cache *ubi_wl_entry_slab;
72
73
74 /* "Show" method for files in '/<sysfs>/class/ubi/' */
75 static ssize_t ubi_version_show(struct class *class, char *buf)
76 {
77         return sprintf(buf, "%d\n", UBI_VERSION);
78 }
79
80 /* UBI version attribute ('/<sysfs>/class/ubi/version') */
81 static struct class_attribute ubi_version =
82         __ATTR(version, S_IRUGO, ubi_version_show, NULL);
83
84 static ssize_t dev_attribute_show(struct device *dev,
85                                   struct device_attribute *attr, char *buf);
86
87 /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
88 static struct device_attribute dev_eraseblock_size =
89         __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
90 static struct device_attribute dev_avail_eraseblocks =
91         __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
92 static struct device_attribute dev_total_eraseblocks =
93         __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
94 static struct device_attribute dev_volumes_count =
95         __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
96 static struct device_attribute dev_max_ec =
97         __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
98 static struct device_attribute dev_reserved_for_bad =
99         __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
100 static struct device_attribute dev_bad_peb_count =
101         __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
102 static struct device_attribute dev_max_vol_count =
103         __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
104 static struct device_attribute dev_min_io_size =
105         __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
106 static struct device_attribute dev_bgt_enabled =
107         __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
108
109 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
110 static ssize_t dev_attribute_show(struct device *dev,
111                                   struct device_attribute *attr, char *buf)
112 {
113         const struct ubi_device *ubi;
114
115         ubi = container_of(dev, struct ubi_device, dev);
116         if (attr == &dev_eraseblock_size)
117                 return sprintf(buf, "%d\n", ubi->leb_size);
118         else if (attr == &dev_avail_eraseblocks)
119                 return sprintf(buf, "%d\n", ubi->avail_pebs);
120         else if (attr == &dev_total_eraseblocks)
121                 return sprintf(buf, "%d\n", ubi->good_peb_count);
122         else if (attr == &dev_volumes_count)
123                 return sprintf(buf, "%d\n", ubi->vol_count);
124         else if (attr == &dev_max_ec)
125                 return sprintf(buf, "%d\n", ubi->max_ec);
126         else if (attr == &dev_reserved_for_bad)
127                 return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
128         else if (attr == &dev_bad_peb_count)
129                 return sprintf(buf, "%d\n", ubi->bad_peb_count);
130         else if (attr == &dev_max_vol_count)
131                 return sprintf(buf, "%d\n", ubi->vtbl_slots);
132         else if (attr == &dev_min_io_size)
133                 return sprintf(buf, "%d\n", ubi->min_io_size);
134         else if (attr == &dev_bgt_enabled)
135                 return sprintf(buf, "%d\n", ubi->thread_enabled);
136         else
137                 BUG();
138
139         return 0;
140 }
141
142 /* Fake "release" method for UBI devices */
143 static void dev_release(struct device *dev) { }
144
145 /**
146  * ubi_sysfs_init - initialize sysfs for an UBI device.
147  * @ubi: UBI device description object
148  *
149  * This function returns zero in case of success and a negative error code in
150  * case of failure.
151  */
152 static int ubi_sysfs_init(struct ubi_device *ubi)
153 {
154         int err;
155
156         ubi->dev.release = dev_release;
157         ubi->dev.devt = ubi->cdev.dev;
158         ubi->dev.class = ubi_class;
159         sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
160         err = device_register(&ubi->dev);
161         if (err)
162                 goto out;
163
164         err = device_create_file(&ubi->dev, &dev_eraseblock_size);
165         if (err)
166                 goto out_unregister;
167         err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
168         if (err)
169                 goto out_eraseblock_size;
170         err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
171         if (err)
172                 goto out_avail_eraseblocks;
173         err = device_create_file(&ubi->dev, &dev_volumes_count);
174         if (err)
175                 goto out_total_eraseblocks;
176         err = device_create_file(&ubi->dev, &dev_max_ec);
177         if (err)
178                 goto out_volumes_count;
179         err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
180         if (err)
181                 goto out_volumes_max_ec;
182         err = device_create_file(&ubi->dev, &dev_bad_peb_count);
183         if (err)
184                 goto out_reserved_for_bad;
185         err = device_create_file(&ubi->dev, &dev_max_vol_count);
186         if (err)
187                 goto out_bad_peb_count;
188         err = device_create_file(&ubi->dev, &dev_min_io_size);
189         if (err)
190                 goto out_max_vol_count;
191         err = device_create_file(&ubi->dev, &dev_bgt_enabled);
192         if (err)
193                 goto out_min_io_size;
194
195         return 0;
196
197 out_min_io_size:
198         device_remove_file(&ubi->dev, &dev_min_io_size);
199 out_max_vol_count:
200         device_remove_file(&ubi->dev, &dev_max_vol_count);
201 out_bad_peb_count:
202         device_remove_file(&ubi->dev, &dev_bad_peb_count);
203 out_reserved_for_bad:
204         device_remove_file(&ubi->dev, &dev_reserved_for_bad);
205 out_volumes_max_ec:
206         device_remove_file(&ubi->dev, &dev_max_ec);
207 out_volumes_count:
208         device_remove_file(&ubi->dev, &dev_volumes_count);
209 out_total_eraseblocks:
210         device_remove_file(&ubi->dev, &dev_total_eraseblocks);
211 out_avail_eraseblocks:
212         device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
213 out_eraseblock_size:
214         device_remove_file(&ubi->dev, &dev_eraseblock_size);
215 out_unregister:
216         device_unregister(&ubi->dev);
217 out:
218         ubi_err("failed to initialize sysfs for %s, error %d",
219                 ubi->ubi_name, err);
220         return err;
221 }
222
223 /**
224  * ubi_sysfs_close - close sysfs for an UBI device.
225  * @ubi: UBI device description object
226  */
227 static void ubi_sysfs_close(struct ubi_device *ubi)
228 {
229         device_remove_file(&ubi->dev, &dev_bgt_enabled);
230         device_remove_file(&ubi->dev, &dev_min_io_size);
231         device_remove_file(&ubi->dev, &dev_max_vol_count);
232         device_remove_file(&ubi->dev, &dev_bad_peb_count);
233         device_remove_file(&ubi->dev, &dev_reserved_for_bad);
234         device_remove_file(&ubi->dev, &dev_max_ec);
235         device_remove_file(&ubi->dev, &dev_volumes_count);
236         device_remove_file(&ubi->dev, &dev_total_eraseblocks);
237         device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
238         device_remove_file(&ubi->dev, &dev_eraseblock_size);
239         device_unregister(&ubi->dev);
240 }
241
242 /**
243  * kill_volumes - destroy all volumes.
244  * @ubi: UBI device description object
245  */
246 static void kill_volumes(struct ubi_device *ubi)
247 {
248         int i;
249
250         for (i = 0; i < ubi->vtbl_slots; i++)
251                 if (ubi->volumes[i])
252                         ubi_free_volume(ubi, ubi->volumes[i]);
253 }
254
255 /**
256  * uif_init - initialize user interfaces for an UBI device.
257  * @ubi: UBI device description object
258  *
259  * This function returns zero in case of success and a negative error code in
260  * case of failure.
261  */
262 static int uif_init(struct ubi_device *ubi)
263 {
264         int i, err;
265         dev_t dev;
266
267         mutex_init(&ubi->volumes_mutex);
268         spin_lock_init(&ubi->volumes_lock);
269
270         sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
271
272         /*
273          * Major numbers for the UBI character devices are allocated
274          * dynamically. Major numbers of volume character devices are
275          * equivalent to ones of the corresponding UBI character device. Minor
276          * numbers of UBI character devices are 0, while minor numbers of
277          * volume character devices start from 1. Thus, we allocate one major
278          * number and ubi->vtbl_slots + 1 minor numbers.
279          */
280         err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
281         if (err) {
282                 ubi_err("cannot register UBI character devices");
283                 return err;
284         }
285
286         ubi_assert(MINOR(dev) == 0);
287         cdev_init(&ubi->cdev, &ubi_cdev_operations);
288         dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
289         ubi->cdev.owner = THIS_MODULE;
290
291         err = cdev_add(&ubi->cdev, dev, 1);
292         if (err) {
293                 ubi_err("cannot add character device");
294                 goto out_unreg;
295         }
296
297         err = ubi_sysfs_init(ubi);
298         if (err)
299                 goto out_cdev;
300
301         for (i = 0; i < ubi->vtbl_slots; i++)
302                 if (ubi->volumes[i]) {
303                         err = ubi_add_volume(ubi, ubi->volumes[i]);
304                         if (err) {
305                                 ubi_err("cannot add volume %d", i);
306                                 goto out_volumes;
307                         }
308                 }
309
310         return 0;
311
312 out_volumes:
313         kill_volumes(ubi);
314         ubi_sysfs_close(ubi);
315 out_cdev:
316         cdev_del(&ubi->cdev);
317 out_unreg:
318         unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
319         ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
320         return err;
321 }
322
323 /**
324  * uif_close - close user interfaces for an UBI device.
325  * @ubi: UBI device description object
326  */
327 static void uif_close(struct ubi_device *ubi)
328 {
329         kill_volumes(ubi);
330         ubi_sysfs_close(ubi);
331         cdev_del(&ubi->cdev);
332         unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
333 }
334
335 /**
336  * attach_by_scanning - attach an MTD device using scanning method.
337  * @ubi: UBI device descriptor
338  *
339  * This function returns zero in case of success and a negative error code in
340  * case of failure.
341  *
342  * Note, currently this is the only method to attach UBI devices. Hopefully in
343  * the future we'll have more scalable attaching methods and avoid full media
344  * scanning. But even in this case scanning will be needed as a fall-back
345  * attaching method if there are some on-flash table corruptions.
346  */
347 static int attach_by_scanning(struct ubi_device *ubi)
348 {
349         int err;
350         struct ubi_scan_info *si;
351
352         si = ubi_scan(ubi);
353         if (IS_ERR(si))
354                 return PTR_ERR(si);
355
356         ubi->bad_peb_count = si->bad_peb_count;
357         ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
358         ubi->max_ec = si->max_ec;
359         ubi->mean_ec = si->mean_ec;
360
361         err = ubi_read_volume_table(ubi, si);
362         if (err)
363                 goto out_si;
364
365         err = ubi_wl_init_scan(ubi, si);
366         if (err)
367                 goto out_vtbl;
368
369         err = ubi_eba_init_scan(ubi, si);
370         if (err)
371                 goto out_wl;
372
373         ubi_scan_destroy_si(si);
374         return 0;
375
376 out_wl:
377         ubi_wl_close(ubi);
378 out_vtbl:
379         vfree(ubi->vtbl);
380 out_si:
381         ubi_scan_destroy_si(si);
382         return err;
383 }
384
385 /**
386  * io_init - initialize I/O unit for a given UBI device.
387  * @ubi: UBI device description object
388  *
389  * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
390  * assumed:
391  *   o EC header is always at offset zero - this cannot be changed;
392  *   o VID header starts just after the EC header at the closest address
393  *   aligned to @io->@hdrs_min_io_size;
394  *   o data starts just after the VID header at the closest address aligned to
395  *     @io->@min_io_size
396  *
397  * This function returns zero in case of success and a negative error code in
398  * case of failure.
399  */
400 static int io_init(struct ubi_device *ubi)
401 {
402         if (ubi->mtd->numeraseregions != 0) {
403                 /*
404                  * Some flashes have several erase regions. Different regions
405                  * may have different eraseblock size and other
406                  * characteristics. It looks like mostly multi-region flashes
407                  * have one "main" region and one or more small regions to
408                  * store boot loader code or boot parameters or whatever. I
409                  * guess we should just pick the largest region. But this is
410                  * not implemented.
411                  */
412                 ubi_err("multiple regions, not implemented");
413                 return -EINVAL;
414         }
415
416         /*
417          * Note, in this implementation we support MTD devices with 0x7FFFFFFF
418          * physical eraseblocks maximum.
419          */
420
421         ubi->peb_size   = ubi->mtd->erasesize;
422         ubi->peb_count  = ubi->mtd->size / ubi->mtd->erasesize;
423         ubi->flash_size = ubi->mtd->size;
424
425         if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
426                 ubi->bad_allowed = 1;
427
428         ubi->min_io_size = ubi->mtd->writesize;
429         ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
430
431         /* Make sure minimal I/O unit is power of 2 */
432         if (!is_power_of_2(ubi->min_io_size)) {
433                 ubi_err("min. I/O unit (%d) is not power of 2",
434                         ubi->min_io_size);
435                 return -EINVAL;
436         }
437
438         ubi_assert(ubi->hdrs_min_io_size > 0);
439         ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
440         ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
441
442         /* Calculate default aligned sizes of EC and VID headers */
443         ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
444         ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
445
446         dbg_msg("min_io_size      %d", ubi->min_io_size);
447         dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
448         dbg_msg("ec_hdr_alsize    %d", ubi->ec_hdr_alsize);
449         dbg_msg("vid_hdr_alsize   %d", ubi->vid_hdr_alsize);
450
451         if (ubi->vid_hdr_offset == 0)
452                 /* Default offset */
453                 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
454                                       ubi->ec_hdr_alsize;
455         else {
456                 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
457                                                 ~(ubi->hdrs_min_io_size - 1);
458                 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
459                                                 ubi->vid_hdr_aloffset;
460         }
461
462         /* Similar for the data offset */
463         if (ubi->leb_start == 0) {
464                 ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
465                 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
466         }
467
468         dbg_msg("vid_hdr_offset   %d", ubi->vid_hdr_offset);
469         dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
470         dbg_msg("vid_hdr_shift    %d", ubi->vid_hdr_shift);
471         dbg_msg("leb_start        %d", ubi->leb_start);
472
473         /* The shift must be aligned to 32-bit boundary */
474         if (ubi->vid_hdr_shift % 4) {
475                 ubi_err("unaligned VID header shift %d",
476                         ubi->vid_hdr_shift);
477                 return -EINVAL;
478         }
479
480         /* Check sanity */
481         if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
482             ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
483             ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
484             ubi->leb_start % ubi->min_io_size) {
485                 ubi_err("bad VID header (%d) or data offsets (%d)",
486                         ubi->vid_hdr_offset, ubi->leb_start);
487                 return -EINVAL;
488         }
489
490         /*
491          * It may happen that EC and VID headers are situated in one minimal
492          * I/O unit. In this case we can only accept this UBI image in
493          * read-only mode.
494          */
495         if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
496                 ubi_warn("EC and VID headers are in the same minimal I/O unit, "
497                          "switch to read-only mode");
498                 ubi->ro_mode = 1;
499         }
500
501         ubi->leb_size = ubi->peb_size - ubi->leb_start;
502
503         if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
504                 ubi_msg("MTD device %d is write-protected, attach in "
505                         "read-only mode", ubi->mtd->index);
506                 ubi->ro_mode = 1;
507         }
508
509         dbg_msg("leb_size         %d", ubi->leb_size);
510         dbg_msg("ro_mode          %d", ubi->ro_mode);
511
512         /*
513          * Note, ideally, we have to initialize ubi->bad_peb_count here. But
514          * unfortunately, MTD does not provide this information. We should loop
515          * over all physical eraseblocks and invoke mtd->block_is_bad() for
516          * each physical eraseblock. So, we skip ubi->bad_peb_count
517          * uninitialized and initialize it after scanning.
518          */
519
520         return 0;
521 }
522
523 /**
524  * attach_mtd_dev - attach an MTD device.
525  * @mtd_dev: MTD device name or number string
526  * @vid_hdr_offset: VID header offset
527  * @data_offset: data offset
528  *
529  * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
530  * MTD device name, and tries to open it by this name. If it is unable to open,
531  * it tries to convert @mtd_dev to an integer and open the MTD device by its
532  * number. Returns zero in case of success and a negative error code in case of
533  * failure.
534  */
535 static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
536                           int data_offset)
537 {
538         struct ubi_device *ubi;
539         struct mtd_info *mtd;
540         int i, err;
541
542         mtd = get_mtd_device_nm(mtd_dev);
543         if (IS_ERR(mtd)) {
544                 int mtd_num;
545                 char *endp;
546
547                 if (PTR_ERR(mtd) != -ENODEV)
548                         return PTR_ERR(mtd);
549
550                 /*
551                  * Probably this is not MTD device name but MTD device number -
552                  * check this out.
553                  */
554                 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
555                 if (*endp != '\0' || mtd_dev == endp) {
556                         ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
557                         return -ENODEV;
558                 }
559
560                 mtd = get_mtd_device(NULL, mtd_num);
561                 if (IS_ERR(mtd))
562                         return PTR_ERR(mtd);
563         }
564
565         /* Check if we already have the same MTD device attached */
566         for (i = 0; i < UBI_MAX_DEVICES; i++)
567                 ubi = ubi_devices[i];
568                 if (ubi && ubi->mtd->index == mtd->index) {
569                         ubi_err("mtd%d is already attached to ubi%d",
570                                 mtd->index, i);
571                         err = -EINVAL;
572                         goto out_mtd;
573                 }
574
575         ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
576         if (!ubi) {
577                 err = -ENOMEM;
578                 goto out_mtd;
579         }
580
581         ubi->mtd = mtd;
582
583         /* Search for an empty slot in the @ubi_devices array */
584         ubi->ubi_num = -1;
585         for (i = 0; i < UBI_MAX_DEVICES; i++)
586                 if (!ubi_devices[i]) {
587                         ubi->ubi_num = i;
588                         break;
589                 }
590
591         if (ubi->ubi_num == -1) {
592                 ubi_err("only %d UBI devices may be created", UBI_MAX_DEVICES);
593                 err = -ENFILE;
594                 goto out_free;
595         }
596
597         dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
598                 ubi->mtd->index, ubi->ubi_num, vid_hdr_offset, data_offset);
599
600         ubi->vid_hdr_offset = vid_hdr_offset;
601         ubi->leb_start = data_offset;
602         err = io_init(ubi);
603         if (err)
604                 goto out_free;
605
606         mutex_init(&ubi->buf_mutex);
607         ubi->peb_buf1 = vmalloc(ubi->peb_size);
608         if (!ubi->peb_buf1)
609                 goto out_free;
610
611         ubi->peb_buf2 = vmalloc(ubi->peb_size);
612         if (!ubi->peb_buf2)
613                  goto out_free;
614
615 #ifdef CONFIG_MTD_UBI_DEBUG
616         mutex_init(&ubi->dbg_buf_mutex);
617         ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
618         if (!ubi->dbg_peb_buf)
619                  goto out_free;
620 #endif
621
622         err = attach_by_scanning(ubi);
623         if (err) {
624                 dbg_err("failed to attach by scanning, error %d", err);
625                 goto out_free;
626         }
627
628         err = uif_init(ubi);
629         if (err)
630                 goto out_detach;
631
632         ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi->ubi_num);
633         ubi_msg("MTD device name:            \"%s\"", ubi->mtd->name);
634         ubi_msg("MTD device size:            %llu MiB", ubi->flash_size >> 20);
635         ubi_msg("physical eraseblock size:   %d bytes (%d KiB)",
636                 ubi->peb_size, ubi->peb_size >> 10);
637         ubi_msg("logical eraseblock size:    %d bytes", ubi->leb_size);
638         ubi_msg("number of good PEBs:        %d", ubi->good_peb_count);
639         ubi_msg("number of bad PEBs:         %d", ubi->bad_peb_count);
640         ubi_msg("smallest flash I/O unit:    %d", ubi->min_io_size);
641         ubi_msg("VID header offset:          %d (aligned %d)",
642                 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
643         ubi_msg("data offset:                %d", ubi->leb_start);
644         ubi_msg("max. allowed volumes:       %d", ubi->vtbl_slots);
645         ubi_msg("wear-leveling threshold:    %d", CONFIG_MTD_UBI_WL_THRESHOLD);
646         ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
647         ubi_msg("number of user volumes:     %d",
648                 ubi->vol_count - UBI_INT_VOL_COUNT);
649         ubi_msg("available PEBs:             %d", ubi->avail_pebs);
650         ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
651         ubi_msg("number of PEBs reserved for bad PEB handling: %d",
652                 ubi->beb_rsvd_pebs);
653         ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
654
655         /* Enable the background thread */
656         if (!DBG_DISABLE_BGT) {
657                 ubi->thread_enabled = 1;
658                 wake_up_process(ubi->bgt_thread);
659         }
660
661         ubi_devices[ubi->ubi_num] = ubi;
662         return 0;
663
664 out_detach:
665         ubi_eba_close(ubi);
666         ubi_wl_close(ubi);
667         vfree(ubi->vtbl);
668 out_free:
669         vfree(ubi->peb_buf1);
670         vfree(ubi->peb_buf2);
671 #ifdef CONFIG_MTD_UBI_DEBUG
672         vfree(ubi->dbg_peb_buf);
673 #endif
674         kfree(ubi);
675 out_mtd:
676         put_mtd_device(mtd);
677         return err;
678 }
679
680 /**
681  * detach_mtd_dev - detach an MTD device.
682  * @ubi: UBI device description object
683  */
684 static void detach_mtd_dev(struct ubi_device *ubi)
685 {
686         int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
687
688         dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
689         uif_close(ubi);
690         ubi_eba_close(ubi);
691         ubi_wl_close(ubi);
692         vfree(ubi->vtbl);
693         put_mtd_device(ubi->mtd);
694         vfree(ubi->peb_buf1);
695         vfree(ubi->peb_buf2);
696 #ifdef CONFIG_MTD_UBI_DEBUG
697         vfree(ubi->dbg_peb_buf);
698 #endif
699         kfree(ubi_devices[ubi_num]);
700         ubi_devices[ubi_num] = NULL;
701         ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
702 }
703
704 /**
705  * ltree_entry_ctor - lock tree entries slab cache constructor.
706  * @obj: the lock-tree entry to construct
707  * @cache: the lock tree entry slab cache
708  * @flags: constructor flags
709  */
710 static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
711 {
712         struct ubi_ltree_entry *le = obj;
713
714         le->users = 0;
715         init_rwsem(&le->mutex);
716 }
717
718 static int __init ubi_init(void)
719 {
720         int err, i, k;
721
722         /* Ensure that EC and VID headers have correct size */
723         BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
724         BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
725
726         if (mtd_devs > UBI_MAX_DEVICES) {
727                 printk("UBI error: too many MTD devices, maximum is %d\n",
728                        UBI_MAX_DEVICES);
729                 return -EINVAL;
730         }
731
732         ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
733         if (IS_ERR(ubi_class))
734                 return PTR_ERR(ubi_class);
735
736         err = class_create_file(ubi_class, &ubi_version);
737         if (err)
738                 goto out_class;
739
740         ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",
741                                            sizeof(struct ubi_ltree_entry), 0,
742                                            0, &ltree_entry_ctor);
743         if (!ubi_ltree_slab)
744                 goto out_version;
745
746         ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
747                                                 sizeof(struct ubi_wl_entry),
748                                                 0, 0, NULL);
749         if (!ubi_wl_entry_slab)
750                 goto out_ltree;
751
752         /* Attach MTD devices */
753         for (i = 0; i < mtd_devs; i++) {
754                 struct mtd_dev_param *p = &mtd_dev_param[i];
755
756                 cond_resched();
757                 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
758                 if (err)
759                         goto out_detach;
760         }
761
762         return 0;
763
764 out_detach:
765         for (k = 0; k < i; k++)
766                 detach_mtd_dev(ubi_devices[k]);
767         kmem_cache_destroy(ubi_wl_entry_slab);
768 out_ltree:
769         kmem_cache_destroy(ubi_ltree_slab);
770 out_version:
771         class_remove_file(ubi_class, &ubi_version);
772 out_class:
773         class_destroy(ubi_class);
774         return err;
775 }
776 module_init(ubi_init);
777
778 static void __exit ubi_exit(void)
779 {
780         int i;
781
782         for (i = 0; i < UBI_MAX_DEVICES; i++)
783                 if (ubi_devices[i])
784                         detach_mtd_dev(ubi_devices[i]);
785         kmem_cache_destroy(ubi_wl_entry_slab);
786         kmem_cache_destroy(ubi_ltree_slab);
787         class_remove_file(ubi_class, &ubi_version);
788         class_destroy(ubi_class);
789 }
790 module_exit(ubi_exit);
791
792 /**
793  * bytes_str_to_int - convert a string representing number of bytes to an
794  * integer.
795  * @str: the string to convert
796  *
797  * This function returns positive resulting integer in case of success and a
798  * negative error code in case of failure.
799  */
800 static int __init bytes_str_to_int(const char *str)
801 {
802         char *endp;
803         unsigned long result;
804
805         result = simple_strtoul(str, &endp, 0);
806         if (str == endp || result < 0) {
807                 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
808                 return -EINVAL;
809         }
810
811         switch (*endp) {
812         case 'G':
813                 result *= 1024;
814         case 'M':
815                 result *= 1024;
816         case 'K':
817         case 'k':
818                 result *= 1024;
819                 if (endp[1] == 'i' && (endp[2] == '\0' ||
820                           endp[2] == 'B'  || endp[2] == 'b'))
821                         endp += 2;
822         case '\0':
823                 break;
824         default:
825                 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
826                 return -EINVAL;
827         }
828
829         return result;
830 }
831
832 /**
833  * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
834  * @val: the parameter value to parse
835  * @kp: not used
836  *
837  * This function returns zero in case of success and a negative error code in
838  * case of error.
839  */
840 static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
841 {
842         int i, len;
843         struct mtd_dev_param *p;
844         char buf[MTD_PARAM_LEN_MAX];
845         char *pbuf = &buf[0];
846         char *tokens[3] = {NULL, NULL, NULL};
847
848         if (!val)
849                 return -EINVAL;
850
851         if (mtd_devs == UBI_MAX_DEVICES) {
852                 printk("UBI error: too many parameters, max. is %d\n",
853                        UBI_MAX_DEVICES);
854                 return -EINVAL;
855         }
856
857         len = strnlen(val, MTD_PARAM_LEN_MAX);
858         if (len == MTD_PARAM_LEN_MAX) {
859                 printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
860                        val, MTD_PARAM_LEN_MAX);
861                 return -EINVAL;
862         }
863
864         if (len == 0) {
865                 printk("UBI warning: empty 'mtd=' parameter - ignored\n");
866                 return 0;
867         }
868
869         strcpy(buf, val);
870
871         /* Get rid of the final newline */
872         if (buf[len - 1] == '\n')
873                 buf[len - 1] = '\0';
874
875         for (i = 0; i < 3; i++)
876                 tokens[i] = strsep(&pbuf, ",");
877
878         if (pbuf) {
879                 printk("UBI error: too many arguments at \"%s\"\n", val);
880                 return -EINVAL;
881         }
882
883         p = &mtd_dev_param[mtd_devs];
884         strcpy(&p->name[0], tokens[0]);
885
886         if (tokens[1])
887                 p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
888         if (tokens[2])
889                 p->data_offs = bytes_str_to_int(tokens[2]);
890
891         if (p->vid_hdr_offs < 0)
892                 return p->vid_hdr_offs;
893         if (p->data_offs < 0)
894                 return p->data_offs;
895
896         mtd_devs += 1;
897         return 0;
898 }
899
900 module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
901 MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
902                       "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
903                       "Multiple \"mtd\" parameters may be specified.\n"
904                       "MTD devices may be specified by their number or name. "
905                       "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
906                       "specify UBI VID header position and data starting "
907                       "position to be used by UBI.\n"
908                       "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
909                       "with name content using VID header offset 1984 and data "
910                       "start 2048, and MTD device number 4 using default "
911                       "offsets");
912
913 MODULE_VERSION(__stringify(UBI_VERSION));
914 MODULE_DESCRIPTION("UBI - Unsorted Block Images");
915 MODULE_AUTHOR("Artem Bityutskiy");
916 MODULE_LICENSE("GPL");