build.c 43 KB

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