kapi.c 24 KB

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