kapi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. /* This file mostly implements UBI kernel API functions */
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. #include <asm/div64.h>
  24. #include "ubi.h"
  25. /**
  26. * ubi_get_device_info - get information about UBI device.
  27. * @ubi_num: UBI device number
  28. * @di: the information is stored here
  29. *
  30. * This function returns %0 in case of success and a %-ENODEV if there is no
  31. * such UBI device.
  32. */
  33. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
  34. {
  35. const struct ubi_device *ubi;
  36. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
  37. !ubi_devices[ubi_num])
  38. return -ENODEV;
  39. ubi = ubi_devices[ubi_num];
  40. di->ubi_num = ubi->ubi_num;
  41. di->leb_size = ubi->leb_size;
  42. di->min_io_size = ubi->min_io_size;
  43. di->ro_mode = ubi->ro_mode;
  44. di->cdev = ubi->cdev.dev;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(ubi_get_device_info);
  48. /**
  49. * ubi_get_volume_info - get information about UBI volume.
  50. * @desc: volume descriptor
  51. * @vi: the information is stored here
  52. */
  53. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  54. struct ubi_volume_info *vi)
  55. {
  56. const struct ubi_volume *vol = desc->vol;
  57. const struct ubi_device *ubi = vol->ubi;
  58. vi->vol_id = vol->vol_id;
  59. vi->ubi_num = ubi->ubi_num;
  60. vi->size = vol->reserved_pebs;
  61. vi->used_bytes = vol->used_bytes;
  62. vi->vol_type = vol->vol_type;
  63. vi->corrupted = vol->corrupted;
  64. vi->upd_marker = vol->upd_marker;
  65. vi->alignment = vol->alignment;
  66. vi->usable_leb_size = vol->usable_leb_size;
  67. vi->name_len = vol->name_len;
  68. vi->name = vol->name;
  69. vi->cdev = vol->cdev.dev;
  70. }
  71. EXPORT_SYMBOL_GPL(ubi_get_volume_info);
  72. /**
  73. * ubi_open_volume - open UBI volume.
  74. * @ubi_num: UBI device number
  75. * @vol_id: volume ID
  76. * @mode: open mode
  77. *
  78. * The @mode parameter specifies if the volume should be opened in read-only
  79. * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
  80. * nobody else will be able to open this volume. UBI allows to have many volume
  81. * readers and one writer at a time.
  82. *
  83. * If a static volume is being opened for the first time since boot, it will be
  84. * checked by this function, which means it will be fully read and the CRC
  85. * checksum of each logical eraseblock will be checked.
  86. *
  87. * This function returns volume descriptor in case of success and a negative
  88. * error code in case of failure.
  89. */
  90. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
  91. {
  92. int err;
  93. struct ubi_volume_desc *desc;
  94. struct ubi_device *ubi;
  95. struct ubi_volume *vol;
  96. dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
  97. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  98. return ERR_PTR(-EINVAL);
  99. if (mode != UBI_READONLY && mode != UBI_READWRITE &&
  100. mode != UBI_EXCLUSIVE)
  101. return ERR_PTR(-EINVAL);
  102. ubi = ubi_devices[ubi_num];
  103. if (!ubi)
  104. return ERR_PTR(-ENODEV);
  105. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  106. return ERR_PTR(-EINVAL);
  107. desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
  108. if (!desc)
  109. return ERR_PTR(-ENOMEM);
  110. err = -ENODEV;
  111. if (!try_module_get(THIS_MODULE))
  112. goto out_free;
  113. spin_lock(&ubi->volumes_lock);
  114. vol = ubi->volumes[vol_id];
  115. if (!vol)
  116. goto out_unlock;
  117. err = -EBUSY;
  118. switch (mode) {
  119. case UBI_READONLY:
  120. if (vol->exclusive)
  121. goto out_unlock;
  122. vol->readers += 1;
  123. break;
  124. case UBI_READWRITE:
  125. if (vol->exclusive || vol->writers > 0)
  126. goto out_unlock;
  127. vol->writers += 1;
  128. break;
  129. case UBI_EXCLUSIVE:
  130. if (vol->exclusive || vol->writers || vol->readers)
  131. goto out_unlock;
  132. vol->exclusive = 1;
  133. break;
  134. }
  135. get_device(&vol->dev);
  136. vol->ref_count += 1;
  137. spin_unlock(&ubi->volumes_lock);
  138. desc->vol = vol;
  139. desc->mode = mode;
  140. /*
  141. * To prevent simultaneous checks of the same volume we use
  142. * @volumes_mutex, although it is not the purpose it was introduced
  143. * for.
  144. */
  145. mutex_lock(&ubi->volumes_mutex);
  146. if (!vol->checked) {
  147. /* This is the first open - check the volume */
  148. err = ubi_check_volume(ubi, vol_id);
  149. if (err < 0) {
  150. mutex_unlock(&ubi->volumes_mutex);
  151. ubi_close_volume(desc);
  152. return ERR_PTR(err);
  153. }
  154. if (err == 1) {
  155. ubi_warn("volume %d on UBI device %d is corrupted",
  156. vol_id, ubi->ubi_num);
  157. vol->corrupted = 1;
  158. }
  159. vol->checked = 1;
  160. }
  161. mutex_unlock(&ubi->volumes_mutex);
  162. return desc;
  163. out_unlock:
  164. spin_unlock(&ubi->volumes_lock);
  165. module_put(THIS_MODULE);
  166. out_free:
  167. kfree(desc);
  168. return ERR_PTR(err);
  169. }
  170. EXPORT_SYMBOL_GPL(ubi_open_volume);
  171. /**
  172. * ubi_open_volume_nm - open UBI volume by name.
  173. * @ubi_num: UBI device number
  174. * @name: volume name
  175. * @mode: open mode
  176. *
  177. * This function is similar to 'ubi_open_volume()', but opens a volume by name.
  178. */
  179. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  180. int mode)
  181. {
  182. int i, vol_id = -1, len;
  183. struct ubi_device *ubi;
  184. dbg_msg("open volume %s, mode %d", name, mode);
  185. if (!name)
  186. return ERR_PTR(-EINVAL);
  187. len = strnlen(name, UBI_VOL_NAME_MAX + 1);
  188. if (len > UBI_VOL_NAME_MAX)
  189. return ERR_PTR(-EINVAL);
  190. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  191. return ERR_PTR(-EINVAL);
  192. ubi = ubi_devices[ubi_num];
  193. if (!ubi)
  194. return ERR_PTR(-ENODEV);
  195. spin_lock(&ubi->volumes_lock);
  196. /* Walk all volumes of this UBI device */
  197. for (i = 0; i < ubi->vtbl_slots; i++) {
  198. struct ubi_volume *vol = ubi->volumes[i];
  199. if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
  200. vol_id = i;
  201. break;
  202. }
  203. }
  204. spin_unlock(&ubi->volumes_lock);
  205. if (vol_id < 0)
  206. return ERR_PTR(-ENODEV);
  207. return ubi_open_volume(ubi_num, vol_id, mode);
  208. }
  209. EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
  210. /**
  211. * ubi_close_volume - close UBI volume.
  212. * @desc: volume descriptor
  213. */
  214. void ubi_close_volume(struct ubi_volume_desc *desc)
  215. {
  216. struct ubi_volume *vol = desc->vol;
  217. dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
  218. spin_lock(&vol->ubi->volumes_lock);
  219. switch (desc->mode) {
  220. case UBI_READONLY:
  221. vol->readers -= 1;
  222. break;
  223. case UBI_READWRITE:
  224. vol->writers -= 1;
  225. break;
  226. case UBI_EXCLUSIVE:
  227. vol->exclusive = 0;
  228. }
  229. vol->ref_count -= 1;
  230. spin_unlock(&vol->ubi->volumes_lock);
  231. put_device(&vol->dev);
  232. kfree(desc);
  233. module_put(THIS_MODULE);
  234. }
  235. EXPORT_SYMBOL_GPL(ubi_close_volume);
  236. /**
  237. * ubi_leb_read - read data.
  238. * @desc: volume descriptor
  239. * @lnum: logical eraseblock number to read from
  240. * @buf: buffer where to store the read data
  241. * @offset: offset within the logical eraseblock to read from
  242. * @len: how many bytes to read
  243. * @check: whether UBI has to check the read data's CRC or not.
  244. *
  245. * This function reads data from offset @offset of logical eraseblock @lnum and
  246. * stores the data at @buf. When reading from static volumes, @check specifies
  247. * whether the data has to be checked or not. If yes, the whole logical
  248. * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
  249. * checksum is per-eraseblock). So checking may substantially slow down the
  250. * read speed. The @check argument is ignored for dynamic volumes.
  251. *
  252. * In case of success, this function returns zero. In case of failure, this
  253. * function returns a negative error code.
  254. *
  255. * %-EBADMSG error code is returned:
  256. * o for both static and dynamic volumes if MTD driver has detected a data
  257. * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
  258. * o for static volumes in case of data CRC mismatch.
  259. *
  260. * If the volume is damaged because of an interrupted update this function just
  261. * returns immediately with %-EBADF error code.
  262. */
  263. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  264. int len, int check)
  265. {
  266. struct ubi_volume *vol = desc->vol;
  267. struct ubi_device *ubi = vol->ubi;
  268. int err, vol_id = vol->vol_id;
  269. dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
  270. if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
  271. lnum >= vol->used_ebs || offset < 0 || len < 0 ||
  272. offset + len > vol->usable_leb_size)
  273. return -EINVAL;
  274. if (vol->vol_type == UBI_STATIC_VOLUME) {
  275. if (vol->used_ebs == 0)
  276. /* Empty static UBI volume */
  277. return 0;
  278. if (lnum == vol->used_ebs - 1 &&
  279. offset + len > vol->last_eb_bytes)
  280. return -EINVAL;
  281. }
  282. if (vol->upd_marker)
  283. return -EBADF;
  284. if (len == 0)
  285. return 0;
  286. err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
  287. if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
  288. ubi_warn("mark volume %d as corrupted", vol_id);
  289. vol->corrupted = 1;
  290. }
  291. return err;
  292. }
  293. EXPORT_SYMBOL_GPL(ubi_leb_read);
  294. /**
  295. * ubi_leb_write - write data.
  296. * @desc: volume descriptor
  297. * @lnum: logical eraseblock number to write to
  298. * @buf: data to write
  299. * @offset: offset within the logical eraseblock where to write
  300. * @len: how many bytes to write
  301. * @dtype: expected data type
  302. *
  303. * This function writes @len bytes of data from @buf to offset @offset of
  304. * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
  305. * the data.
  306. *
  307. * This function takes care of physical eraseblock write failures. If write to
  308. * the physical eraseblock write operation fails, the logical eraseblock is
  309. * re-mapped to another physical eraseblock, the data is recovered, and the
  310. * write finishes. UBI has a pool of reserved physical eraseblocks for this.
  311. *
  312. * If all the data were successfully written, zero is returned. If an error
  313. * occurred and UBI has not been able to recover from it, this function returns
  314. * a negative error code. Note, in case of an error, it is possible that
  315. * something was still written to the flash media, but that may be some
  316. * garbage.
  317. *
  318. * If the volume is damaged because of an interrupted update this function just
  319. * returns immediately with %-EBADF code.
  320. */
  321. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  322. int offset, int len, int dtype)
  323. {
  324. struct ubi_volume *vol = desc->vol;
  325. struct ubi_device *ubi = vol->ubi;
  326. int vol_id = vol->vol_id;
  327. dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
  328. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  329. return -EINVAL;
  330. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  331. return -EROFS;
  332. if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
  333. offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
  334. len % ubi->min_io_size)
  335. return -EINVAL;
  336. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  337. dtype != UBI_UNKNOWN)
  338. return -EINVAL;
  339. if (vol->upd_marker)
  340. return -EBADF;
  341. if (len == 0)
  342. return 0;
  343. return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
  344. }
  345. EXPORT_SYMBOL_GPL(ubi_leb_write);
  346. /*
  347. * ubi_leb_change - change logical eraseblock atomically.
  348. * @desc: volume descriptor
  349. * @lnum: logical eraseblock number to change
  350. * @buf: data to write
  351. * @len: how many bytes to write
  352. * @dtype: expected data type
  353. *
  354. * This function changes the contents of a logical eraseblock atomically. @buf
  355. * has to contain new logical eraseblock data, and @len - the length of the
  356. * data, which has to be aligned. The length may be shorter then the logical
  357. * eraseblock size, ant the logical eraseblock may be appended to more times
  358. * later on. This function guarantees that in case of an unclean reboot the old
  359. * contents is preserved. Returns zero in case of success and a negative error
  360. * code in case of failure.
  361. */
  362. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  363. int len, int dtype)
  364. {
  365. struct ubi_volume *vol = desc->vol;
  366. struct ubi_device *ubi = vol->ubi;
  367. int vol_id = vol->vol_id;
  368. dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
  369. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  370. return -EINVAL;
  371. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  372. return -EROFS;
  373. if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
  374. len > vol->usable_leb_size || len % ubi->min_io_size)
  375. return -EINVAL;
  376. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  377. dtype != UBI_UNKNOWN)
  378. return -EINVAL;
  379. if (vol->upd_marker)
  380. return -EBADF;
  381. if (len == 0)
  382. return 0;
  383. return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
  384. }
  385. EXPORT_SYMBOL_GPL(ubi_leb_change);
  386. /**
  387. * ubi_leb_erase - erase logical eraseblock.
  388. * @desc: volume descriptor
  389. * @lnum: logical eraseblock number
  390. *
  391. * This function un-maps logical eraseblock @lnum and synchronously erases the
  392. * correspondent physical eraseblock. Returns zero in case of success and a
  393. * negative error code in case of failure.
  394. *
  395. * If the volume is damaged because of an interrupted update this function just
  396. * returns immediately with %-EBADF code.
  397. */
  398. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
  399. {
  400. struct ubi_volume *vol = desc->vol;
  401. struct ubi_device *ubi = vol->ubi;
  402. int err, vol_id = vol->vol_id;
  403. dbg_msg("erase LEB %d:%d", vol_id, lnum);
  404. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  405. return -EROFS;
  406. if (lnum < 0 || lnum >= vol->reserved_pebs)
  407. return -EINVAL;
  408. if (vol->upd_marker)
  409. return -EBADF;
  410. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  411. if (err)
  412. return err;
  413. return ubi_wl_flush(ubi);
  414. }
  415. EXPORT_SYMBOL_GPL(ubi_leb_erase);
  416. /**
  417. * ubi_leb_unmap - un-map logical eraseblock.
  418. * @desc: volume descriptor
  419. * @lnum: logical eraseblock number
  420. *
  421. * This function un-maps logical eraseblock @lnum and schedules the
  422. * corresponding physical eraseblock for erasure, so that it will eventually be
  423. * physically erased in background. This operation is much faster then the
  424. * erase operation.
  425. *
  426. * Unlike erase, the un-map operation does not guarantee that the logical
  427. * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
  428. * example, if several logical eraseblocks are un-mapped, and an unclean reboot
  429. * happens after this, the logical eraseblocks will not necessarily be
  430. * un-mapped again when this MTD device is attached. They may actually be
  431. * mapped to the same physical eraseblocks again. So, this function has to be
  432. * used with care.
  433. *
  434. * In other words, when un-mapping a logical eraseblock, UBI does not store
  435. * any information about this on the flash media, it just marks the logical
  436. * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
  437. * eraseblock is physically erased, it will be mapped again to the same logical
  438. * eraseblock when the MTD device is attached again.
  439. *
  440. * The main and obvious use-case of this function is when the contents of a
  441. * logical eraseblock has to be re-written. Then it is much more efficient to
  442. * first un-map it, then write new data, rather then first erase it, then write
  443. * new data. Note, once new data has been written to the logical eraseblock,
  444. * UBI guarantees that the old contents has gone forever. In other words, if an
  445. * unclean reboot happens after the logical eraseblock has been un-mapped and
  446. * then written to, it will contain the last written data.
  447. *
  448. * This function returns zero in case of success and a negative error code in
  449. * case of failure. If the volume is damaged because of an interrupted update
  450. * this function just returns immediately with %-EBADF code.
  451. */
  452. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
  453. {
  454. struct ubi_volume *vol = desc->vol;
  455. struct ubi_device *ubi = vol->ubi;
  456. int vol_id = vol->vol_id;
  457. dbg_msg("unmap LEB %d:%d", vol_id, lnum);
  458. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  459. return -EROFS;
  460. if (lnum < 0 || lnum >= vol->reserved_pebs)
  461. return -EINVAL;
  462. if (vol->upd_marker)
  463. return -EBADF;
  464. return ubi_eba_unmap_leb(ubi, vol, lnum);
  465. }
  466. EXPORT_SYMBOL_GPL(ubi_leb_unmap);
  467. /**
  468. * ubi_leb_map - map logical erasblock to a physical eraseblock.
  469. * @desc: volume descriptor
  470. * @lnum: logical eraseblock number
  471. * @dtype: expected data type
  472. *
  473. * This function maps an un-mapped logical eraseblock @lnum to a physical
  474. * eraseblock. This means, that after a successfull invocation of this
  475. * function the logical eraseblock @lnum will be empty (contain only %0xFF
  476. * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
  477. * happens.
  478. *
  479. * This function returns zero in case of success, %-EBADF if the volume is
  480. * damaged because of an interrupted update, %-EBADMSG if the logical
  481. * eraseblock is already mapped, and other negative error codes in case of
  482. * other failures.
  483. */
  484. int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
  485. {
  486. struct ubi_volume *vol = desc->vol;
  487. struct ubi_device *ubi = vol->ubi;
  488. int vol_id = vol->vol_id;
  489. dbg_msg("unmap LEB %d:%d", vol_id, lnum);
  490. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  491. return -EROFS;
  492. if (lnum < 0 || lnum >= vol->reserved_pebs)
  493. return -EINVAL;
  494. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  495. dtype != UBI_UNKNOWN)
  496. return -EINVAL;
  497. if (vol->upd_marker)
  498. return -EBADF;
  499. if (vol->eba_tbl[lnum] >= 0)
  500. return -EBADMSG;
  501. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
  502. }
  503. EXPORT_SYMBOL_GPL(ubi_leb_map);
  504. /**
  505. * ubi_is_mapped - check if logical eraseblock is mapped.
  506. * @desc: volume descriptor
  507. * @lnum: logical eraseblock number
  508. *
  509. * This function checks if logical eraseblock @lnum is mapped to a physical
  510. * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
  511. * mean it will still be un-mapped after the UBI device is re-attached. The
  512. * logical eraseblock may become mapped to the physical eraseblock it was last
  513. * mapped to.
  514. *
  515. * This function returns %1 if the LEB is mapped, %0 if not, and a negative
  516. * error code in case of failure. If the volume is damaged because of an
  517. * interrupted update this function just returns immediately with %-EBADF error
  518. * code.
  519. */
  520. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
  521. {
  522. struct ubi_volume *vol = desc->vol;
  523. dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
  524. if (lnum < 0 || lnum >= vol->reserved_pebs)
  525. return -EINVAL;
  526. if (vol->upd_marker)
  527. return -EBADF;
  528. return vol->eba_tbl[lnum] >= 0;
  529. }
  530. EXPORT_SYMBOL_GPL(ubi_is_mapped);