eba.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. if (len) {
  577. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  578. if (err) {
  579. ubi_warn("failed to write %d bytes at offset %d of "
  580. "LEB %d:%d, PEB %d", len, offset, vol_id,
  581. lnum, pnum);
  582. goto write_error;
  583. }
  584. }
  585. vol->eba_tbl[lnum] = pnum;
  586. leb_write_unlock(ubi, vol_id, lnum);
  587. ubi_free_vid_hdr(ubi, vid_hdr);
  588. return 0;
  589. write_error:
  590. if (err != -EIO || !ubi->bad_allowed) {
  591. ubi_ro_mode(ubi);
  592. leb_write_unlock(ubi, vol_id, lnum);
  593. ubi_free_vid_hdr(ubi, vid_hdr);
  594. return err;
  595. }
  596. /*
  597. * Fortunately, this is the first write operation to this physical
  598. * eraseblock, so just put it and request a new one. We assume that if
  599. * this physical eraseblock went bad, the erase code will handle that.
  600. */
  601. err = ubi_wl_put_peb(ubi, pnum, 1);
  602. if (err || ++tries > UBI_IO_RETRIES) {
  603. ubi_ro_mode(ubi);
  604. leb_write_unlock(ubi, vol_id, lnum);
  605. ubi_free_vid_hdr(ubi, vid_hdr);
  606. return err;
  607. }
  608. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  609. ubi_msg("try another PEB");
  610. goto retry;
  611. }
  612. /**
  613. * ubi_eba_write_leb_st - write data to static volume.
  614. * @ubi: UBI device description object
  615. * @vol_id: volume ID
  616. * @lnum: logical eraseblock number
  617. * @buf: data to write
  618. * @len: how many bytes to write
  619. * @dtype: data type
  620. * @used_ebs: how many logical eraseblocks will this volume contain
  621. *
  622. * This function writes data to logical eraseblock @lnum of static volume
  623. * @vol_id. The @used_ebs argument should contain total number of logical
  624. * eraseblock in this static volume.
  625. *
  626. * When writing to the last logical eraseblock, the @len argument doesn't have
  627. * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
  628. * to the real data size, although the @buf buffer has to contain the
  629. * alignment. In all other cases, @len has to be aligned.
  630. *
  631. * It is prohibited to write more then once to logical eraseblocks of static
  632. * volumes. This function returns zero in case of success and a negative error
  633. * code in case of failure.
  634. */
  635. int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum,
  636. const void *buf, int len, int dtype, int used_ebs)
  637. {
  638. int err, pnum, tries = 0, data_size = len;
  639. int idx = vol_id2idx(ubi, vol_id);
  640. struct ubi_volume *vol = ubi->volumes[idx];
  641. struct ubi_vid_hdr *vid_hdr;
  642. uint32_t crc;
  643. if (ubi->ro_mode)
  644. return -EROFS;
  645. if (lnum == used_ebs - 1)
  646. /* If this is the last LEB @len may be unaligned */
  647. len = ALIGN(data_size, ubi->min_io_size);
  648. else
  649. ubi_assert(len % ubi->min_io_size == 0);
  650. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  651. if (!vid_hdr)
  652. return -ENOMEM;
  653. err = leb_write_lock(ubi, vol_id, lnum);
  654. if (err) {
  655. ubi_free_vid_hdr(ubi, vid_hdr);
  656. return err;
  657. }
  658. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  659. vid_hdr->vol_id = cpu_to_be32(vol_id);
  660. vid_hdr->lnum = cpu_to_be32(lnum);
  661. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  662. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  663. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  664. vid_hdr->vol_type = UBI_VID_STATIC;
  665. vid_hdr->data_size = cpu_to_be32(data_size);
  666. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  667. vid_hdr->data_crc = cpu_to_be32(crc);
  668. retry:
  669. pnum = ubi_wl_get_peb(ubi, dtype);
  670. if (pnum < 0) {
  671. ubi_free_vid_hdr(ubi, vid_hdr);
  672. leb_write_unlock(ubi, vol_id, lnum);
  673. return pnum;
  674. }
  675. dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
  676. len, vol_id, lnum, pnum, used_ebs);
  677. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  678. if (err) {
  679. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  680. vol_id, lnum, pnum);
  681. goto write_error;
  682. }
  683. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  684. if (err) {
  685. ubi_warn("failed to write %d bytes of data to PEB %d",
  686. len, pnum);
  687. goto write_error;
  688. }
  689. ubi_assert(vol->eba_tbl[lnum] < 0);
  690. vol->eba_tbl[lnum] = pnum;
  691. leb_write_unlock(ubi, vol_id, lnum);
  692. ubi_free_vid_hdr(ubi, vid_hdr);
  693. return 0;
  694. write_error:
  695. if (err != -EIO || !ubi->bad_allowed) {
  696. /*
  697. * This flash device does not admit of bad eraseblocks or
  698. * something nasty and unexpected happened. Switch to read-only
  699. * mode just in case.
  700. */
  701. ubi_ro_mode(ubi);
  702. leb_write_unlock(ubi, vol_id, lnum);
  703. ubi_free_vid_hdr(ubi, vid_hdr);
  704. return err;
  705. }
  706. err = ubi_wl_put_peb(ubi, pnum, 1);
  707. if (err || ++tries > UBI_IO_RETRIES) {
  708. ubi_ro_mode(ubi);
  709. leb_write_unlock(ubi, vol_id, lnum);
  710. ubi_free_vid_hdr(ubi, vid_hdr);
  711. return err;
  712. }
  713. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  714. ubi_msg("try another PEB");
  715. goto retry;
  716. }
  717. /*
  718. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  719. * @ubi: UBI device description object
  720. * @vol_id: volume ID
  721. * @lnum: logical eraseblock number
  722. * @buf: data to write
  723. * @len: how many bytes to write
  724. * @dtype: data type
  725. *
  726. * This function changes the contents of a logical eraseblock atomically. @buf
  727. * has to contain new logical eraseblock data, and @len - the length of the
  728. * data, which has to be aligned. This function guarantees that in case of an
  729. * unclean reboot the old contents is preserved. Returns zero in case of
  730. * success and a negative error code in case of failure.
  731. *
  732. * UBI reserves one LEB for the "atomic LEB change" operation, so only one
  733. * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
  734. */
  735. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
  736. const void *buf, int len, int dtype)
  737. {
  738. int err, pnum, tries = 0, idx = vol_id2idx(ubi, vol_id);
  739. struct ubi_volume *vol = ubi->volumes[idx];
  740. struct ubi_vid_hdr *vid_hdr;
  741. uint32_t crc;
  742. if (ubi->ro_mode)
  743. return -EROFS;
  744. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  745. if (!vid_hdr)
  746. return -ENOMEM;
  747. mutex_lock(&ubi->alc_mutex);
  748. err = leb_write_lock(ubi, vol_id, lnum);
  749. if (err)
  750. goto out_mutex;
  751. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  752. vid_hdr->vol_id = cpu_to_be32(vol_id);
  753. vid_hdr->lnum = cpu_to_be32(lnum);
  754. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  755. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  756. crc = crc32(UBI_CRC32_INIT, buf, len);
  757. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  758. vid_hdr->data_size = cpu_to_be32(len);
  759. vid_hdr->copy_flag = 1;
  760. vid_hdr->data_crc = cpu_to_be32(crc);
  761. retry:
  762. pnum = ubi_wl_get_peb(ubi, dtype);
  763. if (pnum < 0) {
  764. err = pnum;
  765. goto out_leb_unlock;
  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. if (vol->eba_tbl[lnum] >= 0) {
  782. err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
  783. if (err)
  784. goto out_leb_unlock;
  785. }
  786. vol->eba_tbl[lnum] = pnum;
  787. out_leb_unlock:
  788. leb_write_unlock(ubi, vol_id, lnum);
  789. out_mutex:
  790. mutex_unlock(&ubi->alc_mutex);
  791. ubi_free_vid_hdr(ubi, vid_hdr);
  792. return err;
  793. write_error:
  794. if (err != -EIO || !ubi->bad_allowed) {
  795. /*
  796. * This flash device does not admit of bad eraseblocks or
  797. * something nasty and unexpected happened. Switch to read-only
  798. * mode just in case.
  799. */
  800. ubi_ro_mode(ubi);
  801. goto out_leb_unlock;
  802. }
  803. err = ubi_wl_put_peb(ubi, pnum, 1);
  804. if (err || ++tries > UBI_IO_RETRIES) {
  805. ubi_ro_mode(ubi);
  806. goto out_leb_unlock;
  807. }
  808. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  809. ubi_msg("try another PEB");
  810. goto retry;
  811. }
  812. /**
  813. * ltree_entry_ctor - lock tree entries slab cache constructor.
  814. * @obj: the lock-tree entry to construct
  815. * @cache: the lock tree entry slab cache
  816. * @flags: constructor flags
  817. */
  818. static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
  819. {
  820. struct ltree_entry *le = obj;
  821. le->users = 0;
  822. init_rwsem(&le->mutex);
  823. }
  824. /**
  825. * ubi_eba_copy_leb - copy logical eraseblock.
  826. * @ubi: UBI device description object
  827. * @from: physical eraseblock number from where to copy
  828. * @to: physical eraseblock number where to copy
  829. * @vid_hdr: VID header of the @from physical eraseblock
  830. *
  831. * This function copies logical eraseblock from physical eraseblock @from to
  832. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  833. * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation
  834. * was canceled because bit-flips were detected at the target PEB, and a
  835. * negative error code in case of failure.
  836. */
  837. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  838. struct ubi_vid_hdr *vid_hdr)
  839. {
  840. int err, vol_id, lnum, data_size, aldata_size, pnum, idx;
  841. struct ubi_volume *vol;
  842. uint32_t crc;
  843. vol_id = be32_to_cpu(vid_hdr->vol_id);
  844. lnum = be32_to_cpu(vid_hdr->lnum);
  845. dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  846. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  847. data_size = be32_to_cpu(vid_hdr->data_size);
  848. aldata_size = ALIGN(data_size, ubi->min_io_size);
  849. } else
  850. data_size = aldata_size =
  851. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  852. /*
  853. * We do not want anybody to write to this logical eraseblock while we
  854. * are moving it, so we lock it.
  855. */
  856. err = leb_write_lock(ubi, vol_id, lnum);
  857. if (err)
  858. return err;
  859. mutex_lock(&ubi->buf_mutex);
  860. /*
  861. * But the logical eraseblock might have been put by this time.
  862. * Cancel if it is true.
  863. */
  864. idx = vol_id2idx(ubi, vol_id);
  865. /*
  866. * We may race with volume deletion/re-size, so we have to hold
  867. * @ubi->volumes_lock.
  868. */
  869. spin_lock(&ubi->volumes_lock);
  870. vol = ubi->volumes[idx];
  871. if (!vol) {
  872. dbg_eba("volume %d was removed meanwhile", vol_id);
  873. spin_unlock(&ubi->volumes_lock);
  874. goto out_unlock;
  875. }
  876. pnum = vol->eba_tbl[lnum];
  877. if (pnum != from) {
  878. dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
  879. "PEB %d, cancel", vol_id, lnum, from, pnum);
  880. spin_unlock(&ubi->volumes_lock);
  881. goto out_unlock;
  882. }
  883. spin_unlock(&ubi->volumes_lock);
  884. /* OK, now the LEB is locked and we can safely start moving it */
  885. dbg_eba("read %d bytes of data", aldata_size);
  886. err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
  887. if (err && err != UBI_IO_BITFLIPS) {
  888. ubi_warn("error %d while reading data from PEB %d",
  889. err, from);
  890. goto out_unlock;
  891. }
  892. /*
  893. * Now we have got to calculate how much data we have to to copy. In
  894. * case of a static volume it is fairly easy - the VID header contains
  895. * the data size. In case of a dynamic volume it is more difficult - we
  896. * have to read the contents, cut 0xFF bytes from the end and copy only
  897. * the first part. We must do this to avoid writing 0xFF bytes as it
  898. * may have some side-effects. And not only this. It is important not
  899. * to include those 0xFFs to CRC because later the they may be filled
  900. * by data.
  901. */
  902. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  903. aldata_size = data_size =
  904. ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
  905. cond_resched();
  906. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
  907. cond_resched();
  908. /*
  909. * It may turn out to me that the whole @from physical eraseblock
  910. * contains only 0xFF bytes. Then we have to only write the VID header
  911. * and do not write any data. This also means we should not set
  912. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  913. */
  914. if (data_size > 0) {
  915. vid_hdr->copy_flag = 1;
  916. vid_hdr->data_size = cpu_to_be32(data_size);
  917. vid_hdr->data_crc = cpu_to_be32(crc);
  918. }
  919. vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
  920. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  921. if (err)
  922. goto out_unlock;
  923. cond_resched();
  924. /* Read the VID header back and check if it was written correctly */
  925. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  926. if (err) {
  927. if (err != UBI_IO_BITFLIPS)
  928. ubi_warn("cannot read VID header back from PEB %d", to);
  929. goto out_unlock;
  930. }
  931. if (data_size > 0) {
  932. err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
  933. if (err)
  934. goto out_unlock;
  935. cond_resched();
  936. /*
  937. * We've written the data and are going to read it back to make
  938. * sure it was written correctly.
  939. */
  940. err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
  941. if (err) {
  942. if (err != UBI_IO_BITFLIPS)
  943. ubi_warn("cannot read data back from PEB %d",
  944. to);
  945. goto out_unlock;
  946. }
  947. cond_resched();
  948. if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
  949. ubi_warn("read data back from PEB %d - it is different",
  950. to);
  951. goto out_unlock;
  952. }
  953. }
  954. ubi_assert(vol->eba_tbl[lnum] == from);
  955. vol->eba_tbl[lnum] = to;
  956. out_unlock:
  957. mutex_unlock(&ubi->buf_mutex);
  958. leb_write_unlock(ubi, vol_id, lnum);
  959. return err;
  960. }
  961. /**
  962. * ubi_eba_init_scan - initialize the EBA unit using scanning information.
  963. * @ubi: UBI device description object
  964. * @si: scanning information
  965. *
  966. * This function returns zero in case of success and a negative error code in
  967. * case of failure.
  968. */
  969. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
  970. {
  971. int i, j, err, num_volumes;
  972. struct ubi_scan_volume *sv;
  973. struct ubi_volume *vol;
  974. struct ubi_scan_leb *seb;
  975. struct rb_node *rb;
  976. dbg_eba("initialize EBA unit");
  977. spin_lock_init(&ubi->ltree_lock);
  978. mutex_init(&ubi->alc_mutex);
  979. ubi->ltree = RB_ROOT;
  980. if (ubi_devices_cnt == 0) {
  981. ltree_slab = kmem_cache_create("ubi_ltree_slab",
  982. sizeof(struct ltree_entry), 0,
  983. 0, &ltree_entry_ctor);
  984. if (!ltree_slab)
  985. return -ENOMEM;
  986. }
  987. ubi->global_sqnum = si->max_sqnum + 1;
  988. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  989. for (i = 0; i < num_volumes; i++) {
  990. vol = ubi->volumes[i];
  991. if (!vol)
  992. continue;
  993. cond_resched();
  994. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  995. GFP_KERNEL);
  996. if (!vol->eba_tbl) {
  997. err = -ENOMEM;
  998. goto out_free;
  999. }
  1000. for (j = 0; j < vol->reserved_pebs; j++)
  1001. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  1002. sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
  1003. if (!sv)
  1004. continue;
  1005. ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
  1006. if (seb->lnum >= vol->reserved_pebs)
  1007. /*
  1008. * This may happen in case of an unclean reboot
  1009. * during re-size.
  1010. */
  1011. ubi_scan_move_to_list(sv, seb, &si->erase);
  1012. vol->eba_tbl[seb->lnum] = seb->pnum;
  1013. }
  1014. }
  1015. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1016. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1017. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1018. err = -ENOSPC;
  1019. goto out_free;
  1020. }
  1021. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1022. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1023. if (ubi->bad_allowed) {
  1024. ubi_calculate_reserved(ubi);
  1025. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1026. /* No enough free physical eraseblocks */
  1027. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1028. ubi_warn("cannot reserve enough PEBs for bad PEB "
  1029. "handling, reserved %d, need %d",
  1030. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1031. } else
  1032. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1033. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1034. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1035. }
  1036. dbg_eba("EBA unit is initialized");
  1037. return 0;
  1038. out_free:
  1039. for (i = 0; i < num_volumes; i++) {
  1040. if (!ubi->volumes[i])
  1041. continue;
  1042. kfree(ubi->volumes[i]->eba_tbl);
  1043. }
  1044. if (ubi_devices_cnt == 0)
  1045. kmem_cache_destroy(ltree_slab);
  1046. return err;
  1047. }
  1048. /**
  1049. * ubi_eba_close - close EBA unit.
  1050. * @ubi: UBI device description object
  1051. */
  1052. void ubi_eba_close(const struct ubi_device *ubi)
  1053. {
  1054. int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1055. dbg_eba("close EBA unit");
  1056. for (i = 0; i < num_volumes; i++) {
  1057. if (!ubi->volumes[i])
  1058. continue;
  1059. kfree(ubi->volumes[i]->eba_tbl);
  1060. }
  1061. if (ubi_devices_cnt == 1)
  1062. kmem_cache_destroy(ltree_slab);
  1063. }