aboutsummaryrefslogtreecommitdiffstats
path: root/fs/logfs/dev_mtd.c
diff options
context:
space:
mode:
authorJoern Engel <joern@logfs.org>2009-11-20 14:13:39 -0500
committerJoern Engel <joern@logfs.org>2009-11-20 14:13:39 -0500
commit5db53f3e80dee2d9dff5e534f9e9fe1db17c9936 (patch)
tree066f2873eeb7eb86466f6389e45892d957db3de2 /fs/logfs/dev_mtd.c
parent66b00a7c93ec782d118d2c03bd599cfd041e80a1 (diff)
[LogFS] add new flash file system
This is a new flash file system. See Documentation/filesystems/logfs.txt Signed-off-by: Joern Engel <joern@logfs.org>
Diffstat (limited to 'fs/logfs/dev_mtd.c')
-rw-r--r--fs/logfs/dev_mtd.c253
1 files changed, 253 insertions, 0 deletions
diff --git a/fs/logfs/dev_mtd.c b/fs/logfs/dev_mtd.c
new file mode 100644
index 000000000000..68e99d046c23
--- /dev/null
+++ b/fs/logfs/dev_mtd.c
@@ -0,0 +1,253 @@
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>
12
13#define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
14
15static int mtd_read(struct super_block *sb, loff_t ofs, size_t len, void *buf)
16{
17 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
18 size_t retlen;
19 int ret;
20
21 ret = mtd->read(mtd, ofs, len, &retlen, buf);
22 BUG_ON(ret == -EINVAL);
23 if (ret)
24 return ret;
25
26 /* Not sure if we should loop instead. */
27 if (retlen != len)
28 return -EIO;
29
30 return 0;
31}
32
33static int mtd_write(struct super_block *sb, loff_t ofs, size_t len, void *buf)
34{
35 struct logfs_super *super = logfs_super(sb);
36 struct mtd_info *mtd = super->s_mtd;
37 size_t retlen;
38 loff_t page_start, page_end;
39 int ret;
40
41 if (super->s_flags & LOGFS_SB_FLAG_RO)
42 return -EROFS;
43
44 BUG_ON((ofs >= mtd->size) || (len > mtd->size - ofs));
45 BUG_ON(ofs != (ofs >> super->s_writeshift) << super->s_writeshift);
46 BUG_ON(len > PAGE_CACHE_SIZE);
47 page_start = ofs & PAGE_CACHE_MASK;
48 page_end = PAGE_CACHE_ALIGN(ofs + len) - 1;
49 ret = mtd->write(mtd, ofs, len, &retlen, buf);
50 if (ret || (retlen != len))
51 return -EIO;
52
53 return 0;
54}
55
56/*
57 * For as long as I can remember (since about 2001) mtd->erase has been an
58 * asynchronous interface lacking the first driver to actually use the
59 * asynchronous properties. So just to prevent the first implementor of such
60 * a thing from breaking logfs in 2350, we do the usual pointless dance to
61 * declare a completion variable and wait for completion before returning
62 * from mtd_erase(). What an excercise in futility!
63 */
64static void logfs_erase_callback(struct erase_info *ei)
65{
66 complete((struct completion *)ei->priv);
67}
68
69static int mtd_erase_mapping(struct super_block *sb, loff_t ofs, size_t len)
70{
71 struct logfs_super *super = logfs_super(sb);
72 struct address_space *mapping = super->s_mapping_inode->i_mapping;
73 struct page *page;
74 pgoff_t index = ofs >> PAGE_SHIFT;
75
76 for (index = ofs >> PAGE_SHIFT; index < (ofs + len) >> PAGE_SHIFT; index++) {
77 page = find_get_page(mapping, index);
78 if (!page)
79 continue;
80 memset(page_address(page), 0xFF, PAGE_SIZE);
81 page_cache_release(page);
82 }
83 return 0;
84}
85
86static int mtd_erase(struct super_block *sb, loff_t ofs, size_t len)
87{
88 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
89 struct erase_info ei;
90 DECLARE_COMPLETION_ONSTACK(complete);
91 int ret;
92
93 BUG_ON(len % mtd->erasesize);
94 if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO)
95 return -EROFS;
96
97 memset(&ei, 0, sizeof(ei));
98 ei.mtd = mtd;
99 ei.addr = ofs;
100 ei.len = len;
101 ei.callback = logfs_erase_callback;
102 ei.priv = (long)&complete;
103 ret = mtd->erase(mtd, &ei);
104 if (ret)
105 return -EIO;
106
107 wait_for_completion(&complete);
108 if (ei.state != MTD_ERASE_DONE)
109 return -EIO;
110 return mtd_erase_mapping(sb, ofs, len);
111}
112
113static void mtd_sync(struct super_block *sb)
114{
115 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
116
117 if (mtd->sync)
118 mtd->sync(mtd);
119}
120
121static int mtd_readpage(void *_sb, struct page *page)
122{
123 struct super_block *sb = _sb;
124 int err;
125
126 err = mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
127 page_address(page));
128 if (err == -EUCLEAN) {
129 err = 0;
130 /* FIXME: force GC this segment */
131 }
132 if (err) {
133 ClearPageUptodate(page);
134 SetPageError(page);
135 } else {
136 SetPageUptodate(page);
137 ClearPageError(page);
138 }
139 unlock_page(page);
140 return err;
141}
142
143static struct page *mtd_find_first_sb(struct super_block *sb, u64 *ofs)
144{
145 struct logfs_super *super = logfs_super(sb);
146 struct address_space *mapping = super->s_mapping_inode->i_mapping;
147 filler_t *filler = mtd_readpage;
148 struct mtd_info *mtd = super->s_mtd;
149
150 if (!mtd->block_isbad)
151 return NULL;
152
153 *ofs = 0;
154 while (mtd->block_isbad(mtd, *ofs)) {
155 *ofs += mtd->erasesize;
156 if (*ofs >= mtd->size)
157 return NULL;
158 }
159 BUG_ON(*ofs & ~PAGE_MASK);
160 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
161}
162
163static struct page *mtd_find_last_sb(struct super_block *sb, u64 *ofs)
164{
165 struct logfs_super *super = logfs_super(sb);
166 struct address_space *mapping = super->s_mapping_inode->i_mapping;
167 filler_t *filler = mtd_readpage;
168 struct mtd_info *mtd = super->s_mtd;
169
170 if (!mtd->block_isbad)
171 return NULL;
172
173 *ofs = mtd->size - mtd->erasesize;
174 while (mtd->block_isbad(mtd, *ofs)) {
175 *ofs -= mtd->erasesize;
176 if (*ofs <= 0)
177 return NULL;
178 }
179 *ofs = *ofs + mtd->erasesize - 0x1000;
180 BUG_ON(*ofs & ~PAGE_MASK);
181 return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
182}
183
184static int __mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
185 size_t nr_pages)
186{
187 struct logfs_super *super = logfs_super(sb);
188 struct address_space *mapping = super->s_mapping_inode->i_mapping;
189 struct page *page;
190 int i, err;
191
192 for (i = 0; i < nr_pages; i++) {
193 page = find_lock_page(mapping, index + i);
194 BUG_ON(!page);
195
196 err = mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
197 page_address(page));
198 unlock_page(page);
199 page_cache_release(page);
200 if (err)
201 return err;
202 }
203 return 0;
204}
205
206static void mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
207{
208 struct logfs_super *super = logfs_super(sb);
209 int head;
210
211 if (super->s_flags & LOGFS_SB_FLAG_RO)
212 return;
213
214 if (len == 0) {
215 /* This can happen when the object fit perfectly into a
216 * segment, the segment gets written per sync and subsequently
217 * closed.
218 */
219 return;
220 }
221 head = ofs & (PAGE_SIZE - 1);
222 if (head) {
223 ofs -= head;
224 len += head;
225 }
226 len = PAGE_ALIGN(len);
227 __mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
228}
229
230static void mtd_put_device(struct super_block *sb)
231{
232 put_mtd_device(logfs_super(sb)->s_mtd);
233}
234
235static const struct logfs_device_ops mtd_devops = {
236 .find_first_sb = mtd_find_first_sb,
237 .find_last_sb = mtd_find_last_sb,
238 .readpage = mtd_readpage,
239 .writeseg = mtd_writeseg,
240 .erase = mtd_erase,
241 .sync = mtd_sync,
242 .put_device = mtd_put_device,
243};
244
245int logfs_get_sb_mtd(struct file_system_type *type, int flags,
246 int mtdnr, struct vfsmount *mnt)
247{
248 struct mtd_info *mtd;
249 const struct logfs_device_ops *devops = &mtd_devops;
250
251 mtd = get_mtd_device(NULL, mtdnr);
252 return logfs_get_sb_device(type, flags, mtd, NULL, devops, mnt);
253}