upd.c 12 KB

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