upd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём)
  20. *
  21. * Jan 2007: Alexander Schmidt, hacked per-volume update.
  22. */
  23. /*
  24. * This file contains implementation of the volume update functionality.
  25. *
  26. * The update operation is based on the per-volume update marker which is
  27. * stored in the volume table. The update marker is set before the update
  28. * starts, and removed after the update has been finished. So if the update was
  29. * interrupted by an unclean re-boot or due to some other reasons, the update
  30. * marker stays on the flash media and UBI finds it when it attaches the MTD
  31. * device next time. If the update marker is set for a volume, the volume is
  32. * treated as damaged and most I/O operations are prohibited. Only a new update
  33. * operation is allowed.
  34. *
  35. * Note, in general it is possible to implement the update operation as a
  36. * transaction with a roll-back capability.
  37. */
  38. #include <linux/err.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/div64.h>
  41. #include "ubi.h"
  42. /**
  43. * set_update_marker - set update marker.
  44. * @ubi: UBI device description object
  45. * @vol_id: volume ID
  46. *
  47. * This function sets the update marker flag for volume @vol_id. Returns zero
  48. * in case of success and a negative error code in case of failure.
  49. */
  50. static int set_update_marker(struct ubi_device *ubi, int vol_id)
  51. {
  52. int err;
  53. struct ubi_vtbl_record vtbl_rec;
  54. struct ubi_volume *vol = ubi->volumes[vol_id];
  55. dbg_msg("set update marker for volume %d", vol_id);
  56. if (vol->upd_marker) {
  57. ubi_assert(ubi->vtbl[vol_id].upd_marker);
  58. dbg_msg("already set");
  59. return 0;
  60. }
  61. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  62. vtbl_rec.upd_marker = 1;
  63. mutex_lock(&ubi->volumes_mutex);
  64. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  65. mutex_unlock(&ubi->volumes_mutex);
  66. vol->upd_marker = 1;
  67. return err;
  68. }
  69. /**
  70. * clear_update_marker - clear update marker.
  71. * @ubi: UBI device description object
  72. * @vol_id: volume ID
  73. * @bytes: new data size in bytes
  74. *
  75. * This function clears the update marker for volume @vol_id, sets new volume
  76. * data size and clears the "corrupted" flag (static volumes only). Returns
  77. * zero in case of success and a negative error code in case of failure.
  78. */
  79. static int clear_update_marker(struct ubi_device *ubi, int vol_id, long long bytes)
  80. {
  81. int err;
  82. uint64_t tmp;
  83. struct ubi_vtbl_record vtbl_rec;
  84. struct ubi_volume *vol = ubi->volumes[vol_id];
  85. dbg_msg("clear update marker for volume %d", vol_id);
  86. memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
  87. ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
  88. vtbl_rec.upd_marker = 0;
  89. if (vol->vol_type == UBI_STATIC_VOLUME) {
  90. vol->corrupted = 0;
  91. vol->used_bytes = tmp = bytes;
  92. vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size);
  93. vol->used_ebs = tmp;
  94. if (vol->last_eb_bytes)
  95. vol->used_ebs += 1;
  96. else
  97. vol->last_eb_bytes = vol->usable_leb_size;
  98. }
  99. mutex_lock(&ubi->volumes_mutex);
  100. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  101. mutex_unlock(&ubi->volumes_mutex);
  102. vol->upd_marker = 0;
  103. return err;
  104. }
  105. /**
  106. * ubi_start_update - start volume update.
  107. * @ubi: UBI device description object
  108. * @vol_id: volume ID
  109. * @bytes: update bytes
  110. *
  111. * This function starts volume update operation. If @bytes is zero, the volume
  112. * is just wiped out. Returns zero in case of success and a negative error code
  113. * in case of failure.
  114. */
  115. int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes)
  116. {
  117. int i, err;
  118. uint64_t tmp;
  119. struct ubi_volume *vol = ubi->volumes[vol_id];
  120. dbg_msg("start update of volume %d, %llu bytes", vol_id, bytes);
  121. vol->updating = 1;
  122. err = set_update_marker(ubi, vol_id);
  123. if (err)
  124. return err;
  125. /* Before updating - wipe out the volume */
  126. for (i = 0; i < vol->reserved_pebs; i++) {
  127. err = ubi_eba_unmap_leb(ubi, vol, i);
  128. if (err)
  129. return err;
  130. }
  131. if (bytes == 0) {
  132. err = clear_update_marker(ubi, vol_id, 0);
  133. if (err)
  134. return err;
  135. err = ubi_wl_flush(ubi);
  136. if (!err)
  137. vol->updating = 0;
  138. }
  139. vol->upd_buf = vmalloc(ubi->leb_size);
  140. if (!vol->upd_buf)
  141. return -ENOMEM;
  142. tmp = bytes;
  143. vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size);
  144. vol->upd_ebs += tmp;
  145. vol->upd_bytes = bytes;
  146. vol->upd_received = 0;
  147. return 0;
  148. }
  149. /**
  150. * write_leb - write update data.
  151. * @ubi: UBI device description object
  152. * @vol_id: volume ID
  153. * @lnum: logical eraseblock number
  154. * @buf: data to write
  155. * @len: data size
  156. * @used_ebs: how many logical eraseblocks will this volume contain (static
  157. * volumes only)
  158. *
  159. * This function writes update data to corresponding logical eraseblock. In
  160. * case of dynamic volume, this function checks if the data contains 0xFF bytes
  161. * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
  162. * buffer contains only 0xFF bytes, the LEB is left unmapped.
  163. *
  164. * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
  165. * that we want to make sure that more data may be appended to the logical
  166. * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
  167. * this PEB won't be writable anymore. So if one writes the file-system image
  168. * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
  169. * space is writable after the update.
  170. *
  171. * We do not do this for static volumes because they are read-only. But this
  172. * also cannot be done because we have to store per-LEB CRC and the correct
  173. * data length.
  174. *
  175. * This function returns zero in case of success and a negative error code in
  176. * case of failure.
  177. */
  178. static int write_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,
  179. int len, int used_ebs)
  180. {
  181. int err, l;
  182. struct ubi_volume *vol = ubi->volumes[vol_id];
  183. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  184. l = ALIGN(len, ubi->min_io_size);
  185. memset(buf + len, 0xFF, l - len);
  186. l = ubi_calc_data_len(ubi, buf, l);
  187. if (l == 0) {
  188. dbg_msg("all %d bytes contain 0xFF - skip", len);
  189. return 0;
  190. }
  191. if (len != l)
  192. dbg_msg("skip last %d bytes (0xFF)", len - l);
  193. err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, l, UBI_UNKNOWN);
  194. } else {
  195. /*
  196. * When writing static volume, and this is the last logical
  197. * eraseblock, the length (@len) does not have to be aligned to
  198. * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
  199. * function accepts exact (unaligned) length and stores it in
  200. * the VID header. And it takes care of proper alignment by
  201. * padding the buffer. Here we just make sure the padding will
  202. * contain zeros, not random trash.
  203. */
  204. memset(buf + len, 0, vol->usable_leb_size - len);
  205. err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len,
  206. UBI_UNKNOWN, used_ebs);
  207. }
  208. return err;
  209. }
  210. /**
  211. * ubi_more_update_data - write more update data.
  212. * @vol: volume description object
  213. * @buf: write data (user-space memory buffer)
  214. * @count: how much bytes to write
  215. *
  216. * This function writes more data to the volume which is being updated. It may
  217. * be called arbitrary number of times until all of the update data arrive.
  218. * This function returns %0 in case of success, number of bytes written during
  219. * the last call if the whole volume update was successfully finished, and a
  220. * negative error code in case of failure.
  221. */
  222. int ubi_more_update_data(struct ubi_device *ubi, int vol_id,
  223. const void __user *buf, int count)
  224. {
  225. uint64_t tmp;
  226. struct ubi_volume *vol = ubi->volumes[vol_id];
  227. int lnum, offs, err = 0, len, to_write = count;
  228. dbg_msg("write %d of %lld bytes, %lld already passed",
  229. count, vol->upd_bytes, vol->upd_received);
  230. if (ubi->ro_mode)
  231. return -EROFS;
  232. tmp = vol->upd_received;
  233. offs = do_div(tmp, vol->usable_leb_size);
  234. lnum = tmp;
  235. if (vol->upd_received + count > vol->upd_bytes)
  236. to_write = count = vol->upd_bytes - vol->upd_received;
  237. /*
  238. * When updating volumes, we accumulate whole logical eraseblock of
  239. * data and write it at once.
  240. */
  241. if (offs != 0) {
  242. /*
  243. * This is a write to the middle of the logical eraseblock. We
  244. * copy the data to our update buffer and wait for more data or
  245. * flush it if the whole eraseblock is written or the update
  246. * is finished.
  247. */
  248. len = vol->usable_leb_size - offs;
  249. if (len > count)
  250. len = count;
  251. err = copy_from_user(vol->upd_buf + offs, buf, len);
  252. if (err)
  253. return -EFAULT;
  254. if (offs + len == vol->usable_leb_size ||
  255. vol->upd_received + len == vol->upd_bytes) {
  256. int flush_len = offs + len;
  257. /*
  258. * OK, we gathered either the whole eraseblock or this
  259. * is the last chunk, it's time to flush the buffer.
  260. */
  261. ubi_assert(flush_len <= vol->usable_leb_size);
  262. err = write_leb(ubi, vol_id, lnum, vol->upd_buf,
  263. flush_len, vol->upd_ebs);
  264. if (err)
  265. return err;
  266. }
  267. vol->upd_received += len;
  268. count -= len;
  269. buf += len;
  270. lnum += 1;
  271. }
  272. /*
  273. * If we've got more to write, let's continue. At this point we know we
  274. * are starting from the beginning of an eraseblock.
  275. */
  276. while (count) {
  277. if (count > vol->usable_leb_size)
  278. len = vol->usable_leb_size;
  279. else
  280. len = count;
  281. err = copy_from_user(vol->upd_buf, buf, len);
  282. if (err)
  283. return -EFAULT;
  284. if (len == vol->usable_leb_size ||
  285. vol->upd_received + len == vol->upd_bytes) {
  286. err = write_leb(ubi, vol_id, lnum, vol->upd_buf, len,
  287. vol->upd_ebs);
  288. if (err)
  289. break;
  290. }
  291. vol->upd_received += len;
  292. count -= len;
  293. lnum += 1;
  294. buf += len;
  295. }
  296. ubi_assert(vol->upd_received <= vol->upd_bytes);
  297. if (vol->upd_received == vol->upd_bytes) {
  298. /* The update is finished, clear the update marker */
  299. err = clear_update_marker(ubi, vol_id, vol->upd_bytes);
  300. if (err)
  301. return err;
  302. err = ubi_wl_flush(ubi);
  303. if (err == 0) {
  304. err = to_write;
  305. vfree(vol->upd_buf);
  306. }
  307. }
  308. return err;
  309. }