eba.c 43 KB

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