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; 43f43ec882SArtem 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) { 207*32608703STanya Brokhman ubi_warn(ubi, "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); 224*32608703STanya Brokhman ubi_err(ubi, "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); 413d57f4054SBrian Norris if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) { 414*32608703STanya Brokhman ubi_warn(ubi, "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 * 430801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from @buf to offset @offset of 431b36a261eSRichard Weinberger * logical eraseblock @lnum. 432801c135cSArtem B. Bityutskiy * 433801c135cSArtem B. Bityutskiy * This function takes care of physical eraseblock write failures. If write to 434801c135cSArtem B. Bityutskiy * the physical eraseblock write operation fails, the logical eraseblock is 435801c135cSArtem B. Bityutskiy * re-mapped to another physical eraseblock, the data is recovered, and the 436801c135cSArtem B. Bityutskiy * write finishes. UBI has a pool of reserved physical eraseblocks for this. 437801c135cSArtem B. Bityutskiy * 438801c135cSArtem B. Bityutskiy * If all the data were successfully written, zero is returned. If an error 439801c135cSArtem B. Bityutskiy * occurred and UBI has not been able to recover from it, this function returns 440801c135cSArtem B. Bityutskiy * a negative error code. Note, in case of an error, it is possible that 441801c135cSArtem B. Bityutskiy * something was still written to the flash media, but that may be some 442801c135cSArtem B. Bityutskiy * garbage. 443801c135cSArtem B. Bityutskiy * 444801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 445801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 446801c135cSArtem B. Bityutskiy */ 447801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 448b36a261eSRichard Weinberger int offset, int len) 449801c135cSArtem B. Bityutskiy { 450801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 451801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 452801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 453801c135cSArtem B. Bityutskiy 454c8566350SArtem Bityutskiy dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); 455801c135cSArtem B. Bityutskiy 456801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 457801c135cSArtem B. Bityutskiy return -EINVAL; 458801c135cSArtem B. Bityutskiy 459801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 460801c135cSArtem B. Bityutskiy return -EROFS; 461801c135cSArtem B. Bityutskiy 462801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || 463cadb40ccSKyungmin Park offset + len > vol->usable_leb_size || 464cadb40ccSKyungmin Park offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) 465801c135cSArtem B. Bityutskiy return -EINVAL; 466801c135cSArtem B. Bityutskiy 467801c135cSArtem B. Bityutskiy if (vol->upd_marker) 468801c135cSArtem B. Bityutskiy return -EBADF; 469801c135cSArtem B. Bityutskiy 470801c135cSArtem B. Bityutskiy if (len == 0) 471801c135cSArtem B. Bityutskiy return 0; 472801c135cSArtem B. Bityutskiy 473b36a261eSRichard Weinberger return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len); 474801c135cSArtem B. Bityutskiy } 475801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_write); 476801c135cSArtem B. Bityutskiy 477801c135cSArtem B. Bityutskiy /* 478801c135cSArtem B. Bityutskiy * ubi_leb_change - change logical eraseblock atomically. 479801c135cSArtem B. Bityutskiy * @desc: volume descriptor 480801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number to change 481801c135cSArtem B. Bityutskiy * @buf: data to write 482801c135cSArtem B. Bityutskiy * @len: how many bytes to write 483801c135cSArtem B. Bityutskiy * 484801c135cSArtem B. Bityutskiy * This function changes the contents of a logical eraseblock atomically. @buf 485801c135cSArtem B. Bityutskiy * has to contain new logical eraseblock data, and @len - the length of the 4863f502622SShinya Kuribayashi * data, which has to be aligned. The length may be shorter than the logical 487801c135cSArtem B. Bityutskiy * eraseblock size, ant the logical eraseblock may be appended to more times 488801c135cSArtem B. Bityutskiy * later on. This function guarantees that in case of an unclean reboot the old 489801c135cSArtem B. Bityutskiy * contents is preserved. Returns zero in case of success and a negative error 490801c135cSArtem B. Bityutskiy * code in case of failure. 491801c135cSArtem B. Bityutskiy */ 492801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 493b36a261eSRichard Weinberger int len) 494801c135cSArtem B. Bityutskiy { 495801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 496801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 497801c135cSArtem B. Bityutskiy int vol_id = vol->vol_id; 498801c135cSArtem B. Bityutskiy 499c8566350SArtem Bityutskiy dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); 500801c135cSArtem B. Bityutskiy 501801c135cSArtem B. Bityutskiy if (vol_id < 0 || vol_id >= ubi->vtbl_slots) 502801c135cSArtem B. Bityutskiy return -EINVAL; 503801c135cSArtem B. Bityutskiy 504801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 505801c135cSArtem B. Bityutskiy return -EROFS; 506801c135cSArtem B. Bityutskiy 507801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || 508cadb40ccSKyungmin Park len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) 509801c135cSArtem B. Bityutskiy return -EINVAL; 510801c135cSArtem B. Bityutskiy 511801c135cSArtem B. Bityutskiy if (vol->upd_marker) 512801c135cSArtem B. Bityutskiy return -EBADF; 513801c135cSArtem B. Bityutskiy 514801c135cSArtem B. Bityutskiy if (len == 0) 515801c135cSArtem B. Bityutskiy return 0; 516801c135cSArtem B. Bityutskiy 517b36a261eSRichard Weinberger return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len); 518801c135cSArtem B. Bityutskiy } 519801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_change); 520801c135cSArtem B. Bityutskiy 521801c135cSArtem B. Bityutskiy /** 522801c135cSArtem B. Bityutskiy * ubi_leb_erase - erase logical eraseblock. 523801c135cSArtem B. Bityutskiy * @desc: volume descriptor 524801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 525801c135cSArtem B. Bityutskiy * 526801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and synchronously erases the 527801c135cSArtem B. Bityutskiy * correspondent physical eraseblock. Returns zero in case of success and a 528801c135cSArtem B. Bityutskiy * negative error code in case of failure. 529801c135cSArtem B. Bityutskiy * 530801c135cSArtem B. Bityutskiy * If the volume is damaged because of an interrupted update this function just 531801c135cSArtem B. Bityutskiy * returns immediately with %-EBADF code. 532801c135cSArtem B. Bityutskiy */ 533801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) 534801c135cSArtem B. Bityutskiy { 535801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 536801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 537ae616e1bSArtem Bityutskiy int err; 538801c135cSArtem B. Bityutskiy 539c8566350SArtem Bityutskiy dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); 540801c135cSArtem B. Bityutskiy 541801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 542801c135cSArtem B. Bityutskiy return -EROFS; 543801c135cSArtem B. Bityutskiy 544801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 545801c135cSArtem B. Bityutskiy return -EINVAL; 546801c135cSArtem B. Bityutskiy 547801c135cSArtem B. Bityutskiy if (vol->upd_marker) 548801c135cSArtem B. Bityutskiy return -EBADF; 549801c135cSArtem B. Bityutskiy 55089b96b69SArtem Bityutskiy err = ubi_eba_unmap_leb(ubi, vol, lnum); 551801c135cSArtem B. Bityutskiy if (err) 552801c135cSArtem B. Bityutskiy return err; 553801c135cSArtem B. Bityutskiy 55462f38455SJoel Reardon return ubi_wl_flush(ubi, vol->vol_id, lnum); 555801c135cSArtem B. Bityutskiy } 556801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_erase); 557801c135cSArtem B. Bityutskiy 558801c135cSArtem B. Bityutskiy /** 559801c135cSArtem B. Bityutskiy * ubi_leb_unmap - un-map logical eraseblock. 560801c135cSArtem B. Bityutskiy * @desc: volume descriptor 561801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 562801c135cSArtem B. Bityutskiy * 563801c135cSArtem B. Bityutskiy * This function un-maps logical eraseblock @lnum and schedules the 564801c135cSArtem B. Bityutskiy * corresponding physical eraseblock for erasure, so that it will eventually be 5653f502622SShinya Kuribayashi * physically erased in background. This operation is much faster than the 566801c135cSArtem B. Bityutskiy * erase operation. 567801c135cSArtem B. Bityutskiy * 568801c135cSArtem B. Bityutskiy * Unlike erase, the un-map operation does not guarantee that the logical 569801c135cSArtem B. Bityutskiy * eraseblock will contain all 0xFF bytes when UBI is initialized again. For 570801c135cSArtem B. Bityutskiy * example, if several logical eraseblocks are un-mapped, and an unclean reboot 571801c135cSArtem B. Bityutskiy * happens after this, the logical eraseblocks will not necessarily be 572801c135cSArtem B. Bityutskiy * un-mapped again when this MTD device is attached. They may actually be 573801c135cSArtem B. Bityutskiy * mapped to the same physical eraseblocks again. So, this function has to be 574801c135cSArtem B. Bityutskiy * used with care. 575801c135cSArtem B. Bityutskiy * 576801c135cSArtem B. Bityutskiy * In other words, when un-mapping a logical eraseblock, UBI does not store 577801c135cSArtem B. Bityutskiy * any information about this on the flash media, it just marks the logical 578801c135cSArtem B. Bityutskiy * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical 579801c135cSArtem B. Bityutskiy * eraseblock is physically erased, it will be mapped again to the same logical 580801c135cSArtem B. Bityutskiy * eraseblock when the MTD device is attached again. 581801c135cSArtem B. Bityutskiy * 582801c135cSArtem B. Bityutskiy * The main and obvious use-case of this function is when the contents of a 583801c135cSArtem B. Bityutskiy * logical eraseblock has to be re-written. Then it is much more efficient to 5843f502622SShinya Kuribayashi * first un-map it, then write new data, rather than first erase it, then write 585801c135cSArtem B. Bityutskiy * new data. Note, once new data has been written to the logical eraseblock, 586801c135cSArtem B. Bityutskiy * UBI guarantees that the old contents has gone forever. In other words, if an 587801c135cSArtem B. Bityutskiy * unclean reboot happens after the logical eraseblock has been un-mapped and 588801c135cSArtem B. Bityutskiy * then written to, it will contain the last written data. 589801c135cSArtem B. Bityutskiy * 590801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 591801c135cSArtem B. Bityutskiy * case of failure. If the volume is damaged because of an interrupted update 592801c135cSArtem B. Bityutskiy * this function just returns immediately with %-EBADF code. 593801c135cSArtem B. Bityutskiy */ 594801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) 595801c135cSArtem B. Bityutskiy { 596801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 597801c135cSArtem B. Bityutskiy struct ubi_device *ubi = vol->ubi; 598801c135cSArtem B. Bityutskiy 599c8566350SArtem Bityutskiy dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); 600801c135cSArtem B. Bityutskiy 601801c135cSArtem B. Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 602801c135cSArtem B. Bityutskiy return -EROFS; 603801c135cSArtem B. Bityutskiy 604801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 605801c135cSArtem B. Bityutskiy return -EINVAL; 606801c135cSArtem B. Bityutskiy 607801c135cSArtem B. Bityutskiy if (vol->upd_marker) 608801c135cSArtem B. Bityutskiy return -EBADF; 609801c135cSArtem B. Bityutskiy 61089b96b69SArtem Bityutskiy return ubi_eba_unmap_leb(ubi, vol, lnum); 611801c135cSArtem B. Bityutskiy } 612801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_unmap); 613801c135cSArtem B. Bityutskiy 614801c135cSArtem B. Bityutskiy /** 6150e0ee1ccSDmitry Pervushin * ubi_leb_map - map logical eraseblock to a physical eraseblock. 616393852ecSArtem Bityutskiy * @desc: volume descriptor 617393852ecSArtem Bityutskiy * @lnum: logical eraseblock number 618393852ecSArtem Bityutskiy * 619393852ecSArtem Bityutskiy * This function maps an un-mapped logical eraseblock @lnum to a physical 62073ac36eaSColy Li * eraseblock. This means, that after a successful invocation of this 621393852ecSArtem Bityutskiy * function the logical eraseblock @lnum will be empty (contain only %0xFF 622393852ecSArtem Bityutskiy * bytes) and be mapped to a physical eraseblock, even if an unclean reboot 623393852ecSArtem Bityutskiy * happens. 624393852ecSArtem Bityutskiy * 625393852ecSArtem Bityutskiy * This function returns zero in case of success, %-EBADF if the volume is 626393852ecSArtem Bityutskiy * damaged because of an interrupted update, %-EBADMSG if the logical 627393852ecSArtem Bityutskiy * eraseblock is already mapped, and other negative error codes in case of 628393852ecSArtem Bityutskiy * other failures. 629393852ecSArtem Bityutskiy */ 630b36a261eSRichard Weinberger int ubi_leb_map(struct ubi_volume_desc *desc, int lnum) 631393852ecSArtem Bityutskiy { 632393852ecSArtem Bityutskiy struct ubi_volume *vol = desc->vol; 633393852ecSArtem Bityutskiy struct ubi_device *ubi = vol->ubi; 634393852ecSArtem Bityutskiy 635c8566350SArtem Bityutskiy dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); 636393852ecSArtem Bityutskiy 637393852ecSArtem Bityutskiy if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) 638393852ecSArtem Bityutskiy return -EROFS; 639393852ecSArtem Bityutskiy 640393852ecSArtem Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 641393852ecSArtem Bityutskiy return -EINVAL; 642393852ecSArtem Bityutskiy 643393852ecSArtem Bityutskiy if (vol->upd_marker) 644393852ecSArtem Bityutskiy return -EBADF; 645393852ecSArtem Bityutskiy 646393852ecSArtem Bityutskiy if (vol->eba_tbl[lnum] >= 0) 647393852ecSArtem Bityutskiy return -EBADMSG; 648393852ecSArtem Bityutskiy 649b36a261eSRichard Weinberger return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); 650393852ecSArtem Bityutskiy } 651393852ecSArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_leb_map); 652393852ecSArtem Bityutskiy 653393852ecSArtem Bityutskiy /** 654801c135cSArtem B. Bityutskiy * ubi_is_mapped - check if logical eraseblock is mapped. 655801c135cSArtem B. Bityutskiy * @desc: volume descriptor 656801c135cSArtem B. Bityutskiy * @lnum: logical eraseblock number 657801c135cSArtem B. Bityutskiy * 658801c135cSArtem B. Bityutskiy * This function checks if logical eraseblock @lnum is mapped to a physical 659801c135cSArtem B. Bityutskiy * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily 660801c135cSArtem B. Bityutskiy * mean it will still be un-mapped after the UBI device is re-attached. The 661801c135cSArtem B. Bityutskiy * logical eraseblock may become mapped to the physical eraseblock it was last 662801c135cSArtem B. Bityutskiy * mapped to. 663801c135cSArtem B. Bityutskiy * 664801c135cSArtem B. Bityutskiy * This function returns %1 if the LEB is mapped, %0 if not, and a negative 665801c135cSArtem B. Bityutskiy * error code in case of failure. If the volume is damaged because of an 666801c135cSArtem B. Bityutskiy * interrupted update this function just returns immediately with %-EBADF error 667801c135cSArtem B. Bityutskiy * code. 668801c135cSArtem B. Bityutskiy */ 669801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) 670801c135cSArtem B. Bityutskiy { 671801c135cSArtem B. Bityutskiy struct ubi_volume *vol = desc->vol; 672801c135cSArtem B. Bityutskiy 673c8566350SArtem Bityutskiy dbg_gen("test LEB %d:%d", vol->vol_id, lnum); 674801c135cSArtem B. Bityutskiy 675801c135cSArtem B. Bityutskiy if (lnum < 0 || lnum >= vol->reserved_pebs) 676801c135cSArtem B. Bityutskiy return -EINVAL; 677801c135cSArtem B. Bityutskiy 678801c135cSArtem B. Bityutskiy if (vol->upd_marker) 679801c135cSArtem B. Bityutskiy return -EBADF; 680801c135cSArtem B. Bityutskiy 681801c135cSArtem B. Bityutskiy return vol->eba_tbl[lnum] >= 0; 682801c135cSArtem B. Bityutskiy } 683801c135cSArtem B. Bityutskiy EXPORT_SYMBOL_GPL(ubi_is_mapped); 684a5bf6190SArtem Bityutskiy 685a5bf6190SArtem Bityutskiy /** 686a5bf6190SArtem Bityutskiy * ubi_sync - synchronize UBI device buffers. 687a5bf6190SArtem Bityutskiy * @ubi_num: UBI device to synchronize 688a5bf6190SArtem Bityutskiy * 689a5bf6190SArtem Bityutskiy * The underlying MTD device may cache data in hardware or in software. This 690a5bf6190SArtem Bityutskiy * function ensures the caches are flushed. Returns zero in case of success and 691a5bf6190SArtem Bityutskiy * a negative error code in case of failure. 692a5bf6190SArtem Bityutskiy */ 693a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num) 694a5bf6190SArtem Bityutskiy { 695a5bf6190SArtem Bityutskiy struct ubi_device *ubi; 696a5bf6190SArtem Bityutskiy 697a5bf6190SArtem Bityutskiy ubi = ubi_get_device(ubi_num); 698a5bf6190SArtem Bityutskiy if (!ubi) 699a5bf6190SArtem Bityutskiy return -ENODEV; 700a5bf6190SArtem Bityutskiy 70185f2f2a8SArtem Bityutskiy mtd_sync(ubi->mtd); 702a5bf6190SArtem Bityutskiy ubi_put_device(ubi); 703a5bf6190SArtem Bityutskiy return 0; 704a5bf6190SArtem Bityutskiy } 705a5bf6190SArtem Bityutskiy EXPORT_SYMBOL_GPL(ubi_sync); 7060e0ee1ccSDmitry Pervushin 70762f38455SJoel Reardon /** 70862f38455SJoel Reardon * ubi_flush - flush UBI work queue. 70962f38455SJoel Reardon * @ubi_num: UBI device to flush work queue 71062f38455SJoel Reardon * @vol_id: volume id to flush for 71162f38455SJoel Reardon * @lnum: logical eraseblock number to flush for 71262f38455SJoel Reardon * 71362f38455SJoel Reardon * This function executes all pending works for a particular volume id / logical 71462f38455SJoel Reardon * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as 71562f38455SJoel Reardon * a wildcard for all of the corresponding volume numbers or logical 71662f38455SJoel Reardon * eraseblock numbers. It returns zero in case of success and a negative error 71762f38455SJoel Reardon * code in case of failure. 71862f38455SJoel Reardon */ 71962f38455SJoel Reardon int ubi_flush(int ubi_num, int vol_id, int lnum) 72062f38455SJoel Reardon { 72162f38455SJoel Reardon struct ubi_device *ubi; 72262f38455SJoel Reardon int err = 0; 72362f38455SJoel Reardon 72462f38455SJoel Reardon ubi = ubi_get_device(ubi_num); 72562f38455SJoel Reardon if (!ubi) 72662f38455SJoel Reardon return -ENODEV; 72762f38455SJoel Reardon 72862f38455SJoel Reardon err = ubi_wl_flush(ubi, vol_id, lnum); 72962f38455SJoel Reardon ubi_put_device(ubi); 73062f38455SJoel Reardon return err; 73162f38455SJoel Reardon } 73262f38455SJoel Reardon EXPORT_SYMBOL_GPL(ubi_flush); 73362f38455SJoel Reardon 7340e0ee1ccSDmitry Pervushin BLOCKING_NOTIFIER_HEAD(ubi_notifiers); 7350e0ee1ccSDmitry Pervushin 7360e0ee1ccSDmitry Pervushin /** 7370e0ee1ccSDmitry Pervushin * ubi_register_volume_notifier - register a volume notifier. 7380e0ee1ccSDmitry Pervushin * @nb: the notifier description object 7390e0ee1ccSDmitry Pervushin * @ignore_existing: if non-zero, do not send "added" notification for all 7400e0ee1ccSDmitry Pervushin * already existing volumes 7410e0ee1ccSDmitry Pervushin * 7420e0ee1ccSDmitry Pervushin * This function registers a volume notifier, which means that 7430e0ee1ccSDmitry Pervushin * 'nb->notifier_call()' will be invoked when an UBI volume is created, 7440e0ee1ccSDmitry Pervushin * removed, re-sized, re-named, or updated. The first argument of the function 7450e0ee1ccSDmitry Pervushin * is the notification type. The second argument is pointer to a 7460e0ee1ccSDmitry Pervushin * &struct ubi_notification object which describes the notification event. 7470e0ee1ccSDmitry Pervushin * Using UBI API from the volume notifier is prohibited. 7480e0ee1ccSDmitry Pervushin * 7490e0ee1ccSDmitry Pervushin * This function returns zero in case of success and a negative error code 7500e0ee1ccSDmitry Pervushin * in case of failure. 7510e0ee1ccSDmitry Pervushin */ 7520e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb, 7530e0ee1ccSDmitry Pervushin int ignore_existing) 7540e0ee1ccSDmitry Pervushin { 7550e0ee1ccSDmitry Pervushin int err; 7560e0ee1ccSDmitry Pervushin 7570e0ee1ccSDmitry Pervushin err = blocking_notifier_chain_register(&ubi_notifiers, nb); 7580e0ee1ccSDmitry Pervushin if (err != 0) 7590e0ee1ccSDmitry Pervushin return err; 7600e0ee1ccSDmitry Pervushin if (ignore_existing) 7610e0ee1ccSDmitry Pervushin return 0; 7620e0ee1ccSDmitry Pervushin 7630e0ee1ccSDmitry Pervushin /* 7640e0ee1ccSDmitry Pervushin * We are going to walk all UBI devices and all volumes, and 7650e0ee1ccSDmitry Pervushin * notify the user about existing volumes by the %UBI_VOLUME_ADDED 7660e0ee1ccSDmitry Pervushin * event. We have to lock the @ubi_devices_mutex to make sure UBI 7670e0ee1ccSDmitry Pervushin * devices do not disappear. 7680e0ee1ccSDmitry Pervushin */ 7690e0ee1ccSDmitry Pervushin mutex_lock(&ubi_devices_mutex); 7700e0ee1ccSDmitry Pervushin ubi_enumerate_volumes(nb); 7710e0ee1ccSDmitry Pervushin mutex_unlock(&ubi_devices_mutex); 7720e0ee1ccSDmitry Pervushin 7730e0ee1ccSDmitry Pervushin return err; 7740e0ee1ccSDmitry Pervushin } 7750e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_register_volume_notifier); 7760e0ee1ccSDmitry Pervushin 7770e0ee1ccSDmitry Pervushin /** 7780e0ee1ccSDmitry Pervushin * ubi_unregister_volume_notifier - unregister the volume notifier. 7790e0ee1ccSDmitry Pervushin * @nb: the notifier description object 7800e0ee1ccSDmitry Pervushin * 7810e0ee1ccSDmitry Pervushin * This function unregisters volume notifier @nm and returns zero in case of 7820e0ee1ccSDmitry Pervushin * success and a negative error code in case of failure. 7830e0ee1ccSDmitry Pervushin */ 7840e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb) 7850e0ee1ccSDmitry Pervushin { 7860e0ee1ccSDmitry Pervushin return blocking_notifier_chain_unregister(&ubi_notifiers, nb); 7870e0ee1ccSDmitry Pervushin } 7880e0ee1ccSDmitry Pervushin EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier); 789