kapi.c 25 KB

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