eba.c 31 KB

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