2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2007
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.
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.
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
19 * Author: Artem Bityutskiy (Битюцкий Артём),
24 * This file includes UBI initialization and building of UBI devices.
26 * When UBI is initialized, it attaches all the MTD devices specified as the
27 * module load parameters or the kernel boot parameters. If MTD devices were
28 * specified, UBI does not attach any MTD device, but it is possible to do
29 * later using the "UBI control device".
32 #include <linux/err.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/stringify.h>
36 #include <linux/namei.h>
37 #include <linux/stat.h>
38 #include <linux/miscdevice.h>
39 #include <linux/mtd/partitions.h>
40 #include <linux/log2.h>
41 #include <linux/kthread.h>
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
45 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
46 #include <linux/mtd/combo_nand.h>
49 /* Maximum length of the 'mtd=' parameter */
50 #define MTD_PARAM_LEN_MAX 64
52 /* Maximum number of comma-separated items in the 'mtd=' parameter */
53 #define MTD_PARAM_MAX_COUNT 3
55 /* Maximum value for the number of bad PEBs per 1024 PEBs */
56 #define MAX_MTD_UBI_BEB_LIMIT 768
58 #ifdef CONFIG_MTD_UBI_MODULE
59 #define ubi_is_module() 1
61 #define ubi_is_module() 0
65 * struct mtd_dev_param - MTD device parameter description data structure.
66 * @name: MTD character device node path, MTD device name, or MTD device number
68 * @vid_hdr_offs: VID header offset
69 * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
71 struct mtd_dev_param
{
72 char name
[MTD_PARAM_LEN_MAX
];
77 /* Numbers of elements set in the @mtd_dev_param array */
78 static int __initdata mtd_devs
;
80 /* MTD devices specification parameters */
81 static struct mtd_dev_param __initdata mtd_dev_param
[UBI_MAX_DEVICES
];
82 #ifdef CONFIG_MTD_UBI_FASTMAP
83 /* UBI module parameter to enable fastmap automatically on non-fastmap images */
84 #ifdef CONFIG_MTK_NAND_UBIFS_FASTMAP_SUPPORT
85 static bool fm_autoconvert
= 1;
87 static bool fm_autoconvert
= 0;
90 /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
91 struct class *ubi_class
;
93 /* Slab cache for wear-leveling entries */
94 struct kmem_cache
*ubi_wl_entry_slab
;
96 /* UBI control character device */
97 static struct miscdevice ubi_ctrl_cdev
= {
98 .minor
= MISC_DYNAMIC_MINOR
,
100 .fops
= &ubi_ctrl_cdev_operations
,
103 /* All UBI devices in system */
104 static struct ubi_device
*ubi_devices
[UBI_MAX_DEVICES
];
106 /* Serializes UBI devices creations and removals */
107 DEFINE_MUTEX(ubi_devices_mutex
);
109 /* Protects @ubi_devices and @ubi->ref_count */
110 static DEFINE_SPINLOCK(ubi_devices_lock
);
112 /* "Show" method for files in '/<sysfs>/class/ubi/' */
113 static ssize_t
ubi_version_show(struct class *class,
114 struct class_attribute
*attr
, char *buf
)
116 return sprintf(buf
, "%d\n", UBI_VERSION
);
119 /* UBI version attribute ('/<sysfs>/class/ubi/version') */
120 static struct class_attribute ubi_version
=
121 __ATTR(version
, S_IRUGO
, ubi_version_show
, NULL
);
123 static ssize_t
dev_attribute_show(struct device
*dev
,
124 struct device_attribute
*attr
, char *buf
);
126 static ssize_t
dev_attribute_store(struct device
*dev
, struct device_attribute
*attr
,
127 const char *buf
, size_t count
);
129 /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
130 static struct device_attribute dev_eraseblock_size
=
131 __ATTR(eraseblock_size
, S_IRUGO
, dev_attribute_show
, NULL
);
132 static struct device_attribute dev_avail_eraseblocks
=
133 __ATTR(avail_eraseblocks
, S_IRUGO
, dev_attribute_show
, NULL
);
134 static struct device_attribute dev_total_eraseblocks
=
135 __ATTR(total_eraseblocks
, S_IRUGO
, dev_attribute_show
, NULL
);
136 static struct device_attribute dev_volumes_count
=
137 __ATTR(volumes_count
, S_IRUGO
, dev_attribute_show
, NULL
);
138 static struct device_attribute dev_max_ec
=
139 __ATTR(max_ec
, S_IRUGO
, dev_attribute_show
, NULL
);
141 static struct device_attribute dev_lbb
=
142 __ATTR(lbb
, S_IRUGO
, dev_attribute_show
, NULL
);
143 static struct device_attribute dev_move_retry
=
144 __ATTR(move_retry
, S_IRUGO
, dev_attribute_show
, NULL
);
145 static struct device_attribute dev_ec_count
=
146 __ATTR(ec_count
, S_IRUGO
, dev_attribute_show
, NULL
);
147 static struct device_attribute dev_mean_ec
=
148 __ATTR(mean_ec
, S_IRUGO
, dev_attribute_show
, NULL
);
149 static struct device_attribute dev_ec_sum
=
150 __ATTR(ec_sum
, S_IRUGO
, dev_attribute_show
, NULL
);
151 static struct device_attribute dev_min_ec
=
152 __ATTR(min_ec
, S_IRUGO
, dev_attribute_show
, NULL
);
153 static struct device_attribute dev_wl_count
=
154 __ATTR(wl_count
, S_IRUGO
, dev_attribute_show
, NULL
);
155 static struct device_attribute dev_wl_size
=
156 __ATTR(wl_size
, S_IRUGO
, dev_attribute_show
, NULL
);
157 static struct device_attribute dev_scrub_count
=
158 __ATTR(scrub_count
, S_IRUGO
, dev_attribute_show
, NULL
);
159 static struct device_attribute dev_scrub_size
=
160 __ATTR(scrub_size
, S_IRUGO
, dev_attribute_show
, NULL
);
161 static struct device_attribute dev_wl_th
=
162 __ATTR(wl_th
, 00755, dev_attribute_show
, dev_attribute_store
);
163 static struct device_attribute dev_torture
=
164 __ATTR(torture
, 00755, dev_attribute_show
, NULL
);
166 static struct device_attribute dev_reserved_for_bad
=
167 __ATTR(reserved_for_bad
, S_IRUGO
, dev_attribute_show
, NULL
);
168 static struct device_attribute dev_bad_peb_count
=
169 __ATTR(bad_peb_count
, S_IRUGO
, dev_attribute_show
, NULL
);
170 static struct device_attribute dev_max_vol_count
=
171 __ATTR(max_vol_count
, S_IRUGO
, dev_attribute_show
, NULL
);
172 static struct device_attribute dev_min_io_size
=
173 __ATTR(min_io_size
, S_IRUGO
, dev_attribute_show
, NULL
);
174 static struct device_attribute dev_bgt_enabled
=
175 __ATTR(bgt_enabled
, S_IRUGO
, dev_attribute_show
, NULL
);
176 static struct device_attribute dev_mtd_num
=
177 __ATTR(mtd_num
, S_IRUGO
, dev_attribute_show
, NULL
);
180 * ubi_volume_notify - send a volume change notification.
181 * @ubi: UBI device description object
182 * @vol: volume description object of the changed volume
183 * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
185 * This is a helper function which notifies all subscribers about a volume
186 * change event (creation, removal, re-sizing, re-naming, updating). Returns
187 * zero in case of success and a negative error code in case of failure.
189 int ubi_volume_notify(struct ubi_device
*ubi
, struct ubi_volume
*vol
, int ntype
)
191 struct ubi_notification nt
;
193 ubi_do_get_device_info(ubi
, &nt
.di
);
194 ubi_do_get_volume_info(ubi
, vol
, &nt
.vi
);
196 #ifdef CONFIG_MTD_UBI_FASTMAP
198 case UBI_VOLUME_ADDED
:
199 case UBI_VOLUME_REMOVED
:
200 case UBI_VOLUME_RESIZED
:
201 case UBI_VOLUME_RENAMED
:
202 if (ubi_update_fastmap(ubi
)) {
203 ubi_err("Unable to update fastmap!");
208 return blocking_notifier_call_chain(&ubi_notifiers
, ntype
, &nt
);
212 * ubi_notify_all - send a notification to all volumes.
213 * @ubi: UBI device description object
214 * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
215 * @nb: the notifier to call
217 * This function walks all volumes of UBI device @ubi and sends the @ntype
218 * notification for each volume. If @nb is %NULL, then all registered notifiers
219 * are called, otherwise only the @nb notifier is called. Returns the number of
220 * sent notifications.
222 int ubi_notify_all(struct ubi_device
*ubi
, int ntype
, struct notifier_block
*nb
)
224 struct ubi_notification nt
;
227 ubi_do_get_device_info(ubi
, &nt
.di
);
229 mutex_lock(&ubi
->device_mutex
);
230 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
232 * Since the @ubi->device is locked, and we are not going to
233 * change @ubi->volumes, we do not have to lock
234 * @ubi->volumes_lock.
236 if (!ubi
->volumes
[i
])
239 ubi_do_get_volume_info(ubi
, ubi
->volumes
[i
], &nt
.vi
);
241 nb
->notifier_call(nb
, ntype
, &nt
);
243 blocking_notifier_call_chain(&ubi_notifiers
, ntype
,
247 mutex_unlock(&ubi
->device_mutex
);
253 * ubi_enumerate_volumes - send "add" notification for all existing volumes.
254 * @nb: the notifier to call
256 * This function walks all UBI devices and volumes and sends the
257 * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
258 * registered notifiers are called, otherwise only the @nb notifier is called.
259 * Returns the number of sent notifications.
261 int ubi_enumerate_volumes(struct notifier_block
*nb
)
266 * Since the @ubi_devices_mutex is locked, and we are not going to
267 * change @ubi_devices, we do not have to lock @ubi_devices_lock.
269 for (i
= 0; i
< UBI_MAX_DEVICES
; i
++) {
270 struct ubi_device
*ubi
= ubi_devices
[i
];
274 count
+= ubi_notify_all(ubi
, UBI_VOLUME_ADDED
, nb
);
281 * ubi_get_device - get UBI device.
282 * @ubi_num: UBI device number
284 * This function returns UBI device description object for UBI device number
285 * @ubi_num, or %NULL if the device does not exist. This function increases the
286 * device reference count to prevent removal of the device. In other words, the
287 * device cannot be removed if its reference count is not zero.
289 struct ubi_device
*ubi_get_device(int ubi_num
)
291 struct ubi_device
*ubi
;
293 spin_lock(&ubi_devices_lock
);
294 ubi
= ubi_devices
[ubi_num
];
296 ubi_assert(ubi
->ref_count
>= 0);
298 get_device(&ubi
->dev
);
300 spin_unlock(&ubi_devices_lock
);
306 * ubi_put_device - drop an UBI device reference.
307 * @ubi: UBI device description object
309 void ubi_put_device(struct ubi_device
*ubi
)
311 spin_lock(&ubi_devices_lock
);
313 put_device(&ubi
->dev
);
314 spin_unlock(&ubi_devices_lock
);
318 * ubi_get_by_major - get UBI device by character device major number.
319 * @major: major number
321 * This function is similar to 'ubi_get_device()', but it searches the device
322 * by its major number.
324 struct ubi_device
*ubi_get_by_major(int major
)
327 struct ubi_device
*ubi
;
329 spin_lock(&ubi_devices_lock
);
330 for (i
= 0; i
< UBI_MAX_DEVICES
; i
++) {
331 ubi
= ubi_devices
[i
];
332 if (ubi
&& MAJOR(ubi
->cdev
.dev
) == major
) {
333 ubi_assert(ubi
->ref_count
>= 0);
335 get_device(&ubi
->dev
);
336 spin_unlock(&ubi_devices_lock
);
340 spin_unlock(&ubi_devices_lock
);
346 * ubi_major2num - get UBI device number by character device major number.
347 * @major: major number
349 * This function searches UBI device number object by its major number. If UBI
350 * device was not found, this function returns -ENODEV, otherwise the UBI device
351 * number is returned.
353 int ubi_major2num(int major
)
355 int i
, ubi_num
= -ENODEV
;
357 spin_lock(&ubi_devices_lock
);
358 for (i
= 0; i
< UBI_MAX_DEVICES
; i
++) {
359 struct ubi_device
*ubi
= ubi_devices
[i
];
361 if (ubi
&& MAJOR(ubi
->cdev
.dev
) == major
) {
362 ubi_num
= ubi
->ubi_num
;
366 spin_unlock(&ubi_devices_lock
);
371 /* MTK: "Store" method for files in '/<sysfs>/class/ubi/ubiX/' */
372 static ssize_t
dev_attribute_store(struct device
*dev
, struct device_attribute
*attr
,
373 const char *buf
, size_t count
)
375 struct ubi_device
*ubi
;
378 ubi
= container_of(dev
, struct ubi_device
, dev
);
379 ubi
= ubi_get_device(ubi
->ubi_num
);
383 if (attr
== &dev_wl_th
)
385 sscanf(buf
, "%d", &th
);
386 printk("set th=%d\n", th
);
391 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
392 static ssize_t
dev_attribute_show(struct device
*dev
,
393 struct device_attribute
*attr
, char *buf
)
396 struct ubi_device
*ubi
;
399 * The below code looks weird, but it actually makes sense. We get the
400 * UBI device reference from the contained 'struct ubi_device'. But it
401 * is unclear if the device was removed or not yet. Indeed, if the
402 * device was removed before we increased its reference count,
403 * 'ubi_get_device()' will return -ENODEV and we fail.
405 * Remember, 'struct ubi_device' is freed in the release function, so
406 * we still can use 'ubi->ubi_num'.
408 ubi
= container_of(dev
, struct ubi_device
, dev
);
409 ubi
= ubi_get_device(ubi
->ubi_num
);
413 if (attr
== &dev_eraseblock_size
)
414 ret
= sprintf(buf
, "%d\n", ubi
->leb_size
);
415 else if (attr
== &dev_avail_eraseblocks
)
416 ret
= sprintf(buf
, "%d\n", ubi
->avail_pebs
);
417 else if (attr
== &dev_total_eraseblocks
)
418 ret
= sprintf(buf
, "%d\n", ubi
->good_peb_count
);
419 else if (attr
== &dev_volumes_count
)
420 ret
= sprintf(buf
, "%d\n", ubi
->vol_count
- UBI_INT_VOL_COUNT
);
421 else if (attr
== &dev_max_ec
)
422 ret
= sprintf(buf
, "%d\n", ubi
->max_ec
);
424 else if (attr
== &dev_torture
)
425 ret
= sprintf(buf
, "torture: %d\n", ubi
->torture
);
426 else if (attr
== &dev_wl_th
)
427 ret
= sprintf(buf
, "wl_th: %d\n", ubi
->wl_th
);
428 else if (attr
== &dev_wl_count
)
429 ret
= sprintf(buf
, "wl_count: %d\n", ubi
->wl_count
);
430 else if (attr
== &dev_wl_size
)
431 ret
= sprintf(buf
, "wl_size: %lld\n", ubi
->wl_size
);
432 else if (attr
== &dev_scrub_count
)
433 ret
= sprintf(buf
, "scrub_count: %d\n", ubi
->scrub_count
);
434 else if (attr
== &dev_scrub_size
)
435 ret
= sprintf(buf
, "scrub_size: %lld\n", ubi
->scrub_size
);
436 else if (attr
== &dev_move_retry
)
437 ret
= sprintf(buf
, "move_retry: %d\n", atomic_read(&ubi
->move_retry
));
438 else if (attr
== &dev_lbb
)
439 ret
= sprintf(buf
, "lbb: %d\n", atomic_read(&ubi
->lbb
));
440 else if (attr
== &dev_ec_count
)
441 ret
= sprintf(buf
, "ec_count: %d\n", atomic_read(&ubi
->ec_count
));
442 else if (attr
== &dev_mean_ec
)
443 ret
= sprintf(buf
, "mean_ec: %d\n", ubi
->mean_ec
);
444 else if (attr
== &dev_ec_sum
)
445 ret
= sprintf(buf
, "%lld\n", ubi
->ec_sum
);
446 else if (attr
== &dev_min_ec
) {
447 struct ubi_wl_entry
*e
=NULL
, *efree
=NULL
, *eused
=NULL
;
448 spin_lock(&ubi
->wl_lock
);
449 efree
= rb_entry(rb_first(&ubi
->free
), struct ubi_wl_entry
, u
.rb
);
450 eused
= rb_entry(rb_first(&ubi
->used
), struct ubi_wl_entry
, u
.rb
);
452 if(efree
->ec
< eused
->ec
)
461 ret
= sprintf(buf
, "%d\n", e
->ec
);
462 spin_unlock(&ubi
->wl_lock
);
465 else if (attr
== &dev_reserved_for_bad
)
466 ret
= sprintf(buf
, "%d\n", ubi
->beb_rsvd_pebs
);
467 else if (attr
== &dev_bad_peb_count
)
468 ret
= sprintf(buf
, "%d\n", ubi
->bad_peb_count
);
469 else if (attr
== &dev_max_vol_count
)
470 ret
= sprintf(buf
, "%d\n", ubi
->vtbl_slots
);
471 else if (attr
== &dev_min_io_size
)
472 ret
= sprintf(buf
, "%d\n", ubi
->min_io_size
);
473 else if (attr
== &dev_bgt_enabled
)
474 ret
= sprintf(buf
, "%d\n", ubi
->thread_enabled
);
475 else if (attr
== &dev_mtd_num
)
476 ret
= sprintf(buf
, "%d\n", ubi
->mtd
->index
);
484 static void dev_release(struct device
*dev
)
486 struct ubi_device
*ubi
= container_of(dev
, struct ubi_device
, dev
);
492 * ubi_sysfs_init - initialize sysfs for an UBI device.
493 * @ubi: UBI device description object
494 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
497 * This function returns zero in case of success and a negative error code in
500 static int ubi_sysfs_init(struct ubi_device
*ubi
, int *ref
)
504 ubi
->dev
.release
= dev_release
;
505 ubi
->dev
.devt
= ubi
->cdev
.dev
;
506 ubi
->dev
.class = ubi_class
;
507 dev_set_name(&ubi
->dev
, UBI_NAME_STR
"%d", ubi
->ubi_num
);
508 err
= device_register(&ubi
->dev
);
513 err
= device_create_file(&ubi
->dev
, &dev_eraseblock_size
);
516 err
= device_create_file(&ubi
->dev
, &dev_avail_eraseblocks
);
519 err
= device_create_file(&ubi
->dev
, &dev_total_eraseblocks
);
522 err
= device_create_file(&ubi
->dev
, &dev_volumes_count
);
525 err
= device_create_file(&ubi
->dev
, &dev_max_ec
);
529 err
= device_create_file(&ubi
->dev
, &dev_lbb
);
532 err
= device_create_file(&ubi
->dev
, &dev_move_retry
);
535 err
= device_create_file(&ubi
->dev
, &dev_ec_count
);
538 err
= device_create_file(&ubi
->dev
, &dev_mean_ec
);
541 err
= device_create_file(&ubi
->dev
, &dev_ec_sum
);
544 err
= device_create_file(&ubi
->dev
, &dev_min_ec
);
547 err
= device_create_file(&ubi
->dev
, &dev_wl_count
);
550 err
= device_create_file(&ubi
->dev
, &dev_wl_size
);
553 err
= device_create_file(&ubi
->dev
, &dev_scrub_count
);
556 err
= device_create_file(&ubi
->dev
, &dev_scrub_size
);
559 err
= device_create_file(&ubi
->dev
, &dev_wl_th
);
562 err
= device_create_file(&ubi
->dev
, &dev_torture
);
566 err
= device_create_file(&ubi
->dev
, &dev_reserved_for_bad
);
569 err
= device_create_file(&ubi
->dev
, &dev_bad_peb_count
);
572 err
= device_create_file(&ubi
->dev
, &dev_max_vol_count
);
575 err
= device_create_file(&ubi
->dev
, &dev_min_io_size
);
578 err
= device_create_file(&ubi
->dev
, &dev_bgt_enabled
);
581 err
= device_create_file(&ubi
->dev
, &dev_mtd_num
);
586 * ubi_sysfs_close - close sysfs for an UBI device.
587 * @ubi: UBI device description object
589 static void ubi_sysfs_close(struct ubi_device
*ubi
)
591 device_remove_file(&ubi
->dev
, &dev_mtd_num
);
592 device_remove_file(&ubi
->dev
, &dev_bgt_enabled
);
593 device_remove_file(&ubi
->dev
, &dev_min_io_size
);
594 device_remove_file(&ubi
->dev
, &dev_max_vol_count
);
595 device_remove_file(&ubi
->dev
, &dev_bad_peb_count
);
596 device_remove_file(&ubi
->dev
, &dev_reserved_for_bad
);
597 device_remove_file(&ubi
->dev
, &dev_max_ec
);
598 device_remove_file(&ubi
->dev
, &dev_volumes_count
);
599 device_remove_file(&ubi
->dev
, &dev_total_eraseblocks
);
600 device_remove_file(&ubi
->dev
, &dev_avail_eraseblocks
);
601 device_remove_file(&ubi
->dev
, &dev_eraseblock_size
);
602 device_unregister(&ubi
->dev
);
606 * kill_volumes - destroy all user volumes.
607 * @ubi: UBI device description object
609 static void kill_volumes(struct ubi_device
*ubi
)
613 for (i
= 0; i
< ubi
->vtbl_slots
; i
++)
615 ubi_free_volume(ubi
, ubi
->volumes
[i
]);
619 * uif_init - initialize user interfaces for an UBI device.
620 * @ubi: UBI device description object
621 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
622 * taken, otherwise set to %0
624 * This function initializes various user interfaces for an UBI device. If the
625 * initialization fails at an early stage, this function frees all the
626 * resources it allocated, returns an error, and @ref is set to %0. However,
627 * if the initialization fails after the UBI device was registered in the
628 * driver core subsystem, this function takes a reference to @ubi->dev, because
629 * otherwise the release function ('dev_release()') would free whole @ubi
630 * object. The @ref argument is set to %1 in this case. The caller has to put
633 * This function returns zero in case of success and a negative error code in
636 static int uif_init(struct ubi_device
*ubi
, int *ref
)
642 sprintf(ubi
->ubi_name
, UBI_NAME_STR
"%d", ubi
->ubi_num
);
645 * Major numbers for the UBI character devices are allocated
646 * dynamically. Major numbers of volume character devices are
647 * equivalent to ones of the corresponding UBI character device. Minor
648 * numbers of UBI character devices are 0, while minor numbers of
649 * volume character devices start from 1. Thus, we allocate one major
650 * number and ubi->vtbl_slots + 1 minor numbers.
652 err
= alloc_chrdev_region(&dev
, 0, ubi
->vtbl_slots
+ 1, ubi
->ubi_name
);
654 ubi_err("cannot register UBI character devices");
658 ubi_assert(MINOR(dev
) == 0);
659 cdev_init(&ubi
->cdev
, &ubi_cdev_operations
);
660 dbg_gen("%s major is %u", ubi
->ubi_name
, MAJOR(dev
));
661 ubi
->cdev
.owner
= THIS_MODULE
;
663 err
= cdev_add(&ubi
->cdev
, dev
, 1);
665 ubi_err("cannot add character device");
669 err
= ubi_sysfs_init(ubi
, ref
);
673 for (i
= 0; i
< ubi
->vtbl_slots
; i
++)
674 if (ubi
->volumes
[i
]) {
675 err
= ubi_add_volume(ubi
, ubi
->volumes
[i
]);
677 ubi_err("cannot add volume %d", i
);
688 get_device(&ubi
->dev
);
689 ubi_sysfs_close(ubi
);
690 cdev_del(&ubi
->cdev
);
692 unregister_chrdev_region(ubi
->cdev
.dev
, ubi
->vtbl_slots
+ 1);
693 ubi_err("cannot initialize UBI %s, error %d", ubi
->ubi_name
, err
);
698 * uif_close - close user interfaces for an UBI device.
699 * @ubi: UBI device description object
701 * Note, since this function un-registers UBI volume device objects (@vol->dev),
702 * the memory allocated voe the volumes is freed as well (in the release
705 static void uif_close(struct ubi_device
*ubi
)
708 ubi_sysfs_close(ubi
);
709 cdev_del(&ubi
->cdev
);
710 unregister_chrdev_region(ubi
->cdev
.dev
, ubi
->vtbl_slots
+ 1);
714 * ubi_free_internal_volumes - free internal volumes.
715 * @ubi: UBI device description object
717 void ubi_free_internal_volumes(struct ubi_device
*ubi
)
721 for (i
= ubi
->vtbl_slots
;
722 i
< ubi
->vtbl_slots
+ UBI_INT_VOL_COUNT
; i
++) {
723 kfree(ubi
->volumes
[i
]->eba_tbl
);
724 kfree(ubi
->volumes
[i
]);
728 static int get_bad_peb_limit(const struct ubi_device
*ubi
, int max_beb_per1024
)
730 int limit
, device_pebs
;
731 uint64_t device_size
;
733 if (!max_beb_per1024
)
737 * Here we are using size of the entire flash chip and
738 * not just the MTD partition size because the maximum
739 * number of bad eraseblocks is a percentage of the
740 * whole device and bad eraseblocks are not fairly
741 * distributed over the flash chip. So the worst case
742 * is that all the bad eraseblocks of the chip are in
743 * the MTD partition we are attaching (ubi->mtd).
745 device_size
= mtd_get_device_size(ubi
->mtd
);
746 device_pebs
= mtd_div_by_eb(device_size
, ubi
->mtd
);
747 limit
= mult_frac(device_pebs
, max_beb_per1024
, 1024);
750 if (mult_frac(limit
, 1024, max_beb_per1024
) < device_pebs
)
757 * io_init - initialize I/O sub-system for a given UBI device.
758 * @ubi: UBI device description object
759 * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
761 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
763 * o EC header is always at offset zero - this cannot be changed;
764 * o VID header starts just after the EC header at the closest address
765 * aligned to @io->hdrs_min_io_size;
766 * o data starts just after the VID header at the closest address aligned to
769 * This function returns zero in case of success and a negative error code in
772 static int io_init(struct ubi_device
*ubi
, int max_beb_per1024
)
774 dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb
));
775 dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry
));
777 if (ubi
->mtd
->numeraseregions
!= 0) {
779 * Some flashes have several erase regions. Different regions
780 * may have different eraseblock size and other
781 * characteristics. It looks like mostly multi-region flashes
782 * have one "main" region and one or more small regions to
783 * store boot loader code or boot parameters or whatever. I
784 * guess we should just pick the largest region. But this is
787 ubi_err("multiple regions, not implemented");
791 if (ubi
->vid_hdr_offset
< 0)
795 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
796 * physical eraseblocks maximum.
799 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
800 ubi
->peb_size
= COMBO_NAND_BLOCK_SIZE
;
801 ubi
->peb_count
= (int)div_u64(ubi
->mtd
->size
, ubi
->peb_size
);
803 ubi
->peb_size
= ubi
->mtd
->erasesize
;
804 ubi
->peb_count
= mtd_div_by_eb(ubi
->mtd
->size
, ubi
->mtd
);
806 ubi
->flash_size
= ubi
->mtd
->size
;
808 if (mtd_can_have_bb(ubi
->mtd
)) {
809 ubi
->bad_allowed
= 1;
810 ubi
->bad_peb_limit
= get_bad_peb_limit(ubi
, max_beb_per1024
);
813 if (ubi
->mtd
->type
== MTD_NORFLASH
) {
814 ubi_assert(ubi
->mtd
->writesize
== 1);
818 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
819 ubi
->min_io_size
= COMBO_NAND_PAGE_SIZE
;
820 ubi
->hdrs_min_io_size
= ubi
->min_io_size
>> ubi
->mtd
->subpage_sft
;
822 ubi
->min_io_size
= ubi
->mtd
->writesize
;
823 ubi
->hdrs_min_io_size
= ubi
->mtd
->writesize
>> ubi
->mtd
->subpage_sft
;
827 * Make sure minimal I/O unit is power of 2. Note, there is no
828 * fundamental reason for this assumption. It is just an optimization
829 * which allows us to avoid costly division operations.
831 if (!is_power_of_2(ubi
->min_io_size
)) {
832 ubi_err("min. I/O unit (%d) is not power of 2",
837 ubi_assert(ubi
->hdrs_min_io_size
> 0);
838 ubi_assert(ubi
->hdrs_min_io_size
<= ubi
->min_io_size
);
839 ubi_assert(ubi
->min_io_size
% ubi
->hdrs_min_io_size
== 0);
841 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
842 ubi
->max_write_size
= COMBO_NAND_PAGE_SIZE
;
844 ubi
->max_write_size
= ubi
->mtd
->writebufsize
;
846 #ifdef CONFIG_MTK_MLC_NAND_SUPPORT
847 ubi
->max_write_size
= ubi
->mtd
->erasesize
/4;
850 * Maximum write size has to be greater or equivalent to min. I/O
851 * size, and be multiple of min. I/O size.
853 if (ubi
->max_write_size
< ubi
->min_io_size
||
854 ubi
->max_write_size
% ubi
->min_io_size
||
855 !is_power_of_2(ubi
->max_write_size
)) {
856 ubi_err("bad write buffer size %d for %d min. I/O unit",
857 ubi
->max_write_size
, ubi
->min_io_size
);
861 /* Calculate default aligned sizes of EC and VID headers */
862 ubi
->ec_hdr_alsize
= ALIGN(UBI_EC_HDR_SIZE
, ubi
->hdrs_min_io_size
);
863 ubi
->vid_hdr_alsize
= ALIGN(UBI_VID_HDR_SIZE
, ubi
->hdrs_min_io_size
);
865 dbg_gen("min_io_size %d", ubi
->min_io_size
);
866 dbg_gen("max_write_size %d", ubi
->max_write_size
);
867 dbg_gen("hdrs_min_io_size %d", ubi
->hdrs_min_io_size
);
868 dbg_gen("ec_hdr_alsize %d", ubi
->ec_hdr_alsize
);
869 dbg_gen("vid_hdr_alsize %d", ubi
->vid_hdr_alsize
);
871 if (ubi
->vid_hdr_offset
== 0)
873 ubi
->vid_hdr_offset
= ubi
->vid_hdr_aloffset
=
876 ubi
->vid_hdr_aloffset
= ubi
->vid_hdr_offset
&
877 ~(ubi
->hdrs_min_io_size
- 1);
878 ubi
->vid_hdr_shift
= ubi
->vid_hdr_offset
-
879 ubi
->vid_hdr_aloffset
;
882 /* Similar for the data offset */
883 ubi
->leb_start
= ubi
->vid_hdr_offset
+ UBI_VID_HDR_SIZE
;
884 ubi
->leb_start
= ALIGN(ubi
->leb_start
, ubi
->min_io_size
);
886 dbg_gen("vid_hdr_offset %d", ubi
->vid_hdr_offset
);
887 dbg_gen("vid_hdr_aloffset %d", ubi
->vid_hdr_aloffset
);
888 dbg_gen("vid_hdr_shift %d", ubi
->vid_hdr_shift
);
889 dbg_gen("leb_start %d", ubi
->leb_start
);
891 /* The shift must be aligned to 32-bit boundary */
892 if (ubi
->vid_hdr_shift
% 4) {
893 ubi_err("unaligned VID header shift %d",
899 if (ubi
->vid_hdr_offset
< UBI_EC_HDR_SIZE
||
900 ubi
->leb_start
< ubi
->vid_hdr_offset
+ UBI_VID_HDR_SIZE
||
901 ubi
->leb_start
> ubi
->peb_size
- UBI_VID_HDR_SIZE
||
902 ubi
->leb_start
& (ubi
->min_io_size
- 1)) {
903 ubi_err("bad VID header (%d) or data offsets (%d)",
904 ubi
->vid_hdr_offset
, ubi
->leb_start
);
909 * Set maximum amount of physical erroneous eraseblocks to be 10%.
910 * Erroneous PEB are those which have read errors.
912 ubi
->max_erroneous
= ubi
->peb_count
/ 10;
913 if (ubi
->max_erroneous
< 16)
914 ubi
->max_erroneous
= 16;
915 dbg_gen("max_erroneous %d", ubi
->max_erroneous
);
918 * It may happen that EC and VID headers are situated in one minimal
919 * I/O unit. In this case we can only accept this UBI image in
922 if (ubi
->vid_hdr_offset
+ UBI_VID_HDR_SIZE
<= ubi
->hdrs_min_io_size
) {
923 ubi_warn("EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
927 ubi
->leb_size
= ubi
->peb_size
- ubi
->leb_start
;
929 if (!(ubi
->mtd
->flags
& MTD_WRITEABLE
)) {
930 ubi_msg("MTD device %d is write-protected, attach in read-only mode",
935 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
936 ubi
->peb_size
, ubi
->peb_size
>> 10);
937 ubi_msg("logical eraseblock size: %d bytes", ubi
->leb_size
);
938 ubi_msg("smallest flash I/O unit: %d", ubi
->min_io_size
);
939 if (ubi
->hdrs_min_io_size
!= ubi
->min_io_size
)
940 ubi_msg("sub-page size: %d",
941 ubi
->hdrs_min_io_size
);
942 ubi_msg("VID header offset: %d (aligned %d)",
943 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
);
944 ubi_msg("data offset: %d", ubi
->leb_start
);
947 * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
948 * unfortunately, MTD does not provide this information. We should loop
949 * over all physical eraseblocks and invoke mtd->block_is_bad() for
950 * each physical eraseblock. So, we leave @ubi->bad_peb_count
951 * uninitialized so far.
958 * autoresize - re-size the volume which has the "auto-resize" flag set.
959 * @ubi: UBI device description object
960 * @vol_id: ID of the volume to re-size
962 * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
963 * the volume table to the largest possible size. See comments in ubi-header.h
964 * for more description of the flag. Returns zero in case of success and a
965 * negative error code in case of failure.
967 static int autoresize(struct ubi_device
*ubi
, int vol_id
)
969 struct ubi_volume_desc desc
;
970 struct ubi_volume
*vol
= ubi
->volumes
[vol_id
];
971 int err
, old_reserved_pebs
= vol
->reserved_pebs
;
974 ubi_warn("skip auto-resize because of R/O mode");
979 * Clear the auto-resize flag in the volume in-memory copy of the
980 * volume table, and 'ubi_resize_volume()' will propagate this change
983 ubi
->vtbl
[vol_id
].flags
&= ~UBI_VTBL_AUTORESIZE_FLG
;
985 if (ubi
->avail_pebs
== 0) {
986 struct ubi_vtbl_record vtbl_rec
;
989 * No available PEBs to re-size the volume, clear the flag on
992 vtbl_rec
= ubi
->vtbl
[vol_id
];
993 err
= ubi_change_vtbl_record(ubi
, vol_id
, &vtbl_rec
);
995 ubi_err("cannot clean auto-resize flag for volume %d",
999 err
= ubi_resize_volume(&desc
,
1000 old_reserved_pebs
+ ubi
->avail_pebs
);
1002 ubi_err("cannot auto-resize volume %d", vol_id
);
1008 ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id
,
1009 vol
->name
, old_reserved_pebs
, vol
->reserved_pebs
);
1014 * ubi_attach_mtd_dev - attach an MTD device.
1015 * @mtd: MTD device description object
1016 * @ubi_num: number to assign to the new UBI device
1017 * @vid_hdr_offset: VID header offset
1018 * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
1020 * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
1021 * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
1022 * which case this function finds a vacant device number and assigns it
1023 * automatically. Returns the new UBI device number in case of success and a
1024 * negative error code in case of failure.
1026 * Note, the invocations of this function has to be serialized by the
1027 * @ubi_devices_mutex.
1029 int ubi_attach_mtd_dev(struct mtd_info
*mtd
, int ubi_num
,
1030 int vid_hdr_offset
, int max_beb_per1024
)
1032 struct ubi_device
*ubi
;
1033 int i
, err
, ref
= 0;
1034 unsigned long long attach_time
= 0;
1036 if (max_beb_per1024
< 0 || max_beb_per1024
> MAX_MTD_UBI_BEB_LIMIT
)
1039 if (!max_beb_per1024
)
1040 max_beb_per1024
= CONFIG_MTD_UBI_BEB_LIMIT
;
1043 * Check if we already have the same MTD device attached.
1045 * Note, this function assumes that UBI devices creations and deletions
1046 * are serialized, so it does not take the &ubi_devices_lock.
1048 for (i
= 0; i
< UBI_MAX_DEVICES
; i
++) {
1049 ubi
= ubi_devices
[i
];
1050 if (ubi
&& mtd
->index
== ubi
->mtd
->index
) {
1051 ubi_err("mtd%d is already attached to ubi%d",
1058 * Make sure this MTD device is not emulated on top of an UBI volume
1059 * already. Well, generally this recursion works fine, but there are
1060 * different problems like the UBI module takes a reference to itself
1061 * by attaching (and thus, opening) the emulated MTD device. This
1062 * results in inability to unload the module. And in general it makes
1063 * no sense to attach emulated MTD devices, so we prohibit this.
1065 if (mtd
->type
== MTD_UBIVOLUME
) {
1066 ubi_err("refuse attaching mtd%d - it is already emulated on top of UBI",
1071 if (ubi_num
== UBI_DEV_NUM_AUTO
) {
1072 /* Search for an empty slot in the @ubi_devices array */
1073 for (ubi_num
= 0; ubi_num
< UBI_MAX_DEVICES
; ubi_num
++)
1074 if (!ubi_devices
[ubi_num
])
1076 if (ubi_num
== UBI_MAX_DEVICES
) {
1077 ubi_err("only %d UBI devices may be created",
1082 if (ubi_num
>= UBI_MAX_DEVICES
)
1085 /* Make sure ubi_num is not busy */
1086 if (ubi_devices
[ubi_num
]) {
1087 ubi_err("ubi%d already exists", ubi_num
);
1092 ubi
= kzalloc(sizeof(struct ubi_device
), GFP_KERNEL
);
1097 ubi
->ubi_num
= ubi_num
;
1098 ubi
->vid_hdr_offset
= vid_hdr_offset
;
1099 ubi
->autoresize_vol_id
= -1;
1101 ubi
->wl_th
= CONFIG_MTD_UBI_WL_THRESHOLD
;
1102 atomic_set(&ubi
->ec_count
, 0);
1103 atomic_set(&ubi
->move_retry
, 0);
1106 #ifdef CONFIG_MTD_UBI_FASTMAP
1107 ubi
->fm_pool
.used
= ubi
->fm_pool
.size
= 0;
1108 ubi
->fm_wl_pool
.used
= ubi
->fm_wl_pool
.size
= 0;
1111 * fm_pool.max_size is 5% of the total number of PEBs but it's also
1112 * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
1114 ubi
->fm_pool
.max_size
= min(((int)mtd_div_by_eb(ubi
->mtd
->size
,
1115 ubi
->mtd
) / 100) * 5, UBI_FM_MAX_POOL_SIZE
);
1116 if (ubi
->fm_pool
.max_size
< UBI_FM_MIN_POOL_SIZE
)
1117 ubi
->fm_pool
.max_size
= UBI_FM_MIN_POOL_SIZE
;
1119 ubi
->fm_wl_pool
.max_size
= UBI_FM_WL_POOL_SIZE
;
1120 ubi
->fm_disabled
= !fm_autoconvert
;
1122 if (!ubi
->fm_disabled
&& (int)mtd_div_by_eb(ubi
->mtd
->size
, ubi
->mtd
)
1123 <= UBI_FM_MAX_START
) {
1124 ubi_err("More than %i PEBs are needed for fastmap, sorry.",
1126 ubi
->fm_disabled
= 1;
1129 ubi_msg("default fastmap pool size: %d", ubi
->fm_pool
.max_size
);
1130 ubi_msg("default fastmap WL pool size: %d", ubi
->fm_wl_pool
.max_size
);
1132 ubi
->fm_disabled
= 1;
1134 mutex_init(&ubi
->buf_mutex
);
1135 mutex_init(&ubi
->ckvol_mutex
);
1136 mutex_init(&ubi
->device_mutex
);
1137 spin_lock_init(&ubi
->volumes_lock
);
1138 mutex_init(&ubi
->fm_mutex
);
1139 init_rwsem(&ubi
->fm_sem
);
1141 ubi_msg("attaching mtd%d to ubi%d", mtd
->index
, ubi_num
);
1143 err
= io_init(ubi
, max_beb_per1024
);
1148 ubi
->peb_buf
= kmalloc(ubi
->peb_size
, GFP_KERNEL
);
1152 #ifdef CONFIG_MTD_UBI_FASTMAP
1153 ubi
->fm_size
= ubi_calc_fm_size(ubi
);
1154 ubi
->fm_buf
= kzalloc(ubi
->fm_size
, GFP_KERNEL
);
1158 attach_time
= sched_clock();
1159 err
= ubi_attach(ubi
, 0);
1161 ubi_err("failed to attach mtd%d, error %d", mtd
->index
, err
);
1165 if (ubi
->autoresize_vol_id
!= -1) {
1166 err
= autoresize(ubi
, ubi
->autoresize_vol_id
);
1171 /* Make device "available" before it becomes accessible via sysfs */
1172 ubi_devices
[ubi_num
] = ubi
;
1174 err
= uif_init(ubi
, &ref
);
1178 err
= ubi_debugfs_init_dev(ubi
);
1182 ubi
->bgt_thread
= kthread_create(ubi_thread
, ubi
, ubi
->bgt_name
);
1183 if (IS_ERR(ubi
->bgt_thread
)) {
1184 err
= PTR_ERR(ubi
->bgt_thread
);
1185 ubi_err("cannot spawn \"%s\", error %d", ubi
->bgt_name
,
1190 attach_time
= sched_clock() - attach_time
;
1191 do_div(attach_time
, 1000000);
1192 ubi_msg("attached mtd%d (name \"%s\", size %llu MiB) to ubi%d",
1193 mtd
->index
, mtd
->name
, ubi
->flash_size
>> 20, ubi_num
);
1194 ubi_msg("PEB size: %d bytes (%d KiB), LEB size: %d bytes",
1195 ubi
->peb_size
, ubi
->peb_size
>> 10, ubi
->leb_size
);
1196 ubi_msg("min./max. I/O unit sizes: %d/%d, sub-page size %d",
1197 ubi
->min_io_size
, ubi
->max_write_size
, ubi
->hdrs_min_io_size
);
1198 ubi_msg("VID header offset: %d (aligned %d), data offset: %d",
1199 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
, ubi
->leb_start
);
1200 ubi_msg("good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
1201 ubi
->good_peb_count
, ubi
->bad_peb_count
, ubi
->corr_peb_count
);
1202 ubi_msg("user volume: %d, internal volumes: %d, max. volumes count: %d",
1203 ubi
->vol_count
- UBI_INT_VOL_COUNT
, UBI_INT_VOL_COUNT
,
1205 ubi_msg("max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
1206 ubi
->max_ec
, ubi
->mean_ec
, CONFIG_MTD_UBI_WL_THRESHOLD
,
1208 ubi_msg("available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
1209 ubi
->avail_pebs
, ubi
->rsvd_pebs
, ubi
->beb_rsvd_pebs
);
1212 * The below lock makes sure we do not race with 'ubi_thread()' which
1213 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
1215 spin_lock(&ubi
->wl_lock
);
1216 ubi
->thread_enabled
= 1;
1217 wake_up_process(ubi
->bgt_thread
);
1218 spin_unlock(&ubi
->wl_lock
);
1220 ubi_notify_all(ubi
, UBI_VOLUME_ADDED
, NULL
);
1224 ubi_debugfs_exit_dev(ubi
);
1226 get_device(&ubi
->dev
);
1230 ubi_devices
[ubi_num
] = NULL
;
1232 ubi_free_internal_volumes(ubi
);
1235 kfree(ubi
->peb_buf
);
1238 put_device(&ubi
->dev
);
1245 * ubi_detach_mtd_dev - detach an MTD device.
1246 * @ubi_num: UBI device number to detach from
1247 * @anyway: detach MTD even if device reference count is not zero
1249 * This function destroys an UBI device number @ubi_num and detaches the
1250 * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1251 * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1254 * Note, the invocations of this function has to be serialized by the
1255 * @ubi_devices_mutex.
1257 int ubi_detach_mtd_dev(int ubi_num
, int anyway
)
1259 struct ubi_device
*ubi
;
1261 if (ubi_num
< 0 || ubi_num
>= UBI_MAX_DEVICES
)
1264 ubi
= ubi_get_device(ubi_num
);
1268 spin_lock(&ubi_devices_lock
);
1269 put_device(&ubi
->dev
);
1270 ubi
->ref_count
-= 1;
1271 if (ubi
->ref_count
) {
1273 spin_unlock(&ubi_devices_lock
);
1276 /* This may only happen if there is a bug */
1277 ubi_err("%s reference count %d, destroy anyway",
1278 ubi
->ubi_name
, ubi
->ref_count
);
1280 ubi_devices
[ubi_num
] = NULL
;
1281 spin_unlock(&ubi_devices_lock
);
1283 ubi_assert(ubi_num
== ubi
->ubi_num
);
1284 ubi_notify_all(ubi
, UBI_VOLUME_REMOVED
, NULL
);
1285 ubi_msg("detaching mtd%d from ubi%d", ubi
->mtd
->index
, ubi_num
);
1286 #ifdef CONFIG_MTD_UBI_FASTMAP
1287 /* If we don't write a new fastmap at detach time we lose all
1288 * EC updates that have been made since the last written fastmap. */
1289 ubi_update_fastmap(ubi
);
1292 * Before freeing anything, we have to stop the background thread to
1293 * prevent it from doing anything on this device while we are freeing.
1295 if (ubi
->bgt_thread
)
1296 kthread_stop(ubi
->bgt_thread
);
1299 * Get a reference to the device in order to prevent 'dev_release()'
1300 * from freeing the @ubi object.
1302 get_device(&ubi
->dev
);
1304 ubi_debugfs_exit_dev(ubi
);
1308 ubi_free_internal_volumes(ubi
);
1310 put_mtd_device(ubi
->mtd
);
1312 kfree(ubi
->databuf
);
1315 kfree(ubi
->peb_buf
);
1317 ubi_msg("mtd%d is detached from ubi%d", ubi
->mtd
->index
, ubi
->ubi_num
);
1318 put_device(&ubi
->dev
);
1323 * open_mtd_by_chdev - open an MTD device by its character device node path.
1324 * @mtd_dev: MTD character device node path
1326 * This helper function opens an MTD device by its character node device path.
1327 * Returns MTD device description object in case of success and a negative
1328 * error code in case of failure.
1330 static struct mtd_info
* __init
open_mtd_by_chdev(const char *mtd_dev
)
1332 int err
, major
, minor
, mode
;
1335 /* Probably this is an MTD character device node path */
1336 err
= kern_path(mtd_dev
, LOOKUP_FOLLOW
, &path
);
1338 return ERR_PTR(err
);
1340 /* MTD device number is defined by the major / minor numbers */
1341 major
= imajor(path
.dentry
->d_inode
);
1342 minor
= iminor(path
.dentry
->d_inode
);
1343 mode
= path
.dentry
->d_inode
->i_mode
;
1345 if (major
!= MTD_CHAR_MAJOR
|| !S_ISCHR(mode
))
1346 return ERR_PTR(-EINVAL
);
1350 * Just do not think the "/dev/mtdrX" devices support is need,
1351 * so do not support them to avoid doing extra work.
1353 return ERR_PTR(-EINVAL
);
1355 return get_mtd_device(NULL
, minor
/ 2);
1359 * open_mtd_device - open MTD device by name, character device path, or number.
1360 * @mtd_dev: name, character device node path, or MTD device device number
1362 * This function tries to open and MTD device described by @mtd_dev string,
1363 * which is first treated as ASCII MTD device number, and if it is not true, it
1364 * is treated as MTD device name, and if that is also not true, it is treated
1365 * as MTD character device node path. Returns MTD device description object in
1366 * case of success and a negative error code in case of failure.
1368 static struct mtd_info
* __init
open_mtd_device(const char *mtd_dev
)
1370 struct mtd_info
*mtd
;
1374 mtd_num
= simple_strtoul(mtd_dev
, &endp
, 0);
1375 if (*endp
!= '\0' || mtd_dev
== endp
) {
1377 * This does not look like an ASCII integer, probably this is
1380 mtd
= get_mtd_device_nm(mtd_dev
);
1381 if (IS_ERR(mtd
) && PTR_ERR(mtd
) == -ENODEV
)
1382 /* Probably this is an MTD character device node path */
1383 mtd
= open_mtd_by_chdev(mtd_dev
);
1385 mtd
= get_mtd_device(NULL
, mtd_num
);
1390 static int __init
ubi_init(void)
1394 /* Ensure that EC and VID headers have correct size */
1395 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr
) != 64);
1396 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr
) != 64);
1398 if (mtd_devs
> UBI_MAX_DEVICES
) {
1399 ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES
);
1403 /* Create base sysfs directory and sysfs files */
1404 ubi_class
= class_create(THIS_MODULE
, UBI_NAME_STR
);
1405 if (IS_ERR(ubi_class
)) {
1406 err
= PTR_ERR(ubi_class
);
1407 ubi_err("cannot create UBI class");
1411 err
= class_create_file(ubi_class
, &ubi_version
);
1413 ubi_err("cannot create sysfs file");
1417 err
= misc_register(&ubi_ctrl_cdev
);
1419 ubi_err("cannot register device");
1423 ubi_wl_entry_slab
= kmem_cache_create("ubi_wl_entry_slab",
1424 sizeof(struct ubi_wl_entry
),
1426 if (!ubi_wl_entry_slab
)
1429 err
= ubi_debugfs_init();
1434 /* Attach MTD devices */
1435 for (i
= 0; i
< mtd_devs
; i
++) {
1436 struct mtd_dev_param
*p
= &mtd_dev_param
[i
];
1437 struct mtd_info
*mtd
;
1441 mtd
= open_mtd_device(p
->name
);
1447 mutex_lock(&ubi_devices_mutex
);
1448 err
= ubi_attach_mtd_dev(mtd
, UBI_DEV_NUM_AUTO
,
1449 p
->vid_hdr_offs
, p
->max_beb_per1024
);
1450 mutex_unlock(&ubi_devices_mutex
);
1452 ubi_err("cannot attach mtd%d", mtd
->index
);
1453 put_mtd_device(mtd
);
1456 * Originally UBI stopped initializing on any error.
1457 * However, later on it was found out that this
1458 * behavior is not very good when UBI is compiled into
1459 * the kernel and the MTD devices to attach are passed
1460 * through the command line. Indeed, UBI failure
1461 * stopped whole boot sequence.
1463 * To fix this, we changed the behavior for the
1464 * non-module case, but preserved the old behavior for
1465 * the module case, just for compatibility. This is a
1466 * little inconsistent, though.
1468 if (ubi_is_module())
1476 for (k
= 0; k
< i
; k
++)
1477 if (ubi_devices
[k
]) {
1478 mutex_lock(&ubi_devices_mutex
);
1479 ubi_detach_mtd_dev(ubi_devices
[k
]->ubi_num
, 1);
1480 mutex_unlock(&ubi_devices_mutex
);
1484 kmem_cache_destroy(ubi_wl_entry_slab
);
1486 misc_deregister(&ubi_ctrl_cdev
);
1488 class_remove_file(ubi_class
, &ubi_version
);
1490 class_destroy(ubi_class
);
1492 ubi_err("UBI error: cannot initialize UBI, error %d", err
);
1495 late_initcall(ubi_init
);
1497 static void __exit
ubi_exit(void)
1501 for (i
= 0; i
< UBI_MAX_DEVICES
; i
++)
1502 if (ubi_devices
[i
]) {
1503 mutex_lock(&ubi_devices_mutex
);
1504 ubi_detach_mtd_dev(ubi_devices
[i
]->ubi_num
, 1);
1505 mutex_unlock(&ubi_devices_mutex
);
1508 kmem_cache_destroy(ubi_wl_entry_slab
);
1509 misc_deregister(&ubi_ctrl_cdev
);
1510 class_remove_file(ubi_class
, &ubi_version
);
1511 class_destroy(ubi_class
);
1513 module_exit(ubi_exit
);
1516 * bytes_str_to_int - convert a number of bytes string into an integer.
1517 * @str: the string to convert
1519 * This function returns positive resulting integer in case of success and a
1520 * negative error code in case of failure.
1522 static int __init
bytes_str_to_int(const char *str
)
1525 unsigned long result
;
1527 result
= simple_strtoul(str
, &endp
, 0);
1528 if (str
== endp
|| result
>= INT_MAX
) {
1529 ubi_err("UBI error: incorrect bytes count: \"%s\"\n", str
);
1540 if (endp
[1] == 'i' && endp
[2] == 'B')
1545 ubi_err("UBI error: incorrect bytes count: \"%s\"\n", str
);
1553 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1554 * @val: the parameter value to parse
1557 * This function returns zero in case of success and a negative error code in
1560 static int __init
ubi_mtd_param_parse(const char *val
, struct kernel_param
*kp
)
1563 struct mtd_dev_param
*p
;
1564 char buf
[MTD_PARAM_LEN_MAX
];
1565 char *pbuf
= &buf
[0];
1566 char *tokens
[MTD_PARAM_MAX_COUNT
];
1571 if (mtd_devs
== UBI_MAX_DEVICES
) {
1572 ubi_err("UBI error: too many parameters, max. is %d\n",
1577 len
= strnlen(val
, MTD_PARAM_LEN_MAX
);
1578 if (len
== MTD_PARAM_LEN_MAX
) {
1579 ubi_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1580 val
, MTD_PARAM_LEN_MAX
);
1585 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
1591 /* Get rid of the final newline */
1592 if (buf
[len
- 1] == '\n')
1593 buf
[len
- 1] = '\0';
1595 for (i
= 0; i
< MTD_PARAM_MAX_COUNT
; i
++)
1596 tokens
[i
] = strsep(&pbuf
, ",");
1599 ubi_err("UBI error: too many arguments at \"%s\"\n", val
);
1603 p
= &mtd_dev_param
[mtd_devs
];
1604 strcpy(&p
->name
[0], tokens
[0]);
1607 p
->vid_hdr_offs
= bytes_str_to_int(tokens
[1]);
1609 if (p
->vid_hdr_offs
< 0)
1610 return p
->vid_hdr_offs
;
1613 int err
= kstrtoint(tokens
[2], 10, &p
->max_beb_per1024
);
1616 ubi_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1626 module_param_call(mtd
, ubi_mtd_param_parse
, NULL
, NULL
, 000);
1627 MODULE_PARM_DESC(mtd
, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024]].\n"
1628 "Multiple \"mtd\" parameters may be specified.\n"
1629 "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1630 "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1631 "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1632 __stringify(CONFIG_MTD_UBI_BEB_LIMIT
) ") if 0)\n"
1634 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1635 "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"
1636 "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"
1637 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1638 #ifdef CONFIG_MTD_UBI_FASTMAP
1639 module_param(fm_autoconvert
, bool, 0644);
1640 MODULE_PARM_DESC(fm_autoconvert
, "Set this parameter to enable fastmap automatically on images without a fastmap.");
1642 MODULE_VERSION(__stringify(UBI_VERSION
));
1643 MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1644 MODULE_AUTHOR("Artem Bityutskiy");
1645 MODULE_LICENSE("GPL");