eba.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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) sub-system.
  22. *
  23. * This sub-system 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 sub-system implements per-logical eraseblock locking. Before
  30. * accessing a logical eraseblock it is locked for reading or writing. The
  31. * per-logical eraseblock locking is implemented by means of the lock tree. The
  32. * lock tree is an RB-tree which refers all the currently locked logical
  33. * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
  34. * They are indexed by (@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. unsigned long long ubi_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_VOLUME_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. kfree(le_free);
  171. return le;
  172. }
  173. /**
  174. * leb_read_lock - lock logical eraseblock for reading.
  175. * @ubi: UBI device description object
  176. * @vol_id: volume ID
  177. * @lnum: logical eraseblock number
  178. *
  179. * This function locks a logical eraseblock for reading. Returns zero in case
  180. * of success and a negative error code in case of failure.
  181. */
  182. static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
  183. {
  184. struct ubi_ltree_entry *le;
  185. le = ltree_add_entry(ubi, vol_id, lnum);
  186. if (IS_ERR(le))
  187. return PTR_ERR(le);
  188. down_read(&le->mutex);
  189. return 0;
  190. }
  191. /**
  192. * leb_read_unlock - unlock logical eraseblock.
  193. * @ubi: UBI device description object
  194. * @vol_id: volume ID
  195. * @lnum: logical eraseblock number
  196. */
  197. static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  198. {
  199. struct ubi_ltree_entry *le;
  200. spin_lock(&ubi->ltree_lock);
  201. le = ltree_lookup(ubi, vol_id, lnum);
  202. le->users -= 1;
  203. ubi_assert(le->users >= 0);
  204. up_read(&le->mutex);
  205. if (le->users == 0) {
  206. rb_erase(&le->rb, &ubi->ltree);
  207. kfree(le);
  208. }
  209. spin_unlock(&ubi->ltree_lock);
  210. }
  211. /**
  212. * leb_write_lock - lock logical eraseblock for writing.
  213. * @ubi: UBI device description object
  214. * @vol_id: volume ID
  215. * @lnum: logical eraseblock number
  216. *
  217. * This function locks a logical eraseblock for writing. Returns zero in case
  218. * of success and a negative error code in case of failure.
  219. */
  220. static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
  221. {
  222. struct ubi_ltree_entry *le;
  223. le = ltree_add_entry(ubi, vol_id, lnum);
  224. if (IS_ERR(le))
  225. return PTR_ERR(le);
  226. down_write(&le->mutex);
  227. return 0;
  228. }
  229. /**
  230. * leb_write_lock - lock logical eraseblock for writing.
  231. * @ubi: UBI device description object
  232. * @vol_id: volume ID
  233. * @lnum: logical eraseblock number
  234. *
  235. * This function locks a logical eraseblock for writing if there is no
  236. * contention and does nothing if there is contention. Returns %0 in case of
  237. * success, %1 in case of contention, and and a negative error code in case of
  238. * failure.
  239. */
  240. static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
  241. {
  242. struct ubi_ltree_entry *le;
  243. le = ltree_add_entry(ubi, vol_id, lnum);
  244. if (IS_ERR(le))
  245. return PTR_ERR(le);
  246. if (down_write_trylock(&le->mutex))
  247. return 0;
  248. /* Contention, cancel */
  249. spin_lock(&ubi->ltree_lock);
  250. le->users -= 1;
  251. ubi_assert(le->users >= 0);
  252. if (le->users == 0) {
  253. rb_erase(&le->rb, &ubi->ltree);
  254. kfree(le);
  255. }
  256. spin_unlock(&ubi->ltree_lock);
  257. return 1;
  258. }
  259. /**
  260. * leb_write_unlock - unlock logical eraseblock.
  261. * @ubi: UBI device description object
  262. * @vol_id: volume ID
  263. * @lnum: logical eraseblock number
  264. */
  265. static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
  266. {
  267. struct ubi_ltree_entry *le;
  268. spin_lock(&ubi->ltree_lock);
  269. le = ltree_lookup(ubi, vol_id, lnum);
  270. le->users -= 1;
  271. ubi_assert(le->users >= 0);
  272. up_write(&le->mutex);
  273. if (le->users == 0) {
  274. rb_erase(&le->rb, &ubi->ltree);
  275. kfree(le);
  276. }
  277. spin_unlock(&ubi->ltree_lock);
  278. }
  279. /**
  280. * ubi_eba_unmap_leb - un-map logical eraseblock.
  281. * @ubi: UBI device description object
  282. * @vol: volume description object
  283. * @lnum: logical eraseblock number
  284. *
  285. * This function un-maps logical eraseblock @lnum and schedules corresponding
  286. * physical eraseblock for erasure. Returns zero in case of success and a
  287. * negative error code in case of failure.
  288. */
  289. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  290. int lnum)
  291. {
  292. int err, pnum, vol_id = vol->vol_id;
  293. if (ubi->ro_mode)
  294. return -EROFS;
  295. err = leb_write_lock(ubi, vol_id, lnum);
  296. if (err)
  297. return err;
  298. pnum = vol->eba_tbl[lnum];
  299. if (pnum < 0)
  300. /* This logical eraseblock is already unmapped */
  301. goto out_unlock;
  302. dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
  303. vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
  304. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
  305. out_unlock:
  306. leb_write_unlock(ubi, vol_id, lnum);
  307. return err;
  308. }
  309. /**
  310. * ubi_eba_read_leb - read data.
  311. * @ubi: UBI device description object
  312. * @vol: volume description object
  313. * @lnum: logical eraseblock number
  314. * @buf: buffer to store the read data
  315. * @offset: offset from where to read
  316. * @len: how many bytes to read
  317. * @check: data CRC check flag
  318. *
  319. * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
  320. * bytes. The @check flag only makes sense for static volumes and forces
  321. * eraseblock data CRC checking.
  322. *
  323. * In case of success this function returns zero. In case of a static volume,
  324. * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
  325. * returned for any volume type if an ECC error was detected by the MTD device
  326. * driver. Other negative error cored may be returned in case of other errors.
  327. */
  328. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  329. void *buf, int offset, int len, int check)
  330. {
  331. int err, pnum, scrub = 0, vol_id = vol->vol_id;
  332. struct ubi_vid_hdr *vid_hdr;
  333. uint32_t uninitialized_var(crc);
  334. err = leb_read_lock(ubi, vol_id, lnum);
  335. if (err)
  336. return err;
  337. pnum = vol->eba_tbl[lnum];
  338. if (pnum < 0) {
  339. /*
  340. * The logical eraseblock is not mapped, fill the whole buffer
  341. * with 0xFF bytes. The exception is static volumes for which
  342. * it is an error to read unmapped logical eraseblocks.
  343. */
  344. dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
  345. len, offset, vol_id, lnum);
  346. leb_read_unlock(ubi, vol_id, lnum);
  347. ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
  348. memset(buf, 0xFF, len);
  349. return 0;
  350. }
  351. dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
  352. len, offset, vol_id, lnum, pnum);
  353. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  354. check = 0;
  355. retry:
  356. if (check) {
  357. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  358. if (!vid_hdr) {
  359. err = -ENOMEM;
  360. goto out_unlock;
  361. }
  362. err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
  363. if (err && err != UBI_IO_BITFLIPS) {
  364. if (err > 0) {
  365. /*
  366. * The header is either absent or corrupted.
  367. * The former case means there is a bug -
  368. * switch to read-only mode just in case.
  369. * The latter case means a real corruption - we
  370. * may try to recover data. FIXME: but this is
  371. * not implemented.
  372. */
  373. if (err == UBI_IO_BAD_HDR_EBADMSG ||
  374. err == UBI_IO_BAD_HDR) {
  375. ubi_warn("corrupted VID header at PEB %d, LEB %d:%d",
  376. pnum, vol_id, lnum);
  377. err = -EBADMSG;
  378. } else
  379. ubi_ro_mode(ubi);
  380. }
  381. goto out_free;
  382. } else if (err == UBI_IO_BITFLIPS)
  383. scrub = 1;
  384. ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
  385. ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
  386. crc = be32_to_cpu(vid_hdr->data_crc);
  387. ubi_free_vid_hdr(ubi, vid_hdr);
  388. }
  389. err = ubi_io_read_data(ubi, buf, pnum, offset, len);
  390. if (err) {
  391. if (err == UBI_IO_BITFLIPS) {
  392. scrub = 1;
  393. err = 0;
  394. } else if (mtd_is_eccerr(err)) {
  395. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  396. goto out_unlock;
  397. scrub = 1;
  398. if (!check) {
  399. ubi_msg("force data checking");
  400. check = 1;
  401. goto retry;
  402. }
  403. } else
  404. goto out_unlock;
  405. }
  406. if (check) {
  407. uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
  408. if (crc1 != crc) {
  409. ubi_warn("CRC error: calculated %#08x, must be %#08x",
  410. crc1, crc);
  411. err = -EBADMSG;
  412. goto out_unlock;
  413. }
  414. }
  415. if (scrub)
  416. err = ubi_wl_scrub_peb(ubi, pnum);
  417. leb_read_unlock(ubi, vol_id, lnum);
  418. return err;
  419. out_free:
  420. ubi_free_vid_hdr(ubi, vid_hdr);
  421. out_unlock:
  422. leb_read_unlock(ubi, vol_id, lnum);
  423. return err;
  424. }
  425. /**
  426. * recover_peb - recover from write failure.
  427. * @ubi: UBI device description object
  428. * @pnum: the physical eraseblock to recover
  429. * @vol_id: volume ID
  430. * @lnum: logical eraseblock number
  431. * @buf: data which was not written because of the write failure
  432. * @offset: offset of the failed write
  433. * @len: how many bytes should have been written
  434. *
  435. * This function is called in case of a write failure and moves all good data
  436. * from the potentially bad physical eraseblock to a good physical eraseblock.
  437. * This function also writes the data which was not written due to the failure.
  438. * Returns new physical eraseblock number in case of success, and a negative
  439. * error code in case of failure.
  440. */
  441. static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
  442. const void *buf, int offset, int len)
  443. {
  444. int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
  445. struct ubi_volume *vol = ubi->volumes[idx];
  446. struct ubi_vid_hdr *vid_hdr;
  447. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  448. if (!vid_hdr)
  449. return -ENOMEM;
  450. retry:
  451. new_pnum = ubi_wl_get_peb(ubi);
  452. if (new_pnum < 0) {
  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(ubi_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. mutex_lock(&ubi->buf_mutex);
  469. memset(ubi->peb_buf + offset, 0xFF, len);
  470. /* Read everything before the area where the write failure happened */
  471. if (offset > 0) {
  472. err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
  473. if (err && err != UBI_IO_BITFLIPS)
  474. goto out_unlock;
  475. }
  476. memcpy(ubi->peb_buf + offset, buf, len);
  477. err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
  478. if (err) {
  479. mutex_unlock(&ubi->buf_mutex);
  480. goto write_error;
  481. }
  482. mutex_unlock(&ubi->buf_mutex);
  483. ubi_free_vid_hdr(ubi, vid_hdr);
  484. vol->eba_tbl[lnum] = new_pnum;
  485. ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  486. ubi_msg("data was successfully recovered");
  487. return 0;
  488. out_unlock:
  489. mutex_unlock(&ubi->buf_mutex);
  490. out_put:
  491. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  492. ubi_free_vid_hdr(ubi, vid_hdr);
  493. return err;
  494. write_error:
  495. /*
  496. * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
  497. * get another one.
  498. */
  499. ubi_warn("failed to write to PEB %d", new_pnum);
  500. ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
  501. if (++tries > UBI_IO_RETRIES) {
  502. ubi_free_vid_hdr(ubi, vid_hdr);
  503. return err;
  504. }
  505. ubi_msg("try again");
  506. goto retry;
  507. }
  508. /**
  509. * ubi_eba_write_leb - write data to dynamic volume.
  510. * @ubi: UBI device description object
  511. * @vol: volume description object
  512. * @lnum: logical eraseblock number
  513. * @buf: the data to write
  514. * @offset: offset within the logical eraseblock where to write
  515. * @len: how many bytes to write
  516. *
  517. * This function writes data to logical eraseblock @lnum of a dynamic volume
  518. * @vol. Returns zero in case of success and a negative error code in case
  519. * of failure. In case of error, it is possible that something was still
  520. * written to the flash media, but may be some garbage.
  521. */
  522. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  523. const void *buf, int offset, int len)
  524. {
  525. int err, pnum, tries = 0, vol_id = vol->vol_id;
  526. struct ubi_vid_hdr *vid_hdr;
  527. if (ubi->ro_mode)
  528. return -EROFS;
  529. err = leb_write_lock(ubi, vol_id, lnum);
  530. if (err)
  531. return err;
  532. pnum = vol->eba_tbl[lnum];
  533. if (pnum >= 0) {
  534. dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
  535. len, offset, vol_id, lnum, pnum);
  536. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  537. if (err) {
  538. ubi_warn("failed to write data to PEB %d", pnum);
  539. if (err == -EIO && ubi->bad_allowed)
  540. err = recover_peb(ubi, pnum, vol_id, lnum, buf,
  541. offset, len);
  542. if (err)
  543. ubi_ro_mode(ubi);
  544. }
  545. leb_write_unlock(ubi, vol_id, lnum);
  546. return err;
  547. }
  548. /*
  549. * The logical eraseblock is not mapped. We have to get a free physical
  550. * eraseblock and write the volume identifier header there first.
  551. */
  552. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  553. if (!vid_hdr) {
  554. leb_write_unlock(ubi, vol_id, lnum);
  555. return -ENOMEM;
  556. }
  557. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  558. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  559. vid_hdr->vol_id = cpu_to_be32(vol_id);
  560. vid_hdr->lnum = cpu_to_be32(lnum);
  561. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  562. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  563. retry:
  564. pnum = ubi_wl_get_peb(ubi);
  565. if (pnum < 0) {
  566. ubi_free_vid_hdr(ubi, vid_hdr);
  567. leb_write_unlock(ubi, vol_id, lnum);
  568. return pnum;
  569. }
  570. dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
  571. len, offset, vol_id, lnum, pnum);
  572. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  573. if (err) {
  574. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  575. vol_id, lnum, pnum);
  576. goto write_error;
  577. }
  578. if (len) {
  579. err = ubi_io_write_data(ubi, buf, pnum, offset, len);
  580. if (err) {
  581. ubi_warn("failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
  582. len, offset, vol_id, lnum, pnum);
  583. goto write_error;
  584. }
  585. }
  586. vol->eba_tbl[lnum] = pnum;
  587. leb_write_unlock(ubi, vol_id, lnum);
  588. ubi_free_vid_hdr(ubi, vid_hdr);
  589. return 0;
  590. write_error:
  591. if (err != -EIO || !ubi->bad_allowed) {
  592. ubi_ro_mode(ubi);
  593. leb_write_unlock(ubi, vol_id, lnum);
  594. ubi_free_vid_hdr(ubi, vid_hdr);
  595. return err;
  596. }
  597. /*
  598. * Fortunately, this is the first write operation to this physical
  599. * eraseblock, so just put it and request a new one. We assume that if
  600. * this physical eraseblock went bad, the erase code will handle that.
  601. */
  602. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  603. if (err || ++tries > UBI_IO_RETRIES) {
  604. ubi_ro_mode(ubi);
  605. leb_write_unlock(ubi, vol_id, lnum);
  606. ubi_free_vid_hdr(ubi, vid_hdr);
  607. return err;
  608. }
  609. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  610. ubi_msg("try another PEB");
  611. goto retry;
  612. }
  613. /**
  614. * ubi_eba_write_leb_st - write data to static volume.
  615. * @ubi: UBI device description object
  616. * @vol: volume description object
  617. * @lnum: logical eraseblock number
  618. * @buf: data to write
  619. * @len: how many bytes to write
  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. 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 than 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, struct ubi_volume *vol,
  636. int lnum, const void *buf, int len, int used_ebs)
  637. {
  638. int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
  639. struct ubi_vid_hdr *vid_hdr;
  640. uint32_t crc;
  641. if (ubi->ro_mode)
  642. return -EROFS;
  643. if (lnum == used_ebs - 1)
  644. /* If this is the last LEB @len may be unaligned */
  645. len = ALIGN(data_size, ubi->min_io_size);
  646. else
  647. ubi_assert(!(len & (ubi->min_io_size - 1)));
  648. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  649. if (!vid_hdr)
  650. return -ENOMEM;
  651. err = leb_write_lock(ubi, vol_id, lnum);
  652. if (err) {
  653. ubi_free_vid_hdr(ubi, vid_hdr);
  654. return err;
  655. }
  656. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  657. vid_hdr->vol_id = cpu_to_be32(vol_id);
  658. vid_hdr->lnum = cpu_to_be32(lnum);
  659. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  660. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  661. crc = crc32(UBI_CRC32_INIT, buf, data_size);
  662. vid_hdr->vol_type = UBI_VID_STATIC;
  663. vid_hdr->data_size = cpu_to_be32(data_size);
  664. vid_hdr->used_ebs = cpu_to_be32(used_ebs);
  665. vid_hdr->data_crc = cpu_to_be32(crc);
  666. retry:
  667. pnum = ubi_wl_get_peb(ubi);
  668. if (pnum < 0) {
  669. ubi_free_vid_hdr(ubi, vid_hdr);
  670. leb_write_unlock(ubi, vol_id, lnum);
  671. return pnum;
  672. }
  673. dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
  674. len, vol_id, lnum, pnum, used_ebs);
  675. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  676. if (err) {
  677. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  678. vol_id, lnum, pnum);
  679. goto write_error;
  680. }
  681. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  682. if (err) {
  683. ubi_warn("failed to write %d bytes of data to PEB %d",
  684. len, pnum);
  685. goto write_error;
  686. }
  687. ubi_assert(vol->eba_tbl[lnum] < 0);
  688. vol->eba_tbl[lnum] = pnum;
  689. leb_write_unlock(ubi, vol_id, lnum);
  690. ubi_free_vid_hdr(ubi, vid_hdr);
  691. return 0;
  692. write_error:
  693. if (err != -EIO || !ubi->bad_allowed) {
  694. /*
  695. * This flash device does not admit of bad eraseblocks or
  696. * something nasty and unexpected happened. Switch to read-only
  697. * mode just in case.
  698. */
  699. ubi_ro_mode(ubi);
  700. leb_write_unlock(ubi, vol_id, lnum);
  701. ubi_free_vid_hdr(ubi, vid_hdr);
  702. return err;
  703. }
  704. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  705. if (err || ++tries > UBI_IO_RETRIES) {
  706. ubi_ro_mode(ubi);
  707. leb_write_unlock(ubi, vol_id, lnum);
  708. ubi_free_vid_hdr(ubi, vid_hdr);
  709. return err;
  710. }
  711. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  712. ubi_msg("try another PEB");
  713. goto retry;
  714. }
  715. /*
  716. * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
  717. * @ubi: UBI device description object
  718. * @vol: volume description object
  719. * @lnum: logical eraseblock number
  720. * @buf: data to write
  721. * @len: how many bytes to write
  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, struct ubi_volume *vol,
  733. int lnum, const void *buf, int len)
  734. {
  735. int err, pnum, tries = 0, vol_id = vol->vol_id;
  736. struct ubi_vid_hdr *vid_hdr;
  737. uint32_t crc;
  738. if (ubi->ro_mode)
  739. return -EROFS;
  740. if (len == 0) {
  741. /*
  742. * Special case when data length is zero. In this case the LEB
  743. * has to be unmapped and mapped somewhere else.
  744. */
  745. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  746. if (err)
  747. return err;
  748. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
  749. }
  750. vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
  751. if (!vid_hdr)
  752. return -ENOMEM;
  753. mutex_lock(&ubi->alc_mutex);
  754. err = leb_write_lock(ubi, vol_id, lnum);
  755. if (err)
  756. goto out_mutex;
  757. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  758. vid_hdr->vol_id = cpu_to_be32(vol_id);
  759. vid_hdr->lnum = cpu_to_be32(lnum);
  760. vid_hdr->compat = ubi_get_compat(ubi, vol_id);
  761. vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
  762. crc = crc32(UBI_CRC32_INIT, buf, len);
  763. vid_hdr->vol_type = UBI_VID_DYNAMIC;
  764. vid_hdr->data_size = cpu_to_be32(len);
  765. vid_hdr->copy_flag = 1;
  766. vid_hdr->data_crc = cpu_to_be32(crc);
  767. retry:
  768. pnum = ubi_wl_get_peb(ubi);
  769. if (pnum < 0) {
  770. err = pnum;
  771. goto out_leb_unlock;
  772. }
  773. dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
  774. vol_id, lnum, vol->eba_tbl[lnum], pnum);
  775. err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
  776. if (err) {
  777. ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
  778. vol_id, lnum, pnum);
  779. goto write_error;
  780. }
  781. err = ubi_io_write_data(ubi, buf, pnum, 0, len);
  782. if (err) {
  783. ubi_warn("failed to write %d bytes of data to PEB %d",
  784. len, pnum);
  785. goto write_error;
  786. }
  787. if (vol->eba_tbl[lnum] >= 0) {
  788. err = ubi_wl_put_peb(ubi, vol_id, lnum, vol->eba_tbl[lnum], 0);
  789. if (err)
  790. goto out_leb_unlock;
  791. }
  792. vol->eba_tbl[lnum] = pnum;
  793. out_leb_unlock:
  794. leb_write_unlock(ubi, vol_id, lnum);
  795. out_mutex:
  796. mutex_unlock(&ubi->alc_mutex);
  797. ubi_free_vid_hdr(ubi, vid_hdr);
  798. return err;
  799. write_error:
  800. if (err != -EIO || !ubi->bad_allowed) {
  801. /*
  802. * This flash device does not admit of bad eraseblocks or
  803. * something nasty and unexpected happened. Switch to read-only
  804. * mode just in case.
  805. */
  806. ubi_ro_mode(ubi);
  807. goto out_leb_unlock;
  808. }
  809. err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
  810. if (err || ++tries > UBI_IO_RETRIES) {
  811. ubi_ro_mode(ubi);
  812. goto out_leb_unlock;
  813. }
  814. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  815. ubi_msg("try another PEB");
  816. goto retry;
  817. }
  818. /**
  819. * is_error_sane - check whether a read error is sane.
  820. * @err: code of the error happened during reading
  821. *
  822. * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
  823. * cannot read data from the target PEB (an error @err happened). If the error
  824. * code is sane, then we treat this error as non-fatal. Otherwise the error is
  825. * fatal and UBI will be switched to R/O mode later.
  826. *
  827. * The idea is that we try not to switch to R/O mode if the read error is
  828. * something which suggests there was a real read problem. E.g., %-EIO. Or a
  829. * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
  830. * mode, simply because we do not know what happened at the MTD level, and we
  831. * cannot handle this. E.g., the underlying driver may have become crazy, and
  832. * it is safer to switch to R/O mode to preserve the data.
  833. *
  834. * And bear in mind, this is about reading from the target PEB, i.e. the PEB
  835. * which we have just written.
  836. */
  837. static int is_error_sane(int err)
  838. {
  839. if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
  840. err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
  841. return 0;
  842. return 1;
  843. }
  844. /**
  845. * ubi_eba_copy_leb - copy logical eraseblock.
  846. * @ubi: UBI device description object
  847. * @from: physical eraseblock number from where to copy
  848. * @to: physical eraseblock number where to copy
  849. * @vid_hdr: VID header of the @from physical eraseblock
  850. *
  851. * This function copies logical eraseblock from physical eraseblock @from to
  852. * physical eraseblock @to. The @vid_hdr buffer may be changed by this
  853. * function. Returns:
  854. * o %0 in case of success;
  855. * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
  856. * o a negative error code in case of failure.
  857. */
  858. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  859. struct ubi_vid_hdr *vid_hdr)
  860. {
  861. int err, vol_id, lnum, data_size, aldata_size, idx;
  862. struct ubi_volume *vol;
  863. uint32_t crc;
  864. vol_id = be32_to_cpu(vid_hdr->vol_id);
  865. lnum = be32_to_cpu(vid_hdr->lnum);
  866. dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
  867. if (vid_hdr->vol_type == UBI_VID_STATIC) {
  868. data_size = be32_to_cpu(vid_hdr->data_size);
  869. aldata_size = ALIGN(data_size, ubi->min_io_size);
  870. } else
  871. data_size = aldata_size =
  872. ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
  873. idx = vol_id2idx(ubi, vol_id);
  874. spin_lock(&ubi->volumes_lock);
  875. /*
  876. * Note, we may race with volume deletion, which means that the volume
  877. * this logical eraseblock belongs to might be being deleted. Since the
  878. * volume deletion un-maps all the volume's logical eraseblocks, it will
  879. * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
  880. */
  881. vol = ubi->volumes[idx];
  882. spin_unlock(&ubi->volumes_lock);
  883. if (!vol) {
  884. /* No need to do further work, cancel */
  885. dbg_wl("volume %d is being removed, cancel", vol_id);
  886. return MOVE_CANCEL_RACE;
  887. }
  888. /*
  889. * We do not want anybody to write to this logical eraseblock while we
  890. * are moving it, so lock it.
  891. *
  892. * Note, we are using non-waiting locking here, because we cannot sleep
  893. * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
  894. * unmapping the LEB which is mapped to the PEB we are going to move
  895. * (@from). This task locks the LEB and goes sleep in the
  896. * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
  897. * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
  898. * LEB is already locked, we just do not move it and return
  899. * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
  900. * we do not know the reasons of the contention - it may be just a
  901. * normal I/O on this LEB, so we want to re-try.
  902. */
  903. err = leb_write_trylock(ubi, vol_id, lnum);
  904. if (err) {
  905. dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
  906. return MOVE_RETRY;
  907. }
  908. /*
  909. * The LEB might have been put meanwhile, and the task which put it is
  910. * probably waiting on @ubi->move_mutex. No need to continue the work,
  911. * cancel it.
  912. */
  913. if (vol->eba_tbl[lnum] != from) {
  914. dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
  915. vol_id, lnum, from, vol->eba_tbl[lnum]);
  916. err = MOVE_CANCEL_RACE;
  917. goto out_unlock_leb;
  918. }
  919. /*
  920. * OK, now the LEB is locked and we can safely start moving it. Since
  921. * this function utilizes the @ubi->peb_buf buffer which is shared
  922. * with some other functions - we lock the buffer by taking the
  923. * @ubi->buf_mutex.
  924. */
  925. mutex_lock(&ubi->buf_mutex);
  926. dbg_wl("read %d bytes of data", aldata_size);
  927. err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
  928. if (err && err != UBI_IO_BITFLIPS) {
  929. ubi_warn("error %d while reading data from PEB %d",
  930. err, from);
  931. err = MOVE_SOURCE_RD_ERR;
  932. goto out_unlock_buf;
  933. }
  934. /*
  935. * Now we have got to calculate how much data we have to copy. In
  936. * case of a static volume it is fairly easy - the VID header contains
  937. * the data size. In case of a dynamic volume it is more difficult - we
  938. * have to read the contents, cut 0xFF bytes from the end and copy only
  939. * the first part. We must do this to avoid writing 0xFF bytes as it
  940. * may have some side-effects. And not only this. It is important not
  941. * to include those 0xFFs to CRC because later the they may be filled
  942. * by data.
  943. */
  944. if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
  945. aldata_size = data_size =
  946. ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
  947. cond_resched();
  948. crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
  949. cond_resched();
  950. /*
  951. * It may turn out to be that the whole @from physical eraseblock
  952. * contains only 0xFF bytes. Then we have to only write the VID header
  953. * and do not write any data. This also means we should not set
  954. * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
  955. */
  956. if (data_size > 0) {
  957. vid_hdr->copy_flag = 1;
  958. vid_hdr->data_size = cpu_to_be32(data_size);
  959. vid_hdr->data_crc = cpu_to_be32(crc);
  960. }
  961. vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
  962. err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
  963. if (err) {
  964. if (err == -EIO)
  965. err = MOVE_TARGET_WR_ERR;
  966. goto out_unlock_buf;
  967. }
  968. cond_resched();
  969. /* Read the VID header back and check if it was written correctly */
  970. err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
  971. if (err) {
  972. if (err != UBI_IO_BITFLIPS) {
  973. ubi_warn("error %d while reading VID header back from PEB %d",
  974. err, to);
  975. if (is_error_sane(err))
  976. err = MOVE_TARGET_RD_ERR;
  977. } else
  978. err = MOVE_TARGET_BITFLIPS;
  979. goto out_unlock_buf;
  980. }
  981. if (data_size > 0) {
  982. err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  983. if (err) {
  984. if (err == -EIO)
  985. err = MOVE_TARGET_WR_ERR;
  986. goto out_unlock_buf;
  987. }
  988. cond_resched();
  989. /*
  990. * We've written the data and are going to read it back to make
  991. * sure it was written correctly.
  992. */
  993. memset(ubi->peb_buf, 0xFF, aldata_size);
  994. err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
  995. if (err) {
  996. if (err != UBI_IO_BITFLIPS) {
  997. ubi_warn("error %d while reading data back from PEB %d",
  998. err, to);
  999. if (is_error_sane(err))
  1000. err = MOVE_TARGET_RD_ERR;
  1001. } else
  1002. err = MOVE_TARGET_BITFLIPS;
  1003. goto out_unlock_buf;
  1004. }
  1005. cond_resched();
  1006. if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
  1007. ubi_warn("read data back from PEB %d and it is different",
  1008. to);
  1009. err = -EINVAL;
  1010. goto out_unlock_buf;
  1011. }
  1012. }
  1013. ubi_assert(vol->eba_tbl[lnum] == from);
  1014. vol->eba_tbl[lnum] = to;
  1015. out_unlock_buf:
  1016. mutex_unlock(&ubi->buf_mutex);
  1017. out_unlock_leb:
  1018. leb_write_unlock(ubi, vol_id, lnum);
  1019. return err;
  1020. }
  1021. /**
  1022. * print_rsvd_warning - warn about not having enough reserved PEBs.
  1023. * @ubi: UBI device description object
  1024. *
  1025. * This is a helper function for 'ubi_eba_init()' which is called when UBI
  1026. * cannot reserve enough PEBs for bad block handling. This function makes a
  1027. * decision whether we have to print a warning or not. The algorithm is as
  1028. * follows:
  1029. * o if this is a new UBI image, then just print the warning
  1030. * o if this is an UBI image which has already been used for some time, print
  1031. * a warning only if we can reserve less than 10% of the expected amount of
  1032. * the reserved PEB.
  1033. *
  1034. * The idea is that when UBI is used, PEBs become bad, and the reserved pool
  1035. * of PEBs becomes smaller, which is normal and we do not want to scare users
  1036. * with a warning every time they attach the MTD device. This was an issue
  1037. * reported by real users.
  1038. */
  1039. static void print_rsvd_warning(struct ubi_device *ubi,
  1040. struct ubi_attach_info *ai)
  1041. {
  1042. /*
  1043. * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
  1044. * large number to distinguish between newly flashed and used images.
  1045. */
  1046. if (ai->max_sqnum > (1 << 18)) {
  1047. int min = ubi->beb_rsvd_level / 10;
  1048. if (!min)
  1049. min = 1;
  1050. if (ubi->beb_rsvd_pebs > min)
  1051. return;
  1052. }
  1053. ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
  1054. ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
  1055. if (ubi->corr_peb_count)
  1056. ubi_warn("%d PEBs are corrupted and not used",
  1057. ubi->corr_peb_count);
  1058. }
  1059. /**
  1060. * ubi_eba_init - initialize the EBA sub-system using attaching information.
  1061. * @ubi: UBI device description object
  1062. * @ai: attaching information
  1063. *
  1064. * This function returns zero in case of success and a negative error code in
  1065. * case of failure.
  1066. */
  1067. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
  1068. {
  1069. int i, j, err, num_volumes;
  1070. struct ubi_ainf_volume *av;
  1071. struct ubi_volume *vol;
  1072. struct ubi_ainf_peb *aeb;
  1073. struct rb_node *rb;
  1074. dbg_eba("initialize EBA sub-system");
  1075. spin_lock_init(&ubi->ltree_lock);
  1076. mutex_init(&ubi->alc_mutex);
  1077. ubi->ltree = RB_ROOT;
  1078. ubi->global_sqnum = ai->max_sqnum + 1;
  1079. num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
  1080. for (i = 0; i < num_volumes; i++) {
  1081. vol = ubi->volumes[i];
  1082. if (!vol)
  1083. continue;
  1084. cond_resched();
  1085. vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
  1086. GFP_KERNEL);
  1087. if (!vol->eba_tbl) {
  1088. err = -ENOMEM;
  1089. goto out_free;
  1090. }
  1091. for (j = 0; j < vol->reserved_pebs; j++)
  1092. vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
  1093. av = ubi_find_av(ai, idx2vol_id(ubi, i));
  1094. if (!av)
  1095. continue;
  1096. ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
  1097. if (aeb->lnum >= vol->reserved_pebs)
  1098. /*
  1099. * This may happen in case of an unclean reboot
  1100. * during re-size.
  1101. */
  1102. ubi_move_aeb_to_list(av, aeb, &ai->erase);
  1103. vol->eba_tbl[aeb->lnum] = aeb->pnum;
  1104. }
  1105. }
  1106. if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
  1107. ubi_err("no enough physical eraseblocks (%d, need %d)",
  1108. ubi->avail_pebs, EBA_RESERVED_PEBS);
  1109. if (ubi->corr_peb_count)
  1110. ubi_err("%d PEBs are corrupted and not used",
  1111. ubi->corr_peb_count);
  1112. err = -ENOSPC;
  1113. goto out_free;
  1114. }
  1115. ubi->avail_pebs -= EBA_RESERVED_PEBS;
  1116. ubi->rsvd_pebs += EBA_RESERVED_PEBS;
  1117. if (ubi->bad_allowed) {
  1118. ubi_calculate_reserved(ubi);
  1119. if (ubi->avail_pebs < ubi->beb_rsvd_level) {
  1120. /* No enough free physical eraseblocks */
  1121. ubi->beb_rsvd_pebs = ubi->avail_pebs;
  1122. print_rsvd_warning(ubi, ai);
  1123. } else
  1124. ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
  1125. ubi->avail_pebs -= ubi->beb_rsvd_pebs;
  1126. ubi->rsvd_pebs += ubi->beb_rsvd_pebs;
  1127. }
  1128. dbg_eba("EBA sub-system is initialized");
  1129. return 0;
  1130. out_free:
  1131. for (i = 0; i < num_volumes; i++) {
  1132. if (!ubi->volumes[i])
  1133. continue;
  1134. kfree(ubi->volumes[i]->eba_tbl);
  1135. ubi->volumes[i]->eba_tbl = NULL;
  1136. }
  1137. return err;
  1138. }