kapi.c 24 KB

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