build.c 39 KB

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