eba.c 32 KB

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