build.c 39 KB

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