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 && |
| 140 | mode != UBI_EXCLUSIVE) |
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: |
| 185 | if (vol->exclusive || vol->writers || vol->readers) |
| 186 | goto out_unlock; |
| 187 | vol->exclusive = 1; |
| 188 | break; |
| 189 | } |
Artem Bityutskiy | 450f872 | 2007-12-17 13:09:09 +0200 | [diff] [blame] | 190 | get_device(&vol->dev); |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 191 | vol->ref_count += 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 192 | spin_unlock(&ubi->volumes_lock); |
| 193 | |
| 194 | desc->vol = vol; |
| 195 | desc->mode = mode; |
| 196 | |
Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 197 | mutex_lock(&ubi->ckvol_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 198 | if (!vol->checked) { |
| 199 | /* This is the first open - check the volume */ |
| 200 | err = ubi_check_volume(ubi, vol_id); |
| 201 | if (err < 0) { |
Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 202 | mutex_unlock(&ubi->ckvol_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 203 | ubi_close_volume(desc); |
| 204 | return ERR_PTR(err); |
| 205 | } |
| 206 | if (err == 1) { |
| 207 | ubi_warn("volume %d on UBI device %d is corrupted", |
| 208 | vol_id, ubi->ubi_num); |
| 209 | vol->corrupted = 1; |
| 210 | } |
| 211 | vol->checked = 1; |
| 212 | } |
Artem Bityutskiy | 783b273 | 2007-12-25 18:13:33 +0200 | [diff] [blame] | 213 | mutex_unlock(&ubi->ckvol_mutex); |
Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 214 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 215 | return desc; |
| 216 | |
| 217 | out_unlock: |
| 218 | spin_unlock(&ubi->volumes_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 219 | module_put(THIS_MODULE); |
Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 220 | out_free: |
| 221 | kfree(desc); |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 222 | out_put_ubi: |
| 223 | ubi_put_device(ubi); |
Artem Bityutskiy | e298682 | 2012-05-16 18:39:56 +0300 | [diff] [blame] | 224 | ubi_err("cannot open device %d, volume %d, error %d", |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 225 | ubi_num, vol_id, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 226 | return ERR_PTR(err); |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(ubi_open_volume); |
| 229 | |
| 230 | /** |
| 231 | * ubi_open_volume_nm - open UBI volume by name. |
| 232 | * @ubi_num: UBI device number |
| 233 | * @name: volume name |
| 234 | * @mode: open mode |
| 235 | * |
| 236 | * This function is similar to 'ubi_open_volume()', but opens a volume by name. |
| 237 | */ |
| 238 | struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, |
| 239 | int mode) |
| 240 | { |
| 241 | int i, vol_id = -1, len; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 242 | struct ubi_device *ubi; |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 243 | struct ubi_volume_desc *ret; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 244 | |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 245 | 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] | 246 | |
| 247 | if (!name) |
| 248 | return ERR_PTR(-EINVAL); |
| 249 | |
| 250 | len = strnlen(name, UBI_VOL_NAME_MAX + 1); |
| 251 | if (len > UBI_VOL_NAME_MAX) |
| 252 | return ERR_PTR(-EINVAL); |
| 253 | |
Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 254 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES) |
| 255 | return ERR_PTR(-EINVAL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 256 | |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 257 | ubi = ubi_get_device(ubi_num); |
Artem Bityutskiy | 35ad5fb | 2007-12-17 14:22:55 +0200 | [diff] [blame] | 258 | if (!ubi) |
| 259 | return ERR_PTR(-ENODEV); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 260 | |
| 261 | spin_lock(&ubi->volumes_lock); |
| 262 | /* Walk all volumes of this UBI device */ |
| 263 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 264 | struct ubi_volume *vol = ubi->volumes[i]; |
| 265 | |
| 266 | if (vol && len == vol->name_len && !strcmp(name, vol->name)) { |
| 267 | vol_id = i; |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | spin_unlock(&ubi->volumes_lock); |
| 272 | |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 273 | if (vol_id >= 0) |
| 274 | ret = ubi_open_volume(ubi_num, vol_id, mode); |
| 275 | else |
| 276 | ret = ERR_PTR(-ENODEV); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 277 | |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 278 | /* |
| 279 | * We should put the UBI device even in case of success, because |
| 280 | * 'ubi_open_volume()' took a reference as well. |
| 281 | */ |
| 282 | ubi_put_device(ubi); |
| 283 | return ret; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 284 | } |
| 285 | EXPORT_SYMBOL_GPL(ubi_open_volume_nm); |
| 286 | |
| 287 | /** |
Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 288 | * ubi_open_volume_path - open UBI volume by its character device node path. |
| 289 | * @pathname: volume character device node path |
| 290 | * @mode: open mode |
| 291 | * |
| 292 | * This function is similar to 'ubi_open_volume()', but opens a volume the path |
| 293 | * to its character device node. |
| 294 | */ |
| 295 | struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode) |
| 296 | { |
Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 297 | int error, ubi_num, vol_id, mod; |
Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 298 | struct inode *inode; |
| 299 | struct path path; |
| 300 | |
| 301 | dbg_gen("open volume %s, mode %d", pathname, mode); |
| 302 | |
| 303 | if (!pathname || !*pathname) |
| 304 | return ERR_PTR(-EINVAL); |
| 305 | |
| 306 | error = kern_path(pathname, LOOKUP_FOLLOW, &path); |
| 307 | if (error) |
| 308 | return ERR_PTR(error); |
| 309 | |
| 310 | inode = path.dentry->d_inode; |
Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 311 | mod = inode->i_mode; |
Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 312 | ubi_num = ubi_major2num(imajor(inode)); |
| 313 | vol_id = iminor(inode) - 1; |
Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 314 | path_put(&path); |
Artem Bityutskiy | b531b55 | 2010-01-05 17:25:59 +0200 | [diff] [blame] | 315 | |
| 316 | if (!S_ISCHR(mod)) |
| 317 | return ERR_PTR(-EINVAL); |
| 318 | if (vol_id >= 0 && ubi_num >= 0) |
| 319 | return ubi_open_volume(ubi_num, vol_id, mode); |
| 320 | return ERR_PTR(-ENODEV); |
Corentin Chary | b571028 | 2009-09-28 21:10:11 +0200 | [diff] [blame] | 321 | } |
| 322 | EXPORT_SYMBOL_GPL(ubi_open_volume_path); |
| 323 | |
| 324 | /** |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 325 | * ubi_close_volume - close UBI volume. |
| 326 | * @desc: volume descriptor |
| 327 | */ |
| 328 | void ubi_close_volume(struct ubi_volume_desc *desc) |
| 329 | { |
| 330 | struct ubi_volume *vol = desc->vol; |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 331 | struct ubi_device *ubi = vol->ubi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 332 | |
Artem Bityutskiy | e1cf7e6 | 2009-05-07 18:24:14 +0300 | [diff] [blame] | 333 | dbg_gen("close device %d, volume %d, mode %d", |
| 334 | ubi->ubi_num, vol->vol_id, desc->mode); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 335 | |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 336 | spin_lock(&ubi->volumes_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 337 | switch (desc->mode) { |
| 338 | case UBI_READONLY: |
| 339 | vol->readers -= 1; |
| 340 | break; |
| 341 | case UBI_READWRITE: |
| 342 | vol->writers -= 1; |
| 343 | break; |
| 344 | case UBI_EXCLUSIVE: |
| 345 | vol->exclusive = 0; |
| 346 | } |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 347 | vol->ref_count -= 1; |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 348 | spin_unlock(&ubi->volumes_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 349 | |
Artem Bityutskiy | d05c77a | 2007-12-17 15:42:57 +0200 | [diff] [blame] | 350 | kfree(desc); |
Artem Bityutskiy | e73f445 | 2007-12-17 17:37:26 +0200 | [diff] [blame] | 351 | put_device(&vol->dev); |
| 352 | ubi_put_device(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 353 | module_put(THIS_MODULE); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(ubi_close_volume); |
| 356 | |
| 357 | /** |
| 358 | * ubi_leb_read - read data. |
| 359 | * @desc: volume descriptor |
| 360 | * @lnum: logical eraseblock number to read from |
| 361 | * @buf: buffer where to store the read data |
| 362 | * @offset: offset within the logical eraseblock to read from |
| 363 | * @len: how many bytes to read |
| 364 | * @check: whether UBI has to check the read data's CRC or not. |
| 365 | * |
| 366 | * This function reads data from offset @offset of logical eraseblock @lnum and |
| 367 | * stores the data at @buf. When reading from static volumes, @check specifies |
| 368 | * whether the data has to be checked or not. If yes, the whole logical |
| 369 | * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC |
| 370 | * checksum is per-eraseblock). So checking may substantially slow down the |
| 371 | * read speed. The @check argument is ignored for dynamic volumes. |
| 372 | * |
| 373 | * In case of success, this function returns zero. In case of failure, this |
| 374 | * function returns a negative error code. |
| 375 | * |
| 376 | * %-EBADMSG error code is returned: |
| 377 | * o for both static and dynamic volumes if MTD driver has detected a data |
| 378 | * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); |
| 379 | * o for static volumes in case of data CRC mismatch. |
| 380 | * |
| 381 | * If the volume is damaged because of an interrupted update this function just |
| 382 | * returns immediately with %-EBADF error code. |
| 383 | */ |
| 384 | int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, |
| 385 | int len, int check) |
| 386 | { |
| 387 | struct ubi_volume *vol = desc->vol; |
| 388 | struct ubi_device *ubi = vol->ubi; |
| 389 | int err, vol_id = vol->vol_id; |
| 390 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 391 | 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] | 392 | |
| 393 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || |
| 394 | lnum >= vol->used_ebs || offset < 0 || len < 0 || |
| 395 | offset + len > vol->usable_leb_size) |
| 396 | return -EINVAL; |
| 397 | |
Artem Bityutskiy | 4ab60a0 | 2007-05-05 14:59:23 +0300 | [diff] [blame] | 398 | if (vol->vol_type == UBI_STATIC_VOLUME) { |
| 399 | if (vol->used_ebs == 0) |
| 400 | /* Empty static UBI volume */ |
| 401 | return 0; |
| 402 | if (lnum == vol->used_ebs - 1 && |
| 403 | offset + len > vol->last_eb_bytes) |
| 404 | return -EINVAL; |
| 405 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 406 | |
| 407 | if (vol->upd_marker) |
| 408 | return -EBADF; |
| 409 | if (len == 0) |
| 410 | return 0; |
| 411 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 412 | err = ubi_eba_read_leb(ubi, vol, lnum, buf, offset, len, check); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 413 | if (err && mtd_is_eccerr(err) && vol->vol_type == UBI_STATIC_VOLUME) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 414 | ubi_warn("mark volume %d as corrupted", vol_id); |
| 415 | vol->corrupted = 1; |
| 416 | } |
| 417 | |
| 418 | return err; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(ubi_leb_read); |
| 421 | |
| 422 | /** |
| 423 | * ubi_leb_write - write data. |
| 424 | * @desc: volume descriptor |
| 425 | * @lnum: logical eraseblock number to write to |
| 426 | * @buf: data to write |
| 427 | * @offset: offset within the logical eraseblock where to write |
| 428 | * @len: how many bytes to write |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 429 | * |
| 430 | * 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] | 431 | * logical eraseblock @lnum. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 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, |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 448 | int offset, int len) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 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 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 467 | if (vol->upd_marker) |
| 468 | return -EBADF; |
| 469 | |
| 470 | if (len == 0) |
| 471 | return 0; |
| 472 | |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 473 | return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 474 | } |
| 475 | EXPORT_SYMBOL_GPL(ubi_leb_write); |
| 476 | |
| 477 | /* |
| 478 | * ubi_leb_change - change logical eraseblock atomically. |
| 479 | * @desc: volume descriptor |
| 480 | * @lnum: logical eraseblock number to change |
| 481 | * @buf: data to write |
| 482 | * @len: how many bytes to write |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 483 | * |
| 484 | * This function changes the contents of a logical eraseblock atomically. @buf |
| 485 | * 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] | 486 | * 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] | 487 | * eraseblock size, ant the logical eraseblock may be appended to more times |
| 488 | * later on. This function guarantees that in case of an unclean reboot the old |
| 489 | * contents is preserved. Returns zero in case of success and a negative error |
| 490 | * code in case of failure. |
| 491 | */ |
| 492 | 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] | 493 | int len) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 494 | { |
| 495 | struct ubi_volume *vol = desc->vol; |
| 496 | struct ubi_device *ubi = vol->ubi; |
| 497 | int vol_id = vol->vol_id; |
| 498 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 499 | 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] | 500 | |
| 501 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) |
| 502 | return -EINVAL; |
| 503 | |
| 504 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) |
| 505 | return -EROFS; |
| 506 | |
| 507 | if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || |
Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 508 | len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 509 | return -EINVAL; |
| 510 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 511 | if (vol->upd_marker) |
| 512 | return -EBADF; |
| 513 | |
| 514 | if (len == 0) |
| 515 | return 0; |
| 516 | |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 517 | return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 518 | } |
| 519 | EXPORT_SYMBOL_GPL(ubi_leb_change); |
| 520 | |
| 521 | /** |
| 522 | * ubi_leb_erase - erase logical eraseblock. |
| 523 | * @desc: volume descriptor |
| 524 | * @lnum: logical eraseblock number |
| 525 | * |
| 526 | * This function un-maps logical eraseblock @lnum and synchronously erases the |
| 527 | * correspondent physical eraseblock. Returns zero in case of success and a |
| 528 | * negative error code in case of failure. |
| 529 | * |
| 530 | * If the volume is damaged because of an interrupted update this function just |
| 531 | * returns immediately with %-EBADF code. |
| 532 | */ |
| 533 | int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) |
| 534 | { |
| 535 | struct ubi_volume *vol = desc->vol; |
| 536 | struct ubi_device *ubi = vol->ubi; |
Artem Bityutskiy | ae616e1 | 2008-01-16 12:15:47 +0200 | [diff] [blame] | 537 | int err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 538 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 539 | dbg_gen("erase LEB %d:%d", vol->vol_id, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 540 | |
| 541 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) |
| 542 | return -EROFS; |
| 543 | |
| 544 | if (lnum < 0 || lnum >= vol->reserved_pebs) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | if (vol->upd_marker) |
| 548 | return -EBADF; |
| 549 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 550 | err = ubi_eba_unmap_leb(ubi, vol, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 551 | if (err) |
| 552 | return err; |
| 553 | |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 554 | return ubi_wl_flush(ubi, vol->vol_id, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 555 | } |
| 556 | EXPORT_SYMBOL_GPL(ubi_leb_erase); |
| 557 | |
| 558 | /** |
| 559 | * ubi_leb_unmap - un-map logical eraseblock. |
| 560 | * @desc: volume descriptor |
| 561 | * @lnum: logical eraseblock number |
| 562 | * |
| 563 | * This function un-maps logical eraseblock @lnum and schedules the |
| 564 | * corresponding physical eraseblock for erasure, so that it will eventually be |
Shinya Kuribayashi | 3f50262 | 2010-05-06 19:21:47 +0900 | [diff] [blame] | 565 | * physically erased in background. This operation is much faster than the |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 566 | * erase operation. |
| 567 | * |
| 568 | * Unlike erase, the un-map operation does not guarantee that the logical |
| 569 | * eraseblock will contain all 0xFF bytes when UBI is initialized again. For |
| 570 | * example, if several logical eraseblocks are un-mapped, and an unclean reboot |
| 571 | * happens after this, the logical eraseblocks will not necessarily be |
| 572 | * un-mapped again when this MTD device is attached. They may actually be |
| 573 | * mapped to the same physical eraseblocks again. So, this function has to be |
| 574 | * used with care. |
| 575 | * |
| 576 | * In other words, when un-mapping a logical eraseblock, UBI does not store |
| 577 | * any information about this on the flash media, it just marks the logical |
| 578 | * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical |
| 579 | * eraseblock is physically erased, it will be mapped again to the same logical |
| 580 | * eraseblock when the MTD device is attached again. |
| 581 | * |
| 582 | * The main and obvious use-case of this function is when the contents of a |
| 583 | * 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] | 584 | * 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] | 585 | * new data. Note, once new data has been written to the logical eraseblock, |
| 586 | * UBI guarantees that the old contents has gone forever. In other words, if an |
| 587 | * unclean reboot happens after the logical eraseblock has been un-mapped and |
| 588 | * then written to, it will contain the last written data. |
| 589 | * |
| 590 | * This function returns zero in case of success and a negative error code in |
| 591 | * case of failure. If the volume is damaged because of an interrupted update |
| 592 | * this function just returns immediately with %-EBADF code. |
| 593 | */ |
| 594 | int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) |
| 595 | { |
| 596 | struct ubi_volume *vol = desc->vol; |
| 597 | struct ubi_device *ubi = vol->ubi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 598 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 599 | dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 600 | |
| 601 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) |
| 602 | return -EROFS; |
| 603 | |
| 604 | if (lnum < 0 || lnum >= vol->reserved_pebs) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | if (vol->upd_marker) |
| 608 | return -EBADF; |
| 609 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 610 | return ubi_eba_unmap_leb(ubi, vol, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 611 | } |
| 612 | EXPORT_SYMBOL_GPL(ubi_leb_unmap); |
| 613 | |
| 614 | /** |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 615 | * ubi_leb_map - map logical eraseblock to a physical eraseblock. |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 616 | * @desc: volume descriptor |
| 617 | * @lnum: logical eraseblock number |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 618 | * |
| 619 | * This function maps an un-mapped logical eraseblock @lnum to a physical |
Coly Li | 73ac36e | 2009-01-07 18:09:16 -0800 | [diff] [blame] | 620 | * eraseblock. This means, that after a successful invocation of this |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 621 | * function the logical eraseblock @lnum will be empty (contain only %0xFF |
| 622 | * bytes) and be mapped to a physical eraseblock, even if an unclean reboot |
| 623 | * happens. |
| 624 | * |
| 625 | * This function returns zero in case of success, %-EBADF if the volume is |
| 626 | * damaged because of an interrupted update, %-EBADMSG if the logical |
| 627 | * eraseblock is already mapped, and other negative error codes in case of |
| 628 | * other failures. |
| 629 | */ |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 630 | int ubi_leb_map(struct ubi_volume_desc *desc, int lnum) |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 631 | { |
| 632 | struct ubi_volume *vol = desc->vol; |
| 633 | struct ubi_device *ubi = vol->ubi; |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 634 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 635 | dbg_gen("unmap LEB %d:%d", vol->vol_id, lnum); |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 636 | |
| 637 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) |
| 638 | return -EROFS; |
| 639 | |
| 640 | if (lnum < 0 || lnum >= vol->reserved_pebs) |
| 641 | return -EINVAL; |
| 642 | |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 643 | if (vol->upd_marker) |
| 644 | return -EBADF; |
| 645 | |
| 646 | if (vol->eba_tbl[lnum] >= 0) |
| 647 | return -EBADMSG; |
| 648 | |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 649 | return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); |
Artem Bityutskiy | 393852e | 2007-12-06 18:47:30 +0200 | [diff] [blame] | 650 | } |
| 651 | EXPORT_SYMBOL_GPL(ubi_leb_map); |
| 652 | |
| 653 | /** |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 654 | * ubi_is_mapped - check if logical eraseblock is mapped. |
| 655 | * @desc: volume descriptor |
| 656 | * @lnum: logical eraseblock number |
| 657 | * |
| 658 | * This function checks if logical eraseblock @lnum is mapped to a physical |
| 659 | * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily |
| 660 | * mean it will still be un-mapped after the UBI device is re-attached. The |
| 661 | * logical eraseblock may become mapped to the physical eraseblock it was last |
| 662 | * mapped to. |
| 663 | * |
| 664 | * This function returns %1 if the LEB is mapped, %0 if not, and a negative |
| 665 | * error code in case of failure. If the volume is damaged because of an |
| 666 | * interrupted update this function just returns immediately with %-EBADF error |
| 667 | * code. |
| 668 | */ |
| 669 | int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) |
| 670 | { |
| 671 | struct ubi_volume *vol = desc->vol; |
| 672 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 673 | dbg_gen("test LEB %d:%d", vol->vol_id, lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 674 | |
| 675 | if (lnum < 0 || lnum >= vol->reserved_pebs) |
| 676 | return -EINVAL; |
| 677 | |
| 678 | if (vol->upd_marker) |
| 679 | return -EBADF; |
| 680 | |
| 681 | return vol->eba_tbl[lnum] >= 0; |
| 682 | } |
| 683 | EXPORT_SYMBOL_GPL(ubi_is_mapped); |
Artem Bityutskiy | a5bf619 | 2008-07-10 18:38:33 +0300 | [diff] [blame] | 684 | |
| 685 | /** |
| 686 | * ubi_sync - synchronize UBI device buffers. |
| 687 | * @ubi_num: UBI device to synchronize |
| 688 | * |
| 689 | * The underlying MTD device may cache data in hardware or in software. This |
| 690 | * function ensures the caches are flushed. Returns zero in case of success and |
| 691 | * a negative error code in case of failure. |
| 692 | */ |
| 693 | int ubi_sync(int ubi_num) |
| 694 | { |
| 695 | struct ubi_device *ubi; |
| 696 | |
| 697 | ubi = ubi_get_device(ubi_num); |
| 698 | if (!ubi) |
| 699 | return -ENODEV; |
| 700 | |
Artem Bityutskiy | 327cf29 | 2011-12-30 16:35:35 +0200 | [diff] [blame] | 701 | mtd_sync(ubi->mtd); |
Artem Bityutskiy | a5bf619 | 2008-07-10 18:38:33 +0300 | [diff] [blame] | 702 | ubi_put_device(ubi); |
| 703 | return 0; |
| 704 | } |
| 705 | EXPORT_SYMBOL_GPL(ubi_sync); |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 706 | |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 707 | /** |
| 708 | * ubi_flush - flush UBI work queue. |
| 709 | * @ubi_num: UBI device to flush work queue |
| 710 | * @vol_id: volume id to flush for |
| 711 | * @lnum: logical eraseblock number to flush for |
| 712 | * |
| 713 | * This function executes all pending works for a particular volume id / logical |
| 714 | * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as |
| 715 | * a wildcard for all of the corresponding volume numbers or logical |
| 716 | * eraseblock numbers. It returns zero in case of success and a negative error |
| 717 | * code in case of failure. |
| 718 | */ |
| 719 | int ubi_flush(int ubi_num, int vol_id, int lnum) |
| 720 | { |
| 721 | struct ubi_device *ubi; |
| 722 | int err = 0; |
| 723 | |
| 724 | ubi = ubi_get_device(ubi_num); |
| 725 | if (!ubi) |
| 726 | return -ENODEV; |
| 727 | |
| 728 | err = ubi_wl_flush(ubi, vol_id, lnum); |
| 729 | ubi_put_device(ubi); |
| 730 | return err; |
| 731 | } |
| 732 | EXPORT_SYMBOL_GPL(ubi_flush); |
| 733 | |
Dmitry Pervushin | 0e0ee1c | 2009-04-29 19:29:38 +0400 | [diff] [blame] | 734 | BLOCKING_NOTIFIER_HEAD(ubi_notifiers); |
| 735 | |
| 736 | /** |
| 737 | * ubi_register_volume_notifier - register a volume notifier. |
| 738 | * @nb: the notifier description object |
| 739 | * @ignore_existing: if non-zero, do not send "added" notification for all |
| 740 | * already existing volumes |
| 741 | * |
| 742 | * This function registers a volume notifier, which means that |
| 743 | * 'nb->notifier_call()' will be invoked when an UBI volume is created, |
| 744 | * removed, re-sized, re-named, or updated. The first argument of the function |
| 745 | * is the notification type. The second argument is pointer to a |
| 746 | * &struct ubi_notification object which describes the notification event. |
| 747 | * Using UBI API from the volume notifier is prohibited. |
| 748 | * |
| 749 | * This function returns zero in case of success and a negative error code |
| 750 | * in case of failure. |
| 751 | */ |
| 752 | int ubi_register_volume_notifier(struct notifier_block *nb, |
| 753 | int ignore_existing) |
| 754 | { |
| 755 | int err; |
| 756 | |
| 757 | err = blocking_notifier_chain_register(&ubi_notifiers, nb); |
| 758 | if (err != 0) |
| 759 | return err; |
| 760 | if (ignore_existing) |
| 761 | return 0; |
| 762 | |
| 763 | /* |
| 764 | * We are going to walk all UBI devices and all volumes, and |
| 765 | * notify the user about existing volumes by the %UBI_VOLUME_ADDED |
| 766 | * event. We have to lock the @ubi_devices_mutex to make sure UBI |
| 767 | * devices do not disappear. |
| 768 | */ |
| 769 | mutex_lock(&ubi_devices_mutex); |
| 770 | ubi_enumerate_volumes(nb); |
| 771 | mutex_unlock(&ubi_devices_mutex); |
| 772 | |
| 773 | return err; |
| 774 | } |
| 775 | EXPORT_SYMBOL_GPL(ubi_register_volume_notifier); |
| 776 | |
| 777 | /** |
| 778 | * ubi_unregister_volume_notifier - unregister the volume notifier. |
| 779 | * @nb: the notifier description object |
| 780 | * |
| 781 | * This function unregisters volume notifier @nm and returns zero in case of |
| 782 | * success and a negative error code in case of failure. |
| 783 | */ |
| 784 | int ubi_unregister_volume_notifier(struct notifier_block *nb) |
| 785 | { |
| 786 | return blocking_notifier_chain_unregister(&ubi_notifiers, nb); |
| 787 | } |
| 788 | EXPORT_SYMBOL_GPL(ubi_unregister_volume_notifier); |