upd.c 12 KB

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