build.c 41 KB

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