build.c 44 KB

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