kapi.c 22 KB

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