eba.c 33 KB

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