build.c 32 KB

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