build.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. #include <linux/err.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/stringify.h>
  34. #include <linux/namei.h>
  35. #include <linux/stat.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/log2.h>
  39. #include <linux/kthread.h>
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/major.h>
  43. #include "ubi.h"
  44. /* Maximum length of the 'mtd=' parameter */
  45. #define MTD_PARAM_LEN_MAX 64
  46. /* Maximum number of comma-separated items in the 'mtd=' parameter */
  47. #define MTD_PARAM_MAX_COUNT 4
  48. /* Maximum value for the number of bad PEBs per 1024 PEBs */
  49. #define MAX_MTD_UBI_BEB_LIMIT 768
  50. #ifdef CONFIG_MTD_UBI_MODULE
  51. #define ubi_is_module() 1
  52. #else
  53. #define ubi_is_module() 0
  54. #endif
  55. /**
  56. * struct mtd_dev_param - MTD device parameter description data structure.
  57. * @name: MTD character device node path, MTD device name, or MTD device number
  58. * string
  59. * @vid_hdr_offs: VID header offset
  60. * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
  61. */
  62. struct mtd_dev_param {
  63. char name[MTD_PARAM_LEN_MAX];
  64. int ubi_num;
  65. int vid_hdr_offs;
  66. int max_beb_per1024;
  67. };
  68. /* Numbers of elements set in the @mtd_dev_param array */
  69. static int __initdata mtd_devs;
  70. /* MTD devices specification parameters */
  71. static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
  72. #ifdef CONFIG_MTD_UBI_FASTMAP
  73. /* UBI module parameter to enable fastmap automatically on non-fastmap images */
  74. static bool fm_autoconvert;
  75. static bool fm_debug;
  76. #endif
  77. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  78. struct class *ubi_class;
  79. /* Slab cache for wear-leveling entries */
  80. struct kmem_cache *ubi_wl_entry_slab;
  81. /* UBI control character device */
  82. static struct miscdevice ubi_ctrl_cdev = {
  83. .minor = MISC_DYNAMIC_MINOR,
  84. .name = "ubi_ctrl",
  85. .fops = &ubi_ctrl_cdev_operations,
  86. };
  87. /* All UBI devices in system */
  88. static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  89. /* Serializes UBI devices creations and removals */
  90. DEFINE_MUTEX(ubi_devices_mutex);
  91. /* Protects @ubi_devices and @ubi->ref_count */
  92. static DEFINE_SPINLOCK(ubi_devices_lock);
  93. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  94. static ssize_t ubi_version_show(struct class *class,
  95. struct class_attribute *attr, char *buf)
  96. {
  97. return sprintf(buf, "%d\n", UBI_VERSION);
  98. }
  99. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  100. static struct class_attribute ubi_version =
  101. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  102. static ssize_t dev_attribute_show(struct device *dev,
  103. struct device_attribute *attr, char *buf);
  104. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  105. static struct device_attribute dev_eraseblock_size =
  106. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  107. static struct device_attribute dev_avail_eraseblocks =
  108. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  109. static struct device_attribute dev_total_eraseblocks =
  110. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  111. static struct device_attribute dev_volumes_count =
  112. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  113. static struct device_attribute dev_max_ec =
  114. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  115. static struct device_attribute dev_reserved_for_bad =
  116. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  117. static struct device_attribute dev_bad_peb_count =
  118. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  119. static struct device_attribute dev_max_vol_count =
  120. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  121. static struct device_attribute dev_min_io_size =
  122. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  123. static struct device_attribute dev_bgt_enabled =
  124. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  125. static struct device_attribute dev_mtd_num =
  126. __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
  127. /**
  128. * ubi_volume_notify - send a volume change notification.
  129. * @ubi: UBI device description object
  130. * @vol: volume description object of the changed volume
  131. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  132. *
  133. * This is a helper function which notifies all subscribers about a volume
  134. * change event (creation, removal, re-sizing, re-naming, updating). Returns
  135. * zero in case of success and a negative error code in case of failure.
  136. */
  137. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
  138. {
  139. int ret;
  140. struct ubi_notification nt;
  141. ubi_do_get_device_info(ubi, &nt.di);
  142. ubi_do_get_volume_info(ubi, vol, &nt.vi);
  143. switch (ntype) {
  144. case UBI_VOLUME_ADDED:
  145. case UBI_VOLUME_REMOVED:
  146. case UBI_VOLUME_RESIZED:
  147. case UBI_VOLUME_RENAMED:
  148. ret = ubi_update_fastmap(ubi);
  149. if (ret)
  150. ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
  151. }
  152. return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
  153. }
  154. /**
  155. * ubi_notify_all - send a notification to all volumes.
  156. * @ubi: UBI device description object
  157. * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
  158. * @nb: the notifier to call
  159. *
  160. * This function walks all volumes of UBI device @ubi and sends the @ntype
  161. * notification for each volume. If @nb is %NULL, then all registered notifiers
  162. * are called, otherwise only the @nb notifier is called. Returns the number of
  163. * sent notifications.
  164. */
  165. int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
  166. {
  167. struct ubi_notification nt;
  168. int i, count = 0;
  169. ubi_do_get_device_info(ubi, &nt.di);
  170. mutex_lock(&ubi->device_mutex);
  171. for (i = 0; i < ubi->vtbl_slots; i++) {
  172. /*
  173. * Since the @ubi->device is locked, and we are not going to
  174. * change @ubi->volumes, we do not have to lock
  175. * @ubi->volumes_lock.
  176. */
  177. if (!ubi->volumes[i])
  178. continue;
  179. ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
  180. if (nb)
  181. nb->notifier_call(nb, ntype, &nt);
  182. else
  183. blocking_notifier_call_chain(&ubi_notifiers, ntype,
  184. &nt);
  185. count += 1;
  186. }
  187. mutex_unlock(&ubi->device_mutex);
  188. return count;
  189. }
  190. /**
  191. * ubi_enumerate_volumes - send "add" notification for all existing volumes.
  192. * @nb: the notifier to call
  193. *
  194. * This function walks all UBI devices and volumes and sends the
  195. * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
  196. * registered notifiers are called, otherwise only the @nb notifier is called.
  197. * Returns the number of sent notifications.
  198. */
  199. int ubi_enumerate_volumes(struct notifier_block *nb)
  200. {
  201. int i, count = 0;
  202. /*
  203. * Since the @ubi_devices_mutex is locked, and we are not going to
  204. * change @ubi_devices, we do not have to lock @ubi_devices_lock.
  205. */
  206. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  207. struct ubi_device *ubi = ubi_devices[i];
  208. if (!ubi)
  209. continue;
  210. count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
  211. }
  212. return count;
  213. }
  214. /**
  215. * ubi_get_device - get UBI device.
  216. * @ubi_num: UBI device number
  217. *
  218. * This function returns UBI device description object for UBI device number
  219. * @ubi_num, or %NULL if the device does not exist. This function increases the
  220. * device reference count to prevent removal of the device. In other words, the
  221. * device cannot be removed if its reference count is not zero.
  222. */
  223. struct ubi_device *ubi_get_device(int ubi_num)
  224. {
  225. struct ubi_device *ubi;
  226. spin_lock(&ubi_devices_lock);
  227. ubi = ubi_devices[ubi_num];
  228. if (ubi) {
  229. ubi_assert(ubi->ref_count >= 0);
  230. ubi->ref_count += 1;
  231. get_device(&ubi->dev);
  232. }
  233. spin_unlock(&ubi_devices_lock);
  234. return ubi;
  235. }
  236. /**
  237. * ubi_put_device - drop an UBI device reference.
  238. * @ubi: UBI device description object
  239. */
  240. void ubi_put_device(struct ubi_device *ubi)
  241. {
  242. spin_lock(&ubi_devices_lock);
  243. ubi->ref_count -= 1;
  244. put_device(&ubi->dev);
  245. spin_unlock(&ubi_devices_lock);
  246. }
  247. /**
  248. * ubi_get_by_major - get UBI device by character device major number.
  249. * @major: major number
  250. *
  251. * This function is similar to 'ubi_get_device()', but it searches the device
  252. * by its major number.
  253. */
  254. struct ubi_device *ubi_get_by_major(int major)
  255. {
  256. int i;
  257. struct ubi_device *ubi;
  258. spin_lock(&ubi_devices_lock);
  259. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  260. ubi = ubi_devices[i];
  261. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  262. ubi_assert(ubi->ref_count >= 0);
  263. ubi->ref_count += 1;
  264. get_device(&ubi->dev);
  265. spin_unlock(&ubi_devices_lock);
  266. return ubi;
  267. }
  268. }
  269. spin_unlock(&ubi_devices_lock);
  270. return NULL;
  271. }
  272. /**
  273. * ubi_major2num - get UBI device number by character device major number.
  274. * @major: major number
  275. *
  276. * This function searches UBI device number object by its major number. If UBI
  277. * device was not found, this function returns -ENODEV, otherwise the UBI device
  278. * number is returned.
  279. */
  280. int ubi_major2num(int major)
  281. {
  282. int i, ubi_num = -ENODEV;
  283. spin_lock(&ubi_devices_lock);
  284. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  285. struct ubi_device *ubi = ubi_devices[i];
  286. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  287. ubi_num = ubi->ubi_num;
  288. break;
  289. }
  290. }
  291. spin_unlock(&ubi_devices_lock);
  292. return ubi_num;
  293. }
  294. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  295. static ssize_t dev_attribute_show(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. ssize_t ret;
  299. struct ubi_device *ubi;
  300. /*
  301. * The below code looks weird, but it actually makes sense. We get the
  302. * UBI device reference from the contained 'struct ubi_device'. But it
  303. * is unclear if the device was removed or not yet. Indeed, if the
  304. * device was removed before we increased its reference count,
  305. * 'ubi_get_device()' will return -ENODEV and we fail.
  306. *
  307. * Remember, 'struct ubi_device' is freed in the release function, so
  308. * we still can use 'ubi->ubi_num'.
  309. */
  310. ubi = container_of(dev, struct ubi_device, dev);
  311. ubi = ubi_get_device(ubi->ubi_num);
  312. if (!ubi)
  313. return -ENODEV;
  314. if (attr == &dev_eraseblock_size)
  315. ret = sprintf(buf, "%d\n", ubi->leb_size);
  316. else if (attr == &dev_avail_eraseblocks)
  317. ret = sprintf(buf, "%d\n", ubi->avail_pebs);
  318. else if (attr == &dev_total_eraseblocks)
  319. ret = sprintf(buf, "%d\n", ubi->good_peb_count);
  320. else if (attr == &dev_volumes_count)
  321. ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
  322. else if (attr == &dev_max_ec)
  323. ret = sprintf(buf, "%d\n", ubi->max_ec);
  324. else if (attr == &dev_reserved_for_bad)
  325. ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  326. else if (attr == &dev_bad_peb_count)
  327. ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
  328. else if (attr == &dev_max_vol_count)
  329. ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
  330. else if (attr == &dev_min_io_size)
  331. ret = sprintf(buf, "%d\n", ubi->min_io_size);
  332. else if (attr == &dev_bgt_enabled)
  333. ret = sprintf(buf, "%d\n", ubi->thread_enabled);
  334. else if (attr == &dev_mtd_num)
  335. ret = sprintf(buf, "%d\n", ubi->mtd->index);
  336. else
  337. ret = -EINVAL;
  338. ubi_put_device(ubi);
  339. return ret;
  340. }
  341. static void dev_release(struct device *dev)
  342. {
  343. struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
  344. kfree(ubi);
  345. }
  346. /**
  347. * ubi_sysfs_init - initialize sysfs for an UBI device.
  348. * @ubi: UBI device description object
  349. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  350. * taken
  351. *
  352. * This function returns zero in case of success and a negative error code in
  353. * case of failure.
  354. */
  355. static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
  356. {
  357. int err;
  358. ubi->dev.release = dev_release;
  359. ubi->dev.devt = ubi->cdev.dev;
  360. ubi->dev.class = ubi_class;
  361. dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
  362. err = device_register(&ubi->dev);
  363. if (err)
  364. return err;
  365. *ref = 1;
  366. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  367. if (err)
  368. return err;
  369. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  370. if (err)
  371. return err;
  372. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  373. if (err)
  374. return err;
  375. err = device_create_file(&ubi->dev, &dev_volumes_count);
  376. if (err)
  377. return err;
  378. err = device_create_file(&ubi->dev, &dev_max_ec);
  379. if (err)
  380. return err;
  381. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  382. if (err)
  383. return err;
  384. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  385. if (err)
  386. return err;
  387. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  388. if (err)
  389. return err;
  390. err = device_create_file(&ubi->dev, &dev_min_io_size);
  391. if (err)
  392. return err;
  393. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  394. if (err)
  395. return err;
  396. err = device_create_file(&ubi->dev, &dev_mtd_num);
  397. return err;
  398. }
  399. /**
  400. * ubi_sysfs_close - close sysfs for an UBI device.
  401. * @ubi: UBI device description object
  402. */
  403. static void ubi_sysfs_close(struct ubi_device *ubi)
  404. {
  405. device_remove_file(&ubi->dev, &dev_mtd_num);
  406. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  407. device_remove_file(&ubi->dev, &dev_min_io_size);
  408. device_remove_file(&ubi->dev, &dev_max_vol_count);
  409. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  410. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  411. device_remove_file(&ubi->dev, &dev_max_ec);
  412. device_remove_file(&ubi->dev, &dev_volumes_count);
  413. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  414. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  415. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  416. device_unregister(&ubi->dev);
  417. }
  418. /**
  419. * kill_volumes - destroy all user volumes.
  420. * @ubi: UBI device description object
  421. */
  422. static void kill_volumes(struct ubi_device *ubi)
  423. {
  424. int i;
  425. for (i = 0; i < ubi->vtbl_slots; i++)
  426. if (ubi->volumes[i])
  427. ubi_free_volume(ubi, ubi->volumes[i]);
  428. }
  429. /**
  430. * uif_init - initialize user interfaces for an UBI device.
  431. * @ubi: UBI device description object
  432. * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
  433. * taken, otherwise set to %0
  434. *
  435. * This function initializes various user interfaces for an UBI device. If the
  436. * initialization fails at an early stage, this function frees all the
  437. * resources it allocated, returns an error, and @ref is set to %0. However,
  438. * if the initialization fails after the UBI device was registered in the
  439. * driver core subsystem, this function takes a reference to @ubi->dev, because
  440. * otherwise the release function ('dev_release()') would free whole @ubi
  441. * object. The @ref argument is set to %1 in this case. The caller has to put
  442. * this reference.
  443. *
  444. * This function returns zero in case of success and a negative error code in
  445. * case of failure.
  446. */
  447. static int uif_init(struct ubi_device *ubi, int *ref)
  448. {
  449. int i, err;
  450. dev_t dev;
  451. *ref = 0;
  452. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  453. /*
  454. * Major numbers for the UBI character devices are allocated
  455. * dynamically. Major numbers of volume character devices are
  456. * equivalent to ones of the corresponding UBI character device. Minor
  457. * numbers of UBI character devices are 0, while minor numbers of
  458. * volume character devices start from 1. Thus, we allocate one major
  459. * number and ubi->vtbl_slots + 1 minor numbers.
  460. */
  461. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  462. if (err) {
  463. ubi_err(ubi, "cannot register UBI character devices");
  464. return err;
  465. }
  466. ubi_assert(MINOR(dev) == 0);
  467. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  468. dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
  469. ubi->cdev.owner = THIS_MODULE;
  470. err = cdev_add(&ubi->cdev, dev, 1);
  471. if (err) {
  472. ubi_err(ubi, "cannot add character device");
  473. goto out_unreg;
  474. }
  475. err = ubi_sysfs_init(ubi, ref);
  476. if (err)
  477. goto out_sysfs;
  478. for (i = 0; i < ubi->vtbl_slots; i++)
  479. if (ubi->volumes[i]) {
  480. err = ubi_add_volume(ubi, ubi->volumes[i]);
  481. if (err) {
  482. ubi_err(ubi, "cannot add volume %d", i);
  483. goto out_volumes;
  484. }
  485. }
  486. return 0;
  487. out_volumes:
  488. kill_volumes(ubi);
  489. out_sysfs:
  490. if (*ref)
  491. get_device(&ubi->dev);
  492. ubi_sysfs_close(ubi);
  493. cdev_del(&ubi->cdev);
  494. out_unreg:
  495. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  496. ubi_err(ubi, "cannot initialize UBI %s, error %d",
  497. ubi->ubi_name, err);
  498. return err;
  499. }
  500. /**
  501. * uif_close - close user interfaces for an UBI device.
  502. * @ubi: UBI device description object
  503. *
  504. * Note, since this function un-registers UBI volume device objects (@vol->dev),
  505. * the memory allocated voe the volumes is freed as well (in the release
  506. * function).
  507. */
  508. static void uif_close(struct ubi_device *ubi)
  509. {
  510. kill_volumes(ubi);
  511. ubi_sysfs_close(ubi);
  512. cdev_del(&ubi->cdev);
  513. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  514. }
  515. /**
  516. * ubi_free_internal_volumes - free internal volumes.
  517. * @ubi: UBI device description object
  518. */
  519. void ubi_free_internal_volumes(struct ubi_device *ubi)
  520. {
  521. int i;
  522. for (i = ubi->vtbl_slots;
  523. i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  524. kfree(ubi->volumes[i]->eba_tbl);
  525. kfree(ubi->volumes[i]);
  526. }
  527. }
  528. static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
  529. {
  530. int limit, device_pebs;
  531. uint64_t device_size;
  532. if (!max_beb_per1024)
  533. return 0;
  534. /*
  535. * Here we are using size of the entire flash chip and
  536. * not just the MTD partition size because the maximum
  537. * number of bad eraseblocks is a percentage of the
  538. * whole device and bad eraseblocks are not fairly
  539. * distributed over the flash chip. So the worst case
  540. * is that all the bad eraseblocks of the chip are in
  541. * the MTD partition we are attaching (ubi->mtd).
  542. */
  543. device_size = mtd_get_device_size(ubi->mtd);
  544. device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
  545. limit = mult_frac(device_pebs, max_beb_per1024, 1024);
  546. /* Round it up */
  547. if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
  548. limit += 1;
  549. return limit;
  550. }
  551. /**
  552. * io_init - initialize I/O sub-system for a given UBI device.
  553. * @ubi: UBI device description object
  554. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  555. *
  556. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  557. * assumed:
  558. * o EC header is always at offset zero - this cannot be changed;
  559. * o VID header starts just after the EC header at the closest address
  560. * aligned to @io->hdrs_min_io_size;
  561. * o data starts just after the VID header at the closest address aligned to
  562. * @io->min_io_size
  563. *
  564. * This function returns zero in case of success and a negative error code in
  565. * case of failure.
  566. */
  567. static int io_init(struct ubi_device *ubi, int max_beb_per1024)
  568. {
  569. dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
  570. dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
  571. if (ubi->mtd->numeraseregions != 0) {
  572. /*
  573. * Some flashes have several erase regions. Different regions
  574. * may have different eraseblock size and other
  575. * characteristics. It looks like mostly multi-region flashes
  576. * have one "main" region and one or more small regions to
  577. * store boot loader code or boot parameters or whatever. I
  578. * guess we should just pick the largest region. But this is
  579. * not implemented.
  580. */
  581. ubi_err(ubi, "multiple regions, not implemented");
  582. return -EINVAL;
  583. }
  584. if (ubi->vid_hdr_offset < 0)
  585. return -EINVAL;
  586. /*
  587. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  588. * physical eraseblocks maximum.
  589. */
  590. ubi->peb_size = ubi->mtd->erasesize;
  591. ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
  592. ubi->flash_size = ubi->mtd->size;
  593. if (mtd_can_have_bb(ubi->mtd)) {
  594. ubi->bad_allowed = 1;
  595. ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
  596. }
  597. if (ubi->mtd->type == MTD_NORFLASH) {
  598. ubi_assert(ubi->mtd->writesize == 1);
  599. ubi->nor_flash = 1;
  600. }
  601. ubi->min_io_size = ubi->mtd->writesize;
  602. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  603. /*
  604. * Make sure minimal I/O unit is power of 2. Note, there is no
  605. * fundamental reason for this assumption. It is just an optimization
  606. * which allows us to avoid costly division operations.
  607. */
  608. if (!is_power_of_2(ubi->min_io_size)) {
  609. ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
  610. ubi->min_io_size);
  611. return -EINVAL;
  612. }
  613. ubi_assert(ubi->hdrs_min_io_size > 0);
  614. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  615. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  616. ubi->max_write_size = ubi->mtd->writebufsize;
  617. /*
  618. * Maximum write size has to be greater or equivalent to min. I/O
  619. * size, and be multiple of min. I/O size.
  620. */
  621. if (ubi->max_write_size < ubi->min_io_size ||
  622. ubi->max_write_size % ubi->min_io_size ||
  623. !is_power_of_2(ubi->max_write_size)) {
  624. ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
  625. ubi->max_write_size, ubi->min_io_size);
  626. return -EINVAL;
  627. }
  628. /* Calculate default aligned sizes of EC and VID headers */
  629. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  630. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  631. dbg_gen("min_io_size %d", ubi->min_io_size);
  632. dbg_gen("max_write_size %d", ubi->max_write_size);
  633. dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  634. dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  635. dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  636. if (ubi->vid_hdr_offset == 0)
  637. /* Default offset */
  638. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  639. ubi->ec_hdr_alsize;
  640. else {
  641. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  642. ~(ubi->hdrs_min_io_size - 1);
  643. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  644. ubi->vid_hdr_aloffset;
  645. }
  646. /* Similar for the data offset */
  647. ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
  648. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  649. dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
  650. dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  651. dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
  652. dbg_gen("leb_start %d", ubi->leb_start);
  653. /* The shift must be aligned to 32-bit boundary */
  654. if (ubi->vid_hdr_shift % 4) {
  655. ubi_err(ubi, "unaligned VID header shift %d",
  656. ubi->vid_hdr_shift);
  657. return -EINVAL;
  658. }
  659. /* Check sanity */
  660. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  661. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  662. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  663. ubi->leb_start & (ubi->min_io_size - 1)) {
  664. ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
  665. ubi->vid_hdr_offset, ubi->leb_start);
  666. return -EINVAL;
  667. }
  668. /*
  669. * Set maximum amount of physical erroneous eraseblocks to be 10%.
  670. * Erroneous PEB are those which have read errors.
  671. */
  672. ubi->max_erroneous = ubi->peb_count / 10;
  673. if (ubi->max_erroneous < 16)
  674. ubi->max_erroneous = 16;
  675. dbg_gen("max_erroneous %d", ubi->max_erroneous);
  676. /*
  677. * It may happen that EC and VID headers are situated in one minimal
  678. * I/O unit. In this case we can only accept this UBI image in
  679. * read-only mode.
  680. */
  681. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  682. ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
  683. ubi->ro_mode = 1;
  684. }
  685. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  686. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  687. ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
  688. ubi->mtd->index);
  689. ubi->ro_mode = 1;
  690. }
  691. /*
  692. * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
  693. * unfortunately, MTD does not provide this information. We should loop
  694. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  695. * each physical eraseblock. So, we leave @ubi->bad_peb_count
  696. * uninitialized so far.
  697. */
  698. return 0;
  699. }
  700. /**
  701. * autoresize - re-size the volume which has the "auto-resize" flag set.
  702. * @ubi: UBI device description object
  703. * @vol_id: ID of the volume to re-size
  704. *
  705. * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
  706. * the volume table to the largest possible size. See comments in ubi-header.h
  707. * for more description of the flag. Returns zero in case of success and a
  708. * negative error code in case of failure.
  709. */
  710. static int autoresize(struct ubi_device *ubi, int vol_id)
  711. {
  712. struct ubi_volume_desc desc;
  713. struct ubi_volume *vol = ubi->volumes[vol_id];
  714. int err, old_reserved_pebs = vol->reserved_pebs;
  715. if (ubi->ro_mode) {
  716. ubi_warn(ubi, "skip auto-resize because of R/O mode");
  717. return 0;
  718. }
  719. /*
  720. * Clear the auto-resize flag in the volume in-memory copy of the
  721. * volume table, and 'ubi_resize_volume()' will propagate this change
  722. * to the flash.
  723. */
  724. ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
  725. if (ubi->avail_pebs == 0) {
  726. struct ubi_vtbl_record vtbl_rec;
  727. /*
  728. * No available PEBs to re-size the volume, clear the flag on
  729. * flash and exit.
  730. */
  731. vtbl_rec = ubi->vtbl[vol_id];
  732. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  733. if (err)
  734. ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
  735. vol_id);
  736. } else {
  737. desc.vol = vol;
  738. err = ubi_resize_volume(&desc,
  739. old_reserved_pebs + ubi->avail_pebs);
  740. if (err)
  741. ubi_err(ubi, "cannot auto-resize volume %d",
  742. vol_id);
  743. }
  744. if (err)
  745. return err;
  746. ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
  747. vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
  748. return 0;
  749. }
  750. /**
  751. * ubi_attach_mtd_dev - attach an MTD device.
  752. * @mtd: MTD device description object
  753. * @ubi_num: number to assign to the new UBI device
  754. * @vid_hdr_offset: VID header offset
  755. * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
  756. *
  757. * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
  758. * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
  759. * which case this function finds a vacant device number and assigns it
  760. * automatically. Returns the new UBI device number in case of success and a
  761. * negative error code in case of failure.
  762. *
  763. * Note, the invocations of this function has to be serialized by the
  764. * @ubi_devices_mutex.
  765. */
  766. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  767. int vid_hdr_offset, int max_beb_per1024)
  768. {
  769. struct ubi_device *ubi;
  770. int i, err, ref = 0;
  771. if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
  772. return -EINVAL;
  773. if (!max_beb_per1024)
  774. max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
  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. ubi_err(ubi, "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(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
  799. 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. ubi_err(ubi, "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. ubi_err(ubi, "already exists");
  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. #ifdef CONFIG_MTD_UBI_FASTMAP
  829. ubi->fm_pool.used = ubi->fm_pool.size = 0;
  830. ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
  831. /*
  832. * fm_pool.max_size is 5% of the total number of PEBs but it's also
  833. * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
  834. */
  835. ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
  836. ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
  837. if (ubi->fm_pool.max_size < UBI_FM_MIN_POOL_SIZE)
  838. ubi->fm_pool.max_size = UBI_FM_MIN_POOL_SIZE;
  839. ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
  840. ubi->fm_disabled = !fm_autoconvert;
  841. if (fm_debug)
  842. ubi_enable_dbg_chk_fastmap(ubi);
  843. if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
  844. <= UBI_FM_MAX_START) {
  845. ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
  846. UBI_FM_MAX_START);
  847. ubi->fm_disabled = 1;
  848. }
  849. ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
  850. ubi_msg(ubi, "default fastmap WL pool size: %d",
  851. ubi->fm_wl_pool.max_size);
  852. #else
  853. ubi->fm_disabled = 1;
  854. #endif
  855. mutex_init(&ubi->buf_mutex);
  856. mutex_init(&ubi->ckvol_mutex);
  857. mutex_init(&ubi->device_mutex);
  858. spin_lock_init(&ubi->volumes_lock);
  859. init_rwsem(&ubi->fm_protect);
  860. init_rwsem(&ubi->fm_eba_sem);
  861. ubi_msg(ubi, "attaching mtd%d", mtd->index);
  862. err = io_init(ubi, max_beb_per1024);
  863. if (err)
  864. goto out_free;
  865. err = -ENOMEM;
  866. ubi->peb_buf = vmalloc(ubi->peb_size);
  867. if (!ubi->peb_buf)
  868. goto out_free;
  869. #ifdef CONFIG_MTD_UBI_FASTMAP
  870. ubi->fm_size = ubi_calc_fm_size(ubi);
  871. ubi->fm_buf = vzalloc(ubi->fm_size);
  872. if (!ubi->fm_buf)
  873. goto out_free;
  874. #endif
  875. err = ubi_attach(ubi, 0);
  876. if (err) {
  877. ubi_err(ubi, "failed to attach mtd%d, error %d",
  878. mtd->index, err);
  879. goto out_free;
  880. }
  881. if (ubi->autoresize_vol_id != -1) {
  882. err = autoresize(ubi, ubi->autoresize_vol_id);
  883. if (err)
  884. goto out_detach;
  885. }
  886. err = uif_init(ubi, &ref);
  887. if (err)
  888. goto out_detach;
  889. err = ubi_debugfs_init_dev(ubi);
  890. if (err)
  891. goto out_uif;
  892. ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
  893. if (IS_ERR(ubi->bgt_thread)) {
  894. err = PTR_ERR(ubi->bgt_thread);
  895. ubi_err(ubi, "cannot spawn \"%s\", error %d",
  896. ubi->bgt_name, err);
  897. goto out_debugfs;
  898. }
  899. ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
  900. mtd->index, mtd->name, ubi->flash_size >> 20);
  901. ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
  902. ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
  903. ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
  904. ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
  905. ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
  906. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
  907. ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
  908. ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
  909. ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
  910. ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
  911. ubi->vtbl_slots);
  912. ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
  913. ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
  914. ubi->image_seq);
  915. ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
  916. ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
  917. /*
  918. * The below lock makes sure we do not race with 'ubi_thread()' which
  919. * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
  920. */
  921. spin_lock(&ubi->wl_lock);
  922. ubi->thread_enabled = 1;
  923. wake_up_process(ubi->bgt_thread);
  924. spin_unlock(&ubi->wl_lock);
  925. ubi_devices[ubi_num] = ubi;
  926. ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
  927. return ubi_num;
  928. out_debugfs:
  929. ubi_debugfs_exit_dev(ubi);
  930. out_uif:
  931. get_device(&ubi->dev);
  932. ubi_assert(ref);
  933. uif_close(ubi);
  934. out_detach:
  935. ubi_wl_close(ubi);
  936. ubi_free_internal_volumes(ubi);
  937. vfree(ubi->vtbl);
  938. out_free:
  939. vfree(ubi->peb_buf);
  940. vfree(ubi->fm_buf);
  941. if (ref)
  942. put_device(&ubi->dev);
  943. else
  944. kfree(ubi);
  945. return err;
  946. }
  947. /**
  948. * ubi_detach_mtd_dev - detach an MTD device.
  949. * @ubi_num: UBI device number to detach from
  950. * @anyway: detach MTD even if device reference count is not zero
  951. *
  952. * This function destroys an UBI device number @ubi_num and detaches the
  953. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  954. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  955. * exist.
  956. *
  957. * Note, the invocations of this function has to be serialized by the
  958. * @ubi_devices_mutex.
  959. */
  960. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  961. {
  962. struct ubi_device *ubi;
  963. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  964. return -EINVAL;
  965. ubi = ubi_get_device(ubi_num);
  966. if (!ubi)
  967. return -EINVAL;
  968. spin_lock(&ubi_devices_lock);
  969. put_device(&ubi->dev);
  970. ubi->ref_count -= 1;
  971. if (ubi->ref_count) {
  972. if (!anyway) {
  973. spin_unlock(&ubi_devices_lock);
  974. return -EBUSY;
  975. }
  976. /* This may only happen if there is a bug */
  977. ubi_err(ubi, "%s reference count %d, destroy anyway",
  978. ubi->ubi_name, ubi->ref_count);
  979. }
  980. ubi_devices[ubi_num] = NULL;
  981. spin_unlock(&ubi_devices_lock);
  982. ubi_assert(ubi_num == ubi->ubi_num);
  983. ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
  984. ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
  985. #ifdef CONFIG_MTD_UBI_FASTMAP
  986. /* If we don't write a new fastmap at detach time we lose all
  987. * EC updates that have been made since the last written fastmap.
  988. * In case of fastmap debugging we omit the update to simulate an
  989. * unclean shutdown. */
  990. if (!ubi_dbg_chk_fastmap(ubi))
  991. ubi_update_fastmap(ubi);
  992. #endif
  993. /*
  994. * Before freeing anything, we have to stop the background thread to
  995. * prevent it from doing anything on this device while we are freeing.
  996. */
  997. if (ubi->bgt_thread)
  998. kthread_stop(ubi->bgt_thread);
  999. /*
  1000. * Get a reference to the device in order to prevent 'dev_release()'
  1001. * from freeing the @ubi object.
  1002. */
  1003. get_device(&ubi->dev);
  1004. ubi_debugfs_exit_dev(ubi);
  1005. uif_close(ubi);
  1006. ubi_wl_close(ubi);
  1007. ubi_free_internal_volumes(ubi);
  1008. vfree(ubi->vtbl);
  1009. put_mtd_device(ubi->mtd);
  1010. vfree(ubi->peb_buf);
  1011. vfree(ubi->fm_buf);
  1012. ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
  1013. put_device(&ubi->dev);
  1014. return 0;
  1015. }
  1016. /**
  1017. * open_mtd_by_chdev - open an MTD device by its character device node path.
  1018. * @mtd_dev: MTD character device node path
  1019. *
  1020. * This helper function opens an MTD device by its character node device path.
  1021. * Returns MTD device description object in case of success and a negative
  1022. * error code in case of failure.
  1023. */
  1024. static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
  1025. {
  1026. int err, major, minor, mode;
  1027. struct path path;
  1028. /* Probably this is an MTD character device node path */
  1029. err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
  1030. if (err)
  1031. return ERR_PTR(err);
  1032. /* MTD device number is defined by the major / minor numbers */
  1033. major = imajor(d_backing_inode(path.dentry));
  1034. minor = iminor(d_backing_inode(path.dentry));
  1035. mode = d_backing_inode(path.dentry)->i_mode;
  1036. path_put(&path);
  1037. if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
  1038. return ERR_PTR(-EINVAL);
  1039. if (minor & 1)
  1040. /*
  1041. * Just do not think the "/dev/mtdrX" devices support is need,
  1042. * so do not support them to avoid doing extra work.
  1043. */
  1044. return ERR_PTR(-EINVAL);
  1045. return get_mtd_device(NULL, minor / 2);
  1046. }
  1047. /**
  1048. * open_mtd_device - open MTD device by name, character device path, or number.
  1049. * @mtd_dev: name, character device node path, or MTD device device number
  1050. *
  1051. * This function tries to open and MTD device described by @mtd_dev string,
  1052. * which is first treated as ASCII MTD device number, and if it is not true, it
  1053. * is treated as MTD device name, and if that is also not true, it is treated
  1054. * as MTD character device node path. Returns MTD device description object in
  1055. * case of success and a negative error code in case of failure.
  1056. */
  1057. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  1058. {
  1059. struct mtd_info *mtd;
  1060. int mtd_num;
  1061. char *endp;
  1062. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  1063. if (*endp != '\0' || mtd_dev == endp) {
  1064. /*
  1065. * This does not look like an ASCII integer, probably this is
  1066. * MTD device name.
  1067. */
  1068. mtd = get_mtd_device_nm(mtd_dev);
  1069. if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
  1070. /* Probably this is an MTD character device node path */
  1071. mtd = open_mtd_by_chdev(mtd_dev);
  1072. } else
  1073. mtd = get_mtd_device(NULL, mtd_num);
  1074. return mtd;
  1075. }
  1076. static int __init ubi_init(void)
  1077. {
  1078. int err, i, k;
  1079. /* Ensure that EC and VID headers have correct size */
  1080. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  1081. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  1082. if (mtd_devs > UBI_MAX_DEVICES) {
  1083. pr_err("UBI error: too many MTD devices, maximum is %d",
  1084. UBI_MAX_DEVICES);
  1085. return -EINVAL;
  1086. }
  1087. /* Create base sysfs directory and sysfs files */
  1088. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  1089. if (IS_ERR(ubi_class)) {
  1090. err = PTR_ERR(ubi_class);
  1091. pr_err("UBI error: cannot create UBI class");
  1092. goto out;
  1093. }
  1094. err = class_create_file(ubi_class, &ubi_version);
  1095. if (err) {
  1096. pr_err("UBI error: cannot create sysfs file");
  1097. goto out_class;
  1098. }
  1099. err = misc_register(&ubi_ctrl_cdev);
  1100. if (err) {
  1101. pr_err("UBI error: cannot register device");
  1102. goto out_version;
  1103. }
  1104. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  1105. sizeof(struct ubi_wl_entry),
  1106. 0, 0, NULL);
  1107. if (!ubi_wl_entry_slab) {
  1108. err = -ENOMEM;
  1109. goto out_dev_unreg;
  1110. }
  1111. err = ubi_debugfs_init();
  1112. if (err)
  1113. goto out_slab;
  1114. /* Attach MTD devices */
  1115. for (i = 0; i < mtd_devs; i++) {
  1116. struct mtd_dev_param *p = &mtd_dev_param[i];
  1117. struct mtd_info *mtd;
  1118. cond_resched();
  1119. mtd = open_mtd_device(p->name);
  1120. if (IS_ERR(mtd)) {
  1121. err = PTR_ERR(mtd);
  1122. pr_err("UBI error: cannot open mtd %s, error %d",
  1123. p->name, err);
  1124. /* See comment below re-ubi_is_module(). */
  1125. if (ubi_is_module())
  1126. goto out_detach;
  1127. continue;
  1128. }
  1129. mutex_lock(&ubi_devices_mutex);
  1130. err = ubi_attach_mtd_dev(mtd, p->ubi_num,
  1131. p->vid_hdr_offs, p->max_beb_per1024);
  1132. mutex_unlock(&ubi_devices_mutex);
  1133. if (err < 0) {
  1134. pr_err("UBI error: cannot attach mtd%d",
  1135. mtd->index);
  1136. put_mtd_device(mtd);
  1137. /*
  1138. * Originally UBI stopped initializing on any error.
  1139. * However, later on it was found out that this
  1140. * behavior is not very good when UBI is compiled into
  1141. * the kernel and the MTD devices to attach are passed
  1142. * through the command line. Indeed, UBI failure
  1143. * stopped whole boot sequence.
  1144. *
  1145. * To fix this, we changed the behavior for the
  1146. * non-module case, but preserved the old behavior for
  1147. * the module case, just for compatibility. This is a
  1148. * little inconsistent, though.
  1149. */
  1150. if (ubi_is_module())
  1151. goto out_detach;
  1152. }
  1153. }
  1154. err = ubiblock_init();
  1155. if (err) {
  1156. pr_err("UBI error: block: cannot initialize, error %d", err);
  1157. /* See comment above re-ubi_is_module(). */
  1158. if (ubi_is_module())
  1159. goto out_detach;
  1160. }
  1161. return 0;
  1162. out_detach:
  1163. for (k = 0; k < i; k++)
  1164. if (ubi_devices[k]) {
  1165. mutex_lock(&ubi_devices_mutex);
  1166. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  1167. mutex_unlock(&ubi_devices_mutex);
  1168. }
  1169. ubi_debugfs_exit();
  1170. out_slab:
  1171. kmem_cache_destroy(ubi_wl_entry_slab);
  1172. out_dev_unreg:
  1173. misc_deregister(&ubi_ctrl_cdev);
  1174. out_version:
  1175. class_remove_file(ubi_class, &ubi_version);
  1176. out_class:
  1177. class_destroy(ubi_class);
  1178. out:
  1179. pr_err("UBI error: cannot initialize UBI, error %d", err);
  1180. return err;
  1181. }
  1182. late_initcall(ubi_init);
  1183. static void __exit ubi_exit(void)
  1184. {
  1185. int i;
  1186. ubiblock_exit();
  1187. for (i = 0; i < UBI_MAX_DEVICES; i++)
  1188. if (ubi_devices[i]) {
  1189. mutex_lock(&ubi_devices_mutex);
  1190. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  1191. mutex_unlock(&ubi_devices_mutex);
  1192. }
  1193. ubi_debugfs_exit();
  1194. kmem_cache_destroy(ubi_wl_entry_slab);
  1195. misc_deregister(&ubi_ctrl_cdev);
  1196. class_remove_file(ubi_class, &ubi_version);
  1197. class_destroy(ubi_class);
  1198. }
  1199. module_exit(ubi_exit);
  1200. /**
  1201. * bytes_str_to_int - convert a number of bytes string into an integer.
  1202. * @str: the string to convert
  1203. *
  1204. * This function returns positive resulting integer in case of success and a
  1205. * negative error code in case of failure.
  1206. */
  1207. static int __init bytes_str_to_int(const char *str)
  1208. {
  1209. char *endp;
  1210. unsigned long result;
  1211. result = simple_strtoul(str, &endp, 0);
  1212. if (str == endp || result >= INT_MAX) {
  1213. pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
  1214. return -EINVAL;
  1215. }
  1216. switch (*endp) {
  1217. case 'G':
  1218. result *= 1024;
  1219. case 'M':
  1220. result *= 1024;
  1221. case 'K':
  1222. result *= 1024;
  1223. if (endp[1] == 'i' && endp[2] == 'B')
  1224. endp += 2;
  1225. case '\0':
  1226. break;
  1227. default:
  1228. pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
  1229. return -EINVAL;
  1230. }
  1231. return result;
  1232. }
  1233. /**
  1234. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  1235. * @val: the parameter value to parse
  1236. * @kp: not used
  1237. *
  1238. * This function returns zero in case of success and a negative error code in
  1239. * case of error.
  1240. */
  1241. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1242. {
  1243. int i, len;
  1244. struct mtd_dev_param *p;
  1245. char buf[MTD_PARAM_LEN_MAX];
  1246. char *pbuf = &buf[0];
  1247. char *tokens[MTD_PARAM_MAX_COUNT], *token;
  1248. if (!val)
  1249. return -EINVAL;
  1250. if (mtd_devs == UBI_MAX_DEVICES) {
  1251. pr_err("UBI error: too many parameters, max. is %d\n",
  1252. UBI_MAX_DEVICES);
  1253. return -EINVAL;
  1254. }
  1255. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1256. if (len == MTD_PARAM_LEN_MAX) {
  1257. pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
  1258. val, MTD_PARAM_LEN_MAX);
  1259. return -EINVAL;
  1260. }
  1261. if (len == 0) {
  1262. pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
  1263. return 0;
  1264. }
  1265. strcpy(buf, val);
  1266. /* Get rid of the final newline */
  1267. if (buf[len - 1] == '\n')
  1268. buf[len - 1] = '\0';
  1269. for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
  1270. tokens[i] = strsep(&pbuf, ",");
  1271. if (pbuf) {
  1272. pr_err("UBI error: too many arguments at \"%s\"\n", val);
  1273. return -EINVAL;
  1274. }
  1275. p = &mtd_dev_param[mtd_devs];
  1276. strcpy(&p->name[0], tokens[0]);
  1277. token = tokens[1];
  1278. if (token) {
  1279. p->vid_hdr_offs = bytes_str_to_int(token);
  1280. if (p->vid_hdr_offs < 0)
  1281. return p->vid_hdr_offs;
  1282. }
  1283. token = tokens[2];
  1284. if (token) {
  1285. int err = kstrtoint(token, 10, &p->max_beb_per1024);
  1286. if (err) {
  1287. pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
  1288. token);
  1289. return -EINVAL;
  1290. }
  1291. }
  1292. token = tokens[3];
  1293. if (token) {
  1294. int err = kstrtoint(token, 10, &p->ubi_num);
  1295. if (err) {
  1296. pr_err("UBI error: bad value for ubi_num parameter: %s",
  1297. token);
  1298. return -EINVAL;
  1299. }
  1300. } else
  1301. p->ubi_num = UBI_DEV_NUM_AUTO;
  1302. mtd_devs += 1;
  1303. return 0;
  1304. }
  1305. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1306. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
  1307. "Multiple \"mtd\" parameters may be specified.\n"
  1308. "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
  1309. "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
  1310. "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
  1311. __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
  1312. "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
  1313. "\n"
  1314. "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
  1315. "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
  1316. "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
  1317. "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
  1318. "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
  1319. #ifdef CONFIG_MTD_UBI_FASTMAP
  1320. module_param(fm_autoconvert, bool, 0644);
  1321. MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
  1322. module_param(fm_debug, bool, 0);
  1323. MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
  1324. #endif
  1325. MODULE_VERSION(__stringify(UBI_VERSION));
  1326. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1327. MODULE_AUTHOR("Artem Bityutskiy");
  1328. MODULE_LICENSE("GPL");