build.c 32 KB

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