build.c 43 KB

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