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 107801c135cSArtem B. Bityutskiy err = -ENODEV; 1080169b49dSJesper Juhl if (ubi_num < 0) 1090169b49dSJesper Juhl return ERR_PTR(err); 1100169b49dSJesper Juhl 1110169b49dSJesper Juhl ubi = ubi_devices[ubi_num]; 1120169b49dSJesper Juhl 113801c135cSArtem B. Bityutskiy if (!try_module_get(THIS_MODULE)) 114801c135cSArtem B. Bityutskiy return ERR_PTR(err); 115801c135cSArtem B. Bityutskiy 1160169b49dSJesper Juhl if (ubi_num >= UBI_MAX_DEVICES || !ubi) 117801c135cSArtem B. Bityutskiy goto out_put; 118801c135cSArtem B. Bityutskiy 119801c135cSArtem B. Bityutskiy err = -EINVAL; 120801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 121801c135cSArtem B. Bityutskiy goto out_put; 122801c135cSArtem B. Bityutskiy if (mode != UBI_READONLY && mode != UBI_READWRITE && 123801c135cSArtem B. Bityutskiy mode != UBI_EXCLUSIVE) 124801c135cSArtem B. Bityutskiy goto out_put; 125801c135cSArtem B. Bityutskiy 126801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 127801c135cSArtem B. Bityutskiy if (!desc) { 128801c135cSArtem B. Bityutskiy err = -ENOMEM; 129801c135cSArtem B. Bityutskiy goto out_put; 130801c135cSArtem B. Bityutskiy } 131801c135cSArtem B. Bityutskiy 132801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 133801c135cSArtem B. Bityutskiy vol = ubi->volumes[vol_id]; 134801c135cSArtem B. Bityutskiy if (!vol) { 135801c135cSArtem B. Bityutskiy err = -ENODEV; 136801c135cSArtem B. Bityutskiy goto out_unlock; 137801c135cSArtem B. Bityutskiy } 138801c135cSArtem B. Bityutskiy 139801c135cSArtem B. Bityutskiy err = -EBUSY; 140801c135cSArtem B. Bityutskiy switch (mode) { 141801c135cSArtem B. Bityutskiy case UBI_READONLY: 142801c135cSArtem B. Bityutskiy if (vol->exclusive) 143801c135cSArtem B. Bityutskiy goto out_unlock; 144801c135cSArtem B. Bityutskiy vol->readers += 1; 145801c135cSArtem B. Bityutskiy break; 146801c135cSArtem B. Bityutskiy 147801c135cSArtem B. Bityutskiy case UBI_READWRITE: 148801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers > 0) 149801c135cSArtem B. Bityutskiy goto out_unlock; 150801c135cSArtem B. Bityutskiy vol->writers += 1; 151801c135cSArtem B. Bityutskiy break; 152801c135cSArtem B. Bityutskiy 153801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 154801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers || vol->readers) 155801c135cSArtem B. Bityutskiy goto out_unlock; 156801c135cSArtem B. Bityutskiy vol->exclusive = 1; 157801c135cSArtem B. Bityutskiy break; 158801c135cSArtem B. Bityutskiy } 159*450f872aSArtem Bityutskiy get_device(&vol->dev); 160801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 161801c135cSArtem B. Bityutskiy 162801c135cSArtem B. Bityutskiy desc->vol = vol; 163801c135cSArtem B. Bityutskiy desc->mode = mode; 164801c135cSArtem B. Bityutskiy 165801c135cSArtem B. Bityutskiy /* 166cae0a771SArtem Bityutskiy * To prevent simultaneous checks of the same volume we use 167cae0a771SArtem Bityutskiy * @volumes_mutex, although it is not the purpose it was introduced 168cae0a771SArtem Bityutskiy * for. 169801c135cSArtem B. Bityutskiy */ 170cae0a771SArtem Bityutskiy mutex_lock(&ubi->volumes_mutex); 171801c135cSArtem B. Bityutskiy if (!vol->checked) { 172801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 173801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 174801c135cSArtem B. Bityutskiy if (err < 0) { 175cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 176801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 177801c135cSArtem B. Bityutskiy return ERR_PTR(err); 178801c135cSArtem B. Bityutskiy } 179801c135cSArtem B. Bityutskiy if (err == 1) { 180801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 181801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 182801c135cSArtem B. Bityutskiy vol->corrupted = 1; 183801c135cSArtem B. Bityutskiy } 184801c135cSArtem B. Bityutskiy vol->checked = 1; 185801c135cSArtem B. Bityutskiy } 186cae0a771SArtem Bityutskiy mutex_unlock(&ubi->volumes_mutex); 187801c135cSArtem B. Bityutskiy return desc; 188801c135cSArtem B. Bityutskiy 189801c135cSArtem B. Bityutskiy out_unlock: 190801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 191801c135cSArtem B. Bityutskiy kfree(desc); 192801c135cSArtem B. Bityutskiy out_put: 193801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 194801c135cSArtem B. Bityutskiy return ERR_PTR(err); 195801c135cSArtem B. Bityutskiy } 196801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 197801c135cSArtem B. Bityutskiy 198801c135cSArtem B. Bityutskiy /** 199801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 200801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 201801c135cSArtem B. Bityutskiy * @name: volume name 202801c135cSArtem B. Bityutskiy * @mode: open mode 203801c135cSArtem B. Bityutskiy * 204801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 205801c135cSArtem B. Bityutskiy */ 206801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 207801c135cSArtem B. Bityutskiy int mode) 208801c135cSArtem B. Bityutskiy { 209801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 210801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ret; 211801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 212801c135cSArtem B. Bityutskiy 213801c135cSArtem B. Bityutskiy dbg_msg("open volume %s, mode %d", name, mode); 214801c135cSArtem B. Bityutskiy 215801c135cSArtem B. Bityutskiy if (!name) 216801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 217801c135cSArtem B. Bityutskiy 218801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 219801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 220801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 221801c135cSArtem B. Bityutskiy 222801c135cSArtem B. Bityutskiy ret = ERR_PTR(-ENODEV); 223801c135cSArtem B. Bityutskiy if (!try_module_get(THIS_MODULE)) 224801c135cSArtem B. Bityutskiy return ret; 225801c135cSArtem B. Bityutskiy 226801c135cSArtem B. Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num]) 227801c135cSArtem B. Bityutskiy goto out_put; 228801c135cSArtem B. Bityutskiy 229801c135cSArtem B. Bityutskiy ubi = ubi_devices[ubi_num]; 230801c135cSArtem B. Bityutskiy 231801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 232801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 233801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 234801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 235801c135cSArtem B. Bityutskiy 236801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 237801c135cSArtem B. Bityutskiy vol_id = i; 238801c135cSArtem B. Bityutskiy break; 239801c135cSArtem B. Bityutskiy } 240801c135cSArtem B. Bityutskiy } 241801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 242801c135cSArtem B. Bityutskiy 243801c135cSArtem B. Bityutskiy if (vol_id < 0) 244801c135cSArtem B. Bityutskiy goto out_put; 245801c135cSArtem B. Bityutskiy 246801c135cSArtem B. Bityutskiy ret = ubi_open_volume(ubi_num, vol_id, mode); 247801c135cSArtem B. Bityutskiy 248801c135cSArtem B. Bityutskiy out_put: 249801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 250801c135cSArtem B. Bityutskiy return ret; 251801c135cSArtem B. Bityutskiy } 252801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 253801c135cSArtem B. Bityutskiy 254801c135cSArtem B. Bityutskiy /** 255801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 256801c135cSArtem B. Bityutskiy * @desc: volume descriptor 257801c135cSArtem B. Bityutskiy */ 258801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 259801c135cSArtem B. Bityutskiy { 260801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 261801c135cSArtem B. Bityutskiy 262801c135cSArtem B. Bityutskiy dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); 263801c135cSArtem B. Bityutskiy 264801c135cSArtem B. Bityutskiy spin_lock(&vol->ubi->volumes_lock); 265801c135cSArtem B. Bityutskiy switch (desc->mode) { 266801c135cSArtem B. Bityutskiy case UBI_READONLY: 267801c135cSArtem B. Bityutskiy vol->readers -= 1; 268801c135cSArtem B. Bityutskiy break; 269801c135cSArtem B. Bityutskiy case UBI_READWRITE: 270801c135cSArtem B. Bityutskiy vol->writers -= 1; 271801c135cSArtem B. Bityutskiy break; 272801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 273801c135cSArtem B. Bityutskiy vol->exclusive = 0; 274801c135cSArtem B. Bityutskiy } 275801c135cSArtem B. Bityutskiy spin_unlock(&vol->ubi->volumes_lock); 276801c135cSArtem B. Bityutskiy 277801c135cSArtem B. Bityutskiy kfree(desc); 278*450f872aSArtem Bityutskiy put_device(&vol->dev); 279801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 280801c135cSArtem B. Bityutskiy } 281801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 282801c135cSArtem B. Bityutskiy 283801c135cSArtem B. Bityutskiy /** 284801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 285801c135cSArtem B. Bityutskiy * @desc: volume descriptor 286801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 287801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 288801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 289801c135cSArtem B. Bityutskiy * @len: how many bytes to read 290801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 291801c135cSArtem B. Bityutskiy * 292801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 293801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 294801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 295801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 296801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 297801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 298801c135cSArtem B. Bityutskiy * 299801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 300801c135cSArtem B. Bityutskiy * function returns a negative error code. 301801c135cSArtem B. Bityutskiy * 302801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 303801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 304801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 305801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 306801c135cSArtem B. Bityutskiy * 307801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 308801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF error code. 309801c135cSArtem B. Bityutskiy */ 310801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 311801c135cSArtem B. Bityutskiy int len, int check) 312801c135cSArtem B. Bityutskiy { 313801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 314801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 315801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 316801c135cSArtem B. Bityutskiy 317801c135cSArtem B. Bityutskiy dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 318801c135cSArtem B. Bityutskiy 319801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 320801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 321801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 322801c135cSArtem B. Bityutskiy return -EINVAL; 323801c135cSArtem B. Bityutskiy 3244ab60a0dSArtem Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME) { 3254ab60a0dSArtem Bityutskiy if (vol->used_ebs == 0) 3264ab60a0dSArtem Bityutskiy /* Empty static UBI volume */ 3274ab60a0dSArtem Bityutskiy return 0; 3284ab60a0dSArtem Bityutskiy if (lnum == vol->used_ebs - 1 && 329801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 330801c135cSArtem B. Bityutskiy return -EINVAL; 3314ab60a0dSArtem Bityutskiy } 332801c135cSArtem B. Bityutskiy 333801c135cSArtem B. Bityutskiy if (vol->upd_marker) 334801c135cSArtem B. Bityutskiy return -EBADF; 335801c135cSArtem B. Bityutskiy if (len == 0) 336801c135cSArtem B. Bityutskiy return 0; 337801c135cSArtem B. Bityutskiy 33889b96b69SArtem Bityutskiy err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); 339801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 340801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 341801c135cSArtem B. Bityutskiy vol->corrupted = 1; 342801c135cSArtem B. Bityutskiy } 343801c135cSArtem B. Bityutskiy 344801c135cSArtem B. Bityutskiy return err; 345801c135cSArtem B. Bityutskiy } 346801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 347801c135cSArtem B. Bityutskiy 348801c135cSArtem B. Bityutskiy /** 349801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 350801c135cSArtem B. Bityutskiy * @desc: volume descriptor 351801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 352801c135cSArtem B. Bityutskiy * @buf: data to write 353801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 354801c135cSArtem B. Bityutskiy * @len: how many bytes to write 355801c135cSArtem B. Bityutskiy * @dtype: expected data type 356801c135cSArtem B. Bityutskiy * 357801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 358801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 359801c135cSArtem B. Bityutskiy * the data. 360801c135cSArtem B. Bityutskiy * 361801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 362801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 363801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 364801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 365801c135cSArtem B. Bityutskiy * 366801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 367801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 368801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 369801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 370801c135cSArtem B. Bityutskiy * garbage. 371801c135cSArtem B. Bityutskiy * 372801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 373801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 374801c135cSArtem B. Bityutskiy */ 375801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 376801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 377801c135cSArtem B. Bityutskiy { 378801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 379801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 380801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 381801c135cSArtem B. Bityutskiy 382801c135cSArtem B. Bityutskiy dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 383801c135cSArtem B. Bityutskiy 384801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 385801c135cSArtem B. Bityutskiy return -EINVAL; 386801c135cSArtem B. Bityutskiy 387801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 388801c135cSArtem B. Bityutskiy return -EROFS; 389801c135cSArtem B. Bityutskiy 390801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 391801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size || offset % ubi->min_io_size || 392801c135cSArtem B. Bityutskiy len % ubi->min_io_size) 393801c135cSArtem B. Bityutskiy return -EINVAL; 394801c135cSArtem B. Bityutskiy 395801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 396801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 397801c135cSArtem B. Bityutskiy return -EINVAL; 398801c135cSArtem B. Bityutskiy 399801c135cSArtem B. Bityutskiy if (vol->upd_marker) 400801c135cSArtem B. Bityutskiy return -EBADF; 401801c135cSArtem B. Bityutskiy 402801c135cSArtem B. Bityutskiy if (len == 0) 403801c135cSArtem B. Bityutskiy return 0; 404801c135cSArtem B. Bityutskiy 40589b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 406801c135cSArtem B. Bityutskiy } 407801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 408801c135cSArtem B. Bityutskiy 409801c135cSArtem B. Bityutskiy /* 410801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 411801c135cSArtem B. Bityutskiy * @desc: volume descriptor 412801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 413801c135cSArtem B. Bityutskiy * @buf: data to write 414801c135cSArtem B. Bityutskiy * @len: how many bytes to write 415801c135cSArtem B. Bityutskiy * @dtype: expected data type 416801c135cSArtem B. Bityutskiy * 417801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 418801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 419801c135cSArtem B. Bityutskiy * data, which has to be aligned. The length may be shorter then the logical 420801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 421801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 422801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 423801c135cSArtem B. Bityutskiy * code in case of failure. 424801c135cSArtem B. Bityutskiy */ 425801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 426801c135cSArtem B. Bityutskiy int len, int dtype) 427801c135cSArtem B. Bityutskiy { 428801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 429801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 430801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 431801c135cSArtem B. Bityutskiy 432801c135cSArtem B. Bityutskiy dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 433801c135cSArtem B. Bityutskiy 434801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 435801c135cSArtem B. Bityutskiy return -EINVAL; 436801c135cSArtem B. Bityutskiy 437801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 438801c135cSArtem B. Bityutskiy return -EROFS; 439801c135cSArtem B. Bityutskiy 440801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 441801c135cSArtem B. Bityutskiy len > vol->usable_leb_size || len % ubi->min_io_size) 442801c135cSArtem B. Bityutskiy return -EINVAL; 443801c135cSArtem B. Bityutskiy 444801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 445801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 446801c135cSArtem B. Bityutskiy return -EINVAL; 447801c135cSArtem B. Bityutskiy 448801c135cSArtem B. Bityutskiy if (vol->upd_marker) 449801c135cSArtem B. Bityutskiy return -EBADF; 450801c135cSArtem B. Bityutskiy 451801c135cSArtem B. Bityutskiy if (len == 0) 452801c135cSArtem B. Bityutskiy return 0; 453801c135cSArtem B. Bityutskiy 45489b96b69SArtem Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 455801c135cSArtem B. Bityutskiy } 456801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 457801c135cSArtem B. Bityutskiy 458801c135cSArtem B. Bityutskiy /** 459801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 460801c135cSArtem B. Bityutskiy * @desc: volume descriptor 461801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 462801c135cSArtem B. Bityutskiy * 463801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 464801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 465801c135cSArtem B. Bityutskiy * negative error code in case of failure. 466801c135cSArtem B. Bityutskiy * 467801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 468801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 469801c135cSArtem B. Bityutskiy */ 470801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 471801c135cSArtem B. Bityutskiy { 472801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 473801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 474801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 475801c135cSArtem B. Bityutskiy 476801c135cSArtem B. Bityutskiy dbg_msg("erase LEB %d:%d", vol_id, lnum); 477801c135cSArtem B. Bityutskiy 478801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 479801c135cSArtem B. Bityutskiy return -EROFS; 480801c135cSArtem B. Bityutskiy 481801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 482801c135cSArtem B. Bityutskiy return -EINVAL; 483801c135cSArtem B. Bityutskiy 484801c135cSArtem B. Bityutskiy if (vol->upd_marker) 485801c135cSArtem B. Bityutskiy return -EBADF; 486801c135cSArtem B. Bityutskiy 48789b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 488801c135cSArtem B. Bityutskiy if (err) 489801c135cSArtem B. Bityutskiy return err; 490801c135cSArtem B. Bityutskiy 491801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 492801c135cSArtem B. Bityutskiy } 493801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 494801c135cSArtem B. Bityutskiy 495801c135cSArtem B. Bityutskiy /** 496801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 497801c135cSArtem B. Bityutskiy * @desc: volume descriptor 498801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 499801c135cSArtem B. Bityutskiy * 500801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 501801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 502801c135cSArtem B. Bityutskiy * physically erased in background. This operation is much faster then the 503801c135cSArtem B. Bityutskiy * erase operation. 504801c135cSArtem B. Bityutskiy * 505801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 506801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 507801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 508801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 509801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 510801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 511801c135cSArtem B. Bityutskiy * used with care. 512801c135cSArtem B. Bityutskiy * 513801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 514801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 515801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 516801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 517801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 518801c135cSArtem B. Bityutskiy * 519801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 520801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 521801c135cSArtem B. Bityutskiy * first un-map it, then write new data, rather then first erase it, then write 522801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 523801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 524801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 525801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 526801c135cSArtem B. Bityutskiy * 527801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 528801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 529801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 530801c135cSArtem B. Bityutskiy */ 531801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 532801c135cSArtem B. Bityutskiy { 533801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 534801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 535801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 536801c135cSArtem B. Bityutskiy 537801c135cSArtem B. Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 538801c135cSArtem B. Bityutskiy 539801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 540801c135cSArtem B. Bityutskiy return -EROFS; 541801c135cSArtem B. Bityutskiy 542801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 543801c135cSArtem B. Bityutskiy return -EINVAL; 544801c135cSArtem B. Bityutskiy 545801c135cSArtem B. Bityutskiy if (vol->upd_marker) 546801c135cSArtem B. Bityutskiy return -EBADF; 547801c135cSArtem B. Bityutskiy 54889b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 549801c135cSArtem B. Bityutskiy } 550801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 551801c135cSArtem B. Bityutskiy 552801c135cSArtem B. Bityutskiy /** 553393852ecSArtem Bityutskiy * ubi_leb_map - map logical erasblock to a physical eraseblock. 554393852ecSArtem Bityutskiy * @desc: volume descriptor 555393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 556393852ecSArtem Bityutskiy * @dtype: expected data type 557393852ecSArtem Bityutskiy * 558393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 559393852ecSArtem Bityutskiy * eraseblock. This means, that after a successfull invocation of this 560393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 561393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 562393852ecSArtem Bityutskiy * happens. 563393852ecSArtem Bityutskiy * 564393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 565393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 566393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 567393852ecSArtem Bityutskiy * other failures. 568393852ecSArtem Bityutskiy */ 569393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 570393852ecSArtem Bityutskiy { 571393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 572393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 573393852ecSArtem Bityutskiy int vol_id = vol->vol_id; 574393852ecSArtem Bityutskiy 575393852ecSArtem Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 576393852ecSArtem Bityutskiy 577393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 578393852ecSArtem Bityutskiy return -EROFS; 579393852ecSArtem Bityutskiy 580393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 581393852ecSArtem Bityutskiy return -EINVAL; 582393852ecSArtem Bityutskiy 583393852ecSArtem Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 584393852ecSArtem Bityutskiy dtype != UBI_UNKNOWN) 585393852ecSArtem Bityutskiy return -EINVAL; 586393852ecSArtem Bityutskiy 587393852ecSArtem Bityutskiy if (vol->upd_marker) 588393852ecSArtem Bityutskiy return -EBADF; 589393852ecSArtem Bityutskiy 590393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 591393852ecSArtem Bityutskiy return -EBADMSG; 592393852ecSArtem Bityutskiy 59389b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 594393852ecSArtem Bityutskiy } 595393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 596393852ecSArtem Bityutskiy 597393852ecSArtem Bityutskiy /** 598801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 599801c135cSArtem B. Bityutskiy * @desc: volume descriptor 600801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 601801c135cSArtem B. Bityutskiy * 602801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 603801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 604801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 605801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 606801c135cSArtem B. Bityutskiy * mapped to. 607801c135cSArtem B. Bityutskiy * 608801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 609801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 610801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 611801c135cSArtem B. Bityutskiy * code. 612801c135cSArtem B. Bityutskiy */ 613801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 614801c135cSArtem B. Bityutskiy { 615801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 616801c135cSArtem B. Bityutskiy 617801c135cSArtem B. Bityutskiy dbg_msg("test LEB %d:%d", vol->vol_id, lnum); 618801c135cSArtem B. Bityutskiy 619801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 620801c135cSArtem B. Bityutskiy return -EINVAL; 621801c135cSArtem B. Bityutskiy 622801c135cSArtem B. Bityutskiy if (vol->upd_marker) 623801c135cSArtem B. Bityutskiy return -EBADF; 624801c135cSArtem B. Bityutskiy 625801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 626801c135cSArtem B. Bityutskiy } 627801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 628