upd.c 12 KB

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