Lines Matching +full:ubi +full:- +full:volume +full:-

1 // SPDX-License-Identifier: GPL-2.0-or-later
8 * Jan 2007: Alexander Schmidt, hacked per-volume update.
12 * This file contains implementation of the volume update and atomic LEB change
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
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
25 * transaction with a roll-back capability.
31 #include "ubi.h"
34 * set_update_marker - set update marker.
35 * @ubi: UBI device description object
36 * @vol: volume description object
38 * This function sets the update marker flag for volume @vol. Returns zero
41 static int set_update_marker(struct ubi_device *ubi, struct ubi_volume *vol) in set_update_marker() argument
46 dbg_gen("set update marker for volume %d", vol->vol_id); in set_update_marker()
48 if (vol->upd_marker) { in set_update_marker()
49 ubi_assert(ubi->vtbl[vol->vol_id].upd_marker); in set_update_marker()
54 vtbl_rec = ubi->vtbl[vol->vol_id]; in set_update_marker()
57 mutex_lock(&ubi->device_mutex); in set_update_marker()
58 err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec); in set_update_marker()
59 vol->upd_marker = 1; in set_update_marker()
60 mutex_unlock(&ubi->device_mutex); in set_update_marker()
65 * clear_update_marker - clear update marker.
66 * @ubi: UBI device description object
67 * @vol: volume description object
70 * This function clears the update marker for volume @vol, sets new volume
74 static int clear_update_marker(struct ubi_device *ubi, struct ubi_volume *vol, in clear_update_marker() argument
80 dbg_gen("clear update marker for volume %d", vol->vol_id); in clear_update_marker()
82 vtbl_rec = ubi->vtbl[vol->vol_id]; in clear_update_marker()
83 ubi_assert(vol->upd_marker && vtbl_rec.upd_marker); in clear_update_marker()
86 if (vol->vol_type == UBI_STATIC_VOLUME) { in clear_update_marker()
87 vol->corrupted = 0; in clear_update_marker()
88 vol->used_bytes = bytes; in clear_update_marker()
89 vol->used_ebs = div_u64_rem(bytes, vol->usable_leb_size, in clear_update_marker()
90 &vol->last_eb_bytes); in clear_update_marker()
91 if (vol->last_eb_bytes) in clear_update_marker()
92 vol->used_ebs += 1; in clear_update_marker()
94 vol->last_eb_bytes = vol->usable_leb_size; in clear_update_marker()
97 mutex_lock(&ubi->device_mutex); in clear_update_marker()
98 err = ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec); in clear_update_marker()
99 vol->upd_marker = 0; in clear_update_marker()
100 mutex_unlock(&ubi->device_mutex); in clear_update_marker()
105 * ubi_start_update - start volume update.
106 * @ubi: UBI device description object
107 * @vol: volume description object
110 * This function starts volume update operation. If @bytes is zero, the volume
114 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_start_update() argument
119 dbg_gen("start update of volume %d, %llu bytes", vol->vol_id, bytes); in ubi_start_update()
120 ubi_assert(!vol->updating && !vol->changing_leb); in ubi_start_update()
121 vol->updating = 1; in ubi_start_update()
123 vol->upd_buf = vmalloc(ubi->leb_size); in ubi_start_update()
124 if (!vol->upd_buf) in ubi_start_update()
125 return -ENOMEM; in ubi_start_update()
127 err = set_update_marker(ubi, vol); in ubi_start_update()
131 /* Before updating - wipe out the volume */ in ubi_start_update()
132 for (i = 0; i < vol->reserved_pebs; i++) { in ubi_start_update()
133 err = ubi_eba_unmap_leb(ubi, vol, i); in ubi_start_update()
138 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL); in ubi_start_update()
143 err = clear_update_marker(ubi, vol, 0); in ubi_start_update()
147 vfree(vol->upd_buf); in ubi_start_update()
148 vol->updating = 0; in ubi_start_update()
152 vol->upd_ebs = div_u64(bytes + vol->usable_leb_size - 1, in ubi_start_update()
153 vol->usable_leb_size); in ubi_start_update()
154 vol->upd_bytes = bytes; in ubi_start_update()
155 vol->upd_received = 0; in ubi_start_update()
160 * ubi_start_leb_change - start atomic LEB change.
161 * @ubi: UBI device description object
162 * @vol: volume description object
168 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_start_leb_change() argument
171 ubi_assert(!vol->updating && !vol->changing_leb); in ubi_start_leb_change()
174 vol->vol_id, req->lnum, req->bytes); in ubi_start_leb_change()
175 if (req->bytes == 0) in ubi_start_leb_change()
176 return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0); in ubi_start_leb_change()
178 vol->upd_bytes = req->bytes; in ubi_start_leb_change()
179 vol->upd_received = 0; in ubi_start_leb_change()
180 vol->changing_leb = 1; in ubi_start_leb_change()
181 vol->ch_lnum = req->lnum; in ubi_start_leb_change()
183 vol->upd_buf = vmalloc(ALIGN((int)req->bytes, ubi->min_io_size)); in ubi_start_leb_change()
184 if (!vol->upd_buf) in ubi_start_leb_change()
185 return -ENOMEM; in ubi_start_leb_change()
191 * write_leb - write update data.
192 * @ubi: UBI device description object
193 * @vol: volume description object
197 * @used_ebs: how many logical eraseblocks will this volume contain (static
201 * case of dynamic volume, this function checks if the data contains 0xFF bytes
205 * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is
208 * this PEB won't be writable anymore. So if one writes the file-system image
209 * to the UBI volume where 0xFFs mean free space - UBI makes sure this free
212 * We do not do this for static volumes because they are read-only. But this
213 * also cannot be done because we have to store per-LEB CRC and the correct
219 static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, in write_leb() argument
224 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { in write_leb()
225 int l = ALIGN(len, ubi->min_io_size); in write_leb()
227 memset(buf + len, 0xFF, l - len); in write_leb()
228 len = ubi_calc_data_len(ubi, buf, l); in write_leb()
230 dbg_gen("all %d bytes contain 0xFF - skip", len); in write_leb()
234 err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len); in write_leb()
237 * When writing static volume, and this is the last logical in write_leb()
245 memset(buf + len, 0, vol->usable_leb_size - len); in write_leb()
246 err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs); in write_leb()
253 * ubi_more_update_data - write more update data.
254 * @ubi: UBI device description object
255 * @vol: volume description object
256 * @buf: write data (user-space memory buffer)
259 * This function writes more data to the volume which is being updated. It may
262 * last call if the whole volume update has been successfully finished, and a
265 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_more_update_data() argument
271 count, vol->upd_bytes, vol->upd_received); in ubi_more_update_data()
273 if (ubi->ro_mode) in ubi_more_update_data()
274 return -EROFS; in ubi_more_update_data()
276 lnum = div_u64_rem(vol->upd_received, vol->usable_leb_size, &offs); in ubi_more_update_data()
277 if (vol->upd_received + count > vol->upd_bytes) in ubi_more_update_data()
278 to_write = count = vol->upd_bytes - vol->upd_received; in ubi_more_update_data()
292 len = vol->usable_leb_size - offs; in ubi_more_update_data()
296 err = copy_from_user(vol->upd_buf + offs, buf, len); in ubi_more_update_data()
298 return -EFAULT; in ubi_more_update_data()
300 if (offs + len == vol->usable_leb_size || in ubi_more_update_data()
301 vol->upd_received + len == vol->upd_bytes) { in ubi_more_update_data()
308 ubi_assert(flush_len <= vol->usable_leb_size); in ubi_more_update_data()
309 err = write_leb(ubi, vol, lnum, vol->upd_buf, flush_len, in ubi_more_update_data()
310 vol->upd_ebs); in ubi_more_update_data()
315 vol->upd_received += len; in ubi_more_update_data()
316 count -= len; in ubi_more_update_data()
326 if (count > vol->usable_leb_size) in ubi_more_update_data()
327 len = vol->usable_leb_size; in ubi_more_update_data()
331 err = copy_from_user(vol->upd_buf, buf, len); in ubi_more_update_data()
333 return -EFAULT; in ubi_more_update_data()
335 if (len == vol->usable_leb_size || in ubi_more_update_data()
336 vol->upd_received + len == vol->upd_bytes) { in ubi_more_update_data()
337 err = write_leb(ubi, vol, lnum, vol->upd_buf, in ubi_more_update_data()
338 len, vol->upd_ebs); in ubi_more_update_data()
343 vol->upd_received += len; in ubi_more_update_data()
344 count -= len; in ubi_more_update_data()
349 ubi_assert(vol->upd_received <= vol->upd_bytes); in ubi_more_update_data()
350 if (vol->upd_received == vol->upd_bytes) { in ubi_more_update_data()
351 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL); in ubi_more_update_data()
355 err = clear_update_marker(ubi, vol, vol->upd_bytes); in ubi_more_update_data()
358 vol->updating = 0; in ubi_more_update_data()
360 vfree(vol->upd_buf); in ubi_more_update_data()
367 * ubi_more_leb_change_data - accept more data for atomic LEB change.
368 * @ubi: UBI device description object
369 * @vol: volume description object
370 * @buf: write data (user-space memory buffer)
373 * This function accepts more data to the volume which is being under the
380 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol, in ubi_more_leb_change_data() argument
386 count, vol->upd_bytes, vol->upd_received); in ubi_more_leb_change_data()
388 if (ubi->ro_mode) in ubi_more_leb_change_data()
389 return -EROFS; in ubi_more_leb_change_data()
391 if (vol->upd_received + count > vol->upd_bytes) in ubi_more_leb_change_data()
392 count = vol->upd_bytes - vol->upd_received; in ubi_more_leb_change_data()
394 err = copy_from_user(vol->upd_buf + vol->upd_received, buf, count); in ubi_more_leb_change_data()
396 return -EFAULT; in ubi_more_leb_change_data()
398 vol->upd_received += count; in ubi_more_leb_change_data()
400 if (vol->upd_received == vol->upd_bytes) { in ubi_more_leb_change_data()
401 int len = ALIGN((int)vol->upd_bytes, ubi->min_io_size); in ubi_more_leb_change_data()
403 memset(vol->upd_buf + vol->upd_bytes, 0xFF, in ubi_more_leb_change_data()
404 len - vol->upd_bytes); in ubi_more_leb_change_data()
405 len = ubi_calc_data_len(ubi, vol->upd_buf, len); in ubi_more_leb_change_data()
406 err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum, in ubi_more_leb_change_data()
407 vol->upd_buf, len); in ubi_more_leb_change_data()
412 ubi_assert(vol->upd_received <= vol->upd_bytes); in ubi_more_leb_change_data()
413 if (vol->upd_received == vol->upd_bytes) { in ubi_more_leb_change_data()
414 vol->changing_leb = 0; in ubi_more_leb_change_data()
416 vfree(vol->upd_buf); in ubi_more_leb_change_data()