1801c135cSArtem B. Bityutskiy /* 2801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006 3801c135cSArtem B. Bityutskiy * 4801c135cSArtem B. Bityutskiy * This program is free software; you can redistribute it and/or modify 5801c135cSArtem B. Bityutskiy * it under the terms of the GNU General Public License as published by 6801c135cSArtem B. Bityutskiy * the Free Software Foundation; either version 2 of the License, or 7801c135cSArtem B. Bityutskiy * (at your option) any later version. 8801c135cSArtem B. Bityutskiy * 9801c135cSArtem B. Bityutskiy * This program is distributed in the hope that it will be useful, 10801c135cSArtem B. Bityutskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 11801c135cSArtem B. Bityutskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12801c135cSArtem B. Bityutskiy * the GNU General Public License for more details. 13801c135cSArtem B. Bityutskiy * 14801c135cSArtem B. Bityutskiy * You should have received a copy of the GNU General Public License 15801c135cSArtem B. Bityutskiy * along with this program; if not, write to the Free Software 16801c135cSArtem B. Bityutskiy * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17801c135cSArtem B. Bityutskiy * 18801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём) 19801c135cSArtem B. Bityutskiy */ 20801c135cSArtem B. Bityutskiy 21801c135cSArtem B. Bityutskiy /* This file mostly implements UBI kernel API functions */ 22801c135cSArtem B. Bityutskiy 23801c135cSArtem B. Bityutskiy #include <linux/module.h> 24801c135cSArtem B. Bityutskiy #include <linux/err.h> 25801c135cSArtem B. Bityutskiy #include <asm/div64.h> 26801c135cSArtem B. Bityutskiy #include "ubi.h" 27801c135cSArtem B. Bityutskiy 28801c135cSArtem B. Bityutskiy /** 29801c135cSArtem B. Bityutskiy * ubi_get_device_info - get information about UBI device. 30801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 31801c135cSArtem B. Bityutskiy * @di: the information is stored here 32801c135cSArtem B. Bityutskiy * 33801c135cSArtem B. Bityutskiy * This function returns %0 in case of success and a %-ENODEV if there is no 34801c135cSArtem B. Bityutskiy * such UBI device. 35801c135cSArtem B. Bityutskiy */ 36801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) 37801c135cSArtem B. Bityutskiy { 38801c135cSArtem B. Bityutskiy const struct ubi_device *ubi; 39801c135cSArtem B. Bityutskiy 40801c135cSArtem B. Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || 41503990ebSArtem Bityutskiy !ubi_devices[ubi_num]) 42801c135cSArtem B. Bityutskiy return -ENODEV; 43801c135cSArtem B. Bityutskiy 44801c135cSArtem B. Bityutskiy ubi = ubi_devices[ubi_num]; 45801c135cSArtem B. Bityutskiy di->ubi_num = ubi->ubi_num; 46801c135cSArtem B. Bityutskiy di->leb_size = ubi->leb_size; 47801c135cSArtem B. Bityutskiy di->min_io_size = ubi->min_io_size; 48801c135cSArtem B. Bityutskiy di->ro_mode = ubi->ro_mode; 4949dfc299SArtem Bityutskiy di->cdev = ubi->cdev.dev; 50801c135cSArtem B. Bityutskiy return 0; 51801c135cSArtem B. Bityutskiy } 52801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info); 53801c135cSArtem B. Bityutskiy 54801c135cSArtem B. Bityutskiy /** 55801c135cSArtem B. Bityutskiy * ubi_get_volume_info - get information about UBI volume. 56801c135cSArtem B. Bityutskiy * @desc: volume descriptor 57801c135cSArtem B. Bityutskiy * @vi: the information is stored here 58801c135cSArtem B. Bityutskiy */ 59801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc, 60801c135cSArtem B. Bityutskiy struct ubi_volume_info *vi) 61801c135cSArtem B. Bityutskiy { 62801c135cSArtem B. Bityutskiy const struct ubi_volume *vol = desc->vol; 63801c135cSArtem B. Bityutskiy const struct ubi_device *ubi = vol->ubi; 64801c135cSArtem B. Bityutskiy 65801c135cSArtem B. Bityutskiy vi->vol_id = vol->vol_id; 66801c135cSArtem B. Bityutskiy vi->ubi_num = ubi->ubi_num; 67801c135cSArtem B. Bityutskiy vi->size = vol->reserved_pebs; 68801c135cSArtem B. Bityutskiy vi->used_bytes = vol->used_bytes; 69801c135cSArtem B. Bityutskiy vi->vol_type = vol->vol_type; 70801c135cSArtem B. Bityutskiy vi->corrupted = vol->corrupted; 71801c135cSArtem B. Bityutskiy vi->upd_marker = vol->upd_marker; 72801c135cSArtem B. Bityutskiy vi->alignment = vol->alignment; 73801c135cSArtem B. Bityutskiy vi->usable_leb_size = vol->usable_leb_size; 74801c135cSArtem B. Bityutskiy vi->name_len = vol->name_len; 75801c135cSArtem B. Bityutskiy vi->name = vol->name; 7649dfc299SArtem Bityutskiy vi->cdev = vol->cdev.dev; 77801c135cSArtem B. Bityutskiy } 78801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info); 79801c135cSArtem B. Bityutskiy 80801c135cSArtem B. Bityutskiy /** 81801c135cSArtem B. Bityutskiy * ubi_open_volume - open UBI volume. 82801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 83801c135cSArtem B. Bityutskiy * @vol_id: volume ID 84801c135cSArtem B. Bityutskiy * @mode: open mode 85801c135cSArtem B. Bityutskiy * 86801c135cSArtem B. Bityutskiy * The @mode parameter specifies if the volume should be opened in read-only 87801c135cSArtem B. Bityutskiy * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that 88801c135cSArtem B. Bityutskiy * nobody else will be able to open this volume. UBI allows to have many volume 89801c135cSArtem B. Bityutskiy * readers and one writer at a time. 90801c135cSArtem B. Bityutskiy * 91801c135cSArtem B. Bityutskiy * If a static volume is being opened for the first time since boot, it will be 92801c135cSArtem B. Bityutskiy * checked by this function, which means it will be fully read and the CRC 93801c135cSArtem B. Bityutskiy * checksum of each logical eraseblock will be checked. 94801c135cSArtem B. Bityutskiy * 95801c135cSArtem B. Bityutskiy * This function returns volume descriptor in case of success and a negative 96801c135cSArtem B. Bityutskiy * error code in case of failure. 97801c135cSArtem B. Bityutskiy */ 98801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) 99801c135cSArtem B. Bityutskiy { 100801c135cSArtem B. Bityutskiy int err; 101801c135cSArtem B. Bityutskiy struct ubi_volume_desc *desc; 1020169b49dSJesper Juhl struct ubi_device *ubi; 103801c135cSArtem B. Bityutskiy struct ubi_volume *vol; 104801c135cSArtem B. Bityutskiy 105801c135cSArtem B. Bityutskiy dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode); 106801c135cSArtem B. Bityutskiy 10735ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 10835ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 1090169b49dSJesper Juhl 110801c135cSArtem B. Bityutskiy if (mode != UBI_READONLY && mode != UBI_READWRITE && 111801c135cSArtem B. Bityutskiy mode != UBI_EXCLUSIVE) 11235ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 11335ad5fb7SArtem Bityutskiy 11435ad5fb7SArtem Bityutskiy ubi = ubi_devices[ubi_num]; 11535ad5fb7SArtem Bityutskiy if (!ubi) 11635ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 11735ad5fb7SArtem Bityutskiy 11835ad5fb7SArtem Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 11935ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 120801c135cSArtem B. Bityutskiy 121801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 12235ad5fb7SArtem Bityutskiy if (!desc) 12335ad5fb7SArtem Bityutskiy return ERR_PTR(-ENOMEM); 12435ad5fb7SArtem Bityutskiy 12535ad5fb7SArtem Bityutskiy err = -ENODEV; 12635ad5fb7SArtem Bityutskiy if (!try_module_get(THIS_MODULE)) 12735ad5fb7SArtem Bityutskiy goto out_free; 128801c135cSArtem B. Bityutskiy 129801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 130801c135cSArtem B. Bityutskiy vol = ubi->volumes[vol_id]; 13135ad5fb7SArtem Bityutskiy if (!vol) 132801c135cSArtem B. Bityutskiy goto out_unlock; 133801c135cSArtem B. Bityutskiy 134801c135cSArtem B. Bityutskiy err = -EBUSY; 135801c135cSArtem B. Bityutskiy switch (mode) { 136801c135cSArtem B. Bityutskiy case UBI_READONLY: 137801c135cSArtem B. Bityutskiy if (vol->exclusive) 138801c135cSArtem B. Bityutskiy goto out_unlock; 139801c135cSArtem B. Bityutskiy vol->readers += 1; 140801c135cSArtem B. Bityutskiy break; 141801c135cSArtem B. Bityutskiy 142801c135cSArtem B. Bityutskiy case UBI_READWRITE: 143801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers > 0) 144801c135cSArtem B. Bityutskiy goto out_unlock; 145801c135cSArtem B. Bityutskiy vol->writers += 1; 146801c135cSArtem B. Bityutskiy break; 147801c135cSArtem B. Bityutskiy 148801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 149801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers || vol->readers) 150801c135cSArtem B. Bityutskiy goto out_unlock; 151801c135cSArtem B. Bityutskiy vol->exclusive = 1; 152801c135cSArtem B. Bityutskiy break; 153801c135cSArtem B. Bityutskiy } 154450f872aSArtem Bityutskiy get_device(&vol->dev); 155*d05c77a8SArtem Bityutskiy vol->ref_count += 1; 156801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 157801c135cSArtem B. Bityutskiy 158801c135cSArtem B. Bityutskiy desc->vol = vol; 159801c135cSArtem B. Bityutskiy desc->mode = mode; 160801c135cSArtem B. Bityutskiy 161801c135cSArtem B. Bityutskiy /* 162cae0a771SArtem Bityutskiy * To prevent simultaneous checks of the same volume we use 163cae0a771SArtem Bityutskiy * @volumes_mutex, although it is not the purpose it was introduced 164cae0a771SArtem Bityutskiy * for. 165801c135cSArtem B. Bityutskiy */ 166cae0a771SArtem Bityutskiy mutex_lock(&ubi->volumes_mutex); 167801c135cSArtem B. Bityutskiy if (!vol->checked) { 168801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 169801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 170801c135cSArtem B. Bityutskiy if (err < 0) { 171cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 172801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 173801c135cSArtem B. Bityutskiy return ERR_PTR(err); 174801c135cSArtem B. Bityutskiy } 175801c135cSArtem B. Bityutskiy if (err == 1) { 176801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 177801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 178801c135cSArtem B. Bityutskiy vol->corrupted = 1; 179801c135cSArtem B. Bityutskiy } 180801c135cSArtem B. Bityutskiy vol->checked = 1; 181801c135cSArtem B. Bityutskiy } 182cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 18335ad5fb7SArtem Bityutskiy 184801c135cSArtem B. Bityutskiy return desc; 185801c135cSArtem B. Bityutskiy 186801c135cSArtem B. Bityutskiy out_unlock: 187801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 188801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 18935ad5fb7SArtem Bityutskiy out_free: 19035ad5fb7SArtem Bityutskiy kfree(desc); 191801c135cSArtem B. Bityutskiy return ERR_PTR(err); 192801c135cSArtem B. Bityutskiy } 193801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 194801c135cSArtem B. Bityutskiy 195801c135cSArtem B. Bityutskiy /** 196801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 197801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 198801c135cSArtem B. Bityutskiy * @name: volume name 199801c135cSArtem B. Bityutskiy * @mode: open mode 200801c135cSArtem B. Bityutskiy * 201801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 202801c135cSArtem B. Bityutskiy */ 203801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 204801c135cSArtem B. Bityutskiy int mode) 205801c135cSArtem B. Bityutskiy { 206801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 207801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 208801c135cSArtem B. Bityutskiy 209801c135cSArtem B. Bityutskiy dbg_msg("open volume %s, mode %d", name, mode); 210801c135cSArtem B. Bityutskiy 211801c135cSArtem B. Bityutskiy if (!name) 212801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 213801c135cSArtem B. Bityutskiy 214801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 215801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 216801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 217801c135cSArtem B. Bityutskiy 21835ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 21935ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 220801c135cSArtem B. Bityutskiy 221801c135cSArtem B. Bityutskiy ubi = ubi_devices[ubi_num]; 22235ad5fb7SArtem Bityutskiy if (!ubi) 22335ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 224801c135cSArtem B. Bityutskiy 225801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 226801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 227801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 228801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 229801c135cSArtem B. Bityutskiy 230801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 231801c135cSArtem B. Bityutskiy vol_id = i; 232801c135cSArtem B. Bityutskiy break; 233801c135cSArtem B. Bityutskiy } 234801c135cSArtem B. Bityutskiy } 235801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 236801c135cSArtem B. Bityutskiy 237801c135cSArtem B. Bityutskiy if (vol_id < 0) 23835ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 239801c135cSArtem B. Bityutskiy 24035ad5fb7SArtem Bityutskiy return ubi_open_volume(ubi_num, vol_id, mode); 241801c135cSArtem B. Bityutskiy } 242801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 243801c135cSArtem B. Bityutskiy 244801c135cSArtem B. Bityutskiy /** 245801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 246801c135cSArtem B. Bityutskiy * @desc: volume descriptor 247801c135cSArtem B. Bityutskiy */ 248801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 249801c135cSArtem B. Bityutskiy { 250801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 251801c135cSArtem B. Bityutskiy 252801c135cSArtem B. Bityutskiy dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); 253801c135cSArtem B. Bityutskiy 254801c135cSArtem B. Bityutskiy spin_lock(&vol->ubi->volumes_lock); 255801c135cSArtem B. Bityutskiy switch (desc->mode) { 256801c135cSArtem B. Bityutskiy case UBI_READONLY: 257801c135cSArtem B. Bityutskiy vol->readers -= 1; 258801c135cSArtem B. Bityutskiy break; 259801c135cSArtem B. Bityutskiy case UBI_READWRITE: 260801c135cSArtem B. Bityutskiy vol->writers -= 1; 261801c135cSArtem B. Bityutskiy break; 262801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 263801c135cSArtem B. Bityutskiy vol->exclusive = 0; 264801c135cSArtem B. Bityutskiy } 265*d05c77a8SArtem Bityutskiy vol->ref_count -= 1; 266801c135cSArtem B. Bityutskiy spin_unlock(&vol->ubi->volumes_lock); 267801c135cSArtem B. Bityutskiy 268450f872aSArtem Bityutskiy put_device(&vol->dev); 269*d05c77a8SArtem Bityutskiy kfree(desc); 270801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 271801c135cSArtem B. Bityutskiy } 272801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 273801c135cSArtem B. Bityutskiy 274801c135cSArtem B. Bityutskiy /** 275801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 276801c135cSArtem B. Bityutskiy * @desc: volume descriptor 277801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 278801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 279801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 280801c135cSArtem B. Bityutskiy * @len: how many bytes to read 281801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 282801c135cSArtem B. Bityutskiy * 283801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 284801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 285801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 286801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 287801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 288801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 289801c135cSArtem B. Bityutskiy * 290801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 291801c135cSArtem B. Bityutskiy * function returns a negative error code. 292801c135cSArtem B. Bityutskiy * 293801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 294801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 295801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 296801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 297801c135cSArtem B. Bityutskiy * 298801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 299801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF error code. 300801c135cSArtem B. Bityutskiy */ 301801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 302801c135cSArtem B. Bityutskiy int len, int check) 303801c135cSArtem B. Bityutskiy { 304801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 305801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 306801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 307801c135cSArtem B. Bityutskiy 308801c135cSArtem B. Bityutskiy dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 309801c135cSArtem B. Bityutskiy 310801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 311801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 312801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 313801c135cSArtem B. Bityutskiy return -EINVAL; 314801c135cSArtem B. Bityutskiy 3154ab60a0dSArtem Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME) { 3164ab60a0dSArtem Bityutskiy if (vol->used_ebs == 0) 3174ab60a0dSArtem Bityutskiy /* Empty static UBI volume */ 3184ab60a0dSArtem Bityutskiy return 0; 3194ab60a0dSArtem Bityutskiy if (lnum == vol->used_ebs - 1 && 320801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 321801c135cSArtem B. Bityutskiy return -EINVAL; 3224ab60a0dSArtem Bityutskiy } 323801c135cSArtem B. Bityutskiy 324801c135cSArtem B. Bityutskiy if (vol->upd_marker) 325801c135cSArtem B. Bityutskiy return -EBADF; 326801c135cSArtem B. Bityutskiy if (len == 0) 327801c135cSArtem B. Bityutskiy return 0; 328801c135cSArtem B. Bityutskiy 32989b96b69SArtem Bityutskiy err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); 330801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 331801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 332801c135cSArtem B. Bityutskiy vol->corrupted = 1; 333801c135cSArtem B. Bityutskiy } 334801c135cSArtem B. Bityutskiy 335801c135cSArtem B. Bityutskiy return err; 336801c135cSArtem B. Bityutskiy } 337801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 338801c135cSArtem B. Bityutskiy 339801c135cSArtem B. Bityutskiy /** 340801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 341801c135cSArtem B. Bityutskiy * @desc: volume descriptor 342801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 343801c135cSArtem B. Bityutskiy * @buf: data to write 344801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 345801c135cSArtem B. Bityutskiy * @len: how many bytes to write 346801c135cSArtem B. Bityutskiy * @dtype: expected data type 347801c135cSArtem B. Bityutskiy * 348801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 349801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 350801c135cSArtem B. Bityutskiy * the data. 351801c135cSArtem B. Bityutskiy * 352801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 353801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 354801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 355801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 356801c135cSArtem B. Bityutskiy * 357801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 358801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 359801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 360801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 361801c135cSArtem B. Bityutskiy * garbage. 362801c135cSArtem B. Bityutskiy * 363801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 364801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 365801c135cSArtem B. Bityutskiy */ 366801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 367801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 368801c135cSArtem B. Bityutskiy { 369801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 370801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 371801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 372801c135cSArtem B. Bityutskiy 373801c135cSArtem B. Bityutskiy dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 374801c135cSArtem B. Bityutskiy 375801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 376801c135cSArtem B. Bityutskiy return -EINVAL; 377801c135cSArtem B. Bityutskiy 378801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 379801c135cSArtem B. Bityutskiy return -EROFS; 380801c135cSArtem B. Bityutskiy 381801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 382801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size || offset % ubi->min_io_size || 383801c135cSArtem B. Bityutskiy len % ubi->min_io_size) 384801c135cSArtem B. Bityutskiy return -EINVAL; 385801c135cSArtem B. Bityutskiy 386801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 387801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 388801c135cSArtem B. Bityutskiy return -EINVAL; 389801c135cSArtem B. Bityutskiy 390801c135cSArtem B. Bityutskiy if (vol->upd_marker) 391801c135cSArtem B. Bityutskiy return -EBADF; 392801c135cSArtem B. Bityutskiy 393801c135cSArtem B. Bityutskiy if (len == 0) 394801c135cSArtem B. Bityutskiy return 0; 395801c135cSArtem B. Bityutskiy 39689b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 397801c135cSArtem B. Bityutskiy } 398801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 399801c135cSArtem B. Bityutskiy 400801c135cSArtem B. Bityutskiy /* 401801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 402801c135cSArtem B. Bityutskiy * @desc: volume descriptor 403801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 404801c135cSArtem B. Bityutskiy * @buf: data to write 405801c135cSArtem B. Bityutskiy * @len: how many bytes to write 406801c135cSArtem B. Bityutskiy * @dtype: expected data type 407801c135cSArtem B. Bityutskiy * 408801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 409801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 410801c135cSArtem B. Bityutskiy * data, which has to be aligned. The length may be shorter then the logical 411801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 412801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 413801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 414801c135cSArtem B. Bityutskiy * code in case of failure. 415801c135cSArtem B. Bityutskiy */ 416801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 417801c135cSArtem B. Bityutskiy int len, int dtype) 418801c135cSArtem B. Bityutskiy { 419801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 420801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 421801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 422801c135cSArtem B. Bityutskiy 423801c135cSArtem B. Bityutskiy dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 424801c135cSArtem B. Bityutskiy 425801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 426801c135cSArtem B. Bityutskiy return -EINVAL; 427801c135cSArtem B. Bityutskiy 428801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 429801c135cSArtem B. Bityutskiy return -EROFS; 430801c135cSArtem B. Bityutskiy 431801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 432801c135cSArtem B. Bityutskiy len > vol->usable_leb_size || len % ubi->min_io_size) 433801c135cSArtem B. Bityutskiy return -EINVAL; 434801c135cSArtem B. Bityutskiy 435801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 436801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 437801c135cSArtem B. Bityutskiy return -EINVAL; 438801c135cSArtem B. Bityutskiy 439801c135cSArtem B. Bityutskiy if (vol->upd_marker) 440801c135cSArtem B. Bityutskiy return -EBADF; 441801c135cSArtem B. Bityutskiy 442801c135cSArtem B. Bityutskiy if (len == 0) 443801c135cSArtem B. Bityutskiy return 0; 444801c135cSArtem B. Bityutskiy 44589b96b69SArtem Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 446801c135cSArtem B. Bityutskiy } 447801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 448801c135cSArtem B. Bityutskiy 449801c135cSArtem B. Bityutskiy /** 450801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 451801c135cSArtem B. Bityutskiy * @desc: volume descriptor 452801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 453801c135cSArtem B. Bityutskiy * 454801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 455801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 456801c135cSArtem B. Bityutskiy * negative error code in case of failure. 457801c135cSArtem B. Bityutskiy * 458801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 459801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 460801c135cSArtem B. Bityutskiy */ 461801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 462801c135cSArtem B. Bityutskiy { 463801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 464801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 465801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 466801c135cSArtem B. Bityutskiy 467801c135cSArtem B. Bityutskiy dbg_msg("erase LEB %d:%d", vol_id, lnum); 468801c135cSArtem B. Bityutskiy 469801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 470801c135cSArtem B. Bityutskiy return -EROFS; 471801c135cSArtem B. Bityutskiy 472801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 473801c135cSArtem B. Bityutskiy return -EINVAL; 474801c135cSArtem B. Bityutskiy 475801c135cSArtem B. Bityutskiy if (vol->upd_marker) 476801c135cSArtem B. Bityutskiy return -EBADF; 477801c135cSArtem B. Bityutskiy 47889b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 479801c135cSArtem B. Bityutskiy if (err) 480801c135cSArtem B. Bityutskiy return err; 481801c135cSArtem B. Bityutskiy 482801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 483801c135cSArtem B. Bityutskiy } 484801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 485801c135cSArtem B. Bityutskiy 486801c135cSArtem B. Bityutskiy /** 487801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 488801c135cSArtem B. Bityutskiy * @desc: volume descriptor 489801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 490801c135cSArtem B. Bityutskiy * 491801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 492801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 493801c135cSArtem B. Bityutskiy * physically erased in background. This operation is much faster then the 494801c135cSArtem B. Bityutskiy * erase operation. 495801c135cSArtem B. Bityutskiy * 496801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 497801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 498801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 499801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 500801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 501801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 502801c135cSArtem B. Bityutskiy * used with care. 503801c135cSArtem B. Bityutskiy * 504801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 505801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 506801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 507801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 508801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 509801c135cSArtem B. Bityutskiy * 510801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 511801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 512801c135cSArtem B. Bityutskiy * first un-map it, then write new data, rather then first erase it, then write 513801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 514801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 515801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 516801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 517801c135cSArtem B. Bityutskiy * 518801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 519801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 520801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 521801c135cSArtem B. Bityutskiy */ 522801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 523801c135cSArtem B. Bityutskiy { 524801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 525801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 526801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 527801c135cSArtem B. Bityutskiy 528801c135cSArtem B. Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 529801c135cSArtem B. Bityutskiy 530801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 531801c135cSArtem B. Bityutskiy return -EROFS; 532801c135cSArtem B. Bityutskiy 533801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 534801c135cSArtem B. Bityutskiy return -EINVAL; 535801c135cSArtem B. Bityutskiy 536801c135cSArtem B. Bityutskiy if (vol->upd_marker) 537801c135cSArtem B. Bityutskiy return -EBADF; 538801c135cSArtem B. Bityutskiy 53989b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 540801c135cSArtem B. Bityutskiy } 541801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 542801c135cSArtem B. Bityutskiy 543801c135cSArtem B. Bityutskiy /** 544393852ecSArtem Bityutskiy * ubi_leb_map - map logical erasblock to a physical eraseblock. 545393852ecSArtem Bityutskiy * @desc: volume descriptor 546393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 547393852ecSArtem Bityutskiy * @dtype: expected data type 548393852ecSArtem Bityutskiy * 549393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 550393852ecSArtem Bityutskiy * eraseblock. This means, that after a successfull invocation of this 551393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 552393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 553393852ecSArtem Bityutskiy * happens. 554393852ecSArtem Bityutskiy * 555393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 556393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 557393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 558393852ecSArtem Bityutskiy * other failures. 559393852ecSArtem Bityutskiy */ 560393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 561393852ecSArtem Bityutskiy { 562393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 563393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 564393852ecSArtem Bityutskiy int vol_id = vol->vol_id; 565393852ecSArtem Bityutskiy 566393852ecSArtem Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 567393852ecSArtem Bityutskiy 568393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 569393852ecSArtem Bityutskiy return -EROFS; 570393852ecSArtem Bityutskiy 571393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 572393852ecSArtem Bityutskiy return -EINVAL; 573393852ecSArtem Bityutskiy 574393852ecSArtem Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 575393852ecSArtem Bityutskiy dtype != UBI_UNKNOWN) 576393852ecSArtem Bityutskiy return -EINVAL; 577393852ecSArtem Bityutskiy 578393852ecSArtem Bityutskiy if (vol->upd_marker) 579393852ecSArtem Bityutskiy return -EBADF; 580393852ecSArtem Bityutskiy 581393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 582393852ecSArtem Bityutskiy return -EBADMSG; 583393852ecSArtem Bityutskiy 58489b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 585393852ecSArtem Bityutskiy } 586393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 587393852ecSArtem Bityutskiy 588393852ecSArtem Bityutskiy /** 589801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 590801c135cSArtem B. Bityutskiy * @desc: volume descriptor 591801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 592801c135cSArtem B. Bityutskiy * 593801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 594801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 595801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 596801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 597801c135cSArtem B. Bityutskiy * mapped to. 598801c135cSArtem B. Bityutskiy * 599801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 600801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 601801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 602801c135cSArtem B. Bityutskiy * code. 603801c135cSArtem B. Bityutskiy */ 604801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 605801c135cSArtem B. Bityutskiy { 606801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 607801c135cSArtem B. Bityutskiy 608801c135cSArtem B. Bityutskiy dbg_msg("test LEB %d:%d", vol->vol_id, lnum); 609801c135cSArtem B. Bityutskiy 610801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 611801c135cSArtem B. Bityutskiy return -EINVAL; 612801c135cSArtem B. Bityutskiy 613801c135cSArtem B. Bityutskiy if (vol->upd_marker) 614801c135cSArtem B. Bityutskiy return -EBADF; 615801c135cSArtem B. Bityutskiy 616801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 617801c135cSArtem B. Bityutskiy } 618801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 619