build.c 43 KB

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