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