eba.c 44 KB

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