upd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 and atomic LEB change
  25. * functionality.
  26. *
  27. * The update operation is based on the per-volume update marker which is
  28. * stored in the volume table. The update marker is set before the update
  29. * starts, and removed after the update has been finished. So if the update was
  30. * interrupted by an unclean re-boot or due to some other reasons, the update
  31. * marker stays on the flash media and UBI finds it when it attaches the MTD
  32. * device next time. If the update marker is set for a volume, the volume is
  33. * treated as damaged and most I/O operations are prohibited. Only a new update
  34. * operation is allowed.
  35. *
  36. * Note, in general it is possible to implement the update operation as a
  37. * transaction with a roll-back capability.
  38. */
  39. #ifdef UBI_LINUX
  40. #include <linux/err.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/div64.h>
  43. #endif
  44. #include <ubi_uboot.h>
  45. #include "ubi.h"
  46. /**
  47. * set_update_marker - set update marker.
  48. * @ubi: UBI device description object
  49. * @vol: volume description object
  50. *
  51. * This function sets the update marker flag for volume @vol. Returns zero
  52. * in case of success and a negative error code in case of failure.
  53. */
  54. static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol)
  55. {
  56. int err;
  57. struct ubi_vtbl_record vtbl_rec;
  58. dbg_msg("set update marker for volume %d", vol->vol_id);
  59. if (vol->upd_marker) {
  60. ubi_assert(ubi->vtbl[vol->vol_id].upd_marker);
  61. dbg_msg("already set");
  62. return 0;
  63. }
  64. memcpy(&vtbl_rec, &ubi->vtbl[vol->vol_id],
  65. sizeof(struct ubi_vtbl_record));
  66. vtbl_rec.upd_marker = 1;
  67. mutex_lock(&ubi->volumes_mutex);
  68. err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
  69. mutex_unlock(&ubi->volumes_mutex);
  70. vol->upd_marker = 1;
  71. return err;
  72. }
  73. /**
  74. * clear_update_marker - clear update marker.
  75. * @ubi: UBI device description object
  76. * @vol: volume description object
  77. * @bytes: new data size in bytes
  78. *
  79. * This function clears the update marker for volume @vol, sets new volume
  80. * data size and clears the "corrupted" flag (static volumes only). Returns
  81. * zero in case of success and a negative error code in case of failure.
  82. */
  83. static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol,
  84. long long bytes)
  85. {
  86. int err;
  87. uint64_t tmp;
  88. struct ubi_vtbl_record vtbl_rec;
  89. dbg_msg("clear update marker for volume %d", vol->vol_id);
  90. memcpy(&vtbl_rec, &ubi->vtbl[vol->vol_id],
  91. sizeof(struct ubi_vtbl_record));
  92. ubi_assert(vol->upd_marker && vtbl_rec.upd_marker);
  93. vtbl_rec.upd_marker = 0;
  94. if (vol->vol_type == UBI_STATIC_VOLUME) {
  95. vol->corrupted = 0;
  96. vol->used_bytes = tmp = bytes;
  97. vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size);
  98. vol->used_ebs = tmp;
  99. if (vol->last_eb_bytes)
  100. vol->used_ebs += 1;
  101. else
  102. vol->last_eb_bytes = vol->usable_leb_size;
  103. }
  104. mutex_lock(&ubi->volumes_mutex);
  105. err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
  106. mutex_unlock(&ubi->volumes_mutex);
  107. vol->upd_marker = 0;
  108. return err;
  109. }
  110. /**
  111. * ubi_start_update - start volume update.
  112. * @ubi: UBI device description object
  113. * @vol: volume description object
  114. * @bytes: update bytes
  115. *
  116. * This function starts volume update operation. If @bytes is zero, the volume
  117. * is just wiped out. Returns zero in case of success and a negative error code
  118. * in case of failure.
  119. */
  120. int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
  121. long long bytes)
  122. {
  123. int i, err;
  124. uint64_t tmp;
  125. dbg_msg("start update of volume %d, %llu bytes", vol->vol_id, bytes);
  126. ubi_assert(!vol->updating && !vol->changing_leb);
  127. vol->updating = 1;
  128. err = set_update_marker(ubi, vol);
  129. if (err)
  130. return err;
  131. /* Before updating - wipe out the volume */
  132. for (i = 0; i < vol->reserved_pebs; i++) {
  133. err = ubi_eba_unmap_leb(ubi, vol, i);
  134. if (err)
  135. return err;
  136. }
  137. if (bytes == 0) {
  138. err = clear_update_marker(ubi, vol, 0);
  139. if (err)
  140. return err;
  141. err = ubi_wl_flush(ubi);
  142. if (!err)
  143. vol->updating = 0;
  144. }
  145. vol->upd_buf = vmalloc(ubi->leb_size);
  146. if (!vol->upd_buf)
  147. return -ENOMEM;
  148. tmp = bytes;
  149. vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size);
  150. vol->upd_ebs += tmp;
  151. vol->upd_bytes = bytes;
  152. vol->upd_received = 0;
  153. return 0;
  154. }
  155. /**
  156. * ubi_start_leb_change - start atomic LEB change.
  157. * @ubi: UBI device description object
  158. * @vol: volume description object
  159. * @req: operation request
  160. *
  161. * This function starts atomic LEB change operation. Returns zero in case of
  162. * success and a negative error code in case of failure.
  163. */
  164. int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  165. const struct ubi_leb_change_req *req)
  166. {
  167. ubi_assert(!vol->updating && !vol->changing_leb);
  168. dbg_msg("start changing LEB %d:%d, %u bytes",
  169. vol->vol_id, req->lnum, req->bytes);
  170. if (req->bytes == 0)
  171. return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0,
  172. req->dtype);
  173. vol->upd_bytes = req->bytes;
  174. vol->upd_received = 0;
  175. vol->changing_leb = 1;
  176. vol->ch_lnum = req->lnum;
  177. vol->ch_dtype = req->dtype;
  178. vol->upd_buf = vmalloc(req->bytes);
  179. if (!vol->upd_buf)
  180. return -ENOMEM;
  181. return 0;
  182. }
  183. /**
  184. * write_leb - write update data.
  185. * @ubi: UBI device description object
  186. * @vol: volume description object
  187. * @lnum: logical eraseblock number
  188. * @buf: data to write
  189. * @len: data size
  190. * @used_ebs: how many logical eraseblocks will this volume contain (static
  191. * volumes only)
  192. *
  193. * This function writes update data to corresponding logical eraseblock. In
  194. * case of dynamic volume, this function checks if the data contains 0xFF bytes
  195. * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole
  196. * buffer contains only 0xFF bytes, the LEB is left unmapped.
  197. *
  198. * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
  199. * that we want to make sure that more data may be appended to the logical
  200. * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and
  201. * this PEB won't be writable anymore. So if one writes the file-system image
  202. * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
  203. * space is writable after the update.
  204. *
  205. * We do not do this for static volumes because they are read-only. But this
  206. * also cannot be done because we have to store per-LEB CRC and the correct
  207. * data length.
  208. *
  209. * This function returns zero in case of success and a negative error code in
  210. * case of failure.
  211. */
  212. static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  213. void *buf, int len, int used_ebs)
  214. {
  215. int err;
  216. if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
  217. int l = ALIGN(len, ubi->min_io_size);
  218. memset(buf + len, 0xFF, l - len);
  219. len = ubi_calc_data_len(ubi, buf, l);
  220. if (len == 0) {
  221. dbg_msg("all %d bytes contain 0xFF - skip", len);
  222. return 0;
  223. }
  224. err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len, UBI_UNKNOWN);
  225. } else {
  226. /*
  227. * When writing static volume, and this is the last logical
  228. * eraseblock, the length (@len) does not have to be aligned to
  229. * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()'
  230. * function accepts exact (unaligned) length and stores it in
  231. * the VID header. And it takes care of proper alignment by
  232. * padding the buffer. Here we just make sure the padding will
  233. * contain zeros, not random trash.
  234. */
  235. memset(buf + len, 0, vol->usable_leb_size - len);
  236. err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len,
  237. UBI_UNKNOWN, used_ebs);
  238. }
  239. return err;
  240. }
  241. /**
  242. * ubi_more_update_data - write more update data.
  243. * @vol: volume description object
  244. * @buf: write data (user-space memory buffer)
  245. * @count: how much bytes to write
  246. *
  247. * This function writes more data to the volume which is being updated. It may
  248. * be called arbitrary number of times until all the update data arriveis. This
  249. * function returns %0 in case of success, number of bytes written during the
  250. * last call if the whole volume update has been successfully finished, and a
  251. * negative error code in case of failure.
  252. */
  253. int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
  254. const void __user *buf, int count)
  255. {
  256. uint64_t tmp;
  257. int lnum, offs, err = 0, len, to_write = count;
  258. dbg_msg("write %d of %lld bytes, %lld already passed",
  259. count, vol->upd_bytes, vol->upd_received);
  260. if (ubi->ro_mode)
  261. return -EROFS;
  262. tmp = vol->upd_received;
  263. offs = do_div(tmp, vol->usable_leb_size);
  264. lnum = tmp;
  265. if (vol->upd_received + count > vol->upd_bytes)
  266. to_write = count = vol->upd_bytes - vol->upd_received;
  267. /*
  268. * When updating volumes, we accumulate whole logical eraseblock of
  269. * data and write it at once.
  270. */
  271. if (offs != 0) {
  272. /*
  273. * This is a write to the middle of the logical eraseblock. We
  274. * copy the data to our update buffer and wait for more data or
  275. * flush it if the whole eraseblock is written or the update
  276. * is finished.
  277. */
  278. len = vol->usable_leb_size - offs;
  279. if (len > count)
  280. len = count;
  281. err = copy_from_user(vol->upd_buf + offs, buf, len);
  282. if (err)
  283. return -EFAULT;
  284. if (offs + len == vol->usable_leb_size ||
  285. vol->upd_received + len == vol->upd_bytes) {
  286. int flush_len = offs + len;
  287. /*
  288. * OK, we gathered either the whole eraseblock or this
  289. * is the last chunk, it's time to flush the buffer.
  290. */
  291. ubi_assert(flush_len <= vol->usable_leb_size);
  292. err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len,
  293. vol->upd_ebs);
  294. if (err)
  295. return err;
  296. }
  297. vol->upd_received += len;
  298. count -= len;
  299. buf += len;
  300. lnum += 1;
  301. }
  302. /*
  303. * If we've got more to write, let's continue. At this point we know we
  304. * are starting from the beginning of an eraseblock.
  305. */
  306. while (count) {
  307. if (count > vol->usable_leb_size)
  308. len = vol->usable_leb_size;
  309. else
  310. len = count;
  311. err = copy_from_user(vol->upd_buf, buf, len);
  312. if (err)
  313. return -EFAULT;
  314. if (len == vol->usable_leb_size ||
  315. vol->upd_received + len == vol->upd_bytes) {
  316. err = write_leb(ubi, vol, lnum, vol->upd_buf,
  317. len, vol->upd_ebs);
  318. if (err)
  319. break;
  320. }
  321. vol->upd_received += len;
  322. count -= len;
  323. lnum += 1;
  324. buf += len;
  325. }
  326. ubi_assert(vol->upd_received <= vol->upd_bytes);
  327. if (vol->upd_received == vol->upd_bytes) {
  328. /* The update is finished, clear the update marker */
  329. err = clear_update_marker(ubi, vol, vol->upd_bytes);
  330. if (err)
  331. return err;
  332. err = ubi_wl_flush(ubi);
  333. if (err == 0) {
  334. vol->updating = 0;
  335. err = to_write;
  336. vfree(vol->upd_buf);
  337. }
  338. }
  339. return err;
  340. }
  341. /**
  342. * ubi_more_leb_change_data - accept more data for atomic LEB change.
  343. * @vol: volume description object
  344. * @buf: write data (user-space memory buffer)
  345. * @count: how much bytes to write
  346. *
  347. * This function accepts more data to the volume which is being under the
  348. * "atomic LEB change" operation. It may be called arbitrary number of times
  349. * until all data arrives. This function returns %0 in case of success, number
  350. * of bytes written during the last call if the whole "atomic LEB change"
  351. * operation has been successfully finished, and a negative error code in case
  352. * of failure.
  353. */
  354. int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
  355. const void __user *buf, int count)
  356. {
  357. int err;
  358. dbg_msg("write %d of %lld bytes, %lld already passed",
  359. count, vol->upd_bytes, vol->upd_received);
  360. if (ubi->ro_mode)
  361. return -EROFS;
  362. if (vol->upd_received + count > vol->upd_bytes)
  363. count = vol->upd_bytes - vol->upd_received;
  364. err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count);
  365. if (err)
  366. return -EFAULT;
  367. vol->upd_received += count;
  368. if (vol->upd_received == vol->upd_bytes) {
  369. int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size);
  370. memset(vol->upd_buf + vol->upd_bytes, 0xFF, len - vol->upd_bytes);
  371. len = ubi_calc_data_len(ubi, vol->upd_buf, len);
  372. err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
  373. vol->upd_buf, len, UBI_UNKNOWN);
  374. if (err)
  375. return err;
  376. }
  377. ubi_assert(vol->upd_received <= vol->upd_bytes);
  378. if (vol->upd_received == vol->upd_bytes) {
  379. vol->changing_leb = 0;
  380. err = count;
  381. vfree(vol->upd_buf);
  382. }
  383. return err;
  384. }