gluebi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 (Битюцкий Артём), Joern Engel
  19. */
  20. /*
  21. * This file includes implementation of fake MTD devices for each UBI volume.
  22. * This sounds strange, but it is in fact quite useful to make MTD-oriented
  23. * software (including all the legacy software) to work on top of UBI.
  24. *
  25. * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
  26. * size (mtd->writesize) is equivalent to the UBI minimal I/O unit. The
  27. * eraseblock size is equivalent to the logical eraseblock size of the volume.
  28. */
  29. #include <asm/div64.h>
  30. #include "ubi.h"
  31. /**
  32. * gluebi_get_device - get MTD device reference.
  33. * @mtd: the MTD device description object
  34. *
  35. * This function is called every time the MTD device is being opened and
  36. * implements the MTD get_device() operation. Returns zero in case of success
  37. * and a negative error code in case of failure.
  38. */
  39. static int gluebi_get_device(struct mtd_info *mtd)
  40. {
  41. struct ubi_volume *vol;
  42. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  43. /*
  44. * We do not introduce locks for gluebi reference count because the
  45. * get_device()/put_device() calls are already serialized at MTD.
  46. */
  47. if (vol->gluebi_refcount > 0) {
  48. /*
  49. * The MTD device is already referenced and this is just one
  50. * more reference. MTD allows many users to open the same
  51. * volume simultaneously and do not distinguish between
  52. * readers/writers/exclusive openers as UBI does. So we do not
  53. * open the UBI volume again - just increase the reference
  54. * counter and return.
  55. */
  56. vol->gluebi_refcount += 1;
  57. return 0;
  58. }
  59. /*
  60. * This is the first reference to this UBI volume via the MTD device
  61. * interface. Open the corresponding volume in read-write mode.
  62. */
  63. vol->gluebi_desc = ubi_open_volume(vol->ubi->ubi_num, vol->vol_id,
  64. UBI_READWRITE);
  65. if (IS_ERR(vol->gluebi_desc))
  66. return PTR_ERR(vol->gluebi_desc);
  67. vol->gluebi_refcount += 1;
  68. return 0;
  69. }
  70. /**
  71. * gluebi_put_device - put MTD device reference.
  72. * @mtd: the MTD device description object
  73. *
  74. * This function is called every time the MTD device is being put. Returns
  75. * zero in case of success and a negative error code in case of failure.
  76. */
  77. static void gluebi_put_device(struct mtd_info *mtd)
  78. {
  79. struct ubi_volume *vol;
  80. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  81. vol->gluebi_refcount -= 1;
  82. ubi_assert(vol->gluebi_refcount >= 0);
  83. if (vol->gluebi_refcount == 0)
  84. ubi_close_volume(vol->gluebi_desc);
  85. }
  86. /**
  87. * gluebi_read - read operation of emulated MTD devices.
  88. * @mtd: MTD device description object
  89. * @from: absolute offset from where to read
  90. * @len: how many bytes to read
  91. * @retlen: count of read bytes is returned here
  92. * @buf: buffer to store the read data
  93. *
  94. * This function returns zero in case of success and a negative error code in
  95. * case of failure.
  96. */
  97. static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
  98. size_t *retlen, unsigned char *buf)
  99. {
  100. int err = 0, lnum, offs, total_read;
  101. struct ubi_volume *vol;
  102. struct ubi_device *ubi;
  103. uint64_t tmp = from;
  104. dbg_msg("read %zd bytes from offset %lld", len, from);
  105. if (len < 0 || from < 0 || from + len > mtd->size)
  106. return -EINVAL;
  107. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  108. ubi = vol->ubi;
  109. offs = do_div(tmp, mtd->erasesize);
  110. lnum = tmp;
  111. total_read = len;
  112. while (total_read) {
  113. size_t to_read = mtd->erasesize - offs;
  114. if (to_read > total_read)
  115. to_read = total_read;
  116. err = ubi_eba_read_leb(ubi, vol->vol_id, lnum, buf, offs,
  117. to_read, 0);
  118. if (err)
  119. break;
  120. lnum += 1;
  121. offs = 0;
  122. total_read -= to_read;
  123. buf += to_read;
  124. }
  125. *retlen = len - total_read;
  126. return err;
  127. }
  128. /**
  129. * gluebi_write - write operation of emulated MTD devices.
  130. * @mtd: MTD device description object
  131. * @to: absolute offset where to write
  132. * @len: how many bytes to write
  133. * @retlen: count of written bytes is returned here
  134. * @buf: buffer with data to write
  135. *
  136. * This function returns zero in case of success and a negative error code in
  137. * case of failure.
  138. */
  139. static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
  140. size_t *retlen, const u_char *buf)
  141. {
  142. int err = 0, lnum, offs, total_written;
  143. struct ubi_volume *vol;
  144. struct ubi_device *ubi;
  145. uint64_t tmp = to;
  146. dbg_msg("write %zd bytes to offset %lld", len, to);
  147. if (len < 0 || to < 0 || len + to > mtd->size)
  148. return -EINVAL;
  149. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  150. ubi = vol->ubi;
  151. if (ubi->ro_mode)
  152. return -EROFS;
  153. offs = do_div(tmp, mtd->erasesize);
  154. lnum = tmp;
  155. if (len % mtd->writesize || offs % mtd->writesize)
  156. return -EINVAL;
  157. total_written = len;
  158. while (total_written) {
  159. size_t to_write = mtd->erasesize - offs;
  160. if (to_write > total_written)
  161. to_write = total_written;
  162. err = ubi_eba_write_leb(ubi, vol->vol_id, lnum, buf, offs,
  163. to_write, UBI_UNKNOWN);
  164. if (err)
  165. break;
  166. lnum += 1;
  167. offs = 0;
  168. total_written -= to_write;
  169. buf += to_write;
  170. }
  171. *retlen = len - total_written;
  172. return err;
  173. }
  174. /**
  175. * gluebi_erase - erase operation of emulated MTD devices.
  176. * @mtd: the MTD device description object
  177. * @instr: the erase operation description
  178. *
  179. * This function calls the erase callback when finishes. Returns zero in case
  180. * of success and a negative error code in case of failure.
  181. */
  182. static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
  183. {
  184. int err, i, lnum, count;
  185. struct ubi_volume *vol;
  186. struct ubi_device *ubi;
  187. dbg_msg("erase %u bytes at offset %u", instr->len, instr->addr);
  188. if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
  189. return -EINVAL;
  190. if (instr->len < 0 || instr->addr + instr->len > mtd->size)
  191. return -EINVAL;
  192. if (instr->addr % mtd->writesize || instr->len % mtd->writesize)
  193. return -EINVAL;
  194. lnum = instr->addr / mtd->erasesize;
  195. count = instr->len / mtd->erasesize;
  196. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  197. ubi = vol->ubi;
  198. if (ubi->ro_mode)
  199. return -EROFS;
  200. for (i = 0; i < count; i++) {
  201. err = ubi_eba_unmap_leb(ubi, vol->vol_id, lnum + i);
  202. if (err)
  203. goto out_err;
  204. }
  205. /*
  206. * MTD erase operations are synchronous, so we have to make sure the
  207. * physical eraseblock is wiped out.
  208. */
  209. err = ubi_wl_flush(ubi);
  210. if (err)
  211. goto out_err;
  212. instr->state = MTD_ERASE_DONE;
  213. mtd_erase_callback(instr);
  214. return 0;
  215. out_err:
  216. instr->state = MTD_ERASE_FAILED;
  217. instr->fail_addr = lnum * mtd->erasesize;
  218. return err;
  219. }
  220. /**
  221. * ubi_create_gluebi - initialize gluebi for an UBI volume.
  222. * @ubi: UBI device description object
  223. * @vol: volume description object
  224. *
  225. * This function is called when an UBI volume is created in order to create
  226. * corresponding fake MTD device. Returns zero in case of success and a
  227. * negative error code in case of failure.
  228. */
  229. int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
  230. {
  231. struct mtd_info *mtd = &vol->gluebi_mtd;
  232. mtd->name = kmemdup(vol->name, vol->name_len + 1, GFP_KERNEL);
  233. if (!mtd->name)
  234. return -ENOMEM;
  235. mtd->type = MTD_UBIVOLUME;
  236. if (!ubi->ro_mode)
  237. mtd->flags = MTD_WRITEABLE;
  238. mtd->writesize = ubi->min_io_size;
  239. mtd->owner = THIS_MODULE;
  240. mtd->size = vol->usable_leb_size * vol->reserved_pebs;
  241. mtd->erasesize = vol->usable_leb_size;
  242. mtd->read = gluebi_read;
  243. mtd->write = gluebi_write;
  244. mtd->erase = gluebi_erase;
  245. mtd->get_device = gluebi_get_device;
  246. mtd->put_device = gluebi_put_device;
  247. if (add_mtd_device(mtd)) {
  248. ubi_err("cannot not add MTD device\n");
  249. kfree(mtd->name);
  250. return -ENFILE;
  251. }
  252. dbg_msg("added mtd%d (\"%s\"), size %u, EB size %u",
  253. mtd->index, mtd->name, mtd->size, mtd->erasesize);
  254. return 0;
  255. }
  256. /**
  257. * ubi_destroy_gluebi - close gluebi for an UBI volume.
  258. * @vol: volume description object
  259. *
  260. * This function is called when an UBI volume is removed in order to remove
  261. * corresponding fake MTD device. Returns zero in case of success and a
  262. * negative error code in case of failure.
  263. */
  264. int ubi_destroy_gluebi(struct ubi_volume *vol)
  265. {
  266. int err;
  267. struct mtd_info *mtd = &vol->gluebi_mtd;
  268. dbg_msg("remove mtd%d", mtd->index);
  269. err = del_mtd_device(mtd);
  270. if (err)
  271. return err;
  272. kfree(mtd->name);
  273. return 0;
  274. }