eba.c 46 KB

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