build.c 41 KB

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