kapi.c 25 KB

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