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> 255a0e3ad6STejun Heo #include <linux/slab.h> 26b5710284SCorentin Chary #include <linux/namei.h> 27b5710284SCorentin Chary #include <linux/fs.h> 28801c135cSArtem B. Bityutskiy #include <asm/div64.h> 29801c135cSArtem B. Bityutskiy #include "ubi.h" 30801c135cSArtem B. Bityutskiy 31801c135cSArtem B. Bityutskiy /** 320e0ee1ccSDmitry Pervushin * ubi_do_get_device_info - get information about UBI device. 330e0ee1ccSDmitry Pervushin * @ubi: UBI device description object 340e0ee1ccSDmitry Pervushin * @di: the information is stored here 350e0ee1ccSDmitry Pervushin * 360e0ee1ccSDmitry Pervushin * This function is the same as 'ubi_get_device_info()', but it assumes the UBI 370e0ee1ccSDmitry Pervushin * device is locked and cannot disappear. 380e0ee1ccSDmitry Pervushin */ 390e0ee1ccSDmitry Pervushin void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di) 400e0ee1ccSDmitry Pervushin { 410e0ee1ccSDmitry Pervushin di->ubi_num = ubi->ubi_num; 420e0ee1ccSDmitry Pervushin di->leb_size = ubi->leb_size; 43*f43ec882SArtem Bityutskiy di->leb_start = ubi->leb_start; 440e0ee1ccSDmitry Pervushin di->min_io_size = ubi->min_io_size; 4530b542efSArtem Bityutskiy di->max_write_size = ubi->max_write_size; 460e0ee1ccSDmitry Pervushin di->ro_mode = ubi->ro_mode; 470e0ee1ccSDmitry Pervushin di->cdev = ubi->cdev.dev; 480e0ee1ccSDmitry Pervushin } 490e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_do_get_device_info); 500e0ee1ccSDmitry Pervushin 510e0ee1ccSDmitry Pervushin /** 52801c135cSArtem B. Bityutskiy * ubi_get_device_info - get information about UBI device. 53801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 54801c135cSArtem B. Bityutskiy * @di: the information is stored here 55801c135cSArtem B. Bityutskiy * 56e73f4459SArtem Bityutskiy * This function returns %0 in case of success, %-EINVAL if the UBI device 57e73f4459SArtem Bityutskiy * number is invalid, and %-ENODEV if there is no such UBI device. 58801c135cSArtem B. Bityutskiy */ 59801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) 60801c135cSArtem B. Bityutskiy { 61e73f4459SArtem Bityutskiy struct ubi_device *ubi; 62801c135cSArtem B. Bityutskiy 63e73f4459SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 64e73f4459SArtem Bityutskiy return -EINVAL; 65e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 66e73f4459SArtem Bityutskiy if (!ubi) 67801c135cSArtem B. Bityutskiy return -ENODEV; 680e0ee1ccSDmitry Pervushin ubi_do_get_device_info(ubi, di); 69e73f4459SArtem Bityutskiy ubi_put_device(ubi); 70801c135cSArtem B. Bityutskiy return 0; 71801c135cSArtem B. Bityutskiy } 72801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_device_info); 73801c135cSArtem B. Bityutskiy 74801c135cSArtem B. Bityutskiy /** 750e0ee1ccSDmitry Pervushin * ubi_do_get_volume_info - get information about UBI volume. 760e0ee1ccSDmitry Pervushin * @ubi: UBI device description object 770e0ee1ccSDmitry Pervushin * @vol: volume description object 78801c135cSArtem B. Bityutskiy * @vi: the information is stored here 79801c135cSArtem B. Bityutskiy */ 800e0ee1ccSDmitry Pervushin void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol, 81801c135cSArtem B. Bityutskiy struct ubi_volume_info *vi) 82801c135cSArtem B. Bityutskiy { 83801c135cSArtem B. Bityutskiy vi->vol_id = vol->vol_id; 84801c135cSArtem B. Bityutskiy vi->ubi_num = ubi->ubi_num; 85801c135cSArtem B. Bityutskiy vi->size = vol->reserved_pebs; 86801c135cSArtem B. Bityutskiy vi->used_bytes = vol->used_bytes; 87801c135cSArtem B. Bityutskiy vi->vol_type = vol->vol_type; 88801c135cSArtem B. Bityutskiy vi->corrupted = vol->corrupted; 89801c135cSArtem B. Bityutskiy vi->upd_marker = vol->upd_marker; 90801c135cSArtem B. Bityutskiy vi->alignment = vol->alignment; 91801c135cSArtem B. Bityutskiy vi->usable_leb_size = vol->usable_leb_size; 92801c135cSArtem B. Bityutskiy vi->name_len = vol->name_len; 93801c135cSArtem B. Bityutskiy vi->name = vol->name; 9449dfc299SArtem Bityutskiy vi->cdev = vol->cdev.dev; 95801c135cSArtem B. Bityutskiy } 960e0ee1ccSDmitry Pervushin 970e0ee1ccSDmitry Pervushin /** 980e0ee1ccSDmitry Pervushin * ubi_get_volume_info - get information about UBI volume. 990e0ee1ccSDmitry Pervushin * @desc: volume descriptor 1000e0ee1ccSDmitry Pervushin * @vi: the information is stored here 1010e0ee1ccSDmitry Pervushin */ 1020e0ee1ccSDmitry Pervushin void ubi_get_volume_info(struct ubi_volume_desc *desc, 1030e0ee1ccSDmitry Pervushin struct ubi_volume_info *vi) 1040e0ee1ccSDmitry Pervushin { 1050e0ee1ccSDmitry Pervushin ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi); 1060e0ee1ccSDmitry Pervushin } 107801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_get_volume_info); 108801c135cSArtem B. Bityutskiy 109801c135cSArtem B. Bityutskiy /** 110801c135cSArtem B. Bityutskiy * ubi_open_volume - open UBI volume. 111801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 112801c135cSArtem B. Bityutskiy * @vol_id: volume ID 113801c135cSArtem B. Bityutskiy * @mode: open mode 114801c135cSArtem B. Bityutskiy * 115801c135cSArtem B. Bityutskiy * The @mode parameter specifies if the volume should be opened in read-only 116801c135cSArtem B. Bityutskiy * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that 117801c135cSArtem B. Bityutskiy * nobody else will be able to open this volume. UBI allows to have many volume 118801c135cSArtem B. Bityutskiy * readers and one writer at a time. 119801c135cSArtem B. Bityutskiy * 120801c135cSArtem B. Bityutskiy * If a static volume is being opened for the first time since boot, it will be 121801c135cSArtem B. Bityutskiy * checked by this function, which means it will be fully read and the CRC 122801c135cSArtem B. Bityutskiy * checksum of each logical eraseblock will be checked. 123801c135cSArtem B. Bityutskiy * 124801c135cSArtem B. Bityutskiy * This function returns volume descriptor in case of success and a negative 125801c135cSArtem B. Bityutskiy * error code in case of failure. 126801c135cSArtem B. Bityutskiy */ 127801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) 128801c135cSArtem B. Bityutskiy { 129801c135cSArtem B. Bityutskiy int err; 130801c135cSArtem B. Bityutskiy struct ubi_volume_desc *desc; 1310169b49dSJesper Juhl struct ubi_device *ubi; 132801c135cSArtem B. Bityutskiy struct ubi_volume *vol; 133801c135cSArtem B. Bityutskiy 134e1cf7e6dSArtem Bityutskiy dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode); 135801c135cSArtem B. Bityutskiy 13635ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 13735ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 1380169b49dSJesper Juhl 139801c135cSArtem B. Bityutskiy if (mode != UBI_READONLY && mode != UBI_READWRITE && 140801c135cSArtem B. Bityutskiy mode != UBI_EXCLUSIVE) 14135ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 14235ad5fb7SArtem Bityutskiy 143e73f4459SArtem Bityutskiy /* 144e73f4459SArtem Bityutskiy * First of all, we have to get the UBI device to prevent its removal. 145e73f4459SArtem Bityutskiy */ 146e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 14735ad5fb7SArtem Bityutskiy if (!ubi) 14835ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 14935ad5fb7SArtem Bityutskiy 150e73f4459SArtem Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) { 151e73f4459SArtem Bityutskiy err = -EINVAL; 152e73f4459SArtem Bityutskiy goto out_put_ubi; 153e73f4459SArtem Bityutskiy } 154801c135cSArtem B. Bityutskiy 155801c135cSArtem B. Bityutskiy desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); 156e73f4459SArtem Bityutskiy if (!desc) { 157e73f4459SArtem Bityutskiy err = -ENOMEM; 158e73f4459SArtem Bityutskiy goto out_put_ubi; 159e73f4459SArtem Bityutskiy } 16035ad5fb7SArtem Bityutskiy 16135ad5fb7SArtem Bityutskiy err = -ENODEV; 16235ad5fb7SArtem Bityutskiy if (!try_module_get(THIS_MODULE)) 16335ad5fb7SArtem Bityutskiy goto out_free; 164801c135cSArtem B. Bityutskiy 165801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 166801c135cSArtem B. Bityutskiy vol = ubi->volumes[vol_id]; 16735ad5fb7SArtem Bityutskiy if (!vol) 168801c135cSArtem B. Bityutskiy goto out_unlock; 169801c135cSArtem B. Bityutskiy 170801c135cSArtem B. Bityutskiy err = -EBUSY; 171801c135cSArtem B. Bityutskiy switch (mode) { 172801c135cSArtem B. Bityutskiy case UBI_READONLY: 173801c135cSArtem B. Bityutskiy if (vol->exclusive) 174801c135cSArtem B. Bityutskiy goto out_unlock; 175801c135cSArtem B. Bityutskiy vol->readers += 1; 176801c135cSArtem B. Bityutskiy break; 177801c135cSArtem B. Bityutskiy 178801c135cSArtem B. Bityutskiy case UBI_READWRITE: 179801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers > 0) 180801c135cSArtem B. Bityutskiy goto out_unlock; 181801c135cSArtem B. Bityutskiy vol->writers += 1; 182801c135cSArtem B. Bityutskiy break; 183801c135cSArtem B. Bityutskiy 184801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 185801c135cSArtem B. Bityutskiy if (vol->exclusive || vol->writers || vol->readers) 186801c135cSArtem B. Bityutskiy goto out_unlock; 187801c135cSArtem B. Bityutskiy vol->exclusive = 1; 188801c135cSArtem B. Bityutskiy break; 189801c135cSArtem B. Bityutskiy } 190450f872aSArtem Bityutskiy get_device(&vol->dev); 191d05c77a8SArtem Bityutskiy vol->ref_count += 1; 192801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 193801c135cSArtem B. Bityutskiy 194801c135cSArtem B. Bityutskiy desc->vol = vol; 195801c135cSArtem B. Bityutskiy desc->mode = mode; 196801c135cSArtem B. Bityutskiy 197783b273aSArtem Bityutskiy mutex_lock(&ubi->ckvol_mutex); 198801c135cSArtem B. Bityutskiy if (!vol->checked) { 199801c135cSArtem B. Bityutskiy /* This is the first open - check the volume */ 200801c135cSArtem B. Bityutskiy err = ubi_check_volume(ubi, vol_id); 201801c135cSArtem B. Bityutskiy if (err < 0) { 202783b273aSArtem Bityutskiy mutex_unlock(&ubi->ckvol_mutex); 203801c135cSArtem B. Bityutskiy ubi_close_volume(desc); 204801c135cSArtem B. Bityutskiy return ERR_PTR(err); 205801c135cSArtem B. Bityutskiy } 206801c135cSArtem B. Bityutskiy if (err == 1) { 207801c135cSArtem B. Bityutskiy ubi_warn("volume %d on UBI device %d is corrupted", 208801c135cSArtem B. Bityutskiy vol_id, ubi->ubi_num); 209801c135cSArtem B. Bityutskiy vol->corrupted = 1; 210801c135cSArtem B. Bityutskiy } 211801c135cSArtem B. Bityutskiy vol->checked = 1; 212801c135cSArtem B. Bityutskiy } 213783b273aSArtem Bityutskiy mutex_unlock(&ubi->ckvol_mutex); 21435ad5fb7SArtem Bityutskiy 215801c135cSArtem B. Bityutskiy return desc; 216801c135cSArtem B. Bityutskiy 217801c135cSArtem B. Bityutskiy out_unlock: 218801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 219801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 22035ad5fb7SArtem Bityutskiy out_free: 22135ad5fb7SArtem Bityutskiy kfree(desc); 222e73f4459SArtem Bityutskiy out_put_ubi: 223e73f4459SArtem Bityutskiy ubi_put_device(ubi); 224e1cf7e6dSArtem Bityutskiy dbg_err("cannot open device %d, volume %d, error %d", 225e1cf7e6dSArtem Bityutskiy ubi_num, vol_id, err); 226801c135cSArtem B. Bityutskiy return ERR_PTR(err); 227801c135cSArtem B. Bityutskiy } 228801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume); 229801c135cSArtem B. Bityutskiy 230801c135cSArtem B. Bityutskiy /** 231801c135cSArtem B. Bityutskiy * ubi_open_volume_nm - open UBI volume by name. 232801c135cSArtem B. Bityutskiy * @ubi_num: UBI device number 233801c135cSArtem B. Bityutskiy * @name: volume name 234801c135cSArtem B. Bityutskiy * @mode: open mode 235801c135cSArtem B. Bityutskiy * 236801c135cSArtem B. Bityutskiy * This function is similar to 'ubi_open_volume()', but opens a volume by name. 237801c135cSArtem B. Bityutskiy */ 238801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, 239801c135cSArtem B. Bityutskiy int mode) 240801c135cSArtem B. Bityutskiy { 241801c135cSArtem B. Bityutskiy int i, vol_id = -1, len; 242801c135cSArtem B. Bityutskiy struct ubi_device *ubi; 243e73f4459SArtem Bityutskiy struct ubi_volume_desc *ret; 244801c135cSArtem B. Bityutskiy 245e1cf7e6dSArtem Bityutskiy dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode); 246801c135cSArtem B. Bityutskiy 247801c135cSArtem B. Bityutskiy if (!name) 248801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 249801c135cSArtem B. Bityutskiy 250801c135cSArtem B. Bityutskiy len = strnlen(name, UBI_VOL_NAME_MAX + 1); 251801c135cSArtem B. Bityutskiy if (len > UBI_VOL_NAME_MAX) 252801c135cSArtem B. Bityutskiy return ERR_PTR(-EINVAL); 253801c135cSArtem B. Bityutskiy 25435ad5fb7SArtem Bityutskiy if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) 25535ad5fb7SArtem Bityutskiy return ERR_PTR(-EINVAL); 256801c135cSArtem B. Bityutskiy 257e73f4459SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 25835ad5fb7SArtem Bityutskiy if (!ubi) 25935ad5fb7SArtem Bityutskiy return ERR_PTR(-ENODEV); 260801c135cSArtem B. Bityutskiy 261801c135cSArtem B. Bityutskiy spin_lock(&ubi->volumes_lock); 262801c135cSArtem B. Bityutskiy /* Walk all volumes of this UBI device */ 263801c135cSArtem B. Bityutskiy for (i = 0; i < ubi->vtbl_slots; i++) { 264801c135cSArtem B. Bityutskiy struct ubi_volume *vol = ubi->volumes[i]; 265801c135cSArtem B. Bityutskiy 266801c135cSArtem B. Bityutskiy if (vol && len == vol->name_len && !strcmp(name, vol->name)) { 267801c135cSArtem B. Bityutskiy vol_id = i; 268801c135cSArtem B. Bityutskiy break; 269801c135cSArtem B. Bityutskiy } 270801c135cSArtem B. Bityutskiy } 271801c135cSArtem B. Bityutskiy spin_unlock(&ubi->volumes_lock); 272801c135cSArtem B. Bityutskiy 273e73f4459SArtem Bityutskiy if (vol_id >= 0) 274e73f4459SArtem Bityutskiy ret = ubi_open_volume(ubi_num, vol_id, mode); 275e73f4459SArtem Bityutskiy else 276e73f4459SArtem Bityutskiy ret = ERR_PTR(-ENODEV); 277801c135cSArtem B. Bityutskiy 278e73f4459SArtem Bityutskiy /* 279e73f4459SArtem Bityutskiy * We should put the UBI device even in case of success, because 280e73f4459SArtem Bityutskiy * 'ubi_open_volume()' took a reference as well. 281e73f4459SArtem Bityutskiy */ 282e73f4459SArtem Bityutskiy ubi_put_device(ubi); 283e73f4459SArtem Bityutskiy return ret; 284801c135cSArtem B. Bityutskiy } 285801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_open_volume_nm); 286801c135cSArtem B. Bityutskiy 287801c135cSArtem B. Bityutskiy /** 288b5710284SCorentin Chary * ubi_open_volume_path - open UBI volume by its character device node path. 289b5710284SCorentin Chary * @pathname: volume character device node path 290b5710284SCorentin Chary * @mode: open mode 291b5710284SCorentin Chary * 292b5710284SCorentin Chary * This function is similar to 'ubi_open_volume()', but opens a volume the path 293b5710284SCorentin Chary * to its character device node. 294b5710284SCorentin Chary */ 295b5710284SCorentin Chary struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode) 296b5710284SCorentin Chary { 297b531b55aSArtem Bityutskiy int error, ubi_num, vol_id, mod; 298b5710284SCorentin Chary struct inode *inode; 299b5710284SCorentin Chary struct path path; 300b5710284SCorentin Chary 301b5710284SCorentin Chary dbg_gen("open volume %s, mode %d", pathname, mode); 302b5710284SCorentin Chary 303b5710284SCorentin Chary if (!pathname || !*pathname) 304b5710284SCorentin Chary return ERR_PTR(-EINVAL); 305b5710284SCorentin Chary 306b5710284SCorentin Chary error = kern_path(pathname, LOOKUP_FOLLOW, &path); 307b5710284SCorentin Chary if (error) 308b5710284SCorentin Chary return ERR_PTR(error); 309b5710284SCorentin Chary 310b5710284SCorentin Chary inode = path.dentry->d_inode; 311b531b55aSArtem Bityutskiy mod = inode->i_mode; 312b5710284SCorentin Chary ubi_num = ubi_major2num(imajor(inode)); 313b5710284SCorentin Chary vol_id = iminor(inode) - 1; 314b5710284SCorentin Chary path_put(&path); 315b531b55aSArtem Bityutskiy 316b531b55aSArtem Bityutskiy if (!S_ISCHR(mod)) 317b531b55aSArtem Bityutskiy return ERR_PTR(-EINVAL); 318b531b55aSArtem Bityutskiy if (vol_id >= 0 && ubi_num >= 0) 319b531b55aSArtem Bityutskiy return ubi_open_volume(ubi_num, vol_id, mode); 320b531b55aSArtem Bityutskiy return ERR_PTR(-ENODEV); 321b5710284SCorentin Chary } 322b5710284SCorentin Chary EXPORT_SYMBOL_GPL(ubi_open_volume_path); 323b5710284SCorentin Chary 324b5710284SCorentin Chary /** 325801c135cSArtem B. Bityutskiy * ubi_close_volume - close UBI volume. 326801c135cSArtem B. Bityutskiy * @desc: volume descriptor 327801c135cSArtem B. Bityutskiy */ 328801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc) 329801c135cSArtem B. Bityutskiy { 330801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 331e73f4459SArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 332801c135cSArtem B. Bityutskiy 333e1cf7e6dSArtem Bityutskiy dbg_gen("close device %d, volume %d, mode %d", 334e1cf7e6dSArtem Bityutskiy ubi->ubi_num, vol->vol_id, desc->mode); 335801c135cSArtem B. Bityutskiy 336e73f4459SArtem Bityutskiy spin_lock(&ubi->volumes_lock); 337801c135cSArtem B. Bityutskiy switch (desc->mode) { 338801c135cSArtem B. Bityutskiy case UBI_READONLY: 339801c135cSArtem B. Bityutskiy vol->readers -= 1; 340801c135cSArtem B. Bityutskiy break; 341801c135cSArtem B. Bityutskiy case UBI_READWRITE: 342801c135cSArtem B. Bityutskiy vol->writers -= 1; 343801c135cSArtem B. Bityutskiy break; 344801c135cSArtem B. Bityutskiy case UBI_EXCLUSIVE: 345801c135cSArtem B. Bityutskiy vol->exclusive = 0; 346801c135cSArtem B. Bityutskiy } 347d05c77a8SArtem Bityutskiy vol->ref_count -= 1; 348e73f4459SArtem Bityutskiy spin_unlock(&ubi->volumes_lock); 349801c135cSArtem B. Bityutskiy 350d05c77a8SArtem Bityutskiy kfree(desc); 351e73f4459SArtem Bityutskiy put_device(&vol->dev); 352e73f4459SArtem Bityutskiy ubi_put_device(ubi); 353801c135cSArtem B. Bityutskiy module_put(THIS_MODULE); 354801c135cSArtem B. Bityutskiy } 355801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_close_volume); 356801c135cSArtem B. Bityutskiy 357801c135cSArtem B. Bityutskiy /** 358801c135cSArtem B. Bityutskiy * ubi_leb_read - read data. 359801c135cSArtem B. Bityutskiy * @desc: volume descriptor 360801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to read from 361801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 362801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock to read from 363801c135cSArtem B. Bityutskiy * @len: how many bytes to read 364801c135cSArtem B. Bityutskiy * @check: whether UBI has to check the read data's CRC or not. 365801c135cSArtem B. Bityutskiy * 366801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of logical eraseblock @lnum and 367801c135cSArtem B. Bityutskiy * stores the data at @buf. When reading from static volumes, @check specifies 368801c135cSArtem B. Bityutskiy * whether the data has to be checked or not. If yes, the whole logical 369801c135cSArtem B. Bityutskiy * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC 370801c135cSArtem B. Bityutskiy * checksum is per-eraseblock). So checking may substantially slow down the 371801c135cSArtem B. Bityutskiy * read speed. The @check argument is ignored for dynamic volumes. 372801c135cSArtem B. Bityutskiy * 373801c135cSArtem B. Bityutskiy * In case of success, this function returns zero. In case of failure, this 374801c135cSArtem B. Bityutskiy * function returns a negative error code. 375801c135cSArtem B. Bityutskiy * 376801c135cSArtem B. Bityutskiy * %-EBADMSG error code is returned: 377801c135cSArtem B. Bityutskiy * o for both static and dynamic volumes if MTD driver has detected a data 378801c135cSArtem B. Bityutskiy * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); 379801c135cSArtem B. Bityutskiy * o for static volumes in case of data CRC mismatch. 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 error code. 383801c135cSArtem B. Bityutskiy */ 384801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 385801c135cSArtem B. Bityutskiy int len, int check) 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 err, vol_id = vol->vol_id; 390801c135cSArtem B. Bityutskiy 391c8566350SArtem Bityutskiy dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); 392801c135cSArtem B. Bityutskiy 393801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || 394801c135cSArtem B. Bityutskiy lnum >= vol->used_ebs || offset < 0 || len < 0 || 395801c135cSArtem B. Bityutskiy offset + len > vol->usable_leb_size) 396801c135cSArtem B. Bityutskiy return -EINVAL; 397801c135cSArtem B. Bityutskiy 3984ab60a0dSArtem Bityutskiy if (vol->vol_type == UBI_STATIC_VOLUME) { 3994ab60a0dSArtem Bityutskiy if (vol->used_ebs == 0) 4004ab60a0dSArtem Bityutskiy /* Empty static UBI volume */ 4014ab60a0dSArtem Bityutskiy return 0; 4024ab60a0dSArtem Bityutskiy if (lnum == vol->used_ebs - 1 && 403801c135cSArtem B. Bityutskiy offset + len > vol->last_eb_bytes) 404801c135cSArtem B. Bityutskiy return -EINVAL; 4054ab60a0dSArtem Bityutskiy } 406801c135cSArtem B. Bityutskiy 407801c135cSArtem B. Bityutskiy if (vol->upd_marker) 408801c135cSArtem B. Bityutskiy return -EBADF; 409801c135cSArtem B. Bityutskiy if (len == 0) 410801c135cSArtem B. Bityutskiy return 0; 411801c135cSArtem B. Bityutskiy 41289b96b69SArtem Bityutskiy err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); 413801c135cSArtem B. Bityutskiy if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { 414801c135cSArtem B. Bityutskiy ubi_warn("mark volume %d as corrupted", vol_id); 415801c135cSArtem B. Bityutskiy vol->corrupted = 1; 416801c135cSArtem B. Bityutskiy } 417801c135cSArtem B. Bityutskiy 418801c135cSArtem B. Bityutskiy return err; 419801c135cSArtem B. Bityutskiy } 420801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_read); 421801c135cSArtem B. Bityutskiy 422801c135cSArtem B. Bityutskiy /** 423801c135cSArtem B. Bityutskiy * ubi_leb_write - write data. 424801c135cSArtem B. Bityutskiy * @desc: volume descriptor 425801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to write to 426801c135cSArtem B. Bityutskiy * @buf: data to write 427801c135cSArtem B. Bityutskiy * @offset: offset within the logical eraseblock where to write 428801c135cSArtem B. Bityutskiy * @len: how many bytes to write 429801c135cSArtem B. Bityutskiy * @dtype: expected data type 430801c135cSArtem B. Bityutskiy * 431801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 432801c135cSArtem B. Bityutskiy * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 433801c135cSArtem B. Bityutskiy * the data. 434801c135cSArtem B. Bityutskiy * 435801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 436801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 437801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 438801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 439801c135cSArtem B. Bityutskiy * 440801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 441801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 442801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 443801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 444801c135cSArtem B. Bityutskiy * garbage. 445801c135cSArtem B. Bityutskiy * 446801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 447801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 448801c135cSArtem B. Bityutskiy */ 449801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 450801c135cSArtem B. Bityutskiy int offset, int len, int dtype) 451801c135cSArtem B. Bityutskiy { 452801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 453801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 454801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 455801c135cSArtem B. Bityutskiy 456c8566350SArtem Bityutskiy dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 457801c135cSArtem B. Bityutskiy 458801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 459801c135cSArtem B. Bityutskiy return -EINVAL; 460801c135cSArtem B. Bityutskiy 461801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 462801c135cSArtem B. Bityutskiy return -EROFS; 463801c135cSArtem B. Bityutskiy 464801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 465cadb40ccSKyungmin Park offset + len > vol->usable_leb_size || 466cadb40ccSKyungmin Park offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) 467801c135cSArtem B. Bityutskiy return -EINVAL; 468801c135cSArtem B. Bityutskiy 469801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 470801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 471801c135cSArtem B. Bityutskiy return -EINVAL; 472801c135cSArtem B. Bityutskiy 473801c135cSArtem B. Bityutskiy if (vol->upd_marker) 474801c135cSArtem B. Bityutskiy return -EBADF; 475801c135cSArtem B. Bityutskiy 476801c135cSArtem B. Bityutskiy if (len == 0) 477801c135cSArtem B. Bityutskiy return 0; 478801c135cSArtem B. Bityutskiy 47989b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 480801c135cSArtem B. Bityutskiy } 481801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 482801c135cSArtem B. Bityutskiy 483801c135cSArtem B. Bityutskiy /* 484801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 485801c135cSArtem B. Bityutskiy * @desc: volume descriptor 486801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 487801c135cSArtem B. Bityutskiy * @buf: data to write 488801c135cSArtem B. Bityutskiy * @len: how many bytes to write 489801c135cSArtem B. Bityutskiy * @dtype: expected data type 490801c135cSArtem B. Bityutskiy * 491801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 492801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 4933f502622SShinya Kuribayashi * data, which has to be aligned. The length may be shorter than the logical 494801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 495801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 496801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 497801c135cSArtem B. Bityutskiy * code in case of failure. 498801c135cSArtem B. Bityutskiy */ 499801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 500801c135cSArtem B. Bityutskiy int len, int dtype) 501801c135cSArtem B. Bityutskiy { 502801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 503801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 504801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 505801c135cSArtem B. Bityutskiy 506c8566350SArtem Bityutskiy dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 507801c135cSArtem B. Bityutskiy 508801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 509801c135cSArtem B. Bityutskiy return -EINVAL; 510801c135cSArtem B. Bityutskiy 511801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 512801c135cSArtem B. Bityutskiy return -EROFS; 513801c135cSArtem B. Bityutskiy 514801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 515cadb40ccSKyungmin Park len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) 516801c135cSArtem B. Bityutskiy return -EINVAL; 517801c135cSArtem B. Bityutskiy 518801c135cSArtem B. Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 519801c135cSArtem B. Bityutskiy dtype != UBI_UNKNOWN) 520801c135cSArtem B. Bityutskiy return -EINVAL; 521801c135cSArtem B. Bityutskiy 522801c135cSArtem B. Bityutskiy if (vol->upd_marker) 523801c135cSArtem B. Bityutskiy return -EBADF; 524801c135cSArtem B. Bityutskiy 525801c135cSArtem B. Bityutskiy if (len == 0) 526801c135cSArtem B. Bityutskiy return 0; 527801c135cSArtem B. Bityutskiy 52889b96b69SArtem Bityutskiy return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 529801c135cSArtem B. Bityutskiy } 530801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 531801c135cSArtem B. Bityutskiy 532801c135cSArtem B. Bityutskiy /** 533801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 534801c135cSArtem B. Bityutskiy * @desc: volume descriptor 535801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 536801c135cSArtem B. Bityutskiy * 537801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 538801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 539801c135cSArtem B. Bityutskiy * negative error code in case of failure. 540801c135cSArtem B. Bityutskiy * 541801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 542801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 543801c135cSArtem B. Bityutskiy */ 544801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 545801c135cSArtem B. Bityutskiy { 546801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 547801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 548ae616e1bSArtem Bityutskiy int err; 549801c135cSArtem B. Bityutskiy 550c8566350SArtem Bityutskiy dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); 551801c135cSArtem B. Bityutskiy 552801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 553801c135cSArtem B. Bityutskiy return -EROFS; 554801c135cSArtem B. Bityutskiy 555801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 556801c135cSArtem B. Bityutskiy return -EINVAL; 557801c135cSArtem B. Bityutskiy 558801c135cSArtem B. Bityutskiy if (vol->upd_marker) 559801c135cSArtem B. Bityutskiy return -EBADF; 560801c135cSArtem B. Bityutskiy 56189b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 562801c135cSArtem B. Bityutskiy if (err) 563801c135cSArtem B. Bityutskiy return err; 564801c135cSArtem B. Bityutskiy 565801c135cSArtem B. Bityutskiy return ubi_wl_flush(ubi); 566801c135cSArtem B. Bityutskiy } 567801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 568801c135cSArtem B. Bityutskiy 569801c135cSArtem B. Bityutskiy /** 570801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 571801c135cSArtem B. Bityutskiy * @desc: volume descriptor 572801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 573801c135cSArtem B. Bityutskiy * 574801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 575801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 5763f502622SShinya Kuribayashi * physically erased in background. This operation is much faster than the 577801c135cSArtem B. Bityutskiy * erase operation. 578801c135cSArtem B. Bityutskiy * 579801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 580801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 581801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 582801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 583801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 584801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 585801c135cSArtem B. Bityutskiy * used with care. 586801c135cSArtem B. Bityutskiy * 587801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 588801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 589801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 590801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 591801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 592801c135cSArtem B. Bityutskiy * 593801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 594801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 5953f502622SShinya Kuribayashi * first un-map it, then write new data, rather than first erase it, then write 596801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 597801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 598801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 599801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 600801c135cSArtem B. Bityutskiy * 601801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 602801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 603801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 604801c135cSArtem B. Bityutskiy */ 605801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 606801c135cSArtem B. Bityutskiy { 607801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 608801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 609801c135cSArtem B. Bityutskiy 610c8566350SArtem Bityutskiy dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); 611801c135cSArtem B. Bityutskiy 612801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 613801c135cSArtem B. Bityutskiy return -EROFS; 614801c135cSArtem B. Bityutskiy 615801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 616801c135cSArtem B. Bityutskiy return -EINVAL; 617801c135cSArtem B. Bityutskiy 618801c135cSArtem B. Bityutskiy if (vol->upd_marker) 619801c135cSArtem B. Bityutskiy return -EBADF; 620801c135cSArtem B. Bityutskiy 62189b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 622801c135cSArtem B. Bityutskiy } 623801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 624801c135cSArtem B. Bityutskiy 625801c135cSArtem B. Bityutskiy /** 6260e0ee1ccSDmitry Pervushin * ubi_leb_map - map logical eraseblock to a physical eraseblock. 627393852ecSArtem Bityutskiy * @desc: volume descriptor 628393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 629393852ecSArtem Bityutskiy * @dtype: expected data type 630393852ecSArtem Bityutskiy * 631393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 63273ac36eaSColy Li * eraseblock. This means, that after a successful invocation of this 633393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 634393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 635393852ecSArtem Bityutskiy * happens. 636393852ecSArtem Bityutskiy * 637393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 638393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 639393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 640393852ecSArtem Bityutskiy * other failures. 641393852ecSArtem Bityutskiy */ 642393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 643393852ecSArtem Bityutskiy { 644393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 645393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 646393852ecSArtem Bityutskiy 647c8566350SArtem Bityutskiy dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); 648393852ecSArtem Bityutskiy 649393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 650393852ecSArtem Bityutskiy return -EROFS; 651393852ecSArtem Bityutskiy 652393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 653393852ecSArtem Bityutskiy return -EINVAL; 654393852ecSArtem Bityutskiy 655393852ecSArtem Bityutskiy if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && 656393852ecSArtem Bityutskiy dtype != UBI_UNKNOWN) 657393852ecSArtem Bityutskiy return -EINVAL; 658393852ecSArtem Bityutskiy 659393852ecSArtem Bityutskiy if (vol->upd_marker) 660393852ecSArtem Bityutskiy return -EBADF; 661393852ecSArtem Bityutskiy 662393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 663393852ecSArtem Bityutskiy return -EBADMSG; 664393852ecSArtem Bityutskiy 66589b96b69SArtem Bityutskiy return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 666393852ecSArtem Bityutskiy } 667393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 668393852ecSArtem Bityutskiy 669393852ecSArtem Bityutskiy /** 670801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 671801c135cSArtem B. Bityutskiy * @desc: volume descriptor 672801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 673801c135cSArtem B. Bityutskiy * 674801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 675801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 676801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 677801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 678801c135cSArtem B. Bityutskiy * mapped to. 679801c135cSArtem B. Bityutskiy * 680801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 681801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 682801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 683801c135cSArtem B. Bityutskiy * code. 684801c135cSArtem B. Bityutskiy */ 685801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 686801c135cSArtem B. Bityutskiy { 687801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 688801c135cSArtem B. Bityutskiy 689c8566350SArtem Bityutskiy dbg_gen("test LEB %d:%d", vol->vol_id, lnum); 690801c135cSArtem B. Bityutskiy 691801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 692801c135cSArtem B. Bityutskiy return -EINVAL; 693801c135cSArtem B. Bityutskiy 694801c135cSArtem B. Bityutskiy if (vol->upd_marker) 695801c135cSArtem B. Bityutskiy return -EBADF; 696801c135cSArtem B. Bityutskiy 697801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 698801c135cSArtem B. Bityutskiy } 699801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 700a5bf6190SArtem Bityutskiy 701a5bf6190SArtem Bityutskiy /** 702a5bf6190SArtem Bityutskiy * ubi_sync - synchronize UBI device buffers. 703a5bf6190SArtem Bityutskiy * @ubi_num: UBI device to synchronize 704a5bf6190SArtem Bityutskiy * 705a5bf6190SArtem Bityutskiy * The underlying MTD device may cache data in hardware or in software. This 706a5bf6190SArtem Bityutskiy * function ensures the caches are flushed. Returns zero in case of success and 707a5bf6190SArtem Bityutskiy * a negative error code in case of failure. 708a5bf6190SArtem Bityutskiy */ 709a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num) 710a5bf6190SArtem Bityutskiy { 711a5bf6190SArtem Bityutskiy struct ubi_device *ubi; 712a5bf6190SArtem Bityutskiy 713a5bf6190SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 714a5bf6190SArtem Bityutskiy if (!ubi) 715a5bf6190SArtem Bityutskiy return -ENODEV; 716a5bf6190SArtem Bityutskiy 717a5bf6190SArtem Bityutskiy if (ubi->mtd->sync) 718a5bf6190SArtem Bityutskiy ubi->mtd->sync(ubi->mtd); 719a5bf6190SArtem Bityutskiy 720a5bf6190SArtem Bityutskiy ubi_put_device(ubi); 721a5bf6190SArtem Bityutskiy return 0; 722a5bf6190SArtem Bityutskiy } 723a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync); 7240e0ee1ccSDmitry Pervushin 7250e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers); 7260e0ee1ccSDmitry Pervushin 7270e0ee1ccSDmitry Pervushin /** 7280e0ee1ccSDmitry Pervushin * ubi_register_volume_notifier - register a volume notifier. 7290e0ee1ccSDmitry Pervushin * @nb: the notifier description object 7300e0ee1ccSDmitry Pervushin * @ignore_existing: if non-zero, do not send "added" notification for all 7310e0ee1ccSDmitry Pervushin * already existing volumes 7320e0ee1ccSDmitry Pervushin * 7330e0ee1ccSDmitry Pervushin * This function registers a volume notifier, which means that 7340e0ee1ccSDmitry Pervushin * 'nb->notifier_call()' will be invoked when an UBI volume is created, 7350e0ee1ccSDmitry Pervushin * removed, re-sized, re-named, or updated. The first argument of the function 7360e0ee1ccSDmitry Pervushin * is the notification type. The second argument is pointer to a 7370e0ee1ccSDmitry Pervushin * &struct ubi_notification object which describes the notification event. 7380e0ee1ccSDmitry Pervushin * Using UBI API from the volume notifier is prohibited. 7390e0ee1ccSDmitry Pervushin * 7400e0ee1ccSDmitry Pervushin * This function returns zero in case of success and a negative error code 7410e0ee1ccSDmitry Pervushin * in case of failure. 7420e0ee1ccSDmitry Pervushin */ 7430e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb, 7440e0ee1ccSDmitry Pervushin int ignore_existing) 7450e0ee1ccSDmitry Pervushin { 7460e0ee1ccSDmitry Pervushin int err; 7470e0ee1ccSDmitry Pervushin 7480e0ee1ccSDmitry Pervushin err = blocking_notifier_chain_register(&ubi_notifiers, nb); 7490e0ee1ccSDmitry Pervushin if (err != 0) 7500e0ee1ccSDmitry Pervushin return err; 7510e0ee1ccSDmitry Pervushin if (ignore_existing) 7520e0ee1ccSDmitry Pervushin return 0; 7530e0ee1ccSDmitry Pervushin 7540e0ee1ccSDmitry Pervushin /* 7550e0ee1ccSDmitry Pervushin * We are going to walk all UBI devices and all volumes, and 7560e0ee1ccSDmitry Pervushin * notify the user about existing volumes by the %UBI_VOLUME_ADDED 7570e0ee1ccSDmitry Pervushin * event. We have to lock the @ubi_devices_mutex to make sure UBI 7580e0ee1ccSDmitry Pervushin * devices do not disappear. 7590e0ee1ccSDmitry Pervushin */ 7600e0ee1ccSDmitry Pervushin mutex_lock(&ubi_devices_mutex); 7610e0ee1ccSDmitry Pervushin ubi_enumerate_volumes(nb); 7620e0ee1ccSDmitry Pervushin mutex_unlock(&ubi_devices_mutex); 7630e0ee1ccSDmitry Pervushin 7640e0ee1ccSDmitry Pervushin return err; 7650e0ee1ccSDmitry Pervushin } 7660e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier); 7670e0ee1ccSDmitry Pervushin 7680e0ee1ccSDmitry Pervushin /** 7690e0ee1ccSDmitry Pervushin * ubi_unregister_volume_notifier - unregister the volume notifier. 7700e0ee1ccSDmitry Pervushin * @nb: the notifier description object 7710e0ee1ccSDmitry Pervushin * 7720e0ee1ccSDmitry Pervushin * This function unregisters volume notifier @nm and returns zero in case of 7730e0ee1ccSDmitry Pervushin * success and a negative error code in case of failure. 7740e0ee1ccSDmitry Pervushin */ 7750e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb) 7760e0ee1ccSDmitry Pervushin { 7770e0ee1ccSDmitry Pervushin return blocking_notifier_chain_unregister(&ubi_notifiers, nb); 7780e0ee1ccSDmitry Pervushin } 7790e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier); 780