build.c 42 KB

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