kapi.c 23 KB

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