Merge tag 'v3.10.103' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.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.
25 *
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".
30 */
31
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>
44 #include "ubi.h"
45 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
46 #include <linux/mtd/combo_nand.h>
47 #endif
48
49 /* Maximum length of the 'mtd=' parameter */
50 #define MTD_PARAM_LEN_MAX 64
51
52 /* Maximum number of comma-separated items in the 'mtd=' parameter */
53 #define MTD_PARAM_MAX_COUNT 3
54
55 /* Maximum value for the number of bad PEBs per 1024 PEBs */
56 #define MAX_MTD_UBI_BEB_LIMIT 768
57
58 #ifdef CONFIG_MTD_UBI_MODULE
59 #define ubi_is_module() 1
60 #else
61 #define ubi_is_module() 0
62 #endif
63
64 /**
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
67 * string
68 * @vid_hdr_offs: VID header offset
69 * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
70 */
71 struct mtd_dev_param {
72 char name[MTD_PARAM_LEN_MAX];
73 int vid_hdr_offs;
74 int max_beb_per1024;
75 };
76
77 /* Numbers of elements set in the @mtd_dev_param array */
78 static int __initdata mtd_devs;
79
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;
86 #else
87 static bool fm_autoconvert = 0;
88 #endif
89 #endif
90 /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
91 struct class *ubi_class;
92
93 /* Slab cache for wear-leveling entries */
94 struct kmem_cache *ubi_wl_entry_slab;
95
96 /* UBI control character device */
97 static struct miscdevice ubi_ctrl_cdev = {
98 .minor = MISC_DYNAMIC_MINOR,
99 .name = "ubi_ctrl",
100 .fops = &ubi_ctrl_cdev_operations,
101 };
102
103 /* All UBI devices in system */
104 static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
105
106 /* Serializes UBI devices creations and removals */
107 DEFINE_MUTEX(ubi_devices_mutex);
108
109 /* Protects @ubi_devices and @ubi->ref_count */
110 static DEFINE_SPINLOCK(ubi_devices_lock);
111
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)
115 {
116 return sprintf(buf, "%d\n", UBI_VERSION);
117 }
118
119 /* UBI version attribute ('/<sysfs>/class/ubi/version') */
120 static struct class_attribute ubi_version =
121 __ATTR(version, S_IRUGO, ubi_version_show, NULL);
122
123 static ssize_t dev_attribute_show(struct device *dev,
124 struct device_attribute *attr, char *buf);
125 //MTK
126 static ssize_t dev_attribute_store(struct device *dev, struct device_attribute *attr,
127 const char *buf, size_t count);
128
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);
140 //MTK start
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);
165 //MTK end
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);
178
179 /**
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)
184 *
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.
188 */
189 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
190 {
191 struct ubi_notification nt;
192
193 ubi_do_get_device_info(ubi, &nt.di);
194 ubi_do_get_volume_info(ubi, vol, &nt.vi);
195
196 #ifdef CONFIG_MTD_UBI_FASTMAP
197 switch (ntype) {
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!");
204 ubi_ro_mode(ubi);
205 }
206 }
207 #endif
208 return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
209 }
210
211 /**
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
216 *
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.
221 */
222 int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
223 {
224 struct ubi_notification nt;
225 int i, count = 0;
226
227 ubi_do_get_device_info(ubi, &nt.di);
228
229 mutex_lock(&ubi->device_mutex);
230 for (i = 0; i < ubi->vtbl_slots; i++) {
231 /*
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.
235 */
236 if (!ubi->volumes[i])
237 continue;
238
239 ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
240 if (nb)
241 nb->notifier_call(nb, ntype, &nt);
242 else
243 blocking_notifier_call_chain(&ubi_notifiers, ntype,
244 &nt);
245 count += 1;
246 }
247 mutex_unlock(&ubi->device_mutex);
248
249 return count;
250 }
251
252 /**
253 * ubi_enumerate_volumes - send "add" notification for all existing volumes.
254 * @nb: the notifier to call
255 *
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.
260 */
261 int ubi_enumerate_volumes(struct notifier_block *nb)
262 {
263 int i, count = 0;
264
265 /*
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.
268 */
269 for (i = 0; i < UBI_MAX_DEVICES; i++) {
270 struct ubi_device *ubi = ubi_devices[i];
271
272 if (!ubi)
273 continue;
274 count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
275 }
276
277 return count;
278 }
279
280 /**
281 * ubi_get_device - get UBI device.
282 * @ubi_num: UBI device number
283 *
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.
288 */
289 struct ubi_device *ubi_get_device(int ubi_num)
290 {
291 struct ubi_device *ubi;
292
293 spin_lock(&ubi_devices_lock);
294 ubi = ubi_devices[ubi_num];
295 if (ubi) {
296 ubi_assert(ubi->ref_count >= 0);
297 ubi->ref_count += 1;
298 get_device(&ubi->dev);
299 }
300 spin_unlock(&ubi_devices_lock);
301
302 return ubi;
303 }
304
305 /**
306 * ubi_put_device - drop an UBI device reference.
307 * @ubi: UBI device description object
308 */
309 void ubi_put_device(struct ubi_device *ubi)
310 {
311 spin_lock(&ubi_devices_lock);
312 ubi->ref_count -= 1;
313 put_device(&ubi->dev);
314 spin_unlock(&ubi_devices_lock);
315 }
316
317 /**
318 * ubi_get_by_major - get UBI device by character device major number.
319 * @major: major number
320 *
321 * This function is similar to 'ubi_get_device()', but it searches the device
322 * by its major number.
323 */
324 struct ubi_device *ubi_get_by_major(int major)
325 {
326 int i;
327 struct ubi_device *ubi;
328
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);
334 ubi->ref_count += 1;
335 get_device(&ubi->dev);
336 spin_unlock(&ubi_devices_lock);
337 return ubi;
338 }
339 }
340 spin_unlock(&ubi_devices_lock);
341
342 return NULL;
343 }
344
345 /**
346 * ubi_major2num - get UBI device number by character device major number.
347 * @major: major number
348 *
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.
352 */
353 int ubi_major2num(int major)
354 {
355 int i, ubi_num = -ENODEV;
356
357 spin_lock(&ubi_devices_lock);
358 for (i = 0; i < UBI_MAX_DEVICES; i++) {
359 struct ubi_device *ubi = ubi_devices[i];
360
361 if (ubi && MAJOR(ubi->cdev.dev) == major) {
362 ubi_num = ubi->ubi_num;
363 break;
364 }
365 }
366 spin_unlock(&ubi_devices_lock);
367
368 return ubi_num;
369 }
370
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)
374 {
375 struct ubi_device *ubi;
376 int th=0;
377
378 ubi = container_of(dev, struct ubi_device, dev);
379 ubi = ubi_get_device(ubi->ubi_num);
380 if (!ubi)
381 return -ENODEV;
382
383 if (attr == &dev_wl_th)
384 {
385 sscanf(buf, "%d", &th);
386 printk("set th=%d\n", th);
387 ubi->wl_th = th;
388 }
389 return count;
390 }
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)
394 {
395 ssize_t ret;
396 struct ubi_device *ubi;
397
398 /*
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.
404 *
405 * Remember, 'struct ubi_device' is freed in the release function, so
406 * we still can use 'ubi->ubi_num'.
407 */
408 ubi = container_of(dev, struct ubi_device, dev);
409 ubi = ubi_get_device(ubi->ubi_num);
410 if (!ubi)
411 return -ENODEV;
412
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);
423 //MTK start
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);
451 if(efree && eused) {
452 if(efree->ec < eused->ec)
453 e = efree;
454 else
455 e = eused;
456 } else if(efree){
457 e = efree;
458 } else {
459 e = eused;
460 }
461 ret = sprintf(buf, "%d\n", e->ec);
462 spin_unlock(&ubi->wl_lock);
463 }
464 //MTK end
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);
477 else
478 ret = -EINVAL;
479
480 ubi_put_device(ubi);
481 return ret;
482 }
483
484 static void dev_release(struct device *dev)
485 {
486 struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
487
488 kfree(ubi);
489 }
490
491 /**
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
495 * taken
496 *
497 * This function returns zero in case of success and a negative error code in
498 * case of failure.
499 */
500 static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
501 {
502 int err;
503
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);
509 if (err)
510 return err;
511
512 *ref = 1;
513 err = device_create_file(&ubi->dev, &dev_eraseblock_size);
514 if (err)
515 return err;
516 err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
517 if (err)
518 return err;
519 err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
520 if (err)
521 return err;
522 err = device_create_file(&ubi->dev, &dev_volumes_count);
523 if (err)
524 return err;
525 err = device_create_file(&ubi->dev, &dev_max_ec);
526 if (err)
527 return err;
528 //MTK start
529 err = device_create_file(&ubi->dev, &dev_lbb);
530 if (err)
531 return err;
532 err = device_create_file(&ubi->dev, &dev_move_retry);
533 if (err)
534 return err;
535 err = device_create_file(&ubi->dev, &dev_ec_count);
536 if (err)
537 return err;
538 err = device_create_file(&ubi->dev, &dev_mean_ec);
539 if (err)
540 return err;
541 err = device_create_file(&ubi->dev, &dev_ec_sum);
542 if (err)
543 return err;
544 err = device_create_file(&ubi->dev, &dev_min_ec);
545 if (err)
546 return err;
547 err = device_create_file(&ubi->dev, &dev_wl_count);
548 if (err)
549 return err;
550 err = device_create_file(&ubi->dev, &dev_wl_size);
551 if (err)
552 return err;
553 err = device_create_file(&ubi->dev, &dev_scrub_count);
554 if (err)
555 return err;
556 err = device_create_file(&ubi->dev, &dev_scrub_size);
557 if (err)
558 return err;
559 err = device_create_file(&ubi->dev, &dev_wl_th);
560 if (err)
561 return err;
562 err = device_create_file(&ubi->dev, &dev_torture);
563 if (err)
564 return err;
565 //MTK end
566 err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
567 if (err)
568 return err;
569 err = device_create_file(&ubi->dev, &dev_bad_peb_count);
570 if (err)
571 return err;
572 err = device_create_file(&ubi->dev, &dev_max_vol_count);
573 if (err)
574 return err;
575 err = device_create_file(&ubi->dev, &dev_min_io_size);
576 if (err)
577 return err;
578 err = device_create_file(&ubi->dev, &dev_bgt_enabled);
579 if (err)
580 return err;
581 err = device_create_file(&ubi->dev, &dev_mtd_num);
582 return err;
583 }
584
585 /**
586 * ubi_sysfs_close - close sysfs for an UBI device.
587 * @ubi: UBI device description object
588 */
589 static void ubi_sysfs_close(struct ubi_device *ubi)
590 {
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);
603 }
604
605 /**
606 * kill_volumes - destroy all user volumes.
607 * @ubi: UBI device description object
608 */
609 static void kill_volumes(struct ubi_device *ubi)
610 {
611 int i;
612
613 for (i = 0; i < ubi->vtbl_slots; i++)
614 if (ubi->volumes[i])
615 ubi_free_volume(ubi, ubi->volumes[i]);
616 }
617
618 /**
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
623 *
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
631 * this reference.
632 *
633 * This function returns zero in case of success and a negative error code in
634 * case of failure.
635 */
636 static int uif_init(struct ubi_device *ubi, int *ref)
637 {
638 int i, err;
639 dev_t dev;
640
641 *ref = 0;
642 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
643
644 /*
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.
651 */
652 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
653 if (err) {
654 ubi_err("cannot register UBI character devices");
655 return err;
656 }
657
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;
662
663 err = cdev_add(&ubi->cdev, dev, 1);
664 if (err) {
665 ubi_err("cannot add character device");
666 goto out_unreg;
667 }
668
669 err = ubi_sysfs_init(ubi, ref);
670 if (err)
671 goto out_sysfs;
672
673 for (i = 0; i < ubi->vtbl_slots; i++)
674 if (ubi->volumes[i]) {
675 err = ubi_add_volume(ubi, ubi->volumes[i]);
676 if (err) {
677 ubi_err("cannot add volume %d", i);
678 goto out_volumes;
679 }
680 }
681
682 return 0;
683
684 out_volumes:
685 kill_volumes(ubi);
686 out_sysfs:
687 if (*ref)
688 get_device(&ubi->dev);
689 ubi_sysfs_close(ubi);
690 cdev_del(&ubi->cdev);
691 out_unreg:
692 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
693 ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
694 return err;
695 }
696
697 /**
698 * uif_close - close user interfaces for an UBI device.
699 * @ubi: UBI device description object
700 *
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
703 * function).
704 */
705 static void uif_close(struct ubi_device *ubi)
706 {
707 kill_volumes(ubi);
708 ubi_sysfs_close(ubi);
709 cdev_del(&ubi->cdev);
710 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
711 }
712
713 /**
714 * ubi_free_internal_volumes - free internal volumes.
715 * @ubi: UBI device description object
716 */
717 void ubi_free_internal_volumes(struct ubi_device *ubi)
718 {
719 int i;
720
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]);
725 }
726 }
727
728 static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
729 {
730 int limit, device_pebs;
731 uint64_t device_size;
732
733 if (!max_beb_per1024)
734 return 0;
735
736 /*
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).
744 */
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);
748
749 /* Round it up */
750 if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
751 limit += 1;
752
753 return limit;
754 }
755
756 /**
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
760 *
761 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
762 * assumed:
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
767 * @io->min_io_size
768 *
769 * This function returns zero in case of success and a negative error code in
770 * case of failure.
771 */
772 static int io_init(struct ubi_device *ubi, int max_beb_per1024)
773 {
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));
776
777 if (ubi->mtd->numeraseregions != 0) {
778 /*
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
785 * not implemented.
786 */
787 ubi_err("multiple regions, not implemented");
788 return -EINVAL;
789 }
790
791 if (ubi->vid_hdr_offset < 0)
792 return -EINVAL;
793
794 /*
795 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
796 * physical eraseblocks maximum.
797 */
798
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);
802 #else
803 ubi->peb_size = ubi->mtd->erasesize;
804 ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
805 #endif
806 ubi->flash_size = ubi->mtd->size;
807
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);
811 }
812
813 if (ubi->mtd->type == MTD_NORFLASH) {
814 ubi_assert(ubi->mtd->writesize == 1);
815 ubi->nor_flash = 1;
816 }
817
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;
821 #else
822 ubi->min_io_size = ubi->mtd->writesize;
823 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
824 #endif
825
826 /*
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.
830 */
831 if (!is_power_of_2(ubi->min_io_size)) {
832 ubi_err("min. I/O unit (%d) is not power of 2",
833 ubi->min_io_size);
834 return -EINVAL;
835 }
836
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);
840
841 #ifdef CONFIG_MTK_COMBO_NAND_SUPPORT
842 ubi->max_write_size = COMBO_NAND_PAGE_SIZE;
843 #else
844 ubi->max_write_size = ubi->mtd->writebufsize;
845 #endif
846 #ifdef CONFIG_MTK_MLC_NAND_SUPPORT
847 ubi->max_write_size = ubi->mtd->erasesize/4;
848 #endif
849 /*
850 * Maximum write size has to be greater or equivalent to min. I/O
851 * size, and be multiple of min. I/O size.
852 */
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);
858 return -EINVAL;
859 }
860
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);
864
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);
870
871 if (ubi->vid_hdr_offset == 0)
872 /* Default offset */
873 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
874 ubi->ec_hdr_alsize;
875 else {
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;
880 }
881
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);
885
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);
890
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",
894 ubi->vid_hdr_shift);
895 return -EINVAL;
896 }
897
898 /* Check sanity */
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);
905 return -EINVAL;
906 }
907
908 /*
909 * Set maximum amount of physical erroneous eraseblocks to be 10%.
910 * Erroneous PEB are those which have read errors.
911 */
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);
916
917 /*
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
920 * read-only mode.
921 */
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");
924 ubi->ro_mode = 1;
925 }
926
927 ubi->leb_size = ubi->peb_size - ubi->leb_start;
928
929 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
930 ubi_msg("MTD device %d is write-protected, attach in read-only mode",
931 ubi->mtd->index);
932 ubi->ro_mode = 1;
933 }
934
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);
945
946 /*
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.
952 */
953
954 return 0;
955 }
956
957 /**
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
961 *
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.
966 */
967 static int autoresize(struct ubi_device *ubi, int vol_id)
968 {
969 struct ubi_volume_desc desc;
970 struct ubi_volume *vol = ubi->volumes[vol_id];
971 int err, old_reserved_pebs = vol->reserved_pebs;
972
973 if (ubi->ro_mode) {
974 ubi_warn("skip auto-resize because of R/O mode");
975 return 0;
976 }
977
978 /*
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
981 * to the flash.
982 */
983 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
984
985 if (ubi->avail_pebs == 0) {
986 struct ubi_vtbl_record vtbl_rec;
987
988 /*
989 * No available PEBs to re-size the volume, clear the flag on
990 * flash and exit.
991 */
992 vtbl_rec = ubi->vtbl[vol_id];
993 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
994 if (err)
995 ubi_err("cannot clean auto-resize flag for volume %d",
996 vol_id);
997 } else {
998 desc.vol = vol;
999 err = ubi_resize_volume(&desc,
1000 old_reserved_pebs + ubi->avail_pebs);
1001 if (err)
1002 ubi_err("cannot auto-resize volume %d", vol_id);
1003 }
1004
1005 if (err)
1006 return err;
1007
1008 ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
1009 vol->name, old_reserved_pebs, vol->reserved_pebs);
1010 return 0;
1011 }
1012
1013 /**
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
1019 *
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.
1025 *
1026 * Note, the invocations of this function has to be serialized by the
1027 * @ubi_devices_mutex.
1028 */
1029 int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
1030 int vid_hdr_offset, int max_beb_per1024)
1031 {
1032 struct ubi_device *ubi;
1033 int i, err, ref = 0;
1034 unsigned long long attach_time = 0;
1035
1036 if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
1037 return -EINVAL;
1038
1039 if (!max_beb_per1024)
1040 max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
1041
1042 /*
1043 * Check if we already have the same MTD device attached.
1044 *
1045 * Note, this function assumes that UBI devices creations and deletions
1046 * are serialized, so it does not take the &ubi_devices_lock.
1047 */
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",
1052 mtd->index, i);
1053 return -EEXIST;
1054 }
1055 }
1056
1057 /*
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.
1064 */
1065 if (mtd->type == MTD_UBIVOLUME) {
1066 ubi_err("refuse attaching mtd%d - it is already emulated on top of UBI",
1067 mtd->index);
1068 return -EINVAL;
1069 }
1070
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])
1075 break;
1076 if (ubi_num == UBI_MAX_DEVICES) {
1077 ubi_err("only %d UBI devices may be created",
1078 UBI_MAX_DEVICES);
1079 return -ENFILE;
1080 }
1081 } else {
1082 if (ubi_num >= UBI_MAX_DEVICES)
1083 return -EINVAL;
1084
1085 /* Make sure ubi_num is not busy */
1086 if (ubi_devices[ubi_num]) {
1087 ubi_err("ubi%d already exists", ubi_num);
1088 return -EEXIST;
1089 }
1090 }
1091
1092 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
1093 if (!ubi)
1094 return -ENOMEM;
1095
1096 ubi->mtd = mtd;
1097 ubi->ubi_num = ubi_num;
1098 ubi->vid_hdr_offset = vid_hdr_offset;
1099 ubi->autoresize_vol_id = -1;
1100 //MTK start
1101 ubi->wl_th = CONFIG_MTD_UBI_WL_THRESHOLD;
1102 atomic_set(&ubi->ec_count, 0);
1103 atomic_set(&ubi->move_retry, 0);
1104 //MTK end
1105
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;
1109
1110 /*
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.
1113 */
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;
1118
1119 ubi->fm_wl_pool.max_size = UBI_FM_WL_POOL_SIZE;
1120 ubi->fm_disabled = !fm_autoconvert;
1121
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.",
1125 UBI_FM_MAX_START);
1126 ubi->fm_disabled = 1;
1127 }
1128
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);
1131 #else
1132 ubi->fm_disabled = 1;
1133 #endif
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);
1140
1141 ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
1142
1143 err = io_init(ubi, max_beb_per1024);
1144 if (err)
1145 goto out_free;
1146
1147 err = -ENOMEM;
1148 ubi->peb_buf = kmalloc(ubi->peb_size, GFP_KERNEL);
1149 if (!ubi->peb_buf)
1150 goto out_free;
1151
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);
1155 if (!ubi->fm_buf)
1156 goto out_free;
1157 #endif
1158 attach_time = sched_clock();
1159 err = ubi_attach(ubi, 0);
1160 if (err) {
1161 ubi_err("failed to attach mtd%d, error %d", mtd->index, err);
1162 goto out_free;
1163 }
1164
1165 if (ubi->autoresize_vol_id != -1) {
1166 err = autoresize(ubi, ubi->autoresize_vol_id);
1167 if (err)
1168 goto out_detach;
1169 }
1170
1171 /* Make device "available" before it becomes accessible via sysfs */
1172 ubi_devices[ubi_num] = ubi;
1173
1174 err = uif_init(ubi, &ref);
1175 if (err)
1176 goto out_detach;
1177
1178 err = ubi_debugfs_init_dev(ubi);
1179 if (err)
1180 goto out_uif;
1181
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,
1186 err);
1187 goto out_debugfs;
1188 }
1189
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,
1204 ubi->vtbl_slots);
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,
1207 ubi->image_seq);
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);
1210
1211 /*
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.
1214 */
1215 spin_lock(&ubi->wl_lock);
1216 ubi->thread_enabled = 1;
1217 wake_up_process(ubi->bgt_thread);
1218 spin_unlock(&ubi->wl_lock);
1219
1220 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
1221 return ubi_num;
1222
1223 out_debugfs:
1224 ubi_debugfs_exit_dev(ubi);
1225 out_uif:
1226 get_device(&ubi->dev);
1227 ubi_assert(ref);
1228 uif_close(ubi);
1229 out_detach:
1230 ubi_devices[ubi_num] = NULL;
1231 ubi_wl_close(ubi);
1232 ubi_free_internal_volumes(ubi);
1233 kfree(ubi->vtbl);
1234 out_free:
1235 kfree(ubi->peb_buf);
1236 kfree(ubi->fm_buf);
1237 if (ref)
1238 put_device(&ubi->dev);
1239 else
1240 kfree(ubi);
1241 return err;
1242 }
1243
1244 /**
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
1248 *
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
1252 * exist.
1253 *
1254 * Note, the invocations of this function has to be serialized by the
1255 * @ubi_devices_mutex.
1256 */
1257 int ubi_detach_mtd_dev(int ubi_num, int anyway)
1258 {
1259 struct ubi_device *ubi;
1260
1261 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1262 return -EINVAL;
1263
1264 ubi = ubi_get_device(ubi_num);
1265 if (!ubi)
1266 return -EINVAL;
1267
1268 spin_lock(&ubi_devices_lock);
1269 put_device(&ubi->dev);
1270 ubi->ref_count -= 1;
1271 if (ubi->ref_count) {
1272 if (!anyway) {
1273 spin_unlock(&ubi_devices_lock);
1274 return -EBUSY;
1275 }
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);
1279 }
1280 ubi_devices[ubi_num] = NULL;
1281 spin_unlock(&ubi_devices_lock);
1282
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);
1290 #endif
1291 /*
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.
1294 */
1295 if (ubi->bgt_thread)
1296 kthread_stop(ubi->bgt_thread);
1297
1298 /*
1299 * Get a reference to the device in order to prevent 'dev_release()'
1300 * from freeing the @ubi object.
1301 */
1302 get_device(&ubi->dev);
1303
1304 ubi_debugfs_exit_dev(ubi);
1305 uif_close(ubi);
1306
1307 ubi_wl_close(ubi);
1308 ubi_free_internal_volumes(ubi);
1309 kfree(ubi->vtbl);
1310 put_mtd_device(ubi->mtd);
1311 #ifdef CONFIG_BLB
1312 kfree(ubi->databuf);
1313 kfree(ubi->oobbuf);
1314 #endif
1315 kfree(ubi->peb_buf);
1316 kfree(ubi->fm_buf);
1317 ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
1318 put_device(&ubi->dev);
1319 return 0;
1320 }
1321
1322 /**
1323 * open_mtd_by_chdev - open an MTD device by its character device node path.
1324 * @mtd_dev: MTD character device node path
1325 *
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.
1329 */
1330 static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1331 {
1332 int err, major, minor, mode;
1333 struct path path;
1334
1335 /* Probably this is an MTD character device node path */
1336 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1337 if (err)
1338 return ERR_PTR(err);
1339
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;
1344 path_put(&path);
1345 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1346 return ERR_PTR(-EINVAL);
1347
1348 if (minor & 1)
1349 /*
1350 * Just do not think the "/dev/mtdrX" devices support is need,
1351 * so do not support them to avoid doing extra work.
1352 */
1353 return ERR_PTR(-EINVAL);
1354
1355 return get_mtd_device(NULL, minor / 2);
1356 }
1357
1358 /**
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
1361 *
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.
1367 */
1368 static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1369 {
1370 struct mtd_info *mtd;
1371 int mtd_num;
1372 char *endp;
1373
1374 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1375 if (*endp != '\0' || mtd_dev == endp) {
1376 /*
1377 * This does not look like an ASCII integer, probably this is
1378 * MTD device name.
1379 */
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);
1384 } else
1385 mtd = get_mtd_device(NULL, mtd_num);
1386
1387 return mtd;
1388 }
1389
1390 static int __init ubi_init(void)
1391 {
1392 int err, i, k;
1393
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);
1397
1398 if (mtd_devs > UBI_MAX_DEVICES) {
1399 ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
1400 return -EINVAL;
1401 }
1402
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");
1408 goto out;
1409 }
1410
1411 err = class_create_file(ubi_class, &ubi_version);
1412 if (err) {
1413 ubi_err("cannot create sysfs file");
1414 goto out_class;
1415 }
1416
1417 err = misc_register(&ubi_ctrl_cdev);
1418 if (err) {
1419 ubi_err("cannot register device");
1420 goto out_version;
1421 }
1422
1423 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
1424 sizeof(struct ubi_wl_entry),
1425 0, 0, NULL);
1426 if (!ubi_wl_entry_slab)
1427 goto out_dev_unreg;
1428
1429 err = ubi_debugfs_init();
1430 if (err)
1431 goto out_slab;
1432
1433
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;
1438
1439 cond_resched();
1440
1441 mtd = open_mtd_device(p->name);
1442 if (IS_ERR(mtd)) {
1443 err = PTR_ERR(mtd);
1444 goto out_detach;
1445 }
1446
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);
1451 if (err < 0) {
1452 ubi_err("cannot attach mtd%d", mtd->index);
1453 put_mtd_device(mtd);
1454
1455 /*
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.
1462 *
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.
1467 */
1468 if (ubi_is_module())
1469 goto out_detach;
1470 }
1471 }
1472
1473 return 0;
1474
1475 out_detach:
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);
1481 }
1482 ubi_debugfs_exit();
1483 out_slab:
1484 kmem_cache_destroy(ubi_wl_entry_slab);
1485 out_dev_unreg:
1486 misc_deregister(&ubi_ctrl_cdev);
1487 out_version:
1488 class_remove_file(ubi_class, &ubi_version);
1489 out_class:
1490 class_destroy(ubi_class);
1491 out:
1492 ubi_err("UBI error: cannot initialize UBI, error %d", err);
1493 return err;
1494 }
1495 late_initcall(ubi_init);
1496
1497 static void __exit ubi_exit(void)
1498 {
1499 int i;
1500
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);
1506 }
1507 ubi_debugfs_exit();
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);
1512 }
1513 module_exit(ubi_exit);
1514
1515 /**
1516 * bytes_str_to_int - convert a number of bytes string into an integer.
1517 * @str: the string to convert
1518 *
1519 * This function returns positive resulting integer in case of success and a
1520 * negative error code in case of failure.
1521 */
1522 static int __init bytes_str_to_int(const char *str)
1523 {
1524 char *endp;
1525 unsigned long result;
1526
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);
1530 return -EINVAL;
1531 }
1532
1533 switch (*endp) {
1534 case 'G':
1535 result *= 1024;
1536 case 'M':
1537 result *= 1024;
1538 case 'K':
1539 result *= 1024;
1540 if (endp[1] == 'i' && endp[2] == 'B')
1541 endp += 2;
1542 case '\0':
1543 break;
1544 default:
1545 ubi_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1546 return -EINVAL;
1547 }
1548
1549 return result;
1550 }
1551
1552 /**
1553 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1554 * @val: the parameter value to parse
1555 * @kp: not used
1556 *
1557 * This function returns zero in case of success and a negative error code in
1558 * case of error.
1559 */
1560 static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1561 {
1562 int i, len;
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];
1567
1568 if (!val)
1569 return -EINVAL;
1570
1571 if (mtd_devs == UBI_MAX_DEVICES) {
1572 ubi_err("UBI error: too many parameters, max. is %d\n",
1573 UBI_MAX_DEVICES);
1574 return -EINVAL;
1575 }
1576
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);
1581 return -EINVAL;
1582 }
1583
1584 if (len == 0) {
1585 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
1586 return 0;
1587 }
1588
1589 strcpy(buf, val);
1590
1591 /* Get rid of the final newline */
1592 if (buf[len - 1] == '\n')
1593 buf[len - 1] = '\0';
1594
1595 for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
1596 tokens[i] = strsep(&pbuf, ",");
1597
1598 if (pbuf) {
1599 ubi_err("UBI error: too many arguments at \"%s\"\n", val);
1600 return -EINVAL;
1601 }
1602
1603 p = &mtd_dev_param[mtd_devs];
1604 strcpy(&p->name[0], tokens[0]);
1605
1606 if (tokens[1])
1607 p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
1608
1609 if (p->vid_hdr_offs < 0)
1610 return p->vid_hdr_offs;
1611
1612 if (tokens[2]) {
1613 int err = kstrtoint(tokens[2], 10, &p->max_beb_per1024);
1614
1615 if (err) {
1616 ubi_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1617 tokens[2]);
1618 return -EINVAL;
1619 }
1620 }
1621
1622 mtd_devs += 1;
1623 return 0;
1624 }
1625
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"
1633 "\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.");
1641 #endif
1642 MODULE_VERSION(__stringify(UBI_VERSION));
1643 MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1644 MODULE_AUTHOR("Artem Bityutskiy");
1645 MODULE_LICENSE("GPL");