kapi.c 23 KB

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