build.c 25 KB

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