kapi.c 19 KB

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