blob: b76a62b1978fd699bcf139ef1500720ae7e19cfa [file] [log] [blame]
Joern Engel5db53f32009-11-20 20:13:39 +01001/*
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 Engel6f485b412010-05-07 19:38:40 +020012#include <linux/slab.h>
Joern Engel5db53f32009-11-20 20:13:39 +010013
14#define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))
15
Artem Bityutskiy48d36102011-12-23 15:44:14 +020016static int logfs_mtd_read(struct super_block *sb, loff_t ofs, size_t len,
17 void *buf)
Joern Engel5db53f32009-11-20 20:13:39 +010018{
19 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
20 size_t retlen;
21 int ret;
22
Artem Bityutskiy329ad392011-12-23 17:30:16 +020023 ret = mtd_read(mtd, ofs, len, &retlen, buf);
Joern Engel5db53f32009-11-20 20:13:39 +010024 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 Bityutskiy48d36102011-12-23 15:44:14 +020035static int loffs_mtd_write(struct super_block *sb, loff_t ofs, size_t len,
36 void *buf)
Joern Engel5db53f32009-11-20 20:13:39 +010037{
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);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030049 BUG_ON(len > PAGE_SIZE);
50 page_start = ofs & PAGE_MASK;
51 page_end = PAGE_ALIGN(ofs + len) - 1;
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +020052 ret = mtd_write(mtd, ofs, len, &retlen, buf);
Joern Engel5db53f32009-11-20 20:13:39 +010053 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 Bityutskiy48d36102011-12-23 15:44:14 +020065 * from logfs_mtd_erase(). What an exercise in futility!
Joern Engel5db53f32009-11-20 20:13:39 +010066 */
67static void logfs_erase_callback(struct erase_info *ei)
68{
69 complete((struct completion *)ei->priv);
70}
71
Artem Bityutskiy48d36102011-12-23 15:44:14 +020072static int logfs_mtd_erase_mapping(struct super_block *sb, loff_t ofs,
73 size_t len)
Joern Engel5db53f32009-11-20 20:13:39 +010074{
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);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030085 put_page(page);
Joern Engel5db53f32009-11-20 20:13:39 +010086 }
87 return 0;
88}
89
Artem Bityutskiy48d36102011-12-23 15:44:14 +020090static int logfs_mtd_erase(struct super_block *sb, loff_t ofs, size_t len,
Joern Engel94215022010-03-04 21:30:58 +010091 int ensure_write)
Joern Engel5db53f32009-11-20 20:13:39 +010092{
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 Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200108 ret = mtd_erase(mtd, &ei);
Joern Engel5db53f32009-11-20 20:13:39 +0100109 if (ret)
110 return -EIO;
111
112 wait_for_completion(&complete);
113 if (ei.state != MTD_ERASE_DONE)
114 return -EIO;
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200115 return logfs_mtd_erase_mapping(sb, ofs, len);
Joern Engel5db53f32009-11-20 20:13:39 +0100116}
117
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200118static void logfs_mtd_sync(struct super_block *sb)
Joern Engel5db53f32009-11-20 20:13:39 +0100119{
120 struct mtd_info *mtd = logfs_super(sb)->s_mtd;
121
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200122 mtd_sync(mtd);
Joern Engel5db53f32009-11-20 20:13:39 +0100123}
124
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200125static int logfs_mtd_readpage(void *_sb, struct page *page)
Joern Engel5db53f32009-11-20 20:13:39 +0100126{
127 struct super_block *sb = _sb;
128 int err;
129
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200130 err = logfs_mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
Joern Engel5db53f32009-11-20 20:13:39 +0100131 page_address(page));
Joern Engel6f485b412010-05-07 19:38:40 +0200132 if (err == -EUCLEAN || err == -EBADMSG) {
133 /* -EBADMSG happens regularly on power failures */
Joern Engel5db53f32009-11-20 20:13:39 +0100134 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 Bityutskiy48d36102011-12-23 15:44:14 +0200148static struct page *logfs_mtd_find_first_sb(struct super_block *sb, u64 *ofs)
Joern Engel5db53f32009-11-20 20:13:39 +0100149{
150 struct logfs_super *super = logfs_super(sb);
151 struct address_space *mapping = super->s_mapping_inode->i_mapping;
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200152 filler_t *filler = logfs_mtd_readpage;
Joern Engel5db53f32009-11-20 20:13:39 +0100153 struct mtd_info *mtd = super->s_mtd;
154
Joern Engel5db53f32009-11-20 20:13:39 +0100155 *ofs = 0;
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200156 while (mtd_block_isbad(mtd, *ofs)) {
Joern Engel5db53f32009-11-20 20:13:39 +0100157 *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 Bityutskiy48d36102011-12-23 15:44:14 +0200165static struct page *logfs_mtd_find_last_sb(struct super_block *sb, u64 *ofs)
Joern Engel5db53f32009-11-20 20:13:39 +0100166{
167 struct logfs_super *super = logfs_super(sb);
168 struct address_space *mapping = super->s_mapping_inode->i_mapping;
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200169 filler_t *filler = logfs_mtd_readpage;
Joern Engel5db53f32009-11-20 20:13:39 +0100170 struct mtd_info *mtd = super->s_mtd;
171
Joern Engel5db53f32009-11-20 20:13:39 +0100172 *ofs = mtd->size - mtd->erasesize;
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200173 while (mtd_block_isbad(mtd, *ofs)) {
Joern Engel5db53f32009-11-20 20:13:39 +0100174 *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 Bityutskiy48d36102011-12-23 15:44:14 +0200183static int __logfs_mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
Joern Engel5db53f32009-11-20 20:13:39 +0100184 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 Bityutskiy48d36102011-12-23 15:44:14 +0200195 err = loffs_mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
196 page_address(page));
Joern Engel5db53f32009-11-20 20:13:39 +0100197 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300198 put_page(page);
Joern Engel5db53f32009-11-20 20:13:39 +0100199 if (err)
200 return err;
201 }
202 return 0;
203}
204
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200205static void logfs_mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
Joern Engel5db53f32009-11-20 20:13:39 +0100206{
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 Bityutskiy48d36102011-12-23 15:44:14 +0200226 __logfs_mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
Joern Engel5db53f32009-11-20 20:13:39 +0100227}
228
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200229static void logfs_mtd_put_device(struct logfs_super *s)
Joern Engel5db53f32009-11-20 20:13:39 +0100230{
Al Viroe5a07262010-07-26 12:06:00 +0400231 put_mtd_device(s->s_mtd);
Joern Engel5db53f32009-11-20 20:13:39 +0100232}
233
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200234static int logfs_mtd_can_write_buf(struct super_block *sb, u64 ofs)
Joern Engel6f485b412010-05-07 19:38:40 +0200235{
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 Bityutskiy48d36102011-12-23 15:44:14 +0200243 err = logfs_mtd_read(sb, ofs, super->s_writesize, buf);
Joern Engel6f485b412010-05-07 19:38:40 +0200244 if (err)
245 goto out;
246 if (memchr_inv(buf, 0xff, super->s_writesize))
247 err = -EIO;
248 kfree(buf);
249out:
250 return err;
251}
252
Joern Engel5db53f32009-11-20 20:13:39 +0100253static const struct logfs_device_ops mtd_devops = {
Artem Bityutskiy48d36102011-12-23 15:44:14 +0200254 .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 Engel5db53f32009-11-20 20:13:39 +0100262};
263
Al Viro7d945a32010-07-26 11:53:30 +0400264int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
Joern Engel5db53f32009-11-20 20:13:39 +0100265{
Al Viro0d85c792010-07-26 11:33:39 +0400266 struct mtd_info *mtd = get_mtd_device(NULL, mtdnr);
Al Viro7d945a32010-07-26 11:53:30 +0400267 if (IS_ERR(mtd))
Dan Carpenterccf31c12010-05-07 10:59:06 +0200268 return PTR_ERR(mtd);
Al Viro0d85c792010-07-26 11:33:39 +0400269
270 s->s_bdev = NULL;
271 s->s_mtd = mtd;
272 s->s_devops = &mtd_devops;
Al Viro7d945a32010-07-26 11:53:30 +0400273 return 0;
Joern Engel5db53f32009-11-20 20:13:39 +0100274}