diff options
-rw-r--r-- | drivers/mtd/Kconfig | 2 | ||||
-rw-r--r-- | drivers/mtd/Makefile | 2 | ||||
-rw-r--r-- | drivers/mtd/ubi/Kconfig | 58 | ||||
-rw-r--r-- | drivers/mtd/ubi/Kconfig.debug | 104 | ||||
-rw-r--r-- | drivers/mtd/ubi/Makefile | 7 | ||||
-rw-r--r-- | drivers/mtd/ubi/build.c | 848 | ||||
-rw-r--r-- | drivers/mtd/ubi/cdev.c | 722 | ||||
-rw-r--r-- | drivers/mtd/ubi/debug.c | 224 | ||||
-rw-r--r-- | drivers/mtd/ubi/debug.h | 161 | ||||
-rw-r--r-- | drivers/mtd/ubi/eba.c | 1241 | ||||
-rw-r--r-- | drivers/mtd/ubi/gluebi.c | 324 | ||||
-rw-r--r-- | drivers/mtd/ubi/io.c | 1259 | ||||
-rw-r--r-- | drivers/mtd/ubi/kapi.c | 575 | ||||
-rw-r--r-- | drivers/mtd/ubi/misc.c | 105 | ||||
-rw-r--r-- | drivers/mtd/ubi/scan.c | 1368 | ||||
-rw-r--r-- | drivers/mtd/ubi/scan.h | 167 | ||||
-rw-r--r-- | drivers/mtd/ubi/ubi.h | 535 | ||||
-rw-r--r-- | drivers/mtd/ubi/upd.c | 348 | ||||
-rw-r--r-- | drivers/mtd/ubi/vmt.c | 809 | ||||
-rw-r--r-- | drivers/mtd/ubi/vtbl.c | 809 | ||||
-rw-r--r-- | drivers/mtd/ubi/wl.c | 1671 | ||||
-rw-r--r-- | include/linux/mtd/ubi.h | 202 | ||||
-rw-r--r-- | include/mtd/Kbuild | 2 | ||||
-rw-r--r-- | include/mtd/mtd-abi.h | 1 | ||||
-rw-r--r-- | include/mtd/ubi-header.h | 360 | ||||
-rw-r--r-- | include/mtd/ubi-user.h | 161 |
26 files changed, 12065 insertions, 0 deletions
diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 26f75c299440..6d1b91bf7ad5 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig | |||
@@ -292,5 +292,7 @@ source "drivers/mtd/nand/Kconfig" | |||
292 | 292 | ||
293 | source "drivers/mtd/onenand/Kconfig" | 293 | source "drivers/mtd/onenand/Kconfig" |
294 | 294 | ||
295 | source "drivers/mtd/ubi/Kconfig" | ||
296 | |||
295 | endmenu | 297 | endmenu |
296 | 298 | ||
diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile index c130e6261adf..92055405cb30 100644 --- a/drivers/mtd/Makefile +++ b/drivers/mtd/Makefile | |||
@@ -28,3 +28,5 @@ nftl-objs := nftlcore.o nftlmount.o | |||
28 | inftl-objs := inftlcore.o inftlmount.o | 28 | inftl-objs := inftlcore.o inftlmount.o |
29 | 29 | ||
30 | obj-y += chips/ maps/ devices/ nand/ onenand/ | 30 | obj-y += chips/ maps/ devices/ nand/ onenand/ |
31 | |||
32 | obj-$(CONFIG_MTD_UBI) += ubi/ | ||
diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig new file mode 100644 index 000000000000..b9daf159a4a7 --- /dev/null +++ b/drivers/mtd/ubi/Kconfig | |||
@@ -0,0 +1,58 @@ | |||
1 | # drivers/mtd/ubi/Kconfig | ||
2 | |||
3 | menu "UBI - Unsorted block images" | ||
4 | depends on MTD | ||
5 | |||
6 | config MTD_UBI | ||
7 | tristate "Enable UBI" | ||
8 | depends on MTD | ||
9 | select CRC32 | ||
10 | help | ||
11 | UBI is a software layer above MTD layer which admits of LVM-like | ||
12 | logical volumes on top of MTD devices, hides some complexities of | ||
13 | flash chips like wear and bad blocks and provides some other useful | ||
14 | capabilities. Please, consult the MTD web site for more details | ||
15 | (www.linux-mtd.infradead.org). | ||
16 | |||
17 | config MTD_UBI_WL_THRESHOLD | ||
18 | int "UBI wear-leveling threshold" | ||
19 | default 4096 | ||
20 | range 2 65536 | ||
21 | depends on MTD_UBI | ||
22 | help | ||
23 | This parameter defines the maximum difference between the highest | ||
24 | erase counter value and the lowest erase counter value of eraseblocks | ||
25 | of UBI devices. When this threshold is exceeded, UBI starts performing | ||
26 | wear leveling by means of moving data from eraseblock with low erase | ||
27 | counter to eraseblocks with high erase counter. Leave the default | ||
28 | value if unsure. | ||
29 | |||
30 | config MTD_UBI_BEB_RESERVE | ||
31 | int "Percentage of reserved eraseblocks for bad eraseblocks handling" | ||
32 | default 1 | ||
33 | range 0 25 | ||
34 | depends on MTD_UBI | ||
35 | help | ||
36 | If the MTD device admits of bad eraseblocks (e.g. NAND flash), UBI | ||
37 | reserves some amount of physical eraseblocks to handle new bad | ||
38 | eraseblocks. For example, if a flash physical eraseblock becomes bad, | ||
39 | UBI uses these reserved physical eraseblocks to relocate the bad one. | ||
40 | This option specifies how many physical eraseblocks will be reserved | ||
41 | for bad eraseblock handling (percents of total number of good flash | ||
42 | eraseblocks). If the underlying flash does not admit of bad | ||
43 | eraseblocks (e.g. NOR flash), this value is ignored and nothing is | ||
44 | reserved. Leave the default value if unsure. | ||
45 | |||
46 | config MTD_UBI_GLUEBI | ||
47 | bool "Emulate MTD devices" | ||
48 | default n | ||
49 | depends on MTD_UBI | ||
50 | help | ||
51 | This option enables MTD devices emulation on top of UBI volumes: for | ||
52 | each UBI volumes an MTD device is created, and all I/O to this MTD | ||
53 | device is redirected to the UBI volume. This is handy to make | ||
54 | MTD-oriented software (like JFFS2) work on top of UBI. Do not enable | ||
55 | this if no legacy software will be used. | ||
56 | |||
57 | source "drivers/mtd/ubi/Kconfig.debug" | ||
58 | endmenu | ||
diff --git a/drivers/mtd/ubi/Kconfig.debug b/drivers/mtd/ubi/Kconfig.debug new file mode 100644 index 000000000000..1e2ee22edeff --- /dev/null +++ b/drivers/mtd/ubi/Kconfig.debug | |||
@@ -0,0 +1,104 @@ | |||
1 | comment "UBI debugging options" | ||
2 | depends on MTD_UBI | ||
3 | |||
4 | config MTD_UBI_DEBUG | ||
5 | bool "UBI debugging" | ||
6 | depends on SYSFS | ||
7 | depends on MTD_UBI | ||
8 | select DEBUG_FS | ||
9 | select KALLSYMS_ALL | ||
10 | help | ||
11 | This option enables UBI debugging. | ||
12 | |||
13 | config MTD_UBI_DEBUG_MSG | ||
14 | bool "UBI debugging messages" | ||
15 | depends on MTD_UBI_DEBUG | ||
16 | default n | ||
17 | help | ||
18 | This option enables UBI debugging messages. | ||
19 | |||
20 | config MTD_UBI_DEBUG_PARANOID | ||
21 | bool "Extra self-checks" | ||
22 | default n | ||
23 | depends on MTD_UBI_DEBUG | ||
24 | help | ||
25 | This option enables extra checks in UBI code. Note this slows UBI down | ||
26 | significantly. | ||
27 | |||
28 | config MTD_UBI_DEBUG_DISABLE_BGT | ||
29 | bool "Do not enable the UBI background thread" | ||
30 | depends on MTD_UBI_DEBUG | ||
31 | default n | ||
32 | help | ||
33 | This option switches the background thread off by default. The thread | ||
34 | may be also be enabled/disabled via UBI sysfs. | ||
35 | |||
36 | config MTD_UBI_DEBUG_USERSPACE_IO | ||
37 | bool "Direct user-space write/erase support" | ||
38 | default n | ||
39 | depends on MTD_UBI_DEBUG | ||
40 | help | ||
41 | By default, users cannot directly write and erase individual | ||
42 | eraseblocks of dynamic volumes, and have to use update operation | ||
43 | instead. This option enables this capability - it is very useful for | ||
44 | debugging and testing. | ||
45 | |||
46 | config MTD_UBI_DEBUG_EMULATE_BITFLIPS | ||
47 | bool "Emulate flash bit-flips" | ||
48 | depends on MTD_UBI_DEBUG | ||
49 | default n | ||
50 | help | ||
51 | This option emulates bit-flips with probability 1/50, which in turn | ||
52 | causes scrubbing. Useful for debugging and stressing UBI. | ||
53 | |||
54 | config MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES | ||
55 | bool "Emulate flash write failures" | ||
56 | depends on MTD_UBI_DEBUG | ||
57 | default n | ||
58 | help | ||
59 | This option emulates write failures with probability 1/100. Useful for | ||
60 | debugging and testing how UBI handlines errors. | ||
61 | |||
62 | config MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES | ||
63 | bool "Emulate flash erase failures" | ||
64 | depends on MTD_UBI_DEBUG | ||
65 | default n | ||
66 | help | ||
67 | This option emulates erase failures with probability 1/100. Useful for | ||
68 | debugging and testing how UBI handlines errors. | ||
69 | |||
70 | menu "Additional UBI debugging messages" | ||
71 | depends on MTD_UBI_DEBUG | ||
72 | |||
73 | config MTD_UBI_DEBUG_MSG_BLD | ||
74 | bool "Additional UBI initialization and build messages" | ||
75 | default n | ||
76 | depends on MTD_UBI_DEBUG | ||
77 | help | ||
78 | This option enables detailed UBI initialization and device build | ||
79 | debugging messages. | ||
80 | |||
81 | config MTD_UBI_DEBUG_MSG_EBA | ||
82 | bool "Eraseblock association unit messages" | ||
83 | default n | ||
84 | depends on MTD_UBI_DEBUG | ||
85 | help | ||
86 | This option enables debugging messages from the UBI eraseblock | ||
87 | association unit. | ||
88 | |||
89 | config MTD_UBI_DEBUG_MSG_WL | ||
90 | bool "Wear-leveling unit messages" | ||
91 | default n | ||
92 | depends on MTD_UBI_DEBUG | ||
93 | help | ||
94 | This option enables debugging messages from the UBI wear-leveling | ||
95 | unit. | ||
96 | |||
97 | config MTD_UBI_DEBUG_MSG_IO | ||
98 | bool "Input/output unit messages" | ||
99 | default n | ||
100 | depends on MTD_UBI_DEBUG | ||
101 | help | ||
102 | This option enables debugging messages from the UBI input/output unit. | ||
103 | |||
104 | endmenu # UBI debugging messages | ||
diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile new file mode 100644 index 000000000000..dd834e04151b --- /dev/null +++ b/drivers/mtd/ubi/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | obj-$(CONFIG_MTD_UBI) += ubi.o | ||
2 | |||
3 | ubi-y += vtbl.o vmt.o upd.o build.o cdev.o kapi.o eba.o io.o wl.o scan.o | ||
4 | ubi-y += misc.o | ||
5 | |||
6 | ubi-$(CONFIG_MTD_UBI_DEBUG) += debug.o | ||
7 | ubi-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o | ||
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c new file mode 100644 index 000000000000..555d594d1811 --- /dev/null +++ b/drivers/mtd/ubi/build.c | |||
@@ -0,0 +1,848 @@ | |||
1 | /* | ||
2 | * Copyright (c) International Business Machines Corp., 2006 | ||
3 | * Copyright (c) Nokia Corporation, 2007 | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
13 | * the GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * Author: Artem Bityutskiy (Битюцкий Артём), | ||
20 | * Frank Haverkamp | ||
21 | */ | ||
22 | |||
23 | /* | ||
24 | * This file includes UBI initialization and building of UBI devices. At the | ||
25 | * moment UBI devices may only be added while UBI is initialized, but dynamic | ||
26 | * device add/remove functionality is planned. Also, at the moment we only | ||
27 | * attach UBI devices by scanning, which will become a bottleneck when flashes | ||
28 | * reach certain large size. Then one may improve UBI and add other methods. | ||
29 | */ | ||
30 | |||
31 | #include <linux/err.h> | ||
32 | #include <linux/module.h> | ||
33 | #include <linux/moduleparam.h> | ||
34 | #include <linux/stringify.h> | ||
35 | #include <linux/stat.h> | ||
36 | #include "ubi.h" | ||
37 | |||
38 | /* Maximum length of the 'mtd=' parameter */ | ||
39 | #define MTD_PARAM_LEN_MAX 64 | ||
40 | |||
41 | /** | ||
42 | * struct mtd_dev_param - MTD device parameter description data structure. | ||
43 | * @name: MTD device name or number string | ||
44 | * @vid_hdr_offs: VID header offset | ||
45 | * @data_offs: data offset | ||
46 | */ | ||
47 | struct mtd_dev_param | ||
48 | { | ||
49 | char name[MTD_PARAM_LEN_MAX]; | ||
50 | int vid_hdr_offs; | ||
51 | int data_offs; | ||
52 | }; | ||
53 | |||
54 | /* Numbers of elements set in the @mtd_dev_param array */ | ||
55 | static int mtd_devs = 0; | ||
56 | |||
57 | /* MTD devices specification parameters */ | ||
58 | static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES]; | ||
59 | |||
60 | /* Number of UBI devices in system */ | ||
61 | int ubi_devices_cnt; | ||
62 | |||
63 | /* All UBI devices in system */ | ||
64 | struct ubi_device *ubi_devices[UBI_MAX_DEVICES]; | ||
65 | |||
66 | /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */ | ||
67 | struct class *ubi_class; | ||
68 | |||
69 | /* "Show" method for files in '/<sysfs>/class/ubi/' */ | ||
70 | static ssize_t ubi_version_show(struct class *class, char *buf) | ||
71 | { | ||
72 | return sprintf(buf, "%d\n", UBI_VERSION); | ||
73 | } | ||
74 | |||
75 | /* UBI version attribute ('/<sysfs>/class/ubi/version') */ | ||
76 | static struct class_attribute ubi_version = | ||
77 | __ATTR(version, S_IRUGO, ubi_version_show, NULL); | ||
78 | |||
79 | static ssize_t dev_attribute_show(struct device *dev, | ||
80 | struct device_attribute *attr, char *buf); | ||
81 | |||
82 | /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */ | ||
83 | static struct device_attribute dev_eraseblock_size = | ||
84 | __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL); | ||
85 | static struct device_attribute dev_avail_eraseblocks = | ||
86 | __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL); | ||
87 | static struct device_attribute dev_total_eraseblocks = | ||
88 | __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL); | ||
89 | static struct device_attribute dev_volumes_count = | ||
90 | __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL); | ||
91 | static struct device_attribute dev_max_ec = | ||
92 | __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL); | ||
93 | static struct device_attribute dev_reserved_for_bad = | ||
94 | __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL); | ||
95 | static struct device_attribute dev_bad_peb_count = | ||
96 | __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL); | ||
97 | static struct device_attribute dev_max_vol_count = | ||
98 | __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL); | ||
99 | static struct device_attribute dev_min_io_size = | ||
100 | __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL); | ||
101 | static struct device_attribute dev_bgt_enabled = | ||
102 | __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL); | ||
103 | |||
104 | /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */ | ||
105 | static ssize_t dev_attribute_show(struct device *dev, | ||
106 | struct device_attribute *attr, char *buf) | ||
107 | { | ||
108 | const struct ubi_device *ubi; | ||
109 | |||
110 | ubi = container_of(dev, struct ubi_device, dev); | ||
111 | if (attr == &dev_eraseblock_size) | ||
112 | return sprintf(buf, "%d\n", ubi->leb_size); | ||
113 | else if (attr == &dev_avail_eraseblocks) | ||
114 | return sprintf(buf, "%d\n", ubi->avail_pebs); | ||
115 | else if (attr == &dev_total_eraseblocks) | ||
116 | return sprintf(buf, "%d\n", ubi->good_peb_count); | ||
117 | else if (attr == &dev_volumes_count) | ||
118 | return sprintf(buf, "%d\n", ubi->vol_count); | ||
119 | else if (attr == &dev_max_ec) | ||
120 | return sprintf(buf, "%d\n", ubi->max_ec); | ||
121 | else if (attr == &dev_reserved_for_bad) | ||
122 | return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs); | ||
123 | else if (attr == &dev_bad_peb_count) | ||
124 | return sprintf(buf, "%d\n", ubi->bad_peb_count); | ||
125 | else if (attr == &dev_max_vol_count) | ||
126 | return sprintf(buf, "%d\n", ubi->vtbl_slots); | ||
127 | else if (attr == &dev_min_io_size) | ||
128 | return sprintf(buf, "%d\n", ubi->min_io_size); | ||
129 | else if (attr == &dev_bgt_enabled) | ||
130 | return sprintf(buf, "%d\n", ubi->thread_enabled); | ||
131 | else | ||
132 | BUG(); | ||
133 | |||
134 | return 0; | ||
135 | } | ||
136 | |||
137 | /* Fake "release" method for UBI devices */ | ||
138 | static void dev_release(struct device *dev) { } | ||
139 | |||
140 | /** | ||
141 | * ubi_sysfs_init - initialize sysfs for an UBI device. | ||
142 | * @ubi: UBI device description object | ||
143 | * | ||
144 | * This function returns zero in case of success and a negative error code in | ||
145 | * case of failure. | ||
146 | */ | ||
147 | static int ubi_sysfs_init(struct ubi_device *ubi) | ||
148 | { | ||
149 | int err; | ||
150 | |||
151 | ubi->dev.release = dev_release; | ||
152 | ubi->dev.devt = MKDEV(ubi->major, 0); | ||
153 | ubi->dev.class = ubi_class; | ||
154 | sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num); | ||
155 | err = device_register(&ubi->dev); | ||
156 | if (err) | ||
157 | goto out; | ||
158 | |||
159 | err = device_create_file(&ubi->dev, &dev_eraseblock_size); | ||
160 | if (err) | ||
161 | goto out_unregister; | ||
162 | err = device_create_file(&ubi->dev, &dev_avail_eraseblocks); | ||
163 | if (err) | ||
164 | goto out_eraseblock_size; | ||
165 | err = device_create_file(&ubi->dev, &dev_total_eraseblocks); | ||
166 | if (err) | ||
167 | goto out_avail_eraseblocks; | ||
168 | err = device_create_file(&ubi->dev, &dev_volumes_count); | ||
169 | if (err) | ||
170 | goto out_total_eraseblocks; | ||
171 | err = device_create_file(&ubi->dev, &dev_max_ec); | ||
172 | if (err) | ||
173 | goto out_volumes_count; | ||
174 | err = device_create_file(&ubi->dev, &dev_reserved_for_bad); | ||
175 | if (err) | ||
176 | goto out_volumes_max_ec; | ||
177 | err = device_create_file(&ubi->dev, &dev_bad_peb_count); | ||
178 | if (err) | ||
179 | goto out_reserved_for_bad; | ||
180 | err = device_create_file(&ubi->dev, &dev_max_vol_count); | ||
181 | if (err) | ||
182 | goto out_bad_peb_count; | ||
183 | err = device_create_file(&ubi->dev, &dev_min_io_size); | ||
184 | if (err) | ||
185 | goto out_max_vol_count; | ||
186 | err = device_create_file(&ubi->dev, &dev_bgt_enabled); | ||
187 | if (err) | ||
188 | goto out_min_io_size; | ||
189 | |||
190 | return 0; | ||
191 | |||
192 | out_min_io_size: | ||
193 | device_remove_file(&ubi->dev, &dev_min_io_size); | ||
194 | out_max_vol_count: | ||
195 | device_remove_file(&ubi->dev, &dev_max_vol_count); | ||
196 | out_bad_peb_count: | ||
197 | device_remove_file(&ubi->dev, &dev_bad_peb_count); | ||
198 | out_reserved_for_bad: | ||
199 | device_remove_file(&ubi->dev, &dev_reserved_for_bad); | ||
200 | out_volumes_max_ec: | ||
201 | device_remove_file(&ubi->dev, &dev_max_ec); | ||
202 | out_volumes_count: | ||
203 | device_remove_file(&ubi->dev, &dev_volumes_count); | ||
204 | out_total_eraseblocks: | ||
205 | device_remove_file(&ubi->dev, &dev_total_eraseblocks); | ||
206 | out_avail_eraseblocks: | ||
207 | device_remove_file(&ubi->dev, &dev_avail_eraseblocks); | ||
208 | out_eraseblock_size: | ||
209 | device_remove_file(&ubi->dev, &dev_eraseblock_size); | ||
210 | out_unregister: | ||
211 | device_unregister(&ubi->dev); | ||
212 | out: | ||
213 | ubi_err("failed to initialize sysfs for %s", ubi->ubi_name); | ||
214 | return err; | ||
215 | } | ||
216 | |||
217 | /** | ||
218 | * ubi_sysfs_close - close sysfs for an UBI device. | ||
219 | * @ubi: UBI device description object | ||
220 | */ | ||
221 | static void ubi_sysfs_close(struct ubi_device *ubi) | ||
222 | { | ||
223 | device_remove_file(&ubi->dev, &dev_bgt_enabled); | ||
224 | device_remove_file(&ubi->dev, &dev_min_io_size); | ||
225 | device_remove_file(&ubi->dev, &dev_max_vol_count); | ||
226 | device_remove_file(&ubi->dev, &dev_bad_peb_count); | ||
227 | device_remove_file(&ubi->dev, &dev_reserved_for_bad); | ||
228 | device_remove_file(&ubi->dev, &dev_max_ec); | ||
229 | device_remove_file(&ubi->dev, &dev_volumes_count); | ||
230 | device_remove_file(&ubi->dev, &dev_total_eraseblocks); | ||
231 | device_remove_file(&ubi->dev, &dev_avail_eraseblocks); | ||
232 | device_remove_file(&ubi->dev, &dev_eraseblock_size); | ||
233 | device_unregister(&ubi->dev); | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * kill_volumes - destroy all volumes. | ||
238 | * @ubi: UBI device description object | ||
239 | */ | ||
240 | static void kill_volumes(struct ubi_device *ubi) | ||
241 | { | ||
242 | int i; | ||
243 | |||
244 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
245 | if (ubi->volumes[i]) | ||
246 | ubi_free_volume(ubi, i); | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * uif_init - initialize user interfaces for an UBI device. | ||
251 | * @ubi: UBI device description object | ||
252 | * | ||
253 | * This function returns zero in case of success and a negative error code in | ||
254 | * case of failure. | ||
255 | */ | ||
256 | static int uif_init(struct ubi_device *ubi) | ||
257 | { | ||
258 | int i, err; | ||
259 | dev_t dev; | ||
260 | |||
261 | mutex_init(&ubi->vtbl_mutex); | ||
262 | spin_lock_init(&ubi->volumes_lock); | ||
263 | |||
264 | sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num); | ||
265 | |||
266 | /* | ||
267 | * Major numbers for the UBI character devices are allocated | ||
268 | * dynamically. Major numbers of volume character devices are | ||
269 | * equivalent to ones of the corresponding UBI character device. Minor | ||
270 | * numbers of UBI character devices are 0, while minor numbers of | ||
271 | * volume character devices start from 1. Thus, we allocate one major | ||
272 | * number and ubi->vtbl_slots + 1 minor numbers. | ||
273 | */ | ||
274 | err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name); | ||
275 | if (err) { | ||
276 | ubi_err("cannot register UBI character devices"); | ||
277 | return err; | ||
278 | } | ||
279 | |||
280 | cdev_init(&ubi->cdev, &ubi_cdev_operations); | ||
281 | ubi->major = MAJOR(dev); | ||
282 | dbg_msg("%s major is %u", ubi->ubi_name, ubi->major); | ||
283 | ubi->cdev.owner = THIS_MODULE; | ||
284 | |||
285 | dev = MKDEV(ubi->major, 0); | ||
286 | err = cdev_add(&ubi->cdev, dev, 1); | ||
287 | if (err) { | ||
288 | ubi_err("cannot add character device %s", ubi->ubi_name); | ||
289 | goto out_unreg; | ||
290 | } | ||
291 | |||
292 | err = ubi_sysfs_init(ubi); | ||
293 | if (err) | ||
294 | goto out_cdev; | ||
295 | |||
296 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
297 | if (ubi->volumes[i]) { | ||
298 | err = ubi_add_volume(ubi, i); | ||
299 | if (err) | ||
300 | goto out_volumes; | ||
301 | } | ||
302 | |||
303 | return 0; | ||
304 | |||
305 | out_volumes: | ||
306 | kill_volumes(ubi); | ||
307 | ubi_sysfs_close(ubi); | ||
308 | out_cdev: | ||
309 | cdev_del(&ubi->cdev); | ||
310 | out_unreg: | ||
311 | unregister_chrdev_region(MKDEV(ubi->major, 0), | ||
312 | ubi->vtbl_slots + 1); | ||
313 | return err; | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * uif_close - close user interfaces for an UBI device. | ||
318 | * @ubi: UBI device description object | ||
319 | */ | ||
320 | static void uif_close(struct ubi_device *ubi) | ||
321 | { | ||
322 | kill_volumes(ubi); | ||
323 | ubi_sysfs_close(ubi); | ||
324 | cdev_del(&ubi->cdev); | ||
325 | unregister_chrdev_region(MKDEV(ubi->major, 0), ubi->vtbl_slots + 1); | ||
326 | } | ||
327 | |||
328 | /** | ||
329 | * attach_by_scanning - attach an MTD device using scanning method. | ||
330 | * @ubi: UBI device descriptor | ||
331 | * | ||
332 | * This function returns zero in case of success and a negative error code in | ||
333 | * case of failure. | ||
334 | * | ||
335 | * Note, currently this is the only method to attach UBI devices. Hopefully in | ||
336 | * the future we'll have more scalable attaching methods and avoid full media | ||
337 | * scanning. But even in this case scanning will be needed as a fall-back | ||
338 | * attaching method if there are some on-flash table corruptions. | ||
339 | */ | ||
340 | static int attach_by_scanning(struct ubi_device *ubi) | ||
341 | { | ||
342 | int err; | ||
343 | struct ubi_scan_info *si; | ||
344 | |||
345 | si = ubi_scan(ubi); | ||
346 | if (IS_ERR(si)) | ||
347 | return PTR_ERR(si); | ||
348 | |||
349 | ubi->bad_peb_count = si->bad_peb_count; | ||
350 | ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; | ||
351 | ubi->max_ec = si->max_ec; | ||
352 | ubi->mean_ec = si->mean_ec; | ||
353 | |||
354 | err = ubi_read_volume_table(ubi, si); | ||
355 | if (err) | ||
356 | goto out_si; | ||
357 | |||
358 | err = ubi_wl_init_scan(ubi, si); | ||
359 | if (err) | ||
360 | goto out_vtbl; | ||
361 | |||
362 | err = ubi_eba_init_scan(ubi, si); | ||
363 | if (err) | ||
364 | goto out_wl; | ||
365 | |||
366 | ubi_scan_destroy_si(si); | ||
367 | return 0; | ||
368 | |||
369 | out_wl: | ||
370 | ubi_wl_close(ubi); | ||
371 | out_vtbl: | ||
372 | kfree(ubi->vtbl); | ||
373 | out_si: | ||
374 | ubi_scan_destroy_si(si); | ||
375 | return err; | ||
376 | } | ||
377 | |||
378 | /** | ||
379 | * io_init - initialize I/O unit for a given UBI device. | ||
380 | * @ubi: UBI device description object | ||
381 | * | ||
382 | * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are | ||
383 | * assumed: | ||
384 | * o EC header is always at offset zero - this cannot be changed; | ||
385 | * o VID header starts just after the EC header at the closest address | ||
386 | * aligned to @io->@hdrs_min_io_size; | ||
387 | * o data starts just after the VID header at the closest address aligned to | ||
388 | * @io->@min_io_size | ||
389 | * | ||
390 | * This function returns zero in case of success and a negative error code in | ||
391 | * case of failure. | ||
392 | */ | ||
393 | static int io_init(struct ubi_device *ubi) | ||
394 | { | ||
395 | if (ubi->mtd->numeraseregions != 0) { | ||
396 | /* | ||
397 | * Some flashes have several erase regions. Different regions | ||
398 | * may have different eraseblock size and other | ||
399 | * characteristics. It looks like mostly multi-region flashes | ||
400 | * have one "main" region and one or more small regions to | ||
401 | * store boot loader code or boot parameters or whatever. I | ||
402 | * guess we should just pick the largest region. But this is | ||
403 | * not implemented. | ||
404 | */ | ||
405 | ubi_err("multiple regions, not implemented"); | ||
406 | return -EINVAL; | ||
407 | } | ||
408 | |||
409 | /* | ||
410 | * Note, in this implementation we support MTD devices with 0x7FFFFFFF | ||
411 | * physical eraseblocks maximum. | ||
412 | */ | ||
413 | |||
414 | ubi->peb_size = ubi->mtd->erasesize; | ||
415 | ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize; | ||
416 | ubi->flash_size = ubi->mtd->size; | ||
417 | |||
418 | if (ubi->mtd->block_isbad && ubi->mtd->block_markbad) | ||
419 | ubi->bad_allowed = 1; | ||
420 | |||
421 | ubi->min_io_size = ubi->mtd->writesize; | ||
422 | ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft; | ||
423 | |||
424 | /* Make sure minimal I/O unit is power of 2 */ | ||
425 | if (ubi->min_io_size == 0 || | ||
426 | (ubi->min_io_size & (ubi->min_io_size - 1))) { | ||
427 | ubi_err("bad min. I/O unit"); | ||
428 | return -EINVAL; | ||
429 | } | ||
430 | |||
431 | ubi_assert(ubi->hdrs_min_io_size > 0); | ||
432 | ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size); | ||
433 | ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0); | ||
434 | |||
435 | /* Calculate default aligned sizes of EC and VID headers */ | ||
436 | ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size); | ||
437 | ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size); | ||
438 | |||
439 | dbg_msg("min_io_size %d", ubi->min_io_size); | ||
440 | dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size); | ||
441 | dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize); | ||
442 | dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize); | ||
443 | |||
444 | if (ubi->vid_hdr_offset == 0) | ||
445 | /* Default offset */ | ||
446 | ubi->vid_hdr_offset = ubi->vid_hdr_aloffset = | ||
447 | ubi->ec_hdr_alsize; | ||
448 | else { | ||
449 | ubi->vid_hdr_aloffset = ubi->vid_hdr_offset & | ||
450 | ~(ubi->hdrs_min_io_size - 1); | ||
451 | ubi->vid_hdr_shift = ubi->vid_hdr_offset - | ||
452 | ubi->vid_hdr_aloffset; | ||
453 | } | ||
454 | |||
455 | /* Similar for the data offset */ | ||
456 | if (ubi->leb_start == 0) { | ||
457 | ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize; | ||
458 | ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size); | ||
459 | } | ||
460 | |||
461 | dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset); | ||
462 | dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset); | ||
463 | dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift); | ||
464 | dbg_msg("leb_start %d", ubi->leb_start); | ||
465 | |||
466 | /* The shift must be aligned to 32-bit boundary */ | ||
467 | if (ubi->vid_hdr_shift % 4) { | ||
468 | ubi_err("unaligned VID header shift %d", | ||
469 | ubi->vid_hdr_shift); | ||
470 | return -EINVAL; | ||
471 | } | ||
472 | |||
473 | /* Check sanity */ | ||
474 | if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE || | ||
475 | ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE || | ||
476 | ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE || | ||
477 | ubi->leb_start % ubi->min_io_size) { | ||
478 | ubi_err("bad VID header (%d) or data offsets (%d)", | ||
479 | ubi->vid_hdr_offset, ubi->leb_start); | ||
480 | return -EINVAL; | ||
481 | } | ||
482 | |||
483 | /* | ||
484 | * It may happen that EC and VID headers are situated in one minimal | ||
485 | * I/O unit. In this case we can only accept this UBI image in | ||
486 | * read-only mode. | ||
487 | */ | ||
488 | if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) { | ||
489 | ubi_warn("EC and VID headers are in the same minimal I/O unit, " | ||
490 | "switch to read-only mode"); | ||
491 | ubi->ro_mode = 1; | ||
492 | } | ||
493 | |||
494 | ubi->leb_size = ubi->peb_size - ubi->leb_start; | ||
495 | |||
496 | if (!(ubi->mtd->flags & MTD_WRITEABLE)) { | ||
497 | ubi_msg("MTD device %d is write-protected, attach in " | ||
498 | "read-only mode", ubi->mtd->index); | ||
499 | ubi->ro_mode = 1; | ||
500 | } | ||
501 | |||
502 | dbg_msg("leb_size %d", ubi->leb_size); | ||
503 | dbg_msg("ro_mode %d", ubi->ro_mode); | ||
504 | |||
505 | /* | ||
506 | * Note, ideally, we have to initialize ubi->bad_peb_count here. But | ||
507 | * unfortunately, MTD does not provide this information. We should loop | ||
508 | * over all physical eraseblocks and invoke mtd->block_is_bad() for | ||
509 | * each physical eraseblock. So, we skip ubi->bad_peb_count | ||
510 | * uninitialized and initialize it after scanning. | ||
511 | */ | ||
512 | |||
513 | return 0; | ||
514 | } | ||
515 | |||
516 | /** | ||
517 | * attach_mtd_dev - attach an MTD device. | ||
518 | * @mtd_dev: MTD device name or number string | ||
519 | * @vid_hdr_offset: VID header offset | ||
520 | * @data_offset: data offset | ||
521 | * | ||
522 | * This function attaches an MTD device to UBI. It first treats @mtd_dev as the | ||
523 | * MTD device name, and tries to open it by this name. If it is unable to open, | ||
524 | * it tries to convert @mtd_dev to an integer and open the MTD device by its | ||
525 | * number. Returns zero in case of success and a negative error code in case of | ||
526 | * failure. | ||
527 | */ | ||
528 | static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset, | ||
529 | int data_offset) | ||
530 | { | ||
531 | struct ubi_device *ubi; | ||
532 | struct mtd_info *mtd; | ||
533 | int i, err; | ||
534 | |||
535 | mtd = get_mtd_device_nm(mtd_dev); | ||
536 | if (IS_ERR(mtd)) { | ||
537 | int mtd_num; | ||
538 | char *endp; | ||
539 | |||
540 | if (PTR_ERR(mtd) != -ENODEV) | ||
541 | return PTR_ERR(mtd); | ||
542 | |||
543 | /* | ||
544 | * Probably this is not MTD device name but MTD device number - | ||
545 | * check this out. | ||
546 | */ | ||
547 | mtd_num = simple_strtoul(mtd_dev, &endp, 0); | ||
548 | if (*endp != '\0' || mtd_dev == endp) { | ||
549 | ubi_err("incorrect MTD device: \"%s\"", mtd_dev); | ||
550 | return -ENODEV; | ||
551 | } | ||
552 | |||
553 | mtd = get_mtd_device(NULL, mtd_num); | ||
554 | if (IS_ERR(mtd)) | ||
555 | return PTR_ERR(mtd); | ||
556 | } | ||
557 | |||
558 | /* Check if we already have the same MTD device attached */ | ||
559 | for (i = 0; i < ubi_devices_cnt; i++) | ||
560 | if (ubi_devices[i]->mtd->index == mtd->index) { | ||
561 | ubi_err("mtd%d is already attached to ubi%d", | ||
562 | mtd->index, i); | ||
563 | err = -EINVAL; | ||
564 | goto out_mtd; | ||
565 | } | ||
566 | |||
567 | ubi = ubi_devices[ubi_devices_cnt] = kzalloc(sizeof(struct ubi_device), | ||
568 | GFP_KERNEL); | ||
569 | if (!ubi) { | ||
570 | err = -ENOMEM; | ||
571 | goto out_mtd; | ||
572 | } | ||
573 | |||
574 | ubi->ubi_num = ubi_devices_cnt; | ||
575 | ubi->mtd = mtd; | ||
576 | |||
577 | dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d", | ||
578 | ubi->mtd->index, ubi_devices_cnt, vid_hdr_offset, data_offset); | ||
579 | |||
580 | ubi->vid_hdr_offset = vid_hdr_offset; | ||
581 | ubi->leb_start = data_offset; | ||
582 | err = io_init(ubi); | ||
583 | if (err) | ||
584 | goto out_free; | ||
585 | |||
586 | err = attach_by_scanning(ubi); | ||
587 | if (err) { | ||
588 | dbg_err("failed to attach by scanning, error %d", err); | ||
589 | goto out_free; | ||
590 | } | ||
591 | |||
592 | err = uif_init(ubi); | ||
593 | if (err) | ||
594 | goto out_detach; | ||
595 | |||
596 | ubi_devices_cnt += 1; | ||
597 | |||
598 | ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt); | ||
599 | ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); | ||
600 | ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); | ||
601 | ubi_msg("physical eraseblock size: %d bytes (%d KiB)", | ||
602 | ubi->peb_size, ubi->peb_size >> 10); | ||
603 | ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size); | ||
604 | ubi_msg("number of good PEBs: %d", ubi->good_peb_count); | ||
605 | ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); | ||
606 | ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size); | ||
607 | ubi_msg("VID header offset: %d (aligned %d)", | ||
608 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset); | ||
609 | ubi_msg("data offset: %d", ubi->leb_start); | ||
610 | ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); | ||
611 | ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); | ||
612 | ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); | ||
613 | ubi_msg("number of user volumes: %d", | ||
614 | ubi->vol_count - UBI_INT_VOL_COUNT); | ||
615 | ubi_msg("available PEBs: %d", ubi->avail_pebs); | ||
616 | ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs); | ||
617 | ubi_msg("number of PEBs reserved for bad PEB handling: %d", | ||
618 | ubi->beb_rsvd_pebs); | ||
619 | ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); | ||
620 | |||
621 | /* Enable the background thread */ | ||
622 | if (!DBG_DISABLE_BGT) { | ||
623 | ubi->thread_enabled = 1; | ||
624 | wake_up_process(ubi->bgt_thread); | ||
625 | } | ||
626 | |||
627 | return 0; | ||
628 | |||
629 | out_detach: | ||
630 | ubi_eba_close(ubi); | ||
631 | ubi_wl_close(ubi); | ||
632 | kfree(ubi->vtbl); | ||
633 | out_free: | ||
634 | kfree(ubi); | ||
635 | out_mtd: | ||
636 | put_mtd_device(mtd); | ||
637 | ubi_devices[ubi_devices_cnt] = NULL; | ||
638 | return err; | ||
639 | } | ||
640 | |||
641 | /** | ||
642 | * detach_mtd_dev - detach an MTD device. | ||
643 | * @ubi: UBI device description object | ||
644 | */ | ||
645 | static void detach_mtd_dev(struct ubi_device *ubi) | ||
646 | { | ||
647 | int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index; | ||
648 | |||
649 | dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num); | ||
650 | uif_close(ubi); | ||
651 | ubi_eba_close(ubi); | ||
652 | ubi_wl_close(ubi); | ||
653 | kfree(ubi->vtbl); | ||
654 | put_mtd_device(ubi->mtd); | ||
655 | kfree(ubi_devices[ubi_num]); | ||
656 | ubi_devices[ubi_num] = NULL; | ||
657 | ubi_devices_cnt -= 1; | ||
658 | ubi_assert(ubi_devices_cnt >= 0); | ||
659 | ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num); | ||
660 | } | ||
661 | |||
662 | static int __init ubi_init(void) | ||
663 | { | ||
664 | int err, i, k; | ||
665 | |||
666 | /* Ensure that EC and VID headers have correct size */ | ||
667 | BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64); | ||
668 | BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); | ||
669 | |||
670 | if (mtd_devs > UBI_MAX_DEVICES) { | ||
671 | printk("UBI error: too many MTD devices, maximum is %d\n", | ||
672 | UBI_MAX_DEVICES); | ||
673 | return -EINVAL; | ||
674 | } | ||
675 | |||
676 | ubi_class = class_create(THIS_MODULE, UBI_NAME_STR); | ||
677 | if (IS_ERR(ubi_class)) | ||
678 | return PTR_ERR(ubi_class); | ||
679 | |||
680 | err = class_create_file(ubi_class, &ubi_version); | ||
681 | if (err) | ||
682 | goto out_class; | ||
683 | |||
684 | /* Attach MTD devices */ | ||
685 | for (i = 0; i < mtd_devs; i++) { | ||
686 | struct mtd_dev_param *p = &mtd_dev_param[i]; | ||
687 | |||
688 | cond_resched(); | ||
689 | |||
690 | if (!p->name) { | ||
691 | dbg_err("empty name"); | ||
692 | err = -EINVAL; | ||
693 | goto out_detach; | ||
694 | } | ||
695 | |||
696 | err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs); | ||
697 | if (err) | ||
698 | goto out_detach; | ||
699 | } | ||
700 | |||
701 | return 0; | ||
702 | |||
703 | out_detach: | ||
704 | for (k = 0; k < i; k++) | ||
705 | detach_mtd_dev(ubi_devices[k]); | ||
706 | class_remove_file(ubi_class, &ubi_version); | ||
707 | out_class: | ||
708 | class_destroy(ubi_class); | ||
709 | return err; | ||
710 | } | ||
711 | module_init(ubi_init); | ||
712 | |||
713 | static void __exit ubi_exit(void) | ||
714 | { | ||
715 | int i, n = ubi_devices_cnt; | ||
716 | |||
717 | for (i = 0; i < n; i++) | ||
718 | detach_mtd_dev(ubi_devices[i]); | ||
719 | class_remove_file(ubi_class, &ubi_version); | ||
720 | class_destroy(ubi_class); | ||
721 | } | ||
722 | module_exit(ubi_exit); | ||
723 | |||
724 | /** | ||
725 | * bytes_str_to_int - convert a string representing number of bytes to an | ||
726 | * integer. | ||
727 | * @str: the string to convert | ||
728 | * | ||
729 | * This function returns positive resulting integer in case of success and a | ||
730 | * negative error code in case of failure. | ||
731 | */ | ||
732 | static int __init bytes_str_to_int(const char *str) | ||
733 | { | ||
734 | char *endp; | ||
735 | unsigned long result; | ||
736 | |||
737 | result = simple_strtoul(str, &endp, 0); | ||
738 | if (str == endp || result < 0) { | ||
739 | printk("UBI error: incorrect bytes count: \"%s\"\n", str); | ||
740 | return -EINVAL; | ||
741 | } | ||
742 | |||
743 | switch (*endp) { | ||
744 | case 'G': | ||
745 | result *= 1024; | ||
746 | case 'M': | ||
747 | result *= 1024; | ||
748 | case 'K': | ||
749 | case 'k': | ||
750 | result *= 1024; | ||
751 | if (endp[1] == 'i' && (endp[2] == '\0' || | ||
752 | endp[2] == 'B' || endp[2] == 'b')) | ||
753 | endp += 2; | ||
754 | case '\0': | ||
755 | break; | ||
756 | default: | ||
757 | printk("UBI error: incorrect bytes count: \"%s\"\n", str); | ||
758 | return -EINVAL; | ||
759 | } | ||
760 | |||
761 | return result; | ||
762 | } | ||
763 | |||
764 | /** | ||
765 | * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter. | ||
766 | * @val: the parameter value to parse | ||
767 | * @kp: not used | ||
768 | * | ||
769 | * This function returns zero in case of success and a negative error code in | ||
770 | * case of error. | ||
771 | */ | ||
772 | static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp) | ||
773 | { | ||
774 | int i, len; | ||
775 | struct mtd_dev_param *p; | ||
776 | char buf[MTD_PARAM_LEN_MAX]; | ||
777 | char *pbuf = &buf[0]; | ||
778 | char *tokens[3] = {NULL, NULL, NULL}; | ||
779 | |||
780 | if (mtd_devs == UBI_MAX_DEVICES) { | ||
781 | printk("UBI error: too many parameters, max. is %d\n", | ||
782 | UBI_MAX_DEVICES); | ||
783 | return -EINVAL; | ||
784 | } | ||
785 | |||
786 | len = strnlen(val, MTD_PARAM_LEN_MAX); | ||
787 | if (len == MTD_PARAM_LEN_MAX) { | ||
788 | printk("UBI error: parameter \"%s\" is too long, max. is %d\n", | ||
789 | val, MTD_PARAM_LEN_MAX); | ||
790 | return -EINVAL; | ||
791 | } | ||
792 | |||
793 | if (len == 0) { | ||
794 | printk("UBI warning: empty 'mtd=' parameter - ignored\n"); | ||
795 | return 0; | ||
796 | } | ||
797 | |||
798 | strcpy(buf, val); | ||
799 | |||
800 | /* Get rid of the final newline */ | ||
801 | if (buf[len - 1] == '\n') | ||
802 | buf[len - 1] = 0; | ||
803 | |||
804 | for (i = 0; i < 3; i++) | ||
805 | tokens[i] = strsep(&pbuf, ","); | ||
806 | |||
807 | if (pbuf) { | ||
808 | printk("UBI error: too many arguments at \"%s\"\n", val); | ||
809 | return -EINVAL; | ||
810 | } | ||
811 | |||
812 | if (tokens[0] == '\0') | ||
813 | return -EINVAL; | ||
814 | |||
815 | p = &mtd_dev_param[mtd_devs]; | ||
816 | strcpy(&p->name[0], tokens[0]); | ||
817 | |||
818 | if (tokens[1]) | ||
819 | p->vid_hdr_offs = bytes_str_to_int(tokens[1]); | ||
820 | if (tokens[2]) | ||
821 | p->data_offs = bytes_str_to_int(tokens[2]); | ||
822 | |||
823 | if (p->vid_hdr_offs < 0) | ||
824 | return p->vid_hdr_offs; | ||
825 | if (p->data_offs < 0) | ||
826 | return p->data_offs; | ||
827 | |||
828 | mtd_devs += 1; | ||
829 | return 0; | ||
830 | } | ||
831 | |||
832 | module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000); | ||
833 | MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: " | ||
834 | "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. " | ||
835 | "Multiple \"mtd\" parameters may be specified.\n" | ||
836 | "MTD devices may be specified by their number or name. " | ||
837 | "Optional \"vid_hdr_offs\" and \"data_offs\" parameters " | ||
838 | "specify UBI VID header position and data starting " | ||
839 | "position to be used by UBI.\n" | ||
840 | "Example: mtd=content,1984,2048 mtd=4 - attach MTD device" | ||
841 | "with name content using VID header offset 1984 and data " | ||
842 | "start 2048, and MTD device number 4 using default " | ||
843 | "offsets"); | ||
844 | |||
845 | MODULE_VERSION(__stringify(UBI_VERSION)); | ||
846 | MODULE_DESCRIPTION("UBI - Unsorted Block Images"); | ||
847 | MODULE_AUTHOR("Artem Bityutskiy"); | ||
848 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c new file mode 100644 index 000000000000..6612eb79bf17 --- /dev/null +++ b/drivers/mtd/ubi/cdev.c | |||
@@ -0,0 +1,722 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * This file includes implementation of UBI character device operations. | ||
23 | * | ||
24 | * There are two kinds of character devices in UBI: UBI character devices and | ||
25 | * UBI volume character devices. UBI character devices allow users to | ||
26 | * manipulate whole volumes: create, remove, and re-size them. Volume character | ||
27 | * devices provide volume I/O capabilities. | ||
28 | * | ||
29 | * Major and minor numbers are assigned dynamically to both UBI and volume | ||
30 | * character devices. | ||
31 | */ | ||
32 | |||
33 | #include <linux/module.h> | ||
34 | #include <linux/stat.h> | ||
35 | #include <linux/ioctl.h> | ||
36 | #include <linux/capability.h> | ||
37 | #include <mtd/ubi-user.h> | ||
38 | #include <asm/uaccess.h> | ||
39 | #include <asm/div64.h> | ||
40 | #include "ubi.h" | ||
41 | |||
42 | /* | ||
43 | * Maximum sequence numbers of UBI and volume character device IOCTLs (direct | ||
44 | * logical eraseblock erase is a debug-only feature). | ||
45 | */ | ||
46 | #define UBI_CDEV_IOC_MAX_SEQ 2 | ||
47 | #ifndef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO | ||
48 | #define VOL_CDEV_IOC_MAX_SEQ 1 | ||
49 | #else | ||
50 | #define VOL_CDEV_IOC_MAX_SEQ 2 | ||
51 | #endif | ||
52 | |||
53 | /** | ||
54 | * major_to_device - get UBI device object by character device major number. | ||
55 | * @major: major number | ||
56 | * | ||
57 | * This function returns a pointer to the UBI device object. | ||
58 | */ | ||
59 | static struct ubi_device *major_to_device(int major) | ||
60 | { | ||
61 | int i; | ||
62 | |||
63 | for (i = 0; i < ubi_devices_cnt; i++) | ||
64 | if (ubi_devices[i] && ubi_devices[i]->major == major) | ||
65 | return ubi_devices[i]; | ||
66 | BUG(); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * get_exclusive - get exclusive access to an UBI volume. | ||
71 | * @desc: volume descriptor | ||
72 | * | ||
73 | * This function changes UBI volume open mode to "exclusive". Returns previous | ||
74 | * mode value (positive integer) in case of success and a negative error code | ||
75 | * in case of failure. | ||
76 | */ | ||
77 | static int get_exclusive(struct ubi_volume_desc *desc) | ||
78 | { | ||
79 | int users, err; | ||
80 | struct ubi_volume *vol = desc->vol; | ||
81 | |||
82 | spin_lock(&vol->ubi->volumes_lock); | ||
83 | users = vol->readers + vol->writers + vol->exclusive; | ||
84 | ubi_assert(users > 0); | ||
85 | if (users > 1) { | ||
86 | dbg_err("%d users for volume %d", users, vol->vol_id); | ||
87 | err = -EBUSY; | ||
88 | } else { | ||
89 | vol->readers = vol->writers = 0; | ||
90 | vol->exclusive = 1; | ||
91 | err = desc->mode; | ||
92 | desc->mode = UBI_EXCLUSIVE; | ||
93 | } | ||
94 | spin_unlock(&vol->ubi->volumes_lock); | ||
95 | |||
96 | return err; | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * revoke_exclusive - revoke exclusive mode. | ||
101 | * @desc: volume descriptor | ||
102 | * @mode: new mode to switch to | ||
103 | */ | ||
104 | static void revoke_exclusive(struct ubi_volume_desc *desc, int mode) | ||
105 | { | ||
106 | struct ubi_volume *vol = desc->vol; | ||
107 | |||
108 | spin_lock(&vol->ubi->volumes_lock); | ||
109 | ubi_assert(vol->readers == 0 && vol->writers == 0); | ||
110 | ubi_assert(vol->exclusive == 1 && desc->mode == UBI_EXCLUSIVE); | ||
111 | vol->exclusive = 0; | ||
112 | if (mode == UBI_READONLY) | ||
113 | vol->readers = 1; | ||
114 | else if (mode == UBI_READWRITE) | ||
115 | vol->writers = 1; | ||
116 | else | ||
117 | vol->exclusive = 1; | ||
118 | spin_unlock(&vol->ubi->volumes_lock); | ||
119 | |||
120 | desc->mode = mode; | ||
121 | } | ||
122 | |||
123 | static int vol_cdev_open(struct inode *inode, struct file *file) | ||
124 | { | ||
125 | struct ubi_volume_desc *desc; | ||
126 | const struct ubi_device *ubi = major_to_device(imajor(inode)); | ||
127 | int vol_id = iminor(inode) - 1; | ||
128 | int mode; | ||
129 | |||
130 | if (file->f_mode & FMODE_WRITE) | ||
131 | mode = UBI_READWRITE; | ||
132 | else | ||
133 | mode = UBI_READONLY; | ||
134 | |||
135 | dbg_msg("open volume %d, mode %d", vol_id, mode); | ||
136 | |||
137 | desc = ubi_open_volume(ubi->ubi_num, vol_id, mode); | ||
138 | if (IS_ERR(desc)) | ||
139 | return PTR_ERR(desc); | ||
140 | |||
141 | file->private_data = desc; | ||
142 | return 0; | ||
143 | } | ||
144 | |||
145 | static int vol_cdev_release(struct inode *inode, struct file *file) | ||
146 | { | ||
147 | struct ubi_volume_desc *desc = file->private_data; | ||
148 | struct ubi_volume *vol = desc->vol; | ||
149 | |||
150 | dbg_msg("release volume %d, mode %d", vol->vol_id, desc->mode); | ||
151 | |||
152 | if (vol->updating) { | ||
153 | ubi_warn("update of volume %d not finished, volume is damaged", | ||
154 | vol->vol_id); | ||
155 | vol->updating = 0; | ||
156 | kfree(vol->upd_buf); | ||
157 | } | ||
158 | |||
159 | ubi_close_volume(desc); | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin) | ||
164 | { | ||
165 | struct ubi_volume_desc *desc = file->private_data; | ||
166 | struct ubi_volume *vol = desc->vol; | ||
167 | loff_t new_offset; | ||
168 | |||
169 | if (vol->updating) { | ||
170 | /* Update is in progress, seeking is prohibited */ | ||
171 | dbg_err("updating"); | ||
172 | return -EBUSY; | ||
173 | } | ||
174 | |||
175 | switch (origin) { | ||
176 | case 0: /* SEEK_SET */ | ||
177 | new_offset = offset; | ||
178 | break; | ||
179 | case 1: /* SEEK_CUR */ | ||
180 | new_offset = file->f_pos + offset; | ||
181 | break; | ||
182 | case 2: /* SEEK_END */ | ||
183 | new_offset = vol->used_bytes + offset; | ||
184 | break; | ||
185 | default: | ||
186 | return -EINVAL; | ||
187 | } | ||
188 | |||
189 | if (new_offset < 0 || new_offset > vol->used_bytes) { | ||
190 | dbg_err("bad seek %lld", new_offset); | ||
191 | return -EINVAL; | ||
192 | } | ||
193 | |||
194 | dbg_msg("seek volume %d, offset %lld, origin %d, new offset %lld", | ||
195 | vol->vol_id, offset, origin, new_offset); | ||
196 | |||
197 | file->f_pos = new_offset; | ||
198 | return new_offset; | ||
199 | } | ||
200 | |||
201 | static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count, | ||
202 | loff_t *offp) | ||
203 | { | ||
204 | struct ubi_volume_desc *desc = file->private_data; | ||
205 | struct ubi_volume *vol = desc->vol; | ||
206 | struct ubi_device *ubi = vol->ubi; | ||
207 | int err, lnum, off, len, vol_id = desc->vol->vol_id, tbuf_size; | ||
208 | size_t count_save = count; | ||
209 | void *tbuf; | ||
210 | uint64_t tmp; | ||
211 | |||
212 | dbg_msg("read %zd bytes from offset %lld of volume %d", | ||
213 | count, *offp, vol_id); | ||
214 | |||
215 | if (vol->updating) { | ||
216 | dbg_err("updating"); | ||
217 | return -EBUSY; | ||
218 | } | ||
219 | if (vol->upd_marker) { | ||
220 | dbg_err("damaged volume, update marker is set"); | ||
221 | return -EBADF; | ||
222 | } | ||
223 | if (*offp == vol->used_bytes || count == 0) | ||
224 | return 0; | ||
225 | |||
226 | if (vol->corrupted) | ||
227 | dbg_msg("read from corrupted volume %d", vol_id); | ||
228 | |||
229 | if (*offp + count > vol->used_bytes) | ||
230 | count_save = count = vol->used_bytes - *offp; | ||
231 | |||
232 | tbuf_size = vol->usable_leb_size; | ||
233 | if (count < tbuf_size) | ||
234 | tbuf_size = ALIGN(count, ubi->min_io_size); | ||
235 | tbuf = kmalloc(tbuf_size, GFP_KERNEL); | ||
236 | if (!tbuf) | ||
237 | return -ENOMEM; | ||
238 | |||
239 | len = count > tbuf_size ? tbuf_size : count; | ||
240 | |||
241 | tmp = *offp; | ||
242 | off = do_div(tmp, vol->usable_leb_size); | ||
243 | lnum = tmp; | ||
244 | |||
245 | do { | ||
246 | cond_resched(); | ||
247 | |||
248 | if (off + len >= vol->usable_leb_size) | ||
249 | len = vol->usable_leb_size - off; | ||
250 | |||
251 | err = ubi_eba_read_leb(ubi, vol_id, lnum, tbuf, off, len, 0); | ||
252 | if (err) | ||
253 | break; | ||
254 | |||
255 | off += len; | ||
256 | if (off == vol->usable_leb_size) { | ||
257 | lnum += 1; | ||
258 | off -= vol->usable_leb_size; | ||
259 | } | ||
260 | |||
261 | count -= len; | ||
262 | *offp += len; | ||
263 | |||
264 | err = copy_to_user(buf, tbuf, len); | ||
265 | if (err) { | ||
266 | err = -EFAULT; | ||
267 | break; | ||
268 | } | ||
269 | |||
270 | buf += len; | ||
271 | len = count > tbuf_size ? tbuf_size : count; | ||
272 | } while (count); | ||
273 | |||
274 | kfree(tbuf); | ||
275 | return err ? err : count_save - count; | ||
276 | } | ||
277 | |||
278 | #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO | ||
279 | |||
280 | /* | ||
281 | * This function allows to directly write to dynamic UBI volumes, without | ||
282 | * issuing the volume update operation. Available only as a debugging feature. | ||
283 | * Very useful for testing UBI. | ||
284 | */ | ||
285 | static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf, | ||
286 | size_t count, loff_t *offp) | ||
287 | { | ||
288 | struct ubi_volume_desc *desc = file->private_data; | ||
289 | struct ubi_volume *vol = desc->vol; | ||
290 | struct ubi_device *ubi = vol->ubi; | ||
291 | int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err = 0; | ||
292 | size_t count_save = count; | ||
293 | char *tbuf; | ||
294 | uint64_t tmp; | ||
295 | |||
296 | dbg_msg("requested: write %zd bytes to offset %lld of volume %u", | ||
297 | count, *offp, desc->vol->vol_id); | ||
298 | |||
299 | if (vol->vol_type == UBI_STATIC_VOLUME) | ||
300 | return -EROFS; | ||
301 | |||
302 | tmp = *offp; | ||
303 | off = do_div(tmp, vol->usable_leb_size); | ||
304 | lnum = tmp; | ||
305 | |||
306 | if (off % ubi->min_io_size) { | ||
307 | dbg_err("unaligned position"); | ||
308 | return -EINVAL; | ||
309 | } | ||
310 | |||
311 | if (*offp + count > vol->used_bytes) | ||
312 | count_save = count = vol->used_bytes - *offp; | ||
313 | |||
314 | /* We can write only in fractions of the minimum I/O unit */ | ||
315 | if (count % ubi->min_io_size) { | ||
316 | dbg_err("unaligned write length"); | ||
317 | return -EINVAL; | ||
318 | } | ||
319 | |||
320 | tbuf_size = vol->usable_leb_size; | ||
321 | if (count < tbuf_size) | ||
322 | tbuf_size = ALIGN(count, ubi->min_io_size); | ||
323 | tbuf = kmalloc(tbuf_size, GFP_KERNEL); | ||
324 | if (!tbuf) | ||
325 | return -ENOMEM; | ||
326 | |||
327 | len = count > tbuf_size ? tbuf_size : count; | ||
328 | |||
329 | while (count) { | ||
330 | cond_resched(); | ||
331 | |||
332 | if (off + len >= vol->usable_leb_size) | ||
333 | len = vol->usable_leb_size - off; | ||
334 | |||
335 | err = copy_from_user(tbuf, buf, len); | ||
336 | if (err) { | ||
337 | err = -EFAULT; | ||
338 | break; | ||
339 | } | ||
340 | |||
341 | err = ubi_eba_write_leb(ubi, vol_id, lnum, tbuf, off, len, | ||
342 | UBI_UNKNOWN); | ||
343 | if (err) | ||
344 | break; | ||
345 | |||
346 | off += len; | ||
347 | if (off == vol->usable_leb_size) { | ||
348 | lnum += 1; | ||
349 | off -= vol->usable_leb_size; | ||
350 | } | ||
351 | |||
352 | count -= len; | ||
353 | *offp += len; | ||
354 | buf += len; | ||
355 | len = count > tbuf_size ? tbuf_size : count; | ||
356 | } | ||
357 | |||
358 | kfree(tbuf); | ||
359 | return err ? err : count_save - count; | ||
360 | } | ||
361 | |||
362 | #else | ||
363 | #define vol_cdev_direct_write(file, buf, count, offp) -EPERM | ||
364 | #endif /* CONFIG_MTD_UBI_DEBUG_USERSPACE_IO */ | ||
365 | |||
366 | static ssize_t vol_cdev_write(struct file *file, const char __user *buf, | ||
367 | size_t count, loff_t *offp) | ||
368 | { | ||
369 | int err = 0; | ||
370 | struct ubi_volume_desc *desc = file->private_data; | ||
371 | struct ubi_volume *vol = desc->vol; | ||
372 | struct ubi_device *ubi = vol->ubi; | ||
373 | |||
374 | if (!vol->updating) | ||
375 | return vol_cdev_direct_write(file, buf, count, offp); | ||
376 | |||
377 | err = ubi_more_update_data(ubi, vol->vol_id, buf, count); | ||
378 | if (err < 0) { | ||
379 | ubi_err("cannot write %zd bytes of update data", count); | ||
380 | return err; | ||
381 | } | ||
382 | |||
383 | if (err) { | ||
384 | /* | ||
385 | * Update is finished, @err contains number of actually written | ||
386 | * bytes now. | ||
387 | */ | ||
388 | count = err; | ||
389 | |||
390 | err = ubi_check_volume(ubi, vol->vol_id); | ||
391 | if (err < 0) | ||
392 | return err; | ||
393 | |||
394 | if (err) { | ||
395 | ubi_warn("volume %d on UBI device %d is corrupted", | ||
396 | vol->vol_id, ubi->ubi_num); | ||
397 | vol->corrupted = 1; | ||
398 | } | ||
399 | vol->checked = 1; | ||
400 | revoke_exclusive(desc, UBI_READWRITE); | ||
401 | } | ||
402 | |||
403 | *offp += count; | ||
404 | return count; | ||
405 | } | ||
406 | |||
407 | static int vol_cdev_ioctl(struct inode *inode, struct file *file, | ||
408 | unsigned int cmd, unsigned long arg) | ||
409 | { | ||
410 | int err = 0; | ||
411 | struct ubi_volume_desc *desc = file->private_data; | ||
412 | struct ubi_volume *vol = desc->vol; | ||
413 | struct ubi_device *ubi = vol->ubi; | ||
414 | void __user *argp = (void __user *)arg; | ||
415 | |||
416 | if (_IOC_NR(cmd) > VOL_CDEV_IOC_MAX_SEQ || | ||
417 | _IOC_TYPE(cmd) != UBI_VOL_IOC_MAGIC) | ||
418 | return -ENOTTY; | ||
419 | |||
420 | if (_IOC_DIR(cmd) && _IOC_READ) | ||
421 | err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd)); | ||
422 | else if (_IOC_DIR(cmd) && _IOC_WRITE) | ||
423 | err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd)); | ||
424 | if (err) | ||
425 | return -EFAULT; | ||
426 | |||
427 | switch (cmd) { | ||
428 | |||
429 | /* Volume update command */ | ||
430 | case UBI_IOCVOLUP: | ||
431 | { | ||
432 | int64_t bytes, rsvd_bytes; | ||
433 | |||
434 | if (!capable(CAP_SYS_RESOURCE)) { | ||
435 | err = -EPERM; | ||
436 | break; | ||
437 | } | ||
438 | |||
439 | err = copy_from_user(&bytes, argp, sizeof(int64_t)); | ||
440 | if (err) { | ||
441 | err = -EFAULT; | ||
442 | break; | ||
443 | } | ||
444 | |||
445 | if (desc->mode == UBI_READONLY) { | ||
446 | err = -EROFS; | ||
447 | break; | ||
448 | } | ||
449 | |||
450 | rsvd_bytes = vol->reserved_pebs * (ubi->leb_size-vol->data_pad); | ||
451 | if (bytes < 0 || bytes > rsvd_bytes) { | ||
452 | err = -EINVAL; | ||
453 | break; | ||
454 | } | ||
455 | |||
456 | err = get_exclusive(desc); | ||
457 | if (err < 0) | ||
458 | break; | ||
459 | |||
460 | err = ubi_start_update(ubi, vol->vol_id, bytes); | ||
461 | if (bytes == 0) | ||
462 | revoke_exclusive(desc, UBI_READWRITE); | ||
463 | |||
464 | file->f_pos = 0; | ||
465 | break; | ||
466 | } | ||
467 | |||
468 | #ifdef CONFIG_MTD_UBI_DEBUG_USERSPACE_IO | ||
469 | /* Logical eraseblock erasure command */ | ||
470 | case UBI_IOCEBER: | ||
471 | { | ||
472 | int32_t lnum; | ||
473 | |||
474 | err = __get_user(lnum, (__user int32_t *)argp); | ||
475 | if (err) { | ||
476 | err = -EFAULT; | ||
477 | break; | ||
478 | } | ||
479 | |||
480 | if (desc->mode == UBI_READONLY) { | ||
481 | err = -EROFS; | ||
482 | break; | ||
483 | } | ||
484 | |||
485 | if (lnum < 0 || lnum >= vol->reserved_pebs) { | ||
486 | err = -EINVAL; | ||
487 | break; | ||
488 | } | ||
489 | |||
490 | if (vol->vol_type != UBI_DYNAMIC_VOLUME) { | ||
491 | err = -EROFS; | ||
492 | break; | ||
493 | } | ||
494 | |||
495 | dbg_msg("erase LEB %d:%d", vol->vol_id, lnum); | ||
496 | err = ubi_eba_unmap_leb(ubi, vol->vol_id, lnum); | ||
497 | if (err) | ||
498 | break; | ||
499 | |||
500 | err = ubi_wl_flush(ubi); | ||
501 | break; | ||
502 | } | ||
503 | #endif | ||
504 | |||
505 | default: | ||
506 | err = -ENOTTY; | ||
507 | break; | ||
508 | } | ||
509 | |||
510 | return err; | ||
511 | } | ||
512 | |||
513 | /** | ||
514 | * verify_mkvol_req - verify volume creation request. | ||
515 | * @ubi: UBI device description object | ||
516 | * @req: the request to check | ||
517 | * | ||
518 | * This function zero if the request is correct, and %-EINVAL if not. | ||
519 | */ | ||
520 | static int verify_mkvol_req(const struct ubi_device *ubi, | ||
521 | const struct ubi_mkvol_req *req) | ||
522 | { | ||
523 | int n, err = -EINVAL; | ||
524 | |||
525 | if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 || | ||
526 | req->name_len < 0) | ||
527 | goto bad; | ||
528 | |||
529 | if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) && | ||
530 | req->vol_id != UBI_VOL_NUM_AUTO) | ||
531 | goto bad; | ||
532 | |||
533 | if (req->alignment == 0) | ||
534 | goto bad; | ||
535 | |||
536 | if (req->bytes == 0) | ||
537 | goto bad; | ||
538 | |||
539 | if (req->vol_type != UBI_DYNAMIC_VOLUME && | ||
540 | req->vol_type != UBI_STATIC_VOLUME) | ||
541 | goto bad; | ||
542 | |||
543 | if (req->alignment > ubi->leb_size) | ||
544 | goto bad; | ||
545 | |||
546 | n = req->alignment % ubi->min_io_size; | ||
547 | if (req->alignment != 1 && n) | ||
548 | goto bad; | ||
549 | |||
550 | if (req->name_len > UBI_VOL_NAME_MAX) { | ||
551 | err = -ENAMETOOLONG; | ||
552 | goto bad; | ||
553 | } | ||
554 | |||
555 | return 0; | ||
556 | |||
557 | bad: | ||
558 | dbg_err("bad volume creation request"); | ||
559 | ubi_dbg_dump_mkvol_req(req); | ||
560 | return err; | ||
561 | } | ||
562 | |||
563 | /** | ||
564 | * verify_rsvol_req - verify volume re-size request. | ||
565 | * @ubi: UBI device description object | ||
566 | * @req: the request to check | ||
567 | * | ||
568 | * This function returns zero if the request is correct, and %-EINVAL if not. | ||
569 | */ | ||
570 | static int verify_rsvol_req(const struct ubi_device *ubi, | ||
571 | const struct ubi_rsvol_req *req) | ||
572 | { | ||
573 | if (req->bytes <= 0) | ||
574 | return -EINVAL; | ||
575 | |||
576 | if (req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) | ||
577 | return -EINVAL; | ||
578 | |||
579 | return 0; | ||
580 | } | ||
581 | |||
582 | static int ubi_cdev_ioctl(struct inode *inode, struct file *file, | ||
583 | unsigned int cmd, unsigned long arg) | ||
584 | { | ||
585 | int err = 0; | ||
586 | struct ubi_device *ubi; | ||
587 | struct ubi_volume_desc *desc; | ||
588 | void __user *argp = (void __user *)arg; | ||
589 | |||
590 | if (_IOC_NR(cmd) > UBI_CDEV_IOC_MAX_SEQ || | ||
591 | _IOC_TYPE(cmd) != UBI_IOC_MAGIC) | ||
592 | return -ENOTTY; | ||
593 | |||
594 | if (_IOC_DIR(cmd) && _IOC_READ) | ||
595 | err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd)); | ||
596 | else if (_IOC_DIR(cmd) && _IOC_WRITE) | ||
597 | err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd)); | ||
598 | if (err) | ||
599 | return -EFAULT; | ||
600 | |||
601 | if (!capable(CAP_SYS_RESOURCE)) | ||
602 | return -EPERM; | ||
603 | |||
604 | ubi = major_to_device(imajor(inode)); | ||
605 | if (IS_ERR(ubi)) | ||
606 | return PTR_ERR(ubi); | ||
607 | |||
608 | switch (cmd) { | ||
609 | /* Create volume command */ | ||
610 | case UBI_IOCMKVOL: | ||
611 | { | ||
612 | struct ubi_mkvol_req req; | ||
613 | |||
614 | dbg_msg("create volume"); | ||
615 | err = __copy_from_user(&req, argp, | ||
616 | sizeof(struct ubi_mkvol_req)); | ||
617 | if (err) { | ||
618 | err = -EFAULT; | ||
619 | break; | ||
620 | } | ||
621 | |||
622 | err = verify_mkvol_req(ubi, &req); | ||
623 | if (err) | ||
624 | break; | ||
625 | |||
626 | req.name[req.name_len] = '\0'; | ||
627 | |||
628 | err = ubi_create_volume(ubi, &req); | ||
629 | if (err) | ||
630 | break; | ||
631 | |||
632 | err = __put_user(req.vol_id, (__user int32_t *)argp); | ||
633 | if (err) | ||
634 | err = -EFAULT; | ||
635 | |||
636 | break; | ||
637 | } | ||
638 | |||
639 | /* Remove volume command */ | ||
640 | case UBI_IOCRMVOL: | ||
641 | { | ||
642 | int vol_id; | ||
643 | |||
644 | dbg_msg("remove volume"); | ||
645 | err = __get_user(vol_id, (__user int32_t *)argp); | ||
646 | if (err) { | ||
647 | err = -EFAULT; | ||
648 | break; | ||
649 | } | ||
650 | |||
651 | desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE); | ||
652 | if (IS_ERR(desc)) { | ||
653 | err = PTR_ERR(desc); | ||
654 | break; | ||
655 | } | ||
656 | |||
657 | err = ubi_remove_volume(desc); | ||
658 | if (err) | ||
659 | ubi_close_volume(desc); | ||
660 | |||
661 | break; | ||
662 | } | ||
663 | |||
664 | /* Re-size volume command */ | ||
665 | case UBI_IOCRSVOL: | ||
666 | { | ||
667 | int pebs; | ||
668 | uint64_t tmp; | ||
669 | struct ubi_rsvol_req req; | ||
670 | |||
671 | dbg_msg("re-size volume"); | ||
672 | err = __copy_from_user(&req, argp, | ||
673 | sizeof(struct ubi_rsvol_req)); | ||
674 | if (err) { | ||
675 | err = -EFAULT; | ||
676 | break; | ||
677 | } | ||
678 | |||
679 | err = verify_rsvol_req(ubi, &req); | ||
680 | if (err) | ||
681 | break; | ||
682 | |||
683 | desc = ubi_open_volume(ubi->ubi_num, req.vol_id, UBI_EXCLUSIVE); | ||
684 | if (IS_ERR(desc)) { | ||
685 | err = PTR_ERR(desc); | ||
686 | break; | ||
687 | } | ||
688 | |||
689 | tmp = req.bytes; | ||
690 | pebs = !!do_div(tmp, desc->vol->usable_leb_size); | ||
691 | pebs += tmp; | ||
692 | |||
693 | err = ubi_resize_volume(desc, pebs); | ||
694 | ubi_close_volume(desc); | ||
695 | break; | ||
696 | } | ||
697 | |||
698 | default: | ||
699 | err = -ENOTTY; | ||
700 | break; | ||
701 | } | ||
702 | |||
703 | return err; | ||
704 | } | ||
705 | |||
706 | /* UBI character device operations */ | ||
707 | struct file_operations ubi_cdev_operations = { | ||
708 | .owner = THIS_MODULE, | ||
709 | .ioctl = ubi_cdev_ioctl, | ||
710 | .llseek = no_llseek | ||
711 | }; | ||
712 | |||
713 | /* UBI volume character device operations */ | ||
714 | struct file_operations ubi_vol_cdev_operations = { | ||
715 | .owner = THIS_MODULE, | ||
716 | .open = vol_cdev_open, | ||
717 | .release = vol_cdev_release, | ||
718 | .llseek = vol_cdev_llseek, | ||
719 | .read = vol_cdev_read, | ||
720 | .write = vol_cdev_write, | ||
721 | .ioctl = vol_cdev_ioctl | ||
722 | }; | ||
diff --git a/drivers/mtd/ubi/debug.c b/drivers/mtd/ubi/debug.c new file mode 100644 index 000000000000..86364221fafe --- /dev/null +++ b/drivers/mtd/ubi/debug.c | |||
@@ -0,0 +1,224 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Here we keep all the UBI debugging stuff which should normally be disabled | ||
23 | * and compiled-out, but it is extremely helpful when hunting bugs or doing big | ||
24 | * changes. | ||
25 | */ | ||
26 | |||
27 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG | ||
28 | |||
29 | #include "ubi.h" | ||
30 | |||
31 | /** | ||
32 | * ubi_dbg_dump_ec_hdr - dump an erase counter header. | ||
33 | * @ec_hdr: the erase counter header to dump | ||
34 | */ | ||
35 | void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) | ||
36 | { | ||
37 | dbg_msg("erase counter header dump:"); | ||
38 | dbg_msg("magic %#08x", ubi32_to_cpu(ec_hdr->magic)); | ||
39 | dbg_msg("version %d", (int)ec_hdr->version); | ||
40 | dbg_msg("ec %llu", (long long)ubi64_to_cpu(ec_hdr->ec)); | ||
41 | dbg_msg("vid_hdr_offset %d", ubi32_to_cpu(ec_hdr->vid_hdr_offset)); | ||
42 | dbg_msg("data_offset %d", ubi32_to_cpu(ec_hdr->data_offset)); | ||
43 | dbg_msg("hdr_crc %#08x", ubi32_to_cpu(ec_hdr->hdr_crc)); | ||
44 | dbg_msg("erase counter header hexdump:"); | ||
45 | ubi_dbg_hexdump(ec_hdr, UBI_EC_HDR_SIZE); | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * ubi_dbg_dump_vid_hdr - dump a volume identifier header. | ||
50 | * @vid_hdr: the volume identifier header to dump | ||
51 | */ | ||
52 | void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) | ||
53 | { | ||
54 | dbg_msg("volume identifier header dump:"); | ||
55 | dbg_msg("magic %08x", ubi32_to_cpu(vid_hdr->magic)); | ||
56 | dbg_msg("version %d", (int)vid_hdr->version); | ||
57 | dbg_msg("vol_type %d", (int)vid_hdr->vol_type); | ||
58 | dbg_msg("copy_flag %d", (int)vid_hdr->copy_flag); | ||
59 | dbg_msg("compat %d", (int)vid_hdr->compat); | ||
60 | dbg_msg("vol_id %d", ubi32_to_cpu(vid_hdr->vol_id)); | ||
61 | dbg_msg("lnum %d", ubi32_to_cpu(vid_hdr->lnum)); | ||
62 | dbg_msg("leb_ver %u", ubi32_to_cpu(vid_hdr->leb_ver)); | ||
63 | dbg_msg("data_size %d", ubi32_to_cpu(vid_hdr->data_size)); | ||
64 | dbg_msg("used_ebs %d", ubi32_to_cpu(vid_hdr->used_ebs)); | ||
65 | dbg_msg("data_pad %d", ubi32_to_cpu(vid_hdr->data_pad)); | ||
66 | dbg_msg("sqnum %llu", | ||
67 | (unsigned long long)ubi64_to_cpu(vid_hdr->sqnum)); | ||
68 | dbg_msg("hdr_crc %08x", ubi32_to_cpu(vid_hdr->hdr_crc)); | ||
69 | dbg_msg("volume identifier header hexdump:"); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * ubi_dbg_dump_vol_info- dump volume information. | ||
74 | * @vol: UBI volume description object | ||
75 | */ | ||
76 | void ubi_dbg_dump_vol_info(const struct ubi_volume *vol) | ||
77 | { | ||
78 | dbg_msg("volume information dump:"); | ||
79 | dbg_msg("vol_id %d", vol->vol_id); | ||
80 | dbg_msg("reserved_pebs %d", vol->reserved_pebs); | ||
81 | dbg_msg("alignment %d", vol->alignment); | ||
82 | dbg_msg("data_pad %d", vol->data_pad); | ||
83 | dbg_msg("vol_type %d", vol->vol_type); | ||
84 | dbg_msg("name_len %d", vol->name_len); | ||
85 | dbg_msg("usable_leb_size %d", vol->usable_leb_size); | ||
86 | dbg_msg("used_ebs %d", vol->used_ebs); | ||
87 | dbg_msg("used_bytes %lld", vol->used_bytes); | ||
88 | dbg_msg("last_eb_bytes %d", vol->last_eb_bytes); | ||
89 | dbg_msg("corrupted %d", vol->corrupted); | ||
90 | dbg_msg("upd_marker %d", vol->upd_marker); | ||
91 | |||
92 | if (vol->name_len <= UBI_VOL_NAME_MAX && | ||
93 | strnlen(vol->name, vol->name_len + 1) == vol->name_len) { | ||
94 | dbg_msg("name %s", vol->name); | ||
95 | } else { | ||
96 | dbg_msg("the 1st 5 characters of the name: %c%c%c%c%c", | ||
97 | vol->name[0], vol->name[1], vol->name[2], | ||
98 | vol->name[3], vol->name[4]); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * ubi_dbg_dump_vtbl_record - dump a &struct ubi_vtbl_record object. | ||
104 | * @r: the object to dump | ||
105 | * @idx: volume table index | ||
106 | */ | ||
107 | void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) | ||
108 | { | ||
109 | int name_len = ubi16_to_cpu(r->name_len); | ||
110 | |||
111 | dbg_msg("volume table record %d dump:", idx); | ||
112 | dbg_msg("reserved_pebs %d", ubi32_to_cpu(r->reserved_pebs)); | ||
113 | dbg_msg("alignment %d", ubi32_to_cpu(r->alignment)); | ||
114 | dbg_msg("data_pad %d", ubi32_to_cpu(r->data_pad)); | ||
115 | dbg_msg("vol_type %d", (int)r->vol_type); | ||
116 | dbg_msg("upd_marker %d", (int)r->upd_marker); | ||
117 | dbg_msg("name_len %d", name_len); | ||
118 | |||
119 | if (r->name[0] == '\0') { | ||
120 | dbg_msg("name NULL"); | ||
121 | return; | ||
122 | } | ||
123 | |||
124 | if (name_len <= UBI_VOL_NAME_MAX && | ||
125 | strnlen(&r->name[0], name_len + 1) == name_len) { | ||
126 | dbg_msg("name %s", &r->name[0]); | ||
127 | } else { | ||
128 | dbg_msg("1st 5 characters of the name: %c%c%c%c%c", | ||
129 | r->name[0], r->name[1], r->name[2], r->name[3], | ||
130 | r->name[4]); | ||
131 | } | ||
132 | dbg_msg("crc %#08x", ubi32_to_cpu(r->crc)); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * ubi_dbg_dump_sv - dump a &struct ubi_scan_volume object. | ||
137 | * @sv: the object to dump | ||
138 | */ | ||
139 | void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) | ||
140 | { | ||
141 | dbg_msg("volume scanning information dump:"); | ||
142 | dbg_msg("vol_id %d", sv->vol_id); | ||
143 | dbg_msg("highest_lnum %d", sv->highest_lnum); | ||
144 | dbg_msg("leb_count %d", sv->leb_count); | ||
145 | dbg_msg("compat %d", sv->compat); | ||
146 | dbg_msg("vol_type %d", sv->vol_type); | ||
147 | dbg_msg("used_ebs %d", sv->used_ebs); | ||
148 | dbg_msg("last_data_size %d", sv->last_data_size); | ||
149 | dbg_msg("data_pad %d", sv->data_pad); | ||
150 | } | ||
151 | |||
152 | /** | ||
153 | * ubi_dbg_dump_seb - dump a &struct ubi_scan_leb object. | ||
154 | * @seb: the object to dump | ||
155 | * @type: object type: 0 - not corrupted, 1 - corrupted | ||
156 | */ | ||
157 | void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type) | ||
158 | { | ||
159 | dbg_msg("eraseblock scanning information dump:"); | ||
160 | dbg_msg("ec %d", seb->ec); | ||
161 | dbg_msg("pnum %d", seb->pnum); | ||
162 | if (type == 0) { | ||
163 | dbg_msg("lnum %d", seb->lnum); | ||
164 | dbg_msg("scrub %d", seb->scrub); | ||
165 | dbg_msg("sqnum %llu", seb->sqnum); | ||
166 | dbg_msg("leb_ver %u", seb->leb_ver); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * ubi_dbg_dump_mkvol_req - dump a &struct ubi_mkvol_req object. | ||
172 | * @req: the object to dump | ||
173 | */ | ||
174 | void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) | ||
175 | { | ||
176 | char nm[17]; | ||
177 | |||
178 | dbg_msg("volume creation request dump:"); | ||
179 | dbg_msg("vol_id %d", req->vol_id); | ||
180 | dbg_msg("alignment %d", req->alignment); | ||
181 | dbg_msg("bytes %lld", (long long)req->bytes); | ||
182 | dbg_msg("vol_type %d", req->vol_type); | ||
183 | dbg_msg("name_len %d", req->name_len); | ||
184 | |||
185 | memcpy(nm, req->name, 16); | ||
186 | nm[16] = 0; | ||
187 | dbg_msg("the 1st 16 characters of the name: %s", nm); | ||
188 | } | ||
189 | |||
190 | #define BYTES_PER_LINE 32 | ||
191 | |||
192 | /** | ||
193 | * ubi_dbg_hexdump - dump a buffer. | ||
194 | * @ptr: the buffer to dump | ||
195 | * @size: buffer size which must be multiple of 4 bytes | ||
196 | */ | ||
197 | void ubi_dbg_hexdump(const void *ptr, int size) | ||
198 | { | ||
199 | int i, k = 0, rows, columns; | ||
200 | const uint8_t *p = ptr; | ||
201 | |||
202 | size = ALIGN(size, 4); | ||
203 | rows = size/BYTES_PER_LINE + size % BYTES_PER_LINE; | ||
204 | for (i = 0; i < rows; i++) { | ||
205 | int j; | ||
206 | |||
207 | cond_resched(); | ||
208 | columns = min(size - k, BYTES_PER_LINE) / 4; | ||
209 | if (columns == 0) | ||
210 | break; | ||
211 | printk(KERN_DEBUG "%5d: ", i * BYTES_PER_LINE); | ||
212 | for (j = 0; j < columns; j++) { | ||
213 | int n, N; | ||
214 | |||
215 | N = size - k > 4 ? 4 : size - k; | ||
216 | for (n = 0; n < N; n++) | ||
217 | printk("%02x", p[k++]); | ||
218 | printk(" "); | ||
219 | } | ||
220 | printk("\n"); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | #endif /* CONFIG_MTD_UBI_DEBUG_MSG */ | ||
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h new file mode 100644 index 000000000000..f816ad9a36c0 --- /dev/null +++ b/drivers/mtd/ubi/debug.h | |||
@@ -0,0 +1,161 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | #ifndef __UBI_DEBUG_H__ | ||
22 | #define __UBI_DEBUG_H__ | ||
23 | |||
24 | #ifdef CONFIG_MTD_UBI_DEBUG | ||
25 | #include <linux/random.h> | ||
26 | |||
27 | #define ubi_assert(expr) BUG_ON(!(expr)) | ||
28 | #define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__) | ||
29 | #else | ||
30 | #define ubi_assert(expr) ({}) | ||
31 | #define dbg_err(fmt, ...) ({}) | ||
32 | #endif | ||
33 | |||
34 | #ifdef CONFIG_MTD_UBI_DEBUG_DISABLE_BGT | ||
35 | #define DBG_DISABLE_BGT 1 | ||
36 | #else | ||
37 | #define DBG_DISABLE_BGT 0 | ||
38 | #endif | ||
39 | |||
40 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG | ||
41 | /* Generic debugging message */ | ||
42 | #define dbg_msg(fmt, ...) \ | ||
43 | printk(KERN_DEBUG "UBI DBG: %s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) | ||
44 | |||
45 | #define ubi_dbg_dump_stack() dump_stack() | ||
46 | |||
47 | struct ubi_ec_hdr; | ||
48 | struct ubi_vid_hdr; | ||
49 | struct ubi_volume; | ||
50 | struct ubi_vtbl_record; | ||
51 | struct ubi_scan_volume; | ||
52 | struct ubi_scan_leb; | ||
53 | struct ubi_mkvol_req; | ||
54 | |||
55 | void ubi_dbg_print(int type, const char *func, const char *fmt, ...); | ||
56 | void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr); | ||
57 | void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr); | ||
58 | void ubi_dbg_dump_vol_info(const struct ubi_volume *vol); | ||
59 | void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx); | ||
60 | void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv); | ||
61 | void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type); | ||
62 | void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req); | ||
63 | void ubi_dbg_hexdump(const void *buf, int size); | ||
64 | |||
65 | #else | ||
66 | |||
67 | #define dbg_msg(fmt, ...) ({}) | ||
68 | #define ubi_dbg_dump_stack() ({}) | ||
69 | #define ubi_dbg_print(func, fmt, ...) ({}) | ||
70 | #define ubi_dbg_dump_ec_hdr(ec_hdr) ({}) | ||
71 | #define ubi_dbg_dump_vid_hdr(vid_hdr) ({}) | ||
72 | #define ubi_dbg_dump_vol_info(vol) ({}) | ||
73 | #define ubi_dbg_dump_vtbl_record(r, idx) ({}) | ||
74 | #define ubi_dbg_dump_sv(sv) ({}) | ||
75 | #define ubi_dbg_dump_seb(seb, type) ({}) | ||
76 | #define ubi_dbg_dump_mkvol_req(req) ({}) | ||
77 | #define ubi_dbg_hexdump(buf, size) ({}) | ||
78 | |||
79 | #endif /* CONFIG_MTD_UBI_DEBUG_MSG */ | ||
80 | |||
81 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG_EBA | ||
82 | /* Messages from the eraseblock association unit */ | ||
83 | #define dbg_eba(fmt, ...) \ | ||
84 | printk(KERN_DEBUG "UBI DBG eba: %s: " fmt "\n", __FUNCTION__, \ | ||
85 | ##__VA_ARGS__) | ||
86 | #else | ||
87 | #define dbg_eba(fmt, ...) ({}) | ||
88 | #endif | ||
89 | |||
90 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG_WL | ||
91 | /* Messages from the wear-leveling unit */ | ||
92 | #define dbg_wl(fmt, ...) \ | ||
93 | printk(KERN_DEBUG "UBI DBG wl: %s: " fmt "\n", __FUNCTION__, \ | ||
94 | ##__VA_ARGS__) | ||
95 | #else | ||
96 | #define dbg_wl(fmt, ...) ({}) | ||
97 | #endif | ||
98 | |||
99 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG_IO | ||
100 | /* Messages from the input/output unit */ | ||
101 | #define dbg_io(fmt, ...) \ | ||
102 | printk(KERN_DEBUG "UBI DBG io: %s: " fmt "\n", __FUNCTION__, \ | ||
103 | ##__VA_ARGS__) | ||
104 | #else | ||
105 | #define dbg_io(fmt, ...) ({}) | ||
106 | #endif | ||
107 | |||
108 | #ifdef CONFIG_MTD_UBI_DEBUG_MSG_BLD | ||
109 | /* Initialization and build messages */ | ||
110 | #define dbg_bld(fmt, ...) \ | ||
111 | printk(KERN_DEBUG "UBI DBG bld: %s: " fmt "\n", __FUNCTION__, \ | ||
112 | ##__VA_ARGS__) | ||
113 | #else | ||
114 | #define dbg_bld(fmt, ...) ({}) | ||
115 | #endif | ||
116 | |||
117 | #ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_BITFLIPS | ||
118 | /** | ||
119 | * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip. | ||
120 | * | ||
121 | * Returns non-zero if a bit-flip should be emulated, otherwise returns zero. | ||
122 | */ | ||
123 | static inline int ubi_dbg_is_bitflip(void) | ||
124 | { | ||
125 | return !(random32() % 200); | ||
126 | } | ||
127 | #else | ||
128 | #define ubi_dbg_is_bitflip() 0 | ||
129 | #endif | ||
130 | |||
131 | #ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES | ||
132 | /** | ||
133 | * ubi_dbg_is_write_failure - if it is time to emulate a write failure. | ||
134 | * | ||
135 | * Returns non-zero if a write failure should be emulated, otherwise returns | ||
136 | * zero. | ||
137 | */ | ||
138 | static inline int ubi_dbg_is_write_failure(void) | ||
139 | { | ||
140 | return !(random32() % 500); | ||
141 | } | ||
142 | #else | ||
143 | #define ubi_dbg_is_write_failure() 0 | ||
144 | #endif | ||
145 | |||
146 | #ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES | ||
147 | /** | ||
148 | * ubi_dbg_is_erase_failure - if its time to emulate an erase failure. | ||
149 | * | ||
150 | * Returns non-zero if an erase failure should be emulated, otherwise returns | ||
151 | * zero. | ||
152 | */ | ||
153 | static inline int ubi_dbg_is_erase_failure(void) | ||
154 | { | ||
155 | return !(random32() % 400); | ||
156 | } | ||
157 | #else | ||
158 | #define ubi_dbg_is_erase_failure() 0 | ||
159 | #endif | ||
160 | |||
161 | #endif /* !__UBI_DEBUG_H__ */ | ||
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c new file mode 100644 index 000000000000..d847ee1da3d9 --- /dev/null +++ b/drivers/mtd/ubi/eba.c | |||
@@ -0,0 +1,1241 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * The UBI Eraseblock Association (EBA) unit. | ||
23 | * | ||
24 | * This unit is responsible for I/O to/from logical eraseblock. | ||
25 | * | ||
26 | * Although in this implementation the EBA table is fully kept and managed in | ||
27 | * RAM, which assumes poor scalability, it might be (partially) maintained on | ||
28 | * flash in future implementations. | ||
29 | * | ||
30 | * The EBA unit implements per-logical eraseblock locking. Before accessing a | ||
31 | * logical eraseblock it is locked for reading or writing. The per-logical | ||
32 | * eraseblock locking is implemented by means of the lock tree. The lock tree | ||
33 | * is an RB-tree which refers all the currently locked logical eraseblocks. The | ||
34 | * lock tree elements are &struct ltree_entry objects. They are indexed by | ||
35 | * (@vol_id, @lnum) pairs. | ||
36 | * | ||
37 | * EBA also maintains the global sequence counter which is incremented each | ||
38 | * time a logical eraseblock is mapped to a physical eraseblock and it is | ||
39 | * stored in the volume identifier header. This means that each VID header has | ||
40 | * a unique sequence number. The sequence number is only increased an we assume | ||
41 | * 64 bits is enough to never overflow. | ||
42 | */ | ||
43 | |||
44 | #include <linux/slab.h> | ||
45 | #include <linux/crc32.h> | ||
46 | #include <linux/err.h> | ||
47 | #include "ubi.h" | ||
48 | |||
49 | /** | ||
50 | * struct ltree_entry - an entry in the lock tree. | ||
51 | * @rb: links RB-tree nodes | ||
52 | * @vol_id: volume ID of the locked logical eraseblock | ||
53 | * @lnum: locked logical eraseblock number | ||
54 | * @users: how many tasks are using this logical eraseblock or wait for it | ||
55 | * @mutex: read/write mutex to implement read/write access serialization to | ||
56 | * the (@vol_id, @lnum) logical eraseblock | ||
57 | * | ||
58 | * When a logical eraseblock is being locked - corresponding &struct ltree_entry | ||
59 | * object is inserted to the lock tree (@ubi->ltree). | ||
60 | */ | ||
61 | struct ltree_entry { | ||
62 | struct rb_node rb; | ||
63 | int vol_id; | ||
64 | int lnum; | ||
65 | int users; | ||
66 | struct rw_semaphore mutex; | ||
67 | }; | ||
68 | |||
69 | /* Slab cache for lock-tree entries */ | ||
70 | static struct kmem_cache *ltree_slab; | ||
71 | |||
72 | /** | ||
73 | * next_sqnum - get next sequence number. | ||
74 | * @ubi: UBI device description object | ||
75 | * | ||
76 | * This function returns next sequence number to use, which is just the current | ||
77 | * global sequence counter value. It also increases the global sequence | ||
78 | * counter. | ||
79 | */ | ||
80 | static unsigned long long next_sqnum(struct ubi_device *ubi) | ||
81 | { | ||
82 | unsigned long long sqnum; | ||
83 | |||
84 | spin_lock(&ubi->ltree_lock); | ||
85 | sqnum = ubi->global_sqnum++; | ||
86 | spin_unlock(&ubi->ltree_lock); | ||
87 | |||
88 | return sqnum; | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * ubi_get_compat - get compatibility flags of a volume. | ||
93 | * @ubi: UBI device description object | ||
94 | * @vol_id: volume ID | ||
95 | * | ||
96 | * This function returns compatibility flags for an internal volume. User | ||
97 | * volumes have no compatibility flags, so %0 is returned. | ||
98 | */ | ||
99 | static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) | ||
100 | { | ||
101 | if (vol_id == UBI_LAYOUT_VOL_ID) | ||
102 | return UBI_LAYOUT_VOLUME_COMPAT; | ||
103 | return 0; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * ltree_lookup - look up the lock tree. | ||
108 | * @ubi: UBI device description object | ||
109 | * @vol_id: volume ID | ||
110 | * @lnum: logical eraseblock number | ||
111 | * | ||
112 | * This function returns a pointer to the corresponding &struct ltree_entry | ||
113 | * object if the logical eraseblock is locked and %NULL if it is not. | ||
114 | * @ubi->ltree_lock has to be locked. | ||
115 | */ | ||
116 | static struct ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, | ||
117 | int lnum) | ||
118 | { | ||
119 | struct rb_node *p; | ||
120 | |||
121 | p = ubi->ltree.rb_node; | ||
122 | while (p) { | ||
123 | struct ltree_entry *le; | ||
124 | |||
125 | le = rb_entry(p, struct ltree_entry, rb); | ||
126 | |||
127 | if (vol_id < le->vol_id) | ||
128 | p = p->rb_left; | ||
129 | else if (vol_id > le->vol_id) | ||
130 | p = p->rb_right; | ||
131 | else { | ||
132 | if (lnum < le->lnum) | ||
133 | p = p->rb_left; | ||
134 | else if (lnum > le->lnum) | ||
135 | p = p->rb_right; | ||
136 | else | ||
137 | return le; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return NULL; | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * ltree_add_entry - add new entry to the lock tree. | ||
146 | * @ubi: UBI device description object | ||
147 | * @vol_id: volume ID | ||
148 | * @lnum: logical eraseblock number | ||
149 | * | ||
150 | * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the | ||
151 | * lock tree. If such entry is already there, its usage counter is increased. | ||
152 | * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation | ||
153 | * failed. | ||
154 | */ | ||
155 | static struct ltree_entry *ltree_add_entry(struct ubi_device *ubi, int vol_id, | ||
156 | int lnum) | ||
157 | { | ||
158 | struct ltree_entry *le, *le1, *le_free; | ||
159 | |||
160 | le = kmem_cache_alloc(ltree_slab, GFP_KERNEL); | ||
161 | if (!le) | ||
162 | return ERR_PTR(-ENOMEM); | ||
163 | |||
164 | le->vol_id = vol_id; | ||
165 | le->lnum = lnum; | ||
166 | |||
167 | spin_lock(&ubi->ltree_lock); | ||
168 | le1 = ltree_lookup(ubi, vol_id, lnum); | ||
169 | |||
170 | if (le1) { | ||
171 | /* | ||
172 | * This logical eraseblock is already locked. The newly | ||
173 | * allocated lock entry is not needed. | ||
174 | */ | ||
175 | le_free = le; | ||
176 | le = le1; | ||
177 | } else { | ||
178 | struct rb_node **p, *parent = NULL; | ||
179 | |||
180 | /* | ||
181 | * No lock entry, add the newly allocated one to the | ||
182 | * @ubi->ltree RB-tree. | ||
183 | */ | ||
184 | le_free = NULL; | ||
185 | |||
186 | p = &ubi->ltree.rb_node; | ||
187 | while (*p) { | ||
188 | parent = *p; | ||
189 | le1 = rb_entry(parent, struct ltree_entry, rb); | ||
190 | |||
191 | if (vol_id < le1->vol_id) | ||
192 | p = &(*p)->rb_left; | ||
193 | else if (vol_id > le1->vol_id) | ||
194 | p = &(*p)->rb_right; | ||
195 | else { | ||
196 | ubi_assert(lnum != le1->lnum); | ||
197 | if (lnum < le1->lnum) | ||
198 | p = &(*p)->rb_left; | ||
199 | else | ||
200 | p = &(*p)->rb_right; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | rb_link_node(&le->rb, parent, p); | ||
205 | rb_insert_color(&le->rb, &ubi->ltree); | ||
206 | } | ||
207 | le->users += 1; | ||
208 | spin_unlock(&ubi->ltree_lock); | ||
209 | |||
210 | if (le_free) | ||
211 | kmem_cache_free(ltree_slab, le_free); | ||
212 | |||
213 | return le; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * leb_read_lock - lock logical eraseblock for reading. | ||
218 | * @ubi: UBI device description object | ||
219 | * @vol_id: volume ID | ||
220 | * @lnum: logical eraseblock number | ||
221 | * | ||
222 | * This function locks a logical eraseblock for reading. Returns zero in case | ||
223 | * of success and a negative error code in case of failure. | ||
224 | */ | ||
225 | static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) | ||
226 | { | ||
227 | struct ltree_entry *le; | ||
228 | |||
229 | le = ltree_add_entry(ubi, vol_id, lnum); | ||
230 | if (IS_ERR(le)) | ||
231 | return PTR_ERR(le); | ||
232 | down_read(&le->mutex); | ||
233 | return 0; | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * leb_read_unlock - unlock logical eraseblock. | ||
238 | * @ubi: UBI device description object | ||
239 | * @vol_id: volume ID | ||
240 | * @lnum: logical eraseblock number | ||
241 | */ | ||
242 | static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) | ||
243 | { | ||
244 | int free = 0; | ||
245 | struct ltree_entry *le; | ||
246 | |||
247 | spin_lock(&ubi->ltree_lock); | ||
248 | le = ltree_lookup(ubi, vol_id, lnum); | ||
249 | le->users -= 1; | ||
250 | ubi_assert(le->users >= 0); | ||
251 | if (le->users == 0) { | ||
252 | rb_erase(&le->rb, &ubi->ltree); | ||
253 | free = 1; | ||
254 | } | ||
255 | spin_unlock(&ubi->ltree_lock); | ||
256 | |||
257 | up_read(&le->mutex); | ||
258 | if (free) | ||
259 | kmem_cache_free(ltree_slab, le); | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * leb_write_lock - lock logical eraseblock for writing. | ||
264 | * @ubi: UBI device description object | ||
265 | * @vol_id: volume ID | ||
266 | * @lnum: logical eraseblock number | ||
267 | * | ||
268 | * This function locks a logical eraseblock for writing. Returns zero in case | ||
269 | * of success and a negative error code in case of failure. | ||
270 | */ | ||
271 | static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) | ||
272 | { | ||
273 | struct ltree_entry *le; | ||
274 | |||
275 | le = ltree_add_entry(ubi, vol_id, lnum); | ||
276 | if (IS_ERR(le)) | ||
277 | return PTR_ERR(le); | ||
278 | down_write(&le->mutex); | ||
279 | return 0; | ||
280 | } | ||
281 | |||
282 | /** | ||
283 | * leb_write_unlock - unlock logical eraseblock. | ||
284 | * @ubi: UBI device description object | ||
285 | * @vol_id: volume ID | ||
286 | * @lnum: logical eraseblock number | ||
287 | */ | ||
288 | static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) | ||
289 | { | ||
290 | int free; | ||
291 | struct ltree_entry *le; | ||
292 | |||
293 | spin_lock(&ubi->ltree_lock); | ||
294 | le = ltree_lookup(ubi, vol_id, lnum); | ||
295 | le->users -= 1; | ||
296 | ubi_assert(le->users >= 0); | ||
297 | if (le->users == 0) { | ||
298 | rb_erase(&le->rb, &ubi->ltree); | ||
299 | free = 1; | ||
300 | } else | ||
301 | free = 0; | ||
302 | spin_unlock(&ubi->ltree_lock); | ||
303 | |||
304 | up_write(&le->mutex); | ||
305 | if (free) | ||
306 | kmem_cache_free(ltree_slab, le); | ||
307 | } | ||
308 | |||
309 | /** | ||
310 | * ubi_eba_unmap_leb - un-map logical eraseblock. | ||
311 | * @ubi: UBI device description object | ||
312 | * @vol_id: volume ID | ||
313 | * @lnum: logical eraseblock number | ||
314 | * | ||
315 | * This function un-maps logical eraseblock @lnum and schedules corresponding | ||
316 | * physical eraseblock for erasure. Returns zero in case of success and a | ||
317 | * negative error code in case of failure. | ||
318 | */ | ||
319 | int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum) | ||
320 | { | ||
321 | int idx = vol_id2idx(ubi, vol_id), err, pnum; | ||
322 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
323 | |||
324 | if (ubi->ro_mode) | ||
325 | return -EROFS; | ||
326 | |||
327 | err = leb_write_lock(ubi, vol_id, lnum); | ||
328 | if (err) | ||
329 | return err; | ||
330 | |||
331 | pnum = vol->eba_tbl[lnum]; | ||
332 | if (pnum < 0) | ||
333 | /* This logical eraseblock is already unmapped */ | ||
334 | goto out_unlock; | ||
335 | |||
336 | dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum); | ||
337 | |||
338 | vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED; | ||
339 | err = ubi_wl_put_peb(ubi, pnum, 0); | ||
340 | |||
341 | out_unlock: | ||
342 | leb_write_unlock(ubi, vol_id, lnum); | ||
343 | return err; | ||
344 | } | ||
345 | |||
346 | /** | ||
347 | * ubi_eba_read_leb - read data. | ||
348 | * @ubi: UBI device description object | ||
349 | * @vol_id: volume ID | ||
350 | * @lnum: logical eraseblock number | ||
351 | * @buf: buffer to store the read data | ||
352 | * @offset: offset from where to read | ||
353 | * @len: how many bytes to read | ||
354 | * @check: data CRC check flag | ||
355 | * | ||
356 | * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF | ||
357 | * bytes. The @check flag only makes sense for static volumes and forces | ||
358 | * eraseblock data CRC checking. | ||
359 | * | ||
360 | * In case of success this function returns zero. In case of a static volume, | ||
361 | * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be | ||
362 | * returned for any volume type if an ECC error was detected by the MTD device | ||
363 | * driver. Other negative error cored may be returned in case of other errors. | ||
364 | */ | ||
365 | int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf, | ||
366 | int offset, int len, int check) | ||
367 | { | ||
368 | int err, pnum, scrub = 0, idx = vol_id2idx(ubi, vol_id); | ||
369 | struct ubi_vid_hdr *vid_hdr; | ||
370 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
371 | uint32_t crc, crc1; | ||
372 | |||
373 | err = leb_read_lock(ubi, vol_id, lnum); | ||
374 | if (err) | ||
375 | return err; | ||
376 | |||
377 | pnum = vol->eba_tbl[lnum]; | ||
378 | if (pnum < 0) { | ||
379 | /* | ||
380 | * The logical eraseblock is not mapped, fill the whole buffer | ||
381 | * with 0xFF bytes. The exception is static volumes for which | ||
382 | * it is an error to read unmapped logical eraseblocks. | ||
383 | */ | ||
384 | dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)", | ||
385 | len, offset, vol_id, lnum); | ||
386 | leb_read_unlock(ubi, vol_id, lnum); | ||
387 | ubi_assert(vol->vol_type != UBI_STATIC_VOLUME); | ||
388 | memset(buf, 0xFF, len); | ||
389 | return 0; | ||
390 | } | ||
391 | |||
392 | dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d", | ||
393 | len, offset, vol_id, lnum, pnum); | ||
394 | |||
395 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) | ||
396 | check = 0; | ||
397 | |||
398 | retry: | ||
399 | if (check) { | ||
400 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
401 | if (!vid_hdr) { | ||
402 | err = -ENOMEM; | ||
403 | goto out_unlock; | ||
404 | } | ||
405 | |||
406 | err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); | ||
407 | if (err && err != UBI_IO_BITFLIPS) { | ||
408 | if (err > 0) { | ||
409 | /* | ||
410 | * The header is either absent or corrupted. | ||
411 | * The former case means there is a bug - | ||
412 | * switch to read-only mode just in case. | ||
413 | * The latter case means a real corruption - we | ||
414 | * may try to recover data. FIXME: but this is | ||
415 | * not implemented. | ||
416 | */ | ||
417 | if (err == UBI_IO_BAD_VID_HDR) { | ||
418 | ubi_warn("bad VID header at PEB %d, LEB" | ||
419 | "%d:%d", pnum, vol_id, lnum); | ||
420 | err = -EBADMSG; | ||
421 | } else | ||
422 | ubi_ro_mode(ubi); | ||
423 | } | ||
424 | goto out_free; | ||
425 | } else if (err == UBI_IO_BITFLIPS) | ||
426 | scrub = 1; | ||
427 | |||
428 | ubi_assert(lnum < ubi32_to_cpu(vid_hdr->used_ebs)); | ||
429 | ubi_assert(len == ubi32_to_cpu(vid_hdr->data_size)); | ||
430 | |||
431 | crc = ubi32_to_cpu(vid_hdr->data_crc); | ||
432 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
433 | } | ||
434 | |||
435 | err = ubi_io_read_data(ubi, buf, pnum, offset, len); | ||
436 | if (err) { | ||
437 | if (err == UBI_IO_BITFLIPS) { | ||
438 | scrub = 1; | ||
439 | err = 0; | ||
440 | } else if (err == -EBADMSG) { | ||
441 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) | ||
442 | goto out_unlock; | ||
443 | scrub = 1; | ||
444 | if (!check) { | ||
445 | ubi_msg("force data checking"); | ||
446 | check = 1; | ||
447 | goto retry; | ||
448 | } | ||
449 | } else | ||
450 | goto out_unlock; | ||
451 | } | ||
452 | |||
453 | if (check) { | ||
454 | crc1 = crc32(UBI_CRC32_INIT, buf, len); | ||
455 | if (crc1 != crc) { | ||
456 | ubi_warn("CRC error: calculated %#08x, must be %#08x", | ||
457 | crc1, crc); | ||
458 | err = -EBADMSG; | ||
459 | goto out_unlock; | ||
460 | } | ||
461 | } | ||
462 | |||
463 | if (scrub) | ||
464 | err = ubi_wl_scrub_peb(ubi, pnum); | ||
465 | |||
466 | leb_read_unlock(ubi, vol_id, lnum); | ||
467 | return err; | ||
468 | |||
469 | out_free: | ||
470 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
471 | out_unlock: | ||
472 | leb_read_unlock(ubi, vol_id, lnum); | ||
473 | return err; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * recover_peb - recover from write failure. | ||
478 | * @ubi: UBI device description object | ||
479 | * @pnum: the physical eraseblock to recover | ||
480 | * @vol_id: volume ID | ||
481 | * @lnum: logical eraseblock number | ||
482 | * @buf: data which was not written because of the write failure | ||
483 | * @offset: offset of the failed write | ||
484 | * @len: how many bytes should have been written | ||
485 | * | ||
486 | * This function is called in case of a write failure and moves all good data | ||
487 | * from the potentially bad physical eraseblock to a good physical eraseblock. | ||
488 | * This function also writes the data which was not written due to the failure. | ||
489 | * Returns new physical eraseblock number in case of success, and a negative | ||
490 | * error code in case of failure. | ||
491 | */ | ||
492 | static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, | ||
493 | const void *buf, int offset, int len) | ||
494 | { | ||
495 | int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0; | ||
496 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
497 | struct ubi_vid_hdr *vid_hdr; | ||
498 | unsigned char *new_buf; | ||
499 | |||
500 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
501 | if (!vid_hdr) { | ||
502 | return -ENOMEM; | ||
503 | } | ||
504 | |||
505 | retry: | ||
506 | new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN); | ||
507 | if (new_pnum < 0) { | ||
508 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
509 | return new_pnum; | ||
510 | } | ||
511 | |||
512 | ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum); | ||
513 | |||
514 | err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); | ||
515 | if (err && err != UBI_IO_BITFLIPS) { | ||
516 | if (err > 0) | ||
517 | err = -EIO; | ||
518 | goto out_put; | ||
519 | } | ||
520 | |||
521 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
522 | err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr); | ||
523 | if (err) | ||
524 | goto write_error; | ||
525 | |||
526 | data_size = offset + len; | ||
527 | new_buf = kmalloc(data_size, GFP_KERNEL); | ||
528 | if (!new_buf) { | ||
529 | err = -ENOMEM; | ||
530 | goto out_put; | ||
531 | } | ||
532 | memset(new_buf + offset, 0xFF, len); | ||
533 | |||
534 | /* Read everything before the area where the write failure happened */ | ||
535 | if (offset > 0) { | ||
536 | err = ubi_io_read_data(ubi, new_buf, pnum, 0, offset); | ||
537 | if (err && err != UBI_IO_BITFLIPS) { | ||
538 | kfree(new_buf); | ||
539 | goto out_put; | ||
540 | } | ||
541 | } | ||
542 | |||
543 | memcpy(new_buf + offset, buf, len); | ||
544 | |||
545 | err = ubi_io_write_data(ubi, new_buf, new_pnum, 0, data_size); | ||
546 | if (err) { | ||
547 | kfree(new_buf); | ||
548 | goto write_error; | ||
549 | } | ||
550 | |||
551 | kfree(new_buf); | ||
552 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
553 | |||
554 | vol->eba_tbl[lnum] = new_pnum; | ||
555 | ubi_wl_put_peb(ubi, pnum, 1); | ||
556 | |||
557 | ubi_msg("data was successfully recovered"); | ||
558 | return 0; | ||
559 | |||
560 | out_put: | ||
561 | ubi_wl_put_peb(ubi, new_pnum, 1); | ||
562 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
563 | return err; | ||
564 | |||
565 | write_error: | ||
566 | /* | ||
567 | * Bad luck? This physical eraseblock is bad too? Crud. Let's try to | ||
568 | * get another one. | ||
569 | */ | ||
570 | ubi_warn("failed to write to PEB %d", new_pnum); | ||
571 | ubi_wl_put_peb(ubi, new_pnum, 1); | ||
572 | if (++tries > UBI_IO_RETRIES) { | ||
573 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
574 | return err; | ||
575 | } | ||
576 | ubi_msg("try again"); | ||
577 | goto retry; | ||
578 | } | ||
579 | |||
580 | /** | ||
581 | * ubi_eba_write_leb - write data to dynamic volume. | ||
582 | * @ubi: UBI device description object | ||
583 | * @vol_id: volume ID | ||
584 | * @lnum: logical eraseblock number | ||
585 | * @buf: the data to write | ||
586 | * @offset: offset within the logical eraseblock where to write | ||
587 | * @len: how many bytes to write | ||
588 | * @dtype: data type | ||
589 | * | ||
590 | * This function writes data to logical eraseblock @lnum of a dynamic volume | ||
591 | * @vol_id. Returns zero in case of success and a negative error code in case | ||
592 | * of failure. In case of error, it is possible that something was still | ||
593 | * written to the flash media, but may be some garbage. | ||
594 | */ | ||
595 | int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum, | ||
596 | const void *buf, int offset, int len, int dtype) | ||
597 | { | ||
598 | int idx = vol_id2idx(ubi, vol_id), err, pnum, tries = 0; | ||
599 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
600 | struct ubi_vid_hdr *vid_hdr; | ||
601 | |||
602 | if (ubi->ro_mode) | ||
603 | return -EROFS; | ||
604 | |||
605 | err = leb_write_lock(ubi, vol_id, lnum); | ||
606 | if (err) | ||
607 | return err; | ||
608 | |||
609 | pnum = vol->eba_tbl[lnum]; | ||
610 | if (pnum >= 0) { | ||
611 | dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d", | ||
612 | len, offset, vol_id, lnum, pnum); | ||
613 | |||
614 | err = ubi_io_write_data(ubi, buf, pnum, offset, len); | ||
615 | if (err) { | ||
616 | ubi_warn("failed to write data to PEB %d", pnum); | ||
617 | if (err == -EIO && ubi->bad_allowed) | ||
618 | err = recover_peb(ubi, pnum, vol_id, lnum, buf, offset, len); | ||
619 | if (err) | ||
620 | ubi_ro_mode(ubi); | ||
621 | } | ||
622 | leb_write_unlock(ubi, vol_id, lnum); | ||
623 | return err; | ||
624 | } | ||
625 | |||
626 | /* | ||
627 | * The logical eraseblock is not mapped. We have to get a free physical | ||
628 | * eraseblock and write the volume identifier header there first. | ||
629 | */ | ||
630 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
631 | if (!vid_hdr) { | ||
632 | leb_write_unlock(ubi, vol_id, lnum); | ||
633 | return -ENOMEM; | ||
634 | } | ||
635 | |||
636 | vid_hdr->vol_type = UBI_VID_DYNAMIC; | ||
637 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
638 | vid_hdr->vol_id = cpu_to_ubi32(vol_id); | ||
639 | vid_hdr->lnum = cpu_to_ubi32(lnum); | ||
640 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); | ||
641 | vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); | ||
642 | |||
643 | retry: | ||
644 | pnum = ubi_wl_get_peb(ubi, dtype); | ||
645 | if (pnum < 0) { | ||
646 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
647 | leb_write_unlock(ubi, vol_id, lnum); | ||
648 | return pnum; | ||
649 | } | ||
650 | |||
651 | dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d", | ||
652 | len, offset, vol_id, lnum, pnum); | ||
653 | |||
654 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); | ||
655 | if (err) { | ||
656 | ubi_warn("failed to write VID header to LEB %d:%d, PEB %d", | ||
657 | vol_id, lnum, pnum); | ||
658 | goto write_error; | ||
659 | } | ||
660 | |||
661 | err = ubi_io_write_data(ubi, buf, pnum, offset, len); | ||
662 | if (err) { | ||
663 | ubi_warn("failed to write %d bytes at offset %d of LEB %d:%d, " | ||
664 | "PEB %d", len, offset, vol_id, lnum, pnum); | ||
665 | goto write_error; | ||
666 | } | ||
667 | |||
668 | vol->eba_tbl[lnum] = pnum; | ||
669 | |||
670 | leb_write_unlock(ubi, vol_id, lnum); | ||
671 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
672 | return 0; | ||
673 | |||
674 | write_error: | ||
675 | if (err != -EIO || !ubi->bad_allowed) { | ||
676 | ubi_ro_mode(ubi); | ||
677 | leb_write_unlock(ubi, vol_id, lnum); | ||
678 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
679 | return err; | ||
680 | } | ||
681 | |||
682 | /* | ||
683 | * Fortunately, this is the first write operation to this physical | ||
684 | * eraseblock, so just put it and request a new one. We assume that if | ||
685 | * this physical eraseblock went bad, the erase code will handle that. | ||
686 | */ | ||
687 | err = ubi_wl_put_peb(ubi, pnum, 1); | ||
688 | if (err || ++tries > UBI_IO_RETRIES) { | ||
689 | ubi_ro_mode(ubi); | ||
690 | leb_write_unlock(ubi, vol_id, lnum); | ||
691 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
692 | return err; | ||
693 | } | ||
694 | |||
695 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
696 | ubi_msg("try another PEB"); | ||
697 | goto retry; | ||
698 | } | ||
699 | |||
700 | /** | ||
701 | * ubi_eba_write_leb_st - write data to static volume. | ||
702 | * @ubi: UBI device description object | ||
703 | * @vol_id: volume ID | ||
704 | * @lnum: logical eraseblock number | ||
705 | * @buf: data to write | ||
706 | * @len: how many bytes to write | ||
707 | * @dtype: data type | ||
708 | * @used_ebs: how many logical eraseblocks will this volume contain | ||
709 | * | ||
710 | * This function writes data to logical eraseblock @lnum of static volume | ||
711 | * @vol_id. The @used_ebs argument should contain total number of logical | ||
712 | * eraseblock in this static volume. | ||
713 | * | ||
714 | * When writing to the last logical eraseblock, the @len argument doesn't have | ||
715 | * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent | ||
716 | * to the real data size, although the @buf buffer has to contain the | ||
717 | * alignment. In all other cases, @len has to be aligned. | ||
718 | * | ||
719 | * It is prohibited to write more then once to logical eraseblocks of static | ||
720 | * volumes. This function returns zero in case of success and a negative error | ||
721 | * code in case of failure. | ||
722 | */ | ||
723 | int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum, | ||
724 | const void *buf, int len, int dtype, int used_ebs) | ||
725 | { | ||
726 | int err, pnum, tries = 0, data_size = len; | ||
727 | int idx = vol_id2idx(ubi, vol_id); | ||
728 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
729 | struct ubi_vid_hdr *vid_hdr; | ||
730 | uint32_t crc; | ||
731 | |||
732 | if (ubi->ro_mode) | ||
733 | return -EROFS; | ||
734 | |||
735 | if (lnum == used_ebs - 1) | ||
736 | /* If this is the last LEB @len may be unaligned */ | ||
737 | len = ALIGN(data_size, ubi->min_io_size); | ||
738 | else | ||
739 | ubi_assert(len % ubi->min_io_size == 0); | ||
740 | |||
741 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
742 | if (!vid_hdr) | ||
743 | return -ENOMEM; | ||
744 | |||
745 | err = leb_write_lock(ubi, vol_id, lnum); | ||
746 | if (err) { | ||
747 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
748 | return err; | ||
749 | } | ||
750 | |||
751 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
752 | vid_hdr->vol_id = cpu_to_ubi32(vol_id); | ||
753 | vid_hdr->lnum = cpu_to_ubi32(lnum); | ||
754 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); | ||
755 | vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); | ||
756 | |||
757 | crc = crc32(UBI_CRC32_INIT, buf, data_size); | ||
758 | vid_hdr->vol_type = UBI_VID_STATIC; | ||
759 | vid_hdr->data_size = cpu_to_ubi32(data_size); | ||
760 | vid_hdr->used_ebs = cpu_to_ubi32(used_ebs); | ||
761 | vid_hdr->data_crc = cpu_to_ubi32(crc); | ||
762 | |||
763 | retry: | ||
764 | pnum = ubi_wl_get_peb(ubi, dtype); | ||
765 | if (pnum < 0) { | ||
766 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
767 | leb_write_unlock(ubi, vol_id, lnum); | ||
768 | return pnum; | ||
769 | } | ||
770 | |||
771 | dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d", | ||
772 | len, vol_id, lnum, pnum, used_ebs); | ||
773 | |||
774 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); | ||
775 | if (err) { | ||
776 | ubi_warn("failed to write VID header to LEB %d:%d, PEB %d", | ||
777 | vol_id, lnum, pnum); | ||
778 | goto write_error; | ||
779 | } | ||
780 | |||
781 | err = ubi_io_write_data(ubi, buf, pnum, 0, len); | ||
782 | if (err) { | ||
783 | ubi_warn("failed to write %d bytes of data to PEB %d", | ||
784 | len, pnum); | ||
785 | goto write_error; | ||
786 | } | ||
787 | |||
788 | ubi_assert(vol->eba_tbl[lnum] < 0); | ||
789 | vol->eba_tbl[lnum] = pnum; | ||
790 | |||
791 | leb_write_unlock(ubi, vol_id, lnum); | ||
792 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
793 | return 0; | ||
794 | |||
795 | write_error: | ||
796 | if (err != -EIO || !ubi->bad_allowed) { | ||
797 | /* | ||
798 | * This flash device does not admit of bad eraseblocks or | ||
799 | * something nasty and unexpected happened. Switch to read-only | ||
800 | * mode just in case. | ||
801 | */ | ||
802 | ubi_ro_mode(ubi); | ||
803 | leb_write_unlock(ubi, vol_id, lnum); | ||
804 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
805 | return err; | ||
806 | } | ||
807 | |||
808 | err = ubi_wl_put_peb(ubi, pnum, 1); | ||
809 | if (err || ++tries > UBI_IO_RETRIES) { | ||
810 | ubi_ro_mode(ubi); | ||
811 | leb_write_unlock(ubi, vol_id, lnum); | ||
812 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
813 | return err; | ||
814 | } | ||
815 | |||
816 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
817 | ubi_msg("try another PEB"); | ||
818 | goto retry; | ||
819 | } | ||
820 | |||
821 | /* | ||
822 | * ubi_eba_atomic_leb_change - change logical eraseblock atomically. | ||
823 | * @ubi: UBI device description object | ||
824 | * @vol_id: volume ID | ||
825 | * @lnum: logical eraseblock number | ||
826 | * @buf: data to write | ||
827 | * @len: how many bytes to write | ||
828 | * @dtype: data type | ||
829 | * | ||
830 | * This function changes the contents of a logical eraseblock atomically. @buf | ||
831 | * has to contain new logical eraseblock data, and @len - the length of the | ||
832 | * data, which has to be aligned. This function guarantees that in case of an | ||
833 | * unclean reboot the old contents is preserved. Returns zero in case of | ||
834 | * success and a negative error code in case of failure. | ||
835 | */ | ||
836 | int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum, | ||
837 | const void *buf, int len, int dtype) | ||
838 | { | ||
839 | int err, pnum, tries = 0, idx = vol_id2idx(ubi, vol_id); | ||
840 | struct ubi_volume *vol = ubi->volumes[idx]; | ||
841 | struct ubi_vid_hdr *vid_hdr; | ||
842 | uint32_t crc; | ||
843 | |||
844 | if (ubi->ro_mode) | ||
845 | return -EROFS; | ||
846 | |||
847 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
848 | if (!vid_hdr) | ||
849 | return -ENOMEM; | ||
850 | |||
851 | err = leb_write_lock(ubi, vol_id, lnum); | ||
852 | if (err) { | ||
853 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
854 | return err; | ||
855 | } | ||
856 | |||
857 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
858 | vid_hdr->vol_id = cpu_to_ubi32(vol_id); | ||
859 | vid_hdr->lnum = cpu_to_ubi32(lnum); | ||
860 | vid_hdr->compat = ubi_get_compat(ubi, vol_id); | ||
861 | vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); | ||
862 | |||
863 | crc = crc32(UBI_CRC32_INIT, buf, len); | ||
864 | vid_hdr->vol_type = UBI_VID_STATIC; | ||
865 | vid_hdr->data_size = cpu_to_ubi32(len); | ||
866 | vid_hdr->copy_flag = 1; | ||
867 | vid_hdr->data_crc = cpu_to_ubi32(crc); | ||
868 | |||
869 | retry: | ||
870 | pnum = ubi_wl_get_peb(ubi, dtype); | ||
871 | if (pnum < 0) { | ||
872 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
873 | leb_write_unlock(ubi, vol_id, lnum); | ||
874 | return pnum; | ||
875 | } | ||
876 | |||
877 | dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d", | ||
878 | vol_id, lnum, vol->eba_tbl[lnum], pnum); | ||
879 | |||
880 | err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); | ||
881 | if (err) { | ||
882 | ubi_warn("failed to write VID header to LEB %d:%d, PEB %d", | ||
883 | vol_id, lnum, pnum); | ||
884 | goto write_error; | ||
885 | } | ||
886 | |||
887 | err = ubi_io_write_data(ubi, buf, pnum, 0, len); | ||
888 | if (err) { | ||
889 | ubi_warn("failed to write %d bytes of data to PEB %d", | ||
890 | len, pnum); | ||
891 | goto write_error; | ||
892 | } | ||
893 | |||
894 | err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1); | ||
895 | if (err) { | ||
896 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
897 | leb_write_unlock(ubi, vol_id, lnum); | ||
898 | return err; | ||
899 | } | ||
900 | |||
901 | vol->eba_tbl[lnum] = pnum; | ||
902 | leb_write_unlock(ubi, vol_id, lnum); | ||
903 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
904 | return 0; | ||
905 | |||
906 | write_error: | ||
907 | if (err != -EIO || !ubi->bad_allowed) { | ||
908 | /* | ||
909 | * This flash device does not admit of bad eraseblocks or | ||
910 | * something nasty and unexpected happened. Switch to read-only | ||
911 | * mode just in case. | ||
912 | */ | ||
913 | ubi_ro_mode(ubi); | ||
914 | leb_write_unlock(ubi, vol_id, lnum); | ||
915 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
916 | return err; | ||
917 | } | ||
918 | |||
919 | err = ubi_wl_put_peb(ubi, pnum, 1); | ||
920 | if (err || ++tries > UBI_IO_RETRIES) { | ||
921 | ubi_ro_mode(ubi); | ||
922 | leb_write_unlock(ubi, vol_id, lnum); | ||
923 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
924 | return err; | ||
925 | } | ||
926 | |||
927 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
928 | ubi_msg("try another PEB"); | ||
929 | goto retry; | ||
930 | } | ||
931 | |||
932 | /** | ||
933 | * ltree_entry_ctor - lock tree entries slab cache constructor. | ||
934 | * @obj: the lock-tree entry to construct | ||
935 | * @cache: the lock tree entry slab cache | ||
936 | * @flags: constructor flags | ||
937 | */ | ||
938 | static void ltree_entry_ctor(void *obj, struct kmem_cache *cache, | ||
939 | unsigned long flags) | ||
940 | { | ||
941 | struct ltree_entry *le = obj; | ||
942 | |||
943 | if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) != | ||
944 | SLAB_CTOR_CONSTRUCTOR) | ||
945 | return; | ||
946 | |||
947 | le->users = 0; | ||
948 | init_rwsem(&le->mutex); | ||
949 | } | ||
950 | |||
951 | /** | ||
952 | * ubi_eba_copy_leb - copy logical eraseblock. | ||
953 | * @ubi: UBI device description object | ||
954 | * @from: physical eraseblock number from where to copy | ||
955 | * @to: physical eraseblock number where to copy | ||
956 | * @vid_hdr: VID header of the @from physical eraseblock | ||
957 | * | ||
958 | * This function copies logical eraseblock from physical eraseblock @from to | ||
959 | * physical eraseblock @to. The @vid_hdr buffer may be changed by this | ||
960 | * function. Returns zero in case of success, %UBI_IO_BITFLIPS if the operation | ||
961 | * was canceled because bit-flips were detected at the target PEB, and a | ||
962 | * negative error code in case of failure. | ||
963 | */ | ||
964 | int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, | ||
965 | struct ubi_vid_hdr *vid_hdr) | ||
966 | { | ||
967 | int err, vol_id, lnum, data_size, aldata_size, pnum, idx; | ||
968 | struct ubi_volume *vol; | ||
969 | uint32_t crc; | ||
970 | void *buf, *buf1 = NULL; | ||
971 | |||
972 | vol_id = ubi32_to_cpu(vid_hdr->vol_id); | ||
973 | lnum = ubi32_to_cpu(vid_hdr->lnum); | ||
974 | |||
975 | dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to); | ||
976 | |||
977 | if (vid_hdr->vol_type == UBI_VID_STATIC) { | ||
978 | data_size = ubi32_to_cpu(vid_hdr->data_size); | ||
979 | aldata_size = ALIGN(data_size, ubi->min_io_size); | ||
980 | } else | ||
981 | data_size = aldata_size = | ||
982 | ubi->leb_size - ubi32_to_cpu(vid_hdr->data_pad); | ||
983 | |||
984 | buf = kmalloc(aldata_size, GFP_KERNEL); | ||
985 | if (!buf) | ||
986 | return -ENOMEM; | ||
987 | |||
988 | /* | ||
989 | * We do not want anybody to write to this logical eraseblock while we | ||
990 | * are moving it, so we lock it. | ||
991 | */ | ||
992 | err = leb_write_lock(ubi, vol_id, lnum); | ||
993 | if (err) { | ||
994 | kfree(buf); | ||
995 | return err; | ||
996 | } | ||
997 | |||
998 | /* | ||
999 | * But the logical eraseblock might have been put by this time. | ||
1000 | * Cancel if it is true. | ||
1001 | */ | ||
1002 | idx = vol_id2idx(ubi, vol_id); | ||
1003 | |||
1004 | /* | ||
1005 | * We may race with volume deletion/re-size, so we have to hold | ||
1006 | * @ubi->volumes_lock. | ||
1007 | */ | ||
1008 | spin_lock(&ubi->volumes_lock); | ||
1009 | vol = ubi->volumes[idx]; | ||
1010 | if (!vol) { | ||
1011 | dbg_eba("volume %d was removed meanwhile", vol_id); | ||
1012 | spin_unlock(&ubi->volumes_lock); | ||
1013 | goto out_unlock; | ||
1014 | } | ||
1015 | |||
1016 | pnum = vol->eba_tbl[lnum]; | ||
1017 | if (pnum != from) { | ||
1018 | dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to " | ||
1019 | "PEB %d, cancel", vol_id, lnum, from, pnum); | ||
1020 | spin_unlock(&ubi->volumes_lock); | ||
1021 | goto out_unlock; | ||
1022 | } | ||
1023 | spin_unlock(&ubi->volumes_lock); | ||
1024 | |||
1025 | /* OK, now the LEB is locked and we can safely start moving it */ | ||
1026 | |||
1027 | dbg_eba("read %d bytes of data", aldata_size); | ||
1028 | err = ubi_io_read_data(ubi, buf, from, 0, aldata_size); | ||
1029 | if (err && err != UBI_IO_BITFLIPS) { | ||
1030 | ubi_warn("error %d while reading data from PEB %d", | ||
1031 | err, from); | ||
1032 | goto out_unlock; | ||
1033 | } | ||
1034 | |||
1035 | /* | ||
1036 | * Now we have got to calculate how much data we have to to copy. In | ||
1037 | * case of a static volume it is fairly easy - the VID header contains | ||
1038 | * the data size. In case of a dynamic volume it is more difficult - we | ||
1039 | * have to read the contents, cut 0xFF bytes from the end and copy only | ||
1040 | * the first part. We must do this to avoid writing 0xFF bytes as it | ||
1041 | * may have some side-effects. And not only this. It is important not | ||
1042 | * to include those 0xFFs to CRC because later the they may be filled | ||
1043 | * by data. | ||
1044 | */ | ||
1045 | if (vid_hdr->vol_type == UBI_VID_DYNAMIC) | ||
1046 | aldata_size = data_size = | ||
1047 | ubi_calc_data_len(ubi, buf, data_size); | ||
1048 | |||
1049 | cond_resched(); | ||
1050 | crc = crc32(UBI_CRC32_INIT, buf, data_size); | ||
1051 | cond_resched(); | ||
1052 | |||
1053 | /* | ||
1054 | * It may turn out to me that the whole @from physical eraseblock | ||
1055 | * contains only 0xFF bytes. Then we have to only write the VID header | ||
1056 | * and do not write any data. This also means we should not set | ||
1057 | * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc. | ||
1058 | */ | ||
1059 | if (data_size > 0) { | ||
1060 | vid_hdr->copy_flag = 1; | ||
1061 | vid_hdr->data_size = cpu_to_ubi32(data_size); | ||
1062 | vid_hdr->data_crc = cpu_to_ubi32(crc); | ||
1063 | } | ||
1064 | vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); | ||
1065 | |||
1066 | err = ubi_io_write_vid_hdr(ubi, to, vid_hdr); | ||
1067 | if (err) | ||
1068 | goto out_unlock; | ||
1069 | |||
1070 | cond_resched(); | ||
1071 | |||
1072 | /* Read the VID header back and check if it was written correctly */ | ||
1073 | err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1); | ||
1074 | if (err) { | ||
1075 | if (err != UBI_IO_BITFLIPS) | ||
1076 | ubi_warn("cannot read VID header back from PEB %d", to); | ||
1077 | goto out_unlock; | ||
1078 | } | ||
1079 | |||
1080 | if (data_size > 0) { | ||
1081 | err = ubi_io_write_data(ubi, buf, to, 0, aldata_size); | ||
1082 | if (err) | ||
1083 | goto out_unlock; | ||
1084 | |||
1085 | /* | ||
1086 | * We've written the data and are going to read it back to make | ||
1087 | * sure it was written correctly. | ||
1088 | */ | ||
1089 | buf1 = kmalloc(aldata_size, GFP_KERNEL); | ||
1090 | if (!buf1) { | ||
1091 | err = -ENOMEM; | ||
1092 | goto out_unlock; | ||
1093 | } | ||
1094 | |||
1095 | cond_resched(); | ||
1096 | |||
1097 | err = ubi_io_read_data(ubi, buf1, to, 0, aldata_size); | ||
1098 | if (err) { | ||
1099 | if (err != UBI_IO_BITFLIPS) | ||
1100 | ubi_warn("cannot read data back from PEB %d", | ||
1101 | to); | ||
1102 | goto out_unlock; | ||
1103 | } | ||
1104 | |||
1105 | cond_resched(); | ||
1106 | |||
1107 | if (memcmp(buf, buf1, aldata_size)) { | ||
1108 | ubi_warn("read data back from PEB %d - it is different", | ||
1109 | to); | ||
1110 | goto out_unlock; | ||
1111 | } | ||
1112 | } | ||
1113 | |||
1114 | ubi_assert(vol->eba_tbl[lnum] == from); | ||
1115 | vol->eba_tbl[lnum] = to; | ||
1116 | |||
1117 | leb_write_unlock(ubi, vol_id, lnum); | ||
1118 | kfree(buf); | ||
1119 | kfree(buf1); | ||
1120 | |||
1121 | return 0; | ||
1122 | |||
1123 | out_unlock: | ||
1124 | leb_write_unlock(ubi, vol_id, lnum); | ||
1125 | kfree(buf); | ||
1126 | kfree(buf1); | ||
1127 | return err; | ||
1128 | } | ||
1129 | |||
1130 | /** | ||
1131 | * ubi_eba_init_scan - initialize the EBA unit using scanning information. | ||
1132 | * @ubi: UBI device description object | ||
1133 | * @si: scanning information | ||
1134 | * | ||
1135 | * This function returns zero in case of success and a negative error code in | ||
1136 | * case of failure. | ||
1137 | */ | ||
1138 | int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) | ||
1139 | { | ||
1140 | int i, j, err, num_volumes; | ||
1141 | struct ubi_scan_volume *sv; | ||
1142 | struct ubi_volume *vol; | ||
1143 | struct ubi_scan_leb *seb; | ||
1144 | struct rb_node *rb; | ||
1145 | |||
1146 | dbg_eba("initialize EBA unit"); | ||
1147 | |||
1148 | spin_lock_init(&ubi->ltree_lock); | ||
1149 | ubi->ltree = RB_ROOT; | ||
1150 | |||
1151 | if (ubi_devices_cnt == 0) { | ||
1152 | ltree_slab = kmem_cache_create("ubi_ltree_slab", | ||
1153 | sizeof(struct ltree_entry), 0, | ||
1154 | 0, <ree_entry_ctor, NULL); | ||
1155 | if (!ltree_slab) | ||
1156 | return -ENOMEM; | ||
1157 | } | ||
1158 | |||
1159 | ubi->global_sqnum = si->max_sqnum + 1; | ||
1160 | num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; | ||
1161 | |||
1162 | for (i = 0; i < num_volumes; i++) { | ||
1163 | vol = ubi->volumes[i]; | ||
1164 | if (!vol) | ||
1165 | continue; | ||
1166 | |||
1167 | cond_resched(); | ||
1168 | |||
1169 | vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), | ||
1170 | GFP_KERNEL); | ||
1171 | if (!vol->eba_tbl) { | ||
1172 | err = -ENOMEM; | ||
1173 | goto out_free; | ||
1174 | } | ||
1175 | |||
1176 | for (j = 0; j < vol->reserved_pebs; j++) | ||
1177 | vol->eba_tbl[j] = UBI_LEB_UNMAPPED; | ||
1178 | |||
1179 | sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i)); | ||
1180 | if (!sv) | ||
1181 | continue; | ||
1182 | |||
1183 | ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { | ||
1184 | if (seb->lnum >= vol->reserved_pebs) | ||
1185 | /* | ||
1186 | * This may happen in case of an unclean reboot | ||
1187 | * during re-size. | ||
1188 | */ | ||
1189 | ubi_scan_move_to_list(sv, seb, &si->erase); | ||
1190 | vol->eba_tbl[seb->lnum] = seb->pnum; | ||
1191 | } | ||
1192 | } | ||
1193 | |||
1194 | if (ubi->bad_allowed) { | ||
1195 | ubi_calculate_reserved(ubi); | ||
1196 | |||
1197 | if (ubi->avail_pebs < ubi->beb_rsvd_level) { | ||
1198 | /* No enough free physical eraseblocks */ | ||
1199 | ubi->beb_rsvd_pebs = ubi->avail_pebs; | ||
1200 | ubi_warn("cannot reserve enough PEBs for bad PEB " | ||
1201 | "handling, reserved %d, need %d", | ||
1202 | ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); | ||
1203 | } else | ||
1204 | ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; | ||
1205 | |||
1206 | ubi->avail_pebs -= ubi->beb_rsvd_pebs; | ||
1207 | ubi->rsvd_pebs += ubi->beb_rsvd_pebs; | ||
1208 | } | ||
1209 | |||
1210 | dbg_eba("EBA unit is initialized"); | ||
1211 | return 0; | ||
1212 | |||
1213 | out_free: | ||
1214 | for (i = 0; i < num_volumes; i++) { | ||
1215 | if (!ubi->volumes[i]) | ||
1216 | continue; | ||
1217 | kfree(ubi->volumes[i]->eba_tbl); | ||
1218 | } | ||
1219 | if (ubi_devices_cnt == 0) | ||
1220 | kmem_cache_destroy(ltree_slab); | ||
1221 | return err; | ||
1222 | } | ||
1223 | |||
1224 | /** | ||
1225 | * ubi_eba_close - close EBA unit. | ||
1226 | * @ubi: UBI device description object | ||
1227 | */ | ||
1228 | void ubi_eba_close(const struct ubi_device *ubi) | ||
1229 | { | ||
1230 | int i, num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; | ||
1231 | |||
1232 | dbg_eba("close EBA unit"); | ||
1233 | |||
1234 | for (i = 0; i < num_volumes; i++) { | ||
1235 | if (!ubi->volumes[i]) | ||
1236 | continue; | ||
1237 | kfree(ubi->volumes[i]->eba_tbl); | ||
1238 | } | ||
1239 | if (ubi_devices_cnt == 1) | ||
1240 | kmem_cache_destroy(ltree_slab); | ||
1241 | } | ||
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 | */ | ||
42 | static 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 | */ | ||
84 | static 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 | */ | ||
106 | static 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 | */ | ||
158 | static 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 | */ | ||
213 | static 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 | |||
257 | out_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 | */ | ||
272 | int 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 | */ | ||
313 | int 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 | } | ||
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c new file mode 100644 index 000000000000..438914d05151 --- /dev/null +++ b/drivers/mtd/ubi/io.c | |||
@@ -0,0 +1,1259 @@ | |||
1 | /* | ||
2 | * Copyright (c) International Business Machines Corp., 2006 | ||
3 | * Copyright (c) Nokia Corporation, 2006, 2007 | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
13 | * the GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * Author: Artem Bityutskiy (Битюцкий Артём) | ||
20 | */ | ||
21 | |||
22 | /* | ||
23 | * UBI input/output unit. | ||
24 | * | ||
25 | * This unit provides a uniform way to work with all kinds of the underlying | ||
26 | * MTD devices. It also implements handy functions for reading and writing UBI | ||
27 | * headers. | ||
28 | * | ||
29 | * We are trying to have a paranoid mindset and not to trust to what we read | ||
30 | * from the flash media in order to be more secure and robust. So this unit | ||
31 | * validates every single header it reads from the flash media. | ||
32 | * | ||
33 | * Some words about how the eraseblock headers are stored. | ||
34 | * | ||
35 | * The erase counter header is always stored at offset zero. By default, the | ||
36 | * VID header is stored after the EC header at the closest aligned offset | ||
37 | * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID | ||
38 | * header at the closest aligned offset. But this default layout may be | ||
39 | * changed. For example, for different reasons (e.g., optimization) UBI may be | ||
40 | * asked to put the VID header at further offset, and even at an unaligned | ||
41 | * offset. Of course, if the offset of the VID header is unaligned, UBI adds | ||
42 | * proper padding in front of it. Data offset may also be changed but it has to | ||
43 | * be aligned. | ||
44 | * | ||
45 | * About minimal I/O units. In general, UBI assumes flash device model where | ||
46 | * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1, | ||
47 | * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the | ||
48 | * @ubi->mtd->writesize field. But as an exception, UBI admits of using another | ||
49 | * (smaller) minimal I/O unit size for EC and VID headers to make it possible | ||
50 | * to do different optimizations. | ||
51 | * | ||
52 | * This is extremely useful in case of NAND flashes which admit of several | ||
53 | * write operations to one NAND page. In this case UBI can fit EC and VID | ||
54 | * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal | ||
55 | * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still | ||
56 | * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI | ||
57 | * users. | ||
58 | * | ||
59 | * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so | ||
60 | * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID | ||
61 | * headers. | ||
62 | * | ||
63 | * Q: why not just to treat sub-page as a minimal I/O unit of this flash | ||
64 | * device, e.g., make @ubi->min_io_size = 512 in the example above? | ||
65 | * | ||
66 | * A: because when writing a sub-page, MTD still writes a full 2K page but the | ||
67 | * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing | ||
68 | * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we | ||
69 | * prefer to use sub-pages only for EV and VID headers. | ||
70 | * | ||
71 | * As it was noted above, the VID header may start at a non-aligned offset. | ||
72 | * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page, | ||
73 | * the VID header may reside at offset 1984 which is the last 64 bytes of the | ||
74 | * last sub-page (EC header is always at offset zero). This causes some | ||
75 | * difficulties when reading and writing VID headers. | ||
76 | * | ||
77 | * Suppose we have a 64-byte buffer and we read a VID header at it. We change | ||
78 | * the data and want to write this VID header out. As we can only write in | ||
79 | * 512-byte chunks, we have to allocate one more buffer and copy our VID header | ||
80 | * to offset 448 of this buffer. | ||
81 | * | ||
82 | * The I/O unit does the following trick in order to avoid this extra copy. | ||
83 | * It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID header | ||
84 | * and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. When the | ||
85 | * VID header is being written out, it shifts the VID header pointer back and | ||
86 | * writes the whole sub-page. | ||
87 | */ | ||
88 | |||
89 | #include <linux/crc32.h> | ||
90 | #include <linux/err.h> | ||
91 | #include "ubi.h" | ||
92 | |||
93 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
94 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); | ||
95 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); | ||
96 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
97 | const struct ubi_ec_hdr *ec_hdr); | ||
98 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); | ||
99 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
100 | const struct ubi_vid_hdr *vid_hdr); | ||
101 | static int paranoid_check_all_ff(const struct ubi_device *ubi, int pnum, | ||
102 | int offset, int len); | ||
103 | #else | ||
104 | #define paranoid_check_not_bad(ubi, pnum) 0 | ||
105 | #define paranoid_check_peb_ec_hdr(ubi, pnum) 0 | ||
106 | #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0 | ||
107 | #define paranoid_check_peb_vid_hdr(ubi, pnum) 0 | ||
108 | #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0 | ||
109 | #define paranoid_check_all_ff(ubi, pnum, offset, len) 0 | ||
110 | #endif | ||
111 | |||
112 | /** | ||
113 | * ubi_io_read - read data from a physical eraseblock. | ||
114 | * @ubi: UBI device description object | ||
115 | * @buf: buffer where to store the read data | ||
116 | * @pnum: physical eraseblock number to read from | ||
117 | * @offset: offset within the physical eraseblock from where to read | ||
118 | * @len: how many bytes to read | ||
119 | * | ||
120 | * This function reads data from offset @offset of physical eraseblock @pnum | ||
121 | * and stores the read data in the @buf buffer. The following return codes are | ||
122 | * possible: | ||
123 | * | ||
124 | * o %0 if all the requested data were successfully read; | ||
125 | * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but | ||
126 | * correctable bit-flips were detected; this is harmless but may indicate | ||
127 | * that this eraseblock may become bad soon (but do not have to); | ||
128 | * o %-EBADMSG if the MTD subsystem reported about data data integrity | ||
129 | * problems, for example it can me an ECC error in case of NAND; this most | ||
130 | * probably means that the data is corrupted; | ||
131 | * o %-EIO if some I/O error occurred; | ||
132 | * o other negative error codes in case of other errors. | ||
133 | */ | ||
134 | int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, | ||
135 | int len) | ||
136 | { | ||
137 | int err, retries = 0; | ||
138 | size_t read; | ||
139 | loff_t addr; | ||
140 | |||
141 | dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); | ||
142 | |||
143 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
144 | ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); | ||
145 | ubi_assert(len > 0); | ||
146 | |||
147 | err = paranoid_check_not_bad(ubi, pnum); | ||
148 | if (err) | ||
149 | return err > 0 ? -EINVAL : err; | ||
150 | |||
151 | addr = (loff_t)pnum * ubi->peb_size + offset; | ||
152 | retry: | ||
153 | err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); | ||
154 | if (err) { | ||
155 | if (err == -EUCLEAN) { | ||
156 | /* | ||
157 | * -EUCLEAN is reported if there was a bit-flip which | ||
158 | * was corrected, so this is harmless. | ||
159 | */ | ||
160 | ubi_msg("fixable bit-flip detected at PEB %d", pnum); | ||
161 | ubi_assert(len == read); | ||
162 | return UBI_IO_BITFLIPS; | ||
163 | } | ||
164 | |||
165 | if (read != len && retries++ < UBI_IO_RETRIES) { | ||
166 | dbg_io("error %d while reading %d bytes from PEB %d:%d, " | ||
167 | "read only %zd bytes, retry", | ||
168 | err, len, pnum, offset, read); | ||
169 | yield(); | ||
170 | goto retry; | ||
171 | } | ||
172 | |||
173 | ubi_err("error %d while reading %d bytes from PEB %d:%d, " | ||
174 | "read %zd bytes", err, len, pnum, offset, read); | ||
175 | ubi_dbg_dump_stack(); | ||
176 | } else { | ||
177 | ubi_assert(len == read); | ||
178 | |||
179 | if (ubi_dbg_is_bitflip()) { | ||
180 | dbg_msg("bit-flip (emulated)"); | ||
181 | err = UBI_IO_BITFLIPS; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | return err; | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * ubi_io_write - write data to a physical eraseblock. | ||
190 | * @ubi: UBI device description object | ||
191 | * @buf: buffer with the data to write | ||
192 | * @pnum: physical eraseblock number to write to | ||
193 | * @offset: offset within the physical eraseblock where to write | ||
194 | * @len: how many bytes to write | ||
195 | * | ||
196 | * This function writes @len bytes of data from buffer @buf to offset @offset | ||
197 | * of physical eraseblock @pnum. If all the data were successfully written, | ||
198 | * zero is returned. If an error occurred, this function returns a negative | ||
199 | * error code. If %-EIO is returned, the physical eraseblock most probably went | ||
200 | * bad. | ||
201 | * | ||
202 | * Note, in case of an error, it is possible that something was still written | ||
203 | * to the flash media, but may be some garbage. | ||
204 | */ | ||
205 | int ubi_io_write(const struct ubi_device *ubi, const void *buf, int pnum, | ||
206 | int offset, int len) | ||
207 | { | ||
208 | int err; | ||
209 | size_t written; | ||
210 | loff_t addr; | ||
211 | |||
212 | dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); | ||
213 | |||
214 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
215 | ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); | ||
216 | ubi_assert(offset % ubi->hdrs_min_io_size == 0); | ||
217 | ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); | ||
218 | |||
219 | if (ubi->ro_mode) { | ||
220 | ubi_err("read-only mode"); | ||
221 | return -EROFS; | ||
222 | } | ||
223 | |||
224 | /* The below has to be compiled out if paranoid checks are disabled */ | ||
225 | |||
226 | err = paranoid_check_not_bad(ubi, pnum); | ||
227 | if (err) | ||
228 | return err > 0 ? -EINVAL : err; | ||
229 | |||
230 | /* The area we are writing to has to contain all 0xFF bytes */ | ||
231 | err = paranoid_check_all_ff(ubi, pnum, offset, len); | ||
232 | if (err) | ||
233 | return err > 0 ? -EINVAL : err; | ||
234 | |||
235 | if (offset >= ubi->leb_start) { | ||
236 | /* | ||
237 | * We write to the data area of the physical eraseblock. Make | ||
238 | * sure it has valid EC and VID headers. | ||
239 | */ | ||
240 | err = paranoid_check_peb_ec_hdr(ubi, pnum); | ||
241 | if (err) | ||
242 | return err > 0 ? -EINVAL : err; | ||
243 | err = paranoid_check_peb_vid_hdr(ubi, pnum); | ||
244 | if (err) | ||
245 | return err > 0 ? -EINVAL : err; | ||
246 | } | ||
247 | |||
248 | if (ubi_dbg_is_write_failure()) { | ||
249 | dbg_err("cannot write %d bytes to PEB %d:%d " | ||
250 | "(emulated)", len, pnum, offset); | ||
251 | ubi_dbg_dump_stack(); | ||
252 | return -EIO; | ||
253 | } | ||
254 | |||
255 | addr = (loff_t)pnum * ubi->peb_size + offset; | ||
256 | err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); | ||
257 | if (err) { | ||
258 | ubi_err("error %d while writing %d bytes to PEB %d:%d, written" | ||
259 | " %zd bytes", err, len, pnum, offset, written); | ||
260 | ubi_dbg_dump_stack(); | ||
261 | } else | ||
262 | ubi_assert(written == len); | ||
263 | |||
264 | return err; | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * erase_callback - MTD erasure call-back. | ||
269 | * @ei: MTD erase information object. | ||
270 | * | ||
271 | * Note, even though MTD erase interface is asynchronous, all the current | ||
272 | * implementations are synchronous anyway. | ||
273 | */ | ||
274 | static void erase_callback(struct erase_info *ei) | ||
275 | { | ||
276 | wake_up_interruptible((wait_queue_head_t *)ei->priv); | ||
277 | } | ||
278 | |||
279 | /** | ||
280 | * do_sync_erase - synchronously erase a physical eraseblock. | ||
281 | * @ubi: UBI device description object | ||
282 | * @pnum: the physical eraseblock number to erase | ||
283 | * | ||
284 | * This function synchronously erases physical eraseblock @pnum and returns | ||
285 | * zero in case of success and a negative error code in case of failure. If | ||
286 | * %-EIO is returned, the physical eraseblock most probably went bad. | ||
287 | */ | ||
288 | static int do_sync_erase(const struct ubi_device *ubi, int pnum) | ||
289 | { | ||
290 | int err, retries = 0; | ||
291 | struct erase_info ei; | ||
292 | wait_queue_head_t wq; | ||
293 | |||
294 | dbg_io("erase PEB %d", pnum); | ||
295 | |||
296 | retry: | ||
297 | init_waitqueue_head(&wq); | ||
298 | memset(&ei, 0, sizeof(struct erase_info)); | ||
299 | |||
300 | ei.mtd = ubi->mtd; | ||
301 | ei.addr = pnum * ubi->peb_size; | ||
302 | ei.len = ubi->peb_size; | ||
303 | ei.callback = erase_callback; | ||
304 | ei.priv = (unsigned long)&wq; | ||
305 | |||
306 | err = ubi->mtd->erase(ubi->mtd, &ei); | ||
307 | if (err) { | ||
308 | if (retries++ < UBI_IO_RETRIES) { | ||
309 | dbg_io("error %d while erasing PEB %d, retry", | ||
310 | err, pnum); | ||
311 | yield(); | ||
312 | goto retry; | ||
313 | } | ||
314 | ubi_err("cannot erase PEB %d, error %d", pnum, err); | ||
315 | ubi_dbg_dump_stack(); | ||
316 | return err; | ||
317 | } | ||
318 | |||
319 | err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || | ||
320 | ei.state == MTD_ERASE_FAILED); | ||
321 | if (err) { | ||
322 | ubi_err("interrupted PEB %d erasure", pnum); | ||
323 | return -EINTR; | ||
324 | } | ||
325 | |||
326 | if (ei.state == MTD_ERASE_FAILED) { | ||
327 | if (retries++ < UBI_IO_RETRIES) { | ||
328 | dbg_io("error while erasing PEB %d, retry", pnum); | ||
329 | yield(); | ||
330 | goto retry; | ||
331 | } | ||
332 | ubi_err("cannot erase PEB %d", pnum); | ||
333 | ubi_dbg_dump_stack(); | ||
334 | return -EIO; | ||
335 | } | ||
336 | |||
337 | err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size); | ||
338 | if (err) | ||
339 | return err > 0 ? -EINVAL : err; | ||
340 | |||
341 | if (ubi_dbg_is_erase_failure() && !err) { | ||
342 | dbg_err("cannot erase PEB %d (emulated)", pnum); | ||
343 | return -EIO; | ||
344 | } | ||
345 | |||
346 | return 0; | ||
347 | } | ||
348 | |||
349 | /** | ||
350 | * check_pattern - check if buffer contains only a certain byte pattern. | ||
351 | * @buf: buffer to check | ||
352 | * @patt: the pattern to check | ||
353 | * @size: buffer size in bytes | ||
354 | * | ||
355 | * This function returns %1 in there are only @patt bytes in @buf, and %0 if | ||
356 | * something else was also found. | ||
357 | */ | ||
358 | static int check_pattern(const void *buf, uint8_t patt, int size) | ||
359 | { | ||
360 | int i; | ||
361 | |||
362 | for (i = 0; i < size; i++) | ||
363 | if (((const uint8_t *)buf)[i] != patt) | ||
364 | return 0; | ||
365 | return 1; | ||
366 | } | ||
367 | |||
368 | /* Patterns to write to a physical eraseblock when torturing it */ | ||
369 | static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; | ||
370 | |||
371 | /** | ||
372 | * torture_peb - test a supposedly bad physical eraseblock. | ||
373 | * @ubi: UBI device description object | ||
374 | * @pnum: the physical eraseblock number to test | ||
375 | * | ||
376 | * This function returns %-EIO if the physical eraseblock did not pass the | ||
377 | * test, a positive number of erase operations done if the test was | ||
378 | * successfully passed, and other negative error codes in case of other errors. | ||
379 | */ | ||
380 | static int torture_peb(const struct ubi_device *ubi, int pnum) | ||
381 | { | ||
382 | void *buf; | ||
383 | int err, i, patt_count; | ||
384 | |||
385 | buf = kmalloc(ubi->peb_size, GFP_KERNEL); | ||
386 | if (!buf) | ||
387 | return -ENOMEM; | ||
388 | |||
389 | patt_count = ARRAY_SIZE(patterns); | ||
390 | ubi_assert(patt_count > 0); | ||
391 | |||
392 | for (i = 0; i < patt_count; i++) { | ||
393 | err = do_sync_erase(ubi, pnum); | ||
394 | if (err) | ||
395 | goto out; | ||
396 | |||
397 | /* Make sure the PEB contains only 0xFF bytes */ | ||
398 | err = ubi_io_read(ubi, buf, pnum, 0, ubi->peb_size); | ||
399 | if (err) | ||
400 | goto out; | ||
401 | |||
402 | err = check_pattern(buf, 0xFF, ubi->peb_size); | ||
403 | if (err == 0) { | ||
404 | ubi_err("erased PEB %d, but a non-0xFF byte found", | ||
405 | pnum); | ||
406 | err = -EIO; | ||
407 | goto out; | ||
408 | } | ||
409 | |||
410 | /* Write a pattern and check it */ | ||
411 | memset(buf, patterns[i], ubi->peb_size); | ||
412 | err = ubi_io_write(ubi, buf, pnum, 0, ubi->peb_size); | ||
413 | if (err) | ||
414 | goto out; | ||
415 | |||
416 | memset(buf, ~patterns[i], ubi->peb_size); | ||
417 | err = ubi_io_read(ubi, buf, pnum, 0, ubi->peb_size); | ||
418 | if (err) | ||
419 | goto out; | ||
420 | |||
421 | err = check_pattern(buf, patterns[i], ubi->peb_size); | ||
422 | if (err == 0) { | ||
423 | ubi_err("pattern %x checking failed for PEB %d", | ||
424 | patterns[i], pnum); | ||
425 | err = -EIO; | ||
426 | goto out; | ||
427 | } | ||
428 | } | ||
429 | |||
430 | err = patt_count; | ||
431 | |||
432 | out: | ||
433 | if (err == UBI_IO_BITFLIPS || err == -EBADMSG) | ||
434 | /* | ||
435 | * If a bit-flip or data integrity error was detected, the test | ||
436 | * has not passed because it happened on a freshly erased | ||
437 | * physical eraseblock which means something is wrong with it. | ||
438 | */ | ||
439 | err = -EIO; | ||
440 | kfree(buf); | ||
441 | return err; | ||
442 | } | ||
443 | |||
444 | /** | ||
445 | * ubi_io_sync_erase - synchronously erase a physical eraseblock. | ||
446 | * @ubi: UBI device description object | ||
447 | * @pnum: physical eraseblock number to erase | ||
448 | * @torture: if this physical eraseblock has to be tortured | ||
449 | * | ||
450 | * This function synchronously erases physical eraseblock @pnum. If @torture | ||
451 | * flag is not zero, the physical eraseblock is checked by means of writing | ||
452 | * different patterns to it and reading them back. If the torturing is enabled, | ||
453 | * the physical eraseblock is erased more then once. | ||
454 | * | ||
455 | * This function returns the number of erasures made in case of success, %-EIO | ||
456 | * if the erasure failed or the torturing test failed, and other negative error | ||
457 | * codes in case of other errors. Note, %-EIO means that the physical | ||
458 | * eraseblock is bad. | ||
459 | */ | ||
460 | int ubi_io_sync_erase(const struct ubi_device *ubi, int pnum, int torture) | ||
461 | { | ||
462 | int err, ret = 0; | ||
463 | |||
464 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
465 | |||
466 | err = paranoid_check_not_bad(ubi, pnum); | ||
467 | if (err != 0) | ||
468 | return err > 0 ? -EINVAL : err; | ||
469 | |||
470 | if (ubi->ro_mode) { | ||
471 | ubi_err("read-only mode"); | ||
472 | return -EROFS; | ||
473 | } | ||
474 | |||
475 | if (torture) { | ||
476 | ret = torture_peb(ubi, pnum); | ||
477 | if (ret < 0) | ||
478 | return ret; | ||
479 | } | ||
480 | |||
481 | err = do_sync_erase(ubi, pnum); | ||
482 | if (err) | ||
483 | return err; | ||
484 | |||
485 | return ret + 1; | ||
486 | } | ||
487 | |||
488 | /** | ||
489 | * ubi_io_is_bad - check if a physical eraseblock is bad. | ||
490 | * @ubi: UBI device description object | ||
491 | * @pnum: the physical eraseblock number to check | ||
492 | * | ||
493 | * This function returns a positive number if the physical eraseblock is bad, | ||
494 | * zero if not, and a negative error code if an error occurred. | ||
495 | */ | ||
496 | int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) | ||
497 | { | ||
498 | struct mtd_info *mtd = ubi->mtd; | ||
499 | |||
500 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
501 | |||
502 | if (ubi->bad_allowed) { | ||
503 | int ret; | ||
504 | |||
505 | ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); | ||
506 | if (ret < 0) | ||
507 | ubi_err("error %d while checking if PEB %d is bad", | ||
508 | ret, pnum); | ||
509 | else if (ret) | ||
510 | dbg_io("PEB %d is bad", pnum); | ||
511 | return ret; | ||
512 | } | ||
513 | |||
514 | return 0; | ||
515 | } | ||
516 | |||
517 | /** | ||
518 | * ubi_io_mark_bad - mark a physical eraseblock as bad. | ||
519 | * @ubi: UBI device description object | ||
520 | * @pnum: the physical eraseblock number to mark | ||
521 | * | ||
522 | * This function returns zero in case of success and a negative error code in | ||
523 | * case of failure. | ||
524 | */ | ||
525 | int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) | ||
526 | { | ||
527 | int err; | ||
528 | struct mtd_info *mtd = ubi->mtd; | ||
529 | |||
530 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
531 | |||
532 | if (ubi->ro_mode) { | ||
533 | ubi_err("read-only mode"); | ||
534 | return -EROFS; | ||
535 | } | ||
536 | |||
537 | if (!ubi->bad_allowed) | ||
538 | return 0; | ||
539 | |||
540 | err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); | ||
541 | if (err) | ||
542 | ubi_err("cannot mark PEB %d bad, error %d", pnum, err); | ||
543 | return err; | ||
544 | } | ||
545 | |||
546 | /** | ||
547 | * validate_ec_hdr - validate an erase counter header. | ||
548 | * @ubi: UBI device description object | ||
549 | * @ec_hdr: the erase counter header to check | ||
550 | * | ||
551 | * This function returns zero if the erase counter header is OK, and %1 if | ||
552 | * not. | ||
553 | */ | ||
554 | static int validate_ec_hdr(const struct ubi_device *ubi, | ||
555 | const struct ubi_ec_hdr *ec_hdr) | ||
556 | { | ||
557 | long long ec; | ||
558 | int vid_hdr_offset, leb_start; | ||
559 | |||
560 | ec = ubi64_to_cpu(ec_hdr->ec); | ||
561 | vid_hdr_offset = ubi32_to_cpu(ec_hdr->vid_hdr_offset); | ||
562 | leb_start = ubi32_to_cpu(ec_hdr->data_offset); | ||
563 | |||
564 | if (ec_hdr->version != UBI_VERSION) { | ||
565 | ubi_err("node with incompatible UBI version found: " | ||
566 | "this UBI version is %d, image version is %d", | ||
567 | UBI_VERSION, (int)ec_hdr->version); | ||
568 | goto bad; | ||
569 | } | ||
570 | |||
571 | if (vid_hdr_offset != ubi->vid_hdr_offset) { | ||
572 | ubi_err("bad VID header offset %d, expected %d", | ||
573 | vid_hdr_offset, ubi->vid_hdr_offset); | ||
574 | goto bad; | ||
575 | } | ||
576 | |||
577 | if (leb_start != ubi->leb_start) { | ||
578 | ubi_err("bad data offset %d, expected %d", | ||
579 | leb_start, ubi->leb_start); | ||
580 | goto bad; | ||
581 | } | ||
582 | |||
583 | if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { | ||
584 | ubi_err("bad erase counter %lld", ec); | ||
585 | goto bad; | ||
586 | } | ||
587 | |||
588 | return 0; | ||
589 | |||
590 | bad: | ||
591 | ubi_err("bad EC header"); | ||
592 | ubi_dbg_dump_ec_hdr(ec_hdr); | ||
593 | ubi_dbg_dump_stack(); | ||
594 | return 1; | ||
595 | } | ||
596 | |||
597 | /** | ||
598 | * ubi_io_read_ec_hdr - read and check an erase counter header. | ||
599 | * @ubi: UBI device description object | ||
600 | * @pnum: physical eraseblock to read from | ||
601 | * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter | ||
602 | * header | ||
603 | * @verbose: be verbose if the header is corrupted or was not found | ||
604 | * | ||
605 | * This function reads erase counter header from physical eraseblock @pnum and | ||
606 | * stores it in @ec_hdr. This function also checks CRC checksum of the read | ||
607 | * erase counter header. The following codes may be returned: | ||
608 | * | ||
609 | * o %0 if the CRC checksum is correct and the header was successfully read; | ||
610 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected | ||
611 | * and corrected by the flash driver; this is harmless but may indicate that | ||
612 | * this eraseblock may become bad soon (but may be not); | ||
613 | * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); | ||
614 | * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; | ||
615 | * o a negative error code in case of failure. | ||
616 | */ | ||
617 | int ubi_io_read_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
618 | struct ubi_ec_hdr *ec_hdr, int verbose) | ||
619 | { | ||
620 | int err, read_err = 0; | ||
621 | uint32_t crc, magic, hdr_crc; | ||
622 | |||
623 | dbg_io("read EC header from PEB %d", pnum); | ||
624 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
625 | |||
626 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); | ||
627 | if (err) { | ||
628 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) | ||
629 | return err; | ||
630 | |||
631 | /* | ||
632 | * We read all the data, but either a correctable bit-flip | ||
633 | * occurred, or MTD reported about some data integrity error, | ||
634 | * like an ECC error in case of NAND. The former is harmless, | ||
635 | * the later may mean that the read data is corrupted. But we | ||
636 | * have a CRC check-sum and we will detect this. If the EC | ||
637 | * header is still OK, we just report this as there was a | ||
638 | * bit-flip. | ||
639 | */ | ||
640 | read_err = err; | ||
641 | } | ||
642 | |||
643 | magic = ubi32_to_cpu(ec_hdr->magic); | ||
644 | if (magic != UBI_EC_HDR_MAGIC) { | ||
645 | /* | ||
646 | * The magic field is wrong. Let's check if we have read all | ||
647 | * 0xFF. If yes, this physical eraseblock is assumed to be | ||
648 | * empty. | ||
649 | * | ||
650 | * But if there was a read error, we do not test it for all | ||
651 | * 0xFFs. Even if it does contain all 0xFFs, this error | ||
652 | * indicates that something is still wrong with this physical | ||
653 | * eraseblock and we anyway cannot treat it as empty. | ||
654 | */ | ||
655 | if (read_err != -EBADMSG && | ||
656 | check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { | ||
657 | /* The physical eraseblock is supposedly empty */ | ||
658 | |||
659 | /* | ||
660 | * The below is just a paranoid check, it has to be | ||
661 | * compiled out if paranoid checks are disabled. | ||
662 | */ | ||
663 | err = paranoid_check_all_ff(ubi, pnum, 0, | ||
664 | ubi->peb_size); | ||
665 | if (err) | ||
666 | return err > 0 ? UBI_IO_BAD_EC_HDR : err; | ||
667 | |||
668 | if (verbose) | ||
669 | ubi_warn("no EC header found at PEB %d, " | ||
670 | "only 0xFF bytes", pnum); | ||
671 | return UBI_IO_PEB_EMPTY; | ||
672 | } | ||
673 | |||
674 | /* | ||
675 | * This is not a valid erase counter header, and these are not | ||
676 | * 0xFF bytes. Report that the header is corrupted. | ||
677 | */ | ||
678 | if (verbose) { | ||
679 | ubi_warn("bad magic number at PEB %d: %08x instead of " | ||
680 | "%08x", pnum, magic, UBI_EC_HDR_MAGIC); | ||
681 | ubi_dbg_dump_ec_hdr(ec_hdr); | ||
682 | } | ||
683 | return UBI_IO_BAD_EC_HDR; | ||
684 | } | ||
685 | |||
686 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); | ||
687 | hdr_crc = ubi32_to_cpu(ec_hdr->hdr_crc); | ||
688 | |||
689 | if (hdr_crc != crc) { | ||
690 | if (verbose) { | ||
691 | ubi_warn("bad EC header CRC at PEB %d, calculated %#08x," | ||
692 | " read %#08x", pnum, crc, hdr_crc); | ||
693 | ubi_dbg_dump_ec_hdr(ec_hdr); | ||
694 | } | ||
695 | return UBI_IO_BAD_EC_HDR; | ||
696 | } | ||
697 | |||
698 | /* And of course validate what has just been read from the media */ | ||
699 | err = validate_ec_hdr(ubi, ec_hdr); | ||
700 | if (err) { | ||
701 | ubi_err("validation failed for PEB %d", pnum); | ||
702 | return -EINVAL; | ||
703 | } | ||
704 | |||
705 | return read_err ? UBI_IO_BITFLIPS : 0; | ||
706 | } | ||
707 | |||
708 | /** | ||
709 | * ubi_io_write_ec_hdr - write an erase counter header. | ||
710 | * @ubi: UBI device description object | ||
711 | * @pnum: physical eraseblock to write to | ||
712 | * @ec_hdr: the erase counter header to write | ||
713 | * | ||
714 | * This function writes erase counter header described by @ec_hdr to physical | ||
715 | * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so | ||
716 | * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec | ||
717 | * field. | ||
718 | * | ||
719 | * This function returns zero in case of success and a negative error code in | ||
720 | * case of failure. If %-EIO is returned, the physical eraseblock most probably | ||
721 | * went bad. | ||
722 | */ | ||
723 | int ubi_io_write_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
724 | struct ubi_ec_hdr *ec_hdr) | ||
725 | { | ||
726 | int err; | ||
727 | uint32_t crc; | ||
728 | |||
729 | dbg_io("write EC header to PEB %d", pnum); | ||
730 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
731 | |||
732 | ec_hdr->magic = cpu_to_ubi32(UBI_EC_HDR_MAGIC); | ||
733 | ec_hdr->version = UBI_VERSION; | ||
734 | ec_hdr->vid_hdr_offset = cpu_to_ubi32(ubi->vid_hdr_offset); | ||
735 | ec_hdr->data_offset = cpu_to_ubi32(ubi->leb_start); | ||
736 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); | ||
737 | ec_hdr->hdr_crc = cpu_to_ubi32(crc); | ||
738 | |||
739 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); | ||
740 | if (err) | ||
741 | return -EINVAL; | ||
742 | |||
743 | err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); | ||
744 | return err; | ||
745 | } | ||
746 | |||
747 | /** | ||
748 | * validate_vid_hdr - validate a volume identifier header. | ||
749 | * @ubi: UBI device description object | ||
750 | * @vid_hdr: the volume identifier header to check | ||
751 | * | ||
752 | * This function checks that data stored in the volume identifier header | ||
753 | * @vid_hdr. Returns zero if the VID header is OK and %1 if not. | ||
754 | */ | ||
755 | static int validate_vid_hdr(const struct ubi_device *ubi, | ||
756 | const struct ubi_vid_hdr *vid_hdr) | ||
757 | { | ||
758 | int vol_type = vid_hdr->vol_type; | ||
759 | int copy_flag = vid_hdr->copy_flag; | ||
760 | int vol_id = ubi32_to_cpu(vid_hdr->vol_id); | ||
761 | int lnum = ubi32_to_cpu(vid_hdr->lnum); | ||
762 | int compat = vid_hdr->compat; | ||
763 | int data_size = ubi32_to_cpu(vid_hdr->data_size); | ||
764 | int used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); | ||
765 | int data_pad = ubi32_to_cpu(vid_hdr->data_pad); | ||
766 | int data_crc = ubi32_to_cpu(vid_hdr->data_crc); | ||
767 | int usable_leb_size = ubi->leb_size - data_pad; | ||
768 | |||
769 | if (copy_flag != 0 && copy_flag != 1) { | ||
770 | dbg_err("bad copy_flag"); | ||
771 | goto bad; | ||
772 | } | ||
773 | |||
774 | if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || | ||
775 | data_pad < 0) { | ||
776 | dbg_err("negative values"); | ||
777 | goto bad; | ||
778 | } | ||
779 | |||
780 | if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { | ||
781 | dbg_err("bad vol_id"); | ||
782 | goto bad; | ||
783 | } | ||
784 | |||
785 | if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { | ||
786 | dbg_err("bad compat"); | ||
787 | goto bad; | ||
788 | } | ||
789 | |||
790 | if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && | ||
791 | compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && | ||
792 | compat != UBI_COMPAT_REJECT) { | ||
793 | dbg_err("bad compat"); | ||
794 | goto bad; | ||
795 | } | ||
796 | |||
797 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { | ||
798 | dbg_err("bad vol_type"); | ||
799 | goto bad; | ||
800 | } | ||
801 | |||
802 | if (data_pad >= ubi->leb_size / 2) { | ||
803 | dbg_err("bad data_pad"); | ||
804 | goto bad; | ||
805 | } | ||
806 | |||
807 | if (vol_type == UBI_VID_STATIC) { | ||
808 | /* | ||
809 | * Although from high-level point of view static volumes may | ||
810 | * contain zero bytes of data, but no VID headers can contain | ||
811 | * zero at these fields, because they empty volumes do not have | ||
812 | * mapped logical eraseblocks. | ||
813 | */ | ||
814 | if (used_ebs == 0) { | ||
815 | dbg_err("zero used_ebs"); | ||
816 | goto bad; | ||
817 | } | ||
818 | if (data_size == 0) { | ||
819 | dbg_err("zero data_size"); | ||
820 | goto bad; | ||
821 | } | ||
822 | if (lnum < used_ebs - 1) { | ||
823 | if (data_size != usable_leb_size) { | ||
824 | dbg_err("bad data_size"); | ||
825 | goto bad; | ||
826 | } | ||
827 | } else if (lnum == used_ebs - 1) { | ||
828 | if (data_size == 0) { | ||
829 | dbg_err("bad data_size at last LEB"); | ||
830 | goto bad; | ||
831 | } | ||
832 | } else { | ||
833 | dbg_err("too high lnum"); | ||
834 | goto bad; | ||
835 | } | ||
836 | } else { | ||
837 | if (copy_flag == 0) { | ||
838 | if (data_crc != 0) { | ||
839 | dbg_err("non-zero data CRC"); | ||
840 | goto bad; | ||
841 | } | ||
842 | if (data_size != 0) { | ||
843 | dbg_err("non-zero data_size"); | ||
844 | goto bad; | ||
845 | } | ||
846 | } else { | ||
847 | if (data_size == 0) { | ||
848 | dbg_err("zero data_size of copy"); | ||
849 | goto bad; | ||
850 | } | ||
851 | } | ||
852 | if (used_ebs != 0) { | ||
853 | dbg_err("bad used_ebs"); | ||
854 | goto bad; | ||
855 | } | ||
856 | } | ||
857 | |||
858 | return 0; | ||
859 | |||
860 | bad: | ||
861 | ubi_err("bad VID header"); | ||
862 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
863 | ubi_dbg_dump_stack(); | ||
864 | return 1; | ||
865 | } | ||
866 | |||
867 | /** | ||
868 | * ubi_io_read_vid_hdr - read and check a volume identifier header. | ||
869 | * @ubi: UBI device description object | ||
870 | * @pnum: physical eraseblock number to read from | ||
871 | * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume | ||
872 | * identifier header | ||
873 | * @verbose: be verbose if the header is corrupted or wasn't found | ||
874 | * | ||
875 | * This function reads the volume identifier header from physical eraseblock | ||
876 | * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read | ||
877 | * volume identifier header. The following codes may be returned: | ||
878 | * | ||
879 | * o %0 if the CRC checksum is correct and the header was successfully read; | ||
880 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected | ||
881 | * and corrected by the flash driver; this is harmless but may indicate that | ||
882 | * this eraseblock may become bad soon; | ||
883 | * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC | ||
884 | * error detected); | ||
885 | * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID | ||
886 | * header there); | ||
887 | * o a negative error code in case of failure. | ||
888 | */ | ||
889 | int ubi_io_read_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
890 | struct ubi_vid_hdr *vid_hdr, int verbose) | ||
891 | { | ||
892 | int err, read_err = 0; | ||
893 | uint32_t crc, magic, hdr_crc; | ||
894 | void *p; | ||
895 | |||
896 | dbg_io("read VID header from PEB %d", pnum); | ||
897 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
898 | |||
899 | p = (char *)vid_hdr - ubi->vid_hdr_shift; | ||
900 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, | ||
901 | ubi->vid_hdr_alsize); | ||
902 | if (err) { | ||
903 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) | ||
904 | return err; | ||
905 | |||
906 | /* | ||
907 | * We read all the data, but either a correctable bit-flip | ||
908 | * occurred, or MTD reported about some data integrity error, | ||
909 | * like an ECC error in case of NAND. The former is harmless, | ||
910 | * the later may mean the read data is corrupted. But we have a | ||
911 | * CRC check-sum and we will identify this. If the VID header is | ||
912 | * still OK, we just report this as there was a bit-flip. | ||
913 | */ | ||
914 | read_err = err; | ||
915 | } | ||
916 | |||
917 | magic = ubi32_to_cpu(vid_hdr->magic); | ||
918 | if (magic != UBI_VID_HDR_MAGIC) { | ||
919 | /* | ||
920 | * If we have read all 0xFF bytes, the VID header probably does | ||
921 | * not exist and the physical eraseblock is assumed to be free. | ||
922 | * | ||
923 | * But if there was a read error, we do not test the data for | ||
924 | * 0xFFs. Even if it does contain all 0xFFs, this error | ||
925 | * indicates that something is still wrong with this physical | ||
926 | * eraseblock and it cannot be regarded as free. | ||
927 | */ | ||
928 | if (read_err != -EBADMSG && | ||
929 | check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { | ||
930 | /* The physical eraseblock is supposedly free */ | ||
931 | |||
932 | /* | ||
933 | * The below is just a paranoid check, it has to be | ||
934 | * compiled out if paranoid checks are disabled. | ||
935 | */ | ||
936 | err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start, | ||
937 | ubi->leb_size); | ||
938 | if (err) | ||
939 | return err > 0 ? UBI_IO_BAD_VID_HDR : err; | ||
940 | |||
941 | if (verbose) | ||
942 | ubi_warn("no VID header found at PEB %d, " | ||
943 | "only 0xFF bytes", pnum); | ||
944 | return UBI_IO_PEB_FREE; | ||
945 | } | ||
946 | |||
947 | /* | ||
948 | * This is not a valid VID header, and these are not 0xFF | ||
949 | * bytes. Report that the header is corrupted. | ||
950 | */ | ||
951 | if (verbose) { | ||
952 | ubi_warn("bad magic number at PEB %d: %08x instead of " | ||
953 | "%08x", pnum, magic, UBI_VID_HDR_MAGIC); | ||
954 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
955 | } | ||
956 | return UBI_IO_BAD_VID_HDR; | ||
957 | } | ||
958 | |||
959 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); | ||
960 | hdr_crc = ubi32_to_cpu(vid_hdr->hdr_crc); | ||
961 | |||
962 | if (hdr_crc != crc) { | ||
963 | if (verbose) { | ||
964 | ubi_warn("bad CRC at PEB %d, calculated %#08x, " | ||
965 | "read %#08x", pnum, crc, hdr_crc); | ||
966 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
967 | } | ||
968 | return UBI_IO_BAD_VID_HDR; | ||
969 | } | ||
970 | |||
971 | /* Validate the VID header that we have just read */ | ||
972 | err = validate_vid_hdr(ubi, vid_hdr); | ||
973 | if (err) { | ||
974 | ubi_err("validation failed for PEB %d", pnum); | ||
975 | return -EINVAL; | ||
976 | } | ||
977 | |||
978 | return read_err ? UBI_IO_BITFLIPS : 0; | ||
979 | } | ||
980 | |||
981 | /** | ||
982 | * ubi_io_write_vid_hdr - write a volume identifier header. | ||
983 | * @ubi: UBI device description object | ||
984 | * @pnum: the physical eraseblock number to write to | ||
985 | * @vid_hdr: the volume identifier header to write | ||
986 | * | ||
987 | * This function writes the volume identifier header described by @vid_hdr to | ||
988 | * physical eraseblock @pnum. This function automatically fills the | ||
989 | * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates | ||
990 | * header CRC checksum and stores it at vid_hdr->hdr_crc. | ||
991 | * | ||
992 | * This function returns zero in case of success and a negative error code in | ||
993 | * case of failure. If %-EIO is returned, the physical eraseblock probably went | ||
994 | * bad. | ||
995 | */ | ||
996 | int ubi_io_write_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
997 | struct ubi_vid_hdr *vid_hdr) | ||
998 | { | ||
999 | int err; | ||
1000 | uint32_t crc; | ||
1001 | void *p; | ||
1002 | |||
1003 | dbg_io("write VID header to PEB %d", pnum); | ||
1004 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | ||
1005 | |||
1006 | err = paranoid_check_peb_ec_hdr(ubi, pnum); | ||
1007 | if (err) | ||
1008 | return err > 0 ? -EINVAL: err; | ||
1009 | |||
1010 | vid_hdr->magic = cpu_to_ubi32(UBI_VID_HDR_MAGIC); | ||
1011 | vid_hdr->version = UBI_VERSION; | ||
1012 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); | ||
1013 | vid_hdr->hdr_crc = cpu_to_ubi32(crc); | ||
1014 | |||
1015 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); | ||
1016 | if (err) | ||
1017 | return -EINVAL; | ||
1018 | |||
1019 | p = (char *)vid_hdr - ubi->vid_hdr_shift; | ||
1020 | err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, | ||
1021 | ubi->vid_hdr_alsize); | ||
1022 | return err; | ||
1023 | } | ||
1024 | |||
1025 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
1026 | |||
1027 | /** | ||
1028 | * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. | ||
1029 | * @ubi: UBI device description object | ||
1030 | * @pnum: physical eraseblock number to check | ||
1031 | * | ||
1032 | * This function returns zero if the physical eraseblock is good, a positive | ||
1033 | * number if it is bad and a negative error code if an error occurred. | ||
1034 | */ | ||
1035 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) | ||
1036 | { | ||
1037 | int err; | ||
1038 | |||
1039 | err = ubi_io_is_bad(ubi, pnum); | ||
1040 | if (!err) | ||
1041 | return err; | ||
1042 | |||
1043 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1044 | ubi_dbg_dump_stack(); | ||
1045 | return err; | ||
1046 | } | ||
1047 | |||
1048 | /** | ||
1049 | * paranoid_check_ec_hdr - check if an erase counter header is all right. | ||
1050 | * @ubi: UBI device description object | ||
1051 | * @pnum: physical eraseblock number the erase counter header belongs to | ||
1052 | * @ec_hdr: the erase counter header to check | ||
1053 | * | ||
1054 | * This function returns zero if the erase counter header contains valid | ||
1055 | * values, and %1 if not. | ||
1056 | */ | ||
1057 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
1058 | const struct ubi_ec_hdr *ec_hdr) | ||
1059 | { | ||
1060 | int err; | ||
1061 | uint32_t magic; | ||
1062 | |||
1063 | magic = ubi32_to_cpu(ec_hdr->magic); | ||
1064 | if (magic != UBI_EC_HDR_MAGIC) { | ||
1065 | ubi_err("bad magic %#08x, must be %#08x", | ||
1066 | magic, UBI_EC_HDR_MAGIC); | ||
1067 | goto fail; | ||
1068 | } | ||
1069 | |||
1070 | err = validate_ec_hdr(ubi, ec_hdr); | ||
1071 | if (err) { | ||
1072 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1073 | goto fail; | ||
1074 | } | ||
1075 | |||
1076 | return 0; | ||
1077 | |||
1078 | fail: | ||
1079 | ubi_dbg_dump_ec_hdr(ec_hdr); | ||
1080 | ubi_dbg_dump_stack(); | ||
1081 | return 1; | ||
1082 | } | ||
1083 | |||
1084 | /** | ||
1085 | * paranoid_check_peb_ec_hdr - check that the erase counter header of a | ||
1086 | * physical eraseblock is in-place and is all right. | ||
1087 | * @ubi: UBI device description object | ||
1088 | * @pnum: the physical eraseblock number to check | ||
1089 | * | ||
1090 | * This function returns zero if the erase counter header is all right, %1 if | ||
1091 | * not, and a negative error code if an error occurred. | ||
1092 | */ | ||
1093 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) | ||
1094 | { | ||
1095 | int err; | ||
1096 | uint32_t crc, hdr_crc; | ||
1097 | struct ubi_ec_hdr *ec_hdr; | ||
1098 | |||
1099 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | ||
1100 | if (!ec_hdr) | ||
1101 | return -ENOMEM; | ||
1102 | |||
1103 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); | ||
1104 | if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) | ||
1105 | goto exit; | ||
1106 | |||
1107 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); | ||
1108 | hdr_crc = ubi32_to_cpu(ec_hdr->hdr_crc); | ||
1109 | if (hdr_crc != crc) { | ||
1110 | ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); | ||
1111 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1112 | ubi_dbg_dump_ec_hdr(ec_hdr); | ||
1113 | ubi_dbg_dump_stack(); | ||
1114 | err = 1; | ||
1115 | goto exit; | ||
1116 | } | ||
1117 | |||
1118 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); | ||
1119 | |||
1120 | exit: | ||
1121 | kfree(ec_hdr); | ||
1122 | return err; | ||
1123 | } | ||
1124 | |||
1125 | /** | ||
1126 | * paranoid_check_vid_hdr - check that a volume identifier header is all right. | ||
1127 | * @ubi: UBI device description object | ||
1128 | * @pnum: physical eraseblock number the volume identifier header belongs to | ||
1129 | * @vid_hdr: the volume identifier header to check | ||
1130 | * | ||
1131 | * This function returns zero if the volume identifier header is all right, and | ||
1132 | * %1 if not. | ||
1133 | */ | ||
1134 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
1135 | const struct ubi_vid_hdr *vid_hdr) | ||
1136 | { | ||
1137 | int err; | ||
1138 | uint32_t magic; | ||
1139 | |||
1140 | magic = ubi32_to_cpu(vid_hdr->magic); | ||
1141 | if (magic != UBI_VID_HDR_MAGIC) { | ||
1142 | ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", | ||
1143 | magic, pnum, UBI_VID_HDR_MAGIC); | ||
1144 | goto fail; | ||
1145 | } | ||
1146 | |||
1147 | err = validate_vid_hdr(ubi, vid_hdr); | ||
1148 | if (err) { | ||
1149 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1150 | goto fail; | ||
1151 | } | ||
1152 | |||
1153 | return err; | ||
1154 | |||
1155 | fail: | ||
1156 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1157 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
1158 | ubi_dbg_dump_stack(); | ||
1159 | return 1; | ||
1160 | |||
1161 | } | ||
1162 | |||
1163 | /** | ||
1164 | * paranoid_check_peb_vid_hdr - check that the volume identifier header of a | ||
1165 | * physical eraseblock is in-place and is all right. | ||
1166 | * @ubi: UBI device description object | ||
1167 | * @pnum: the physical eraseblock number to check | ||
1168 | * | ||
1169 | * This function returns zero if the volume identifier header is all right, | ||
1170 | * %1 if not, and a negative error code if an error occurred. | ||
1171 | */ | ||
1172 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) | ||
1173 | { | ||
1174 | int err; | ||
1175 | uint32_t crc, hdr_crc; | ||
1176 | struct ubi_vid_hdr *vid_hdr; | ||
1177 | void *p; | ||
1178 | |||
1179 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
1180 | if (!vid_hdr) | ||
1181 | return -ENOMEM; | ||
1182 | |||
1183 | p = (char *)vid_hdr - ubi->vid_hdr_shift; | ||
1184 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, | ||
1185 | ubi->vid_hdr_alsize); | ||
1186 | if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) | ||
1187 | goto exit; | ||
1188 | |||
1189 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); | ||
1190 | hdr_crc = ubi32_to_cpu(vid_hdr->hdr_crc); | ||
1191 | if (hdr_crc != crc) { | ||
1192 | ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " | ||
1193 | "read %#08x", pnum, crc, hdr_crc); | ||
1194 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1195 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
1196 | ubi_dbg_dump_stack(); | ||
1197 | err = 1; | ||
1198 | goto exit; | ||
1199 | } | ||
1200 | |||
1201 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); | ||
1202 | |||
1203 | exit: | ||
1204 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
1205 | return err; | ||
1206 | } | ||
1207 | |||
1208 | /** | ||
1209 | * paranoid_check_all_ff - check that a region of flash is empty. | ||
1210 | * @ubi: UBI device description object | ||
1211 | * @pnum: the physical eraseblock number to check | ||
1212 | * @offset: the starting offset within the physical eraseblock to check | ||
1213 | * @len: the length of the region to check | ||
1214 | * | ||
1215 | * This function returns zero if only 0xFF bytes are present at offset | ||
1216 | * @offset of the physical eraseblock @pnum, %1 if not, and a negative error | ||
1217 | * code if an error occurred. | ||
1218 | */ | ||
1219 | static int paranoid_check_all_ff(const struct ubi_device *ubi, int pnum, | ||
1220 | int offset, int len) | ||
1221 | { | ||
1222 | size_t read; | ||
1223 | int err; | ||
1224 | void *buf; | ||
1225 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; | ||
1226 | |||
1227 | buf = kzalloc(len, GFP_KERNEL); | ||
1228 | if (!buf) | ||
1229 | return -ENOMEM; | ||
1230 | |||
1231 | err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); | ||
1232 | if (err && err != -EUCLEAN) { | ||
1233 | ubi_err("error %d while reading %d bytes from PEB %d:%d, " | ||
1234 | "read %zd bytes", err, len, pnum, offset, read); | ||
1235 | goto error; | ||
1236 | } | ||
1237 | |||
1238 | err = check_pattern(buf, 0xFF, len); | ||
1239 | if (err == 0) { | ||
1240 | ubi_err("flash region at PEB %d:%d, length %d does not " | ||
1241 | "contain all 0xFF bytes", pnum, offset, len); | ||
1242 | goto fail; | ||
1243 | } | ||
1244 | |||
1245 | kfree(buf); | ||
1246 | return 0; | ||
1247 | |||
1248 | fail: | ||
1249 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1250 | dbg_msg("hex dump of the %d-%d region", offset, offset + len); | ||
1251 | ubi_dbg_hexdump(buf, len); | ||
1252 | err = 1; | ||
1253 | error: | ||
1254 | ubi_dbg_dump_stack(); | ||
1255 | kfree(buf); | ||
1256 | return err; | ||
1257 | } | ||
1258 | |||
1259 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ | ||
diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c new file mode 100644 index 000000000000..d352c4575c3d --- /dev/null +++ b/drivers/mtd/ubi/kapi.c | |||
@@ -0,0 +1,575 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* This file mostly implements UBI kernel API functions */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/err.h> | ||
25 | #include <asm/div64.h> | ||
26 | #include "ubi.h" | ||
27 | |||
28 | /** | ||
29 | * ubi_get_device_info - get information about UBI device. | ||
30 | * @ubi_num: UBI device number | ||
31 | * @di: the information is stored here | ||
32 | * | ||
33 | * This function returns %0 in case of success and a %-ENODEV if there is no | ||
34 | * such UBI device. | ||
35 | */ | ||
36 | int ubi_get_device_info(int ubi_num, struct ubi_device_info *di) | ||
37 | { | ||
38 | const struct ubi_device *ubi; | ||
39 | |||
40 | if (!try_module_get(THIS_MODULE)) | ||
41 | return -ENODEV; | ||
42 | |||
43 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || | ||
44 | !ubi_devices[ubi_num]) { | ||
45 | module_put(THIS_MODULE); | ||
46 | return -ENODEV; | ||
47 | } | ||
48 | |||
49 | ubi = ubi_devices[ubi_num]; | ||
50 | di->ubi_num = ubi->ubi_num; | ||
51 | di->leb_size = ubi->leb_size; | ||
52 | di->min_io_size = ubi->min_io_size; | ||
53 | di->ro_mode = ubi->ro_mode; | ||
54 | di->cdev = MKDEV(ubi->major, 0); | ||
55 | module_put(THIS_MODULE); | ||
56 | return 0; | ||
57 | } | ||
58 | EXPORT_SYMBOL_GPL(ubi_get_device_info); | ||
59 | |||
60 | /** | ||
61 | * ubi_get_volume_info - get information about UBI volume. | ||
62 | * @desc: volume descriptor | ||
63 | * @vi: the information is stored here | ||
64 | */ | ||
65 | void ubi_get_volume_info(struct ubi_volume_desc *desc, | ||
66 | struct ubi_volume_info *vi) | ||
67 | { | ||
68 | const struct ubi_volume *vol = desc->vol; | ||
69 | const struct ubi_device *ubi = vol->ubi; | ||
70 | |||
71 | vi->vol_id = vol->vol_id; | ||
72 | vi->ubi_num = ubi->ubi_num; | ||
73 | vi->size = vol->reserved_pebs; | ||
74 | vi->used_bytes = vol->used_bytes; | ||
75 | vi->vol_type = vol->vol_type; | ||
76 | vi->corrupted = vol->corrupted; | ||
77 | vi->upd_marker = vol->upd_marker; | ||
78 | vi->alignment = vol->alignment; | ||
79 | vi->usable_leb_size = vol->usable_leb_size; | ||
80 | vi->name_len = vol->name_len; | ||
81 | vi->name = vol->name; | ||
82 | vi->cdev = MKDEV(ubi->major, vi->vol_id + 1); | ||
83 | } | ||
84 | EXPORT_SYMBOL_GPL(ubi_get_volume_info); | ||
85 | |||
86 | /** | ||
87 | * ubi_open_volume - open UBI volume. | ||
88 | * @ubi_num: UBI device number | ||
89 | * @vol_id: volume ID | ||
90 | * @mode: open mode | ||
91 | * | ||
92 | * The @mode parameter specifies if the volume should be opened in read-only | ||
93 | * mode, read-write mode, or exclusive mode. The exclusive mode guarantees that | ||
94 | * nobody else will be able to open this volume. UBI allows to have many volume | ||
95 | * readers and one writer at a time. | ||
96 | * | ||
97 | * If a static volume is being opened for the first time since boot, it will be | ||
98 | * checked by this function, which means it will be fully read and the CRC | ||
99 | * checksum of each logical eraseblock will be checked. | ||
100 | * | ||
101 | * This function returns volume descriptor in case of success and a negative | ||
102 | * error code in case of failure. | ||
103 | */ | ||
104 | struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode) | ||
105 | { | ||
106 | int err; | ||
107 | struct ubi_volume_desc *desc; | ||
108 | struct ubi_device *ubi = ubi_devices[ubi_num]; | ||
109 | struct ubi_volume *vol; | ||
110 | |||
111 | dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode); | ||
112 | |||
113 | err = -ENODEV; | ||
114 | if (!try_module_get(THIS_MODULE)) | ||
115 | return ERR_PTR(err); | ||
116 | |||
117 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi) | ||
118 | goto out_put; | ||
119 | |||
120 | err = -EINVAL; | ||
121 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
122 | goto out_put; | ||
123 | if (mode != UBI_READONLY && mode != UBI_READWRITE && | ||
124 | mode != UBI_EXCLUSIVE) | ||
125 | goto out_put; | ||
126 | |||
127 | desc = kmalloc(sizeof(struct ubi_volume_desc), GFP_KERNEL); | ||
128 | if (!desc) { | ||
129 | err = -ENOMEM; | ||
130 | goto out_put; | ||
131 | } | ||
132 | |||
133 | spin_lock(&ubi->volumes_lock); | ||
134 | vol = ubi->volumes[vol_id]; | ||
135 | if (!vol) { | ||
136 | err = -ENODEV; | ||
137 | goto out_unlock; | ||
138 | } | ||
139 | |||
140 | err = -EBUSY; | ||
141 | switch (mode) { | ||
142 | case UBI_READONLY: | ||
143 | if (vol->exclusive) | ||
144 | goto out_unlock; | ||
145 | vol->readers += 1; | ||
146 | break; | ||
147 | |||
148 | case UBI_READWRITE: | ||
149 | if (vol->exclusive || vol->writers > 0) | ||
150 | goto out_unlock; | ||
151 | vol->writers += 1; | ||
152 | break; | ||
153 | |||
154 | case UBI_EXCLUSIVE: | ||
155 | if (vol->exclusive || vol->writers || vol->readers) | ||
156 | goto out_unlock; | ||
157 | vol->exclusive = 1; | ||
158 | break; | ||
159 | } | ||
160 | spin_unlock(&ubi->volumes_lock); | ||
161 | |||
162 | desc->vol = vol; | ||
163 | desc->mode = mode; | ||
164 | |||
165 | /* | ||
166 | * To prevent simultaneous checks of the same volume we use @vtbl_mutex, | ||
167 | * although it is not the purpose it was introduced for. | ||
168 | */ | ||
169 | mutex_lock(&ubi->vtbl_mutex); | ||
170 | if (!vol->checked) { | ||
171 | /* This is the first open - check the volume */ | ||
172 | err = ubi_check_volume(ubi, vol_id); | ||
173 | if (err < 0) { | ||
174 | mutex_unlock(&ubi->vtbl_mutex); | ||
175 | ubi_close_volume(desc); | ||
176 | return ERR_PTR(err); | ||
177 | } | ||
178 | if (err == 1) { | ||
179 | ubi_warn("volume %d on UBI device %d is corrupted", | ||
180 | vol_id, ubi->ubi_num); | ||
181 | vol->corrupted = 1; | ||
182 | } | ||
183 | vol->checked = 1; | ||
184 | } | ||
185 | mutex_unlock(&ubi->vtbl_mutex); | ||
186 | return desc; | ||
187 | |||
188 | out_unlock: | ||
189 | spin_unlock(&ubi->volumes_lock); | ||
190 | kfree(desc); | ||
191 | out_put: | ||
192 | module_put(THIS_MODULE); | ||
193 | return ERR_PTR(err); | ||
194 | } | ||
195 | EXPORT_SYMBOL_GPL(ubi_open_volume); | ||
196 | |||
197 | /** | ||
198 | * ubi_open_volume_nm - open UBI volume by name. | ||
199 | * @ubi_num: UBI device number | ||
200 | * @name: volume name | ||
201 | * @mode: open mode | ||
202 | * | ||
203 | * This function is similar to 'ubi_open_volume()', but opens a volume by name. | ||
204 | */ | ||
205 | struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, | ||
206 | int mode) | ||
207 | { | ||
208 | int i, vol_id = -1, len; | ||
209 | struct ubi_volume_desc *ret; | ||
210 | struct ubi_device *ubi; | ||
211 | |||
212 | dbg_msg("open volume %s, mode %d", name, mode); | ||
213 | |||
214 | if (!name) | ||
215 | return ERR_PTR(-EINVAL); | ||
216 | |||
217 | len = strnlen(name, UBI_VOL_NAME_MAX + 1); | ||
218 | if (len > UBI_VOL_NAME_MAX) | ||
219 | return ERR_PTR(-EINVAL); | ||
220 | |||
221 | ret = ERR_PTR(-ENODEV); | ||
222 | if (!try_module_get(THIS_MODULE)) | ||
223 | return ret; | ||
224 | |||
225 | if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi_devices[ubi_num]) | ||
226 | goto out_put; | ||
227 | |||
228 | ubi = ubi_devices[ubi_num]; | ||
229 | |||
230 | spin_lock(&ubi->volumes_lock); | ||
231 | /* Walk all volumes of this UBI device */ | ||
232 | for (i = 0; i < ubi->vtbl_slots; i++) { | ||
233 | struct ubi_volume *vol = ubi->volumes[i]; | ||
234 | |||
235 | if (vol && len == vol->name_len && !strcmp(name, vol->name)) { | ||
236 | vol_id = i; | ||
237 | break; | ||
238 | } | ||
239 | } | ||
240 | spin_unlock(&ubi->volumes_lock); | ||
241 | |||
242 | if (vol_id < 0) | ||
243 | goto out_put; | ||
244 | |||
245 | ret = ubi_open_volume(ubi_num, vol_id, mode); | ||
246 | |||
247 | out_put: | ||
248 | module_put(THIS_MODULE); | ||
249 | return ret; | ||
250 | } | ||
251 | EXPORT_SYMBOL_GPL(ubi_open_volume_nm); | ||
252 | |||
253 | /** | ||
254 | * ubi_close_volume - close UBI volume. | ||
255 | * @desc: volume descriptor | ||
256 | */ | ||
257 | void ubi_close_volume(struct ubi_volume_desc *desc) | ||
258 | { | ||
259 | struct ubi_volume *vol = desc->vol; | ||
260 | |||
261 | dbg_msg("close volume %d, mode %d", vol->vol_id, desc->mode); | ||
262 | |||
263 | spin_lock(&vol->ubi->volumes_lock); | ||
264 | switch (desc->mode) { | ||
265 | case UBI_READONLY: | ||
266 | vol->readers -= 1; | ||
267 | break; | ||
268 | case UBI_READWRITE: | ||
269 | vol->writers -= 1; | ||
270 | break; | ||
271 | case UBI_EXCLUSIVE: | ||
272 | vol->exclusive = 0; | ||
273 | } | ||
274 | spin_unlock(&vol->ubi->volumes_lock); | ||
275 | |||
276 | kfree(desc); | ||
277 | module_put(THIS_MODULE); | ||
278 | } | ||
279 | EXPORT_SYMBOL_GPL(ubi_close_volume); | ||
280 | |||
281 | /** | ||
282 | * ubi_leb_read - read data. | ||
283 | * @desc: volume descriptor | ||
284 | * @lnum: logical eraseblock number to read from | ||
285 | * @buf: buffer where to store the read data | ||
286 | * @offset: offset within the logical eraseblock to read from | ||
287 | * @len: how many bytes to read | ||
288 | * @check: whether UBI has to check the read data's CRC or not. | ||
289 | * | ||
290 | * This function reads data from offset @offset of logical eraseblock @lnum and | ||
291 | * stores the data at @buf. When reading from static volumes, @check specifies | ||
292 | * whether the data has to be checked or not. If yes, the whole logical | ||
293 | * eraseblock will be read and its CRC checksum will be checked (i.e., the CRC | ||
294 | * checksum is per-eraseblock). So checking may substantially slow down the | ||
295 | * read speed. The @check argument is ignored for dynamic volumes. | ||
296 | * | ||
297 | * In case of success, this function returns zero. In case of failure, this | ||
298 | * function returns a negative error code. | ||
299 | * | ||
300 | * %-EBADMSG error code is returned: | ||
301 | * o for both static and dynamic volumes if MTD driver has detected a data | ||
302 | * integrity problem (unrecoverable ECC checksum mismatch in case of NAND); | ||
303 | * o for static volumes in case of data CRC mismatch. | ||
304 | * | ||
305 | * If the volume is damaged because of an interrupted update this function just | ||
306 | * returns immediately with %-EBADF error code. | ||
307 | */ | ||
308 | int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, | ||
309 | int len, int check) | ||
310 | { | ||
311 | struct ubi_volume *vol = desc->vol; | ||
312 | struct ubi_device *ubi = vol->ubi; | ||
313 | int err, vol_id = vol->vol_id; | ||
314 | |||
315 | dbg_msg("read %d bytes from LEB %d:%d:%d", len, vol_id, lnum, offset); | ||
316 | |||
317 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots || lnum < 0 || | ||
318 | lnum >= vol->used_ebs || offset < 0 || len < 0 || | ||
319 | offset + len > vol->usable_leb_size) | ||
320 | return -EINVAL; | ||
321 | |||
322 | if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 && | ||
323 | offset + len > vol->last_eb_bytes) | ||
324 | return -EINVAL; | ||
325 | |||
326 | if (vol->upd_marker) | ||
327 | return -EBADF; | ||
328 | if (len == 0) | ||
329 | return 0; | ||
330 | |||
331 | err = ubi_eba_read_leb(ubi, vol_id, lnum, buf, offset, len, check); | ||
332 | if (err && err == -EBADMSG && vol->vol_type == UBI_STATIC_VOLUME) { | ||
333 | ubi_warn("mark volume %d as corrupted", vol_id); | ||
334 | vol->corrupted = 1; | ||
335 | } | ||
336 | |||
337 | return err; | ||
338 | } | ||
339 | EXPORT_SYMBOL_GPL(ubi_leb_read); | ||
340 | |||
341 | /** | ||
342 | * ubi_leb_write - write data. | ||
343 | * @desc: volume descriptor | ||
344 | * @lnum: logical eraseblock number to write to | ||
345 | * @buf: data to write | ||
346 | * @offset: offset within the logical eraseblock where to write | ||
347 | * @len: how many bytes to write | ||
348 | * @dtype: expected data type | ||
349 | * | ||
350 | * This function writes @len bytes of data from @buf to offset @offset of | ||
351 | * logical eraseblock @lnum. The @dtype argument describes expected lifetime of | ||
352 | * the data. | ||
353 | * | ||
354 | * This function takes care of physical eraseblock write failures. If write to | ||
355 | * the physical eraseblock write operation fails, the logical eraseblock is | ||
356 | * re-mapped to another physical eraseblock, the data is recovered, and the | ||
357 | * write finishes. UBI has a pool of reserved physical eraseblocks for this. | ||
358 | * | ||
359 | * If all the data were successfully written, zero is returned. If an error | ||
360 | * occurred and UBI has not been able to recover from it, this function returns | ||
361 | * a negative error code. Note, in case of an error, it is possible that | ||
362 | * something was still written to the flash media, but that may be some | ||
363 | * garbage. | ||
364 | * | ||
365 | * If the volume is damaged because of an interrupted update this function just | ||
366 | * returns immediately with %-EBADF code. | ||
367 | */ | ||
368 | int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
369 | int offset, int len, int dtype) | ||
370 | { | ||
371 | struct ubi_volume *vol = desc->vol; | ||
372 | struct ubi_device *ubi = vol->ubi; | ||
373 | int vol_id = vol->vol_id; | ||
374 | |||
375 | dbg_msg("write %d bytes to LEB %d:%d:%d", len, vol_id, lnum, offset); | ||
376 | |||
377 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
378 | return -EINVAL; | ||
379 | |||
380 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
381 | return -EROFS; | ||
382 | |||
383 | if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 || | ||
384 | offset + len > vol->usable_leb_size || offset % ubi->min_io_size || | ||
385 | len % ubi->min_io_size) | ||
386 | return -EINVAL; | ||
387 | |||
388 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | ||
389 | dtype != UBI_UNKNOWN) | ||
390 | return -EINVAL; | ||
391 | |||
392 | if (vol->upd_marker) | ||
393 | return -EBADF; | ||
394 | |||
395 | if (len == 0) | ||
396 | return 0; | ||
397 | |||
398 | return ubi_eba_write_leb(ubi, vol_id, lnum, buf, offset, len, dtype); | ||
399 | } | ||
400 | EXPORT_SYMBOL_GPL(ubi_leb_write); | ||
401 | |||
402 | /* | ||
403 | * ubi_leb_change - change logical eraseblock atomically. | ||
404 | * @desc: volume descriptor | ||
405 | * @lnum: logical eraseblock number to change | ||
406 | * @buf: data to write | ||
407 | * @len: how many bytes to write | ||
408 | * @dtype: expected data type | ||
409 | * | ||
410 | * This function changes the contents of a logical eraseblock atomically. @buf | ||
411 | * has to contain new logical eraseblock data, and @len - the length of the | ||
412 | * data, which has to be aligned. The length may be shorter then the logical | ||
413 | * eraseblock size, ant the logical eraseblock may be appended to more times | ||
414 | * later on. This function guarantees that in case of an unclean reboot the old | ||
415 | * contents is preserved. Returns zero in case of success and a negative error | ||
416 | * code in case of failure. | ||
417 | */ | ||
418 | int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
419 | int len, int dtype) | ||
420 | { | ||
421 | struct ubi_volume *vol = desc->vol; | ||
422 | struct ubi_device *ubi = vol->ubi; | ||
423 | int vol_id = vol->vol_id; | ||
424 | |||
425 | dbg_msg("atomically write %d bytes to LEB %d:%d", len, vol_id, lnum); | ||
426 | |||
427 | if (vol_id < 0 || vol_id >= ubi->vtbl_slots) | ||
428 | return -EINVAL; | ||
429 | |||
430 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
431 | return -EROFS; | ||
432 | |||
433 | if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 || | ||
434 | len > vol->usable_leb_size || len % ubi->min_io_size) | ||
435 | return -EINVAL; | ||
436 | |||
437 | if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM && | ||
438 | dtype != UBI_UNKNOWN) | ||
439 | return -EINVAL; | ||
440 | |||
441 | if (vol->upd_marker) | ||
442 | return -EBADF; | ||
443 | |||
444 | if (len == 0) | ||
445 | return 0; | ||
446 | |||
447 | return ubi_eba_atomic_leb_change(ubi, vol_id, lnum, buf, len, dtype); | ||
448 | } | ||
449 | EXPORT_SYMBOL_GPL(ubi_leb_change); | ||
450 | |||
451 | /** | ||
452 | * ubi_leb_erase - erase logical eraseblock. | ||
453 | * @desc: volume descriptor | ||
454 | * @lnum: logical eraseblock number | ||
455 | * | ||
456 | * This function un-maps logical eraseblock @lnum and synchronously erases the | ||
457 | * correspondent physical eraseblock. Returns zero in case of success and a | ||
458 | * negative error code in case of failure. | ||
459 | * | ||
460 | * If the volume is damaged because of an interrupted update this function just | ||
461 | * returns immediately with %-EBADF code. | ||
462 | */ | ||
463 | int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum) | ||
464 | { | ||
465 | struct ubi_volume *vol = desc->vol; | ||
466 | struct ubi_device *ubi = vol->ubi; | ||
467 | int err, vol_id = vol->vol_id; | ||
468 | |||
469 | dbg_msg("erase LEB %d:%d", vol_id, lnum); | ||
470 | |||
471 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
472 | return -EROFS; | ||
473 | |||
474 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
475 | return -EINVAL; | ||
476 | |||
477 | if (vol->upd_marker) | ||
478 | return -EBADF; | ||
479 | |||
480 | err = ubi_eba_unmap_leb(ubi, vol_id, lnum); | ||
481 | if (err) | ||
482 | return err; | ||
483 | |||
484 | return ubi_wl_flush(ubi); | ||
485 | } | ||
486 | EXPORT_SYMBOL_GPL(ubi_leb_erase); | ||
487 | |||
488 | /** | ||
489 | * ubi_leb_unmap - un-map logical eraseblock. | ||
490 | * @desc: volume descriptor | ||
491 | * @lnum: logical eraseblock number | ||
492 | * | ||
493 | * This function un-maps logical eraseblock @lnum and schedules the | ||
494 | * corresponding physical eraseblock for erasure, so that it will eventually be | ||
495 | * physically erased in background. This operation is much faster then the | ||
496 | * erase operation. | ||
497 | * | ||
498 | * Unlike erase, the un-map operation does not guarantee that the logical | ||
499 | * eraseblock will contain all 0xFF bytes when UBI is initialized again. For | ||
500 | * example, if several logical eraseblocks are un-mapped, and an unclean reboot | ||
501 | * happens after this, the logical eraseblocks will not necessarily be | ||
502 | * un-mapped again when this MTD device is attached. They may actually be | ||
503 | * mapped to the same physical eraseblocks again. So, this function has to be | ||
504 | * used with care. | ||
505 | * | ||
506 | * In other words, when un-mapping a logical eraseblock, UBI does not store | ||
507 | * any information about this on the flash media, it just marks the logical | ||
508 | * eraseblock as "un-mapped" in RAM. If UBI is detached before the physical | ||
509 | * eraseblock is physically erased, it will be mapped again to the same logical | ||
510 | * eraseblock when the MTD device is attached again. | ||
511 | * | ||
512 | * The main and obvious use-case of this function is when the contents of a | ||
513 | * logical eraseblock has to be re-written. Then it is much more efficient to | ||
514 | * first un-map it, then write new data, rather then first erase it, then write | ||
515 | * new data. Note, once new data has been written to the logical eraseblock, | ||
516 | * UBI guarantees that the old contents has gone forever. In other words, if an | ||
517 | * unclean reboot happens after the logical eraseblock has been un-mapped and | ||
518 | * then written to, it will contain the last written data. | ||
519 | * | ||
520 | * This function returns zero in case of success and a negative error code in | ||
521 | * case of failure. If the volume is damaged because of an interrupted update | ||
522 | * this function just returns immediately with %-EBADF code. | ||
523 | */ | ||
524 | int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum) | ||
525 | { | ||
526 | struct ubi_volume *vol = desc->vol; | ||
527 | struct ubi_device *ubi = vol->ubi; | ||
528 | int vol_id = vol->vol_id; | ||
529 | |||
530 | dbg_msg("unmap LEB %d:%d", vol_id, lnum); | ||
531 | |||
532 | if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME) | ||
533 | return -EROFS; | ||
534 | |||
535 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
536 | return -EINVAL; | ||
537 | |||
538 | if (vol->upd_marker) | ||
539 | return -EBADF; | ||
540 | |||
541 | return ubi_eba_unmap_leb(ubi, vol_id, lnum); | ||
542 | } | ||
543 | EXPORT_SYMBOL_GPL(ubi_leb_unmap); | ||
544 | |||
545 | /** | ||
546 | * ubi_is_mapped - check if logical eraseblock is mapped. | ||
547 | * @desc: volume descriptor | ||
548 | * @lnum: logical eraseblock number | ||
549 | * | ||
550 | * This function checks if logical eraseblock @lnum is mapped to a physical | ||
551 | * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily | ||
552 | * mean it will still be un-mapped after the UBI device is re-attached. The | ||
553 | * logical eraseblock may become mapped to the physical eraseblock it was last | ||
554 | * mapped to. | ||
555 | * | ||
556 | * This function returns %1 if the LEB is mapped, %0 if not, and a negative | ||
557 | * error code in case of failure. If the volume is damaged because of an | ||
558 | * interrupted update this function just returns immediately with %-EBADF error | ||
559 | * code. | ||
560 | */ | ||
561 | int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum) | ||
562 | { | ||
563 | struct ubi_volume *vol = desc->vol; | ||
564 | |||
565 | dbg_msg("test LEB %d:%d", vol->vol_id, lnum); | ||
566 | |||
567 | if (lnum < 0 || lnum >= vol->reserved_pebs) | ||
568 | return -EINVAL; | ||
569 | |||
570 | if (vol->upd_marker) | ||
571 | return -EBADF; | ||
572 | |||
573 | return vol->eba_tbl[lnum] >= 0; | ||
574 | } | ||
575 | EXPORT_SYMBOL_GPL(ubi_is_mapped); | ||
diff --git a/drivers/mtd/ubi/misc.c b/drivers/mtd/ubi/misc.c new file mode 100644 index 000000000000..38d4e6757dc7 --- /dev/null +++ b/drivers/mtd/ubi/misc.c | |||
@@ -0,0 +1,105 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* Here we keep miscellaneous functions which are used all over the UBI code */ | ||
22 | |||
23 | #include "ubi.h" | ||
24 | |||
25 | /** | ||
26 | * calc_data_len - calculate how much real data is stored in a buffer. | ||
27 | * @ubi: UBI device description object | ||
28 | * @buf: a buffer with the contents of the physical eraseblock | ||
29 | * @length: the buffer length | ||
30 | * | ||
31 | * This function calculates how much "real data" is stored in @buf and returnes | ||
32 | * the length. Continuous 0xFF bytes at the end of the buffer are not | ||
33 | * considered as "real data". | ||
34 | */ | ||
35 | int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, | ||
36 | int length) | ||
37 | { | ||
38 | int i; | ||
39 | |||
40 | ubi_assert(length % ubi->min_io_size == 0); | ||
41 | |||
42 | for (i = length - 1; i >= 0; i--) | ||
43 | if (((const uint8_t *)buf)[i] != 0xFF) | ||
44 | break; | ||
45 | |||
46 | /* The resulting length must be aligned to the minimum flash I/O size */ | ||
47 | length = ALIGN(i + 1, ubi->min_io_size); | ||
48 | return length; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * ubi_check_volume - check the contents of a static volume. | ||
53 | * @ubi: UBI device description object | ||
54 | * @vol_id: ID of the volume to check | ||
55 | * | ||
56 | * This function checks if static volume @vol_id is corrupted by fully reading | ||
57 | * it and checking data CRC. This function returns %0 if the volume is not | ||
58 | * corrupted, %1 if it is corrupted and a negative error code in case of | ||
59 | * failure. Dynamic volumes are not checked and zero is returned immediately. | ||
60 | */ | ||
61 | int ubi_check_volume(struct ubi_device *ubi, int vol_id) | ||
62 | { | ||
63 | void *buf; | ||
64 | int err = 0, i; | ||
65 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
66 | |||
67 | if (vol->vol_type != UBI_STATIC_VOLUME) | ||
68 | return 0; | ||
69 | |||
70 | buf = kmalloc(vol->usable_leb_size, GFP_KERNEL); | ||
71 | if (!buf) | ||
72 | return -ENOMEM; | ||
73 | |||
74 | for (i = 0; i < vol->used_ebs; i++) { | ||
75 | int size; | ||
76 | |||
77 | if (i == vol->used_ebs - 1) | ||
78 | size = vol->last_eb_bytes; | ||
79 | else | ||
80 | size = vol->usable_leb_size; | ||
81 | |||
82 | err = ubi_eba_read_leb(ubi, vol_id, i, buf, 0, size, 1); | ||
83 | if (err) { | ||
84 | if (err == -EBADMSG) | ||
85 | err = 1; | ||
86 | break; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | kfree(buf); | ||
91 | return err; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * ubi_calculate_rsvd_pool - calculate how many PEBs must be reserved for bad | ||
96 | * eraseblock handling. | ||
97 | * @ubi: UBI device description object | ||
98 | */ | ||
99 | void ubi_calculate_reserved(struct ubi_device *ubi) | ||
100 | { | ||
101 | ubi->beb_rsvd_level = ubi->good_peb_count/100; | ||
102 | ubi->beb_rsvd_level *= CONFIG_MTD_UBI_BEB_RESERVE; | ||
103 | if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS) | ||
104 | ubi->beb_rsvd_level = MIN_RESEVED_PEBS; | ||
105 | } | ||
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c new file mode 100644 index 000000000000..473f3200b868 --- /dev/null +++ b/drivers/mtd/ubi/scan.c | |||
@@ -0,0 +1,1368 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * UBI scanning unit. | ||
23 | * | ||
24 | * This unit is responsible for scanning the flash media, checking UBI | ||
25 | * headers and providing complete information about the UBI flash image. | ||
26 | * | ||
27 | * The scanning information is reoresented by a &struct ubi_scan_info' object. | ||
28 | * Information about found volumes is represented by &struct ubi_scan_volume | ||
29 | * objects which are kept in volume RB-tree with root at the @volumes field. | ||
30 | * The RB-tree is indexed by the volume ID. | ||
31 | * | ||
32 | * Found logical eraseblocks are represented by &struct ubi_scan_leb objects. | ||
33 | * These objects are kept in per-volume RB-trees with the root at the | ||
34 | * corresponding &struct ubi_scan_volume object. To put it differently, we keep | ||
35 | * an RB-tree of per-volume objects and each of these objects is the root of | ||
36 | * RB-tree of per-eraseblock objects. | ||
37 | * | ||
38 | * Corrupted physical eraseblocks are put to the @corr list, free physical | ||
39 | * eraseblocks are put to the @free list and the physical eraseblock to be | ||
40 | * erased are put to the @erase list. | ||
41 | */ | ||
42 | |||
43 | #include <linux/err.h> | ||
44 | #include <linux/crc32.h> | ||
45 | #include "ubi.h" | ||
46 | |||
47 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
48 | static int paranoid_check_si(const struct ubi_device *ubi, | ||
49 | struct ubi_scan_info *si); | ||
50 | #else | ||
51 | #define paranoid_check_si(ubi, si) 0 | ||
52 | #endif | ||
53 | |||
54 | /* Temporary variables used during scanning */ | ||
55 | static struct ubi_ec_hdr *ech; | ||
56 | static struct ubi_vid_hdr *vidh; | ||
57 | |||
58 | int ubi_scan_add_to_list(struct ubi_scan_info *si, int pnum, int ec, | ||
59 | struct list_head *list) | ||
60 | { | ||
61 | struct ubi_scan_leb *seb; | ||
62 | |||
63 | if (list == &si->free) | ||
64 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); | ||
65 | else if (list == &si->erase) | ||
66 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); | ||
67 | else if (list == &si->corr) | ||
68 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); | ||
69 | else if (list == &si->alien) | ||
70 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); | ||
71 | else | ||
72 | BUG(); | ||
73 | |||
74 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); | ||
75 | if (!seb) | ||
76 | return -ENOMEM; | ||
77 | |||
78 | seb->pnum = pnum; | ||
79 | seb->ec = ec; | ||
80 | list_add_tail(&seb->u.list, list); | ||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * commit_to_mean_value - commit intermediate results to the final mean erase | ||
86 | * counter value. | ||
87 | * @si: scanning information | ||
88 | * | ||
89 | * This is a helper function which calculates partial mean erase counter mean | ||
90 | * value and adds it to the resulting mean value. As we can work only in | ||
91 | * integer arithmetic and we want to calculate the mean value of erase counter | ||
92 | * accurately, we first sum erase counter values in @si->ec_sum variable and | ||
93 | * count these components in @si->ec_count. If this temporary @si->ec_sum is | ||
94 | * going to overflow, we calculate the partial mean value | ||
95 | * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec. | ||
96 | */ | ||
97 | static void commit_to_mean_value(struct ubi_scan_info *si) | ||
98 | { | ||
99 | si->ec_sum /= si->ec_count; | ||
100 | if (si->ec_sum % si->ec_count >= si->ec_count / 2) | ||
101 | si->mean_ec += 1; | ||
102 | si->mean_ec += si->ec_sum; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * validate_vid_hdr - check that volume identifier header is correct and | ||
107 | * consistent. | ||
108 | * @vid_hdr: the volume identifier header to check | ||
109 | * @sv: information about the volume this logical eraseblock belongs to | ||
110 | * @pnum: physical eraseblock number the VID header came from | ||
111 | * | ||
112 | * This function checks that data stored in @vid_hdr is consistent. Returns | ||
113 | * non-zero if an inconsistency was found and zero if not. | ||
114 | * | ||
115 | * Note, UBI does sanity check of everything it reads from the flash media. | ||
116 | * Most of the checks are done in the I/O unit. Here we check that the | ||
117 | * information in the VID header is consistent to the information in other VID | ||
118 | * headers of the same volume. | ||
119 | */ | ||
120 | static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, | ||
121 | const struct ubi_scan_volume *sv, int pnum) | ||
122 | { | ||
123 | int vol_type = vid_hdr->vol_type; | ||
124 | int vol_id = ubi32_to_cpu(vid_hdr->vol_id); | ||
125 | int used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); | ||
126 | int data_pad = ubi32_to_cpu(vid_hdr->data_pad); | ||
127 | |||
128 | if (sv->leb_count != 0) { | ||
129 | int sv_vol_type; | ||
130 | |||
131 | /* | ||
132 | * This is not the first logical eraseblock belonging to this | ||
133 | * volume. Ensure that the data in its VID header is consistent | ||
134 | * to the data in previous logical eraseblock headers. | ||
135 | */ | ||
136 | |||
137 | if (vol_id != sv->vol_id) { | ||
138 | dbg_err("inconsistent vol_id"); | ||
139 | goto bad; | ||
140 | } | ||
141 | |||
142 | if (sv->vol_type == UBI_STATIC_VOLUME) | ||
143 | sv_vol_type = UBI_VID_STATIC; | ||
144 | else | ||
145 | sv_vol_type = UBI_VID_DYNAMIC; | ||
146 | |||
147 | if (vol_type != sv_vol_type) { | ||
148 | dbg_err("inconsistent vol_type"); | ||
149 | goto bad; | ||
150 | } | ||
151 | |||
152 | if (used_ebs != sv->used_ebs) { | ||
153 | dbg_err("inconsistent used_ebs"); | ||
154 | goto bad; | ||
155 | } | ||
156 | |||
157 | if (data_pad != sv->data_pad) { | ||
158 | dbg_err("inconsistent data_pad"); | ||
159 | goto bad; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | return 0; | ||
164 | |||
165 | bad: | ||
166 | ubi_err("inconsistent VID header at PEB %d", pnum); | ||
167 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
168 | ubi_dbg_dump_sv(sv); | ||
169 | return -EINVAL; | ||
170 | } | ||
171 | |||
172 | /** | ||
173 | * add_volume - add volume to the scanning information. | ||
174 | * @si: scanning information | ||
175 | * @vol_id: ID of the volume to add | ||
176 | * @pnum: physical eraseblock number | ||
177 | * @vid_hdr: volume identifier header | ||
178 | * | ||
179 | * If the volume corresponding to the @vid_hdr logical eraseblock is already | ||
180 | * present in the scanning information, this function does nothing. Otherwise | ||
181 | * it adds corresponding volume to the scanning information. Returns a pointer | ||
182 | * to the scanning volume object in case of success and a negative error code | ||
183 | * in case of failure. | ||
184 | */ | ||
185 | static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id, | ||
186 | int pnum, | ||
187 | const struct ubi_vid_hdr *vid_hdr) | ||
188 | { | ||
189 | struct ubi_scan_volume *sv; | ||
190 | struct rb_node **p = &si->volumes.rb_node, *parent = NULL; | ||
191 | |||
192 | ubi_assert(vol_id == ubi32_to_cpu(vid_hdr->vol_id)); | ||
193 | |||
194 | /* Walk the volume RB-tree to look if this volume is already present */ | ||
195 | while (*p) { | ||
196 | parent = *p; | ||
197 | sv = rb_entry(parent, struct ubi_scan_volume, rb); | ||
198 | |||
199 | if (vol_id == sv->vol_id) | ||
200 | return sv; | ||
201 | |||
202 | if (vol_id > sv->vol_id) | ||
203 | p = &(*p)->rb_left; | ||
204 | else | ||
205 | p = &(*p)->rb_right; | ||
206 | } | ||
207 | |||
208 | /* The volume is absent - add it */ | ||
209 | sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL); | ||
210 | if (!sv) | ||
211 | return ERR_PTR(-ENOMEM); | ||
212 | |||
213 | sv->highest_lnum = sv->leb_count = 0; | ||
214 | si->max_sqnum = 0; | ||
215 | sv->vol_id = vol_id; | ||
216 | sv->root = RB_ROOT; | ||
217 | sv->used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); | ||
218 | sv->data_pad = ubi32_to_cpu(vid_hdr->data_pad); | ||
219 | sv->compat = vid_hdr->compat; | ||
220 | sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME | ||
221 | : UBI_STATIC_VOLUME; | ||
222 | if (vol_id > si->highest_vol_id) | ||
223 | si->highest_vol_id = vol_id; | ||
224 | |||
225 | rb_link_node(&sv->rb, parent, p); | ||
226 | rb_insert_color(&sv->rb, &si->volumes); | ||
227 | si->vols_found += 1; | ||
228 | dbg_bld("added volume %d", vol_id); | ||
229 | return sv; | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * compare_lebs - find out which logical eraseblock is newer. | ||
234 | * @ubi: UBI device description object | ||
235 | * @seb: first logical eraseblock to compare | ||
236 | * @pnum: physical eraseblock number of the second logical eraseblock to | ||
237 | * compare | ||
238 | * @vid_hdr: volume identifier header of the second logical eraseblock | ||
239 | * | ||
240 | * This function compares 2 copies of a LEB and informs which one is newer. In | ||
241 | * case of success this function returns a positive value, in case of failure, a | ||
242 | * negative error code is returned. The success return codes use the following | ||
243 | * bits: | ||
244 | * o bit 0 is cleared: the first PEB (described by @seb) is newer then the | ||
245 | * second PEB (described by @pnum and @vid_hdr); | ||
246 | * o bit 0 is set: the second PEB is newer; | ||
247 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; | ||
248 | * o bit 1 is set: bit-flips were detected in the newer LEB; | ||
249 | * o bit 2 is cleared: the older LEB is not corrupted; | ||
250 | * o bit 2 is set: the older LEB is corrupted. | ||
251 | */ | ||
252 | static int compare_lebs(const struct ubi_device *ubi, | ||
253 | const struct ubi_scan_leb *seb, int pnum, | ||
254 | const struct ubi_vid_hdr *vid_hdr) | ||
255 | { | ||
256 | void *buf; | ||
257 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; | ||
258 | uint32_t data_crc, crc; | ||
259 | struct ubi_vid_hdr *vidh = NULL; | ||
260 | unsigned long long sqnum2 = ubi64_to_cpu(vid_hdr->sqnum); | ||
261 | |||
262 | if (seb->sqnum == 0 && sqnum2 == 0) { | ||
263 | long long abs, v1 = seb->leb_ver, v2 = ubi32_to_cpu(vid_hdr->leb_ver); | ||
264 | |||
265 | /* | ||
266 | * UBI constantly increases the logical eraseblock version | ||
267 | * number and it can overflow. Thus, we have to bear in mind | ||
268 | * that versions that are close to %0xFFFFFFFF are less then | ||
269 | * versions that are close to %0. | ||
270 | * | ||
271 | * The UBI WL unit guarantees that the number of pending tasks | ||
272 | * is not greater then %0x7FFFFFFF. So, if the difference | ||
273 | * between any two versions is greater or equivalent to | ||
274 | * %0x7FFFFFFF, there was an overflow and the logical | ||
275 | * eraseblock with lower version is actually newer then the one | ||
276 | * with higher version. | ||
277 | * | ||
278 | * FIXME: but this is anyway obsolete and will be removed at | ||
279 | * some point. | ||
280 | */ | ||
281 | |||
282 | dbg_bld("using old crappy leb_ver stuff"); | ||
283 | |||
284 | abs = v1 - v2; | ||
285 | if (abs < 0) | ||
286 | abs = -abs; | ||
287 | |||
288 | if (abs < 0x7FFFFFFF) | ||
289 | /* Non-overflow situation */ | ||
290 | second_is_newer = (v2 > v1); | ||
291 | else | ||
292 | second_is_newer = (v2 < v1); | ||
293 | } else | ||
294 | /* Obviously the LEB with lower sequence counter is older */ | ||
295 | second_is_newer = sqnum2 > seb->sqnum; | ||
296 | |||
297 | /* | ||
298 | * Now we know which copy is newer. If the copy flag of the PEB with | ||
299 | * newer version is not set, then we just return, otherwise we have to | ||
300 | * check data CRC. For the second PEB we already have the VID header, | ||
301 | * for the first one - we'll need to re-read it from flash. | ||
302 | * | ||
303 | * FIXME: this may be optimized so that we wouldn't read twice. | ||
304 | */ | ||
305 | |||
306 | if (second_is_newer) { | ||
307 | if (!vid_hdr->copy_flag) { | ||
308 | /* It is not a copy, so it is newer */ | ||
309 | dbg_bld("second PEB %d is newer, copy_flag is unset", | ||
310 | pnum); | ||
311 | return 1; | ||
312 | } | ||
313 | } else { | ||
314 | pnum = seb->pnum; | ||
315 | |||
316 | vidh = ubi_zalloc_vid_hdr(ubi); | ||
317 | if (!vidh) | ||
318 | return -ENOMEM; | ||
319 | |||
320 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); | ||
321 | if (err) { | ||
322 | if (err == UBI_IO_BITFLIPS) | ||
323 | bitflips = 1; | ||
324 | else { | ||
325 | dbg_err("VID of PEB %d header is bad, but it " | ||
326 | "was OK earlier", pnum); | ||
327 | if (err > 0) | ||
328 | err = -EIO; | ||
329 | |||
330 | goto out_free_vidh; | ||
331 | } | ||
332 | } | ||
333 | |||
334 | if (!vidh->copy_flag) { | ||
335 | /* It is not a copy, so it is newer */ | ||
336 | dbg_bld("first PEB %d is newer, copy_flag is unset", | ||
337 | pnum); | ||
338 | err = bitflips << 1; | ||
339 | goto out_free_vidh; | ||
340 | } | ||
341 | |||
342 | vid_hdr = vidh; | ||
343 | } | ||
344 | |||
345 | /* Read the data of the copy and check the CRC */ | ||
346 | |||
347 | len = ubi32_to_cpu(vid_hdr->data_size); | ||
348 | buf = kmalloc(len, GFP_KERNEL); | ||
349 | if (!buf) { | ||
350 | err = -ENOMEM; | ||
351 | goto out_free_vidh; | ||
352 | } | ||
353 | |||
354 | err = ubi_io_read_data(ubi, buf, pnum, 0, len); | ||
355 | if (err && err != UBI_IO_BITFLIPS) | ||
356 | goto out_free_buf; | ||
357 | |||
358 | data_crc = ubi32_to_cpu(vid_hdr->data_crc); | ||
359 | crc = crc32(UBI_CRC32_INIT, buf, len); | ||
360 | if (crc != data_crc) { | ||
361 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", | ||
362 | pnum, crc, data_crc); | ||
363 | corrupted = 1; | ||
364 | bitflips = 0; | ||
365 | second_is_newer = !second_is_newer; | ||
366 | } else { | ||
367 | dbg_bld("PEB %d CRC is OK", pnum); | ||
368 | bitflips = !!err; | ||
369 | } | ||
370 | |||
371 | kfree(buf); | ||
372 | ubi_free_vid_hdr(ubi, vidh); | ||
373 | |||
374 | if (second_is_newer) | ||
375 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); | ||
376 | else | ||
377 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); | ||
378 | |||
379 | return second_is_newer | (bitflips << 1) | (corrupted << 2); | ||
380 | |||
381 | out_free_buf: | ||
382 | kfree(buf); | ||
383 | out_free_vidh: | ||
384 | ubi_free_vid_hdr(ubi, vidh); | ||
385 | ubi_assert(err < 0); | ||
386 | return err; | ||
387 | } | ||
388 | |||
389 | /** | ||
390 | * ubi_scan_add_used - add information about a physical eraseblock to the | ||
391 | * scanning information. | ||
392 | * @ubi: UBI device description object | ||
393 | * @si: scanning information | ||
394 | * @pnum: the physical eraseblock number | ||
395 | * @ec: erase counter | ||
396 | * @vid_hdr: the volume identifier header | ||
397 | * @bitflips: if bit-flips were detected when this physical eraseblock was read | ||
398 | * | ||
399 | * This function returns zero in case of success and a negative error code in | ||
400 | * case of failure. | ||
401 | */ | ||
402 | int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si, | ||
403 | int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, | ||
404 | int bitflips) | ||
405 | { | ||
406 | int err, vol_id, lnum; | ||
407 | uint32_t leb_ver; | ||
408 | unsigned long long sqnum; | ||
409 | struct ubi_scan_volume *sv; | ||
410 | struct ubi_scan_leb *seb; | ||
411 | struct rb_node **p, *parent = NULL; | ||
412 | |||
413 | vol_id = ubi32_to_cpu(vid_hdr->vol_id); | ||
414 | lnum = ubi32_to_cpu(vid_hdr->lnum); | ||
415 | sqnum = ubi64_to_cpu(vid_hdr->sqnum); | ||
416 | leb_ver = ubi32_to_cpu(vid_hdr->leb_ver); | ||
417 | |||
418 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d", | ||
419 | pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips); | ||
420 | |||
421 | sv = add_volume(si, vol_id, pnum, vid_hdr); | ||
422 | if (IS_ERR(sv) < 0) | ||
423 | return PTR_ERR(sv); | ||
424 | |||
425 | /* | ||
426 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look | ||
427 | * if this is the first instance of this logical eraseblock or not. | ||
428 | */ | ||
429 | p = &sv->root.rb_node; | ||
430 | while (*p) { | ||
431 | int cmp_res; | ||
432 | |||
433 | parent = *p; | ||
434 | seb = rb_entry(parent, struct ubi_scan_leb, u.rb); | ||
435 | if (lnum != seb->lnum) { | ||
436 | if (lnum < seb->lnum) | ||
437 | p = &(*p)->rb_left; | ||
438 | else | ||
439 | p = &(*p)->rb_right; | ||
440 | continue; | ||
441 | } | ||
442 | |||
443 | /* | ||
444 | * There is already a physical eraseblock describing the same | ||
445 | * logical eraseblock present. | ||
446 | */ | ||
447 | |||
448 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, " | ||
449 | "LEB ver %u, EC %d", seb->pnum, seb->sqnum, | ||
450 | seb->leb_ver, seb->ec); | ||
451 | |||
452 | /* | ||
453 | * Make sure that the logical eraseblocks have different | ||
454 | * versions. Otherwise the image is bad. | ||
455 | */ | ||
456 | if (seb->leb_ver == leb_ver && leb_ver != 0) { | ||
457 | ubi_err("two LEBs with same version %u", leb_ver); | ||
458 | ubi_dbg_dump_seb(seb, 0); | ||
459 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
460 | return -EINVAL; | ||
461 | } | ||
462 | |||
463 | /* | ||
464 | * Make sure that the logical eraseblocks have different | ||
465 | * sequence numbers. Otherwise the image is bad. | ||
466 | * | ||
467 | * FIXME: remove 'sqnum != 0' check when leb_ver is removed. | ||
468 | */ | ||
469 | if (seb->sqnum == sqnum && sqnum != 0) { | ||
470 | ubi_err("two LEBs with same sequence number %llu", | ||
471 | sqnum); | ||
472 | ubi_dbg_dump_seb(seb, 0); | ||
473 | ubi_dbg_dump_vid_hdr(vid_hdr); | ||
474 | return -EINVAL; | ||
475 | } | ||
476 | |||
477 | /* | ||
478 | * Now we have to drop the older one and preserve the newer | ||
479 | * one. | ||
480 | */ | ||
481 | cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr); | ||
482 | if (cmp_res < 0) | ||
483 | return cmp_res; | ||
484 | |||
485 | if (cmp_res & 1) { | ||
486 | /* | ||
487 | * This logical eraseblock is newer then the one | ||
488 | * found earlier. | ||
489 | */ | ||
490 | err = validate_vid_hdr(vid_hdr, sv, pnum); | ||
491 | if (err) | ||
492 | return err; | ||
493 | |||
494 | if (cmp_res & 4) | ||
495 | err = ubi_scan_add_to_list(si, seb->pnum, | ||
496 | seb->ec, &si->corr); | ||
497 | else | ||
498 | err = ubi_scan_add_to_list(si, seb->pnum, | ||
499 | seb->ec, &si->erase); | ||
500 | if (err) | ||
501 | return err; | ||
502 | |||
503 | seb->ec = ec; | ||
504 | seb->pnum = pnum; | ||
505 | seb->scrub = ((cmp_res & 2) || bitflips); | ||
506 | seb->sqnum = sqnum; | ||
507 | seb->leb_ver = leb_ver; | ||
508 | |||
509 | if (sv->highest_lnum == lnum) | ||
510 | sv->last_data_size = | ||
511 | ubi32_to_cpu(vid_hdr->data_size); | ||
512 | |||
513 | return 0; | ||
514 | } else { | ||
515 | /* | ||
516 | * This logical eraseblock is older then the one found | ||
517 | * previously. | ||
518 | */ | ||
519 | if (cmp_res & 4) | ||
520 | return ubi_scan_add_to_list(si, pnum, ec, | ||
521 | &si->corr); | ||
522 | else | ||
523 | return ubi_scan_add_to_list(si, pnum, ec, | ||
524 | &si->erase); | ||
525 | } | ||
526 | } | ||
527 | |||
528 | /* | ||
529 | * We've met this logical eraseblock for the first time, add it to the | ||
530 | * scanning information. | ||
531 | */ | ||
532 | |||
533 | err = validate_vid_hdr(vid_hdr, sv, pnum); | ||
534 | if (err) | ||
535 | return err; | ||
536 | |||
537 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); | ||
538 | if (!seb) | ||
539 | return -ENOMEM; | ||
540 | |||
541 | seb->ec = ec; | ||
542 | seb->pnum = pnum; | ||
543 | seb->lnum = lnum; | ||
544 | seb->sqnum = sqnum; | ||
545 | seb->scrub = bitflips; | ||
546 | seb->leb_ver = leb_ver; | ||
547 | |||
548 | if (sv->highest_lnum <= lnum) { | ||
549 | sv->highest_lnum = lnum; | ||
550 | sv->last_data_size = ubi32_to_cpu(vid_hdr->data_size); | ||
551 | } | ||
552 | |||
553 | if (si->max_sqnum < sqnum) | ||
554 | si->max_sqnum = sqnum; | ||
555 | |||
556 | sv->leb_count += 1; | ||
557 | rb_link_node(&seb->u.rb, parent, p); | ||
558 | rb_insert_color(&seb->u.rb, &sv->root); | ||
559 | return 0; | ||
560 | } | ||
561 | |||
562 | /** | ||
563 | * ubi_scan_find_sv - find information about a particular volume in the | ||
564 | * scanning information. | ||
565 | * @si: scanning information | ||
566 | * @vol_id: the requested volume ID | ||
567 | * | ||
568 | * This function returns a pointer to the volume description or %NULL if there | ||
569 | * are no data about this volume in the scanning information. | ||
570 | */ | ||
571 | struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si, | ||
572 | int vol_id) | ||
573 | { | ||
574 | struct ubi_scan_volume *sv; | ||
575 | struct rb_node *p = si->volumes.rb_node; | ||
576 | |||
577 | while (p) { | ||
578 | sv = rb_entry(p, struct ubi_scan_volume, rb); | ||
579 | |||
580 | if (vol_id == sv->vol_id) | ||
581 | return sv; | ||
582 | |||
583 | if (vol_id > sv->vol_id) | ||
584 | p = p->rb_left; | ||
585 | else | ||
586 | p = p->rb_right; | ||
587 | } | ||
588 | |||
589 | return NULL; | ||
590 | } | ||
591 | |||
592 | /** | ||
593 | * ubi_scan_find_seb - find information about a particular logical | ||
594 | * eraseblock in the volume scanning information. | ||
595 | * @sv: a pointer to the volume scanning information | ||
596 | * @lnum: the requested logical eraseblock | ||
597 | * | ||
598 | * This function returns a pointer to the scanning logical eraseblock or %NULL | ||
599 | * if there are no data about it in the scanning volume information. | ||
600 | */ | ||
601 | struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, | ||
602 | int lnum) | ||
603 | { | ||
604 | struct ubi_scan_leb *seb; | ||
605 | struct rb_node *p = sv->root.rb_node; | ||
606 | |||
607 | while (p) { | ||
608 | seb = rb_entry(p, struct ubi_scan_leb, u.rb); | ||
609 | |||
610 | if (lnum == seb->lnum) | ||
611 | return seb; | ||
612 | |||
613 | if (lnum > seb->lnum) | ||
614 | p = p->rb_left; | ||
615 | else | ||
616 | p = p->rb_right; | ||
617 | } | ||
618 | |||
619 | return NULL; | ||
620 | } | ||
621 | |||
622 | /** | ||
623 | * ubi_scan_rm_volume - delete scanning information about a volume. | ||
624 | * @si: scanning information | ||
625 | * @sv: the volume scanning information to delete | ||
626 | */ | ||
627 | void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv) | ||
628 | { | ||
629 | struct rb_node *rb; | ||
630 | struct ubi_scan_leb *seb; | ||
631 | |||
632 | dbg_bld("remove scanning information about volume %d", sv->vol_id); | ||
633 | |||
634 | while ((rb = rb_first(&sv->root))) { | ||
635 | seb = rb_entry(rb, struct ubi_scan_leb, u.rb); | ||
636 | rb_erase(&seb->u.rb, &sv->root); | ||
637 | list_add_tail(&seb->u.list, &si->erase); | ||
638 | } | ||
639 | |||
640 | rb_erase(&sv->rb, &si->volumes); | ||
641 | kfree(sv); | ||
642 | si->vols_found -= 1; | ||
643 | } | ||
644 | |||
645 | /** | ||
646 | * ubi_scan_erase_peb - erase a physical eraseblock. | ||
647 | * @ubi: UBI device description object | ||
648 | * @si: scanning information | ||
649 | * @pnum: physical eraseblock number to erase; | ||
650 | * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) | ||
651 | * | ||
652 | * This function erases physical eraseblock 'pnum', and writes the erase | ||
653 | * counter header to it. This function should only be used on UBI device | ||
654 | * initialization stages, when the EBA unit had not been yet initialized. This | ||
655 | * function returns zero in case of success and a negative error code in case | ||
656 | * of failure. | ||
657 | */ | ||
658 | int ubi_scan_erase_peb(const struct ubi_device *ubi, | ||
659 | const struct ubi_scan_info *si, int pnum, int ec) | ||
660 | { | ||
661 | int err; | ||
662 | struct ubi_ec_hdr *ec_hdr; | ||
663 | |||
664 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | ||
665 | if (!ec_hdr) | ||
666 | return -ENOMEM; | ||
667 | |||
668 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { | ||
669 | /* | ||
670 | * Erase counter overflow. Upgrade UBI and use 64-bit | ||
671 | * erase counters internally. | ||
672 | */ | ||
673 | ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec); | ||
674 | return -EINVAL; | ||
675 | } | ||
676 | |||
677 | ec_hdr->ec = cpu_to_ubi64(ec); | ||
678 | |||
679 | err = ubi_io_sync_erase(ubi, pnum, 0); | ||
680 | if (err < 0) | ||
681 | goto out_free; | ||
682 | |||
683 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); | ||
684 | |||
685 | out_free: | ||
686 | kfree(ec_hdr); | ||
687 | return err; | ||
688 | } | ||
689 | |||
690 | /** | ||
691 | * ubi_scan_get_free_peb - get a free physical eraseblock. | ||
692 | * @ubi: UBI device description object | ||
693 | * @si: scanning information | ||
694 | * | ||
695 | * This function returns a free physical eraseblock. It is supposed to be | ||
696 | * called on the UBI initialization stages when the wear-leveling unit is not | ||
697 | * initialized yet. This function picks a physical eraseblocks from one of the | ||
698 | * lists, writes the EC header if it is needed, and removes it from the list. | ||
699 | * | ||
700 | * This function returns scanning physical eraseblock information in case of | ||
701 | * success and an error code in case of failure. | ||
702 | */ | ||
703 | struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi, | ||
704 | struct ubi_scan_info *si) | ||
705 | { | ||
706 | int err = 0, i; | ||
707 | struct ubi_scan_leb *seb; | ||
708 | |||
709 | if (!list_empty(&si->free)) { | ||
710 | seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); | ||
711 | list_del(&seb->u.list); | ||
712 | dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec); | ||
713 | return seb; | ||
714 | } | ||
715 | |||
716 | for (i = 0; i < 2; i++) { | ||
717 | struct list_head *head; | ||
718 | struct ubi_scan_leb *tmp_seb; | ||
719 | |||
720 | if (i == 0) | ||
721 | head = &si->erase; | ||
722 | else | ||
723 | head = &si->corr; | ||
724 | |||
725 | /* | ||
726 | * We try to erase the first physical eraseblock from the @head | ||
727 | * list and pick it if we succeed, or try to erase the | ||
728 | * next one if not. And so forth. We don't want to take care | ||
729 | * about bad eraseblocks here - they'll be handled later. | ||
730 | */ | ||
731 | list_for_each_entry_safe(seb, tmp_seb, head, u.list) { | ||
732 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | ||
733 | seb->ec = si->mean_ec; | ||
734 | |||
735 | err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); | ||
736 | if (err) | ||
737 | continue; | ||
738 | |||
739 | seb->ec += 1; | ||
740 | list_del(&seb->u.list); | ||
741 | dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); | ||
742 | return seb; | ||
743 | } | ||
744 | } | ||
745 | |||
746 | ubi_err("no eraseblocks found"); | ||
747 | return ERR_PTR(-ENOSPC); | ||
748 | } | ||
749 | |||
750 | /** | ||
751 | * process_eb - read UBI headers, check them and add corresponding data | ||
752 | * to the scanning information. | ||
753 | * @ubi: UBI device description object | ||
754 | * @si: scanning information | ||
755 | * @pnum: the physical eraseblock number | ||
756 | * | ||
757 | * This function returns a zero if the physical eraseblock was succesfully | ||
758 | * handled and a negative error code in case of failure. | ||
759 | */ | ||
760 | static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum) | ||
761 | { | ||
762 | long long ec; | ||
763 | int err, bitflips = 0, vol_id, ec_corr = 0; | ||
764 | |||
765 | dbg_bld("scan PEB %d", pnum); | ||
766 | |||
767 | /* Skip bad physical eraseblocks */ | ||
768 | err = ubi_io_is_bad(ubi, pnum); | ||
769 | if (err < 0) | ||
770 | return err; | ||
771 | else if (err) { | ||
772 | /* | ||
773 | * FIXME: this is actually duty of the I/O unit to initialize | ||
774 | * this, but MTD does not provide enough information. | ||
775 | */ | ||
776 | si->bad_peb_count += 1; | ||
777 | return 0; | ||
778 | } | ||
779 | |||
780 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); | ||
781 | if (err < 0) | ||
782 | return err; | ||
783 | else if (err == UBI_IO_BITFLIPS) | ||
784 | bitflips = 1; | ||
785 | else if (err == UBI_IO_PEB_EMPTY) | ||
786 | return ubi_scan_add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, | ||
787 | &si->erase); | ||
788 | else if (err == UBI_IO_BAD_EC_HDR) { | ||
789 | /* | ||
790 | * We have to also look at the VID header, possibly it is not | ||
791 | * corrupted. Set %bitflips flag in order to make this PEB be | ||
792 | * moved and EC be re-created. | ||
793 | */ | ||
794 | ec_corr = 1; | ||
795 | ec = UBI_SCAN_UNKNOWN_EC; | ||
796 | bitflips = 1; | ||
797 | } | ||
798 | |||
799 | si->is_empty = 0; | ||
800 | |||
801 | if (!ec_corr) { | ||
802 | /* Make sure UBI version is OK */ | ||
803 | if (ech->version != UBI_VERSION) { | ||
804 | ubi_err("this UBI version is %d, image version is %d", | ||
805 | UBI_VERSION, (int)ech->version); | ||
806 | return -EINVAL; | ||
807 | } | ||
808 | |||
809 | ec = ubi64_to_cpu(ech->ec); | ||
810 | if (ec > UBI_MAX_ERASECOUNTER) { | ||
811 | /* | ||
812 | * Erase counter overflow. The EC headers have 64 bits | ||
813 | * reserved, but we anyway make use of only 31 bit | ||
814 | * values, as this seems to be enough for any existing | ||
815 | * flash. Upgrade UBI and use 64-bit erase counters | ||
816 | * internally. | ||
817 | */ | ||
818 | ubi_err("erase counter overflow, max is %d", | ||
819 | UBI_MAX_ERASECOUNTER); | ||
820 | ubi_dbg_dump_ec_hdr(ech); | ||
821 | return -EINVAL; | ||
822 | } | ||
823 | } | ||
824 | |||
825 | /* OK, we've done with the EC header, let's look at the VID header */ | ||
826 | |||
827 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); | ||
828 | if (err < 0) | ||
829 | return err; | ||
830 | else if (err == UBI_IO_BITFLIPS) | ||
831 | bitflips = 1; | ||
832 | else if (err == UBI_IO_BAD_VID_HDR || | ||
833 | (err == UBI_IO_PEB_FREE && ec_corr)) { | ||
834 | /* VID header is corrupted */ | ||
835 | err = ubi_scan_add_to_list(si, pnum, ec, &si->corr); | ||
836 | if (err) | ||
837 | return err; | ||
838 | goto adjust_mean_ec; | ||
839 | } else if (err == UBI_IO_PEB_FREE) { | ||
840 | /* No VID header - the physical eraseblock is free */ | ||
841 | err = ubi_scan_add_to_list(si, pnum, ec, &si->free); | ||
842 | if (err) | ||
843 | return err; | ||
844 | goto adjust_mean_ec; | ||
845 | } | ||
846 | |||
847 | vol_id = ubi32_to_cpu(vidh->vol_id); | ||
848 | if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) { | ||
849 | int lnum = ubi32_to_cpu(vidh->lnum); | ||
850 | |||
851 | /* Unsupported internal volume */ | ||
852 | switch (vidh->compat) { | ||
853 | case UBI_COMPAT_DELETE: | ||
854 | ubi_msg("\"delete\" compatible internal volume %d:%d" | ||
855 | " found, remove it", vol_id, lnum); | ||
856 | err = ubi_scan_add_to_list(si, pnum, ec, &si->corr); | ||
857 | if (err) | ||
858 | return err; | ||
859 | break; | ||
860 | |||
861 | case UBI_COMPAT_RO: | ||
862 | ubi_msg("read-only compatible internal volume %d:%d" | ||
863 | " found, switch to read-only mode", | ||
864 | vol_id, lnum); | ||
865 | ubi->ro_mode = 1; | ||
866 | break; | ||
867 | |||
868 | case UBI_COMPAT_PRESERVE: | ||
869 | ubi_msg("\"preserve\" compatible internal volume %d:%d" | ||
870 | " found", vol_id, lnum); | ||
871 | err = ubi_scan_add_to_list(si, pnum, ec, &si->alien); | ||
872 | if (err) | ||
873 | return err; | ||
874 | si->alien_peb_count += 1; | ||
875 | return 0; | ||
876 | |||
877 | case UBI_COMPAT_REJECT: | ||
878 | ubi_err("incompatible internal volume %d:%d found", | ||
879 | vol_id, lnum); | ||
880 | return -EINVAL; | ||
881 | } | ||
882 | } | ||
883 | |||
884 | /* Both UBI headers seem to be fine */ | ||
885 | err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); | ||
886 | if (err) | ||
887 | return err; | ||
888 | |||
889 | adjust_mean_ec: | ||
890 | if (!ec_corr) { | ||
891 | if (si->ec_sum + ec < ec) { | ||
892 | commit_to_mean_value(si); | ||
893 | si->ec_sum = 0; | ||
894 | si->ec_count = 0; | ||
895 | } else { | ||
896 | si->ec_sum += ec; | ||
897 | si->ec_count += 1; | ||
898 | } | ||
899 | |||
900 | if (ec > si->max_ec) | ||
901 | si->max_ec = ec; | ||
902 | if (ec < si->min_ec) | ||
903 | si->min_ec = ec; | ||
904 | } | ||
905 | |||
906 | return 0; | ||
907 | } | ||
908 | |||
909 | /** | ||
910 | * ubi_scan - scan an MTD device. | ||
911 | * @ubi: UBI device description object | ||
912 | * | ||
913 | * This function does full scanning of an MTD device and returns complete | ||
914 | * information about it. In case of failure, an error code is returned. | ||
915 | */ | ||
916 | struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) | ||
917 | { | ||
918 | int err, pnum; | ||
919 | struct rb_node *rb1, *rb2; | ||
920 | struct ubi_scan_volume *sv; | ||
921 | struct ubi_scan_leb *seb; | ||
922 | struct ubi_scan_info *si; | ||
923 | |||
924 | si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL); | ||
925 | if (!si) | ||
926 | return ERR_PTR(-ENOMEM); | ||
927 | |||
928 | INIT_LIST_HEAD(&si->corr); | ||
929 | INIT_LIST_HEAD(&si->free); | ||
930 | INIT_LIST_HEAD(&si->erase); | ||
931 | INIT_LIST_HEAD(&si->alien); | ||
932 | si->volumes = RB_ROOT; | ||
933 | si->is_empty = 1; | ||
934 | |||
935 | err = -ENOMEM; | ||
936 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | ||
937 | if (!ech) | ||
938 | goto out_si; | ||
939 | |||
940 | vidh = ubi_zalloc_vid_hdr(ubi); | ||
941 | if (!vidh) | ||
942 | goto out_ech; | ||
943 | |||
944 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { | ||
945 | cond_resched(); | ||
946 | |||
947 | dbg_msg("process PEB %d", pnum); | ||
948 | err = process_eb(ubi, si, pnum); | ||
949 | if (err < 0) | ||
950 | goto out_vidh; | ||
951 | } | ||
952 | |||
953 | dbg_msg("scanning is finished"); | ||
954 | |||
955 | /* Finish mean erase counter calculations */ | ||
956 | if (si->ec_count) | ||
957 | commit_to_mean_value(si); | ||
958 | |||
959 | if (si->is_empty) | ||
960 | ubi_msg("empty MTD device detected"); | ||
961 | |||
962 | /* | ||
963 | * In case of unknown erase counter we use the mean erase counter | ||
964 | * value. | ||
965 | */ | ||
966 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | ||
967 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) | ||
968 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | ||
969 | seb->ec = si->mean_ec; | ||
970 | } | ||
971 | |||
972 | list_for_each_entry(seb, &si->free, u.list) { | ||
973 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | ||
974 | seb->ec = si->mean_ec; | ||
975 | } | ||
976 | |||
977 | list_for_each_entry(seb, &si->corr, u.list) | ||
978 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | ||
979 | seb->ec = si->mean_ec; | ||
980 | |||
981 | list_for_each_entry(seb, &si->erase, u.list) | ||
982 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) | ||
983 | seb->ec = si->mean_ec; | ||
984 | |||
985 | err = paranoid_check_si(ubi, si); | ||
986 | if (err) { | ||
987 | if (err > 0) | ||
988 | err = -EINVAL; | ||
989 | goto out_vidh; | ||
990 | } | ||
991 | |||
992 | ubi_free_vid_hdr(ubi, vidh); | ||
993 | kfree(ech); | ||
994 | |||
995 | return si; | ||
996 | |||
997 | out_vidh: | ||
998 | ubi_free_vid_hdr(ubi, vidh); | ||
999 | out_ech: | ||
1000 | kfree(ech); | ||
1001 | out_si: | ||
1002 | ubi_scan_destroy_si(si); | ||
1003 | return ERR_PTR(err); | ||
1004 | } | ||
1005 | |||
1006 | /** | ||
1007 | * destroy_sv - free the scanning volume information | ||
1008 | * @sv: scanning volume information | ||
1009 | * | ||
1010 | * This function destroys the volume RB-tree (@sv->root) and the scanning | ||
1011 | * volume information. | ||
1012 | */ | ||
1013 | static void destroy_sv(struct ubi_scan_volume *sv) | ||
1014 | { | ||
1015 | struct ubi_scan_leb *seb; | ||
1016 | struct rb_node *this = sv->root.rb_node; | ||
1017 | |||
1018 | while (this) { | ||
1019 | if (this->rb_left) | ||
1020 | this = this->rb_left; | ||
1021 | else if (this->rb_right) | ||
1022 | this = this->rb_right; | ||
1023 | else { | ||
1024 | seb = rb_entry(this, struct ubi_scan_leb, u.rb); | ||
1025 | this = rb_parent(this); | ||
1026 | if (this) { | ||
1027 | if (this->rb_left == &seb->u.rb) | ||
1028 | this->rb_left = NULL; | ||
1029 | else | ||
1030 | this->rb_right = NULL; | ||
1031 | } | ||
1032 | |||
1033 | kfree(seb); | ||
1034 | } | ||
1035 | } | ||
1036 | kfree(sv); | ||
1037 | } | ||
1038 | |||
1039 | /** | ||
1040 | * ubi_scan_destroy_si - destroy scanning information. | ||
1041 | * @si: scanning information | ||
1042 | */ | ||
1043 | void ubi_scan_destroy_si(struct ubi_scan_info *si) | ||
1044 | { | ||
1045 | struct ubi_scan_leb *seb, *seb_tmp; | ||
1046 | struct ubi_scan_volume *sv; | ||
1047 | struct rb_node *rb; | ||
1048 | |||
1049 | list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { | ||
1050 | list_del(&seb->u.list); | ||
1051 | kfree(seb); | ||
1052 | } | ||
1053 | list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { | ||
1054 | list_del(&seb->u.list); | ||
1055 | kfree(seb); | ||
1056 | } | ||
1057 | list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { | ||
1058 | list_del(&seb->u.list); | ||
1059 | kfree(seb); | ||
1060 | } | ||
1061 | list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { | ||
1062 | list_del(&seb->u.list); | ||
1063 | kfree(seb); | ||
1064 | } | ||
1065 | |||
1066 | /* Destroy the volume RB-tree */ | ||
1067 | rb = si->volumes.rb_node; | ||
1068 | while (rb) { | ||
1069 | if (rb->rb_left) | ||
1070 | rb = rb->rb_left; | ||
1071 | else if (rb->rb_right) | ||
1072 | rb = rb->rb_right; | ||
1073 | else { | ||
1074 | sv = rb_entry(rb, struct ubi_scan_volume, rb); | ||
1075 | |||
1076 | rb = rb_parent(rb); | ||
1077 | if (rb) { | ||
1078 | if (rb->rb_left == &sv->rb) | ||
1079 | rb->rb_left = NULL; | ||
1080 | else | ||
1081 | rb->rb_right = NULL; | ||
1082 | } | ||
1083 | |||
1084 | destroy_sv(sv); | ||
1085 | } | ||
1086 | } | ||
1087 | |||
1088 | kfree(si); | ||
1089 | } | ||
1090 | |||
1091 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
1092 | |||
1093 | /** | ||
1094 | * paranoid_check_si - check if the scanning information is correct and | ||
1095 | * consistent. | ||
1096 | * @ubi: UBI device description object | ||
1097 | * @si: scanning information | ||
1098 | * | ||
1099 | * This function returns zero if the scanning information is all right, %1 if | ||
1100 | * not and a negative error code if an error occurred. | ||
1101 | */ | ||
1102 | static int paranoid_check_si(const struct ubi_device *ubi, | ||
1103 | struct ubi_scan_info *si) | ||
1104 | { | ||
1105 | int pnum, err, vols_found = 0; | ||
1106 | struct rb_node *rb1, *rb2; | ||
1107 | struct ubi_scan_volume *sv; | ||
1108 | struct ubi_scan_leb *seb, *last_seb; | ||
1109 | uint8_t *buf; | ||
1110 | |||
1111 | /* | ||
1112 | * At first, check that scanning information is ok. | ||
1113 | */ | ||
1114 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | ||
1115 | int leb_count = 0; | ||
1116 | |||
1117 | cond_resched(); | ||
1118 | |||
1119 | vols_found += 1; | ||
1120 | |||
1121 | if (si->is_empty) { | ||
1122 | ubi_err("bad is_empty flag"); | ||
1123 | goto bad_sv; | ||
1124 | } | ||
1125 | |||
1126 | if (sv->vol_id < 0 || sv->highest_lnum < 0 || | ||
1127 | sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 || | ||
1128 | sv->data_pad < 0 || sv->last_data_size < 0) { | ||
1129 | ubi_err("negative values"); | ||
1130 | goto bad_sv; | ||
1131 | } | ||
1132 | |||
1133 | if (sv->vol_id >= UBI_MAX_VOLUMES && | ||
1134 | sv->vol_id < UBI_INTERNAL_VOL_START) { | ||
1135 | ubi_err("bad vol_id"); | ||
1136 | goto bad_sv; | ||
1137 | } | ||
1138 | |||
1139 | if (sv->vol_id > si->highest_vol_id) { | ||
1140 | ubi_err("highest_vol_id is %d, but vol_id %d is there", | ||
1141 | si->highest_vol_id, sv->vol_id); | ||
1142 | goto out; | ||
1143 | } | ||
1144 | |||
1145 | if (sv->vol_type != UBI_DYNAMIC_VOLUME && | ||
1146 | sv->vol_type != UBI_STATIC_VOLUME) { | ||
1147 | ubi_err("bad vol_type"); | ||
1148 | goto bad_sv; | ||
1149 | } | ||
1150 | |||
1151 | if (sv->data_pad > ubi->leb_size / 2) { | ||
1152 | ubi_err("bad data_pad"); | ||
1153 | goto bad_sv; | ||
1154 | } | ||
1155 | |||
1156 | last_seb = NULL; | ||
1157 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { | ||
1158 | cond_resched(); | ||
1159 | |||
1160 | last_seb = seb; | ||
1161 | leb_count += 1; | ||
1162 | |||
1163 | if (seb->pnum < 0 || seb->ec < 0) { | ||
1164 | ubi_err("negative values"); | ||
1165 | goto bad_seb; | ||
1166 | } | ||
1167 | |||
1168 | if (seb->ec < si->min_ec) { | ||
1169 | ubi_err("bad si->min_ec (%d), %d found", | ||
1170 | si->min_ec, seb->ec); | ||
1171 | goto bad_seb; | ||
1172 | } | ||
1173 | |||
1174 | if (seb->ec > si->max_ec) { | ||
1175 | ubi_err("bad si->max_ec (%d), %d found", | ||
1176 | si->max_ec, seb->ec); | ||
1177 | goto bad_seb; | ||
1178 | } | ||
1179 | |||
1180 | if (seb->pnum >= ubi->peb_count) { | ||
1181 | ubi_err("too high PEB number %d, total PEBs %d", | ||
1182 | seb->pnum, ubi->peb_count); | ||
1183 | goto bad_seb; | ||
1184 | } | ||
1185 | |||
1186 | if (sv->vol_type == UBI_STATIC_VOLUME) { | ||
1187 | if (seb->lnum >= sv->used_ebs) { | ||
1188 | ubi_err("bad lnum or used_ebs"); | ||
1189 | goto bad_seb; | ||
1190 | } | ||
1191 | } else { | ||
1192 | if (sv->used_ebs != 0) { | ||
1193 | ubi_err("non-zero used_ebs"); | ||
1194 | goto bad_seb; | ||
1195 | } | ||
1196 | } | ||
1197 | |||
1198 | if (seb->lnum > sv->highest_lnum) { | ||
1199 | ubi_err("incorrect highest_lnum or lnum"); | ||
1200 | goto bad_seb; | ||
1201 | } | ||
1202 | } | ||
1203 | |||
1204 | if (sv->leb_count != leb_count) { | ||
1205 | ubi_err("bad leb_count, %d objects in the tree", | ||
1206 | leb_count); | ||
1207 | goto bad_sv; | ||
1208 | } | ||
1209 | |||
1210 | if (!last_seb) | ||
1211 | continue; | ||
1212 | |||
1213 | seb = last_seb; | ||
1214 | |||
1215 | if (seb->lnum != sv->highest_lnum) { | ||
1216 | ubi_err("bad highest_lnum"); | ||
1217 | goto bad_seb; | ||
1218 | } | ||
1219 | } | ||
1220 | |||
1221 | if (vols_found != si->vols_found) { | ||
1222 | ubi_err("bad si->vols_found %d, should be %d", | ||
1223 | si->vols_found, vols_found); | ||
1224 | goto out; | ||
1225 | } | ||
1226 | |||
1227 | /* Check that scanning information is correct */ | ||
1228 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | ||
1229 | last_seb = NULL; | ||
1230 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { | ||
1231 | int vol_type; | ||
1232 | |||
1233 | cond_resched(); | ||
1234 | |||
1235 | last_seb = seb; | ||
1236 | |||
1237 | err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1); | ||
1238 | if (err && err != UBI_IO_BITFLIPS) { | ||
1239 | ubi_err("VID header is not OK (%d)", err); | ||
1240 | if (err > 0) | ||
1241 | err = -EIO; | ||
1242 | return err; | ||
1243 | } | ||
1244 | |||
1245 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? | ||
1246 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; | ||
1247 | if (sv->vol_type != vol_type) { | ||
1248 | ubi_err("bad vol_type"); | ||
1249 | goto bad_vid_hdr; | ||
1250 | } | ||
1251 | |||
1252 | if (seb->sqnum != ubi64_to_cpu(vidh->sqnum)) { | ||
1253 | ubi_err("bad sqnum %llu", seb->sqnum); | ||
1254 | goto bad_vid_hdr; | ||
1255 | } | ||
1256 | |||
1257 | if (sv->vol_id != ubi32_to_cpu(vidh->vol_id)) { | ||
1258 | ubi_err("bad vol_id %d", sv->vol_id); | ||
1259 | goto bad_vid_hdr; | ||
1260 | } | ||
1261 | |||
1262 | if (sv->compat != vidh->compat) { | ||
1263 | ubi_err("bad compat %d", vidh->compat); | ||
1264 | goto bad_vid_hdr; | ||
1265 | } | ||
1266 | |||
1267 | if (seb->lnum != ubi32_to_cpu(vidh->lnum)) { | ||
1268 | ubi_err("bad lnum %d", seb->lnum); | ||
1269 | goto bad_vid_hdr; | ||
1270 | } | ||
1271 | |||
1272 | if (sv->used_ebs != ubi32_to_cpu(vidh->used_ebs)) { | ||
1273 | ubi_err("bad used_ebs %d", sv->used_ebs); | ||
1274 | goto bad_vid_hdr; | ||
1275 | } | ||
1276 | |||
1277 | if (sv->data_pad != ubi32_to_cpu(vidh->data_pad)) { | ||
1278 | ubi_err("bad data_pad %d", sv->data_pad); | ||
1279 | goto bad_vid_hdr; | ||
1280 | } | ||
1281 | |||
1282 | if (seb->leb_ver != ubi32_to_cpu(vidh->leb_ver)) { | ||
1283 | ubi_err("bad leb_ver %u", seb->leb_ver); | ||
1284 | goto bad_vid_hdr; | ||
1285 | } | ||
1286 | } | ||
1287 | |||
1288 | if (!last_seb) | ||
1289 | continue; | ||
1290 | |||
1291 | if (sv->highest_lnum != ubi32_to_cpu(vidh->lnum)) { | ||
1292 | ubi_err("bad highest_lnum %d", sv->highest_lnum); | ||
1293 | goto bad_vid_hdr; | ||
1294 | } | ||
1295 | |||
1296 | if (sv->last_data_size != ubi32_to_cpu(vidh->data_size)) { | ||
1297 | ubi_err("bad last_data_size %d", sv->last_data_size); | ||
1298 | goto bad_vid_hdr; | ||
1299 | } | ||
1300 | } | ||
1301 | |||
1302 | /* | ||
1303 | * Make sure that all the physical eraseblocks are in one of the lists | ||
1304 | * or trees. | ||
1305 | */ | ||
1306 | buf = kmalloc(ubi->peb_count, GFP_KERNEL); | ||
1307 | if (!buf) | ||
1308 | return -ENOMEM; | ||
1309 | |||
1310 | memset(buf, 1, ubi->peb_count); | ||
1311 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { | ||
1312 | err = ubi_io_is_bad(ubi, pnum); | ||
1313 | if (err < 0) | ||
1314 | return err; | ||
1315 | else if (err) | ||
1316 | buf[pnum] = 0; | ||
1317 | } | ||
1318 | |||
1319 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) | ||
1320 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) | ||
1321 | buf[seb->pnum] = 0; | ||
1322 | |||
1323 | list_for_each_entry(seb, &si->free, u.list) | ||
1324 | buf[seb->pnum] = 0; | ||
1325 | |||
1326 | list_for_each_entry(seb, &si->corr, u.list) | ||
1327 | buf[seb->pnum] = 0; | ||
1328 | |||
1329 | list_for_each_entry(seb, &si->erase, u.list) | ||
1330 | buf[seb->pnum] = 0; | ||
1331 | |||
1332 | list_for_each_entry(seb, &si->alien, u.list) | ||
1333 | buf[seb->pnum] = 0; | ||
1334 | |||
1335 | err = 0; | ||
1336 | for (pnum = 0; pnum < ubi->peb_count; pnum++) | ||
1337 | if (buf[pnum]) { | ||
1338 | ubi_err("PEB %d is not referred", pnum); | ||
1339 | err = 1; | ||
1340 | } | ||
1341 | |||
1342 | kfree(buf); | ||
1343 | if (err) | ||
1344 | goto out; | ||
1345 | return 0; | ||
1346 | |||
1347 | bad_seb: | ||
1348 | ubi_err("bad scanning information about LEB %d", seb->lnum); | ||
1349 | ubi_dbg_dump_seb(seb, 0); | ||
1350 | ubi_dbg_dump_sv(sv); | ||
1351 | goto out; | ||
1352 | |||
1353 | bad_sv: | ||
1354 | ubi_err("bad scanning information about volume %d", sv->vol_id); | ||
1355 | ubi_dbg_dump_sv(sv); | ||
1356 | goto out; | ||
1357 | |||
1358 | bad_vid_hdr: | ||
1359 | ubi_err("bad scanning information about volume %d", sv->vol_id); | ||
1360 | ubi_dbg_dump_sv(sv); | ||
1361 | ubi_dbg_dump_vid_hdr(vidh); | ||
1362 | |||
1363 | out: | ||
1364 | ubi_dbg_dump_stack(); | ||
1365 | return 1; | ||
1366 | } | ||
1367 | |||
1368 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ | ||
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h new file mode 100644 index 000000000000..3949f6192c76 --- /dev/null +++ b/drivers/mtd/ubi/scan.h | |||
@@ -0,0 +1,167 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | #ifndef __UBI_SCAN_H__ | ||
22 | #define __UBI_SCAN_H__ | ||
23 | |||
24 | /* The erase counter value for this physical eraseblock is unknown */ | ||
25 | #define UBI_SCAN_UNKNOWN_EC (-1) | ||
26 | |||
27 | /** | ||
28 | * struct ubi_scan_leb - scanning information about a physical eraseblock. | ||
29 | * @ec: erase counter (%UBI_SCAN_UNKNOWN_EC if it is unknown) | ||
30 | * @pnum: physical eraseblock number | ||
31 | * @lnum: logical eraseblock number | ||
32 | * @scrub: if this physical eraseblock needs scrubbing | ||
33 | * @sqnum: sequence number | ||
34 | * @u: unions RB-tree or @list links | ||
35 | * @u.rb: link in the per-volume RB-tree of &struct ubi_scan_leb objects | ||
36 | * @u.list: link in one of the eraseblock lists | ||
37 | * @leb_ver: logical eraseblock version (obsolete) | ||
38 | * | ||
39 | * One object of this type is allocated for each physical eraseblock during | ||
40 | * scanning. | ||
41 | */ | ||
42 | struct ubi_scan_leb { | ||
43 | int ec; | ||
44 | int pnum; | ||
45 | int lnum; | ||
46 | int scrub; | ||
47 | unsigned long long sqnum; | ||
48 | union { | ||
49 | struct rb_node rb; | ||
50 | struct list_head list; | ||
51 | } u; | ||
52 | uint32_t leb_ver; | ||
53 | }; | ||
54 | |||
55 | /** | ||
56 | * struct ubi_scan_volume - scanning information about a volume. | ||
57 | * @vol_id: volume ID | ||
58 | * @highest_lnum: highest logical eraseblock number in this volume | ||
59 | * @leb_count: number of logical eraseblocks in this volume | ||
60 | * @vol_type: volume type | ||
61 | * @used_ebs: number of used logical eraseblocks in this volume (only for | ||
62 | * static volumes) | ||
63 | * @last_data_size: amount of data in the last logical eraseblock of this | ||
64 | * volume (always equivalent to the usable logical eraseblock size in case of | ||
65 | * dynamic volumes) | ||
66 | * @data_pad: how many bytes at the end of logical eraseblocks of this volume | ||
67 | * are not used (due to volume alignment) | ||
68 | * @compat: compatibility flags of this volume | ||
69 | * @rb: link in the volume RB-tree | ||
70 | * @root: root of the RB-tree containing all the eraseblock belonging to this | ||
71 | * volume (&struct ubi_scan_leb objects) | ||
72 | * | ||
73 | * One object of this type is allocated for each volume during scanning. | ||
74 | */ | ||
75 | struct ubi_scan_volume { | ||
76 | int vol_id; | ||
77 | int highest_lnum; | ||
78 | int leb_count; | ||
79 | int vol_type; | ||
80 | int used_ebs; | ||
81 | int last_data_size; | ||
82 | int data_pad; | ||
83 | int compat; | ||
84 | struct rb_node rb; | ||
85 | struct rb_root root; | ||
86 | }; | ||
87 | |||
88 | /** | ||
89 | * struct ubi_scan_info - UBI scanning information. | ||
90 | * @volumes: root of the volume RB-tree | ||
91 | * @corr: list of corrupted physical eraseblocks | ||
92 | * @free: list of free physical eraseblocks | ||
93 | * @erase: list of physical eraseblocks which have to be erased | ||
94 | * @alien: list of physical eraseblocks which should not be used by UBI (e.g., | ||
95 | * @bad_peb_count: count of bad physical eraseblocks | ||
96 | * those belonging to "preserve"-compatible internal volumes) | ||
97 | * @vols_found: number of volumes found during scanning | ||
98 | * @highest_vol_id: highest volume ID | ||
99 | * @alien_peb_count: count of physical eraseblocks in the @alien list | ||
100 | * @is_empty: flag indicating whether the MTD device is empty or not | ||
101 | * @min_ec: lowest erase counter value | ||
102 | * @max_ec: highest erase counter value | ||
103 | * @max_sqnum: highest sequence number value | ||
104 | * @mean_ec: mean erase counter value | ||
105 | * @ec_sum: a temporary variable used when calculating @mean_ec | ||
106 | * @ec_count: a temporary variable used when calculating @mean_ec | ||
107 | * | ||
108 | * This data structure contains the result of scanning and may be used by other | ||
109 | * UBI units to build final UBI data structures, further error-recovery and so | ||
110 | * on. | ||
111 | */ | ||
112 | struct ubi_scan_info { | ||
113 | struct rb_root volumes; | ||
114 | struct list_head corr; | ||
115 | struct list_head free; | ||
116 | struct list_head erase; | ||
117 | struct list_head alien; | ||
118 | int bad_peb_count; | ||
119 | int vols_found; | ||
120 | int highest_vol_id; | ||
121 | int alien_peb_count; | ||
122 | int is_empty; | ||
123 | int min_ec; | ||
124 | int max_ec; | ||
125 | unsigned long long max_sqnum; | ||
126 | int mean_ec; | ||
127 | int ec_sum; | ||
128 | int ec_count; | ||
129 | }; | ||
130 | |||
131 | struct ubi_device; | ||
132 | struct ubi_vid_hdr; | ||
133 | |||
134 | /* | ||
135 | * ubi_scan_move_to_list - move a physical eraseblock from the volume tree to a | ||
136 | * list. | ||
137 | * | ||
138 | * @sv: volume scanning information | ||
139 | * @seb: scanning eraseblock infprmation | ||
140 | * @list: the list to move to | ||
141 | */ | ||
142 | static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv, | ||
143 | struct ubi_scan_leb *seb, | ||
144 | struct list_head *list) | ||
145 | { | ||
146 | rb_erase(&seb->u.rb, &sv->root); | ||
147 | list_add_tail(&seb->u.list, list); | ||
148 | } | ||
149 | |||
150 | int ubi_scan_add_to_list(struct ubi_scan_info *si, int pnum, int ec, | ||
151 | struct list_head *list); | ||
152 | int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si, | ||
153 | int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, | ||
154 | int bitflips); | ||
155 | struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si, | ||
156 | int vol_id); | ||
157 | struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, | ||
158 | int lnum); | ||
159 | void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv); | ||
160 | struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi, | ||
161 | struct ubi_scan_info *si); | ||
162 | int ubi_scan_erase_peb(const struct ubi_device *ubi, | ||
163 | const struct ubi_scan_info *si, int pnum, int ec); | ||
164 | struct ubi_scan_info *ubi_scan(struct ubi_device *ubi); | ||
165 | void ubi_scan_destroy_si(struct ubi_scan_info *si); | ||
166 | |||
167 | #endif /* !__UBI_SCAN_H__ */ | ||
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h new file mode 100644 index 000000000000..feb647f108f0 --- /dev/null +++ b/drivers/mtd/ubi/ubi.h | |||
@@ -0,0 +1,535 @@ | |||
1 | /* | ||
2 | * Copyright (c) International Business Machines Corp., 2006 | ||
3 | * Copyright (c) Nokia Corporation, 2006, 2007 | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
13 | * the GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * Author: Artem Bityutskiy (Битюцкий Артём) | ||
20 | */ | ||
21 | |||
22 | #ifndef __UBI_UBI_H__ | ||
23 | #define __UBI_UBI_H__ | ||
24 | |||
25 | #include <linux/init.h> | ||
26 | #include <linux/types.h> | ||
27 | #include <linux/list.h> | ||
28 | #include <linux/rbtree.h> | ||
29 | #include <linux/sched.h> | ||
30 | #include <linux/wait.h> | ||
31 | #include <linux/mutex.h> | ||
32 | #include <linux/rwsem.h> | ||
33 | #include <linux/spinlock.h> | ||
34 | #include <linux/fs.h> | ||
35 | #include <linux/cdev.h> | ||
36 | #include <linux/device.h> | ||
37 | #include <linux/string.h> | ||
38 | #include <linux/mtd/mtd.h> | ||
39 | |||
40 | #include <mtd/ubi-header.h> | ||
41 | #include <linux/mtd/ubi.h> | ||
42 | |||
43 | #include "scan.h" | ||
44 | #include "debug.h" | ||
45 | |||
46 | /* Maximum number of supported UBI devices */ | ||
47 | #define UBI_MAX_DEVICES 32 | ||
48 | |||
49 | /* UBI name used for character devices, sysfs, etc */ | ||
50 | #define UBI_NAME_STR "ubi" | ||
51 | |||
52 | /* Normal UBI messages */ | ||
53 | #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__) | ||
54 | /* UBI warning messages */ | ||
55 | #define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \ | ||
56 | __FUNCTION__, ##__VA_ARGS__) | ||
57 | /* UBI error messages */ | ||
58 | #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \ | ||
59 | __FUNCTION__, ##__VA_ARGS__) | ||
60 | |||
61 | /* Lowest number PEBs reserved for bad PEB handling */ | ||
62 | #define MIN_RESEVED_PEBS 2 | ||
63 | |||
64 | /* Background thread name pattern */ | ||
65 | #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd" | ||
66 | |||
67 | /* This marker in the EBA table means that the LEB is um-mapped */ | ||
68 | #define UBI_LEB_UNMAPPED -1 | ||
69 | |||
70 | /* | ||
71 | * In case of errors, UBI tries to repeat the operation several times before | ||
72 | * returning error. The below constant defines how many times UBI re-tries. | ||
73 | */ | ||
74 | #define UBI_IO_RETRIES 3 | ||
75 | |||
76 | /* | ||
77 | * Error codes returned by the I/O unit. | ||
78 | * | ||
79 | * UBI_IO_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only | ||
80 | * 0xFF bytes | ||
81 | * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a | ||
82 | * valid erase counter header, and the rest are %0xFF bytes | ||
83 | * UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC) | ||
84 | * UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or | ||
85 | * CRC) | ||
86 | * UBI_IO_BITFLIPS: bit-flips were detected and corrected | ||
87 | */ | ||
88 | enum { | ||
89 | UBI_IO_PEB_EMPTY = 1, | ||
90 | UBI_IO_PEB_FREE, | ||
91 | UBI_IO_BAD_EC_HDR, | ||
92 | UBI_IO_BAD_VID_HDR, | ||
93 | UBI_IO_BITFLIPS | ||
94 | }; | ||
95 | |||
96 | extern int ubi_devices_cnt; | ||
97 | extern struct ubi_device *ubi_devices[]; | ||
98 | |||
99 | struct ubi_volume_desc; | ||
100 | |||
101 | /** | ||
102 | * struct ubi_volume - UBI volume description data structure. | ||
103 | * @dev: device object to make use of the the Linux device model | ||
104 | * @cdev: character device object to create character device | ||
105 | * @ubi: reference to the UBI device description object | ||
106 | * @vol_id: volume ID | ||
107 | * @readers: number of users holding this volume in read-only mode | ||
108 | * @writers: number of users holding this volume in read-write mode | ||
109 | * @exclusive: whether somebody holds this volume in exclusive mode | ||
110 | * @removed: if the volume was removed | ||
111 | * @checked: if this static volume was checked | ||
112 | * | ||
113 | * @reserved_pebs: how many physical eraseblocks are reserved for this volume | ||
114 | * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) | ||
115 | * @usable_leb_size: logical eraseblock size without padding | ||
116 | * @used_ebs: how many logical eraseblocks in this volume contain data | ||
117 | * @last_eb_bytes: how many bytes are stored in the last logical eraseblock | ||
118 | * @used_bytes: how many bytes of data this volume contains | ||
119 | * @upd_marker: non-zero if the update marker is set for this volume | ||
120 | * @corrupted: non-zero if the volume is corrupted (static volumes only) | ||
121 | * @alignment: volume alignment | ||
122 | * @data_pad: how many bytes are not used at the end of physical eraseblocks to | ||
123 | * satisfy the requested alignment | ||
124 | * @name_len: volume name length | ||
125 | * @name: volume name | ||
126 | * | ||
127 | * @updating: whether the volume is being updated | ||
128 | * @upd_ebs: how many eraseblocks are expected to be updated | ||
129 | * @upd_bytes: how many bytes are expected to be received | ||
130 | * @upd_received: how many update bytes were already received | ||
131 | * @upd_buf: update buffer which is used to collect update data | ||
132 | * | ||
133 | * @eba_tbl: EBA table of this volume (LEB->PEB mapping) | ||
134 | * | ||
135 | * @gluebi_desc: gluebi UBI volume descriptor | ||
136 | * @gluebi_refcount: reference count of the gluebi MTD device | ||
137 | * @gluebi_mtd: MTD device description object of the gluebi MTD device | ||
138 | * | ||
139 | * The @corrupted field indicates that the volume's contents is corrupted. | ||
140 | * Since UBI protects only static volumes, this field is not relevant to | ||
141 | * dynamic volumes - it is user's responsibility to assure their data | ||
142 | * integrity. | ||
143 | * | ||
144 | * The @upd_marker flag indicates that this volume is either being updated at | ||
145 | * the moment or is damaged because of an unclean reboot. | ||
146 | */ | ||
147 | struct ubi_volume { | ||
148 | struct device dev; | ||
149 | struct cdev cdev; | ||
150 | struct ubi_device *ubi; | ||
151 | int vol_id; | ||
152 | int readers; | ||
153 | int writers; | ||
154 | int exclusive; | ||
155 | int removed; | ||
156 | int checked; | ||
157 | |||
158 | int reserved_pebs; | ||
159 | int vol_type; | ||
160 | int usable_leb_size; | ||
161 | int used_ebs; | ||
162 | int last_eb_bytes; | ||
163 | long long used_bytes; | ||
164 | int upd_marker; | ||
165 | int corrupted; | ||
166 | int alignment; | ||
167 | int data_pad; | ||
168 | int name_len; | ||
169 | char name[UBI_VOL_NAME_MAX+1]; | ||
170 | |||
171 | int updating; | ||
172 | int upd_ebs; | ||
173 | long long upd_bytes; | ||
174 | long long upd_received; | ||
175 | void *upd_buf; | ||
176 | |||
177 | int *eba_tbl; | ||
178 | |||
179 | #ifdef CONFIG_MTD_UBI_GLUEBI | ||
180 | /* Gluebi-related stuff may be compiled out */ | ||
181 | struct ubi_volume_desc *gluebi_desc; | ||
182 | int gluebi_refcount; | ||
183 | struct mtd_info gluebi_mtd; | ||
184 | #endif | ||
185 | }; | ||
186 | |||
187 | /** | ||
188 | * struct ubi_volume_desc - descriptor of the UBI volume returned when it is | ||
189 | * opened. | ||
190 | * @vol: reference to the corresponding volume description object | ||
191 | * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE) | ||
192 | */ | ||
193 | struct ubi_volume_desc { | ||
194 | struct ubi_volume *vol; | ||
195 | int mode; | ||
196 | }; | ||
197 | |||
198 | struct ubi_wl_entry; | ||
199 | |||
200 | /** | ||
201 | * struct ubi_device - UBI device description structure | ||
202 | * @dev: class device object to use the the Linux device model | ||
203 | * @cdev: character device object to create character device | ||
204 | * @ubi_num: UBI device number | ||
205 | * @ubi_name: UBI device name | ||
206 | * @major: character device major number | ||
207 | * @vol_count: number of volumes in this UBI device | ||
208 | * @volumes: volumes of this UBI device | ||
209 | * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs, | ||
210 | * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count, @vol->readers, | ||
211 | * @vol->writers, @vol->exclusive, @vol->removed, @vol->mapping and | ||
212 | * @vol->eba_tbl. | ||
213 | * | ||
214 | * @rsvd_pebs: count of reserved physical eraseblocks | ||
215 | * @avail_pebs: count of available physical eraseblocks | ||
216 | * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB | ||
217 | * handling | ||
218 | * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling | ||
219 | * | ||
220 | * @vtbl_slots: how many slots are available in the volume table | ||
221 | * @vtbl_size: size of the volume table in bytes | ||
222 | * @vtbl: in-RAM volume table copy | ||
223 | * | ||
224 | * @max_ec: current highest erase counter value | ||
225 | * @mean_ec: current mean erase counter value | ||
226 | * | ||
227 | * global_sqnum: global sequence number | ||
228 | * @ltree_lock: protects the lock tree and @global_sqnum | ||
229 | * @ltree: the lock tree | ||
230 | * @vtbl_mutex: protects on-flash volume table | ||
231 | * | ||
232 | * @used: RB-tree of used physical eraseblocks | ||
233 | * @free: RB-tree of free physical eraseblocks | ||
234 | * @scrub: RB-tree of physical eraseblocks which need scrubbing | ||
235 | * @prot: protection trees | ||
236 | * @prot.pnum: protection tree indexed by physical eraseblock numbers | ||
237 | * @prot.aec: protection tree indexed by absolute erase counter value | ||
238 | * @wl_lock: protects the @used, @free, @prot, @lookuptbl, @abs_ec, @move_from, | ||
239 | * @move_to, @move_to_put @erase_pending, @wl_scheduled, and @works | ||
240 | * fields | ||
241 | * @wl_scheduled: non-zero if the wear-leveling was scheduled | ||
242 | * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any | ||
243 | * physical eraseblock | ||
244 | * @abs_ec: absolute erase counter | ||
245 | * @move_from: physical eraseblock from where the data is being moved | ||
246 | * @move_to: physical eraseblock where the data is being moved to | ||
247 | * @move_from_put: if the "from" PEB was put | ||
248 | * @move_to_put: if the "to" PEB was put | ||
249 | * @works: list of pending works | ||
250 | * @works_count: count of pending works | ||
251 | * @bgt_thread: background thread description object | ||
252 | * @thread_enabled: if the background thread is enabled | ||
253 | * @bgt_name: background thread name | ||
254 | * | ||
255 | * @flash_size: underlying MTD device size (in bytes) | ||
256 | * @peb_count: count of physical eraseblocks on the MTD device | ||
257 | * @peb_size: physical eraseblock size | ||
258 | * @bad_peb_count: count of bad physical eraseblocks | ||
259 | * @good_peb_count: count of good physical eraseblocks | ||
260 | * @min_io_size: minimal input/output unit size of the underlying MTD device | ||
261 | * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers | ||
262 | * @ro_mode: if the UBI device is in read-only mode | ||
263 | * @leb_size: logical eraseblock size | ||
264 | * @leb_start: starting offset of logical eraseblocks within physical | ||
265 | * eraseblocks | ||
266 | * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size | ||
267 | * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size | ||
268 | * @vid_hdr_offset: starting offset of the volume identifier header (might be | ||
269 | * unaligned) | ||
270 | * @vid_hdr_aloffset: starting offset of the VID header aligned to | ||
271 | * @hdrs_min_io_size | ||
272 | * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset | ||
273 | * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or | ||
274 | * not | ||
275 | * @mtd: MTD device descriptor | ||
276 | */ | ||
277 | struct ubi_device { | ||
278 | struct cdev cdev; | ||
279 | struct device dev; | ||
280 | int ubi_num; | ||
281 | char ubi_name[sizeof(UBI_NAME_STR)+5]; | ||
282 | int major; | ||
283 | int vol_count; | ||
284 | struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT]; | ||
285 | spinlock_t volumes_lock; | ||
286 | |||
287 | int rsvd_pebs; | ||
288 | int avail_pebs; | ||
289 | int beb_rsvd_pebs; | ||
290 | int beb_rsvd_level; | ||
291 | |||
292 | int vtbl_slots; | ||
293 | int vtbl_size; | ||
294 | struct ubi_vtbl_record *vtbl; | ||
295 | struct mutex vtbl_mutex; | ||
296 | |||
297 | int max_ec; | ||
298 | int mean_ec; | ||
299 | |||
300 | /* EBA unit's stuff */ | ||
301 | unsigned long long global_sqnum; | ||
302 | spinlock_t ltree_lock; | ||
303 | struct rb_root ltree; | ||
304 | |||
305 | /* Wear-leveling unit's stuff */ | ||
306 | struct rb_root used; | ||
307 | struct rb_root free; | ||
308 | struct rb_root scrub; | ||
309 | struct { | ||
310 | struct rb_root pnum; | ||
311 | struct rb_root aec; | ||
312 | } prot; | ||
313 | spinlock_t wl_lock; | ||
314 | int wl_scheduled; | ||
315 | struct ubi_wl_entry **lookuptbl; | ||
316 | unsigned long long abs_ec; | ||
317 | struct ubi_wl_entry *move_from; | ||
318 | struct ubi_wl_entry *move_to; | ||
319 | int move_from_put; | ||
320 | int move_to_put; | ||
321 | struct list_head works; | ||
322 | int works_count; | ||
323 | struct task_struct *bgt_thread; | ||
324 | int thread_enabled; | ||
325 | char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2]; | ||
326 | |||
327 | /* I/O unit's stuff */ | ||
328 | long long flash_size; | ||
329 | int peb_count; | ||
330 | int peb_size; | ||
331 | int bad_peb_count; | ||
332 | int good_peb_count; | ||
333 | int min_io_size; | ||
334 | int hdrs_min_io_size; | ||
335 | int ro_mode; | ||
336 | int leb_size; | ||
337 | int leb_start; | ||
338 | int ec_hdr_alsize; | ||
339 | int vid_hdr_alsize; | ||
340 | int vid_hdr_offset; | ||
341 | int vid_hdr_aloffset; | ||
342 | int vid_hdr_shift; | ||
343 | int bad_allowed; | ||
344 | struct mtd_info *mtd; | ||
345 | }; | ||
346 | |||
347 | extern struct file_operations ubi_cdev_operations; | ||
348 | extern struct file_operations ubi_vol_cdev_operations; | ||
349 | extern struct class *ubi_class; | ||
350 | |||
351 | /* vtbl.c */ | ||
352 | int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, | ||
353 | struct ubi_vtbl_record *vtbl_rec); | ||
354 | int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si); | ||
355 | |||
356 | /* vmt.c */ | ||
357 | int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req); | ||
358 | int ubi_remove_volume(struct ubi_volume_desc *desc); | ||
359 | int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs); | ||
360 | int ubi_add_volume(struct ubi_device *ubi, int vol_id); | ||
361 | void ubi_free_volume(struct ubi_device *ubi, int vol_id); | ||
362 | |||
363 | /* upd.c */ | ||
364 | int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes); | ||
365 | int ubi_more_update_data(struct ubi_device *ubi, int vol_id, | ||
366 | const void __user *buf, int count); | ||
367 | |||
368 | /* misc.c */ | ||
369 | int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length); | ||
370 | int ubi_check_volume(struct ubi_device *ubi, int vol_id); | ||
371 | void ubi_calculate_reserved(struct ubi_device *ubi); | ||
372 | |||
373 | /* gluebi.c */ | ||
374 | #ifdef CONFIG_MTD_UBI_GLUEBI | ||
375 | int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol); | ||
376 | int ubi_destroy_gluebi(struct ubi_volume *vol); | ||
377 | #else | ||
378 | #define ubi_create_gluebi(ubi, vol) 0 | ||
379 | #define ubi_destroy_gluebi(vol) 0 | ||
380 | #endif | ||
381 | |||
382 | /* eba.c */ | ||
383 | int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum); | ||
384 | int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf, | ||
385 | int offset, int len, int check); | ||
386 | int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum, | ||
387 | const void *buf, int offset, int len, int dtype); | ||
388 | int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum, | ||
389 | const void *buf, int len, int dtype, | ||
390 | int used_ebs); | ||
391 | int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum, | ||
392 | const void *buf, int len, int dtype); | ||
393 | int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, | ||
394 | struct ubi_vid_hdr *vid_hdr); | ||
395 | int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si); | ||
396 | void ubi_eba_close(const struct ubi_device *ubi); | ||
397 | |||
398 | /* wl.c */ | ||
399 | int ubi_wl_get_peb(struct ubi_device *ubi, int dtype); | ||
400 | int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture); | ||
401 | int ubi_wl_flush(struct ubi_device *ubi); | ||
402 | int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum); | ||
403 | int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si); | ||
404 | void ubi_wl_close(struct ubi_device *ubi); | ||
405 | |||
406 | /* io.c */ | ||
407 | int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, | ||
408 | int len); | ||
409 | int ubi_io_write(const struct ubi_device *ubi, const void *buf, int pnum, | ||
410 | int offset, int len); | ||
411 | int ubi_io_sync_erase(const struct ubi_device *ubi, int pnum, int torture); | ||
412 | int ubi_io_is_bad(const struct ubi_device *ubi, int pnum); | ||
413 | int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum); | ||
414 | int ubi_io_read_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
415 | struct ubi_ec_hdr *ec_hdr, int verbose); | ||
416 | int ubi_io_write_ec_hdr(const struct ubi_device *ubi, int pnum, | ||
417 | struct ubi_ec_hdr *ec_hdr); | ||
418 | int ubi_io_read_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
419 | struct ubi_vid_hdr *vid_hdr, int verbose); | ||
420 | int ubi_io_write_vid_hdr(const struct ubi_device *ubi, int pnum, | ||
421 | struct ubi_vid_hdr *vid_hdr); | ||
422 | |||
423 | /* | ||
424 | * ubi_rb_for_each_entry - walk an RB-tree. | ||
425 | * @rb: a pointer to type 'struct rb_node' to to use as a loop counter | ||
426 | * @pos: a pointer to RB-tree entry type to use as a loop counter | ||
427 | * @root: RB-tree's root | ||
428 | * @member: the name of the 'struct rb_node' within the RB-tree entry | ||
429 | */ | ||
430 | #define ubi_rb_for_each_entry(rb, pos, root, member) \ | ||
431 | for (rb = rb_first(root), \ | ||
432 | pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \ | ||
433 | rb; \ | ||
434 | rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member)) | ||
435 | |||
436 | /** | ||
437 | * ubi_zalloc_vid_hdr - allocate a volume identifier header object. | ||
438 | * @ubi: UBI device description object | ||
439 | * | ||
440 | * This function returns a pointer to the newly allocated and zero-filled | ||
441 | * volume identifier header object in case of success and %NULL in case of | ||
442 | * failure. | ||
443 | */ | ||
444 | static inline struct ubi_vid_hdr *ubi_zalloc_vid_hdr(const struct ubi_device *ubi) | ||
445 | { | ||
446 | void *vid_hdr; | ||
447 | |||
448 | vid_hdr = kzalloc(ubi->vid_hdr_alsize, GFP_KERNEL); | ||
449 | if (!vid_hdr) | ||
450 | return NULL; | ||
451 | |||
452 | /* | ||
453 | * VID headers may be stored at un-aligned flash offsets, so we shift | ||
454 | * the pointer. | ||
455 | */ | ||
456 | return vid_hdr + ubi->vid_hdr_shift; | ||
457 | } | ||
458 | |||
459 | /** | ||
460 | * ubi_free_vid_hdr - free a volume identifier header object. | ||
461 | * @ubi: UBI device description object | ||
462 | * @vid_hdr: the object to free | ||
463 | */ | ||
464 | static inline void ubi_free_vid_hdr(const struct ubi_device *ubi, | ||
465 | struct ubi_vid_hdr *vid_hdr) | ||
466 | { | ||
467 | void *p = vid_hdr; | ||
468 | |||
469 | if (!p) | ||
470 | return; | ||
471 | |||
472 | kfree(p - ubi->vid_hdr_shift); | ||
473 | } | ||
474 | |||
475 | /* | ||
476 | * This function is equivalent to 'ubi_io_read()', but @offset is relative to | ||
477 | * the beginning of the logical eraseblock, not to the beginning of the | ||
478 | * physical eraseblock. | ||
479 | */ | ||
480 | static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf, | ||
481 | int pnum, int offset, int len) | ||
482 | { | ||
483 | ubi_assert(offset >= 0); | ||
484 | return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len); | ||
485 | } | ||
486 | |||
487 | /* | ||
488 | * This function is equivalent to 'ubi_io_write()', but @offset is relative to | ||
489 | * the beginning of the logical eraseblock, not to the beginning of the | ||
490 | * physical eraseblock. | ||
491 | */ | ||
492 | static inline int ubi_io_write_data(const struct ubi_device *ubi, const void *buf, | ||
493 | int pnum, int offset, int len) | ||
494 | { | ||
495 | ubi_assert(offset >= 0); | ||
496 | return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len); | ||
497 | } | ||
498 | |||
499 | /** | ||
500 | * ubi_ro_mode - switch to read-only mode. | ||
501 | * @ubi: UBI device description object | ||
502 | */ | ||
503 | static inline void ubi_ro_mode(struct ubi_device *ubi) | ||
504 | { | ||
505 | ubi->ro_mode = 1; | ||
506 | ubi_warn("switch to read-only mode"); | ||
507 | } | ||
508 | |||
509 | /** | ||
510 | * vol_id2idx - get table index by volume ID. | ||
511 | * @ubi: UBI device description object | ||
512 | * @vol_id: volume ID | ||
513 | */ | ||
514 | static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id) | ||
515 | { | ||
516 | if (vol_id >= UBI_INTERNAL_VOL_START) | ||
517 | return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots; | ||
518 | else | ||
519 | return vol_id; | ||
520 | } | ||
521 | |||
522 | /** | ||
523 | * idx2vol_id - get volume ID by table index. | ||
524 | * @ubi: UBI device description object | ||
525 | * @idx: table index | ||
526 | */ | ||
527 | static inline int idx2vol_id(const struct ubi_device *ubi, int idx) | ||
528 | { | ||
529 | if (idx >= ubi->vtbl_slots) | ||
530 | return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START; | ||
531 | else | ||
532 | return idx; | ||
533 | } | ||
534 | |||
535 | #endif /* !__UBI_UBI_H__ */ | ||
diff --git a/drivers/mtd/ubi/upd.c b/drivers/mtd/ubi/upd.c new file mode 100644 index 000000000000..8925b977e3dc --- /dev/null +++ b/drivers/mtd/ubi/upd.c | |||
@@ -0,0 +1,348 @@ | |||
1 | /* | ||
2 | * Copyright (c) International Business Machines Corp., 2006 | ||
3 | * Copyright (c) Nokia Corporation, 2006 | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
13 | * the GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * Author: Artem Bityutskiy (Битюцкий Артём) | ||
20 | * | ||
21 | * Jan 2007: Alexander Schmidt, hacked per-volume update. | ||
22 | */ | ||
23 | |||
24 | /* | ||
25 | * This file contains implementation of the volume update functionality. | ||
26 | * | ||
27 | * The update operation is based on the per-volume update marker which is | ||
28 | * stored in the volume table. The update marker is set before the update | ||
29 | * starts, and removed after the update has been finished. So if the update was | ||
30 | * interrupted by an unclean re-boot or due to some other reasons, the update | ||
31 | * marker stays on the flash media and UBI finds it when it attaches the MTD | ||
32 | * device next time. If the update marker is set for a volume, the volume is | ||
33 | * treated as damaged and most I/O operations are prohibited. Only a new update | ||
34 | * operation is allowed. | ||
35 | * | ||
36 | * Note, in general it is possible to implement the update operation as a | ||
37 | * transaction with a roll-back capability. | ||
38 | */ | ||
39 | |||
40 | #include <linux/err.h> | ||
41 | #include <asm/uaccess.h> | ||
42 | #include <asm/div64.h> | ||
43 | #include "ubi.h" | ||
44 | |||
45 | /** | ||
46 | * set_update_marker - set update marker. | ||
47 | * @ubi: UBI device description object | ||
48 | * @vol_id: volume ID | ||
49 | * | ||
50 | * This function sets the update marker flag for volume @vol_id. Returns zero | ||
51 | * in case of success and a negative error code in case of failure. | ||
52 | */ | ||
53 | static int set_update_marker(struct ubi_device *ubi, int vol_id) | ||
54 | { | ||
55 | int err; | ||
56 | struct ubi_vtbl_record vtbl_rec; | ||
57 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
58 | |||
59 | dbg_msg("set update marker for volume %d", vol_id); | ||
60 | |||
61 | if (vol->upd_marker) { | ||
62 | ubi_assert(ubi->vtbl[vol_id].upd_marker); | ||
63 | dbg_msg("already set"); | ||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record)); | ||
68 | vtbl_rec.upd_marker = 1; | ||
69 | |||
70 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); | ||
71 | vol->upd_marker = 1; | ||
72 | return err; | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * clear_update_marker - clear update marker. | ||
77 | * @ubi: UBI device description object | ||
78 | * @vol_id: volume ID | ||
79 | * @bytes: new data size in bytes | ||
80 | * | ||
81 | * This function clears the update marker for volume @vol_id, sets new volume | ||
82 | * data size and clears the "corrupted" flag (static volumes only). Returns | ||
83 | * zero in case of success and a negative error code in case of failure. | ||
84 | */ | ||
85 | static int clear_update_marker(struct ubi_device *ubi, int vol_id, long long bytes) | ||
86 | { | ||
87 | int err; | ||
88 | uint64_t tmp; | ||
89 | struct ubi_vtbl_record vtbl_rec; | ||
90 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
91 | |||
92 | dbg_msg("clear update marker for volume %d", vol_id); | ||
93 | |||
94 | memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record)); | ||
95 | ubi_assert(vol->upd_marker && vtbl_rec.upd_marker); | ||
96 | vtbl_rec.upd_marker = 0; | ||
97 | |||
98 | if (vol->vol_type == UBI_STATIC_VOLUME) { | ||
99 | vol->corrupted = 0; | ||
100 | vol->used_bytes = tmp = bytes; | ||
101 | vol->last_eb_bytes = do_div(tmp, vol->usable_leb_size); | ||
102 | vol->used_ebs = tmp; | ||
103 | if (vol->last_eb_bytes) | ||
104 | vol->used_ebs += 1; | ||
105 | else | ||
106 | vol->last_eb_bytes = vol->usable_leb_size; | ||
107 | } | ||
108 | |||
109 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); | ||
110 | vol->upd_marker = 0; | ||
111 | return err; | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * ubi_start_update - start volume update. | ||
116 | * @ubi: UBI device description object | ||
117 | * @vol_id: volume ID | ||
118 | * @bytes: update bytes | ||
119 | * | ||
120 | * This function starts volume update operation. If @bytes is zero, the volume | ||
121 | * is just wiped out. Returns zero in case of success and a negative error code | ||
122 | * in case of failure. | ||
123 | */ | ||
124 | int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes) | ||
125 | { | ||
126 | int i, err; | ||
127 | uint64_t tmp; | ||
128 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
129 | |||
130 | dbg_msg("start update of volume %d, %llu bytes", vol_id, bytes); | ||
131 | vol->updating = 1; | ||
132 | |||
133 | err = set_update_marker(ubi, vol_id); | ||
134 | if (err) | ||
135 | return err; | ||
136 | |||
137 | /* Before updating - wipe out the volume */ | ||
138 | for (i = 0; i < vol->reserved_pebs; i++) { | ||
139 | err = ubi_eba_unmap_leb(ubi, vol_id, i); | ||
140 | if (err) | ||
141 | return err; | ||
142 | } | ||
143 | |||
144 | if (bytes == 0) { | ||
145 | err = clear_update_marker(ubi, vol_id, 0); | ||
146 | if (err) | ||
147 | return err; | ||
148 | err = ubi_wl_flush(ubi); | ||
149 | if (!err) | ||
150 | vol->updating = 0; | ||
151 | } | ||
152 | |||
153 | vol->upd_buf = kmalloc(ubi->leb_size, GFP_KERNEL); | ||
154 | if (!vol->upd_buf) | ||
155 | return -ENOMEM; | ||
156 | |||
157 | tmp = bytes; | ||
158 | vol->upd_ebs = !!do_div(tmp, vol->usable_leb_size); | ||
159 | vol->upd_ebs += tmp; | ||
160 | vol->upd_bytes = bytes; | ||
161 | vol->upd_received = 0; | ||
162 | return 0; | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * write_leb - write update data. | ||
167 | * @ubi: UBI device description object | ||
168 | * @vol_id: volume ID | ||
169 | * @lnum: logical eraseblock number | ||
170 | * @buf: data to write | ||
171 | * @len: data size | ||
172 | * @used_ebs: how many logical eraseblocks will this volume contain (static | ||
173 | * volumes only) | ||
174 | * | ||
175 | * This function writes update data to corresponding logical eraseblock. In | ||
176 | * case of dynamic volume, this function checks if the data contains 0xFF bytes | ||
177 | * at the end. If yes, the 0xFF bytes are cut and not written. So if the whole | ||
178 | * buffer contains only 0xFF bytes, the LEB is left unmapped. | ||
179 | * | ||
180 | * The reason why we skip the trailing 0xFF bytes in case of dynamic volume is | ||
181 | * that we want to make sure that more data may be appended to the logical | ||
182 | * eraseblock in future. Indeed, writing 0xFF bytes may have side effects and | ||
183 | * this PEB won't be writable anymore. So if one writes the file-system image | ||
184 | * to the UBI volume where 0xFFs mean free space - UBI makes sure this free | ||
185 | * space is writable after the update. | ||
186 | * | ||
187 | * We do not do this for static volumes because they are read-only. But this | ||
188 | * also cannot be done because we have to store per-LEB CRC and the correct | ||
189 | * data length. | ||
190 | * | ||
191 | * This function returns zero in case of success and a negative error code in | ||
192 | * case of failure. | ||
193 | */ | ||
194 | static int write_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf, | ||
195 | int len, int used_ebs) | ||
196 | { | ||
197 | int err, l; | ||
198 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
199 | |||
200 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { | ||
201 | l = ALIGN(len, ubi->min_io_size); | ||
202 | memset(buf + len, 0xFF, l - len); | ||
203 | |||
204 | l = ubi_calc_data_len(ubi, buf, l); | ||
205 | if (l == 0) { | ||
206 | dbg_msg("all %d bytes contain 0xFF - skip", len); | ||
207 | return 0; | ||
208 | } | ||
209 | if (len != l) | ||
210 | dbg_msg("skip last %d bytes (0xFF)", len - l); | ||
211 | |||
212 | err = ubi_eba_write_leb(ubi, vol_id, lnum, buf, 0, l, | ||
213 | UBI_UNKNOWN); | ||
214 | } else { | ||
215 | /* | ||
216 | * When writing static volume, and this is the last logical | ||
217 | * eraseblock, the length (@len) does not have to be aligned to | ||
218 | * the minimal flash I/O unit. The 'ubi_eba_write_leb_st()' | ||
219 | * function accepts exact (unaligned) length and stores it in | ||
220 | * the VID header. And it takes care of proper alignment by | ||
221 | * padding the buffer. Here we just make sure the padding will | ||
222 | * contain zeros, not random trash. | ||
223 | */ | ||
224 | memset(buf + len, 0, vol->usable_leb_size - len); | ||
225 | err = ubi_eba_write_leb_st(ubi, vol_id, lnum, buf, len, | ||
226 | UBI_UNKNOWN, used_ebs); | ||
227 | } | ||
228 | |||
229 | return err; | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * ubi_more_update_data - write more update data. | ||
234 | * @vol: volume description object | ||
235 | * @buf: write data (user-space memory buffer) | ||
236 | * @count: how much bytes to write | ||
237 | * | ||
238 | * This function writes more data to the volume which is being updated. It may | ||
239 | * be called arbitrary number of times until all of the update data arrive. | ||
240 | * This function returns %0 in case of success, number of bytes written during | ||
241 | * the last call if the whole volume update was successfully finished, and a | ||
242 | * negative error code in case of failure. | ||
243 | */ | ||
244 | int ubi_more_update_data(struct ubi_device *ubi, int vol_id, | ||
245 | const void __user *buf, int count) | ||
246 | { | ||
247 | uint64_t tmp; | ||
248 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
249 | int lnum, offs, err = 0, len, to_write = count; | ||
250 | |||
251 | dbg_msg("write %d of %lld bytes, %lld already passed", | ||
252 | count, vol->upd_bytes, vol->upd_received); | ||
253 | |||
254 | if (ubi->ro_mode) | ||
255 | return -EROFS; | ||
256 | |||
257 | tmp = vol->upd_received; | ||
258 | offs = do_div(tmp, vol->usable_leb_size); | ||
259 | lnum = tmp; | ||
260 | |||
261 | if (vol->upd_received + count > vol->upd_bytes) | ||
262 | to_write = count = vol->upd_bytes - vol->upd_received; | ||
263 | |||
264 | /* | ||
265 | * When updating volumes, we accumulate whole logical eraseblock of | ||
266 | * data and write it at once. | ||
267 | */ | ||
268 | if (offs != 0) { | ||
269 | /* | ||
270 | * This is a write to the middle of the logical eraseblock. We | ||
271 | * copy the data to our update buffer and wait for more data or | ||
272 | * flush it if the whole eraseblock is written or the update | ||
273 | * is finished. | ||
274 | */ | ||
275 | |||
276 | len = vol->usable_leb_size - offs; | ||
277 | if (len > count) | ||
278 | len = count; | ||
279 | |||
280 | err = copy_from_user(vol->upd_buf + offs, buf, len); | ||
281 | if (err) | ||
282 | return -EFAULT; | ||
283 | |||
284 | if (offs + len == vol->usable_leb_size || | ||
285 | vol->upd_received + len == vol->upd_bytes) { | ||
286 | int flush_len = offs + len; | ||
287 | |||
288 | /* | ||
289 | * OK, we gathered either the whole eraseblock or this | ||
290 | * is the last chunk, it's time to flush the buffer. | ||
291 | */ | ||
292 | ubi_assert(flush_len <= vol->usable_leb_size); | ||
293 | err = write_leb(ubi, vol_id, lnum, vol->upd_buf, | ||
294 | flush_len, vol->upd_ebs); | ||
295 | if (err) | ||
296 | return err; | ||
297 | } | ||
298 | |||
299 | vol->upd_received += len; | ||
300 | count -= len; | ||
301 | buf += len; | ||
302 | lnum += 1; | ||
303 | } | ||
304 | |||
305 | /* | ||
306 | * If we've got more to write, let's continue. At this point we know we | ||
307 | * are starting from the beginning of an eraseblock. | ||
308 | */ | ||
309 | while (count) { | ||
310 | if (count > vol->usable_leb_size) | ||
311 | len = vol->usable_leb_size; | ||
312 | else | ||
313 | len = count; | ||
314 | |||
315 | err = copy_from_user(vol->upd_buf, buf, len); | ||
316 | if (err) | ||
317 | return -EFAULT; | ||
318 | |||
319 | if (len == vol->usable_leb_size || | ||
320 | vol->upd_received + len == vol->upd_bytes) { | ||
321 | err = write_leb(ubi, vol_id, lnum, vol->upd_buf, len, | ||
322 | vol->upd_ebs); | ||
323 | if (err) | ||
324 | break; | ||
325 | } | ||
326 | |||
327 | vol->upd_received += len; | ||
328 | count -= len; | ||
329 | lnum += 1; | ||
330 | buf += len; | ||
331 | } | ||
332 | |||
333 | ubi_assert(vol->upd_received <= vol->upd_bytes); | ||
334 | if (vol->upd_received == vol->upd_bytes) { | ||
335 | /* The update is finished, clear the update marker */ | ||
336 | err = clear_update_marker(ubi, vol_id, vol->upd_bytes); | ||
337 | if (err) | ||
338 | return err; | ||
339 | err = ubi_wl_flush(ubi); | ||
340 | if (err == 0) { | ||
341 | err = to_write; | ||
342 | kfree(vol->upd_buf); | ||
343 | vol->updating = 0; | ||
344 | } | ||
345 | } | ||
346 | |||
347 | return err; | ||
348 | } | ||
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c new file mode 100644 index 000000000000..622d0d18952c --- /dev/null +++ b/drivers/mtd/ubi/vmt.c | |||
@@ -0,0 +1,809 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * This file contains implementation of volume creation, deletion, updating and | ||
23 | * resizing. | ||
24 | */ | ||
25 | |||
26 | #include <linux/err.h> | ||
27 | #include <asm/div64.h> | ||
28 | #include "ubi.h" | ||
29 | |||
30 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
31 | static void paranoid_check_volumes(struct ubi_device *ubi); | ||
32 | #else | ||
33 | #define paranoid_check_volumes(ubi) | ||
34 | #endif | ||
35 | |||
36 | static ssize_t vol_attribute_show(struct device *dev, | ||
37 | struct device_attribute *attr, char *buf); | ||
38 | |||
39 | /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */ | ||
40 | static struct device_attribute vol_reserved_ebs = | ||
41 | __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL); | ||
42 | static struct device_attribute vol_type = | ||
43 | __ATTR(type, S_IRUGO, vol_attribute_show, NULL); | ||
44 | static struct device_attribute vol_name = | ||
45 | __ATTR(name, S_IRUGO, vol_attribute_show, NULL); | ||
46 | static struct device_attribute vol_corrupted = | ||
47 | __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL); | ||
48 | static struct device_attribute vol_alignment = | ||
49 | __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL); | ||
50 | static struct device_attribute vol_usable_eb_size = | ||
51 | __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL); | ||
52 | static struct device_attribute vol_data_bytes = | ||
53 | __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL); | ||
54 | static struct device_attribute vol_upd_marker = | ||
55 | __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL); | ||
56 | |||
57 | /* | ||
58 | * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'. | ||
59 | * | ||
60 | * Consider a situation: | ||
61 | * A. process 1 opens a sysfs file related to volume Y, say | ||
62 | * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs; | ||
63 | * B. process 2 removes volume Y; | ||
64 | * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file; | ||
65 | * | ||
66 | * What we want to do in a situation like that is to return error when the file | ||
67 | * is read. This is done by means of the 'removed' flag and the 'vol_lock' of | ||
68 | * the UBI volume description object. | ||
69 | */ | ||
70 | static ssize_t vol_attribute_show(struct device *dev, | ||
71 | struct device_attribute *attr, char *buf) | ||
72 | { | ||
73 | int ret; | ||
74 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); | ||
75 | |||
76 | spin_lock(&vol->ubi->volumes_lock); | ||
77 | if (vol->removed) { | ||
78 | spin_unlock(&vol->ubi->volumes_lock); | ||
79 | return -ENODEV; | ||
80 | } | ||
81 | if (attr == &vol_reserved_ebs) | ||
82 | ret = sprintf(buf, "%d\n", vol->reserved_pebs); | ||
83 | else if (attr == &vol_type) { | ||
84 | const char *tp; | ||
85 | tp = vol->vol_type == UBI_DYNAMIC_VOLUME ? "dynamic" : "static"; | ||
86 | ret = sprintf(buf, "%s\n", tp); | ||
87 | } else if (attr == &vol_name) | ||
88 | ret = sprintf(buf, "%s\n", vol->name); | ||
89 | else if (attr == &vol_corrupted) | ||
90 | ret = sprintf(buf, "%d\n", vol->corrupted); | ||
91 | else if (attr == &vol_alignment) | ||
92 | ret = sprintf(buf, "%d\n", vol->alignment); | ||
93 | else if (attr == &vol_usable_eb_size) { | ||
94 | ret = sprintf(buf, "%d\n", vol->usable_leb_size); | ||
95 | } else if (attr == &vol_data_bytes) | ||
96 | ret = sprintf(buf, "%lld\n", vol->used_bytes); | ||
97 | else if (attr == &vol_upd_marker) | ||
98 | ret = sprintf(buf, "%d\n", vol->upd_marker); | ||
99 | else | ||
100 | BUG(); | ||
101 | spin_unlock(&vol->ubi->volumes_lock); | ||
102 | return ret; | ||
103 | } | ||
104 | |||
105 | /* Release method for volume devices */ | ||
106 | static void vol_release(struct device *dev) | ||
107 | { | ||
108 | struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); | ||
109 | ubi_assert(vol->removed); | ||
110 | kfree(vol); | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * volume_sysfs_init - initialize sysfs for new volume. | ||
115 | * @ubi: UBI device description object | ||
116 | * @vol: volume description object | ||
117 | * | ||
118 | * This function returns zero in case of success and a negative error code in | ||
119 | * case of failure. | ||
120 | * | ||
121 | * Note, this function does not free allocated resources in case of failure - | ||
122 | * the caller does it. This is because this would cause release() here and the | ||
123 | * caller would oops. | ||
124 | */ | ||
125 | static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol) | ||
126 | { | ||
127 | int err; | ||
128 | |||
129 | err = device_create_file(&vol->dev, &vol_reserved_ebs); | ||
130 | if (err) | ||
131 | return err; | ||
132 | err = device_create_file(&vol->dev, &vol_type); | ||
133 | if (err) | ||
134 | return err; | ||
135 | err = device_create_file(&vol->dev, &vol_name); | ||
136 | if (err) | ||
137 | return err; | ||
138 | err = device_create_file(&vol->dev, &vol_corrupted); | ||
139 | if (err) | ||
140 | return err; | ||
141 | err = device_create_file(&vol->dev, &vol_alignment); | ||
142 | if (err) | ||
143 | return err; | ||
144 | err = device_create_file(&vol->dev, &vol_usable_eb_size); | ||
145 | if (err) | ||
146 | return err; | ||
147 | err = device_create_file(&vol->dev, &vol_data_bytes); | ||
148 | if (err) | ||
149 | return err; | ||
150 | err = device_create_file(&vol->dev, &vol_upd_marker); | ||
151 | if (err) | ||
152 | return err; | ||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * volume_sysfs_close - close sysfs for a volume. | ||
158 | * @vol: volume description object | ||
159 | */ | ||
160 | static void volume_sysfs_close(struct ubi_volume *vol) | ||
161 | { | ||
162 | device_remove_file(&vol->dev, &vol_upd_marker); | ||
163 | device_remove_file(&vol->dev, &vol_data_bytes); | ||
164 | device_remove_file(&vol->dev, &vol_usable_eb_size); | ||
165 | device_remove_file(&vol->dev, &vol_alignment); | ||
166 | device_remove_file(&vol->dev, &vol_corrupted); | ||
167 | device_remove_file(&vol->dev, &vol_name); | ||
168 | device_remove_file(&vol->dev, &vol_type); | ||
169 | device_remove_file(&vol->dev, &vol_reserved_ebs); | ||
170 | device_unregister(&vol->dev); | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * ubi_create_volume - create volume. | ||
175 | * @ubi: UBI device description object | ||
176 | * @req: volume creation request | ||
177 | * | ||
178 | * This function creates volume described by @req. If @req->vol_id id | ||
179 | * %UBI_VOL_NUM_AUTO, this function automatically assigne ID to the new volume | ||
180 | * and saves it in @req->vol_id. Returns zero in case of success and a negative | ||
181 | * error code in case of failure. | ||
182 | */ | ||
183 | int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req) | ||
184 | { | ||
185 | int i, err, vol_id = req->vol_id; | ||
186 | struct ubi_volume *vol; | ||
187 | struct ubi_vtbl_record vtbl_rec; | ||
188 | uint64_t bytes; | ||
189 | |||
190 | if (ubi->ro_mode) | ||
191 | return -EROFS; | ||
192 | |||
193 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); | ||
194 | if (!vol) | ||
195 | return -ENOMEM; | ||
196 | |||
197 | spin_lock(&ubi->volumes_lock); | ||
198 | |||
199 | if (vol_id == UBI_VOL_NUM_AUTO) { | ||
200 | /* Find unused volume ID */ | ||
201 | dbg_msg("search for vacant volume ID"); | ||
202 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
203 | if (!ubi->volumes[i]) { | ||
204 | vol_id = i; | ||
205 | break; | ||
206 | } | ||
207 | |||
208 | if (vol_id == UBI_VOL_NUM_AUTO) { | ||
209 | dbg_err("out of volume IDs"); | ||
210 | err = -ENFILE; | ||
211 | goto out_unlock; | ||
212 | } | ||
213 | req->vol_id = vol_id; | ||
214 | } | ||
215 | |||
216 | dbg_msg("volume ID %d, %llu bytes, type %d, name %s", | ||
217 | vol_id, (unsigned long long)req->bytes, | ||
218 | (int)req->vol_type, req->name); | ||
219 | |||
220 | /* Ensure that this volume does not exist */ | ||
221 | err = -EEXIST; | ||
222 | if (ubi->volumes[vol_id]) { | ||
223 | dbg_err("volume %d already exists", vol_id); | ||
224 | goto out_unlock; | ||
225 | } | ||
226 | |||
227 | /* Ensure that the name is unique */ | ||
228 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
229 | if (ubi->volumes[i] && | ||
230 | ubi->volumes[i]->name_len == req->name_len && | ||
231 | strcmp(ubi->volumes[i]->name, req->name) == 0) { | ||
232 | dbg_err("volume \"%s\" exists (ID %d)", req->name, i); | ||
233 | goto out_unlock; | ||
234 | } | ||
235 | |||
236 | /* Calculate how many eraseblocks are requested */ | ||
237 | vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment; | ||
238 | bytes = req->bytes; | ||
239 | if (do_div(bytes, vol->usable_leb_size)) | ||
240 | vol->reserved_pebs = 1; | ||
241 | vol->reserved_pebs += bytes; | ||
242 | |||
243 | /* Reserve physical eraseblocks */ | ||
244 | if (vol->reserved_pebs > ubi->avail_pebs) { | ||
245 | dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); | ||
246 | spin_unlock(&ubi->volumes_lock); | ||
247 | err = -ENOSPC; | ||
248 | goto out_unlock; | ||
249 | } | ||
250 | ubi->avail_pebs -= vol->reserved_pebs; | ||
251 | ubi->rsvd_pebs += vol->reserved_pebs; | ||
252 | |||
253 | vol->vol_id = vol_id; | ||
254 | vol->alignment = req->alignment; | ||
255 | vol->data_pad = ubi->leb_size % vol->alignment; | ||
256 | vol->vol_type = req->vol_type; | ||
257 | vol->name_len = req->name_len; | ||
258 | memcpy(vol->name, req->name, vol->name_len + 1); | ||
259 | vol->exclusive = 1; | ||
260 | vol->ubi = ubi; | ||
261 | ubi->volumes[vol_id] = vol; | ||
262 | spin_unlock(&ubi->volumes_lock); | ||
263 | |||
264 | /* | ||
265 | * Finish all pending erases because there may be some LEBs belonging | ||
266 | * to the same volume ID. | ||
267 | */ | ||
268 | err = ubi_wl_flush(ubi); | ||
269 | if (err) | ||
270 | goto out_acc; | ||
271 | |||
272 | vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL); | ||
273 | if (!vol->eba_tbl) { | ||
274 | err = -ENOMEM; | ||
275 | goto out_acc; | ||
276 | } | ||
277 | |||
278 | for (i = 0; i < vol->reserved_pebs; i++) | ||
279 | vol->eba_tbl[i] = UBI_LEB_UNMAPPED; | ||
280 | |||
281 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { | ||
282 | vol->used_ebs = vol->reserved_pebs; | ||
283 | vol->last_eb_bytes = vol->usable_leb_size; | ||
284 | vol->used_bytes = vol->used_ebs * vol->usable_leb_size; | ||
285 | } else { | ||
286 | bytes = vol->used_bytes; | ||
287 | vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size); | ||
288 | vol->used_ebs = bytes; | ||
289 | if (vol->last_eb_bytes) | ||
290 | vol->used_ebs += 1; | ||
291 | else | ||
292 | vol->last_eb_bytes = vol->usable_leb_size; | ||
293 | } | ||
294 | |||
295 | /* Register character device for the volume */ | ||
296 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); | ||
297 | vol->cdev.owner = THIS_MODULE; | ||
298 | err = cdev_add(&vol->cdev, MKDEV(ubi->major, vol_id + 1), 1); | ||
299 | if (err) { | ||
300 | ubi_err("cannot add character device for volume %d", vol_id); | ||
301 | goto out_mapping; | ||
302 | } | ||
303 | |||
304 | err = ubi_create_gluebi(ubi, vol); | ||
305 | if (err) | ||
306 | goto out_cdev; | ||
307 | |||
308 | vol->dev.release = vol_release; | ||
309 | vol->dev.parent = &ubi->dev; | ||
310 | vol->dev.devt = MKDEV(ubi->major, vol->vol_id + 1); | ||
311 | vol->dev.class = ubi_class; | ||
312 | sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id); | ||
313 | err = device_register(&vol->dev); | ||
314 | if (err) | ||
315 | goto out_gluebi; | ||
316 | |||
317 | err = volume_sysfs_init(ubi, vol); | ||
318 | if (err) | ||
319 | goto out_sysfs; | ||
320 | |||
321 | /* Fill volume table record */ | ||
322 | memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record)); | ||
323 | vtbl_rec.reserved_pebs = cpu_to_ubi32(vol->reserved_pebs); | ||
324 | vtbl_rec.alignment = cpu_to_ubi32(vol->alignment); | ||
325 | vtbl_rec.data_pad = cpu_to_ubi32(vol->data_pad); | ||
326 | vtbl_rec.name_len = cpu_to_ubi16(vol->name_len); | ||
327 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) | ||
328 | vtbl_rec.vol_type = UBI_VID_DYNAMIC; | ||
329 | else | ||
330 | vtbl_rec.vol_type = UBI_VID_STATIC; | ||
331 | memcpy(vtbl_rec.name, vol->name, vol->name_len + 1); | ||
332 | |||
333 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); | ||
334 | if (err) | ||
335 | goto out_sysfs; | ||
336 | |||
337 | spin_lock(&ubi->volumes_lock); | ||
338 | ubi->vol_count += 1; | ||
339 | vol->exclusive = 0; | ||
340 | spin_unlock(&ubi->volumes_lock); | ||
341 | |||
342 | paranoid_check_volumes(ubi); | ||
343 | return 0; | ||
344 | |||
345 | out_gluebi: | ||
346 | err = ubi_destroy_gluebi(vol); | ||
347 | out_cdev: | ||
348 | cdev_del(&vol->cdev); | ||
349 | out_mapping: | ||
350 | kfree(vol->eba_tbl); | ||
351 | out_acc: | ||
352 | spin_lock(&ubi->volumes_lock); | ||
353 | ubi->rsvd_pebs -= vol->reserved_pebs; | ||
354 | ubi->avail_pebs += vol->reserved_pebs; | ||
355 | out_unlock: | ||
356 | spin_unlock(&ubi->volumes_lock); | ||
357 | kfree(vol); | ||
358 | return err; | ||
359 | |||
360 | /* | ||
361 | * We are registered, so @vol is destroyed in the release function and | ||
362 | * we have to de-initialize differently. | ||
363 | */ | ||
364 | out_sysfs: | ||
365 | err = ubi_destroy_gluebi(vol); | ||
366 | cdev_del(&vol->cdev); | ||
367 | kfree(vol->eba_tbl); | ||
368 | spin_lock(&ubi->volumes_lock); | ||
369 | ubi->rsvd_pebs -= vol->reserved_pebs; | ||
370 | ubi->avail_pebs += vol->reserved_pebs; | ||
371 | spin_unlock(&ubi->volumes_lock); | ||
372 | volume_sysfs_close(vol); | ||
373 | return err; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * ubi_remove_volume - remove volume. | ||
378 | * @desc: volume descriptor | ||
379 | * | ||
380 | * This function removes volume described by @desc. The volume has to be opened | ||
381 | * in "exclusive" mode. Returns zero in case of success and a negative error | ||
382 | * code in case of failure. | ||
383 | */ | ||
384 | int ubi_remove_volume(struct ubi_volume_desc *desc) | ||
385 | { | ||
386 | struct ubi_volume *vol = desc->vol; | ||
387 | struct ubi_device *ubi = vol->ubi; | ||
388 | int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs; | ||
389 | |||
390 | dbg_msg("remove UBI volume %d", vol_id); | ||
391 | ubi_assert(desc->mode == UBI_EXCLUSIVE); | ||
392 | ubi_assert(vol == ubi->volumes[vol_id]); | ||
393 | |||
394 | if (ubi->ro_mode) | ||
395 | return -EROFS; | ||
396 | |||
397 | err = ubi_destroy_gluebi(vol); | ||
398 | if (err) | ||
399 | return err; | ||
400 | |||
401 | err = ubi_change_vtbl_record(ubi, vol_id, NULL); | ||
402 | if (err) | ||
403 | return err; | ||
404 | |||
405 | for (i = 0; i < vol->reserved_pebs; i++) { | ||
406 | err = ubi_eba_unmap_leb(ubi, vol_id, i); | ||
407 | if (err) | ||
408 | return err; | ||
409 | } | ||
410 | |||
411 | spin_lock(&ubi->volumes_lock); | ||
412 | vol->removed = 1; | ||
413 | ubi->volumes[vol_id] = NULL; | ||
414 | spin_unlock(&ubi->volumes_lock); | ||
415 | |||
416 | kfree(vol->eba_tbl); | ||
417 | vol->eba_tbl = NULL; | ||
418 | cdev_del(&vol->cdev); | ||
419 | volume_sysfs_close(vol); | ||
420 | kfree(desc); | ||
421 | |||
422 | spin_lock(&ubi->volumes_lock); | ||
423 | ubi->rsvd_pebs -= reserved_pebs; | ||
424 | ubi->avail_pebs += reserved_pebs; | ||
425 | i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; | ||
426 | if (i > 0) { | ||
427 | i = ubi->avail_pebs >= i ? i : ubi->avail_pebs; | ||
428 | ubi->avail_pebs -= i; | ||
429 | ubi->rsvd_pebs += i; | ||
430 | ubi->beb_rsvd_pebs += i; | ||
431 | if (i > 0) | ||
432 | ubi_msg("reserve more %d PEBs", i); | ||
433 | } | ||
434 | ubi->vol_count -= 1; | ||
435 | spin_unlock(&ubi->volumes_lock); | ||
436 | |||
437 | paranoid_check_volumes(ubi); | ||
438 | module_put(THIS_MODULE); | ||
439 | return 0; | ||
440 | } | ||
441 | |||
442 | /** | ||
443 | * ubi_resize_volume - re-size volume. | ||
444 | * @desc: volume descriptor | ||
445 | * @reserved_pebs: new size in physical eraseblocks | ||
446 | * | ||
447 | * This function returns zero in case of success, and a negative error code in | ||
448 | * case of failure. | ||
449 | */ | ||
450 | int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs) | ||
451 | { | ||
452 | int i, err, pebs, *new_mapping; | ||
453 | struct ubi_volume *vol = desc->vol; | ||
454 | struct ubi_device *ubi = vol->ubi; | ||
455 | struct ubi_vtbl_record vtbl_rec; | ||
456 | int vol_id = vol->vol_id; | ||
457 | |||
458 | if (ubi->ro_mode) | ||
459 | return -EROFS; | ||
460 | |||
461 | dbg_msg("re-size volume %d to from %d to %d PEBs", | ||
462 | vol_id, vol->reserved_pebs, reserved_pebs); | ||
463 | ubi_assert(desc->mode == UBI_EXCLUSIVE); | ||
464 | ubi_assert(vol == ubi->volumes[vol_id]); | ||
465 | |||
466 | if (vol->vol_type == UBI_STATIC_VOLUME && | ||
467 | reserved_pebs < vol->used_ebs) { | ||
468 | dbg_err("too small size %d, %d LEBs contain data", | ||
469 | reserved_pebs, vol->used_ebs); | ||
470 | return -EINVAL; | ||
471 | } | ||
472 | |||
473 | /* If the size is the same, we have nothing to do */ | ||
474 | if (reserved_pebs == vol->reserved_pebs) | ||
475 | return 0; | ||
476 | |||
477 | new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL); | ||
478 | if (!new_mapping) | ||
479 | return -ENOMEM; | ||
480 | |||
481 | for (i = 0; i < reserved_pebs; i++) | ||
482 | new_mapping[i] = UBI_LEB_UNMAPPED; | ||
483 | |||
484 | /* Reserve physical eraseblocks */ | ||
485 | pebs = reserved_pebs - vol->reserved_pebs; | ||
486 | if (pebs > 0) { | ||
487 | spin_lock(&ubi->volumes_lock); | ||
488 | if (pebs > ubi->avail_pebs) { | ||
489 | dbg_err("not enough PEBs: requested %d, available %d", | ||
490 | pebs, ubi->avail_pebs); | ||
491 | spin_unlock(&ubi->volumes_lock); | ||
492 | err = -ENOSPC; | ||
493 | goto out_free; | ||
494 | } | ||
495 | ubi->avail_pebs -= pebs; | ||
496 | ubi->rsvd_pebs += pebs; | ||
497 | for (i = 0; i < vol->reserved_pebs; i++) | ||
498 | new_mapping[i] = vol->eba_tbl[i]; | ||
499 | kfree(vol->eba_tbl); | ||
500 | vol->eba_tbl = new_mapping; | ||
501 | spin_unlock(&ubi->volumes_lock); | ||
502 | } | ||
503 | |||
504 | /* Change volume table record */ | ||
505 | memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record)); | ||
506 | vtbl_rec.reserved_pebs = cpu_to_ubi32(reserved_pebs); | ||
507 | err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); | ||
508 | if (err) | ||
509 | goto out_acc; | ||
510 | |||
511 | if (pebs < 0) { | ||
512 | for (i = 0; i < -pebs; i++) { | ||
513 | err = ubi_eba_unmap_leb(ubi, vol_id, reserved_pebs + i); | ||
514 | if (err) | ||
515 | goto out_acc; | ||
516 | } | ||
517 | spin_lock(&ubi->volumes_lock); | ||
518 | ubi->rsvd_pebs += pebs; | ||
519 | ubi->avail_pebs -= pebs; | ||
520 | pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; | ||
521 | if (pebs > 0) { | ||
522 | pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs; | ||
523 | ubi->avail_pebs -= pebs; | ||
524 | ubi->rsvd_pebs += pebs; | ||
525 | ubi->beb_rsvd_pebs += pebs; | ||
526 | if (pebs > 0) | ||
527 | ubi_msg("reserve more %d PEBs", pebs); | ||
528 | } | ||
529 | for (i = 0; i < reserved_pebs; i++) | ||
530 | new_mapping[i] = vol->eba_tbl[i]; | ||
531 | kfree(vol->eba_tbl); | ||
532 | vol->eba_tbl = new_mapping; | ||
533 | spin_unlock(&ubi->volumes_lock); | ||
534 | } | ||
535 | |||
536 | vol->reserved_pebs = reserved_pebs; | ||
537 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { | ||
538 | vol->used_ebs = reserved_pebs; | ||
539 | vol->last_eb_bytes = vol->usable_leb_size; | ||
540 | vol->used_bytes = vol->used_ebs * vol->usable_leb_size; | ||
541 | } | ||
542 | |||
543 | paranoid_check_volumes(ubi); | ||
544 | return 0; | ||
545 | |||
546 | out_acc: | ||
547 | if (pebs > 0) { | ||
548 | spin_lock(&ubi->volumes_lock); | ||
549 | ubi->rsvd_pebs -= pebs; | ||
550 | ubi->avail_pebs += pebs; | ||
551 | spin_unlock(&ubi->volumes_lock); | ||
552 | } | ||
553 | out_free: | ||
554 | kfree(new_mapping); | ||
555 | return err; | ||
556 | } | ||
557 | |||
558 | /** | ||
559 | * ubi_add_volume - add volume. | ||
560 | * @ubi: UBI device description object | ||
561 | * @vol_id: volume ID | ||
562 | * | ||
563 | * This function adds an existin volume and initializes all its data | ||
564 | * structures. Returnes zero in case of success and a negative error code in | ||
565 | * case of failure. | ||
566 | */ | ||
567 | int ubi_add_volume(struct ubi_device *ubi, int vol_id) | ||
568 | { | ||
569 | int err; | ||
570 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
571 | |||
572 | dbg_msg("add volume %d", vol_id); | ||
573 | ubi_dbg_dump_vol_info(vol); | ||
574 | ubi_assert(vol); | ||
575 | |||
576 | /* Register character device for the volume */ | ||
577 | cdev_init(&vol->cdev, &ubi_vol_cdev_operations); | ||
578 | vol->cdev.owner = THIS_MODULE; | ||
579 | err = cdev_add(&vol->cdev, MKDEV(ubi->major, vol->vol_id + 1), 1); | ||
580 | if (err) { | ||
581 | ubi_err("cannot add character device for volume %d", vol_id); | ||
582 | return err; | ||
583 | } | ||
584 | |||
585 | err = ubi_create_gluebi(ubi, vol); | ||
586 | if (err) | ||
587 | goto out_cdev; | ||
588 | |||
589 | vol->dev.release = vol_release; | ||
590 | vol->dev.parent = &ubi->dev; | ||
591 | vol->dev.devt = MKDEV(ubi->major, vol->vol_id + 1); | ||
592 | vol->dev.class = ubi_class; | ||
593 | sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id); | ||
594 | err = device_register(&vol->dev); | ||
595 | if (err) | ||
596 | goto out_gluebi; | ||
597 | |||
598 | err = volume_sysfs_init(ubi, vol); | ||
599 | if (err) { | ||
600 | cdev_del(&vol->cdev); | ||
601 | err = ubi_destroy_gluebi(vol); | ||
602 | volume_sysfs_close(vol); | ||
603 | return err; | ||
604 | } | ||
605 | |||
606 | paranoid_check_volumes(ubi); | ||
607 | return 0; | ||
608 | |||
609 | out_gluebi: | ||
610 | err = ubi_destroy_gluebi(vol); | ||
611 | out_cdev: | ||
612 | cdev_del(&vol->cdev); | ||
613 | return err; | ||
614 | } | ||
615 | |||
616 | /** | ||
617 | * ubi_free_volume - free volume. | ||
618 | * @ubi: UBI device description object | ||
619 | * @vol_id: volume ID | ||
620 | * | ||
621 | * This function frees all resources for volume @vol_id but does not remove it. | ||
622 | * Used only when the UBI device is detached. | ||
623 | */ | ||
624 | void ubi_free_volume(struct ubi_device *ubi, int vol_id) | ||
625 | { | ||
626 | int err; | ||
627 | struct ubi_volume *vol = ubi->volumes[vol_id]; | ||
628 | |||
629 | dbg_msg("free volume %d", vol_id); | ||
630 | ubi_assert(vol); | ||
631 | |||
632 | vol->removed = 1; | ||
633 | err = ubi_destroy_gluebi(vol); | ||
634 | ubi->volumes[vol_id] = NULL; | ||
635 | cdev_del(&vol->cdev); | ||
636 | volume_sysfs_close(vol); | ||
637 | } | ||
638 | |||
639 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
640 | |||
641 | /** | ||
642 | * paranoid_check_volume - check volume information. | ||
643 | * @ubi: UBI device description object | ||
644 | * @vol_id: volume ID | ||
645 | */ | ||
646 | static void paranoid_check_volume(const struct ubi_device *ubi, int vol_id) | ||
647 | { | ||
648 | int idx = vol_id2idx(ubi, vol_id); | ||
649 | int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker; | ||
650 | const struct ubi_volume *vol = ubi->volumes[idx]; | ||
651 | long long n; | ||
652 | const char *name; | ||
653 | |||
654 | reserved_pebs = ubi32_to_cpu(ubi->vtbl[vol_id].reserved_pebs); | ||
655 | |||
656 | if (!vol) { | ||
657 | if (reserved_pebs) { | ||
658 | ubi_err("no volume info, but volume exists"); | ||
659 | goto fail; | ||
660 | } | ||
661 | return; | ||
662 | } | ||
663 | |||
664 | if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 || | ||
665 | vol->name_len < 0) { | ||
666 | ubi_err("negative values"); | ||
667 | goto fail; | ||
668 | } | ||
669 | if (vol->alignment > ubi->leb_size || vol->alignment == 0) { | ||
670 | ubi_err("bad alignment"); | ||
671 | goto fail; | ||
672 | } | ||
673 | |||
674 | n = vol->alignment % ubi->min_io_size; | ||
675 | if (vol->alignment != 1 && n) { | ||
676 | ubi_err("alignment is not multiple of min I/O unit"); | ||
677 | goto fail; | ||
678 | } | ||
679 | |||
680 | n = ubi->leb_size % vol->alignment; | ||
681 | if (vol->data_pad != n) { | ||
682 | ubi_err("bad data_pad, has to be %lld", n); | ||
683 | goto fail; | ||
684 | } | ||
685 | |||
686 | if (vol->vol_type != UBI_DYNAMIC_VOLUME && | ||
687 | vol->vol_type != UBI_STATIC_VOLUME) { | ||
688 | ubi_err("bad vol_type"); | ||
689 | goto fail; | ||
690 | } | ||
691 | |||
692 | if (vol->upd_marker != 0 && vol->upd_marker != 1) { | ||
693 | ubi_err("bad upd_marker"); | ||
694 | goto fail; | ||
695 | } | ||
696 | |||
697 | if (vol->upd_marker && vol->corrupted) { | ||
698 | dbg_err("update marker and corrupted simultaneously"); | ||
699 | goto fail; | ||
700 | } | ||
701 | |||
702 | if (vol->reserved_pebs > ubi->good_peb_count) { | ||
703 | ubi_err("too large reserved_pebs"); | ||
704 | goto fail; | ||
705 | } | ||
706 | |||
707 | n = ubi->leb_size - vol->data_pad; | ||
708 | if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) { | ||
709 | ubi_err("bad usable_leb_size, has to be %lld", n); | ||
710 | goto fail; | ||
711 | } | ||
712 | |||
713 | if (vol->name_len > UBI_VOL_NAME_MAX) { | ||
714 | ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX); | ||
715 | goto fail; | ||
716 | } | ||
717 | |||
718 | if (!vol->name) { | ||
719 | ubi_err("NULL volume name"); | ||
720 | goto fail; | ||
721 | } | ||
722 | |||
723 | n = strnlen(vol->name, vol->name_len + 1); | ||
724 | if (n != vol->name_len) { | ||
725 | ubi_err("bad name_len %lld", n); | ||
726 | goto fail; | ||
727 | } | ||
728 | |||
729 | n = vol->used_ebs * vol->usable_leb_size; | ||
730 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { | ||
731 | if (vol->corrupted != 0) { | ||
732 | ubi_err("corrupted dynamic volume"); | ||
733 | goto fail; | ||
734 | } | ||
735 | if (vol->used_ebs != vol->reserved_pebs) { | ||
736 | ubi_err("bad used_ebs"); | ||
737 | goto fail; | ||
738 | } | ||
739 | if (vol->last_eb_bytes != vol->usable_leb_size) { | ||
740 | ubi_err("bad last_eb_bytes"); | ||
741 | goto fail; | ||
742 | } | ||
743 | if (vol->used_bytes != n) { | ||
744 | ubi_err("bad used_bytes"); | ||
745 | goto fail; | ||
746 | } | ||
747 | } else { | ||
748 | if (vol->corrupted != 0 && vol->corrupted != 1) { | ||
749 | ubi_err("bad corrupted"); | ||
750 | goto fail; | ||
751 | } | ||
752 | if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) { | ||
753 | ubi_err("bad used_ebs"); | ||
754 | goto fail; | ||
755 | } | ||
756 | if (vol->last_eb_bytes < 0 || | ||
757 | vol->last_eb_bytes > vol->usable_leb_size) { | ||
758 | ubi_err("bad last_eb_bytes"); | ||
759 | goto fail; | ||
760 | } | ||
761 | if (vol->used_bytes < 0 || vol->used_bytes > n || | ||
762 | vol->used_bytes < n - vol->usable_leb_size) { | ||
763 | ubi_err("bad used_bytes"); | ||
764 | goto fail; | ||
765 | } | ||
766 | } | ||
767 | |||
768 | alignment = ubi32_to_cpu(ubi->vtbl[vol_id].alignment); | ||
769 | data_pad = ubi32_to_cpu(ubi->vtbl[vol_id].data_pad); | ||
770 | name_len = ubi16_to_cpu(ubi->vtbl[vol_id].name_len); | ||
771 | upd_marker = ubi->vtbl[vol_id].upd_marker; | ||
772 | name = &ubi->vtbl[vol_id].name[0]; | ||
773 | if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC) | ||
774 | vol_type = UBI_DYNAMIC_VOLUME; | ||
775 | else | ||
776 | vol_type = UBI_STATIC_VOLUME; | ||
777 | |||
778 | if (alignment != vol->alignment || data_pad != vol->data_pad || | ||
779 | upd_marker != vol->upd_marker || vol_type != vol->vol_type || | ||
780 | name_len!= vol->name_len || strncmp(name, vol->name, name_len)) { | ||
781 | ubi_err("volume info is different"); | ||
782 | goto fail; | ||
783 | } | ||
784 | |||
785 | return; | ||
786 | |||
787 | fail: | ||
788 | ubi_err("paranoid check failed"); | ||
789 | ubi_dbg_dump_vol_info(vol); | ||
790 | ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id); | ||
791 | BUG(); | ||
792 | } | ||
793 | |||
794 | /** | ||
795 | * paranoid_check_volumes - check information about all volumes. | ||
796 | * @ubi: UBI device description object | ||
797 | */ | ||
798 | static void paranoid_check_volumes(struct ubi_device *ubi) | ||
799 | { | ||
800 | int i; | ||
801 | |||
802 | mutex_lock(&ubi->vtbl_mutex); | ||
803 | spin_lock(&ubi->volumes_lock); | ||
804 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
805 | paranoid_check_volume(ubi, i); | ||
806 | spin_unlock(&ubi->volumes_lock); | ||
807 | mutex_unlock(&ubi->vtbl_mutex); | ||
808 | } | ||
809 | #endif | ||
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c new file mode 100644 index 000000000000..b6fd6bbd941e --- /dev/null +++ b/drivers/mtd/ubi/vtbl.c | |||
@@ -0,0 +1,809 @@ | |||
1 | /* | ||
2 | * Copyright (c) International Business Machines Corp., 2006 | ||
3 | * Copyright (c) Nokia Corporation, 2006, 2007 | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | ||
13 | * the GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | * | ||
19 | * Author: Artem Bityutskiy (Битюцкий Артём) | ||
20 | */ | ||
21 | |||
22 | /* | ||
23 | * This file includes volume table manipulation code. The volume table is an | ||
24 | * on-flash table containing volume meta-data like name, number of reserved | ||
25 | * physical eraseblocks, type, etc. The volume table is stored in the so-called | ||
26 | * "layout volume". | ||
27 | * | ||
28 | * The layout volume is an internal volume which is organized as follows. It | ||
29 | * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical | ||
30 | * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each | ||
31 | * other. This redundancy guarantees robustness to unclean reboots. The volume | ||
32 | * table is basically an array of volume table records. Each record contains | ||
33 | * full information about the volume and protected by a CRC checksum. | ||
34 | * | ||
35 | * The volume table is changed, it is first changed in RAM. Then LEB 0 is | ||
36 | * erased, and the updated volume table is written back to LEB 0. Then same for | ||
37 | * LEB 1. This scheme guarantees recoverability from unclean reboots. | ||
38 | * | ||
39 | * In this UBI implementation the on-flash volume table does not contain any | ||
40 | * information about how many data static volumes contain. This information may | ||
41 | * be found from the scanning data. | ||
42 | * | ||
43 | * But it would still be beneficial to store this information in the volume | ||
44 | * table. For example, suppose we have a static volume X, and all its physical | ||
45 | * eraseblocks became bad for some reasons. Suppose we are attaching the | ||
46 | * corresponding MTD device, the scanning has found no logical eraseblocks | ||
47 | * corresponding to the volume X. According to the volume table volume X does | ||
48 | * exist. So we don't know whether it is just empty or all its physical | ||
49 | * eraseblocks went bad. So we cannot alarm the user about this corruption. | ||
50 | * | ||
51 | * The volume table also stores so-called "update marker", which is used for | ||
52 | * volume updates. Before updating the volume, the update marker is set, and | ||
53 | * after the update operation is finished, the update marker is cleared. So if | ||
54 | * the update operation was interrupted (e.g. by an unclean reboot) - the | ||
55 | * update marker is still there and we know that the volume's contents is | ||
56 | * damaged. | ||
57 | */ | ||
58 | |||
59 | #include <linux/crc32.h> | ||
60 | #include <linux/err.h> | ||
61 | #include <asm/div64.h> | ||
62 | #include "ubi.h" | ||
63 | |||
64 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
65 | static void paranoid_vtbl_check(const struct ubi_device *ubi); | ||
66 | #else | ||
67 | #define paranoid_vtbl_check(ubi) | ||
68 | #endif | ||
69 | |||
70 | /* Empty volume table record */ | ||
71 | static struct ubi_vtbl_record empty_vtbl_record; | ||
72 | |||
73 | /** | ||
74 | * ubi_change_vtbl_record - change volume table record. | ||
75 | * @ubi: UBI device description object | ||
76 | * @idx: table index to change | ||
77 | * @vtbl_rec: new volume table record | ||
78 | * | ||
79 | * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty | ||
80 | * volume table record is written. The caller does not have to calculate CRC of | ||
81 | * the record as it is done by this function. Returns zero in case of success | ||
82 | * and a negative error code in case of failure. | ||
83 | */ | ||
84 | int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, | ||
85 | struct ubi_vtbl_record *vtbl_rec) | ||
86 | { | ||
87 | int i, err; | ||
88 | uint32_t crc; | ||
89 | |||
90 | ubi_assert(idx >= 0 && idx < ubi->vtbl_slots); | ||
91 | |||
92 | if (!vtbl_rec) | ||
93 | vtbl_rec = &empty_vtbl_record; | ||
94 | else { | ||
95 | crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC); | ||
96 | vtbl_rec->crc = cpu_to_ubi32(crc); | ||
97 | } | ||
98 | |||
99 | dbg_msg("change record %d", idx); | ||
100 | ubi_dbg_dump_vtbl_record(vtbl_rec, idx); | ||
101 | |||
102 | mutex_lock(&ubi->vtbl_mutex); | ||
103 | memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); | ||
104 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { | ||
105 | err = ubi_eba_unmap_leb(ubi, UBI_LAYOUT_VOL_ID, i); | ||
106 | if (err) { | ||
107 | mutex_unlock(&ubi->vtbl_mutex); | ||
108 | return err; | ||
109 | } | ||
110 | err = ubi_eba_write_leb(ubi, UBI_LAYOUT_VOL_ID, i, ubi->vtbl, 0, | ||
111 | ubi->vtbl_size, UBI_LONGTERM); | ||
112 | if (err) { | ||
113 | mutex_unlock(&ubi->vtbl_mutex); | ||
114 | return err; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | paranoid_vtbl_check(ubi); | ||
119 | mutex_unlock(&ubi->vtbl_mutex); | ||
120 | return ubi_wl_flush(ubi); | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * vol_til_check - check if volume table is not corrupted and contains sensible | ||
125 | * data. | ||
126 | * | ||
127 | * @ubi: UBI device description object | ||
128 | * @vtbl: volume table | ||
129 | * | ||
130 | * This function returns zero if @vtbl is all right, %1 if CRC is incorrect, | ||
131 | * and %-EINVAL if it contains inconsistent data. | ||
132 | */ | ||
133 | static int vtbl_check(const struct ubi_device *ubi, | ||
134 | const struct ubi_vtbl_record *vtbl) | ||
135 | { | ||
136 | int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len; | ||
137 | int upd_marker; | ||
138 | uint32_t crc; | ||
139 | const char *name; | ||
140 | |||
141 | for (i = 0; i < ubi->vtbl_slots; i++) { | ||
142 | cond_resched(); | ||
143 | |||
144 | reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs); | ||
145 | alignment = ubi32_to_cpu(vtbl[i].alignment); | ||
146 | data_pad = ubi32_to_cpu(vtbl[i].data_pad); | ||
147 | upd_marker = vtbl[i].upd_marker; | ||
148 | vol_type = vtbl[i].vol_type; | ||
149 | name_len = ubi16_to_cpu(vtbl[i].name_len); | ||
150 | name = &vtbl[i].name[0]; | ||
151 | |||
152 | crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC); | ||
153 | if (ubi32_to_cpu(vtbl[i].crc) != crc) { | ||
154 | ubi_err("bad CRC at record %u: %#08x, not %#08x", | ||
155 | i, crc, ubi32_to_cpu(vtbl[i].crc)); | ||
156 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); | ||
157 | return 1; | ||
158 | } | ||
159 | |||
160 | if (reserved_pebs == 0) { | ||
161 | if (memcmp(&vtbl[i], &empty_vtbl_record, | ||
162 | UBI_VTBL_RECORD_SIZE)) { | ||
163 | dbg_err("bad empty record"); | ||
164 | goto bad; | ||
165 | } | ||
166 | continue; | ||
167 | } | ||
168 | |||
169 | if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 || | ||
170 | name_len < 0) { | ||
171 | dbg_err("negative values"); | ||
172 | goto bad; | ||
173 | } | ||
174 | |||
175 | if (alignment > ubi->leb_size || alignment == 0) { | ||
176 | dbg_err("bad alignment"); | ||
177 | goto bad; | ||
178 | } | ||
179 | |||
180 | n = alignment % ubi->min_io_size; | ||
181 | if (alignment != 1 && n) { | ||
182 | dbg_err("alignment is not multiple of min I/O unit"); | ||
183 | goto bad; | ||
184 | } | ||
185 | |||
186 | n = ubi->leb_size % alignment; | ||
187 | if (data_pad != n) { | ||
188 | dbg_err("bad data_pad, has to be %d", n); | ||
189 | goto bad; | ||
190 | } | ||
191 | |||
192 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { | ||
193 | dbg_err("bad vol_type"); | ||
194 | goto bad; | ||
195 | } | ||
196 | |||
197 | if (upd_marker != 0 && upd_marker != 1) { | ||
198 | dbg_err("bad upd_marker"); | ||
199 | goto bad; | ||
200 | } | ||
201 | |||
202 | if (reserved_pebs > ubi->good_peb_count) { | ||
203 | dbg_err("too large reserved_pebs, good PEBs %d", | ||
204 | ubi->good_peb_count); | ||
205 | goto bad; | ||
206 | } | ||
207 | |||
208 | if (name_len > UBI_VOL_NAME_MAX) { | ||
209 | dbg_err("too long volume name, max %d", | ||
210 | UBI_VOL_NAME_MAX); | ||
211 | goto bad; | ||
212 | } | ||
213 | |||
214 | if (name[0] == '\0') { | ||
215 | dbg_err("NULL volume name"); | ||
216 | goto bad; | ||
217 | } | ||
218 | |||
219 | if (name_len != strnlen(name, name_len + 1)) { | ||
220 | dbg_err("bad name_len"); | ||
221 | goto bad; | ||
222 | } | ||
223 | } | ||
224 | |||
225 | /* Checks that all names are unique */ | ||
226 | for (i = 0; i < ubi->vtbl_slots - 1; i++) { | ||
227 | for (n = i + 1; n < ubi->vtbl_slots; n++) { | ||
228 | int len1 = ubi16_to_cpu(vtbl[i].name_len); | ||
229 | int len2 = ubi16_to_cpu(vtbl[n].name_len); | ||
230 | |||
231 | if (len1 > 0 && len1 == len2 && | ||
232 | !strncmp(vtbl[i].name, vtbl[n].name, len1)) { | ||
233 | ubi_err("volumes %d and %d have the same name" | ||
234 | " \"%s\"", i, n, vtbl[i].name); | ||
235 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); | ||
236 | ubi_dbg_dump_vtbl_record(&vtbl[n], n); | ||
237 | return -EINVAL; | ||
238 | } | ||
239 | } | ||
240 | } | ||
241 | |||
242 | return 0; | ||
243 | |||
244 | bad: | ||
245 | ubi_err("volume table check failed, record %d", i); | ||
246 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); | ||
247 | return -EINVAL; | ||
248 | } | ||
249 | |||
250 | /** | ||
251 | * create_vtbl - create a copy of volume table. | ||
252 | * @ubi: UBI device description object | ||
253 | * @si: scanning information | ||
254 | * @copy: number of the volume table copy | ||
255 | * @vtbl: contents of the volume table | ||
256 | * | ||
257 | * This function returns zero in case of success and a negative error code in | ||
258 | * case of failure. | ||
259 | */ | ||
260 | static int create_vtbl(const struct ubi_device *ubi, struct ubi_scan_info *si, | ||
261 | int copy, void *vtbl) | ||
262 | { | ||
263 | int err, tries = 0; | ||
264 | static struct ubi_vid_hdr *vid_hdr; | ||
265 | struct ubi_scan_volume *sv; | ||
266 | struct ubi_scan_leb *new_seb, *old_seb = NULL; | ||
267 | |||
268 | ubi_msg("create volume table (copy #%d)", copy + 1); | ||
269 | |||
270 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
271 | if (!vid_hdr) | ||
272 | return -ENOMEM; | ||
273 | |||
274 | /* | ||
275 | * Check if there is a logical eraseblock which would have to contain | ||
276 | * this volume table copy was found during scanning. It has to be wiped | ||
277 | * out. | ||
278 | */ | ||
279 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID); | ||
280 | if (sv) | ||
281 | old_seb = ubi_scan_find_seb(sv, copy); | ||
282 | |||
283 | retry: | ||
284 | new_seb = ubi_scan_get_free_peb(ubi, si); | ||
285 | if (IS_ERR(new_seb)) { | ||
286 | err = PTR_ERR(new_seb); | ||
287 | goto out_free; | ||
288 | } | ||
289 | |||
290 | vid_hdr->vol_type = UBI_VID_DYNAMIC; | ||
291 | vid_hdr->vol_id = cpu_to_ubi32(UBI_LAYOUT_VOL_ID); | ||
292 | vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; | ||
293 | vid_hdr->data_size = vid_hdr->used_ebs = | ||
294 | vid_hdr->data_pad = cpu_to_ubi32(0); | ||
295 | vid_hdr->lnum = cpu_to_ubi32(copy); | ||
296 | vid_hdr->sqnum = cpu_to_ubi64(++si->max_sqnum); | ||
297 | vid_hdr->leb_ver = cpu_to_ubi32(old_seb ? old_seb->leb_ver + 1: 0); | ||
298 | |||
299 | /* The EC header is already there, write the VID header */ | ||
300 | err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); | ||
301 | if (err) | ||
302 | goto write_error; | ||
303 | |||
304 | /* Write the layout volume contents */ | ||
305 | err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size); | ||
306 | if (err) | ||
307 | goto write_error; | ||
308 | |||
309 | /* | ||
310 | * And add it to the scanning information. Don't delete the old | ||
311 | * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'. | ||
312 | */ | ||
313 | err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec, | ||
314 | vid_hdr, 0); | ||
315 | kfree(new_seb); | ||
316 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
317 | return err; | ||
318 | |||
319 | write_error: | ||
320 | kfree(new_seb); | ||
321 | /* May be this physical eraseblock went bad, try to pick another one */ | ||
322 | if (++tries <= 5) { | ||
323 | err = ubi_scan_add_to_list(si, new_seb->pnum, new_seb->ec, | ||
324 | &si->corr); | ||
325 | if (!err) | ||
326 | goto retry; | ||
327 | } | ||
328 | out_free: | ||
329 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
330 | return err; | ||
331 | |||
332 | } | ||
333 | |||
334 | /** | ||
335 | * process_lvol - process the layout volume. | ||
336 | * @ubi: UBI device description object | ||
337 | * @si: scanning information | ||
338 | * @sv: layout volume scanning information | ||
339 | * | ||
340 | * This function is responsible for reading the layout volume, ensuring it is | ||
341 | * not corrupted, and recovering from corruptions if needed. Returns volume | ||
342 | * table in case of success and a negative error code in case of failure. | ||
343 | */ | ||
344 | static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi, | ||
345 | struct ubi_scan_info *si, | ||
346 | struct ubi_scan_volume *sv) | ||
347 | { | ||
348 | int err; | ||
349 | struct rb_node *rb; | ||
350 | struct ubi_scan_leb *seb; | ||
351 | struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL }; | ||
352 | int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1}; | ||
353 | |||
354 | /* | ||
355 | * UBI goes through the following steps when it changes the layout | ||
356 | * volume: | ||
357 | * a. erase LEB 0; | ||
358 | * b. write new data to LEB 0; | ||
359 | * c. erase LEB 1; | ||
360 | * d. write new data to LEB 1. | ||
361 | * | ||
362 | * Before the change, both LEBs contain the same data. | ||
363 | * | ||
364 | * Due to unclean reboots, the contents of LEB 0 may be lost, but there | ||
365 | * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not. | ||
366 | * Similarly, LEB 1 may be lost, but there should be LEB 0. And | ||
367 | * finally, unclean reboots may result in a situation when neither LEB | ||
368 | * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB | ||
369 | * 0 contains more recent information. | ||
370 | * | ||
371 | * So the plan is to first check LEB 0. Then | ||
372 | * a. if LEB 0 is OK, it must be containing the most resent data; then | ||
373 | * we compare it with LEB 1, and if they are different, we copy LEB | ||
374 | * 0 to LEB 1; | ||
375 | * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1 | ||
376 | * to LEB 0. | ||
377 | */ | ||
378 | |||
379 | dbg_msg("check layout volume"); | ||
380 | |||
381 | /* Read both LEB 0 and LEB 1 into memory */ | ||
382 | ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { | ||
383 | leb[seb->lnum] = kzalloc(ubi->vtbl_size, GFP_KERNEL); | ||
384 | if (!leb[seb->lnum]) { | ||
385 | err = -ENOMEM; | ||
386 | goto out_free; | ||
387 | } | ||
388 | |||
389 | err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, | ||
390 | ubi->vtbl_size); | ||
391 | if (err == UBI_IO_BITFLIPS || err == -EBADMSG) | ||
392 | /* Scrub the PEB later */ | ||
393 | seb->scrub = 1; | ||
394 | else if (err) | ||
395 | goto out_free; | ||
396 | } | ||
397 | |||
398 | err = -EINVAL; | ||
399 | if (leb[0]) { | ||
400 | leb_corrupted[0] = vtbl_check(ubi, leb[0]); | ||
401 | if (leb_corrupted[0] < 0) | ||
402 | goto out_free; | ||
403 | } | ||
404 | |||
405 | if (!leb_corrupted[0]) { | ||
406 | /* LEB 0 is OK */ | ||
407 | if (leb[1]) | ||
408 | leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size); | ||
409 | if (leb_corrupted[1]) { | ||
410 | ubi_warn("volume table copy #2 is corrupted"); | ||
411 | err = create_vtbl(ubi, si, 1, leb[0]); | ||
412 | if (err) | ||
413 | goto out_free; | ||
414 | ubi_msg("volume table was restored"); | ||
415 | } | ||
416 | |||
417 | /* Both LEB 1 and LEB 2 are OK and consistent */ | ||
418 | kfree(leb[1]); | ||
419 | return leb[0]; | ||
420 | } else { | ||
421 | /* LEB 0 is corrupted or does not exist */ | ||
422 | if (leb[1]) { | ||
423 | leb_corrupted[1] = vtbl_check(ubi, leb[1]); | ||
424 | if (leb_corrupted[1] < 0) | ||
425 | goto out_free; | ||
426 | } | ||
427 | if (leb_corrupted[1]) { | ||
428 | /* Both LEB 0 and LEB 1 are corrupted */ | ||
429 | ubi_err("both volume tables are corrupted"); | ||
430 | goto out_free; | ||
431 | } | ||
432 | |||
433 | ubi_warn("volume table copy #1 is corrupted"); | ||
434 | err = create_vtbl(ubi, si, 0, leb[1]); | ||
435 | if (err) | ||
436 | goto out_free; | ||
437 | ubi_msg("volume table was restored"); | ||
438 | |||
439 | kfree(leb[0]); | ||
440 | return leb[1]; | ||
441 | } | ||
442 | |||
443 | out_free: | ||
444 | kfree(leb[0]); | ||
445 | kfree(leb[1]); | ||
446 | return ERR_PTR(err); | ||
447 | } | ||
448 | |||
449 | /** | ||
450 | * create_empty_lvol - create empty layout volume. | ||
451 | * @ubi: UBI device description object | ||
452 | * @si: scanning information | ||
453 | * | ||
454 | * This function returns volume table contents in case of success and a | ||
455 | * negative error code in case of failure. | ||
456 | */ | ||
457 | static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi, | ||
458 | struct ubi_scan_info *si) | ||
459 | { | ||
460 | int i; | ||
461 | struct ubi_vtbl_record *vtbl; | ||
462 | |||
463 | vtbl = kzalloc(ubi->vtbl_size, GFP_KERNEL); | ||
464 | if (!vtbl) | ||
465 | return ERR_PTR(-ENOMEM); | ||
466 | |||
467 | for (i = 0; i < ubi->vtbl_slots; i++) | ||
468 | memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); | ||
469 | |||
470 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { | ||
471 | int err; | ||
472 | |||
473 | err = create_vtbl(ubi, si, i, vtbl); | ||
474 | if (err) { | ||
475 | kfree(vtbl); | ||
476 | return ERR_PTR(err); | ||
477 | } | ||
478 | } | ||
479 | |||
480 | return vtbl; | ||
481 | } | ||
482 | |||
483 | /** | ||
484 | * init_volumes - initialize volume information for existing volumes. | ||
485 | * @ubi: UBI device description object | ||
486 | * @si: scanning information | ||
487 | * @vtbl: volume table | ||
488 | * | ||
489 | * This function allocates volume description objects for existing volumes. | ||
490 | * Returns zero in case of success and a negative error code in case of | ||
491 | * failure. | ||
492 | */ | ||
493 | static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si, | ||
494 | const struct ubi_vtbl_record *vtbl) | ||
495 | { | ||
496 | int i, reserved_pebs = 0; | ||
497 | struct ubi_scan_volume *sv; | ||
498 | struct ubi_volume *vol; | ||
499 | |||
500 | for (i = 0; i < ubi->vtbl_slots; i++) { | ||
501 | cond_resched(); | ||
502 | |||
503 | if (ubi32_to_cpu(vtbl[i].reserved_pebs) == 0) | ||
504 | continue; /* Empty record */ | ||
505 | |||
506 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); | ||
507 | if (!vol) | ||
508 | return -ENOMEM; | ||
509 | |||
510 | vol->reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs); | ||
511 | vol->alignment = ubi32_to_cpu(vtbl[i].alignment); | ||
512 | vol->data_pad = ubi32_to_cpu(vtbl[i].data_pad); | ||
513 | vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? | ||
514 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; | ||
515 | vol->name_len = ubi16_to_cpu(vtbl[i].name_len); | ||
516 | vol->usable_leb_size = ubi->leb_size - vol->data_pad; | ||
517 | memcpy(vol->name, vtbl[i].name, vol->name_len); | ||
518 | vol->name[vol->name_len] = '\0'; | ||
519 | vol->vol_id = i; | ||
520 | |||
521 | ubi_assert(!ubi->volumes[i]); | ||
522 | ubi->volumes[i] = vol; | ||
523 | ubi->vol_count += 1; | ||
524 | vol->ubi = ubi; | ||
525 | reserved_pebs += vol->reserved_pebs; | ||
526 | |||
527 | /* | ||
528 | * In case of dynamic volume UBI knows nothing about how many | ||
529 | * data is stored there. So assume the whole volume is used. | ||
530 | */ | ||
531 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { | ||
532 | vol->used_ebs = vol->reserved_pebs; | ||
533 | vol->last_eb_bytes = vol->usable_leb_size; | ||
534 | vol->used_bytes = vol->used_ebs * vol->usable_leb_size; | ||
535 | continue; | ||
536 | } | ||
537 | |||
538 | /* Static volumes only */ | ||
539 | sv = ubi_scan_find_sv(si, i); | ||
540 | if (!sv) { | ||
541 | /* | ||
542 | * No eraseblocks belonging to this volume found. We | ||
543 | * don't actually know whether this static volume is | ||
544 | * completely corrupted or just contains no data. And | ||
545 | * we cannot know this as long as data size is not | ||
546 | * stored on flash. So we just assume the volume is | ||
547 | * empty. FIXME: this should be handled. | ||
548 | */ | ||
549 | continue; | ||
550 | } | ||
551 | |||
552 | if (sv->leb_count != sv->used_ebs) { | ||
553 | /* | ||
554 | * We found a static volume which misses several | ||
555 | * eraseblocks. Treat it as corrupted. | ||
556 | */ | ||
557 | ubi_warn("static volume %d misses %d LEBs - corrupted", | ||
558 | sv->vol_id, sv->used_ebs - sv->leb_count); | ||
559 | vol->corrupted = 1; | ||
560 | continue; | ||
561 | } | ||
562 | |||
563 | vol->used_ebs = sv->used_ebs; | ||
564 | vol->used_bytes = (vol->used_ebs - 1) * vol->usable_leb_size; | ||
565 | vol->used_bytes += sv->last_data_size; | ||
566 | vol->last_eb_bytes = sv->last_data_size; | ||
567 | } | ||
568 | |||
569 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); | ||
570 | if (!vol) | ||
571 | return -ENOMEM; | ||
572 | |||
573 | vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS; | ||
574 | vol->alignment = 1; | ||
575 | vol->vol_type = UBI_DYNAMIC_VOLUME; | ||
576 | vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1; | ||
577 | memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1); | ||
578 | vol->usable_leb_size = ubi->leb_size; | ||
579 | vol->used_ebs = vol->reserved_pebs; | ||
580 | vol->last_eb_bytes = vol->reserved_pebs; | ||
581 | vol->used_bytes = vol->used_ebs * (ubi->leb_size - vol->data_pad); | ||
582 | vol->vol_id = UBI_LAYOUT_VOL_ID; | ||
583 | |||
584 | ubi_assert(!ubi->volumes[i]); | ||
585 | ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol; | ||
586 | reserved_pebs += vol->reserved_pebs; | ||
587 | ubi->vol_count += 1; | ||
588 | vol->ubi = ubi; | ||
589 | |||
590 | if (reserved_pebs > ubi->avail_pebs) | ||
591 | ubi_err("not enough PEBs, required %d, available %d", | ||
592 | reserved_pebs, ubi->avail_pebs); | ||
593 | ubi->rsvd_pebs += reserved_pebs; | ||
594 | ubi->avail_pebs -= reserved_pebs; | ||
595 | |||
596 | return 0; | ||
597 | } | ||
598 | |||
599 | /** | ||
600 | * check_sv - check volume scanning information. | ||
601 | * @vol: UBI volume description object | ||
602 | * @sv: volume scanning information | ||
603 | * | ||
604 | * This function returns zero if the volume scanning information is consistent | ||
605 | * to the data read from the volume tabla, and %-EINVAL if not. | ||
606 | */ | ||
607 | static int check_sv(const struct ubi_volume *vol, | ||
608 | const struct ubi_scan_volume *sv) | ||
609 | { | ||
610 | if (sv->highest_lnum >= vol->reserved_pebs) { | ||
611 | dbg_err("bad highest_lnum"); | ||
612 | goto bad; | ||
613 | } | ||
614 | if (sv->leb_count > vol->reserved_pebs) { | ||
615 | dbg_err("bad leb_count"); | ||
616 | goto bad; | ||
617 | } | ||
618 | if (sv->vol_type != vol->vol_type) { | ||
619 | dbg_err("bad vol_type"); | ||
620 | goto bad; | ||
621 | } | ||
622 | if (sv->used_ebs > vol->reserved_pebs) { | ||
623 | dbg_err("bad used_ebs"); | ||
624 | goto bad; | ||
625 | } | ||
626 | if (sv->data_pad != vol->data_pad) { | ||
627 | dbg_err("bad data_pad"); | ||
628 | goto bad; | ||
629 | } | ||
630 | return 0; | ||
631 | |||
632 | bad: | ||
633 | ubi_err("bad scanning information"); | ||
634 | ubi_dbg_dump_sv(sv); | ||
635 | ubi_dbg_dump_vol_info(vol); | ||
636 | return -EINVAL; | ||
637 | } | ||
638 | |||
639 | /** | ||
640 | * check_scanning_info - check that scanning information. | ||
641 | * @ubi: UBI device description object | ||
642 | * @si: scanning information | ||
643 | * | ||
644 | * Even though we protect on-flash data by CRC checksums, we still don't trust | ||
645 | * the media. This function ensures that scanning information is consistent to | ||
646 | * the information read from the volume table. Returns zero if the scanning | ||
647 | * information is OK and %-EINVAL if it is not. | ||
648 | */ | ||
649 | static int check_scanning_info(const struct ubi_device *ubi, | ||
650 | struct ubi_scan_info *si) | ||
651 | { | ||
652 | int err, i; | ||
653 | struct ubi_scan_volume *sv; | ||
654 | struct ubi_volume *vol; | ||
655 | |||
656 | if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { | ||
657 | ubi_err("scanning found %d volumes, maximum is %d + %d", | ||
658 | si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); | ||
659 | return -EINVAL; | ||
660 | } | ||
661 | |||
662 | if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT&& | ||
663 | si->highest_vol_id < UBI_INTERNAL_VOL_START) { | ||
664 | ubi_err("too large volume ID %d found by scanning", | ||
665 | si->highest_vol_id); | ||
666 | return -EINVAL; | ||
667 | } | ||
668 | |||
669 | |||
670 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { | ||
671 | cond_resched(); | ||
672 | |||
673 | sv = ubi_scan_find_sv(si, i); | ||
674 | vol = ubi->volumes[i]; | ||
675 | if (!vol) { | ||
676 | if (sv) | ||
677 | ubi_scan_rm_volume(si, sv); | ||
678 | continue; | ||
679 | } | ||
680 | |||
681 | if (vol->reserved_pebs == 0) { | ||
682 | ubi_assert(i < ubi->vtbl_slots); | ||
683 | |||
684 | if (!sv) | ||
685 | continue; | ||
686 | |||
687 | /* | ||
688 | * During scanning we found a volume which does not | ||
689 | * exist according to the information in the volume | ||
690 | * table. This must have happened due to an unclean | ||
691 | * reboot while the volume was being removed. Discard | ||
692 | * these eraseblocks. | ||
693 | */ | ||
694 | ubi_msg("finish volume %d removal", sv->vol_id); | ||
695 | ubi_scan_rm_volume(si, sv); | ||
696 | } else if (sv) { | ||
697 | err = check_sv(vol, sv); | ||
698 | if (err) | ||
699 | return err; | ||
700 | } | ||
701 | } | ||
702 | |||
703 | return 0; | ||
704 | } | ||
705 | |||
706 | /** | ||
707 | * ubi_read_volume_table - read volume table. | ||
708 | * information. | ||
709 | * @ubi: UBI device description object | ||
710 | * @si: scanning information | ||
711 | * | ||
712 | * This function reads volume table, checks it, recover from errors if needed, | ||
713 | * or creates it if needed. Returns zero in case of success and a negative | ||
714 | * error code in case of failure. | ||
715 | */ | ||
716 | int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si) | ||
717 | { | ||
718 | int i, err; | ||
719 | struct ubi_scan_volume *sv; | ||
720 | |||
721 | empty_vtbl_record.crc = cpu_to_ubi32(0xf116c36b); | ||
722 | |||
723 | /* | ||
724 | * The number of supported volumes is limited by the eraseblock size | ||
725 | * and by the UBI_MAX_VOLUMES constant. | ||
726 | */ | ||
727 | ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE; | ||
728 | if (ubi->vtbl_slots > UBI_MAX_VOLUMES) | ||
729 | ubi->vtbl_slots = UBI_MAX_VOLUMES; | ||
730 | |||
731 | ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; | ||
732 | ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); | ||
733 | |||
734 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOL_ID); | ||
735 | if (!sv) { | ||
736 | /* | ||
737 | * No logical eraseblocks belonging to the layout volume were | ||
738 | * found. This could mean that the flash is just empty. In | ||
739 | * this case we create empty layout volume. | ||
740 | * | ||
741 | * But if flash is not empty this must be a corruption or the | ||
742 | * MTD device just contains garbage. | ||
743 | */ | ||
744 | if (si->is_empty) { | ||
745 | ubi->vtbl = create_empty_lvol(ubi, si); | ||
746 | if (IS_ERR(ubi->vtbl)) | ||
747 | return PTR_ERR(ubi->vtbl); | ||
748 | } else { | ||
749 | ubi_err("the layout volume was not found"); | ||
750 | return -EINVAL; | ||
751 | } | ||
752 | } else { | ||
753 | if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) { | ||
754 | /* This must not happen with proper UBI images */ | ||
755 | dbg_err("too many LEBs (%d) in layout volume", | ||
756 | sv->leb_count); | ||
757 | return -EINVAL; | ||
758 | } | ||
759 | |||
760 | ubi->vtbl = process_lvol(ubi, si, sv); | ||
761 | if (IS_ERR(ubi->vtbl)) | ||
762 | return PTR_ERR(ubi->vtbl); | ||
763 | } | ||
764 | |||
765 | ubi->avail_pebs = ubi->good_peb_count; | ||
766 | |||
767 | /* | ||
768 | * The layout volume is OK, initialize the corresponding in-RAM data | ||
769 | * structures. | ||
770 | */ | ||
771 | err = init_volumes(ubi, si, ubi->vtbl); | ||
772 | if (err) | ||
773 | goto out_free; | ||
774 | |||
775 | /* | ||
776 | * Get sure that the scanning information is consistent to the | ||
777 | * information stored in the volume table. | ||
778 | */ | ||
779 | err = check_scanning_info(ubi, si); | ||
780 | if (err) | ||
781 | goto out_free; | ||
782 | |||
783 | return 0; | ||
784 | |||
785 | out_free: | ||
786 | kfree(ubi->vtbl); | ||
787 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) | ||
788 | if (ubi->volumes[i]) { | ||
789 | kfree(ubi->volumes[i]); | ||
790 | ubi->volumes[i] = NULL; | ||
791 | } | ||
792 | return err; | ||
793 | } | ||
794 | |||
795 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
796 | |||
797 | /** | ||
798 | * paranoid_vtbl_check - check volume table. | ||
799 | * @ubi: UBI device description object | ||
800 | */ | ||
801 | static void paranoid_vtbl_check(const struct ubi_device *ubi) | ||
802 | { | ||
803 | if (vtbl_check(ubi, ubi->vtbl)) { | ||
804 | ubi_err("paranoid check failed"); | ||
805 | BUG(); | ||
806 | } | ||
807 | } | ||
808 | |||
809 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ | ||
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c new file mode 100644 index 000000000000..9ecaf77eca9e --- /dev/null +++ b/drivers/mtd/ubi/wl.c | |||
@@ -0,0 +1,1671 @@ | |||
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 | * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * UBI wear-leveling unit. | ||
23 | * | ||
24 | * This unit is responsible for wear-leveling. It works in terms of physical | ||
25 | * eraseblocks and erase counters and knows nothing about logical eraseblocks, | ||
26 | * volumes, etc. From this unit's perspective all physical eraseblocks are of | ||
27 | * two types - used and free. Used physical eraseblocks are those that were | ||
28 | * "get" by the 'ubi_wl_get_peb()' function, and free physical eraseblocks are | ||
29 | * those that were put by the 'ubi_wl_put_peb()' function. | ||
30 | * | ||
31 | * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter | ||
32 | * header. The rest of the physical eraseblock contains only 0xFF bytes. | ||
33 | * | ||
34 | * When physical eraseblocks are returned to the WL unit by means of the | ||
35 | * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is | ||
36 | * done asynchronously in context of the per-UBI device background thread, | ||
37 | * which is also managed by the WL unit. | ||
38 | * | ||
39 | * The wear-leveling is ensured by means of moving the contents of used | ||
40 | * physical eraseblocks with low erase counter to free physical eraseblocks | ||
41 | * with high erase counter. | ||
42 | * | ||
43 | * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick | ||
44 | * an "optimal" physical eraseblock. For example, when it is known that the | ||
45 | * physical eraseblock will be "put" soon because it contains short-term data, | ||
46 | * the WL unit may pick a free physical eraseblock with low erase counter, and | ||
47 | * so forth. | ||
48 | * | ||
49 | * If the WL unit fails to erase a physical eraseblock, it marks it as bad. | ||
50 | * | ||
51 | * This unit is also responsible for scrubbing. If a bit-flip is detected in a | ||
52 | * physical eraseblock, it has to be moved. Technically this is the same as | ||
53 | * moving it for wear-leveling reasons. | ||
54 | * | ||
55 | * As it was said, for the UBI unit all physical eraseblocks are either "free" | ||
56 | * or "used". Free eraseblock are kept in the @wl->free RB-tree, while used | ||
57 | * eraseblocks are kept in a set of different RB-trees: @wl->used, | ||
58 | * @wl->prot.pnum, @wl->prot.aec, and @wl->scrub. | ||
59 | * | ||
60 | * Note, in this implementation, we keep a small in-RAM object for each physical | ||
61 | * eraseblock. This is surely not a scalable solution. But it appears to be good | ||
62 | * enough for moderately large flashes and it is simple. In future, one may | ||
63 | * re-work this unit and make it more scalable. | ||
64 | * | ||
65 | * At the moment this unit does not utilize the sequence number, which was | ||
66 | * introduced relatively recently. But it would be wise to do this because the | ||
67 | * sequence number of a logical eraseblock characterizes how old is it. For | ||
68 | * example, when we move a PEB with low erase counter, and we need to pick the | ||
69 | * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we | ||
70 | * pick target PEB with an average EC if our PEB is not very "old". This is a | ||
71 | * room for future re-works of the WL unit. | ||
72 | * | ||
73 | * FIXME: looks too complex, should be simplified (later). | ||
74 | */ | ||
75 | |||
76 | #include <linux/slab.h> | ||
77 | #include <linux/crc32.h> | ||
78 | #include <linux/freezer.h> | ||
79 | #include <linux/kthread.h> | ||
80 | #include "ubi.h" | ||
81 | |||
82 | /* Number of physical eraseblocks reserved for wear-leveling purposes */ | ||
83 | #define WL_RESERVED_PEBS 1 | ||
84 | |||
85 | /* | ||
86 | * How many erase cycles are short term, unknown, and long term physical | ||
87 | * eraseblocks protected. | ||
88 | */ | ||
89 | #define ST_PROTECTION 16 | ||
90 | #define U_PROTECTION 10 | ||
91 | #define LT_PROTECTION 4 | ||
92 | |||
93 | /* | ||
94 | * Maximum difference between two erase counters. If this threshold is | ||
95 | * exceeded, the WL unit starts moving data from used physical eraseblocks with | ||
96 | * low erase counter to free physical eraseblocks with high erase counter. | ||
97 | */ | ||
98 | #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD | ||
99 | |||
100 | /* | ||
101 | * When a physical eraseblock is moved, the WL unit has to pick the target | ||
102 | * physical eraseblock to move to. The simplest way would be just to pick the | ||
103 | * one with the highest erase counter. But in certain workloads this could lead | ||
104 | * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a | ||
105 | * situation when the picked physical eraseblock is constantly erased after the | ||
106 | * data is written to it. So, we have a constant which limits the highest erase | ||
107 | * counter of the free physical eraseblock to pick. Namely, the WL unit does | ||
108 | * not pick eraseblocks with erase counter greater then the lowest erase | ||
109 | * counter plus %WL_FREE_MAX_DIFF. | ||
110 | */ | ||
111 | #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD) | ||
112 | |||
113 | /* | ||
114 | * Maximum number of consecutive background thread failures which is enough to | ||
115 | * switch to read-only mode. | ||
116 | */ | ||
117 | #define WL_MAX_FAILURES 32 | ||
118 | |||
119 | /** | ||
120 | * struct ubi_wl_entry - wear-leveling entry. | ||
121 | * @rb: link in the corresponding RB-tree | ||
122 | * @ec: erase counter | ||
123 | * @pnum: physical eraseblock number | ||
124 | * | ||
125 | * Each physical eraseblock has a corresponding &struct wl_entry object which | ||
126 | * may be kept in different RB-trees. | ||
127 | */ | ||
128 | struct ubi_wl_entry { | ||
129 | struct rb_node rb; | ||
130 | int ec; | ||
131 | int pnum; | ||
132 | }; | ||
133 | |||
134 | /** | ||
135 | * struct ubi_wl_prot_entry - PEB protection entry. | ||
136 | * @rb_pnum: link in the @wl->prot.pnum RB-tree | ||
137 | * @rb_aec: link in the @wl->prot.aec RB-tree | ||
138 | * @abs_ec: the absolute erase counter value when the protection ends | ||
139 | * @e: the wear-leveling entry of the physical eraseblock under protection | ||
140 | * | ||
141 | * When the WL unit returns a physical eraseblock, the physical eraseblock is | ||
142 | * protected from being moved for some "time". For this reason, the physical | ||
143 | * eraseblock is not directly moved from the @wl->free tree to the @wl->used | ||
144 | * tree. There is one more tree in between where this physical eraseblock is | ||
145 | * temporarily stored (@wl->prot). | ||
146 | * | ||
147 | * All this protection stuff is needed because: | ||
148 | * o we don't want to move physical eraseblocks just after we have given them | ||
149 | * to the user; instead, we first want to let users fill them up with data; | ||
150 | * | ||
151 | * o there is a chance that the user will put the physical eraseblock very | ||
152 | * soon, so it makes sense not to move it for some time, but wait; this is | ||
153 | * especially important in case of "short term" physical eraseblocks. | ||
154 | * | ||
155 | * Physical eraseblocks stay protected only for limited time. But the "time" is | ||
156 | * measured in erase cycles in this case. This is implemented with help of the | ||
157 | * absolute erase counter (@wl->abs_ec). When it reaches certain value, the | ||
158 | * physical eraseblocks are moved from the protection trees (@wl->prot.*) to | ||
159 | * the @wl->used tree. | ||
160 | * | ||
161 | * Protected physical eraseblocks are searched by physical eraseblock number | ||
162 | * (when they are put) and by the absolute erase counter (to check if it is | ||
163 | * time to move them to the @wl->used tree). So there are actually 2 RB-trees | ||
164 | * storing the protected physical eraseblocks: @wl->prot.pnum and | ||
165 | * @wl->prot.aec. They are referred to as the "protection" trees. The | ||
166 | * first one is indexed by the physical eraseblock number. The second one is | ||
167 | * indexed by the absolute erase counter. Both trees store | ||
168 | * &struct ubi_wl_prot_entry objects. | ||
169 | * | ||
170 | * Each physical eraseblock has 2 main states: free and used. The former state | ||
171 | * corresponds to the @wl->free tree. The latter state is split up on several | ||
172 | * sub-states: | ||
173 | * o the WL movement is allowed (@wl->used tree); | ||
174 | * o the WL movement is temporarily prohibited (@wl->prot.pnum and | ||
175 | * @wl->prot.aec trees); | ||
176 | * o scrubbing is needed (@wl->scrub tree). | ||
177 | * | ||
178 | * Depending on the sub-state, wear-leveling entries of the used physical | ||
179 | * eraseblocks may be kept in one of those trees. | ||
180 | */ | ||
181 | struct ubi_wl_prot_entry { | ||
182 | struct rb_node rb_pnum; | ||
183 | struct rb_node rb_aec; | ||
184 | unsigned long long abs_ec; | ||
185 | struct ubi_wl_entry *e; | ||
186 | }; | ||
187 | |||
188 | /** | ||
189 | * struct ubi_work - UBI work description data structure. | ||
190 | * @list: a link in the list of pending works | ||
191 | * @func: worker function | ||
192 | * @priv: private data of the worker function | ||
193 | * | ||
194 | * @e: physical eraseblock to erase | ||
195 | * @torture: if the physical eraseblock has to be tortured | ||
196 | * | ||
197 | * The @func pointer points to the worker function. If the @cancel argument is | ||
198 | * not zero, the worker has to free the resources and exit immediately. The | ||
199 | * worker has to return zero in case of success and a negative error code in | ||
200 | * case of failure. | ||
201 | */ | ||
202 | struct ubi_work { | ||
203 | struct list_head list; | ||
204 | int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel); | ||
205 | /* The below fields are only relevant to erasure works */ | ||
206 | struct ubi_wl_entry *e; | ||
207 | int torture; | ||
208 | }; | ||
209 | |||
210 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
211 | static int paranoid_check_ec(const struct ubi_device *ubi, int pnum, int ec); | ||
212 | static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e, | ||
213 | struct rb_root *root); | ||
214 | #else | ||
215 | #define paranoid_check_ec(ubi, pnum, ec) 0 | ||
216 | #define paranoid_check_in_wl_tree(e, root) | ||
217 | #endif | ||
218 | |||
219 | /* Slab cache for wear-leveling entries */ | ||
220 | static struct kmem_cache *wl_entries_slab; | ||
221 | |||
222 | /** | ||
223 | * tree_empty - a helper function to check if an RB-tree is empty. | ||
224 | * @root: the root of the tree | ||
225 | * | ||
226 | * This function returns non-zero if the RB-tree is empty and zero if not. | ||
227 | */ | ||
228 | static inline int tree_empty(struct rb_root *root) | ||
229 | { | ||
230 | return root->rb_node == NULL; | ||
231 | } | ||
232 | |||
233 | /** | ||
234 | * wl_tree_add - add a wear-leveling entry to a WL RB-tree. | ||
235 | * @e: the wear-leveling entry to add | ||
236 | * @root: the root of the tree | ||
237 | * | ||
238 | * Note, we use (erase counter, physical eraseblock number) pairs as keys in | ||
239 | * the @ubi->used and @ubi->free RB-trees. | ||
240 | */ | ||
241 | static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root) | ||
242 | { | ||
243 | struct rb_node **p, *parent = NULL; | ||
244 | |||
245 | p = &root->rb_node; | ||
246 | while (*p) { | ||
247 | struct ubi_wl_entry *e1; | ||
248 | |||
249 | parent = *p; | ||
250 | e1 = rb_entry(parent, struct ubi_wl_entry, rb); | ||
251 | |||
252 | if (e->ec < e1->ec) | ||
253 | p = &(*p)->rb_left; | ||
254 | else if (e->ec > e1->ec) | ||
255 | p = &(*p)->rb_right; | ||
256 | else { | ||
257 | ubi_assert(e->pnum != e1->pnum); | ||
258 | if (e->pnum < e1->pnum) | ||
259 | p = &(*p)->rb_left; | ||
260 | else | ||
261 | p = &(*p)->rb_right; | ||
262 | } | ||
263 | } | ||
264 | |||
265 | rb_link_node(&e->rb, parent, p); | ||
266 | rb_insert_color(&e->rb, root); | ||
267 | } | ||
268 | |||
269 | |||
270 | /* | ||
271 | * Helper functions to add and delete wear-leveling entries from different | ||
272 | * trees. | ||
273 | */ | ||
274 | |||
275 | static void free_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e) | ||
276 | { | ||
277 | wl_tree_add(e, &ubi->free); | ||
278 | } | ||
279 | static inline void used_tree_add(struct ubi_device *ubi, | ||
280 | struct ubi_wl_entry *e) | ||
281 | { | ||
282 | wl_tree_add(e, &ubi->used); | ||
283 | } | ||
284 | static inline void scrub_tree_add(struct ubi_device *ubi, | ||
285 | struct ubi_wl_entry *e) | ||
286 | { | ||
287 | wl_tree_add(e, &ubi->scrub); | ||
288 | } | ||
289 | static inline void free_tree_del(struct ubi_device *ubi, | ||
290 | struct ubi_wl_entry *e) | ||
291 | { | ||
292 | paranoid_check_in_wl_tree(e, &ubi->free); | ||
293 | rb_erase(&e->rb, &ubi->free); | ||
294 | } | ||
295 | static inline void used_tree_del(struct ubi_device *ubi, | ||
296 | struct ubi_wl_entry *e) | ||
297 | { | ||
298 | paranoid_check_in_wl_tree(e, &ubi->used); | ||
299 | rb_erase(&e->rb, &ubi->used); | ||
300 | } | ||
301 | static inline void scrub_tree_del(struct ubi_device *ubi, | ||
302 | struct ubi_wl_entry *e) | ||
303 | { | ||
304 | paranoid_check_in_wl_tree(e, &ubi->scrub); | ||
305 | rb_erase(&e->rb, &ubi->scrub); | ||
306 | } | ||
307 | |||
308 | /** | ||
309 | * do_work - do one pending work. | ||
310 | * @ubi: UBI device description object | ||
311 | * | ||
312 | * This function returns zero in case of success and a negative error code in | ||
313 | * case of failure. | ||
314 | */ | ||
315 | static int do_work(struct ubi_device *ubi) | ||
316 | { | ||
317 | int err; | ||
318 | struct ubi_work *wrk; | ||
319 | |||
320 | spin_lock(&ubi->wl_lock); | ||
321 | |||
322 | if (list_empty(&ubi->works)) { | ||
323 | spin_unlock(&ubi->wl_lock); | ||
324 | return 0; | ||
325 | } | ||
326 | |||
327 | wrk = list_entry(ubi->works.next, struct ubi_work, list); | ||
328 | list_del(&wrk->list); | ||
329 | spin_unlock(&ubi->wl_lock); | ||
330 | |||
331 | /* | ||
332 | * Call the worker function. Do not touch the work structure | ||
333 | * after this call as it will have been freed or reused by that | ||
334 | * time by the worker function. | ||
335 | */ | ||
336 | err = wrk->func(ubi, wrk, 0); | ||
337 | if (err) | ||
338 | ubi_err("work failed with error code %d", err); | ||
339 | |||
340 | spin_lock(&ubi->wl_lock); | ||
341 | ubi->works_count -= 1; | ||
342 | ubi_assert(ubi->works_count >= 0); | ||
343 | spin_unlock(&ubi->wl_lock); | ||
344 | return err; | ||
345 | } | ||
346 | |||
347 | /** | ||
348 | * produce_free_peb - produce a free physical eraseblock. | ||
349 | * @ubi: UBI device description object | ||
350 | * | ||
351 | * This function tries to make a free PEB by means of synchronous execution of | ||
352 | * pending works. This may be needed if, for example the background thread is | ||
353 | * disabled. Returns zero in case of success and a negative error code in case | ||
354 | * of failure. | ||
355 | */ | ||
356 | static int produce_free_peb(struct ubi_device *ubi) | ||
357 | { | ||
358 | int err; | ||
359 | |||
360 | spin_lock(&ubi->wl_lock); | ||
361 | while (tree_empty(&ubi->free)) { | ||
362 | spin_unlock(&ubi->wl_lock); | ||
363 | |||
364 | dbg_wl("do one work synchronously"); | ||
365 | err = do_work(ubi); | ||
366 | if (err) | ||
367 | return err; | ||
368 | |||
369 | spin_lock(&ubi->wl_lock); | ||
370 | } | ||
371 | spin_unlock(&ubi->wl_lock); | ||
372 | |||
373 | return 0; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree. | ||
378 | * @e: the wear-leveling entry to check | ||
379 | * @root: the root of the tree | ||
380 | * | ||
381 | * This function returns non-zero if @e is in the @root RB-tree and zero if it | ||
382 | * is not. | ||
383 | */ | ||
384 | static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root) | ||
385 | { | ||
386 | struct rb_node *p; | ||
387 | |||
388 | p = root->rb_node; | ||
389 | while (p) { | ||
390 | struct ubi_wl_entry *e1; | ||
391 | |||
392 | e1 = rb_entry(p, struct ubi_wl_entry, rb); | ||
393 | |||
394 | if (e->pnum == e1->pnum) { | ||
395 | ubi_assert(e == e1); | ||
396 | return 1; | ||
397 | } | ||
398 | |||
399 | if (e->ec < e1->ec) | ||
400 | p = p->rb_left; | ||
401 | else if (e->ec > e1->ec) | ||
402 | p = p->rb_right; | ||
403 | else { | ||
404 | ubi_assert(e->pnum != e1->pnum); | ||
405 | if (e->pnum < e1->pnum) | ||
406 | p = p->rb_left; | ||
407 | else | ||
408 | p = p->rb_right; | ||
409 | } | ||
410 | } | ||
411 | |||
412 | return 0; | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * prot_tree_add - add physical eraseblock to protection trees. | ||
417 | * @ubi: UBI device description object | ||
418 | * @e: the physical eraseblock to add | ||
419 | * @pe: protection entry object to use | ||
420 | * @abs_ec: absolute erase counter value when this physical eraseblock has | ||
421 | * to be removed from the protection trees. | ||
422 | * | ||
423 | * @wl->lock has to be locked. | ||
424 | */ | ||
425 | static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e, | ||
426 | struct ubi_wl_prot_entry *pe, int abs_ec) | ||
427 | { | ||
428 | struct rb_node **p, *parent = NULL; | ||
429 | struct ubi_wl_prot_entry *pe1; | ||
430 | |||
431 | pe->e = e; | ||
432 | pe->abs_ec = ubi->abs_ec + abs_ec; | ||
433 | |||
434 | p = &ubi->prot.pnum.rb_node; | ||
435 | while (*p) { | ||
436 | parent = *p; | ||
437 | pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum); | ||
438 | |||
439 | if (e->pnum < pe1->e->pnum) | ||
440 | p = &(*p)->rb_left; | ||
441 | else | ||
442 | p = &(*p)->rb_right; | ||
443 | } | ||
444 | rb_link_node(&pe->rb_pnum, parent, p); | ||
445 | rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum); | ||
446 | |||
447 | p = &ubi->prot.aec.rb_node; | ||
448 | parent = NULL; | ||
449 | while (*p) { | ||
450 | parent = *p; | ||
451 | pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec); | ||
452 | |||
453 | if (pe->abs_ec < pe1->abs_ec) | ||
454 | p = &(*p)->rb_left; | ||
455 | else | ||
456 | p = &(*p)->rb_right; | ||
457 | } | ||
458 | rb_link_node(&pe->rb_aec, parent, p); | ||
459 | rb_insert_color(&pe->rb_aec, &ubi->prot.aec); | ||
460 | } | ||
461 | |||
462 | /** | ||
463 | * find_wl_entry - find wear-leveling entry closest to certain erase counter. | ||
464 | * @root: the RB-tree where to look for | ||
465 | * @max: highest possible erase counter | ||
466 | * | ||
467 | * This function looks for a wear leveling entry with erase counter closest to | ||
468 | * @max and less then @max. | ||
469 | */ | ||
470 | static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max) | ||
471 | { | ||
472 | struct rb_node *p; | ||
473 | struct ubi_wl_entry *e; | ||
474 | |||
475 | e = rb_entry(rb_first(root), struct ubi_wl_entry, rb); | ||
476 | max += e->ec; | ||
477 | |||
478 | p = root->rb_node; | ||
479 | while (p) { | ||
480 | struct ubi_wl_entry *e1; | ||
481 | |||
482 | e1 = rb_entry(p, struct ubi_wl_entry, rb); | ||
483 | if (e1->ec >= max) | ||
484 | p = p->rb_left; | ||
485 | else { | ||
486 | p = p->rb_right; | ||
487 | e = e1; | ||
488 | } | ||
489 | } | ||
490 | |||
491 | return e; | ||
492 | } | ||
493 | |||
494 | /** | ||
495 | * ubi_wl_get_peb - get a physical eraseblock. | ||
496 | * @ubi: UBI device description object | ||
497 | * @dtype: type of data which will be stored in this physical eraseblock | ||
498 | * | ||
499 | * This function returns a physical eraseblock in case of success and a | ||
500 | * negative error code in case of failure. Might sleep. | ||
501 | */ | ||
502 | int ubi_wl_get_peb(struct ubi_device *ubi, int dtype) | ||
503 | { | ||
504 | int err, protect, medium_ec; | ||
505 | struct ubi_wl_entry *e, *first, *last; | ||
506 | struct ubi_wl_prot_entry *pe; | ||
507 | |||
508 | ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM || | ||
509 | dtype == UBI_UNKNOWN); | ||
510 | |||
511 | pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_KERNEL); | ||
512 | if (!pe) | ||
513 | return -ENOMEM; | ||
514 | |||
515 | retry: | ||
516 | spin_lock(&ubi->wl_lock); | ||
517 | if (tree_empty(&ubi->free)) { | ||
518 | if (ubi->works_count == 0) { | ||
519 | ubi_assert(list_empty(&ubi->works)); | ||
520 | ubi_err("no free eraseblocks"); | ||
521 | spin_unlock(&ubi->wl_lock); | ||
522 | kfree(pe); | ||
523 | return -ENOSPC; | ||
524 | } | ||
525 | spin_unlock(&ubi->wl_lock); | ||
526 | |||
527 | err = produce_free_peb(ubi); | ||
528 | if (err < 0) { | ||
529 | kfree(pe); | ||
530 | return err; | ||
531 | } | ||
532 | goto retry; | ||
533 | } | ||
534 | |||
535 | switch (dtype) { | ||
536 | case UBI_LONGTERM: | ||
537 | /* | ||
538 | * For long term data we pick a physical eraseblock | ||
539 | * with high erase counter. But the highest erase | ||
540 | * counter we can pick is bounded by the the lowest | ||
541 | * erase counter plus %WL_FREE_MAX_DIFF. | ||
542 | */ | ||
543 | e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); | ||
544 | protect = LT_PROTECTION; | ||
545 | break; | ||
546 | case UBI_UNKNOWN: | ||
547 | /* | ||
548 | * For unknown data we pick a physical eraseblock with | ||
549 | * medium erase counter. But we by no means can pick a | ||
550 | * physical eraseblock with erase counter greater or | ||
551 | * equivalent than the lowest erase counter plus | ||
552 | * %WL_FREE_MAX_DIFF. | ||
553 | */ | ||
554 | first = rb_entry(rb_first(&ubi->free), | ||
555 | struct ubi_wl_entry, rb); | ||
556 | last = rb_entry(rb_last(&ubi->free), | ||
557 | struct ubi_wl_entry, rb); | ||
558 | |||
559 | if (last->ec - first->ec < WL_FREE_MAX_DIFF) | ||
560 | e = rb_entry(ubi->free.rb_node, | ||
561 | struct ubi_wl_entry, rb); | ||
562 | else { | ||
563 | medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2; | ||
564 | e = find_wl_entry(&ubi->free, medium_ec); | ||
565 | } | ||
566 | protect = U_PROTECTION; | ||
567 | break; | ||
568 | case UBI_SHORTTERM: | ||
569 | /* | ||
570 | * For short term data we pick a physical eraseblock | ||
571 | * with the lowest erase counter as we expect it will | ||
572 | * be erased soon. | ||
573 | */ | ||
574 | e = rb_entry(rb_first(&ubi->free), | ||
575 | struct ubi_wl_entry, rb); | ||
576 | protect = ST_PROTECTION; | ||
577 | break; | ||
578 | default: | ||
579 | protect = 0; | ||
580 | e = NULL; | ||
581 | BUG(); | ||
582 | } | ||
583 | |||
584 | /* | ||
585 | * Move the physical eraseblock to the protection trees where it will | ||
586 | * be protected from being moved for some time. | ||
587 | */ | ||
588 | free_tree_del(ubi, e); | ||
589 | prot_tree_add(ubi, e, pe, protect); | ||
590 | |||
591 | dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect); | ||
592 | spin_unlock(&ubi->wl_lock); | ||
593 | |||
594 | return e->pnum; | ||
595 | } | ||
596 | |||
597 | /** | ||
598 | * prot_tree_del - remove a physical eraseblock from the protection trees | ||
599 | * @ubi: UBI device description object | ||
600 | * @pnum: the physical eraseblock to remove | ||
601 | */ | ||
602 | static void prot_tree_del(struct ubi_device *ubi, int pnum) | ||
603 | { | ||
604 | struct rb_node *p; | ||
605 | struct ubi_wl_prot_entry *pe = NULL; | ||
606 | |||
607 | p = ubi->prot.pnum.rb_node; | ||
608 | while (p) { | ||
609 | |||
610 | pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum); | ||
611 | |||
612 | if (pnum == pe->e->pnum) | ||
613 | break; | ||
614 | |||
615 | if (pnum < pe->e->pnum) | ||
616 | p = p->rb_left; | ||
617 | else | ||
618 | p = p->rb_right; | ||
619 | } | ||
620 | |||
621 | ubi_assert(pe->e->pnum == pnum); | ||
622 | rb_erase(&pe->rb_aec, &ubi->prot.aec); | ||
623 | rb_erase(&pe->rb_pnum, &ubi->prot.pnum); | ||
624 | kfree(pe); | ||
625 | } | ||
626 | |||
627 | /** | ||
628 | * sync_erase - synchronously erase a physical eraseblock. | ||
629 | * @ubi: UBI device description object | ||
630 | * @e: the the physical eraseblock to erase | ||
631 | * @torture: if the physical eraseblock has to be tortured | ||
632 | * | ||
633 | * This function returns zero in case of success and a negative error code in | ||
634 | * case of failure. | ||
635 | */ | ||
636 | static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture) | ||
637 | { | ||
638 | int err; | ||
639 | struct ubi_ec_hdr *ec_hdr; | ||
640 | unsigned long long ec = e->ec; | ||
641 | |||
642 | dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec); | ||
643 | |||
644 | err = paranoid_check_ec(ubi, e->pnum, e->ec); | ||
645 | if (err > 0) | ||
646 | return -EINVAL; | ||
647 | |||
648 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | ||
649 | if (!ec_hdr) | ||
650 | return -ENOMEM; | ||
651 | |||
652 | err = ubi_io_sync_erase(ubi, e->pnum, torture); | ||
653 | if (err < 0) | ||
654 | goto out_free; | ||
655 | |||
656 | ec += err; | ||
657 | if (ec > UBI_MAX_ERASECOUNTER) { | ||
658 | /* | ||
659 | * Erase counter overflow. Upgrade UBI and use 64-bit | ||
660 | * erase counters internally. | ||
661 | */ | ||
662 | ubi_err("erase counter overflow at PEB %d, EC %llu", | ||
663 | e->pnum, ec); | ||
664 | err = -EINVAL; | ||
665 | goto out_free; | ||
666 | } | ||
667 | |||
668 | dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec); | ||
669 | |||
670 | ec_hdr->ec = cpu_to_ubi64(ec); | ||
671 | |||
672 | err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); | ||
673 | if (err) | ||
674 | goto out_free; | ||
675 | |||
676 | e->ec = ec; | ||
677 | spin_lock(&ubi->wl_lock); | ||
678 | if (e->ec > ubi->max_ec) | ||
679 | ubi->max_ec = e->ec; | ||
680 | spin_unlock(&ubi->wl_lock); | ||
681 | |||
682 | out_free: | ||
683 | kfree(ec_hdr); | ||
684 | return err; | ||
685 | } | ||
686 | |||
687 | /** | ||
688 | * check_protection_over - check if it is time to stop protecting some | ||
689 | * physical eraseblocks. | ||
690 | * @ubi: UBI device description object | ||
691 | * | ||
692 | * This function is called after each erase operation, when the absolute erase | ||
693 | * counter is incremented, to check if some physical eraseblock have not to be | ||
694 | * protected any longer. These physical eraseblocks are moved from the | ||
695 | * protection trees to the used tree. | ||
696 | */ | ||
697 | static void check_protection_over(struct ubi_device *ubi) | ||
698 | { | ||
699 | struct ubi_wl_prot_entry *pe; | ||
700 | |||
701 | /* | ||
702 | * There may be several protected physical eraseblock to remove, | ||
703 | * process them all. | ||
704 | */ | ||
705 | while (1) { | ||
706 | spin_lock(&ubi->wl_lock); | ||
707 | if (tree_empty(&ubi->prot.aec)) { | ||
708 | spin_unlock(&ubi->wl_lock); | ||
709 | break; | ||
710 | } | ||
711 | |||
712 | pe = rb_entry(rb_first(&ubi->prot.aec), | ||
713 | struct ubi_wl_prot_entry, rb_aec); | ||
714 | |||
715 | if (pe->abs_ec > ubi->abs_ec) { | ||
716 | spin_unlock(&ubi->wl_lock); | ||
717 | break; | ||
718 | } | ||
719 | |||
720 | dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu", | ||
721 | pe->e->pnum, ubi->abs_ec, pe->abs_ec); | ||
722 | rb_erase(&pe->rb_aec, &ubi->prot.aec); | ||
723 | rb_erase(&pe->rb_pnum, &ubi->prot.pnum); | ||
724 | used_tree_add(ubi, pe->e); | ||
725 | spin_unlock(&ubi->wl_lock); | ||
726 | |||
727 | kfree(pe); | ||
728 | cond_resched(); | ||
729 | } | ||
730 | } | ||
731 | |||
732 | /** | ||
733 | * schedule_ubi_work - schedule a work. | ||
734 | * @ubi: UBI device description object | ||
735 | * @wrk: the work to schedule | ||
736 | * | ||
737 | * This function enqueues a work defined by @wrk to the tail of the pending | ||
738 | * works list. | ||
739 | */ | ||
740 | static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) | ||
741 | { | ||
742 | spin_lock(&ubi->wl_lock); | ||
743 | list_add_tail(&wrk->list, &ubi->works); | ||
744 | ubi_assert(ubi->works_count >= 0); | ||
745 | ubi->works_count += 1; | ||
746 | if (ubi->thread_enabled) | ||
747 | wake_up_process(ubi->bgt_thread); | ||
748 | spin_unlock(&ubi->wl_lock); | ||
749 | } | ||
750 | |||
751 | static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, | ||
752 | int cancel); | ||
753 | |||
754 | /** | ||
755 | * schedule_erase - schedule an erase work. | ||
756 | * @ubi: UBI device description object | ||
757 | * @e: the WL entry of the physical eraseblock to erase | ||
758 | * @torture: if the physical eraseblock has to be tortured | ||
759 | * | ||
760 | * This function returns zero in case of success and a %-ENOMEM in case of | ||
761 | * failure. | ||
762 | */ | ||
763 | static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, | ||
764 | int torture) | ||
765 | { | ||
766 | struct ubi_work *wl_wrk; | ||
767 | |||
768 | dbg_wl("schedule erasure of PEB %d, EC %d, torture %d", | ||
769 | e->pnum, e->ec, torture); | ||
770 | |||
771 | wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_KERNEL); | ||
772 | if (!wl_wrk) | ||
773 | return -ENOMEM; | ||
774 | |||
775 | wl_wrk->func = &erase_worker; | ||
776 | wl_wrk->e = e; | ||
777 | wl_wrk->torture = torture; | ||
778 | |||
779 | schedule_ubi_work(ubi, wl_wrk); | ||
780 | return 0; | ||
781 | } | ||
782 | |||
783 | /** | ||
784 | * wear_leveling_worker - wear-leveling worker function. | ||
785 | * @ubi: UBI device description object | ||
786 | * @wrk: the work object | ||
787 | * @cancel: non-zero if the worker has to free memory and exit | ||
788 | * | ||
789 | * This function copies a more worn out physical eraseblock to a less worn out | ||
790 | * one. Returns zero in case of success and a negative error code in case of | ||
791 | * failure. | ||
792 | */ | ||
793 | static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, | ||
794 | int cancel) | ||
795 | { | ||
796 | int err, put = 0; | ||
797 | struct ubi_wl_entry *e1, *e2; | ||
798 | struct ubi_vid_hdr *vid_hdr; | ||
799 | |||
800 | kfree(wrk); | ||
801 | |||
802 | if (cancel) | ||
803 | return 0; | ||
804 | |||
805 | vid_hdr = ubi_zalloc_vid_hdr(ubi); | ||
806 | if (!vid_hdr) | ||
807 | return -ENOMEM; | ||
808 | |||
809 | spin_lock(&ubi->wl_lock); | ||
810 | |||
811 | /* | ||
812 | * Only one WL worker at a time is supported at this implementation, so | ||
813 | * make sure a PEB is not being moved already. | ||
814 | */ | ||
815 | if (ubi->move_to || tree_empty(&ubi->free) || | ||
816 | (tree_empty(&ubi->used) && tree_empty(&ubi->scrub))) { | ||
817 | /* | ||
818 | * Only one WL worker at a time is supported at this | ||
819 | * implementation, so if a LEB is already being moved, cancel. | ||
820 | * | ||
821 | * No free physical eraseblocks? Well, we cancel wear-leveling | ||
822 | * then. It will be triggered again when a free physical | ||
823 | * eraseblock appears. | ||
824 | * | ||
825 | * No used physical eraseblocks? They must be temporarily | ||
826 | * protected from being moved. They will be moved to the | ||
827 | * @ubi->used tree later and the wear-leveling will be | ||
828 | * triggered again. | ||
829 | */ | ||
830 | dbg_wl("cancel WL, a list is empty: free %d, used %d", | ||
831 | tree_empty(&ubi->free), tree_empty(&ubi->used)); | ||
832 | ubi->wl_scheduled = 0; | ||
833 | spin_unlock(&ubi->wl_lock); | ||
834 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
835 | return 0; | ||
836 | } | ||
837 | |||
838 | if (tree_empty(&ubi->scrub)) { | ||
839 | /* | ||
840 | * Now pick the least worn-out used physical eraseblock and a | ||
841 | * highly worn-out free physical eraseblock. If the erase | ||
842 | * counters differ much enough, start wear-leveling. | ||
843 | */ | ||
844 | e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb); | ||
845 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); | ||
846 | |||
847 | if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) { | ||
848 | dbg_wl("no WL needed: min used EC %d, max free EC %d", | ||
849 | e1->ec, e2->ec); | ||
850 | ubi->wl_scheduled = 0; | ||
851 | spin_unlock(&ubi->wl_lock); | ||
852 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
853 | return 0; | ||
854 | } | ||
855 | used_tree_del(ubi, e1); | ||
856 | dbg_wl("move PEB %d EC %d to PEB %d EC %d", | ||
857 | e1->pnum, e1->ec, e2->pnum, e2->ec); | ||
858 | } else { | ||
859 | e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb); | ||
860 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); | ||
861 | scrub_tree_del(ubi, e1); | ||
862 | dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum); | ||
863 | } | ||
864 | |||
865 | free_tree_del(ubi, e2); | ||
866 | ubi_assert(!ubi->move_from && !ubi->move_to); | ||
867 | ubi_assert(!ubi->move_to_put && !ubi->move_from_put); | ||
868 | ubi->move_from = e1; | ||
869 | ubi->move_to = e2; | ||
870 | spin_unlock(&ubi->wl_lock); | ||
871 | |||
872 | /* | ||
873 | * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum. | ||
874 | * We so far do not know which logical eraseblock our physical | ||
875 | * eraseblock (@e1) belongs to. We have to read the volume identifier | ||
876 | * header first. | ||
877 | */ | ||
878 | |||
879 | err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0); | ||
880 | if (err && err != UBI_IO_BITFLIPS) { | ||
881 | if (err == UBI_IO_PEB_FREE) { | ||
882 | /* | ||
883 | * We are trying to move PEB without a VID header. UBI | ||
884 | * always write VID headers shortly after the PEB was | ||
885 | * given, so we have a situation when it did not have | ||
886 | * chance to write it down because it was preempted. | ||
887 | * Just re-schedule the work, so that next time it will | ||
888 | * likely have the VID header in place. | ||
889 | */ | ||
890 | dbg_wl("PEB %d has no VID header", e1->pnum); | ||
891 | err = 0; | ||
892 | } else { | ||
893 | ubi_err("error %d while reading VID header from PEB %d", | ||
894 | err, e1->pnum); | ||
895 | if (err > 0) | ||
896 | err = -EIO; | ||
897 | } | ||
898 | goto error; | ||
899 | } | ||
900 | |||
901 | err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr); | ||
902 | if (err) { | ||
903 | if (err == UBI_IO_BITFLIPS) | ||
904 | err = 0; | ||
905 | goto error; | ||
906 | } | ||
907 | |||
908 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
909 | spin_lock(&ubi->wl_lock); | ||
910 | if (!ubi->move_to_put) | ||
911 | used_tree_add(ubi, e2); | ||
912 | else | ||
913 | put = 1; | ||
914 | ubi->move_from = ubi->move_to = NULL; | ||
915 | ubi->move_from_put = ubi->move_to_put = 0; | ||
916 | ubi->wl_scheduled = 0; | ||
917 | spin_unlock(&ubi->wl_lock); | ||
918 | |||
919 | if (put) { | ||
920 | /* | ||
921 | * Well, the target PEB was put meanwhile, schedule it for | ||
922 | * erasure. | ||
923 | */ | ||
924 | dbg_wl("PEB %d was put meanwhile, erase", e2->pnum); | ||
925 | err = schedule_erase(ubi, e2, 0); | ||
926 | if (err) { | ||
927 | kmem_cache_free(wl_entries_slab, e2); | ||
928 | ubi_ro_mode(ubi); | ||
929 | } | ||
930 | } | ||
931 | |||
932 | err = schedule_erase(ubi, e1, 0); | ||
933 | if (err) { | ||
934 | kmem_cache_free(wl_entries_slab, e1); | ||
935 | ubi_ro_mode(ubi); | ||
936 | } | ||
937 | |||
938 | dbg_wl("done"); | ||
939 | return err; | ||
940 | |||
941 | /* | ||
942 | * Some error occurred. @e1 was not changed, so return it back. @e2 | ||
943 | * might be changed, schedule it for erasure. | ||
944 | */ | ||
945 | error: | ||
946 | if (err) | ||
947 | dbg_wl("error %d occurred, cancel operation", err); | ||
948 | ubi_assert(err <= 0); | ||
949 | |||
950 | ubi_free_vid_hdr(ubi, vid_hdr); | ||
951 | spin_lock(&ubi->wl_lock); | ||
952 | ubi->wl_scheduled = 0; | ||
953 | if (ubi->move_from_put) | ||
954 | put = 1; | ||
955 | else | ||
956 | used_tree_add(ubi, e1); | ||
957 | ubi->move_from = ubi->move_to = NULL; | ||
958 | ubi->move_from_put = ubi->move_to_put = 0; | ||
959 | spin_unlock(&ubi->wl_lock); | ||
960 | |||
961 | if (put) { | ||
962 | /* | ||
963 | * Well, the target PEB was put meanwhile, schedule it for | ||
964 | * erasure. | ||
965 | */ | ||
966 | dbg_wl("PEB %d was put meanwhile, erase", e1->pnum); | ||
967 | err = schedule_erase(ubi, e1, 0); | ||
968 | if (err) { | ||
969 | kmem_cache_free(wl_entries_slab, e1); | ||
970 | ubi_ro_mode(ubi); | ||
971 | } | ||
972 | } | ||
973 | |||
974 | err = schedule_erase(ubi, e2, 0); | ||
975 | if (err) { | ||
976 | kmem_cache_free(wl_entries_slab, e2); | ||
977 | ubi_ro_mode(ubi); | ||
978 | } | ||
979 | |||
980 | yield(); | ||
981 | return err; | ||
982 | } | ||
983 | |||
984 | /** | ||
985 | * ensure_wear_leveling - schedule wear-leveling if it is needed. | ||
986 | * @ubi: UBI device description object | ||
987 | * | ||
988 | * This function checks if it is time to start wear-leveling and schedules it | ||
989 | * if yes. This function returns zero in case of success and a negative error | ||
990 | * code in case of failure. | ||
991 | */ | ||
992 | static int ensure_wear_leveling(struct ubi_device *ubi) | ||
993 | { | ||
994 | int err = 0; | ||
995 | struct ubi_wl_entry *e1; | ||
996 | struct ubi_wl_entry *e2; | ||
997 | struct ubi_work *wrk; | ||
998 | |||
999 | spin_lock(&ubi->wl_lock); | ||
1000 | if (ubi->wl_scheduled) | ||
1001 | /* Wear-leveling is already in the work queue */ | ||
1002 | goto out_unlock; | ||
1003 | |||
1004 | /* | ||
1005 | * If the ubi->scrub tree is not empty, scrubbing is needed, and the | ||
1006 | * the WL worker has to be scheduled anyway. | ||
1007 | */ | ||
1008 | if (tree_empty(&ubi->scrub)) { | ||
1009 | if (tree_empty(&ubi->used) || tree_empty(&ubi->free)) | ||
1010 | /* No physical eraseblocks - no deal */ | ||
1011 | goto out_unlock; | ||
1012 | |||
1013 | /* | ||
1014 | * We schedule wear-leveling only if the difference between the | ||
1015 | * lowest erase counter of used physical eraseblocks and a high | ||
1016 | * erase counter of free physical eraseblocks is greater then | ||
1017 | * %UBI_WL_THRESHOLD. | ||
1018 | */ | ||
1019 | e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb); | ||
1020 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); | ||
1021 | |||
1022 | if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) | ||
1023 | goto out_unlock; | ||
1024 | dbg_wl("schedule wear-leveling"); | ||
1025 | } else | ||
1026 | dbg_wl("schedule scrubbing"); | ||
1027 | |||
1028 | ubi->wl_scheduled = 1; | ||
1029 | spin_unlock(&ubi->wl_lock); | ||
1030 | |||
1031 | wrk = kmalloc(sizeof(struct ubi_work), GFP_KERNEL); | ||
1032 | if (!wrk) { | ||
1033 | err = -ENOMEM; | ||
1034 | goto out_cancel; | ||
1035 | } | ||
1036 | |||
1037 | wrk->func = &wear_leveling_worker; | ||
1038 | schedule_ubi_work(ubi, wrk); | ||
1039 | return err; | ||
1040 | |||
1041 | out_cancel: | ||
1042 | spin_lock(&ubi->wl_lock); | ||
1043 | ubi->wl_scheduled = 0; | ||
1044 | out_unlock: | ||
1045 | spin_unlock(&ubi->wl_lock); | ||
1046 | return err; | ||
1047 | } | ||
1048 | |||
1049 | /** | ||
1050 | * erase_worker - physical eraseblock erase worker function. | ||
1051 | * @ubi: UBI device description object | ||
1052 | * @wl_wrk: the work object | ||
1053 | * @cancel: non-zero if the worker has to free memory and exit | ||
1054 | * | ||
1055 | * This function erases a physical eraseblock and perform torture testing if | ||
1056 | * needed. It also takes care about marking the physical eraseblock bad if | ||
1057 | * needed. Returns zero in case of success and a negative error code in case of | ||
1058 | * failure. | ||
1059 | */ | ||
1060 | static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, | ||
1061 | int cancel) | ||
1062 | { | ||
1063 | int err; | ||
1064 | struct ubi_wl_entry *e = wl_wrk->e; | ||
1065 | int pnum = e->pnum; | ||
1066 | |||
1067 | if (cancel) { | ||
1068 | dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec); | ||
1069 | kfree(wl_wrk); | ||
1070 | kmem_cache_free(wl_entries_slab, e); | ||
1071 | return 0; | ||
1072 | } | ||
1073 | |||
1074 | dbg_wl("erase PEB %d EC %d", pnum, e->ec); | ||
1075 | |||
1076 | err = sync_erase(ubi, e, wl_wrk->torture); | ||
1077 | if (!err) { | ||
1078 | /* Fine, we've erased it successfully */ | ||
1079 | kfree(wl_wrk); | ||
1080 | |||
1081 | spin_lock(&ubi->wl_lock); | ||
1082 | ubi->abs_ec += 1; | ||
1083 | free_tree_add(ubi, e); | ||
1084 | spin_unlock(&ubi->wl_lock); | ||
1085 | |||
1086 | /* | ||
1087 | * One more erase operation has happened, take care about protected | ||
1088 | * physical eraseblocks. | ||
1089 | */ | ||
1090 | check_protection_over(ubi); | ||
1091 | |||
1092 | /* And take care about wear-leveling */ | ||
1093 | err = ensure_wear_leveling(ubi); | ||
1094 | return err; | ||
1095 | } | ||
1096 | |||
1097 | kfree(wl_wrk); | ||
1098 | kmem_cache_free(wl_entries_slab, e); | ||
1099 | |||
1100 | if (err != -EIO) { | ||
1101 | /* | ||
1102 | * If this is not %-EIO, we have no idea what to do. Scheduling | ||
1103 | * this physical eraseblock for erasure again would cause | ||
1104 | * errors again and again. Well, lets switch to RO mode. | ||
1105 | */ | ||
1106 | ubi_ro_mode(ubi); | ||
1107 | return err; | ||
1108 | } | ||
1109 | |||
1110 | /* It is %-EIO, the PEB went bad */ | ||
1111 | |||
1112 | if (!ubi->bad_allowed) { | ||
1113 | ubi_err("bad physical eraseblock %d detected", pnum); | ||
1114 | ubi_ro_mode(ubi); | ||
1115 | err = -EIO; | ||
1116 | } else { | ||
1117 | int need; | ||
1118 | |||
1119 | spin_lock(&ubi->volumes_lock); | ||
1120 | need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1; | ||
1121 | if (need > 0) { | ||
1122 | need = ubi->avail_pebs >= need ? need : ubi->avail_pebs; | ||
1123 | ubi->avail_pebs -= need; | ||
1124 | ubi->rsvd_pebs += need; | ||
1125 | ubi->beb_rsvd_pebs += need; | ||
1126 | if (need > 0) | ||
1127 | ubi_msg("reserve more %d PEBs", need); | ||
1128 | } | ||
1129 | |||
1130 | if (ubi->beb_rsvd_pebs == 0) { | ||
1131 | spin_unlock(&ubi->volumes_lock); | ||
1132 | ubi_err("no reserved physical eraseblocks"); | ||
1133 | ubi_ro_mode(ubi); | ||
1134 | return -EIO; | ||
1135 | } | ||
1136 | |||
1137 | spin_unlock(&ubi->volumes_lock); | ||
1138 | ubi_msg("mark PEB %d as bad", pnum); | ||
1139 | |||
1140 | err = ubi_io_mark_bad(ubi, pnum); | ||
1141 | if (err) { | ||
1142 | ubi_ro_mode(ubi); | ||
1143 | return err; | ||
1144 | } | ||
1145 | |||
1146 | spin_lock(&ubi->volumes_lock); | ||
1147 | ubi->beb_rsvd_pebs -= 1; | ||
1148 | ubi->bad_peb_count += 1; | ||
1149 | ubi->good_peb_count -= 1; | ||
1150 | ubi_calculate_reserved(ubi); | ||
1151 | if (ubi->beb_rsvd_pebs == 0) | ||
1152 | ubi_warn("last PEB from the reserved pool was used"); | ||
1153 | spin_unlock(&ubi->volumes_lock); | ||
1154 | } | ||
1155 | |||
1156 | return err; | ||
1157 | } | ||
1158 | |||
1159 | /** | ||
1160 | * ubi_wl_put_peb - return a physical eraseblock to the wear-leveling | ||
1161 | * unit. | ||
1162 | * @ubi: UBI device description object | ||
1163 | * @pnum: physical eraseblock to return | ||
1164 | * @torture: if this physical eraseblock has to be tortured | ||
1165 | * | ||
1166 | * This function is called to return physical eraseblock @pnum to the pool of | ||
1167 | * free physical eraseblocks. The @torture flag has to be set if an I/O error | ||
1168 | * occurred to this @pnum and it has to be tested. This function returns zero | ||
1169 | * in case of success and a negative error code in case of failure. | ||
1170 | */ | ||
1171 | int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture) | ||
1172 | { | ||
1173 | int err; | ||
1174 | struct ubi_wl_entry *e; | ||
1175 | |||
1176 | dbg_wl("PEB %d", pnum); | ||
1177 | ubi_assert(pnum >= 0); | ||
1178 | ubi_assert(pnum < ubi->peb_count); | ||
1179 | |||
1180 | spin_lock(&ubi->wl_lock); | ||
1181 | |||
1182 | e = ubi->lookuptbl[pnum]; | ||
1183 | if (e == ubi->move_from) { | ||
1184 | /* | ||
1185 | * User is putting the physical eraseblock which was selected to | ||
1186 | * be moved. It will be scheduled for erasure in the | ||
1187 | * wear-leveling worker. | ||
1188 | */ | ||
1189 | dbg_wl("PEB %d is being moved", pnum); | ||
1190 | ubi_assert(!ubi->move_from_put); | ||
1191 | ubi->move_from_put = 1; | ||
1192 | spin_unlock(&ubi->wl_lock); | ||
1193 | return 0; | ||
1194 | } else if (e == ubi->move_to) { | ||
1195 | /* | ||
1196 | * User is putting the physical eraseblock which was selected | ||
1197 | * as the target the data is moved to. It may happen if the EBA | ||
1198 | * unit already re-mapped the LEB but the WL unit did has not | ||
1199 | * put the PEB to the "used" tree. | ||
1200 | */ | ||
1201 | dbg_wl("PEB %d is the target of data moving", pnum); | ||
1202 | ubi_assert(!ubi->move_to_put); | ||
1203 | ubi->move_to_put = 1; | ||
1204 | spin_unlock(&ubi->wl_lock); | ||
1205 | return 0; | ||
1206 | } else { | ||
1207 | if (in_wl_tree(e, &ubi->used)) | ||
1208 | used_tree_del(ubi, e); | ||
1209 | else if (in_wl_tree(e, &ubi->scrub)) | ||
1210 | scrub_tree_del(ubi, e); | ||
1211 | else | ||
1212 | prot_tree_del(ubi, e->pnum); | ||
1213 | } | ||
1214 | spin_unlock(&ubi->wl_lock); | ||
1215 | |||
1216 | err = schedule_erase(ubi, e, torture); | ||
1217 | if (err) { | ||
1218 | spin_lock(&ubi->wl_lock); | ||
1219 | used_tree_add(ubi, e); | ||
1220 | spin_unlock(&ubi->wl_lock); | ||
1221 | } | ||
1222 | |||
1223 | return err; | ||
1224 | } | ||
1225 | |||
1226 | /** | ||
1227 | * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing. | ||
1228 | * @ubi: UBI device description object | ||
1229 | * @pnum: the physical eraseblock to schedule | ||
1230 | * | ||
1231 | * If a bit-flip in a physical eraseblock is detected, this physical eraseblock | ||
1232 | * needs scrubbing. This function schedules a physical eraseblock for | ||
1233 | * scrubbing which is done in background. This function returns zero in case of | ||
1234 | * success and a negative error code in case of failure. | ||
1235 | */ | ||
1236 | int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) | ||
1237 | { | ||
1238 | struct ubi_wl_entry *e; | ||
1239 | |||
1240 | ubi_msg("schedule PEB %d for scrubbing", pnum); | ||
1241 | |||
1242 | retry: | ||
1243 | spin_lock(&ubi->wl_lock); | ||
1244 | e = ubi->lookuptbl[pnum]; | ||
1245 | if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) { | ||
1246 | spin_unlock(&ubi->wl_lock); | ||
1247 | return 0; | ||
1248 | } | ||
1249 | |||
1250 | if (e == ubi->move_to) { | ||
1251 | /* | ||
1252 | * This physical eraseblock was used to move data to. The data | ||
1253 | * was moved but the PEB was not yet inserted to the proper | ||
1254 | * tree. We should just wait a little and let the WL worker | ||
1255 | * proceed. | ||
1256 | */ | ||
1257 | spin_unlock(&ubi->wl_lock); | ||
1258 | dbg_wl("the PEB %d is not in proper tree, retry", pnum); | ||
1259 | yield(); | ||
1260 | goto retry; | ||
1261 | } | ||
1262 | |||
1263 | if (in_wl_tree(e, &ubi->used)) | ||
1264 | used_tree_del(ubi, e); | ||
1265 | else | ||
1266 | prot_tree_del(ubi, pnum); | ||
1267 | |||
1268 | scrub_tree_add(ubi, e); | ||
1269 | spin_unlock(&ubi->wl_lock); | ||
1270 | |||
1271 | /* | ||
1272 | * Technically scrubbing is the same as wear-leveling, so it is done | ||
1273 | * by the WL worker. | ||
1274 | */ | ||
1275 | return ensure_wear_leveling(ubi); | ||
1276 | } | ||
1277 | |||
1278 | /** | ||
1279 | * ubi_wl_flush - flush all pending works. | ||
1280 | * @ubi: UBI device description object | ||
1281 | * | ||
1282 | * This function returns zero in case of success and a negative error code in | ||
1283 | * case of failure. | ||
1284 | */ | ||
1285 | int ubi_wl_flush(struct ubi_device *ubi) | ||
1286 | { | ||
1287 | int err, pending_count; | ||
1288 | |||
1289 | pending_count = ubi->works_count; | ||
1290 | |||
1291 | dbg_wl("flush (%d pending works)", pending_count); | ||
1292 | |||
1293 | /* | ||
1294 | * Erase while the pending works queue is not empty, but not more then | ||
1295 | * the number of currently pending works. | ||
1296 | */ | ||
1297 | while (pending_count-- > 0) { | ||
1298 | err = do_work(ubi); | ||
1299 | if (err) | ||
1300 | return err; | ||
1301 | } | ||
1302 | |||
1303 | return 0; | ||
1304 | } | ||
1305 | |||
1306 | /** | ||
1307 | * tree_destroy - destroy an RB-tree. | ||
1308 | * @root: the root of the tree to destroy | ||
1309 | */ | ||
1310 | static void tree_destroy(struct rb_root *root) | ||
1311 | { | ||
1312 | struct rb_node *rb; | ||
1313 | struct ubi_wl_entry *e; | ||
1314 | |||
1315 | rb = root->rb_node; | ||
1316 | while (rb) { | ||
1317 | if (rb->rb_left) | ||
1318 | rb = rb->rb_left; | ||
1319 | else if (rb->rb_right) | ||
1320 | rb = rb->rb_right; | ||
1321 | else { | ||
1322 | e = rb_entry(rb, struct ubi_wl_entry, rb); | ||
1323 | |||
1324 | rb = rb_parent(rb); | ||
1325 | if (rb) { | ||
1326 | if (rb->rb_left == &e->rb) | ||
1327 | rb->rb_left = NULL; | ||
1328 | else | ||
1329 | rb->rb_right = NULL; | ||
1330 | } | ||
1331 | |||
1332 | kmem_cache_free(wl_entries_slab, e); | ||
1333 | } | ||
1334 | } | ||
1335 | } | ||
1336 | |||
1337 | /** | ||
1338 | * ubi_thread - UBI background thread. | ||
1339 | * @u: the UBI device description object pointer | ||
1340 | */ | ||
1341 | static int ubi_thread(void *u) | ||
1342 | { | ||
1343 | int failures = 0; | ||
1344 | struct ubi_device *ubi = u; | ||
1345 | |||
1346 | ubi_msg("background thread \"%s\" started, PID %d", | ||
1347 | ubi->bgt_name, current->pid); | ||
1348 | |||
1349 | for (;;) { | ||
1350 | int err; | ||
1351 | |||
1352 | if (kthread_should_stop()) | ||
1353 | goto out; | ||
1354 | |||
1355 | if (try_to_freeze()) | ||
1356 | continue; | ||
1357 | |||
1358 | spin_lock(&ubi->wl_lock); | ||
1359 | if (list_empty(&ubi->works) || ubi->ro_mode || | ||
1360 | !ubi->thread_enabled) { | ||
1361 | set_current_state(TASK_INTERRUPTIBLE); | ||
1362 | spin_unlock(&ubi->wl_lock); | ||
1363 | schedule(); | ||
1364 | continue; | ||
1365 | } | ||
1366 | spin_unlock(&ubi->wl_lock); | ||
1367 | |||
1368 | err = do_work(ubi); | ||
1369 | if (err) { | ||
1370 | ubi_err("%s: work failed with error code %d", | ||
1371 | ubi->bgt_name, err); | ||
1372 | if (failures++ > WL_MAX_FAILURES) { | ||
1373 | /* | ||
1374 | * Too many failures, disable the thread and | ||
1375 | * switch to read-only mode. | ||
1376 | */ | ||
1377 | ubi_msg("%s: %d consecutive failures", | ||
1378 | ubi->bgt_name, WL_MAX_FAILURES); | ||
1379 | ubi_ro_mode(ubi); | ||
1380 | break; | ||
1381 | } | ||
1382 | } else | ||
1383 | failures = 0; | ||
1384 | |||
1385 | cond_resched(); | ||
1386 | } | ||
1387 | |||
1388 | out: | ||
1389 | dbg_wl("background thread \"%s\" is killed", ubi->bgt_name); | ||
1390 | return 0; | ||
1391 | } | ||
1392 | |||
1393 | /** | ||
1394 | * cancel_pending - cancel all pending works. | ||
1395 | * @ubi: UBI device description object | ||
1396 | */ | ||
1397 | static void cancel_pending(struct ubi_device *ubi) | ||
1398 | { | ||
1399 | while (!list_empty(&ubi->works)) { | ||
1400 | struct ubi_work *wrk; | ||
1401 | |||
1402 | wrk = list_entry(ubi->works.next, struct ubi_work, list); | ||
1403 | list_del(&wrk->list); | ||
1404 | wrk->func(ubi, wrk, 1); | ||
1405 | ubi->works_count -= 1; | ||
1406 | ubi_assert(ubi->works_count >= 0); | ||
1407 | } | ||
1408 | } | ||
1409 | |||
1410 | /** | ||
1411 | * ubi_wl_init_scan - initialize the wear-leveling unit using scanning | ||
1412 | * information. | ||
1413 | * @ubi: UBI device description object | ||
1414 | * @si: scanning information | ||
1415 | * | ||
1416 | * This function returns zero in case of success, and a negative error code in | ||
1417 | * case of failure. | ||
1418 | */ | ||
1419 | int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) | ||
1420 | { | ||
1421 | int err; | ||
1422 | struct rb_node *rb1, *rb2; | ||
1423 | struct ubi_scan_volume *sv; | ||
1424 | struct ubi_scan_leb *seb, *tmp; | ||
1425 | struct ubi_wl_entry *e; | ||
1426 | |||
1427 | |||
1428 | ubi->used = ubi->free = ubi->scrub = RB_ROOT; | ||
1429 | ubi->prot.pnum = ubi->prot.aec = RB_ROOT; | ||
1430 | spin_lock_init(&ubi->wl_lock); | ||
1431 | ubi->max_ec = si->max_ec; | ||
1432 | INIT_LIST_HEAD(&ubi->works); | ||
1433 | |||
1434 | sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num); | ||
1435 | |||
1436 | ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name); | ||
1437 | if (IS_ERR(ubi->bgt_thread)) { | ||
1438 | err = PTR_ERR(ubi->bgt_thread); | ||
1439 | ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name, | ||
1440 | err); | ||
1441 | return err; | ||
1442 | } | ||
1443 | |||
1444 | if (ubi_devices_cnt == 0) { | ||
1445 | wl_entries_slab = kmem_cache_create("ubi_wl_entry_slab", | ||
1446 | sizeof(struct ubi_wl_entry), | ||
1447 | 0, 0, NULL, NULL); | ||
1448 | if (!wl_entries_slab) | ||
1449 | return -ENOMEM; | ||
1450 | } | ||
1451 | |||
1452 | err = -ENOMEM; | ||
1453 | ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL); | ||
1454 | if (!ubi->lookuptbl) | ||
1455 | goto out_free; | ||
1456 | |||
1457 | list_for_each_entry_safe(seb, tmp, &si->erase, u.list) { | ||
1458 | cond_resched(); | ||
1459 | |||
1460 | e = kmem_cache_alloc(wl_entries_slab, GFP_KERNEL); | ||
1461 | if (!e) | ||
1462 | goto out_free; | ||
1463 | |||
1464 | e->pnum = seb->pnum; | ||
1465 | e->ec = seb->ec; | ||
1466 | ubi->lookuptbl[e->pnum] = e; | ||
1467 | if (schedule_erase(ubi, e, 0)) { | ||
1468 | kmem_cache_free(wl_entries_slab, e); | ||
1469 | goto out_free; | ||
1470 | } | ||
1471 | } | ||
1472 | |||
1473 | list_for_each_entry(seb, &si->free, u.list) { | ||
1474 | cond_resched(); | ||
1475 | |||
1476 | e = kmem_cache_alloc(wl_entries_slab, GFP_KERNEL); | ||
1477 | if (!e) | ||
1478 | goto out_free; | ||
1479 | |||
1480 | e->pnum = seb->pnum; | ||
1481 | e->ec = seb->ec; | ||
1482 | ubi_assert(e->ec >= 0); | ||
1483 | free_tree_add(ubi, e); | ||
1484 | ubi->lookuptbl[e->pnum] = e; | ||
1485 | } | ||
1486 | |||
1487 | list_for_each_entry(seb, &si->corr, u.list) { | ||
1488 | cond_resched(); | ||
1489 | |||
1490 | e = kmem_cache_alloc(wl_entries_slab, GFP_KERNEL); | ||
1491 | if (!e) | ||
1492 | goto out_free; | ||
1493 | |||
1494 | e->pnum = seb->pnum; | ||
1495 | e->ec = seb->ec; | ||
1496 | ubi->lookuptbl[e->pnum] = e; | ||
1497 | if (schedule_erase(ubi, e, 0)) { | ||
1498 | kmem_cache_free(wl_entries_slab, e); | ||
1499 | goto out_free; | ||
1500 | } | ||
1501 | } | ||
1502 | |||
1503 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { | ||
1504 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { | ||
1505 | cond_resched(); | ||
1506 | |||
1507 | e = kmem_cache_alloc(wl_entries_slab, GFP_KERNEL); | ||
1508 | if (!e) | ||
1509 | goto out_free; | ||
1510 | |||
1511 | e->pnum = seb->pnum; | ||
1512 | e->ec = seb->ec; | ||
1513 | ubi->lookuptbl[e->pnum] = e; | ||
1514 | if (!seb->scrub) { | ||
1515 | dbg_wl("add PEB %d EC %d to the used tree", | ||
1516 | e->pnum, e->ec); | ||
1517 | used_tree_add(ubi, e); | ||
1518 | } else { | ||
1519 | dbg_wl("add PEB %d EC %d to the scrub tree", | ||
1520 | e->pnum, e->ec); | ||
1521 | scrub_tree_add(ubi, e); | ||
1522 | } | ||
1523 | } | ||
1524 | } | ||
1525 | |||
1526 | if (WL_RESERVED_PEBS > ubi->avail_pebs) { | ||
1527 | ubi_err("no enough physical eraseblocks (%d, need %d)", | ||
1528 | ubi->avail_pebs, WL_RESERVED_PEBS); | ||
1529 | goto out_free; | ||
1530 | } | ||
1531 | ubi->avail_pebs -= WL_RESERVED_PEBS; | ||
1532 | ubi->rsvd_pebs += WL_RESERVED_PEBS; | ||
1533 | |||
1534 | /* Schedule wear-leveling if needed */ | ||
1535 | err = ensure_wear_leveling(ubi); | ||
1536 | if (err) | ||
1537 | goto out_free; | ||
1538 | |||
1539 | return 0; | ||
1540 | |||
1541 | out_free: | ||
1542 | cancel_pending(ubi); | ||
1543 | tree_destroy(&ubi->used); | ||
1544 | tree_destroy(&ubi->free); | ||
1545 | tree_destroy(&ubi->scrub); | ||
1546 | kfree(ubi->lookuptbl); | ||
1547 | if (ubi_devices_cnt == 0) | ||
1548 | kmem_cache_destroy(wl_entries_slab); | ||
1549 | return err; | ||
1550 | } | ||
1551 | |||
1552 | /** | ||
1553 | * protection_trees_destroy - destroy the protection RB-trees. | ||
1554 | * @ubi: UBI device description object | ||
1555 | */ | ||
1556 | static void protection_trees_destroy(struct ubi_device *ubi) | ||
1557 | { | ||
1558 | struct rb_node *rb; | ||
1559 | struct ubi_wl_prot_entry *pe; | ||
1560 | |||
1561 | rb = ubi->prot.aec.rb_node; | ||
1562 | while (rb) { | ||
1563 | if (rb->rb_left) | ||
1564 | rb = rb->rb_left; | ||
1565 | else if (rb->rb_right) | ||
1566 | rb = rb->rb_right; | ||
1567 | else { | ||
1568 | pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec); | ||
1569 | |||
1570 | rb = rb_parent(rb); | ||
1571 | if (rb) { | ||
1572 | if (rb->rb_left == &pe->rb_aec) | ||
1573 | rb->rb_left = NULL; | ||
1574 | else | ||
1575 | rb->rb_right = NULL; | ||
1576 | } | ||
1577 | |||
1578 | kmem_cache_free(wl_entries_slab, pe->e); | ||
1579 | kfree(pe); | ||
1580 | } | ||
1581 | } | ||
1582 | } | ||
1583 | |||
1584 | /** | ||
1585 | * ubi_wl_close - close the wear-leveling unit. | ||
1586 | * @ubi: UBI device description object | ||
1587 | */ | ||
1588 | void ubi_wl_close(struct ubi_device *ubi) | ||
1589 | { | ||
1590 | dbg_wl("disable \"%s\"", ubi->bgt_name); | ||
1591 | if (ubi->bgt_thread) | ||
1592 | kthread_stop(ubi->bgt_thread); | ||
1593 | |||
1594 | dbg_wl("close the UBI wear-leveling unit"); | ||
1595 | |||
1596 | cancel_pending(ubi); | ||
1597 | protection_trees_destroy(ubi); | ||
1598 | tree_destroy(&ubi->used); | ||
1599 | tree_destroy(&ubi->free); | ||
1600 | tree_destroy(&ubi->scrub); | ||
1601 | kfree(ubi->lookuptbl); | ||
1602 | if (ubi_devices_cnt == 1) | ||
1603 | kmem_cache_destroy(wl_entries_slab); | ||
1604 | } | ||
1605 | |||
1606 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | ||
1607 | |||
1608 | /** | ||
1609 | * paranoid_check_ec - make sure that the erase counter of a physical eraseblock | ||
1610 | * is correct. | ||
1611 | * @ubi: UBI device description object | ||
1612 | * @pnum: the physical eraseblock number to check | ||
1613 | * @ec: the erase counter to check | ||
1614 | * | ||
1615 | * This function returns zero if the erase counter of physical eraseblock @pnum | ||
1616 | * is equivalent to @ec, %1 if not, and a negative error code if an error | ||
1617 | * occurred. | ||
1618 | */ | ||
1619 | static int paranoid_check_ec(const struct ubi_device *ubi, int pnum, int ec) | ||
1620 | { | ||
1621 | int err; | ||
1622 | long long read_ec; | ||
1623 | struct ubi_ec_hdr *ec_hdr; | ||
1624 | |||
1625 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | ||
1626 | if (!ec_hdr) | ||
1627 | return -ENOMEM; | ||
1628 | |||
1629 | err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); | ||
1630 | if (err && err != UBI_IO_BITFLIPS) { | ||
1631 | /* The header does not have to exist */ | ||
1632 | err = 0; | ||
1633 | goto out_free; | ||
1634 | } | ||
1635 | |||
1636 | read_ec = ubi64_to_cpu(ec_hdr->ec); | ||
1637 | if (ec != read_ec) { | ||
1638 | ubi_err("paranoid check failed for PEB %d", pnum); | ||
1639 | ubi_err("read EC is %lld, should be %d", read_ec, ec); | ||
1640 | ubi_dbg_dump_stack(); | ||
1641 | err = 1; | ||
1642 | } else | ||
1643 | err = 0; | ||
1644 | |||
1645 | out_free: | ||
1646 | kfree(ec_hdr); | ||
1647 | return err; | ||
1648 | } | ||
1649 | |||
1650 | /** | ||
1651 | * paranoid_check_in_wl_tree - make sure that a wear-leveling entry is present | ||
1652 | * in a WL RB-tree. | ||
1653 | * @e: the wear-leveling entry to check | ||
1654 | * @root: the root of the tree | ||
1655 | * | ||
1656 | * This function returns zero if @e is in the @root RB-tree and %1 if it | ||
1657 | * is not. | ||
1658 | */ | ||
1659 | static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e, | ||
1660 | struct rb_root *root) | ||
1661 | { | ||
1662 | if (in_wl_tree(e, root)) | ||
1663 | return 0; | ||
1664 | |||
1665 | ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ", | ||
1666 | e->pnum, e->ec, root); | ||
1667 | ubi_dbg_dump_stack(); | ||
1668 | return 1; | ||
1669 | } | ||
1670 | |||
1671 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ | ||
diff --git a/include/linux/mtd/ubi.h b/include/linux/mtd/ubi.h new file mode 100644 index 000000000000..3d967b6b120a --- /dev/null +++ b/include/linux/mtd/ubi.h | |||
@@ -0,0 +1,202 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | #ifndef __LINUX_UBI_H__ | ||
22 | #define __LINUX_UBI_H__ | ||
23 | |||
24 | #include <asm/ioctl.h> | ||
25 | #include <linux/types.h> | ||
26 | #include <mtd/ubi-user.h> | ||
27 | |||
28 | /* | ||
29 | * UBI data type hint constants. | ||
30 | * | ||
31 | * UBI_LONGTERM: long-term data | ||
32 | * UBI_SHORTTERM: short-term data | ||
33 | * UBI_UNKNOWN: data persistence is unknown | ||
34 | * | ||
35 | * These constants are used when data is written to UBI volumes in order to | ||
36 | * help the UBI wear-leveling unit to find more appropriate physical | ||
37 | * eraseblocks. | ||
38 | */ | ||
39 | enum { | ||
40 | UBI_LONGTERM = 1, | ||
41 | UBI_SHORTTERM, | ||
42 | UBI_UNKNOWN | ||
43 | }; | ||
44 | |||
45 | /* | ||
46 | * enum ubi_open_mode - UBI volume open mode constants. | ||
47 | * | ||
48 | * UBI_READONLY: read-only mode | ||
49 | * UBI_READWRITE: read-write mode | ||
50 | * UBI_EXCLUSIVE: exclusive mode | ||
51 | */ | ||
52 | enum { | ||
53 | UBI_READONLY = 1, | ||
54 | UBI_READWRITE, | ||
55 | UBI_EXCLUSIVE | ||
56 | }; | ||
57 | |||
58 | /** | ||
59 | * struct ubi_volume_info - UBI volume description data structure. | ||
60 | * @vol_id: volume ID | ||
61 | * @ubi_num: UBI device number this volume belongs to | ||
62 | * @size: how many physical eraseblocks are reserved for this volume | ||
63 | * @used_bytes: how many bytes of data this volume contains | ||
64 | * @used_ebs: how many physical eraseblocks of this volume actually contain any | ||
65 | * data | ||
66 | * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) | ||
67 | * @corrupted: non-zero if the volume is corrupted (static volumes only) | ||
68 | * @upd_marker: non-zero if the volume has update marker set | ||
69 | * @alignment: volume alignment | ||
70 | * @usable_leb_size: how many bytes are available in logical eraseblocks of | ||
71 | * this volume | ||
72 | * @name_len: volume name length | ||
73 | * @name: volume name | ||
74 | * @cdev: UBI volume character device major and minor numbers | ||
75 | * | ||
76 | * The @corrupted flag is only relevant to static volumes and is always zero | ||
77 | * for dynamic ones. This is because UBI does not care about dynamic volume | ||
78 | * data protection and only cares about protecting static volume data. | ||
79 | * | ||
80 | * The @upd_marker flag is set if the volume update operation was interrupted. | ||
81 | * Before touching the volume data during the update operation, UBI first sets | ||
82 | * the update marker flag for this volume. If the volume update operation was | ||
83 | * further interrupted, the update marker indicates this. If the update marker | ||
84 | * is set, the contents of the volume is certainly damaged and a new volume | ||
85 | * update operation has to be started. | ||
86 | * | ||
87 | * To put it differently, @corrupted and @upd_marker fields have different | ||
88 | * semantics: | ||
89 | * o the @corrupted flag means that this static volume is corrupted for some | ||
90 | * reasons, but not because an interrupted volume update | ||
91 | * o the @upd_marker field means that the volume is damaged because of an | ||
92 | * interrupted update operation. | ||
93 | * | ||
94 | * I.e., the @corrupted flag is never set if the @upd_marker flag is set. | ||
95 | * | ||
96 | * The @used_bytes and @used_ebs fields are only really needed for static | ||
97 | * volumes and contain the number of bytes stored in this static volume and how | ||
98 | * many eraseblock this data occupies. In case of dynamic volumes, the | ||
99 | * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs | ||
100 | * field is equivalent to @size. | ||
101 | * | ||
102 | * In general, logical eraseblock size is a property of the UBI device, not | ||
103 | * of the UBI volume. Indeed, the logical eraseblock size depends on the | ||
104 | * physical eraseblock size and on how much bytes UBI headers consume. But | ||
105 | * because of the volume alignment (@alignment), the usable size of logical | ||
106 | * eraseblocks if a volume may be less. The following equation is true: | ||
107 | * @usable_leb_size = LEB size - (LEB size mod @alignment), | ||
108 | * where LEB size is the logical eraseblock size defined by the UBI device. | ||
109 | * | ||
110 | * The alignment is multiple to the minimal flash input/output unit size or %1 | ||
111 | * if all the available space is used. | ||
112 | * | ||
113 | * To put this differently, alignment may be considered is a way to change | ||
114 | * volume logical eraseblock sizes. | ||
115 | */ | ||
116 | struct ubi_volume_info { | ||
117 | int ubi_num; | ||
118 | int vol_id; | ||
119 | int size; | ||
120 | long long used_bytes; | ||
121 | int used_ebs; | ||
122 | int vol_type; | ||
123 | int corrupted; | ||
124 | int upd_marker; | ||
125 | int alignment; | ||
126 | int usable_leb_size; | ||
127 | int name_len; | ||
128 | const char *name; | ||
129 | dev_t cdev; | ||
130 | }; | ||
131 | |||
132 | /** | ||
133 | * struct ubi_device_info - UBI device description data structure. | ||
134 | * @ubi_num: ubi device number | ||
135 | * @leb_size: logical eraseblock size on this UBI device | ||
136 | * @min_io_size: minimal I/O unit size | ||
137 | * @ro_mode: if this device is in read-only mode | ||
138 | * @cdev: UBI character device major and minor numbers | ||
139 | * | ||
140 | * Note, @leb_size is the logical eraseblock size offered by the UBI device. | ||
141 | * Volumes of this UBI device may have smaller logical eraseblock size if their | ||
142 | * alignment is not equivalent to %1. | ||
143 | */ | ||
144 | struct ubi_device_info { | ||
145 | int ubi_num; | ||
146 | int leb_size; | ||
147 | int min_io_size; | ||
148 | int ro_mode; | ||
149 | dev_t cdev; | ||
150 | }; | ||
151 | |||
152 | /* UBI descriptor given to users when they open UBI volumes */ | ||
153 | struct ubi_volume_desc; | ||
154 | |||
155 | int ubi_get_device_info(int ubi_num, struct ubi_device_info *di); | ||
156 | void ubi_get_volume_info(struct ubi_volume_desc *desc, | ||
157 | struct ubi_volume_info *vi); | ||
158 | struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode); | ||
159 | struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name, | ||
160 | int mode); | ||
161 | void ubi_close_volume(struct ubi_volume_desc *desc); | ||
162 | int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, | ||
163 | int len, int check); | ||
164 | int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
165 | int offset, int len, int dtype); | ||
166 | int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, | ||
167 | int len, int dtype); | ||
168 | int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum); | ||
169 | int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum); | ||
170 | int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum); | ||
171 | |||
172 | /* | ||
173 | * This function is the same as the 'ubi_leb_read()' function, but it does not | ||
174 | * provide the checking capability. | ||
175 | */ | ||
176 | static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf, | ||
177 | int offset, int len) | ||
178 | { | ||
179 | return ubi_leb_read(desc, lnum, buf, offset, len, 0); | ||
180 | } | ||
181 | |||
182 | /* | ||
183 | * This function is the same as the 'ubi_leb_write()' functions, but it does | ||
184 | * not have the data type argument. | ||
185 | */ | ||
186 | static inline int ubi_write(struct ubi_volume_desc *desc, int lnum, | ||
187 | const void *buf, int offset, int len) | ||
188 | { | ||
189 | return ubi_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN); | ||
190 | } | ||
191 | |||
192 | /* | ||
193 | * This function is the same as the 'ubi_leb_change()' functions, but it does | ||
194 | * not have the data type argument. | ||
195 | */ | ||
196 | static inline int ubi_change(struct ubi_volume_desc *desc, int lnum, | ||
197 | const void *buf, int len) | ||
198 | { | ||
199 | return ubi_leb_change(desc, lnum, buf, len, UBI_UNKNOWN); | ||
200 | } | ||
201 | |||
202 | #endif /* !__LINUX_UBI_H__ */ | ||
diff --git a/include/mtd/Kbuild b/include/mtd/Kbuild index e0fe92b03a4e..4d46b3bdebd8 100644 --- a/include/mtd/Kbuild +++ b/include/mtd/Kbuild | |||
@@ -3,3 +3,5 @@ header-y += jffs2-user.h | |||
3 | header-y += mtd-abi.h | 3 | header-y += mtd-abi.h |
4 | header-y += mtd-user.h | 4 | header-y += mtd-user.h |
5 | header-y += nftl-user.h | 5 | header-y += nftl-user.h |
6 | header-y += ubi-header.h | ||
7 | header-y += ubi-user.h | ||
diff --git a/include/mtd/mtd-abi.h b/include/mtd/mtd-abi.h index 8e501a75a764..f71dac420394 100644 --- a/include/mtd/mtd-abi.h +++ b/include/mtd/mtd-abi.h | |||
@@ -24,6 +24,7 @@ struct mtd_oob_buf { | |||
24 | #define MTD_NORFLASH 3 | 24 | #define MTD_NORFLASH 3 |
25 | #define MTD_NANDFLASH 4 | 25 | #define MTD_NANDFLASH 4 |
26 | #define MTD_DATAFLASH 6 | 26 | #define MTD_DATAFLASH 6 |
27 | #define MTD_UBIVOLUME 7 | ||
27 | 28 | ||
28 | #define MTD_WRITEABLE 0x400 /* Device is writeable */ | 29 | #define MTD_WRITEABLE 0x400 /* Device is writeable */ |
29 | #define MTD_BIT_WRITEABLE 0x800 /* Single bits can be flipped */ | 30 | #define MTD_BIT_WRITEABLE 0x800 /* Single bits can be flipped */ |
diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h new file mode 100644 index 000000000000..fa479c71aa34 --- /dev/null +++ b/include/mtd/ubi-header.h | |||
@@ -0,0 +1,360 @@ | |||
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 | * Authors: Artem Bityutskiy (Битюцкий Артём) | ||
19 | * Thomas Gleixner | ||
20 | * Frank Haverkamp | ||
21 | * Oliver Lohmann | ||
22 | * Andreas Arnez | ||
23 | */ | ||
24 | |||
25 | /* | ||
26 | * This file defines the layout of UBI headers and all the other UBI on-flash | ||
27 | * data structures. May be included by user-space. | ||
28 | */ | ||
29 | |||
30 | #ifndef __UBI_HEADER_H__ | ||
31 | #define __UBI_HEADER_H__ | ||
32 | |||
33 | #include <asm/byteorder.h> | ||
34 | |||
35 | /* The version of UBI images supported by this implementation */ | ||
36 | #define UBI_VERSION 1 | ||
37 | |||
38 | /* The highest erase counter value supported by this implementation */ | ||
39 | #define UBI_MAX_ERASECOUNTER 0x7FFFFFFF | ||
40 | |||
41 | /* The initial CRC32 value used when calculating CRC checksums */ | ||
42 | #define UBI_CRC32_INIT 0xFFFFFFFFU | ||
43 | |||
44 | /* Erase counter header magic number (ASCII "UBI#") */ | ||
45 | #define UBI_EC_HDR_MAGIC 0x55424923 | ||
46 | /* Volume identifier header magic number (ASCII "UBI!") */ | ||
47 | #define UBI_VID_HDR_MAGIC 0x55424921 | ||
48 | |||
49 | /* | ||
50 | * Volume type constants used in the volume identifier header. | ||
51 | * | ||
52 | * @UBI_VID_DYNAMIC: dynamic volume | ||
53 | * @UBI_VID_STATIC: static volume | ||
54 | */ | ||
55 | enum { | ||
56 | UBI_VID_DYNAMIC = 1, | ||
57 | UBI_VID_STATIC = 2 | ||
58 | }; | ||
59 | |||
60 | /* | ||
61 | * Compatibility constants used by internal volumes. | ||
62 | * | ||
63 | * @UBI_COMPAT_DELETE: delete this internal volume before anything is written | ||
64 | * to the flash | ||
65 | * @UBI_COMPAT_RO: attach this device in read-only mode | ||
66 | * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its | ||
67 | * physical eraseblocks, don't allow the wear-leveling unit to move them | ||
68 | * @UBI_COMPAT_REJECT: reject this UBI image | ||
69 | */ | ||
70 | enum { | ||
71 | UBI_COMPAT_DELETE = 1, | ||
72 | UBI_COMPAT_RO = 2, | ||
73 | UBI_COMPAT_PRESERVE = 4, | ||
74 | UBI_COMPAT_REJECT = 5 | ||
75 | }; | ||
76 | |||
77 | /* | ||
78 | * ubi16_t/ubi32_t/ubi64_t - 16, 32, and 64-bit integers used in UBI on-flash | ||
79 | * data structures. | ||
80 | */ | ||
81 | typedef struct { | ||
82 | uint16_t int16; | ||
83 | } __attribute__ ((packed)) ubi16_t; | ||
84 | |||
85 | typedef struct { | ||
86 | uint32_t int32; | ||
87 | } __attribute__ ((packed)) ubi32_t; | ||
88 | |||
89 | typedef struct { | ||
90 | uint64_t int64; | ||
91 | } __attribute__ ((packed)) ubi64_t; | ||
92 | |||
93 | /* | ||
94 | * In this implementation of UBI uses the big-endian format for on-flash | ||
95 | * integers. The below are the corresponding conversion macros. | ||
96 | */ | ||
97 | #define cpu_to_ubi16(x) ((ubi16_t){__cpu_to_be16(x)}) | ||
98 | #define ubi16_to_cpu(x) ((uint16_t)__be16_to_cpu((x).int16)) | ||
99 | |||
100 | #define cpu_to_ubi32(x) ((ubi32_t){__cpu_to_be32(x)}) | ||
101 | #define ubi32_to_cpu(x) ((uint32_t)__be32_to_cpu((x).int32)) | ||
102 | |||
103 | #define cpu_to_ubi64(x) ((ubi64_t){__cpu_to_be64(x)}) | ||
104 | #define ubi64_to_cpu(x) ((uint64_t)__be64_to_cpu((x).int64)) | ||
105 | |||
106 | /* Sizes of UBI headers */ | ||
107 | #define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr) | ||
108 | #define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr) | ||
109 | |||
110 | /* Sizes of UBI headers without the ending CRC */ | ||
111 | #define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(ubi32_t)) | ||
112 | #define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(ubi32_t)) | ||
113 | |||
114 | /** | ||
115 | * struct ubi_ec_hdr - UBI erase counter header. | ||
116 | * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC) | ||
117 | * @version: version of UBI implementation which is supposed to accept this | ||
118 | * UBI image | ||
119 | * @padding1: reserved for future, zeroes | ||
120 | * @ec: the erase counter | ||
121 | * @vid_hdr_offset: where the VID header starts | ||
122 | * @data_offset: where the user data start | ||
123 | * @padding2: reserved for future, zeroes | ||
124 | * @hdr_crc: erase counter header CRC checksum | ||
125 | * | ||
126 | * The erase counter header takes 64 bytes and has a plenty of unused space for | ||
127 | * future usage. The unused fields are zeroed. The @version field is used to | ||
128 | * indicate the version of UBI implementation which is supposed to be able to | ||
129 | * work with this UBI image. If @version is greater then the current UBI | ||
130 | * version, the image is rejected. This may be useful in future if something | ||
131 | * is changed radically. This field is duplicated in the volume identifier | ||
132 | * header. | ||
133 | * | ||
134 | * The @vid_hdr_offset and @data_offset fields contain the offset of the the | ||
135 | * volume identifier header and user data, relative to the beginning of the | ||
136 | * physical eraseblock. These values have to be the same for all physical | ||
137 | * eraseblocks. | ||
138 | */ | ||
139 | struct ubi_ec_hdr { | ||
140 | ubi32_t magic; | ||
141 | uint8_t version; | ||
142 | uint8_t padding1[3]; | ||
143 | ubi64_t ec; /* Warning: the current limit is 31-bit anyway! */ | ||
144 | ubi32_t vid_hdr_offset; | ||
145 | ubi32_t data_offset; | ||
146 | uint8_t padding2[36]; | ||
147 | ubi32_t hdr_crc; | ||
148 | } __attribute__ ((packed)); | ||
149 | |||
150 | /** | ||
151 | * struct ubi_vid_hdr - on-flash UBI volume identifier header. | ||
152 | * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC) | ||
153 | * @version: UBI implementation version which is supposed to accept this UBI | ||
154 | * image (%UBI_VERSION) | ||
155 | * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC) | ||
156 | * @copy_flag: if this logical eraseblock was copied from another physical | ||
157 | * eraseblock (for wear-leveling reasons) | ||
158 | * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE, | ||
159 | * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT) | ||
160 | * @vol_id: ID of this volume | ||
161 | * @lnum: logical eraseblock number | ||
162 | * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be | ||
163 | * removed, kept only for not breaking older UBI users) | ||
164 | * @data_size: how many bytes of data this logical eraseblock contains | ||
165 | * @used_ebs: total number of used logical eraseblocks in this volume | ||
166 | * @data_pad: how many bytes at the end of this physical eraseblock are not | ||
167 | * used | ||
168 | * @data_crc: CRC checksum of the data stored in this logical eraseblock | ||
169 | * @padding1: reserved for future, zeroes | ||
170 | * @sqnum: sequence number | ||
171 | * @padding2: reserved for future, zeroes | ||
172 | * @hdr_crc: volume identifier header CRC checksum | ||
173 | * | ||
174 | * The @sqnum is the value of the global sequence counter at the time when this | ||
175 | * VID header was created. The global sequence counter is incremented each time | ||
176 | * UBI writes a new VID header to the flash, i.e. when it maps a logical | ||
177 | * eraseblock to a new physical eraseblock. The global sequence counter is an | ||
178 | * unsigned 64-bit integer and we assume it never overflows. The @sqnum | ||
179 | * (sequence number) is used to distinguish between older and newer versions of | ||
180 | * logical eraseblocks. | ||
181 | * | ||
182 | * There are 2 situations when there may be more then one physical eraseblock | ||
183 | * corresponding to the same logical eraseblock, i.e., having the same @vol_id | ||
184 | * and @lnum values in the volume identifier header. Suppose we have a logical | ||
185 | * eraseblock L and it is mapped to the physical eraseblock P. | ||
186 | * | ||
187 | * 1. Because UBI may erase physical eraseblocks asynchronously, the following | ||
188 | * situation is possible: L is asynchronously erased, so P is scheduled for | ||
189 | * erasure, then L is written to,i.e. mapped to another physical eraseblock P1, | ||
190 | * so P1 is written to, then an unclean reboot happens. Result - there are 2 | ||
191 | * physical eraseblocks P and P1 corresponding to the same logical eraseblock | ||
192 | * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the | ||
193 | * flash. | ||
194 | * | ||
195 | * 2. From time to time UBI moves logical eraseblocks to other physical | ||
196 | * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P | ||
197 | * to P1, and an unclean reboot happens before P is physically erased, there | ||
198 | * are two physical eraseblocks P and P1 corresponding to L and UBI has to | ||
199 | * select one of them when the flash is attached. The @sqnum field says which | ||
200 | * PEB is the original (obviously P will have lower @sqnum) and the copy. But | ||
201 | * it is not enough to select the physical eraseblock with the higher sequence | ||
202 | * number, because the unclean reboot could have happen in the middle of the | ||
203 | * copying process, so the data in P is corrupted. It is also not enough to | ||
204 | * just select the physical eraseblock with lower sequence number, because the | ||
205 | * data there may be old (consider a case if more data was added to P1 after | ||
206 | * the copying). Moreover, the unclean reboot may happen when the erasure of P | ||
207 | * was just started, so it result in unstable P, which is "mostly" OK, but | ||
208 | * still has unstable bits. | ||
209 | * | ||
210 | * UBI uses the @copy_flag field to indicate that this logical eraseblock is a | ||
211 | * copy. UBI also calculates data CRC when the data is moved and stores it at | ||
212 | * the @data_crc field of the copy (P1). So when UBI needs to pick one physical | ||
213 | * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is | ||
214 | * examined. If it is cleared, the situation* is simple and the newer one is | ||
215 | * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC | ||
216 | * checksum is correct, this physical eraseblock is selected (P1). Otherwise | ||
217 | * the older one (P) is selected. | ||
218 | * | ||
219 | * Note, there is an obsolete @leb_ver field which was used instead of @sqnum | ||
220 | * in the past. But it is not used anymore and we keep it in order to be able | ||
221 | * to deal with old UBI images. It will be removed at some point. | ||
222 | * | ||
223 | * There are 2 sorts of volumes in UBI: user volumes and internal volumes. | ||
224 | * Internal volumes are not seen from outside and are used for various internal | ||
225 | * UBI purposes. In this implementation there is only one internal volume - the | ||
226 | * layout volume. Internal volumes are the main mechanism of UBI extensions. | ||
227 | * For example, in future one may introduce a journal internal volume. Internal | ||
228 | * volumes have their own reserved range of IDs. | ||
229 | * | ||
230 | * The @compat field is only used for internal volumes and contains the "degree | ||
231 | * of their compatibility". It is always zero for user volumes. This field | ||
232 | * provides a mechanism to introduce UBI extensions and to be still compatible | ||
233 | * with older UBI binaries. For example, if someone introduced a journal in | ||
234 | * future, he would probably use %UBI_COMPAT_DELETE compatibility for the | ||
235 | * journal volume. And in this case, older UBI binaries, which know nothing | ||
236 | * about the journal volume, would just delete this volume and work perfectly | ||
237 | * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image | ||
238 | * - it just ignores the Ext3fs journal. | ||
239 | * | ||
240 | * The @data_crc field contains the CRC checksum of the contents of the logical | ||
241 | * eraseblock if this is a static volume. In case of dynamic volumes, it does | ||
242 | * not contain the CRC checksum as a rule. The only exception is when the | ||
243 | * data of the physical eraseblock was moved by the wear-leveling unit, then | ||
244 | * the wear-leveling unit calculates the data CRC and stores it in the | ||
245 | * @data_crc field. And of course, the @copy_flag is %in this case. | ||
246 | * | ||
247 | * The @data_size field is used only for static volumes because UBI has to know | ||
248 | * how many bytes of data are stored in this eraseblock. For dynamic volumes, | ||
249 | * this field usually contains zero. The only exception is when the data of the | ||
250 | * physical eraseblock was moved to another physical eraseblock for | ||
251 | * wear-leveling reasons. In this case, UBI calculates CRC checksum of the | ||
252 | * contents and uses both @data_crc and @data_size fields. In this case, the | ||
253 | * @data_size field contains data size. | ||
254 | * | ||
255 | * The @used_ebs field is used only for static volumes and indicates how many | ||
256 | * eraseblocks the data of the volume takes. For dynamic volumes this field is | ||
257 | * not used and always contains zero. | ||
258 | * | ||
259 | * The @data_pad is calculated when volumes are created using the alignment | ||
260 | * parameter. So, effectively, the @data_pad field reduces the size of logical | ||
261 | * eraseblocks of this volume. This is very handy when one uses block-oriented | ||
262 | * software (say, cramfs) on top of the UBI volume. | ||
263 | */ | ||
264 | struct ubi_vid_hdr { | ||
265 | ubi32_t magic; | ||
266 | uint8_t version; | ||
267 | uint8_t vol_type; | ||
268 | uint8_t copy_flag; | ||
269 | uint8_t compat; | ||
270 | ubi32_t vol_id; | ||
271 | ubi32_t lnum; | ||
272 | ubi32_t leb_ver; /* obsolete, to be removed, don't use */ | ||
273 | ubi32_t data_size; | ||
274 | ubi32_t used_ebs; | ||
275 | ubi32_t data_pad; | ||
276 | ubi32_t data_crc; | ||
277 | uint8_t padding1[4]; | ||
278 | ubi64_t sqnum; | ||
279 | uint8_t padding2[12]; | ||
280 | ubi32_t hdr_crc; | ||
281 | } __attribute__ ((packed)); | ||
282 | |||
283 | /* Internal UBI volumes count */ | ||
284 | #define UBI_INT_VOL_COUNT 1 | ||
285 | |||
286 | /* | ||
287 | * Starting ID of internal volumes. There is reserved room for 4096 internal | ||
288 | * volumes. | ||
289 | */ | ||
290 | #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) | ||
291 | |||
292 | /* The layout volume contains the volume table */ | ||
293 | |||
294 | #define UBI_LAYOUT_VOL_ID UBI_INTERNAL_VOL_START | ||
295 | #define UBI_LAYOUT_VOLUME_EBS 2 | ||
296 | #define UBI_LAYOUT_VOLUME_NAME "layout volume" | ||
297 | #define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT | ||
298 | |||
299 | /* The maximum number of volumes per one UBI device */ | ||
300 | #define UBI_MAX_VOLUMES 128 | ||
301 | |||
302 | /* The maximum volume name length */ | ||
303 | #define UBI_VOL_NAME_MAX 127 | ||
304 | |||
305 | /* Size of the volume table record */ | ||
306 | #define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record) | ||
307 | |||
308 | /* Size of the volume table record without the ending CRC */ | ||
309 | #define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(ubi32_t)) | ||
310 | |||
311 | /** | ||
312 | * struct ubi_vtbl_record - a record in the volume table. | ||
313 | * @reserved_pebs: how many physical eraseblocks are reserved for this volume | ||
314 | * @alignment: volume alignment | ||
315 | * @data_pad: how many bytes are unused at the end of the each physical | ||
316 | * eraseblock to satisfy the requested alignment | ||
317 | * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) | ||
318 | * @upd_marker: if volume update was started but not finished | ||
319 | * @name_len: volume name length | ||
320 | * @name: the volume name | ||
321 | * @padding2: reserved, zeroes | ||
322 | * @crc: a CRC32 checksum of the record | ||
323 | * | ||
324 | * The volume table records are stored in the volume table, which is stored in | ||
325 | * the layout volume. The layout volume consists of 2 logical eraseblock, each | ||
326 | * of which contains a copy of the volume table (i.e., the volume table is | ||
327 | * duplicated). The volume table is an array of &struct ubi_vtbl_record | ||
328 | * objects indexed by the volume ID. | ||
329 | * | ||
330 | * If the size of the logical eraseblock is large enough to fit | ||
331 | * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES | ||
332 | * records. Otherwise, it contains as many records as it can fit (i.e., size of | ||
333 | * logical eraseblock divided by sizeof(struct ubi_vtbl_record)). | ||
334 | * | ||
335 | * The @upd_marker flag is used to implement volume update. It is set to %1 | ||
336 | * before update and set to %0 after the update. So if the update operation was | ||
337 | * interrupted, UBI knows that the volume is corrupted. | ||
338 | * | ||
339 | * The @alignment field is specified when the volume is created and cannot be | ||
340 | * later changed. It may be useful, for example, when a block-oriented file | ||
341 | * system works on top of UBI. The @data_pad field is calculated using the | ||
342 | * logical eraseblock size and @alignment. The alignment must be multiple to the | ||
343 | * minimal flash I/O unit. If @alignment is 1, all the available space of | ||
344 | * the physical eraseblocks is used. | ||
345 | * | ||
346 | * Empty records contain all zeroes and the CRC checksum of those zeroes. | ||
347 | */ | ||
348 | struct ubi_vtbl_record { | ||
349 | ubi32_t reserved_pebs; | ||
350 | ubi32_t alignment; | ||
351 | ubi32_t data_pad; | ||
352 | uint8_t vol_type; | ||
353 | uint8_t upd_marker; | ||
354 | ubi16_t name_len; | ||
355 | uint8_t name[UBI_VOL_NAME_MAX+1]; | ||
356 | uint8_t padding2[24]; | ||
357 | ubi32_t crc; | ||
358 | } __attribute__ ((packed)); | ||
359 | |||
360 | #endif /* !__UBI_HEADER_H__ */ | ||
diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h new file mode 100644 index 000000000000..fe06ded0e6b8 --- /dev/null +++ b/include/mtd/ubi-user.h | |||
@@ -0,0 +1,161 @@ | |||
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 (Битюцкий Артём) | ||
19 | */ | ||
20 | |||
21 | #ifndef __UBI_USER_H__ | ||
22 | #define __UBI_USER_H__ | ||
23 | |||
24 | /* | ||
25 | * UBI volume creation | ||
26 | * ~~~~~~~~~~~~~~~~~~~ | ||
27 | * | ||
28 | * UBI volumes are created via the %UBI_IOCMKVOL IOCTL command of UBI character | ||
29 | * device. A &struct ubi_mkvol_req object has to be properly filled and a | ||
30 | * pointer to it has to be passed to the IOCTL. | ||
31 | * | ||
32 | * UBI volume deletion | ||
33 | * ~~~~~~~~~~~~~~~~~~~ | ||
34 | * | ||
35 | * To delete a volume, the %UBI_IOCRMVOL IOCTL command of the UBI character | ||
36 | * device should be used. A pointer to the 32-bit volume ID hast to be passed | ||
37 | * to the IOCTL. | ||
38 | * | ||
39 | * UBI volume re-size | ||
40 | * ~~~~~~~~~~~~~~~~~~ | ||
41 | * | ||
42 | * To re-size a volume, the %UBI_IOCRSVOL IOCTL command of the UBI character | ||
43 | * device should be used. A &struct ubi_rsvol_req object has to be properly | ||
44 | * filled and a pointer to it has to be passed to the IOCTL. | ||
45 | * | ||
46 | * UBI volume update | ||
47 | * ~~~~~~~~~~~~~~~~~ | ||
48 | * | ||
49 | * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the | ||
50 | * corresponding UBI volume character device. A pointer to a 64-bit update | ||
51 | * size should be passed to the IOCTL. After then, UBI expects user to write | ||
52 | * this number of bytes to the volume character device. The update is finished | ||
53 | * when the claimed number of bytes is passed. So, the volume update sequence | ||
54 | * is something like: | ||
55 | * | ||
56 | * fd = open("/dev/my_volume"); | ||
57 | * ioctl(fd, UBI_IOCVOLUP, &image_size); | ||
58 | * write(fd, buf, image_size); | ||
59 | * close(fd); | ||
60 | */ | ||
61 | |||
62 | /* | ||
63 | * When a new volume is created, users may either specify the volume number they | ||
64 | * want to create or to let UBI automatically assign a volume number using this | ||
65 | * constant. | ||
66 | */ | ||
67 | #define UBI_VOL_NUM_AUTO (-1) | ||
68 | |||
69 | /* Maximum volume name length */ | ||
70 | #define UBI_MAX_VOLUME_NAME 127 | ||
71 | |||
72 | /* IOCTL commands of UBI character devices */ | ||
73 | |||
74 | #define UBI_IOC_MAGIC 'o' | ||
75 | |||
76 | /* Create an UBI volume */ | ||
77 | #define UBI_IOCMKVOL _IOW(UBI_IOC_MAGIC, 0, struct ubi_mkvol_req) | ||
78 | /* Remove an UBI volume */ | ||
79 | #define UBI_IOCRMVOL _IOW(UBI_IOC_MAGIC, 1, int32_t) | ||
80 | /* Re-size an UBI volume */ | ||
81 | #define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req) | ||
82 | |||
83 | /* IOCTL commands of UBI volume character devices */ | ||
84 | |||
85 | #define UBI_VOL_IOC_MAGIC 'O' | ||
86 | |||
87 | /* Start UBI volume update */ | ||
88 | #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t) | ||
89 | /* An eraseblock erasure command, used for debugging, disabled by default */ | ||
90 | #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t) | ||
91 | |||
92 | /* | ||
93 | * UBI volume type constants. | ||
94 | * | ||
95 | * @UBI_DYNAMIC_VOLUME: dynamic volume | ||
96 | * @UBI_STATIC_VOLUME: static volume | ||
97 | */ | ||
98 | enum { | ||
99 | UBI_DYNAMIC_VOLUME = 3, | ||
100 | UBI_STATIC_VOLUME = 4 | ||
101 | }; | ||
102 | |||
103 | /** | ||
104 | * struct ubi_mkvol_req - volume description data structure used in | ||
105 | * volume creation requests. | ||
106 | * @vol_id: volume number | ||
107 | * @alignment: volume alignment | ||
108 | * @bytes: volume size in bytes | ||
109 | * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) | ||
110 | * @padding1: reserved for future, not used | ||
111 | * @name_len: volume name length | ||
112 | * @padding2: reserved for future, not used | ||
113 | * @name: volume name | ||
114 | * | ||
115 | * This structure is used by userspace programs when creating new volumes. The | ||
116 | * @used_bytes field is only necessary when creating static volumes. | ||
117 | * | ||
118 | * The @alignment field specifies the required alignment of the volume logical | ||
119 | * eraseblock. This means, that the size of logical eraseblocks will be aligned | ||
120 | * to this number, i.e., | ||
121 | * (UBI device logical eraseblock size) mod (@alignment) = 0. | ||
122 | * | ||
123 | * To put it differently, the logical eraseblock of this volume may be slightly | ||
124 | * shortened in order to make it properly aligned. The alignment has to be | ||
125 | * multiple of the flash minimal input/output unit, or %1 to utilize the entire | ||
126 | * available space of logical eraseblocks. | ||
127 | * | ||
128 | * The @alignment field may be useful, for example, when one wants to maintain | ||
129 | * a block device on top of an UBI volume. In this case, it is desirable to fit | ||
130 | * an integer number of blocks in logical eraseblocks of this UBI volume. With | ||
131 | * alignment it is possible to update this volume using plane UBI volume image | ||
132 | * BLOBs, without caring about how to properly align them. | ||
133 | */ | ||
134 | struct ubi_mkvol_req { | ||
135 | int32_t vol_id; | ||
136 | int32_t alignment; | ||
137 | int64_t bytes; | ||
138 | int8_t vol_type; | ||
139 | int8_t padding1; | ||
140 | int16_t name_len; | ||
141 | int8_t padding2[4]; | ||
142 | char name[UBI_MAX_VOLUME_NAME+1]; | ||
143 | } __attribute__ ((packed)); | ||
144 | |||
145 | /** | ||
146 | * struct ubi_rsvol_req - a data structure used in volume re-size requests. | ||
147 | * @vol_id: ID of the volume to re-size | ||
148 | * @bytes: new size of the volume in bytes | ||
149 | * | ||
150 | * Re-sizing is possible for both dynamic and static volumes. But while dynamic | ||
151 | * volumes may be re-sized arbitrarily, static volumes cannot be made to be | ||
152 | * smaller then the number of bytes they bear. To arbitrarily shrink a static | ||
153 | * volume, it must be wiped out first (by means of volume update operation with | ||
154 | * zero number of bytes). | ||
155 | */ | ||
156 | struct ubi_rsvol_req { | ||
157 | int64_t bytes; | ||
158 | int32_t vol_id; | ||
159 | } __attribute__ ((packed)); | ||
160 | |||
161 | #endif /* __UBI_USER_H__ */ | ||