build.c 43 KB

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