xref: /linux/drivers/mtd/mtdblock.c (revision 520b7aa00d8cd8e411ecc09f63a2acd90feb6d29)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Direct MTD block device access
4  *
5  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6  * Copyright © 2000-2003 Nicolas Pitre <nico@fluxnic.net>
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/vmalloc.h>
17 
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/blktrans.h>
20 #include <linux/mutex.h>
21 #include <linux/major.h>
22 
23 
24 struct mtdblk_dev {
25 	struct mtd_blktrans_dev mbd;
26 	int count;
27 	struct mutex cache_mutex;
28 	unsigned char *cache_data;
29 	unsigned long cache_offset;
30 	unsigned int cache_size;
31 	enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
32 };
33 
34 /*
35  * Cache stuff...
36  *
37  * Since typical flash erasable sectors are much larger than what Linux's
38  * buffer cache can handle, we must implement read-modify-write on flash
39  * sectors for each block write requests.  To avoid over-erasing flash sectors
40  * and to speed things up, we locally cache a whole flash sector while it is
41  * being written to until a different sector is required.
42  */
43 
44 static int erase_write (struct mtd_info *mtd, unsigned long pos,
45 			unsigned int len, const char *buf)
46 {
47 	struct erase_info erase;
48 	size_t retlen;
49 	int ret;
50 
51 	/*
52 	 * First, let's erase the flash block.
53 	 */
54 	erase.addr = pos;
55 	erase.len = len;
56 
57 	ret = mtd_erase(mtd, &erase);
58 	if (ret) {
59 		printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
60 				     "on \"%s\" failed\n",
61 			pos, len, mtd->name);
62 		return ret;
63 	}
64 
65 	/*
66 	 * Next, write the data to flash.
67 	 */
68 
69 	ret = mtd_write(mtd, pos, len, &retlen, buf);
70 	if (ret)
71 		return ret;
72 	if (retlen != len)
73 		return -EIO;
74 	return 0;
75 }
76 
77 
78 static int write_cached_data (struct mtdblk_dev *mtdblk)
79 {
80 	struct mtd_info *mtd = mtdblk->mbd.mtd;
81 	int ret;
82 
83 	if (mtdblk->cache_state != STATE_DIRTY)
84 		return 0;
85 
86 	pr_debug("mtdblock: writing cached data for \"%s\" "
87 			"at 0x%lx, size 0x%x\n", mtd->name,
88 			mtdblk->cache_offset, mtdblk->cache_size);
89 
90 	ret = erase_write (mtd, mtdblk->cache_offset,
91 			   mtdblk->cache_size, mtdblk->cache_data);
92 	if (ret)
93 		return ret;
94 
95 	/*
96 	 * Here we could arguably set the cache state to STATE_CLEAN.
97 	 * However this could lead to inconsistency since we will not
98 	 * be notified if this content is altered on the flash by other
99 	 * means.  Let's declare it empty and leave buffering tasks to
100 	 * the buffer cache instead.
101 	 */
102 	mtdblk->cache_state = STATE_EMPTY;
103 	return 0;
104 }
105 
106 
107 static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
108 			    int len, const char *buf)
109 {
110 	struct mtd_info *mtd = mtdblk->mbd.mtd;
111 	unsigned int sect_size = mtdblk->cache_size;
112 	size_t retlen;
113 	int ret;
114 
115 	pr_debug("mtdblock: write on \"%s\" at 0x%lx, size 0x%x\n",
116 		mtd->name, pos, len);
117 
118 	if (!sect_size)
119 		return mtd_write(mtd, pos, len, &retlen, buf);
120 
121 	while (len > 0) {
122 		unsigned long sect_start = (pos/sect_size)*sect_size;
123 		unsigned int offset = pos - sect_start;
124 		unsigned int size = sect_size - offset;
125 		if( size > len )
126 			size = len;
127 
128 		if (size == sect_size) {
129 			/*
130 			 * We are covering a whole sector.  Thus there is no
131 			 * need to bother with the cache while it may still be
132 			 * useful for other partial writes.
133 			 */
134 			ret = erase_write (mtd, pos, size, buf);
135 			if (ret)
136 				return ret;
137 		} else {
138 			/* Partial sector: need to use the cache */
139 
140 			if (mtdblk->cache_state == STATE_DIRTY &&
141 			    mtdblk->cache_offset != sect_start) {
142 				ret = write_cached_data(mtdblk);
143 				if (ret)
144 					return ret;
145 			}
146 
147 			if (mtdblk->cache_state == STATE_EMPTY ||
148 			    mtdblk->cache_offset != sect_start) {
149 				/* fill the cache with the current sector */
150 				mtdblk->cache_state = STATE_EMPTY;
151 				ret = mtd_read(mtd, sect_start, sect_size,
152 					       &retlen, mtdblk->cache_data);
153 				if (ret)
154 					return ret;
155 				if (retlen != sect_size)
156 					return -EIO;
157 
158 				mtdblk->cache_offset = sect_start;
159 				mtdblk->cache_size = sect_size;
160 				mtdblk->cache_state = STATE_CLEAN;
161 			}
162 
163 			/* write data to our local cache */
164 			memcpy (mtdblk->cache_data + offset, buf, size);
165 			mtdblk->cache_state = STATE_DIRTY;
166 		}
167 
168 		buf += size;
169 		pos += size;
170 		len -= size;
171 	}
172 
173 	return 0;
174 }
175 
176 
177 static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
178 			   int len, char *buf)
179 {
180 	struct mtd_info *mtd = mtdblk->mbd.mtd;
181 	unsigned int sect_size = mtdblk->cache_size;
182 	size_t retlen;
183 	int ret;
184 
185 	pr_debug("mtdblock: read on \"%s\" at 0x%lx, size 0x%x\n",
186 			mtd->name, pos, len);
187 
188 	if (!sect_size)
189 		return mtd_read(mtd, pos, len, &retlen, buf);
190 
191 	while (len > 0) {
192 		unsigned long sect_start = (pos/sect_size)*sect_size;
193 		unsigned int offset = pos - sect_start;
194 		unsigned int size = sect_size - offset;
195 		if (size > len)
196 			size = len;
197 
198 		/*
199 		 * Check if the requested data is already cached
200 		 * Read the requested amount of data from our internal cache if it
201 		 * contains what we want, otherwise we read the data directly
202 		 * from flash.
203 		 */
204 		if (mtdblk->cache_state != STATE_EMPTY &&
205 		    mtdblk->cache_offset == sect_start) {
206 			memcpy (buf, mtdblk->cache_data + offset, size);
207 		} else {
208 			ret = mtd_read(mtd, pos, size, &retlen, buf);
209 			if (ret)
210 				return ret;
211 			if (retlen != size)
212 				return -EIO;
213 		}
214 
215 		buf += size;
216 		pos += size;
217 		len -= size;
218 	}
219 
220 	return 0;
221 }
222 
223 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
224 			      unsigned long block, char *buf)
225 {
226 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
227 	return do_cached_read(mtdblk, block<<9, 512, buf);
228 }
229 
230 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
231 			      unsigned long block, char *buf)
232 {
233 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
234 	if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
235 		mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
236 		if (!mtdblk->cache_data)
237 			return -EINTR;
238 		/* -EINTR is not really correct, but it is the best match
239 		 * documented in man 2 write for all cases.  We could also
240 		 * return -EAGAIN sometimes, but why bother?
241 		 */
242 	}
243 	return do_cached_write(mtdblk, block<<9, 512, buf);
244 }
245 
246 static int mtdblock_open(struct mtd_blktrans_dev *mbd)
247 {
248 	struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
249 
250 	pr_debug("mtdblock_open\n");
251 
252 	if (mtdblk->count) {
253 		mtdblk->count++;
254 		return 0;
255 	}
256 
257 	/* OK, it's not open. Create cache info for it */
258 	mtdblk->count = 1;
259 	mutex_init(&mtdblk->cache_mutex);
260 	mtdblk->cache_state = STATE_EMPTY;
261 	if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
262 		mtdblk->cache_size = mbd->mtd->erasesize;
263 		mtdblk->cache_data = NULL;
264 	}
265 
266 	pr_debug("ok\n");
267 
268 	return 0;
269 }
270 
271 static void mtdblock_release(struct mtd_blktrans_dev *mbd)
272 {
273 	struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
274 
275 	pr_debug("mtdblock_release\n");
276 
277 	mutex_lock(&mtdblk->cache_mutex);
278 	write_cached_data(mtdblk);
279 	mutex_unlock(&mtdblk->cache_mutex);
280 
281 	if (!--mtdblk->count) {
282 		/*
283 		 * It was the last usage. Free the cache, but only sync if
284 		 * opened for writing.
285 		 */
286 		if (mbd->file_mode & FMODE_WRITE)
287 			mtd_sync(mbd->mtd);
288 		vfree(mtdblk->cache_data);
289 	}
290 
291 	pr_debug("ok\n");
292 }
293 
294 static int mtdblock_flush(struct mtd_blktrans_dev *dev)
295 {
296 	struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
297 
298 	mutex_lock(&mtdblk->cache_mutex);
299 	write_cached_data(mtdblk);
300 	mutex_unlock(&mtdblk->cache_mutex);
301 	mtd_sync(dev->mtd);
302 	return 0;
303 }
304 
305 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
306 {
307 	struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
308 
309 	if (!dev)
310 		return;
311 
312 	dev->mbd.mtd = mtd;
313 	dev->mbd.devnum = mtd->index;
314 
315 	dev->mbd.size = mtd->size >> 9;
316 	dev->mbd.tr = tr;
317 
318 	if (!(mtd->flags & MTD_WRITEABLE))
319 		dev->mbd.readonly = 1;
320 
321 	if (add_mtd_blktrans_dev(&dev->mbd))
322 		kfree(dev);
323 }
324 
325 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
326 {
327 	del_mtd_blktrans_dev(dev);
328 }
329 
330 static struct mtd_blktrans_ops mtdblock_tr = {
331 	.name		= "mtdblock",
332 	.major		= MTD_BLOCK_MAJOR,
333 	.part_bits	= 0,
334 	.blksize 	= 512,
335 	.open		= mtdblock_open,
336 	.flush		= mtdblock_flush,
337 	.release	= mtdblock_release,
338 	.readsect	= mtdblock_readsect,
339 	.writesect	= mtdblock_writesect,
340 	.add_mtd	= mtdblock_add_mtd,
341 	.remove_dev	= mtdblock_remove_dev,
342 	.owner		= THIS_MODULE,
343 };
344 
345 static int __init init_mtdblock(void)
346 {
347 	return register_mtd_blktrans(&mtdblock_tr);
348 }
349 
350 static void __exit cleanup_mtdblock(void)
351 {
352 	deregister_mtd_blktrans(&mtdblock_tr);
353 }
354 
355 module_init(init_mtdblock);
356 module_exit(cleanup_mtdblock);
357 
358 
359 MODULE_LICENSE("GPL");
360 MODULE_AUTHOR("Nicolas Pitre <nico@fluxnic.net> et al.");
361 MODULE_DESCRIPTION("Caching read/erase/writeback block device emulation access to MTD devices");
362