kapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 <asm/div64.h>
  24. #include "ubi.h"
  25. /**
  26. * ubi_get_device_info - get information about UBI device.
  27. * @ubi_num: UBI device number
  28. * @di: the information is stored here
  29. *
  30. * This function returns %0 in case of success and a %-ENODEV if there is no
  31. * such UBI device.
  32. */
  33. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
  34. {
  35. const struct ubi_device *ubi;
  36. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
  37. !ubi_devices[ubi_num])
  38. return -ENODEV;
  39. ubi = ubi_devices[ubi_num];
  40. di->ubi_num = ubi->ubi_num;
  41. di->leb_size = ubi->leb_size;
  42. di->min_io_size = ubi->min_io_size;
  43. di->ro_mode = ubi->ro_mode;
  44. di->cdev = MKDEV(ubi->major, 0);
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(ubi_get_device_info);
  48. /**
  49. * ubi_get_volume_info - get information about UBI volume.
  50. * @desc: volume descriptor
  51. * @vi: the information is stored here
  52. */
  53. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  54. struct ubi_volume_info *vi)
  55. {
  56. const struct ubi_volume *vol = desc->vol;
  57. const struct ubi_device *ubi = vol->ubi;
  58. vi->vol_id = vol->vol_id;
  59. vi->ubi_num = ubi->ubi_num;
  60. vi->size = vol->reserved_pebs;
  61. vi->used_bytes = vol->used_bytes;
  62. vi->vol_type = vol->vol_type;
  63. vi->corrupted = vol->corrupted;
  64. vi->upd_marker = vol->upd_marker;
  65. vi->alignment = vol->alignment;
  66. vi->usable_leb_size = vol->usable_leb_size;
  67. vi->name_len = vol->name_len;
  68. vi->name = vol->name;
  69. vi->cdev = MKDEV(ubi->major, vi->vol_id + 1);
  70. }
  71. EXPORT_SYMBOL_GPL(ubi_get_volume_info);
  72. /**
  73. * ubi_open_volume - open UBI volume.
  74. * @ubi_num: UBI device number
  75. * @vol_id: volume ID
  76. * @mode: open mode
  77. *
  78. * The @mode parameter specifies if the volume should be opened in read-only
  79. * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
  80. * nobody else will be able to open this volume. UBI allows to have many volume
  81. * readers and one writer at a time.
  82. *
  83. * If a static volume is being opened for the first time since boot, it will be
  84. * checked by this function, which means it will be fully read and the CRC
  85. * checksum of each logical eraseblock will be checked.
  86. *
  87. * This function returns volume descriptor in case of success and a negative
  88. * error code in case of failure.
  89. */
  90. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
  91. {
  92. int err;
  93. struct ubi_volume_desc *desc;
  94. struct ubi_device *ubi = ubi_devices[ubi_num];
  95. struct ubi_volume *vol;
  96. dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
  97. err = -ENODEV;
  98. if (!try_module_get(THIS_MODULE))
  99. return ERR_PTR(err);
  100. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
  101. goto out_put;
  102. err = -EINVAL;
  103. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  104. goto out_put;
  105. if (mode != UBI_READONLY && mode != UBI_READWRITE &&
  106. mode != UBI_EXCLUSIVE)
  107. goto out_put;
  108. desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
  109. if (!desc) {
  110. err = -ENOMEM;
  111. goto out_put;
  112. }
  113. spin_lock(&ubi->volumes_lock);
  114. vol = ubi->volumes[vol_id];
  115. if (!vol) {
  116. err = -ENODEV;
  117. goto out_unlock;
  118. }
  119. err = -EBUSY;
  120. switch (mode) {
  121. case UBI_READONLY:
  122. if (vol->exclusive)
  123. goto out_unlock;
  124. vol->readers += 1;
  125. break;
  126. case UBI_READWRITE:
  127. if (vol->exclusive || vol->writers > 0)
  128. goto out_unlock;
  129. vol->writers += 1;
  130. break;
  131. case UBI_EXCLUSIVE:
  132. if (vol->exclusive || vol->writers || vol->readers)
  133. goto out_unlock;
  134. vol->exclusive = 1;
  135. break;
  136. }
  137. spin_unlock(&ubi->volumes_lock);
  138. desc->vol = vol;
  139. desc->mode = mode;
  140. /*
  141. * To prevent simultaneous checks of the same volume we use @vtbl_mutex,
  142. * although it is not the purpose it was introduced for.
  143. */
  144. mutex_lock(&ubi->vtbl_mutex);
  145. if (!vol->checked) {
  146. /* This is the first open - check the volume */
  147. err = ubi_check_volume(ubi, vol_id);
  148. if (err < 0) {
  149. mutex_unlock(&ubi->vtbl_mutex);
  150. ubi_close_volume(desc);
  151. return ERR_PTR(err);
  152. }
  153. if (err == 1) {
  154. ubi_warn("volume %d on UBI device %d is corrupted",
  155. vol_id, ubi->ubi_num);
  156. vol->corrupted = 1;
  157. }
  158. vol->checked = 1;
  159. }
  160. mutex_unlock(&ubi->vtbl_mutex);
  161. return desc;
  162. out_unlock:
  163. spin_unlock(&ubi->volumes_lock);
  164. kfree(desc);
  165. out_put:
  166. module_put(THIS_MODULE);
  167. return ERR_PTR(err);
  168. }
  169. EXPORT_SYMBOL_GPL(ubi_open_volume);
  170. /**
  171. * ubi_open_volume_nm - open UBI volume by name.
  172. * @ubi_num: UBI device number
  173. * @name: volume name
  174. * @mode: open mode
  175. *
  176. * This function is similar to 'ubi_open_volume()', but opens a volume by name.
  177. */
  178. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  179. int mode)
  180. {
  181. int i, vol_id = -1, len;
  182. struct ubi_volume_desc *ret;
  183. struct ubi_device *ubi;
  184. dbg_msg("open volume %s, mode %d", name, mode);
  185. if (!name)
  186. return ERR_PTR(-EINVAL);
  187. len = strnlen(name, UBI_VOL_NAME_MAX + 1);
  188. if (len > UBI_VOL_NAME_MAX)
  189. return ERR_PTR(-EINVAL);
  190. ret = ERR_PTR(-ENODEV);
  191. if (!try_module_get(THIS_MODULE))
  192. return ret;
  193. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num])
  194. goto out_put;
  195. ubi = ubi_devices[ubi_num];
  196. spin_lock(&ubi->volumes_lock);
  197. /* Walk all volumes of this UBI device */
  198. for (i = 0; i < ubi->vtbl_slots; i++) {
  199. struct ubi_volume *vol = ubi->volumes[i];
  200. if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
  201. vol_id = i;
  202. break;
  203. }
  204. }
  205. spin_unlock(&ubi->volumes_lock);
  206. if (vol_id < 0)
  207. goto out_put;
  208. ret = ubi_open_volume(ubi_num, vol_id, mode);
  209. out_put:
  210. module_put(THIS_MODULE);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
  214. /**
  215. * ubi_close_volume - close UBI volume.
  216. * @desc: volume descriptor
  217. */
  218. void ubi_close_volume(struct ubi_volume_desc *desc)
  219. {
  220. struct ubi_volume *vol = desc->vol;
  221. dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
  222. spin_lock(&vol->ubi->volumes_lock);
  223. switch (desc->mode) {
  224. case UBI_READONLY:
  225. vol->readers -= 1;
  226. break;
  227. case UBI_READWRITE:
  228. vol->writers -= 1;
  229. break;
  230. case UBI_EXCLUSIVE:
  231. vol->exclusive = 0;
  232. }
  233. spin_unlock(&vol->ubi->volumes_lock);
  234. kfree(desc);
  235. module_put(THIS_MODULE);
  236. }
  237. EXPORT_SYMBOL_GPL(ubi_close_volume);
  238. /**
  239. * ubi_leb_read - read data.
  240. * @desc: volume descriptor
  241. * @lnum: logical eraseblock number to read from
  242. * @buf: buffer where to store the read data
  243. * @offset: offset within the logical eraseblock to read from
  244. * @len: how many bytes to read
  245. * @check: whether UBI has to check the read data's CRC or not.
  246. *
  247. * This function reads data from offset @offset of logical eraseblock @lnum and
  248. * stores the data at @buf. When reading from static volumes, @check specifies
  249. * whether the data has to be checked or not. If yes, the whole logical
  250. * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
  251. * checksum is per-eraseblock). So checking may substantially slow down the
  252. * read speed. The @check argument is ignored for dynamic volumes.
  253. *
  254. * In case of success, this function returns zero. In case of failure, this
  255. * function returns a negative error code.
  256. *
  257. * %-EBADMSG error code is returned:
  258. * o for both static and dynamic volumes if MTD driver has detected a data
  259. * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
  260. * o for static volumes in case of data CRC mismatch.
  261. *
  262. * If the volume is damaged because of an interrupted update this function just
  263. * returns immediately with %-EBADF error code.
  264. */
  265. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  266. int len, int check)
  267. {
  268. struct ubi_volume *vol = desc->vol;
  269. struct ubi_device *ubi = vol->ubi;
  270. int err, vol_id = vol->vol_id;
  271. dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
  272. if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
  273. lnum >= vol->used_ebs || offset < 0 || len < 0 ||
  274. offset + len > vol->usable_leb_size)
  275. return -EINVAL;
  276. if (vol->vol_type == UBI_STATIC_VOLUME) {
  277. if (vol->used_ebs == 0)
  278. /* Empty static UBI volume */
  279. return 0;
  280. if (lnum == vol->used_ebs - 1 &&
  281. offset + len > vol->last_eb_bytes)
  282. return -EINVAL;
  283. }
  284. if (vol->upd_marker)
  285. return -EBADF;
  286. if (len == 0)
  287. return 0;
  288. err = ubi_eba_read_leb(ubi, vol_id, lnum, buf, offset, len, check);
  289. if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) {
  290. ubi_warn("mark volume %d as corrupted", vol_id);
  291. vol->corrupted = 1;
  292. }
  293. return err;
  294. }
  295. EXPORT_SYMBOL_GPL(ubi_leb_read);
  296. /**
  297. * ubi_leb_write - write data.
  298. * @desc: volume descriptor
  299. * @lnum: logical eraseblock number to write to
  300. * @buf: data to write
  301. * @offset: offset within the logical eraseblock where to write
  302. * @len: how many bytes to write
  303. * @dtype: expected data type
  304. *
  305. * This function writes @len bytes of data from @buf to offset @offset of
  306. * logical eraseblock @lnum. The @dtype argument describes expected lifetime of
  307. * the data.
  308. *
  309. * This function takes care of physical eraseblock write failures. If write to
  310. * the physical eraseblock write operation fails, the logical eraseblock is
  311. * re-mapped to another physical eraseblock, the data is recovered, and the
  312. * write finishes. UBI has a pool of reserved physical eraseblocks for this.
  313. *
  314. * If all the data were successfully written, zero is returned. If an error
  315. * occurred and UBI has not been able to recover from it, this function returns
  316. * a negative error code. Note, in case of an error, it is possible that
  317. * something was still written to the flash media, but that may be some
  318. * garbage.
  319. *
  320. * If the volume is damaged because of an interrupted update this function just
  321. * returns immediately with %-EBADF code.
  322. */
  323. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  324. int offset, int len, int dtype)
  325. {
  326. struct ubi_volume *vol = desc->vol;
  327. struct ubi_device *ubi = vol->ubi;
  328. int vol_id = vol->vol_id;
  329. dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset);
  330. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  331. return -EINVAL;
  332. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  333. return -EROFS;
  334. if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
  335. offset + len > vol->usable_leb_size || offset % ubi->min_io_size ||
  336. len % ubi->min_io_size)
  337. return -EINVAL;
  338. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  339. dtype != UBI_UNKNOWN)
  340. return -EINVAL;
  341. if (vol->upd_marker)
  342. return -EBADF;
  343. if (len == 0)
  344. return 0;
  345. return ubi_eba_write_leb(ubi, vol_id, lnum, buf, offset, len, dtype);
  346. }
  347. EXPORT_SYMBOL_GPL(ubi_leb_write);
  348. /*
  349. * ubi_leb_change - change logical eraseblock atomically.
  350. * @desc: volume descriptor
  351. * @lnum: logical eraseblock number to change
  352. * @buf: data to write
  353. * @len: how many bytes to write
  354. * @dtype: expected data type
  355. *
  356. * This function changes the contents of a logical eraseblock atomically. @buf
  357. * has to contain new logical eraseblock data, and @len - the length of the
  358. * data, which has to be aligned. The length may be shorter then the logical
  359. * eraseblock size, ant the logical eraseblock may be appended to more times
  360. * later on. This function guarantees that in case of an unclean reboot the old
  361. * contents is preserved. Returns zero in case of success and a negative error
  362. * code in case of failure.
  363. */
  364. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  365. int len, int dtype)
  366. {
  367. struct ubi_volume *vol = desc->vol;
  368. struct ubi_device *ubi = vol->ubi;
  369. int vol_id = vol->vol_id;
  370. dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum);
  371. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  372. return -EINVAL;
  373. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  374. return -EROFS;
  375. if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
  376. len > vol->usable_leb_size || len % ubi->min_io_size)
  377. return -EINVAL;
  378. if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
  379. dtype != UBI_UNKNOWN)
  380. return -EINVAL;
  381. if (vol->upd_marker)
  382. return -EBADF;
  383. if (len == 0)
  384. return 0;
  385. return ubi_eba_atomic_leb_change(ubi, vol_id, lnum, buf, len, dtype);
  386. }
  387. EXPORT_SYMBOL_GPL(ubi_leb_change);
  388. /**
  389. * ubi_leb_erase - erase logical eraseblock.
  390. * @desc: volume descriptor
  391. * @lnum: logical eraseblock number
  392. *
  393. * This function un-maps logical eraseblock @lnum and synchronously erases the
  394. * correspondent physical eraseblock. Returns zero in case of success and a
  395. * negative error code in case of failure.
  396. *
  397. * If the volume is damaged because of an interrupted update this function just
  398. * returns immediately with %-EBADF code.
  399. */
  400. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
  401. {
  402. struct ubi_volume *vol = desc->vol;
  403. struct ubi_device *ubi = vol->ubi;
  404. int err, vol_id = vol->vol_id;
  405. dbg_msg("erase LEB %d:%d", vol_id, lnum);
  406. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  407. return -EROFS;
  408. if (lnum < 0 || lnum >= vol->reserved_pebs)
  409. return -EINVAL;
  410. if (vol->upd_marker)
  411. return -EBADF;
  412. err = ubi_eba_unmap_leb(ubi, vol_id, lnum);
  413. if (err)
  414. return err;
  415. return ubi_wl_flush(ubi);
  416. }
  417. EXPORT_SYMBOL_GPL(ubi_leb_erase);
  418. /**
  419. * ubi_leb_unmap - un-map logical eraseblock.
  420. * @desc: volume descriptor
  421. * @lnum: logical eraseblock number
  422. *
  423. * This function un-maps logical eraseblock @lnum and schedules the
  424. * corresponding physical eraseblock for erasure, so that it will eventually be
  425. * physically erased in background. This operation is much faster then the
  426. * erase operation.
  427. *
  428. * Unlike erase, the un-map operation does not guarantee that the logical
  429. * eraseblock will contain all 0xFF bytes when UBI is initialized again. For
  430. * example, if several logical eraseblocks are un-mapped, and an unclean reboot
  431. * happens after this, the logical eraseblocks will not necessarily be
  432. * un-mapped again when this MTD device is attached. They may actually be
  433. * mapped to the same physical eraseblocks again. So, this function has to be
  434. * used with care.
  435. *
  436. * In other words, when un-mapping a logical eraseblock, UBI does not store
  437. * any information about this on the flash media, it just marks the logical
  438. * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical
  439. * eraseblock is physically erased, it will be mapped again to the same logical
  440. * eraseblock when the MTD device is attached again.
  441. *
  442. * The main and obvious use-case of this function is when the contents of a
  443. * logical eraseblock has to be re-written. Then it is much more efficient to
  444. * first un-map it, then write new data, rather then first erase it, then write
  445. * new data. Note, once new data has been written to the logical eraseblock,
  446. * UBI guarantees that the old contents has gone forever. In other words, if an
  447. * unclean reboot happens after the logical eraseblock has been un-mapped and
  448. * then written to, it will contain the last written data.
  449. *
  450. * This function returns zero in case of success and a negative error code in
  451. * case of failure. If the volume is damaged because of an interrupted update
  452. * this function just returns immediately with %-EBADF code.
  453. */
  454. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum)
  455. {
  456. struct ubi_volume *vol = desc->vol;
  457. struct ubi_device *ubi = vol->ubi;
  458. int vol_id = vol->vol_id;
  459. dbg_msg("unmap LEB %d:%d", vol_id, lnum);
  460. if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
  461. return -EROFS;
  462. if (lnum < 0 || lnum >= vol->reserved_pebs)
  463. return -EINVAL;
  464. if (vol->upd_marker)
  465. return -EBADF;
  466. return ubi_eba_unmap_leb(ubi, vol_id, lnum);
  467. }
  468. EXPORT_SYMBOL_GPL(ubi_leb_unmap);
  469. /**
  470. * ubi_is_mapped - check if logical eraseblock is mapped.
  471. * @desc: volume descriptor
  472. * @lnum: logical eraseblock number
  473. *
  474. * This function checks if logical eraseblock @lnum is mapped to a physical
  475. * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
  476. * mean it will still be un-mapped after the UBI device is re-attached. The
  477. * logical eraseblock may become mapped to the physical eraseblock it was last
  478. * mapped to.
  479. *
  480. * This function returns %1 if the LEB is mapped, %0 if not, and a negative
  481. * error code in case of failure. If the volume is damaged because of an
  482. * interrupted update this function just returns immediately with %-EBADF error
  483. * code.
  484. */
  485. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum)
  486. {
  487. struct ubi_volume *vol = desc->vol;
  488. dbg_msg("test LEB %d:%d", vol->vol_id, lnum);
  489. if (lnum < 0 || lnum >= vol->reserved_pebs)
  490. return -EINVAL;
  491. if (vol->upd_marker)
  492. return -EBADF;
  493. return vol->eba_tbl[lnum] >= 0;
  494. }
  495. EXPORT_SYMBOL_GPL(ubi_is_mapped);