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