build.c 41 KB

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