build.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2007
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Author: Artem Bityutskiy (Битюцкий Артём),
  8. * Frank Haverkamp
  9. */
  10. /*
  11. * This file includes UBI initialization and building of UBI devices.
  12. *
  13. * When UBI is initialized, it attaches all the MTD devices specified as the
  14. * module load parameters or the kernel boot parameters. If MTD devices were
  15. * specified, UBI does not attach any MTD device, but it is possible to do
  16. * later using the "UBI control device".
  17. */
  18. #ifndef __UBOOT__
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/stringify.h>
  22. #include <linux/namei.h>
  23. #include <linux/stat.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/log2.h>
  26. #include <linux/kthread.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/major.h>
  30. #else
  31. #include <linux/bug.h>
  32. #include <linux/log2.h>
  33. #endif
  34. #include <linux/err.h>
  35. #include <ubi_uboot.h>
  36. #include <linux/mtd/partitions.h>
  37. #include "ubi.h"
  38. /* Maximum length of the 'mtd=' parameter */
  39. #define MTD_PARAM_LEN_MAX 64
  40. /* Maximum number of comma-separated items in the 'mtd=' parameter */
  41. #define MTD_PARAM_MAX_COUNT 4
  42. /* Maximum value for the number of bad PEBs per 1024 PEBs */
  43. #define MAX_MTD_UBI_BEB_LIMIT 768
  44. #ifdef CONFIG_MTD_UBI_MODULE
  45. #define ubi_is_module() 1
  46. #else
  47. #define ubi_is_module() 0
  48. #endif
  49. #if (CONFIG_SYS_MALLOC_LEN < (512 << 10))
  50. #error Malloc area too small for UBI, increase CONFIG_SYS_MALLOC_LEN to >= 512k
  51. #endif
  52. /**
  53. * struct mtd_dev_param - MTD device parameter description data structure.
  54. * @name: MTD character device node path, MTD device name, or MTD device number
  55. * string
  56. * @vid_hdr_offs: VID header offset
  57. * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
  58. */
  59. struct mtd_dev_param {
  60. char name[MTD_PARAM_LEN_MAX];
  61. int ubi_num;
  62. int vid_hdr_offs;
  63. int max_beb_per1024;
  64. };
  65. /* Numbers of elements set in the @mtd_dev_param array */
  66. static int __initdata mtd_devs;
  67. /* MTD devices specification parameters */
  68. static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
  69. #ifndef __UBOOT__
  70. #ifdef CONFIG_MTD_UBI_FASTMAP
  71. /* UBI module parameter to enable fastmap automatically on non-fastmap images */
  72. static bool fm_autoconvert;
  73. static bool fm_debug;
  74. #endif
  75. #else
  76. #ifdef CONFIG_MTD_UBI_FASTMAP
  77. #if !defined(CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT)
  78. #define CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT 0
  79. #endif
  80. static bool fm_autoconvert = CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT;
  81. #if !defined(CONFIG_MTD_UBI_FM_DEBUG)
  82. #define CONFIG_MTD_UBI_FM_DEBUG 0
  83. #endif
  84. static bool fm_debug = CONFIG_MTD_UBI_FM_DEBUG;
  85. #endif
  86. #endif
  87. /* Slab cache for wear-leveling entries */
  88. struct kmem_cache *ubi_wl_entry_slab;
  89. #ifndef __UBOOT__
  90. /* UBI control character device */
  91. static struct miscdevice ubi_ctrl_cdev = {
  92. .minor = MISC_DYNAMIC_MINOR,
  93. .name = "ubi_ctrl",
  94. .fops = &ubi_ctrl_cdev_operations,
  95. };
  96. #endif
  97. /* All UBI devices in system */
  98. #ifndef __UBOOT__
  99. static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  100. #else
  101. struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  102. #endif
  103. #ifndef __UBOOT__
  104. /* Serializes UBI devices creations and removals */
  105. DEFINE_MUTEX(ubi_devices_mutex);
  106. /* Protects @ubi_devices and @ubi->ref_count */
  107. static DEFINE_SPINLOCK(ubi_devices_lock);
  108. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  109. static ssize_t ubi_version_show(struct class *class,
  110. struct class_attribute *attr, char *buf)
  111. {
  112. return sprintf(buf, "%d\n", UBI_VERSION);
  113. }
  114. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  115. static struct class_attribute ubi_class_attrs[] = {
  116. __ATTR(version, S_IRUGO, ubi_version_show, NULL),
  117. __ATTR_NULL
  118. };
  119. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  120. struct class ubi_class = {
  121. .name = UBI_NAME_STR,
  122. .owner = THIS_MODULE,
  123. .class_attrs = ubi_class_attrs,
  124. };
  125. static ssize_t dev_attribute_show(struct device *dev,
  126. struct device_attribute *attr, char *buf);
  127. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  128. static struct device_attribute dev_eraseblock_size =
  129. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  130. static struct device_attribute dev_avail_eraseblocks =
  131. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  132. static struct device_attribute dev_total_eraseblocks =
  133. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  134. static struct device_attribute dev_volumes_count =
  135. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  136. static struct device_attribute dev_max_ec =
  137. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  138. static struct device_attribute dev_reserved_for_bad =
  139. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  140. static struct device_attribute dev_bad_peb_count =
  141. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  142. static struct device_attribute dev_max_vol_count =
  143. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  144. static struct device_attribute dev_min_io_size =
  145. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  146. static struct device_attribute dev_bgt_enabled =
  147. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  148. static struct device_attribute dev_mtd_num =
  149. __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
  150. #endif
  151. /**
  152. * ubi_volume_notify - send a volume change notification.
  153. * @ubi: UBI device description object
  154. * @vol: volume description object of the changed volume
  155. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  156. *
  157. * This is a helper function which notifies all subscribers about a volume
  158. * change event (creation, removal, re-sizing, re-naming, updating). Returns
  159. * zero in case of success and a negative error code in case of failure.
  160. */
  161. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
  162. {
  163. int ret;
  164. struct ubi_notification nt;
  165. ubi_do_get_device_info(ubi, &nt.di);
  166. ubi_do_get_volume_info(ubi, vol, &nt.vi);
  167. switch (ntype) {
  168. case UBI_VOLUME_ADDED:
  169. case UBI_VOLUME_REMOVED:
  170. case UBI_VOLUME_RESIZED:
  171. case UBI_VOLUME_RENAMED:
  172. ret = ubi_update_fastmap(ubi);
  173. if (ret)
  174. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  175. }
  176. return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
  177. }
  178. /**
  179. * ubi_notify_all - send a notification to all volumes.
  180. * @ubi: UBI device description object
  181. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  182. * @nb: the notifier to call
  183. *
  184. * This function walks all volumes of UBI device @ubi and sends the @ntype
  185. * notification for each volume. If @nb is %NULL, then all registered notifiers
  186. * are called, otherwise only the @nb notifier is called. Returns the number of
  187. * sent notifications.
  188. */
  189. int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
  190. {
  191. struct ubi_notification nt;
  192. int i, count = 0;
  193. #ifndef __UBOOT__
  194. int ret;
  195. #endif
  196. ubi_do_get_device_info(ubi, &nt.di);
  197. mutex_lock(&ubi->device_mutex);
  198. for (i = 0; i < ubi->vtbl_slots; i++) {
  199. /*
  200. * Since the @ubi->device is locked, and we are not going to
  201. * change @ubi->volumes, we do not have to lock
  202. * @ubi->volumes_lock.
  203. */
  204. if (!ubi->volumes[i])
  205. continue;
  206. ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
  207. #ifndef __UBOOT__
  208. if (nb)
  209. nb->notifier_call(nb, ntype, &nt);
  210. else
  211. ret = blocking_notifier_call_chain(&ubi_notifiers, ntype,
  212. &nt);
  213. #endif
  214. count += 1;
  215. }
  216. mutex_unlock(&ubi->device_mutex);
  217. return count;
  218. }
  219. /**
  220. * ubi_enumerate_volumes - send "add" notification for all existing volumes.
  221. * @nb: the notifier to call
  222. *
  223. * This function walks all UBI devices and volumes and sends the
  224. * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
  225. * registered notifiers are called, otherwise only the @nb notifier is called.
  226. * Returns the number of sent notifications.
  227. */
  228. int ubi_enumerate_volumes(struct notifier_block *nb)
  229. {
  230. int i, count = 0;
  231. /*
  232. * Since the @ubi_devices_mutex is locked, and we are not going to
  233. * change @ubi_devices, we do not have to lock @ubi_devices_lock.
  234. */
  235. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  236. struct ubi_device *ubi = ubi_devices[i];
  237. if (!ubi)
  238. continue;
  239. count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
  240. }
  241. return count;
  242. }
  243. /**
  244. * ubi_get_device - get UBI device.
  245. * @ubi_num: UBI device number
  246. *
  247. * This function returns UBI device description object for UBI device number
  248. * @ubi_num, or %NULL if the device does not exist. This function increases the
  249. * device reference count to prevent removal of the device. In other words, the
  250. * device cannot be removed if its reference count is not zero.
  251. */
  252. struct ubi_device *ubi_get_device(int ubi_num)
  253. {
  254. struct ubi_device *ubi;
  255. spin_lock(&ubi_devices_lock);
  256. ubi = ubi_devices[ubi_num];
  257. if (ubi) {
  258. ubi_assert(ubi->ref_count >= 0);
  259. ubi->ref_count += 1;
  260. get_device(&ubi->dev);
  261. }
  262. spin_unlock(&ubi_devices_lock);
  263. return ubi;
  264. }
  265. /**
  266. * ubi_put_device - drop an UBI device reference.
  267. * @ubi: UBI device description object
  268. */
  269. void ubi_put_device(struct ubi_device *ubi)
  270. {
  271. spin_lock(&ubi_devices_lock);
  272. ubi->ref_count -= 1;
  273. put_device(&ubi->dev);
  274. spin_unlock(&ubi_devices_lock);
  275. }
  276. /**
  277. * ubi_get_by_major - get UBI device by character device major number.
  278. * @major: major number
  279. *
  280. * This function is similar to 'ubi_get_device()', but it searches the device
  281. * by its major number.
  282. */
  283. struct ubi_device *ubi_get_by_major(int major)
  284. {
  285. int i;
  286. struct ubi_device *ubi;
  287. spin_lock(&ubi_devices_lock);
  288. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  289. ubi = ubi_devices[i];
  290. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  291. ubi_assert(ubi->ref_count >= 0);
  292. ubi->ref_count += 1;
  293. get_device(&ubi->dev);
  294. spin_unlock(&ubi_devices_lock);
  295. return ubi;
  296. }
  297. }
  298. spin_unlock(&ubi_devices_lock);
  299. return NULL;
  300. }
  301. /**
  302. * ubi_major2num - get UBI device number by character device major number.
  303. * @major: major number
  304. *
  305. * This function searches UBI device number object by its major number. If UBI
  306. * device was not found, this function returns -ENODEV, otherwise the UBI device
  307. * number is returned.
  308. */
  309. int ubi_major2num(int major)
  310. {
  311. int i, ubi_num = -ENODEV;
  312. spin_lock(&ubi_devices_lock);
  313. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  314. struct ubi_device *ubi = ubi_devices[i];
  315. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  316. ubi_num = ubi->ubi_num;
  317. break;
  318. }
  319. }
  320. spin_unlock(&ubi_devices_lock);
  321. return ubi_num;
  322. }
  323. #ifndef __UBOOT__
  324. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  325. static ssize_t dev_attribute_show(struct device *dev,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. ssize_t ret;
  329. struct ubi_device *ubi;
  330. /*
  331. * The below code looks weird, but it actually makes sense. We get the
  332. * UBI device reference from the contained 'struct ubi_device'. But it
  333. * is unclear if the device was removed or not yet. Indeed, if the
  334. * device was removed before we increased its reference count,
  335. * 'ubi_get_device()' will return -ENODEV and we fail.
  336. *
  337. * Remember, 'struct ubi_device' is freed in the release function, so
  338. * we still can use 'ubi->ubi_num'.
  339. */
  340. ubi = container_of(dev, struct ubi_device, dev);
  341. ubi = ubi_get_device(ubi->ubi_num);
  342. if (!ubi)
  343. return -ENODEV;
  344. if (attr == &dev_eraseblock_size)
  345. ret = sprintf(buf, "%d\n", ubi->leb_size);
  346. else if (attr == &dev_avail_eraseblocks)
  347. ret = sprintf(buf, "%d\n", ubi->avail_pebs);
  348. else if (attr == &dev_total_eraseblocks)
  349. ret = sprintf(buf, "%d\n", ubi->good_peb_count);
  350. else if (attr == &dev_volumes_count)
  351. ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
  352. else if (attr == &dev_max_ec)
  353. ret = sprintf(buf, "%d\n", ubi->max_ec);
  354. else if (attr == &dev_reserved_for_bad)
  355. ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  356. else if (attr == &dev_bad_peb_count)
  357. ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
  358. else if (attr == &dev_max_vol_count)
  359. ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
  360. else if (attr == &dev_min_io_size)
  361. ret = sprintf(buf, "%d\n", ubi->min_io_size);
  362. else if (attr == &dev_bgt_enabled)
  363. ret = sprintf(buf, "%d\n", ubi->thread_enabled);
  364. else if (attr == &dev_mtd_num)
  365. ret = sprintf(buf, "%d\n", ubi->mtd->index);
  366. else
  367. ret = -EINVAL;
  368. ubi_put_device(ubi);
  369. return ret;
  370. }
  371. static struct attribute *ubi_dev_attrs[] = {
  372. &dev_eraseblock_size.attr,
  373. &dev_avail_eraseblocks.attr,
  374. &dev_total_eraseblocks.attr,
  375. &dev_volumes_count.attr,
  376. &dev_max_ec.attr,
  377. &dev_reserved_for_bad.attr,
  378. &dev_bad_peb_count.attr,
  379. &dev_max_vol_count.attr,
  380. &dev_min_io_size.attr,
  381. &dev_bgt_enabled.attr,
  382. &dev_mtd_num.attr,
  383. NULL
  384. };
  385. ATTRIBUTE_GROUPS(ubi_dev);
  386. static void dev_release(struct device *dev)
  387. {
  388. struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
  389. kfree(ubi);
  390. }
  391. /**
  392. * ubi_sysfs_init - initialize sysfs for an UBI device.
  393. * @ubi: UBI device description object
  394. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  395. * taken
  396. *
  397. * This function returns zero in case of success and a negative error code in
  398. * case of failure.
  399. */
  400. static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
  401. {
  402. int err;
  403. ubi->dev.release = dev_release;
  404. ubi->dev.devt = ubi->cdev.dev;
  405. ubi->dev.class = &ubi_class;
  406. ubi->dev.groups = ubi_dev_groups;
  407. dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
  408. err = device_register(&ubi->dev);
  409. if (err)
  410. return err;
  411. *ref = 1;
  412. return 0;
  413. }
  414. /**
  415. * ubi_sysfs_close - close sysfs for an UBI device.
  416. * @ubi: UBI device description object
  417. */
  418. static void ubi_sysfs_close(struct ubi_device *ubi)
  419. {
  420. device_unregister(&ubi->dev);
  421. }
  422. #endif
  423. /**
  424. * kill_volumes - destroy all user volumes.
  425. * @ubi: UBI device description object
  426. */
  427. static void kill_volumes(struct ubi_device *ubi)
  428. {
  429. int i;
  430. for (i = 0; i < ubi->vtbl_slots; i++)
  431. if (ubi->volumes[i])
  432. ubi_free_volume(ubi, ubi->volumes[i]);
  433. }
  434. /**
  435. * uif_init - initialize user interfaces for an UBI device.
  436. * @ubi: UBI device description object
  437. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  438. * taken, otherwise set to %0
  439. *
  440. * This function initializes various user interfaces for an UBI device. If the
  441. * initialization fails at an early stage, this function frees all the
  442. * resources it allocated, returns an error, and @ref is set to %0. However,
  443. * if the initialization fails after the UBI device was registered in the
  444. * driver core subsystem, this function takes a reference to @ubi->dev, because
  445. * otherwise the release function ('dev_release()') would free whole @ubi
  446. * object. The @ref argument is set to %1 in this case. The caller has to put
  447. * this reference.
  448. *
  449. * This function returns zero in case of success and a negative error code in
  450. * case of failure.
  451. */
  452. static int uif_init(struct ubi_device *ubi, int *ref)
  453. {
  454. int i, err;
  455. #ifndef __UBOOT__
  456. dev_t dev;
  457. #endif
  458. *ref = 0;
  459. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  460. /*
  461. * Major numbers for the UBI character devices are allocated
  462. * dynamically. Major numbers of volume character devices are
  463. * equivalent to ones of the corresponding UBI character device. Minor
  464. * numbers of UBI character devices are 0, while minor numbers of
  465. * volume character devices start from 1. Thus, we allocate one major
  466. * number and ubi->vtbl_slots + 1 minor numbers.
  467. */
  468. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  469. if (err) {
  470. ubi_err(ubi, "cannot register UBI character devices");
  471. return err;
  472. }
  473. ubi_assert(MINOR(dev) == 0);
  474. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  475. dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
  476. ubi->cdev.owner = THIS_MODULE;
  477. err = cdev_add(&ubi->cdev, dev, 1);
  478. if (err) {
  479. ubi_err(ubi, "cannot add character device");
  480. goto out_unreg;
  481. }
  482. err = ubi_sysfs_init(ubi, ref);
  483. if (err)
  484. goto out_sysfs;
  485. for (i = 0; i < ubi->vtbl_slots; i++)
  486. if (ubi->volumes[i]) {
  487. err = ubi_add_volume(ubi, ubi->volumes[i]);
  488. if (err) {
  489. ubi_err(ubi, "cannot add volume %d", i);
  490. goto out_volumes;
  491. }
  492. }
  493. return 0;
  494. out_volumes:
  495. kill_volumes(ubi);
  496. out_sysfs:
  497. if (*ref)
  498. get_device(&ubi->dev);
  499. ubi_sysfs_close(ubi);
  500. cdev_del(&ubi->cdev);
  501. out_unreg:
  502. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  503. ubi_err(ubi, "cannot initialize UBI %s, error %d",
  504. ubi->ubi_name, err);
  505. return err;
  506. }
  507. /**
  508. * uif_close - close user interfaces for an UBI device.
  509. * @ubi: UBI device description object
  510. *
  511. * Note, since this function un-registers UBI volume device objects (@vol->dev),
  512. * the memory allocated voe the volumes is freed as well (in the release
  513. * function).
  514. */
  515. static void uif_close(struct ubi_device *ubi)
  516. {
  517. kill_volumes(ubi);
  518. ubi_sysfs_close(ubi);
  519. cdev_del(&ubi->cdev);
  520. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  521. }
  522. /**
  523. * ubi_free_internal_volumes - free internal volumes.
  524. * @ubi: UBI device description object
  525. */
  526. void ubi_free_internal_volumes(struct ubi_device *ubi)
  527. {
  528. int i;
  529. for (i = ubi->vtbl_slots;
  530. i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  531. kfree(ubi->volumes[i]->eba_tbl);
  532. kfree(ubi->volumes[i]);
  533. }
  534. }
  535. static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
  536. {
  537. int limit, device_pebs;
  538. uint64_t device_size;
  539. if (!max_beb_per1024)
  540. return 0;
  541. /*
  542. * Here we are using size of the entire flash chip and
  543. * not just the MTD partition size because the maximum
  544. * number of bad eraseblocks is a percentage of the
  545. * whole device and bad eraseblocks are not fairly
  546. * distributed over the flash chip. So the worst case
  547. * is that all the bad eraseblocks of the chip are in
  548. * the MTD partition we are attaching (ubi->mtd).
  549. */
  550. device_size = mtd_get_device_size(ubi->mtd);
  551. device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
  552. limit = mult_frac(device_pebs, max_beb_per1024, 1024);
  553. /* Round it up */
  554. if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
  555. limit += 1;
  556. return limit;
  557. }
  558. /**
  559. * io_init - initialize I/O sub-system for a given UBI device.
  560. * @ubi: UBI device description object
  561. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  562. *
  563. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  564. * assumed:
  565. * o EC header is always at offset zero - this cannot be changed;
  566. * o VID header starts just after the EC header at the closest address
  567. * aligned to @io->hdrs_min_io_size;
  568. * o data starts just after the VID header at the closest address aligned to
  569. * @io->min_io_size
  570. *
  571. * This function returns zero in case of success and a negative error code in
  572. * case of failure.
  573. */
  574. static int io_init(struct ubi_device *ubi, int max_beb_per1024)
  575. {
  576. dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
  577. dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
  578. if (ubi->mtd->numeraseregions != 0) {
  579. /*
  580. * Some flashes have several erase regions. Different regions
  581. * may have different eraseblock size and other
  582. * characteristics. It looks like mostly multi-region flashes
  583. * have one "main" region and one or more small regions to
  584. * store boot loader code or boot parameters or whatever. I
  585. * guess we should just pick the largest region. But this is
  586. * not implemented.
  587. */
  588. ubi_err(ubi, "multiple regions, not implemented");
  589. return -EINVAL;
  590. }
  591. if (ubi->vid_hdr_offset < 0)
  592. return -EINVAL;
  593. /*
  594. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  595. * physical eraseblocks maximum.
  596. */
  597. ubi->peb_size = ubi->mtd->erasesize;
  598. ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
  599. ubi->flash_size = ubi->mtd->size;
  600. if (mtd_can_have_bb(ubi->mtd)) {
  601. ubi->bad_allowed = 1;
  602. ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
  603. }
  604. if (ubi->mtd->type == MTD_NORFLASH) {
  605. ubi_assert(ubi->mtd->writesize == 1);
  606. ubi->nor_flash = 1;
  607. }
  608. ubi->min_io_size = ubi->mtd->writesize;
  609. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  610. /*
  611. * Make sure minimal I/O unit is power of 2. Note, there is no
  612. * fundamental reason for this assumption. It is just an optimization
  613. * which allows us to avoid costly division operations.
  614. */
  615. if (!is_power_of_2(ubi->min_io_size)) {
  616. ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
  617. ubi->min_io_size);
  618. return -EINVAL;
  619. }
  620. ubi_assert(ubi->hdrs_min_io_size > 0);
  621. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  622. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  623. ubi->max_write_size = ubi->mtd->writebufsize;
  624. /*
  625. * Maximum write size has to be greater or equivalent to min. I/O
  626. * size, and be multiple of min. I/O size.
  627. */
  628. if (ubi->max_write_size < ubi->min_io_size ||
  629. ubi->max_write_size % ubi->min_io_size ||
  630. !is_power_of_2(ubi->max_write_size)) {
  631. ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
  632. ubi->max_write_size, ubi->min_io_size);
  633. return -EINVAL;
  634. }
  635. /* Calculate default aligned sizes of EC and VID headers */
  636. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  637. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  638. dbg_gen("min_io_size %d", ubi->min_io_size);
  639. dbg_gen("max_write_size %d", ubi->max_write_size);
  640. dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  641. dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  642. dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  643. if (ubi->vid_hdr_offset == 0)
  644. /* Default offset */
  645. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  646. ubi->ec_hdr_alsize;
  647. else {
  648. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  649. ~(ubi->hdrs_min_io_size - 1);
  650. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  651. ubi->vid_hdr_aloffset;
  652. }
  653. /* Similar for the data offset */
  654. ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
  655. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  656. dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
  657. dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  658. dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
  659. dbg_gen("leb_start %d", ubi->leb_start);
  660. /* The shift must be aligned to 32-bit boundary */
  661. if (ubi->vid_hdr_shift % 4) {
  662. ubi_err(ubi, "unaligned VID header shift %d",
  663. ubi->vid_hdr_shift);
  664. return -EINVAL;
  665. }
  666. /* Check sanity */
  667. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  668. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  669. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  670. ubi->leb_start & (ubi->min_io_size - 1)) {
  671. ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
  672. ubi->vid_hdr_offset, ubi->leb_start);
  673. return -EINVAL;
  674. }
  675. /*
  676. * Set maximum amount of physical erroneous eraseblocks to be 10%.
  677. * Erroneous PEB are those which have read errors.
  678. */
  679. ubi->max_erroneous = ubi->peb_count / 10;
  680. if (ubi->max_erroneous < 16)
  681. ubi->max_erroneous = 16;
  682. dbg_gen("max_erroneous %d", ubi->max_erroneous);
  683. /*
  684. * It may happen that EC and VID headers are situated in one minimal
  685. * I/O unit. In this case we can only accept this UBI image in
  686. * read-only mode.
  687. */
  688. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  689. ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
  690. ubi->ro_mode = 1;
  691. }
  692. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  693. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  694. ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
  695. ubi->mtd->index);
  696. ubi->ro_mode = 1;
  697. }
  698. /*
  699. * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
  700. * unfortunately, MTD does not provide this information. We should loop
  701. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  702. * each physical eraseblock. So, we leave @ubi->bad_peb_count
  703. * uninitialized so far.
  704. */
  705. return 0;
  706. }
  707. /**
  708. * autoresize - re-size the volume which has the "auto-resize" flag set.
  709. * @ubi: UBI device description object
  710. * @vol_id: ID of the volume to re-size
  711. *
  712. * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
  713. * the volume table to the largest possible size. See comments in ubi-header.h
  714. * for more description of the flag. Returns zero in case of success and a
  715. * negative error code in case of failure.
  716. */
  717. static int autoresize(struct ubi_device *ubi, int vol_id)
  718. {
  719. struct ubi_volume_desc desc;
  720. struct ubi_volume *vol = ubi->volumes[vol_id];
  721. int err, old_reserved_pebs = vol->reserved_pebs;
  722. if (ubi->ro_mode) {
  723. ubi_warn(ubi, "skip auto-resize because of R/O mode");
  724. return 0;
  725. }
  726. /*
  727. * Clear the auto-resize flag in the volume in-memory copy of the
  728. * volume table, and 'ubi_resize_volume()' will propagate this change
  729. * to the flash.
  730. */
  731. ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
  732. if (ubi->avail_pebs == 0) {
  733. struct ubi_vtbl_record vtbl_rec;
  734. /*
  735. * No available PEBs to re-size the volume, clear the flag on
  736. * flash and exit.
  737. */
  738. vtbl_rec = ubi->vtbl[vol_id];
  739. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  740. if (err)
  741. ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
  742. vol_id);
  743. } else {
  744. desc.vol = vol;
  745. err = ubi_resize_volume(&desc,
  746. old_reserved_pebs + ubi->avail_pebs);
  747. if (err)
  748. ubi_err(ubi, "cannot auto-resize volume %d",
  749. vol_id);
  750. }
  751. if (err)
  752. return err;
  753. ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
  754. vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
  755. return 0;
  756. }
  757. /**
  758. * ubi_attach_mtd_dev - attach an MTD device.
  759. * @mtd: MTD device description object
  760. * @ubi_num: number to assign to the new UBI device
  761. * @vid_hdr_offset: VID header offset
  762. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  763. *
  764. * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
  765. * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
  766. * which case this function finds a vacant device number and assigns it
  767. * automatically. Returns the new UBI device number in case of success and a
  768. * negative error code in case of failure.
  769. *
  770. * Note, the invocations of this function has to be serialized by the
  771. * @ubi_devices_mutex.
  772. */
  773. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  774. int vid_hdr_offset, int max_beb_per1024)
  775. {
  776. struct ubi_device *ubi;
  777. int i, err, ref = 0;
  778. if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
  779. return -EINVAL;
  780. if (!max_beb_per1024)
  781. max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
  782. /*
  783. * Check if we already have the same MTD device attached.
  784. *
  785. * Note, this function assumes that UBI devices creations and deletions
  786. * are serialized, so it does not take the &ubi_devices_lock.
  787. */
  788. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  789. ubi = ubi_devices[i];
  790. if (ubi && mtd->index == ubi->mtd->index) {
  791. ubi_err(ubi, "mtd%d is already attached to ubi%d",
  792. mtd->index, i);
  793. return -EEXIST;
  794. }
  795. }
  796. /*
  797. * Make sure this MTD device is not emulated on top of an UBI volume
  798. * already. Well, generally this recursion works fine, but there are
  799. * different problems like the UBI module takes a reference to itself
  800. * by attaching (and thus, opening) the emulated MTD device. This
  801. * results in inability to unload the module. And in general it makes
  802. * no sense to attach emulated MTD devices, so we prohibit this.
  803. */
  804. if (mtd->type == MTD_UBIVOLUME) {
  805. ubi_err(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
  806. mtd->index);
  807. return -EINVAL;
  808. }
  809. if (ubi_num == UBI_DEV_NUM_AUTO) {
  810. /* Search for an empty slot in the @ubi_devices array */
  811. for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
  812. if (!ubi_devices[ubi_num])
  813. break;
  814. if (ubi_num == UBI_MAX_DEVICES) {
  815. ubi_err(ubi, "only %d UBI devices may be created",
  816. UBI_MAX_DEVICES);
  817. return -ENFILE;
  818. }
  819. } else {
  820. if (ubi_num >= UBI_MAX_DEVICES)
  821. return -EINVAL;
  822. /* Make sure ubi_num is not busy */
  823. if (ubi_devices[ubi_num]) {
  824. ubi_err(ubi, "already exists");
  825. return -EEXIST;
  826. }
  827. }
  828. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  829. if (!ubi)
  830. return -ENOMEM;
  831. ubi->mtd = mtd;
  832. ubi->ubi_num = ubi_num;
  833. ubi->vid_hdr_offset = vid_hdr_offset;
  834. ubi->autoresize_vol_id = -1;
  835. #ifdef CONFIG_MTD_UBI_FASTMAP
  836. ubi->fm_pool.used = ubi->fm_pool.size = 0;
  837. ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
  838. /*
  839. * fm_pool.max_size is 5% of the total number of PEBs but it's also
  840. * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
  841. */
  842. ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
  843. ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
  844. ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
  845. UBI_FM_MIN_POOL_SIZE);
  846. ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
  847. ubi->fm_disabled = !fm_autoconvert;
  848. if (fm_debug)
  849. ubi_enable_dbg_chk_fastmap(ubi);
  850. if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
  851. <= UBI_FM_MAX_START) {
  852. ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
  853. UBI_FM_MAX_START);
  854. ubi->fm_disabled = 1;
  855. }
  856. ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
  857. ubi_msg(ubi, "default fastmap WL pool size: %d",
  858. ubi->fm_wl_pool.max_size);
  859. #else
  860. ubi->fm_disabled = 1;
  861. #endif
  862. mutex_init(&ubi->buf_mutex);
  863. mutex_init(&ubi->ckvol_mutex);
  864. mutex_init(&ubi->device_mutex);
  865. spin_lock_init(&ubi->volumes_lock);
  866. init_rwsem(&ubi->fm_protect);
  867. init_rwsem(&ubi->fm_eba_sem);
  868. ubi_msg(ubi, "attaching mtd%d", mtd->index);
  869. err = io_init(ubi, max_beb_per1024);
  870. if (err)
  871. goto out_free;
  872. err = -ENOMEM;
  873. ubi->peb_buf = vmalloc(ubi->peb_size);
  874. if (!ubi->peb_buf)
  875. goto out_free;
  876. #ifdef CONFIG_MTD_UBI_FASTMAP
  877. ubi->fm_size = ubi_calc_fm_size(ubi);
  878. ubi->fm_buf = vzalloc(ubi->fm_size);
  879. if (!ubi->fm_buf)
  880. goto out_free;
  881. #endif
  882. err = ubi_attach(ubi, 0);
  883. if (err) {
  884. ubi_err(ubi, "failed to attach mtd%d, error %d",
  885. mtd->index, err);
  886. goto out_free;
  887. }
  888. if (ubi->autoresize_vol_id != -1) {
  889. err = autoresize(ubi, ubi->autoresize_vol_id);
  890. if (err)
  891. goto out_detach;
  892. }
  893. err = uif_init(ubi, &ref);
  894. if (err)
  895. goto out_detach;
  896. err = ubi_debugfs_init_dev(ubi);
  897. if (err)
  898. goto out_uif;
  899. ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
  900. if (IS_ERR(ubi->bgt_thread)) {
  901. err = PTR_ERR(ubi->bgt_thread);
  902. ubi_err(ubi, "cannot spawn \"%s\", error %d",
  903. ubi->bgt_name, err);
  904. goto out_debugfs;
  905. }
  906. ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
  907. mtd->index, mtd->name, ubi->flash_size >> 20);
  908. ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
  909. ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
  910. ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
  911. ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
  912. ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
  913. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
  914. ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
  915. ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
  916. ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
  917. ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
  918. ubi->vtbl_slots);
  919. ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
  920. ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
  921. ubi->image_seq);
  922. ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
  923. ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
  924. /*
  925. * The below lock makes sure we do not race with 'ubi_thread()' which
  926. * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
  927. */
  928. spin_lock(&ubi->wl_lock);
  929. ubi->thread_enabled = 1;
  930. #ifndef __UBOOT__
  931. wake_up_process(ubi->bgt_thread);
  932. #else
  933. /*
  934. * U-Boot special: We have no bgt_thread in U-Boot!
  935. * So just call do_work() here directly.
  936. */
  937. err = do_work(ubi);
  938. if (err) {
  939. ubi_err(ubi, "%s: work failed with error code %d",
  940. ubi->bgt_name, err);
  941. }
  942. #endif
  943. spin_unlock(&ubi->wl_lock);
  944. ubi_devices[ubi_num] = ubi;
  945. ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
  946. return ubi_num;
  947. out_debugfs:
  948. ubi_debugfs_exit_dev(ubi);
  949. out_uif:
  950. get_device(&ubi->dev);
  951. ubi_assert(ref);
  952. uif_close(ubi);
  953. out_detach:
  954. ubi_wl_close(ubi);
  955. ubi_free_internal_volumes(ubi);
  956. vfree(ubi->vtbl);
  957. out_free:
  958. vfree(ubi->peb_buf);
  959. vfree(ubi->fm_buf);
  960. if (ref)
  961. put_device(&ubi->dev);
  962. else
  963. kfree(ubi);
  964. return err;
  965. }
  966. /**
  967. * ubi_detach_mtd_dev - detach an MTD device.
  968. * @ubi_num: UBI device number to detach from
  969. * @anyway: detach MTD even if device reference count is not zero
  970. *
  971. * This function destroys an UBI device number @ubi_num and detaches the
  972. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  973. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  974. * exist.
  975. *
  976. * Note, the invocations of this function has to be serialized by the
  977. * @ubi_devices_mutex.
  978. */
  979. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  980. {
  981. struct ubi_device *ubi;
  982. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  983. return -EINVAL;
  984. ubi = ubi_get_device(ubi_num);
  985. if (!ubi)
  986. return -EINVAL;
  987. spin_lock(&ubi_devices_lock);
  988. put_device(&ubi->dev);
  989. ubi->ref_count -= 1;
  990. if (ubi->ref_count) {
  991. if (!anyway) {
  992. spin_unlock(&ubi_devices_lock);
  993. return -EBUSY;
  994. }
  995. /* This may only happen if there is a bug */
  996. ubi_err(ubi, "%s reference count %d, destroy anyway",
  997. ubi->ubi_name, ubi->ref_count);
  998. }
  999. ubi_devices[ubi_num] = NULL;
  1000. spin_unlock(&ubi_devices_lock);
  1001. ubi_assert(ubi_num == ubi->ubi_num);
  1002. ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
  1003. ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
  1004. #ifdef CONFIG_MTD_UBI_FASTMAP
  1005. /* If we don't write a new fastmap at detach time we lose all
  1006. * EC updates that have been made since the last written fastmap.
  1007. * In case of fastmap debugging we omit the update to simulate an
  1008. * unclean shutdown. */
  1009. if (!ubi_dbg_chk_fastmap(ubi))
  1010. ubi_update_fastmap(ubi);
  1011. #endif
  1012. /*
  1013. * Before freeing anything, we have to stop the background thread to
  1014. * prevent it from doing anything on this device while we are freeing.
  1015. */
  1016. if (ubi->bgt_thread)
  1017. kthread_stop(ubi->bgt_thread);
  1018. /*
  1019. * Get a reference to the device in order to prevent 'dev_release()'
  1020. * from freeing the @ubi object.
  1021. */
  1022. get_device(&ubi->dev);
  1023. ubi_debugfs_exit_dev(ubi);
  1024. uif_close(ubi);
  1025. ubi_wl_close(ubi);
  1026. ubi_free_internal_volumes(ubi);
  1027. vfree(ubi->vtbl);
  1028. put_mtd_device(ubi->mtd);
  1029. vfree(ubi->peb_buf);
  1030. vfree(ubi->fm_buf);
  1031. ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
  1032. put_device(&ubi->dev);
  1033. return 0;
  1034. }
  1035. #ifndef __UBOOT__
  1036. /**
  1037. * open_mtd_by_chdev - open an MTD device by its character device node path.
  1038. * @mtd_dev: MTD character device node path
  1039. *
  1040. * This helper function opens an MTD device by its character node device path.
  1041. * Returns MTD device description object in case of success and a negative
  1042. * error code in case of failure.
  1043. */
  1044. static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
  1045. {
  1046. int err, major, minor, mode;
  1047. struct path path;
  1048. /* Probably this is an MTD character device node path */
  1049. err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
  1050. if (err)
  1051. return ERR_PTR(err);
  1052. /* MTD device number is defined by the major / minor numbers */
  1053. major = imajor(d_backing_inode(path.dentry));
  1054. minor = iminor(d_backing_inode(path.dentry));
  1055. mode = d_backing_inode(path.dentry)->i_mode;
  1056. path_put(&path);
  1057. if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
  1058. return ERR_PTR(-EINVAL);
  1059. if (minor & 1)
  1060. /*
  1061. * Just do not think the "/dev/mtdrX" devices support is need,
  1062. * so do not support them to avoid doing extra work.
  1063. */
  1064. return ERR_PTR(-EINVAL);
  1065. return get_mtd_device(NULL, minor / 2);
  1066. }
  1067. #endif
  1068. /**
  1069. * open_mtd_device - open MTD device by name, character device path, or number.
  1070. * @mtd_dev: name, character device node path, or MTD device device number
  1071. *
  1072. * This function tries to open and MTD device described by @mtd_dev string,
  1073. * which is first treated as ASCII MTD device number, and if it is not true, it
  1074. * is treated as MTD device name, and if that is also not true, it is treated
  1075. * as MTD character device node path. Returns MTD device description object in
  1076. * case of success and a negative error code in case of failure.
  1077. */
  1078. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  1079. {
  1080. struct mtd_info *mtd;
  1081. int mtd_num;
  1082. char *endp;
  1083. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  1084. if (*endp != '\0' || mtd_dev == endp) {
  1085. /*
  1086. * This does not look like an ASCII integer, probably this is
  1087. * MTD device name.
  1088. */
  1089. mtd = get_mtd_device_nm(mtd_dev);
  1090. #ifndef __UBOOT__
  1091. if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
  1092. /* Probably this is an MTD character device node path */
  1093. mtd = open_mtd_by_chdev(mtd_dev);
  1094. #endif
  1095. } else
  1096. mtd = get_mtd_device(NULL, mtd_num);
  1097. return mtd;
  1098. }
  1099. #ifndef __UBOOT__
  1100. static int __init ubi_init(void)
  1101. #else
  1102. int ubi_init(void)
  1103. #endif
  1104. {
  1105. int err, i, k;
  1106. /* Ensure that EC and VID headers have correct size */
  1107. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  1108. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  1109. if (mtd_devs > UBI_MAX_DEVICES) {
  1110. pr_err("UBI error: too many MTD devices, maximum is %d",
  1111. UBI_MAX_DEVICES);
  1112. return -EINVAL;
  1113. }
  1114. /* Create base sysfs directory and sysfs files */
  1115. err = class_register(&ubi_class);
  1116. if (err < 0)
  1117. return err;
  1118. err = misc_register(&ubi_ctrl_cdev);
  1119. if (err) {
  1120. pr_err("UBI error: cannot register device");
  1121. goto out;
  1122. }
  1123. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  1124. sizeof(struct ubi_wl_entry),
  1125. 0, 0, NULL);
  1126. if (!ubi_wl_entry_slab) {
  1127. err = -ENOMEM;
  1128. goto out_dev_unreg;
  1129. }
  1130. err = ubi_debugfs_init();
  1131. if (err)
  1132. goto out_slab;
  1133. /* Attach MTD devices */
  1134. for (i = 0; i < mtd_devs; i++) {
  1135. struct mtd_dev_param *p = &mtd_dev_param[i];
  1136. struct mtd_info *mtd;
  1137. cond_resched();
  1138. mtd = open_mtd_device(p->name);
  1139. if (IS_ERR(mtd)) {
  1140. err = PTR_ERR(mtd);
  1141. pr_err("UBI error: cannot open mtd %s, error %d",
  1142. p->name, err);
  1143. /* See comment below re-ubi_is_module(). */
  1144. if (ubi_is_module())
  1145. goto out_detach;
  1146. continue;
  1147. }
  1148. mutex_lock(&ubi_devices_mutex);
  1149. err = ubi_attach_mtd_dev(mtd, p->ubi_num,
  1150. p->vid_hdr_offs, p->max_beb_per1024);
  1151. mutex_unlock(&ubi_devices_mutex);
  1152. if (err < 0) {
  1153. pr_err("UBI error: cannot attach mtd%d",
  1154. mtd->index);
  1155. put_mtd_device(mtd);
  1156. /*
  1157. * Originally UBI stopped initializing on any error.
  1158. * However, later on it was found out that this
  1159. * behavior is not very good when UBI is compiled into
  1160. * the kernel and the MTD devices to attach are passed
  1161. * through the command line. Indeed, UBI failure
  1162. * stopped whole boot sequence.
  1163. *
  1164. * To fix this, we changed the behavior for the
  1165. * non-module case, but preserved the old behavior for
  1166. * the module case, just for compatibility. This is a
  1167. * little inconsistent, though.
  1168. */
  1169. if (ubi_is_module())
  1170. goto out_detach;
  1171. }
  1172. }
  1173. err = ubiblock_init();
  1174. if (err) {
  1175. pr_err("UBI error: block: cannot initialize, error %d", err);
  1176. /* See comment above re-ubi_is_module(). */
  1177. if (ubi_is_module())
  1178. goto out_detach;
  1179. }
  1180. return 0;
  1181. out_detach:
  1182. for (k = 0; k < i; k++)
  1183. if (ubi_devices[k]) {
  1184. mutex_lock(&ubi_devices_mutex);
  1185. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  1186. mutex_unlock(&ubi_devices_mutex);
  1187. }
  1188. ubi_debugfs_exit();
  1189. out_slab:
  1190. kmem_cache_destroy(ubi_wl_entry_slab);
  1191. out_dev_unreg:
  1192. misc_deregister(&ubi_ctrl_cdev);
  1193. out:
  1194. #ifdef __UBOOT__
  1195. /* Reset any globals that the driver depends on being zeroed */
  1196. mtd_devs = 0;
  1197. #endif
  1198. class_unregister(&ubi_class);
  1199. pr_err("UBI error: cannot initialize UBI, error %d", err);
  1200. return err;
  1201. }
  1202. late_initcall(ubi_init);
  1203. #ifndef __UBOOT__
  1204. static void __exit ubi_exit(void)
  1205. #else
  1206. void ubi_exit(void)
  1207. #endif
  1208. {
  1209. int i;
  1210. ubiblock_exit();
  1211. for (i = 0; i < UBI_MAX_DEVICES; i++)
  1212. if (ubi_devices[i]) {
  1213. mutex_lock(&ubi_devices_mutex);
  1214. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  1215. mutex_unlock(&ubi_devices_mutex);
  1216. }
  1217. ubi_debugfs_exit();
  1218. kmem_cache_destroy(ubi_wl_entry_slab);
  1219. misc_deregister(&ubi_ctrl_cdev);
  1220. class_unregister(&ubi_class);
  1221. #ifdef __UBOOT__
  1222. /* Reset any globals that the driver depends on being zeroed */
  1223. mtd_devs = 0;
  1224. #endif
  1225. }
  1226. module_exit(ubi_exit);
  1227. /**
  1228. * bytes_str_to_int - convert a number of bytes string into an integer.
  1229. * @str: the string to convert
  1230. *
  1231. * This function returns positive resulting integer in case of success and a
  1232. * negative error code in case of failure.
  1233. */
  1234. static int __init bytes_str_to_int(const char *str)
  1235. {
  1236. char *endp;
  1237. unsigned long result;
  1238. result = simple_strtoul(str, &endp, 0);
  1239. if (str == endp || result >= INT_MAX) {
  1240. pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
  1241. return -EINVAL;
  1242. }
  1243. switch (*endp) {
  1244. case 'G':
  1245. result *= 1024;
  1246. case 'M':
  1247. result *= 1024;
  1248. case 'K':
  1249. result *= 1024;
  1250. if (endp[1] == 'i' && endp[2] == 'B')
  1251. endp += 2;
  1252. case '\0':
  1253. break;
  1254. default:
  1255. pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
  1256. return -EINVAL;
  1257. }
  1258. return result;
  1259. }
  1260. int kstrtoint(const char *s, unsigned int base, int *res)
  1261. {
  1262. unsigned long long tmp;
  1263. tmp = simple_strtoull(s, NULL, base);
  1264. if (tmp != (unsigned long long)(int)tmp)
  1265. return -ERANGE;
  1266. return (int)tmp;
  1267. }
  1268. /**
  1269. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  1270. * @val: the parameter value to parse
  1271. * @kp: not used
  1272. *
  1273. * This function returns zero in case of success and a negative error code in
  1274. * case of error.
  1275. */
  1276. #ifndef __UBOOT__
  1277. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1278. #else
  1279. int ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1280. #endif
  1281. {
  1282. int i, len;
  1283. struct mtd_dev_param *p;
  1284. char buf[MTD_PARAM_LEN_MAX];
  1285. char *pbuf = &buf[0];
  1286. char *tokens[MTD_PARAM_MAX_COUNT], *token;
  1287. if (!val)
  1288. return -EINVAL;
  1289. if (mtd_devs == UBI_MAX_DEVICES) {
  1290. pr_err("UBI error: too many parameters, max. is %d\n",
  1291. UBI_MAX_DEVICES);
  1292. return -EINVAL;
  1293. }
  1294. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1295. if (len == MTD_PARAM_LEN_MAX) {
  1296. pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
  1297. val, MTD_PARAM_LEN_MAX);
  1298. return -EINVAL;
  1299. }
  1300. if (len == 0) {
  1301. pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
  1302. return 0;
  1303. }
  1304. strcpy(buf, val);
  1305. /* Get rid of the final newline */
  1306. if (buf[len - 1] == '\n')
  1307. buf[len - 1] = '\0';
  1308. for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
  1309. tokens[i] = strsep(&pbuf, ",");
  1310. if (pbuf) {
  1311. pr_err("UBI error: too many arguments at \"%s\"\n", val);
  1312. return -EINVAL;
  1313. }
  1314. p = &mtd_dev_param[mtd_devs];
  1315. strcpy(&p->name[0], tokens[0]);
  1316. token = tokens[1];
  1317. if (token) {
  1318. p->vid_hdr_offs = bytes_str_to_int(token);
  1319. if (p->vid_hdr_offs < 0)
  1320. return p->vid_hdr_offs;
  1321. }
  1322. token = tokens[2];
  1323. if (token) {
  1324. int err = kstrtoint(token, 10, &p->max_beb_per1024);
  1325. if (err) {
  1326. pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
  1327. token);
  1328. return -EINVAL;
  1329. }
  1330. }
  1331. token = tokens[3];
  1332. if (token) {
  1333. int err = kstrtoint(token, 10, &p->ubi_num);
  1334. if (err) {
  1335. pr_err("UBI error: bad value for ubi_num parameter: %s",
  1336. token);
  1337. return -EINVAL;
  1338. }
  1339. } else
  1340. p->ubi_num = UBI_DEV_NUM_AUTO;
  1341. mtd_devs += 1;
  1342. return 0;
  1343. }
  1344. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1345. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
  1346. "Multiple \"mtd\" parameters may be specified.\n"
  1347. "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
  1348. "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
  1349. "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
  1350. __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
  1351. "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
  1352. "\n"
  1353. "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
  1354. "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"
  1355. "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"
  1356. "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
  1357. "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
  1358. #ifdef CONFIG_MTD_UBI_FASTMAP
  1359. module_param(fm_autoconvert, bool, 0644);
  1360. MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
  1361. module_param(fm_debug, bool, 0);
  1362. MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
  1363. #endif
  1364. MODULE_VERSION(__stringify(UBI_VERSION));
  1365. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1366. MODULE_AUTHOR("Artem Bityutskiy");
  1367. MODULE_LICENSE("GPL");