eba.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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. /**
  312. * ubi_eba_read_leb - read data.
  313. * @ubi: UBI device description object
  314. * @vol: volume description object
  315. * @lnum: logical eraseblock number
  316. * @buf: buffer to store the read data
  317. * @offset: offset from where to read
  318. * @len: how many bytes to read
  319. * @check: data CRC check flag
  320. *
  321. * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
  322. * bytes. The @check flag only makes sense for static volumes and forces
  323. * eraseblock data CRC checking.
  324. *
  325. * In case of success this function returns zero. In case of a static volume,
  326. * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
  327. * returned for any volume type if an ECC error was detected by the MTD device
  328. * driver. Other negative error cored may be returned in case of other errors.
  329. */
  330. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  331. void *buf, int offset, int len, int check)
  332. {
  333. int err, pnum, scrub = 0, vol_id = vol->vol_id;
  334. struct ubi_vid_hdr *vid_hdr;
  335. uint32_t uninitialized_var(crc);
  336. err = leb_read_lock(ubi, vol_id, lnum);
  337. if (err)
  338. return err;
  339. pnum = vol->eba_tbl[lnum];
  340. if (pnum < 0) {
  341. /*
  342. * The logical eraseblock is not mapped, fill the whole buffer
  343. * with 0xFF bytes. The exception is static volumes for which
  344. * it is an error to read unmapped logical eraseblocks.
  345. */
  346. dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
  347. len, offset, vol_id, lnum);
  348. leb_read_unlock(ubi, vol_id, lnum);
  349. ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
  350. memset(buf, 0xFF, len);
  351. return 0;
  352. }
  353. dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
  354. len, offset, vol_id, lnum, pnum);
  355. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  356. check = 0;
  357. retry:
  358. if (check) {
  359. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  360. if (!vid_hdr) {
  361. err = -ENOMEM;
  362. goto out_unlock;
  363. }
  364. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  365. if (err && err != UBI_IO_BITFLIPS) {
  366. if (err > 0) {
  367. /*
  368. * The header is either absent or corrupted.
  369. * The former case means there is a bug -
  370. * switch to read-only mode just in case.
  371. * The latter case means a real corruption - we
  372. * may try to recover data. FIXME: but this is
  373. * not implemented.
  374. */
  375. if (err == UBI_IO_BAD_HDR_EBADMSG ||
  376. err == UBI_IO_BAD_HDR) {
  377. ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
  378. pnum, vol_id, lnum);
  379. err = -EBADMSG;
  380. } else {
  381. err = -EINVAL;
  382. ubi_ro_mode(ubi);
  383. }
  384. }
  385. goto out_free;
  386. } else if (err == UBI_IO_BITFLIPS)
  387. scrub = 1;
  388. ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
  389. ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
  390. crc = be32_to_cpu(vid_hdr->data_crc);
  391. ubi_free_vid_hdr(ubi, vid_hdr);
  392. }
  393. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  394. if (err) {
  395. if (err == UBI_IO_BITFLIPS)
  396. scrub = 1;
  397. else if (mtd_is_eccerr(err)) {
  398. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  399. goto out_unlock;
  400. scrub = 1;
  401. if (!check) {
  402. ubi_msg(ubi, "force data checking");
  403. check = 1;
  404. goto retry;
  405. }
  406. } else
  407. goto out_unlock;
  408. }
  409. if (check) {
  410. uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
  411. if (crc1 != crc) {
  412. ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
  413. crc1, crc);
  414. err = -EBADMSG;
  415. goto out_unlock;
  416. }
  417. }
  418. if (scrub)
  419. err = ubi_wl_scrub_peb(ubi, pnum);
  420. leb_read_unlock(ubi, vol_id, lnum);
  421. return err;
  422. out_free:
  423. ubi_free_vid_hdr(ubi, vid_hdr);
  424. out_unlock:
  425. leb_read_unlock(ubi, vol_id, lnum);
  426. return err;
  427. }
  428. /**
  429. * ubi_eba_read_leb_sg - read data into a scatter gather list.
  430. * @ubi: UBI device description object
  431. * @vol: volume description object
  432. * @lnum: logical eraseblock number
  433. * @sgl: UBI scatter gather list to store the read data
  434. * @offset: offset from where to read
  435. * @len: how many bytes to read
  436. * @check: data CRC check flag
  437. *
  438. * This function works exactly like ubi_eba_read_leb(). But instead of
  439. * storing the read data into a buffer it writes to an UBI scatter gather
  440. * list.
  441. */
  442. int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
  443. struct ubi_sgl *sgl, int lnum, int offset, int len,
  444. int check)
  445. {
  446. int to_read;
  447. int ret;
  448. struct scatterlist *sg;
  449. for (;;) {
  450. ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
  451. sg = &sgl->sg[sgl->list_pos];
  452. if (len < sg->length - sgl->page_pos)
  453. to_read = len;
  454. else
  455. to_read = sg->length - sgl->page_pos;
  456. ret = ubi_eba_read_leb(ubi, vol, lnum,
  457. sg_virt(sg) + sgl->page_pos, offset,
  458. to_read, check);
  459. if (ret < 0)
  460. return ret;
  461. offset += to_read;
  462. len -= to_read;
  463. if (!len) {
  464. sgl->page_pos += to_read;
  465. if (sgl->page_pos == sg->length) {
  466. sgl->list_pos++;
  467. sgl->page_pos = 0;
  468. }
  469. break;
  470. }
  471. sgl->list_pos++;
  472. sgl->page_pos = 0;
  473. }
  474. return ret;
  475. }
  476. /**
  477. * recover_peb - recover from write failure.
  478. * @ubi: UBI device description object
  479. * @pnum: the physical eraseblock to recover
  480. * @vol_id: volume ID
  481. * @lnum: logical eraseblock number
  482. * @buf: data which was not written because of the write failure
  483. * @offset: offset of the failed write
  484. * @len: how many bytes should have been written
  485. *
  486. * This function is called in case of a write failure and moves all good data
  487. * from the potentially bad physical eraseblock to a good physical eraseblock.
  488. * This function also writes the data which was not written due to the failure.
  489. * Returns new physical eraseblock number in case of success, and a negative
  490. * error code in case of failure.
  491. */
  492. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  493. const void *buf, int offset, int len)
  494. {
  495. int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
  496. struct ubi_volume *vol = ubi->volumes[idx];
  497. struct ubi_vid_hdr *vid_hdr;
  498. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  499. if (!vid_hdr)
  500. return -ENOMEM;
  501. retry:
  502. new_pnum = ubi_wl_get_peb(ubi);
  503. if (new_pnum < 0) {
  504. ubi_free_vid_hdr(ubi, vid_hdr);
  505. up_read(&ubi->fm_eba_sem);
  506. return new_pnum;
  507. }
  508. ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
  509. pnum, new_pnum);
  510. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  511. if (err && err != UBI_IO_BITFLIPS) {
  512. if (err > 0)
  513. err = -EIO;
  514. up_read(&ubi->fm_eba_sem);
  515. goto out_put;
  516. }
  517. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  518. err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
  519. if (err) {
  520. up_read(&ubi->fm_eba_sem);
  521. goto write_error;
  522. }
  523. data_size = offset + len;
  524. mutex_lock(&ubi->buf_mutex);
  525. memset(ubi->peb_buf + offset, 0xFF, len);
  526. /* Read everything before the area where the write failure happened */
  527. if (offset > 0) {
  528. err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
  529. if (err && err != UBI_IO_BITFLIPS) {
  530. up_read(&ubi->fm_eba_sem);
  531. goto out_unlock;
  532. }
  533. }
  534. memcpy(ubi->peb_buf + offset, buf, len);
  535. err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
  536. if (err) {
  537. mutex_unlock(&ubi->buf_mutex);
  538. up_read(&ubi->fm_eba_sem);
  539. goto write_error;
  540. }
  541. mutex_unlock(&ubi->buf_mutex);
  542. ubi_free_vid_hdr(ubi, vid_hdr);
  543. vol->eba_tbl[lnum] = new_pnum;
  544. up_read(&ubi->fm_eba_sem);
  545. ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  546. ubi_msg(ubi, "data was successfully recovered");
  547. return 0;
  548. out_unlock:
  549. mutex_unlock(&ubi->buf_mutex);
  550. out_put:
  551. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  552. ubi_free_vid_hdr(ubi, vid_hdr);
  553. return err;
  554. write_error:
  555. /*
  556. * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
  557. * get another one.
  558. */
  559. ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
  560. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  561. if (++tries > UBI_IO_RETRIES) {
  562. ubi_free_vid_hdr(ubi, vid_hdr);
  563. return err;
  564. }
  565. ubi_msg(ubi, "try again");
  566. goto retry;
  567. }
  568. /**
  569. * ubi_eba_write_leb - write data to dynamic volume.
  570. * @ubi: UBI device description object
  571. * @vol: volume description object
  572. * @lnum: logical eraseblock number
  573. * @buf: the data to write
  574. * @offset: offset within the logical eraseblock where to write
  575. * @len: how many bytes to write
  576. *
  577. * This function writes data to logical eraseblock @lnum of a dynamic volume
  578. * @vol. Returns zero in case of success and a negative error code in case
  579. * of failure. In case of error, it is possible that something was still
  580. * written to the flash media, but may be some garbage.
  581. */
  582. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  583. const void *buf, int offset, int len)
  584. {
  585. int err, pnum, tries = 0, vol_id = vol->vol_id;
  586. struct ubi_vid_hdr *vid_hdr;
  587. if (ubi->ro_mode)
  588. return -EROFS;
  589. err = leb_write_lock(ubi, vol_id, lnum);
  590. if (err)
  591. return err;
  592. pnum = vol->eba_tbl[lnum];
  593. if (pnum >= 0) {
  594. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  595. len, offset, vol_id, lnum, pnum);
  596. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  597. if (err) {
  598. ubi_warn(ubi, "failed to write data to PEB %d", pnum);
  599. if (err == -EIO && ubi->bad_allowed)
  600. err = recover_peb(ubi, pnum, vol_id, lnum, buf,
  601. offset, len);
  602. if (err)
  603. ubi_ro_mode(ubi);
  604. }
  605. leb_write_unlock(ubi, vol_id, lnum);
  606. return err;
  607. }
  608. /*
  609. * The logical eraseblock is not mapped. We have to get a free physical
  610. * eraseblock and write the volume identifier header there first.
  611. */
  612. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  613. if (!vid_hdr) {
  614. leb_write_unlock(ubi, vol_id, lnum);
  615. return -ENOMEM;
  616. }
  617. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  618. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  619. vid_hdr->vol_id = cpu_to_be32(vol_id);
  620. vid_hdr->lnum = cpu_to_be32(lnum);
  621. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  622. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  623. retry:
  624. pnum = ubi_wl_get_peb(ubi);
  625. if (pnum < 0) {
  626. ubi_free_vid_hdr(ubi, vid_hdr);
  627. leb_write_unlock(ubi, vol_id, lnum);
  628. up_read(&ubi->fm_eba_sem);
  629. return pnum;
  630. }
  631. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  632. len, offset, vol_id, lnum, pnum);
  633. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  634. if (err) {
  635. ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
  636. vol_id, lnum, pnum);
  637. up_read(&ubi->fm_eba_sem);
  638. goto write_error;
  639. }
  640. if (len) {
  641. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  642. if (err) {
  643. ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
  644. len, offset, vol_id, lnum, pnum);
  645. up_read(&ubi->fm_eba_sem);
  646. goto write_error;
  647. }
  648. }
  649. vol->eba_tbl[lnum] = pnum;
  650. up_read(&ubi->fm_eba_sem);
  651. leb_write_unlock(ubi, vol_id, lnum);
  652. ubi_free_vid_hdr(ubi, vid_hdr);
  653. return 0;
  654. write_error:
  655. if (err != -EIO || !ubi->bad_allowed) {
  656. ubi_ro_mode(ubi);
  657. leb_write_unlock(ubi, vol_id, lnum);
  658. ubi_free_vid_hdr(ubi, vid_hdr);
  659. return err;
  660. }
  661. /*
  662. * Fortunately, this is the first write operation to this physical
  663. * eraseblock, so just put it and request a new one. We assume that if
  664. * this physical eraseblock went bad, the erase code will handle that.
  665. */
  666. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  667. if (err || ++tries > UBI_IO_RETRIES) {
  668. ubi_ro_mode(ubi);
  669. leb_write_unlock(ubi, vol_id, lnum);
  670. ubi_free_vid_hdr(ubi, vid_hdr);
  671. return err;
  672. }
  673. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  674. ubi_msg(ubi, "try another PEB");
  675. goto retry;
  676. }
  677. /**
  678. * ubi_eba_write_leb_st - write data to static volume.
  679. * @ubi: UBI device description object
  680. * @vol: volume description object
  681. * @lnum: logical eraseblock number
  682. * @buf: data to write
  683. * @len: how many bytes to write
  684. * @used_ebs: how many logical eraseblocks will this volume contain
  685. *
  686. * This function writes data to logical eraseblock @lnum of static volume
  687. * @vol. The @used_ebs argument should contain total number of logical
  688. * eraseblock in this static volume.
  689. *
  690. * When writing to the last logical eraseblock, the @len argument doesn't have
  691. * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
  692. * to the real data size, although the @buf buffer has to contain the
  693. * alignment. In all other cases, @len has to be aligned.
  694. *
  695. * It is prohibited to write more than once to logical eraseblocks of static
  696. * volumes. This function returns zero in case of success and a negative error
  697. * code in case of failure.
  698. */
  699. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  700. int lnum, const void *buf, int len, int used_ebs)
  701. {
  702. int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
  703. struct ubi_vid_hdr *vid_hdr;
  704. uint32_t crc;
  705. if (ubi->ro_mode)
  706. return -EROFS;
  707. if (lnum == used_ebs - 1)
  708. /* If this is the last LEB @len may be unaligned */
  709. len = ALIGN(data_size, ubi->min_io_size);
  710. else
  711. ubi_assert(!(len & (ubi->min_io_size - 1)));
  712. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  713. if (!vid_hdr)
  714. return -ENOMEM;
  715. err = leb_write_lock(ubi, vol_id, lnum);
  716. if (err) {
  717. ubi_free_vid_hdr(ubi, vid_hdr);
  718. return err;
  719. }
  720. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  721. vid_hdr->vol_id = cpu_to_be32(vol_id);
  722. vid_hdr->lnum = cpu_to_be32(lnum);
  723. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  724. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  725. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  726. vid_hdr->vol_type = UBI_VID_STATIC;
  727. vid_hdr->data_size = cpu_to_be32(data_size);
  728. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  729. vid_hdr->data_crc = cpu_to_be32(crc);
  730. retry:
  731. pnum = ubi_wl_get_peb(ubi);
  732. if (pnum < 0) {
  733. ubi_free_vid_hdr(ubi, vid_hdr);
  734. leb_write_unlock(ubi, vol_id, lnum);
  735. up_read(&ubi->fm_eba_sem);
  736. return pnum;
  737. }
  738. dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
  739. len, vol_id, lnum, pnum, used_ebs);
  740. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  741. if (err) {
  742. ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
  743. vol_id, lnum, pnum);
  744. up_read(&ubi->fm_eba_sem);
  745. goto write_error;
  746. }
  747. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  748. if (err) {
  749. ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
  750. len, pnum);
  751. up_read(&ubi->fm_eba_sem);
  752. goto write_error;
  753. }
  754. ubi_assert(vol->eba_tbl[lnum] < 0);
  755. vol->eba_tbl[lnum] = pnum;
  756. up_read(&ubi->fm_eba_sem);
  757. leb_write_unlock(ubi, vol_id, lnum);
  758. ubi_free_vid_hdr(ubi, vid_hdr);
  759. return 0;
  760. write_error:
  761. if (err != -EIO || !ubi->bad_allowed) {
  762. /*
  763. * This flash device does not admit of bad eraseblocks or
  764. * something nasty and unexpected happened. Switch to read-only
  765. * mode just in case.
  766. */
  767. ubi_ro_mode(ubi);
  768. leb_write_unlock(ubi, vol_id, lnum);
  769. ubi_free_vid_hdr(ubi, vid_hdr);
  770. return err;
  771. }
  772. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  773. if (err || ++tries > UBI_IO_RETRIES) {
  774. ubi_ro_mode(ubi);
  775. leb_write_unlock(ubi, vol_id, lnum);
  776. ubi_free_vid_hdr(ubi, vid_hdr);
  777. return err;
  778. }
  779. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  780. ubi_msg(ubi, "try another PEB");
  781. goto retry;
  782. }
  783. /*
  784. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  785. * @ubi: UBI device description object
  786. * @vol: volume description object
  787. * @lnum: logical eraseblock number
  788. * @buf: data to write
  789. * @len: how many bytes to write
  790. *
  791. * This function changes the contents of a logical eraseblock atomically. @buf
  792. * has to contain new logical eraseblock data, and @len - the length of the
  793. * data, which has to be aligned. This function guarantees that in case of an
  794. * unclean reboot the old contents is preserved. Returns zero in case of
  795. * success and a negative error code in case of failure.
  796. *
  797. * UBI reserves one LEB for the "atomic LEB change" operation, so only one
  798. * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  799. */
  800. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  801. int lnum, const void *buf, int len)
  802. {
  803. int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
  804. struct ubi_vid_hdr *vid_hdr;
  805. uint32_t crc;
  806. if (ubi->ro_mode)
  807. return -EROFS;
  808. if (len == 0) {
  809. /*
  810. * Special case when data length is zero. In this case the LEB
  811. * has to be unmapped and mapped somewhere else.
  812. */
  813. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  814. if (err)
  815. return err;
  816. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
  817. }
  818. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  819. if (!vid_hdr)
  820. return -ENOMEM;
  821. mutex_lock(&ubi->alc_mutex);
  822. err = leb_write_lock(ubi, vol_id, lnum);
  823. if (err)
  824. goto out_mutex;
  825. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  826. vid_hdr->vol_id = cpu_to_be32(vol_id);
  827. vid_hdr->lnum = cpu_to_be32(lnum);
  828. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  829. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  830. crc = crc32(UBI_CRC32_INIT, buf, len);
  831. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  832. vid_hdr->data_size = cpu_to_be32(len);
  833. vid_hdr->copy_flag = 1;
  834. vid_hdr->data_crc = cpu_to_be32(crc);
  835. retry:
  836. pnum = ubi_wl_get_peb(ubi);
  837. if (pnum < 0) {
  838. err = pnum;
  839. up_read(&ubi->fm_eba_sem);
  840. goto out_leb_unlock;
  841. }
  842. dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
  843. vol_id, lnum, vol->eba_tbl[lnum], pnum);
  844. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  845. if (err) {
  846. ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
  847. vol_id, lnum, pnum);
  848. up_read(&ubi->fm_eba_sem);
  849. goto write_error;
  850. }
  851. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  852. if (err) {
  853. ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
  854. len, pnum);
  855. up_read(&ubi->fm_eba_sem);
  856. goto write_error;
  857. }
  858. old_pnum = vol->eba_tbl[lnum];
  859. vol->eba_tbl[lnum] = pnum;
  860. up_read(&ubi->fm_eba_sem);
  861. if (old_pnum >= 0) {
  862. err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
  863. if (err)
  864. goto out_leb_unlock;
  865. }
  866. out_leb_unlock:
  867. leb_write_unlock(ubi, vol_id, lnum);
  868. out_mutex:
  869. mutex_unlock(&ubi->alc_mutex);
  870. ubi_free_vid_hdr(ubi, vid_hdr);
  871. return err;
  872. write_error:
  873. if (err != -EIO || !ubi->bad_allowed) {
  874. /*
  875. * This flash device does not admit of bad eraseblocks or
  876. * something nasty and unexpected happened. Switch to read-only
  877. * mode just in case.
  878. */
  879. ubi_ro_mode(ubi);
  880. goto out_leb_unlock;
  881. }
  882. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  883. if (err || ++tries > UBI_IO_RETRIES) {
  884. ubi_ro_mode(ubi);
  885. goto out_leb_unlock;
  886. }
  887. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  888. ubi_msg(ubi, "try another PEB");
  889. goto retry;
  890. }
  891. /**
  892. * is_error_sane - check whether a read error is sane.
  893. * @err: code of the error happened during reading
  894. *
  895. * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
  896. * cannot read data from the target PEB (an error @err happened). If the error
  897. * code is sane, then we treat this error as non-fatal. Otherwise the error is
  898. * fatal and UBI will be switched to R/O mode later.
  899. *
  900. * The idea is that we try not to switch to R/O mode if the read error is
  901. * something which suggests there was a real read problem. E.g., %-EIO. Or a
  902. * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
  903. * mode, simply because we do not know what happened at the MTD level, and we
  904. * cannot handle this. E.g., the underlying driver may have become crazy, and
  905. * it is safer to switch to R/O mode to preserve the data.
  906. *
  907. * And bear in mind, this is about reading from the target PEB, i.e. the PEB
  908. * which we have just written.
  909. */
  910. static int is_error_sane(int err)
  911. {
  912. if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
  913. err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
  914. return 0;
  915. return 1;
  916. }
  917. /**
  918. * ubi_eba_copy_leb - copy logical eraseblock.
  919. * @ubi: UBI device description object
  920. * @from: physical eraseblock number from where to copy
  921. * @to: physical eraseblock number where to copy
  922. * @vid_hdr: VID header of the @from physical eraseblock
  923. *
  924. * This function copies logical eraseblock from physical eraseblock @from to
  925. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  926. * function. Returns:
  927. * o %0 in case of success;
  928. * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
  929. * o a negative error code in case of failure.
  930. */
  931. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  932. struct ubi_vid_hdr *vid_hdr)
  933. {
  934. int err, vol_id, lnum, data_size, aldata_size, idx;
  935. struct ubi_volume *vol;
  936. uint32_t crc;
  937. vol_id = be32_to_cpu(vid_hdr->vol_id);
  938. lnum = be32_to_cpu(vid_hdr->lnum);
  939. dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  940. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  941. data_size = be32_to_cpu(vid_hdr->data_size);
  942. aldata_size = ALIGN(data_size, ubi->min_io_size);
  943. } else
  944. data_size = aldata_size =
  945. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  946. idx = vol_id2idx(ubi, vol_id);
  947. spin_lock(&ubi->volumes_lock);
  948. /*
  949. * Note, we may race with volume deletion, which means that the volume
  950. * this logical eraseblock belongs to might be being deleted. Since the
  951. * volume deletion un-maps all the volume's logical eraseblocks, it will
  952. * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
  953. */
  954. vol = ubi->volumes[idx];
  955. spin_unlock(&ubi->volumes_lock);
  956. if (!vol) {
  957. /* No need to do further work, cancel */
  958. dbg_wl("volume %d is being removed, cancel", vol_id);
  959. return MOVE_CANCEL_RACE;
  960. }
  961. /*
  962. * We do not want anybody to write to this logical eraseblock while we
  963. * are moving it, so lock it.
  964. *
  965. * Note, we are using non-waiting locking here, because we cannot sleep
  966. * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
  967. * unmapping the LEB which is mapped to the PEB we are going to move
  968. * (@from). This task locks the LEB and goes sleep in the
  969. * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
  970. * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
  971. * LEB is already locked, we just do not move it and return
  972. * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
  973. * we do not know the reasons of the contention - it may be just a
  974. * normal I/O on this LEB, so we want to re-try.
  975. */
  976. err = leb_write_trylock(ubi, vol_id, lnum);
  977. if (err) {
  978. dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
  979. return MOVE_RETRY;
  980. }
  981. /*
  982. * The LEB might have been put meanwhile, and the task which put it is
  983. * probably waiting on @ubi->move_mutex. No need to continue the work,
  984. * cancel it.
  985. */
  986. if (vol->eba_tbl[lnum] != from) {
  987. dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
  988. vol_id, lnum, from, vol->eba_tbl[lnum]);
  989. err = MOVE_CANCEL_RACE;
  990. goto out_unlock_leb;
  991. }
  992. /*
  993. * OK, now the LEB is locked and we can safely start moving it. Since
  994. * this function utilizes the @ubi->peb_buf buffer which is shared
  995. * with some other functions - we lock the buffer by taking the
  996. * @ubi->buf_mutex.
  997. */
  998. mutex_lock(&ubi->buf_mutex);
  999. dbg_wl("read %d bytes of data", aldata_size);
  1000. err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
  1001. if (err && err != UBI_IO_BITFLIPS) {
  1002. ubi_warn(ubi, "error %d while reading data from PEB %d",
  1003. err, from);
  1004. err = MOVE_SOURCE_RD_ERR;
  1005. goto out_unlock_buf;
  1006. }
  1007. /*
  1008. * Now we have got to calculate how much data we have to copy. In
  1009. * case of a static volume it is fairly easy - the VID header contains
  1010. * the data size. In case of a dynamic volume it is more difficult - we
  1011. * have to read the contents, cut 0xFF bytes from the end and copy only
  1012. * the first part. We must do this to avoid writing 0xFF bytes as it
  1013. * may have some side-effects. And not only this. It is important not
  1014. * to include those 0xFFs to CRC because later the they may be filled
  1015. * by data.
  1016. */
  1017. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  1018. aldata_size = data_size =
  1019. ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
  1020. cond_resched();
  1021. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  1022. cond_resched();
  1023. /*
  1024. * It may turn out to be that the whole @from physical eraseblock
  1025. * contains only 0xFF bytes. Then we have to only write the VID header
  1026. * and do not write any data. This also means we should not set
  1027. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  1028. */
  1029. if (data_size > 0) {
  1030. vid_hdr->copy_flag = 1;
  1031. vid_hdr->data_size = cpu_to_be32(data_size);
  1032. vid_hdr->data_crc = cpu_to_be32(crc);
  1033. }
  1034. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  1035. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  1036. if (err) {
  1037. if (err == -EIO)
  1038. err = MOVE_TARGET_WR_ERR;
  1039. goto out_unlock_buf;
  1040. }
  1041. cond_resched();
  1042. /* Read the VID header back and check if it was written correctly */
  1043. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  1044. if (err) {
  1045. if (err != UBI_IO_BITFLIPS) {
  1046. ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
  1047. err, to);
  1048. if (is_error_sane(err))
  1049. err = MOVE_TARGET_RD_ERR;
  1050. } else
  1051. err = MOVE_TARGET_BITFLIPS;
  1052. goto out_unlock_buf;
  1053. }
  1054. if (data_size > 0) {
  1055. err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  1056. if (err) {
  1057. if (err == -EIO)
  1058. err = MOVE_TARGET_WR_ERR;
  1059. goto out_unlock_buf;
  1060. }
  1061. cond_resched();
  1062. /*
  1063. * We've written the data and are going to read it back to make
  1064. * sure it was written correctly.
  1065. */
  1066. memset(ubi->peb_buf, 0xFF, aldata_size);
  1067. err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  1068. if (err) {
  1069. if (err != UBI_IO_BITFLIPS) {
  1070. ubi_warn(ubi, "error %d while reading data back from PEB %d",
  1071. err, to);
  1072. if (is_error_sane(err))
  1073. err = MOVE_TARGET_RD_ERR;
  1074. } else
  1075. err = MOVE_TARGET_BITFLIPS;
  1076. goto out_unlock_buf;
  1077. }
  1078. cond_resched();
  1079. if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
  1080. ubi_warn(ubi, "read data back from PEB %d and it is different",
  1081. to);
  1082. err = -EINVAL;
  1083. goto out_unlock_buf;
  1084. }
  1085. }
  1086. ubi_assert(vol->eba_tbl[lnum] == from);
  1087. down_read(&ubi->fm_eba_sem);
  1088. vol->eba_tbl[lnum] = to;
  1089. up_read(&ubi->fm_eba_sem);
  1090. out_unlock_buf:
  1091. mutex_unlock(&ubi->buf_mutex);
  1092. out_unlock_leb:
  1093. leb_write_unlock(ubi, vol_id, lnum);
  1094. return err;
  1095. }
  1096. /**
  1097. * print_rsvd_warning - warn about not having enough reserved PEBs.
  1098. * @ubi: UBI device description object
  1099. *
  1100. * This is a helper function for 'ubi_eba_init()' which is called when UBI
  1101. * cannot reserve enough PEBs for bad block handling. This function makes a
  1102. * decision whether we have to print a warning or not. The algorithm is as
  1103. * follows:
  1104. * o if this is a new UBI image, then just print the warning
  1105. * o if this is an UBI image which has already been used for some time, print
  1106. * a warning only if we can reserve less than 10% of the expected amount of
  1107. * the reserved PEB.
  1108. *
  1109. * The idea is that when UBI is used, PEBs become bad, and the reserved pool
  1110. * of PEBs becomes smaller, which is normal and we do not want to scare users
  1111. * with a warning every time they attach the MTD device. This was an issue
  1112. * reported by real users.
  1113. */
  1114. static void print_rsvd_warning(struct ubi_device *ubi,
  1115. struct ubi_attach_info *ai)
  1116. {
  1117. /*
  1118. * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
  1119. * large number to distinguish between newly flashed and used images.
  1120. */
  1121. if (ai->max_sqnum > (1 << 18)) {
  1122. int min = ubi->beb_rsvd_level / 10;
  1123. if (!min)
  1124. min = 1;
  1125. if (ubi->beb_rsvd_pebs > min)
  1126. return;
  1127. }
  1128. ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
  1129. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1130. if (ubi->corr_peb_count)
  1131. ubi_warn(ubi, "%d PEBs are corrupted and not used",
  1132. ubi->corr_peb_count);
  1133. }
  1134. /**
  1135. * self_check_eba - run a self check on the EBA table constructed by fastmap.
  1136. * @ubi: UBI device description object
  1137. * @ai_fastmap: UBI attach info object created by fastmap
  1138. * @ai_scan: UBI attach info object created by scanning
  1139. *
  1140. * Returns < 0 in case of an internal error, 0 otherwise.
  1141. * If a bad EBA table entry was found it will be printed out and
  1142. * ubi_assert() triggers.
  1143. */
  1144. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  1145. struct ubi_attach_info *ai_scan)
  1146. {
  1147. int i, j, num_volumes, ret = 0;
  1148. int **scan_eba, **fm_eba;
  1149. struct ubi_ainf_volume *av;
  1150. struct ubi_volume *vol;
  1151. struct ubi_ainf_peb *aeb;
  1152. struct rb_node *rb;
  1153. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1154. scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
  1155. if (!scan_eba)
  1156. return -ENOMEM;
  1157. fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
  1158. if (!fm_eba) {
  1159. kfree(scan_eba);
  1160. return -ENOMEM;
  1161. }
  1162. for (i = 0; i < num_volumes; i++) {
  1163. vol = ubi->volumes[i];
  1164. if (!vol)
  1165. continue;
  1166. scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
  1167. GFP_KERNEL);
  1168. if (!scan_eba[i]) {
  1169. ret = -ENOMEM;
  1170. goto out_free;
  1171. }
  1172. fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
  1173. GFP_KERNEL);
  1174. if (!fm_eba[i]) {
  1175. ret = -ENOMEM;
  1176. goto out_free;
  1177. }
  1178. for (j = 0; j < vol->reserved_pebs; j++)
  1179. scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
  1180. av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
  1181. if (!av)
  1182. continue;
  1183. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1184. scan_eba[i][aeb->lnum] = aeb->pnum;
  1185. av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
  1186. if (!av)
  1187. continue;
  1188. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
  1189. fm_eba[i][aeb->lnum] = aeb->pnum;
  1190. for (j = 0; j < vol->reserved_pebs; j++) {
  1191. if (scan_eba[i][j] != fm_eba[i][j]) {
  1192. if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
  1193. fm_eba[i][j] == UBI_LEB_UNMAPPED)
  1194. continue;
  1195. ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
  1196. vol->vol_id, i, fm_eba[i][j],
  1197. scan_eba[i][j]);
  1198. ubi_assert(0);
  1199. }
  1200. }
  1201. }
  1202. out_free:
  1203. for (i = 0; i < num_volumes; i++) {
  1204. if (!ubi->volumes[i])
  1205. continue;
  1206. kfree(scan_eba[i]);
  1207. kfree(fm_eba[i]);
  1208. }
  1209. kfree(scan_eba);
  1210. kfree(fm_eba);
  1211. return ret;
  1212. }
  1213. /**
  1214. * ubi_eba_init - initialize the EBA sub-system using attaching information.
  1215. * @ubi: UBI device description object
  1216. * @ai: attaching information
  1217. *
  1218. * This function returns zero in case of success and a negative error code in
  1219. * case of failure.
  1220. */
  1221. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1222. {
  1223. int i, j, err, num_volumes;
  1224. struct ubi_ainf_volume *av;
  1225. struct ubi_volume *vol;
  1226. struct ubi_ainf_peb *aeb;
  1227. struct rb_node *rb;
  1228. dbg_eba("initialize EBA sub-system");
  1229. spin_lock_init(&ubi->ltree_lock);
  1230. mutex_init(&ubi->alc_mutex);
  1231. ubi->ltree = RB_ROOT;
  1232. ubi->global_sqnum = ai->max_sqnum + 1;
  1233. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1234. for (i = 0; i < num_volumes; i++) {
  1235. vol = ubi->volumes[i];
  1236. if (!vol)
  1237. continue;
  1238. cond_resched();
  1239. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  1240. GFP_KERNEL);
  1241. if (!vol->eba_tbl) {
  1242. err = -ENOMEM;
  1243. goto out_free;
  1244. }
  1245. for (j = 0; j < vol->reserved_pebs; j++)
  1246. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  1247. av = ubi_find_av(ai, idx2vol_id(ubi, i));
  1248. if (!av)
  1249. continue;
  1250. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  1251. if (aeb->lnum >= vol->reserved_pebs)
  1252. /*
  1253. * This may happen in case of an unclean reboot
  1254. * during re-size.
  1255. */
  1256. ubi_move_aeb_to_list(av, aeb, &ai->erase);
  1257. else
  1258. vol->eba_tbl[aeb->lnum] = aeb->pnum;
  1259. }
  1260. }
  1261. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1262. ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
  1263. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1264. if (ubi->corr_peb_count)
  1265. ubi_err(ubi, "%d PEBs are corrupted and not used",
  1266. ubi->corr_peb_count);
  1267. err = -ENOSPC;
  1268. goto out_free;
  1269. }
  1270. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1271. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1272. if (ubi->bad_allowed) {
  1273. ubi_calculate_reserved(ubi);
  1274. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1275. /* No enough free physical eraseblocks */
  1276. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1277. print_rsvd_warning(ubi, ai);
  1278. } else
  1279. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1280. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1281. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1282. }
  1283. dbg_eba("EBA sub-system is initialized");
  1284. return 0;
  1285. out_free:
  1286. for (i = 0; i < num_volumes; i++) {
  1287. if (!ubi->volumes[i])
  1288. continue;
  1289. kfree(ubi->volumes[i]->eba_tbl);
  1290. ubi->volumes[i]->eba_tbl = NULL;
  1291. }
  1292. return err;
  1293. }