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 (Битюцкий Артём), Joern Engel |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This file includes implementation of fake MTD devices for each UBI volume. |
| 23 | * This sounds strange, but it is in fact quite useful to make MTD-oriented |
| 24 | * software (including all the legacy software) to work on top of UBI. |
| 25 | * |
| 26 | * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit |
| 27 | * size (mtd->writesize) is equivalent to the UBI minimal I/O unit. The |
| 28 | * eraseblock size is equivalent to the logical eraseblock size of the volume. |
| 29 | */ |
| 30 | |
| 31 | #include <asm/div64.h> |
| 32 | #include "ubi.h" |
| 33 | |
| 34 | /** |
| 35 | * gluebi_get_device - get MTD device reference. |
| 36 | * @mtd: the MTD device description object |
| 37 | * |
| 38 | * This function is called every time the MTD device is being opened and |
| 39 | * implements the MTD get_device() operation. Returns zero in case of success |
| 40 | * and a negative error code in case of failure. |
| 41 | */ |
| 42 | static int gluebi_get_device(struct mtd_info *mtd) |
| 43 | { |
| 44 | struct ubi_volume *vol; |
| 45 | |
| 46 | vol = container_of(mtd, struct ubi_volume, gluebi_mtd); |
| 47 | |
| 48 | /* |
| 49 | * We do not introduce locks for gluebi reference count because the |
| 50 | * get_device()/put_device() calls are already serialized at MTD. |
| 51 | */ |
| 52 | if (vol->gluebi_refcount > 0) { |
| 53 | /* |
| 54 | * The MTD device is already referenced and this is just one |
| 55 | * more reference. MTD allows many users to open the same |
| 56 | * volume simultaneously and do not distinguish between |
| 57 | * readers/writers/exclusive openers as UBI does. So we do not |
| 58 | * open the UBI volume again - just increase the reference |
| 59 | * counter and return. |
| 60 | */ |
| 61 | vol->gluebi_refcount += 1; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * This is the first reference to this UBI volume via the MTD device |
| 67 | * interface. Open the corresponding volume in read-write mode. |
| 68 | */ |
| 69 | vol->gluebi_desc = ubi_open_volume(vol->ubi->ubi_num, vol->vol_id, |
| 70 | UBI_READWRITE); |
| 71 | if (IS_ERR(vol->gluebi_desc)) |
| 72 | return PTR_ERR(vol->gluebi_desc); |
| 73 | vol->gluebi_refcount += 1; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * gluebi_put_device - put MTD device reference. |
| 79 | * @mtd: the MTD device description object |
| 80 | * |
| 81 | * This function is called every time the MTD device is being put. Returns |
| 82 | * zero in case of success and a negative error code in case of failure. |
| 83 | */ |
| 84 | static void gluebi_put_device(struct mtd_info *mtd) |
| 85 | { |
| 86 | struct ubi_volume *vol; |
| 87 | |
| 88 | vol = container_of(mtd, struct ubi_volume, gluebi_mtd); |
| 89 | vol->gluebi_refcount -= 1; |
| 90 | ubi_assert(vol->gluebi_refcount >= 0); |
| 91 | if (vol->gluebi_refcount == 0) |
| 92 | ubi_close_volume(vol->gluebi_desc); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * gluebi_read - read operation of emulated MTD devices. |
| 97 | * @mtd: MTD device description object |
| 98 | * @from: absolute offset from where to read |
| 99 | * @len: how many bytes to read |
| 100 | * @retlen: count of read bytes is returned here |
| 101 | * @buf: buffer to store the read data |
| 102 | * |
| 103 | * This function returns zero in case of success and a negative error code in |
| 104 | * case of failure. |
| 105 | */ |
| 106 | static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 107 | size_t *retlen, unsigned char *buf) |
| 108 | { |
| 109 | int err = 0, lnum, offs, total_read; |
| 110 | struct ubi_volume *vol; |
| 111 | struct ubi_device *ubi; |
| 112 | uint64_t tmp = from; |
| 113 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 114 | dbg_gen("read %zd bytes from offset %lld", len, from); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 115 | |
| 116 | if (len < 0 || from < 0 || from + len > mtd->size) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | vol = container_of(mtd, struct ubi_volume, gluebi_mtd); |
| 120 | ubi = vol->ubi; |
| 121 | |
| 122 | offs = do_div(tmp, mtd->erasesize); |
| 123 | lnum = tmp; |
| 124 | |
| 125 | total_read = len; |
| 126 | while (total_read) { |
| 127 | size_t to_read = mtd->erasesize - offs; |
| 128 | |
| 129 | if (to_read > total_read) |
| 130 | to_read = total_read; |
| 131 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 132 | err = ubi_eba_read_leb(ubi, vol, lnum, buf, offs, to_read, 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 133 | if (err) |
| 134 | break; |
| 135 | |
| 136 | lnum += 1; |
| 137 | offs = 0; |
| 138 | total_read -= to_read; |
| 139 | buf += to_read; |
| 140 | } |
| 141 | |
| 142 | *retlen = len - total_read; |
| 143 | return err; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * gluebi_write - write operation of emulated MTD devices. |
| 148 | * @mtd: MTD device description object |
| 149 | * @to: absolute offset where to write |
| 150 | * @len: how many bytes to write |
| 151 | * @retlen: count of written bytes is returned here |
| 152 | * @buf: buffer with data to write |
| 153 | * |
| 154 | * This function returns zero in case of success and a negative error code in |
| 155 | * case of failure. |
| 156 | */ |
| 157 | static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 158 | size_t *retlen, const u_char *buf) |
| 159 | { |
| 160 | int err = 0, lnum, offs, total_written; |
| 161 | struct ubi_volume *vol; |
| 162 | struct ubi_device *ubi; |
| 163 | uint64_t tmp = to; |
| 164 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 165 | dbg_gen("write %zd bytes to offset %lld", len, to); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 166 | |
| 167 | if (len < 0 || to < 0 || len + to > mtd->size) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | vol = container_of(mtd, struct ubi_volume, gluebi_mtd); |
| 171 | ubi = vol->ubi; |
| 172 | |
| 173 | if (ubi->ro_mode) |
| 174 | return -EROFS; |
| 175 | |
| 176 | offs = do_div(tmp, mtd->erasesize); |
| 177 | lnum = tmp; |
| 178 | |
| 179 | if (len % mtd->writesize || offs % mtd->writesize) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | total_written = len; |
| 183 | while (total_written) { |
| 184 | size_t to_write = mtd->erasesize - offs; |
| 185 | |
| 186 | if (to_write > total_written) |
| 187 | to_write = total_written; |
| 188 | |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 189 | err = ubi_eba_write_leb(ubi, vol, lnum, buf, offs, to_write, |
| 190 | UBI_UNKNOWN); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 191 | if (err) |
| 192 | break; |
| 193 | |
| 194 | lnum += 1; |
| 195 | offs = 0; |
| 196 | total_written -= to_write; |
| 197 | buf += to_write; |
| 198 | } |
| 199 | |
| 200 | *retlen = len - total_written; |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * gluebi_erase - erase operation of emulated MTD devices. |
| 206 | * @mtd: the MTD device description object |
| 207 | * @instr: the erase operation description |
| 208 | * |
| 209 | * This function calls the erase callback when finishes. Returns zero in case |
| 210 | * of success and a negative error code in case of failure. |
| 211 | */ |
| 212 | static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 213 | { |
| 214 | int err, i, lnum, count; |
| 215 | struct ubi_volume *vol; |
| 216 | struct ubi_device *ubi; |
| 217 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 218 | dbg_gen("erase %llu bytes at offset %llu", (unsigned long long)instr->len, |
| 219 | (unsigned long long)instr->addr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 220 | |
| 221 | if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | if (instr->len < 0 || instr->addr + instr->len > mtd->size) |
| 225 | return -EINVAL; |
| 226 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 227 | if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 228 | return -EINVAL; |
| 229 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 230 | lnum = mtd_div_by_eb(instr->addr, mtd); |
| 231 | count = mtd_div_by_eb(instr->len, mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 232 | |
| 233 | vol = container_of(mtd, struct ubi_volume, gluebi_mtd); |
| 234 | ubi = vol->ubi; |
| 235 | |
| 236 | if (ubi->ro_mode) |
| 237 | return -EROFS; |
| 238 | |
| 239 | for (i = 0; i < count; i++) { |
Artem Bityutskiy | 89b96b6 | 2007-12-16 20:00:38 +0200 | [diff] [blame] | 240 | err = ubi_eba_unmap_leb(ubi, vol, lnum + i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 241 | if (err) |
| 242 | goto out_err; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * MTD erase operations are synchronous, so we have to make sure the |
| 247 | * physical eraseblock is wiped out. |
| 248 | */ |
| 249 | err = ubi_wl_flush(ubi); |
| 250 | if (err) |
| 251 | goto out_err; |
| 252 | |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 253 | instr->state = MTD_ERASE_DONE; |
| 254 | mtd_erase_callback(instr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 255 | return 0; |
| 256 | |
| 257 | out_err: |
| 258 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 259 | instr->fail_addr = (long long)lnum * mtd->erasesize; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 260 | return err; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * ubi_create_gluebi - initialize gluebi for an UBI volume. |
| 265 | * @ubi: UBI device description object |
| 266 | * @vol: volume description object |
| 267 | * |
| 268 | * This function is called when an UBI volume is created in order to create |
| 269 | * corresponding fake MTD device. Returns zero in case of success and a |
| 270 | * negative error code in case of failure. |
| 271 | */ |
| 272 | int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol) |
| 273 | { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 274 | struct mtd_info *mtd = &vol->gluebi_mtd; |
| 275 | |
| 276 | mtd->name = kmemdup(vol->name, vol->name_len + 1, GFP_KERNEL); |
| 277 | if (!mtd->name) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | mtd->type = MTD_UBIVOLUME; |
| 281 | if (!ubi->ro_mode) |
| 282 | mtd->flags = MTD_WRITEABLE; |
| 283 | mtd->writesize = ubi->min_io_size; |
| 284 | mtd->owner = THIS_MODULE; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 285 | mtd->erasesize = vol->usable_leb_size; |
| 286 | mtd->read = gluebi_read; |
| 287 | mtd->write = gluebi_write; |
| 288 | mtd->erase = gluebi_erase; |
| 289 | mtd->get_device = gluebi_get_device; |
| 290 | mtd->put_device = gluebi_put_device; |
| 291 | |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 292 | /* |
| 293 | * In case of dynamic volume, MTD device size is just volume size. In |
| 294 | * case of a static volume the size is equivalent to the amount of data |
Jan Altenberg | cbd8a9d | 2008-03-28 16:13:53 +0100 | [diff] [blame] | 295 | * bytes. |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 296 | */ |
| 297 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 298 | mtd->size = (long long)vol->usable_leb_size * vol->reserved_pebs; |
Jan Altenberg | cbd8a9d | 2008-03-28 16:13:53 +0100 | [diff] [blame] | 299 | else |
| 300 | mtd->size = vol->used_bytes; |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 301 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 302 | if (add_mtd_device(mtd)) { |
Artem Bityutskiy | bb84c1a | 2008-07-14 12:57:27 +0300 | [diff] [blame] | 303 | ubi_err("cannot not add MTD device"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 304 | kfree(mtd->name); |
| 305 | return -ENFILE; |
| 306 | } |
| 307 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 308 | dbg_gen("added mtd%d (\"%s\"), size %llu, EB size %u", |
| 309 | mtd->index, mtd->name, (unsigned long long)mtd->size, mtd->erasesize); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * ubi_destroy_gluebi - close gluebi for an UBI volume. |
| 315 | * @vol: volume description object |
| 316 | * |
| 317 | * This function is called when an UBI volume is removed in order to remove |
| 318 | * corresponding fake MTD device. Returns zero in case of success and a |
| 319 | * negative error code in case of failure. |
| 320 | */ |
| 321 | int ubi_destroy_gluebi(struct ubi_volume *vol) |
| 322 | { |
| 323 | int err; |
| 324 | struct mtd_info *mtd = &vol->gluebi_mtd; |
| 325 | |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 326 | dbg_gen("remove mtd%d", mtd->index); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 327 | err = del_mtd_device(mtd); |
| 328 | if (err) |
| 329 | return err; |
| 330 | kfree(mtd->name); |
| 331 | return 0; |
| 332 | } |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * ubi_gluebi_updated - UBI volume was updated notifier. |
| 336 | * @vol: volume description object |
| 337 | * |
| 338 | * This function is called every time an UBI volume is updated. This function |
| 339 | * does nothing if volume @vol is dynamic, and changes MTD device size if the |
| 340 | * volume is static. This is needed because static volumes cannot be read past |
| 341 | * data they contain. |
| 342 | */ |
| 343 | void ubi_gluebi_updated(struct ubi_volume *vol) |
| 344 | { |
| 345 | struct mtd_info *mtd = &vol->gluebi_mtd; |
| 346 | |
| 347 | if (vol->vol_type == UBI_STATIC_VOLUME) |
| 348 | mtd->size = vol->used_bytes; |
| 349 | } |