gluebi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_gen("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, lnum, buf, offs, to_read, 0);
  117. if (err)
  118. break;
  119. lnum += 1;
  120. offs = 0;
  121. total_read -= to_read;
  122. buf += to_read;
  123. }
  124. *retlen = len - total_read;
  125. return err;
  126. }
  127. /**
  128. * gluebi_write - write operation of emulated MTD devices.
  129. * @mtd: MTD device description object
  130. * @to: absolute offset where to write
  131. * @len: how many bytes to write
  132. * @retlen: count of written bytes is returned here
  133. * @buf: buffer with data to write
  134. *
  135. * This function returns zero in case of success and a negative error code in
  136. * case of failure.
  137. */
  138. static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
  139. size_t *retlen, const u_char *buf)
  140. {
  141. int err = 0, lnum, offs, total_written;
  142. struct ubi_volume *vol;
  143. struct ubi_device *ubi;
  144. uint64_t tmp = to;
  145. dbg_gen("write %zd bytes to offset %lld", len, to);
  146. if (len < 0 || to < 0 || len + to > mtd->size)
  147. return -EINVAL;
  148. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  149. ubi = vol->ubi;
  150. if (ubi->ro_mode)
  151. return -EROFS;
  152. offs = do_div(tmp, mtd->erasesize);
  153. lnum = tmp;
  154. if (len % mtd->writesize || offs % mtd->writesize)
  155. return -EINVAL;
  156. total_written = len;
  157. while (total_written) {
  158. size_t to_write = mtd->erasesize - offs;
  159. if (to_write > total_written)
  160. to_write = total_written;
  161. err = ubi_eba_write_leb(ubi, vol, lnum, buf, offs, to_write,
  162. UBI_UNKNOWN);
  163. if (err)
  164. break;
  165. lnum += 1;
  166. offs = 0;
  167. total_written -= to_write;
  168. buf += to_write;
  169. }
  170. *retlen = len - total_written;
  171. return err;
  172. }
  173. /**
  174. * gluebi_erase - erase operation of emulated MTD devices.
  175. * @mtd: the MTD device description object
  176. * @instr: the erase operation description
  177. *
  178. * This function calls the erase callback when finishes. Returns zero in case
  179. * of success and a negative error code in case of failure.
  180. */
  181. static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
  182. {
  183. int err, i, lnum, count;
  184. struct ubi_volume *vol;
  185. struct ubi_device *ubi;
  186. dbg_gen("erase %u bytes at offset %u", instr->len, instr->addr);
  187. if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
  188. return -EINVAL;
  189. if (instr->len < 0 || instr->addr + instr->len > mtd->size)
  190. return -EINVAL;
  191. if (instr->addr % mtd->writesize || instr->len % mtd->writesize)
  192. return -EINVAL;
  193. lnum = instr->addr / mtd->erasesize;
  194. count = instr->len / mtd->erasesize;
  195. vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
  196. ubi = vol->ubi;
  197. if (ubi->ro_mode)
  198. return -EROFS;
  199. for (i = 0; i < count; i++) {
  200. err = ubi_eba_unmap_leb(ubi, vol, lnum + i);
  201. if (err)
  202. goto out_err;
  203. }
  204. /*
  205. * MTD erase operations are synchronous, so we have to make sure the
  206. * physical eraseblock is wiped out.
  207. */
  208. err = ubi_wl_flush(ubi);
  209. if (err)
  210. goto out_err;
  211. instr->state = MTD_ERASE_DONE;
  212. mtd_erase_callback(instr);
  213. return 0;
  214. out_err:
  215. instr->state = MTD_ERASE_FAILED;
  216. instr->fail_addr = lnum * mtd->erasesize;
  217. return err;
  218. }
  219. /**
  220. * ubi_create_gluebi - initialize gluebi for an UBI volume.
  221. * @ubi: UBI device description object
  222. * @vol: volume description object
  223. *
  224. * This function is called when an UBI volume is created in order to create
  225. * corresponding fake MTD device. Returns zero in case of success and a
  226. * negative error code in case of failure.
  227. */
  228. int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
  229. {
  230. struct mtd_info *mtd = &vol->gluebi_mtd;
  231. mtd->name = kmemdup(vol->name, vol->name_len + 1, GFP_KERNEL);
  232. if (!mtd->name)
  233. return -ENOMEM;
  234. mtd->type = MTD_UBIVOLUME;
  235. if (!ubi->ro_mode)
  236. mtd->flags = MTD_WRITEABLE;
  237. mtd->writesize = ubi->min_io_size;
  238. mtd->owner = THIS_MODULE;
  239. mtd->erasesize = vol->usable_leb_size;
  240. mtd->read = gluebi_read;
  241. mtd->write = gluebi_write;
  242. mtd->erase = gluebi_erase;
  243. mtd->get_device = gluebi_get_device;
  244. mtd->put_device = gluebi_put_device;
  245. /*
  246. * In case of dynamic volume, MTD device size is just volume size. In
  247. * case of a static volume the size is equivalent to the amount of data
  248. * bytes.
  249. */
  250. if (vol->vol_type == UBI_DYNAMIC_VOLUME)
  251. mtd->size = vol->usable_leb_size * vol->reserved_pebs;
  252. else
  253. mtd->size = vol->used_bytes;
  254. if (add_mtd_device(mtd)) {
  255. ubi_err("cannot not add MTD device");
  256. kfree(mtd->name);
  257. return -ENFILE;
  258. }
  259. dbg_gen("added mtd%d (\"%s\"), size %u, EB size %u",
  260. mtd->index, mtd->name, mtd->size, mtd->erasesize);
  261. return 0;
  262. }
  263. /**
  264. * ubi_destroy_gluebi - close gluebi for an UBI volume.
  265. * @vol: volume description object
  266. *
  267. * This function is called when an UBI volume is removed in order to remove
  268. * corresponding fake MTD device. Returns zero in case of success and a
  269. * negative error code in case of failure.
  270. */
  271. int ubi_destroy_gluebi(struct ubi_volume *vol)
  272. {
  273. int err;
  274. struct mtd_info *mtd = &vol->gluebi_mtd;
  275. dbg_gen("remove mtd%d", mtd->index);
  276. err = del_mtd_device(mtd);
  277. if (err)
  278. return err;
  279. kfree(mtd->name);
  280. return 0;
  281. }
  282. /**
  283. * ubi_gluebi_updated - UBI volume was updated notifier.
  284. * @vol: volume description object
  285. *
  286. * This function is called every time an UBI volume is updated. This function
  287. * does nothing if volume @vol is dynamic, and changes MTD device size if the
  288. * volume is static. This is needed because static volumes cannot be read past
  289. * data they contain.
  290. */
  291. void ubi_gluebi_updated(struct ubi_volume *vol)
  292. {
  293. struct mtd_info *mtd = &vol->gluebi_mtd;
  294. if (vol->vol_type == UBI_STATIC_VOLUME)
  295. mtd->size = vol->used_bytes;
  296. }