eba.c 40 KB

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