eba.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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(struct kmem_cache *cache, void *obj)
  816. {
  817. struct ltree_entry *le = obj;
  818. le->users = 0;
  819. init_rwsem(&le->mutex);
  820. }
  821. /**
  822. * ubi_eba_copy_leb - copy logical eraseblock.
  823. * @ubi: UBI device description object
  824. * @from: physical eraseblock number from where to copy
  825. * @to: physical eraseblock number where to copy
  826. * @vid_hdr: VID header of the @from physical eraseblock
  827. *
  828. * This function copies logical eraseblock from physical eraseblock @from to
  829. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  830. * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
  831. * was canceled because bit-flips were detected at the target PEB, and a
  832. * negative error code in case of failure.
  833. */
  834. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  835. struct ubi_vid_hdr *vid_hdr)
  836. {
  837. int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
  838. struct ubi_volume *vol;
  839. uint32_t crc;
  840. vol_id = be32_to_cpu(vid_hdr->vol_id);
  841. lnum = be32_to_cpu(vid_hdr->lnum);
  842. dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  843. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  844. data_size = be32_to_cpu(vid_hdr->data_size);
  845. aldata_size = ALIGN(data_size, ubi->min_io_size);
  846. } else
  847. data_size = aldata_size =
  848. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  849. /*
  850. * We do not want anybody to write to this logical eraseblock while we
  851. * are moving it, so we lock it.
  852. */
  853. err = leb_write_lock(ubi, vol_id, lnum);
  854. if (err)
  855. return err;
  856. mutex_lock(&ubi->buf_mutex);
  857. /*
  858. * But the logical eraseblock might have been put by this time.
  859. * Cancel if it is true.
  860. */
  861. idx = vol_id2idx(ubi, vol_id);
  862. /*
  863. * We may race with volume deletion/re-size, so we have to hold
  864. * @ubi->volumes_lock.
  865. */
  866. spin_lock(&ubi->volumes_lock);
  867. vol = ubi->volumes[idx];
  868. if (!vol) {
  869. dbg_eba("volume %d was removed meanwhile", vol_id);
  870. spin_unlock(&ubi->volumes_lock);
  871. goto out_unlock;
  872. }
  873. pnum = vol->eba_tbl[lnum];
  874. if (pnum != from) {
  875. dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
  876. "PEB %d, cancel", vol_id, lnum, from, pnum);
  877. spin_unlock(&ubi->volumes_lock);
  878. goto out_unlock;
  879. }
  880. spin_unlock(&ubi->volumes_lock);
  881. /* OK, now the LEB is locked and we can safely start moving it */
  882. dbg_eba("read %d bytes of data", aldata_size);
  883. err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
  884. if (err && err != UBI_IO_BITFLIPS) {
  885. ubi_warn("error %d while reading data from PEB %d",
  886. err, from);
  887. goto out_unlock;
  888. }
  889. /*
  890. * Now we have got to calculate how much data we have to to copy. In
  891. * case of a static volume it is fairly easy - the VID header contains
  892. * the data size. In case of a dynamic volume it is more difficult - we
  893. * have to read the contents, cut 0xFF bytes from the end and copy only
  894. * the first part. We must do this to avoid writing 0xFF bytes as it
  895. * may have some side-effects. And not only this. It is important not
  896. * to include those 0xFFs to CRC because later the they may be filled
  897. * by data.
  898. */
  899. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  900. aldata_size = data_size =
  901. ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
  902. cond_resched();
  903. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
  904. cond_resched();
  905. /*
  906. * It may turn out to me that the whole @from physical eraseblock
  907. * contains only 0xFF bytes. Then we have to only write the VID header
  908. * and do not write any data. This also means we should not set
  909. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  910. */
  911. if (data_size > 0) {
  912. vid_hdr->copy_flag = 1;
  913. vid_hdr->data_size = cpu_to_be32(data_size);
  914. vid_hdr->data_crc = cpu_to_be32(crc);
  915. }
  916. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  917. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  918. if (err)
  919. goto out_unlock;
  920. cond_resched();
  921. /* Read the VID header back and check if it was written correctly */
  922. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  923. if (err) {
  924. if (err != UBI_IO_BITFLIPS)
  925. ubi_warn("cannot read VID header back from PEB %d", to);
  926. goto out_unlock;
  927. }
  928. if (data_size > 0) {
  929. err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
  930. if (err)
  931. goto out_unlock;
  932. cond_resched();
  933. /*
  934. * We've written the data and are going to read it back to make
  935. * sure it was written correctly.
  936. */
  937. err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
  938. if (err) {
  939. if (err != UBI_IO_BITFLIPS)
  940. ubi_warn("cannot read data back from PEB %d",
  941. to);
  942. goto out_unlock;
  943. }
  944. cond_resched();
  945. if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
  946. ubi_warn("read data back from PEB %d - it is different",
  947. to);
  948. goto out_unlock;
  949. }
  950. }
  951. ubi_assert(vol->eba_tbl[lnum] == from);
  952. vol->eba_tbl[lnum] = to;
  953. out_unlock:
  954. mutex_unlock(&ubi->buf_mutex);
  955. leb_write_unlock(ubi, vol_id, lnum);
  956. return err;
  957. }
  958. /**
  959. * ubi_eba_init_scan - initialize the EBA unit using scanning information.
  960. * @ubi: UBI device description object
  961. * @si: scanning information
  962. *
  963. * This function returns zero in case of success and a negative error code in
  964. * case of failure.
  965. */
  966. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  967. {
  968. int i, j, err, num_volumes;
  969. struct ubi_scan_volume *sv;
  970. struct ubi_volume *vol;
  971. struct ubi_scan_leb *seb;
  972. struct rb_node *rb;
  973. dbg_eba("initialize EBA unit");
  974. spin_lock_init(&ubi->ltree_lock);
  975. mutex_init(&ubi->alc_mutex);
  976. ubi->ltree = RB_ROOT;
  977. if (ubi_devices_cnt == 0) {
  978. ltree_slab = kmem_cache_create("ubi_ltree_slab",
  979. sizeof(struct ltree_entry), 0,
  980. 0, &ltree_entry_ctor);
  981. if (!ltree_slab)
  982. return -ENOMEM;
  983. }
  984. ubi->global_sqnum = si->max_sqnum + 1;
  985. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  986. for (i = 0; i < num_volumes; i++) {
  987. vol = ubi->volumes[i];
  988. if (!vol)
  989. continue;
  990. cond_resched();
  991. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  992. GFP_KERNEL);
  993. if (!vol->eba_tbl) {
  994. err = -ENOMEM;
  995. goto out_free;
  996. }
  997. for (j = 0; j < vol->reserved_pebs; j++)
  998. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  999. sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
  1000. if (!sv)
  1001. continue;
  1002. ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
  1003. if (seb->lnum >= vol->reserved_pebs)
  1004. /*
  1005. * This may happen in case of an unclean reboot
  1006. * during re-size.
  1007. */
  1008. ubi_scan_move_to_list(sv, seb, &si->erase);
  1009. vol->eba_tbl[seb->lnum] = seb->pnum;
  1010. }
  1011. }
  1012. if (ubi->bad_allowed) {
  1013. ubi_calculate_reserved(ubi);
  1014. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1015. /* No enough free physical eraseblocks */
  1016. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1017. ubi_warn("cannot reserve enough PEBs for bad PEB "
  1018. "handling, reserved %d, need %d",
  1019. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1020. } else
  1021. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1022. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1023. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1024. }
  1025. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1026. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1027. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1028. err = -ENOSPC;
  1029. goto out_free;
  1030. }
  1031. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1032. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1033. dbg_eba("EBA unit is initialized");
  1034. return 0;
  1035. out_free:
  1036. for (i = 0; i < num_volumes; i++) {
  1037. if (!ubi->volumes[i])
  1038. continue;
  1039. kfree(ubi->volumes[i]->eba_tbl);
  1040. }
  1041. if (ubi_devices_cnt == 0)
  1042. kmem_cache_destroy(ltree_slab);
  1043. return err;
  1044. }
  1045. /**
  1046. * ubi_eba_close - close EBA unit.
  1047. * @ubi: UBI device description object
  1048. */
  1049. void ubi_eba_close(const struct ubi_device *ubi)
  1050. {
  1051. int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1052. dbg_eba("close EBA unit");
  1053. for (i = 0; i < num_volumes; i++) {
  1054. if (!ubi->volumes[i])
  1055. continue;
  1056. kfree(ubi->volumes[i]->eba_tbl);
  1057. }
  1058. if (ubi_devices_cnt == 1)
  1059. kmem_cache_destroy(ltree_slab);
  1060. }