Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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