build.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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/stat.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/log2.h>
  41. #include "ubi.h"
  42. /* Maximum length of the 'mtd=' parameter */
  43. #define MTD_PARAM_LEN_MAX 64
  44. /**
  45. * struct mtd_dev_param - MTD device parameter description data structure.
  46. * @name: MTD device name or number string
  47. * @vid_hdr_offs: VID header offset
  48. * @data_offs: data offset
  49. */
  50. struct mtd_dev_param
  51. {
  52. char name[MTD_PARAM_LEN_MAX];
  53. int vid_hdr_offs;
  54. int data_offs;
  55. };
  56. /* Numbers of elements set in the @mtd_dev_param array */
  57. static int mtd_devs = 0;
  58. /* MTD devices specification parameters */
  59. static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
  60. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  61. struct class *ubi_class;
  62. /* Slab cache for lock-tree entries */
  63. struct kmem_cache *ubi_ltree_slab;
  64. /* Slab cache for wear-leveling entries */
  65. struct kmem_cache *ubi_wl_entry_slab;
  66. /* UBI control character device */
  67. static struct miscdevice ubi_ctrl_cdev = {
  68. .minor = MISC_DYNAMIC_MINOR,
  69. .name = "ubi_ctrl",
  70. .fops = &ubi_ctrl_cdev_operations,
  71. };
  72. /* All UBI devices in system */
  73. static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  74. /* Protects @ubi_devices and @ubi->ref_count */
  75. static DEFINE_SPINLOCK(ubi_devices_lock);
  76. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  77. static ssize_t ubi_version_show(struct class *class, char *buf)
  78. {
  79. return sprintf(buf, "%d\n", UBI_VERSION);
  80. }
  81. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  82. static struct class_attribute ubi_version =
  83. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  84. static ssize_t dev_attribute_show(struct device *dev,
  85. struct device_attribute *attr, char *buf);
  86. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  87. static struct device_attribute dev_eraseblock_size =
  88. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  89. static struct device_attribute dev_avail_eraseblocks =
  90. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  91. static struct device_attribute dev_total_eraseblocks =
  92. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  93. static struct device_attribute dev_volumes_count =
  94. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  95. static struct device_attribute dev_max_ec =
  96. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  97. static struct device_attribute dev_reserved_for_bad =
  98. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  99. static struct device_attribute dev_bad_peb_count =
  100. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  101. static struct device_attribute dev_max_vol_count =
  102. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  103. static struct device_attribute dev_min_io_size =
  104. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  105. static struct device_attribute dev_bgt_enabled =
  106. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  107. /**
  108. * ubi_get_device - get UBI device.
  109. * @ubi_num: UBI device number
  110. *
  111. * This function returns UBI device description object for UBI device number
  112. * @ubi_num, or %NULL if the device does not exist. This function increases the
  113. * device reference count to prevent removal of the device. In other words, the
  114. * device cannot be removed if its reference count is not zero.
  115. */
  116. struct ubi_device *ubi_get_device(int ubi_num)
  117. {
  118. struct ubi_device *ubi;
  119. spin_lock(&ubi_devices_lock);
  120. ubi = ubi_devices[ubi_num];
  121. if (ubi) {
  122. ubi_assert(ubi->ref_count >= 0);
  123. ubi->ref_count += 1;
  124. get_device(&ubi->dev);
  125. }
  126. spin_unlock(&ubi_devices_lock);
  127. return ubi;
  128. }
  129. /**
  130. * ubi_put_device - drop an UBI device reference.
  131. * @ubi: UBI device description object
  132. */
  133. void ubi_put_device(struct ubi_device *ubi)
  134. {
  135. spin_lock(&ubi_devices_lock);
  136. ubi->ref_count -= 1;
  137. put_device(&ubi->dev);
  138. spin_unlock(&ubi_devices_lock);
  139. }
  140. /**
  141. * ubi_get_by_major - get UBI device description object by character device
  142. * major number.
  143. * @major: major number
  144. *
  145. * This function is similar to 'ubi_get_device()', but it searches the device
  146. * by its major number.
  147. */
  148. struct ubi_device *ubi_get_by_major(int major)
  149. {
  150. int i;
  151. struct ubi_device *ubi;
  152. spin_lock(&ubi_devices_lock);
  153. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  154. ubi = ubi_devices[i];
  155. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  156. ubi_assert(ubi->ref_count >= 0);
  157. ubi->ref_count += 1;
  158. get_device(&ubi->dev);
  159. spin_unlock(&ubi_devices_lock);
  160. return ubi;
  161. }
  162. }
  163. spin_unlock(&ubi_devices_lock);
  164. return NULL;
  165. }
  166. /**
  167. * ubi_major2num - get UBI device number by character device major number.
  168. * @major: major number
  169. *
  170. * This function searches UBI device number object by its major number. If UBI
  171. * device was not found, this function returns -ENODEV, othewise the UBI device
  172. * number is returned.
  173. */
  174. int ubi_major2num(int major)
  175. {
  176. int i, ubi_num = -ENODEV;
  177. spin_lock(&ubi_devices_lock);
  178. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  179. struct ubi_device *ubi = ubi_devices[i];
  180. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  181. ubi_num = ubi->ubi_num;
  182. break;
  183. }
  184. }
  185. spin_unlock(&ubi_devices_lock);
  186. return ubi_num;
  187. }
  188. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  189. static ssize_t dev_attribute_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. ssize_t ret;
  193. struct ubi_device *ubi;
  194. /*
  195. * The below code looks weird, but it actually makes sense. We get the
  196. * UBI device reference from the contained 'struct ubi_device'. But it
  197. * is unclear if the device was removed or not yet. Indeed, if the
  198. * device was removed before we increased its reference count,
  199. * 'ubi_get_device()' will return -ENODEV and we fail.
  200. *
  201. * Remember, 'struct ubi_device' is freed in the release function, so
  202. * we still can use 'ubi->ubi_num'.
  203. */
  204. ubi = container_of(dev, struct ubi_device, dev);
  205. ubi = ubi_get_device(ubi->ubi_num);
  206. if (!ubi)
  207. return -ENODEV;
  208. if (attr == &dev_eraseblock_size)
  209. ret = sprintf(buf, "%d\n", ubi->leb_size);
  210. else if (attr == &dev_avail_eraseblocks)
  211. ret = sprintf(buf, "%d\n", ubi->avail_pebs);
  212. else if (attr == &dev_total_eraseblocks)
  213. ret = sprintf(buf, "%d\n", ubi->good_peb_count);
  214. else if (attr == &dev_volumes_count)
  215. ret = sprintf(buf, "%d\n", ubi->vol_count);
  216. else if (attr == &dev_max_ec)
  217. ret = sprintf(buf, "%d\n", ubi->max_ec);
  218. else if (attr == &dev_reserved_for_bad)
  219. ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  220. else if (attr == &dev_bad_peb_count)
  221. ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
  222. else if (attr == &dev_max_vol_count)
  223. ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
  224. else if (attr == &dev_min_io_size)
  225. ret = sprintf(buf, "%d\n", ubi->min_io_size);
  226. else if (attr == &dev_bgt_enabled)
  227. ret = sprintf(buf, "%d\n", ubi->thread_enabled);
  228. else
  229. BUG();
  230. ubi_put_device(ubi);
  231. return ret;
  232. }
  233. /* Fake "release" method for UBI devices */
  234. static void dev_release(struct device *dev) { }
  235. /**
  236. * ubi_sysfs_init - initialize sysfs for an UBI device.
  237. * @ubi: UBI device description object
  238. *
  239. * This function returns zero in case of success and a negative error code in
  240. * case of failure.
  241. */
  242. static int ubi_sysfs_init(struct ubi_device *ubi)
  243. {
  244. int err;
  245. ubi->dev.release = dev_release;
  246. ubi->dev.devt = ubi->cdev.dev;
  247. ubi->dev.class = ubi_class;
  248. sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
  249. err = device_register(&ubi->dev);
  250. if (err)
  251. return err;
  252. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  253. if (err)
  254. return err;
  255. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  256. if (err)
  257. return err;
  258. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  259. if (err)
  260. return err;
  261. err = device_create_file(&ubi->dev, &dev_volumes_count);
  262. if (err)
  263. return err;
  264. err = device_create_file(&ubi->dev, &dev_max_ec);
  265. if (err)
  266. return err;
  267. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  268. if (err)
  269. return err;
  270. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  271. if (err)
  272. return err;
  273. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  274. if (err)
  275. return err;
  276. err = device_create_file(&ubi->dev, &dev_min_io_size);
  277. if (err)
  278. return err;
  279. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  280. return err;
  281. }
  282. /**
  283. * ubi_sysfs_close - close sysfs for an UBI device.
  284. * @ubi: UBI device description object
  285. */
  286. static void ubi_sysfs_close(struct ubi_device *ubi)
  287. {
  288. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  289. device_remove_file(&ubi->dev, &dev_min_io_size);
  290. device_remove_file(&ubi->dev, &dev_max_vol_count);
  291. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  292. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  293. device_remove_file(&ubi->dev, &dev_max_ec);
  294. device_remove_file(&ubi->dev, &dev_volumes_count);
  295. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  296. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  297. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  298. device_unregister(&ubi->dev);
  299. }
  300. /**
  301. * kill_volumes - destroy all volumes.
  302. * @ubi: UBI device description object
  303. */
  304. static void kill_volumes(struct ubi_device *ubi)
  305. {
  306. int i;
  307. for (i = 0; i < ubi->vtbl_slots; i++)
  308. if (ubi->volumes[i])
  309. ubi_free_volume(ubi, ubi->volumes[i]);
  310. }
  311. /**
  312. * uif_init - initialize user interfaces for an UBI device.
  313. * @ubi: UBI device description object
  314. *
  315. * This function returns zero in case of success and a negative error code in
  316. * case of failure.
  317. */
  318. static int uif_init(struct ubi_device *ubi)
  319. {
  320. int i, err;
  321. dev_t dev;
  322. mutex_init(&ubi->volumes_mutex);
  323. spin_lock_init(&ubi->volumes_lock);
  324. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  325. /*
  326. * Major numbers for the UBI character devices are allocated
  327. * dynamically. Major numbers of volume character devices are
  328. * equivalent to ones of the corresponding UBI character device. Minor
  329. * numbers of UBI character devices are 0, while minor numbers of
  330. * volume character devices start from 1. Thus, we allocate one major
  331. * number and ubi->vtbl_slots + 1 minor numbers.
  332. */
  333. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  334. if (err) {
  335. ubi_err("cannot register UBI character devices");
  336. return err;
  337. }
  338. ubi_assert(MINOR(dev) == 0);
  339. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  340. dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
  341. ubi->cdev.owner = THIS_MODULE;
  342. err = cdev_add(&ubi->cdev, dev, 1);
  343. if (err) {
  344. ubi_err("cannot add character device");
  345. goto out_unreg;
  346. }
  347. err = ubi_sysfs_init(ubi);
  348. if (err)
  349. goto out_sysfs;
  350. for (i = 0; i < ubi->vtbl_slots; i++)
  351. if (ubi->volumes[i]) {
  352. err = ubi_add_volume(ubi, ubi->volumes[i]);
  353. if (err) {
  354. ubi_err("cannot add volume %d", i);
  355. goto out_volumes;
  356. }
  357. }
  358. return 0;
  359. out_volumes:
  360. kill_volumes(ubi);
  361. out_sysfs:
  362. ubi_sysfs_close(ubi);
  363. cdev_del(&ubi->cdev);
  364. out_unreg:
  365. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  366. ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
  367. return err;
  368. }
  369. /**
  370. * uif_close - close user interfaces for an UBI device.
  371. * @ubi: UBI device description object
  372. */
  373. static void uif_close(struct ubi_device *ubi)
  374. {
  375. kill_volumes(ubi);
  376. ubi_sysfs_close(ubi);
  377. cdev_del(&ubi->cdev);
  378. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  379. }
  380. /**
  381. * attach_by_scanning - attach an MTD device using scanning method.
  382. * @ubi: UBI device descriptor
  383. *
  384. * This function returns zero in case of success and a negative error code in
  385. * case of failure.
  386. *
  387. * Note, currently this is the only method to attach UBI devices. Hopefully in
  388. * the future we'll have more scalable attaching methods and avoid full media
  389. * scanning. But even in this case scanning will be needed as a fall-back
  390. * attaching method if there are some on-flash table corruptions.
  391. */
  392. static int attach_by_scanning(struct ubi_device *ubi)
  393. {
  394. int err;
  395. struct ubi_scan_info *si;
  396. si = ubi_scan(ubi);
  397. if (IS_ERR(si))
  398. return PTR_ERR(si);
  399. ubi->bad_peb_count = si->bad_peb_count;
  400. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  401. ubi->max_ec = si->max_ec;
  402. ubi->mean_ec = si->mean_ec;
  403. err = ubi_read_volume_table(ubi, si);
  404. if (err)
  405. goto out_si;
  406. err = ubi_wl_init_scan(ubi, si);
  407. if (err)
  408. goto out_vtbl;
  409. err = ubi_eba_init_scan(ubi, si);
  410. if (err)
  411. goto out_wl;
  412. ubi_scan_destroy_si(si);
  413. return 0;
  414. out_wl:
  415. ubi_wl_close(ubi);
  416. out_vtbl:
  417. vfree(ubi->vtbl);
  418. out_si:
  419. ubi_scan_destroy_si(si);
  420. return err;
  421. }
  422. /**
  423. * io_init - initialize I/O unit for a given UBI device.
  424. * @ubi: UBI device description object
  425. *
  426. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  427. * assumed:
  428. * o EC header is always at offset zero - this cannot be changed;
  429. * o VID header starts just after the EC header at the closest address
  430. * aligned to @io->@hdrs_min_io_size;
  431. * o data starts just after the VID header at the closest address aligned to
  432. * @io->@min_io_size
  433. *
  434. * This function returns zero in case of success and a negative error code in
  435. * case of failure.
  436. */
  437. static int io_init(struct ubi_device *ubi)
  438. {
  439. if (ubi->mtd->numeraseregions != 0) {
  440. /*
  441. * Some flashes have several erase regions. Different regions
  442. * may have different eraseblock size and other
  443. * characteristics. It looks like mostly multi-region flashes
  444. * have one "main" region and one or more small regions to
  445. * store boot loader code or boot parameters or whatever. I
  446. * guess we should just pick the largest region. But this is
  447. * not implemented.
  448. */
  449. ubi_err("multiple regions, not implemented");
  450. return -EINVAL;
  451. }
  452. /*
  453. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  454. * physical eraseblocks maximum.
  455. */
  456. ubi->peb_size = ubi->mtd->erasesize;
  457. ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
  458. ubi->flash_size = ubi->mtd->size;
  459. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  460. ubi->bad_allowed = 1;
  461. ubi->min_io_size = ubi->mtd->writesize;
  462. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  463. /* Make sure minimal I/O unit is power of 2 */
  464. if (!is_power_of_2(ubi->min_io_size)) {
  465. ubi_err("min. I/O unit (%d) is not power of 2",
  466. ubi->min_io_size);
  467. return -EINVAL;
  468. }
  469. ubi_assert(ubi->hdrs_min_io_size > 0);
  470. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  471. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  472. /* Calculate default aligned sizes of EC and VID headers */
  473. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  474. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  475. dbg_msg("min_io_size %d", ubi->min_io_size);
  476. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  477. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  478. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  479. if (ubi->vid_hdr_offset == 0)
  480. /* Default offset */
  481. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  482. ubi->ec_hdr_alsize;
  483. else {
  484. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  485. ~(ubi->hdrs_min_io_size - 1);
  486. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  487. ubi->vid_hdr_aloffset;
  488. }
  489. /* Similar for the data offset */
  490. if (ubi->leb_start == 0) {
  491. ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
  492. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  493. }
  494. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  495. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  496. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  497. dbg_msg("leb_start %d", ubi->leb_start);
  498. /* The shift must be aligned to 32-bit boundary */
  499. if (ubi->vid_hdr_shift % 4) {
  500. ubi_err("unaligned VID header shift %d",
  501. ubi->vid_hdr_shift);
  502. return -EINVAL;
  503. }
  504. /* Check sanity */
  505. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  506. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  507. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  508. ubi->leb_start % ubi->min_io_size) {
  509. ubi_err("bad VID header (%d) or data offsets (%d)",
  510. ubi->vid_hdr_offset, ubi->leb_start);
  511. return -EINVAL;
  512. }
  513. /*
  514. * It may happen that EC and VID headers are situated in one minimal
  515. * I/O unit. In this case we can only accept this UBI image in
  516. * read-only mode.
  517. */
  518. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  519. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  520. "switch to read-only mode");
  521. ubi->ro_mode = 1;
  522. }
  523. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  524. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  525. ubi_msg("MTD device %d is write-protected, attach in "
  526. "read-only mode", ubi->mtd->index);
  527. ubi->ro_mode = 1;
  528. }
  529. dbg_msg("leb_size %d", ubi->leb_size);
  530. dbg_msg("ro_mode %d", ubi->ro_mode);
  531. /*
  532. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  533. * unfortunately, MTD does not provide this information. We should loop
  534. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  535. * each physical eraseblock. So, we skip ubi->bad_peb_count
  536. * uninitialized and initialize it after scanning.
  537. */
  538. return 0;
  539. }
  540. /**
  541. * attach_mtd_dev - attach an MTD device.
  542. * @mtd_dev: MTD device name or number string
  543. * @vid_hdr_offset: VID header offset
  544. * @data_offset: data offset
  545. *
  546. * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
  547. * MTD device name, and tries to open it by this name. If it is unable to open,
  548. * it tries to convert @mtd_dev to an integer and open the MTD device by its
  549. * number. Returns zero in case of success and a negative error code in case of
  550. * failure.
  551. */
  552. static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
  553. int data_offset)
  554. {
  555. struct ubi_device *ubi;
  556. struct mtd_info *mtd;
  557. int i, err;
  558. mtd = get_mtd_device_nm(mtd_dev);
  559. if (IS_ERR(mtd)) {
  560. int mtd_num;
  561. char *endp;
  562. if (PTR_ERR(mtd) != -ENODEV)
  563. return PTR_ERR(mtd);
  564. /*
  565. * Probably this is not MTD device name but MTD device number -
  566. * check this out.
  567. */
  568. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  569. if (*endp != '\0' || mtd_dev == endp) {
  570. ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
  571. return -ENODEV;
  572. }
  573. mtd = get_mtd_device(NULL, mtd_num);
  574. if (IS_ERR(mtd))
  575. return PTR_ERR(mtd);
  576. }
  577. /* Check if we already have the same MTD device attached */
  578. for (i = 0; i < UBI_MAX_DEVICES; i++)
  579. ubi = ubi_devices[i];
  580. if (ubi && ubi->mtd->index == mtd->index) {
  581. ubi_err("mtd%d is already attached to ubi%d",
  582. mtd->index, i);
  583. err = -EINVAL;
  584. goto out_mtd;
  585. }
  586. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  587. if (!ubi) {
  588. err = -ENOMEM;
  589. goto out_mtd;
  590. }
  591. ubi->mtd = mtd;
  592. /* Search for an empty slot in the @ubi_devices array */
  593. ubi->ubi_num = -1;
  594. for (i = 0; i < UBI_MAX_DEVICES; i++)
  595. if (!ubi_devices[i]) {
  596. ubi->ubi_num = i;
  597. break;
  598. }
  599. if (ubi->ubi_num == -1) {
  600. ubi_err("only %d UBI devices may be created", UBI_MAX_DEVICES);
  601. err = -ENFILE;
  602. goto out_free;
  603. }
  604. dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
  605. ubi->mtd->index, ubi->ubi_num, vid_hdr_offset, data_offset);
  606. ubi->vid_hdr_offset = vid_hdr_offset;
  607. ubi->leb_start = data_offset;
  608. err = io_init(ubi);
  609. if (err)
  610. goto out_free;
  611. mutex_init(&ubi->buf_mutex);
  612. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  613. if (!ubi->peb_buf1)
  614. goto out_free;
  615. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  616. if (!ubi->peb_buf2)
  617. goto out_free;
  618. #ifdef CONFIG_MTD_UBI_DEBUG
  619. mutex_init(&ubi->dbg_buf_mutex);
  620. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  621. if (!ubi->dbg_peb_buf)
  622. goto out_free;
  623. #endif
  624. err = attach_by_scanning(ubi);
  625. if (err) {
  626. dbg_err("failed to attach by scanning, error %d", err);
  627. goto out_free;
  628. }
  629. err = uif_init(ubi);
  630. if (err)
  631. goto out_detach;
  632. ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi->ubi_num);
  633. ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
  634. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  635. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  636. ubi->peb_size, ubi->peb_size >> 10);
  637. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  638. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  639. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  640. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  641. ubi_msg("VID header offset: %d (aligned %d)",
  642. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  643. ubi_msg("data offset: %d", ubi->leb_start);
  644. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  645. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  646. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  647. ubi_msg("number of user volumes: %d",
  648. ubi->vol_count - UBI_INT_VOL_COUNT);
  649. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  650. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  651. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  652. ubi->beb_rsvd_pebs);
  653. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  654. /* Enable the background thread */
  655. if (!DBG_DISABLE_BGT) {
  656. ubi->thread_enabled = 1;
  657. wake_up_process(ubi->bgt_thread);
  658. }
  659. ubi_devices[ubi->ubi_num] = ubi;
  660. return 0;
  661. out_detach:
  662. ubi_eba_close(ubi);
  663. ubi_wl_close(ubi);
  664. vfree(ubi->vtbl);
  665. out_free:
  666. vfree(ubi->peb_buf1);
  667. vfree(ubi->peb_buf2);
  668. #ifdef CONFIG_MTD_UBI_DEBUG
  669. vfree(ubi->dbg_peb_buf);
  670. #endif
  671. kfree(ubi);
  672. out_mtd:
  673. put_mtd_device(mtd);
  674. return err;
  675. }
  676. /**
  677. * detach_mtd_dev - detach an MTD device.
  678. * @ubi: UBI device description object
  679. */
  680. static void detach_mtd_dev(struct ubi_device *ubi)
  681. {
  682. int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
  683. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  684. ubi_assert(ubi->ref_count == 0);
  685. uif_close(ubi);
  686. ubi_eba_close(ubi);
  687. ubi_wl_close(ubi);
  688. vfree(ubi->vtbl);
  689. put_mtd_device(ubi->mtd);
  690. vfree(ubi->peb_buf1);
  691. vfree(ubi->peb_buf2);
  692. #ifdef CONFIG_MTD_UBI_DEBUG
  693. vfree(ubi->dbg_peb_buf);
  694. #endif
  695. kfree(ubi_devices[ubi_num]);
  696. ubi_devices[ubi_num] = NULL;
  697. ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
  698. }
  699. /**
  700. * ltree_entry_ctor - lock tree entries slab cache constructor.
  701. * @obj: the lock-tree entry to construct
  702. * @cache: the lock tree entry slab cache
  703. * @flags: constructor flags
  704. */
  705. static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
  706. {
  707. struct ubi_ltree_entry *le = obj;
  708. le->users = 0;
  709. init_rwsem(&le->mutex);
  710. }
  711. static int __init ubi_init(void)
  712. {
  713. int err, i, k;
  714. /* Ensure that EC and VID headers have correct size */
  715. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  716. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  717. if (mtd_devs > UBI_MAX_DEVICES) {
  718. printk(KERN_ERR "UBI error: too many MTD devices, "
  719. "maximum is %d\n", UBI_MAX_DEVICES);
  720. return -EINVAL;
  721. }
  722. /* Create base sysfs directory and sysfs files */
  723. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  724. if (IS_ERR(ubi_class)) {
  725. err = PTR_ERR(ubi_class);
  726. printk(KERN_ERR "UBI error: cannot create UBI class\n");
  727. goto out;
  728. }
  729. err = class_create_file(ubi_class, &ubi_version);
  730. if (err) {
  731. printk(KERN_ERR "UBI error: cannot create sysfs file\n");
  732. goto out_class;
  733. }
  734. err = misc_register(&ubi_ctrl_cdev);
  735. if (err) {
  736. printk(KERN_ERR "UBI error: cannot register device\n");
  737. goto out_version;
  738. }
  739. ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",
  740. sizeof(struct ubi_ltree_entry), 0,
  741. 0, &ltree_entry_ctor);
  742. if (!ubi_ltree_slab)
  743. goto out_dev_unreg;
  744. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  745. sizeof(struct ubi_wl_entry),
  746. 0, 0, NULL);
  747. if (!ubi_wl_entry_slab)
  748. goto out_ltree;
  749. /* Attach MTD devices */
  750. for (i = 0; i < mtd_devs; i++) {
  751. struct mtd_dev_param *p = &mtd_dev_param[i];
  752. cond_resched();
  753. err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
  754. if (err) {
  755. printk(KERN_ERR "UBI error: cannot attach %s\n",
  756. p->name);
  757. goto out_detach;
  758. }
  759. }
  760. return 0;
  761. out_detach:
  762. for (k = 0; k < i; k++)
  763. detach_mtd_dev(ubi_devices[k]);
  764. kmem_cache_destroy(ubi_wl_entry_slab);
  765. out_ltree:
  766. kmem_cache_destroy(ubi_ltree_slab);
  767. out_dev_unreg:
  768. misc_deregister(&ubi_ctrl_cdev);
  769. out_version:
  770. class_remove_file(ubi_class, &ubi_version);
  771. out_class:
  772. class_destroy(ubi_class);
  773. out:
  774. printk(KERN_ERR "UBI error: cannot initialize UBI, error %d\n", err);
  775. return err;
  776. }
  777. module_init(ubi_init);
  778. static void __exit ubi_exit(void)
  779. {
  780. int i;
  781. for (i = 0; i < UBI_MAX_DEVICES; i++)
  782. if (ubi_devices[i])
  783. detach_mtd_dev(ubi_devices[i]);
  784. kmem_cache_destroy(ubi_wl_entry_slab);
  785. kmem_cache_destroy(ubi_ltree_slab);
  786. misc_deregister(&ubi_ctrl_cdev);
  787. class_remove_file(ubi_class, &ubi_version);
  788. class_destroy(ubi_class);
  789. }
  790. module_exit(ubi_exit);
  791. /**
  792. * bytes_str_to_int - convert a string representing number of bytes to an
  793. * integer.
  794. * @str: the string to convert
  795. *
  796. * This function returns positive resulting integer in case of success and a
  797. * negative error code in case of failure.
  798. */
  799. static int __init bytes_str_to_int(const char *str)
  800. {
  801. char *endp;
  802. unsigned long result;
  803. result = simple_strtoul(str, &endp, 0);
  804. if (str == endp || result < 0) {
  805. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  806. str);
  807. return -EINVAL;
  808. }
  809. switch (*endp) {
  810. case 'G':
  811. result *= 1024;
  812. case 'M':
  813. result *= 1024;
  814. case 'K':
  815. case 'k':
  816. result *= 1024;
  817. if (endp[1] == 'i' && (endp[2] == '\0' ||
  818. endp[2] == 'B' || endp[2] == 'b'))
  819. endp += 2;
  820. case '\0':
  821. break;
  822. default:
  823. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  824. str);
  825. return -EINVAL;
  826. }
  827. return result;
  828. }
  829. /**
  830. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  831. * @val: the parameter value to parse
  832. * @kp: not used
  833. *
  834. * This function returns zero in case of success and a negative error code in
  835. * case of error.
  836. */
  837. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  838. {
  839. int i, len;
  840. struct mtd_dev_param *p;
  841. char buf[MTD_PARAM_LEN_MAX];
  842. char *pbuf = &buf[0];
  843. char *tokens[3] = {NULL, NULL, NULL};
  844. if (!val)
  845. return -EINVAL;
  846. if (mtd_devs == UBI_MAX_DEVICES) {
  847. printk(KERN_ERR "UBI error: too many parameters, max. is %d\n",
  848. UBI_MAX_DEVICES);
  849. return -EINVAL;
  850. }
  851. len = strnlen(val, MTD_PARAM_LEN_MAX);
  852. if (len == MTD_PARAM_LEN_MAX) {
  853. printk(KERN_ERR "UBI error: parameter \"%s\" is too long, "
  854. "max. is %d\n", val, MTD_PARAM_LEN_MAX);
  855. return -EINVAL;
  856. }
  857. if (len == 0) {
  858. printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - "
  859. "ignored\n");
  860. return 0;
  861. }
  862. strcpy(buf, val);
  863. /* Get rid of the final newline */
  864. if (buf[len - 1] == '\n')
  865. buf[len - 1] = '\0';
  866. for (i = 0; i < 3; i++)
  867. tokens[i] = strsep(&pbuf, ",");
  868. if (pbuf) {
  869. printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n",
  870. val);
  871. return -EINVAL;
  872. }
  873. p = &mtd_dev_param[mtd_devs];
  874. strcpy(&p->name[0], tokens[0]);
  875. if (tokens[1])
  876. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  877. if (tokens[2])
  878. p->data_offs = bytes_str_to_int(tokens[2]);
  879. if (p->vid_hdr_offs < 0)
  880. return p->vid_hdr_offs;
  881. if (p->data_offs < 0)
  882. return p->data_offs;
  883. mtd_devs += 1;
  884. return 0;
  885. }
  886. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  887. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  888. "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
  889. "Multiple \"mtd\" parameters may be specified.\n"
  890. "MTD devices may be specified by their number or name. "
  891. "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
  892. "specify UBI VID header position and data starting "
  893. "position to be used by UBI.\n"
  894. "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
  895. "with name content using VID header offset 1984 and data "
  896. "start 2048, and MTD device number 4 using default "
  897. "offsets");
  898. MODULE_VERSION(__stringify(UBI_VERSION));
  899. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  900. MODULE_AUTHOR("Artem Bityutskiy");
  901. MODULE_LICENSE("GPL");