Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fs/logfs/dev_mtd.c - Device access methods for MTD |
| 3 | * |
| 4 | * As should be obvious for Linux kernel code, license is GPLv2 |
| 5 | * |
| 6 | * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org> |
| 7 | */ |
| 8 | #include "logfs.h" |
| 9 | #include <linux/completion.h> |
| 10 | #include <linux/mount.h> |
| 11 | #include <linux/sched.h> |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 12 | #include <linux/slab.h> |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 13 | |
| 14 | #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1)) |
| 15 | |
| 16 | static int mtd_read(struct super_block *sb, loff_t ofs, size_t len, void *buf) |
| 17 | { |
| 18 | struct mtd_info *mtd = logfs_super(sb)->s_mtd; |
| 19 | size_t retlen; |
| 20 | int ret; |
| 21 | |
| 22 | ret = mtd->read(mtd, ofs, len, &retlen, buf); |
| 23 | BUG_ON(ret == -EINVAL); |
| 24 | if (ret) |
| 25 | return ret; |
| 26 | |
| 27 | /* Not sure if we should loop instead. */ |
| 28 | if (retlen != len) |
| 29 | return -EIO; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static int mtd_write(struct super_block *sb, loff_t ofs, size_t len, void *buf) |
| 35 | { |
| 36 | struct logfs_super *super = logfs_super(sb); |
| 37 | struct mtd_info *mtd = super->s_mtd; |
| 38 | size_t retlen; |
| 39 | loff_t page_start, page_end; |
| 40 | int ret; |
| 41 | |
| 42 | if (super->s_flags & LOGFS_SB_FLAG_RO) |
| 43 | return -EROFS; |
| 44 | |
| 45 | BUG_ON((ofs >= mtd->size) || (len > mtd->size - ofs)); |
| 46 | BUG_ON(ofs != (ofs >> super->s_writeshift) << super->s_writeshift); |
| 47 | BUG_ON(len > PAGE_CACHE_SIZE); |
| 48 | page_start = ofs & PAGE_CACHE_MASK; |
| 49 | page_end = PAGE_CACHE_ALIGN(ofs + len) - 1; |
| 50 | ret = mtd->write(mtd, ofs, len, &retlen, buf); |
| 51 | if (ret || (retlen != len)) |
| 52 | return -EIO; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * For as long as I can remember (since about 2001) mtd->erase has been an |
| 59 | * asynchronous interface lacking the first driver to actually use the |
| 60 | * asynchronous properties. So just to prevent the first implementor of such |
| 61 | * a thing from breaking logfs in 2350, we do the usual pointless dance to |
| 62 | * declare a completion variable and wait for completion before returning |
| 63 | * from mtd_erase(). What an excercise in futility! |
| 64 | */ |
| 65 | static void logfs_erase_callback(struct erase_info *ei) |
| 66 | { |
| 67 | complete((struct completion *)ei->priv); |
| 68 | } |
| 69 | |
| 70 | static int mtd_erase_mapping(struct super_block *sb, loff_t ofs, size_t len) |
| 71 | { |
| 72 | struct logfs_super *super = logfs_super(sb); |
| 73 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 74 | struct page *page; |
| 75 | pgoff_t index = ofs >> PAGE_SHIFT; |
| 76 | |
| 77 | for (index = ofs >> PAGE_SHIFT; index < (ofs + len) >> PAGE_SHIFT; index++) { |
| 78 | page = find_get_page(mapping, index); |
| 79 | if (!page) |
| 80 | continue; |
| 81 | memset(page_address(page), 0xFF, PAGE_SIZE); |
| 82 | page_cache_release(page); |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 87 | static int mtd_erase(struct super_block *sb, loff_t ofs, size_t len, |
| 88 | int ensure_write) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 89 | { |
| 90 | struct mtd_info *mtd = logfs_super(sb)->s_mtd; |
| 91 | struct erase_info ei; |
| 92 | DECLARE_COMPLETION_ONSTACK(complete); |
| 93 | int ret; |
| 94 | |
| 95 | BUG_ON(len % mtd->erasesize); |
| 96 | if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO) |
| 97 | return -EROFS; |
| 98 | |
| 99 | memset(&ei, 0, sizeof(ei)); |
| 100 | ei.mtd = mtd; |
| 101 | ei.addr = ofs; |
| 102 | ei.len = len; |
| 103 | ei.callback = logfs_erase_callback; |
| 104 | ei.priv = (long)&complete; |
| 105 | ret = mtd->erase(mtd, &ei); |
| 106 | if (ret) |
| 107 | return -EIO; |
| 108 | |
| 109 | wait_for_completion(&complete); |
| 110 | if (ei.state != MTD_ERASE_DONE) |
| 111 | return -EIO; |
| 112 | return mtd_erase_mapping(sb, ofs, len); |
| 113 | } |
| 114 | |
| 115 | static void mtd_sync(struct super_block *sb) |
| 116 | { |
| 117 | struct mtd_info *mtd = logfs_super(sb)->s_mtd; |
| 118 | |
| 119 | if (mtd->sync) |
| 120 | mtd->sync(mtd); |
| 121 | } |
| 122 | |
| 123 | static int mtd_readpage(void *_sb, struct page *page) |
| 124 | { |
| 125 | struct super_block *sb = _sb; |
| 126 | int err; |
| 127 | |
| 128 | err = mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE, |
| 129 | page_address(page)); |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 130 | if (err == -EUCLEAN || err == -EBADMSG) { |
| 131 | /* -EBADMSG happens regularly on power failures */ |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 132 | err = 0; |
| 133 | /* FIXME: force GC this segment */ |
| 134 | } |
| 135 | if (err) { |
| 136 | ClearPageUptodate(page); |
| 137 | SetPageError(page); |
| 138 | } else { |
| 139 | SetPageUptodate(page); |
| 140 | ClearPageError(page); |
| 141 | } |
| 142 | unlock_page(page); |
| 143 | return err; |
| 144 | } |
| 145 | |
| 146 | static struct page *mtd_find_first_sb(struct super_block *sb, u64 *ofs) |
| 147 | { |
| 148 | struct logfs_super *super = logfs_super(sb); |
| 149 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 150 | filler_t *filler = mtd_readpage; |
| 151 | struct mtd_info *mtd = super->s_mtd; |
| 152 | |
| 153 | if (!mtd->block_isbad) |
| 154 | return NULL; |
| 155 | |
| 156 | *ofs = 0; |
| 157 | while (mtd->block_isbad(mtd, *ofs)) { |
| 158 | *ofs += mtd->erasesize; |
| 159 | if (*ofs >= mtd->size) |
| 160 | return NULL; |
| 161 | } |
| 162 | BUG_ON(*ofs & ~PAGE_MASK); |
| 163 | return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb); |
| 164 | } |
| 165 | |
| 166 | static struct page *mtd_find_last_sb(struct super_block *sb, u64 *ofs) |
| 167 | { |
| 168 | struct logfs_super *super = logfs_super(sb); |
| 169 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 170 | filler_t *filler = mtd_readpage; |
| 171 | struct mtd_info *mtd = super->s_mtd; |
| 172 | |
| 173 | if (!mtd->block_isbad) |
| 174 | return NULL; |
| 175 | |
| 176 | *ofs = mtd->size - mtd->erasesize; |
| 177 | while (mtd->block_isbad(mtd, *ofs)) { |
| 178 | *ofs -= mtd->erasesize; |
| 179 | if (*ofs <= 0) |
| 180 | return NULL; |
| 181 | } |
| 182 | *ofs = *ofs + mtd->erasesize - 0x1000; |
| 183 | BUG_ON(*ofs & ~PAGE_MASK); |
| 184 | return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb); |
| 185 | } |
| 186 | |
| 187 | static int __mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index, |
| 188 | size_t nr_pages) |
| 189 | { |
| 190 | struct logfs_super *super = logfs_super(sb); |
| 191 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 192 | struct page *page; |
| 193 | int i, err; |
| 194 | |
| 195 | for (i = 0; i < nr_pages; i++) { |
| 196 | page = find_lock_page(mapping, index + i); |
| 197 | BUG_ON(!page); |
| 198 | |
| 199 | err = mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE, |
| 200 | page_address(page)); |
| 201 | unlock_page(page); |
| 202 | page_cache_release(page); |
| 203 | if (err) |
| 204 | return err; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void mtd_writeseg(struct super_block *sb, u64 ofs, size_t len) |
| 210 | { |
| 211 | struct logfs_super *super = logfs_super(sb); |
| 212 | int head; |
| 213 | |
| 214 | if (super->s_flags & LOGFS_SB_FLAG_RO) |
| 215 | return; |
| 216 | |
| 217 | if (len == 0) { |
| 218 | /* This can happen when the object fit perfectly into a |
| 219 | * segment, the segment gets written per sync and subsequently |
| 220 | * closed. |
| 221 | */ |
| 222 | return; |
| 223 | } |
| 224 | head = ofs & (PAGE_SIZE - 1); |
| 225 | if (head) { |
| 226 | ofs -= head; |
| 227 | len += head; |
| 228 | } |
| 229 | len = PAGE_ALIGN(len); |
| 230 | __mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT); |
| 231 | } |
| 232 | |
Al Viro | e5a0726 | 2010-07-26 12:06:00 +0400 | [diff] [blame] | 233 | static void mtd_put_device(struct logfs_super *s) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 234 | { |
Al Viro | e5a0726 | 2010-07-26 12:06:00 +0400 | [diff] [blame] | 235 | put_mtd_device(s->s_mtd); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 236 | } |
| 237 | |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 238 | static int mtd_can_write_buf(struct super_block *sb, u64 ofs) |
| 239 | { |
| 240 | struct logfs_super *super = logfs_super(sb); |
| 241 | void *buf; |
| 242 | int err; |
| 243 | |
| 244 | buf = kmalloc(super->s_writesize, GFP_KERNEL); |
| 245 | if (!buf) |
| 246 | return -ENOMEM; |
| 247 | err = mtd_read(sb, ofs, super->s_writesize, buf); |
| 248 | if (err) |
| 249 | goto out; |
| 250 | if (memchr_inv(buf, 0xff, super->s_writesize)) |
| 251 | err = -EIO; |
| 252 | kfree(buf); |
| 253 | out: |
| 254 | return err; |
| 255 | } |
| 256 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 257 | static const struct logfs_device_ops mtd_devops = { |
| 258 | .find_first_sb = mtd_find_first_sb, |
| 259 | .find_last_sb = mtd_find_last_sb, |
| 260 | .readpage = mtd_readpage, |
| 261 | .writeseg = mtd_writeseg, |
| 262 | .erase = mtd_erase, |
Joern Engel | 6f485b41 | 2010-05-07 19:38:40 +0200 | [diff] [blame] | 263 | .can_write_buf = mtd_can_write_buf, |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 264 | .sync = mtd_sync, |
| 265 | .put_device = mtd_put_device, |
| 266 | }; |
| 267 | |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 268 | int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 269 | { |
Al Viro | 0d85c79 | 2010-07-26 11:33:39 +0400 | [diff] [blame] | 270 | struct mtd_info *mtd = get_mtd_device(NULL, mtdnr); |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 271 | if (IS_ERR(mtd)) |
Dan Carpenter | ccf31c1 | 2010-05-07 10:59:06 +0200 | [diff] [blame] | 272 | return PTR_ERR(mtd); |
Al Viro | 0d85c79 | 2010-07-26 11:33:39 +0400 | [diff] [blame] | 273 | |
| 274 | s->s_bdev = NULL; |
| 275 | s->s_mtd = mtd; |
| 276 | s->s_devops = &mtd_devops; |
Al Viro | 7d945a3 | 2010-07-26 11:53:30 +0400 | [diff] [blame] | 277 | return 0; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 278 | } |