eba.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 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. /**
  47. * struct ltree_entry - an entry in the lock tree.
  48. * @rb: links RB-tree nodes
  49. * @vol_id: volume ID of the locked logical eraseblock
  50. * @lnum: locked logical eraseblock number
  51. * @users: how many tasks are using this logical eraseblock or wait for it
  52. * @mutex: read/write mutex to implement read/write access serialization to
  53. * the (@vol_id, @lnum) logical eraseblock
  54. *
  55. * When a logical eraseblock is being locked - corresponding &struct ltree_entry
  56. * object is inserted to the lock tree (@ubi->ltree).
  57. */
  58. struct ltree_entry {
  59. struct rb_node rb;
  60. int vol_id;
  61. int lnum;
  62. int users;
  63. struct rw_semaphore mutex;
  64. };
  65. /* Slab cache for lock-tree entries */
  66. static struct kmem_cache *ltree_slab;
  67. /**
  68. * next_sqnum - get next sequence number.
  69. * @ubi: UBI device description object
  70. *
  71. * This function returns next sequence number to use, which is just the current
  72. * global sequence counter value. It also increases the global sequence
  73. * counter.
  74. */
  75. static unsigned long long next_sqnum(struct ubi_device *ubi)
  76. {
  77. unsigned long long sqnum;
  78. spin_lock(&ubi->ltree_lock);
  79. sqnum = ubi->global_sqnum++;
  80. spin_unlock(&ubi->ltree_lock);
  81. return sqnum;
  82. }
  83. /**
  84. * ubi_get_compat - get compatibility flags of a volume.
  85. * @ubi: UBI device description object
  86. * @vol_id: volume ID
  87. *
  88. * This function returns compatibility flags for an internal volume. User
  89. * volumes have no compatibility flags, so %0 is returned.
  90. */
  91. static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
  92. {
  93. if (vol_id == UBI_LAYOUT_VOL_ID)
  94. return UBI_LAYOUT_VOLUME_COMPAT;
  95. return 0;
  96. }
  97. /**
  98. * ltree_lookup - look up the lock tree.
  99. * @ubi: UBI device description object
  100. * @vol_id: volume ID
  101. * @lnum: logical eraseblock number
  102. *
  103. * This function returns a pointer to the corresponding &struct ltree_entry
  104. * object if the logical eraseblock is locked and %NULL if it is not.
  105. * @ubi->ltree_lock has to be locked.
  106. */
  107. static struct ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
  108. int lnum)
  109. {
  110. struct rb_node *p;
  111. p = ubi->ltree.rb_node;
  112. while (p) {
  113. struct ltree_entry *le;
  114. le = rb_entry(p, struct ltree_entry, rb);
  115. if (vol_id < le->vol_id)
  116. p = p->rb_left;
  117. else if (vol_id > le->vol_id)
  118. p = p->rb_right;
  119. else {
  120. if (lnum < le->lnum)
  121. p = p->rb_left;
  122. else if (lnum > le->lnum)
  123. p = p->rb_right;
  124. else
  125. return le;
  126. }
  127. }
  128. return NULL;
  129. }
  130. /**
  131. * ltree_add_entry - add new entry to the lock tree.
  132. * @ubi: UBI device description object
  133. * @vol_id: volume ID
  134. * @lnum: logical eraseblock number
  135. *
  136. * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
  137. * lock tree. If such entry is already there, its usage counter is increased.
  138. * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
  139. * failed.
  140. */
  141. static struct ltree_entry *ltree_add_entry(struct ubi_device *ubi, int vol_id,
  142. int lnum)
  143. {
  144. struct ltree_entry *le, *le1, *le_free;
  145. le = kmem_cache_alloc(ltree_slab, GFP_KERNEL);
  146. if (!le)
  147. return ERR_PTR(-ENOMEM);
  148. le->vol_id = vol_id;
  149. le->lnum = lnum;
  150. spin_lock(&ubi->ltree_lock);
  151. le1 = ltree_lookup(ubi, vol_id, lnum);
  152. if (le1) {
  153. /*
  154. * This logical eraseblock is already locked. The newly
  155. * allocated lock entry is not needed.
  156. */
  157. le_free = le;
  158. le = le1;
  159. } else {
  160. struct rb_node **p, *parent = NULL;
  161. /*
  162. * No lock entry, add the newly allocated one to the
  163. * @ubi->ltree RB-tree.
  164. */
  165. le_free = NULL;
  166. p = &ubi->ltree.rb_node;
  167. while (*p) {
  168. parent = *p;
  169. le1 = rb_entry(parent, struct ltree_entry, rb);
  170. if (vol_id < le1->vol_id)
  171. p = &(*p)->rb_left;
  172. else if (vol_id > le1->vol_id)
  173. p = &(*p)->rb_right;
  174. else {
  175. ubi_assert(lnum != le1->lnum);
  176. if (lnum < le1->lnum)
  177. p = &(*p)->rb_left;
  178. else
  179. p = &(*p)->rb_right;
  180. }
  181. }
  182. rb_link_node(&le->rb, parent, p);
  183. rb_insert_color(&le->rb, &ubi->ltree);
  184. }
  185. le->users += 1;
  186. spin_unlock(&ubi->ltree_lock);
  187. if (le_free)
  188. kmem_cache_free(ltree_slab, le_free);
  189. return le;
  190. }
  191. /**
  192. * leb_read_lock - lock logical eraseblock for reading.
  193. * @ubi: UBI device description object
  194. * @vol_id: volume ID
  195. * @lnum: logical eraseblock number
  196. *
  197. * This function locks a logical eraseblock for reading. Returns zero in case
  198. * of success and a negative error code in case of failure.
  199. */
  200. static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
  201. {
  202. struct ltree_entry *le;
  203. le = ltree_add_entry(ubi, vol_id, lnum);
  204. if (IS_ERR(le))
  205. return PTR_ERR(le);
  206. down_read(&le->mutex);
  207. return 0;
  208. }
  209. /**
  210. * leb_read_unlock - unlock logical eraseblock.
  211. * @ubi: UBI device description object
  212. * @vol_id: volume ID
  213. * @lnum: logical eraseblock number
  214. */
  215. static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  216. {
  217. int free = 0;
  218. struct ltree_entry *le;
  219. spin_lock(&ubi->ltree_lock);
  220. le = ltree_lookup(ubi, vol_id, lnum);
  221. le->users -= 1;
  222. ubi_assert(le->users >= 0);
  223. if (le->users == 0) {
  224. rb_erase(&le->rb, &ubi->ltree);
  225. free = 1;
  226. }
  227. spin_unlock(&ubi->ltree_lock);
  228. up_read(&le->mutex);
  229. if (free)
  230. kmem_cache_free(ltree_slab, le);
  231. }
  232. /**
  233. * leb_write_lock - lock logical eraseblock for writing.
  234. * @ubi: UBI device description object
  235. * @vol_id: volume ID
  236. * @lnum: logical eraseblock number
  237. *
  238. * This function locks a logical eraseblock for writing. Returns zero in case
  239. * of success and a negative error code in case of failure.
  240. */
  241. static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
  242. {
  243. struct ltree_entry *le;
  244. le = ltree_add_entry(ubi, vol_id, lnum);
  245. if (IS_ERR(le))
  246. return PTR_ERR(le);
  247. down_write(&le->mutex);
  248. return 0;
  249. }
  250. /**
  251. * leb_write_unlock - unlock logical eraseblock.
  252. * @ubi: UBI device description object
  253. * @vol_id: volume ID
  254. * @lnum: logical eraseblock number
  255. */
  256. static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  257. {
  258. int free;
  259. struct ltree_entry *le;
  260. spin_lock(&ubi->ltree_lock);
  261. le = ltree_lookup(ubi, vol_id, lnum);
  262. le->users -= 1;
  263. ubi_assert(le->users >= 0);
  264. if (le->users == 0) {
  265. rb_erase(&le->rb, &ubi->ltree);
  266. free = 1;
  267. } else
  268. free = 0;
  269. spin_unlock(&ubi->ltree_lock);
  270. up_write(&le->mutex);
  271. if (free)
  272. kmem_cache_free(ltree_slab, le);
  273. }
  274. /**
  275. * ubi_eba_unmap_leb - un-map logical eraseblock.
  276. * @ubi: UBI device description object
  277. * @vol_id: volume ID
  278. * @lnum: logical eraseblock number
  279. *
  280. * This function un-maps logical eraseblock @lnum and schedules corresponding
  281. * physical eraseblock for erasure. Returns zero in case of success and a
  282. * negative error code in case of failure.
  283. */
  284. int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum)
  285. {
  286. int idx = vol_id2idx(ubi, vol_id), err, pnum;
  287. struct ubi_volume *vol = ubi->volumes[idx];
  288. if (ubi->ro_mode)
  289. return -EROFS;
  290. err = leb_write_lock(ubi, vol_id, lnum);
  291. if (err)
  292. return err;
  293. pnum = vol->eba_tbl[lnum];
  294. if (pnum < 0)
  295. /* This logical eraseblock is already unmapped */
  296. goto out_unlock;
  297. dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
  298. vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
  299. err = ubi_wl_put_peb(ubi, pnum, 0);
  300. out_unlock:
  301. leb_write_unlock(ubi, vol_id, lnum);
  302. return err;
  303. }
  304. /**
  305. * ubi_eba_read_leb - read data.
  306. * @ubi: UBI device description object
  307. * @vol_id: volume ID
  308. * @lnum: logical eraseblock number
  309. * @buf: buffer to store the read data
  310. * @offset: offset from where to read
  311. * @len: how many bytes to read
  312. * @check: data CRC check flag
  313. *
  314. * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
  315. * bytes. The @check flag only makes sense for static volumes and forces
  316. * eraseblock data CRC checking.
  317. *
  318. * In case of success this function returns zero. In case of a static volume,
  319. * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
  320. * returned for any volume type if an ECC error was detected by the MTD device
  321. * driver. Other negative error cored may be returned in case of other errors.
  322. */
  323. int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,
  324. int offset, int len, int check)
  325. {
  326. int err, pnum, scrub = 0, idx = vol_id2idx(ubi, vol_id);
  327. struct ubi_vid_hdr *vid_hdr;
  328. struct ubi_volume *vol = ubi->volumes[idx];
  329. uint32_t crc, crc1;
  330. err = leb_read_lock(ubi, vol_id, lnum);
  331. if (err)
  332. return err;
  333. pnum = vol->eba_tbl[lnum];
  334. if (pnum < 0) {
  335. /*
  336. * The logical eraseblock is not mapped, fill the whole buffer
  337. * with 0xFF bytes. The exception is static volumes for which
  338. * it is an error to read unmapped logical eraseblocks.
  339. */
  340. dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
  341. len, offset, vol_id, lnum);
  342. leb_read_unlock(ubi, vol_id, lnum);
  343. ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
  344. memset(buf, 0xFF, len);
  345. return 0;
  346. }
  347. dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
  348. len, offset, vol_id, lnum, pnum);
  349. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  350. check = 0;
  351. retry:
  352. if (check) {
  353. vid_hdr = ubi_zalloc_vid_hdr(ubi);
  354. if (!vid_hdr) {
  355. err = -ENOMEM;
  356. goto out_unlock;
  357. }
  358. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  359. if (err && err != UBI_IO_BITFLIPS) {
  360. if (err > 0) {
  361. /*
  362. * The header is either absent or corrupted.
  363. * The former case means there is a bug -
  364. * switch to read-only mode just in case.
  365. * The latter case means a real corruption - we
  366. * may try to recover data. FIXME: but this is
  367. * not implemented.
  368. */
  369. if (err == UBI_IO_BAD_VID_HDR) {
  370. ubi_warn("bad VID header at PEB %d, LEB"
  371. "%d:%d", pnum, vol_id, lnum);
  372. err = -EBADMSG;
  373. } else
  374. ubi_ro_mode(ubi);
  375. }
  376. goto out_free;
  377. } else if (err == UBI_IO_BITFLIPS)
  378. scrub = 1;
  379. ubi_assert(lnum < ubi32_to_cpu(vid_hdr->used_ebs));
  380. ubi_assert(len == ubi32_to_cpu(vid_hdr->data_size));
  381. crc = ubi32_to_cpu(vid_hdr->data_crc);
  382. ubi_free_vid_hdr(ubi, vid_hdr);
  383. }
  384. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  385. if (err) {
  386. if (err == UBI_IO_BITFLIPS) {
  387. scrub = 1;
  388. err = 0;
  389. } else if (err == -EBADMSG) {
  390. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  391. goto out_unlock;
  392. scrub = 1;
  393. if (!check) {
  394. ubi_msg("force data checking");
  395. check = 1;
  396. goto retry;
  397. }
  398. } else
  399. goto out_unlock;
  400. }
  401. if (check) {
  402. crc1 = crc32(UBI_CRC32_INIT, buf, len);
  403. if (crc1 != crc) {
  404. ubi_warn("CRC error: calculated %#08x, must be %#08x",
  405. crc1, crc);
  406. err = -EBADMSG;
  407. goto out_unlock;
  408. }
  409. }
  410. if (scrub)
  411. err = ubi_wl_scrub_peb(ubi, pnum);
  412. leb_read_unlock(ubi, vol_id, lnum);
  413. return err;
  414. out_free:
  415. ubi_free_vid_hdr(ubi, vid_hdr);
  416. out_unlock:
  417. leb_read_unlock(ubi, vol_id, lnum);
  418. return err;
  419. }
  420. /**
  421. * recover_peb - recover from write failure.
  422. * @ubi: UBI device description object
  423. * @pnum: the physical eraseblock to recover
  424. * @vol_id: volume ID
  425. * @lnum: logical eraseblock number
  426. * @buf: data which was not written because of the write failure
  427. * @offset: offset of the failed write
  428. * @len: how many bytes should have been written
  429. *
  430. * This function is called in case of a write failure and moves all good data
  431. * from the potentially bad physical eraseblock to a good physical eraseblock.
  432. * This function also writes the data which was not written due to the failure.
  433. * Returns new physical eraseblock number in case of success, and a negative
  434. * error code in case of failure.
  435. */
  436. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  437. const void *buf, int offset, int len)
  438. {
  439. int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
  440. struct ubi_volume *vol = ubi->volumes[idx];
  441. struct ubi_vid_hdr *vid_hdr;
  442. unsigned char *new_buf;
  443. vid_hdr = ubi_zalloc_vid_hdr(ubi);
  444. if (!vid_hdr) {
  445. return -ENOMEM;
  446. }
  447. retry:
  448. new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
  449. if (new_pnum < 0) {
  450. ubi_free_vid_hdr(ubi, vid_hdr);
  451. return new_pnum;
  452. }
  453. ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
  454. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  455. if (err && err != UBI_IO_BITFLIPS) {
  456. if (err > 0)
  457. err = -EIO;
  458. goto out_put;
  459. }
  460. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  461. err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
  462. if (err)
  463. goto write_error;
  464. data_size = offset + len;
  465. new_buf = kmalloc(data_size, GFP_KERNEL);
  466. if (!new_buf) {
  467. err = -ENOMEM;
  468. goto out_put;
  469. }
  470. memset(new_buf + offset, 0xFF, len);
  471. /* Read everything before the area where the write failure happened */
  472. if (offset > 0) {
  473. err = ubi_io_read_data(ubi, new_buf, pnum, 0, offset);
  474. if (err && err != UBI_IO_BITFLIPS) {
  475. kfree(new_buf);
  476. goto out_put;
  477. }
  478. }
  479. memcpy(new_buf + offset, buf, len);
  480. err = ubi_io_write_data(ubi, new_buf, new_pnum, 0, data_size);
  481. if (err) {
  482. kfree(new_buf);
  483. goto write_error;
  484. }
  485. kfree(new_buf);
  486. ubi_free_vid_hdr(ubi, vid_hdr);
  487. vol->eba_tbl[lnum] = new_pnum;
  488. ubi_wl_put_peb(ubi, pnum, 1);
  489. ubi_msg("data was successfully recovered");
  490. return 0;
  491. out_put:
  492. ubi_wl_put_peb(ubi, new_pnum, 1);
  493. ubi_free_vid_hdr(ubi, vid_hdr);
  494. return err;
  495. write_error:
  496. /*
  497. * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
  498. * get another one.
  499. */
  500. ubi_warn("failed to write to PEB %d", new_pnum);
  501. ubi_wl_put_peb(ubi, new_pnum, 1);
  502. if (++tries > UBI_IO_RETRIES) {
  503. ubi_free_vid_hdr(ubi, vid_hdr);
  504. return err;
  505. }
  506. ubi_msg("try again");
  507. goto retry;
  508. }
  509. /**
  510. * ubi_eba_write_leb - write data to dynamic volume.
  511. * @ubi: UBI device description object
  512. * @vol_id: volume ID
  513. * @lnum: logical eraseblock number
  514. * @buf: the data to write
  515. * @offset: offset within the logical eraseblock where to write
  516. * @len: how many bytes to write
  517. * @dtype: data type
  518. *
  519. * This function writes data to logical eraseblock @lnum of a dynamic volume
  520. * @vol_id. Returns zero in case of success and a negative error code in case
  521. * of failure. In case of error, it is possible that something was still
  522. * written to the flash media, but may be some garbage.
  523. */
  524. int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum,
  525. const void *buf, int offset, int len, int dtype)
  526. {
  527. int idx = vol_id2idx(ubi, vol_id), err, pnum, tries = 0;
  528. struct ubi_volume *vol = ubi->volumes[idx];
  529. struct ubi_vid_hdr *vid_hdr;
  530. if (ubi->ro_mode)
  531. return -EROFS;
  532. err = leb_write_lock(ubi, vol_id, lnum);
  533. if (err)
  534. return err;
  535. pnum = vol->eba_tbl[lnum];
  536. if (pnum >= 0) {
  537. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  538. len, offset, vol_id, lnum, pnum);
  539. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  540. if (err) {
  541. ubi_warn("failed to write data to PEB %d", pnum);
  542. if (err == -EIO && ubi->bad_allowed)
  543. err = recover_peb(ubi, pnum, vol_id, lnum, buf, offset, len);
  544. if (err)
  545. ubi_ro_mode(ubi);
  546. }
  547. leb_write_unlock(ubi, vol_id, lnum);
  548. return err;
  549. }
  550. /*
  551. * The logical eraseblock is not mapped. We have to get a free physical
  552. * eraseblock and write the volume identifier header there first.
  553. */
  554. vid_hdr = ubi_zalloc_vid_hdr(ubi);
  555. if (!vid_hdr) {
  556. leb_write_unlock(ubi, vol_id, lnum);
  557. return -ENOMEM;
  558. }
  559. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  560. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  561. vid_hdr->vol_id = cpu_to_ubi32(vol_id);
  562. vid_hdr->lnum = cpu_to_ubi32(lnum);
  563. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  564. vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad);
  565. retry:
  566. pnum = ubi_wl_get_peb(ubi, dtype);
  567. if (pnum < 0) {
  568. ubi_free_vid_hdr(ubi, vid_hdr);
  569. leb_write_unlock(ubi, vol_id, lnum);
  570. return pnum;
  571. }
  572. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  573. len, offset, vol_id, lnum, pnum);
  574. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  575. if (err) {
  576. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  577. vol_id, lnum, pnum);
  578. goto write_error;
  579. }
  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 LEB %d:%d, "
  583. "PEB %d", len, offset, vol_id, lnum, pnum);
  584. goto write_error;
  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_ubi64(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_id: volume ID
  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_id. 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, int vol_id, int lnum,
  637. const void *buf, int len, int dtype, int used_ebs)
  638. {
  639. int err, pnum, tries = 0, data_size = len;
  640. int idx = vol_id2idx(ubi, vol_id);
  641. struct ubi_volume *vol = ubi->volumes[idx];
  642. struct ubi_vid_hdr *vid_hdr;
  643. uint32_t crc;
  644. if (ubi->ro_mode)
  645. return -EROFS;
  646. if (lnum == used_ebs - 1)
  647. /* If this is the last LEB @len may be unaligned */
  648. len = ALIGN(data_size, ubi->min_io_size);
  649. else
  650. ubi_assert(len % ubi->min_io_size == 0);
  651. vid_hdr = ubi_zalloc_vid_hdr(ubi);
  652. if (!vid_hdr)
  653. return -ENOMEM;
  654. err = leb_write_lock(ubi, vol_id, lnum);
  655. if (err) {
  656. ubi_free_vid_hdr(ubi, vid_hdr);
  657. return err;
  658. }
  659. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  660. vid_hdr->vol_id = cpu_to_ubi32(vol_id);
  661. vid_hdr->lnum = cpu_to_ubi32(lnum);
  662. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  663. vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad);
  664. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  665. vid_hdr->vol_type = UBI_VID_STATIC;
  666. vid_hdr->data_size = cpu_to_ubi32(data_size);
  667. vid_hdr->used_ebs = cpu_to_ubi32(used_ebs);
  668. vid_hdr->data_crc = cpu_to_ubi32(crc);
  669. retry:
  670. pnum = ubi_wl_get_peb(ubi, dtype);
  671. if (pnum < 0) {
  672. ubi_free_vid_hdr(ubi, vid_hdr);
  673. leb_write_unlock(ubi, vol_id, lnum);
  674. return pnum;
  675. }
  676. dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
  677. len, vol_id, lnum, pnum, used_ebs);
  678. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  679. if (err) {
  680. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  681. vol_id, lnum, pnum);
  682. goto write_error;
  683. }
  684. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  685. if (err) {
  686. ubi_warn("failed to write %d bytes of data to PEB %d",
  687. len, pnum);
  688. goto write_error;
  689. }
  690. ubi_assert(vol->eba_tbl[lnum] < 0);
  691. vol->eba_tbl[lnum] = pnum;
  692. leb_write_unlock(ubi, vol_id, lnum);
  693. ubi_free_vid_hdr(ubi, vid_hdr);
  694. return 0;
  695. write_error:
  696. if (err != -EIO || !ubi->bad_allowed) {
  697. /*
  698. * This flash device does not admit of bad eraseblocks or
  699. * something nasty and unexpected happened. Switch to read-only
  700. * mode just in case.
  701. */
  702. ubi_ro_mode(ubi);
  703. leb_write_unlock(ubi, vol_id, lnum);
  704. ubi_free_vid_hdr(ubi, vid_hdr);
  705. return err;
  706. }
  707. err = ubi_wl_put_peb(ubi, pnum, 1);
  708. if (err || ++tries > UBI_IO_RETRIES) {
  709. ubi_ro_mode(ubi);
  710. leb_write_unlock(ubi, vol_id, lnum);
  711. ubi_free_vid_hdr(ubi, vid_hdr);
  712. return err;
  713. }
  714. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  715. ubi_msg("try another PEB");
  716. goto retry;
  717. }
  718. /*
  719. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  720. * @ubi: UBI device description object
  721. * @vol_id: volume ID
  722. * @lnum: logical eraseblock number
  723. * @buf: data to write
  724. * @len: how many bytes to write
  725. * @dtype: data type
  726. *
  727. * This function changes the contents of a logical eraseblock atomically. @buf
  728. * has to contain new logical eraseblock data, and @len - the length of the
  729. * data, which has to be aligned. This function guarantees that in case of an
  730. * unclean reboot the old contents is preserved. Returns zero in case of
  731. * success and a negative error code in case of failure.
  732. */
  733. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
  734. const void *buf, int len, int dtype)
  735. {
  736. int err, pnum, tries = 0, idx = vol_id2idx(ubi, vol_id);
  737. struct ubi_volume *vol = ubi->volumes[idx];
  738. struct ubi_vid_hdr *vid_hdr;
  739. uint32_t crc;
  740. if (ubi->ro_mode)
  741. return -EROFS;
  742. vid_hdr = ubi_zalloc_vid_hdr(ubi);
  743. if (!vid_hdr)
  744. return -ENOMEM;
  745. err = leb_write_lock(ubi, vol_id, lnum);
  746. if (err) {
  747. ubi_free_vid_hdr(ubi, vid_hdr);
  748. return err;
  749. }
  750. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  751. vid_hdr->vol_id = cpu_to_ubi32(vol_id);
  752. vid_hdr->lnum = cpu_to_ubi32(lnum);
  753. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  754. vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad);
  755. crc = crc32(UBI_CRC32_INIT, buf, len);
  756. vid_hdr->vol_type = UBI_VID_STATIC;
  757. vid_hdr->data_size = cpu_to_ubi32(len);
  758. vid_hdr->copy_flag = 1;
  759. vid_hdr->data_crc = cpu_to_ubi32(crc);
  760. retry:
  761. pnum = ubi_wl_get_peb(ubi, dtype);
  762. if (pnum < 0) {
  763. ubi_free_vid_hdr(ubi, vid_hdr);
  764. leb_write_unlock(ubi, vol_id, lnum);
  765. return pnum;
  766. }
  767. dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
  768. vol_id, lnum, vol->eba_tbl[lnum], pnum);
  769. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  770. if (err) {
  771. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  772. vol_id, lnum, pnum);
  773. goto write_error;
  774. }
  775. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  776. if (err) {
  777. ubi_warn("failed to write %d bytes of data to PEB %d",
  778. len, pnum);
  779. goto write_error;
  780. }
  781. err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
  782. if (err) {
  783. ubi_free_vid_hdr(ubi, vid_hdr);
  784. leb_write_unlock(ubi, vol_id, lnum);
  785. return err;
  786. }
  787. vol->eba_tbl[lnum] = pnum;
  788. leb_write_unlock(ubi, vol_id, lnum);
  789. ubi_free_vid_hdr(ubi, vid_hdr);
  790. return 0;
  791. write_error:
  792. if (err != -EIO || !ubi->bad_allowed) {
  793. /*
  794. * This flash device does not admit of bad eraseblocks or
  795. * something nasty and unexpected happened. Switch to read-only
  796. * mode just in case.
  797. */
  798. ubi_ro_mode(ubi);
  799. leb_write_unlock(ubi, vol_id, lnum);
  800. ubi_free_vid_hdr(ubi, vid_hdr);
  801. return err;
  802. }
  803. err = ubi_wl_put_peb(ubi, pnum, 1);
  804. if (err || ++tries > UBI_IO_RETRIES) {
  805. ubi_ro_mode(ubi);
  806. leb_write_unlock(ubi, vol_id, lnum);
  807. ubi_free_vid_hdr(ubi, vid_hdr);
  808. return err;
  809. }
  810. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  811. ubi_msg("try another PEB");
  812. goto retry;
  813. }
  814. /**
  815. * ltree_entry_ctor - lock tree entries slab cache constructor.
  816. * @obj: the lock-tree entry to construct
  817. * @cache: the lock tree entry slab cache
  818. * @flags: constructor flags
  819. */
  820. static void ltree_entry_ctor(void *obj, struct kmem_cache *cache,
  821. unsigned long flags)
  822. {
  823. struct ltree_entry *le = obj;
  824. if (flags & SLAB_CTOR_CONSTRUCTOR)
  825. return;
  826. le->users = 0;
  827. init_rwsem(&le->mutex);
  828. }
  829. /**
  830. * ubi_eba_copy_leb - copy logical eraseblock.
  831. * @ubi: UBI device description object
  832. * @from: physical eraseblock number from where to copy
  833. * @to: physical eraseblock number where to copy
  834. * @vid_hdr: VID header of the @from physical eraseblock
  835. *
  836. * This function copies logical eraseblock from physical eraseblock @from to
  837. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  838. * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
  839. * was canceled because bit-flips were detected at the target PEB, and a
  840. * negative error code in case of failure.
  841. */
  842. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  843. struct ubi_vid_hdr *vid_hdr)
  844. {
  845. int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
  846. struct ubi_volume *vol;
  847. uint32_t crc;
  848. void *buf, *buf1 = NULL;
  849. vol_id = ubi32_to_cpu(vid_hdr->vol_id);
  850. lnum = ubi32_to_cpu(vid_hdr->lnum);
  851. dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  852. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  853. data_size = ubi32_to_cpu(vid_hdr->data_size);
  854. aldata_size = ALIGN(data_size, ubi->min_io_size);
  855. } else
  856. data_size = aldata_size =
  857. ubi->leb_size - ubi32_to_cpu(vid_hdr->data_pad);
  858. buf = kmalloc(aldata_size, GFP_KERNEL);
  859. if (!buf)
  860. return -ENOMEM;
  861. /*
  862. * We do not want anybody to write to this logical eraseblock while we
  863. * are moving it, so we lock it.
  864. */
  865. err = leb_write_lock(ubi, vol_id, lnum);
  866. if (err) {
  867. kfree(buf);
  868. return err;
  869. }
  870. /*
  871. * But the logical eraseblock might have been put by this time.
  872. * Cancel if it is true.
  873. */
  874. idx = vol_id2idx(ubi, vol_id);
  875. /*
  876. * We may race with volume deletion/re-size, so we have to hold
  877. * @ubi->volumes_lock.
  878. */
  879. spin_lock(&ubi->volumes_lock);
  880. vol = ubi->volumes[idx];
  881. if (!vol) {
  882. dbg_eba("volume %d was removed meanwhile", vol_id);
  883. spin_unlock(&ubi->volumes_lock);
  884. goto out_unlock;
  885. }
  886. pnum = vol->eba_tbl[lnum];
  887. if (pnum != from) {
  888. dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
  889. "PEB %d, cancel", vol_id, lnum, from, pnum);
  890. spin_unlock(&ubi->volumes_lock);
  891. goto out_unlock;
  892. }
  893. spin_unlock(&ubi->volumes_lock);
  894. /* OK, now the LEB is locked and we can safely start moving it */
  895. dbg_eba("read %d bytes of data", aldata_size);
  896. err = ubi_io_read_data(ubi, buf, from, 0, aldata_size);
  897. if (err && err != UBI_IO_BITFLIPS) {
  898. ubi_warn("error %d while reading data from PEB %d",
  899. err, from);
  900. goto out_unlock;
  901. }
  902. /*
  903. * Now we have got to calculate how much data we have to to copy. In
  904. * case of a static volume it is fairly easy - the VID header contains
  905. * the data size. In case of a dynamic volume it is more difficult - we
  906. * have to read the contents, cut 0xFF bytes from the end and copy only
  907. * the first part. We must do this to avoid writing 0xFF bytes as it
  908. * may have some side-effects. And not only this. It is important not
  909. * to include those 0xFFs to CRC because later the they may be filled
  910. * by data.
  911. */
  912. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  913. aldata_size = data_size =
  914. ubi_calc_data_len(ubi, buf, data_size);
  915. cond_resched();
  916. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  917. cond_resched();
  918. /*
  919. * It may turn out to me that the whole @from physical eraseblock
  920. * contains only 0xFF bytes. Then we have to only write the VID header
  921. * and do not write any data. This also means we should not set
  922. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  923. */
  924. if (data_size > 0) {
  925. vid_hdr->copy_flag = 1;
  926. vid_hdr->data_size = cpu_to_ubi32(data_size);
  927. vid_hdr->data_crc = cpu_to_ubi32(crc);
  928. }
  929. vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi));
  930. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  931. if (err)
  932. goto out_unlock;
  933. cond_resched();
  934. /* Read the VID header back and check if it was written correctly */
  935. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  936. if (err) {
  937. if (err != UBI_IO_BITFLIPS)
  938. ubi_warn("cannot read VID header back from PEB %d", to);
  939. goto out_unlock;
  940. }
  941. if (data_size > 0) {
  942. err = ubi_io_write_data(ubi, buf, to, 0, aldata_size);
  943. if (err)
  944. goto out_unlock;
  945. /*
  946. * We've written the data and are going to read it back to make
  947. * sure it was written correctly.
  948. */
  949. buf1 = kmalloc(aldata_size, GFP_KERNEL);
  950. if (!buf1) {
  951. err = -ENOMEM;
  952. goto out_unlock;
  953. }
  954. cond_resched();
  955. err = ubi_io_read_data(ubi, buf1, to, 0, aldata_size);
  956. if (err) {
  957. if (err != UBI_IO_BITFLIPS)
  958. ubi_warn("cannot read data back from PEB %d",
  959. to);
  960. goto out_unlock;
  961. }
  962. cond_resched();
  963. if (memcmp(buf, buf1, aldata_size)) {
  964. ubi_warn("read data back from PEB %d - it is different",
  965. to);
  966. goto out_unlock;
  967. }
  968. }
  969. ubi_assert(vol->eba_tbl[lnum] == from);
  970. vol->eba_tbl[lnum] = to;
  971. leb_write_unlock(ubi, vol_id, lnum);
  972. kfree(buf);
  973. kfree(buf1);
  974. return 0;
  975. out_unlock:
  976. leb_write_unlock(ubi, vol_id, lnum);
  977. kfree(buf);
  978. kfree(buf1);
  979. return err;
  980. }
  981. /**
  982. * ubi_eba_init_scan - initialize the EBA unit using scanning information.
  983. * @ubi: UBI device description object
  984. * @si: scanning information
  985. *
  986. * This function returns zero in case of success and a negative error code in
  987. * case of failure.
  988. */
  989. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  990. {
  991. int i, j, err, num_volumes;
  992. struct ubi_scan_volume *sv;
  993. struct ubi_volume *vol;
  994. struct ubi_scan_leb *seb;
  995. struct rb_node *rb;
  996. dbg_eba("initialize EBA unit");
  997. spin_lock_init(&ubi->ltree_lock);
  998. ubi->ltree = RB_ROOT;
  999. if (ubi_devices_cnt == 0) {
  1000. ltree_slab = kmem_cache_create("ubi_ltree_slab",
  1001. sizeof(struct ltree_entry), 0,
  1002. 0, &ltree_entry_ctor, NULL);
  1003. if (!ltree_slab)
  1004. return -ENOMEM;
  1005. }
  1006. ubi->global_sqnum = si->max_sqnum + 1;
  1007. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1008. for (i = 0; i < num_volumes; i++) {
  1009. vol = ubi->volumes[i];
  1010. if (!vol)
  1011. continue;
  1012. cond_resched();
  1013. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  1014. GFP_KERNEL);
  1015. if (!vol->eba_tbl) {
  1016. err = -ENOMEM;
  1017. goto out_free;
  1018. }
  1019. for (j = 0; j < vol->reserved_pebs; j++)
  1020. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  1021. sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
  1022. if (!sv)
  1023. continue;
  1024. ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
  1025. if (seb->lnum >= vol->reserved_pebs)
  1026. /*
  1027. * This may happen in case of an unclean reboot
  1028. * during re-size.
  1029. */
  1030. ubi_scan_move_to_list(sv, seb, &si->erase);
  1031. vol->eba_tbl[seb->lnum] = seb->pnum;
  1032. }
  1033. }
  1034. if (ubi->bad_allowed) {
  1035. ubi_calculate_reserved(ubi);
  1036. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1037. /* No enough free physical eraseblocks */
  1038. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1039. ubi_warn("cannot reserve enough PEBs for bad PEB "
  1040. "handling, reserved %d, need %d",
  1041. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1042. } else
  1043. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1044. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1045. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1046. }
  1047. dbg_eba("EBA unit is initialized");
  1048. return 0;
  1049. out_free:
  1050. for (i = 0; i < num_volumes; i++) {
  1051. if (!ubi->volumes[i])
  1052. continue;
  1053. kfree(ubi->volumes[i]->eba_tbl);
  1054. }
  1055. if (ubi_devices_cnt == 0)
  1056. kmem_cache_destroy(ltree_slab);
  1057. return err;
  1058. }
  1059. /**
  1060. * ubi_eba_close - close EBA unit.
  1061. * @ubi: UBI device description object
  1062. */
  1063. void ubi_eba_close(const struct ubi_device *ubi)
  1064. {
  1065. int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1066. dbg_eba("close EBA unit");
  1067. for (i = 0; i < num_volumes; i++) {
  1068. if (!ubi->volumes[i])
  1069. continue;
  1070. kfree(ubi->volumes[i]->eba_tbl);
  1071. }
  1072. if (ubi_devices_cnt == 1)
  1073. kmem_cache_destroy(ltree_slab);
  1074. }