build.c 40 KB

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