kapi.c 19 KB

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