build.c 44 KB

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