Merge remote-tracking branch 'net/master'
[linux-2.6/next.git] / fs / logfs / dev_mtd.c
blobd054d7e975caf159912a2503f1e792a1efe1c01f
1 /*
2 * fs/logfs/dev_mtd.c - Device access methods for MTD
4 * As should be obvious for Linux kernel code, license is GPLv2
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>
12 #include <linux/slab.h>
14 #define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
16 static int mtd_read(struct super_block *sb, loff_t ofs, size_t len, void *buf)
18 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
19 size_t retlen;
20 int ret;
22 ret = mtd->read(mtd, ofs, len, &retlen, buf);
23 BUG_ON(ret == -EINVAL);
24 if (ret)
25 return ret;
27 /* Not sure if we should loop instead. */
28 if (retlen != len)
29 return -EIO;
31 return 0;
34 static int mtd_write(struct super_block *sb, loff_t ofs, size_t len, void *buf)
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;
42 if (super->s_flags & LOGFS_SB_FLAG_RO)
43 return -EROFS;
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;
54 return 0;
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 exercise in futility!
65 static void logfs_erase_callback(struct erase_info *ei)
67 complete((struct completion *)ei->priv);
70 static int mtd_erase_mapping(struct super_block *sb, loff_t ofs, size_t len)
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;
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);
84 return 0;
87 static int mtd_erase(struct super_block *sb, loff_t ofs, size_t len,
88 int ensure_write)
90 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
91 struct erase_info ei;
92 DECLARE_COMPLETION_ONSTACK(complete);
93 int ret;
95 BUG_ON(len % mtd->erasesize);
96 if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO)
97 return -EROFS;
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;
109 wait_for_completion(&complete);
110 if (ei.state != MTD_ERASE_DONE)
111 return -EIO;
112 return mtd_erase_mapping(sb, ofs, len);
115 static void mtd_sync(struct super_block *sb)
117 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
119 if (mtd->sync)
120 mtd->sync(mtd);
123 static int mtd_readpage(void *_sb, struct page *page)
125 struct super_block *sb = _sb;
126 int err;
128 err = mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
129 page_address(page));
130 if (err == -EUCLEAN || err == -EBADMSG) {
131 /* -EBADMSG happens regularly on power failures */
132 err = 0;
133 /* FIXME: force GC this segment */
135 if (err) {
136 ClearPageUptodate(page);
137 SetPageError(page);
138 } else {
139 SetPageUptodate(page);
140 ClearPageError(page);
142 unlock_page(page);
143 return err;
146 static struct page *mtd_find_first_sb(struct super_block *sb, u64 *ofs)
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;
153 *ofs = 0;
154 if (mtd->block_isbad) {
155 while (mtd->block_isbad(mtd, *ofs)) {
156 *ofs += mtd->erasesize;
157 if (*ofs >= mtd->size)
158 return NULL;
161 BUG_ON(*ofs & ~PAGE_MASK);
162 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
165 static struct page *mtd_find_last_sb(struct super_block *sb, u64 *ofs)
167 struct logfs_super *super = logfs_super(sb);
168 struct address_space *mapping = super->s_mapping_inode->i_mapping;
169 filler_t *filler = mtd_readpage;
170 struct mtd_info *mtd = super->s_mtd;
172 *ofs = mtd->size - mtd->erasesize;
173 if (mtd->block_isbad) {
174 while (mtd->block_isbad(mtd, *ofs)) {
175 *ofs -= mtd->erasesize;
176 if (*ofs <= 0)
177 return NULL;
180 *ofs = *ofs + mtd->erasesize - 0x1000;
181 BUG_ON(*ofs & ~PAGE_MASK);
182 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
185 static int __mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
186 size_t nr_pages)
188 struct logfs_super *super = logfs_super(sb);
189 struct address_space *mapping = super->s_mapping_inode->i_mapping;
190 struct page *page;
191 int i, err;
193 for (i = 0; i < nr_pages; i++) {
194 page = find_lock_page(mapping, index + i);
195 BUG_ON(!page);
197 err = mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
198 page_address(page));
199 unlock_page(page);
200 page_cache_release(page);
201 if (err)
202 return err;
204 return 0;
207 static void mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
209 struct logfs_super *super = logfs_super(sb);
210 int head;
212 if (super->s_flags & LOGFS_SB_FLAG_RO)
213 return;
215 if (len == 0) {
216 /* This can happen when the object fit perfectly into a
217 * segment, the segment gets written per sync and subsequently
218 * closed.
220 return;
222 head = ofs & (PAGE_SIZE - 1);
223 if (head) {
224 ofs -= head;
225 len += head;
227 len = PAGE_ALIGN(len);
228 __mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
231 static void mtd_put_device(struct logfs_super *s)
233 put_mtd_device(s->s_mtd);
236 static int mtd_can_write_buf(struct super_block *sb, u64 ofs)
238 struct logfs_super *super = logfs_super(sb);
239 void *buf;
240 int err;
242 buf = kmalloc(super->s_writesize, GFP_KERNEL);
243 if (!buf)
244 return -ENOMEM;
245 err = mtd_read(sb, ofs, super->s_writesize, buf);
246 if (err)
247 goto out;
248 if (memchr_inv(buf, 0xff, super->s_writesize))
249 err = -EIO;
250 kfree(buf);
251 out:
252 return err;
255 static const struct logfs_device_ops mtd_devops = {
256 .find_first_sb = mtd_find_first_sb,
257 .find_last_sb = mtd_find_last_sb,
258 .readpage = mtd_readpage,
259 .writeseg = mtd_writeseg,
260 .erase = mtd_erase,
261 .can_write_buf = mtd_can_write_buf,
262 .sync = mtd_sync,
263 .put_device = mtd_put_device,
266 int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
268 struct mtd_info *mtd = get_mtd_device(NULL, mtdnr);
269 if (IS_ERR(mtd))
270 return PTR_ERR(mtd);
272 s->s_bdev = NULL;
273 s->s_mtd = mtd;
274 s->s_devops = &mtd_devops;
275 return 0;