ARM: 6965/1: ep93xx: add model detection for ts-7300 and ts-7400 boards
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / logfs / dev_mtd.c
blob339e17e9133d0a87f3f8306062d016ea9d328500
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 if (!mtd->block_isbad)
154 return NULL;
156 *ofs = 0;
157 while (mtd->block_isbad(mtd, *ofs)) {
158 *ofs += mtd->erasesize;
159 if (*ofs >= mtd->size)
160 return NULL;
162 BUG_ON(*ofs & ~PAGE_MASK);
163 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
166 static struct page *mtd_find_last_sb(struct super_block *sb, u64 *ofs)
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;
173 if (!mtd->block_isbad)
174 return NULL;
176 *ofs = mtd->size - mtd->erasesize;
177 while (mtd->block_isbad(mtd, *ofs)) {
178 *ofs -= mtd->erasesize;
179 if (*ofs <= 0)
180 return NULL;
182 *ofs = *ofs + mtd->erasesize - 0x1000;
183 BUG_ON(*ofs & ~PAGE_MASK);
184 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
187 static int __mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
188 size_t nr_pages)
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;
195 for (i = 0; i < nr_pages; i++) {
196 page = find_lock_page(mapping, index + i);
197 BUG_ON(!page);
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;
206 return 0;
209 static void mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
211 struct logfs_super *super = logfs_super(sb);
212 int head;
214 if (super->s_flags & LOGFS_SB_FLAG_RO)
215 return;
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.
222 return;
224 head = ofs & (PAGE_SIZE - 1);
225 if (head) {
226 ofs -= head;
227 len += head;
229 len = PAGE_ALIGN(len);
230 __mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
233 static void mtd_put_device(struct logfs_super *s)
235 put_mtd_device(s->s_mtd);
238 static int mtd_can_write_buf(struct super_block *sb, u64 ofs)
240 struct logfs_super *super = logfs_super(sb);
241 void *buf;
242 int err;
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;
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,
263 .can_write_buf = mtd_can_write_buf,
264 .sync = mtd_sync,
265 .put_device = mtd_put_device,
268 int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
270 struct mtd_info *mtd = get_mtd_device(NULL, mtdnr);
271 if (IS_ERR(mtd))
272 return PTR_ERR(mtd);
274 s->s_bdev = NULL;
275 s->s_mtd = mtd;
276 s->s_devops = &mtd_devops;
277 return 0;