| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) International Business Machines Corp., 2006 | 
|  | 3 | * | 
|  | 4 | * This program is free software; you can redistribute it and/or modify | 
|  | 5 | * it under the terms of the GNU General Public License as published by | 
|  | 6 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 7 | * (at your option) any later version. | 
|  | 8 | * | 
|  | 9 | * This program is distributed in the hope that it will be useful, | 
|  | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | 
|  | 12 | * the GNU General Public License for more details. | 
|  | 13 | * | 
|  | 14 | * You should have received a copy of the GNU General Public License | 
|  | 15 | * along with this program; if not, write to the Free Software | 
|  | 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
|  | 17 | * | 
|  | 18 | * Author: Artem Bityutskiy (Битюцкий Артём) | 
|  | 19 | */ | 
|  | 20 |  | 
|  | 21 | /* This file mostly implements UBI kernel API functions */ | 
|  | 22 |  | 
|  | 23 | #include <linux/module.h> | 
|  | 24 | #include <linux/err.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 26 | #include <linux/namei.h> | 
|  | 27 | #include <linux/fs.h> | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 28 | #include <asm/div64.h> | 
|  | 29 | #include "ubi.h" | 
|  | 30 |  | 
|  | 31 | /** | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 32 | * ubi_do_get_device_info - get information about UBI device. | 
|  | 33 | * @ubi: UBI device description object | 
|  | 34 | * @di: the information is stored here | 
|  | 35 | * | 
|  | 36 | * This function is the same as 'ubi_get_device_info()', but it assumes the UBI | 
|  | 37 | * device is locked and cannot disappear. | 
|  | 38 | */ | 
|  | 39 | void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di) | 
|  | 40 | { | 
|  | 41 | di->ubi_num = ubi->ubi_num; | 
|  | 42 | di->leb_size = ubi->leb_size; | 
|  | 43 | di->min_io_size = ubi->min_io_size; | 
|  | 44 | di->ro_mode = ubi->ro_mode; | 
|  | 45 | di->cdev = ubi->cdev.dev; | 
|  | 46 | } | 
|  | 47 | EXPORT_SYMBOL_GPL(ubi_do_get_device_info); | 
|  | 48 |  | 
|  | 49 | /** | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 50 | * ubi_get_device_info - get information about UBI device. | 
|  | 51 | * @ubi_num: UBI device number | 
|  | 52 | * @di: the information is stored here | 
|  | 53 | * | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 54 | * This function returns %0 in case of success, %-EINVAL if the UBI device | 
|  | 55 | * number is invalid, and %-ENODEV if there is no such UBI device. | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 56 | */ | 
|  | 57 | int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) | 
|  | 58 | { | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 59 | struct ubi_device *ubi; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 60 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 61 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) | 
|  | 62 | return -EINVAL; | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 63 | ubi = ubi_get_device(ubi_num); | 
|  | 64 | if (!ubi) | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 65 | return -ENODEV; | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 66 | ubi_do_get_device_info(ubi, di); | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 67 | ubi_put_device(ubi); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 68 | return 0; | 
|  | 69 | } | 
|  | 70 | EXPORT_SYMBOL_GPL(ubi_get_device_info); | 
|  | 71 |  | 
|  | 72 | /** | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 73 | * ubi_do_get_volume_info - get information about UBI volume. | 
|  | 74 | * @ubi: UBI device description object | 
|  | 75 | * @vol: volume description object | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 76 | * @vi: the information is stored here | 
|  | 77 | */ | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 78 | void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol, | 
|  | 79 | struct ubi_volume_info *vi) | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 80 | { | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 81 | vi->vol_id = vol->vol_id; | 
|  | 82 | vi->ubi_num = ubi->ubi_num; | 
|  | 83 | vi->size = vol->reserved_pebs; | 
|  | 84 | vi->used_bytes = vol->used_bytes; | 
|  | 85 | vi->vol_type = vol->vol_type; | 
|  | 86 | vi->corrupted = vol->corrupted; | 
|  | 87 | vi->upd_marker = vol->upd_marker; | 
|  | 88 | vi->alignment = vol->alignment; | 
|  | 89 | vi->usable_leb_size = vol->usable_leb_size; | 
|  | 90 | vi->name_len = vol->name_len; | 
|  | 91 | vi->name = vol->name; | 
| Artem Bityutskiy | 49dfc29 | 2007-12-15 18:13:56 +0200 | [diff] [blame] | 92 | vi->cdev = vol->cdev.dev; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 93 | } | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 94 |  | 
|  | 95 | /** | 
|  | 96 | * ubi_get_volume_info - get information about UBI volume. | 
|  | 97 | * @desc: volume descriptor | 
|  | 98 | * @vi: the information is stored here | 
|  | 99 | */ | 
|  | 100 | void ubi_get_volume_info(struct ubi_volume_desc *desc, | 
|  | 101 | struct ubi_volume_info *vi) | 
|  | 102 | { | 
|  | 103 | ubi_do_get_volume_info(desc->vol->ubi, desc->vol, vi); | 
|  | 104 | } | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | EXPORT_SYMBOL_GPL(ubi_get_volume_info); | 
|  | 106 |  | 
|  | 107 | /** | 
|  | 108 | * ubi_open_volume - open UBI volume. | 
|  | 109 | * @ubi_num: UBI device number | 
|  | 110 | * @vol_id: volume ID | 
|  | 111 | * @mode: open mode | 
|  | 112 | * | 
|  | 113 | * The @mode parameter specifies if the volume should be opened in read-only | 
|  | 114 | * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that | 
|  | 115 | * nobody else will be able to open this volume. UBI allows to have many volume | 
|  | 116 | * readers and one writer at a time. | 
|  | 117 | * | 
|  | 118 | * If a static volume is being opened for the first time since boot, it will be | 
|  | 119 | * checked by this function, which means it will be fully read and the CRC | 
|  | 120 | * checksum of each logical eraseblock will be checked. | 
|  | 121 | * | 
|  | 122 | * This function returns volume descriptor in case of success and a negative | 
|  | 123 | * error code in case of failure. | 
|  | 124 | */ | 
|  | 125 | struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) | 
|  | 126 | { | 
|  | 127 | int err; | 
|  | 128 | struct ubi_volume_desc *desc; | 
| Jesper Juhl | 0169b49 | 2007-08-04 01:25:26 +0200 | [diff] [blame] | 129 | struct ubi_device *ubi; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 130 | struct ubi_volume *vol; | 
|  | 131 |  | 
| Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 132 | dbg_gen("open device %d, volume %d, mode %d", ubi_num, vol_id, mode); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 133 |  | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 134 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) | 
|  | 135 | return ERR_PTR(-EINVAL); | 
| Jesper Juhl | 0169b49 | 2007-08-04 01:25:26 +0200 | [diff] [blame] | 136 |  | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 137 | if (mode != UBI_READONLY && mode != UBI_READWRITE && | 
|  | 138 | mode != UBI_EXCLUSIVE) | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 139 | return ERR_PTR(-EINVAL); | 
|  | 140 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 141 | /* | 
|  | 142 | * First of all, we have to get the UBI device to prevent its removal. | 
|  | 143 | */ | 
|  | 144 | ubi = ubi_get_device(ubi_num); | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 145 | if (!ubi) | 
|  | 146 | return ERR_PTR(-ENODEV); | 
|  | 147 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 148 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) { | 
|  | 149 | err = -EINVAL; | 
|  | 150 | goto out_put_ubi; | 
|  | 151 | } | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 152 |  | 
|  | 153 | desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 154 | if (!desc) { | 
|  | 155 | err = -ENOMEM; | 
|  | 156 | goto out_put_ubi; | 
|  | 157 | } | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 158 |  | 
|  | 159 | err = -ENODEV; | 
|  | 160 | if (!try_module_get(THIS_MODULE)) | 
|  | 161 | goto out_free; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 162 |  | 
|  | 163 | spin_lock(&ubi->volumes_lock); | 
|  | 164 | vol = ubi->volumes[vol_id]; | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 165 | if (!vol) | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 166 | goto out_unlock; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 167 |  | 
|  | 168 | err = -EBUSY; | 
|  | 169 | switch (mode) { | 
|  | 170 | case UBI_READONLY: | 
|  | 171 | if (vol->exclusive) | 
|  | 172 | goto out_unlock; | 
|  | 173 | vol->readers += 1; | 
|  | 174 | break; | 
|  | 175 |  | 
|  | 176 | case UBI_READWRITE: | 
|  | 177 | if (vol->exclusive || vol->writers > 0) | 
|  | 178 | goto out_unlock; | 
|  | 179 | vol->writers += 1; | 
|  | 180 | break; | 
|  | 181 |  | 
|  | 182 | case UBI_EXCLUSIVE: | 
|  | 183 | if (vol->exclusive || vol->writers || vol->readers) | 
|  | 184 | goto out_unlock; | 
|  | 185 | vol->exclusive = 1; | 
|  | 186 | break; | 
|  | 187 | } | 
| Artem Bityutskiy | 450f872 | 2007-12-17 13:09:09 +0200 | [diff] [blame] | 188 | get_device(&vol->dev); | 
| Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 189 | vol->ref_count += 1; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 190 | spin_unlock(&ubi->volumes_lock); | 
|  | 191 |  | 
|  | 192 | desc->vol = vol; | 
|  | 193 | desc->mode = mode; | 
|  | 194 |  | 
| Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 195 | mutex_lock(&ubi->ckvol_mutex); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 196 | if (!vol->checked) { | 
|  | 197 | /* This is the first open - check the volume */ | 
|  | 198 | err = ubi_check_volume(ubi, vol_id); | 
|  | 199 | if (err < 0) { | 
| Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 200 | mutex_unlock(&ubi->ckvol_mutex); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 201 | ubi_close_volume(desc); | 
|  | 202 | return ERR_PTR(err); | 
|  | 203 | } | 
|  | 204 | if (err == 1) { | 
|  | 205 | ubi_warn("volume %d on UBI device %d is corrupted", | 
|  | 206 | vol_id, ubi->ubi_num); | 
|  | 207 | vol->corrupted = 1; | 
|  | 208 | } | 
|  | 209 | vol->checked = 1; | 
|  | 210 | } | 
| Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 211 | mutex_unlock(&ubi->ckvol_mutex); | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 212 |  | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 213 | return desc; | 
|  | 214 |  | 
|  | 215 | out_unlock: | 
|  | 216 | spin_unlock(&ubi->volumes_lock); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 217 | module_put(THIS_MODULE); | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 218 | out_free: | 
|  | 219 | kfree(desc); | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 220 | out_put_ubi: | 
|  | 221 | ubi_put_device(ubi); | 
| Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 222 | dbg_err("cannot open device %d, volume %d, error %d", | 
|  | 223 | ubi_num, vol_id, err); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 224 | return ERR_PTR(err); | 
|  | 225 | } | 
|  | 226 | EXPORT_SYMBOL_GPL(ubi_open_volume); | 
|  | 227 |  | 
|  | 228 | /** | 
|  | 229 | * ubi_open_volume_nm - open UBI volume by name. | 
|  | 230 | * @ubi_num: UBI device number | 
|  | 231 | * @name: volume name | 
|  | 232 | * @mode: open mode | 
|  | 233 | * | 
|  | 234 | * This function is similar to 'ubi_open_volume()', but opens a volume by name. | 
|  | 235 | */ | 
|  | 236 | struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, | 
|  | 237 | int mode) | 
|  | 238 | { | 
|  | 239 | int i, vol_id = -1, len; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 240 | struct ubi_device *ubi; | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 241 | struct ubi_volume_desc *ret; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 242 |  | 
| Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 243 | dbg_gen("open device %d, volume %s, mode %d", ubi_num, name, mode); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 244 |  | 
|  | 245 | if (!name) | 
|  | 246 | return ERR_PTR(-EINVAL); | 
|  | 247 |  | 
|  | 248 | len = strnlen(name, UBI_VOL_NAME_MAX + 1); | 
|  | 249 | if (len > UBI_VOL_NAME_MAX) | 
|  | 250 | return ERR_PTR(-EINVAL); | 
|  | 251 |  | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 252 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) | 
|  | 253 | return ERR_PTR(-EINVAL); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 254 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 255 | ubi = ubi_get_device(ubi_num); | 
| Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 256 | if (!ubi) | 
|  | 257 | return ERR_PTR(-ENODEV); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 258 |  | 
|  | 259 | spin_lock(&ubi->volumes_lock); | 
|  | 260 | /* Walk all volumes of this UBI device */ | 
|  | 261 | for (i = 0; i < ubi->vtbl_slots; i++) { | 
|  | 262 | struct ubi_volume *vol = ubi->volumes[i]; | 
|  | 263 |  | 
|  | 264 | if (vol && len == vol->name_len && !strcmp(name, vol->name)) { | 
|  | 265 | vol_id = i; | 
|  | 266 | break; | 
|  | 267 | } | 
|  | 268 | } | 
|  | 269 | spin_unlock(&ubi->volumes_lock); | 
|  | 270 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 271 | if (vol_id >= 0) | 
|  | 272 | ret = ubi_open_volume(ubi_num, vol_id, mode); | 
|  | 273 | else | 
|  | 274 | ret = ERR_PTR(-ENODEV); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 275 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 276 | /* | 
|  | 277 | * We should put the UBI device even in case of success, because | 
|  | 278 | * 'ubi_open_volume()' took a reference as well. | 
|  | 279 | */ | 
|  | 280 | ubi_put_device(ubi); | 
|  | 281 | return ret; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 282 | } | 
|  | 283 | EXPORT_SYMBOL_GPL(ubi_open_volume_nm); | 
|  | 284 |  | 
|  | 285 | /** | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 286 | * ubi_open_volume_path - open UBI volume by its character device node path. | 
|  | 287 | * @pathname: volume character device node path | 
|  | 288 | * @mode: open mode | 
|  | 289 | * | 
|  | 290 | * This function is similar to 'ubi_open_volume()', but opens a volume the path | 
|  | 291 | * to its character device node. | 
|  | 292 | */ | 
|  | 293 | struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode) | 
|  | 294 | { | 
| Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 295 | int error, ubi_num, vol_id, mod; | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 296 | struct inode *inode; | 
|  | 297 | struct path path; | 
|  | 298 |  | 
|  | 299 | dbg_gen("open volume %s, mode %d", pathname, mode); | 
|  | 300 |  | 
|  | 301 | if (!pathname || !*pathname) | 
|  | 302 | return ERR_PTR(-EINVAL); | 
|  | 303 |  | 
|  | 304 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); | 
|  | 305 | if (error) | 
|  | 306 | return ERR_PTR(error); | 
|  | 307 |  | 
|  | 308 | inode = path.dentry->d_inode; | 
| Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 309 | mod = inode->i_mode; | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 310 | ubi_num = ubi_major2num(imajor(inode)); | 
|  | 311 | vol_id = iminor(inode) - 1; | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 312 | path_put(&path); | 
| Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 313 |  | 
|  | 314 | if (!S_ISCHR(mod)) | 
|  | 315 | return ERR_PTR(-EINVAL); | 
|  | 316 | if (vol_id >= 0 && ubi_num >= 0) | 
|  | 317 | return ubi_open_volume(ubi_num, vol_id, mode); | 
|  | 318 | return ERR_PTR(-ENODEV); | 
| Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 319 | } | 
|  | 320 | EXPORT_SYMBOL_GPL(ubi_open_volume_path); | 
|  | 321 |  | 
|  | 322 | /** | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 323 | * ubi_close_volume - close UBI volume. | 
|  | 324 | * @desc: volume descriptor | 
|  | 325 | */ | 
|  | 326 | void ubi_close_volume(struct ubi_volume_desc *desc) | 
|  | 327 | { | 
|  | 328 | struct ubi_volume *vol = desc->vol; | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 329 | struct ubi_device *ubi = vol->ubi; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 330 |  | 
| Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 331 | dbg_gen("close device %d, volume %d, mode %d", | 
|  | 332 | ubi->ubi_num, vol->vol_id, desc->mode); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 333 |  | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 334 | spin_lock(&ubi->volumes_lock); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 335 | switch (desc->mode) { | 
|  | 336 | case UBI_READONLY: | 
|  | 337 | vol->readers -= 1; | 
|  | 338 | break; | 
|  | 339 | case UBI_READWRITE: | 
|  | 340 | vol->writers -= 1; | 
|  | 341 | break; | 
|  | 342 | case UBI_EXCLUSIVE: | 
|  | 343 | vol->exclusive = 0; | 
|  | 344 | } | 
| Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 345 | vol->ref_count -= 1; | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 346 | spin_unlock(&ubi->volumes_lock); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 347 |  | 
| Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 348 | kfree(desc); | 
| Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 349 | put_device(&vol->dev); | 
|  | 350 | ubi_put_device(ubi); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 351 | module_put(THIS_MODULE); | 
|  | 352 | } | 
|  | 353 | EXPORT_SYMBOL_GPL(ubi_close_volume); | 
|  | 354 |  | 
|  | 355 | /** | 
|  | 356 | * ubi_leb_read - read data. | 
|  | 357 | * @desc: volume descriptor | 
|  | 358 | * @lnum: logical eraseblock number to read from | 
|  | 359 | * @buf: buffer where to store the read data | 
|  | 360 | * @offset: offset within the logical eraseblock to read from | 
|  | 361 | * @len: how many bytes to read | 
|  | 362 | * @check: whether UBI has to check the read data's CRC or not. | 
|  | 363 | * | 
|  | 364 | * This function reads data from offset @offset of logical eraseblock @lnum and | 
|  | 365 | * stores the data at @buf. When reading from static volumes, @check specifies | 
|  | 366 | * whether the data has to be checked or not. If yes, the whole logical | 
|  | 367 | * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC | 
|  | 368 | * checksum is per-eraseblock). So checking may substantially slow down the | 
|  | 369 | * read speed. The @check argument is ignored for dynamic volumes. | 
|  | 370 | * | 
|  | 371 | * In case of success, this function returns zero. In case of failure, this | 
|  | 372 | * function returns a negative error code. | 
|  | 373 | * | 
|  | 374 | * %-EBADMSG error code is returned: | 
|  | 375 | * o for both static and dynamic volumes if MTD driver has detected a data | 
|  | 376 | *   integrity problem (unrecoverable ECC checksum mismatch in case of NAND); | 
|  | 377 | * o for static volumes in case of data CRC mismatch. | 
|  | 378 | * | 
|  | 379 | * If the volume is damaged because of an interrupted update this function just | 
|  | 380 | * returns immediately with %-EBADF error code. | 
|  | 381 | */ | 
|  | 382 | int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, | 
|  | 383 | int len, int check) | 
|  | 384 | { | 
|  | 385 | struct ubi_volume *vol = desc->vol; | 
|  | 386 | struct ubi_device *ubi = vol->ubi; | 
|  | 387 | int err, vol_id = vol->vol_id; | 
|  | 388 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 389 | dbg_gen("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 390 |  | 
|  | 391 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || | 
|  | 392 | lnum >= vol->used_ebs || offset < 0 || len < 0 || | 
|  | 393 | offset + len > vol->usable_leb_size) | 
|  | 394 | return -EINVAL; | 
|  | 395 |  | 
| Artem Bityutskiy | 4ab60a0 | 2007-05-05 14:59:23 +0300 | [diff] [blame] | 396 | if (vol->vol_type == UBI_STATIC_VOLUME) { | 
|  | 397 | if (vol->used_ebs == 0) | 
|  | 398 | /* Empty static UBI volume */ | 
|  | 399 | return 0; | 
|  | 400 | if (lnum == vol->used_ebs - 1 && | 
|  | 401 | offset + len > vol->last_eb_bytes) | 
|  | 402 | return -EINVAL; | 
|  | 403 | } | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 404 |  | 
|  | 405 | if (vol->upd_marker) | 
|  | 406 | return -EBADF; | 
|  | 407 | if (len == 0) | 
|  | 408 | return 0; | 
|  | 409 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 410 | err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 411 | if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { | 
|  | 412 | ubi_warn("mark volume %d as corrupted", vol_id); | 
|  | 413 | vol->corrupted = 1; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | return err; | 
|  | 417 | } | 
|  | 418 | EXPORT_SYMBOL_GPL(ubi_leb_read); | 
|  | 419 |  | 
|  | 420 | /** | 
|  | 421 | * ubi_leb_write - write data. | 
|  | 422 | * @desc: volume descriptor | 
|  | 423 | * @lnum: logical eraseblock number to write to | 
|  | 424 | * @buf: data to write | 
|  | 425 | * @offset: offset within the logical eraseblock where to write | 
|  | 426 | * @len: how many bytes to write | 
|  | 427 | * @dtype: expected data type | 
|  | 428 | * | 
|  | 429 | * This function writes @len bytes of data from @buf to offset @offset of | 
|  | 430 | * logical eraseblock @lnum. The @dtype argument describes expected lifetime of | 
|  | 431 | * the data. | 
|  | 432 | * | 
|  | 433 | * This function takes care of physical eraseblock write failures. If write to | 
|  | 434 | * the physical eraseblock write operation fails, the logical eraseblock is | 
|  | 435 | * re-mapped to another physical eraseblock, the data is recovered, and the | 
|  | 436 | * write finishes. UBI has a pool of reserved physical eraseblocks for this. | 
|  | 437 | * | 
|  | 438 | * If all the data were successfully written, zero is returned. If an error | 
|  | 439 | * occurred and UBI has not been able to recover from it, this function returns | 
|  | 440 | * a negative error code. Note, in case of an error, it is possible that | 
|  | 441 | * something was still written to the flash media, but that may be some | 
|  | 442 | * garbage. | 
|  | 443 | * | 
|  | 444 | * If the volume is damaged because of an interrupted update this function just | 
|  | 445 | * returns immediately with %-EBADF code. | 
|  | 446 | */ | 
|  | 447 | int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, | 
|  | 448 | int offset, int len, int dtype) | 
|  | 449 | { | 
|  | 450 | struct ubi_volume *vol = desc->vol; | 
|  | 451 | struct ubi_device *ubi = vol->ubi; | 
|  | 452 | int vol_id = vol->vol_id; | 
|  | 453 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 454 | dbg_gen("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 455 |  | 
|  | 456 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | 
|  | 457 | return -EINVAL; | 
|  | 458 |  | 
|  | 459 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | 
|  | 460 | return -EROFS; | 
|  | 461 |  | 
|  | 462 | if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || | 
| Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 463 | offset + len > vol->usable_leb_size || | 
|  | 464 | offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 465 | return -EINVAL; | 
|  | 466 |  | 
|  | 467 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | 
|  | 468 | dtype != UBI_UNKNOWN) | 
|  | 469 | return -EINVAL; | 
|  | 470 |  | 
|  | 471 | if (vol->upd_marker) | 
|  | 472 | return -EBADF; | 
|  | 473 |  | 
|  | 474 | if (len == 0) | 
|  | 475 | return 0; | 
|  | 476 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 477 | return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 478 | } | 
|  | 479 | EXPORT_SYMBOL_GPL(ubi_leb_write); | 
|  | 480 |  | 
|  | 481 | /* | 
|  | 482 | * ubi_leb_change - change logical eraseblock atomically. | 
|  | 483 | * @desc: volume descriptor | 
|  | 484 | * @lnum: logical eraseblock number to change | 
|  | 485 | * @buf: data to write | 
|  | 486 | * @len: how many bytes to write | 
|  | 487 | * @dtype: expected data type | 
|  | 488 | * | 
|  | 489 | * This function changes the contents of a logical eraseblock atomically. @buf | 
|  | 490 | * has to contain new logical eraseblock data, and @len - the length of the | 
| Shinya Kuribayashi | 3f50262 | 2010-05-06 19:21:47 +0900 | [diff] [blame] | 491 | * data, which has to be aligned. The length may be shorter than the logical | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 492 | * eraseblock size, ant the logical eraseblock may be appended to more times | 
|  | 493 | * later on. This function guarantees that in case of an unclean reboot the old | 
|  | 494 | * contents is preserved. Returns zero in case of success and a negative error | 
|  | 495 | * code in case of failure. | 
|  | 496 | */ | 
|  | 497 | int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, | 
|  | 498 | int len, int dtype) | 
|  | 499 | { | 
|  | 500 | struct ubi_volume *vol = desc->vol; | 
|  | 501 | struct ubi_device *ubi = vol->ubi; | 
|  | 502 | int vol_id = vol->vol_id; | 
|  | 503 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 504 | dbg_gen("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 505 |  | 
|  | 506 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | 
|  | 507 | return -EINVAL; | 
|  | 508 |  | 
|  | 509 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | 
|  | 510 | return -EROFS; | 
|  | 511 |  | 
|  | 512 | if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || | 
| Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 513 | len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 514 | return -EINVAL; | 
|  | 515 |  | 
|  | 516 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | 
|  | 517 | dtype != UBI_UNKNOWN) | 
|  | 518 | return -EINVAL; | 
|  | 519 |  | 
|  | 520 | if (vol->upd_marker) | 
|  | 521 | return -EBADF; | 
|  | 522 |  | 
|  | 523 | if (len == 0) | 
|  | 524 | return 0; | 
|  | 525 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 526 | return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 527 | } | 
|  | 528 | EXPORT_SYMBOL_GPL(ubi_leb_change); | 
|  | 529 |  | 
|  | 530 | /** | 
|  | 531 | * ubi_leb_erase - erase logical eraseblock. | 
|  | 532 | * @desc: volume descriptor | 
|  | 533 | * @lnum: logical eraseblock number | 
|  | 534 | * | 
|  | 535 | * This function un-maps logical eraseblock @lnum and synchronously erases the | 
|  | 536 | * correspondent physical eraseblock. Returns zero in case of success and a | 
|  | 537 | * negative error code in case of failure. | 
|  | 538 | * | 
|  | 539 | * If the volume is damaged because of an interrupted update this function just | 
|  | 540 | * returns immediately with %-EBADF code. | 
|  | 541 | */ | 
|  | 542 | int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) | 
|  | 543 | { | 
|  | 544 | struct ubi_volume *vol = desc->vol; | 
|  | 545 | struct ubi_device *ubi = vol->ubi; | 
| Artem Bityutskiy | ae616e1 | 2008-01-16 12:15:47 +0200 | [diff] [blame] | 546 | int err; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 547 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 548 | dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 549 |  | 
|  | 550 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | 
|  | 551 | return -EROFS; | 
|  | 552 |  | 
|  | 553 | if (lnum < 0 || lnum >= vol->reserved_pebs) | 
|  | 554 | return -EINVAL; | 
|  | 555 |  | 
|  | 556 | if (vol->upd_marker) | 
|  | 557 | return -EBADF; | 
|  | 558 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 559 | err = ubi_eba_unmap_leb(ubi, vol, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 560 | if (err) | 
|  | 561 | return err; | 
|  | 562 |  | 
|  | 563 | return ubi_wl_flush(ubi); | 
|  | 564 | } | 
|  | 565 | EXPORT_SYMBOL_GPL(ubi_leb_erase); | 
|  | 566 |  | 
|  | 567 | /** | 
|  | 568 | * ubi_leb_unmap - un-map logical eraseblock. | 
|  | 569 | * @desc: volume descriptor | 
|  | 570 | * @lnum: logical eraseblock number | 
|  | 571 | * | 
|  | 572 | * This function un-maps logical eraseblock @lnum and schedules the | 
|  | 573 | * corresponding physical eraseblock for erasure, so that it will eventually be | 
| Shinya Kuribayashi | 3f50262 | 2010-05-06 19:21:47 +0900 | [diff] [blame] | 574 | * physically erased in background. This operation is much faster than the | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 575 | * erase operation. | 
|  | 576 | * | 
|  | 577 | * Unlike erase, the un-map operation does not guarantee that the logical | 
|  | 578 | * eraseblock will contain all 0xFF bytes when UBI is initialized again. For | 
|  | 579 | * example, if several logical eraseblocks are un-mapped, and an unclean reboot | 
|  | 580 | * happens after this, the logical eraseblocks will not necessarily be | 
|  | 581 | * un-mapped again when this MTD device is attached. They may actually be | 
|  | 582 | * mapped to the same physical eraseblocks again. So, this function has to be | 
|  | 583 | * used with care. | 
|  | 584 | * | 
|  | 585 | * In other words, when un-mapping a logical eraseblock, UBI does not store | 
|  | 586 | * any information about this on the flash media, it just marks the logical | 
|  | 587 | * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical | 
|  | 588 | * eraseblock is physically erased, it will be mapped again to the same logical | 
|  | 589 | * eraseblock when the MTD device is attached again. | 
|  | 590 | * | 
|  | 591 | * The main and obvious use-case of this function is when the contents of a | 
|  | 592 | * logical eraseblock has to be re-written. Then it is much more efficient to | 
| Shinya Kuribayashi | 3f50262 | 2010-05-06 19:21:47 +0900 | [diff] [blame] | 593 | * first un-map it, then write new data, rather than first erase it, then write | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 594 | * new data. Note, once new data has been written to the logical eraseblock, | 
|  | 595 | * UBI guarantees that the old contents has gone forever. In other words, if an | 
|  | 596 | * unclean reboot happens after the logical eraseblock has been un-mapped and | 
|  | 597 | * then written to, it will contain the last written data. | 
|  | 598 | * | 
|  | 599 | * This function returns zero in case of success and a negative error code in | 
|  | 600 | * case of failure. If the volume is damaged because of an interrupted update | 
|  | 601 | * this function just returns immediately with %-EBADF code. | 
|  | 602 | */ | 
|  | 603 | int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) | 
|  | 604 | { | 
|  | 605 | struct ubi_volume *vol = desc->vol; | 
|  | 606 | struct ubi_device *ubi = vol->ubi; | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 607 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 608 | dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 609 |  | 
|  | 610 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | 
|  | 611 | return -EROFS; | 
|  | 612 |  | 
|  | 613 | if (lnum < 0 || lnum >= vol->reserved_pebs) | 
|  | 614 | return -EINVAL; | 
|  | 615 |  | 
|  | 616 | if (vol->upd_marker) | 
|  | 617 | return -EBADF; | 
|  | 618 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 619 | return ubi_eba_unmap_leb(ubi, vol, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 620 | } | 
|  | 621 | EXPORT_SYMBOL_GPL(ubi_leb_unmap); | 
|  | 622 |  | 
|  | 623 | /** | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 624 | * ubi_leb_map - map logical eraseblock to a physical eraseblock. | 
| Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 625 | * @desc: volume descriptor | 
|  | 626 | * @lnum: logical eraseblock number | 
|  | 627 | * @dtype: expected data type | 
|  | 628 | * | 
|  | 629 | * This function maps an un-mapped logical eraseblock @lnum to a physical | 
| Coly Li | 73ac36e | 2009-01-07 18:09:16 -0800 | [diff] [blame] | 630 | * eraseblock. This means, that after a successful invocation of this | 
| Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 631 | * function the logical eraseblock @lnum will be empty (contain only %0xFF | 
|  | 632 | * bytes) and be mapped to a physical eraseblock, even if an unclean reboot | 
|  | 633 | * happens. | 
|  | 634 | * | 
|  | 635 | * This function returns zero in case of success, %-EBADF if the volume is | 
|  | 636 | * damaged because of an interrupted update, %-EBADMSG if the logical | 
|  | 637 | * eraseblock is already mapped, and other negative error codes in case of | 
|  | 638 | * other failures. | 
|  | 639 | */ | 
|  | 640 | int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) | 
|  | 641 | { | 
|  | 642 | struct ubi_volume *vol = desc->vol; | 
|  | 643 | struct ubi_device *ubi = vol->ubi; | 
| Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 644 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 645 | dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); | 
| Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 646 |  | 
|  | 647 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | 
|  | 648 | return -EROFS; | 
|  | 649 |  | 
|  | 650 | if (lnum < 0 || lnum >= vol->reserved_pebs) | 
|  | 651 | return -EINVAL; | 
|  | 652 |  | 
|  | 653 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | 
|  | 654 | dtype != UBI_UNKNOWN) | 
|  | 655 | return -EINVAL; | 
|  | 656 |  | 
|  | 657 | if (vol->upd_marker) | 
|  | 658 | return -EBADF; | 
|  | 659 |  | 
|  | 660 | if (vol->eba_tbl[lnum] >= 0) | 
|  | 661 | return -EBADMSG; | 
|  | 662 |  | 
| Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 663 | return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); | 
| Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 664 | } | 
|  | 665 | EXPORT_SYMBOL_GPL(ubi_leb_map); | 
|  | 666 |  | 
|  | 667 | /** | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 668 | * ubi_is_mapped - check if logical eraseblock is mapped. | 
|  | 669 | * @desc: volume descriptor | 
|  | 670 | * @lnum: logical eraseblock number | 
|  | 671 | * | 
|  | 672 | * This function checks if logical eraseblock @lnum is mapped to a physical | 
|  | 673 | * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily | 
|  | 674 | * mean it will still be un-mapped after the UBI device is re-attached. The | 
|  | 675 | * logical eraseblock may become mapped to the physical eraseblock it was last | 
|  | 676 | * mapped to. | 
|  | 677 | * | 
|  | 678 | * This function returns %1 if the LEB is mapped, %0 if not, and a negative | 
|  | 679 | * error code in case of failure. If the volume is damaged because of an | 
|  | 680 | * interrupted update this function just returns immediately with %-EBADF error | 
|  | 681 | * code. | 
|  | 682 | */ | 
|  | 683 | int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) | 
|  | 684 | { | 
|  | 685 | struct ubi_volume *vol = desc->vol; | 
|  | 686 |  | 
| Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 687 | dbg_gen("test LEB %d:%d", vol->vol_id, lnum); | 
| Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 688 |  | 
|  | 689 | if (lnum < 0 || lnum >= vol->reserved_pebs) | 
|  | 690 | return -EINVAL; | 
|  | 691 |  | 
|  | 692 | if (vol->upd_marker) | 
|  | 693 | return -EBADF; | 
|  | 694 |  | 
|  | 695 | return vol->eba_tbl[lnum] >= 0; | 
|  | 696 | } | 
|  | 697 | EXPORT_SYMBOL_GPL(ubi_is_mapped); | 
| Artem Bityutskiy | a5bf619 | 2008-07-10 18:38:33 +0300 | [diff] [blame] | 698 |  | 
|  | 699 | /** | 
|  | 700 | * ubi_sync - synchronize UBI device buffers. | 
|  | 701 | * @ubi_num: UBI device to synchronize | 
|  | 702 | * | 
|  | 703 | * The underlying MTD device may cache data in hardware or in software. This | 
|  | 704 | * function ensures the caches are flushed. Returns zero in case of success and | 
|  | 705 | * a negative error code in case of failure. | 
|  | 706 | */ | 
|  | 707 | int ubi_sync(int ubi_num) | 
|  | 708 | { | 
|  | 709 | struct ubi_device *ubi; | 
|  | 710 |  | 
|  | 711 | ubi = ubi_get_device(ubi_num); | 
|  | 712 | if (!ubi) | 
|  | 713 | return -ENODEV; | 
|  | 714 |  | 
|  | 715 | if (ubi->mtd->sync) | 
|  | 716 | ubi->mtd->sync(ubi->mtd); | 
|  | 717 |  | 
|  | 718 | ubi_put_device(ubi); | 
|  | 719 | return 0; | 
|  | 720 | } | 
|  | 721 | EXPORT_SYMBOL_GPL(ubi_sync); | 
| Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 722 |  | 
|  | 723 | BLOCKING_NOTIFIER_HEAD(ubi_notifiers); | 
|  | 724 |  | 
|  | 725 | /** | 
|  | 726 | * ubi_register_volume_notifier - register a volume notifier. | 
|  | 727 | * @nb: the notifier description object | 
|  | 728 | * @ignore_existing: if non-zero, do not send "added" notification for all | 
|  | 729 | *                   already existing volumes | 
|  | 730 | * | 
|  | 731 | * This function registers a volume notifier, which means that | 
|  | 732 | * 'nb->notifier_call()' will be invoked when an UBI  volume is created, | 
|  | 733 | * removed, re-sized, re-named, or updated. The first argument of the function | 
|  | 734 | * is the notification type. The second argument is pointer to a | 
|  | 735 | * &struct ubi_notification object which describes the notification event. | 
|  | 736 | * Using UBI API from the volume notifier is prohibited. | 
|  | 737 | * | 
|  | 738 | * This function returns zero in case of success and a negative error code | 
|  | 739 | * in case of failure. | 
|  | 740 | */ | 
|  | 741 | int ubi_register_volume_notifier(struct notifier_block *nb, | 
|  | 742 | int ignore_existing) | 
|  | 743 | { | 
|  | 744 | int err; | 
|  | 745 |  | 
|  | 746 | err = blocking_notifier_chain_register(&ubi_notifiers, nb); | 
|  | 747 | if (err != 0) | 
|  | 748 | return err; | 
|  | 749 | if (ignore_existing) | 
|  | 750 | return 0; | 
|  | 751 |  | 
|  | 752 | /* | 
|  | 753 | * We are going to walk all UBI devices and all volumes, and | 
|  | 754 | * notify the user about existing volumes by the %UBI_VOLUME_ADDED | 
|  | 755 | * event. We have to lock the @ubi_devices_mutex to make sure UBI | 
|  | 756 | * devices do not disappear. | 
|  | 757 | */ | 
|  | 758 | mutex_lock(&ubi_devices_mutex); | 
|  | 759 | ubi_enumerate_volumes(nb); | 
|  | 760 | mutex_unlock(&ubi_devices_mutex); | 
|  | 761 |  | 
|  | 762 | return err; | 
|  | 763 | } | 
|  | 764 | EXPORT_SYMBOL_GPL(ubi_register_volume_notifier); | 
|  | 765 |  | 
|  | 766 | /** | 
|  | 767 | * ubi_unregister_volume_notifier - unregister the volume notifier. | 
|  | 768 | * @nb: the notifier description object | 
|  | 769 | * | 
|  | 770 | * This function unregisters volume notifier @nm and returns zero in case of | 
|  | 771 | * success and a negative error code in case of failure. | 
|  | 772 | */ | 
|  | 773 | int ubi_unregister_volume_notifier(struct notifier_block *nb) | 
|  | 774 | { | 
|  | 775 | return blocking_notifier_chain_unregister(&ubi_notifiers, nb); | 
|  | 776 | } | 
|  | 777 | EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier); |