eba.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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("bad VID header at PEB %d, LEB"
  375. "%d:%d", pnum, vol_id, lnum);
  376. err = -EBADMSG;
  377. } else
  378. ubi_ro_mode(ubi);
  379. }
  380. goto out_free;
  381. } else if (err == UBI_IO_BITFLIPS)
  382. scrub = 1;
  383. ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
  384. ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
  385. crc = be32_to_cpu(vid_hdr->data_crc);
  386. ubi_free_vid_hdr(ubi, vid_hdr);
  387. }
  388. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  389. if (err) {
  390. if (err == UBI_IO_BITFLIPS) {
  391. scrub = 1;
  392. err = 0;
  393. } else if (err == -EBADMSG) {
  394. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  395. goto out_unlock;
  396. scrub = 1;
  397. if (!check) {
  398. ubi_msg("force data checking");
  399. check = 1;
  400. goto retry;
  401. }
  402. } else
  403. goto out_unlock;
  404. }
  405. if (check) {
  406. uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
  407. if (crc1 != crc) {
  408. ubi_warn("CRC error: calculated %#08x, must be %#08x",
  409. crc1, crc);
  410. err = -EBADMSG;
  411. goto out_unlock;
  412. }
  413. }
  414. if (scrub)
  415. err = ubi_wl_scrub_peb(ubi, pnum);
  416. leb_read_unlock(ubi, vol_id, lnum);
  417. return err;
  418. out_free:
  419. ubi_free_vid_hdr(ubi, vid_hdr);
  420. out_unlock:
  421. leb_read_unlock(ubi, vol_id, lnum);
  422. return err;
  423. }
  424. /**
  425. * recover_peb - recover from write failure.
  426. * @ubi: UBI device description object
  427. * @pnum: the physical eraseblock to recover
  428. * @vol_id: volume ID
  429. * @lnum: logical eraseblock number
  430. * @buf: data which was not written because of the write failure
  431. * @offset: offset of the failed write
  432. * @len: how many bytes should have been written
  433. *
  434. * This function is called in case of a write failure and moves all good data
  435. * from the potentially bad physical eraseblock to a good physical eraseblock.
  436. * This function also writes the data which was not written due to the failure.
  437. * Returns new physical eraseblock number in case of success, and a negative
  438. * error code in case of failure.
  439. */
  440. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  441. const void *buf, int offset, int len)
  442. {
  443. int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
  444. struct ubi_volume *vol = ubi->volumes[idx];
  445. struct ubi_vid_hdr *vid_hdr;
  446. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  447. if (!vid_hdr)
  448. return -ENOMEM;
  449. mutex_lock(&ubi->buf_mutex);
  450. retry:
  451. new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
  452. if (new_pnum < 0) {
  453. mutex_unlock(&ubi->buf_mutex);
  454. ubi_free_vid_hdr(ubi, vid_hdr);
  455. return new_pnum;
  456. }
  457. ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
  458. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  459. if (err && err != UBI_IO_BITFLIPS) {
  460. if (err > 0)
  461. err = -EIO;
  462. goto out_put;
  463. }
  464. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  465. err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
  466. if (err)
  467. goto write_error;
  468. data_size = offset + len;
  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_put;
  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. goto write_error;
  480. mutex_unlock(&ubi->buf_mutex);
  481. ubi_free_vid_hdr(ubi, vid_hdr);
  482. vol->eba_tbl[lnum] = new_pnum;
  483. ubi_wl_put_peb(ubi, pnum, 1);
  484. ubi_msg("data was successfully recovered");
  485. return 0;
  486. out_put:
  487. mutex_unlock(&ubi->buf_mutex);
  488. ubi_wl_put_peb(ubi, new_pnum, 1);
  489. ubi_free_vid_hdr(ubi, vid_hdr);
  490. return err;
  491. write_error:
  492. /*
  493. * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
  494. * get another one.
  495. */
  496. ubi_warn("failed to write to PEB %d", new_pnum);
  497. ubi_wl_put_peb(ubi, new_pnum, 1);
  498. if (++tries > UBI_IO_RETRIES) {
  499. mutex_unlock(&ubi->buf_mutex);
  500. ubi_free_vid_hdr(ubi, vid_hdr);
  501. return err;
  502. }
  503. ubi_msg("try again");
  504. goto retry;
  505. }
  506. /**
  507. * ubi_eba_write_leb - write data to dynamic volume.
  508. * @ubi: UBI device description object
  509. * @vol: volume description object
  510. * @lnum: logical eraseblock number
  511. * @buf: the data to write
  512. * @offset: offset within the logical eraseblock where to write
  513. * @len: how many bytes to write
  514. * @dtype: data type
  515. *
  516. * This function writes data to logical eraseblock @lnum of a dynamic volume
  517. * @vol. Returns zero in case of success and a negative error code in case
  518. * of failure. In case of error, it is possible that something was still
  519. * written to the flash media, but may be some garbage.
  520. */
  521. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  522. const void *buf, int offset, int len, int dtype)
  523. {
  524. int err, pnum, tries = 0, vol_id = vol->vol_id;
  525. struct ubi_vid_hdr *vid_hdr;
  526. if (ubi->ro_mode)
  527. return -EROFS;
  528. err = leb_write_lock(ubi, vol_id, lnum);
  529. if (err)
  530. return err;
  531. pnum = vol->eba_tbl[lnum];
  532. if (pnum >= 0) {
  533. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  534. len, offset, vol_id, lnum, pnum);
  535. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  536. if (err) {
  537. ubi_warn("failed to write data to PEB %d", pnum);
  538. if (err == -EIO && ubi->bad_allowed)
  539. err = recover_peb(ubi, pnum, vol_id, lnum, buf,
  540. offset, len);
  541. if (err)
  542. ubi_ro_mode(ubi);
  543. }
  544. leb_write_unlock(ubi, vol_id, lnum);
  545. return err;
  546. }
  547. /*
  548. * The logical eraseblock is not mapped. We have to get a free physical
  549. * eraseblock and write the volume identifier header there first.
  550. */
  551. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  552. if (!vid_hdr) {
  553. leb_write_unlock(ubi, vol_id, lnum);
  554. return -ENOMEM;
  555. }
  556. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  557. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  558. vid_hdr->vol_id = cpu_to_be32(vol_id);
  559. vid_hdr->lnum = cpu_to_be32(lnum);
  560. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  561. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  562. retry:
  563. pnum = ubi_wl_get_peb(ubi, dtype);
  564. if (pnum < 0) {
  565. ubi_free_vid_hdr(ubi, vid_hdr);
  566. leb_write_unlock(ubi, vol_id, lnum);
  567. return pnum;
  568. }
  569. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  570. len, offset, vol_id, lnum, pnum);
  571. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  572. if (err) {
  573. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  574. vol_id, lnum, pnum);
  575. goto write_error;
  576. }
  577. if (len) {
  578. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  579. if (err) {
  580. ubi_warn("failed to write %d bytes at offset %d of "
  581. "LEB %d:%d, PEB %d", len, offset, vol_id,
  582. lnum, pnum);
  583. goto write_error;
  584. }
  585. }
  586. vol->eba_tbl[lnum] = pnum;
  587. leb_write_unlock(ubi, vol_id, lnum);
  588. ubi_free_vid_hdr(ubi, vid_hdr);
  589. return 0;
  590. write_error:
  591. if (err != -EIO || !ubi->bad_allowed) {
  592. ubi_ro_mode(ubi);
  593. leb_write_unlock(ubi, vol_id, lnum);
  594. ubi_free_vid_hdr(ubi, vid_hdr);
  595. return err;
  596. }
  597. /*
  598. * Fortunately, this is the first write operation to this physical
  599. * eraseblock, so just put it and request a new one. We assume that if
  600. * this physical eraseblock went bad, the erase code will handle that.
  601. */
  602. err = ubi_wl_put_peb(ubi, pnum, 1);
  603. if (err || ++tries > UBI_IO_RETRIES) {
  604. ubi_ro_mode(ubi);
  605. leb_write_unlock(ubi, vol_id, lnum);
  606. ubi_free_vid_hdr(ubi, vid_hdr);
  607. return err;
  608. }
  609. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  610. ubi_msg("try another PEB");
  611. goto retry;
  612. }
  613. /**
  614. * ubi_eba_write_leb_st - write data to static volume.
  615. * @ubi: UBI device description object
  616. * @vol: volume description object
  617. * @lnum: logical eraseblock number
  618. * @buf: data to write
  619. * @len: how many bytes to write
  620. * @dtype: data type
  621. * @used_ebs: how many logical eraseblocks will this volume contain
  622. *
  623. * This function writes data to logical eraseblock @lnum of static volume
  624. * @vol. The @used_ebs argument should contain total number of logical
  625. * eraseblock in this static volume.
  626. *
  627. * When writing to the last logical eraseblock, the @len argument doesn't have
  628. * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
  629. * to the real data size, although the @buf buffer has to contain the
  630. * alignment. In all other cases, @len has to be aligned.
  631. *
  632. * It is prohibited to write more then once to logical eraseblocks of static
  633. * volumes. This function returns zero in case of success and a negative error
  634. * code in case of failure.
  635. */
  636. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  637. int lnum, const void *buf, int len, int dtype,
  638. int used_ebs)
  639. {
  640. int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
  641. struct ubi_vid_hdr *vid_hdr;
  642. uint32_t crc;
  643. if (ubi->ro_mode)
  644. return -EROFS;
  645. if (lnum == used_ebs - 1)
  646. /* If this is the last LEB @len may be unaligned */
  647. len = ALIGN(data_size, ubi->min_io_size);
  648. else
  649. ubi_assert(!(len & (ubi->min_io_size - 1)));
  650. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  651. if (!vid_hdr)
  652. return -ENOMEM;
  653. err = leb_write_lock(ubi, vol_id, lnum);
  654. if (err) {
  655. ubi_free_vid_hdr(ubi, vid_hdr);
  656. return err;
  657. }
  658. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  659. vid_hdr->vol_id = cpu_to_be32(vol_id);
  660. vid_hdr->lnum = cpu_to_be32(lnum);
  661. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  662. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  663. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  664. vid_hdr->vol_type = UBI_VID_STATIC;
  665. vid_hdr->data_size = cpu_to_be32(data_size);
  666. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  667. vid_hdr->data_crc = cpu_to_be32(crc);
  668. retry:
  669. pnum = ubi_wl_get_peb(ubi, dtype);
  670. if (pnum < 0) {
  671. ubi_free_vid_hdr(ubi, vid_hdr);
  672. leb_write_unlock(ubi, vol_id, lnum);
  673. return pnum;
  674. }
  675. dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
  676. len, vol_id, lnum, pnum, used_ebs);
  677. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  678. if (err) {
  679. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  680. vol_id, lnum, pnum);
  681. goto write_error;
  682. }
  683. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  684. if (err) {
  685. ubi_warn("failed to write %d bytes of data to PEB %d",
  686. len, pnum);
  687. goto write_error;
  688. }
  689. ubi_assert(vol->eba_tbl[lnum] < 0);
  690. vol->eba_tbl[lnum] = pnum;
  691. leb_write_unlock(ubi, vol_id, lnum);
  692. ubi_free_vid_hdr(ubi, vid_hdr);
  693. return 0;
  694. write_error:
  695. if (err != -EIO || !ubi->bad_allowed) {
  696. /*
  697. * This flash device does not admit of bad eraseblocks or
  698. * something nasty and unexpected happened. Switch to read-only
  699. * mode just in case.
  700. */
  701. ubi_ro_mode(ubi);
  702. leb_write_unlock(ubi, vol_id, lnum);
  703. ubi_free_vid_hdr(ubi, vid_hdr);
  704. return err;
  705. }
  706. err = ubi_wl_put_peb(ubi, pnum, 1);
  707. if (err || ++tries > UBI_IO_RETRIES) {
  708. ubi_ro_mode(ubi);
  709. leb_write_unlock(ubi, vol_id, lnum);
  710. ubi_free_vid_hdr(ubi, vid_hdr);
  711. return err;
  712. }
  713. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  714. ubi_msg("try another PEB");
  715. goto retry;
  716. }
  717. /*
  718. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  719. * @ubi: UBI device description object
  720. * @vol: volume description object
  721. * @lnum: logical eraseblock number
  722. * @buf: data to write
  723. * @len: how many bytes to write
  724. * @dtype: data type
  725. *
  726. * This function changes the contents of a logical eraseblock atomically. @buf
  727. * has to contain new logical eraseblock data, and @len - the length of the
  728. * data, which has to be aligned. This function guarantees that in case of an
  729. * unclean reboot the old contents is preserved. Returns zero in case of
  730. * success and a negative error code in case of failure.
  731. *
  732. * UBI reserves one LEB for the "atomic LEB change" operation, so only one
  733. * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  734. */
  735. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  736. int lnum, const void *buf, int len, int dtype)
  737. {
  738. int err, pnum, tries = 0, vol_id = vol->vol_id;
  739. struct ubi_vid_hdr *vid_hdr;
  740. uint32_t crc;
  741. if (ubi->ro_mode)
  742. return -EROFS;
  743. if (len == 0) {
  744. /*
  745. * Special case when data length is zero. In this case the LEB
  746. * has to be unmapped and mapped somewhere else.
  747. */
  748. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  749. if (err)
  750. return err;
  751. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
  752. }
  753. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  754. if (!vid_hdr)
  755. return -ENOMEM;
  756. mutex_lock(&ubi->alc_mutex);
  757. err = leb_write_lock(ubi, vol_id, lnum);
  758. if (err)
  759. goto out_mutex;
  760. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  761. vid_hdr->vol_id = cpu_to_be32(vol_id);
  762. vid_hdr->lnum = cpu_to_be32(lnum);
  763. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  764. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  765. crc = crc32(UBI_CRC32_INIT, buf, len);
  766. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  767. vid_hdr->data_size = cpu_to_be32(len);
  768. vid_hdr->copy_flag = 1;
  769. vid_hdr->data_crc = cpu_to_be32(crc);
  770. retry:
  771. pnum = ubi_wl_get_peb(ubi, dtype);
  772. if (pnum < 0) {
  773. err = pnum;
  774. goto out_leb_unlock;
  775. }
  776. dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
  777. vol_id, lnum, vol->eba_tbl[lnum], pnum);
  778. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  779. if (err) {
  780. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  781. vol_id, lnum, pnum);
  782. goto write_error;
  783. }
  784. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  785. if (err) {
  786. ubi_warn("failed to write %d bytes of data to PEB %d",
  787. len, pnum);
  788. goto write_error;
  789. }
  790. if (vol->eba_tbl[lnum] >= 0) {
  791. err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 0);
  792. if (err)
  793. goto out_leb_unlock;
  794. }
  795. vol->eba_tbl[lnum] = pnum;
  796. out_leb_unlock:
  797. leb_write_unlock(ubi, vol_id, lnum);
  798. out_mutex:
  799. mutex_unlock(&ubi->alc_mutex);
  800. ubi_free_vid_hdr(ubi, vid_hdr);
  801. return err;
  802. write_error:
  803. if (err != -EIO || !ubi->bad_allowed) {
  804. /*
  805. * This flash device does not admit of bad eraseblocks or
  806. * something nasty and unexpected happened. Switch to read-only
  807. * mode just in case.
  808. */
  809. ubi_ro_mode(ubi);
  810. goto out_leb_unlock;
  811. }
  812. err = ubi_wl_put_peb(ubi, pnum, 1);
  813. if (err || ++tries > UBI_IO_RETRIES) {
  814. ubi_ro_mode(ubi);
  815. goto out_leb_unlock;
  816. }
  817. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  818. ubi_msg("try another PEB");
  819. goto retry;
  820. }
  821. /**
  822. * ubi_eba_copy_leb - copy logical eraseblock.
  823. * @ubi: UBI device description object
  824. * @from: physical eraseblock number from where to copy
  825. * @to: physical eraseblock number where to copy
  826. * @vid_hdr: VID header of the @from physical eraseblock
  827. *
  828. * This function copies logical eraseblock from physical eraseblock @from to
  829. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  830. * function. Returns:
  831. * o %0 in case of success;
  832. * o %1 if the operation was canceled and should be tried later (e.g.,
  833. * because a bit-flip was detected at the target PEB);
  834. * o %2 if the volume is being deleted and this LEB should not be moved.
  835. */
  836. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  837. struct ubi_vid_hdr *vid_hdr)
  838. {
  839. int err, vol_id, lnum, data_size, aldata_size, idx;
  840. struct ubi_volume *vol;
  841. uint32_t crc;
  842. vol_id = be32_to_cpu(vid_hdr->vol_id);
  843. lnum = be32_to_cpu(vid_hdr->lnum);
  844. dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  845. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  846. data_size = be32_to_cpu(vid_hdr->data_size);
  847. aldata_size = ALIGN(data_size, ubi->min_io_size);
  848. } else
  849. data_size = aldata_size =
  850. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  851. idx = vol_id2idx(ubi, vol_id);
  852. spin_lock(&ubi->volumes_lock);
  853. /*
  854. * Note, we may race with volume deletion, which means that the volume
  855. * this logical eraseblock belongs to might be being deleted. Since the
  856. * volume deletion unmaps all the volume's logical eraseblocks, it will
  857. * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
  858. */
  859. vol = ubi->volumes[idx];
  860. if (!vol) {
  861. /* No need to do further work, cancel */
  862. dbg_eba("volume %d is being removed, cancel", vol_id);
  863. spin_unlock(&ubi->volumes_lock);
  864. return 2;
  865. }
  866. spin_unlock(&ubi->volumes_lock);
  867. /*
  868. * We do not want anybody to write to this logical eraseblock while we
  869. * are moving it, so lock it.
  870. *
  871. * Note, we are using non-waiting locking here, because we cannot sleep
  872. * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
  873. * unmapping the LEB which is mapped to the PEB we are going to move
  874. * (@from). This task locks the LEB and goes sleep in the
  875. * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
  876. * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
  877. * LEB is already locked, we just do not move it and return %1.
  878. */
  879. err = leb_write_trylock(ubi, vol_id, lnum);
  880. if (err) {
  881. dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
  882. return err;
  883. }
  884. /*
  885. * The LEB might have been put meanwhile, and the task which put it is
  886. * probably waiting on @ubi->move_mutex. No need to continue the work,
  887. * cancel it.
  888. */
  889. if (vol->eba_tbl[lnum] != from) {
  890. dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
  891. "PEB %d, cancel", vol_id, lnum, from,
  892. vol->eba_tbl[lnum]);
  893. err = 1;
  894. goto out_unlock_leb;
  895. }
  896. /*
  897. * OK, now the LEB is locked and we can safely start moving iy. Since
  898. * this function utilizes thie @ubi->peb1_buf buffer which is shared
  899. * with some other functions, so lock the buffer by taking the
  900. * @ubi->buf_mutex.
  901. */
  902. mutex_lock(&ubi->buf_mutex);
  903. dbg_eba("read %d bytes of data", aldata_size);
  904. err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
  905. if (err && err != UBI_IO_BITFLIPS) {
  906. ubi_warn("error %d while reading data from PEB %d",
  907. err, from);
  908. goto out_unlock_buf;
  909. }
  910. /*
  911. * Now we have got to calculate how much data we have to to copy. In
  912. * case of a static volume it is fairly easy - the VID header contains
  913. * the data size. In case of a dynamic volume it is more difficult - we
  914. * have to read the contents, cut 0xFF bytes from the end and copy only
  915. * the first part. We must do this to avoid writing 0xFF bytes as it
  916. * may have some side-effects. And not only this. It is important not
  917. * to include those 0xFFs to CRC because later the they may be filled
  918. * by data.
  919. */
  920. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  921. aldata_size = data_size =
  922. ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
  923. cond_resched();
  924. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
  925. cond_resched();
  926. /*
  927. * It may turn out to me that the whole @from physical eraseblock
  928. * contains only 0xFF bytes. Then we have to only write the VID header
  929. * and do not write any data. This also means we should not set
  930. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  931. */
  932. if (data_size > 0) {
  933. vid_hdr->copy_flag = 1;
  934. vid_hdr->data_size = cpu_to_be32(data_size);
  935. vid_hdr->data_crc = cpu_to_be32(crc);
  936. }
  937. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  938. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  939. if (err)
  940. goto out_unlock_buf;
  941. cond_resched();
  942. /* Read the VID header back and check if it was written correctly */
  943. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  944. if (err) {
  945. if (err != UBI_IO_BITFLIPS)
  946. ubi_warn("cannot read VID header back from PEB %d", to);
  947. else
  948. err = 1;
  949. goto out_unlock_buf;
  950. }
  951. if (data_size > 0) {
  952. err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
  953. if (err)
  954. goto out_unlock_buf;
  955. cond_resched();
  956. /*
  957. * We've written the data and are going to read it back to make
  958. * sure it was written correctly.
  959. */
  960. err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
  961. if (err) {
  962. if (err != UBI_IO_BITFLIPS)
  963. ubi_warn("cannot read data back from PEB %d",
  964. to);
  965. else
  966. err = 1;
  967. goto out_unlock_buf;
  968. }
  969. cond_resched();
  970. if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
  971. ubi_warn("read data back from PEB %d - it is different",
  972. to);
  973. goto out_unlock_buf;
  974. }
  975. }
  976. ubi_assert(vol->eba_tbl[lnum] == from);
  977. vol->eba_tbl[lnum] = to;
  978. out_unlock_buf:
  979. mutex_unlock(&ubi->buf_mutex);
  980. out_unlock_leb:
  981. leb_write_unlock(ubi, vol_id, lnum);
  982. return err;
  983. }
  984. /**
  985. * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
  986. * @ubi: UBI device description object
  987. * @si: scanning information
  988. *
  989. * This function returns zero in case of success and a negative error code in
  990. * case of failure.
  991. */
  992. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  993. {
  994. int i, j, err, num_volumes;
  995. struct ubi_scan_volume *sv;
  996. struct ubi_volume *vol;
  997. struct ubi_scan_leb *seb;
  998. struct rb_node *rb;
  999. dbg_eba("initialize EBA sub-system");
  1000. spin_lock_init(&ubi->ltree_lock);
  1001. mutex_init(&ubi->alc_mutex);
  1002. ubi->ltree = RB_ROOT;
  1003. ubi->global_sqnum = si->max_sqnum + 1;
  1004. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1005. for (i = 0; i < num_volumes; i++) {
  1006. vol = ubi->volumes[i];
  1007. if (!vol)
  1008. continue;
  1009. cond_resched();
  1010. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  1011. GFP_KERNEL);
  1012. if (!vol->eba_tbl) {
  1013. err = -ENOMEM;
  1014. goto out_free;
  1015. }
  1016. for (j = 0; j < vol->reserved_pebs; j++)
  1017. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  1018. sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
  1019. if (!sv)
  1020. continue;
  1021. ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
  1022. if (seb->lnum >= vol->reserved_pebs)
  1023. /*
  1024. * This may happen in case of an unclean reboot
  1025. * during re-size.
  1026. */
  1027. ubi_scan_move_to_list(sv, seb, &si->erase);
  1028. vol->eba_tbl[seb->lnum] = seb->pnum;
  1029. }
  1030. }
  1031. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1032. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1033. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1034. err = -ENOSPC;
  1035. goto out_free;
  1036. }
  1037. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1038. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1039. if (ubi->bad_allowed) {
  1040. ubi_calculate_reserved(ubi);
  1041. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1042. /* No enough free physical eraseblocks */
  1043. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1044. ubi_warn("cannot reserve enough PEBs for bad PEB "
  1045. "handling, reserved %d, need %d",
  1046. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1047. } else
  1048. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1049. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1050. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1051. }
  1052. dbg_eba("EBA sub-system is initialized");
  1053. return 0;
  1054. out_free:
  1055. for (i = 0; i < num_volumes; i++) {
  1056. if (!ubi->volumes[i])
  1057. continue;
  1058. kfree(ubi->volumes[i]->eba_tbl);
  1059. }
  1060. return err;
  1061. }