build.c 41 KB

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