build.c 44 KB

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