kapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 (!try_module_get(THIS_MODULE))
  37. return -ENODEV;
  38. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
  39. !ubi_devices[ubi_num]) {
  40. module_put(THIS_MODULE);
  41. return -ENODEV;
  42. }
  43. ubi = ubi_devices[ubi_num];
  44. di->ubi_num = ubi->ubi_num;
  45. di->leb_size = ubi->leb_size;
  46. di->min_io_size = ubi->min_io_size;
  47. di->ro_mode = ubi->ro_mode;
  48. di->cdev = MKDEV(ubi->major, 0);
  49. module_put(THIS_MODULE);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(ubi_get_device_info);
  53. /**
  54. * ubi_get_volume_info - get information about UBI volume.
  55. * @desc: volume descriptor
  56. * @vi: the information is stored here
  57. */
  58. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  59. struct ubi_volume_info *vi)
  60. {
  61. const struct ubi_volume *vol = desc->vol;
  62. const struct ubi_device *ubi = vol->ubi;
  63. vi->vol_id = vol->vol_id;
  64. vi->ubi_num = ubi->ubi_num;
  65. vi->size = vol->reserved_pebs;
  66. vi->used_bytes = vol->used_bytes;
  67. vi->vol_type = vol->vol_type;
  68. vi->corrupted = vol->corrupted;
  69. vi->upd_marker = vol->upd_marker;
  70. vi->alignment = vol->alignment;
  71. vi->usable_leb_size = vol->usable_leb_size;
  72. vi->name_len = vol->name_len;
  73. vi->name = vol->name;
  74. vi->cdev = MKDEV(ubi->major, vi->vol_id + 1);
  75. }
  76. EXPORT_SYMBOL_GPL(ubi_get_volume_info);
  77. /**
  78. * ubi_open_volume - open UBI volume.
  79. * @ubi_num: UBI device number
  80. * @vol_id: volume ID
  81. * @mode: open mode
  82. *
  83. * The @mode parameter specifies if the volume should be opened in read-only
  84. * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that
  85. * nobody else will be able to open this volume. UBI allows to have many volume
  86. * readers and one writer at a time.
  87. *
  88. * If a static volume is being opened for the first time since boot, it will be
  89. * checked by this function, which means it will be fully read and the CRC
  90. * checksum of each logical eraseblock will be checked.
  91. *
  92. * This function returns volume descriptor in case of success and a negative
  93. * error code in case of failure.
  94. */
  95. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
  96. {
  97. int err;
  98. struct ubi_volume_desc *desc;
  99. struct ubi_device *ubi = ubi_devices[ubi_num];
  100. struct ubi_volume *vol;
  101. dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
  102. err = -ENODEV;
  103. if (!try_module_get(THIS_MODULE))
  104. return ERR_PTR(err);
  105. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
  106. goto out_put;
  107. err = -EINVAL;
  108. if (vol_id < 0 || vol_id >= ubi->vtbl_slots)
  109. goto out_put;
  110. if (mode != UBI_READONLY && mode != UBI_READWRITE &&
  111. mode != UBI_EXCLUSIVE)
  112. goto out_put;
  113. desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL);
  114. if (!desc) {
  115. err = -ENOMEM;
  116. goto out_put;
  117. }
  118. spin_lock(&ubi->volumes_lock);
  119. vol = ubi->volumes[vol_id];
  120. if (!vol) {
  121. err = -ENODEV;
  122. goto out_unlock;
  123. }
  124. err = -EBUSY;
  125. switch (mode) {
  126. case UBI_READONLY:
  127. if (vol->exclusive)
  128. goto out_unlock;
  129. vol->readers += 1;
  130. break;
  131. case UBI_READWRITE:
  132. if (vol->exclusive || vol->writers > 0)
  133. goto out_unlock;
  134. vol->writers += 1;
  135. break;
  136. case UBI_EXCLUSIVE:
  137. if (vol->exclusive || vol->writers || vol->readers)
  138. goto out_unlock;
  139. vol->exclusive = 1;
  140. break;
  141. }
  142. spin_unlock(&ubi->volumes_lock);
  143. desc->vol = vol;
  144. desc->mode = mode;
  145. /*
  146. * To prevent simultaneous checks of the same volume we use @vtbl_mutex,
  147. * although it is not the purpose it was introduced for.
  148. */
  149. mutex_lock(&ubi->vtbl_mutex);
  150. if (!vol->checked) {
  151. /* This is the first open - check the volume */
  152. err = ubi_check_volume(ubi, vol_id);
  153. if (err < 0) {
  154. mutex_unlock(&ubi->vtbl_mutex);
  155. ubi_close_volume(desc);
  156. return ERR_PTR(err);
  157. }
  158. if (err == 1) {
  159. ubi_warn("volume %d on UBI device %d is corrupted",
  160. vol_id, ubi->ubi_num);
  161. vol->corrupted = 1;
  162. }
  163. vol->checked = 1;
  164. }
  165. mutex_unlock(&ubi->vtbl_mutex);
  166. return desc;
  167. out_unlock:
  168. spin_unlock(&ubi->volumes_lock);
  169. kfree(desc);
  170. out_put:
  171. module_put(THIS_MODULE);
  172. return ERR_PTR(err);
  173. }
  174. EXPORT_SYMBOL_GPL(ubi_open_volume);
  175. /**
  176. * ubi_open_volume_nm - open UBI volume by name.
  177. * @ubi_num: UBI device number
  178. * @name: volume name
  179. * @mode: open mode
  180. *
  181. * This function is similar to 'ubi_open_volume()', but opens a volume by name.
  182. */
  183. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  184. int mode)
  185. {
  186. int i, vol_id = -1, len;
  187. struct ubi_volume_desc *ret;
  188. struct ubi_device *ubi;
  189. dbg_msg("open volume %s, mode %d", name, mode);
  190. if (!name)
  191. return ERR_PTR(-EINVAL);
  192. len = strnlen(name, UBI_VOL_NAME_MAX + 1);
  193. if (len > UBI_VOL_NAME_MAX)
  194. return ERR_PTR(-EINVAL);
  195. ret = ERR_PTR(-ENODEV);
  196. if (!try_module_get(THIS_MODULE))
  197. return ret;
  198. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num])
  199. goto out_put;
  200. ubi = ubi_devices[ubi_num];
  201. spin_lock(&ubi->volumes_lock);
  202. /* Walk all volumes of this UBI device */
  203. for (i = 0; i < ubi->vtbl_slots; i++) {
  204. struct ubi_volume *vol = ubi->volumes[i];
  205. if (vol && len == vol->name_len && !strcmp(name, vol->name)) {
  206. vol_id = i;
  207. break;
  208. }
  209. }
  210. spin_unlock(&ubi->volumes_lock);
  211. if (vol_id < 0)
  212. goto out_put;
  213. ret = ubi_open_volume(ubi_num, vol_id, mode);
  214. out_put:
  215. module_put(THIS_MODULE);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
  219. /**
  220. * ubi_close_volume - close UBI volume.
  221. * @desc: volume descriptor
  222. */
  223. void ubi_close_volume(struct ubi_volume_desc *desc)
  224. {
  225. struct ubi_volume *vol = desc->vol;
  226. dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode);
  227. spin_lock(&vol->ubi->volumes_lock);
  228. switch (desc->mode) {
  229. case UBI_READONLY:
  230. vol->readers -= 1;
  231. break;
  232. case UBI_READWRITE:
  233. vol->writers -= 1;
  234. break;
  235. case UBI_EXCLUSIVE:
  236. vol->exclusive = 0;
  237. }
  238. spin_unlock(&vol->ubi->volumes_lock);
  239. kfree(desc);
  240. module_put(THIS_MODULE);
  241. }
  242. EXPORT_SYMBOL_GPL(ubi_close_volume);
  243. /**
  244. * ubi_leb_read - read data.
  245. * @desc: volume descriptor
  246. * @lnum: logical eraseblock number to read from
  247. * @buf: buffer where to store the read data
  248. * @offset: offset within the logical eraseblock to read from
  249. * @len: how many bytes to read
  250. * @check: whether UBI has to check the read data's CRC or not.
  251. *
  252. * This function reads data from offset @offset of logical eraseblock @lnum and
  253. * stores the data at @buf. When reading from static volumes, @check specifies
  254. * whether the data has to be checked or not. If yes, the whole logical
  255. * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC
  256. * checksum is per-eraseblock). So checking may substantially slow down the
  257. * read speed. The @check argument is ignored for dynamic volumes.
  258. *
  259. * In case of success, this function returns zero. In case of failure, this
  260. * function returns a negative error code.
  261. *
  262. * %-EBADMSG error code is returned:
  263. * o for both static and dynamic volumes if MTD driver has detected a data
  264. * integrity problem (unrecoverable ECC checksum mismatch in case of NAND);
  265. * o for static volumes in case of data CRC mismatch.
  266. *
  267. * If the volume is damaged because of an interrupted update this function just
  268. * returns immediately with %-EBADF error code.
  269. */
  270. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  271. int len, int check)
  272. {
  273. struct ubi_volume *vol = desc->vol;
  274. struct ubi_device *ubi = vol->ubi;
  275. int err, vol_id = vol->vol_id;
  276. dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset);
  277. if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 ||
  278. lnum >= vol->used_ebs || offset < 0 || len < 0 ||
  279. offset + len > vol->usable_leb_size)
  280. return -EINVAL;
  281. if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 &&
  282. offset + len > vol->last_eb_bytes)
  283. return -EINVAL;
  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);