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