build.c 41 KB

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