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