kapi.c 24 KB

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