kapi.c 17 KB

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