build.c 41 KB

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