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