dev_mtd.c 6.6 KB
Newer Older
J
Joern Engel 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * fs/logfs/dev_mtd.c	- Device access methods for MTD
 *
 * As should be obvious for Linux kernel code, license is GPLv2
 *
 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
 */
#include "logfs.h"
#include <linux/completion.h>
#include <linux/mount.h>
#include <linux/sched.h>
12
#include <linux/slab.h>
J
Joern Engel 已提交
13 14 15

#define PAGE_OFS(ofs) ((ofs) & (PAGE_SIZE-1))

16 17
static int logfs_mtd_read(struct super_block *sb, loff_t ofs, size_t len,
			void *buf)
J
Joern Engel 已提交
18 19 20 21 22
{
	struct mtd_info *mtd = logfs_super(sb)->s_mtd;
	size_t retlen;
	int ret;

23
	ret = mtd_read(mtd, ofs, len, &retlen, buf);
J
Joern Engel 已提交
24 25 26 27 28 29 30 31 32 33 34
	BUG_ON(ret == -EINVAL);
	if (ret)
		return ret;

	/* Not sure if we should loop instead. */
	if (retlen != len)
		return -EIO;

	return 0;
}

35 36
static int loffs_mtd_write(struct super_block *sb, loff_t ofs, size_t len,
			void *buf)
J
Joern Engel 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
	struct logfs_super *super = logfs_super(sb);
	struct mtd_info *mtd = super->s_mtd;
	size_t retlen;
	loff_t page_start, page_end;
	int ret;

	if (super->s_flags & LOGFS_SB_FLAG_RO)
		return -EROFS;

	BUG_ON((ofs >= mtd->size) || (len > mtd->size - ofs));
	BUG_ON(ofs != (ofs >> super->s_writeshift) << super->s_writeshift);
	BUG_ON(len > PAGE_CACHE_SIZE);
	page_start = ofs & PAGE_CACHE_MASK;
	page_end = PAGE_CACHE_ALIGN(ofs + len) - 1;
52
	ret = mtd_write(mtd, ofs, len, &retlen, buf);
J
Joern Engel 已提交
53 54 55 56 57 58 59 60 61 62 63 64
	if (ret || (retlen != len))
		return -EIO;

	return 0;
}

/*
 * For as long as I can remember (since about 2001) mtd->erase has been an
 * asynchronous interface lacking the first driver to actually use the
 * asynchronous properties.  So just to prevent the first implementor of such
 * a thing from breaking logfs in 2350, we do the usual pointless dance to
 * declare a completion variable and wait for completion before returning
65
 * from logfs_mtd_erase().  What an exercise in futility!
J
Joern Engel 已提交
66 67 68 69 70 71
 */
static void logfs_erase_callback(struct erase_info *ei)
{
	complete((struct completion *)ei->priv);
}

72 73
static int logfs_mtd_erase_mapping(struct super_block *sb, loff_t ofs,
				size_t len)
J
Joern Engel 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
	struct logfs_super *super = logfs_super(sb);
	struct address_space *mapping = super->s_mapping_inode->i_mapping;
	struct page *page;
	pgoff_t index = ofs >> PAGE_SHIFT;

	for (index = ofs >> PAGE_SHIFT; index < (ofs + len) >> PAGE_SHIFT; index++) {
		page = find_get_page(mapping, index);
		if (!page)
			continue;
		memset(page_address(page), 0xFF, PAGE_SIZE);
		page_cache_release(page);
	}
	return 0;
}

90
static int logfs_mtd_erase(struct super_block *sb, loff_t ofs, size_t len,
J
Joern Engel 已提交
91
		int ensure_write)
J
Joern Engel 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	struct mtd_info *mtd = logfs_super(sb)->s_mtd;
	struct erase_info ei;
	DECLARE_COMPLETION_ONSTACK(complete);
	int ret;

	BUG_ON(len % mtd->erasesize);
	if (logfs_super(sb)->s_flags & LOGFS_SB_FLAG_RO)
		return -EROFS;

	memset(&ei, 0, sizeof(ei));
	ei.mtd = mtd;
	ei.addr = ofs;
	ei.len = len;
	ei.callback = logfs_erase_callback;
	ei.priv = (long)&complete;
108
	ret = mtd_erase(mtd, &ei);
J
Joern Engel 已提交
109 110 111 112 113 114
	if (ret)
		return -EIO;

	wait_for_completion(&complete);
	if (ei.state != MTD_ERASE_DONE)
		return -EIO;
115
	return logfs_mtd_erase_mapping(sb, ofs, len);
J
Joern Engel 已提交
116 117
}

118
static void logfs_mtd_sync(struct super_block *sb)
J
Joern Engel 已提交
119 120 121
{
	struct mtd_info *mtd = logfs_super(sb)->s_mtd;

122
	mtd_sync(mtd);
J
Joern Engel 已提交
123 124
}

125
static int logfs_mtd_readpage(void *_sb, struct page *page)
J
Joern Engel 已提交
126 127 128 129
{
	struct super_block *sb = _sb;
	int err;

130
	err = logfs_mtd_read(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
J
Joern Engel 已提交
131
			page_address(page));
132 133
	if (err == -EUCLEAN || err == -EBADMSG) {
		/* -EBADMSG happens regularly on power failures */
J
Joern Engel 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
		err = 0;
		/* FIXME: force GC this segment */
	}
	if (err) {
		ClearPageUptodate(page);
		SetPageError(page);
	} else {
		SetPageUptodate(page);
		ClearPageError(page);
	}
	unlock_page(page);
	return err;
}

148
static struct page *logfs_mtd_find_first_sb(struct super_block *sb, u64 *ofs)
J
Joern Engel 已提交
149 150 151
{
	struct logfs_super *super = logfs_super(sb);
	struct address_space *mapping = super->s_mapping_inode->i_mapping;
152
	filler_t *filler = logfs_mtd_readpage;
J
Joern Engel 已提交
153 154 155
	struct mtd_info *mtd = super->s_mtd;

	*ofs = 0;
156
	while (mtd_block_isbad(mtd, *ofs)) {
J
Joern Engel 已提交
157 158 159 160 161 162 163 164
		*ofs += mtd->erasesize;
		if (*ofs >= mtd->size)
			return NULL;
	}
	BUG_ON(*ofs & ~PAGE_MASK);
	return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
}

165
static struct page *logfs_mtd_find_last_sb(struct super_block *sb, u64 *ofs)
J
Joern Engel 已提交
166 167 168
{
	struct logfs_super *super = logfs_super(sb);
	struct address_space *mapping = super->s_mapping_inode->i_mapping;
169
	filler_t *filler = logfs_mtd_readpage;
J
Joern Engel 已提交
170 171 172
	struct mtd_info *mtd = super->s_mtd;

	*ofs = mtd->size - mtd->erasesize;
173
	while (mtd_block_isbad(mtd, *ofs)) {
J
Joern Engel 已提交
174 175 176 177 178 179 180 181 182
		*ofs -= mtd->erasesize;
		if (*ofs <= 0)
			return NULL;
	}
	*ofs = *ofs + mtd->erasesize - 0x1000;
	BUG_ON(*ofs & ~PAGE_MASK);
	return read_cache_page(mapping, *ofs >> PAGE_SHIFT, filler, sb);
}

183
static int __logfs_mtd_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
J
Joern Engel 已提交
184 185 186 187 188 189 190 191 192 193 194
		size_t nr_pages)
{
	struct logfs_super *super = logfs_super(sb);
	struct address_space *mapping = super->s_mapping_inode->i_mapping;
	struct page *page;
	int i, err;

	for (i = 0; i < nr_pages; i++) {
		page = find_lock_page(mapping, index + i);
		BUG_ON(!page);

195 196
		err = loffs_mtd_write(sb, page->index << PAGE_SHIFT, PAGE_SIZE,
					page_address(page));
J
Joern Engel 已提交
197 198 199 200 201 202 203 204
		unlock_page(page);
		page_cache_release(page);
		if (err)
			return err;
	}
	return 0;
}

205
static void logfs_mtd_writeseg(struct super_block *sb, u64 ofs, size_t len)
J
Joern Engel 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
{
	struct logfs_super *super = logfs_super(sb);
	int head;

	if (super->s_flags & LOGFS_SB_FLAG_RO)
		return;

	if (len == 0) {
		/* This can happen when the object fit perfectly into a
		 * segment, the segment gets written per sync and subsequently
		 * closed.
		 */
		return;
	}
	head = ofs & (PAGE_SIZE - 1);
	if (head) {
		ofs -= head;
		len += head;
	}
	len = PAGE_ALIGN(len);
226
	__logfs_mtd_writeseg(sb, ofs, ofs >> PAGE_SHIFT, len >> PAGE_SHIFT);
J
Joern Engel 已提交
227 228
}

229
static void logfs_mtd_put_device(struct logfs_super *s)
J
Joern Engel 已提交
230
{
A
Al Viro 已提交
231
	put_mtd_device(s->s_mtd);
J
Joern Engel 已提交
232 233
}

234
static int logfs_mtd_can_write_buf(struct super_block *sb, u64 ofs)
235 236 237 238 239 240 241 242
{
	struct logfs_super *super = logfs_super(sb);
	void *buf;
	int err;

	buf = kmalloc(super->s_writesize, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
243
	err = logfs_mtd_read(sb, ofs, super->s_writesize, buf);
244 245 246 247 248 249 250 251 252
	if (err)
		goto out;
	if (memchr_inv(buf, 0xff, super->s_writesize))
		err = -EIO;
	kfree(buf);
out:
	return err;
}

J
Joern Engel 已提交
253
static const struct logfs_device_ops mtd_devops = {
254 255 256 257 258 259 260 261
	.find_first_sb	= logfs_mtd_find_first_sb,
	.find_last_sb	= logfs_mtd_find_last_sb,
	.readpage	= logfs_mtd_readpage,
	.writeseg	= logfs_mtd_writeseg,
	.erase		= logfs_mtd_erase,
	.can_write_buf	= logfs_mtd_can_write_buf,
	.sync		= logfs_mtd_sync,
	.put_device	= logfs_mtd_put_device,
J
Joern Engel 已提交
262 263
};

A
Al Viro 已提交
264
int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
J
Joern Engel 已提交
265
{
A
Al Viro 已提交
266
	struct mtd_info *mtd = get_mtd_device(NULL, mtdnr);
A
Al Viro 已提交
267
	if (IS_ERR(mtd))
268
		return PTR_ERR(mtd);
A
Al Viro 已提交
269 270 271 272

	s->s_bdev = NULL;
	s->s_mtd = mtd;
	s->s_devops = &mtd_devops;
A
Al Viro 已提交
273
	return 0;
J
Joern Engel 已提交
274
}