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 * 33*e73f4459SArtem Bityutskiy * This function returns %0 in case of success, %-EINVAL if the UBI device 34*e73f4459SArtem Bityutskiy * number is invalid, and %-ENODEV if there is no 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 { 38*e73f4459SArtem Bityutskiy struct ubi_device *ubi; 39801c135cSArtem B. Bityutskiy 40*e73f4459SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 41*e73f4459SArtem Bityutskiy return -EINVAL; 42*e73f4459SArtem Bityutskiy 43*e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 44*e73f4459SArtem Bityutskiy if (!ubi) 45801c135cSArtem B. Bityutskiy return -ENODEV; 46801c135cSArtem B. Bityutskiy 47801c135cSArtem B. Bityutskiy di->ubi_num = ubi->ubi_num; 48801c135cSArtem B. Bityutskiy di->leb_size = ubi->leb_size; 49801c135cSArtem B. Bityutskiy di->min_io_size = ubi->min_io_size; 50801c135cSArtem B. Bityutskiy di->ro_mode = ubi->ro_mode; 5149dfc299SArtem Bityutskiy di->cdev = ubi->cdev.dev; 52*e73f4459SArtem Bityutskiy 53*e73f4459SArtem Bityutskiy ubi_put_device(ubi); 54801c135cSArtem B. Bityutskiy return 0; 55801c135cSArtem B. Bityutskiy } 56801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info); 57801c135cSArtem B. Bityutskiy 58801c135cSArtem B. Bityutskiy /** 59801c135cSArtem B. Bityutskiy * ubi_get_volume_info - get information about UBI volume. 60801c135cSArtem B. Bityutskiy * @desc: volume descriptor 61801c135cSArtem B. Bityutskiy * @vi: the information is stored here 62801c135cSArtem B. Bityutskiy */ 63801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc, 64801c135cSArtem B. Bityutskiy struct ubi_volume_info *vi) 65801c135cSArtem B. Bityutskiy { 66801c135cSArtem B. Bityutskiy const struct ubi_volume *vol = desc->vol; 67801c135cSArtem B. Bityutskiy const struct ubi_device *ubi = vol->ubi; 68801c135cSArtem B. Bityutskiy 69801c135cSArtem B. Bityutskiy vi->vol_id = vol->vol_id; 70801c135cSArtem B. Bityutskiy vi->ubi_num = ubi->ubi_num; 71801c135cSArtem B. Bityutskiy vi->size = vol->reserved_pebs; 72801c135cSArtem B. Bityutskiy vi->used_bytes = vol->used_bytes; 73801c135cSArtem B. Bityutskiy vi->vol_type = vol->vol_type; 74801c135cSArtem B. Bityutskiy vi->corrupted = vol->corrupted; 75801c135cSArtem B. Bityutskiy vi->upd_marker = vol->upd_marker; 76801c135cSArtem B. Bityutskiy vi->alignment = vol->alignment; 77801c135cSArtem B. Bityutskiy vi->usable_leb_size = vol->usable_leb_size; 78801c135cSArtem B. Bityutskiy vi->name_len = vol->name_len; 79801c135cSArtem B. Bityutskiy vi->name = vol->name; 8049dfc299SArtem Bityutskiy vi->cdev = vol->cdev.dev; 81801c135cSArtem B. Bityutskiy } 82801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info); 83801c135cSArtem B. Bityutskiy 84801c135cSArtem B. Bityutskiy /** 85801c135cSArtem B. Bityutskiy * ubi_open_volume - open UBI volume. 86801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 87801c135cSArtem B. Bityutskiy * @vol_id: volume ID 88801c135cSArtem B. Bityutskiy * @mode: open mode 89801c135cSArtem B. Bityutskiy * 90801c135cSArtem B. Bityutskiy * The @mode parameter specifies if the volume should be opened in read-only 91801c135cSArtem B. Bityutskiy * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that 92801c135cSArtem B. Bityutskiy * nobody else will be able to open this volume. UBI allows to have many volume 93801c135cSArtem B. Bityutskiy * readers and one writer at a time. 94801c135cSArtem B. Bityutskiy * 95801c135cSArtem B. Bityutskiy * If a static volume is being opened for the first time since boot, it will be 96801c135cSArtem B. Bityutskiy * checked by this function, which means it will be fully read and the CRC 97801c135cSArtem B. Bityutskiy * checksum of each logical eraseblock will be checked. 98801c135cSArtem B. Bityutskiy * 99801c135cSArtem B. Bityutskiy * This function returns volume descriptor in case of success and a negative 100801c135cSArtem B. Bityutskiy * error code in case of failure. 101801c135cSArtem B. Bityutskiy */ 102801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) 103801c135cSArtem B. Bityutskiy { 104801c135cSArtem B. Bityutskiy int err; 105801c135cSArtem B. Bityutskiy struct ubi_volume_desc *desc; 1060169b49dSJesper Juhl struct ubi_device *ubi; 107801c135cSArtem B. Bityutskiy struct ubi_volume *vol; 108801c135cSArtem B. Bityutskiy 109801c135cSArtem B. Bityutskiy dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode); 110801c135cSArtem B. Bityutskiy 11135ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 11235ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 1130169b49dSJesper Juhl 114801c135cSArtem B. Bityutskiy if (mode != UBI_READONLY && mode != UBI_READWRITE && 115801c135cSArtem B. Bityutskiy mode != UBI_EXCLUSIVE) 11635ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 11735ad5fb7SArtem Bityutskiy 118*e73f4459SArtem Bityutskiy /* 119*e73f4459SArtem Bityutskiy * First of all, we have to get the UBI device to prevent its removal. 120*e73f4459SArtem Bityutskiy */ 121*e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 12235ad5fb7SArtem Bityutskiy if (!ubi) 12335ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 12435ad5fb7SArtem Bityutskiy 125*e73f4459SArtem Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) { 126*e73f4459SArtem Bityutskiy err = -EINVAL; 127*e73f4459SArtem Bityutskiy goto out_put_ubi; 128*e73f4459SArtem Bityutskiy } 129801c135cSArtem B. Bityutskiy 130801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 131*e73f4459SArtem Bityutskiy if (!desc) { 132*e73f4459SArtem Bityutskiy err = -ENOMEM; 133*e73f4459SArtem Bityutskiy goto out_put_ubi; 134*e73f4459SArtem Bityutskiy } 13535ad5fb7SArtem Bityutskiy 13635ad5fb7SArtem Bityutskiy err = -ENODEV; 13735ad5fb7SArtem Bityutskiy if (!try_module_get(THIS_MODULE)) 13835ad5fb7SArtem Bityutskiy goto out_free; 139801c135cSArtem B. Bityutskiy 140801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 141801c135cSArtem B. Bityutskiy vol = ubi->volumes[vol_id]; 14235ad5fb7SArtem Bityutskiy if (!vol) 143801c135cSArtem B. Bityutskiy goto out_unlock; 144801c135cSArtem B. Bityutskiy 145801c135cSArtem B. Bityutskiy err = -EBUSY; 146801c135cSArtem B. Bityutskiy switch (mode) { 147801c135cSArtem B. Bityutskiy case UBI_READONLY: 148801c135cSArtem B. Bityutskiy if (vol->exclusive) 149801c135cSArtem B. Bityutskiy goto out_unlock; 150801c135cSArtem B. Bityutskiy vol->readers += 1; 151801c135cSArtem B. Bityutskiy break; 152801c135cSArtem B. Bityutskiy 153801c135cSArtem B. Bityutskiy case UBI_READWRITE: 154801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers > 0) 155801c135cSArtem B. Bityutskiy goto out_unlock; 156801c135cSArtem B. Bityutskiy vol->writers += 1; 157801c135cSArtem B. Bityutskiy break; 158801c135cSArtem B. Bityutskiy 159801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 160801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers || vol->readers) 161801c135cSArtem B. Bityutskiy goto out_unlock; 162801c135cSArtem B. Bityutskiy vol->exclusive = 1; 163801c135cSArtem B. Bityutskiy break; 164801c135cSArtem B. Bityutskiy } 165450f872aSArtem Bityutskiy get_device(&vol->dev); 166d05c77a8SArtem Bityutskiy vol->ref_count += 1; 167801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 168801c135cSArtem B. Bityutskiy 169801c135cSArtem B. Bityutskiy desc->vol = vol; 170801c135cSArtem B. Bityutskiy desc->mode = mode; 171801c135cSArtem B. Bityutskiy 172801c135cSArtem B. Bityutskiy /* 173cae0a771SArtem Bityutskiy * To prevent simultaneous checks of the same volume we use 174cae0a771SArtem Bityutskiy * @volumes_mutex, although it is not the purpose it was introduced 175cae0a771SArtem Bityutskiy * for. 176801c135cSArtem B. Bityutskiy */ 177cae0a771SArtem Bityutskiy mutex_lock(&ubi->volumes_mutex); 178801c135cSArtem B. Bityutskiy if (!vol->checked) { 179801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 180801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 181801c135cSArtem B. Bityutskiy if (err < 0) { 182cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 183801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 184801c135cSArtem B. Bityutskiy return ERR_PTR(err); 185801c135cSArtem B. Bityutskiy } 186801c135cSArtem B. Bityutskiy if (err == 1) { 187801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 188801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 189801c135cSArtem B. Bityutskiy vol->corrupted = 1; 190801c135cSArtem B. Bityutskiy } 191801c135cSArtem B. Bityutskiy vol->checked = 1; 192801c135cSArtem B. Bityutskiy } 193cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 19435ad5fb7SArtem Bityutskiy 195801c135cSArtem B. Bityutskiy return desc; 196801c135cSArtem B. Bityutskiy 197801c135cSArtem B. Bityutskiy out_unlock: 198801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 199801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 20035ad5fb7SArtem Bityutskiy out_free: 20135ad5fb7SArtem Bityutskiy kfree(desc); 202*e73f4459SArtem Bityutskiy out_put_ubi: 203*e73f4459SArtem Bityutskiy ubi_put_device(ubi); 204801c135cSArtem B. Bityutskiy return ERR_PTR(err); 205801c135cSArtem B. Bityutskiy } 206801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 207801c135cSArtem B. Bityutskiy 208801c135cSArtem B. Bityutskiy /** 209801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 210801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 211801c135cSArtem B. Bityutskiy * @name: volume name 212801c135cSArtem B. Bityutskiy * @mode: open mode 213801c135cSArtem B. Bityutskiy * 214801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 215801c135cSArtem B. Bityutskiy */ 216801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 217801c135cSArtem B. Bityutskiy int mode) 218801c135cSArtem B. Bityutskiy { 219801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 220801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 221*e73f4459SArtem Bityutskiy struct ubi_volume_desc *ret; 222801c135cSArtem B. Bityutskiy 223801c135cSArtem B. Bityutskiy dbg_msg("open volume %s, mode %d", name, mode); 224801c135cSArtem B. Bityutskiy 225801c135cSArtem B. Bityutskiy if (!name) 226801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 227801c135cSArtem B. Bityutskiy 228801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 229801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 230801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 231801c135cSArtem B. Bityutskiy 23235ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 23335ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 234801c135cSArtem B. Bityutskiy 235*e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 23635ad5fb7SArtem Bityutskiy if (!ubi) 23735ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 238801c135cSArtem B. Bityutskiy 239801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 240801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 241801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 242801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 243801c135cSArtem B. Bityutskiy 244801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 245801c135cSArtem B. Bityutskiy vol_id = i; 246801c135cSArtem B. Bityutskiy break; 247801c135cSArtem B. Bityutskiy } 248801c135cSArtem B. Bityutskiy } 249801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 250801c135cSArtem B. Bityutskiy 251*e73f4459SArtem Bityutskiy if (vol_id >= 0) 252*e73f4459SArtem Bityutskiy ret = ubi_open_volume(ubi_num, vol_id, mode); 253*e73f4459SArtem Bityutskiy else 254*e73f4459SArtem Bityutskiy ret = ERR_PTR(-ENODEV); 255801c135cSArtem B. Bityutskiy 256*e73f4459SArtem Bityutskiy /* 257*e73f4459SArtem Bityutskiy * We should put the UBI device even in case of success, because 258*e73f4459SArtem Bityutskiy * 'ubi_open_volume()' took a reference as well. 259*e73f4459SArtem Bityutskiy */ 260*e73f4459SArtem Bityutskiy ubi_put_device(ubi); 261*e73f4459SArtem Bityutskiy return ret; 262801c135cSArtem B. Bityutskiy } 263801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 264801c135cSArtem B. Bityutskiy 265801c135cSArtem B. Bityutskiy /** 266801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 267801c135cSArtem B. Bityutskiy * @desc: volume descriptor 268801c135cSArtem B. Bityutskiy */ 269801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 270801c135cSArtem B. Bityutskiy { 271801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 272*e73f4459SArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 273801c135cSArtem B. Bityutskiy 274801c135cSArtem B. Bityutskiy dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); 275801c135cSArtem B. Bityutskiy 276*e73f4459SArtem Bityutskiy spin_lock(&ubi->volumes_lock); 277801c135cSArtem B. Bityutskiy switch (desc->mode) { 278801c135cSArtem B. Bityutskiy case UBI_READONLY: 279801c135cSArtem B. Bityutskiy vol->readers -= 1; 280801c135cSArtem B. Bityutskiy break; 281801c135cSArtem B. Bityutskiy case UBI_READWRITE: 282801c135cSArtem B. Bityutskiy vol->writers -= 1; 283801c135cSArtem B. Bityutskiy break; 284801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 285801c135cSArtem B. Bityutskiy vol->exclusive = 0; 286801c135cSArtem B. Bityutskiy } 287d05c77a8SArtem Bityutskiy vol->ref_count -= 1; 288*e73f4459SArtem Bityutskiy spin_unlock(&ubi->volumes_lock); 289801c135cSArtem B. Bityutskiy 290d05c77a8SArtem Bityutskiy kfree(desc); 291*e73f4459SArtem Bityutskiy put_device(&vol->dev); 292*e73f4459SArtem Bityutskiy ubi_put_device(ubi); 293801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 294801c135cSArtem B. Bityutskiy } 295801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 296801c135cSArtem B. Bityutskiy 297801c135cSArtem B. Bityutskiy /** 298801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 299801c135cSArtem B. Bityutskiy * @desc: volume descriptor 300801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 301801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 302801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 303801c135cSArtem B. Bityutskiy * @len: how many bytes to read 304801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 305801c135cSArtem B. Bityutskiy * 306801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 307801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 308801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 309801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 310801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 311801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 312801c135cSArtem B. Bityutskiy * 313801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 314801c135cSArtem B. Bityutskiy * function returns a negative error code. 315801c135cSArtem B. Bityutskiy * 316801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 317801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 318801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 319801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 320801c135cSArtem B. Bityutskiy * 321801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 322801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF error code. 323801c135cSArtem B. Bityutskiy */ 324801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 325801c135cSArtem B. Bityutskiy int len, int check) 326801c135cSArtem B. Bityutskiy { 327801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 328801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 329801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 330801c135cSArtem B. Bityutskiy 331801c135cSArtem B. Bityutskiy dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 332801c135cSArtem B. Bityutskiy 333801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 334801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 335801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 336801c135cSArtem B. Bityutskiy return -EINVAL; 337801c135cSArtem B. Bityutskiy 3384ab60a0dSArtem Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME) { 3394ab60a0dSArtem Bityutskiy if (vol->used_ebs == 0) 3404ab60a0dSArtem Bityutskiy /* Empty static UBI volume */ 3414ab60a0dSArtem Bityutskiy return 0; 3424ab60a0dSArtem Bityutskiy if (lnum == vol->used_ebs - 1 && 343801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 344801c135cSArtem B. Bityutskiy return -EINVAL; 3454ab60a0dSArtem Bityutskiy } 346801c135cSArtem B. Bityutskiy 347801c135cSArtem B. Bityutskiy if (vol->upd_marker) 348801c135cSArtem B. Bityutskiy return -EBADF; 349801c135cSArtem B. Bityutskiy if (len == 0) 350801c135cSArtem B. Bityutskiy return 0; 351801c135cSArtem B. Bityutskiy 35289b96b69SArtem Bityutskiy err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); 353801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 354801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 355801c135cSArtem B. Bityutskiy vol->corrupted = 1; 356801c135cSArtem B. Bityutskiy } 357801c135cSArtem B. Bityutskiy 358801c135cSArtem B. Bityutskiy return err; 359801c135cSArtem B. Bityutskiy } 360801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 361801c135cSArtem B. Bityutskiy 362801c135cSArtem B. Bityutskiy /** 363801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 364801c135cSArtem B. Bityutskiy * @desc: volume descriptor 365801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 366801c135cSArtem B. Bityutskiy * @buf: data to write 367801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 368801c135cSArtem B. Bityutskiy * @len: how many bytes to write 369801c135cSArtem B. Bityutskiy * @dtype: expected data type 370801c135cSArtem B. Bityutskiy * 371801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 372801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 373801c135cSArtem B. Bityutskiy * the data. 374801c135cSArtem B. Bityutskiy * 375801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 376801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 377801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 378801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 379801c135cSArtem B. Bityutskiy * 380801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 381801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 382801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 383801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 384801c135cSArtem B. Bityutskiy * garbage. 385801c135cSArtem B. Bityutskiy * 386801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 387801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 388801c135cSArtem B. Bityutskiy */ 389801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 390801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 391801c135cSArtem B. Bityutskiy { 392801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 393801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 394801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 395801c135cSArtem B. Bityutskiy 396801c135cSArtem B. Bityutskiy dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 397801c135cSArtem B. Bityutskiy 398801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 399801c135cSArtem B. Bityutskiy return -EINVAL; 400801c135cSArtem B. Bityutskiy 401801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 402801c135cSArtem B. Bityutskiy return -EROFS; 403801c135cSArtem B. Bityutskiy 404801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 405801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size || offset % ubi->min_io_size || 406801c135cSArtem B. Bityutskiy len % ubi->min_io_size) 407801c135cSArtem B. Bityutskiy return -EINVAL; 408801c135cSArtem B. Bityutskiy 409801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 410801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 411801c135cSArtem B. Bityutskiy return -EINVAL; 412801c135cSArtem B. Bityutskiy 413801c135cSArtem B. Bityutskiy if (vol->upd_marker) 414801c135cSArtem B. Bityutskiy return -EBADF; 415801c135cSArtem B. Bityutskiy 416801c135cSArtem B. Bityutskiy if (len == 0) 417801c135cSArtem B. Bityutskiy return 0; 418801c135cSArtem B. Bityutskiy 41989b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 420801c135cSArtem B. Bityutskiy } 421801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 422801c135cSArtem B. Bityutskiy 423801c135cSArtem B. Bityutskiy /* 424801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 425801c135cSArtem B. Bityutskiy * @desc: volume descriptor 426801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 427801c135cSArtem B. Bityutskiy * @buf: data to write 428801c135cSArtem B. Bityutskiy * @len: how many bytes to write 429801c135cSArtem B. Bityutskiy * @dtype: expected data type 430801c135cSArtem B. Bityutskiy * 431801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 432801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 433801c135cSArtem B. Bityutskiy * data, which has to be aligned. The length may be shorter then the logical 434801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 435801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 436801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 437801c135cSArtem B. Bityutskiy * code in case of failure. 438801c135cSArtem B. Bityutskiy */ 439801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 440801c135cSArtem B. Bityutskiy int len, int dtype) 441801c135cSArtem B. Bityutskiy { 442801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 443801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 444801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 445801c135cSArtem B. Bityutskiy 446801c135cSArtem B. Bityutskiy dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 447801c135cSArtem B. Bityutskiy 448801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 449801c135cSArtem B. Bityutskiy return -EINVAL; 450801c135cSArtem B. Bityutskiy 451801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 452801c135cSArtem B. Bityutskiy return -EROFS; 453801c135cSArtem B. Bityutskiy 454801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 455801c135cSArtem B. Bityutskiy len > vol->usable_leb_size || len % ubi->min_io_size) 456801c135cSArtem B. Bityutskiy return -EINVAL; 457801c135cSArtem B. Bityutskiy 458801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 459801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 460801c135cSArtem B. Bityutskiy return -EINVAL; 461801c135cSArtem B. Bityutskiy 462801c135cSArtem B. Bityutskiy if (vol->upd_marker) 463801c135cSArtem B. Bityutskiy return -EBADF; 464801c135cSArtem B. Bityutskiy 465801c135cSArtem B. Bityutskiy if (len == 0) 466801c135cSArtem B. Bityutskiy return 0; 467801c135cSArtem B. Bityutskiy 46889b96b69SArtem Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 469801c135cSArtem B. Bityutskiy } 470801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 471801c135cSArtem B. Bityutskiy 472801c135cSArtem B. Bityutskiy /** 473801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 474801c135cSArtem B. Bityutskiy * @desc: volume descriptor 475801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 476801c135cSArtem B. Bityutskiy * 477801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 478801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 479801c135cSArtem B. Bityutskiy * negative error code in case of failure. 480801c135cSArtem B. Bityutskiy * 481801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 482801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 483801c135cSArtem B. Bityutskiy */ 484801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 485801c135cSArtem B. Bityutskiy { 486801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 487801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 488801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 489801c135cSArtem B. Bityutskiy 490801c135cSArtem B. Bityutskiy dbg_msg("erase LEB %d:%d", vol_id, lnum); 491801c135cSArtem B. Bityutskiy 492801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 493801c135cSArtem B. Bityutskiy return -EROFS; 494801c135cSArtem B. Bityutskiy 495801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 496801c135cSArtem B. Bityutskiy return -EINVAL; 497801c135cSArtem B. Bityutskiy 498801c135cSArtem B. Bityutskiy if (vol->upd_marker) 499801c135cSArtem B. Bityutskiy return -EBADF; 500801c135cSArtem B. Bityutskiy 50189b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 502801c135cSArtem B. Bityutskiy if (err) 503801c135cSArtem B. Bityutskiy return err; 504801c135cSArtem B. Bityutskiy 505801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 506801c135cSArtem B. Bityutskiy } 507801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 508801c135cSArtem B. Bityutskiy 509801c135cSArtem B. Bityutskiy /** 510801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 511801c135cSArtem B. Bityutskiy * @desc: volume descriptor 512801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 513801c135cSArtem B. Bityutskiy * 514801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 515801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 516801c135cSArtem B. Bityutskiy * physically erased in background. This operation is much faster then the 517801c135cSArtem B. Bityutskiy * erase operation. 518801c135cSArtem B. Bityutskiy * 519801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 520801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 521801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 522801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 523801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 524801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 525801c135cSArtem B. Bityutskiy * used with care. 526801c135cSArtem B. Bityutskiy * 527801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 528801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 529801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 530801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 531801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 532801c135cSArtem B. Bityutskiy * 533801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 534801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 535801c135cSArtem B. Bityutskiy * first un-map it, then write new data, rather then first erase it, then write 536801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 537801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 538801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 539801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 540801c135cSArtem B. Bityutskiy * 541801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 542801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 543801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 544801c135cSArtem B. Bityutskiy */ 545801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 546801c135cSArtem B. Bityutskiy { 547801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 548801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 549801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 550801c135cSArtem B. Bityutskiy 551801c135cSArtem B. Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 552801c135cSArtem B. Bityutskiy 553801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 554801c135cSArtem B. Bityutskiy return -EROFS; 555801c135cSArtem B. Bityutskiy 556801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 557801c135cSArtem B. Bityutskiy return -EINVAL; 558801c135cSArtem B. Bityutskiy 559801c135cSArtem B. Bityutskiy if (vol->upd_marker) 560801c135cSArtem B. Bityutskiy return -EBADF; 561801c135cSArtem B. Bityutskiy 56289b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 563801c135cSArtem B. Bityutskiy } 564801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 565801c135cSArtem B. Bityutskiy 566801c135cSArtem B. Bityutskiy /** 567393852ecSArtem Bityutskiy * ubi_leb_map - map logical erasblock to a physical eraseblock. 568393852ecSArtem Bityutskiy * @desc: volume descriptor 569393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 570393852ecSArtem Bityutskiy * @dtype: expected data type 571393852ecSArtem Bityutskiy * 572393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 573393852ecSArtem Bityutskiy * eraseblock. This means, that after a successfull invocation of this 574393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 575393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 576393852ecSArtem Bityutskiy * happens. 577393852ecSArtem Bityutskiy * 578393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 579393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 580393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 581393852ecSArtem Bityutskiy * other failures. 582393852ecSArtem Bityutskiy */ 583393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 584393852ecSArtem Bityutskiy { 585393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 586393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 587393852ecSArtem Bityutskiy int vol_id = vol->vol_id; 588393852ecSArtem Bityutskiy 589393852ecSArtem Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 590393852ecSArtem Bityutskiy 591393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 592393852ecSArtem Bityutskiy return -EROFS; 593393852ecSArtem Bityutskiy 594393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 595393852ecSArtem Bityutskiy return -EINVAL; 596393852ecSArtem Bityutskiy 597393852ecSArtem Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 598393852ecSArtem Bityutskiy dtype != UBI_UNKNOWN) 599393852ecSArtem Bityutskiy return -EINVAL; 600393852ecSArtem Bityutskiy 601393852ecSArtem Bityutskiy if (vol->upd_marker) 602393852ecSArtem Bityutskiy return -EBADF; 603393852ecSArtem Bityutskiy 604393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 605393852ecSArtem Bityutskiy return -EBADMSG; 606393852ecSArtem Bityutskiy 60789b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 608393852ecSArtem Bityutskiy } 609393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 610393852ecSArtem Bityutskiy 611393852ecSArtem Bityutskiy /** 612801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 613801c135cSArtem B. Bityutskiy * @desc: volume descriptor 614801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 615801c135cSArtem B. Bityutskiy * 616801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 617801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 618801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 619801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 620801c135cSArtem B. Bityutskiy * mapped to. 621801c135cSArtem B. Bityutskiy * 622801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 623801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 624801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 625801c135cSArtem B. Bityutskiy * code. 626801c135cSArtem B. Bityutskiy */ 627801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 628801c135cSArtem B. Bityutskiy { 629801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 630801c135cSArtem B. Bityutskiy 631801c135cSArtem B. Bityutskiy dbg_msg("test LEB %d:%d", vol->vol_id, lnum); 632801c135cSArtem B. Bityutskiy 633801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 634801c135cSArtem B. Bityutskiy return -EINVAL; 635801c135cSArtem B. Bityutskiy 636801c135cSArtem B. Bityutskiy if (vol->upd_marker) 637801c135cSArtem B. Bityutskiy return -EBADF; 638801c135cSArtem B. Bityutskiy 639801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 640801c135cSArtem B. Bityutskiy } 641801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 642