aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/gluebi.c
diff options
context:
space:
mode:
authorArtem B. Bityutskiy <dedekind@linutronix.de>2006-06-27 04:22:22 -0400
committerFrank Haverkamp <haver@vnet.ibm.com>2007-04-27 07:23:33 -0400
commit801c135ce73d5df1caf3eca35b66a10824ae0707 (patch)
treeeaf6e7859650557192533b70746479de686c56e1 /drivers/mtd/ubi/gluebi.c
parentde46c33745f5e2ad594c72f2cf5f490861b16ce1 (diff)
UBI: Unsorted Block Images
UBI (Latin: "where?") manages multiple logical volumes on a single flash device, specifically supporting NAND flash devices. UBI provides a flexible partitioning concept which still allows for wear-levelling across the whole flash device. In a sense, UBI may be compared to the Logical Volume Manager (LVM). Whereas LVM maps logical sector numbers to physical HDD sector numbers, UBI maps logical eraseblocks to physical eraseblocks. More information may be found at http://www.linux-mtd.infradead.org/doc/ubi.html Partitioning/Re-partitioning An UBI volume occupies a certain number of erase blocks. This is limited by a configured maximum volume size, which could also be viewed as the partition size. Each individual UBI volume's size can be changed independently of the other UBI volumes, provided that the sum of all volume sizes doesn't exceed a certain limit. UBI supports dynamic volumes and static volumes. Static volumes are read-only and their contents are protected by CRC check sums. Bad eraseblocks handling UBI transparently handles bad eraseblocks. When a physical eraseblock becomes bad, it is substituted by a good physical eraseblock, and the user does not even notice this. Scrubbing On a NAND flash bit flips can occur on any write operation, sometimes also on read. If bit flips persist on the device, at first they can still be corrected by ECC, but once they accumulate, correction will become impossible. Thus it is best to actively scrub the affected eraseblock, by first copying it to a free eraseblock and then erasing the original. The UBI layer performs this type of scrubbing under the covers, transparently to the UBI volume users. Erase Counts UBI maintains an erase count header per eraseblock. This frees higher-level layers (like file systems) from doing this and allows for centralized erase count management instead. The erase counts are used by the wear-levelling algorithm in the UBI layer. The algorithm itself is exchangeable. Booting from NAND For booting directly from NAND flash the hardware must at least be capable of fetching and executing a small portion of the NAND flash. Some NAND flash controllers have this kind of support. They usually limit the window to a few kilobytes in erase block 0. This "initial program loader" (IPL) must then contain sufficient logic to load and execute the next boot phase. Due to bad eraseblocks, which may be randomly scattered over the flash device, it is problematic to store the "secondary program loader" (SPL) statically. Also, due to bit-flips it may become corrupted over time. UBI allows to solve this problem gracefully by storing the SPL in a small static UBI volume. UBI volumes vs. static partitions UBI volumes are still very similar to static MTD partitions: * both consist of eraseblocks (logical eraseblocks in case of UBI volumes, and physical eraseblocks in case of static partitions; * both support three basic operations - read, write, erase. But UBI volumes have the following advantages over traditional static MTD partitions: * there are no eraseblock wear-leveling constraints in case of UBI volumes, so the user should not care about this; * there are no bit-flips and bad eraseblocks in case of UBI volumes. So, UBI volumes may be considered as flash devices with relaxed restrictions. Where can it be found? Documentation, kernel code and applications can be found in the MTD gits. What are the applications for? The applications help to create binary flash images for two purposes: pfi files (partial flash images) for in-system update of UBI volumes, and plain binary images, with or without OOB data in case of NAND, for a manufacturing step. Furthermore some tools are/and will be created that allow flash content analysis after a system has crashed.. Who did UBI? The original ideas, where UBI is based on, were developed by Andreas Arnez, Frank Haverkamp and Thomas Gleixner. Josh W. Boyer and some others were involved too. The implementation of the kernel layer was done by Artem B. Bityutskiy. The user-space applications and tools were written by Oliver Lohmann with contributions from Frank Haverkamp, Andreas Arnez, and Artem. Joern Engel contributed a patch which modifies JFFS2 so that it can be run on a UBI volume. Thomas Gleixner did modifications to the NAND layer. Alexander Schmidt made some testing work as well as core functionality improvements. Signed-off-by: Artem B. Bityutskiy <dedekind@linutronix.de> Signed-off-by: Frank Haverkamp <haver@vnet.ibm.com>
Diffstat (limited to 'drivers/mtd/ubi/gluebi.c')
-rw-r--r--drivers/mtd/ubi/gluebi.c324
1 files changed, 324 insertions, 0 deletions
diff --git a/drivers/mtd/ubi/gluebi.c b/drivers/mtd/ubi/gluebi.c
new file mode 100644
index 000000000000..c8bbfd1e67ab
--- /dev/null
+++ b/drivers/mtd/ubi/gluebi.c
@@ -0,0 +1,324 @@
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
19 */
20
21/*
22 * This file includes implementation of fake MTD devices for each UBI volume.
23 * This sounds strange, but it is in fact quite useful to make MTD-oriented
24 * software (including all the legacy software) to work on top of UBI.
25 *
26 * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
27 * size (mtd->writesize) is equivalent to the UBI minimal I/O unit. The
28 * eraseblock size is equivalent to the logical eraseblock size of the volume.
29 */
30
31#include <asm/div64.h>
32#include "ubi.h"
33
34/**
35 * gluebi_get_device - get MTD device reference.
36 * @mtd: the MTD device description object
37 *
38 * This function is called every time the MTD device is being opened and
39 * implements the MTD get_device() operation. Returns zero in case of success
40 * and a negative error code in case of failure.
41 */
42static int gluebi_get_device(struct mtd_info *mtd)
43{
44 struct ubi_volume *vol;
45
46 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
47
48 /*
49 * We do not introduce locks for gluebi reference count because the
50 * get_device()/put_device() calls are already serialized at MTD.
51 */
52 if (vol->gluebi_refcount > 0) {
53 /*
54 * The MTD device is already referenced and this is just one
55 * more reference. MTD allows many users to open the same
56 * volume simultaneously and do not distinguish between
57 * readers/writers/exclusive openers as UBI does. So we do not
58 * open the UBI volume again - just increase the reference
59 * counter and return.
60 */
61 vol->gluebi_refcount += 1;
62 return 0;
63 }
64
65 /*
66 * This is the first reference to this UBI volume via the MTD device
67 * interface. Open the corresponding volume in read-write mode.
68 */
69 vol->gluebi_desc = ubi_open_volume(vol->ubi->ubi_num, vol->vol_id,
70 UBI_READWRITE);
71 if (IS_ERR(vol->gluebi_desc))
72 return PTR_ERR(vol->gluebi_desc);
73 vol->gluebi_refcount += 1;
74 return 0;
75}
76
77/**
78 * gluebi_put_device - put MTD device reference.
79 * @mtd: the MTD device description object
80 *
81 * This function is called every time the MTD device is being put. Returns
82 * zero in case of success and a negative error code in case of failure.
83 */
84static void gluebi_put_device(struct mtd_info *mtd)
85{
86 struct ubi_volume *vol;
87
88 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
89 vol->gluebi_refcount -= 1;
90 ubi_assert(vol->gluebi_refcount >= 0);
91 if (vol->gluebi_refcount == 0)
92 ubi_close_volume(vol->gluebi_desc);
93}
94
95/**
96 * gluebi_read - read operation of emulated MTD devices.
97 * @mtd: MTD device description object
98 * @from: absolute offset from where to read
99 * @len: how many bytes to read
100 * @retlen: count of read bytes is returned here
101 * @buf: buffer to store the read data
102 *
103 * This function returns zero in case of success and a negative error code in
104 * case of failure.
105 */
106static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, unsigned char *buf)
108{
109 int err = 0, lnum, offs, total_read;
110 struct ubi_volume *vol;
111 struct ubi_device *ubi;
112 uint64_t tmp = from;
113
114 dbg_msg("read %zd bytes from offset %lld", len, from);
115
116 if (len < 0 || from < 0 || from + len > mtd->size)
117 return -EINVAL;
118
119 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
120 ubi = vol->ubi;
121
122 offs = do_div(tmp, mtd->erasesize);
123 lnum = tmp;
124
125 total_read = len;
126 while (total_read) {
127 size_t to_read = mtd->erasesize - offs;
128
129 if (to_read > total_read)
130 to_read = total_read;
131
132 err = ubi_eba_read_leb(ubi, vol->vol_id, lnum, buf, offs,
133 to_read, 0);
134 if (err)
135 break;
136
137 lnum += 1;
138 offs = 0;
139 total_read -= to_read;
140 buf += to_read;
141 }
142
143 *retlen = len - total_read;
144 return err;
145}
146
147/**
148 * gluebi_write - write operation of emulated MTD devices.
149 * @mtd: MTD device description object
150 * @to: absolute offset where to write
151 * @len: how many bytes to write
152 * @retlen: count of written bytes is returned here
153 * @buf: buffer with data to write
154 *
155 * This function returns zero in case of success and a negative error code in
156 * case of failure.
157 */
158static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
159 size_t *retlen, const u_char *buf)
160{
161 int err = 0, lnum, offs, total_written;
162 struct ubi_volume *vol;
163 struct ubi_device *ubi;
164 uint64_t tmp = to;
165
166 dbg_msg("write %zd bytes to offset %lld", len, to);
167
168 if (len < 0 || to < 0 || len + to > mtd->size)
169 return -EINVAL;
170
171 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
172 ubi = vol->ubi;
173
174 if (ubi->ro_mode)
175 return -EROFS;
176
177 offs = do_div(tmp, mtd->erasesize);
178 lnum = tmp;
179
180 if (len % mtd->writesize || offs % mtd->writesize)
181 return -EINVAL;
182
183 total_written = len;
184 while (total_written) {
185 size_t to_write = mtd->erasesize - offs;
186
187 if (to_write > total_written)
188 to_write = total_written;
189
190 err = ubi_eba_write_leb(ubi, vol->vol_id, lnum, buf, offs,
191 to_write, UBI_UNKNOWN);
192 if (err)
193 break;
194
195 lnum += 1;
196 offs = 0;
197 total_written -= to_write;
198 buf += to_write;
199 }
200
201 *retlen = len - total_written;
202 return err;
203}
204
205/**
206 * gluebi_erase - erase operation of emulated MTD devices.
207 * @mtd: the MTD device description object
208 * @instr: the erase operation description
209 *
210 * This function calls the erase callback when finishes. Returns zero in case
211 * of success and a negative error code in case of failure.
212 */
213static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
214{
215 int err, i, lnum, count;
216 struct ubi_volume *vol;
217 struct ubi_device *ubi;
218
219 dbg_msg("erase %u bytes at offset %u", instr->len, instr->addr);
220
221 if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
222 return -EINVAL;
223
224 if (instr->len < 0 || instr->addr + instr->len > mtd->size)
225 return -EINVAL;
226
227 if (instr->addr % mtd->writesize || instr->len % mtd->writesize)
228 return -EINVAL;
229
230 lnum = instr->addr / mtd->erasesize;
231 count = instr->len / mtd->erasesize;
232
233 vol = container_of(mtd, struct ubi_volume, gluebi_mtd);
234 ubi = vol->ubi;
235
236 if (ubi->ro_mode)
237 return -EROFS;
238
239 for (i = 0; i < count; i++) {
240 err = ubi_eba_unmap_leb(ubi, vol->vol_id, lnum + i);
241 if (err)
242 goto out_err;
243 }
244
245 /*
246 * MTD erase operations are synchronous, so we have to make sure the
247 * physical eraseblock is wiped out.
248 */
249 err = ubi_wl_flush(ubi);
250 if (err)
251 goto out_err;
252
253 instr->state = MTD_ERASE_DONE;
254 mtd_erase_callback(instr);
255 return 0;
256
257out_err:
258 instr->state = MTD_ERASE_FAILED;
259 instr->fail_addr = lnum * mtd->erasesize;
260 return err;
261}
262
263/**
264 * ubi_create_gluebi - initialize gluebi for an UBI volume.
265 * @ubi: UBI device description object
266 * @vol: volume description object
267 *
268 * This function is called when an UBI volume is created in order to create
269 * corresponding fake MTD device. Returns zero in case of success and a
270 * negative error code in case of failure.
271 */
272int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
273{
274 int err;
275 struct mtd_info *mtd = &vol->gluebi_mtd;
276
277 mtd->name = kmemdup(vol->name, vol->name_len + 1, GFP_KERNEL);
278 if (!mtd->name)
279 return -ENOMEM;
280
281 mtd->type = MTD_UBIVOLUME;
282 if (!ubi->ro_mode)
283 mtd->flags = MTD_WRITEABLE;
284 mtd->writesize = ubi->min_io_size;
285 mtd->owner = THIS_MODULE;
286 mtd->size = vol->usable_leb_size * vol->reserved_pebs;
287 mtd->erasesize = vol->usable_leb_size;
288 mtd->read = gluebi_read;
289 mtd->write = gluebi_write;
290 mtd->erase = gluebi_erase;
291 mtd->get_device = gluebi_get_device;
292 mtd->put_device = gluebi_put_device;
293
294 if (add_mtd_device(mtd)) {
295 ubi_err("cannot not add MTD device\n");
296 kfree(mtd->name);
297 return -ENFILE;
298 }
299
300 dbg_msg("added mtd%d (\"%s\"), size %u, EB size %u",
301 mtd->index, mtd->name, mtd->size, mtd->erasesize);
302 return 0;
303}
304
305/**
306 * ubi_destroy_gluebi - close gluebi for an UBI volume.
307 * @vol: volume description object
308 *
309 * This function is called when an UBI volume is removed in order to remove
310 * corresponding fake MTD device. Returns zero in case of success and a
311 * negative error code in case of failure.
312 */
313int ubi_destroy_gluebi(struct ubi_volume *vol)
314{
315 int err;
316 struct mtd_info *mtd = &vol->gluebi_mtd;
317
318 dbg_msg("remove mtd%d", mtd->index);
319 err = del_mtd_device(mtd);
320 if (err)
321 return err;
322 kfree(mtd->name);
323 return 0;
324}