kapi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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_gen("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. mutex_lock(&ubi->ckvol_mutex);
  150. if (!vol->checked) {
  151. /* This is the first open - check the volume */
  152. err = ubi_check_volume(ubi, vol_id);
  153. if (err < 0) {
  154. mutex_unlock(&ubi->ckvol_mutex);
  155. ubi_close_volume(desc);
  156. return ERR_PTR(err);
  157. }
  158. if (err == 1) {
  159. ubi_warn("volume %d on UBI device %d is corrupted",
  160. vol_id, ubi->ubi_num);
  161. vol->corrupted = 1;
  162. }
  163. vol->checked = 1;
  164. }
  165. mutex_unlock(&ubi->ckvol_mutex);
  166. return desc;
  167. out_unlock:
  168. spin_unlock(&ubi->volumes_lock);
  169. module_put(THIS_MODULE);
  170. out_free:
  171. kfree(desc);
  172. out_put_ubi:
  173. ubi_put_device(ubi);
  174. return ERR_PTR(err);
  175. }
  176. EXPORT_SYMBOL_GPL(ubi_open_volume);
  177. /**
  178. * ubi_open_volume_nm - open UBI volume by name.
  179. * @ubi_num: UBI device number
  180. * @name: volume name
  181. * @mode: open mode
  182. *
  183. * This function is similar to 'ubi_open_volume()', but opens a volume by name.
  184. */
  185. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  186. int mode)
  187. {
  188. int i, vol_id = -1, len;
  189. struct ubi_device *ubi;
  190. struct ubi_volume_desc *ret;
  191. dbg_gen("open volume %s, mode %d", name, mode);
  192. if (!name)
  193. return ERR_PTR(-EINVAL);
  194. len = strnlen(name, UBI_VOL_NAME_MAX + 1);
  195. if (len > UBI_VOL_NAME_MAX)
  196. return ERR_PTR(-EINVAL);
  197. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  198. return ERR_PTR(-EINVAL);
  199. ubi = ubi_get_device(ubi_num);
  200. if (!ubi)
  201. return ERR_PTR(-ENODEV);
  202. spin_lock(&ubi->volumes_lock);
  203. /* Walk all volumes of this UBI device */
  204. for (i = 0; i < ubi->vtbl_slots; i++) {
  205. struct ubi_volume *vol = ubi->volumes[i];
  206. if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
  207. vol_id = i;
  208. break;
  209. }
  210. }
  211. spin_unlock(&ubi->volumes_lock);
  212. if (vol_id >= 0)
  213. ret = ubi_open_volume(ubi_num, vol_id, mode);
  214. else
  215. ret = ERR_PTR(-ENODEV);
  216. /*
  217. * We should put the UBI device even in case of success, because
  218. * 'ubi_open_volume()' took a reference as well.
  219. */
  220. ubi_put_device(ubi);
  221. return ret;
  222. }
  223. EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
  224. /**
  225. * ubi_close_volume - close UBI volume.
  226. * @desc: volume descriptor
  227. */
  228. void ubi_close_volume(struct ubi_volume_desc *desc)
  229. {
  230. struct ubi_volume *vol = desc->vol;
  231. struct ubi_device *ubi = vol->ubi;
  232. dbg_gen("close volume %d, mode %d", vol->vol_id, desc->mode);
  233. spin_lock(&ubi->volumes_lock);
  234. switch (desc->mode) {
  235. case UBI_READONLY:
  236. vol->readers -= 1;
  237. break;
  238. case UBI_READWRITE:
  239. vol->writers -= 1;
  240. break;
  241. case UBI_EXCLUSIVE:
  242. vol->exclusive = 0;
  243. }
  244. vol->ref_count -= 1;
  245. spin_unlock(&ubi->volumes_lock);
  246. kfree(desc);
  247. put_device(&vol->dev);
  248. ubi_put_device(ubi);
  249. module_put(THIS_MODULE);
  250. }
  251. EXPORT_SYMBOL_GPL(ubi_close_volume);
  252. /**
  253. * ubi_leb_read - read data.
  254. * @desc: volume descriptor
  255. * @lnum: logical eraseblock number to read from
  256. * @buf: buffer where to store the read data
  257. * @offset: offset within the logical eraseblock to read from
  258. * @len: how many bytes to read
  259. * @check: whether UBI has to check the read data's CRC or not.
  260. *
  261. * This function reads data from offset @offset of logical eraseblock @lnum and
  262. * stores the data at @buf. When reading from static volumes, @check specifies
  263. * whether the data has to be checked or not. If yes, the whole logical
  264. * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
  265. * checksum is per-eraseblock). So checking may substantially slow down the
  266. * read speed. The @check argument is ignored for dynamic volumes.
  267. *
  268. * In case of success, this function returns zero. In case of failure, this
  269. * function returns a negative error code.
  270. *
  271. * %-EBADMSG error code is returned:
  272. * o for both static and dynamic volumes if MTD driver has detected a data
  273. * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
  274. * o for static volumes in case of data CRC mismatch.
  275. *
  276. * If the volume is damaged because of an interrupted update this function just
  277. * returns immediately with %-EBADF error code.
  278. */
  279. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  280. int len, int check)
  281. {
  282. struct ubi_volume *vol = desc->vol;
  283. struct ubi_device *ubi = vol->ubi;
  284. int err, vol_id = vol->vol_id;
  285. dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
  286. if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
  287. lnum >= vol->used_ebs || offset < 0 || len < 0 ||
  288. offset + len > vol->usable_leb_size)
  289. return -EINVAL;
  290. if (vol->vol_type == UBI_STATIC_VOLUME) {
  291. if (vol->used_ebs == 0)
  292. /* Empty static UBI volume */
  293. return 0;
  294. if (lnum == vol->used_ebs - 1 &&
  295. offset + len > vol->last_eb_bytes)
  296. return -EINVAL;
  297. }
  298. if (vol->upd_marker)
  299. return -EBADF;
  300. if (len == 0)
  301. return 0;
  302. err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check);
  303. if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
  304. ubi_warn("mark volume %d as corrupted", vol_id);
  305. vol->corrupted = 1;
  306. }
  307. return err;
  308. }
  309. EXPORT_SYMBOL_GPL(ubi_leb_read);
  310. /**
  311. * ubi_leb_write - write data.
  312. * @desc: volume descriptor
  313. * @lnum: logical eraseblock number to write to
  314. * @buf: data to write
  315. * @offset: offset within the logical eraseblock where to write
  316. * @len: how many bytes to write
  317. * @dtype: expected data type
  318. *
  319. * This function writes @len bytes of data from @buf to offset @offset of
  320. * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
  321. * the data.
  322. *
  323. * This function takes care of physical eraseblock write failures. If write to
  324. * the physical eraseblock write operation fails, the logical eraseblock is
  325. * re-mapped to another physical eraseblock, the data is recovered, and the
  326. * write finishes. UBI has a pool of reserved physical eraseblocks for this.
  327. *
  328. * If all the data were successfully written, zero is returned. If an error
  329. * occurred and UBI has not been able to recover from it, this function returns
  330. * a negative error code. Note, in case of an error, it is possible that
  331. * something was still written to the flash media, but that may be some
  332. * garbage.
  333. *
  334. * If the volume is damaged because of an interrupted update this function just
  335. * returns immediately with %-EBADF code.
  336. */
  337. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  338. int offset, int len, int dtype)
  339. {
  340. struct ubi_volume *vol = desc->vol;
  341. struct ubi_device *ubi = vol->ubi;
  342. int vol_id = vol->vol_id;
  343. dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
  344. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  345. return -EINVAL;
  346. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  347. return -EROFS;
  348. if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
  349. offset + len > vol->usable_leb_size ||
  350. offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
  351. return -EINVAL;
  352. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  353. dtype != UBI_UNKNOWN)
  354. return -EINVAL;
  355. if (vol->upd_marker)
  356. return -EBADF;
  357. if (len == 0)
  358. return 0;
  359. return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype);
  360. }
  361. EXPORT_SYMBOL_GPL(ubi_leb_write);
  362. /*
  363. * ubi_leb_change - change logical eraseblock atomically.
  364. * @desc: volume descriptor
  365. * @lnum: logical eraseblock number to change
  366. * @buf: data to write
  367. * @len: how many bytes to write
  368. * @dtype: expected data type
  369. *
  370. * This function changes the contents of a logical eraseblock atomically. @buf
  371. * has to contain new logical eraseblock data, and @len - the length of the
  372. * data, which has to be aligned. The length may be shorter then the logical
  373. * eraseblock size, ant the logical eraseblock may be appended to more times
  374. * later on. This function guarantees that in case of an unclean reboot the old
  375. * contents is preserved. Returns zero in case of success and a negative error
  376. * code in case of failure.
  377. */
  378. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  379. int len, int dtype)
  380. {
  381. struct ubi_volume *vol = desc->vol;
  382. struct ubi_device *ubi = vol->ubi;
  383. int vol_id = vol->vol_id;
  384. dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
  385. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  386. return -EINVAL;
  387. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  388. return -EROFS;
  389. if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
  390. len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
  391. return -EINVAL;
  392. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  393. dtype != UBI_UNKNOWN)
  394. return -EINVAL;
  395. if (vol->upd_marker)
  396. return -EBADF;
  397. if (len == 0)
  398. return 0;
  399. return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype);
  400. }
  401. EXPORT_SYMBOL_GPL(ubi_leb_change);
  402. /**
  403. * ubi_leb_erase - erase logical eraseblock.
  404. * @desc: volume descriptor
  405. * @lnum: logical eraseblock number
  406. *
  407. * This function un-maps logical eraseblock @lnum and synchronously erases the
  408. * correspondent physical eraseblock. Returns zero in case of success and a
  409. * negative error code in case of failure.
  410. *
  411. * If the volume is damaged because of an interrupted update this function just
  412. * returns immediately with %-EBADF code.
  413. */
  414. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
  415. {
  416. struct ubi_volume *vol = desc->vol;
  417. struct ubi_device *ubi = vol->ubi;
  418. int err;
  419. dbg_gen("erase LEB %d:%d", vol->vol_id, lnum);
  420. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  421. return -EROFS;
  422. if (lnum < 0 || lnum >= vol->reserved_pebs)
  423. return -EINVAL;
  424. if (vol->upd_marker)
  425. return -EBADF;
  426. err = ubi_eba_unmap_leb(ubi, vol, lnum);
  427. if (err)
  428. return err;
  429. return ubi_wl_flush(ubi);
  430. }
  431. EXPORT_SYMBOL_GPL(ubi_leb_erase);
  432. /**
  433. * ubi_leb_unmap - un-map logical eraseblock.
  434. * @desc: volume descriptor
  435. * @lnum: logical eraseblock number
  436. *
  437. * This function un-maps logical eraseblock @lnum and schedules the
  438. * corresponding physical eraseblock for erasure, so that it will eventually be
  439. * physically erased in background. This operation is much faster then the
  440. * erase operation.
  441. *
  442. * Unlike erase, the un-map operation does not guarantee that the logical
  443. * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
  444. * example, if several logical eraseblocks are un-mapped, and an unclean reboot
  445. * happens after this, the logical eraseblocks will not necessarily be
  446. * un-mapped again when this MTD device is attached. They may actually be
  447. * mapped to the same physical eraseblocks again. So, this function has to be
  448. * used with care.
  449. *
  450. * In other words, when un-mapping a logical eraseblock, UBI does not store
  451. * any information about this on the flash media, it just marks the logical
  452. * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
  453. * eraseblock is physically erased, it will be mapped again to the same logical
  454. * eraseblock when the MTD device is attached again.
  455. *
  456. * The main and obvious use-case of this function is when the contents of a
  457. * logical eraseblock has to be re-written. Then it is much more efficient to
  458. * first un-map it, then write new data, rather then first erase it, then write
  459. * new data. Note, once new data has been written to the logical eraseblock,
  460. * UBI guarantees that the old contents has gone forever. In other words, if an
  461. * unclean reboot happens after the logical eraseblock has been un-mapped and
  462. * then written to, it will contain the last written data.
  463. *
  464. * This function returns zero in case of success and a negative error code in
  465. * case of failure. If the volume is damaged because of an interrupted update
  466. * this function just returns immediately with %-EBADF code.
  467. */
  468. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
  469. {
  470. struct ubi_volume *vol = desc->vol;
  471. struct ubi_device *ubi = vol->ubi;
  472. dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
  473. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  474. return -EROFS;
  475. if (lnum < 0 || lnum >= vol->reserved_pebs)
  476. return -EINVAL;
  477. if (vol->upd_marker)
  478. return -EBADF;
  479. return ubi_eba_unmap_leb(ubi, vol, lnum);
  480. }
  481. EXPORT_SYMBOL_GPL(ubi_leb_unmap);
  482. /**
  483. * ubi_leb_map - map logical erasblock to a physical eraseblock.
  484. * @desc: volume descriptor
  485. * @lnum: logical eraseblock number
  486. * @dtype: expected data type
  487. *
  488. * This function maps an un-mapped logical eraseblock @lnum to a physical
  489. * eraseblock. This means, that after a successfull invocation of this
  490. * function the logical eraseblock @lnum will be empty (contain only %0xFF
  491. * bytes) and be mapped to a physical eraseblock, even if an unclean reboot
  492. * happens.
  493. *
  494. * This function returns zero in case of success, %-EBADF if the volume is
  495. * damaged because of an interrupted update, %-EBADMSG if the logical
  496. * eraseblock is already mapped, and other negative error codes in case of
  497. * other failures.
  498. */
  499. int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
  500. {
  501. struct ubi_volume *vol = desc->vol;
  502. struct ubi_device *ubi = vol->ubi;
  503. dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum);
  504. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  505. return -EROFS;
  506. if (lnum < 0 || lnum >= vol->reserved_pebs)
  507. return -EINVAL;
  508. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  509. dtype != UBI_UNKNOWN)
  510. return -EINVAL;
  511. if (vol->upd_marker)
  512. return -EBADF;
  513. if (vol->eba_tbl[lnum] >= 0)
  514. return -EBADMSG;
  515. return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
  516. }
  517. EXPORT_SYMBOL_GPL(ubi_leb_map);
  518. /**
  519. * ubi_is_mapped - check if logical eraseblock is mapped.
  520. * @desc: volume descriptor
  521. * @lnum: logical eraseblock number
  522. *
  523. * This function checks if logical eraseblock @lnum is mapped to a physical
  524. * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
  525. * mean it will still be un-mapped after the UBI device is re-attached. The
  526. * logical eraseblock may become mapped to the physical eraseblock it was last
  527. * mapped to.
  528. *
  529. * This function returns %1 if the LEB is mapped, %0 if not, and a negative
  530. * error code in case of failure. If the volume is damaged because of an
  531. * interrupted update this function just returns immediately with %-EBADF error
  532. * code.
  533. */
  534. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
  535. {
  536. struct ubi_volume *vol = desc->vol;
  537. dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
  538. if (lnum < 0 || lnum >= vol->reserved_pebs)
  539. return -EINVAL;
  540. if (vol->upd_marker)
  541. return -EBADF;
  542. return vol->eba_tbl[lnum] >= 0;
  543. }
  544. EXPORT_SYMBOL_GPL(ubi_is_mapped);
  545. /**
  546. * ubi_sync - synchronize UBI device buffers.
  547. * @ubi_num: UBI device to synchronize
  548. *
  549. * The underlying MTD device may cache data in hardware or in software. This
  550. * function ensures the caches are flushed. Returns zero in case of success and
  551. * a negative error code in case of failure.
  552. */
  553. int ubi_sync(int ubi_num)
  554. {
  555. struct ubi_device *ubi;
  556. ubi = ubi_get_device(ubi_num);
  557. if (!ubi)
  558. return -ENODEV;
  559. if (ubi->mtd->sync)
  560. ubi->mtd->sync(ubi->mtd);
  561. ubi_put_device(ubi);
  562. return 0;
  563. }
  564. EXPORT_SYMBOL_GPL(ubi_sync);