eba.c 33 KB

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