gluebi.c 9.5 KB

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