eba.c 34 KB

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