kapi.c 22 KB

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