eba.c 32 KB

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