| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Direct MTD block device access |
| 3 | * |
| 4 | * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> |
| 5 | * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/vmalloc.h> |
| 31 | |
| 32 | #include <linux/mtd/mtd.h> |
| 33 | #include <linux/mtd/blktrans.h> |
| 34 | #include <linux/mutex.h> |
| 35 | #include <linux/major.h> |
| 36 | |
| 37 | |
| 38 | struct mtdblk_dev { |
| 39 | struct mtd_blktrans_dev mbd; |
| 40 | int count; |
| 41 | struct mutex cache_mutex; |
| 42 | unsigned char *cache_data; |
| 43 | unsigned long cache_offset; |
| 44 | unsigned int cache_size; |
| 45 | enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state; |
| xf.li | 4cf7639 | 2023-11-15 00:00:38 -0800 | [diff] [blame] | 46 | unsigned short *block_mapping; |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * Cache stuff... |
| 51 | * |
| 52 | * Since typical flash erasable sectors are much larger than what Linux's |
| 53 | * buffer cache can handle, we must implement read-modify-write on flash |
| 54 | * sectors for each block write requests. To avoid over-erasing flash sectors |
| 55 | * and to speed things up, we locally cache a whole flash sector while it is |
| 56 | * being written to until a different sector is required. |
| 57 | */ |
| 58 | |
| 59 | static int erase_write (struct mtd_info *mtd, unsigned long pos, |
| 60 | int len, const char *buf) |
| 61 | { |
| 62 | struct erase_info erase; |
| 63 | size_t retlen; |
| 64 | int ret; |
| 65 | |
| 66 | /* |
| 67 | * First, let's erase the flash block. |
| 68 | */ |
| 69 | erase.addr = pos; |
| 70 | erase.len = len; |
| 71 | |
| 72 | ret = mtd_erase(mtd, &erase); |
| 73 | if (ret) { |
| 74 | printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] " |
| 75 | "on \"%s\" failed\n", |
| 76 | pos, len, mtd->name); |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Next, write the data to flash. |
| 82 | */ |
| 83 | |
| 84 | ret = mtd_write(mtd, pos, len, &retlen, buf); |
| 85 | if (ret) |
| 86 | return ret; |
| 87 | if (retlen != len) |
| 88 | return -EIO; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static int write_cached_data (struct mtdblk_dev *mtdblk) |
| 94 | { |
| 95 | struct mtd_info *mtd = mtdblk->mbd.mtd; |
| 96 | int ret; |
| 97 | |
| 98 | if (mtdblk->cache_state != STATE_DIRTY) |
| 99 | return 0; |
| 100 | |
| 101 | pr_debug("mtdblock: writing cached data for \"%s\" " |
| 102 | "at 0x%lx, size 0x%x\n", mtd->name, |
| 103 | mtdblk->cache_offset, mtdblk->cache_size); |
| 104 | |
| 105 | ret = erase_write (mtd, mtdblk->cache_offset, |
| 106 | mtdblk->cache_size, mtdblk->cache_data); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | |
| 110 | /* |
| 111 | * Here we could arguably set the cache state to STATE_CLEAN. |
| 112 | * However this could lead to inconsistency since we will not |
| 113 | * be notified if this content is altered on the flash by other |
| 114 | * means. Let's declare it empty and leave buffering tasks to |
| 115 | * the buffer cache instead. |
| 116 | */ |
| 117 | mtdblk->cache_state = STATE_EMPTY; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos, |
| 123 | int len, const char *buf) |
| 124 | { |
| 125 | struct mtd_info *mtd = mtdblk->mbd.mtd; |
| 126 | unsigned int sect_size = mtdblk->cache_size; |
| 127 | size_t retlen; |
| 128 | int ret; |
| 129 | |
| 130 | pr_debug("mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n", |
| 131 | mtd->name, pos, len); |
| 132 | |
| 133 | if (!sect_size) |
| 134 | return mtd_write(mtd, pos, len, &retlen, buf); |
| 135 | |
| 136 | while (len > 0) { |
| 137 | unsigned long sect_start = (pos/sect_size)*sect_size; |
| 138 | unsigned int offset = pos - sect_start; |
| 139 | unsigned int size = sect_size - offset; |
| 140 | if( size > len ) |
| 141 | size = len; |
| 142 | |
| 143 | if (size == sect_size) { |
| 144 | /* |
| 145 | * We are covering a whole sector. Thus there is no |
| 146 | * need to bother with the cache while it may still be |
| 147 | * useful for other partial writes. |
| 148 | */ |
| 149 | ret = erase_write (mtd, pos, size, buf); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | } else { |
| 153 | /* Partial sector: need to use the cache */ |
| 154 | |
| 155 | if (mtdblk->cache_state == STATE_DIRTY && |
| 156 | mtdblk->cache_offset != sect_start) { |
| 157 | ret = write_cached_data(mtdblk); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | if (mtdblk->cache_state == STATE_EMPTY || |
| 163 | mtdblk->cache_offset != sect_start) { |
| 164 | /* fill the cache with the current sector */ |
| 165 | mtdblk->cache_state = STATE_EMPTY; |
| 166 | ret = mtd_read(mtd, sect_start, sect_size, |
| 167 | &retlen, mtdblk->cache_data); |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | if (retlen != sect_size) |
| 171 | return -EIO; |
| 172 | |
| 173 | mtdblk->cache_offset = sect_start; |
| 174 | mtdblk->cache_size = sect_size; |
| 175 | mtdblk->cache_state = STATE_CLEAN; |
| 176 | } |
| 177 | |
| 178 | /* write data to our local cache */ |
| 179 | memcpy (mtdblk->cache_data + offset, buf, size); |
| 180 | mtdblk->cache_state = STATE_DIRTY; |
| 181 | } |
| 182 | |
| 183 | buf += size; |
| 184 | pos += size; |
| 185 | len -= size; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos, |
| 193 | int len, char *buf) |
| 194 | { |
| 195 | struct mtd_info *mtd = mtdblk->mbd.mtd; |
| 196 | unsigned int sect_size = mtdblk->cache_size; |
| 197 | size_t retlen; |
| 198 | int ret; |
| 199 | |
| 200 | pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n", |
| 201 | mtd->name, pos, len); |
| 202 | |
| 203 | if (!sect_size) |
| 204 | return mtd_read(mtd, pos, len, &retlen, buf); |
| 205 | |
| 206 | while (len > 0) { |
| 207 | unsigned long sect_start = (pos/sect_size)*sect_size; |
| 208 | unsigned int offset = pos - sect_start; |
| 209 | unsigned int size = sect_size - offset; |
| 210 | if (size > len) |
| 211 | size = len; |
| 212 | |
| 213 | /* |
| 214 | * Check if the requested data is already cached |
| 215 | * Read the requested amount of data from our internal cache if it |
| 216 | * contains what we want, otherwise we read the data directly |
| 217 | * from flash. |
| 218 | */ |
| 219 | if (mtdblk->cache_state != STATE_EMPTY && |
| 220 | mtdblk->cache_offset == sect_start) { |
| 221 | memcpy (buf, mtdblk->cache_data + offset, size); |
| 222 | } else { |
| 223 | ret = mtd_read(mtd, pos, size, &retlen, buf); |
| 224 | if (ret) |
| 225 | return ret; |
| 226 | if (retlen != size) |
| 227 | return -EIO; |
| 228 | } |
| 229 | |
| 230 | buf += size; |
| 231 | pos += size; |
| 232 | len -= size; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int mtdblock_readsect(struct mtd_blktrans_dev *dev, |
| 239 | unsigned long block, char *buf) |
| 240 | { |
| 241 | struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd); |
| xf.li | 4cf7639 | 2023-11-15 00:00:38 -0800 | [diff] [blame] | 242 | struct mtd_info *mtd = dev->mtd; |
| 243 | unsigned short target_block, total_blocks; |
| 244 | unsigned long pos; |
| 245 | |
| 246 | if (!mtdblk->block_mapping) |
| 247 | return do_cached_read(mtdblk, block << 9, 512, buf); |
| 248 | |
| 249 | total_blocks = (unsigned short) ((uint32_t)mtd->size / mtd->erasesize); |
| 250 | target_block = mtdblk->block_mapping[(block << 9) / mtd->erasesize]; |
| 251 | if (target_block >= total_blocks) |
| 252 | return -EIO; |
| 253 | |
| 254 | pos = target_block * mtdblk->mbd.mtd->erasesize + |
| 255 | ((block << 9) & (mtdblk->mbd.mtd->erasesize - 1)); |
| 256 | return do_cached_read(mtdblk, pos, 512, buf); |
| 257 | |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int mtdblock_writesect(struct mtd_blktrans_dev *dev, |
| 261 | unsigned long block, char *buf) |
| 262 | { |
| 263 | struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd); |
| 264 | if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) { |
| 265 | mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize); |
| 266 | if (!mtdblk->cache_data) |
| 267 | return -EINTR; |
| 268 | /* -EINTR is not really correct, but it is the best match |
| 269 | * documented in man 2 write for all cases. We could also |
| 270 | * return -EAGAIN sometimes, but why bother? |
| 271 | */ |
| 272 | } |
| 273 | return do_cached_write(mtdblk, block<<9, 512, buf); |
| 274 | } |
| 275 | |
| 276 | static int mtdblock_open(struct mtd_blktrans_dev *mbd) |
| 277 | { |
| 278 | struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd); |
| xf.li | 4cf7639 | 2023-11-15 00:00:38 -0800 | [diff] [blame] | 279 | struct mtd_info *mtd = mbd->mtd; |
| 280 | unsigned short total_blocks; |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 281 | |
| 282 | pr_debug("mtdblock_open\n"); |
| 283 | |
| 284 | if (mtdblk->count) { |
| 285 | mtdblk->count++; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* OK, it's not open. Create cache info for it */ |
| 290 | mtdblk->count = 1; |
| 291 | mutex_init(&mtdblk->cache_mutex); |
| 292 | mtdblk->cache_state = STATE_EMPTY; |
| 293 | if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) { |
| 294 | mtdblk->cache_size = mbd->mtd->erasesize; |
| 295 | mtdblk->cache_data = NULL; |
| 296 | } |
| 297 | |
| xf.li | 4cf7639 | 2023-11-15 00:00:38 -0800 | [diff] [blame] | 298 | total_blocks = (unsigned short) ((uint32_t)mtd->size / mtd->erasesize); |
| 299 | mtdblk->block_mapping = |
| 300 | vmalloc(total_blocks * sizeof(mtdblk->block_mapping[0])); |
| 301 | if (mtdblk->block_mapping) { |
| 302 | unsigned short i, idx = 0; |
| 303 | |
| 304 | memset(mtdblk->block_mapping, 0xFF, |
| 305 | total_blocks * sizeof(mtdblk->block_mapping[0])); |
| 306 | for (i = 0; i < total_blocks; i++) { |
| 307 | if (mtd->_block_isbad(mtd, i * mtd->erasesize)) { |
| 308 | pr_info("MTDBLOCK%d: block %d is bad\n", |
| 309 | mtd->index, i); |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | mtdblk->block_mapping[idx] = i; |
| 314 | idx++; |
| 315 | } |
| 316 | } |
| 317 | |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 318 | pr_debug("ok\n"); |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static void mtdblock_release(struct mtd_blktrans_dev *mbd) |
| 324 | { |
| 325 | struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd); |
| 326 | |
| 327 | pr_debug("mtdblock_release\n"); |
| 328 | |
| 329 | mutex_lock(&mtdblk->cache_mutex); |
| 330 | write_cached_data(mtdblk); |
| 331 | mutex_unlock(&mtdblk->cache_mutex); |
| 332 | |
| 333 | if (!--mtdblk->count) { |
| 334 | /* |
| 335 | * It was the last usage. Free the cache, but only sync if |
| 336 | * opened for writing. |
| 337 | */ |
| 338 | if (mbd->file_mode & FMODE_WRITE) |
| 339 | mtd_sync(mbd->mtd); |
| 340 | vfree(mtdblk->cache_data); |
| xf.li | 4cf7639 | 2023-11-15 00:00:38 -0800 | [diff] [blame] | 341 | |
| 342 | if (mtdblk->block_mapping) { |
| 343 | vfree(mtdblk->block_mapping); |
| 344 | mtdblk->block_mapping = NULL; |
| 345 | } |
| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | pr_debug("ok\n"); |
| 349 | } |
| 350 | |
| 351 | static int mtdblock_flush(struct mtd_blktrans_dev *dev) |
| 352 | { |
| 353 | struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd); |
| 354 | |
| 355 | mutex_lock(&mtdblk->cache_mutex); |
| 356 | write_cached_data(mtdblk); |
| 357 | mutex_unlock(&mtdblk->cache_mutex); |
| 358 | mtd_sync(dev->mtd); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 363 | { |
| 364 | struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 365 | |
| 366 | if (!dev) |
| 367 | return; |
| 368 | |
| 369 | dev->mbd.mtd = mtd; |
| 370 | dev->mbd.devnum = mtd->index; |
| 371 | |
| 372 | dev->mbd.size = mtd->size >> 9; |
| 373 | dev->mbd.tr = tr; |
| 374 | |
| 375 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 376 | dev->mbd.readonly = 1; |
| 377 | |
| 378 | if (add_mtd_blktrans_dev(&dev->mbd)) |
| 379 | kfree(dev); |
| 380 | } |
| 381 | |
| 382 | static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) |
| 383 | { |
| 384 | del_mtd_blktrans_dev(dev); |
| 385 | } |
| 386 | |
| 387 | static struct mtd_blktrans_ops mtdblock_tr = { |
| 388 | .name = "mtdblock", |
| 389 | .major = MTD_BLOCK_MAJOR, |
| 390 | .part_bits = 0, |
| 391 | .blksize = 512, |
| 392 | .open = mtdblock_open, |
| 393 | .flush = mtdblock_flush, |
| 394 | .release = mtdblock_release, |
| 395 | .readsect = mtdblock_readsect, |
| 396 | .writesect = mtdblock_writesect, |
| 397 | .add_mtd = mtdblock_add_mtd, |
| 398 | .remove_dev = mtdblock_remove_dev, |
| 399 | .owner = THIS_MODULE, |
| 400 | }; |
| 401 | |
| 402 | static int __init init_mtdblock(void) |
| 403 | { |
| 404 | return register_mtd_blktrans(&mtdblock_tr); |
| 405 | } |
| 406 | |
| 407 | static void __exit cleanup_mtdblock(void) |
| 408 | { |
| 409 | deregister_mtd_blktrans(&mtdblock_tr); |
| 410 | } |
| 411 | |
| 412 | module_init(init_mtdblock); |
| 413 | module_exit(cleanup_mtdblock); |
| 414 | |
| 415 | |
| 416 | MODULE_LICENSE("GPL"); |
| 417 | MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al."); |
| 418 | MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices"); |