eba.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Artem Bityutskiy (Битюцкий Артём)
  19. */
  20. /*
  21. * The UBI Eraseblock Association (EBA) sub-system.
  22. *
  23. * This sub-system is responsible for I/O to/from logical eraseblock.
  24. *
  25. * Although in this implementation the EBA table is fully kept and managed in
  26. * RAM, which assumes poor scalability, it might be (partially) maintained on
  27. * flash in future implementations.
  28. *
  29. * The EBA sub-system implements per-logical eraseblock locking. Before
  30. * accessing a logical eraseblock it is locked for reading or writing. The
  31. * per-logical eraseblock locking is implemented by means of the lock tree. The
  32. * lock tree is an RB-tree which refers all the currently locked logical
  33. * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
  34. * They are indexed by (@vol_id, @lnum) pairs.
  35. *
  36. * EBA also maintains the global sequence counter which is incremented each
  37. * time a logical eraseblock is mapped to a physical eraseblock and it is
  38. * stored in the volume identifier header. This means that each VID header has
  39. * a unique sequence number. The sequence number is only increased an we assume
  40. * 64 bits is enough to never overflow.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/crc32.h>
  44. #include <linux/err.h>
  45. #include "ubi.h"
  46. /* Number of physical eraseblocks reserved for atomic LEB change operation */
  47. #define EBA_RESERVED_PEBS 1
  48. /**
  49. * struct ubi_eba_entry - structure encoding a single LEB -> PEB association
  50. * @pnum: the physical eraseblock number attached to the LEB
  51. *
  52. * This structure is encoding a LEB -> PEB association. Note that the LEB
  53. * number is not stored here, because it is the index used to access the
  54. * entries table.
  55. */
  56. struct ubi_eba_entry {
  57. int pnum;
  58. };
  59. /**
  60. * struct ubi_eba_table - LEB -> PEB association information
  61. * @entries: the LEB to PEB mapping (one entry per LEB).
  62. *
  63. * This structure is private to the EBA logic and should be kept here.
  64. * It is encoding the LEB to PEB association table, and is subject to
  65. * changes.
  66. */
  67. struct ubi_eba_table {
  68. struct ubi_eba_entry *entries;
  69. };
  70. /**
  71. * next_sqnum - get next sequence number.
  72. * @ubi: UBI device description object
  73. *
  74. * This function returns next sequence number to use, which is just the current
  75. * global sequence counter value. It also increases the global sequence
  76. * counter.
  77. */
  78. unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
  79. {
  80. unsigned long long sqnum;
  81. spin_lock(&ubi->ltree_lock);
  82. sqnum = ubi->global_sqnum++;
  83. spin_unlock(&ubi->ltree_lock);
  84. return sqnum;
  85. }
  86. /**
  87. * ubi_get_compat - get compatibility flags of a volume.
  88. * @ubi: UBI device description object
  89. * @vol_id: volume ID
  90. *
  91. * This function returns compatibility flags for an internal volume. User
  92. * volumes have no compatibility flags, so %0 is returned.
  93. */
  94. static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
  95. {
  96. if (vol_id == UBI_LAYOUT_VOLUME_ID)
  97. return UBI_LAYOUT_VOLUME_COMPAT;
  98. return 0;
  99. }
  100. /**
  101. * ubi_eba_get_ldesc - get information about a LEB
  102. * @vol: volume description object
  103. * @lnum: logical eraseblock number
  104. * @ldesc: the LEB descriptor to fill
  105. *
  106. * Used to query information about a specific LEB.
  107. * It is currently only returning the physical position of the LEB, but will be
  108. * extended to provide more information.
  109. */
  110. void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
  111. struct ubi_eba_leb_desc *ldesc)
  112. {
  113. ldesc->lnum = lnum;
  114. ldesc->pnum = vol->eba_tbl->entries[lnum].pnum;
  115. }
  116. /**
  117. * ubi_eba_create_table - allocate a new EBA table and initialize it with all
  118. * LEBs unmapped
  119. * @vol: volume containing the EBA table to copy
  120. * @nentries: number of entries in the table
  121. *
  122. * Allocate a new EBA table and initialize it with all LEBs unmapped.
  123. * Returns a valid pointer if it succeed, an ERR_PTR() otherwise.
  124. */
  125. struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
  126. int nentries)
  127. {
  128. struct ubi_eba_table *tbl;
  129. int err = -ENOMEM;
  130. int i;
  131. tbl = kzalloc(sizeof(*tbl), GFP_KERNEL);
  132. if (!tbl)
  133. return ERR_PTR(-ENOMEM);
  134. tbl->entries = kmalloc_array(nentries, sizeof(*tbl->entries),
  135. GFP_KERNEL);
  136. if (!tbl->entries)
  137. goto err;
  138. for (i = 0; i < nentries; i++)
  139. tbl->entries[i].pnum = UBI_LEB_UNMAPPED;
  140. return tbl;
  141. err:
  142. kfree(tbl->entries);
  143. kfree(tbl);
  144. return ERR_PTR(err);
  145. }
  146. /**
  147. * ubi_eba_destroy_table - destroy an EBA table
  148. * @tbl: the table to destroy
  149. *
  150. * Destroy an EBA table.
  151. */
  152. void ubi_eba_destroy_table(struct ubi_eba_table *tbl)
  153. {
  154. if (!tbl)
  155. return;
  156. kfree(tbl->entries);
  157. kfree(tbl);
  158. }
  159. /**
  160. * ubi_eba_copy_table - copy the EBA table attached to vol into another table
  161. * @vol: volume containing the EBA table to copy
  162. * @dst: destination
  163. * @nentries: number of entries to copy
  164. *
  165. * Copy the EBA table stored in vol into the one pointed by dst.
  166. */
  167. void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
  168. int nentries)
  169. {
  170. struct ubi_eba_table *src;
  171. int i;
  172. ubi_assert(dst && vol && vol->eba_tbl);
  173. src = vol->eba_tbl;
  174. for (i = 0; i < nentries; i++)
  175. dst->entries[i].pnum = src->entries[i].pnum;
  176. }
  177. /**
  178. * ubi_eba_replace_table - assign a new EBA table to a volume
  179. * @vol: volume containing the EBA table to copy
  180. * @tbl: new EBA table
  181. *
  182. * Assign a new EBA table to the volume and release the old one.
  183. */
  184. void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl)
  185. {
  186. ubi_eba_destroy_table(vol->eba_tbl);
  187. vol->eba_tbl = tbl;
  188. }
  189. /**
  190. * ltree_lookup - look up the lock tree.
  191. * @ubi: UBI device description object
  192. * @vol_id: volume ID
  193. * @lnum: logical eraseblock number
  194. *
  195. * This function returns a pointer to the corresponding &struct ubi_ltree_entry
  196. * object if the logical eraseblock is locked and %NULL if it is not.
  197. * @ubi->ltree_lock has to be locked.
  198. */
  199. static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
  200. int lnum)
  201. {
  202. struct rb_node *p;
  203. p = ubi->ltree.rb_node;
  204. while (p) {
  205. struct ubi_ltree_entry *le;
  206. le = rb_entry(p, struct ubi_ltree_entry, rb);
  207. if (vol_id < le->vol_id)
  208. p = p->rb_left;
  209. else if (vol_id > le->vol_id)
  210. p = p->rb_right;
  211. else {
  212. if (lnum < le->lnum)
  213. p = p->rb_left;
  214. else if (lnum > le->lnum)
  215. p = p->rb_right;
  216. else
  217. return le;
  218. }
  219. }
  220. return NULL;
  221. }
  222. /**
  223. * ltree_add_entry - add new entry to the lock tree.
  224. * @ubi: UBI device description object
  225. * @vol_id: volume ID
  226. * @lnum: logical eraseblock number
  227. *
  228. * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
  229. * lock tree. If such entry is already there, its usage counter is increased.
  230. * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
  231. * failed.
  232. */
  233. static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
  234. int vol_id, int lnum)
  235. {
  236. struct ubi_ltree_entry *le, *le1, *le_free;
  237. le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
  238. if (!le)
  239. return ERR_PTR(-ENOMEM);
  240. le->users = 0;
  241. init_rwsem(&le->mutex);
  242. le->vol_id = vol_id;
  243. le->lnum = lnum;
  244. spin_lock(&ubi->ltree_lock);
  245. le1 = ltree_lookup(ubi, vol_id, lnum);
  246. if (le1) {
  247. /*
  248. * This logical eraseblock is already locked. The newly
  249. * allocated lock entry is not needed.
  250. */
  251. le_free = le;
  252. le = le1;
  253. } else {
  254. struct rb_node **p, *parent = NULL;
  255. /*
  256. * No lock entry, add the newly allocated one to the
  257. * @ubi->ltree RB-tree.
  258. */
  259. le_free = NULL;
  260. p = &ubi->ltree.rb_node;
  261. while (*p) {
  262. parent = *p;
  263. le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
  264. if (vol_id < le1->vol_id)
  265. p = &(*p)->rb_left;
  266. else if (vol_id > le1->vol_id)
  267. p = &(*p)->rb_right;
  268. else {
  269. ubi_assert(lnum != le1->lnum);
  270. if (lnum < le1->lnum)
  271. p = &(*p)->rb_left;
  272. else
  273. p = &(*p)->rb_right;
  274. }
  275. }
  276. rb_link_node(&le->rb, parent, p);
  277. rb_insert_color(&le->rb, &ubi->ltree);
  278. }
  279. le->users += 1;
  280. spin_unlock(&ubi->ltree_lock);
  281. kfree(le_free);
  282. return le;
  283. }
  284. /**
  285. * leb_read_lock - lock logical eraseblock for reading.
  286. * @ubi: UBI device description object
  287. * @vol_id: volume ID
  288. * @lnum: logical eraseblock number
  289. *
  290. * This function locks a logical eraseblock for reading. Returns zero in case
  291. * of success and a negative error code in case of failure.
  292. */
  293. static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
  294. {
  295. struct ubi_ltree_entry *le;
  296. le = ltree_add_entry(ubi, vol_id, lnum);
  297. if (IS_ERR(le))
  298. return PTR_ERR(le);
  299. down_read(&le->mutex);
  300. return 0;
  301. }
  302. /**
  303. * leb_read_unlock - unlock logical eraseblock.
  304. * @ubi: UBI device description object
  305. * @vol_id: volume ID
  306. * @lnum: logical eraseblock number
  307. */
  308. static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  309. {
  310. struct ubi_ltree_entry *le;
  311. spin_lock(&ubi->ltree_lock);
  312. le = ltree_lookup(ubi, vol_id, lnum);
  313. le->users -= 1;
  314. ubi_assert(le->users >= 0);
  315. up_read(&le->mutex);
  316. if (le->users == 0) {
  317. rb_erase(&le->rb, &ubi->ltree);
  318. kfree(le);
  319. }
  320. spin_unlock(&ubi->ltree_lock);
  321. }
  322. /**
  323. * leb_write_lock - lock logical eraseblock for writing.
  324. * @ubi: UBI device description object
  325. * @vol_id: volume ID
  326. * @lnum: logical eraseblock number
  327. *
  328. * This function locks a logical eraseblock for writing. Returns zero in case
  329. * of success and a negative error code in case of failure.
  330. */
  331. static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
  332. {
  333. struct ubi_ltree_entry *le;
  334. le = ltree_add_entry(ubi, vol_id, lnum);
  335. if (IS_ERR(le))
  336. return PTR_ERR(le);
  337. down_write(&le->mutex);
  338. return 0;
  339. }
  340. /**
  341. * leb_write_lock - lock logical eraseblock for writing.
  342. * @ubi: UBI device description object
  343. * @vol_id: volume ID
  344. * @lnum: logical eraseblock number
  345. *
  346. * This function locks a logical eraseblock for writing if there is no
  347. * contention and does nothing if there is contention. Returns %0 in case of
  348. * success, %1 in case of contention, and and a negative error code in case of
  349. * failure.
  350. */
  351. static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
  352. {
  353. struct ubi_ltree_entry *le;
  354. le = ltree_add_entry(ubi, vol_id, lnum);
  355. if (IS_ERR(le))
  356. return PTR_ERR(le);
  357. if (down_write_trylock(&le->mutex))
  358. return 0;
  359. /* Contention, cancel */
  360. spin_lock(&ubi->ltree_lock);
  361. le->users -= 1;
  362. ubi_assert(le->users >= 0);
  363. if (le->users == 0) {
  364. rb_erase(&le->rb, &ubi->ltree);
  365. kfree(le);
  366. }
  367. spin_unlock(&ubi->ltree_lock);
  368. return 1;
  369. }
  370. /**
  371. * leb_write_unlock - unlock logical eraseblock.
  372. * @ubi: UBI device description object
  373. * @vol_id: volume ID
  374. * @lnum: logical eraseblock number
  375. */
  376. static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  377. {
  378. struct ubi_ltree_entry *le;
  379. spin_lock(&ubi->ltree_lock);
  380. le = ltree_lookup(ubi, vol_id, lnum);
  381. le->users -= 1;
  382. ubi_assert(le->users >= 0);
  383. up_write(&le->mutex);
  384. if (le->users == 0) {
  385. rb_erase(&le->rb, &ubi->ltree);
  386. kfree(le);
  387. }
  388. spin_unlock(&ubi->ltree_lock);
  389. }
  390. /**
  391. * ubi_eba_is_mapped - check if a LEB is mapped.
  392. * @vol: volume description object
  393. * @lnum: logical eraseblock number
  394. *
  395. * This function returns true if the LEB is mapped, false otherwise.
  396. */
  397. bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum)
  398. {
  399. return vol->eba_tbl->entries[lnum].pnum >= 0;
  400. }
  401. /**
  402. * ubi_eba_unmap_leb - un-map logical eraseblock.
  403. * @ubi: UBI device description object
  404. * @vol: volume description object
  405. * @lnum: logical eraseblock number
  406. *
  407. * This function un-maps logical eraseblock @lnum and schedules corresponding
  408. * physical eraseblock for erasure. Returns zero in case of success and a
  409. * negative error code in case of failure.
  410. */
  411. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  412. int lnum)
  413. {
  414. int err, pnum, vol_id = vol->vol_id;
  415. if (ubi->ro_mode)
  416. return -EROFS;
  417. err = leb_write_lock(ubi, vol_id, lnum);
  418. if (err)
  419. return err;
  420. pnum = vol->eba_tbl->entries[lnum].pnum;
  421. if (pnum < 0)
  422. /* This logical eraseblock is already unmapped */
  423. goto out_unlock;
  424. dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
  425. down_read(&ubi->fm_eba_sem);
  426. vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
  427. up_read(&ubi->fm_eba_sem);
  428. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
  429. out_unlock:
  430. leb_write_unlock(ubi, vol_id, lnum);
  431. return err;
  432. }
  433. #ifdef CONFIG_MTD_UBI_FASTMAP
  434. /**
  435. * check_mapping - check and fixup a mapping
  436. * @ubi: UBI device description object
  437. * @vol: volume description object
  438. * @lnum: logical eraseblock number
  439. * @pnum: physical eraseblock number
  440. *
  441. * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap
  442. * operations, if such an operation is interrupted the mapping still looks
  443. * good, but upon first read an ECC is reported to the upper layer.
  444. * Normaly during the full-scan at attach time this is fixed, for Fastmap
  445. * we have to deal with it while reading.
  446. * If the PEB behind a LEB shows this symthom we change the mapping to
  447. * %UBI_LEB_UNMAPPED and schedule the PEB for erasure.
  448. *
  449. * Returns 0 on success, negative error code in case of failure.
  450. */
  451. static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  452. int *pnum)
  453. {
  454. int err;
  455. struct ubi_vid_io_buf *vidb;
  456. if (!ubi->fast_attach)
  457. return 0;
  458. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  459. if (!vidb)
  460. return -ENOMEM;
  461. err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0);
  462. if (err > 0 && err != UBI_IO_BITFLIPS) {
  463. int torture = 0;
  464. switch (err) {
  465. case UBI_IO_FF:
  466. case UBI_IO_FF_BITFLIPS:
  467. case UBI_IO_BAD_HDR:
  468. case UBI_IO_BAD_HDR_EBADMSG:
  469. break;
  470. default:
  471. ubi_assert(0);
  472. }
  473. if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS)
  474. torture = 1;
  475. down_read(&ubi->fm_eba_sem);
  476. vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED;
  477. up_read(&ubi->fm_eba_sem);
  478. ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture);
  479. *pnum = UBI_LEB_UNMAPPED;
  480. } else if (err < 0) {
  481. ubi_err(ubi, "unable to read VID header back from PEB %i: %i",
  482. *pnum, err);
  483. goto out_free;
  484. }
  485. err = 0;
  486. out_free:
  487. ubi_free_vid_buf(vidb);
  488. return err;
  489. }
  490. #else
  491. static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  492. int *pnum)
  493. {
  494. return 0;
  495. }
  496. #endif
  497. /**
  498. * ubi_eba_read_leb - read data.
  499. * @ubi: UBI device description object
  500. * @vol: volume description object
  501. * @lnum: logical eraseblock number
  502. * @buf: buffer to store the read data
  503. * @offset: offset from where to read
  504. * @len: how many bytes to read
  505. * @check: data CRC check flag
  506. *
  507. * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
  508. * bytes. The @check flag only makes sense for static volumes and forces
  509. * eraseblock data CRC checking.
  510. *
  511. * In case of success this function returns zero. In case of a static volume,
  512. * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
  513. * returned for any volume type if an ECC error was detected by the MTD device
  514. * driver. Other negative error cored may be returned in case of other errors.
  515. */
  516. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  517. void *buf, int offset, int len, int check)
  518. {
  519. int err, pnum, scrub = 0, vol_id = vol->vol_id;
  520. struct ubi_vid_io_buf *vidb;
  521. struct ubi_vid_hdr *vid_hdr;
  522. uint32_t uninitialized_var(crc);
  523. err = leb_read_lock(ubi, vol_id, lnum);
  524. if (err)
  525. return err;
  526. pnum = vol->eba_tbl->entries[lnum].pnum;
  527. if (pnum >= 0) {
  528. err = check_mapping(ubi, vol, lnum, &pnum);
  529. if (err < 0)
  530. goto out_unlock;
  531. }
  532. if (pnum == UBI_LEB_UNMAPPED) {
  533. /*
  534. * The logical eraseblock is not mapped, fill the whole buffer
  535. * with 0xFF bytes. The exception is static volumes for which
  536. * it is an error to read unmapped logical eraseblocks.
  537. */
  538. dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
  539. len, offset, vol_id, lnum);
  540. leb_read_unlock(ubi, vol_id, lnum);
  541. ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
  542. memset(buf, 0xFF, len);
  543. return 0;
  544. }
  545. dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
  546. len, offset, vol_id, lnum, pnum);
  547. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  548. check = 0;
  549. retry:
  550. if (check) {
  551. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  552. if (!vidb) {
  553. err = -ENOMEM;
  554. goto out_unlock;
  555. }
  556. vid_hdr = ubi_get_vid_hdr(vidb);
  557. err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
  558. if (err && err != UBI_IO_BITFLIPS) {
  559. if (err > 0) {
  560. /*
  561. * The header is either absent or corrupted.
  562. * The former case means there is a bug -
  563. * switch to read-only mode just in case.
  564. * The latter case means a real corruption - we
  565. * may try to recover data. FIXME: but this is
  566. * not implemented.
  567. */
  568. if (err == UBI_IO_BAD_HDR_EBADMSG ||
  569. err == UBI_IO_BAD_HDR) {
  570. ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
  571. pnum, vol_id, lnum);
  572. err = -EBADMSG;
  573. } else {
  574. /*
  575. * Ending up here in the non-Fastmap case
  576. * is a clear bug as the VID header had to
  577. * be present at scan time to have it referenced.
  578. * With fastmap the story is more complicated.
  579. * Fastmap has the mapping info without the need
  580. * of a full scan. So the LEB could have been
  581. * unmapped, Fastmap cannot know this and keeps
  582. * the LEB referenced.
  583. * This is valid and works as the layer above UBI
  584. * has to do bookkeeping about used/referenced
  585. * LEBs in any case.
  586. */
  587. if (ubi->fast_attach) {
  588. err = -EBADMSG;
  589. } else {
  590. err = -EINVAL;
  591. ubi_ro_mode(ubi);
  592. }
  593. }
  594. }
  595. goto out_free;
  596. } else if (err == UBI_IO_BITFLIPS)
  597. scrub = 1;
  598. ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
  599. ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
  600. crc = be32_to_cpu(vid_hdr->data_crc);
  601. ubi_free_vid_buf(vidb);
  602. }
  603. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  604. if (err) {
  605. if (err == UBI_IO_BITFLIPS)
  606. scrub = 1;
  607. else if (mtd_is_eccerr(err)) {
  608. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  609. goto out_unlock;
  610. scrub = 1;
  611. if (!check) {
  612. ubi_msg(ubi, "force data checking");
  613. check = 1;
  614. goto retry;
  615. }
  616. } else
  617. goto out_unlock;
  618. }
  619. if (check) {
  620. uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
  621. if (crc1 != crc) {
  622. ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
  623. crc1, crc);
  624. err = -EBADMSG;
  625. goto out_unlock;
  626. }
  627. }
  628. if (scrub)
  629. err = ubi_wl_scrub_peb(ubi, pnum);
  630. leb_read_unlock(ubi, vol_id, lnum);
  631. return err;
  632. out_free:
  633. ubi_free_vid_buf(vidb);
  634. out_unlock:
  635. leb_read_unlock(ubi, vol_id, lnum);
  636. return err;
  637. }
  638. /**
  639. * ubi_eba_read_leb_sg - read data into a scatter gather list.
  640. * @ubi: UBI device description object
  641. * @vol: volume description object
  642. * @lnum: logical eraseblock number
  643. * @sgl: UBI scatter gather list to store the read data
  644. * @offset: offset from where to read
  645. * @len: how many bytes to read
  646. * @check: data CRC check flag
  647. *
  648. * This function works exactly like ubi_eba_read_leb(). But instead of
  649. * storing the read data into a buffer it writes to an UBI scatter gather
  650. * list.
  651. */
  652. int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
  653. struct ubi_sgl *sgl, int lnum, int offset, int len,
  654. int check)
  655. {
  656. int to_read;
  657. int ret;
  658. struct scatterlist *sg;
  659. for (;;) {
  660. ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
  661. sg = &sgl->sg[sgl->list_pos];
  662. if (len < sg->length - sgl->page_pos)
  663. to_read = len;
  664. else
  665. to_read = sg->length - sgl->page_pos;
  666. ret = ubi_eba_read_leb(ubi, vol, lnum,
  667. sg_virt(sg) + sgl->page_pos, offset,
  668. to_read, check);
  669. if (ret < 0)
  670. return ret;
  671. offset += to_read;
  672. len -= to_read;
  673. if (!len) {
  674. sgl->page_pos += to_read;
  675. if (sgl->page_pos == sg->length) {
  676. sgl->list_pos++;
  677. sgl->page_pos = 0;
  678. }
  679. break;
  680. }
  681. sgl->list_pos++;
  682. sgl->page_pos = 0;
  683. }
  684. return ret;
  685. }
  686. /**
  687. * try_recover_peb - try to recover from write failure.
  688. * @vol: volume description object
  689. * @pnum: the physical eraseblock to recover
  690. * @lnum: logical eraseblock number
  691. * @buf: data which was not written because of the write failure
  692. * @offset: offset of the failed write
  693. * @len: how many bytes should have been written
  694. * @vidb: VID buffer
  695. * @retry: whether the caller should retry in case of failure
  696. *
  697. * This function is called in case of a write failure and moves all good data
  698. * from the potentially bad physical eraseblock to a good physical eraseblock.
  699. * This function also writes the data which was not written due to the failure.
  700. * Returns 0 in case of success, and a negative error code in case of failure.
  701. * In case of failure, the %retry parameter is set to false if this is a fatal
  702. * error (retrying won't help), and true otherwise.
  703. */
  704. static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
  705. const void *buf, int offset, int len,
  706. struct ubi_vid_io_buf *vidb, bool *retry)
  707. {
  708. struct ubi_device *ubi = vol->ubi;
  709. struct ubi_vid_hdr *vid_hdr;
  710. int new_pnum, err, vol_id = vol->vol_id, data_size;
  711. uint32_t crc;
  712. *retry = false;
  713. new_pnum = ubi_wl_get_peb(ubi);
  714. if (new_pnum < 0) {
  715. err = new_pnum;
  716. goto out_put;
  717. }
  718. ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
  719. pnum, new_pnum);
  720. err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
  721. if (err && err != UBI_IO_BITFLIPS) {
  722. if (err > 0)
  723. err = -EIO;
  724. goto out_put;
  725. }
  726. vid_hdr = ubi_get_vid_hdr(vidb);
  727. ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC);
  728. mutex_lock(&ubi->buf_mutex);
  729. memset(ubi->peb_buf + offset, 0xFF, len);
  730. /* Read everything before the area where the write failure happened */
  731. if (offset > 0) {
  732. err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
  733. if (err && err != UBI_IO_BITFLIPS)
  734. goto out_unlock;
  735. }
  736. *retry = true;
  737. memcpy(ubi->peb_buf + offset, buf, len);
  738. data_size = offset + len;
  739. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  740. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  741. vid_hdr->copy_flag = 1;
  742. vid_hdr->data_size = cpu_to_be32(data_size);
  743. vid_hdr->data_crc = cpu_to_be32(crc);
  744. err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
  745. if (err)
  746. goto out_unlock;
  747. err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
  748. out_unlock:
  749. mutex_unlock(&ubi->buf_mutex);
  750. if (!err)
  751. vol->eba_tbl->entries[lnum].pnum = new_pnum;
  752. out_put:
  753. up_read(&ubi->fm_eba_sem);
  754. if (!err) {
  755. ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  756. ubi_msg(ubi, "data was successfully recovered");
  757. } else if (new_pnum >= 0) {
  758. /*
  759. * Bad luck? This physical eraseblock is bad too? Crud. Let's
  760. * try to get another one.
  761. */
  762. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  763. ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
  764. }
  765. return err;
  766. }
  767. /**
  768. * recover_peb - recover from write failure.
  769. * @ubi: UBI device description object
  770. * @pnum: the physical eraseblock to recover
  771. * @vol_id: volume ID
  772. * @lnum: logical eraseblock number
  773. * @buf: data which was not written because of the write failure
  774. * @offset: offset of the failed write
  775. * @len: how many bytes should have been written
  776. *
  777. * This function is called in case of a write failure and moves all good data
  778. * from the potentially bad physical eraseblock to a good physical eraseblock.
  779. * This function also writes the data which was not written due to the failure.
  780. * Returns 0 in case of success, and a negative error code in case of failure.
  781. * This function tries %UBI_IO_RETRIES before giving up.
  782. */
  783. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  784. const void *buf, int offset, int len)
  785. {
  786. int err, idx = vol_id2idx(ubi, vol_id), tries;
  787. struct ubi_volume *vol = ubi->volumes[idx];
  788. struct ubi_vid_io_buf *vidb;
  789. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  790. if (!vidb)
  791. return -ENOMEM;
  792. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  793. bool retry;
  794. err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
  795. &retry);
  796. if (!err || !retry)
  797. break;
  798. ubi_msg(ubi, "try again");
  799. }
  800. ubi_free_vid_buf(vidb);
  801. return err;
  802. }
  803. /**
  804. * try_write_vid_and_data - try to write VID header and data to a new PEB.
  805. * @vol: volume description object
  806. * @lnum: logical eraseblock number
  807. * @vidb: the VID buffer to write
  808. * @buf: buffer containing the data
  809. * @offset: where to start writing data
  810. * @len: how many bytes should be written
  811. *
  812. * This function tries to write VID header and data belonging to logical
  813. * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero
  814. * in case of success and a negative error code in case of failure.
  815. * In case of error, it is possible that something was still written to the
  816. * flash media, but may be some garbage.
  817. */
  818. static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
  819. struct ubi_vid_io_buf *vidb, const void *buf,
  820. int offset, int len)
  821. {
  822. struct ubi_device *ubi = vol->ubi;
  823. int pnum, opnum, err, vol_id = vol->vol_id;
  824. pnum = ubi_wl_get_peb(ubi);
  825. if (pnum < 0) {
  826. err = pnum;
  827. goto out_put;
  828. }
  829. opnum = vol->eba_tbl->entries[lnum].pnum;
  830. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  831. len, offset, vol_id, lnum, pnum);
  832. err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
  833. if (err) {
  834. ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
  835. vol_id, lnum, pnum);
  836. goto out_put;
  837. }
  838. if (len) {
  839. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  840. if (err) {
  841. ubi_warn(ubi,
  842. "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
  843. len, offset, vol_id, lnum, pnum);
  844. goto out_put;
  845. }
  846. }
  847. vol->eba_tbl->entries[lnum].pnum = pnum;
  848. out_put:
  849. up_read(&ubi->fm_eba_sem);
  850. if (err && pnum >= 0)
  851. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  852. else if (!err && opnum >= 0)
  853. err = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0);
  854. return err;
  855. }
  856. /**
  857. * ubi_eba_write_leb - write data to dynamic volume.
  858. * @ubi: UBI device description object
  859. * @vol: volume description object
  860. * @lnum: logical eraseblock number
  861. * @buf: the data to write
  862. * @offset: offset within the logical eraseblock where to write
  863. * @len: how many bytes to write
  864. *
  865. * This function writes data to logical eraseblock @lnum of a dynamic volume
  866. * @vol. Returns zero in case of success and a negative error code in case
  867. * of failure. In case of error, it is possible that something was still
  868. * written to the flash media, but may be some garbage.
  869. * This function retries %UBI_IO_RETRIES times before giving up.
  870. */
  871. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  872. const void *buf, int offset, int len)
  873. {
  874. int err, pnum, tries, vol_id = vol->vol_id;
  875. struct ubi_vid_io_buf *vidb;
  876. struct ubi_vid_hdr *vid_hdr;
  877. if (ubi->ro_mode)
  878. return -EROFS;
  879. err = leb_write_lock(ubi, vol_id, lnum);
  880. if (err)
  881. return err;
  882. pnum = vol->eba_tbl->entries[lnum].pnum;
  883. if (pnum >= 0) {
  884. err = check_mapping(ubi, vol, lnum, &pnum);
  885. if (err < 0)
  886. goto out;
  887. }
  888. if (pnum >= 0) {
  889. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  890. len, offset, vol_id, lnum, pnum);
  891. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  892. if (err) {
  893. ubi_warn(ubi, "failed to write data to PEB %d", pnum);
  894. if (err == -EIO && ubi->bad_allowed)
  895. err = recover_peb(ubi, pnum, vol_id, lnum, buf,
  896. offset, len);
  897. }
  898. goto out;
  899. }
  900. /*
  901. * The logical eraseblock is not mapped. We have to get a free physical
  902. * eraseblock and write the volume identifier header there first.
  903. */
  904. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  905. if (!vidb) {
  906. leb_write_unlock(ubi, vol_id, lnum);
  907. return -ENOMEM;
  908. }
  909. vid_hdr = ubi_get_vid_hdr(vidb);
  910. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  911. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  912. vid_hdr->vol_id = cpu_to_be32(vol_id);
  913. vid_hdr->lnum = cpu_to_be32(lnum);
  914. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  915. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  916. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  917. err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
  918. if (err != -EIO || !ubi->bad_allowed)
  919. break;
  920. /*
  921. * Fortunately, this is the first write operation to this
  922. * physical eraseblock, so just put it and request a new one.
  923. * We assume that if this physical eraseblock went bad, the
  924. * erase code will handle that.
  925. */
  926. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  927. ubi_msg(ubi, "try another PEB");
  928. }
  929. ubi_free_vid_buf(vidb);
  930. out:
  931. if (err)
  932. ubi_ro_mode(ubi);
  933. leb_write_unlock(ubi, vol_id, lnum);
  934. return err;
  935. }
  936. /**
  937. * ubi_eba_write_leb_st - write data to static volume.
  938. * @ubi: UBI device description object
  939. * @vol: volume description object
  940. * @lnum: logical eraseblock number
  941. * @buf: data to write
  942. * @len: how many bytes to write
  943. * @used_ebs: how many logical eraseblocks will this volume contain
  944. *
  945. * This function writes data to logical eraseblock @lnum of static volume
  946. * @vol. The @used_ebs argument should contain total number of logical
  947. * eraseblock in this static volume.
  948. *
  949. * When writing to the last logical eraseblock, the @len argument doesn't have
  950. * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
  951. * to the real data size, although the @buf buffer has to contain the
  952. * alignment. In all other cases, @len has to be aligned.
  953. *
  954. * It is prohibited to write more than once to logical eraseblocks of static
  955. * volumes. This function returns zero in case of success and a negative error
  956. * code in case of failure.
  957. */
  958. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  959. int lnum, const void *buf, int len, int used_ebs)
  960. {
  961. int err, tries, data_size = len, vol_id = vol->vol_id;
  962. struct ubi_vid_io_buf *vidb;
  963. struct ubi_vid_hdr *vid_hdr;
  964. uint32_t crc;
  965. if (ubi->ro_mode)
  966. return -EROFS;
  967. if (lnum == used_ebs - 1)
  968. /* If this is the last LEB @len may be unaligned */
  969. len = ALIGN(data_size, ubi->min_io_size);
  970. else
  971. ubi_assert(!(len & (ubi->min_io_size - 1)));
  972. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  973. if (!vidb)
  974. return -ENOMEM;
  975. vid_hdr = ubi_get_vid_hdr(vidb);
  976. err = leb_write_lock(ubi, vol_id, lnum);
  977. if (err)
  978. goto out;
  979. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  980. vid_hdr->vol_id = cpu_to_be32(vol_id);
  981. vid_hdr->lnum = cpu_to_be32(lnum);
  982. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  983. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  984. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  985. vid_hdr->vol_type = UBI_VID_STATIC;
  986. vid_hdr->data_size = cpu_to_be32(data_size);
  987. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  988. vid_hdr->data_crc = cpu_to_be32(crc);
  989. ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
  990. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  991. err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
  992. if (err != -EIO || !ubi->bad_allowed)
  993. break;
  994. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  995. ubi_msg(ubi, "try another PEB");
  996. }
  997. if (err)
  998. ubi_ro_mode(ubi);
  999. leb_write_unlock(ubi, vol_id, lnum);
  1000. out:
  1001. ubi_free_vid_buf(vidb);
  1002. return err;
  1003. }
  1004. /*
  1005. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  1006. * @ubi: UBI device description object
  1007. * @vol: volume description object
  1008. * @lnum: logical eraseblock number
  1009. * @buf: data to write
  1010. * @len: how many bytes to write
  1011. *
  1012. * This function changes the contents of a logical eraseblock atomically. @buf
  1013. * has to contain new logical eraseblock data, and @len - the length of the
  1014. * data, which has to be aligned. This function guarantees that in case of an
  1015. * unclean reboot the old contents is preserved. Returns zero in case of
  1016. * success and a negative error code in case of failure.
  1017. *
  1018. * UBI reserves one LEB for the "atomic LEB change" operation, so only one
  1019. * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  1020. */
  1021. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  1022. int lnum, const void *buf, int len)
  1023. {
  1024. int err, tries, vol_id = vol->vol_id;
  1025. struct ubi_vid_io_buf *vidb;
  1026. struct ubi_vid_hdr *vid_hdr;
  1027. uint32_t crc;
  1028. if (ubi->ro_mode)
  1029. return -EROFS;
  1030. if (len == 0) {
  1031. /*
  1032. * Special case when data length is zero. In this case the LEB
  1033. * has to be unmapped and mapped somewhere else.
  1034. */
  1035. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  1036. if (err)
  1037. return err;
  1038. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
  1039. }
  1040. vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
  1041. if (!vidb)
  1042. return -ENOMEM;
  1043. vid_hdr = ubi_get_vid_hdr(vidb);
  1044. mutex_lock(&ubi->alc_mutex);
  1045. err = leb_write_lock(ubi, vol_id, lnum);
  1046. if (err)
  1047. goto out_mutex;
  1048. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1049. vid_hdr->vol_id = cpu_to_be32(vol_id);
  1050. vid_hdr->lnum = cpu_to_be32(lnum);
  1051. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  1052. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  1053. crc = crc32(UBI_CRC32_INIT, buf, len);
  1054. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  1055. vid_hdr->data_size = cpu_to_be32(len);
  1056. vid_hdr->copy_flag = 1;
  1057. vid_hdr->data_crc = cpu_to_be32(crc);
  1058. dbg_eba("change LEB %d:%d", vol_id, lnum);
  1059. for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
  1060. err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
  1061. if (err != -EIO || !ubi->bad_allowed)
  1062. break;
  1063. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1064. ubi_msg(ubi, "try another PEB");
  1065. }
  1066. /*
  1067. * This flash device does not admit of bad eraseblocks or
  1068. * something nasty and unexpected happened. Switch to read-only
  1069. * mode just in case.
  1070. */
  1071. if (err)
  1072. ubi_ro_mode(ubi);
  1073. leb_write_unlock(ubi, vol_id, lnum);
  1074. out_mutex:
  1075. mutex_unlock(&ubi->alc_mutex);
  1076. ubi_free_vid_buf(vidb);
  1077. return err;
  1078. }
  1079. /**
  1080. * is_error_sane - check whether a read error is sane.
  1081. * @err: code of the error happened during reading
  1082. *
  1083. * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
  1084. * cannot read data from the target PEB (an error @err happened). If the error
  1085. * code is sane, then we treat this error as non-fatal. Otherwise the error is
  1086. * fatal and UBI will be switched to R/O mode later.
  1087. *
  1088. * The idea is that we try not to switch to R/O mode if the read error is
  1089. * something which suggests there was a real read problem. E.g., %-EIO. Or a
  1090. * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
  1091. * mode, simply because we do not know what happened at the MTD level, and we
  1092. * cannot handle this. E.g., the underlying driver may have become crazy, and
  1093. * it is safer to switch to R/O mode to preserve the data.
  1094. *
  1095. * And bear in mind, this is about reading from the target PEB, i.e. the PEB
  1096. * which we have just written.
  1097. */
  1098. static int is_error_sane(int err)
  1099. {
  1100. if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
  1101. err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
  1102. return 0;
  1103. return 1;
  1104. }
  1105. /**
  1106. * ubi_eba_copy_leb - copy logical eraseblock.
  1107. * @ubi: UBI device description object
  1108. * @from: physical eraseblock number from where to copy
  1109. * @to: physical eraseblock number where to copy
  1110. * @vid_hdr: VID header of the @from physical eraseblock
  1111. *
  1112. * This function copies logical eraseblock from physical eraseblock @from to
  1113. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  1114. * function. Returns:
  1115. * o %0 in case of success;
  1116. * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
  1117. * o a negative error code in case of failure.
  1118. */
  1119. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  1120. struct ubi_vid_io_buf *vidb)
  1121. {
  1122. int err, vol_id, lnum, data_size, aldata_size, idx;
  1123. struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
  1124. struct ubi_volume *vol;
  1125. uint32_t crc;
  1126. ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
  1127. vol_id = be32_to_cpu(vid_hdr->vol_id);
  1128. lnum = be32_to_cpu(vid_hdr->lnum);
  1129. dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  1130. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  1131. data_size = be32_to_cpu(vid_hdr->data_size);
  1132. aldata_size = ALIGN(data_size, ubi->min_io_size);
  1133. } else
  1134. data_size = aldata_size =
  1135. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  1136. idx = vol_id2idx(ubi, vol_id);
  1137. spin_lock(&ubi->volumes_lock);
  1138. /*
  1139. * Note, we may race with volume deletion, which means that the volume
  1140. * this logical eraseblock belongs to might be being deleted. Since the
  1141. * volume deletion un-maps all the volume's logical eraseblocks, it will
  1142. * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
  1143. */
  1144. vol = ubi->volumes[idx];
  1145. spin_unlock(&ubi->volumes_lock);
  1146. if (!vol) {
  1147. /* No need to do further work, cancel */
  1148. dbg_wl("volume %d is being removed, cancel", vol_id);
  1149. return MOVE_CANCEL_RACE;
  1150. }
  1151. /*
  1152. * We do not want anybody to write to this logical eraseblock while we
  1153. * are moving it, so lock it.
  1154. *
  1155. * Note, we are using non-waiting locking here, because we cannot sleep
  1156. * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
  1157. * unmapping the LEB which is mapped to the PEB we are going to move
  1158. * (@from). This task locks the LEB and goes sleep in the
  1159. * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
  1160. * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
  1161. * LEB is already locked, we just do not move it and return
  1162. * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
  1163. * we do not know the reasons of the contention - it may be just a
  1164. * normal I/O on this LEB, so we want to re-try.
  1165. */
  1166. err = leb_write_trylock(ubi, vol_id, lnum);
  1167. if (err) {
  1168. dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
  1169. return MOVE_RETRY;
  1170. }
  1171. /*
  1172. * The LEB might have been put meanwhile, and the task which put it is
  1173. * probably waiting on @ubi->move_mutex. No need to continue the work,
  1174. * cancel it.
  1175. */
  1176. if (vol->eba_tbl->entries[lnum].pnum != from) {
  1177. dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
  1178. vol_id, lnum, from, vol->eba_tbl->entries[lnum].pnum);
  1179. err = MOVE_CANCEL_RACE;
  1180. goto out_unlock_leb;
  1181. }
  1182. /*
  1183. * OK, now the LEB is locked and we can safely start moving it. Since
  1184. * this function utilizes the @ubi->peb_buf buffer which is shared
  1185. * with some other functions - we lock the buffer by taking the
  1186. * @ubi->buf_mutex.
  1187. */
  1188. mutex_lock(&ubi->buf_mutex);
  1189. dbg_wl("read %d bytes of data", aldata_size);
  1190. err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
  1191. if (err && err != UBI_IO_BITFLIPS) {
  1192. ubi_warn(ubi, "error %d while reading data from PEB %d",
  1193. err, from);
  1194. err = MOVE_SOURCE_RD_ERR;
  1195. goto out_unlock_buf;
  1196. }
  1197. /*
  1198. * Now we have got to calculate how much data we have to copy. In
  1199. * case of a static volume it is fairly easy - the VID header contains
  1200. * the data size. In case of a dynamic volume it is more difficult - we
  1201. * have to read the contents, cut 0xFF bytes from the end and copy only
  1202. * the first part. We must do this to avoid writing 0xFF bytes as it
  1203. * may have some side-effects. And not only this. It is important not
  1204. * to include those 0xFFs to CRC because later the they may be filled
  1205. * by data.
  1206. */
  1207. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  1208. aldata_size = data_size =
  1209. ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
  1210. cond_resched();
  1211. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  1212. cond_resched();
  1213. /*
  1214. * It may turn out to be that the whole @from physical eraseblock
  1215. * contains only 0xFF bytes. Then we have to only write the VID header
  1216. * and do not write any data. This also means we should not set
  1217. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  1218. */
  1219. if (data_size > 0) {
  1220. vid_hdr->copy_flag = 1;
  1221. vid_hdr->data_size = cpu_to_be32(data_size);
  1222. vid_hdr->data_crc = cpu_to_be32(crc);
  1223. }
  1224. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1225. err = ubi_io_write_vid_hdr(ubi, to, vidb);
  1226. if (err) {
  1227. if (err == -EIO)
  1228. err = MOVE_TARGET_WR_ERR;
  1229. goto out_unlock_buf;
  1230. }
  1231. cond_resched();
  1232. /* Read the VID header back and check if it was written correctly */
  1233. err = ubi_io_read_vid_hdr(ubi, to, vidb, 1);
  1234. if (err) {
  1235. if (err != UBI_IO_BITFLIPS) {
  1236. ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
  1237. err, to);
  1238. if (is_error_sane(err))
  1239. err = MOVE_TARGET_RD_ERR;
  1240. } else
  1241. err = MOVE_TARGET_BITFLIPS;
  1242. goto out_unlock_buf;
  1243. }
  1244. if (data_size > 0) {
  1245. err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  1246. if (err) {
  1247. if (err == -EIO)
  1248. err = MOVE_TARGET_WR_ERR;
  1249. goto out_unlock_buf;
  1250. }
  1251. cond_resched();
  1252. }
  1253. ubi_assert(vol->eba_tbl->entries[lnum].pnum == from);
  1254. vol->eba_tbl->entries[lnum].pnum = to;
  1255. out_unlock_buf:
  1256. mutex_unlock(&ubi->buf_mutex);
  1257. out_unlock_leb:
  1258. leb_write_unlock(ubi, vol_id, lnum);
  1259. return err;
  1260. }
  1261. /**
  1262. * print_rsvd_warning - warn about not having enough reserved PEBs.
  1263. * @ubi: UBI device description object
  1264. *
  1265. * This is a helper function for 'ubi_eba_init()' which is called when UBI
  1266. * cannot reserve enough PEBs for bad block handling. This function makes a
  1267. * decision whether we have to print a warning or not. The algorithm is as
  1268. * follows:
  1269. * o if this is a new UBI image, then just print the warning
  1270. * o if this is an UBI image which has already been used for some time, print
  1271. * a warning only if we can reserve less than 10% of the expected amount of
  1272. * the reserved PEB.
  1273. *
  1274. * The idea is that when UBI is used, PEBs become bad, and the reserved pool
  1275. * of PEBs becomes smaller, which is normal and we do not want to scare users
  1276. * with a warning every time they attach the MTD device. This was an issue
  1277. * reported by real users.
  1278. */
  1279. static void print_rsvd_warning(struct ubi_device *ubi,
  1280. struct ubi_attach_info *ai)
  1281. {
  1282. /*
  1283. * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
  1284. * large number to distinguish between newly flashed and used images.
  1285. */
  1286. if (ai->max_sqnum > (1 << 18)) {
  1287. int min = ubi->beb_rsvd_level / 10;
  1288. if (!min)
  1289. min = 1;
  1290. if (ubi->beb_rsvd_pebs > min)
  1291. return;
  1292. }
  1293. ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
  1294. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1295. if (ubi->corr_peb_count)
  1296. ubi_warn(ubi, "%d PEBs are corrupted and not used",
  1297. ubi->corr_peb_count);
  1298. }
  1299. /**
  1300. * self_check_eba - run a self check on the EBA table constructed by fastmap.
  1301. * @ubi: UBI device description object
  1302. * @ai_fastmap: UBI attach info object created by fastmap
  1303. * @ai_scan: UBI attach info object created by scanning
  1304. *
  1305. * Returns < 0 in case of an internal error, 0 otherwise.
  1306. * If a bad EBA table entry was found it will be printed out and
  1307. * ubi_assert() triggers.
  1308. */
  1309. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  1310. struct ubi_attach_info *ai_scan)
  1311. {
  1312. int i, j, num_volumes, ret = 0;
  1313. int **scan_eba, **fm_eba;
  1314. struct ubi_ainf_volume *av;
  1315. struct ubi_volume *vol;
  1316. struct ubi_ainf_peb *aeb;
  1317. struct rb_node *rb;
  1318. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1319. scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
  1320. if (!scan_eba)
  1321. return -ENOMEM;
  1322. fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
  1323. if (!fm_eba) {
  1324. kfree(scan_eba);
  1325. return -ENOMEM;
  1326. }
  1327. for (i = 0; i < num_volumes; i++) {
  1328. vol = ubi->volumes[i];
  1329. if (!vol)
  1330. continue;
  1331. scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
  1332. GFP_KERNEL);
  1333. if (!scan_eba[i]) {
  1334. ret = -ENOMEM;
  1335. goto out_free;
  1336. }
  1337. fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
  1338. GFP_KERNEL);
  1339. if (!fm_eba[i]) {
  1340. ret = -ENOMEM;
  1341. goto out_free;
  1342. }
  1343. for (j = 0; j < vol->reserved_pebs; j++)
  1344. scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
  1345. av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
  1346. if (!av)
  1347. continue;
  1348. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1349. scan_eba[i][aeb->lnum] = aeb->pnum;
  1350. av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
  1351. if (!av)
  1352. continue;
  1353. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1354. fm_eba[i][aeb->lnum] = aeb->pnum;
  1355. for (j = 0; j < vol->reserved_pebs; j++) {
  1356. if (scan_eba[i][j] != fm_eba[i][j]) {
  1357. if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
  1358. fm_eba[i][j] == UBI_LEB_UNMAPPED)
  1359. continue;
  1360. ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
  1361. vol->vol_id, j, fm_eba[i][j],
  1362. scan_eba[i][j]);
  1363. ubi_assert(0);
  1364. }
  1365. }
  1366. }
  1367. out_free:
  1368. for (i = 0; i < num_volumes; i++) {
  1369. if (!ubi->volumes[i])
  1370. continue;
  1371. kfree(scan_eba[i]);
  1372. kfree(fm_eba[i]);
  1373. }
  1374. kfree(scan_eba);
  1375. kfree(fm_eba);
  1376. return ret;
  1377. }
  1378. /**
  1379. * ubi_eba_init - initialize the EBA sub-system using attaching information.
  1380. * @ubi: UBI device description object
  1381. * @ai: attaching information
  1382. *
  1383. * This function returns zero in case of success and a negative error code in
  1384. * case of failure.
  1385. */
  1386. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1387. {
  1388. int i, err, num_volumes;
  1389. struct ubi_ainf_volume *av;
  1390. struct ubi_volume *vol;
  1391. struct ubi_ainf_peb *aeb;
  1392. struct rb_node *rb;
  1393. dbg_eba("initialize EBA sub-system");
  1394. spin_lock_init(&ubi->ltree_lock);
  1395. mutex_init(&ubi->alc_mutex);
  1396. ubi->ltree = RB_ROOT;
  1397. ubi->global_sqnum = ai->max_sqnum + 1;
  1398. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1399. for (i = 0; i < num_volumes; i++) {
  1400. struct ubi_eba_table *tbl;
  1401. vol = ubi->volumes[i];
  1402. if (!vol)
  1403. continue;
  1404. cond_resched();
  1405. tbl = ubi_eba_create_table(vol, vol->reserved_pebs);
  1406. if (IS_ERR(tbl)) {
  1407. err = PTR_ERR(tbl);
  1408. goto out_free;
  1409. }
  1410. ubi_eba_replace_table(vol, tbl);
  1411. av = ubi_find_av(ai, idx2vol_id(ubi, i));
  1412. if (!av)
  1413. continue;
  1414. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  1415. if (aeb->lnum >= vol->reserved_pebs) {
  1416. /*
  1417. * This may happen in case of an unclean reboot
  1418. * during re-size.
  1419. */
  1420. ubi_move_aeb_to_list(av, aeb, &ai->erase);
  1421. } else {
  1422. struct ubi_eba_entry *entry;
  1423. entry = &vol->eba_tbl->entries[aeb->lnum];
  1424. entry->pnum = aeb->pnum;
  1425. }
  1426. }
  1427. }
  1428. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1429. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1430. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1431. if (ubi->corr_peb_count)
  1432. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1433. ubi->corr_peb_count);
  1434. err = -ENOSPC;
  1435. goto out_free;
  1436. }
  1437. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1438. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1439. if (ubi->bad_allowed) {
  1440. ubi_calculate_reserved(ubi);
  1441. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1442. /* No enough free physical eraseblocks */
  1443. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1444. print_rsvd_warning(ubi, ai);
  1445. } else
  1446. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1447. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1448. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1449. }
  1450. dbg_eba("EBA sub-system is initialized");
  1451. return 0;
  1452. out_free:
  1453. for (i = 0; i < num_volumes; i++) {
  1454. if (!ubi->volumes[i])
  1455. continue;
  1456. ubi_eba_replace_table(ubi->volumes[i], NULL);
  1457. }
  1458. return err;
  1459. }