build.c 43 KB

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