build.c 43 KB

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