build.c 33 KB

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