eba.c 40 KB

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