1*801c135cSArtem B. Bityutskiy /* 2*801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006 3*801c135cSArtem B. Bityutskiy * 4*801c135cSArtem B. Bityutskiy * This program is free software; you can redistribute it and/or modify 5*801c135cSArtem B. Bityutskiy * it under the terms of the GNU General Public License as published by 6*801c135cSArtem B. Bityutskiy * the Free Software Foundation; either version 2 of the License, or 7*801c135cSArtem B. Bityutskiy * (at your option) any later version. 8*801c135cSArtem B. Bityutskiy * 9*801c135cSArtem B. Bityutskiy * This program is distributed in the hope that it will be useful, 10*801c135cSArtem B. Bityutskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*801c135cSArtem B. Bityutskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12*801c135cSArtem B. Bityutskiy * the GNU General Public License for more details. 13*801c135cSArtem B. Bityutskiy * 14*801c135cSArtem B. Bityutskiy * You should have received a copy of the GNU General Public License 15*801c135cSArtem B. Bityutskiy * along with this program; if not, write to the Free Software 16*801c135cSArtem B. Bityutskiy * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17*801c135cSArtem B. Bityutskiy * 18*801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём) 19*801c135cSArtem B. Bityutskiy */ 20*801c135cSArtem B. Bityutskiy 21*801c135cSArtem B. Bityutskiy /* This file mostly implements UBI kernel API functions */ 22*801c135cSArtem B. Bityutskiy 23*801c135cSArtem B. Bityutskiy #include <linux/module.h> 24*801c135cSArtem B. Bityutskiy #include <linux/err.h> 25*801c135cSArtem B. Bityutskiy #include <asm/div64.h> 26*801c135cSArtem B. Bityutskiy #include "ubi.h" 27*801c135cSArtem B. Bityutskiy 28*801c135cSArtem B. Bityutskiy /** 29*801c135cSArtem B. Bityutskiy * ubi_get_device_info - get information about UBI device. 30*801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 31*801c135cSArtem B. Bityutskiy * @di: the information is stored here 32*801c135cSArtem B. Bityutskiy * 33*801c135cSArtem B. Bityutskiy * This function returns %0 in case of success and a %-ENODEV if there is no 34*801c135cSArtem B. Bityutskiy * such UBI device. 35*801c135cSArtem B. Bityutskiy */ 36*801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) 37*801c135cSArtem B. Bityutskiy { 38*801c135cSArtem B. Bityutskiy const struct ubi_device *ubi; 39*801c135cSArtem B. Bityutskiy 40*801c135cSArtem B. Bityutskiy if (!try_module_get(THIS_MODULE)) 41*801c135cSArtem B. Bityutskiy return -ENODEV; 42*801c135cSArtem B. Bityutskiy 43*801c135cSArtem B. Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || 44*801c135cSArtem B. Bityutskiy !ubi_devices[ubi_num]) { 45*801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 46*801c135cSArtem B. Bityutskiy return -ENODEV; 47*801c135cSArtem B. Bityutskiy } 48*801c135cSArtem B. Bityutskiy 49*801c135cSArtem B. Bityutskiy ubi = ubi_devices[ubi_num]; 50*801c135cSArtem B. Bityutskiy di->ubi_num = ubi->ubi_num; 51*801c135cSArtem B. Bityutskiy di->leb_size = ubi->leb_size; 52*801c135cSArtem B. Bityutskiy di->min_io_size = ubi->min_io_size; 53*801c135cSArtem B. Bityutskiy di->ro_mode = ubi->ro_mode; 54*801c135cSArtem B. Bityutskiy di->cdev = MKDEV(ubi->major, 0); 55*801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 56*801c135cSArtem B. Bityutskiy return 0; 57*801c135cSArtem B. Bityutskiy } 58*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info); 59*801c135cSArtem B. Bityutskiy 60*801c135cSArtem B. Bityutskiy /** 61*801c135cSArtem B. Bityutskiy * ubi_get_volume_info - get information about UBI volume. 62*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 63*801c135cSArtem B. Bityutskiy * @vi: the information is stored here 64*801c135cSArtem B. Bityutskiy */ 65*801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc, 66*801c135cSArtem B. Bityutskiy struct ubi_volume_info *vi) 67*801c135cSArtem B. Bityutskiy { 68*801c135cSArtem B. Bityutskiy const struct ubi_volume *vol = desc->vol; 69*801c135cSArtem B. Bityutskiy const struct ubi_device *ubi = vol->ubi; 70*801c135cSArtem B. Bityutskiy 71*801c135cSArtem B. Bityutskiy vi->vol_id = vol->vol_id; 72*801c135cSArtem B. Bityutskiy vi->ubi_num = ubi->ubi_num; 73*801c135cSArtem B. Bityutskiy vi->size = vol->reserved_pebs; 74*801c135cSArtem B. Bityutskiy vi->used_bytes = vol->used_bytes; 75*801c135cSArtem B. Bityutskiy vi->vol_type = vol->vol_type; 76*801c135cSArtem B. Bityutskiy vi->corrupted = vol->corrupted; 77*801c135cSArtem B. Bityutskiy vi->upd_marker = vol->upd_marker; 78*801c135cSArtem B. Bityutskiy vi->alignment = vol->alignment; 79*801c135cSArtem B. Bityutskiy vi->usable_leb_size = vol->usable_leb_size; 80*801c135cSArtem B. Bityutskiy vi->name_len = vol->name_len; 81*801c135cSArtem B. Bityutskiy vi->name = vol->name; 82*801c135cSArtem B. Bityutskiy vi->cdev = MKDEV(ubi->major, vi->vol_id + 1); 83*801c135cSArtem B. Bityutskiy } 84*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info); 85*801c135cSArtem B. Bityutskiy 86*801c135cSArtem B. Bityutskiy /** 87*801c135cSArtem B. Bityutskiy * ubi_open_volume - open UBI volume. 88*801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 89*801c135cSArtem B. Bityutskiy * @vol_id: volume ID 90*801c135cSArtem B. Bityutskiy * @mode: open mode 91*801c135cSArtem B. Bityutskiy * 92*801c135cSArtem B. Bityutskiy * The @mode parameter specifies if the volume should be opened in read-only 93*801c135cSArtem B. Bityutskiy * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that 94*801c135cSArtem B. Bityutskiy * nobody else will be able to open this volume. UBI allows to have many volume 95*801c135cSArtem B. Bityutskiy * readers and one writer at a time. 96*801c135cSArtem B. Bityutskiy * 97*801c135cSArtem B. Bityutskiy * If a static volume is being opened for the first time since boot, it will be 98*801c135cSArtem B. Bityutskiy * checked by this function, which means it will be fully read and the CRC 99*801c135cSArtem B. Bityutskiy * checksum of each logical eraseblock will be checked. 100*801c135cSArtem B. Bityutskiy * 101*801c135cSArtem B. Bityutskiy * This function returns volume descriptor in case of success and a negative 102*801c135cSArtem B. Bityutskiy * error code in case of failure. 103*801c135cSArtem B. Bityutskiy */ 104*801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) 105*801c135cSArtem B. Bityutskiy { 106*801c135cSArtem B. Bityutskiy int err; 107*801c135cSArtem B. Bityutskiy struct ubi_volume_desc *desc; 108*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = ubi_devices[ubi_num]; 109*801c135cSArtem B. Bityutskiy struct ubi_volume *vol; 110*801c135cSArtem B. Bityutskiy 111*801c135cSArtem B. Bityutskiy dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode); 112*801c135cSArtem B. Bityutskiy 113*801c135cSArtem B. Bityutskiy err = -ENODEV; 114*801c135cSArtem B. Bityutskiy if (!try_module_get(THIS_MODULE)) 115*801c135cSArtem B. Bityutskiy return ERR_PTR(err); 116*801c135cSArtem B. Bityutskiy 117*801c135cSArtem B. Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi) 118*801c135cSArtem B. Bityutskiy goto out_put; 119*801c135cSArtem B. Bityutskiy 120*801c135cSArtem B. Bityutskiy err = -EINVAL; 121*801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 122*801c135cSArtem B. Bityutskiy goto out_put; 123*801c135cSArtem B. Bityutskiy if (mode != UBI_READONLY && mode != UBI_READWRITE && 124*801c135cSArtem B. Bityutskiy mode != UBI_EXCLUSIVE) 125*801c135cSArtem B. Bityutskiy goto out_put; 126*801c135cSArtem B. Bityutskiy 127*801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 128*801c135cSArtem B. Bityutskiy if (!desc) { 129*801c135cSArtem B. Bityutskiy err = -ENOMEM; 130*801c135cSArtem B. Bityutskiy goto out_put; 131*801c135cSArtem B. Bityutskiy } 132*801c135cSArtem B. Bityutskiy 133*801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 134*801c135cSArtem B. Bityutskiy vol = ubi->volumes[vol_id]; 135*801c135cSArtem B. Bityutskiy if (!vol) { 136*801c135cSArtem B. Bityutskiy err = -ENODEV; 137*801c135cSArtem B. Bityutskiy goto out_unlock; 138*801c135cSArtem B. Bityutskiy } 139*801c135cSArtem B. Bityutskiy 140*801c135cSArtem B. Bityutskiy err = -EBUSY; 141*801c135cSArtem B. Bityutskiy switch (mode) { 142*801c135cSArtem B. Bityutskiy case UBI_READONLY: 143*801c135cSArtem B. Bityutskiy if (vol->exclusive) 144*801c135cSArtem B. Bityutskiy goto out_unlock; 145*801c135cSArtem B. Bityutskiy vol->readers += 1; 146*801c135cSArtem B. Bityutskiy break; 147*801c135cSArtem B. Bityutskiy 148*801c135cSArtem B. Bityutskiy case UBI_READWRITE: 149*801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers > 0) 150*801c135cSArtem B. Bityutskiy goto out_unlock; 151*801c135cSArtem B. Bityutskiy vol->writers += 1; 152*801c135cSArtem B. Bityutskiy break; 153*801c135cSArtem B. Bityutskiy 154*801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 155*801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers || vol->readers) 156*801c135cSArtem B. Bityutskiy goto out_unlock; 157*801c135cSArtem B. Bityutskiy vol->exclusive = 1; 158*801c135cSArtem B. Bityutskiy break; 159*801c135cSArtem B. Bityutskiy } 160*801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 161*801c135cSArtem B. Bityutskiy 162*801c135cSArtem B. Bityutskiy desc->vol = vol; 163*801c135cSArtem B. Bityutskiy desc->mode = mode; 164*801c135cSArtem B. Bityutskiy 165*801c135cSArtem B. Bityutskiy /* 166*801c135cSArtem B. Bityutskiy * To prevent simultaneous checks of the same volume we use @vtbl_mutex, 167*801c135cSArtem B. Bityutskiy * although it is not the purpose it was introduced for. 168*801c135cSArtem B. Bityutskiy */ 169*801c135cSArtem B. Bityutskiy mutex_lock(&ubi->vtbl_mutex); 170*801c135cSArtem B. Bityutskiy if (!vol->checked) { 171*801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 172*801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 173*801c135cSArtem B. Bityutskiy if (err < 0) { 174*801c135cSArtem B. Bityutskiy mutex_unlock(&ubi->vtbl_mutex); 175*801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 176*801c135cSArtem B. Bityutskiy return ERR_PTR(err); 177*801c135cSArtem B. Bityutskiy } 178*801c135cSArtem B. Bityutskiy if (err == 1) { 179*801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 180*801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 181*801c135cSArtem B. Bityutskiy vol->corrupted = 1; 182*801c135cSArtem B. Bityutskiy } 183*801c135cSArtem B. Bityutskiy vol->checked = 1; 184*801c135cSArtem B. Bityutskiy } 185*801c135cSArtem B. Bityutskiy mutex_unlock(&ubi->vtbl_mutex); 186*801c135cSArtem B. Bityutskiy return desc; 187*801c135cSArtem B. Bityutskiy 188*801c135cSArtem B. Bityutskiy out_unlock: 189*801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 190*801c135cSArtem B. Bityutskiy kfree(desc); 191*801c135cSArtem B. Bityutskiy out_put: 192*801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 193*801c135cSArtem B. Bityutskiy return ERR_PTR(err); 194*801c135cSArtem B. Bityutskiy } 195*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 196*801c135cSArtem B. Bityutskiy 197*801c135cSArtem B. Bityutskiy /** 198*801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 199*801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 200*801c135cSArtem B. Bityutskiy * @name: volume name 201*801c135cSArtem B. Bityutskiy * @mode: open mode 202*801c135cSArtem B. Bityutskiy * 203*801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 204*801c135cSArtem B. Bityutskiy */ 205*801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 206*801c135cSArtem B. Bityutskiy int mode) 207*801c135cSArtem B. Bityutskiy { 208*801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 209*801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ret; 210*801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 211*801c135cSArtem B. Bityutskiy 212*801c135cSArtem B. Bityutskiy dbg_msg("open volume %s, mode %d", name, mode); 213*801c135cSArtem B. Bityutskiy 214*801c135cSArtem B. Bityutskiy if (!name) 215*801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 216*801c135cSArtem B. Bityutskiy 217*801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 218*801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 219*801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 220*801c135cSArtem B. Bityutskiy 221*801c135cSArtem B. Bityutskiy ret = ERR_PTR(-ENODEV); 222*801c135cSArtem B. Bityutskiy if (!try_module_get(THIS_MODULE)) 223*801c135cSArtem B. Bityutskiy return ret; 224*801c135cSArtem B. Bityutskiy 225*801c135cSArtem B. Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num]) 226*801c135cSArtem B. Bityutskiy goto out_put; 227*801c135cSArtem B. Bityutskiy 228*801c135cSArtem B. Bityutskiy ubi = ubi_devices[ubi_num]; 229*801c135cSArtem B. Bityutskiy 230*801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 231*801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 232*801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 233*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 234*801c135cSArtem B. Bityutskiy 235*801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 236*801c135cSArtem B. Bityutskiy vol_id = i; 237*801c135cSArtem B. Bityutskiy break; 238*801c135cSArtem B. Bityutskiy } 239*801c135cSArtem B. Bityutskiy } 240*801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 241*801c135cSArtem B. Bityutskiy 242*801c135cSArtem B. Bityutskiy if (vol_id < 0) 243*801c135cSArtem B. Bityutskiy goto out_put; 244*801c135cSArtem B. Bityutskiy 245*801c135cSArtem B. Bityutskiy ret = ubi_open_volume(ubi_num, vol_id, mode); 246*801c135cSArtem B. Bityutskiy 247*801c135cSArtem B. Bityutskiy out_put: 248*801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 249*801c135cSArtem B. Bityutskiy return ret; 250*801c135cSArtem B. Bityutskiy } 251*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 252*801c135cSArtem B. Bityutskiy 253*801c135cSArtem B. Bityutskiy /** 254*801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 255*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 256*801c135cSArtem B. Bityutskiy */ 257*801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 258*801c135cSArtem B. Bityutskiy { 259*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 260*801c135cSArtem B. Bityutskiy 261*801c135cSArtem B. Bityutskiy dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); 262*801c135cSArtem B. Bityutskiy 263*801c135cSArtem B. Bityutskiy spin_lock(&vol->ubi->volumes_lock); 264*801c135cSArtem B. Bityutskiy switch (desc->mode) { 265*801c135cSArtem B. Bityutskiy case UBI_READONLY: 266*801c135cSArtem B. Bityutskiy vol->readers -= 1; 267*801c135cSArtem B. Bityutskiy break; 268*801c135cSArtem B. Bityutskiy case UBI_READWRITE: 269*801c135cSArtem B. Bityutskiy vol->writers -= 1; 270*801c135cSArtem B. Bityutskiy break; 271*801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 272*801c135cSArtem B. Bityutskiy vol->exclusive = 0; 273*801c135cSArtem B. Bityutskiy } 274*801c135cSArtem B. Bityutskiy spin_unlock(&vol->ubi->volumes_lock); 275*801c135cSArtem B. Bityutskiy 276*801c135cSArtem B. Bityutskiy kfree(desc); 277*801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 278*801c135cSArtem B. Bityutskiy } 279*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 280*801c135cSArtem B. Bityutskiy 281*801c135cSArtem B. Bityutskiy /** 282*801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 283*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 284*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 285*801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 286*801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 287*801c135cSArtem B. Bityutskiy * @len: how many bytes to read 288*801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 289*801c135cSArtem B. Bityutskiy * 290*801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 291*801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 292*801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 293*801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 294*801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 295*801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 296*801c135cSArtem B. Bityutskiy * 297*801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 298*801c135cSArtem B. Bityutskiy * function returns a negative error code. 299*801c135cSArtem B. Bityutskiy * 300*801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 301*801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 302*801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 303*801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 304*801c135cSArtem B. Bityutskiy * 305*801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 306*801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF error code. 307*801c135cSArtem B. Bityutskiy */ 308*801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 309*801c135cSArtem B. Bityutskiy int len, int check) 310*801c135cSArtem B. Bityutskiy { 311*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 312*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 313*801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 314*801c135cSArtem B. Bityutskiy 315*801c135cSArtem B. Bityutskiy dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 316*801c135cSArtem B. Bityutskiy 317*801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 318*801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 319*801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 320*801c135cSArtem B. Bityutskiy return -EINVAL; 321*801c135cSArtem B. Bityutskiy 322*801c135cSArtem B. Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 && 323*801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 324*801c135cSArtem B. Bityutskiy return -EINVAL; 325*801c135cSArtem B. Bityutskiy 326*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 327*801c135cSArtem B. Bityutskiy return -EBADF; 328*801c135cSArtem B. Bityutskiy if (len == 0) 329*801c135cSArtem B. Bityutskiy return 0; 330*801c135cSArtem B. Bityutskiy 331*801c135cSArtem B. Bityutskiy err = ubi_eba_read_leb(ubi, vol_id, lnum, buf, offset, len, check); 332*801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 333*801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 334*801c135cSArtem B. Bityutskiy vol->corrupted = 1; 335*801c135cSArtem B. Bityutskiy } 336*801c135cSArtem B. Bityutskiy 337*801c135cSArtem B. Bityutskiy return err; 338*801c135cSArtem B. Bityutskiy } 339*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 340*801c135cSArtem B. Bityutskiy 341*801c135cSArtem B. Bityutskiy /** 342*801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 343*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 344*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 345*801c135cSArtem B. Bityutskiy * @buf: data to write 346*801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 347*801c135cSArtem B. Bityutskiy * @len: how many bytes to write 348*801c135cSArtem B. Bityutskiy * @dtype: expected data type 349*801c135cSArtem B. Bityutskiy * 350*801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 351*801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 352*801c135cSArtem B. Bityutskiy * the data. 353*801c135cSArtem B. Bityutskiy * 354*801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 355*801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 356*801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 357*801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 358*801c135cSArtem B. Bityutskiy * 359*801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 360*801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 361*801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 362*801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 363*801c135cSArtem B. Bityutskiy * garbage. 364*801c135cSArtem B. Bityutskiy * 365*801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 366*801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 367*801c135cSArtem B. Bityutskiy */ 368*801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 369*801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 370*801c135cSArtem B. Bityutskiy { 371*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 372*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 373*801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 374*801c135cSArtem B. Bityutskiy 375*801c135cSArtem B. Bityutskiy dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 376*801c135cSArtem B. Bityutskiy 377*801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 378*801c135cSArtem B. Bityutskiy return -EINVAL; 379*801c135cSArtem B. Bityutskiy 380*801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 381*801c135cSArtem B. Bityutskiy return -EROFS; 382*801c135cSArtem B. Bityutskiy 383*801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 384*801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size || offset % ubi->min_io_size || 385*801c135cSArtem B. Bityutskiy len % ubi->min_io_size) 386*801c135cSArtem B. Bityutskiy return -EINVAL; 387*801c135cSArtem B. Bityutskiy 388*801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 389*801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 390*801c135cSArtem B. Bityutskiy return -EINVAL; 391*801c135cSArtem B. Bityutskiy 392*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 393*801c135cSArtem B. Bityutskiy return -EBADF; 394*801c135cSArtem B. Bityutskiy 395*801c135cSArtem B. Bityutskiy if (len == 0) 396*801c135cSArtem B. Bityutskiy return 0; 397*801c135cSArtem B. Bityutskiy 398*801c135cSArtem B. Bityutskiy return ubi_eba_write_leb(ubi, vol_id, lnum, buf, offset, len, dtype); 399*801c135cSArtem B. Bityutskiy } 400*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 401*801c135cSArtem B. Bityutskiy 402*801c135cSArtem B. Bityutskiy /* 403*801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 404*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 405*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 406*801c135cSArtem B. Bityutskiy * @buf: data to write 407*801c135cSArtem B. Bityutskiy * @len: how many bytes to write 408*801c135cSArtem B. Bityutskiy * @dtype: expected data type 409*801c135cSArtem B. Bityutskiy * 410*801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 411*801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 412*801c135cSArtem B. Bityutskiy * data, which has to be aligned. The length may be shorter then the logical 413*801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 414*801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 415*801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 416*801c135cSArtem B. Bityutskiy * code in case of failure. 417*801c135cSArtem B. Bityutskiy */ 418*801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 419*801c135cSArtem B. Bityutskiy int len, int dtype) 420*801c135cSArtem B. Bityutskiy { 421*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 422*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 423*801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 424*801c135cSArtem B. Bityutskiy 425*801c135cSArtem B. Bityutskiy dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 426*801c135cSArtem B. Bityutskiy 427*801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 428*801c135cSArtem B. Bityutskiy return -EINVAL; 429*801c135cSArtem B. Bityutskiy 430*801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 431*801c135cSArtem B. Bityutskiy return -EROFS; 432*801c135cSArtem B. Bityutskiy 433*801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 434*801c135cSArtem B. Bityutskiy len > vol->usable_leb_size || len % ubi->min_io_size) 435*801c135cSArtem B. Bityutskiy return -EINVAL; 436*801c135cSArtem B. Bityutskiy 437*801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 438*801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 439*801c135cSArtem B. Bityutskiy return -EINVAL; 440*801c135cSArtem B. Bityutskiy 441*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 442*801c135cSArtem B. Bityutskiy return -EBADF; 443*801c135cSArtem B. Bityutskiy 444*801c135cSArtem B. Bityutskiy if (len == 0) 445*801c135cSArtem B. Bityutskiy return 0; 446*801c135cSArtem B. Bityutskiy 447*801c135cSArtem B. Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol_id, lnum, buf, len, dtype); 448*801c135cSArtem B. Bityutskiy } 449*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 450*801c135cSArtem B. Bityutskiy 451*801c135cSArtem B. Bityutskiy /** 452*801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 453*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 454*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 455*801c135cSArtem B. Bityutskiy * 456*801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 457*801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 458*801c135cSArtem B. Bityutskiy * negative error code in case of failure. 459*801c135cSArtem B. Bityutskiy * 460*801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 461*801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 462*801c135cSArtem B. Bityutskiy */ 463*801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 464*801c135cSArtem B. Bityutskiy { 465*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 466*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 467*801c135cSArtem B. Bityutskiy int err, vol_id = vol->vol_id; 468*801c135cSArtem B. Bityutskiy 469*801c135cSArtem B. Bityutskiy dbg_msg("erase LEB %d:%d", vol_id, lnum); 470*801c135cSArtem B. Bityutskiy 471*801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 472*801c135cSArtem B. Bityutskiy return -EROFS; 473*801c135cSArtem B. Bityutskiy 474*801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 475*801c135cSArtem B. Bityutskiy return -EINVAL; 476*801c135cSArtem B. Bityutskiy 477*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 478*801c135cSArtem B. Bityutskiy return -EBADF; 479*801c135cSArtem B. Bityutskiy 480*801c135cSArtem B. Bityutskiy err = ubi_eba_unmap_leb(ubi, vol_id, lnum); 481*801c135cSArtem B. Bityutskiy if (err) 482*801c135cSArtem B. Bityutskiy return err; 483*801c135cSArtem B. Bityutskiy 484*801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 485*801c135cSArtem B. Bityutskiy } 486*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 487*801c135cSArtem B. Bityutskiy 488*801c135cSArtem B. Bityutskiy /** 489*801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 490*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 491*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 492*801c135cSArtem B. Bityutskiy * 493*801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 494*801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 495*801c135cSArtem B. Bityutskiy * physically erased in background. This operation is much faster then the 496*801c135cSArtem B. Bityutskiy * erase operation. 497*801c135cSArtem B. Bityutskiy * 498*801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 499*801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 500*801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 501*801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 502*801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 503*801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 504*801c135cSArtem B. Bityutskiy * used with care. 505*801c135cSArtem B. Bityutskiy * 506*801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 507*801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 508*801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 509*801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 510*801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 511*801c135cSArtem B. Bityutskiy * 512*801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 513*801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 514*801c135cSArtem B. Bityutskiy * first un-map it, then write new data, rather then first erase it, then write 515*801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 516*801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 517*801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 518*801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 519*801c135cSArtem B. Bityutskiy * 520*801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 521*801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 522*801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 523*801c135cSArtem B. Bityutskiy */ 524*801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 525*801c135cSArtem B. Bityutskiy { 526*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 527*801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 528*801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 529*801c135cSArtem B. Bityutskiy 530*801c135cSArtem B. Bityutskiy dbg_msg("unmap LEB %d:%d", vol_id, lnum); 531*801c135cSArtem B. Bityutskiy 532*801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 533*801c135cSArtem B. Bityutskiy return -EROFS; 534*801c135cSArtem B. Bityutskiy 535*801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 536*801c135cSArtem B. Bityutskiy return -EINVAL; 537*801c135cSArtem B. Bityutskiy 538*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 539*801c135cSArtem B. Bityutskiy return -EBADF; 540*801c135cSArtem B. Bityutskiy 541*801c135cSArtem B. Bityutskiy return ubi_eba_unmap_leb(ubi, vol_id, lnum); 542*801c135cSArtem B. Bityutskiy } 543*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 544*801c135cSArtem B. Bityutskiy 545*801c135cSArtem B. Bityutskiy /** 546*801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 547*801c135cSArtem B. Bityutskiy * @desc: volume descriptor 548*801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 549*801c135cSArtem B. Bityutskiy * 550*801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 551*801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 552*801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 553*801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 554*801c135cSArtem B. Bityutskiy * mapped to. 555*801c135cSArtem B. Bityutskiy * 556*801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 557*801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 558*801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 559*801c135cSArtem B. Bityutskiy * code. 560*801c135cSArtem B. Bityutskiy */ 561*801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 562*801c135cSArtem B. Bityutskiy { 563*801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 564*801c135cSArtem B. Bityutskiy 565*801c135cSArtem B. Bityutskiy dbg_msg("test LEB %d:%d", vol->vol_id, lnum); 566*801c135cSArtem B. Bityutskiy 567*801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 568*801c135cSArtem B. Bityutskiy return -EINVAL; 569*801c135cSArtem B. Bityutskiy 570*801c135cSArtem B. Bityutskiy if (vol->upd_marker) 571*801c135cSArtem B. Bityutskiy return -EBADF; 572*801c135cSArtem B. Bityutskiy 573*801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 574*801c135cSArtem B. Bityutskiy } 575*801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 576