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 * 33e73f4459SArtem Bityutskiy * This function returns %0 in case of success, %-EINVAL if the UBI device 34e73f4459SArtem 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 { 38e73f4459SArtem Bityutskiy struct ubi_device *ubi; 39801c135cSArtem B. Bityutskiy 40e73f4459SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 41e73f4459SArtem Bityutskiy return -EINVAL; 42e73f4459SArtem Bityutskiy 43e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 44e73f4459SArtem 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; 52e73f4459SArtem Bityutskiy 53e73f4459SArtem 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 118e73f4459SArtem Bityutskiy /* 119e73f4459SArtem Bityutskiy * First of all, we have to get the UBI device to prevent its removal. 120e73f4459SArtem Bityutskiy */ 121e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 12235ad5fb7SArtem Bityutskiy if (!ubi) 12335ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 12435ad5fb7SArtem Bityutskiy 125e73f4459SArtem Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) { 126e73f4459SArtem Bityutskiy err = -EINVAL; 127e73f4459SArtem Bityutskiy goto out_put_ubi; 128e73f4459SArtem Bityutskiy } 129801c135cSArtem B. Bityutskiy 130801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 131e73f4459SArtem Bityutskiy if (!desc) { 132e73f4459SArtem Bityutskiy err = -ENOMEM; 133e73f4459SArtem Bityutskiy goto out_put_ubi; 134e73f4459SArtem 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 172783b273aSArtem Bityutskiy mutex_lock(&ubi->ckvol_mutex); 173801c135cSArtem B. Bityutskiy if (!vol->checked) { 174801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 175801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 176801c135cSArtem B. Bityutskiy if (err < 0) { 177783b273aSArtem Bityutskiy mutex_unlock(&ubi->ckvol_mutex); 178801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 179801c135cSArtem B. Bityutskiy return ERR_PTR(err); 180801c135cSArtem B. Bityutskiy } 181801c135cSArtem B. Bityutskiy if (err == 1) { 182801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 183801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 184801c135cSArtem B. Bityutskiy vol->corrupted = 1; 185801c135cSArtem B. Bityutskiy } 186801c135cSArtem B. Bityutskiy vol->checked = 1; 187801c135cSArtem B. Bityutskiy } 188783b273aSArtem Bityutskiy mutex_unlock(&ubi->ckvol_mutex); 18935ad5fb7SArtem Bityutskiy 190801c135cSArtem B. Bityutskiy return desc; 191801c135cSArtem B. Bityutskiy 192801c135cSArtem B. Bityutskiy out_unlock: 193801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 194801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 19535ad5fb7SArtem Bityutskiy out_free: 19635ad5fb7SArtem Bityutskiy kfree(desc); 197e73f4459SArtem Bityutskiy out_put_ubi: 198e73f4459SArtem Bityutskiy ubi_put_device(ubi); 199801c135cSArtem B. Bityutskiy return ERR_PTR(err); 200801c135cSArtem B. Bityutskiy } 201801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 202801c135cSArtem B. Bityutskiy 203801c135cSArtem B. Bityutskiy /** 204801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 205801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 206801c135cSArtem B. Bityutskiy * @name: volume name 207801c135cSArtem B. Bityutskiy * @mode: open mode 208801c135cSArtem B. Bityutskiy * 209801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 210801c135cSArtem B. Bityutskiy */ 211801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 212801c135cSArtem B. Bityutskiy int mode) 213801c135cSArtem B. Bityutskiy { 214801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 215801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 216e73f4459SArtem Bityutskiy struct ubi_volume_desc *ret; 217801c135cSArtem B. Bityutskiy 218801c135cSArtem B. Bityutskiy dbg_msg("open volume %s, mode %d", name, mode); 219801c135cSArtem B. Bityutskiy 220801c135cSArtem B. Bityutskiy if (!name) 221801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 222801c135cSArtem B. Bityutskiy 223801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 224801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 225801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 226801c135cSArtem B. Bityutskiy 22735ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 22835ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 229801c135cSArtem B. Bityutskiy 230e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 23135ad5fb7SArtem Bityutskiy if (!ubi) 23235ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 233801c135cSArtem B. Bityutskiy 234801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 235801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 236801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 237801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 238801c135cSArtem B. Bityutskiy 239801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 240801c135cSArtem B. Bityutskiy vol_id = i; 241801c135cSArtem B. Bityutskiy break; 242801c135cSArtem B. Bityutskiy } 243801c135cSArtem B. Bityutskiy } 244801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 245801c135cSArtem B. Bityutskiy 246e73f4459SArtem Bityutskiy if (vol_id >= 0) 247e73f4459SArtem Bityutskiy ret = ubi_open_volume(ubi_num, vol_id, mode); 248e73f4459SArtem Bityutskiy else 249e73f4459SArtem Bityutskiy ret = ERR_PTR(-ENODEV); 250801c135cSArtem B. Bityutskiy 251e73f4459SArtem Bityutskiy /* 252e73f4459SArtem Bityutskiy * We should put the UBI device even in case of success, because 253e73f4459SArtem Bityutskiy * 'ubi_open_volume()' took a reference as well. 254e73f4459SArtem Bityutskiy */ 255e73f4459SArtem Bityutskiy ubi_put_device(ubi); 256e73f4459SArtem Bityutskiy return ret; 257801c135cSArtem B. Bityutskiy } 258801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 259801c135cSArtem B. Bityutskiy 260801c135cSArtem B. Bityutskiy /** 261801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 262801c135cSArtem B. Bityutskiy * @desc: volume descriptor 263801c135cSArtem B. Bityutskiy */ 264801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 265801c135cSArtem B. Bityutskiy { 266801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 267e73f4459SArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 268801c135cSArtem B. Bityutskiy 269801c135cSArtem B. Bityutskiy dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); 270801c135cSArtem B. Bityutskiy 271e73f4459SArtem Bityutskiy spin_lock(&ubi->volumes_lock); 272801c135cSArtem B. Bityutskiy switch (desc->mode) { 273801c135cSArtem B. Bityutskiy case UBI_READONLY: 274801c135cSArtem B. Bityutskiy vol->readers -= 1; 275801c135cSArtem B. Bityutskiy break; 276801c135cSArtem B. Bityutskiy case UBI_READWRITE: 277801c135cSArtem B. Bityutskiy vol->writers -= 1; 278801c135cSArtem B. Bityutskiy break; 279801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 280801c135cSArtem B. Bityutskiy vol->exclusive = 0; 281801c135cSArtem B. Bityutskiy } 282d05c77a8SArtem Bityutskiy vol->ref_count -= 1; 283e73f4459SArtem Bityutskiy spin_unlock(&ubi->volumes_lock); 284801c135cSArtem B. Bityutskiy 285d05c77a8SArtem Bityutskiy kfree(desc); 286e73f4459SArtem Bityutskiy put_device(&vol->dev); 287e73f4459SArtem Bityutskiy ubi_put_device(ubi); 288801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 289801c135cSArtem B. Bityutskiy } 290801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 291801c135cSArtem B. Bityutskiy 292801c135cSArtem B. Bityutskiy /** 293801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 294801c135cSArtem B. Bityutskiy * @desc: volume descriptor 295801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 296801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 297801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 298801c135cSArtem B. Bityutskiy * @len: how many bytes to read 299801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 300801c135cSArtem B. Bityutskiy * 301801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 302801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 303801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 304801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 305801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 306801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 307801c135cSArtem B. Bityutskiy * 308801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 309801c135cSArtem B. Bityutskiy * function returns a negative error code. 310801c135cSArtem B. Bityutskiy * 311801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 312801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 313801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 314801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 315801c135cSArtem B. Bityutskiy * 316801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 317801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF error code. 318801c135cSArtem B. Bityutskiy */ 319801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 320801c135cSArtem B. Bityutskiy int len, int check) 321801c135cSArtem B. Bityutskiy { 322801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 323801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 324801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 325801c135cSArtem B. Bityutskiy 326801c135cSArtem B. Bityutskiy dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 327801c135cSArtem B. Bityutskiy 328801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 329801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 330801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 331801c135cSArtem B. Bityutskiy return -EINVAL; 332801c135cSArtem B. Bityutskiy 3334ab60a0dSArtem Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME) { 3344ab60a0dSArtem Bityutskiy if (vol->used_ebs == 0) 3354ab60a0dSArtem Bityutskiy /* Empty static UBI volume */ 3364ab60a0dSArtem Bityutskiy return 0; 3374ab60a0dSArtem Bityutskiy if (lnum == vol->used_ebs - 1 && 338801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 339801c135cSArtem B. Bityutskiy return -EINVAL; 3404ab60a0dSArtem Bityutskiy } 341801c135cSArtem B. Bityutskiy 342801c135cSArtem B. Bityutskiy if (vol->upd_marker) 343801c135cSArtem B. Bityutskiy return -EBADF; 344801c135cSArtem B. Bityutskiy if (len == 0) 345801c135cSArtem B. Bityutskiy return 0; 346801c135cSArtem B. Bityutskiy 34789b96b69SArtem Bityutskiy err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); 348801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 349801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 350801c135cSArtem B. Bityutskiy vol->corrupted = 1; 351801c135cSArtem B. Bityutskiy } 352801c135cSArtem B. Bityutskiy 353801c135cSArtem B. Bityutskiy return err; 354801c135cSArtem B. Bityutskiy } 355801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 356801c135cSArtem B. Bityutskiy 357801c135cSArtem B. Bityutskiy /** 358801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 359801c135cSArtem B. Bityutskiy * @desc: volume descriptor 360801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 361801c135cSArtem B. Bityutskiy * @buf: data to write 362801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 363801c135cSArtem B. Bityutskiy * @len: how many bytes to write 364801c135cSArtem B. Bityutskiy * @dtype: expected data type 365801c135cSArtem B. Bityutskiy * 366801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 367801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 368801c135cSArtem B. Bityutskiy * the data. 369801c135cSArtem B. Bityutskiy * 370801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 371801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 372801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 373801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 374801c135cSArtem B. Bityutskiy * 375801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 376801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 377801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 378801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 379801c135cSArtem B. Bityutskiy * garbage. 380801c135cSArtem B. Bityutskiy * 381801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 382801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 383801c135cSArtem B. Bityutskiy */ 384801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 385801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 386801c135cSArtem B. Bityutskiy { 387801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 388801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 389801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 390801c135cSArtem B. Bityutskiy 391801c135cSArtem B. Bityutskiy dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 392801c135cSArtem B. Bityutskiy 393801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 394801c135cSArtem B. Bityutskiy return -EINVAL; 395801c135cSArtem B. Bityutskiy 396801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 397801c135cSArtem B. Bityutskiy return -EROFS; 398801c135cSArtem B. Bityutskiy 399801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 400*cadb40ccSKyungmin Park offset + len > vol->usable_leb_size || 401*cadb40ccSKyungmin Park offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) 402801c135cSArtem B. Bityutskiy return -EINVAL; 403801c135cSArtem B. Bityutskiy 404801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 405801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 406801c135cSArtem B. Bityutskiy return -EINVAL; 407801c135cSArtem B. Bityutskiy 408801c135cSArtem B. Bityutskiy if (vol->upd_marker) 409801c135cSArtem B. Bityutskiy return -EBADF; 410801c135cSArtem B. Bityutskiy 411801c135cSArtem B. Bityutskiy if (len == 0) 412801c135cSArtem B. Bityutskiy return 0; 413801c135cSArtem B. Bityutskiy 41489b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 415801c135cSArtem B. Bityutskiy } 416801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 417801c135cSArtem B. Bityutskiy 418801c135cSArtem B. Bityutskiy /* 419801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 420801c135cSArtem B. Bityutskiy * @desc: volume descriptor 421801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 422801c135cSArtem B. Bityutskiy * @buf: data to write 423801c135cSArtem B. Bityutskiy * @len: how many bytes to write 424801c135cSArtem B. Bityutskiy * @dtype: expected data type 425801c135cSArtem B. Bityutskiy * 426801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 427801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 428801c135cSArtem B. Bityutskiy * data, which has to be aligned. The length may be shorter then the logical 429801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 430801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 431801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 432801c135cSArtem B. Bityutskiy * code in case of failure. 433801c135cSArtem B. Bityutskiy */ 434801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 435801c135cSArtem B. Bityutskiy int len, int dtype) 436801c135cSArtem B. Bityutskiy { 437801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 438801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 439801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 440801c135cSArtem B. Bityutskiy 441801c135cSArtem B. Bityutskiy dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 442801c135cSArtem B. Bityutskiy 443801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 444801c135cSArtem B. Bityutskiy return -EINVAL; 445801c135cSArtem B. Bityutskiy 446801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 447801c135cSArtem B. Bityutskiy return -EROFS; 448801c135cSArtem B. Bityutskiy 449801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 450*cadb40ccSKyungmin Park len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) 451801c135cSArtem B. Bityutskiy return -EINVAL; 452801c135cSArtem B. Bityutskiy 453801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 454801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 455801c135cSArtem B. Bityutskiy return -EINVAL; 456801c135cSArtem B. Bityutskiy 457801c135cSArtem B. Bityutskiy if (vol->upd_marker) 458801c135cSArtem B. Bityutskiy return -EBADF; 459801c135cSArtem B. Bityutskiy 460801c135cSArtem B. Bityutskiy if (len == 0) 461801c135cSArtem B. Bityutskiy return 0; 462801c135cSArtem B. Bityutskiy 46389b96b69SArtem Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 464801c135cSArtem B. Bityutskiy } 465801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 466801c135cSArtem B. Bityutskiy 467801c135cSArtem B. Bityutskiy /** 468801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 469801c135cSArtem B. Bityutskiy * @desc: volume descriptor 470801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 471801c135cSArtem B. Bityutskiy * 472801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 473801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 474801c135cSArtem B. Bityutskiy * negative error code in case of failure. 475801c135cSArtem B. Bityutskiy * 476801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 477801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 478801c135cSArtem B. Bityutskiy */ 479801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 480801c135cSArtem B. Bityutskiy { 481801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 482801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 483ae616e1bSArtem Bityutskiy int err; 484801c135cSArtem B. Bityutskiy 485ae616e1bSArtem Bityutskiy dbg_msg("erase LEB %d:%d", vol->vol_id, lnum); 486801c135cSArtem B. Bityutskiy 487801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 488801c135cSArtem B. Bityutskiy return -EROFS; 489801c135cSArtem B. Bityutskiy 490801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 491801c135cSArtem B. Bityutskiy return -EINVAL; 492801c135cSArtem B. Bityutskiy 493801c135cSArtem B. Bityutskiy if (vol->upd_marker) 494801c135cSArtem B. Bityutskiy return -EBADF; 495801c135cSArtem B. Bityutskiy 49689b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 497801c135cSArtem B. Bityutskiy if (err) 498801c135cSArtem B. Bityutskiy return err; 499801c135cSArtem B. Bityutskiy 500801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 501801c135cSArtem B. Bityutskiy } 502801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 503801c135cSArtem B. Bityutskiy 504801c135cSArtem B. Bityutskiy /** 505801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 506801c135cSArtem B. Bityutskiy * @desc: volume descriptor 507801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 508801c135cSArtem B. Bityutskiy * 509801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 510801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 511801c135cSArtem B. Bityutskiy * physically erased in background. This operation is much faster then the 512801c135cSArtem B. Bityutskiy * erase operation. 513801c135cSArtem B. Bityutskiy * 514801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 515801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 516801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 517801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 518801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 519801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 520801c135cSArtem B. Bityutskiy * used with care. 521801c135cSArtem B. Bityutskiy * 522801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 523801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 524801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 525801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 526801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 527801c135cSArtem B. Bityutskiy * 528801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 529801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 530801c135cSArtem B. Bityutskiy * first un-map it, then write new data, rather then first erase it, then write 531801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 532801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 533801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 534801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 535801c135cSArtem B. Bityutskiy * 536801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 537801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 538801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 539801c135cSArtem B. Bityutskiy */ 540801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 541801c135cSArtem B. Bityutskiy { 542801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 543801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 544801c135cSArtem B. Bityutskiy 545ae616e1bSArtem Bityutskiy dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum); 546801c135cSArtem B. Bityutskiy 547801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 548801c135cSArtem B. Bityutskiy return -EROFS; 549801c135cSArtem B. Bityutskiy 550801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 551801c135cSArtem B. Bityutskiy return -EINVAL; 552801c135cSArtem B. Bityutskiy 553801c135cSArtem B. Bityutskiy if (vol->upd_marker) 554801c135cSArtem B. Bityutskiy return -EBADF; 555801c135cSArtem B. Bityutskiy 55689b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 557801c135cSArtem B. Bityutskiy } 558801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 559801c135cSArtem B. Bityutskiy 560801c135cSArtem B. Bityutskiy /** 561393852ecSArtem Bityutskiy * ubi_leb_map - map logical erasblock to a physical eraseblock. 562393852ecSArtem Bityutskiy * @desc: volume descriptor 563393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 564393852ecSArtem Bityutskiy * @dtype: expected data type 565393852ecSArtem Bityutskiy * 566393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 567393852ecSArtem Bityutskiy * eraseblock. This means, that after a successfull invocation of this 568393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 569393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 570393852ecSArtem Bityutskiy * happens. 571393852ecSArtem Bityutskiy * 572393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 573393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 574393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 575393852ecSArtem Bityutskiy * other failures. 576393852ecSArtem Bityutskiy */ 577393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 578393852ecSArtem Bityutskiy { 579393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 580393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 581393852ecSArtem Bityutskiy 582ae616e1bSArtem Bityutskiy dbg_msg("unmap LEB %d:%d", vol->vol_id, lnum); 583393852ecSArtem Bityutskiy 584393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 585393852ecSArtem Bityutskiy return -EROFS; 586393852ecSArtem Bityutskiy 587393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 588393852ecSArtem Bityutskiy return -EINVAL; 589393852ecSArtem Bityutskiy 590393852ecSArtem Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 591393852ecSArtem Bityutskiy dtype != UBI_UNKNOWN) 592393852ecSArtem Bityutskiy return -EINVAL; 593393852ecSArtem Bityutskiy 594393852ecSArtem Bityutskiy if (vol->upd_marker) 595393852ecSArtem Bityutskiy return -EBADF; 596393852ecSArtem Bityutskiy 597393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 598393852ecSArtem Bityutskiy return -EBADMSG; 599393852ecSArtem Bityutskiy 60089b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 601393852ecSArtem Bityutskiy } 602393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 603393852ecSArtem Bityutskiy 604393852ecSArtem Bityutskiy /** 605801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 606801c135cSArtem B. Bityutskiy * @desc: volume descriptor 607801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 608801c135cSArtem B. Bityutskiy * 609801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 610801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 611801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 612801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 613801c135cSArtem B. Bityutskiy * mapped to. 614801c135cSArtem B. Bityutskiy * 615801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 616801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 617801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 618801c135cSArtem B. Bityutskiy * code. 619801c135cSArtem B. Bityutskiy */ 620801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 621801c135cSArtem B. Bityutskiy { 622801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 623801c135cSArtem B. Bityutskiy 624801c135cSArtem B. Bityutskiy dbg_msg("test LEB %d:%d", vol->vol_id, lnum); 625801c135cSArtem B. Bityutskiy 626801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 627801c135cSArtem B. Bityutskiy return -EINVAL; 628801c135cSArtem B. Bityutskiy 629801c135cSArtem B. Bityutskiy if (vol->upd_marker) 630801c135cSArtem B. Bityutskiy return -EBADF; 631801c135cSArtem B. Bityutskiy 632801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 633801c135cSArtem B. Bityutskiy } 634801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 635