aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'drivers')
-rw-r--r--drivers/mtd/ubi/Kconfig8
-rw-r--r--drivers/mtd/ubi/Makefile5
-rw-r--r--drivers/mtd/ubi/attach.c (renamed from drivers/mtd/ubi/scan.c)970
-rw-r--r--drivers/mtd/ubi/build.c85
-rw-r--r--drivers/mtd/ubi/cdev.c36
-rw-r--r--drivers/mtd/ubi/debug.c137
-rw-r--r--drivers/mtd/ubi/debug.h98
-rw-r--r--drivers/mtd/ubi/eba.c68
-rw-r--r--drivers/mtd/ubi/gluebi.c2
-rw-r--r--drivers/mtd/ubi/io.c211
-rw-r--r--drivers/mtd/ubi/kapi.c61
-rw-r--r--drivers/mtd/ubi/scan.h174
-rw-r--r--drivers/mtd/ubi/ubi-media.h8
-rw-r--r--drivers/mtd/ubi/ubi.h179
-rw-r--r--drivers/mtd/ubi/upd.c16
-rw-r--r--drivers/mtd/ubi/vmt.c62
-rw-r--r--drivers/mtd/ubi/vtbl.c228
-rw-r--r--drivers/mtd/ubi/wl.c260
18 files changed, 1198 insertions, 1410 deletions
diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig
index 4dcc752a0c0b..738ee8dc16cd 100644
--- a/drivers/mtd/ubi/Kconfig
+++ b/drivers/mtd/ubi/Kconfig
@@ -52,12 +52,4 @@ config MTD_UBI_GLUEBI
52 work on top of UBI. Do not enable this unless you use legacy 52 work on top of UBI. Do not enable this unless you use legacy
53 software. 53 software.
54 54
55config MTD_UBI_DEBUG
56 bool "UBI debugging"
57 depends on SYSFS
58 select DEBUG_FS
59 select KALLSYMS
60 help
61 This option enables UBI debugging.
62
63endif # MTD_UBI 55endif # MTD_UBI
diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile
index c9302a5452b0..a0803ac74712 100644
--- a/drivers/mtd/ubi/Makefile
+++ b/drivers/mtd/ubi/Makefile
@@ -1,7 +1,6 @@
1obj-$(CONFIG_MTD_UBI) += ubi.o 1obj-$(CONFIG_MTD_UBI) += ubi.o
2 2
3ubi-y += vtbl.o vmt.o upd.o build.o cdev.o kapi.o eba.o io.o wl.o scan.o 3ubi-y += vtbl.o vmt.o upd.o build.o cdev.o kapi.o eba.o io.o wl.o attach.o
4ubi-y += misc.o 4ubi-y += misc.o debug.o
5 5
6ubi-$(CONFIG_MTD_UBI_DEBUG) += debug.o
7obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o 6obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/attach.c
index 12c43b44f815..bd27cbbb4066 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/attach.c
@@ -19,21 +19,21 @@
19 */ 19 */
20 20
21/* 21/*
22 * UBI scanning sub-system. 22 * UBI attaching sub-system.
23 * 23 *
24 * This sub-system is responsible for scanning the flash media, checking UBI 24 * This sub-system is responsible for attaching MTD devices and it also
25 * headers and providing complete information about the UBI flash image. 25 * implements flash media scanning.
26 * 26 *
27 * The scanning information is represented by a &struct ubi_scan_info' object. 27 * The attaching information is represented by a &struct ubi_attach_info'
28 * Information about found volumes is represented by &struct ubi_scan_volume 28 * object. Information about volumes is represented by &struct ubi_ainf_volume
29 * objects which are kept in volume RB-tree with root at the @volumes field. 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. 30 * The RB-tree is indexed by the volume ID.
31 * 31 *
32 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects. 32 * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These
33 * These objects are kept in per-volume RB-trees with the root at the 33 * objects are kept in per-volume RB-trees with the root at the corresponding
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep 34 * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of
35 * an RB-tree of per-volume objects and each of these objects is the root of 35 * per-volume objects and each of these objects is the root of RB-tree of
36 * RB-tree of per-eraseblock objects. 36 * per-LEB objects.
37 * 37 *
38 * Corrupted physical eraseblocks are put to the @corr list, free physical 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 39 * eraseblocks are put to the @free list and the physical eraseblock to be
@@ -51,28 +51,29 @@
51 * 51 *
52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI 52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
53 * tries to handle them gracefully, without printing too many warnings and 53 * tries to handle them gracefully, without printing too many warnings and
54 * error messages. The idea is that we do not lose important data in these case 54 * error messages. The idea is that we do not lose important data in these
55 * - we may lose only the data which was being written to the media just before 55 * cases - we may lose only the data which were being written to the media just
56 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to 56 * before the power cut happened, and the upper layers (e.g., UBIFS) are
57 * handle such data losses (e.g., by using the FS journal). 57 * supposed to handle such data losses (e.g., by using the FS journal).
58 * 58 *
59 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like 59 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
60 * the reason is a power cut, UBI puts this PEB to the @erase list, and all 60 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
61 * PEBs in the @erase list are scheduled for erasure later. 61 * PEBs in the @erase list are scheduled for erasure later.
62 * 62 *
63 * 2. Unexpected corruptions which are not caused by power cuts. During 63 * 2. Unexpected corruptions which are not caused by power cuts. During
64 * scanning, such PEBs are put to the @corr list and UBI preserves them. 64 * attaching, such PEBs are put to the @corr list and UBI preserves them.
65 * Obviously, this lessens the amount of available PEBs, and if at some point 65 * Obviously, this lessens the amount of available PEBs, and if at some point
66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs 66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
67 * about such PEBs every time the MTD device is attached. 67 * about such PEBs every time the MTD device is attached.
68 * 68 *
69 * However, it is difficult to reliably distinguish between these types of 69 * However, it is difficult to reliably distinguish between these types of
70 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2 70 * corruptions and UBI's strategy is as follows (in case of attaching by
71 * if the VID header is corrupted and the data area does not contain all 0xFFs, 71 * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
72 * and there were no bit-flips or integrity errors while reading the data area. 72 * the data area does not contain all 0xFFs, and there were no bit-flips or
73 * Otherwise UBI assumes corruption type 1. So the decision criteria are as 73 * integrity errors (e.g., ECC errors in case of NAND) while reading the data
74 * follows. 74 * area. Otherwise UBI assumes corruption type 1. So the decision criteria
75 * o If the data area contains only 0xFFs, there is no data, and it is safe 75 * are as follows.
76 * o If the data area contains only 0xFFs, there are no data, and it is safe
76 * to just erase this PEB - this is corruption type 1. 77 * to just erase this PEB - this is corruption type 1.
77 * o If the data area has bit-flips or data integrity errors (ECC errors on 78 * o If the data area has bit-flips or data integrity errors (ECC errors on
78 * NAND), it is probably a PEB which was being erased when power cut 79 * NAND), it is probably a PEB which was being erased when power cut
@@ -88,11 +89,7 @@
88#include <linux/random.h> 89#include <linux/random.h>
89#include "ubi.h" 90#include "ubi.h"
90 91
91#ifdef CONFIG_MTD_UBI_DEBUG 92static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
92static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
93#else
94#define paranoid_check_si(ubi, si) 0
95#endif
96 93
97/* Temporary variables used during scanning */ 94/* Temporary variables used during scanning */
98static struct ubi_ec_hdr *ech; 95static struct ubi_ec_hdr *ech;
@@ -100,13 +97,18 @@ static struct ubi_vid_hdr *vidh;
100 97
101/** 98/**
102 * add_to_list - add physical eraseblock to a list. 99 * add_to_list - add physical eraseblock to a list.
103 * @si: scanning information 100 * @ai: attaching information
104 * @pnum: physical eraseblock number to add 101 * @pnum: physical eraseblock number to add
102 * @vol_id: the last used volume id for the PEB
103 * @lnum: the last used LEB number for the PEB
105 * @ec: erase counter of the physical eraseblock 104 * @ec: erase counter of the physical eraseblock
106 * @to_head: if not zero, add to the head of the list 105 * @to_head: if not zero, add to the head of the list
107 * @list: the list to add to 106 * @list: the list to add to
108 * 107 *
109 * This function adds physical eraseblock @pnum to free, erase, or alien lists. 108 * This function allocates a 'struct ubi_ainf_peb' object for physical
109 * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists.
110 * It stores the @lnum and @vol_id alongside, which can both be
111 * %UBI_UNKNOWN if they are not available, not readable, or not assigned.
110 * If @to_head is not zero, PEB will be added to the head of the list, which 112 * If @to_head is not zero, PEB will be added to the head of the list, which
111 * basically means it will be processed first later. E.g., we add corrupted 113 * basically means it will be processed first later. E.g., we add corrupted
112 * PEBs (corrupted due to power cuts) to the head of the erase list to make 114 * PEBs (corrupted due to power cuts) to the head of the erase list to make
@@ -114,65 +116,68 @@ static struct ubi_vid_hdr *vidh;
114 * returns zero in case of success and a negative error code in case of 116 * returns zero in case of success and a negative error code in case of
115 * failure. 117 * failure.
116 */ 118 */
117static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head, 119static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
118 struct list_head *list) 120 int lnum, int ec, int to_head, struct list_head *list)
119{ 121{
120 struct ubi_scan_leb *seb; 122 struct ubi_ainf_peb *aeb;
121 123
122 if (list == &si->free) { 124 if (list == &ai->free) {
123 dbg_bld("add to free: PEB %d, EC %d", pnum, ec); 125 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
124 } else if (list == &si->erase) { 126 } else if (list == &ai->erase) {
125 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); 127 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
126 } else if (list == &si->alien) { 128 } else if (list == &ai->alien) {
127 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); 129 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
128 si->alien_peb_count += 1; 130 ai->alien_peb_count += 1;
129 } else 131 } else
130 BUG(); 132 BUG();
131 133
132 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL); 134 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
133 if (!seb) 135 if (!aeb)
134 return -ENOMEM; 136 return -ENOMEM;
135 137
136 seb->pnum = pnum; 138 aeb->pnum = pnum;
137 seb->ec = ec; 139 aeb->vol_id = vol_id;
140 aeb->lnum = lnum;
141 aeb->ec = ec;
138 if (to_head) 142 if (to_head)
139 list_add(&seb->u.list, list); 143 list_add(&aeb->u.list, list);
140 else 144 else
141 list_add_tail(&seb->u.list, list); 145 list_add_tail(&aeb->u.list, list);
142 return 0; 146 return 0;
143} 147}
144 148
145/** 149/**
146 * add_corrupted - add a corrupted physical eraseblock. 150 * add_corrupted - add a corrupted physical eraseblock.
147 * @si: scanning information 151 * @ai: attaching information
148 * @pnum: physical eraseblock number to add 152 * @pnum: physical eraseblock number to add
149 * @ec: erase counter of the physical eraseblock 153 * @ec: erase counter of the physical eraseblock
150 * 154 *
151 * This function adds corrupted physical eraseblock @pnum to the 'corr' list. 155 * This function allocates a 'struct ubi_ainf_peb' object for a corrupted
152 * The corruption was presumably not caused by a power cut. Returns zero in 156 * physical eraseblock @pnum and adds it to the 'corr' list. The corruption
153 * case of success and a negative error code in case of failure. 157 * was presumably not caused by a power cut. Returns zero in case of success
158 * and a negative error code in case of failure.
154 */ 159 */
155static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec) 160static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
156{ 161{
157 struct ubi_scan_leb *seb; 162 struct ubi_ainf_peb *aeb;
158 163
159 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); 164 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
160 165
161 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL); 166 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
162 if (!seb) 167 if (!aeb)
163 return -ENOMEM; 168 return -ENOMEM;
164 169
165 si->corr_peb_count += 1; 170 ai->corr_peb_count += 1;
166 seb->pnum = pnum; 171 aeb->pnum = pnum;
167 seb->ec = ec; 172 aeb->ec = ec;
168 list_add(&seb->u.list, &si->corr); 173 list_add(&aeb->u.list, &ai->corr);
169 return 0; 174 return 0;
170} 175}
171 176
172/** 177/**
173 * validate_vid_hdr - check volume identifier header. 178 * validate_vid_hdr - check volume identifier header.
174 * @vid_hdr: the volume identifier header to check 179 * @vid_hdr: the volume identifier header to check
175 * @sv: information about the volume this logical eraseblock belongs to 180 * @av: information about the volume this logical eraseblock belongs to
176 * @pnum: physical eraseblock number the VID header came from 181 * @pnum: physical eraseblock number the VID header came from
177 * 182 *
178 * This function checks that data stored in @vid_hdr is consistent. Returns 183 * This function checks that data stored in @vid_hdr is consistent. Returns
@@ -184,15 +189,15 @@ static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
184 * headers of the same volume. 189 * headers of the same volume.
185 */ 190 */
186static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, 191static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
187 const struct ubi_scan_volume *sv, int pnum) 192 const struct ubi_ainf_volume *av, int pnum)
188{ 193{
189 int vol_type = vid_hdr->vol_type; 194 int vol_type = vid_hdr->vol_type;
190 int vol_id = be32_to_cpu(vid_hdr->vol_id); 195 int vol_id = be32_to_cpu(vid_hdr->vol_id);
191 int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 196 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
192 int data_pad = be32_to_cpu(vid_hdr->data_pad); 197 int data_pad = be32_to_cpu(vid_hdr->data_pad);
193 198
194 if (sv->leb_count != 0) { 199 if (av->leb_count != 0) {
195 int sv_vol_type; 200 int av_vol_type;
196 201
197 /* 202 /*
198 * This is not the first logical eraseblock belonging to this 203 * This is not the first logical eraseblock belonging to this
@@ -200,28 +205,28 @@ static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
200 * to the data in previous logical eraseblock headers. 205 * to the data in previous logical eraseblock headers.
201 */ 206 */
202 207
203 if (vol_id != sv->vol_id) { 208 if (vol_id != av->vol_id) {
204 dbg_err("inconsistent vol_id"); 209 ubi_err("inconsistent vol_id");
205 goto bad; 210 goto bad;
206 } 211 }
207 212
208 if (sv->vol_type == UBI_STATIC_VOLUME) 213 if (av->vol_type == UBI_STATIC_VOLUME)
209 sv_vol_type = UBI_VID_STATIC; 214 av_vol_type = UBI_VID_STATIC;
210 else 215 else
211 sv_vol_type = UBI_VID_DYNAMIC; 216 av_vol_type = UBI_VID_DYNAMIC;
212 217
213 if (vol_type != sv_vol_type) { 218 if (vol_type != av_vol_type) {
214 dbg_err("inconsistent vol_type"); 219 ubi_err("inconsistent vol_type");
215 goto bad; 220 goto bad;
216 } 221 }
217 222
218 if (used_ebs != sv->used_ebs) { 223 if (used_ebs != av->used_ebs) {
219 dbg_err("inconsistent used_ebs"); 224 ubi_err("inconsistent used_ebs");
220 goto bad; 225 goto bad;
221 } 226 }
222 227
223 if (data_pad != sv->data_pad) { 228 if (data_pad != av->data_pad) {
224 dbg_err("inconsistent data_pad"); 229 ubi_err("inconsistent data_pad");
225 goto bad; 230 goto bad;
226 } 231 }
227 } 232 }
@@ -230,74 +235,74 @@ static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
230 235
231bad: 236bad:
232 ubi_err("inconsistent VID header at PEB %d", pnum); 237 ubi_err("inconsistent VID header at PEB %d", pnum);
233 ubi_dbg_dump_vid_hdr(vid_hdr); 238 ubi_dump_vid_hdr(vid_hdr);
234 ubi_dbg_dump_sv(sv); 239 ubi_dump_av(av);
235 return -EINVAL; 240 return -EINVAL;
236} 241}
237 242
238/** 243/**
239 * add_volume - add volume to the scanning information. 244 * add_volume - add volume to the attaching information.
240 * @si: scanning information 245 * @ai: attaching information
241 * @vol_id: ID of the volume to add 246 * @vol_id: ID of the volume to add
242 * @pnum: physical eraseblock number 247 * @pnum: physical eraseblock number
243 * @vid_hdr: volume identifier header 248 * @vid_hdr: volume identifier header
244 * 249 *
245 * If the volume corresponding to the @vid_hdr logical eraseblock is already 250 * If the volume corresponding to the @vid_hdr logical eraseblock is already
246 * present in the scanning information, this function does nothing. Otherwise 251 * present in the attaching information, this function does nothing. Otherwise
247 * it adds corresponding volume to the scanning information. Returns a pointer 252 * it adds corresponding volume to the attaching information. Returns a pointer
248 * to the scanning volume object in case of success and a negative error code 253 * to the allocated "av" object in case of success and a negative error code in
249 * in case of failure. 254 * case of failure.
250 */ 255 */
251static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id, 256static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
252 int pnum, 257 int vol_id, int pnum,
253 const struct ubi_vid_hdr *vid_hdr) 258 const struct ubi_vid_hdr *vid_hdr)
254{ 259{
255 struct ubi_scan_volume *sv; 260 struct ubi_ainf_volume *av;
256 struct rb_node **p = &si->volumes.rb_node, *parent = NULL; 261 struct rb_node **p = &ai->volumes.rb_node, *parent = NULL;
257 262
258 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); 263 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
259 264
260 /* Walk the volume RB-tree to look if this volume is already present */ 265 /* Walk the volume RB-tree to look if this volume is already present */
261 while (*p) { 266 while (*p) {
262 parent = *p; 267 parent = *p;
263 sv = rb_entry(parent, struct ubi_scan_volume, rb); 268 av = rb_entry(parent, struct ubi_ainf_volume, rb);
264 269
265 if (vol_id == sv->vol_id) 270 if (vol_id == av->vol_id)
266 return sv; 271 return av;
267 272
268 if (vol_id > sv->vol_id) 273 if (vol_id > av->vol_id)
269 p = &(*p)->rb_left; 274 p = &(*p)->rb_left;
270 else 275 else
271 p = &(*p)->rb_right; 276 p = &(*p)->rb_right;
272 } 277 }
273 278
274 /* The volume is absent - add it */ 279 /* The volume is absent - add it */
275 sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL); 280 av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL);
276 if (!sv) 281 if (!av)
277 return ERR_PTR(-ENOMEM); 282 return ERR_PTR(-ENOMEM);
278 283
279 sv->highest_lnum = sv->leb_count = 0; 284 av->highest_lnum = av->leb_count = 0;
280 sv->vol_id = vol_id; 285 av->vol_id = vol_id;
281 sv->root = RB_ROOT; 286 av->root = RB_ROOT;
282 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs); 287 av->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
283 sv->data_pad = be32_to_cpu(vid_hdr->data_pad); 288 av->data_pad = be32_to_cpu(vid_hdr->data_pad);
284 sv->compat = vid_hdr->compat; 289 av->compat = vid_hdr->compat;
285 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME 290 av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
286 : UBI_STATIC_VOLUME; 291 : UBI_STATIC_VOLUME;
287 if (vol_id > si->highest_vol_id) 292 if (vol_id > ai->highest_vol_id)
288 si->highest_vol_id = vol_id; 293 ai->highest_vol_id = vol_id;
289 294
290 rb_link_node(&sv->rb, parent, p); 295 rb_link_node(&av->rb, parent, p);
291 rb_insert_color(&sv->rb, &si->volumes); 296 rb_insert_color(&av->rb, &ai->volumes);
292 si->vols_found += 1; 297 ai->vols_found += 1;
293 dbg_bld("added volume %d", vol_id); 298 dbg_bld("added volume %d", vol_id);
294 return sv; 299 return av;
295} 300}
296 301
297/** 302/**
298 * compare_lebs - find out which logical eraseblock is newer. 303 * compare_lebs - find out which logical eraseblock is newer.
299 * @ubi: UBI device description object 304 * @ubi: UBI device description object
300 * @seb: first logical eraseblock to compare 305 * @aeb: first logical eraseblock to compare
301 * @pnum: physical eraseblock number of the second logical eraseblock to 306 * @pnum: physical eraseblock number of the second logical eraseblock to
302 * compare 307 * compare
303 * @vid_hdr: volume identifier header of the second logical eraseblock 308 * @vid_hdr: volume identifier header of the second logical eraseblock
@@ -306,7 +311,7 @@ static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
306 * case of success this function returns a positive value, in case of failure, a 311 * case of success this function returns a positive value, in case of failure, a
307 * negative error code is returned. The success return codes use the following 312 * negative error code is returned. The success return codes use the following
308 * bits: 313 * bits:
309 * o bit 0 is cleared: the first PEB (described by @seb) is newer than the 314 * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the
310 * second PEB (described by @pnum and @vid_hdr); 315 * second PEB (described by @pnum and @vid_hdr);
311 * o bit 0 is set: the second PEB is newer; 316 * o bit 0 is set: the second PEB is newer;
312 * o bit 1 is cleared: no bit-flips were detected in the newer LEB; 317 * o bit 1 is cleared: no bit-flips were detected in the newer LEB;
@@ -314,7 +319,7 @@ static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
314 * o bit 2 is cleared: the older LEB is not corrupted; 319 * o bit 2 is cleared: the older LEB is not corrupted;
315 * o bit 2 is set: the older LEB is corrupted. 320 * o bit 2 is set: the older LEB is corrupted.
316 */ 321 */
317static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb, 322static int compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
318 int pnum, const struct ubi_vid_hdr *vid_hdr) 323 int pnum, const struct ubi_vid_hdr *vid_hdr)
319{ 324{
320 void *buf; 325 void *buf;
@@ -323,7 +328,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
323 struct ubi_vid_hdr *vh = NULL; 328 struct ubi_vid_hdr *vh = NULL;
324 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); 329 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
325 330
326 if (sqnum2 == seb->sqnum) { 331 if (sqnum2 == aeb->sqnum) {
327 /* 332 /*
328 * This must be a really ancient UBI image which has been 333 * This must be a really ancient UBI image which has been
329 * created before sequence numbers support has been added. At 334 * created before sequence numbers support has been added. At
@@ -337,7 +342,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
337 } 342 }
338 343
339 /* Obviously the LEB with lower sequence counter is older */ 344 /* Obviously the LEB with lower sequence counter is older */
340 second_is_newer = !!(sqnum2 > seb->sqnum); 345 second_is_newer = (sqnum2 > aeb->sqnum);
341 346
342 /* 347 /*
343 * Now we know which copy is newer. If the copy flag of the PEB with 348 * Now we know which copy is newer. If the copy flag of the PEB with
@@ -356,7 +361,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
356 return 1; 361 return 1;
357 } 362 }
358 } else { 363 } else {
359 if (!seb->copy_flag) { 364 if (!aeb->copy_flag) {
360 /* It is not a copy, so it is newer */ 365 /* It is not a copy, so it is newer */
361 dbg_bld("first PEB %d is newer, copy_flag is unset", 366 dbg_bld("first PEB %d is newer, copy_flag is unset",
362 pnum); 367 pnum);
@@ -367,13 +372,13 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
367 if (!vh) 372 if (!vh)
368 return -ENOMEM; 373 return -ENOMEM;
369 374
370 pnum = seb->pnum; 375 pnum = aeb->pnum;
371 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 376 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
372 if (err) { 377 if (err) {
373 if (err == UBI_IO_BITFLIPS) 378 if (err == UBI_IO_BITFLIPS)
374 bitflips = 1; 379 bitflips = 1;
375 else { 380 else {
376 dbg_err("VID of PEB %d header is bad, but it " 381 ubi_err("VID of PEB %d header is bad, but it "
377 "was OK earlier, err %d", pnum, err); 382 "was OK earlier, err %d", pnum, err);
378 if (err > 0) 383 if (err > 0)
379 err = -EIO; 384 err = -EIO;
@@ -429,9 +434,9 @@ out_free_vidh:
429} 434}
430 435
431/** 436/**
432 * ubi_scan_add_used - add physical eraseblock to the scanning information. 437 * ubi_add_to_av - add used physical eraseblock to the attaching information.
433 * @ubi: UBI device description object 438 * @ubi: UBI device description object
434 * @si: scanning information 439 * @ai: attaching information
435 * @pnum: the physical eraseblock number 440 * @pnum: the physical eraseblock number
436 * @ec: erase counter 441 * @ec: erase counter
437 * @vid_hdr: the volume identifier header 442 * @vid_hdr: the volume identifier header
@@ -444,14 +449,13 @@ out_free_vidh:
444 * to be picked, while the older one has to be dropped. This function returns 449 * to be picked, while the older one has to be dropped. This function returns
445 * zero in case of success and a negative error code in case of failure. 450 * zero in case of success and a negative error code in case of failure.
446 */ 451 */
447int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si, 452int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
448 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, 453 int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips)
449 int bitflips)
450{ 454{
451 int err, vol_id, lnum; 455 int err, vol_id, lnum;
452 unsigned long long sqnum; 456 unsigned long long sqnum;
453 struct ubi_scan_volume *sv; 457 struct ubi_ainf_volume *av;
454 struct ubi_scan_leb *seb; 458 struct ubi_ainf_peb *aeb;
455 struct rb_node **p, *parent = NULL; 459 struct rb_node **p, *parent = NULL;
456 460
457 vol_id = be32_to_cpu(vid_hdr->vol_id); 461 vol_id = be32_to_cpu(vid_hdr->vol_id);
@@ -461,25 +465,25 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
461 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d", 465 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
462 pnum, vol_id, lnum, ec, sqnum, bitflips); 466 pnum, vol_id, lnum, ec, sqnum, bitflips);
463 467
464 sv = add_volume(si, vol_id, pnum, vid_hdr); 468 av = add_volume(ai, vol_id, pnum, vid_hdr);
465 if (IS_ERR(sv)) 469 if (IS_ERR(av))
466 return PTR_ERR(sv); 470 return PTR_ERR(av);
467 471
468 if (si->max_sqnum < sqnum) 472 if (ai->max_sqnum < sqnum)
469 si->max_sqnum = sqnum; 473 ai->max_sqnum = sqnum;
470 474
471 /* 475 /*
472 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look 476 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
473 * if this is the first instance of this logical eraseblock or not. 477 * if this is the first instance of this logical eraseblock or not.
474 */ 478 */
475 p = &sv->root.rb_node; 479 p = &av->root.rb_node;
476 while (*p) { 480 while (*p) {
477 int cmp_res; 481 int cmp_res;
478 482
479 parent = *p; 483 parent = *p;
480 seb = rb_entry(parent, struct ubi_scan_leb, u.rb); 484 aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb);
481 if (lnum != seb->lnum) { 485 if (lnum != aeb->lnum) {
482 if (lnum < seb->lnum) 486 if (lnum < aeb->lnum)
483 p = &(*p)->rb_left; 487 p = &(*p)->rb_left;
484 else 488 else
485 p = &(*p)->rb_right; 489 p = &(*p)->rb_right;
@@ -491,8 +495,8 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
491 * logical eraseblock present. 495 * logical eraseblock present.
492 */ 496 */
493 497
494 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, " 498 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d",
495 "EC %d", seb->pnum, seb->sqnum, seb->ec); 499 aeb->pnum, aeb->sqnum, aeb->ec);
496 500
497 /* 501 /*
498 * Make sure that the logical eraseblocks have different 502 * Make sure that the logical eraseblocks have different
@@ -507,11 +511,11 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
507 * images, but refuse attaching old images with duplicated 511 * images, but refuse attaching old images with duplicated
508 * logical eraseblocks because there was an unclean reboot. 512 * logical eraseblocks because there was an unclean reboot.
509 */ 513 */
510 if (seb->sqnum == sqnum && sqnum != 0) { 514 if (aeb->sqnum == sqnum && sqnum != 0) {
511 ubi_err("two LEBs with same sequence number %llu", 515 ubi_err("two LEBs with same sequence number %llu",
512 sqnum); 516 sqnum);
513 ubi_dbg_dump_seb(seb, 0); 517 ubi_dump_aeb(aeb, 0);
514 ubi_dbg_dump_vid_hdr(vid_hdr); 518 ubi_dump_vid_hdr(vid_hdr);
515 return -EINVAL; 519 return -EINVAL;
516 } 520 }
517 521
@@ -519,7 +523,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
519 * Now we have to drop the older one and preserve the newer 523 * Now we have to drop the older one and preserve the newer
520 * one. 524 * one.
521 */ 525 */
522 cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr); 526 cmp_res = compare_lebs(ubi, aeb, pnum, vid_hdr);
523 if (cmp_res < 0) 527 if (cmp_res < 0)
524 return cmp_res; 528 return cmp_res;
525 529
@@ -528,23 +532,26 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
528 * This logical eraseblock is newer than the one 532 * This logical eraseblock is newer than the one
529 * found earlier. 533 * found earlier.
530 */ 534 */
531 err = validate_vid_hdr(vid_hdr, sv, pnum); 535 err = validate_vid_hdr(vid_hdr, av, pnum);
532 if (err) 536 if (err)
533 return err; 537 return err;
534 538
535 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4, 539 err = add_to_list(ai, aeb->pnum, aeb->vol_id,
536 &si->erase); 540 aeb->lnum, aeb->ec, cmp_res & 4,
541 &ai->erase);
537 if (err) 542 if (err)
538 return err; 543 return err;
539 544
540 seb->ec = ec; 545 aeb->ec = ec;
541 seb->pnum = pnum; 546 aeb->pnum = pnum;
542 seb->scrub = ((cmp_res & 2) || bitflips); 547 aeb->vol_id = vol_id;
543 seb->copy_flag = vid_hdr->copy_flag; 548 aeb->lnum = lnum;
544 seb->sqnum = sqnum; 549 aeb->scrub = ((cmp_res & 2) || bitflips);
550 aeb->copy_flag = vid_hdr->copy_flag;
551 aeb->sqnum = sqnum;
545 552
546 if (sv->highest_lnum == lnum) 553 if (av->highest_lnum == lnum)
547 sv->last_data_size = 554 av->last_data_size =
548 be32_to_cpu(vid_hdr->data_size); 555 be32_to_cpu(vid_hdr->data_size);
549 556
550 return 0; 557 return 0;
@@ -553,92 +560,64 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
553 * This logical eraseblock is older than the one found 560 * This logical eraseblock is older than the one found
554 * previously. 561 * previously.
555 */ 562 */
556 return add_to_list(si, pnum, ec, cmp_res & 4, 563 return add_to_list(ai, pnum, vol_id, lnum, ec,
557 &si->erase); 564 cmp_res & 4, &ai->erase);
558 } 565 }
559 } 566 }
560 567
561 /* 568 /*
562 * We've met this logical eraseblock for the first time, add it to the 569 * We've met this logical eraseblock for the first time, add it to the
563 * scanning information. 570 * attaching information.
564 */ 571 */
565 572
566 err = validate_vid_hdr(vid_hdr, sv, pnum); 573 err = validate_vid_hdr(vid_hdr, av, pnum);
567 if (err) 574 if (err)
568 return err; 575 return err;
569 576
570 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL); 577 aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
571 if (!seb) 578 if (!aeb)
572 return -ENOMEM; 579 return -ENOMEM;
573 580
574 seb->ec = ec; 581 aeb->ec = ec;
575 seb->pnum = pnum; 582 aeb->pnum = pnum;
576 seb->lnum = lnum; 583 aeb->vol_id = vol_id;
577 seb->scrub = bitflips; 584 aeb->lnum = lnum;
578 seb->copy_flag = vid_hdr->copy_flag; 585 aeb->scrub = bitflips;
579 seb->sqnum = sqnum; 586 aeb->copy_flag = vid_hdr->copy_flag;
580 587 aeb->sqnum = sqnum;
581 if (sv->highest_lnum <= lnum) { 588
582 sv->highest_lnum = lnum; 589 if (av->highest_lnum <= lnum) {
583 sv->last_data_size = be32_to_cpu(vid_hdr->data_size); 590 av->highest_lnum = lnum;
591 av->last_data_size = be32_to_cpu(vid_hdr->data_size);
584 } 592 }
585 593
586 sv->leb_count += 1; 594 av->leb_count += 1;
587 rb_link_node(&seb->u.rb, parent, p); 595 rb_link_node(&aeb->u.rb, parent, p);
588 rb_insert_color(&seb->u.rb, &sv->root); 596 rb_insert_color(&aeb->u.rb, &av->root);
589 return 0; 597 return 0;
590} 598}
591 599
592/** 600/**
593 * ubi_scan_find_sv - find volume in the scanning information. 601 * ubi_find_av - find volume in the attaching information.
594 * @si: scanning information 602 * @ai: attaching information
595 * @vol_id: the requested volume ID 603 * @vol_id: the requested volume ID
596 * 604 *
597 * This function returns a pointer to the volume description or %NULL if there 605 * This function returns a pointer to the volume description or %NULL if there
598 * are no data about this volume in the scanning information. 606 * are no data about this volume in the attaching information.
599 */
600struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
601 int vol_id)
602{
603 struct ubi_scan_volume *sv;
604 struct rb_node *p = si->volumes.rb_node;
605
606 while (p) {
607 sv = rb_entry(p, struct ubi_scan_volume, rb);
608
609 if (vol_id == sv->vol_id)
610 return sv;
611
612 if (vol_id > sv->vol_id)
613 p = p->rb_left;
614 else
615 p = p->rb_right;
616 }
617
618 return NULL;
619}
620
621/**
622 * ubi_scan_find_seb - find LEB in the volume scanning information.
623 * @sv: a pointer to the volume scanning information
624 * @lnum: the requested logical eraseblock
625 *
626 * This function returns a pointer to the scanning logical eraseblock or %NULL
627 * if there are no data about it in the scanning volume information.
628 */ 607 */
629struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, 608struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
630 int lnum) 609 int vol_id)
631{ 610{
632 struct ubi_scan_leb *seb; 611 struct ubi_ainf_volume *av;
633 struct rb_node *p = sv->root.rb_node; 612 struct rb_node *p = ai->volumes.rb_node;
634 613
635 while (p) { 614 while (p) {
636 seb = rb_entry(p, struct ubi_scan_leb, u.rb); 615 av = rb_entry(p, struct ubi_ainf_volume, rb);
637 616
638 if (lnum == seb->lnum) 617 if (vol_id == av->vol_id)
639 return seb; 618 return av;
640 619
641 if (lnum > seb->lnum) 620 if (vol_id > av->vol_id)
642 p = p->rb_left; 621 p = p->rb_left;
643 else 622 else
644 p = p->rb_right; 623 p = p->rb_right;
@@ -648,34 +627,34 @@ struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
648} 627}
649 628
650/** 629/**
651 * ubi_scan_rm_volume - delete scanning information about a volume. 630 * ubi_remove_av - delete attaching information about a volume.
652 * @si: scanning information 631 * @ai: attaching information
653 * @sv: the volume scanning information to delete 632 * @av: the volume attaching information to delete
654 */ 633 */
655void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv) 634void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
656{ 635{
657 struct rb_node *rb; 636 struct rb_node *rb;
658 struct ubi_scan_leb *seb; 637 struct ubi_ainf_peb *aeb;
659 638
660 dbg_bld("remove scanning information about volume %d", sv->vol_id); 639 dbg_bld("remove attaching information about volume %d", av->vol_id);
661 640
662 while ((rb = rb_first(&sv->root))) { 641 while ((rb = rb_first(&av->root))) {
663 seb = rb_entry(rb, struct ubi_scan_leb, u.rb); 642 aeb = rb_entry(rb, struct ubi_ainf_peb, u.rb);
664 rb_erase(&seb->u.rb, &sv->root); 643 rb_erase(&aeb->u.rb, &av->root);
665 list_add_tail(&seb->u.list, &si->erase); 644 list_add_tail(&aeb->u.list, &ai->erase);
666 } 645 }
667 646
668 rb_erase(&sv->rb, &si->volumes); 647 rb_erase(&av->rb, &ai->volumes);
669 kfree(sv); 648 kfree(av);
670 si->vols_found -= 1; 649 ai->vols_found -= 1;
671} 650}
672 651
673/** 652/**
674 * ubi_scan_erase_peb - erase a physical eraseblock. 653 * early_erase_peb - erase a physical eraseblock.
675 * @ubi: UBI device description object 654 * @ubi: UBI device description object
676 * @si: scanning information 655 * @ai: attaching information
677 * @pnum: physical eraseblock number to erase; 656 * @pnum: physical eraseblock number to erase;
678 * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) 657 * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown)
679 * 658 *
680 * This function erases physical eraseblock 'pnum', and writes the erase 659 * This function erases physical eraseblock 'pnum', and writes the erase
681 * counter header to it. This function should only be used on UBI device 660 * counter header to it. This function should only be used on UBI device
@@ -683,8 +662,8 @@ void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
683 * This function returns zero in case of success and a negative error code in 662 * This function returns zero in case of success and a negative error code in
684 * case of failure. 663 * case of failure.
685 */ 664 */
686int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si, 665static int early_erase_peb(struct ubi_device *ubi,
687 int pnum, int ec) 666 const struct ubi_attach_info *ai, int pnum, int ec)
688{ 667{
689 int err; 668 int err;
690 struct ubi_ec_hdr *ec_hdr; 669 struct ubi_ec_hdr *ec_hdr;
@@ -716,9 +695,9 @@ out_free:
716} 695}
717 696
718/** 697/**
719 * ubi_scan_get_free_peb - get a free physical eraseblock. 698 * ubi_early_get_peb - get a free physical eraseblock.
720 * @ubi: UBI device description object 699 * @ubi: UBI device description object
721 * @si: scanning information 700 * @ai: attaching information
722 * 701 *
723 * This function returns a free physical eraseblock. It is supposed to be 702 * This function returns a free physical eraseblock. It is supposed to be
724 * called on the UBI initialization stages when the wear-leveling sub-system is 703 * called on the UBI initialization stages when the wear-leveling sub-system is
@@ -726,20 +705,20 @@ out_free:
726 * the lists, writes the EC header if it is needed, and removes it from the 705 * the lists, writes the EC header if it is needed, and removes it from the
727 * list. 706 * list.
728 * 707 *
729 * This function returns scanning physical eraseblock information in case of 708 * This function returns a pointer to the "aeb" of the found free PEB in case
730 * success and an error code in case of failure. 709 * of success and an error code in case of failure.
731 */ 710 */
732struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, 711struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
733 struct ubi_scan_info *si) 712 struct ubi_attach_info *ai)
734{ 713{
735 int err = 0; 714 int err = 0;
736 struct ubi_scan_leb *seb, *tmp_seb; 715 struct ubi_ainf_peb *aeb, *tmp_aeb;
737 716
738 if (!list_empty(&si->free)) { 717 if (!list_empty(&ai->free)) {
739 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); 718 aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list);
740 list_del(&seb->u.list); 719 list_del(&aeb->u.list);
741 dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec); 720 dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec);
742 return seb; 721 return aeb;
743 } 722 }
744 723
745 /* 724 /*
@@ -748,18 +727,18 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
748 * so forth. We don't want to take care about bad eraseblocks here - 727 * so forth. We don't want to take care about bad eraseblocks here -
749 * they'll be handled later. 728 * they'll be handled later.
750 */ 729 */
751 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) { 730 list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) {
752 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 731 if (aeb->ec == UBI_UNKNOWN)
753 seb->ec = si->mean_ec; 732 aeb->ec = ai->mean_ec;
754 733
755 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 734 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1);
756 if (err) 735 if (err)
757 continue; 736 continue;
758 737
759 seb->ec += 1; 738 aeb->ec += 1;
760 list_del(&seb->u.list); 739 list_del(&aeb->u.list);
761 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); 740 dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec);
762 return seb; 741 return aeb;
763 } 742 }
764 743
765 ubi_err("no free eraseblocks"); 744 ubi_err("no free eraseblocks");
@@ -814,7 +793,7 @@ static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
814 ubi_err("PEB %d contains corrupted VID header, and the data does not " 793 ubi_err("PEB %d contains corrupted VID header, and the data does not "
815 "contain all 0xFF, this may be a non-UBI PEB or a severe VID " 794 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
816 "header corruption which requires manual inspection", pnum); 795 "header corruption which requires manual inspection", pnum);
817 ubi_dbg_dump_vid_hdr(vid_hdr); 796 ubi_dump_vid_hdr(vid_hdr);
818 dbg_msg("hexdump of PEB %d offset %d, length %d", 797 dbg_msg("hexdump of PEB %d offset %d, length %d",
819 pnum, ubi->leb_start, ubi->leb_size); 798 pnum, ubi->leb_start, ubi->leb_size);
820 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 799 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
@@ -827,16 +806,18 @@ out_unlock:
827} 806}
828 807
829/** 808/**
830 * process_eb - read, check UBI headers, and add them to scanning information. 809 * scan_peb - scan and process UBI headers of a PEB.
831 * @ubi: UBI device description object 810 * @ubi: UBI device description object
832 * @si: scanning information 811 * @ai: attaching information
833 * @pnum: the physical eraseblock number 812 * @pnum: the physical eraseblock number
834 * 813 *
835 * This function returns a zero if the physical eraseblock was successfully 814 * This function reads UBI headers of PEB @pnum, checks them, and adds
836 * handled and a negative error code in case of failure. 815 * information about this PEB to the corresponding list or RB-tree in the
816 * "attaching info" structure. Returns zero if the physical eraseblock was
817 * successfully handled and a negative error code in case of failure.
837 */ 818 */
838static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, 819static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
839 int pnum) 820 int pnum)
840{ 821{
841 long long uninitialized_var(ec); 822 long long uninitialized_var(ec);
842 int err, bitflips = 0, vol_id, ec_err = 0; 823 int err, bitflips = 0, vol_id, ec_err = 0;
@@ -848,12 +829,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
848 if (err < 0) 829 if (err < 0)
849 return err; 830 return err;
850 else if (err) { 831 else if (err) {
851 /* 832 ai->bad_peb_count += 1;
852 * FIXME: this is actually duty of the I/O sub-system to
853 * initialize this, but MTD does not provide enough
854 * information.
855 */
856 si->bad_peb_count += 1;
857 return 0; 833 return 0;
858 } 834 }
859 835
@@ -867,13 +843,13 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
867 bitflips = 1; 843 bitflips = 1;
868 break; 844 break;
869 case UBI_IO_FF: 845 case UBI_IO_FF:
870 si->empty_peb_count += 1; 846 ai->empty_peb_count += 1;
871 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0, 847 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
872 &si->erase); 848 UBI_UNKNOWN, 0, &ai->erase);
873 case UBI_IO_FF_BITFLIPS: 849 case UBI_IO_FF_BITFLIPS:
874 si->empty_peb_count += 1; 850 ai->empty_peb_count += 1;
875 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1, 851 return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
876 &si->erase); 852 UBI_UNKNOWN, 1, &ai->erase);
877 case UBI_IO_BAD_HDR_EBADMSG: 853 case UBI_IO_BAD_HDR_EBADMSG:
878 case UBI_IO_BAD_HDR: 854 case UBI_IO_BAD_HDR:
879 /* 855 /*
@@ -882,7 +858,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
882 * moved and EC be re-created. 858 * moved and EC be re-created.
883 */ 859 */
884 ec_err = err; 860 ec_err = err;
885 ec = UBI_SCAN_UNKNOWN_EC; 861 ec = UBI_UNKNOWN;
886 bitflips = 1; 862 bitflips = 1;
887 break; 863 break;
888 default: 864 default:
@@ -911,7 +887,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
911 */ 887 */
912 ubi_err("erase counter overflow, max is %d", 888 ubi_err("erase counter overflow, max is %d",
913 UBI_MAX_ERASECOUNTER); 889 UBI_MAX_ERASECOUNTER);
914 ubi_dbg_dump_ec_hdr(ech); 890 ubi_dump_ec_hdr(ech);
915 return -EINVAL; 891 return -EINVAL;
916 } 892 }
917 893
@@ -933,7 +909,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
933 ubi->image_seq != image_seq) { 909 ubi->image_seq != image_seq) {
934 ubi_err("bad image sequence number %d in PEB %d, " 910 ubi_err("bad image sequence number %d in PEB %d, "
935 "expected %d", image_seq, pnum, ubi->image_seq); 911 "expected %d", image_seq, pnum, ubi->image_seq);
936 ubi_dbg_dump_ec_hdr(ech); 912 ubi_dump_ec_hdr(ech);
937 return -EINVAL; 913 return -EINVAL;
938 } 914 }
939 } 915 }
@@ -957,7 +933,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
957 * PEB, bit it is not marked as bad yet. This may also 933 * PEB, bit it is not marked as bad yet. This may also
958 * be a result of power cut during erasure. 934 * be a result of power cut during erasure.
959 */ 935 */
960 si->maybe_bad_peb_count += 1; 936 ai->maybe_bad_peb_count += 1;
961 case UBI_IO_BAD_HDR: 937 case UBI_IO_BAD_HDR:
962 if (ec_err) 938 if (ec_err)
963 /* 939 /*
@@ -984,23 +960,27 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
984 return err; 960 return err;
985 else if (!err) 961 else if (!err)
986 /* This corruption is caused by a power cut */ 962 /* This corruption is caused by a power cut */
987 err = add_to_list(si, pnum, ec, 1, &si->erase); 963 err = add_to_list(ai, pnum, UBI_UNKNOWN,
964 UBI_UNKNOWN, ec, 1, &ai->erase);
988 else 965 else
989 /* This is an unexpected corruption */ 966 /* This is an unexpected corruption */
990 err = add_corrupted(si, pnum, ec); 967 err = add_corrupted(ai, pnum, ec);
991 if (err) 968 if (err)
992 return err; 969 return err;
993 goto adjust_mean_ec; 970 goto adjust_mean_ec;
994 case UBI_IO_FF_BITFLIPS: 971 case UBI_IO_FF_BITFLIPS:
995 err = add_to_list(si, pnum, ec, 1, &si->erase); 972 err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN,
973 ec, 1, &ai->erase);
996 if (err) 974 if (err)
997 return err; 975 return err;
998 goto adjust_mean_ec; 976 goto adjust_mean_ec;
999 case UBI_IO_FF: 977 case UBI_IO_FF:
1000 if (ec_err) 978 if (ec_err)
1001 err = add_to_list(si, pnum, ec, 1, &si->erase); 979 err = add_to_list(ai, pnum, UBI_UNKNOWN,
980 UBI_UNKNOWN, ec, 1, &ai->erase);
1002 else 981 else
1003 err = add_to_list(si, pnum, ec, 0, &si->free); 982 err = add_to_list(ai, pnum, UBI_UNKNOWN,
983 UBI_UNKNOWN, ec, 0, &ai->free);
1004 if (err) 984 if (err)
1005 return err; 985 return err;
1006 goto adjust_mean_ec; 986 goto adjust_mean_ec;
@@ -1019,7 +999,8 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
1019 case UBI_COMPAT_DELETE: 999 case UBI_COMPAT_DELETE:
1020 ubi_msg("\"delete\" compatible internal volume %d:%d" 1000 ubi_msg("\"delete\" compatible internal volume %d:%d"
1021 " found, will remove it", vol_id, lnum); 1001 " found, will remove it", vol_id, lnum);
1022 err = add_to_list(si, pnum, ec, 1, &si->erase); 1002 err = add_to_list(ai, pnum, vol_id, lnum,
1003 ec, 1, &ai->erase);
1023 if (err) 1004 if (err)
1024 return err; 1005 return err;
1025 return 0; 1006 return 0;
@@ -1034,7 +1015,8 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
1034 case UBI_COMPAT_PRESERVE: 1015 case UBI_COMPAT_PRESERVE:
1035 ubi_msg("\"preserve\" compatible internal volume %d:%d" 1016 ubi_msg("\"preserve\" compatible internal volume %d:%d"
1036 " found", vol_id, lnum); 1017 " found", vol_id, lnum);
1037 err = add_to_list(si, pnum, ec, 0, &si->alien); 1018 err = add_to_list(ai, pnum, vol_id, lnum,
1019 ec, 0, &ai->alien);
1038 if (err) 1020 if (err)
1039 return err; 1021 return err;
1040 return 0; 1022 return 0;
@@ -1049,40 +1031,40 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
1049 if (ec_err) 1031 if (ec_err)
1050 ubi_warn("valid VID header but corrupted EC header at PEB %d", 1032 ubi_warn("valid VID header but corrupted EC header at PEB %d",
1051 pnum); 1033 pnum);
1052 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); 1034 err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips);
1053 if (err) 1035 if (err)
1054 return err; 1036 return err;
1055 1037
1056adjust_mean_ec: 1038adjust_mean_ec:
1057 if (!ec_err) { 1039 if (!ec_err) {
1058 si->ec_sum += ec; 1040 ai->ec_sum += ec;
1059 si->ec_count += 1; 1041 ai->ec_count += 1;
1060 if (ec > si->max_ec) 1042 if (ec > ai->max_ec)
1061 si->max_ec = ec; 1043 ai->max_ec = ec;
1062 if (ec < si->min_ec) 1044 if (ec < ai->min_ec)
1063 si->min_ec = ec; 1045 ai->min_ec = ec;
1064 } 1046 }
1065 1047
1066 return 0; 1048 return 0;
1067} 1049}
1068 1050
1069/** 1051/**
1070 * check_what_we_have - check what PEB were found by scanning. 1052 * late_analysis - analyze the overall situation with PEB.
1071 * @ubi: UBI device description object 1053 * @ubi: UBI device description object
1072 * @si: scanning information 1054 * @ai: attaching information
1073 * 1055 *
1074 * This is a helper function which takes a look what PEBs were found by 1056 * This is a helper function which takes a look what PEBs we have after we
1075 * scanning, and decides whether the flash is empty and should be formatted and 1057 * gather information about all of them ("ai" is compete). It decides whether
1076 * whether there are too many corrupted PEBs and we should not attach this 1058 * the flash is empty and should be formatted of whether there are too many
1077 * MTD device. Returns zero if we should proceed with attaching the MTD device, 1059 * corrupted PEBs and we should not attach this MTD device. Returns zero if we
1078 * and %-EINVAL if we should not. 1060 * should proceed with attaching the MTD device, and %-EINVAL if we should not.
1079 */ 1061 */
1080static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si) 1062static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai)
1081{ 1063{
1082 struct ubi_scan_leb *seb; 1064 struct ubi_ainf_peb *aeb;
1083 int max_corr, peb_count; 1065 int max_corr, peb_count;
1084 1066
1085 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count; 1067 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count;
1086 max_corr = peb_count / 20 ?: 8; 1068 max_corr = peb_count / 20 ?: 8;
1087 1069
1088 /* 1070 /*
@@ -1090,25 +1072,25 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
1090 * unclean reboots. However, many of them may indicate some problems 1072 * unclean reboots. However, many of them may indicate some problems
1091 * with the flash HW or driver. 1073 * with the flash HW or driver.
1092 */ 1074 */
1093 if (si->corr_peb_count) { 1075 if (ai->corr_peb_count) {
1094 ubi_err("%d PEBs are corrupted and preserved", 1076 ubi_err("%d PEBs are corrupted and preserved",
1095 si->corr_peb_count); 1077 ai->corr_peb_count);
1096 printk(KERN_ERR "Corrupted PEBs are:"); 1078 printk(KERN_ERR "Corrupted PEBs are:");
1097 list_for_each_entry(seb, &si->corr, u.list) 1079 list_for_each_entry(aeb, &ai->corr, u.list)
1098 printk(KERN_CONT " %d", seb->pnum); 1080 printk(KERN_CONT " %d", aeb->pnum);
1099 printk(KERN_CONT "\n"); 1081 printk(KERN_CONT "\n");
1100 1082
1101 /* 1083 /*
1102 * If too many PEBs are corrupted, we refuse attaching, 1084 * If too many PEBs are corrupted, we refuse attaching,
1103 * otherwise, only print a warning. 1085 * otherwise, only print a warning.
1104 */ 1086 */
1105 if (si->corr_peb_count >= max_corr) { 1087 if (ai->corr_peb_count >= max_corr) {
1106 ubi_err("too many corrupted PEBs, refusing"); 1088 ubi_err("too many corrupted PEBs, refusing");
1107 return -EINVAL; 1089 return -EINVAL;
1108 } 1090 }
1109 } 1091 }
1110 1092
1111 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) { 1093 if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) {
1112 /* 1094 /*
1113 * All PEBs are empty, or almost all - a couple PEBs look like 1095 * All PEBs are empty, or almost all - a couple PEBs look like
1114 * they may be bad PEBs which were not marked as bad yet. 1096 * they may be bad PEBs which were not marked as bad yet.
@@ -1124,8 +1106,8 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
1124 * 2. Flash contains non-UBI data and we do not want to format 1106 * 2. Flash contains non-UBI data and we do not want to format
1125 * it and destroy possibly important information. 1107 * it and destroy possibly important information.
1126 */ 1108 */
1127 if (si->maybe_bad_peb_count <= 2) { 1109 if (ai->maybe_bad_peb_count <= 2) {
1128 si->is_empty = 1; 1110 ai->is_empty = 1;
1129 ubi_msg("empty MTD device detected"); 1111 ubi_msg("empty MTD device detected");
1130 get_random_bytes(&ubi->image_seq, 1112 get_random_bytes(&ubi->image_seq,
1131 sizeof(ubi->image_seq)); 1113 sizeof(ubi->image_seq));
@@ -1141,40 +1123,41 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
1141} 1123}
1142 1124
1143/** 1125/**
1144 * ubi_scan - scan an MTD device. 1126 * scan_all - scan entire MTD device.
1145 * @ubi: UBI device description object 1127 * @ubi: UBI device description object
1146 * 1128 *
1147 * This function does full scanning of an MTD device and returns complete 1129 * This function does full scanning of an MTD device and returns complete
1148 * information about it. In case of failure, an error code is returned. 1130 * information about it in form of a "struct ubi_attach_info" object. In case
1131 * of failure, an error code is returned.
1149 */ 1132 */
1150struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) 1133static struct ubi_attach_info *scan_all(struct ubi_device *ubi)
1151{ 1134{
1152 int err, pnum; 1135 int err, pnum;
1153 struct rb_node *rb1, *rb2; 1136 struct rb_node *rb1, *rb2;
1154 struct ubi_scan_volume *sv; 1137 struct ubi_ainf_volume *av;
1155 struct ubi_scan_leb *seb; 1138 struct ubi_ainf_peb *aeb;
1156 struct ubi_scan_info *si; 1139 struct ubi_attach_info *ai;
1157 1140
1158 si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL); 1141 ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL);
1159 if (!si) 1142 if (!ai)
1160 return ERR_PTR(-ENOMEM); 1143 return ERR_PTR(-ENOMEM);
1161 1144
1162 INIT_LIST_HEAD(&si->corr); 1145 INIT_LIST_HEAD(&ai->corr);
1163 INIT_LIST_HEAD(&si->free); 1146 INIT_LIST_HEAD(&ai->free);
1164 INIT_LIST_HEAD(&si->erase); 1147 INIT_LIST_HEAD(&ai->erase);
1165 INIT_LIST_HEAD(&si->alien); 1148 INIT_LIST_HEAD(&ai->alien);
1166 si->volumes = RB_ROOT; 1149 ai->volumes = RB_ROOT;
1167 1150
1168 err = -ENOMEM; 1151 err = -ENOMEM;
1169 si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab", 1152 ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache",
1170 sizeof(struct ubi_scan_leb), 1153 sizeof(struct ubi_ainf_peb),
1171 0, 0, NULL); 1154 0, 0, NULL);
1172 if (!si->scan_leb_slab) 1155 if (!ai->aeb_slab_cache)
1173 goto out_si; 1156 goto out_ai;
1174 1157
1175 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1158 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
1176 if (!ech) 1159 if (!ech)
1177 goto out_si; 1160 goto out_ai;
1178 1161
1179 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1162 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1180 if (!vidh) 1163 if (!vidh)
@@ -1184,7 +1167,7 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1184 cond_resched(); 1167 cond_resched();
1185 1168
1186 dbg_gen("process PEB %d", pnum); 1169 dbg_gen("process PEB %d", pnum);
1187 err = process_eb(ubi, si, pnum); 1170 err = scan_peb(ubi, ai, pnum);
1188 if (err < 0) 1171 if (err < 0)
1189 goto out_vidh; 1172 goto out_vidh;
1190 } 1173 }
@@ -1192,10 +1175,10 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1192 dbg_msg("scanning is finished"); 1175 dbg_msg("scanning is finished");
1193 1176
1194 /* Calculate mean erase counter */ 1177 /* Calculate mean erase counter */
1195 if (si->ec_count) 1178 if (ai->ec_count)
1196 si->mean_ec = div_u64(si->ec_sum, si->ec_count); 1179 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
1197 1180
1198 err = check_what_we_have(ubi, si); 1181 err = late_analysis(ubi, ai);
1199 if (err) 1182 if (err)
1200 goto out_vidh; 1183 goto out_vidh;
1201 1184
@@ -1203,55 +1186,102 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
1203 * In case of unknown erase counter we use the mean erase counter 1186 * In case of unknown erase counter we use the mean erase counter
1204 * value. 1187 * value.
1205 */ 1188 */
1206 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1189 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1207 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) 1190 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1208 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 1191 if (aeb->ec == UBI_UNKNOWN)
1209 seb->ec = si->mean_ec; 1192 aeb->ec = ai->mean_ec;
1210 } 1193 }
1211 1194
1212 list_for_each_entry(seb, &si->free, u.list) { 1195 list_for_each_entry(aeb, &ai->free, u.list) {
1213 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 1196 if (aeb->ec == UBI_UNKNOWN)
1214 seb->ec = si->mean_ec; 1197 aeb->ec = ai->mean_ec;
1215 } 1198 }
1216 1199
1217 list_for_each_entry(seb, &si->corr, u.list) 1200 list_for_each_entry(aeb, &ai->corr, u.list)
1218 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 1201 if (aeb->ec == UBI_UNKNOWN)
1219 seb->ec = si->mean_ec; 1202 aeb->ec = ai->mean_ec;
1220 1203
1221 list_for_each_entry(seb, &si->erase, u.list) 1204 list_for_each_entry(aeb, &ai->erase, u.list)
1222 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 1205 if (aeb->ec == UBI_UNKNOWN)
1223 seb->ec = si->mean_ec; 1206 aeb->ec = ai->mean_ec;
1224 1207
1225 err = paranoid_check_si(ubi, si); 1208 err = self_check_ai(ubi, ai);
1226 if (err) 1209 if (err)
1227 goto out_vidh; 1210 goto out_vidh;
1228 1211
1229 ubi_free_vid_hdr(ubi, vidh); 1212 ubi_free_vid_hdr(ubi, vidh);
1230 kfree(ech); 1213 kfree(ech);
1231 1214
1232 return si; 1215 return ai;
1233 1216
1234out_vidh: 1217out_vidh:
1235 ubi_free_vid_hdr(ubi, vidh); 1218 ubi_free_vid_hdr(ubi, vidh);
1236out_ech: 1219out_ech:
1237 kfree(ech); 1220 kfree(ech);
1238out_si: 1221out_ai:
1239 ubi_scan_destroy_si(si); 1222 ubi_destroy_ai(ai);
1240 return ERR_PTR(err); 1223 return ERR_PTR(err);
1241} 1224}
1242 1225
1243/** 1226/**
1244 * destroy_sv - free the scanning volume information 1227 * ubi_attach - attach an MTD device.
1245 * @sv: scanning volume information 1228 * @ubi: UBI device descriptor
1246 * @si: scanning information
1247 * 1229 *
1248 * This function destroys the volume RB-tree (@sv->root) and the scanning 1230 * This function returns zero in case of success and a negative error code in
1249 * volume information. 1231 * case of failure.
1250 */ 1232 */
1251static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv) 1233int ubi_attach(struct ubi_device *ubi)
1252{ 1234{
1253 struct ubi_scan_leb *seb; 1235 int err;
1254 struct rb_node *this = sv->root.rb_node; 1236 struct ubi_attach_info *ai;
1237
1238 ai = scan_all(ubi);
1239 if (IS_ERR(ai))
1240 return PTR_ERR(ai);
1241
1242 ubi->bad_peb_count = ai->bad_peb_count;
1243 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
1244 ubi->corr_peb_count = ai->corr_peb_count;
1245 ubi->max_ec = ai->max_ec;
1246 ubi->mean_ec = ai->mean_ec;
1247 ubi_msg("max. sequence number: %llu", ai->max_sqnum);
1248
1249 err = ubi_read_volume_table(ubi, ai);
1250 if (err)
1251 goto out_ai;
1252
1253 err = ubi_wl_init(ubi, ai);
1254 if (err)
1255 goto out_vtbl;
1256
1257 err = ubi_eba_init(ubi, ai);
1258 if (err)
1259 goto out_wl;
1260
1261 ubi_destroy_ai(ai);
1262 return 0;
1263
1264out_wl:
1265 ubi_wl_close(ubi);
1266out_vtbl:
1267 ubi_free_internal_volumes(ubi);
1268 vfree(ubi->vtbl);
1269out_ai:
1270 ubi_destroy_ai(ai);
1271 return err;
1272}
1273
1274/**
1275 * destroy_av - free volume attaching information.
1276 * @av: volume attaching information
1277 * @ai: attaching information
1278 *
1279 * This function destroys the volume attaching information.
1280 */
1281static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av)
1282{
1283 struct ubi_ainf_peb *aeb;
1284 struct rb_node *this = av->root.rb_node;
1255 1285
1256 while (this) { 1286 while (this) {
1257 if (this->rb_left) 1287 if (this->rb_left)
@@ -1259,224 +1289,222 @@ static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
1259 else if (this->rb_right) 1289 else if (this->rb_right)
1260 this = this->rb_right; 1290 this = this->rb_right;
1261 else { 1291 else {
1262 seb = rb_entry(this, struct ubi_scan_leb, u.rb); 1292 aeb = rb_entry(this, struct ubi_ainf_peb, u.rb);
1263 this = rb_parent(this); 1293 this = rb_parent(this);
1264 if (this) { 1294 if (this) {
1265 if (this->rb_left == &seb->u.rb) 1295 if (this->rb_left == &aeb->u.rb)
1266 this->rb_left = NULL; 1296 this->rb_left = NULL;
1267 else 1297 else
1268 this->rb_right = NULL; 1298 this->rb_right = NULL;
1269 } 1299 }
1270 1300
1271 kmem_cache_free(si->scan_leb_slab, seb); 1301 kmem_cache_free(ai->aeb_slab_cache, aeb);
1272 } 1302 }
1273 } 1303 }
1274 kfree(sv); 1304 kfree(av);
1275} 1305}
1276 1306
1277/** 1307/**
1278 * ubi_scan_destroy_si - destroy scanning information. 1308 * ubi_destroy_ai - destroy attaching information.
1279 * @si: scanning information 1309 * @ai: attaching information
1280 */ 1310 */
1281void ubi_scan_destroy_si(struct ubi_scan_info *si) 1311void ubi_destroy_ai(struct ubi_attach_info *ai)
1282{ 1312{
1283 struct ubi_scan_leb *seb, *seb_tmp; 1313 struct ubi_ainf_peb *aeb, *aeb_tmp;
1284 struct ubi_scan_volume *sv; 1314 struct ubi_ainf_volume *av;
1285 struct rb_node *rb; 1315 struct rb_node *rb;
1286 1316
1287 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { 1317 list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
1288 list_del(&seb->u.list); 1318 list_del(&aeb->u.list);
1289 kmem_cache_free(si->scan_leb_slab, seb); 1319 kmem_cache_free(ai->aeb_slab_cache, aeb);
1290 } 1320 }
1291 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { 1321 list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
1292 list_del(&seb->u.list); 1322 list_del(&aeb->u.list);
1293 kmem_cache_free(si->scan_leb_slab, seb); 1323 kmem_cache_free(ai->aeb_slab_cache, aeb);
1294 } 1324 }
1295 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { 1325 list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
1296 list_del(&seb->u.list); 1326 list_del(&aeb->u.list);
1297 kmem_cache_free(si->scan_leb_slab, seb); 1327 kmem_cache_free(ai->aeb_slab_cache, aeb);
1298 } 1328 }
1299 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { 1329 list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
1300 list_del(&seb->u.list); 1330 list_del(&aeb->u.list);
1301 kmem_cache_free(si->scan_leb_slab, seb); 1331 kmem_cache_free(ai->aeb_slab_cache, aeb);
1302 } 1332 }
1303 1333
1304 /* Destroy the volume RB-tree */ 1334 /* Destroy the volume RB-tree */
1305 rb = si->volumes.rb_node; 1335 rb = ai->volumes.rb_node;
1306 while (rb) { 1336 while (rb) {
1307 if (rb->rb_left) 1337 if (rb->rb_left)
1308 rb = rb->rb_left; 1338 rb = rb->rb_left;
1309 else if (rb->rb_right) 1339 else if (rb->rb_right)
1310 rb = rb->rb_right; 1340 rb = rb->rb_right;
1311 else { 1341 else {
1312 sv = rb_entry(rb, struct ubi_scan_volume, rb); 1342 av = rb_entry(rb, struct ubi_ainf_volume, rb);
1313 1343
1314 rb = rb_parent(rb); 1344 rb = rb_parent(rb);
1315 if (rb) { 1345 if (rb) {
1316 if (rb->rb_left == &sv->rb) 1346 if (rb->rb_left == &av->rb)
1317 rb->rb_left = NULL; 1347 rb->rb_left = NULL;
1318 else 1348 else
1319 rb->rb_right = NULL; 1349 rb->rb_right = NULL;
1320 } 1350 }
1321 1351
1322 destroy_sv(si, sv); 1352 destroy_av(ai, av);
1323 } 1353 }
1324 } 1354 }
1325 1355
1326 if (si->scan_leb_slab) 1356 if (ai->aeb_slab_cache)
1327 kmem_cache_destroy(si->scan_leb_slab); 1357 kmem_cache_destroy(ai->aeb_slab_cache);
1328 1358
1329 kfree(si); 1359 kfree(ai);
1330} 1360}
1331 1361
1332#ifdef CONFIG_MTD_UBI_DEBUG
1333
1334/** 1362/**
1335 * paranoid_check_si - check the scanning information. 1363 * self_check_ai - check the attaching information.
1336 * @ubi: UBI device description object 1364 * @ubi: UBI device description object
1337 * @si: scanning information 1365 * @ai: attaching information
1338 * 1366 *
1339 * This function returns zero if the scanning information is all right, and a 1367 * This function returns zero if the attaching information is all right, and a
1340 * negative error code if not or if an error occurred. 1368 * negative error code if not or if an error occurred.
1341 */ 1369 */
1342static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si) 1370static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
1343{ 1371{
1344 int pnum, err, vols_found = 0; 1372 int pnum, err, vols_found = 0;
1345 struct rb_node *rb1, *rb2; 1373 struct rb_node *rb1, *rb2;
1346 struct ubi_scan_volume *sv; 1374 struct ubi_ainf_volume *av;
1347 struct ubi_scan_leb *seb, *last_seb; 1375 struct ubi_ainf_peb *aeb, *last_aeb;
1348 uint8_t *buf; 1376 uint8_t *buf;
1349 1377
1350 if (!ubi->dbg->chk_gen) 1378 if (!ubi->dbg->chk_gen)
1351 return 0; 1379 return 0;
1352 1380
1353 /* 1381 /*
1354 * At first, check that scanning information is OK. 1382 * At first, check that attaching information is OK.
1355 */ 1383 */
1356 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1384 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1357 int leb_count = 0; 1385 int leb_count = 0;
1358 1386
1359 cond_resched(); 1387 cond_resched();
1360 1388
1361 vols_found += 1; 1389 vols_found += 1;
1362 1390
1363 if (si->is_empty) { 1391 if (ai->is_empty) {
1364 ubi_err("bad is_empty flag"); 1392 ubi_err("bad is_empty flag");
1365 goto bad_sv; 1393 goto bad_av;
1366 } 1394 }
1367 1395
1368 if (sv->vol_id < 0 || sv->highest_lnum < 0 || 1396 if (av->vol_id < 0 || av->highest_lnum < 0 ||
1369 sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 || 1397 av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 ||
1370 sv->data_pad < 0 || sv->last_data_size < 0) { 1398 av->data_pad < 0 || av->last_data_size < 0) {
1371 ubi_err("negative values"); 1399 ubi_err("negative values");
1372 goto bad_sv; 1400 goto bad_av;
1373 } 1401 }
1374 1402
1375 if (sv->vol_id >= UBI_MAX_VOLUMES && 1403 if (av->vol_id >= UBI_MAX_VOLUMES &&
1376 sv->vol_id < UBI_INTERNAL_VOL_START) { 1404 av->vol_id < UBI_INTERNAL_VOL_START) {
1377 ubi_err("bad vol_id"); 1405 ubi_err("bad vol_id");
1378 goto bad_sv; 1406 goto bad_av;
1379 } 1407 }
1380 1408
1381 if (sv->vol_id > si->highest_vol_id) { 1409 if (av->vol_id > ai->highest_vol_id) {
1382 ubi_err("highest_vol_id is %d, but vol_id %d is there", 1410 ubi_err("highest_vol_id is %d, but vol_id %d is there",
1383 si->highest_vol_id, sv->vol_id); 1411 ai->highest_vol_id, av->vol_id);
1384 goto out; 1412 goto out;
1385 } 1413 }
1386 1414
1387 if (sv->vol_type != UBI_DYNAMIC_VOLUME && 1415 if (av->vol_type != UBI_DYNAMIC_VOLUME &&
1388 sv->vol_type != UBI_STATIC_VOLUME) { 1416 av->vol_type != UBI_STATIC_VOLUME) {
1389 ubi_err("bad vol_type"); 1417 ubi_err("bad vol_type");
1390 goto bad_sv; 1418 goto bad_av;
1391 } 1419 }
1392 1420
1393 if (sv->data_pad > ubi->leb_size / 2) { 1421 if (av->data_pad > ubi->leb_size / 2) {
1394 ubi_err("bad data_pad"); 1422 ubi_err("bad data_pad");
1395 goto bad_sv; 1423 goto bad_av;
1396 } 1424 }
1397 1425
1398 last_seb = NULL; 1426 last_aeb = NULL;
1399 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { 1427 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1400 cond_resched(); 1428 cond_resched();
1401 1429
1402 last_seb = seb; 1430 last_aeb = aeb;
1403 leb_count += 1; 1431 leb_count += 1;
1404 1432
1405 if (seb->pnum < 0 || seb->ec < 0) { 1433 if (aeb->pnum < 0 || aeb->ec < 0) {
1406 ubi_err("negative values"); 1434 ubi_err("negative values");
1407 goto bad_seb; 1435 goto bad_aeb;
1408 } 1436 }
1409 1437
1410 if (seb->ec < si->min_ec) { 1438 if (aeb->ec < ai->min_ec) {
1411 ubi_err("bad si->min_ec (%d), %d found", 1439 ubi_err("bad ai->min_ec (%d), %d found",
1412 si->min_ec, seb->ec); 1440 ai->min_ec, aeb->ec);
1413 goto bad_seb; 1441 goto bad_aeb;
1414 } 1442 }
1415 1443
1416 if (seb->ec > si->max_ec) { 1444 if (aeb->ec > ai->max_ec) {
1417 ubi_err("bad si->max_ec (%d), %d found", 1445 ubi_err("bad ai->max_ec (%d), %d found",
1418 si->max_ec, seb->ec); 1446 ai->max_ec, aeb->ec);
1419 goto bad_seb; 1447 goto bad_aeb;
1420 } 1448 }
1421 1449
1422 if (seb->pnum >= ubi->peb_count) { 1450 if (aeb->pnum >= ubi->peb_count) {
1423 ubi_err("too high PEB number %d, total PEBs %d", 1451 ubi_err("too high PEB number %d, total PEBs %d",
1424 seb->pnum, ubi->peb_count); 1452 aeb->pnum, ubi->peb_count);
1425 goto bad_seb; 1453 goto bad_aeb;
1426 } 1454 }
1427 1455
1428 if (sv->vol_type == UBI_STATIC_VOLUME) { 1456 if (av->vol_type == UBI_STATIC_VOLUME) {
1429 if (seb->lnum >= sv->used_ebs) { 1457 if (aeb->lnum >= av->used_ebs) {
1430 ubi_err("bad lnum or used_ebs"); 1458 ubi_err("bad lnum or used_ebs");
1431 goto bad_seb; 1459 goto bad_aeb;
1432 } 1460 }
1433 } else { 1461 } else {
1434 if (sv->used_ebs != 0) { 1462 if (av->used_ebs != 0) {
1435 ubi_err("non-zero used_ebs"); 1463 ubi_err("non-zero used_ebs");
1436 goto bad_seb; 1464 goto bad_aeb;
1437 } 1465 }
1438 } 1466 }
1439 1467
1440 if (seb->lnum > sv->highest_lnum) { 1468 if (aeb->lnum > av->highest_lnum) {
1441 ubi_err("incorrect highest_lnum or lnum"); 1469 ubi_err("incorrect highest_lnum or lnum");
1442 goto bad_seb; 1470 goto bad_aeb;
1443 } 1471 }
1444 } 1472 }
1445 1473
1446 if (sv->leb_count != leb_count) { 1474 if (av->leb_count != leb_count) {
1447 ubi_err("bad leb_count, %d objects in the tree", 1475 ubi_err("bad leb_count, %d objects in the tree",
1448 leb_count); 1476 leb_count);
1449 goto bad_sv; 1477 goto bad_av;
1450 } 1478 }
1451 1479
1452 if (!last_seb) 1480 if (!last_aeb)
1453 continue; 1481 continue;
1454 1482
1455 seb = last_seb; 1483 aeb = last_aeb;
1456 1484
1457 if (seb->lnum != sv->highest_lnum) { 1485 if (aeb->lnum != av->highest_lnum) {
1458 ubi_err("bad highest_lnum"); 1486 ubi_err("bad highest_lnum");
1459 goto bad_seb; 1487 goto bad_aeb;
1460 } 1488 }
1461 } 1489 }
1462 1490
1463 if (vols_found != si->vols_found) { 1491 if (vols_found != ai->vols_found) {
1464 ubi_err("bad si->vols_found %d, should be %d", 1492 ubi_err("bad ai->vols_found %d, should be %d",
1465 si->vols_found, vols_found); 1493 ai->vols_found, vols_found);
1466 goto out; 1494 goto out;
1467 } 1495 }
1468 1496
1469 /* Check that scanning information is correct */ 1497 /* Check that attaching information is correct */
1470 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1498 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1471 last_seb = NULL; 1499 last_aeb = NULL;
1472 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { 1500 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1473 int vol_type; 1501 int vol_type;
1474 1502
1475 cond_resched(); 1503 cond_resched();
1476 1504
1477 last_seb = seb; 1505 last_aeb = aeb;
1478 1506
1479 err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1); 1507 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1);
1480 if (err && err != UBI_IO_BITFLIPS) { 1508 if (err && err != UBI_IO_BITFLIPS) {
1481 ubi_err("VID header is not OK (%d)", err); 1509 ubi_err("VID header is not OK (%d)", err);
1482 if (err > 0) 1510 if (err > 0)
@@ -1486,52 +1514,52 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1486 1514
1487 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? 1515 vol_type = vidh->vol_type == UBI_VID_DYNAMIC ?
1488 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; 1516 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
1489 if (sv->vol_type != vol_type) { 1517 if (av->vol_type != vol_type) {
1490 ubi_err("bad vol_type"); 1518 ubi_err("bad vol_type");
1491 goto bad_vid_hdr; 1519 goto bad_vid_hdr;
1492 } 1520 }
1493 1521
1494 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) { 1522 if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) {
1495 ubi_err("bad sqnum %llu", seb->sqnum); 1523 ubi_err("bad sqnum %llu", aeb->sqnum);
1496 goto bad_vid_hdr; 1524 goto bad_vid_hdr;
1497 } 1525 }
1498 1526
1499 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) { 1527 if (av->vol_id != be32_to_cpu(vidh->vol_id)) {
1500 ubi_err("bad vol_id %d", sv->vol_id); 1528 ubi_err("bad vol_id %d", av->vol_id);
1501 goto bad_vid_hdr; 1529 goto bad_vid_hdr;
1502 } 1530 }
1503 1531
1504 if (sv->compat != vidh->compat) { 1532 if (av->compat != vidh->compat) {
1505 ubi_err("bad compat %d", vidh->compat); 1533 ubi_err("bad compat %d", vidh->compat);
1506 goto bad_vid_hdr; 1534 goto bad_vid_hdr;
1507 } 1535 }
1508 1536
1509 if (seb->lnum != be32_to_cpu(vidh->lnum)) { 1537 if (aeb->lnum != be32_to_cpu(vidh->lnum)) {
1510 ubi_err("bad lnum %d", seb->lnum); 1538 ubi_err("bad lnum %d", aeb->lnum);
1511 goto bad_vid_hdr; 1539 goto bad_vid_hdr;
1512 } 1540 }
1513 1541
1514 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) { 1542 if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1515 ubi_err("bad used_ebs %d", sv->used_ebs); 1543 ubi_err("bad used_ebs %d", av->used_ebs);
1516 goto bad_vid_hdr; 1544 goto bad_vid_hdr;
1517 } 1545 }
1518 1546
1519 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) { 1547 if (av->data_pad != be32_to_cpu(vidh->data_pad)) {
1520 ubi_err("bad data_pad %d", sv->data_pad); 1548 ubi_err("bad data_pad %d", av->data_pad);
1521 goto bad_vid_hdr; 1549 goto bad_vid_hdr;
1522 } 1550 }
1523 } 1551 }
1524 1552
1525 if (!last_seb) 1553 if (!last_aeb)
1526 continue; 1554 continue;
1527 1555
1528 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) { 1556 if (av->highest_lnum != be32_to_cpu(vidh->lnum)) {
1529 ubi_err("bad highest_lnum %d", sv->highest_lnum); 1557 ubi_err("bad highest_lnum %d", av->highest_lnum);
1530 goto bad_vid_hdr; 1558 goto bad_vid_hdr;
1531 } 1559 }
1532 1560
1533 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) { 1561 if (av->last_data_size != be32_to_cpu(vidh->data_size)) {
1534 ubi_err("bad last_data_size %d", sv->last_data_size); 1562 ubi_err("bad last_data_size %d", av->last_data_size);
1535 goto bad_vid_hdr; 1563 goto bad_vid_hdr;
1536 } 1564 }
1537 } 1565 }
@@ -1553,21 +1581,21 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1553 buf[pnum] = 1; 1581 buf[pnum] = 1;
1554 } 1582 }
1555 1583
1556 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) 1584 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
1557 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) 1585 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
1558 buf[seb->pnum] = 1; 1586 buf[aeb->pnum] = 1;
1559 1587
1560 list_for_each_entry(seb, &si->free, u.list) 1588 list_for_each_entry(aeb, &ai->free, u.list)
1561 buf[seb->pnum] = 1; 1589 buf[aeb->pnum] = 1;
1562 1590
1563 list_for_each_entry(seb, &si->corr, u.list) 1591 list_for_each_entry(aeb, &ai->corr, u.list)
1564 buf[seb->pnum] = 1; 1592 buf[aeb->pnum] = 1;
1565 1593
1566 list_for_each_entry(seb, &si->erase, u.list) 1594 list_for_each_entry(aeb, &ai->erase, u.list)
1567 buf[seb->pnum] = 1; 1595 buf[aeb->pnum] = 1;
1568 1596
1569 list_for_each_entry(seb, &si->alien, u.list) 1597 list_for_each_entry(aeb, &ai->alien, u.list)
1570 buf[seb->pnum] = 1; 1598 buf[aeb->pnum] = 1;
1571 1599
1572 err = 0; 1600 err = 0;
1573 for (pnum = 0; pnum < ubi->peb_count; pnum++) 1601 for (pnum = 0; pnum < ubi->peb_count; pnum++)
@@ -1581,25 +1609,23 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1581 goto out; 1609 goto out;
1582 return 0; 1610 return 0;
1583 1611
1584bad_seb: 1612bad_aeb:
1585 ubi_err("bad scanning information about LEB %d", seb->lnum); 1613 ubi_err("bad attaching information about LEB %d", aeb->lnum);
1586 ubi_dbg_dump_seb(seb, 0); 1614 ubi_dump_aeb(aeb, 0);
1587 ubi_dbg_dump_sv(sv); 1615 ubi_dump_av(av);
1588 goto out; 1616 goto out;
1589 1617
1590bad_sv: 1618bad_av:
1591 ubi_err("bad scanning information about volume %d", sv->vol_id); 1619 ubi_err("bad attaching information about volume %d", av->vol_id);
1592 ubi_dbg_dump_sv(sv); 1620 ubi_dump_av(av);
1593 goto out; 1621 goto out;
1594 1622
1595bad_vid_hdr: 1623bad_vid_hdr:
1596 ubi_err("bad scanning information about volume %d", sv->vol_id); 1624 ubi_err("bad attaching information about volume %d", av->vol_id);
1597 ubi_dbg_dump_sv(sv); 1625 ubi_dump_av(av);
1598 ubi_dbg_dump_vid_hdr(vidh); 1626 ubi_dump_vid_hdr(vidh);
1599 1627
1600out: 1628out:
1601 ubi_dbg_dump_stack(); 1629 dump_stack();
1602 return -EINVAL; 1630 return -EINVAL;
1603} 1631}
1604
1605#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 0fde9fc7d2e5..2c5ed5ca9c33 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -27,10 +27,6 @@
27 * module load parameters or the kernel boot parameters. If MTD devices were 27 * module load parameters or the kernel boot parameters. If MTD devices were
28 * specified, UBI does not attach any MTD device, but it is possible to do 28 * specified, UBI does not attach any MTD device, but it is possible to do
29 * later using the "UBI control device". 29 * later using the "UBI control device".
30 *
31 * At the moment we only attach UBI devices by scanning, which will become a
32 * bottleneck when flashes reach certain large size. Then one may improve UBI
33 * and add other methods, although it does not seem to be easy to do.
34 */ 30 */
35 31
36#include <linux/err.h> 32#include <linux/err.h>
@@ -554,10 +550,10 @@ static void uif_close(struct ubi_device *ubi)
554} 550}
555 551
556/** 552/**
557 * free_internal_volumes - free internal volumes. 553 * ubi_free_internal_volumes - free internal volumes.
558 * @ubi: UBI device description object 554 * @ubi: UBI device description object
559 */ 555 */
560static void free_internal_volumes(struct ubi_device *ubi) 556void ubi_free_internal_volumes(struct ubi_device *ubi)
561{ 557{
562 int i; 558 int i;
563 559
@@ -569,59 +565,6 @@ static void free_internal_volumes(struct ubi_device *ubi)
569} 565}
570 566
571/** 567/**
572 * attach_by_scanning - attach an MTD device using scanning method.
573 * @ubi: UBI device descriptor
574 *
575 * This function returns zero in case of success and a negative error code in
576 * case of failure.
577 *
578 * Note, currently this is the only method to attach UBI devices. Hopefully in
579 * the future we'll have more scalable attaching methods and avoid full media
580 * scanning. But even in this case scanning will be needed as a fall-back
581 * attaching method if there are some on-flash table corruptions.
582 */
583static int attach_by_scanning(struct ubi_device *ubi)
584{
585 int err;
586 struct ubi_scan_info *si;
587
588 si = ubi_scan(ubi);
589 if (IS_ERR(si))
590 return PTR_ERR(si);
591
592 ubi->bad_peb_count = si->bad_peb_count;
593 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
594 ubi->corr_peb_count = si->corr_peb_count;
595 ubi->max_ec = si->max_ec;
596 ubi->mean_ec = si->mean_ec;
597 ubi_msg("max. sequence number: %llu", si->max_sqnum);
598
599 err = ubi_read_volume_table(ubi, si);
600 if (err)
601 goto out_si;
602
603 err = ubi_wl_init_scan(ubi, si);
604 if (err)
605 goto out_vtbl;
606
607 err = ubi_eba_init_scan(ubi, si);
608 if (err)
609 goto out_wl;
610
611 ubi_scan_destroy_si(si);
612 return 0;
613
614out_wl:
615 ubi_wl_close(ubi);
616out_vtbl:
617 free_internal_volumes(ubi);
618 vfree(ubi->vtbl);
619out_si:
620 ubi_scan_destroy_si(si);
621 return err;
622}
623
624/**
625 * io_init - initialize I/O sub-system for a given UBI device. 568 * io_init - initialize I/O sub-system for a given UBI device.
626 * @ubi: UBI device description object 569 * @ubi: UBI device description object
627 * 570 *
@@ -790,11 +733,11 @@ static int io_init(struct ubi_device *ubi)
790 ubi_msg("data offset: %d", ubi->leb_start); 733 ubi_msg("data offset: %d", ubi->leb_start);
791 734
792 /* 735 /*
793 * Note, ideally, we have to initialize ubi->bad_peb_count here. But 736 * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
794 * unfortunately, MTD does not provide this information. We should loop 737 * unfortunately, MTD does not provide this information. We should loop
795 * over all physical eraseblocks and invoke mtd->block_is_bad() for 738 * over all physical eraseblocks and invoke mtd->block_is_bad() for
796 * each physical eraseblock. So, we skip ubi->bad_peb_count 739 * each physical eraseblock. So, we leave @ubi->bad_peb_count
797 * uninitialized and initialize it after scanning. 740 * uninitialized so far.
798 */ 741 */
799 742
800 return 0; 743 return 0;
@@ -805,7 +748,7 @@ static int io_init(struct ubi_device *ubi)
805 * @ubi: UBI device description object 748 * @ubi: UBI device description object
806 * @vol_id: ID of the volume to re-size 749 * @vol_id: ID of the volume to re-size
807 * 750 *
808 * This function re-sizes the volume marked by the @UBI_VTBL_AUTORESIZE_FLG in 751 * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
809 * the volume table to the largest possible size. See comments in ubi-header.h 752 * the volume table to the largest possible size. See comments in ubi-header.h
810 * for more description of the flag. Returns zero in case of success and a 753 * for more description of the flag. Returns zero in case of success and a
811 * negative error code in case of failure. 754 * negative error code in case of failure.
@@ -881,7 +824,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
881 for (i = 0; i < UBI_MAX_DEVICES; i++) { 824 for (i = 0; i < UBI_MAX_DEVICES; i++) {
882 ubi = ubi_devices[i]; 825 ubi = ubi_devices[i];
883 if (ubi && mtd->index == ubi->mtd->index) { 826 if (ubi && mtd->index == ubi->mtd->index) {
884 dbg_err("mtd%d is already attached to ubi%d", 827 ubi_err("mtd%d is already attached to ubi%d",
885 mtd->index, i); 828 mtd->index, i);
886 return -EEXIST; 829 return -EEXIST;
887 } 830 }
@@ -907,7 +850,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
907 if (!ubi_devices[ubi_num]) 850 if (!ubi_devices[ubi_num])
908 break; 851 break;
909 if (ubi_num == UBI_MAX_DEVICES) { 852 if (ubi_num == UBI_MAX_DEVICES) {
910 dbg_err("only %d UBI devices may be created", 853 ubi_err("only %d UBI devices may be created",
911 UBI_MAX_DEVICES); 854 UBI_MAX_DEVICES);
912 return -ENFILE; 855 return -ENFILE;
913 } 856 }
@@ -917,7 +860,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
917 860
918 /* Make sure ubi_num is not busy */ 861 /* Make sure ubi_num is not busy */
919 if (ubi_devices[ubi_num]) { 862 if (ubi_devices[ubi_num]) {
920 dbg_err("ubi%d already exists", ubi_num); 863 ubi_err("ubi%d already exists", ubi_num);
921 return -EEXIST; 864 return -EEXIST;
922 } 865 }
923 } 866 }
@@ -937,7 +880,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
937 spin_lock_init(&ubi->volumes_lock); 880 spin_lock_init(&ubi->volumes_lock);
938 881
939 ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num); 882 ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
940 dbg_msg("sizeof(struct ubi_scan_leb) %zu", sizeof(struct ubi_scan_leb)); 883 dbg_msg("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
941 dbg_msg("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry)); 884 dbg_msg("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
942 885
943 err = io_init(ubi); 886 err = io_init(ubi);
@@ -953,9 +896,9 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
953 if (err) 896 if (err)
954 goto out_free; 897 goto out_free;
955 898
956 err = attach_by_scanning(ubi); 899 err = ubi_attach(ubi);
957 if (err) { 900 if (err) {
958 dbg_err("failed to attach by scanning, error %d", err); 901 ubi_err("failed to attach mtd%d, error %d", mtd->index, err);
959 goto out_debugging; 902 goto out_debugging;
960 } 903 }
961 904
@@ -1020,7 +963,7 @@ out_uif:
1020 uif_close(ubi); 963 uif_close(ubi);
1021out_detach: 964out_detach:
1022 ubi_wl_close(ubi); 965 ubi_wl_close(ubi);
1023 free_internal_volumes(ubi); 966 ubi_free_internal_volumes(ubi);
1024 vfree(ubi->vtbl); 967 vfree(ubi->vtbl);
1025out_debugging: 968out_debugging:
1026 ubi_debugging_exit_dev(ubi); 969 ubi_debugging_exit_dev(ubi);
@@ -1092,7 +1035,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1092 ubi_debugfs_exit_dev(ubi); 1035 ubi_debugfs_exit_dev(ubi);
1093 uif_close(ubi); 1036 uif_close(ubi);
1094 ubi_wl_close(ubi); 1037 ubi_wl_close(ubi);
1095 free_internal_volumes(ubi); 1038 ubi_free_internal_volumes(ubi);
1096 vfree(ubi->vtbl); 1039 vfree(ubi->vtbl);
1097 put_mtd_device(ubi->mtd); 1040 put_mtd_device(ubi->mtd);
1098 ubi_debugging_exit_dev(ubi); 1041 ubi_debugging_exit_dev(ubi);
diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index ad76592fb2f4..acec85deb6af 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -63,7 +63,7 @@ static int get_exclusive(struct ubi_volume_desc *desc)
63 users = vol->readers + vol->writers + vol->exclusive; 63 users = vol->readers + vol->writers + vol->exclusive;
64 ubi_assert(users > 0); 64 ubi_assert(users > 0);
65 if (users > 1) { 65 if (users > 1) {
66 dbg_err("%d users for volume %d", users, vol->vol_id); 66 ubi_err("%d users for volume %d", users, vol->vol_id);
67 err = -EBUSY; 67 err = -EBUSY;
68 } else { 68 } else {
69 vol->readers = vol->writers = 0; 69 vol->readers = vol->writers = 0;
@@ -159,7 +159,7 @@ static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
159 159
160 if (vol->updating) { 160 if (vol->updating) {
161 /* Update is in progress, seeking is prohibited */ 161 /* Update is in progress, seeking is prohibited */
162 dbg_err("updating"); 162 ubi_err("updating");
163 return -EBUSY; 163 return -EBUSY;
164 } 164 }
165 165
@@ -178,7 +178,7 @@ static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
178 } 178 }
179 179
180 if (new_offset < 0 || new_offset > vol->used_bytes) { 180 if (new_offset < 0 || new_offset > vol->used_bytes) {
181 dbg_err("bad seek %lld", new_offset); 181 ubi_err("bad seek %lld", new_offset);
182 return -EINVAL; 182 return -EINVAL;
183 } 183 }
184 184
@@ -216,11 +216,11 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
216 count, *offp, vol->vol_id); 216 count, *offp, vol->vol_id);
217 217
218 if (vol->updating) { 218 if (vol->updating) {
219 dbg_err("updating"); 219 ubi_err("updating");
220 return -EBUSY; 220 return -EBUSY;
221 } 221 }
222 if (vol->upd_marker) { 222 if (vol->upd_marker) {
223 dbg_err("damaged volume, update marker is set"); 223 ubi_err("damaged volume, update marker is set");
224 return -EBADF; 224 return -EBADF;
225 } 225 }
226 if (*offp == vol->used_bytes || count == 0) 226 if (*offp == vol->used_bytes || count == 0)
@@ -300,7 +300,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
300 300
301 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off); 301 lnum = div_u64_rem(*offp, vol->usable_leb_size, &off);
302 if (off & (ubi->min_io_size - 1)) { 302 if (off & (ubi->min_io_size - 1)) {
303 dbg_err("unaligned position"); 303 ubi_err("unaligned position");
304 return -EINVAL; 304 return -EINVAL;
305 } 305 }
306 306
@@ -309,7 +309,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
309 309
310 /* We can write only in fractions of the minimum I/O unit */ 310 /* We can write only in fractions of the minimum I/O unit */
311 if (count & (ubi->min_io_size - 1)) { 311 if (count & (ubi->min_io_size - 1)) {
312 dbg_err("unaligned write length"); 312 ubi_err("unaligned write length");
313 return -EINVAL; 313 return -EINVAL;
314 } 314 }
315 315
@@ -334,8 +334,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
334 break; 334 break;
335 } 335 }
336 336
337 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len, 337 err = ubi_eba_write_leb(ubi, vol, lnum, tbuf, off, len);
338 UBI_UNKNOWN);
339 if (err) 338 if (err)
340 break; 339 break;
341 340
@@ -477,9 +476,6 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
477 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs || 476 if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
478 req.bytes < 0 || req.lnum >= vol->usable_leb_size) 477 req.bytes < 0 || req.lnum >= vol->usable_leb_size)
479 break; 478 break;
480 if (req.dtype != UBI_LONGTERM && req.dtype != UBI_SHORTTERM &&
481 req.dtype != UBI_UNKNOWN)
482 break;
483 479
484 err = get_exclusive(desc); 480 err = get_exclusive(desc);
485 if (err < 0) 481 if (err < 0)
@@ -518,7 +514,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
518 if (err) 514 if (err)
519 break; 515 break;
520 516
521 err = ubi_wl_flush(ubi); 517 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
522 break; 518 break;
523 } 519 }
524 520
@@ -532,7 +528,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
532 err = -EFAULT; 528 err = -EFAULT;
533 break; 529 break;
534 } 530 }
535 err = ubi_leb_map(desc, req.lnum, req.dtype); 531 err = ubi_leb_map(desc, req.lnum);
536 break; 532 break;
537 } 533 }
538 534
@@ -647,8 +643,8 @@ static int verify_mkvol_req(const struct ubi_device *ubi,
647 return 0; 643 return 0;
648 644
649bad: 645bad:
650 dbg_err("bad volume creation request"); 646 ubi_err("bad volume creation request");
651 ubi_dbg_dump_mkvol_req(req); 647 ubi_dump_mkvol_req(req);
652 return err; 648 return err;
653} 649}
654 650
@@ -713,12 +709,12 @@ static int rename_volumes(struct ubi_device *ubi,
713 for (i = 0; i < req->count - 1; i++) { 709 for (i = 0; i < req->count - 1; i++) {
714 for (n = i + 1; n < req->count; n++) { 710 for (n = i + 1; n < req->count; n++) {
715 if (req->ents[i].vol_id == req->ents[n].vol_id) { 711 if (req->ents[i].vol_id == req->ents[n].vol_id) {
716 dbg_err("duplicated volume id %d", 712 ubi_err("duplicated volume id %d",
717 req->ents[i].vol_id); 713 req->ents[i].vol_id);
718 return -EINVAL; 714 return -EINVAL;
719 } 715 }
720 if (!strcmp(req->ents[i].name, req->ents[n].name)) { 716 if (!strcmp(req->ents[i].name, req->ents[n].name)) {
721 dbg_err("duplicated volume name \"%s\"", 717 ubi_err("duplicated volume name \"%s\"",
722 req->ents[i].name); 718 req->ents[i].name);
723 return -EINVAL; 719 return -EINVAL;
724 } 720 }
@@ -741,7 +737,7 @@ static int rename_volumes(struct ubi_device *ubi,
741 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE); 737 re->desc = ubi_open_volume(ubi->ubi_num, vol_id, UBI_EXCLUSIVE);
742 if (IS_ERR(re->desc)) { 738 if (IS_ERR(re->desc)) {
743 err = PTR_ERR(re->desc); 739 err = PTR_ERR(re->desc);
744 dbg_err("cannot open volume %d, error %d", vol_id, err); 740 ubi_err("cannot open volume %d, error %d", vol_id, err);
745 kfree(re); 741 kfree(re);
746 goto out_free; 742 goto out_free;
747 } 743 }
@@ -800,7 +796,7 @@ static int rename_volumes(struct ubi_device *ubi,
800 continue; 796 continue;
801 797
802 /* The volume exists but busy, or an error occurred */ 798 /* The volume exists but busy, or an error occurred */
803 dbg_err("cannot open volume \"%s\", error %d", 799 ubi_err("cannot open volume \"%s\", error %d",
804 re->new_name, err); 800 re->new_name, err);
805 goto out_free; 801 goto out_free;
806 } 802 }
diff --git a/drivers/mtd/ubi/debug.c b/drivers/mtd/ubi/debug.c
index 61af9bb560ab..9f957c2d48e9 100644
--- a/drivers/mtd/ubi/debug.c
+++ b/drivers/mtd/ubi/debug.c
@@ -18,24 +18,49 @@
18 * Author: Artem Bityutskiy (Битюцкий Артём) 18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */ 19 */
20 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
28
29#include "ubi.h" 21#include "ubi.h"
30#include <linux/debugfs.h> 22#include <linux/debugfs.h>
31#include <linux/uaccess.h> 23#include <linux/uaccess.h>
32#include <linux/module.h> 24#include <linux/module.h>
33 25
26
27/**
28 * ubi_dump_flash - dump a region of flash.
29 * @ubi: UBI device description object
30 * @pnum: the physical eraseblock number to dump
31 * @offset: the starting offset within the physical eraseblock to dump
32 * @len: the length of the region to dump
33 */
34void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
35{
36 int err;
37 size_t read;
38 void *buf;
39 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
40
41 buf = vmalloc(len);
42 if (!buf)
43 return;
44 err = mtd_read(ubi->mtd, addr, len, &read, buf);
45 if (err && err != -EUCLEAN) {
46 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
47 "read %zd bytes", err, len, pnum, offset, read);
48 goto out;
49 }
50
51 ubi_msg("dumping %d bytes of data from PEB %d, offset %d",
52 len, pnum, offset);
53 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
54out:
55 vfree(buf);
56 return;
57}
58
34/** 59/**
35 * ubi_dbg_dump_ec_hdr - dump an erase counter header. 60 * ubi_dump_ec_hdr - dump an erase counter header.
36 * @ec_hdr: the erase counter header to dump 61 * @ec_hdr: the erase counter header to dump
37 */ 62 */
38void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) 63void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
39{ 64{
40 printk(KERN_DEBUG "Erase counter header dump:\n"); 65 printk(KERN_DEBUG "Erase counter header dump:\n");
41 printk(KERN_DEBUG "\tmagic %#08x\n", 66 printk(KERN_DEBUG "\tmagic %#08x\n",
@@ -57,10 +82,10 @@ void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
57} 82}
58 83
59/** 84/**
60 * ubi_dbg_dump_vid_hdr - dump a volume identifier header. 85 * ubi_dump_vid_hdr - dump a volume identifier header.
61 * @vid_hdr: the volume identifier header to dump 86 * @vid_hdr: the volume identifier header to dump
62 */ 87 */
63void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) 88void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
64{ 89{
65 printk(KERN_DEBUG "Volume identifier header dump:\n"); 90 printk(KERN_DEBUG "Volume identifier header dump:\n");
66 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic)); 91 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
@@ -82,10 +107,10 @@ void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
82} 107}
83 108
84/** 109/**
85 * ubi_dbg_dump_vol_info- dump volume information. 110 * ubi_dump_vol_info - dump volume information.
86 * @vol: UBI volume description object 111 * @vol: UBI volume description object
87 */ 112 */
88void ubi_dbg_dump_vol_info(const struct ubi_volume *vol) 113void ubi_dump_vol_info(const struct ubi_volume *vol)
89{ 114{
90 printk(KERN_DEBUG "Volume information dump:\n"); 115 printk(KERN_DEBUG "Volume information dump:\n");
91 printk(KERN_DEBUG "\tvol_id %d\n", vol->vol_id); 116 printk(KERN_DEBUG "\tvol_id %d\n", vol->vol_id);
@@ -112,11 +137,11 @@ void ubi_dbg_dump_vol_info(const struct ubi_volume *vol)
112} 137}
113 138
114/** 139/**
115 * ubi_dbg_dump_vtbl_record - dump a &struct ubi_vtbl_record object. 140 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
116 * @r: the object to dump 141 * @r: the object to dump
117 * @idx: volume table index 142 * @idx: volume table index
118 */ 143 */
119void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) 144void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
120{ 145{
121 int name_len = be16_to_cpu(r->name_len); 146 int name_len = be16_to_cpu(r->name_len);
122 147
@@ -146,44 +171,44 @@ void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
146} 171}
147 172
148/** 173/**
149 * ubi_dbg_dump_sv - dump a &struct ubi_scan_volume object. 174 * ubi_dump_av - dump a &struct ubi_ainf_volume object.
150 * @sv: the object to dump 175 * @av: the object to dump
151 */ 176 */
152void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) 177void ubi_dump_av(const struct ubi_ainf_volume *av)
153{ 178{
154 printk(KERN_DEBUG "Volume scanning information dump:\n"); 179 printk(KERN_DEBUG "Volume attaching information dump:\n");
155 printk(KERN_DEBUG "\tvol_id %d\n", sv->vol_id); 180 printk(KERN_DEBUG "\tvol_id %d\n", av->vol_id);
156 printk(KERN_DEBUG "\thighest_lnum %d\n", sv->highest_lnum); 181 printk(KERN_DEBUG "\thighest_lnum %d\n", av->highest_lnum);
157 printk(KERN_DEBUG "\tleb_count %d\n", sv->leb_count); 182 printk(KERN_DEBUG "\tleb_count %d\n", av->leb_count);
158 printk(KERN_DEBUG "\tcompat %d\n", sv->compat); 183 printk(KERN_DEBUG "\tcompat %d\n", av->compat);
159 printk(KERN_DEBUG "\tvol_type %d\n", sv->vol_type); 184 printk(KERN_DEBUG "\tvol_type %d\n", av->vol_type);
160 printk(KERN_DEBUG "\tused_ebs %d\n", sv->used_ebs); 185 printk(KERN_DEBUG "\tused_ebs %d\n", av->used_ebs);
161 printk(KERN_DEBUG "\tlast_data_size %d\n", sv->last_data_size); 186 printk(KERN_DEBUG "\tlast_data_size %d\n", av->last_data_size);
162 printk(KERN_DEBUG "\tdata_pad %d\n", sv->data_pad); 187 printk(KERN_DEBUG "\tdata_pad %d\n", av->data_pad);
163} 188}
164 189
165/** 190/**
166 * ubi_dbg_dump_seb - dump a &struct ubi_scan_leb object. 191 * ubi_dump_aeb - dump a &struct ubi_ainf_peb object.
167 * @seb: the object to dump 192 * @aeb: the object to dump
168 * @type: object type: 0 - not corrupted, 1 - corrupted 193 * @type: object type: 0 - not corrupted, 1 - corrupted
169 */ 194 */
170void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type) 195void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type)
171{ 196{
172 printk(KERN_DEBUG "eraseblock scanning information dump:\n"); 197 printk(KERN_DEBUG "eraseblock attaching information dump:\n");
173 printk(KERN_DEBUG "\tec %d\n", seb->ec); 198 printk(KERN_DEBUG "\tec %d\n", aeb->ec);
174 printk(KERN_DEBUG "\tpnum %d\n", seb->pnum); 199 printk(KERN_DEBUG "\tpnum %d\n", aeb->pnum);
175 if (type == 0) { 200 if (type == 0) {
176 printk(KERN_DEBUG "\tlnum %d\n", seb->lnum); 201 printk(KERN_DEBUG "\tlnum %d\n", aeb->lnum);
177 printk(KERN_DEBUG "\tscrub %d\n", seb->scrub); 202 printk(KERN_DEBUG "\tscrub %d\n", aeb->scrub);
178 printk(KERN_DEBUG "\tsqnum %llu\n", seb->sqnum); 203 printk(KERN_DEBUG "\tsqnum %llu\n", aeb->sqnum);
179 } 204 }
180} 205}
181 206
182/** 207/**
183 * ubi_dbg_dump_mkvol_req - dump a &struct ubi_mkvol_req object. 208 * ubi_dump_mkvol_req - dump a &struct ubi_mkvol_req object.
184 * @req: the object to dump 209 * @req: the object to dump
185 */ 210 */
186void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) 211void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
187{ 212{
188 char nm[17]; 213 char nm[17];
189 214
@@ -200,38 +225,6 @@ void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req)
200} 225}
201 226
202/** 227/**
203 * ubi_dbg_dump_flash - dump a region of flash.
204 * @ubi: UBI device description object
205 * @pnum: the physical eraseblock number to dump
206 * @offset: the starting offset within the physical eraseblock to dump
207 * @len: the length of the region to dump
208 */
209void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
210{
211 int err;
212 size_t read;
213 void *buf;
214 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
215
216 buf = vmalloc(len);
217 if (!buf)
218 return;
219 err = mtd_read(ubi->mtd, addr, len, &read, buf);
220 if (err && err != -EUCLEAN) {
221 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
222 "read %zd bytes", err, len, pnum, offset, read);
223 goto out;
224 }
225
226 dbg_msg("dumping %d bytes of data from PEB %d, offset %d",
227 len, pnum, offset);
228 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
229out:
230 vfree(buf);
231 return;
232}
233
234/**
235 * ubi_debugging_init_dev - initialize debugging for an UBI device. 228 * ubi_debugging_init_dev - initialize debugging for an UBI device.
236 * @ubi: UBI device description object 229 * @ubi: UBI device description object
237 * 230 *
@@ -479,5 +472,3 @@ void ubi_debugfs_exit_dev(struct ubi_device *ubi)
479{ 472{
480 debugfs_remove_recursive(ubi->dbg->dfs_dir); 473 debugfs_remove_recursive(ubi->dbg->dfs_dir);
481} 474}
482
483#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index ead2cd16ba75..d5d2645b51a7 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -21,21 +21,20 @@
21#ifndef __UBI_DEBUG_H__ 21#ifndef __UBI_DEBUG_H__
22#define __UBI_DEBUG_H__ 22#define __UBI_DEBUG_H__
23 23
24#ifdef CONFIG_MTD_UBI_DEBUG 24void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
25void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
26void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
27
25#include <linux/random.h> 28#include <linux/random.h>
26 29
27#define ubi_assert(expr) do { \ 30#define ubi_assert(expr) do { \
28 if (unlikely(!(expr))) { \ 31 if (unlikely(!(expr))) { \
29 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \ 32 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
30 __func__, __LINE__, current->pid); \ 33 __func__, __LINE__, current->pid); \
31 ubi_dbg_dump_stack(); \ 34 dump_stack(); \
32 } \ 35 } \
33} while (0) 36} while (0)
34 37
35#define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
36
37#define ubi_dbg_dump_stack() dump_stack()
38
39#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \ 38#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
40 print_hex_dump(l, ps, pt, r, g, b, len, a) 39 print_hex_dump(l, ps, pt, r, g, b, len, a)
41 40
@@ -58,17 +57,13 @@
58/* Initialization and build messages */ 57/* Initialization and build messages */
59#define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__) 58#define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
60 59
61void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr); 60void ubi_dump_vol_info(const struct ubi_volume *vol);
62void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr); 61void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
63void ubi_dbg_dump_vol_info(const struct ubi_volume *vol); 62void ubi_dump_av(const struct ubi_ainf_volume *av);
64void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx); 63void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type);
65void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv); 64void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req);
66void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type); 65int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
67void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req); 66 int len);
68void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
69int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
70int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
71 int offset, int len);
72int ubi_debugging_init_dev(struct ubi_device *ubi); 67int ubi_debugging_init_dev(struct ubi_device *ubi);
73void ubi_debugging_exit_dev(struct ubi_device *ubi); 68void ubi_debugging_exit_dev(struct ubi_device *ubi);
74int ubi_debugfs_init(void); 69int ubi_debugfs_init(void);
@@ -167,73 +162,4 @@ static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
167 return 0; 162 return 0;
168} 163}
169 164
170#else
171
172/* Use "if (0)" to make compiler check arguments even if debugging is off */
173#define ubi_assert(expr) do { \
174 if (0) { \
175 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
176 __func__, __LINE__, current->pid); \
177 } \
178} while (0)
179
180#define dbg_err(fmt, ...) do { \
181 if (0) \
182 ubi_err(fmt, ##__VA_ARGS__); \
183} while (0)
184
185#define ubi_dbg_msg(fmt, ...) do { \
186 if (0) \
187 printk(KERN_DEBUG fmt "\n", ##__VA_ARGS__); \
188} while (0)
189
190#define dbg_msg(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
191#define dbg_gen(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
192#define dbg_eba(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
193#define dbg_wl(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
194#define dbg_io(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
195#define dbg_bld(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
196
197static inline void ubi_dbg_dump_stack(void) { return; }
198static inline void
199ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) { return; }
200static inline void
201ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) { return; }
202static inline void
203ubi_dbg_dump_vol_info(const struct ubi_volume *vol) { return; }
204static inline void
205ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) { return; }
206static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; }
207static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb,
208 int type) { return; }
209static inline void
210ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) { return; }
211static inline void ubi_dbg_dump_flash(struct ubi_device *ubi,
212 int pnum, int offset, int len) { return; }
213static inline void
214ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r,
215 int g, const void *b, size_t len, bool a) { return; }
216static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi,
217 int pnum, int offset,
218 int len) { return 0; }
219static inline int ubi_dbg_check_write(struct ubi_device *ubi,
220 const void *buf, int pnum,
221 int offset, int len) { return 0; }
222
223static inline int ubi_debugging_init_dev(struct ubi_device *ubi) { return 0; }
224static inline void ubi_debugging_exit_dev(struct ubi_device *ubi) { return; }
225static inline int ubi_debugfs_init(void) { return 0; }
226static inline void ubi_debugfs_exit(void) { return; }
227static inline int ubi_debugfs_init_dev(struct ubi_device *ubi) { return 0; }
228static inline void ubi_debugfs_exit_dev(struct ubi_device *ubi) { return; }
229
230static inline int
231ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi) { return 0; }
232static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi) { return 0; }
233static inline int
234ubi_dbg_is_write_failure(const struct ubi_device *ubi) { return 0; }
235static inline int
236ubi_dbg_is_erase_failure(const struct ubi_device *ubi) { return 0; }
237
238#endif /* !CONFIG_MTD_UBI_DEBUG */
239#endif /* !__UBI_DEBUG_H__ */ 165#endif /* !__UBI_DEBUG_H__ */
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c
index 2455d620d96b..b703ac7729cf 100644
--- a/drivers/mtd/ubi/eba.c
+++ b/drivers/mtd/ubi/eba.c
@@ -341,7 +341,7 @@ int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
341 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum); 341 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
342 342
343 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED; 343 vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
344 err = ubi_wl_put_peb(ubi, pnum, 0); 344 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
345 345
346out_unlock: 346out_unlock:
347 leb_write_unlock(ubi, vol_id, lnum); 347 leb_write_unlock(ubi, vol_id, lnum);
@@ -507,7 +507,7 @@ static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
507 return -ENOMEM; 507 return -ENOMEM;
508 508
509retry: 509retry:
510 new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN); 510 new_pnum = ubi_wl_get_peb(ubi);
511 if (new_pnum < 0) { 511 if (new_pnum < 0) {
512 ubi_free_vid_hdr(ubi, vid_hdr); 512 ubi_free_vid_hdr(ubi, vid_hdr);
513 return new_pnum; 513 return new_pnum;
@@ -550,7 +550,7 @@ retry:
550 ubi_free_vid_hdr(ubi, vid_hdr); 550 ubi_free_vid_hdr(ubi, vid_hdr);
551 551
552 vol->eba_tbl[lnum] = new_pnum; 552 vol->eba_tbl[lnum] = new_pnum;
553 ubi_wl_put_peb(ubi, pnum, 1); 553 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
554 554
555 ubi_msg("data was successfully recovered"); 555 ubi_msg("data was successfully recovered");
556 return 0; 556 return 0;
@@ -558,7 +558,7 @@ retry:
558out_unlock: 558out_unlock:
559 mutex_unlock(&ubi->buf_mutex); 559 mutex_unlock(&ubi->buf_mutex);
560out_put: 560out_put:
561 ubi_wl_put_peb(ubi, new_pnum, 1); 561 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
562 ubi_free_vid_hdr(ubi, vid_hdr); 562 ubi_free_vid_hdr(ubi, vid_hdr);
563 return err; 563 return err;
564 564
@@ -568,7 +568,7 @@ write_error:
568 * get another one. 568 * get another one.
569 */ 569 */
570 ubi_warn("failed to write to PEB %d", new_pnum); 570 ubi_warn("failed to write to PEB %d", new_pnum);
571 ubi_wl_put_peb(ubi, new_pnum, 1); 571 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
572 if (++tries > UBI_IO_RETRIES) { 572 if (++tries > UBI_IO_RETRIES) {
573 ubi_free_vid_hdr(ubi, vid_hdr); 573 ubi_free_vid_hdr(ubi, vid_hdr);
574 return err; 574 return err;
@@ -585,7 +585,6 @@ write_error:
585 * @buf: the data to write 585 * @buf: the data to write
586 * @offset: offset within the logical eraseblock where to write 586 * @offset: offset within the logical eraseblock where to write
587 * @len: how many bytes to write 587 * @len: how many bytes to write
588 * @dtype: data type
589 * 588 *
590 * This function writes data to logical eraseblock @lnum of a dynamic volume 589 * This function writes data to logical eraseblock @lnum of a dynamic volume
591 * @vol. Returns zero in case of success and a negative error code in case 590 * @vol. Returns zero in case of success and a negative error code in case
@@ -593,7 +592,7 @@ write_error:
593 * written to the flash media, but may be some garbage. 592 * written to the flash media, but may be some garbage.
594 */ 593 */
595int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 594int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
596 const void *buf, int offset, int len, int dtype) 595 const void *buf, int offset, int len)
597{ 596{
598 int err, pnum, tries = 0, vol_id = vol->vol_id; 597 int err, pnum, tries = 0, vol_id = vol->vol_id;
599 struct ubi_vid_hdr *vid_hdr; 598 struct ubi_vid_hdr *vid_hdr;
@@ -641,7 +640,7 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
641 vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 640 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
642 641
643retry: 642retry:
644 pnum = ubi_wl_get_peb(ubi, dtype); 643 pnum = ubi_wl_get_peb(ubi);
645 if (pnum < 0) { 644 if (pnum < 0) {
646 ubi_free_vid_hdr(ubi, vid_hdr); 645 ubi_free_vid_hdr(ubi, vid_hdr);
647 leb_write_unlock(ubi, vol_id, lnum); 646 leb_write_unlock(ubi, vol_id, lnum);
@@ -687,7 +686,7 @@ write_error:
687 * eraseblock, so just put it and request a new one. We assume that if 686 * eraseblock, so just put it and request a new one. We assume that if
688 * this physical eraseblock went bad, the erase code will handle that. 687 * this physical eraseblock went bad, the erase code will handle that.
689 */ 688 */
690 err = ubi_wl_put_peb(ubi, pnum, 1); 689 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
691 if (err || ++tries > UBI_IO_RETRIES) { 690 if (err || ++tries > UBI_IO_RETRIES) {
692 ubi_ro_mode(ubi); 691 ubi_ro_mode(ubi);
693 leb_write_unlock(ubi, vol_id, lnum); 692 leb_write_unlock(ubi, vol_id, lnum);
@@ -707,7 +706,6 @@ write_error:
707 * @lnum: logical eraseblock number 706 * @lnum: logical eraseblock number
708 * @buf: data to write 707 * @buf: data to write
709 * @len: how many bytes to write 708 * @len: how many bytes to write
710 * @dtype: data type
711 * @used_ebs: how many logical eraseblocks will this volume contain 709 * @used_ebs: how many logical eraseblocks will this volume contain
712 * 710 *
713 * This function writes data to logical eraseblock @lnum of static volume 711 * This function writes data to logical eraseblock @lnum of static volume
@@ -724,8 +722,7 @@ write_error:
724 * code in case of failure. 722 * code in case of failure.
725 */ 723 */
726int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, 724int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
727 int lnum, const void *buf, int len, int dtype, 725 int lnum, const void *buf, int len, int used_ebs)
728 int used_ebs)
729{ 726{
730 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id; 727 int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
731 struct ubi_vid_hdr *vid_hdr; 728 struct ubi_vid_hdr *vid_hdr;
@@ -763,7 +760,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
763 vid_hdr->data_crc = cpu_to_be32(crc); 760 vid_hdr->data_crc = cpu_to_be32(crc);
764 761
765retry: 762retry:
766 pnum = ubi_wl_get_peb(ubi, dtype); 763 pnum = ubi_wl_get_peb(ubi);
767 if (pnum < 0) { 764 if (pnum < 0) {
768 ubi_free_vid_hdr(ubi, vid_hdr); 765 ubi_free_vid_hdr(ubi, vid_hdr);
769 leb_write_unlock(ubi, vol_id, lnum); 766 leb_write_unlock(ubi, vol_id, lnum);
@@ -807,7 +804,7 @@ write_error:
807 return err; 804 return err;
808 } 805 }
809 806
810 err = ubi_wl_put_peb(ubi, pnum, 1); 807 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
811 if (err || ++tries > UBI_IO_RETRIES) { 808 if (err || ++tries > UBI_IO_RETRIES) {
812 ubi_ro_mode(ubi); 809 ubi_ro_mode(ubi);
813 leb_write_unlock(ubi, vol_id, lnum); 810 leb_write_unlock(ubi, vol_id, lnum);
@@ -827,7 +824,6 @@ write_error:
827 * @lnum: logical eraseblock number 824 * @lnum: logical eraseblock number
828 * @buf: data to write 825 * @buf: data to write
829 * @len: how many bytes to write 826 * @len: how many bytes to write
830 * @dtype: data type
831 * 827 *
832 * This function changes the contents of a logical eraseblock atomically. @buf 828 * This function changes the contents of a logical eraseblock atomically. @buf
833 * has to contain new logical eraseblock data, and @len - the length of the 829 * has to contain new logical eraseblock data, and @len - the length of the
@@ -839,7 +835,7 @@ write_error:
839 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex. 835 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
840 */ 836 */
841int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, 837int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
842 int lnum, const void *buf, int len, int dtype) 838 int lnum, const void *buf, int len)
843{ 839{
844 int err, pnum, tries = 0, vol_id = vol->vol_id; 840 int err, pnum, tries = 0, vol_id = vol->vol_id;
845 struct ubi_vid_hdr *vid_hdr; 841 struct ubi_vid_hdr *vid_hdr;
@@ -856,7 +852,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
856 err = ubi_eba_unmap_leb(ubi, vol, lnum); 852 err = ubi_eba_unmap_leb(ubi, vol, lnum);
857 if (err) 853 if (err)
858 return err; 854 return err;
859 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 855 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
860 } 856 }
861 857
862 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 858 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
@@ -881,7 +877,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
881 vid_hdr->data_crc = cpu_to_be32(crc); 877 vid_hdr->data_crc = cpu_to_be32(crc);
882 878
883retry: 879retry:
884 pnum = ubi_wl_get_peb(ubi, dtype); 880 pnum = ubi_wl_get_peb(ubi);
885 if (pnum < 0) { 881 if (pnum < 0) {
886 err = pnum; 882 err = pnum;
887 goto out_leb_unlock; 883 goto out_leb_unlock;
@@ -905,7 +901,7 @@ retry:
905 } 901 }
906 902
907 if (vol->eba_tbl[lnum] >= 0) { 903 if (vol->eba_tbl[lnum] >= 0) {
908 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 0); 904 err = ubi_wl_put_peb(ubi, vol_id, lnum, vol->eba_tbl[lnum], 0);
909 if (err) 905 if (err)
910 goto out_leb_unlock; 906 goto out_leb_unlock;
911 } 907 }
@@ -930,7 +926,7 @@ write_error:
930 goto out_leb_unlock; 926 goto out_leb_unlock;
931 } 927 }
932 928
933 err = ubi_wl_put_peb(ubi, pnum, 1); 929 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
934 if (err || ++tries > UBI_IO_RETRIES) { 930 if (err || ++tries > UBI_IO_RETRIES) {
935 ubi_ro_mode(ubi); 931 ubi_ro_mode(ubi);
936 goto out_leb_unlock; 932 goto out_leb_unlock;
@@ -1171,7 +1167,7 @@ out_unlock_leb:
1171 * print_rsvd_warning - warn about not having enough reserved PEBs. 1167 * print_rsvd_warning - warn about not having enough reserved PEBs.
1172 * @ubi: UBI device description object 1168 * @ubi: UBI device description object
1173 * 1169 *
1174 * This is a helper function for 'ubi_eba_init_scan()' which is called when UBI 1170 * This is a helper function for 'ubi_eba_init()' which is called when UBI
1175 * cannot reserve enough PEBs for bad block handling. This function makes a 1171 * cannot reserve enough PEBs for bad block handling. This function makes a
1176 * decision whether we have to print a warning or not. The algorithm is as 1172 * decision whether we have to print a warning or not. The algorithm is as
1177 * follows: 1173 * follows:
@@ -1186,13 +1182,13 @@ out_unlock_leb:
1186 * reported by real users. 1182 * reported by real users.
1187 */ 1183 */
1188static void print_rsvd_warning(struct ubi_device *ubi, 1184static void print_rsvd_warning(struct ubi_device *ubi,
1189 struct ubi_scan_info *si) 1185 struct ubi_attach_info *ai)
1190{ 1186{
1191 /* 1187 /*
1192 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably 1188 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1193 * large number to distinguish between newly flashed and used images. 1189 * large number to distinguish between newly flashed and used images.
1194 */ 1190 */
1195 if (si->max_sqnum > (1 << 18)) { 1191 if (ai->max_sqnum > (1 << 18)) {
1196 int min = ubi->beb_rsvd_level / 10; 1192 int min = ubi->beb_rsvd_level / 10;
1197 1193
1198 if (!min) 1194 if (!min)
@@ -1209,19 +1205,19 @@ static void print_rsvd_warning(struct ubi_device *ubi,
1209} 1205}
1210 1206
1211/** 1207/**
1212 * ubi_eba_init_scan - initialize the EBA sub-system using scanning information. 1208 * ubi_eba_init - initialize the EBA sub-system using attaching information.
1213 * @ubi: UBI device description object 1209 * @ubi: UBI device description object
1214 * @si: scanning information 1210 * @ai: attaching information
1215 * 1211 *
1216 * This function returns zero in case of success and a negative error code in 1212 * This function returns zero in case of success and a negative error code in
1217 * case of failure. 1213 * case of failure.
1218 */ 1214 */
1219int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) 1215int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1220{ 1216{
1221 int i, j, err, num_volumes; 1217 int i, j, err, num_volumes;
1222 struct ubi_scan_volume *sv; 1218 struct ubi_ainf_volume *av;
1223 struct ubi_volume *vol; 1219 struct ubi_volume *vol;
1224 struct ubi_scan_leb *seb; 1220 struct ubi_ainf_peb *aeb;
1225 struct rb_node *rb; 1221 struct rb_node *rb;
1226 1222
1227 dbg_eba("initialize EBA sub-system"); 1223 dbg_eba("initialize EBA sub-system");
@@ -1230,7 +1226,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1230 mutex_init(&ubi->alc_mutex); 1226 mutex_init(&ubi->alc_mutex);
1231 ubi->ltree = RB_ROOT; 1227 ubi->ltree = RB_ROOT;
1232 1228
1233 ubi->global_sqnum = si->max_sqnum + 1; 1229 ubi->global_sqnum = ai->max_sqnum + 1;
1234 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; 1230 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1235 1231
1236 for (i = 0; i < num_volumes; i++) { 1232 for (i = 0; i < num_volumes; i++) {
@@ -1250,18 +1246,18 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1250 for (j = 0; j < vol->reserved_pebs; j++) 1246 for (j = 0; j < vol->reserved_pebs; j++)
1251 vol->eba_tbl[j] = UBI_LEB_UNMAPPED; 1247 vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1252 1248
1253 sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i)); 1249 av = ubi_find_av(ai, idx2vol_id(ubi, i));
1254 if (!sv) 1250 if (!av)
1255 continue; 1251 continue;
1256 1252
1257 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { 1253 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1258 if (seb->lnum >= vol->reserved_pebs) 1254 if (aeb->lnum >= vol->reserved_pebs)
1259 /* 1255 /*
1260 * This may happen in case of an unclean reboot 1256 * This may happen in case of an unclean reboot
1261 * during re-size. 1257 * during re-size.
1262 */ 1258 */
1263 ubi_scan_move_to_list(sv, seb, &si->erase); 1259 ubi_move_aeb_to_list(av, aeb, &ai->erase);
1264 vol->eba_tbl[seb->lnum] = seb->pnum; 1260 vol->eba_tbl[aeb->lnum] = aeb->pnum;
1265 } 1261 }
1266 } 1262 }
1267 1263
@@ -1283,7 +1279,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1283 if (ubi->avail_pebs < ubi->beb_rsvd_level) { 1279 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1284 /* No enough free physical eraseblocks */ 1280 /* No enough free physical eraseblocks */
1285 ubi->beb_rsvd_pebs = ubi->avail_pebs; 1281 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1286 print_rsvd_warning(ubi, si); 1282 print_rsvd_warning(ubi, ai);
1287 } else 1283 } else
1288 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; 1284 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1289 1285
diff --git a/drivers/mtd/ubi/gluebi.c b/drivers/mtd/ubi/gluebi.c
index 90b98822d9a4..4e44bee4c564 100644
--- a/drivers/mtd/ubi/gluebi.c
+++ b/drivers/mtd/ubi/gluebi.c
@@ -227,7 +227,7 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
227 if (to_write > total_written) 227 if (to_write > total_written)
228 to_write = total_written; 228 to_write = total_written;
229 229
230 err = ubi_write(gluebi->desc, lnum, buf, offs, to_write); 230 err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
231 if (err) 231 if (err)
232 break; 232 break;
233 233
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 43f1a0011a55..a8d523794b52 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -91,21 +91,15 @@
91#include <linux/slab.h> 91#include <linux/slab.h>
92#include "ubi.h" 92#include "ubi.h"
93 93
94#ifdef CONFIG_MTD_UBI_DEBUG 94static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
95static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); 95static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
96static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 96static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
97static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 97 const struct ubi_ec_hdr *ec_hdr);
98 const struct ubi_ec_hdr *ec_hdr); 98static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 99static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 100 const struct ubi_vid_hdr *vid_hdr);
101 const struct ubi_vid_hdr *vid_hdr); 101static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
102#else 102 int offset, int len);
103#define paranoid_check_not_bad(ubi, pnum) 0
104#define paranoid_check_peb_ec_hdr(ubi, pnum) 0
105#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
106#define paranoid_check_peb_vid_hdr(ubi, pnum) 0
107#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
108#endif
109 103
110/** 104/**
111 * ubi_io_read - read data from a physical eraseblock. 105 * ubi_io_read - read data from a physical eraseblock.
@@ -142,7 +136,7 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
142 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 136 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
143 ubi_assert(len > 0); 137 ubi_assert(len > 0);
144 138
145 err = paranoid_check_not_bad(ubi, pnum); 139 err = self_check_not_bad(ubi, pnum);
146 if (err) 140 if (err)
147 return err; 141 return err;
148 142
@@ -189,16 +183,16 @@ retry:
189 } 183 }
190 184
191 if (retries++ < UBI_IO_RETRIES) { 185 if (retries++ < UBI_IO_RETRIES) {
192 dbg_io("error %d%s while reading %d bytes from PEB " 186 ubi_warn("error %d%s while reading %d bytes from PEB "
193 "%d:%d, read only %zd bytes, retry", 187 "%d:%d, read only %zd bytes, retry",
194 err, errstr, len, pnum, offset, read); 188 err, errstr, len, pnum, offset, read);
195 yield(); 189 yield();
196 goto retry; 190 goto retry;
197 } 191 }
198 192
199 ubi_err("error %d%s while reading %d bytes from PEB %d:%d, " 193 ubi_err("error %d%s while reading %d bytes from PEB %d:%d, "
200 "read %zd bytes", err, errstr, len, pnum, offset, read); 194 "read %zd bytes", err, errstr, len, pnum, offset, read);
201 ubi_dbg_dump_stack(); 195 dump_stack();
202 196
203 /* 197 /*
204 * The driver should never return -EBADMSG if it failed to read 198 * The driver should never return -EBADMSG if it failed to read
@@ -257,14 +251,12 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
257 return -EROFS; 251 return -EROFS;
258 } 252 }
259 253
260 /* The below has to be compiled out if paranoid checks are disabled */ 254 err = self_check_not_bad(ubi, pnum);
261
262 err = paranoid_check_not_bad(ubi, pnum);
263 if (err) 255 if (err)
264 return err; 256 return err;
265 257
266 /* The area we are writing to has to contain all 0xFF bytes */ 258 /* The area we are writing to has to contain all 0xFF bytes */
267 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 259 err = ubi_self_check_all_ff(ubi, pnum, offset, len);
268 if (err) 260 if (err)
269 return err; 261 return err;
270 262
@@ -273,18 +265,18 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
273 * We write to the data area of the physical eraseblock. Make 265 * We write to the data area of the physical eraseblock. Make
274 * sure it has valid EC and VID headers. 266 * sure it has valid EC and VID headers.
275 */ 267 */
276 err = paranoid_check_peb_ec_hdr(ubi, pnum); 268 err = self_check_peb_ec_hdr(ubi, pnum);
277 if (err) 269 if (err)
278 return err; 270 return err;
279 err = paranoid_check_peb_vid_hdr(ubi, pnum); 271 err = self_check_peb_vid_hdr(ubi, pnum);
280 if (err) 272 if (err)
281 return err; 273 return err;
282 } 274 }
283 275
284 if (ubi_dbg_is_write_failure(ubi)) { 276 if (ubi_dbg_is_write_failure(ubi)) {
285 dbg_err("cannot write %d bytes to PEB %d:%d " 277 ubi_err("cannot write %d bytes to PEB %d:%d "
286 "(emulated)", len, pnum, offset); 278 "(emulated)", len, pnum, offset);
287 ubi_dbg_dump_stack(); 279 dump_stack();
288 return -EIO; 280 return -EIO;
289 } 281 }
290 282
@@ -293,13 +285,13 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
293 if (err) { 285 if (err) {
294 ubi_err("error %d while writing %d bytes to PEB %d:%d, written " 286 ubi_err("error %d while writing %d bytes to PEB %d:%d, written "
295 "%zd bytes", err, len, pnum, offset, written); 287 "%zd bytes", err, len, pnum, offset, written);
296 ubi_dbg_dump_stack(); 288 dump_stack();
297 ubi_dbg_dump_flash(ubi, pnum, offset, len); 289 ubi_dump_flash(ubi, pnum, offset, len);
298 } else 290 } else
299 ubi_assert(written == len); 291 ubi_assert(written == len);
300 292
301 if (!err) { 293 if (!err) {
302 err = ubi_dbg_check_write(ubi, buf, pnum, offset, len); 294 err = self_check_write(ubi, buf, pnum, offset, len);
303 if (err) 295 if (err)
304 return err; 296 return err;
305 297
@@ -310,7 +302,7 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
310 offset += len; 302 offset += len;
311 len = ubi->peb_size - offset; 303 len = ubi->peb_size - offset;
312 if (len) 304 if (len)
313 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 305 err = ubi_self_check_all_ff(ubi, pnum, offset, len);
314 } 306 }
315 307
316 return err; 308 return err;
@@ -364,13 +356,13 @@ retry:
364 err = mtd_erase(ubi->mtd, &ei); 356 err = mtd_erase(ubi->mtd, &ei);
365 if (err) { 357 if (err) {
366 if (retries++ < UBI_IO_RETRIES) { 358 if (retries++ < UBI_IO_RETRIES) {
367 dbg_io("error %d while erasing PEB %d, retry", 359 ubi_warn("error %d while erasing PEB %d, retry",
368 err, pnum); 360 err, pnum);
369 yield(); 361 yield();
370 goto retry; 362 goto retry;
371 } 363 }
372 ubi_err("cannot erase PEB %d, error %d", pnum, err); 364 ubi_err("cannot erase PEB %d, error %d", pnum, err);
373 ubi_dbg_dump_stack(); 365 dump_stack();
374 return err; 366 return err;
375 } 367 }
376 368
@@ -383,21 +375,21 @@ retry:
383 375
384 if (ei.state == MTD_ERASE_FAILED) { 376 if (ei.state == MTD_ERASE_FAILED) {
385 if (retries++ < UBI_IO_RETRIES) { 377 if (retries++ < UBI_IO_RETRIES) {
386 dbg_io("error while erasing PEB %d, retry", pnum); 378 ubi_warn("error while erasing PEB %d, retry", pnum);
387 yield(); 379 yield();
388 goto retry; 380 goto retry;
389 } 381 }
390 ubi_err("cannot erase PEB %d", pnum); 382 ubi_err("cannot erase PEB %d", pnum);
391 ubi_dbg_dump_stack(); 383 dump_stack();
392 return -EIO; 384 return -EIO;
393 } 385 }
394 386
395 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 387 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size);
396 if (err) 388 if (err)
397 return err; 389 return err;
398 390
399 if (ubi_dbg_is_erase_failure(ubi)) { 391 if (ubi_dbg_is_erase_failure(ubi)) {
400 dbg_err("cannot erase PEB %d (emulated)", pnum); 392 ubi_err("cannot erase PEB %d (emulated)", pnum);
401 return -EIO; 393 return -EIO;
402 } 394 }
403 395
@@ -521,8 +513,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
521 * It is important to first invalidate the EC header, and then the VID 513 * It is important to first invalidate the EC header, and then the VID
522 * header. Otherwise a power cut may lead to valid EC header and 514 * header. Otherwise a power cut may lead to valid EC header and
523 * invalid VID header, in which case UBI will treat this PEB as 515 * invalid VID header, in which case UBI will treat this PEB as
524 * corrupted and will try to preserve it, and print scary warnings (see 516 * corrupted and will try to preserve it, and print scary warnings.
525 * the header comment in scan.c for more information).
526 */ 517 */
527 addr = (loff_t)pnum * ubi->peb_size; 518 addr = (loff_t)pnum * ubi->peb_size;
528 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 519 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
@@ -563,7 +554,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
563 */ 554 */
564 ubi_err("cannot invalidate PEB %d, write returned %d read returned %d", 555 ubi_err("cannot invalidate PEB %d, write returned %d read returned %d",
565 pnum, err, err1); 556 pnum, err, err1);
566 ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size); 557 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size);
567 return -EIO; 558 return -EIO;
568} 559}
569 560
@@ -589,7 +580,7 @@ int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
589 580
590 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 581 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
591 582
592 err = paranoid_check_not_bad(ubi, pnum); 583 err = self_check_not_bad(ubi, pnum);
593 if (err != 0) 584 if (err != 0)
594 return err; 585 return err;
595 586
@@ -721,8 +712,8 @@ static int validate_ec_hdr(const struct ubi_device *ubi,
721 712
722bad: 713bad:
723 ubi_err("bad EC header"); 714 ubi_err("bad EC header");
724 ubi_dbg_dump_ec_hdr(ec_hdr); 715 ubi_dump_ec_hdr(ec_hdr);
725 ubi_dbg_dump_stack(); 716 dump_stack();
726 return 1; 717 return 1;
727} 718}
728 719
@@ -803,7 +794,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
803 if (verbose) { 794 if (verbose) {
804 ubi_warn("bad magic number at PEB %d: %08x instead of " 795 ubi_warn("bad magic number at PEB %d: %08x instead of "
805 "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 796 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
806 ubi_dbg_dump_ec_hdr(ec_hdr); 797 ubi_dump_ec_hdr(ec_hdr);
807 } 798 }
808 dbg_bld("bad magic number at PEB %d: %08x instead of " 799 dbg_bld("bad magic number at PEB %d: %08x instead of "
809 "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 800 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
@@ -817,7 +808,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
817 if (verbose) { 808 if (verbose) {
818 ubi_warn("bad EC header CRC at PEB %d, calculated " 809 ubi_warn("bad EC header CRC at PEB %d, calculated "
819 "%#08x, read %#08x", pnum, crc, hdr_crc); 810 "%#08x, read %#08x", pnum, crc, hdr_crc);
820 ubi_dbg_dump_ec_hdr(ec_hdr); 811 ubi_dump_ec_hdr(ec_hdr);
821 } 812 }
822 dbg_bld("bad EC header CRC at PEB %d, calculated " 813 dbg_bld("bad EC header CRC at PEB %d, calculated "
823 "%#08x, read %#08x", pnum, crc, hdr_crc); 814 "%#08x, read %#08x", pnum, crc, hdr_crc);
@@ -874,7 +865,7 @@ int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
874 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 865 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
875 ec_hdr->hdr_crc = cpu_to_be32(crc); 866 ec_hdr->hdr_crc = cpu_to_be32(crc);
876 867
877 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 868 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
878 if (err) 869 if (err)
879 return err; 870 return err;
880 871
@@ -905,40 +896,40 @@ static int validate_vid_hdr(const struct ubi_device *ubi,
905 int usable_leb_size = ubi->leb_size - data_pad; 896 int usable_leb_size = ubi->leb_size - data_pad;
906 897
907 if (copy_flag != 0 && copy_flag != 1) { 898 if (copy_flag != 0 && copy_flag != 1) {
908 dbg_err("bad copy_flag"); 899 ubi_err("bad copy_flag");
909 goto bad; 900 goto bad;
910 } 901 }
911 902
912 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 903 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
913 data_pad < 0) { 904 data_pad < 0) {
914 dbg_err("negative values"); 905 ubi_err("negative values");
915 goto bad; 906 goto bad;
916 } 907 }
917 908
918 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 909 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
919 dbg_err("bad vol_id"); 910 ubi_err("bad vol_id");
920 goto bad; 911 goto bad;
921 } 912 }
922 913
923 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 914 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
924 dbg_err("bad compat"); 915 ubi_err("bad compat");
925 goto bad; 916 goto bad;
926 } 917 }
927 918
928 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 919 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
929 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 920 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
930 compat != UBI_COMPAT_REJECT) { 921 compat != UBI_COMPAT_REJECT) {
931 dbg_err("bad compat"); 922 ubi_err("bad compat");
932 goto bad; 923 goto bad;
933 } 924 }
934 925
935 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 926 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
936 dbg_err("bad vol_type"); 927 ubi_err("bad vol_type");
937 goto bad; 928 goto bad;
938 } 929 }
939 930
940 if (data_pad >= ubi->leb_size / 2) { 931 if (data_pad >= ubi->leb_size / 2) {
941 dbg_err("bad data_pad"); 932 ubi_err("bad data_pad");
942 goto bad; 933 goto bad;
943 } 934 }
944 935
@@ -950,45 +941,45 @@ static int validate_vid_hdr(const struct ubi_device *ubi,
950 * mapped logical eraseblocks. 941 * mapped logical eraseblocks.
951 */ 942 */
952 if (used_ebs == 0) { 943 if (used_ebs == 0) {
953 dbg_err("zero used_ebs"); 944 ubi_err("zero used_ebs");
954 goto bad; 945 goto bad;
955 } 946 }
956 if (data_size == 0) { 947 if (data_size == 0) {
957 dbg_err("zero data_size"); 948 ubi_err("zero data_size");
958 goto bad; 949 goto bad;
959 } 950 }
960 if (lnum < used_ebs - 1) { 951 if (lnum < used_ebs - 1) {
961 if (data_size != usable_leb_size) { 952 if (data_size != usable_leb_size) {
962 dbg_err("bad data_size"); 953 ubi_err("bad data_size");
963 goto bad; 954 goto bad;
964 } 955 }
965 } else if (lnum == used_ebs - 1) { 956 } else if (lnum == used_ebs - 1) {
966 if (data_size == 0) { 957 if (data_size == 0) {
967 dbg_err("bad data_size at last LEB"); 958 ubi_err("bad data_size at last LEB");
968 goto bad; 959 goto bad;
969 } 960 }
970 } else { 961 } else {
971 dbg_err("too high lnum"); 962 ubi_err("too high lnum");
972 goto bad; 963 goto bad;
973 } 964 }
974 } else { 965 } else {
975 if (copy_flag == 0) { 966 if (copy_flag == 0) {
976 if (data_crc != 0) { 967 if (data_crc != 0) {
977 dbg_err("non-zero data CRC"); 968 ubi_err("non-zero data CRC");
978 goto bad; 969 goto bad;
979 } 970 }
980 if (data_size != 0) { 971 if (data_size != 0) {
981 dbg_err("non-zero data_size"); 972 ubi_err("non-zero data_size");
982 goto bad; 973 goto bad;
983 } 974 }
984 } else { 975 } else {
985 if (data_size == 0) { 976 if (data_size == 0) {
986 dbg_err("zero data_size of copy"); 977 ubi_err("zero data_size of copy");
987 goto bad; 978 goto bad;
988 } 979 }
989 } 980 }
990 if (used_ebs != 0) { 981 if (used_ebs != 0) {
991 dbg_err("bad used_ebs"); 982 ubi_err("bad used_ebs");
992 goto bad; 983 goto bad;
993 } 984 }
994 } 985 }
@@ -997,8 +988,8 @@ static int validate_vid_hdr(const struct ubi_device *ubi,
997 988
998bad: 989bad:
999 ubi_err("bad VID header"); 990 ubi_err("bad VID header");
1000 ubi_dbg_dump_vid_hdr(vid_hdr); 991 ubi_dump_vid_hdr(vid_hdr);
1001 ubi_dbg_dump_stack(); 992 dump_stack();
1002 return 1; 993 return 1;
1003} 994}
1004 995
@@ -1054,7 +1045,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1054 if (verbose) { 1045 if (verbose) {
1055 ubi_warn("bad magic number at PEB %d: %08x instead of " 1046 ubi_warn("bad magic number at PEB %d: %08x instead of "
1056 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1047 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1057 ubi_dbg_dump_vid_hdr(vid_hdr); 1048 ubi_dump_vid_hdr(vid_hdr);
1058 } 1049 }
1059 dbg_bld("bad magic number at PEB %d: %08x instead of " 1050 dbg_bld("bad magic number at PEB %d: %08x instead of "
1060 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1051 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
@@ -1068,7 +1059,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1068 if (verbose) { 1059 if (verbose) {
1069 ubi_warn("bad CRC at PEB %d, calculated %#08x, " 1060 ubi_warn("bad CRC at PEB %d, calculated %#08x, "
1070 "read %#08x", pnum, crc, hdr_crc); 1061 "read %#08x", pnum, crc, hdr_crc);
1071 ubi_dbg_dump_vid_hdr(vid_hdr); 1062 ubi_dump_vid_hdr(vid_hdr);
1072 } 1063 }
1073 dbg_bld("bad CRC at PEB %d, calculated %#08x, " 1064 dbg_bld("bad CRC at PEB %d, calculated %#08x, "
1074 "read %#08x", pnum, crc, hdr_crc); 1065 "read %#08x", pnum, crc, hdr_crc);
@@ -1112,7 +1103,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1112 dbg_io("write VID header to PEB %d", pnum); 1103 dbg_io("write VID header to PEB %d", pnum);
1113 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1104 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1114 1105
1115 err = paranoid_check_peb_ec_hdr(ubi, pnum); 1106 err = self_check_peb_ec_hdr(ubi, pnum);
1116 if (err) 1107 if (err)
1117 return err; 1108 return err;
1118 1109
@@ -1121,7 +1112,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1121 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 1112 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1122 vid_hdr->hdr_crc = cpu_to_be32(crc); 1113 vid_hdr->hdr_crc = cpu_to_be32(crc);
1123 1114
1124 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1115 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1125 if (err) 1116 if (err)
1126 return err; 1117 return err;
1127 1118
@@ -1131,17 +1122,15 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1131 return err; 1122 return err;
1132} 1123}
1133 1124
1134#ifdef CONFIG_MTD_UBI_DEBUG
1135
1136/** 1125/**
1137 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 1126 * self_check_not_bad - ensure that a physical eraseblock is not bad.
1138 * @ubi: UBI device description object 1127 * @ubi: UBI device description object
1139 * @pnum: physical eraseblock number to check 1128 * @pnum: physical eraseblock number to check
1140 * 1129 *
1141 * This function returns zero if the physical eraseblock is good, %-EINVAL if 1130 * This function returns zero if the physical eraseblock is good, %-EINVAL if
1142 * it is bad and a negative error code if an error occurred. 1131 * it is bad and a negative error code if an error occurred.
1143 */ 1132 */
1144static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1133static int self_check_not_bad(const struct ubi_device *ubi, int pnum)
1145{ 1134{
1146 int err; 1135 int err;
1147 1136
@@ -1152,13 +1141,13 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1152 if (!err) 1141 if (!err)
1153 return err; 1142 return err;
1154 1143
1155 ubi_err("paranoid check failed for PEB %d", pnum); 1144 ubi_err("self-check failed for PEB %d", pnum);
1156 ubi_dbg_dump_stack(); 1145 dump_stack();
1157 return err > 0 ? -EINVAL : err; 1146 return err > 0 ? -EINVAL : err;
1158} 1147}
1159 1148
1160/** 1149/**
1161 * paranoid_check_ec_hdr - check if an erase counter header is all right. 1150 * self_check_ec_hdr - check if an erase counter header is all right.
1162 * @ubi: UBI device description object 1151 * @ubi: UBI device description object
1163 * @pnum: physical eraseblock number the erase counter header belongs to 1152 * @pnum: physical eraseblock number the erase counter header belongs to
1164 * @ec_hdr: the erase counter header to check 1153 * @ec_hdr: the erase counter header to check
@@ -1166,8 +1155,8 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1166 * This function returns zero if the erase counter header contains valid 1155 * This function returns zero if the erase counter header contains valid
1167 * values, and %-EINVAL if not. 1156 * values, and %-EINVAL if not.
1168 */ 1157 */
1169static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1158static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1170 const struct ubi_ec_hdr *ec_hdr) 1159 const struct ubi_ec_hdr *ec_hdr)
1171{ 1160{
1172 int err; 1161 int err;
1173 uint32_t magic; 1162 uint32_t magic;
@@ -1184,27 +1173,27 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1184 1173
1185 err = validate_ec_hdr(ubi, ec_hdr); 1174 err = validate_ec_hdr(ubi, ec_hdr);
1186 if (err) { 1175 if (err) {
1187 ubi_err("paranoid check failed for PEB %d", pnum); 1176 ubi_err("self-check failed for PEB %d", pnum);
1188 goto fail; 1177 goto fail;
1189 } 1178 }
1190 1179
1191 return 0; 1180 return 0;
1192 1181
1193fail: 1182fail:
1194 ubi_dbg_dump_ec_hdr(ec_hdr); 1183 ubi_dump_ec_hdr(ec_hdr);
1195 ubi_dbg_dump_stack(); 1184 dump_stack();
1196 return -EINVAL; 1185 return -EINVAL;
1197} 1186}
1198 1187
1199/** 1188/**
1200 * paranoid_check_peb_ec_hdr - check erase counter header. 1189 * self_check_peb_ec_hdr - check erase counter header.
1201 * @ubi: UBI device description object 1190 * @ubi: UBI device description object
1202 * @pnum: the physical eraseblock number to check 1191 * @pnum: the physical eraseblock number to check
1203 * 1192 *
1204 * This function returns zero if the erase counter header is all right and and 1193 * This function returns zero if the erase counter header is all right and and
1205 * a negative error code if not or if an error occurred. 1194 * a negative error code if not or if an error occurred.
1206 */ 1195 */
1207static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1196static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1208{ 1197{
1209 int err; 1198 int err;
1210 uint32_t crc, hdr_crc; 1199 uint32_t crc, hdr_crc;
@@ -1225,14 +1214,14 @@ static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1225 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 1214 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1226 if (hdr_crc != crc) { 1215 if (hdr_crc != crc) {
1227 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 1216 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1228 ubi_err("paranoid check failed for PEB %d", pnum); 1217 ubi_err("self-check failed for PEB %d", pnum);
1229 ubi_dbg_dump_ec_hdr(ec_hdr); 1218 ubi_dump_ec_hdr(ec_hdr);
1230 ubi_dbg_dump_stack(); 1219 dump_stack();
1231 err = -EINVAL; 1220 err = -EINVAL;
1232 goto exit; 1221 goto exit;
1233 } 1222 }
1234 1223
1235 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 1224 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
1236 1225
1237exit: 1226exit:
1238 kfree(ec_hdr); 1227 kfree(ec_hdr);
@@ -1240,7 +1229,7 @@ exit:
1240} 1229}
1241 1230
1242/** 1231/**
1243 * paranoid_check_vid_hdr - check that a volume identifier header is all right. 1232 * self_check_vid_hdr - check that a volume identifier header is all right.
1244 * @ubi: UBI device description object 1233 * @ubi: UBI device description object
1245 * @pnum: physical eraseblock number the volume identifier header belongs to 1234 * @pnum: physical eraseblock number the volume identifier header belongs to
1246 * @vid_hdr: the volume identifier header to check 1235 * @vid_hdr: the volume identifier header to check
@@ -1248,8 +1237,8 @@ exit:
1248 * This function returns zero if the volume identifier header is all right, and 1237 * This function returns zero if the volume identifier header is all right, and
1249 * %-EINVAL if not. 1238 * %-EINVAL if not.
1250 */ 1239 */
1251static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1240static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1252 const struct ubi_vid_hdr *vid_hdr) 1241 const struct ubi_vid_hdr *vid_hdr)
1253{ 1242{
1254 int err; 1243 int err;
1255 uint32_t magic; 1244 uint32_t magic;
@@ -1266,29 +1255,29 @@ static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1266 1255
1267 err = validate_vid_hdr(ubi, vid_hdr); 1256 err = validate_vid_hdr(ubi, vid_hdr);
1268 if (err) { 1257 if (err) {
1269 ubi_err("paranoid check failed for PEB %d", pnum); 1258 ubi_err("self-check failed for PEB %d", pnum);
1270 goto fail; 1259 goto fail;
1271 } 1260 }
1272 1261
1273 return err; 1262 return err;
1274 1263
1275fail: 1264fail:
1276 ubi_err("paranoid check failed for PEB %d", pnum); 1265 ubi_err("self-check failed for PEB %d", pnum);
1277 ubi_dbg_dump_vid_hdr(vid_hdr); 1266 ubi_dump_vid_hdr(vid_hdr);
1278 ubi_dbg_dump_stack(); 1267 dump_stack();
1279 return -EINVAL; 1268 return -EINVAL;
1280 1269
1281} 1270}
1282 1271
1283/** 1272/**
1284 * paranoid_check_peb_vid_hdr - check volume identifier header. 1273 * self_check_peb_vid_hdr - check volume identifier header.
1285 * @ubi: UBI device description object 1274 * @ubi: UBI device description object
1286 * @pnum: the physical eraseblock number to check 1275 * @pnum: the physical eraseblock number to check
1287 * 1276 *
1288 * This function returns zero if the volume identifier header is all right, 1277 * This function returns zero if the volume identifier header is all right,
1289 * and a negative error code if not or if an error occurred. 1278 * and a negative error code if not or if an error occurred.
1290 */ 1279 */
1291static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1280static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1292{ 1281{
1293 int err; 1282 int err;
1294 uint32_t crc, hdr_crc; 1283 uint32_t crc, hdr_crc;
@@ -1313,14 +1302,14 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1313 if (hdr_crc != crc) { 1302 if (hdr_crc != crc) {
1314 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 1303 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1315 "read %#08x", pnum, crc, hdr_crc); 1304 "read %#08x", pnum, crc, hdr_crc);
1316 ubi_err("paranoid check failed for PEB %d", pnum); 1305 ubi_err("self-check failed for PEB %d", pnum);
1317 ubi_dbg_dump_vid_hdr(vid_hdr); 1306 ubi_dump_vid_hdr(vid_hdr);
1318 ubi_dbg_dump_stack(); 1307 dump_stack();
1319 err = -EINVAL; 1308 err = -EINVAL;
1320 goto exit; 1309 goto exit;
1321 } 1310 }
1322 1311
1323 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1312 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1324 1313
1325exit: 1314exit:
1326 ubi_free_vid_hdr(ubi, vid_hdr); 1315 ubi_free_vid_hdr(ubi, vid_hdr);
@@ -1328,7 +1317,7 @@ exit:
1328} 1317}
1329 1318
1330/** 1319/**
1331 * ubi_dbg_check_write - make sure write succeeded. 1320 * self_check_write - make sure write succeeded.
1332 * @ubi: UBI device description object 1321 * @ubi: UBI device description object
1333 * @buf: buffer with data which were written 1322 * @buf: buffer with data which were written
1334 * @pnum: physical eraseblock number the data were written to 1323 * @pnum: physical eraseblock number the data were written to
@@ -1339,8 +1328,8 @@ exit:
1339 * the original data buffer - the data have to match. Returns zero if the data 1328 * the original data buffer - the data have to match. Returns zero if the data
1340 * match and a negative error code if not or in case of failure. 1329 * match and a negative error code if not or in case of failure.
1341 */ 1330 */
1342int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, 1331static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1343 int offset, int len) 1332 int offset, int len)
1344{ 1333{
1345 int err, i; 1334 int err, i;
1346 size_t read; 1335 size_t read;
@@ -1368,7 +1357,7 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1368 if (c == c1) 1357 if (c == c1)
1369 continue; 1358 continue;
1370 1359
1371 ubi_err("paranoid check failed for PEB %d:%d, len %d", 1360 ubi_err("self-check failed for PEB %d:%d, len %d",
1372 pnum, offset, len); 1361 pnum, offset, len);
1373 ubi_msg("data differ at position %d", i); 1362 ubi_msg("data differ at position %d", i);
1374 dump_len = max_t(int, 128, len - i); 1363 dump_len = max_t(int, 128, len - i);
@@ -1380,7 +1369,7 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1380 i, i + dump_len); 1369 i, i + dump_len);
1381 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1370 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1382 buf1 + i, dump_len, 1); 1371 buf1 + i, dump_len, 1);
1383 ubi_dbg_dump_stack(); 1372 dump_stack();
1384 err = -EINVAL; 1373 err = -EINVAL;
1385 goto out_free; 1374 goto out_free;
1386 } 1375 }
@@ -1394,7 +1383,7 @@ out_free:
1394} 1383}
1395 1384
1396/** 1385/**
1397 * ubi_dbg_check_all_ff - check that a region of flash is empty. 1386 * ubi_self_check_all_ff - check that a region of flash is empty.
1398 * @ubi: UBI device description object 1387 * @ubi: UBI device description object
1399 * @pnum: the physical eraseblock number to check 1388 * @pnum: the physical eraseblock number to check
1400 * @offset: the starting offset within the physical eraseblock to check 1389 * @offset: the starting offset within the physical eraseblock to check
@@ -1404,7 +1393,7 @@ out_free:
1404 * @offset of the physical eraseblock @pnum, and a negative error code if not 1393 * @offset of the physical eraseblock @pnum, and a negative error code if not
1405 * or if an error occurred. 1394 * or if an error occurred.
1406 */ 1395 */
1407int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1396int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1408{ 1397{
1409 size_t read; 1398 size_t read;
1410 int err; 1399 int err;
@@ -1438,14 +1427,12 @@ int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1438 return 0; 1427 return 0;
1439 1428
1440fail: 1429fail:
1441 ubi_err("paranoid check failed for PEB %d", pnum); 1430 ubi_err("self-check failed for PEB %d", pnum);
1442 ubi_msg("hex dump of the %d-%d region", offset, offset + len); 1431 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1443 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); 1432 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1444 err = -EINVAL; 1433 err = -EINVAL;
1445error: 1434error:
1446 ubi_dbg_dump_stack(); 1435 dump_stack();
1447 vfree(buf); 1436 vfree(buf);
1448 return err; 1437 return err;
1449} 1438}
1450
1451#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c
index 9fdb35367fe0..3aac1acceeb4 100644
--- a/drivers/mtd/ubi/kapi.c
+++ b/drivers/mtd/ubi/kapi.c
@@ -221,7 +221,7 @@ out_free:
221 kfree(desc); 221 kfree(desc);
222out_put_ubi: 222out_put_ubi:
223 ubi_put_device(ubi); 223 ubi_put_device(ubi);
224 dbg_err("cannot open device %d, volume %d, error %d", 224 ubi_err("cannot open device %d, volume %d, error %d",
225 ubi_num, vol_id, err); 225 ubi_num, vol_id, err);
226 return ERR_PTR(err); 226 return ERR_PTR(err);
227} 227}
@@ -426,11 +426,9 @@ EXPORT_SYMBOL_GPL(ubi_leb_read);
426 * @buf: data to write 426 * @buf: data to write
427 * @offset: offset within the logical eraseblock where to write 427 * @offset: offset within the logical eraseblock where to write
428 * @len: how many bytes to write 428 * @len: how many bytes to write
429 * @dtype: expected data type
430 * 429 *
431 * This function writes @len bytes of data from @buf to offset @offset of 430 * This function writes @len bytes of data from @buf to offset @offset of
432 * logical eraseblock @lnum. The @dtype argument describes expected lifetime of 431 * logical eraseblock @lnum.
433 * the data.
434 * 432 *
435 * This function takes care of physical eraseblock write failures. If write to 433 * This function takes care of physical eraseblock write failures. If write to
436 * the physical eraseblock write operation fails, the logical eraseblock is 434 * the physical eraseblock write operation fails, the logical eraseblock is
@@ -447,7 +445,7 @@ EXPORT_SYMBOL_GPL(ubi_leb_read);
447 * returns immediately with %-EBADF code. 445 * returns immediately with %-EBADF code.
448 */ 446 */
449int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 447int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
450 int offset, int len, int dtype) 448 int offset, int len)
451{ 449{
452 struct ubi_volume *vol = desc->vol; 450 struct ubi_volume *vol = desc->vol;
453 struct ubi_device *ubi = vol->ubi; 451 struct ubi_device *ubi = vol->ubi;
@@ -466,17 +464,13 @@ int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
466 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1)) 464 offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
467 return -EINVAL; 465 return -EINVAL;
468 466
469 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
470 dtype != UBI_UNKNOWN)
471 return -EINVAL;
472
473 if (vol->upd_marker) 467 if (vol->upd_marker)
474 return -EBADF; 468 return -EBADF;
475 469
476 if (len == 0) 470 if (len == 0)
477 return 0; 471 return 0;
478 472
479 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len, dtype); 473 return ubi_eba_write_leb(ubi, vol, lnum, buf, offset, len);
480} 474}
481EXPORT_SYMBOL_GPL(ubi_leb_write); 475EXPORT_SYMBOL_GPL(ubi_leb_write);
482 476
@@ -486,7 +480,6 @@ EXPORT_SYMBOL_GPL(ubi_leb_write);
486 * @lnum: logical eraseblock number to change 480 * @lnum: logical eraseblock number to change
487 * @buf: data to write 481 * @buf: data to write
488 * @len: how many bytes to write 482 * @len: how many bytes to write
489 * @dtype: expected data type
490 * 483 *
491 * This function changes the contents of a logical eraseblock atomically. @buf 484 * This function changes the contents of a logical eraseblock atomically. @buf
492 * has to contain new logical eraseblock data, and @len - the length of the 485 * has to contain new logical eraseblock data, and @len - the length of the
@@ -497,7 +490,7 @@ EXPORT_SYMBOL_GPL(ubi_leb_write);
497 * code in case of failure. 490 * code in case of failure.
498 */ 491 */
499int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 492int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
500 int len, int dtype) 493 int len)
501{ 494{
502 struct ubi_volume *vol = desc->vol; 495 struct ubi_volume *vol = desc->vol;
503 struct ubi_device *ubi = vol->ubi; 496 struct ubi_device *ubi = vol->ubi;
@@ -515,17 +508,13 @@ int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
515 len > vol->usable_leb_size || len & (ubi->min_io_size - 1)) 508 len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
516 return -EINVAL; 509 return -EINVAL;
517 510
518 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
519 dtype != UBI_UNKNOWN)
520 return -EINVAL;
521
522 if (vol->upd_marker) 511 if (vol->upd_marker)
523 return -EBADF; 512 return -EBADF;
524 513
525 if (len == 0) 514 if (len == 0)
526 return 0; 515 return 0;
527 516
528 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len, dtype); 517 return ubi_eba_atomic_leb_change(ubi, vol, lnum, buf, len);
529} 518}
530EXPORT_SYMBOL_GPL(ubi_leb_change); 519EXPORT_SYMBOL_GPL(ubi_leb_change);
531 520
@@ -562,7 +551,7 @@ int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum)
562 if (err) 551 if (err)
563 return err; 552 return err;
564 553
565 return ubi_wl_flush(ubi); 554 return ubi_wl_flush(ubi, vol->vol_id, lnum);
566} 555}
567EXPORT_SYMBOL_GPL(ubi_leb_erase); 556EXPORT_SYMBOL_GPL(ubi_leb_erase);
568 557
@@ -626,7 +615,6 @@ EXPORT_SYMBOL_GPL(ubi_leb_unmap);
626 * ubi_leb_map - map logical eraseblock to a physical eraseblock. 615 * ubi_leb_map - map logical eraseblock to a physical eraseblock.
627 * @desc: volume descriptor 616 * @desc: volume descriptor
628 * @lnum: logical eraseblock number 617 * @lnum: logical eraseblock number
629 * @dtype: expected data type
630 * 618 *
631 * This function maps an un-mapped logical eraseblock @lnum to a physical 619 * This function maps an un-mapped logical eraseblock @lnum to a physical
632 * eraseblock. This means, that after a successful invocation of this 620 * eraseblock. This means, that after a successful invocation of this
@@ -639,7 +627,7 @@ EXPORT_SYMBOL_GPL(ubi_leb_unmap);
639 * eraseblock is already mapped, and other negative error codes in case of 627 * eraseblock is already mapped, and other negative error codes in case of
640 * other failures. 628 * other failures.
641 */ 629 */
642int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 630int ubi_leb_map(struct ubi_volume_desc *desc, int lnum)
643{ 631{
644 struct ubi_volume *vol = desc->vol; 632 struct ubi_volume *vol = desc->vol;
645 struct ubi_device *ubi = vol->ubi; 633 struct ubi_device *ubi = vol->ubi;
@@ -652,17 +640,13 @@ int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
652 if (lnum < 0 || lnum >= vol->reserved_pebs) 640 if (lnum < 0 || lnum >= vol->reserved_pebs)
653 return -EINVAL; 641 return -EINVAL;
654 642
655 if (dtype != UBI_LONGTERM && dtype != UBI_SHORTTERM &&
656 dtype != UBI_UNKNOWN)
657 return -EINVAL;
658
659 if (vol->upd_marker) 643 if (vol->upd_marker)
660 return -EBADF; 644 return -EBADF;
661 645
662 if (vol->eba_tbl[lnum] >= 0) 646 if (vol->eba_tbl[lnum] >= 0)
663 return -EBADMSG; 647 return -EBADMSG;
664 648
665 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype); 649 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
666} 650}
667EXPORT_SYMBOL_GPL(ubi_leb_map); 651EXPORT_SYMBOL_GPL(ubi_leb_map);
668 652
@@ -720,6 +704,33 @@ int ubi_sync(int ubi_num)
720} 704}
721EXPORT_SYMBOL_GPL(ubi_sync); 705EXPORT_SYMBOL_GPL(ubi_sync);
722 706
707/**
708 * ubi_flush - flush UBI work queue.
709 * @ubi_num: UBI device to flush work queue
710 * @vol_id: volume id to flush for
711 * @lnum: logical eraseblock number to flush for
712 *
713 * This function executes all pending works for a particular volume id / logical
714 * eraseblock number pair. If either value is set to %UBI_ALL, then it acts as
715 * a wildcard for all of the corresponding volume numbers or logical
716 * eraseblock numbers. It returns zero in case of success and a negative error
717 * code in case of failure.
718 */
719int ubi_flush(int ubi_num, int vol_id, int lnum)
720{
721 struct ubi_device *ubi;
722 int err = 0;
723
724 ubi = ubi_get_device(ubi_num);
725 if (!ubi)
726 return -ENODEV;
727
728 err = ubi_wl_flush(ubi, vol_id, lnum);
729 ubi_put_device(ubi);
730 return err;
731}
732EXPORT_SYMBOL_GPL(ubi_flush);
733
723BLOCKING_NOTIFIER_HEAD(ubi_notifiers); 734BLOCKING_NOTIFIER_HEAD(ubi_notifiers);
724 735
725/** 736/**
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
deleted file mode 100644
index d48aef15ab5d..000000000000
--- a/drivers/mtd/ubi/scan.h
+++ /dev/null
@@ -1,174 +0,0 @@
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 * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
34 * @sqnum: sequence number
35 * @u: unions RB-tree or @list links
36 * @u.rb: link in the per-volume RB-tree of &struct ubi_scan_leb objects
37 * @u.list: link in one of the eraseblock lists
38 *
39 * One object of this type is allocated for each physical eraseblock during
40 * scanning.
41 */
42struct ubi_scan_leb {
43 int ec;
44 int pnum;
45 int lnum;
46 unsigned int scrub:1;
47 unsigned int copy_flag:1;
48 unsigned long long sqnum;
49 union {
50 struct rb_node rb;
51 struct list_head list;
52 } u;
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
65 * size in case of 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 */
75struct 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 * those belonging to "preserve"-compatible internal volumes)
96 * @corr_peb_count: count of PEBs in the @corr list
97 * @empty_peb_count: count of PEBs which are presumably empty (contain only
98 * 0xFF bytes)
99 * @alien_peb_count: count of PEBs in the @alien list
100 * @bad_peb_count: count of bad physical eraseblocks
101 * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
102 * as bad yet, but which look like bad
103 * @vols_found: number of volumes found during scanning
104 * @highest_vol_id: highest volume ID
105 * @is_empty: flag indicating whether the MTD device is empty or not
106 * @min_ec: lowest erase counter value
107 * @max_ec: highest erase counter value
108 * @max_sqnum: highest sequence number value
109 * @mean_ec: mean erase counter value
110 * @ec_sum: a temporary variable used when calculating @mean_ec
111 * @ec_count: a temporary variable used when calculating @mean_ec
112 * @scan_leb_slab: slab cache for &struct ubi_scan_leb objects
113 *
114 * This data structure contains the result of scanning and may be used by other
115 * UBI sub-systems to build final UBI data structures, further error-recovery
116 * and so on.
117 */
118struct ubi_scan_info {
119 struct rb_root volumes;
120 struct list_head corr;
121 struct list_head free;
122 struct list_head erase;
123 struct list_head alien;
124 int corr_peb_count;
125 int empty_peb_count;
126 int alien_peb_count;
127 int bad_peb_count;
128 int maybe_bad_peb_count;
129 int vols_found;
130 int highest_vol_id;
131 int is_empty;
132 int min_ec;
133 int max_ec;
134 unsigned long long max_sqnum;
135 int mean_ec;
136 uint64_t ec_sum;
137 int ec_count;
138 struct kmem_cache *scan_leb_slab;
139};
140
141struct ubi_device;
142struct ubi_vid_hdr;
143
144/*
145 * ubi_scan_move_to_list - move a PEB from the volume tree to a list.
146 *
147 * @sv: volume scanning information
148 * @seb: scanning eraseblock information
149 * @list: the list to move to
150 */
151static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv,
152 struct ubi_scan_leb *seb,
153 struct list_head *list)
154{
155 rb_erase(&seb->u.rb, &sv->root);
156 list_add_tail(&seb->u.list, list);
157}
158
159int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
160 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
161 int bitflips);
162struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
163 int vol_id);
164struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv,
165 int lnum);
166void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv);
167struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
168 struct ubi_scan_info *si);
169int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
170 int pnum, int ec);
171struct ubi_scan_info *ubi_scan(struct ubi_device *ubi);
172void ubi_scan_destroy_si(struct ubi_scan_info *si);
173
174#endif /* !__UBI_SCAN_H__ */
diff --git a/drivers/mtd/ubi/ubi-media.h b/drivers/mtd/ubi/ubi-media.h
index 6fb8ec2174a5..468ffbc0eabd 100644
--- a/drivers/mtd/ubi/ubi-media.h
+++ b/drivers/mtd/ubi/ubi-media.h
@@ -149,10 +149,10 @@ enum {
149 * The @image_seq field is used to validate a UBI image that has been prepared 149 * The @image_seq field is used to validate a UBI image that has been prepared
150 * for a UBI device. The @image_seq value can be any value, but it must be the 150 * for a UBI device. The @image_seq value can be any value, but it must be the
151 * same on all eraseblocks. UBI will ensure that all new erase counter headers 151 * same on all eraseblocks. UBI will ensure that all new erase counter headers
152 * also contain this value, and will check the value when scanning at start-up. 152 * also contain this value, and will check the value when attaching the flash.
153 * One way to make use of @image_seq is to increase its value by one every time 153 * One way to make use of @image_seq is to increase its value by one every time
154 * an image is flashed over an existing image, then, if the flashing does not 154 * an image is flashed over an existing image, then, if the flashing does not
155 * complete, UBI will detect the error when scanning. 155 * complete, UBI will detect the error when attaching the media.
156 */ 156 */
157struct ubi_ec_hdr { 157struct ubi_ec_hdr {
158 __be32 magic; 158 __be32 magic;
@@ -298,8 +298,8 @@ struct ubi_vid_hdr {
298#define UBI_INT_VOL_COUNT 1 298#define UBI_INT_VOL_COUNT 1
299 299
300/* 300/*
301 * Starting ID of internal volumes. There is reserved room for 4096 internal 301 * Starting ID of internal volumes: 0x7fffefff.
302 * volumes. 302 * There is reserved room for 4096 internal volumes.
303 */ 303 */
304#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) 304#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
305 305
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index b162790790a9..a1a81c9ea8ce 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -43,7 +43,6 @@
43#include <asm/pgtable.h> 43#include <asm/pgtable.h>
44 44
45#include "ubi-media.h" 45#include "ubi-media.h"
46#include "scan.h"
47 46
48/* Maximum number of supported UBI devices */ 47/* Maximum number of supported UBI devices */
49#define UBI_MAX_DEVICES 32 48#define UBI_MAX_DEVICES 32
@@ -66,7 +65,10 @@
66/* Background thread name pattern */ 65/* Background thread name pattern */
67#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd" 66#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
68 67
69/* This marker in the EBA table means that the LEB is um-mapped */ 68/*
69 * This marker in the EBA table means that the LEB is um-mapped.
70 * NOTE! It has to have the same value as %UBI_ALL.
71 */
70#define UBI_LEB_UNMAPPED -1 72#define UBI_LEB_UNMAPPED -1
71 73
72/* 74/*
@@ -82,6 +84,9 @@
82 */ 84 */
83#define UBI_PROT_QUEUE_LEN 10 85#define UBI_PROT_QUEUE_LEN 10
84 86
87/* The volume ID/LEB number/erase counter is unknown */
88#define UBI_UNKNOWN -1
89
85/* 90/*
86 * Error codes returned by the I/O sub-system. 91 * Error codes returned by the I/O sub-system.
87 * 92 *
@@ -222,8 +227,6 @@ struct ubi_volume_desc;
222 * @upd_ebs: how many eraseblocks are expected to be updated 227 * @upd_ebs: how many eraseblocks are expected to be updated
223 * @ch_lnum: LEB number which is being changing by the atomic LEB change 228 * @ch_lnum: LEB number which is being changing by the atomic LEB change
224 * operation 229 * operation
225 * @ch_dtype: data persistency type which is being changing by the atomic LEB
226 * change operation
227 * @upd_bytes: how many bytes are expected to be received for volume update or 230 * @upd_bytes: how many bytes are expected to be received for volume update or
228 * atomic LEB change 231 * atomic LEB change
229 * @upd_received: how many bytes were already received for volume update or 232 * @upd_received: how many bytes were already received for volume update or
@@ -270,7 +273,6 @@ struct ubi_volume {
270 273
271 int upd_ebs; 274 int upd_ebs;
272 int ch_lnum; 275 int ch_lnum;
273 int ch_dtype;
274 long long upd_bytes; 276 long long upd_bytes;
275 long long upd_received; 277 long long upd_received;
276 void *upd_buf; 278 void *upd_buf;
@@ -477,6 +479,124 @@ struct ubi_device {
477 struct ubi_debug_info *dbg; 479 struct ubi_debug_info *dbg;
478}; 480};
479 481
482/**
483 * struct ubi_ainf_peb - attach information about a physical eraseblock.
484 * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
485 * @pnum: physical eraseblock number
486 * @vol_id: ID of the volume this LEB belongs to
487 * @lnum: logical eraseblock number
488 * @scrub: if this physical eraseblock needs scrubbing
489 * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
490 * @sqnum: sequence number
491 * @u: unions RB-tree or @list links
492 * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
493 * @u.list: link in one of the eraseblock lists
494 *
495 * One object of this type is allocated for each physical eraseblock when
496 * attaching an MTD device. Note, if this PEB does not belong to any LEB /
497 * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
498 */
499struct ubi_ainf_peb {
500 int ec;
501 int pnum;
502 int vol_id;
503 int lnum;
504 unsigned int scrub:1;
505 unsigned int copy_flag:1;
506 unsigned long long sqnum;
507 union {
508 struct rb_node rb;
509 struct list_head list;
510 } u;
511};
512
513/**
514 * struct ubi_ainf_volume - attaching information about a volume.
515 * @vol_id: volume ID
516 * @highest_lnum: highest logical eraseblock number in this volume
517 * @leb_count: number of logical eraseblocks in this volume
518 * @vol_type: volume type
519 * @used_ebs: number of used logical eraseblocks in this volume (only for
520 * static volumes)
521 * @last_data_size: amount of data in the last logical eraseblock of this
522 * volume (always equivalent to the usable logical eraseblock
523 * size in case of dynamic volumes)
524 * @data_pad: how many bytes at the end of logical eraseblocks of this volume
525 * are not used (due to volume alignment)
526 * @compat: compatibility flags of this volume
527 * @rb: link in the volume RB-tree
528 * @root: root of the RB-tree containing all the eraseblock belonging to this
529 * volume (&struct ubi_ainf_peb objects)
530 *
531 * One object of this type is allocated for each volume when attaching an MTD
532 * device.
533 */
534struct ubi_ainf_volume {
535 int vol_id;
536 int highest_lnum;
537 int leb_count;
538 int vol_type;
539 int used_ebs;
540 int last_data_size;
541 int data_pad;
542 int compat;
543 struct rb_node rb;
544 struct rb_root root;
545};
546
547/**
548 * struct ubi_attach_info - MTD device attaching information.
549 * @volumes: root of the volume RB-tree
550 * @corr: list of corrupted physical eraseblocks
551 * @free: list of free physical eraseblocks
552 * @erase: list of physical eraseblocks which have to be erased
553 * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
554 * those belonging to "preserve"-compatible internal volumes)
555 * @corr_peb_count: count of PEBs in the @corr list
556 * @empty_peb_count: count of PEBs which are presumably empty (contain only
557 * 0xFF bytes)
558 * @alien_peb_count: count of PEBs in the @alien list
559 * @bad_peb_count: count of bad physical eraseblocks
560 * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
561 * as bad yet, but which look like bad
562 * @vols_found: number of volumes found
563 * @highest_vol_id: highest volume ID
564 * @is_empty: flag indicating whether the MTD device is empty or not
565 * @min_ec: lowest erase counter value
566 * @max_ec: highest erase counter value
567 * @max_sqnum: highest sequence number value
568 * @mean_ec: mean erase counter value
569 * @ec_sum: a temporary variable used when calculating @mean_ec
570 * @ec_count: a temporary variable used when calculating @mean_ec
571 * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
572 *
573 * This data structure contains the result of attaching an MTD device and may
574 * be used by other UBI sub-systems to build final UBI data structures, further
575 * error-recovery and so on.
576 */
577struct ubi_attach_info {
578 struct rb_root volumes;
579 struct list_head corr;
580 struct list_head free;
581 struct list_head erase;
582 struct list_head alien;
583 int corr_peb_count;
584 int empty_peb_count;
585 int alien_peb_count;
586 int bad_peb_count;
587 int maybe_bad_peb_count;
588 int vols_found;
589 int highest_vol_id;
590 int is_empty;
591 int min_ec;
592 int max_ec;
593 unsigned long long max_sqnum;
594 int mean_ec;
595 uint64_t ec_sum;
596 int ec_count;
597 struct kmem_cache *aeb_slab_cache;
598};
599
480#include "debug.h" 600#include "debug.h"
481 601
482extern struct kmem_cache *ubi_wl_entry_slab; 602extern struct kmem_cache *ubi_wl_entry_slab;
@@ -487,12 +607,23 @@ extern struct class *ubi_class;
487extern struct mutex ubi_devices_mutex; 607extern struct mutex ubi_devices_mutex;
488extern struct blocking_notifier_head ubi_notifiers; 608extern struct blocking_notifier_head ubi_notifiers;
489 609
610/* scan.c */
611int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
612 int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
613struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
614 int vol_id);
615void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
616struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
617 struct ubi_attach_info *ai);
618int ubi_attach(struct ubi_device *ubi);
619void ubi_destroy_ai(struct ubi_attach_info *ai);
620
490/* vtbl.c */ 621/* vtbl.c */
491int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, 622int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
492 struct ubi_vtbl_record *vtbl_rec); 623 struct ubi_vtbl_record *vtbl_rec);
493int ubi_vtbl_rename_volumes(struct ubi_device *ubi, 624int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
494 struct list_head *rename_list); 625 struct list_head *rename_list);
495int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si); 626int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
496 627
497/* vmt.c */ 628/* vmt.c */
498int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req); 629int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
@@ -525,22 +656,22 @@ int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
525int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 656int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
526 void *buf, int offset, int len, int check); 657 void *buf, int offset, int len, int check);
527int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 658int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
528 const void *buf, int offset, int len, int dtype); 659 const void *buf, int offset, int len);
529int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, 660int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
530 int lnum, const void *buf, int len, int dtype, 661 int lnum, const void *buf, int len, int used_ebs);
531 int used_ebs);
532int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, 662int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
533 int lnum, const void *buf, int len, int dtype); 663 int lnum, const void *buf, int len);
534int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, 664int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
535 struct ubi_vid_hdr *vid_hdr); 665 struct ubi_vid_hdr *vid_hdr);
536int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si); 666int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
537 667
538/* wl.c */ 668/* wl.c */
539int ubi_wl_get_peb(struct ubi_device *ubi, int dtype); 669int ubi_wl_get_peb(struct ubi_device *ubi);
540int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture); 670int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
541int ubi_wl_flush(struct ubi_device *ubi); 671 int pnum, int torture);
672int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
542int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum); 673int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
543int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si); 674int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
544void ubi_wl_close(struct ubi_device *ubi); 675void ubi_wl_close(struct ubi_device *ubi);
545int ubi_thread(void *u); 676int ubi_thread(void *u);
546 677
@@ -573,6 +704,7 @@ int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
573int ubi_notify_all(struct ubi_device *ubi, int ntype, 704int ubi_notify_all(struct ubi_device *ubi, int ntype,
574 struct notifier_block *nb); 705 struct notifier_block *nb);
575int ubi_enumerate_volumes(struct notifier_block *nb); 706int ubi_enumerate_volumes(struct notifier_block *nb);
707void ubi_free_internal_volumes(struct ubi_device *ubi);
576 708
577/* kapi.c */ 709/* kapi.c */
578void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di); 710void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
@@ -593,6 +725,21 @@ void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
593 rb = rb_next(rb), \ 725 rb = rb_next(rb), \
594 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL)) 726 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
595 727
728/*
729 * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
730 *
731 * @av: volume attaching information
732 * @aeb: attaching eraseblock information
733 * @list: the list to move to
734 */
735static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
736 struct ubi_ainf_peb *aeb,
737 struct list_head *list)
738{
739 rb_erase(&aeb->u.rb, &av->root);
740 list_add_tail(&aeb->u.list, list);
741}
742
596/** 743/**
597 * ubi_zalloc_vid_hdr - allocate a volume identifier header object. 744 * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
598 * @ubi: UBI device description object 745 * @ubi: UBI device description object
@@ -667,7 +814,7 @@ static inline void ubi_ro_mode(struct ubi_device *ubi)
667 if (!ubi->ro_mode) { 814 if (!ubi->ro_mode) {
668 ubi->ro_mode = 1; 815 ubi->ro_mode = 1;
669 ubi_warn("switch to read-only mode"); 816 ubi_warn("switch to read-only mode");
670 ubi_dbg_dump_stack(); 817 dump_stack();
671 } 818 }
672} 819}
673 820
diff --git a/drivers/mtd/ubi/upd.c b/drivers/mtd/ubi/upd.c
index 425bf5a3edd4..9f2ebd8750e7 100644
--- a/drivers/mtd/ubi/upd.c
+++ b/drivers/mtd/ubi/upd.c
@@ -147,7 +147,7 @@ int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
147 } 147 }
148 148
149 if (bytes == 0) { 149 if (bytes == 0) {
150 err = ubi_wl_flush(ubi); 150 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
151 if (err) 151 if (err)
152 return err; 152 return err;
153 153
@@ -186,14 +186,12 @@ int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
186 dbg_gen("start changing LEB %d:%d, %u bytes", 186 dbg_gen("start changing LEB %d:%d, %u bytes",
187 vol->vol_id, req->lnum, req->bytes); 187 vol->vol_id, req->lnum, req->bytes);
188 if (req->bytes == 0) 188 if (req->bytes == 0)
189 return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0, 189 return ubi_eba_atomic_leb_change(ubi, vol, req->lnum, NULL, 0);
190 req->dtype);
191 190
192 vol->upd_bytes = req->bytes; 191 vol->upd_bytes = req->bytes;
193 vol->upd_received = 0; 192 vol->upd_received = 0;
194 vol->changing_leb = 1; 193 vol->changing_leb = 1;
195 vol->ch_lnum = req->lnum; 194 vol->ch_lnum = req->lnum;
196 vol->ch_dtype = req->dtype;
197 195
198 vol->upd_buf = vmalloc(req->bytes); 196 vol->upd_buf = vmalloc(req->bytes);
199 if (!vol->upd_buf) 197 if (!vol->upd_buf)
@@ -246,8 +244,7 @@ static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
246 return 0; 244 return 0;
247 } 245 }
248 246
249 err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len, 247 err = ubi_eba_write_leb(ubi, vol, lnum, buf, 0, len);
250 UBI_UNKNOWN);
251 } else { 248 } else {
252 /* 249 /*
253 * When writing static volume, and this is the last logical 250 * When writing static volume, and this is the last logical
@@ -259,8 +256,7 @@ static int write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
259 * contain zeros, not random trash. 256 * contain zeros, not random trash.
260 */ 257 */
261 memset(buf + len, 0, vol->usable_leb_size - len); 258 memset(buf + len, 0, vol->usable_leb_size - len);
262 err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, 259 err = ubi_eba_write_leb_st(ubi, vol, lnum, buf, len, used_ebs);
263 UBI_UNKNOWN, used_ebs);
264 } 260 }
265 261
266 return err; 262 return err;
@@ -365,7 +361,7 @@ int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
365 361
366 ubi_assert(vol->upd_received <= vol->upd_bytes); 362 ubi_assert(vol->upd_received <= vol->upd_bytes);
367 if (vol->upd_received == vol->upd_bytes) { 363 if (vol->upd_received == vol->upd_bytes) {
368 err = ubi_wl_flush(ubi); 364 err = ubi_wl_flush(ubi, UBI_ALL, UBI_ALL);
369 if (err) 365 if (err)
370 return err; 366 return err;
371 /* The update is finished, clear the update marker */ 367 /* The update is finished, clear the update marker */
@@ -421,7 +417,7 @@ int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
421 len - vol->upd_bytes); 417 len - vol->upd_bytes);
422 len = ubi_calc_data_len(ubi, vol->upd_buf, len); 418 len = ubi_calc_data_len(ubi, vol->upd_buf, len);
423 err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum, 419 err = ubi_eba_atomic_leb_change(ubi, vol, vol->ch_lnum,
424 vol->upd_buf, len, UBI_UNKNOWN); 420 vol->upd_buf, len);
425 if (err) 421 if (err)
426 return err; 422 return err;
427 } 423 }
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 863835f4aefe..0669cff8ac3c 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -29,11 +29,7 @@
29#include <linux/export.h> 29#include <linux/export.h>
30#include "ubi.h" 30#include "ubi.h"
31 31
32#ifdef CONFIG_MTD_UBI_DEBUG 32static int self_check_volumes(struct ubi_device *ubi);
33static int paranoid_check_volumes(struct ubi_device *ubi);
34#else
35#define paranoid_check_volumes(ubi) 0
36#endif
37 33
38static ssize_t vol_attribute_show(struct device *dev, 34static ssize_t vol_attribute_show(struct device *dev,
39 struct device_attribute *attr, char *buf); 35 struct device_attribute *attr, char *buf);
@@ -227,7 +223,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
227 } 223 }
228 224
229 if (vol_id == UBI_VOL_NUM_AUTO) { 225 if (vol_id == UBI_VOL_NUM_AUTO) {
230 dbg_err("out of volume IDs"); 226 ubi_err("out of volume IDs");
231 err = -ENFILE; 227 err = -ENFILE;
232 goto out_unlock; 228 goto out_unlock;
233 } 229 }
@@ -241,7 +237,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
241 /* Ensure that this volume does not exist */ 237 /* Ensure that this volume does not exist */
242 err = -EEXIST; 238 err = -EEXIST;
243 if (ubi->volumes[vol_id]) { 239 if (ubi->volumes[vol_id]) {
244 dbg_err("volume %d already exists", vol_id); 240 ubi_err("volume %d already exists", vol_id);
245 goto out_unlock; 241 goto out_unlock;
246 } 242 }
247 243
@@ -250,7 +246,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
250 if (ubi->volumes[i] && 246 if (ubi->volumes[i] &&
251 ubi->volumes[i]->name_len == req->name_len && 247 ubi->volumes[i]->name_len == req->name_len &&
252 !strcmp(ubi->volumes[i]->name, req->name)) { 248 !strcmp(ubi->volumes[i]->name, req->name)) {
253 dbg_err("volume \"%s\" exists (ID %d)", req->name, i); 249 ubi_err("volume \"%s\" exists (ID %d)", req->name, i);
254 goto out_unlock; 250 goto out_unlock;
255 } 251 }
256 252
@@ -261,9 +257,9 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
261 257
262 /* Reserve physical eraseblocks */ 258 /* Reserve physical eraseblocks */
263 if (vol->reserved_pebs > ubi->avail_pebs) { 259 if (vol->reserved_pebs > ubi->avail_pebs) {
264 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); 260 ubi_err("not enough PEBs, only %d available", ubi->avail_pebs);
265 if (ubi->corr_peb_count) 261 if (ubi->corr_peb_count)
266 dbg_err("%d PEBs are corrupted and not used", 262 ubi_err("%d PEBs are corrupted and not used",
267 ubi->corr_peb_count); 263 ubi->corr_peb_count);
268 err = -ENOSPC; 264 err = -ENOSPC;
269 goto out_unlock; 265 goto out_unlock;
@@ -284,7 +280,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
284 * Finish all pending erases because there may be some LEBs belonging 280 * Finish all pending erases because there may be some LEBs belonging
285 * to the same volume ID. 281 * to the same volume ID.
286 */ 282 */
287 err = ubi_wl_flush(ubi); 283 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
288 if (err) 284 if (err)
289 goto out_acc; 285 goto out_acc;
290 286
@@ -360,8 +356,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
360 spin_unlock(&ubi->volumes_lock); 356 spin_unlock(&ubi->volumes_lock);
361 357
362 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED); 358 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
363 if (paranoid_check_volumes(ubi)) 359 self_check_volumes(ubi);
364 dbg_err("check failed while creating volume %d", vol_id);
365 return err; 360 return err;
366 361
367out_sysfs: 362out_sysfs:
@@ -461,8 +456,8 @@ int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
461 spin_unlock(&ubi->volumes_lock); 456 spin_unlock(&ubi->volumes_lock);
462 457
463 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED); 458 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
464 if (!no_vtbl && paranoid_check_volumes(ubi)) 459 if (!no_vtbl)
465 dbg_err("check failed while removing volume %d", vol_id); 460 self_check_volumes(ubi);
466 461
467 return err; 462 return err;
468 463
@@ -500,7 +495,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
500 495
501 if (vol->vol_type == UBI_STATIC_VOLUME && 496 if (vol->vol_type == UBI_STATIC_VOLUME &&
502 reserved_pebs < vol->used_ebs) { 497 reserved_pebs < vol->used_ebs) {
503 dbg_err("too small size %d, %d LEBs contain data", 498 ubi_err("too small size %d, %d LEBs contain data",
504 reserved_pebs, vol->used_ebs); 499 reserved_pebs, vol->used_ebs);
505 return -EINVAL; 500 return -EINVAL;
506 } 501 }
@@ -529,10 +524,10 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
529 if (pebs > 0) { 524 if (pebs > 0) {
530 spin_lock(&ubi->volumes_lock); 525 spin_lock(&ubi->volumes_lock);
531 if (pebs > ubi->avail_pebs) { 526 if (pebs > ubi->avail_pebs) {
532 dbg_err("not enough PEBs: requested %d, available %d", 527 ubi_err("not enough PEBs: requested %d, available %d",
533 pebs, ubi->avail_pebs); 528 pebs, ubi->avail_pebs);
534 if (ubi->corr_peb_count) 529 if (ubi->corr_peb_count)
535 dbg_err("%d PEBs are corrupted and not used", 530 ubi_err("%d PEBs are corrupted and not used",
536 ubi->corr_peb_count); 531 ubi->corr_peb_count);
537 spin_unlock(&ubi->volumes_lock); 532 spin_unlock(&ubi->volumes_lock);
538 err = -ENOSPC; 533 err = -ENOSPC;
@@ -588,8 +583,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
588 } 583 }
589 584
590 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED); 585 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
591 if (paranoid_check_volumes(ubi)) 586 self_check_volumes(ubi);
592 dbg_err("check failed while re-sizing volume %d", vol_id);
593 return err; 587 return err;
594 588
595out_acc: 589out_acc:
@@ -638,8 +632,8 @@ int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
638 } 632 }
639 } 633 }
640 634
641 if (!err && paranoid_check_volumes(ubi)) 635 if (!err)
642 ; 636 self_check_volumes(ubi);
643 return err; 637 return err;
644} 638}
645 639
@@ -686,8 +680,7 @@ int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
686 return err; 680 return err;
687 } 681 }
688 682
689 if (paranoid_check_volumes(ubi)) 683 self_check_volumes(ubi);
690 dbg_err("check failed while adding volume %d", vol_id);
691 return err; 684 return err;
692 685
693out_cdev: 686out_cdev:
@@ -712,16 +705,14 @@ void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
712 volume_sysfs_close(vol); 705 volume_sysfs_close(vol);
713} 706}
714 707
715#ifdef CONFIG_MTD_UBI_DEBUG
716
717/** 708/**
718 * paranoid_check_volume - check volume information. 709 * self_check_volume - check volume information.
719 * @ubi: UBI device description object 710 * @ubi: UBI device description object
720 * @vol_id: volume ID 711 * @vol_id: volume ID
721 * 712 *
722 * Returns zero if volume is all right and a a negative error code if not. 713 * Returns zero if volume is all right and a a negative error code if not.
723 */ 714 */
724static int paranoid_check_volume(struct ubi_device *ubi, int vol_id) 715static int self_check_volume(struct ubi_device *ubi, int vol_id)
725{ 716{
726 int idx = vol_id2idx(ubi, vol_id); 717 int idx = vol_id2idx(ubi, vol_id);
727 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker; 718 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
@@ -771,7 +762,7 @@ static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
771 } 762 }
772 763
773 if (vol->upd_marker && vol->corrupted) { 764 if (vol->upd_marker && vol->corrupted) {
774 dbg_err("update marker and corrupted simultaneously"); 765 ubi_err("update marker and corrupted simultaneously");
775 goto fail; 766 goto fail;
776 } 767 }
777 768
@@ -853,22 +844,22 @@ static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
853 return 0; 844 return 0;
854 845
855fail: 846fail:
856 ubi_err("paranoid check failed for volume %d", vol_id); 847 ubi_err("self-check failed for volume %d", vol_id);
857 if (vol) 848 if (vol)
858 ubi_dbg_dump_vol_info(vol); 849 ubi_dump_vol_info(vol);
859 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id); 850 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
860 dump_stack(); 851 dump_stack();
861 spin_unlock(&ubi->volumes_lock); 852 spin_unlock(&ubi->volumes_lock);
862 return -EINVAL; 853 return -EINVAL;
863} 854}
864 855
865/** 856/**
866 * paranoid_check_volumes - check information about all volumes. 857 * self_check_volumes - check information about all volumes.
867 * @ubi: UBI device description object 858 * @ubi: UBI device description object
868 * 859 *
869 * Returns zero if volumes are all right and a a negative error code if not. 860 * Returns zero if volumes are all right and a a negative error code if not.
870 */ 861 */
871static int paranoid_check_volumes(struct ubi_device *ubi) 862static int self_check_volumes(struct ubi_device *ubi)
872{ 863{
873 int i, err = 0; 864 int i, err = 0;
874 865
@@ -876,11 +867,10 @@ static int paranoid_check_volumes(struct ubi_device *ubi)
876 return 0; 867 return 0;
877 868
878 for (i = 0; i < ubi->vtbl_slots; i++) { 869 for (i = 0; i < ubi->vtbl_slots; i++) {
879 err = paranoid_check_volume(ubi, i); 870 err = self_check_volume(ubi, i);
880 if (err) 871 if (err)
881 break; 872 break;
882 } 873 }
883 874
884 return err; 875 return err;
885} 876}
886#endif
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
index 17cec0c01544..437bc193e170 100644
--- a/drivers/mtd/ubi/vtbl.c
+++ b/drivers/mtd/ubi/vtbl.c
@@ -37,16 +37,15 @@
37 * LEB 1. This scheme guarantees recoverability from unclean reboots. 37 * LEB 1. This scheme guarantees recoverability from unclean reboots.
38 * 38 *
39 * In this UBI implementation the on-flash volume table does not contain any 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 40 * information about how much data static volumes contain.
41 * be found from the scanning data.
42 * 41 *
43 * But it would still be beneficial to store this information in the volume 42 * 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 43 * 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 44 * eraseblocks became bad for some reasons. Suppose we are attaching the
46 * corresponding MTD device, the scanning has found no logical eraseblocks 45 * corresponding MTD device, for some reason we find no logical eraseblocks
47 * corresponding to the volume X. According to the volume table volume X does 46 * 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 47 * 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. 48 * eraseblocks went bad. So we cannot alarm the user properly.
50 * 49 *
51 * The volume table also stores so-called "update marker", which is used for 50 * 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 51 * volume updates. Before updating the volume, the update marker is set, and
@@ -62,11 +61,7 @@
62#include <asm/div64.h> 61#include <asm/div64.h>
63#include "ubi.h" 62#include "ubi.h"
64 63
65#ifdef CONFIG_MTD_UBI_DEBUG 64static void self_vtbl_check(const struct ubi_device *ubi);
66static void paranoid_vtbl_check(const struct ubi_device *ubi);
67#else
68#define paranoid_vtbl_check(ubi)
69#endif
70 65
71/* Empty volume table record */ 66/* Empty volume table record */
72static struct ubi_vtbl_record empty_vtbl_record; 67static struct ubi_vtbl_record empty_vtbl_record;
@@ -106,12 +101,12 @@ int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
106 return err; 101 return err;
107 102
108 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0, 103 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
109 ubi->vtbl_size, UBI_LONGTERM); 104 ubi->vtbl_size);
110 if (err) 105 if (err)
111 return err; 106 return err;
112 } 107 }
113 108
114 paranoid_vtbl_check(ubi); 109 self_vtbl_check(ubi);
115 return 0; 110 return 0;
116} 111}
117 112
@@ -158,7 +153,7 @@ int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
158 return err; 153 return err;
159 154
160 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0, 155 err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0,
161 ubi->vtbl_size, UBI_LONGTERM); 156 ubi->vtbl_size);
162 if (err) 157 if (err)
163 return err; 158 return err;
164 } 159 }
@@ -197,7 +192,7 @@ static int vtbl_check(const struct ubi_device *ubi,
197 if (be32_to_cpu(vtbl[i].crc) != crc) { 192 if (be32_to_cpu(vtbl[i].crc) != crc) {
198 ubi_err("bad CRC at record %u: %#08x, not %#08x", 193 ubi_err("bad CRC at record %u: %#08x, not %#08x",
199 i, crc, be32_to_cpu(vtbl[i].crc)); 194 i, crc, be32_to_cpu(vtbl[i].crc));
200 ubi_dbg_dump_vtbl_record(&vtbl[i], i); 195 ubi_dump_vtbl_record(&vtbl[i], i);
201 return 1; 196 return 1;
202 } 197 }
203 198
@@ -229,7 +224,7 @@ static int vtbl_check(const struct ubi_device *ubi,
229 224
230 n = ubi->leb_size % alignment; 225 n = ubi->leb_size % alignment;
231 if (data_pad != n) { 226 if (data_pad != n) {
232 dbg_err("bad data_pad, has to be %d", n); 227 ubi_err("bad data_pad, has to be %d", n);
233 err = 6; 228 err = 6;
234 goto bad; 229 goto bad;
235 } 230 }
@@ -245,7 +240,7 @@ static int vtbl_check(const struct ubi_device *ubi,
245 } 240 }
246 241
247 if (reserved_pebs > ubi->good_peb_count) { 242 if (reserved_pebs > ubi->good_peb_count) {
248 dbg_err("too large reserved_pebs %d, good PEBs %d", 243 ubi_err("too large reserved_pebs %d, good PEBs %d",
249 reserved_pebs, ubi->good_peb_count); 244 reserved_pebs, ubi->good_peb_count);
250 err = 9; 245 err = 9;
251 goto bad; 246 goto bad;
@@ -277,8 +272,8 @@ static int vtbl_check(const struct ubi_device *ubi,
277 !strncmp(vtbl[i].name, vtbl[n].name, len1)) { 272 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
278 ubi_err("volumes %d and %d have the same name" 273 ubi_err("volumes %d and %d have the same name"
279 " \"%s\"", i, n, vtbl[i].name); 274 " \"%s\"", i, n, vtbl[i].name);
280 ubi_dbg_dump_vtbl_record(&vtbl[i], i); 275 ubi_dump_vtbl_record(&vtbl[i], i);
281 ubi_dbg_dump_vtbl_record(&vtbl[n], n); 276 ubi_dump_vtbl_record(&vtbl[n], n);
282 return -EINVAL; 277 return -EINVAL;
283 } 278 }
284 } 279 }
@@ -288,26 +283,26 @@ static int vtbl_check(const struct ubi_device *ubi,
288 283
289bad: 284bad:
290 ubi_err("volume table check failed: record %d, error %d", i, err); 285 ubi_err("volume table check failed: record %d, error %d", i, err);
291 ubi_dbg_dump_vtbl_record(&vtbl[i], i); 286 ubi_dump_vtbl_record(&vtbl[i], i);
292 return -EINVAL; 287 return -EINVAL;
293} 288}
294 289
295/** 290/**
296 * create_vtbl - create a copy of volume table. 291 * create_vtbl - create a copy of volume table.
297 * @ubi: UBI device description object 292 * @ubi: UBI device description object
298 * @si: scanning information 293 * @ai: attaching information
299 * @copy: number of the volume table copy 294 * @copy: number of the volume table copy
300 * @vtbl: contents of the volume table 295 * @vtbl: contents of the volume table
301 * 296 *
302 * This function returns zero in case of success and a negative error code in 297 * This function returns zero in case of success and a negative error code in
303 * case of failure. 298 * case of failure.
304 */ 299 */
305static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si, 300static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
306 int copy, void *vtbl) 301 int copy, void *vtbl)
307{ 302{
308 int err, tries = 0; 303 int err, tries = 0;
309 struct ubi_vid_hdr *vid_hdr; 304 struct ubi_vid_hdr *vid_hdr;
310 struct ubi_scan_leb *new_seb; 305 struct ubi_ainf_peb *new_aeb;
311 306
312 ubi_msg("create volume table (copy #%d)", copy + 1); 307 ubi_msg("create volume table (copy #%d)", copy + 1);
313 308
@@ -316,9 +311,9 @@ static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si,
316 return -ENOMEM; 311 return -ENOMEM;
317 312
318retry: 313retry:
319 new_seb = ubi_scan_get_free_peb(ubi, si); 314 new_aeb = ubi_early_get_peb(ubi, ai);
320 if (IS_ERR(new_seb)) { 315 if (IS_ERR(new_aeb)) {
321 err = PTR_ERR(new_seb); 316 err = PTR_ERR(new_aeb);
322 goto out_free; 317 goto out_free;
323 } 318 }
324 319
@@ -328,25 +323,24 @@ retry:
328 vid_hdr->data_size = vid_hdr->used_ebs = 323 vid_hdr->data_size = vid_hdr->used_ebs =
329 vid_hdr->data_pad = cpu_to_be32(0); 324 vid_hdr->data_pad = cpu_to_be32(0);
330 vid_hdr->lnum = cpu_to_be32(copy); 325 vid_hdr->lnum = cpu_to_be32(copy);
331 vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum); 326 vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
332 327
333 /* The EC header is already there, write the VID header */ 328 /* The EC header is already there, write the VID header */
334 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); 329 err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr);
335 if (err) 330 if (err)
336 goto write_error; 331 goto write_error;
337 332
338 /* Write the layout volume contents */ 333 /* Write the layout volume contents */
339 err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size); 334 err = ubi_io_write_data(ubi, vtbl, new_aeb->pnum, 0, ubi->vtbl_size);
340 if (err) 335 if (err)
341 goto write_error; 336 goto write_error;
342 337
343 /* 338 /*
344 * And add it to the scanning information. Don't delete the old version 339 * And add it to the attaching information. Don't delete the old version
345 * of this LEB as it will be deleted and freed in 'ubi_scan_add_used()'. 340 * of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
346 */ 341 */
347 err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec, 342 err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
348 vid_hdr, 0); 343 kfree(new_aeb);
349 kfree(new_seb);
350 ubi_free_vid_hdr(ubi, vid_hdr); 344 ubi_free_vid_hdr(ubi, vid_hdr);
351 return err; 345 return err;
352 346
@@ -356,10 +350,10 @@ write_error:
356 * Probably this physical eraseblock went bad, try to pick 350 * Probably this physical eraseblock went bad, try to pick
357 * another one. 351 * another one.
358 */ 352 */
359 list_add(&new_seb->u.list, &si->erase); 353 list_add(&new_aeb->u.list, &ai->erase);
360 goto retry; 354 goto retry;
361 } 355 }
362 kfree(new_seb); 356 kfree(new_aeb);
363out_free: 357out_free:
364 ubi_free_vid_hdr(ubi, vid_hdr); 358 ubi_free_vid_hdr(ubi, vid_hdr);
365 return err; 359 return err;
@@ -369,20 +363,20 @@ out_free:
369/** 363/**
370 * process_lvol - process the layout volume. 364 * process_lvol - process the layout volume.
371 * @ubi: UBI device description object 365 * @ubi: UBI device description object
372 * @si: scanning information 366 * @ai: attaching information
373 * @sv: layout volume scanning information 367 * @av: layout volume attaching information
374 * 368 *
375 * This function is responsible for reading the layout volume, ensuring it is 369 * This function is responsible for reading the layout volume, ensuring it is
376 * not corrupted, and recovering from corruptions if needed. Returns volume 370 * not corrupted, and recovering from corruptions if needed. Returns volume
377 * table in case of success and a negative error code in case of failure. 371 * table in case of success and a negative error code in case of failure.
378 */ 372 */
379static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi, 373static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
380 struct ubi_scan_info *si, 374 struct ubi_attach_info *ai,
381 struct ubi_scan_volume *sv) 375 struct ubi_ainf_volume *av)
382{ 376{
383 int err; 377 int err;
384 struct rb_node *rb; 378 struct rb_node *rb;
385 struct ubi_scan_leb *seb; 379 struct ubi_ainf_peb *aeb;
386 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL }; 380 struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL };
387 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1}; 381 int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1};
388 382
@@ -414,14 +408,14 @@ static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
414 dbg_gen("check layout volume"); 408 dbg_gen("check layout volume");
415 409
416 /* Read both LEB 0 and LEB 1 into memory */ 410 /* Read both LEB 0 and LEB 1 into memory */
417 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { 411 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
418 leb[seb->lnum] = vzalloc(ubi->vtbl_size); 412 leb[aeb->lnum] = vzalloc(ubi->vtbl_size);
419 if (!leb[seb->lnum]) { 413 if (!leb[aeb->lnum]) {
420 err = -ENOMEM; 414 err = -ENOMEM;
421 goto out_free; 415 goto out_free;
422 } 416 }
423 417
424 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, 418 err = ubi_io_read_data(ubi, leb[aeb->lnum], aeb->pnum, 0,
425 ubi->vtbl_size); 419 ubi->vtbl_size);
426 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) 420 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err))
427 /* 421 /*
@@ -429,12 +423,12 @@ static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
429 * uncorrectable ECC error, but we have our own CRC and 423 * uncorrectable ECC error, but we have our own CRC and
430 * the data will be checked later. If the data is OK, 424 * the data will be checked later. If the data is OK,
431 * the PEB will be scrubbed (because we set 425 * the PEB will be scrubbed (because we set
432 * seb->scrub). If the data is not OK, the contents of 426 * aeb->scrub). If the data is not OK, the contents of
433 * the PEB will be recovered from the second copy, and 427 * the PEB will be recovered from the second copy, and
434 * seb->scrub will be cleared in 428 * aeb->scrub will be cleared in
435 * 'ubi_scan_add_used()'. 429 * 'ubi_add_to_av()'.
436 */ 430 */
437 seb->scrub = 1; 431 aeb->scrub = 1;
438 else if (err) 432 else if (err)
439 goto out_free; 433 goto out_free;
440 } 434 }
@@ -453,7 +447,7 @@ static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
453 ubi->vtbl_size); 447 ubi->vtbl_size);
454 if (leb_corrupted[1]) { 448 if (leb_corrupted[1]) {
455 ubi_warn("volume table copy #2 is corrupted"); 449 ubi_warn("volume table copy #2 is corrupted");
456 err = create_vtbl(ubi, si, 1, leb[0]); 450 err = create_vtbl(ubi, ai, 1, leb[0]);
457 if (err) 451 if (err)
458 goto out_free; 452 goto out_free;
459 ubi_msg("volume table was restored"); 453 ubi_msg("volume table was restored");
@@ -476,7 +470,7 @@ static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
476 } 470 }
477 471
478 ubi_warn("volume table copy #1 is corrupted"); 472 ubi_warn("volume table copy #1 is corrupted");
479 err = create_vtbl(ubi, si, 0, leb[1]); 473 err = create_vtbl(ubi, ai, 0, leb[1]);
480 if (err) 474 if (err)
481 goto out_free; 475 goto out_free;
482 ubi_msg("volume table was restored"); 476 ubi_msg("volume table was restored");
@@ -494,13 +488,13 @@ out_free:
494/** 488/**
495 * create_empty_lvol - create empty layout volume. 489 * create_empty_lvol - create empty layout volume.
496 * @ubi: UBI device description object 490 * @ubi: UBI device description object
497 * @si: scanning information 491 * @ai: attaching information
498 * 492 *
499 * This function returns volume table contents in case of success and a 493 * This function returns volume table contents in case of success and a
500 * negative error code in case of failure. 494 * negative error code in case of failure.
501 */ 495 */
502static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi, 496static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
503 struct ubi_scan_info *si) 497 struct ubi_attach_info *ai)
504{ 498{
505 int i; 499 int i;
506 struct ubi_vtbl_record *vtbl; 500 struct ubi_vtbl_record *vtbl;
@@ -515,7 +509,7 @@ static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
515 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { 509 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
516 int err; 510 int err;
517 511
518 err = create_vtbl(ubi, si, i, vtbl); 512 err = create_vtbl(ubi, ai, i, vtbl);
519 if (err) { 513 if (err) {
520 vfree(vtbl); 514 vfree(vtbl);
521 return ERR_PTR(err); 515 return ERR_PTR(err);
@@ -528,18 +522,19 @@ static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
528/** 522/**
529 * init_volumes - initialize volume information for existing volumes. 523 * init_volumes - initialize volume information for existing volumes.
530 * @ubi: UBI device description object 524 * @ubi: UBI device description object
531 * @si: scanning information 525 * @ai: scanning information
532 * @vtbl: volume table 526 * @vtbl: volume table
533 * 527 *
534 * This function allocates volume description objects for existing volumes. 528 * This function allocates volume description objects for existing volumes.
535 * Returns zero in case of success and a negative error code in case of 529 * Returns zero in case of success and a negative error code in case of
536 * failure. 530 * failure.
537 */ 531 */
538static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si, 532static int init_volumes(struct ubi_device *ubi,
533 const struct ubi_attach_info *ai,
539 const struct ubi_vtbl_record *vtbl) 534 const struct ubi_vtbl_record *vtbl)
540{ 535{
541 int i, reserved_pebs = 0; 536 int i, reserved_pebs = 0;
542 struct ubi_scan_volume *sv; 537 struct ubi_ainf_volume *av;
543 struct ubi_volume *vol; 538 struct ubi_volume *vol;
544 539
545 for (i = 0; i < ubi->vtbl_slots; i++) { 540 for (i = 0; i < ubi->vtbl_slots; i++) {
@@ -595,8 +590,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
595 } 590 }
596 591
597 /* Static volumes only */ 592 /* Static volumes only */
598 sv = ubi_scan_find_sv(si, i); 593 av = ubi_find_av(ai, i);
599 if (!sv) { 594 if (!av) {
600 /* 595 /*
601 * No eraseblocks belonging to this volume found. We 596 * No eraseblocks belonging to this volume found. We
602 * don't actually know whether this static volume is 597 * don't actually know whether this static volume is
@@ -608,22 +603,22 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
608 continue; 603 continue;
609 } 604 }
610 605
611 if (sv->leb_count != sv->used_ebs) { 606 if (av->leb_count != av->used_ebs) {
612 /* 607 /*
613 * We found a static volume which misses several 608 * We found a static volume which misses several
614 * eraseblocks. Treat it as corrupted. 609 * eraseblocks. Treat it as corrupted.
615 */ 610 */
616 ubi_warn("static volume %d misses %d LEBs - corrupted", 611 ubi_warn("static volume %d misses %d LEBs - corrupted",
617 sv->vol_id, sv->used_ebs - sv->leb_count); 612 av->vol_id, av->used_ebs - av->leb_count);
618 vol->corrupted = 1; 613 vol->corrupted = 1;
619 continue; 614 continue;
620 } 615 }
621 616
622 vol->used_ebs = sv->used_ebs; 617 vol->used_ebs = av->used_ebs;
623 vol->used_bytes = 618 vol->used_bytes =
624 (long long)(vol->used_ebs - 1) * vol->usable_leb_size; 619 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
625 vol->used_bytes += sv->last_data_size; 620 vol->used_bytes += av->last_data_size;
626 vol->last_eb_bytes = sv->last_data_size; 621 vol->last_eb_bytes = av->last_data_size;
627 } 622 }
628 623
629 /* And add the layout volume */ 624 /* And add the layout volume */
@@ -664,105 +659,104 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
664} 659}
665 660
666/** 661/**
667 * check_sv - check volume scanning information. 662 * check_av - check volume attaching information.
668 * @vol: UBI volume description object 663 * @vol: UBI volume description object
669 * @sv: volume scanning information 664 * @av: volume attaching information
670 * 665 *
671 * This function returns zero if the volume scanning information is consistent 666 * This function returns zero if the volume attaching information is consistent
672 * to the data read from the volume tabla, and %-EINVAL if not. 667 * to the data read from the volume tabla, and %-EINVAL if not.
673 */ 668 */
674static int check_sv(const struct ubi_volume *vol, 669static int check_av(const struct ubi_volume *vol,
675 const struct ubi_scan_volume *sv) 670 const struct ubi_ainf_volume *av)
676{ 671{
677 int err; 672 int err;
678 673
679 if (sv->highest_lnum >= vol->reserved_pebs) { 674 if (av->highest_lnum >= vol->reserved_pebs) {
680 err = 1; 675 err = 1;
681 goto bad; 676 goto bad;
682 } 677 }
683 if (sv->leb_count > vol->reserved_pebs) { 678 if (av->leb_count > vol->reserved_pebs) {
684 err = 2; 679 err = 2;
685 goto bad; 680 goto bad;
686 } 681 }
687 if (sv->vol_type != vol->vol_type) { 682 if (av->vol_type != vol->vol_type) {
688 err = 3; 683 err = 3;
689 goto bad; 684 goto bad;
690 } 685 }
691 if (sv->used_ebs > vol->reserved_pebs) { 686 if (av->used_ebs > vol->reserved_pebs) {
692 err = 4; 687 err = 4;
693 goto bad; 688 goto bad;
694 } 689 }
695 if (sv->data_pad != vol->data_pad) { 690 if (av->data_pad != vol->data_pad) {
696 err = 5; 691 err = 5;
697 goto bad; 692 goto bad;
698 } 693 }
699 return 0; 694 return 0;
700 695
701bad: 696bad:
702 ubi_err("bad scanning information, error %d", err); 697 ubi_err("bad attaching information, error %d", err);
703 ubi_dbg_dump_sv(sv); 698 ubi_dump_av(av);
704 ubi_dbg_dump_vol_info(vol); 699 ubi_dump_vol_info(vol);
705 return -EINVAL; 700 return -EINVAL;
706} 701}
707 702
708/** 703/**
709 * check_scanning_info - check that scanning information. 704 * check_attaching_info - check that attaching information.
710 * @ubi: UBI device description object 705 * @ubi: UBI device description object
711 * @si: scanning information 706 * @ai: attaching information
712 * 707 *
713 * Even though we protect on-flash data by CRC checksums, we still don't trust 708 * Even though we protect on-flash data by CRC checksums, we still don't trust
714 * the media. This function ensures that scanning information is consistent to 709 * the media. This function ensures that attaching information is consistent to
715 * the information read from the volume table. Returns zero if the scanning 710 * the information read from the volume table. Returns zero if the attaching
716 * information is OK and %-EINVAL if it is not. 711 * information is OK and %-EINVAL if it is not.
717 */ 712 */
718static int check_scanning_info(const struct ubi_device *ubi, 713static int check_attaching_info(const struct ubi_device *ubi,
719 struct ubi_scan_info *si) 714 struct ubi_attach_info *ai)
720{ 715{
721 int err, i; 716 int err, i;
722 struct ubi_scan_volume *sv; 717 struct ubi_ainf_volume *av;
723 struct ubi_volume *vol; 718 struct ubi_volume *vol;
724 719
725 if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { 720 if (ai->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) {
726 ubi_err("scanning found %d volumes, maximum is %d + %d", 721 ubi_err("found %d volumes while attaching, maximum is %d + %d",
727 si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); 722 ai->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots);
728 return -EINVAL; 723 return -EINVAL;
729 } 724 }
730 725
731 if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT && 726 if (ai->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT &&
732 si->highest_vol_id < UBI_INTERNAL_VOL_START) { 727 ai->highest_vol_id < UBI_INTERNAL_VOL_START) {
733 ubi_err("too large volume ID %d found by scanning", 728 ubi_err("too large volume ID %d found", ai->highest_vol_id);
734 si->highest_vol_id);
735 return -EINVAL; 729 return -EINVAL;
736 } 730 }
737 731
738 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { 732 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
739 cond_resched(); 733 cond_resched();
740 734
741 sv = ubi_scan_find_sv(si, i); 735 av = ubi_find_av(ai, i);
742 vol = ubi->volumes[i]; 736 vol = ubi->volumes[i];
743 if (!vol) { 737 if (!vol) {
744 if (sv) 738 if (av)
745 ubi_scan_rm_volume(si, sv); 739 ubi_remove_av(ai, av);
746 continue; 740 continue;
747 } 741 }
748 742
749 if (vol->reserved_pebs == 0) { 743 if (vol->reserved_pebs == 0) {
750 ubi_assert(i < ubi->vtbl_slots); 744 ubi_assert(i < ubi->vtbl_slots);
751 745
752 if (!sv) 746 if (!av)
753 continue; 747 continue;
754 748
755 /* 749 /*
756 * During scanning we found a volume which does not 750 * During attaching we found a volume which does not
757 * exist according to the information in the volume 751 * exist according to the information in the volume
758 * table. This must have happened due to an unclean 752 * table. This must have happened due to an unclean
759 * reboot while the volume was being removed. Discard 753 * reboot while the volume was being removed. Discard
760 * these eraseblocks. 754 * these eraseblocks.
761 */ 755 */
762 ubi_msg("finish volume %d removal", sv->vol_id); 756 ubi_msg("finish volume %d removal", av->vol_id);
763 ubi_scan_rm_volume(si, sv); 757 ubi_remove_av(ai, av);
764 } else if (sv) { 758 } else if (av) {
765 err = check_sv(vol, sv); 759 err = check_av(vol, av);
766 if (err) 760 if (err)
767 return err; 761 return err;
768 } 762 }
@@ -774,16 +768,16 @@ static int check_scanning_info(const struct ubi_device *ubi,
774/** 768/**
775 * ubi_read_volume_table - read the volume table. 769 * ubi_read_volume_table - read the volume table.
776 * @ubi: UBI device description object 770 * @ubi: UBI device description object
777 * @si: scanning information 771 * @ai: attaching information
778 * 772 *
779 * This function reads volume table, checks it, recover from errors if needed, 773 * This function reads volume table, checks it, recover from errors if needed,
780 * or creates it if needed. Returns zero in case of success and a negative 774 * or creates it if needed. Returns zero in case of success and a negative
781 * error code in case of failure. 775 * error code in case of failure.
782 */ 776 */
783int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si) 777int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai)
784{ 778{
785 int i, err; 779 int i, err;
786 struct ubi_scan_volume *sv; 780 struct ubi_ainf_volume *av;
787 781
788 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b); 782 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
789 783
@@ -798,8 +792,8 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
798 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; 792 ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE;
799 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); 793 ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size);
800 794
801 sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID); 795 av = ubi_find_av(ai, UBI_LAYOUT_VOLUME_ID);
802 if (!sv) { 796 if (!av) {
803 /* 797 /*
804 * No logical eraseblocks belonging to the layout volume were 798 * No logical eraseblocks belonging to the layout volume were
805 * found. This could mean that the flash is just empty. In 799 * found. This could mean that the flash is just empty. In
@@ -808,8 +802,8 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
808 * But if flash is not empty this must be a corruption or the 802 * But if flash is not empty this must be a corruption or the
809 * MTD device just contains garbage. 803 * MTD device just contains garbage.
810 */ 804 */
811 if (si->is_empty) { 805 if (ai->is_empty) {
812 ubi->vtbl = create_empty_lvol(ubi, si); 806 ubi->vtbl = create_empty_lvol(ubi, ai);
813 if (IS_ERR(ubi->vtbl)) 807 if (IS_ERR(ubi->vtbl))
814 return PTR_ERR(ubi->vtbl); 808 return PTR_ERR(ubi->vtbl);
815 } else { 809 } else {
@@ -817,14 +811,14 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
817 return -EINVAL; 811 return -EINVAL;
818 } 812 }
819 } else { 813 } else {
820 if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) { 814 if (av->leb_count > UBI_LAYOUT_VOLUME_EBS) {
821 /* This must not happen with proper UBI images */ 815 /* This must not happen with proper UBI images */
822 dbg_err("too many LEBs (%d) in layout volume", 816 ubi_err("too many LEBs (%d) in layout volume",
823 sv->leb_count); 817 av->leb_count);
824 return -EINVAL; 818 return -EINVAL;
825 } 819 }
826 820
827 ubi->vtbl = process_lvol(ubi, si, sv); 821 ubi->vtbl = process_lvol(ubi, ai, av);
828 if (IS_ERR(ubi->vtbl)) 822 if (IS_ERR(ubi->vtbl))
829 return PTR_ERR(ubi->vtbl); 823 return PTR_ERR(ubi->vtbl);
830 } 824 }
@@ -835,15 +829,15 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
835 * The layout volume is OK, initialize the corresponding in-RAM data 829 * The layout volume is OK, initialize the corresponding in-RAM data
836 * structures. 830 * structures.
837 */ 831 */
838 err = init_volumes(ubi, si, ubi->vtbl); 832 err = init_volumes(ubi, ai, ubi->vtbl);
839 if (err) 833 if (err)
840 goto out_free; 834 goto out_free;
841 835
842 /* 836 /*
843 * Make sure that the scanning information is consistent to the 837 * Make sure that the attaching information is consistent to the
844 * information stored in the volume table. 838 * information stored in the volume table.
845 */ 839 */
846 err = check_scanning_info(ubi, si); 840 err = check_attaching_info(ubi, ai);
847 if (err) 841 if (err)
848 goto out_free; 842 goto out_free;
849 843
@@ -858,21 +852,17 @@ out_free:
858 return err; 852 return err;
859} 853}
860 854
861#ifdef CONFIG_MTD_UBI_DEBUG
862
863/** 855/**
864 * paranoid_vtbl_check - check volume table. 856 * self_vtbl_check - check volume table.
865 * @ubi: UBI device description object 857 * @ubi: UBI device description object
866 */ 858 */
867static void paranoid_vtbl_check(const struct ubi_device *ubi) 859static void self_vtbl_check(const struct ubi_device *ubi)
868{ 860{
869 if (!ubi->dbg->chk_gen) 861 if (!ubi->dbg->chk_gen)
870 return; 862 return;
871 863
872 if (vtbl_check(ubi, ubi->vtbl)) { 864 if (vtbl_check(ubi, ubi->vtbl)) {
873 ubi_err("paranoid check failed"); 865 ubi_err("self-check failed");
874 BUG(); 866 BUG();
875 } 867 }
876} 868}
877
878#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 7c1a9bf8ac86..9df100a4ec38 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -41,12 +41,6 @@
41 * physical eraseblocks with low erase counter to free physical eraseblocks 41 * physical eraseblocks with low erase counter to free physical eraseblocks
42 * with high erase counter. 42 * with high erase counter.
43 * 43 *
44 * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
45 * an "optimal" physical eraseblock. For example, when it is known that the
46 * physical eraseblock will be "put" soon because it contains short-term data,
47 * the WL sub-system may pick a free physical eraseblock with low erase
48 * counter, and so forth.
49 *
50 * If the WL sub-system fails to erase a physical eraseblock, it marks it as 44 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
51 * bad. 45 * bad.
52 * 46 *
@@ -70,8 +64,7 @@
70 * to the user; instead, we first want to let users fill them up with data; 64 * to the user; instead, we first want to let users fill them up with data;
71 * 65 *
72 * o there is a chance that the user will put the physical eraseblock very 66 * o there is a chance that the user will put the physical eraseblock very
73 * soon, so it makes sense not to move it for some time, but wait; this is 67 * soon, so it makes sense not to move it for some time, but wait.
74 * especially important in case of "short term" physical eraseblocks.
75 * 68 *
76 * Physical eraseblocks stay protected only for limited time. But the "time" is 69 * Physical eraseblocks stay protected only for limited time. But the "time" is
77 * measured in erase cycles in this case. This is implemented with help of the 70 * measured in erase cycles in this case. This is implemented with help of the
@@ -147,6 +140,8 @@
147 * @list: a link in the list of pending works 140 * @list: a link in the list of pending works
148 * @func: worker function 141 * @func: worker function
149 * @e: physical eraseblock to erase 142 * @e: physical eraseblock to erase
143 * @vol_id: the volume ID on which this erasure is being performed
144 * @lnum: the logical eraseblock number
150 * @torture: if the physical eraseblock has to be tortured 145 * @torture: if the physical eraseblock has to be tortured
151 * 146 *
152 * The @func pointer points to the worker function. If the @cancel argument is 147 * The @func pointer points to the worker function. If the @cancel argument is
@@ -159,21 +154,16 @@ struct ubi_work {
159 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel); 154 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
160 /* The below fields are only relevant to erasure works */ 155 /* The below fields are only relevant to erasure works */
161 struct ubi_wl_entry *e; 156 struct ubi_wl_entry *e;
157 int vol_id;
158 int lnum;
162 int torture; 159 int torture;
163}; 160};
164 161
165#ifdef CONFIG_MTD_UBI_DEBUG 162static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
166static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec); 163static int self_check_in_wl_tree(const struct ubi_device *ubi,
167static int paranoid_check_in_wl_tree(const struct ubi_device *ubi, 164 struct ubi_wl_entry *e, struct rb_root *root);
168 struct ubi_wl_entry *e, 165static int self_check_in_pq(const struct ubi_device *ubi,
169 struct rb_root *root); 166 struct ubi_wl_entry *e);
170static int paranoid_check_in_pq(const struct ubi_device *ubi,
171 struct ubi_wl_entry *e);
172#else
173#define paranoid_check_ec(ubi, pnum, ec) 0
174#define paranoid_check_in_wl_tree(ubi, e, root)
175#define paranoid_check_in_pq(ubi, e) 0
176#endif
177 167
178/** 168/**
179 * wl_tree_add - add a wear-leveling entry to a WL RB-tree. 169 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
@@ -383,19 +373,15 @@ static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff)
383/** 373/**
384 * ubi_wl_get_peb - get a physical eraseblock. 374 * ubi_wl_get_peb - get a physical eraseblock.
385 * @ubi: UBI device description object 375 * @ubi: UBI device description object
386 * @dtype: type of data which will be stored in this physical eraseblock
387 * 376 *
388 * This function returns a physical eraseblock in case of success and a 377 * This function returns a physical eraseblock in case of success and a
389 * negative error code in case of failure. Might sleep. 378 * negative error code in case of failure. Might sleep.
390 */ 379 */
391int ubi_wl_get_peb(struct ubi_device *ubi, int dtype) 380int ubi_wl_get_peb(struct ubi_device *ubi)
392{ 381{
393 int err; 382 int err;
394 struct ubi_wl_entry *e, *first, *last; 383 struct ubi_wl_entry *e, *first, *last;
395 384
396 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
397 dtype == UBI_UNKNOWN);
398
399retry: 385retry:
400 spin_lock(&ubi->wl_lock); 386 spin_lock(&ubi->wl_lock);
401 if (!ubi->free.rb_node) { 387 if (!ubi->free.rb_node) {
@@ -413,45 +399,15 @@ retry:
413 goto retry; 399 goto retry;
414 } 400 }
415 401
416 switch (dtype) { 402 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
417 case UBI_LONGTERM: 403 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
418 /* 404
419 * For long term data we pick a physical eraseblock with high 405 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
420 * erase counter. But the highest erase counter we can pick is 406 e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb);
421 * bounded by the the lowest erase counter plus 407 else
422 * %WL_FREE_MAX_DIFF. 408 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
423 */
424 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
425 break;
426 case UBI_UNKNOWN:
427 /*
428 * For unknown data we pick a physical eraseblock with medium
429 * erase counter. But we by no means can pick a physical
430 * eraseblock with erase counter greater or equivalent than the
431 * lowest erase counter plus %WL_FREE_MAX_DIFF/2.
432 */
433 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry,
434 u.rb);
435 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
436
437 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
438 e = rb_entry(ubi->free.rb_node,
439 struct ubi_wl_entry, u.rb);
440 else
441 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
442 break;
443 case UBI_SHORTTERM:
444 /*
445 * For short term data we pick a physical eraseblock with the
446 * lowest erase counter as we expect it will be erased soon.
447 */
448 e = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
449 break;
450 default:
451 BUG();
452 }
453 409
454 paranoid_check_in_wl_tree(ubi, e, &ubi->free); 410 self_check_in_wl_tree(ubi, e, &ubi->free);
455 411
456 /* 412 /*
457 * Move the physical eraseblock to the protection queue where it will 413 * Move the physical eraseblock to the protection queue where it will
@@ -462,8 +418,8 @@ retry:
462 prot_queue_add(ubi, e); 418 prot_queue_add(ubi, e);
463 spin_unlock(&ubi->wl_lock); 419 spin_unlock(&ubi->wl_lock);
464 420
465 err = ubi_dbg_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset, 421 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
466 ubi->peb_size - ubi->vid_hdr_aloffset); 422 ubi->peb_size - ubi->vid_hdr_aloffset);
467 if (err) { 423 if (err) {
468 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum); 424 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
469 return err; 425 return err;
@@ -488,7 +444,7 @@ static int prot_queue_del(struct ubi_device *ubi, int pnum)
488 if (!e) 444 if (!e)
489 return -ENODEV; 445 return -ENODEV;
490 446
491 if (paranoid_check_in_pq(ubi, e)) 447 if (self_check_in_pq(ubi, e))
492 return -ENODEV; 448 return -ENODEV;
493 449
494 list_del(&e->u.list); 450 list_del(&e->u.list);
@@ -514,7 +470,7 @@ static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
514 470
515 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec); 471 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
516 472
517 err = paranoid_check_ec(ubi, e->pnum, e->ec); 473 err = self_check_ec(ubi, e->pnum, e->ec);
518 if (err) 474 if (err)
519 return -EINVAL; 475 return -EINVAL;
520 476
@@ -627,13 +583,15 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
627 * schedule_erase - schedule an erase work. 583 * schedule_erase - schedule an erase work.
628 * @ubi: UBI device description object 584 * @ubi: UBI device description object
629 * @e: the WL entry of the physical eraseblock to erase 585 * @e: the WL entry of the physical eraseblock to erase
586 * @vol_id: the volume ID that last used this PEB
587 * @lnum: the last used logical eraseblock number for the PEB
630 * @torture: if the physical eraseblock has to be tortured 588 * @torture: if the physical eraseblock has to be tortured
631 * 589 *
632 * This function returns zero in case of success and a %-ENOMEM in case of 590 * This function returns zero in case of success and a %-ENOMEM in case of
633 * failure. 591 * failure.
634 */ 592 */
635static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, 593static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
636 int torture) 594 int vol_id, int lnum, int torture)
637{ 595{
638 struct ubi_work *wl_wrk; 596 struct ubi_work *wl_wrk;
639 597
@@ -646,6 +604,8 @@ static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
646 604
647 wl_wrk->func = &erase_worker; 605 wl_wrk->func = &erase_worker;
648 wl_wrk->e = e; 606 wl_wrk->e = e;
607 wl_wrk->vol_id = vol_id;
608 wl_wrk->lnum = lnum;
649 wl_wrk->torture = torture; 609 wl_wrk->torture = torture;
650 610
651 schedule_ubi_work(ubi, wl_wrk); 611 schedule_ubi_work(ubi, wl_wrk);
@@ -714,7 +674,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
714 e1->ec, e2->ec); 674 e1->ec, e2->ec);
715 goto out_cancel; 675 goto out_cancel;
716 } 676 }
717 paranoid_check_in_wl_tree(ubi, e1, &ubi->used); 677 self_check_in_wl_tree(ubi, e1, &ubi->used);
718 rb_erase(&e1->u.rb, &ubi->used); 678 rb_erase(&e1->u.rb, &ubi->used);
719 dbg_wl("move PEB %d EC %d to PEB %d EC %d", 679 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
720 e1->pnum, e1->ec, e2->pnum, e2->ec); 680 e1->pnum, e1->ec, e2->pnum, e2->ec);
@@ -723,12 +683,12 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
723 scrubbing = 1; 683 scrubbing = 1;
724 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb); 684 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
725 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); 685 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
726 paranoid_check_in_wl_tree(ubi, e1, &ubi->scrub); 686 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
727 rb_erase(&e1->u.rb, &ubi->scrub); 687 rb_erase(&e1->u.rb, &ubi->scrub);
728 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum); 688 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
729 } 689 }
730 690
731 paranoid_check_in_wl_tree(ubi, e2, &ubi->free); 691 self_check_in_wl_tree(ubi, e2, &ubi->free);
732 rb_erase(&e2->u.rb, &ubi->free); 692 rb_erase(&e2->u.rb, &ubi->free);
733 ubi->move_from = e1; 693 ubi->move_from = e1;
734 ubi->move_to = e2; 694 ubi->move_to = e2;
@@ -846,7 +806,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
846 ubi->move_to_put = ubi->wl_scheduled = 0; 806 ubi->move_to_put = ubi->wl_scheduled = 0;
847 spin_unlock(&ubi->wl_lock); 807 spin_unlock(&ubi->wl_lock);
848 808
849 err = schedule_erase(ubi, e1, 0); 809 err = schedule_erase(ubi, e1, vol_id, lnum, 0);
850 if (err) { 810 if (err) {
851 kmem_cache_free(ubi_wl_entry_slab, e1); 811 kmem_cache_free(ubi_wl_entry_slab, e1);
852 if (e2) 812 if (e2)
@@ -861,7 +821,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
861 */ 821 */
862 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase", 822 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
863 e2->pnum, vol_id, lnum); 823 e2->pnum, vol_id, lnum);
864 err = schedule_erase(ubi, e2, 0); 824 err = schedule_erase(ubi, e2, vol_id, lnum, 0);
865 if (err) { 825 if (err) {
866 kmem_cache_free(ubi_wl_entry_slab, e2); 826 kmem_cache_free(ubi_wl_entry_slab, e2);
867 goto out_ro; 827 goto out_ro;
@@ -900,7 +860,7 @@ out_not_moved:
900 spin_unlock(&ubi->wl_lock); 860 spin_unlock(&ubi->wl_lock);
901 861
902 ubi_free_vid_hdr(ubi, vid_hdr); 862 ubi_free_vid_hdr(ubi, vid_hdr);
903 err = schedule_erase(ubi, e2, torture); 863 err = schedule_erase(ubi, e2, vol_id, lnum, torture);
904 if (err) { 864 if (err) {
905 kmem_cache_free(ubi_wl_entry_slab, e2); 865 kmem_cache_free(ubi_wl_entry_slab, e2);
906 goto out_ro; 866 goto out_ro;
@@ -1019,6 +979,8 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1019{ 979{
1020 struct ubi_wl_entry *e = wl_wrk->e; 980 struct ubi_wl_entry *e = wl_wrk->e;
1021 int pnum = e->pnum, err, need; 981 int pnum = e->pnum, err, need;
982 int vol_id = wl_wrk->vol_id;
983 int lnum = wl_wrk->lnum;
1022 984
1023 if (cancel) { 985 if (cancel) {
1024 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec); 986 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
@@ -1027,7 +989,8 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1027 return 0; 989 return 0;
1028 } 990 }
1029 991
1030 dbg_wl("erase PEB %d EC %d", pnum, e->ec); 992 dbg_wl("erase PEB %d EC %d LEB %d:%d",
993 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1031 994
1032 err = sync_erase(ubi, e, wl_wrk->torture); 995 err = sync_erase(ubi, e, wl_wrk->torture);
1033 if (!err) { 996 if (!err) {
@@ -1057,7 +1020,7 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1057 int err1; 1020 int err1;
1058 1021
1059 /* Re-schedule the LEB for erasure */ 1022 /* Re-schedule the LEB for erasure */
1060 err1 = schedule_erase(ubi, e, 0); 1023 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
1061 if (err1) { 1024 if (err1) {
1062 err = err1; 1025 err = err1;
1063 goto out_ro; 1026 goto out_ro;
@@ -1125,6 +1088,8 @@ out_ro:
1125/** 1088/**
1126 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system. 1089 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1127 * @ubi: UBI device description object 1090 * @ubi: UBI device description object
1091 * @vol_id: the volume ID that last used this PEB
1092 * @lnum: the last used logical eraseblock number for the PEB
1128 * @pnum: physical eraseblock to return 1093 * @pnum: physical eraseblock to return
1129 * @torture: if this physical eraseblock has to be tortured 1094 * @torture: if this physical eraseblock has to be tortured
1130 * 1095 *
@@ -1133,7 +1098,8 @@ out_ro:
1133 * occurred to this @pnum and it has to be tested. This function returns zero 1098 * occurred to this @pnum and it has to be tested. This function returns zero
1134 * in case of success, and a negative error code in case of failure. 1099 * in case of success, and a negative error code in case of failure.
1135 */ 1100 */
1136int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture) 1101int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1102 int pnum, int torture)
1137{ 1103{
1138 int err; 1104 int err;
1139 struct ubi_wl_entry *e; 1105 struct ubi_wl_entry *e;
@@ -1175,13 +1141,13 @@ retry:
1175 return 0; 1141 return 0;
1176 } else { 1142 } else {
1177 if (in_wl_tree(e, &ubi->used)) { 1143 if (in_wl_tree(e, &ubi->used)) {
1178 paranoid_check_in_wl_tree(ubi, e, &ubi->used); 1144 self_check_in_wl_tree(ubi, e, &ubi->used);
1179 rb_erase(&e->u.rb, &ubi->used); 1145 rb_erase(&e->u.rb, &ubi->used);
1180 } else if (in_wl_tree(e, &ubi->scrub)) { 1146 } else if (in_wl_tree(e, &ubi->scrub)) {
1181 paranoid_check_in_wl_tree(ubi, e, &ubi->scrub); 1147 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1182 rb_erase(&e->u.rb, &ubi->scrub); 1148 rb_erase(&e->u.rb, &ubi->scrub);
1183 } else if (in_wl_tree(e, &ubi->erroneous)) { 1149 } else if (in_wl_tree(e, &ubi->erroneous)) {
1184 paranoid_check_in_wl_tree(ubi, e, &ubi->erroneous); 1150 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1185 rb_erase(&e->u.rb, &ubi->erroneous); 1151 rb_erase(&e->u.rb, &ubi->erroneous);
1186 ubi->erroneous_peb_count -= 1; 1152 ubi->erroneous_peb_count -= 1;
1187 ubi_assert(ubi->erroneous_peb_count >= 0); 1153 ubi_assert(ubi->erroneous_peb_count >= 0);
@@ -1199,7 +1165,7 @@ retry:
1199 } 1165 }
1200 spin_unlock(&ubi->wl_lock); 1166 spin_unlock(&ubi->wl_lock);
1201 1167
1202 err = schedule_erase(ubi, e, torture); 1168 err = schedule_erase(ubi, e, vol_id, lnum, torture);
1203 if (err) { 1169 if (err) {
1204 spin_lock(&ubi->wl_lock); 1170 spin_lock(&ubi->wl_lock);
1205 wl_tree_add(e, &ubi->used); 1171 wl_tree_add(e, &ubi->used);
@@ -1248,7 +1214,7 @@ retry:
1248 } 1214 }
1249 1215
1250 if (in_wl_tree(e, &ubi->used)) { 1216 if (in_wl_tree(e, &ubi->used)) {
1251 paranoid_check_in_wl_tree(ubi, e, &ubi->used); 1217 self_check_in_wl_tree(ubi, e, &ubi->used);
1252 rb_erase(&e->u.rb, &ubi->used); 1218 rb_erase(&e->u.rb, &ubi->used);
1253 } else { 1219 } else {
1254 int err; 1220 int err;
@@ -1275,44 +1241,55 @@ retry:
1275/** 1241/**
1276 * ubi_wl_flush - flush all pending works. 1242 * ubi_wl_flush - flush all pending works.
1277 * @ubi: UBI device description object 1243 * @ubi: UBI device description object
1244 * @vol_id: the volume id to flush for
1245 * @lnum: the logical eraseblock number to flush for
1278 * 1246 *
1279 * This function returns zero in case of success and a negative error code in 1247 * This function executes all pending works for a particular volume id /
1280 * case of failure. 1248 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1249 * acts as a wildcard for all of the corresponding volume numbers or logical
1250 * eraseblock numbers. It returns zero in case of success and a negative error
1251 * code in case of failure.
1281 */ 1252 */
1282int ubi_wl_flush(struct ubi_device *ubi) 1253int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
1283{ 1254{
1284 int err; 1255 int err = 0;
1256 int found = 1;
1285 1257
1286 /* 1258 /*
1287 * Erase while the pending works queue is not empty, but not more than 1259 * Erase while the pending works queue is not empty, but not more than
1288 * the number of currently pending works. 1260 * the number of currently pending works.
1289 */ 1261 */
1290 dbg_wl("flush (%d pending works)", ubi->works_count); 1262 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1291 while (ubi->works_count) { 1263 vol_id, lnum, ubi->works_count);
1292 err = do_work(ubi);
1293 if (err)
1294 return err;
1295 }
1296 1264
1297 /*
1298 * Make sure all the works which have been done in parallel are
1299 * finished.
1300 */
1301 down_write(&ubi->work_sem); 1265 down_write(&ubi->work_sem);
1302 up_write(&ubi->work_sem); 1266 while (found) {
1267 struct ubi_work *wrk;
1268 found = 0;
1303 1269
1304 /* 1270 spin_lock(&ubi->wl_lock);
1305 * And in case last was the WL worker and it canceled the LEB 1271 list_for_each_entry(wrk, &ubi->works, list) {
1306 * movement, flush again. 1272 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1307 */ 1273 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1308 while (ubi->works_count) { 1274 list_del(&wrk->list);
1309 dbg_wl("flush more (%d pending works)", ubi->works_count); 1275 ubi->works_count -= 1;
1310 err = do_work(ubi); 1276 ubi_assert(ubi->works_count >= 0);
1311 if (err) 1277 spin_unlock(&ubi->wl_lock);
1312 return err; 1278
1279 err = wrk->func(ubi, wrk, 0);
1280 if (err)
1281 goto out;
1282 spin_lock(&ubi->wl_lock);
1283 found = 1;
1284 break;
1285 }
1286 }
1287 spin_unlock(&ubi->wl_lock);
1313 } 1288 }
1314 1289
1315 return 0; 1290out:
1291 up_write(&ubi->work_sem);
1292 return err;
1316} 1293}
1317 1294
1318/** 1295/**
@@ -1421,26 +1398,26 @@ static void cancel_pending(struct ubi_device *ubi)
1421} 1398}
1422 1399
1423/** 1400/**
1424 * ubi_wl_init_scan - initialize the WL sub-system using scanning information. 1401 * ubi_wl_init - initialize the WL sub-system using attaching information.
1425 * @ubi: UBI device description object 1402 * @ubi: UBI device description object
1426 * @si: scanning information 1403 * @ai: attaching information
1427 * 1404 *
1428 * This function returns zero in case of success, and a negative error code in 1405 * This function returns zero in case of success, and a negative error code in
1429 * case of failure. 1406 * case of failure.
1430 */ 1407 */
1431int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) 1408int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1432{ 1409{
1433 int err, i; 1410 int err, i;
1434 struct rb_node *rb1, *rb2; 1411 struct rb_node *rb1, *rb2;
1435 struct ubi_scan_volume *sv; 1412 struct ubi_ainf_volume *av;
1436 struct ubi_scan_leb *seb, *tmp; 1413 struct ubi_ainf_peb *aeb, *tmp;
1437 struct ubi_wl_entry *e; 1414 struct ubi_wl_entry *e;
1438 1415
1439 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT; 1416 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1440 spin_lock_init(&ubi->wl_lock); 1417 spin_lock_init(&ubi->wl_lock);
1441 mutex_init(&ubi->move_mutex); 1418 mutex_init(&ubi->move_mutex);
1442 init_rwsem(&ubi->work_sem); 1419 init_rwsem(&ubi->work_sem);
1443 ubi->max_ec = si->max_ec; 1420 ubi->max_ec = ai->max_ec;
1444 INIT_LIST_HEAD(&ubi->works); 1421 INIT_LIST_HEAD(&ubi->works);
1445 1422
1446 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num); 1423 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
@@ -1454,48 +1431,48 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1454 INIT_LIST_HEAD(&ubi->pq[i]); 1431 INIT_LIST_HEAD(&ubi->pq[i]);
1455 ubi->pq_head = 0; 1432 ubi->pq_head = 0;
1456 1433
1457 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) { 1434 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
1458 cond_resched(); 1435 cond_resched();
1459 1436
1460 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 1437 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1461 if (!e) 1438 if (!e)
1462 goto out_free; 1439 goto out_free;
1463 1440
1464 e->pnum = seb->pnum; 1441 e->pnum = aeb->pnum;
1465 e->ec = seb->ec; 1442 e->ec = aeb->ec;
1466 ubi->lookuptbl[e->pnum] = e; 1443 ubi->lookuptbl[e->pnum] = e;
1467 if (schedule_erase(ubi, e, 0)) { 1444 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
1468 kmem_cache_free(ubi_wl_entry_slab, e); 1445 kmem_cache_free(ubi_wl_entry_slab, e);
1469 goto out_free; 1446 goto out_free;
1470 } 1447 }
1471 } 1448 }
1472 1449
1473 list_for_each_entry(seb, &si->free, u.list) { 1450 list_for_each_entry(aeb, &ai->free, u.list) {
1474 cond_resched(); 1451 cond_resched();
1475 1452
1476 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 1453 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1477 if (!e) 1454 if (!e)
1478 goto out_free; 1455 goto out_free;
1479 1456
1480 e->pnum = seb->pnum; 1457 e->pnum = aeb->pnum;
1481 e->ec = seb->ec; 1458 e->ec = aeb->ec;
1482 ubi_assert(e->ec >= 0); 1459 ubi_assert(e->ec >= 0);
1483 wl_tree_add(e, &ubi->free); 1460 wl_tree_add(e, &ubi->free);
1484 ubi->lookuptbl[e->pnum] = e; 1461 ubi->lookuptbl[e->pnum] = e;
1485 } 1462 }
1486 1463
1487 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1464 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1488 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { 1465 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1489 cond_resched(); 1466 cond_resched();
1490 1467
1491 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 1468 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1492 if (!e) 1469 if (!e)
1493 goto out_free; 1470 goto out_free;
1494 1471
1495 e->pnum = seb->pnum; 1472 e->pnum = aeb->pnum;
1496 e->ec = seb->ec; 1473 e->ec = aeb->ec;
1497 ubi->lookuptbl[e->pnum] = e; 1474 ubi->lookuptbl[e->pnum] = e;
1498 if (!seb->scrub) { 1475 if (!aeb->scrub) {
1499 dbg_wl("add PEB %d EC %d to the used tree", 1476 dbg_wl("add PEB %d EC %d to the used tree",
1500 e->pnum, e->ec); 1477 e->pnum, e->ec);
1501 wl_tree_add(e, &ubi->used); 1478 wl_tree_add(e, &ubi->used);
@@ -1567,10 +1544,8 @@ void ubi_wl_close(struct ubi_device *ubi)
1567 kfree(ubi->lookuptbl); 1544 kfree(ubi->lookuptbl);
1568} 1545}
1569 1546
1570#ifdef CONFIG_MTD_UBI_DEBUG
1571
1572/** 1547/**
1573 * paranoid_check_ec - make sure that the erase counter of a PEB is correct. 1548 * self_check_ec - make sure that the erase counter of a PEB is correct.
1574 * @ubi: UBI device description object 1549 * @ubi: UBI device description object
1575 * @pnum: the physical eraseblock number to check 1550 * @pnum: the physical eraseblock number to check
1576 * @ec: the erase counter to check 1551 * @ec: the erase counter to check
@@ -1579,7 +1554,7 @@ void ubi_wl_close(struct ubi_device *ubi)
1579 * is equivalent to @ec, and a negative error code if not or if an error 1554 * is equivalent to @ec, and a negative error code if not or if an error
1580 * occurred. 1555 * occurred.
1581 */ 1556 */
1582static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec) 1557static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
1583{ 1558{
1584 int err; 1559 int err;
1585 long long read_ec; 1560 long long read_ec;
@@ -1601,9 +1576,9 @@ static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1601 1576
1602 read_ec = be64_to_cpu(ec_hdr->ec); 1577 read_ec = be64_to_cpu(ec_hdr->ec);
1603 if (ec != read_ec) { 1578 if (ec != read_ec) {
1604 ubi_err("paranoid check failed for PEB %d", pnum); 1579 ubi_err("self-check failed for PEB %d", pnum);
1605 ubi_err("read EC is %lld, should be %d", read_ec, ec); 1580 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1606 ubi_dbg_dump_stack(); 1581 dump_stack();
1607 err = 1; 1582 err = 1;
1608 } else 1583 } else
1609 err = 0; 1584 err = 0;
@@ -1614,7 +1589,7 @@ out_free:
1614} 1589}
1615 1590
1616/** 1591/**
1617 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree. 1592 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
1618 * @ubi: UBI device description object 1593 * @ubi: UBI device description object
1619 * @e: the wear-leveling entry to check 1594 * @e: the wear-leveling entry to check
1620 * @root: the root of the tree 1595 * @root: the root of the tree
@@ -1622,9 +1597,8 @@ out_free:
1622 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it 1597 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1623 * is not. 1598 * is not.
1624 */ 1599 */
1625static int paranoid_check_in_wl_tree(const struct ubi_device *ubi, 1600static int self_check_in_wl_tree(const struct ubi_device *ubi,
1626 struct ubi_wl_entry *e, 1601 struct ubi_wl_entry *e, struct rb_root *root)
1627 struct rb_root *root)
1628{ 1602{
1629 if (!ubi->dbg->chk_gen) 1603 if (!ubi->dbg->chk_gen)
1630 return 0; 1604 return 0;
@@ -1632,22 +1606,22 @@ static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
1632 if (in_wl_tree(e, root)) 1606 if (in_wl_tree(e, root))
1633 return 0; 1607 return 0;
1634 1608
1635 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ", 1609 ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
1636 e->pnum, e->ec, root); 1610 e->pnum, e->ec, root);
1637 ubi_dbg_dump_stack(); 1611 dump_stack();
1638 return -EINVAL; 1612 return -EINVAL;
1639} 1613}
1640 1614
1641/** 1615/**
1642 * paranoid_check_in_pq - check if wear-leveling entry is in the protection 1616 * self_check_in_pq - check if wear-leveling entry is in the protection
1643 * queue. 1617 * queue.
1644 * @ubi: UBI device description object 1618 * @ubi: UBI device description object
1645 * @e: the wear-leveling entry to check 1619 * @e: the wear-leveling entry to check
1646 * 1620 *
1647 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not. 1621 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
1648 */ 1622 */
1649static int paranoid_check_in_pq(const struct ubi_device *ubi, 1623static int self_check_in_pq(const struct ubi_device *ubi,
1650 struct ubi_wl_entry *e) 1624 struct ubi_wl_entry *e)
1651{ 1625{
1652 struct ubi_wl_entry *p; 1626 struct ubi_wl_entry *p;
1653 int i; 1627 int i;
@@ -1660,10 +1634,8 @@ static int paranoid_check_in_pq(const struct ubi_device *ubi,
1660 if (p == e) 1634 if (p == e)
1661 return 0; 1635 return 0;
1662 1636
1663 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue", 1637 ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
1664 e->pnum, e->ec); 1638 e->pnum, e->ec);
1665 ubi_dbg_dump_stack(); 1639 dump_stack();
1666 return -EINVAL; 1640 return -EINVAL;
1667} 1641}
1668
1669#endif /* CONFIG_MTD_UBI_DEBUG */