Commit | Line | Data |
---|---|---|
dbb7d2a8 RW |
1 | /* |
2 | * Copyright (c) 2012 Linutronix GmbH | |
3 | * Author: Richard Weinberger <richard@nod.at> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2. | |
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 | */ | |
15 | ||
16 | #include <linux/crc32.h> | |
17 | #include "ubi.h" | |
18 | ||
19 | /** | |
20 | * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device. | |
21 | * @ubi: UBI device description object | |
22 | */ | |
23 | size_t ubi_calc_fm_size(struct ubi_device *ubi) | |
24 | { | |
25 | size_t size; | |
26 | ||
91401a34 RW |
27 | size = sizeof(struct ubi_fm_sb) + \ |
28 | sizeof(struct ubi_fm_hdr) + \ | |
dbb7d2a8 RW |
29 | sizeof(struct ubi_fm_scan_pool) + \ |
30 | sizeof(struct ubi_fm_scan_pool) + \ | |
31 | (ubi->peb_count * sizeof(struct ubi_fm_ec)) + \ | |
32 | (sizeof(struct ubi_fm_eba) + \ | |
33 | (ubi->peb_count * sizeof(__be32))) + \ | |
34 | sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES; | |
35 | return roundup(size, ubi->leb_size); | |
36 | } | |
37 | ||
38 | ||
39 | /** | |
40 | * new_fm_vhdr - allocate a new volume header for fastmap usage. | |
41 | * @ubi: UBI device description object | |
42 | * @vol_id: the VID of the new header | |
43 | * | |
44 | * Returns a new struct ubi_vid_hdr on success. | |
45 | * NULL indicates out of memory. | |
46 | */ | |
47 | static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id) | |
48 | { | |
49 | struct ubi_vid_hdr *new; | |
50 | ||
51 | new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); | |
52 | if (!new) | |
53 | goto out; | |
54 | ||
55 | new->vol_type = UBI_VID_DYNAMIC; | |
56 | new->vol_id = cpu_to_be32(vol_id); | |
57 | ||
58 | /* UBI implementations without fastmap support have to delete the | |
59 | * fastmap. | |
60 | */ | |
61 | new->compat = UBI_COMPAT_DELETE; | |
62 | ||
63 | out: | |
64 | return new; | |
65 | } | |
66 | ||
67 | /** | |
68 | * add_aeb - create and add a attach erase block to a given list. | |
69 | * @ai: UBI attach info object | |
70 | * @list: the target list | |
71 | * @pnum: PEB number of the new attach erase block | |
72 | * @ec: erease counter of the new LEB | |
73 | * @scrub: scrub this PEB after attaching | |
74 | * | |
75 | * Returns 0 on success, < 0 indicates an internal error. | |
76 | */ | |
77 | static int add_aeb(struct ubi_attach_info *ai, struct list_head *list, | |
78 | int pnum, int ec, int scrub) | |
79 | { | |
80 | struct ubi_ainf_peb *aeb; | |
81 | ||
82 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); | |
83 | if (!aeb) | |
84 | return -ENOMEM; | |
85 | ||
86 | aeb->pnum = pnum; | |
87 | aeb->ec = ec; | |
88 | aeb->lnum = -1; | |
89 | aeb->scrub = scrub; | |
90 | aeb->copy_flag = aeb->sqnum = 0; | |
91 | ||
92 | ai->ec_sum += aeb->ec; | |
93 | ai->ec_count++; | |
94 | ||
95 | if (ai->max_ec < aeb->ec) | |
96 | ai->max_ec = aeb->ec; | |
97 | ||
98 | if (ai->min_ec > aeb->ec) | |
99 | ai->min_ec = aeb->ec; | |
100 | ||
101 | list_add_tail(&aeb->u.list, list); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | /** | |
107 | * add_vol - create and add a new volume to ubi_attach_info. | |
108 | * @ai: ubi_attach_info object | |
109 | * @vol_id: VID of the new volume | |
110 | * @used_ebs: number of used EBS | |
111 | * @data_pad: data padding value of the new volume | |
112 | * @vol_type: volume type | |
113 | * @last_eb_bytes: number of bytes in the last LEB | |
114 | * | |
115 | * Returns the new struct ubi_ainf_volume on success. | |
116 | * NULL indicates an error. | |
117 | */ | |
118 | static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id, | |
119 | int used_ebs, int data_pad, u8 vol_type, | |
120 | int last_eb_bytes) | |
121 | { | |
122 | struct ubi_ainf_volume *av; | |
123 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; | |
124 | ||
125 | while (*p) { | |
126 | parent = *p; | |
127 | av = rb_entry(parent, struct ubi_ainf_volume, rb); | |
128 | ||
e9110361 | 129 | if (vol_id > av->vol_id) |
dbb7d2a8 | 130 | p = &(*p)->rb_left; |
604b592e | 131 | else |
dbb7d2a8 RW |
132 | p = &(*p)->rb_right; |
133 | } | |
134 | ||
135 | av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); | |
136 | if (!av) | |
137 | goto out; | |
138 | ||
139 | av->highest_lnum = av->leb_count = 0; | |
140 | av->vol_id = vol_id; | |
141 | av->used_ebs = used_ebs; | |
142 | av->data_pad = data_pad; | |
143 | av->last_data_size = last_eb_bytes; | |
144 | av->compat = 0; | |
145 | av->vol_type = vol_type; | |
146 | av->root = RB_ROOT; | |
147 | ||
148 | dbg_bld("found volume (ID %i)", vol_id); | |
149 | ||
150 | rb_link_node(&av->rb, parent, p); | |
151 | rb_insert_color(&av->rb, &ai->volumes); | |
152 | ||
153 | out: | |
154 | return av; | |
155 | } | |
156 | ||
157 | /** | |
158 | * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it | |
159 | * from it's original list. | |
160 | * @ai: ubi_attach_info object | |
161 | * @aeb: the to be assigned SEB | |
162 | * @av: target scan volume | |
163 | */ | |
164 | static void assign_aeb_to_av(struct ubi_attach_info *ai, | |
165 | struct ubi_ainf_peb *aeb, | |
166 | struct ubi_ainf_volume *av) | |
167 | { | |
168 | struct ubi_ainf_peb *tmp_aeb; | |
169 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; | |
170 | ||
171 | p = &av->root.rb_node; | |
172 | while (*p) { | |
173 | parent = *p; | |
174 | ||
175 | tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); | |
176 | if (aeb->lnum != tmp_aeb->lnum) { | |
177 | if (aeb->lnum < tmp_aeb->lnum) | |
178 | p = &(*p)->rb_left; | |
179 | else | |
180 | p = &(*p)->rb_right; | |
181 | ||
182 | continue; | |
183 | } else | |
184 | break; | |
185 | } | |
186 | ||
187 | list_del(&aeb->u.list); | |
188 | av->leb_count++; | |
189 | ||
190 | rb_link_node(&aeb->u.rb, parent, p); | |
191 | rb_insert_color(&aeb->u.rb, &av->root); | |
192 | } | |
193 | ||
194 | /** | |
195 | * update_vol - inserts or updates a LEB which was found a pool. | |
196 | * @ubi: the UBI device object | |
197 | * @ai: attach info object | |
198 | * @av: the volume this LEB belongs to | |
199 | * @new_vh: the volume header derived from new_aeb | |
200 | * @new_aeb: the AEB to be examined | |
201 | * | |
202 | * Returns 0 on success, < 0 indicates an internal error. | |
203 | */ | |
204 | static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, | |
205 | struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh, | |
206 | struct ubi_ainf_peb *new_aeb) | |
207 | { | |
208 | struct rb_node **p = &av->root.rb_node, *parent = NULL; | |
209 | struct ubi_ainf_peb *aeb, *victim; | |
210 | int cmp_res; | |
211 | ||
212 | while (*p) { | |
213 | parent = *p; | |
214 | aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); | |
215 | ||
216 | if (be32_to_cpu(new_vh->lnum) != aeb->lnum) { | |
217 | if (be32_to_cpu(new_vh->lnum) < aeb->lnum) | |
218 | p = &(*p)->rb_left; | |
219 | else | |
220 | p = &(*p)->rb_right; | |
221 | ||
222 | continue; | |
223 | } | |
224 | ||
225 | /* This case can happen if the fastmap gets written | |
226 | * because of a volume change (creation, deletion, ..). | |
227 | * Then a PEB can be within the persistent EBA and the pool. | |
228 | */ | |
229 | if (aeb->pnum == new_aeb->pnum) { | |
230 | ubi_assert(aeb->lnum == new_aeb->lnum); | |
231 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); | |
237 | if (cmp_res < 0) | |
238 | return cmp_res; | |
239 | ||
240 | /* new_aeb is newer */ | |
241 | if (cmp_res & 1) { | |
242 | victim = kmem_cache_alloc(ai->aeb_slab_cache, | |
243 | GFP_KERNEL); | |
244 | if (!victim) | |
245 | return -ENOMEM; | |
246 | ||
247 | victim->ec = aeb->ec; | |
248 | victim->pnum = aeb->pnum; | |
249 | list_add_tail(&victim->u.list, &ai->erase); | |
250 | ||
251 | if (av->highest_lnum == be32_to_cpu(new_vh->lnum)) | |
252 | av->last_data_size = \ | |
253 | be32_to_cpu(new_vh->data_size); | |
254 | ||
255 | dbg_bld("vol %i: AEB %i's PEB %i is the newer", | |
256 | av->vol_id, aeb->lnum, new_aeb->pnum); | |
257 | ||
258 | aeb->ec = new_aeb->ec; | |
259 | aeb->pnum = new_aeb->pnum; | |
260 | aeb->copy_flag = new_vh->copy_flag; | |
261 | aeb->scrub = new_aeb->scrub; | |
262 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); | |
263 | ||
264 | /* new_aeb is older */ | |
265 | } else { | |
266 | dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it", | |
267 | av->vol_id, aeb->lnum, new_aeb->pnum); | |
268 | list_add_tail(&new_aeb->u.list, &ai->erase); | |
269 | } | |
270 | ||
271 | return 0; | |
272 | } | |
273 | /* This LEB is new, let's add it to the volume */ | |
274 | ||
275 | if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) { | |
276 | av->highest_lnum = be32_to_cpu(new_vh->lnum); | |
277 | av->last_data_size = be32_to_cpu(new_vh->data_size); | |
278 | } | |
279 | ||
280 | if (av->vol_type == UBI_STATIC_VOLUME) | |
281 | av->used_ebs = be32_to_cpu(new_vh->used_ebs); | |
282 | ||
283 | av->leb_count++; | |
284 | ||
285 | rb_link_node(&new_aeb->u.rb, parent, p); | |
286 | rb_insert_color(&new_aeb->u.rb, &av->root); | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | /** | |
292 | * process_pool_aeb - we found a non-empty PEB in a pool. | |
293 | * @ubi: UBI device object | |
294 | * @ai: attach info object | |
295 | * @new_vh: the volume header derived from new_aeb | |
296 | * @new_aeb: the AEB to be examined | |
297 | * | |
298 | * Returns 0 on success, < 0 indicates an internal error. | |
299 | */ | |
300 | static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, | |
301 | struct ubi_vid_hdr *new_vh, | |
302 | struct ubi_ainf_peb *new_aeb) | |
303 | { | |
304 | struct ubi_ainf_volume *av, *tmp_av = NULL; | |
305 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; | |
306 | int found = 0; | |
307 | ||
308 | if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID || | |
309 | be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) { | |
310 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
315 | /* Find the volume this SEB belongs to */ | |
316 | while (*p) { | |
317 | parent = *p; | |
318 | tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb); | |
319 | ||
320 | if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id) | |
321 | p = &(*p)->rb_left; | |
322 | else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id) | |
323 | p = &(*p)->rb_right; | |
324 | else { | |
325 | found = 1; | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | if (found) | |
331 | av = tmp_av; | |
332 | else { | |
32608703 | 333 | ubi_err(ubi, "orphaned volume in fastmap pool!"); |
1bf1890e | 334 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); |
dbb7d2a8 RW |
335 | return UBI_BAD_FASTMAP; |
336 | } | |
337 | ||
338 | ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id); | |
339 | ||
340 | return update_vol(ubi, ai, av, new_vh, new_aeb); | |
341 | } | |
342 | ||
343 | /** | |
344 | * unmap_peb - unmap a PEB. | |
345 | * If fastmap detects a free PEB in the pool it has to check whether | |
346 | * this PEB has been unmapped after writing the fastmap. | |
347 | * | |
348 | * @ai: UBI attach info object | |
349 | * @pnum: The PEB to be unmapped | |
350 | */ | |
351 | static void unmap_peb(struct ubi_attach_info *ai, int pnum) | |
352 | { | |
353 | struct ubi_ainf_volume *av; | |
354 | struct rb_node *node, *node2; | |
355 | struct ubi_ainf_peb *aeb; | |
356 | ||
357 | for (node = rb_first(&ai->volumes); node; node = rb_next(node)) { | |
358 | av = rb_entry(node, struct ubi_ainf_volume, rb); | |
359 | ||
360 | for (node2 = rb_first(&av->root); node2; | |
361 | node2 = rb_next(node2)) { | |
362 | aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb); | |
363 | if (aeb->pnum == pnum) { | |
364 | rb_erase(&aeb->u.rb, &av->root); | |
365 | kmem_cache_free(ai->aeb_slab_cache, aeb); | |
366 | return; | |
367 | } | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | /** | |
373 | * scan_pool - scans a pool for changed (no longer empty PEBs). | |
374 | * @ubi: UBI device object | |
375 | * @ai: attach info object | |
376 | * @pebs: an array of all PEB numbers in the to be scanned pool | |
377 | * @pool_size: size of the pool (number of entries in @pebs) | |
378 | * @max_sqnum: pointer to the maximal sequence number | |
379 | * @eba_orphans: list of PEBs which need to be scanned | |
380 | * @free: list of PEBs which are most likely free (and go into @ai->free) | |
381 | * | |
382 | * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned. | |
383 | * < 0 indicates an internal error. | |
384 | */ | |
385 | static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, | |
386 | int *pebs, int pool_size, unsigned long long *max_sqnum, | |
387 | struct list_head *eba_orphans, struct list_head *free) | |
388 | { | |
389 | struct ubi_vid_hdr *vh; | |
390 | struct ubi_ec_hdr *ech; | |
391 | struct ubi_ainf_peb *new_aeb, *tmp_aeb; | |
392 | int i, pnum, err, found_orphan, ret = 0; | |
393 | ||
394 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | |
395 | if (!ech) | |
396 | return -ENOMEM; | |
397 | ||
398 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); | |
399 | if (!vh) { | |
400 | kfree(ech); | |
401 | return -ENOMEM; | |
402 | } | |
403 | ||
404 | dbg_bld("scanning fastmap pool: size = %i", pool_size); | |
405 | ||
406 | /* | |
407 | * Now scan all PEBs in the pool to find changes which have been made | |
408 | * after the creation of the fastmap | |
409 | */ | |
410 | for (i = 0; i < pool_size; i++) { | |
411 | int scrub = 0; | |
c22301ad | 412 | int image_seq; |
dbb7d2a8 RW |
413 | |
414 | pnum = be32_to_cpu(pebs[i]); | |
415 | ||
416 | if (ubi_io_is_bad(ubi, pnum)) { | |
32608703 | 417 | ubi_err(ubi, "bad PEB in fastmap pool!"); |
dbb7d2a8 RW |
418 | ret = UBI_BAD_FASTMAP; |
419 | goto out; | |
420 | } | |
421 | ||
422 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); | |
423 | if (err && err != UBI_IO_BITFLIPS) { | |
32608703 | 424 | ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", |
dbb7d2a8 RW |
425 | pnum, err); |
426 | ret = err > 0 ? UBI_BAD_FASTMAP : err; | |
427 | goto out; | |
44305ebd | 428 | } else if (err == UBI_IO_BITFLIPS) |
dbb7d2a8 RW |
429 | scrub = 1; |
430 | ||
c22301ad RG |
431 | /* |
432 | * Older UBI implementations have image_seq set to zero, so | |
433 | * we shouldn't fail if image_seq == 0. | |
434 | */ | |
435 | image_seq = be32_to_cpu(ech->image_seq); | |
436 | ||
437 | if (image_seq && (image_seq != ubi->image_seq)) { | |
32608703 | 438 | ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", |
dbb7d2a8 | 439 | be32_to_cpu(ech->image_seq), ubi->image_seq); |
f240dca8 | 440 | ret = UBI_BAD_FASTMAP; |
dbb7d2a8 RW |
441 | goto out; |
442 | } | |
443 | ||
444 | err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); | |
445 | if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) { | |
446 | unsigned long long ec = be64_to_cpu(ech->ec); | |
447 | unmap_peb(ai, pnum); | |
448 | dbg_bld("Adding PEB to free: %i", pnum); | |
449 | if (err == UBI_IO_FF_BITFLIPS) | |
450 | add_aeb(ai, free, pnum, ec, 1); | |
451 | else | |
452 | add_aeb(ai, free, pnum, ec, 0); | |
453 | continue; | |
454 | } else if (err == 0 || err == UBI_IO_BITFLIPS) { | |
455 | dbg_bld("Found non empty PEB:%i in pool", pnum); | |
456 | ||
457 | if (err == UBI_IO_BITFLIPS) | |
458 | scrub = 1; | |
459 | ||
460 | found_orphan = 0; | |
461 | list_for_each_entry(tmp_aeb, eba_orphans, u.list) { | |
462 | if (tmp_aeb->pnum == pnum) { | |
463 | found_orphan = 1; | |
464 | break; | |
465 | } | |
466 | } | |
467 | if (found_orphan) { | |
dbb7d2a8 | 468 | list_del(&tmp_aeb->u.list); |
5547fec7 | 469 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
dbb7d2a8 RW |
470 | } |
471 | ||
472 | new_aeb = kmem_cache_alloc(ai->aeb_slab_cache, | |
473 | GFP_KERNEL); | |
474 | if (!new_aeb) { | |
475 | ret = -ENOMEM; | |
476 | goto out; | |
477 | } | |
478 | ||
479 | new_aeb->ec = be64_to_cpu(ech->ec); | |
480 | new_aeb->pnum = pnum; | |
481 | new_aeb->lnum = be32_to_cpu(vh->lnum); | |
482 | new_aeb->sqnum = be64_to_cpu(vh->sqnum); | |
483 | new_aeb->copy_flag = vh->copy_flag; | |
484 | new_aeb->scrub = scrub; | |
485 | ||
486 | if (*max_sqnum < new_aeb->sqnum) | |
487 | *max_sqnum = new_aeb->sqnum; | |
488 | ||
489 | err = process_pool_aeb(ubi, ai, vh, new_aeb); | |
490 | if (err) { | |
491 | ret = err > 0 ? UBI_BAD_FASTMAP : err; | |
492 | goto out; | |
493 | } | |
494 | } else { | |
495 | /* We are paranoid and fall back to scanning mode */ | |
32608703 | 496 | ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); |
dbb7d2a8 RW |
497 | ret = err > 0 ? UBI_BAD_FASTMAP : err; |
498 | goto out; | |
499 | } | |
500 | ||
501 | } | |
502 | ||
503 | out: | |
504 | ubi_free_vid_hdr(ubi, vh); | |
505 | kfree(ech); | |
506 | return ret; | |
507 | } | |
508 | ||
509 | /** | |
510 | * count_fastmap_pebs - Counts the PEBs found by fastmap. | |
511 | * @ai: The UBI attach info object | |
512 | */ | |
513 | static int count_fastmap_pebs(struct ubi_attach_info *ai) | |
514 | { | |
515 | struct ubi_ainf_peb *aeb; | |
516 | struct ubi_ainf_volume *av; | |
517 | struct rb_node *rb1, *rb2; | |
518 | int n = 0; | |
519 | ||
520 | list_for_each_entry(aeb, &ai->erase, u.list) | |
521 | n++; | |
522 | ||
523 | list_for_each_entry(aeb, &ai->free, u.list) | |
524 | n++; | |
525 | ||
526 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) | |
527 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) | |
528 | n++; | |
529 | ||
530 | return n; | |
531 | } | |
532 | ||
533 | /** | |
534 | * ubi_attach_fastmap - creates ubi_attach_info from a fastmap. | |
535 | * @ubi: UBI device object | |
536 | * @ai: UBI attach info object | |
537 | * @fm: the fastmap to be attached | |
538 | * | |
539 | * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable. | |
540 | * < 0 indicates an internal error. | |
541 | */ | |
542 | static int ubi_attach_fastmap(struct ubi_device *ubi, | |
543 | struct ubi_attach_info *ai, | |
544 | struct ubi_fastmap_layout *fm) | |
545 | { | |
546 | struct list_head used, eba_orphans, free; | |
547 | struct ubi_ainf_volume *av; | |
548 | struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb; | |
549 | struct ubi_ec_hdr *ech; | |
550 | struct ubi_fm_sb *fmsb; | |
551 | struct ubi_fm_hdr *fmhdr; | |
552 | struct ubi_fm_scan_pool *fmpl1, *fmpl2; | |
553 | struct ubi_fm_ec *fmec; | |
554 | struct ubi_fm_volhdr *fmvhdr; | |
555 | struct ubi_fm_eba *fm_eba; | |
556 | int ret, i, j, pool_size, wl_pool_size; | |
557 | size_t fm_pos = 0, fm_size = ubi->fm_size; | |
558 | unsigned long long max_sqnum = 0; | |
559 | void *fm_raw = ubi->fm_buf; | |
560 | ||
561 | INIT_LIST_HEAD(&used); | |
562 | INIT_LIST_HEAD(&free); | |
563 | INIT_LIST_HEAD(&eba_orphans); | |
dbb7d2a8 RW |
564 | ai->min_ec = UBI_MAX_ERASECOUNTER; |
565 | ||
dbb7d2a8 RW |
566 | fmsb = (struct ubi_fm_sb *)(fm_raw); |
567 | ai->max_sqnum = fmsb->sqnum; | |
568 | fm_pos += sizeof(struct ubi_fm_sb); | |
569 | if (fm_pos >= fm_size) | |
570 | goto fail_bad; | |
571 | ||
572 | fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos); | |
573 | fm_pos += sizeof(*fmhdr); | |
574 | if (fm_pos >= fm_size) | |
575 | goto fail_bad; | |
576 | ||
577 | if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) { | |
32608703 | 578 | ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
579 | be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC); |
580 | goto fail_bad; | |
581 | } | |
582 | ||
583 | fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); | |
584 | fm_pos += sizeof(*fmpl1); | |
585 | if (fm_pos >= fm_size) | |
586 | goto fail_bad; | |
587 | if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) { | |
32608703 | 588 | ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
589 | be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC); |
590 | goto fail_bad; | |
591 | } | |
592 | ||
593 | fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); | |
594 | fm_pos += sizeof(*fmpl2); | |
595 | if (fm_pos >= fm_size) | |
596 | goto fail_bad; | |
597 | if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) { | |
32608703 | 598 | ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
599 | be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC); |
600 | goto fail_bad; | |
601 | } | |
602 | ||
603 | pool_size = be16_to_cpu(fmpl1->size); | |
604 | wl_pool_size = be16_to_cpu(fmpl2->size); | |
605 | fm->max_pool_size = be16_to_cpu(fmpl1->max_size); | |
606 | fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size); | |
607 | ||
608 | if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) { | |
32608703 | 609 | ubi_err(ubi, "bad pool size: %i", pool_size); |
dbb7d2a8 RW |
610 | goto fail_bad; |
611 | } | |
612 | ||
613 | if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) { | |
32608703 | 614 | ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); |
dbb7d2a8 RW |
615 | goto fail_bad; |
616 | } | |
617 | ||
618 | ||
619 | if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE || | |
620 | fm->max_pool_size < 0) { | |
32608703 | 621 | ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); |
dbb7d2a8 RW |
622 | goto fail_bad; |
623 | } | |
624 | ||
625 | if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE || | |
626 | fm->max_wl_pool_size < 0) { | |
32608703 TB |
627 | ubi_err(ubi, "bad maximal WL pool size: %i", |
628 | fm->max_wl_pool_size); | |
dbb7d2a8 RW |
629 | goto fail_bad; |
630 | } | |
631 | ||
632 | /* read EC values from free list */ | |
633 | for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) { | |
634 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
635 | fm_pos += sizeof(*fmec); | |
636 | if (fm_pos >= fm_size) | |
637 | goto fail_bad; | |
638 | ||
639 | add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum), | |
640 | be32_to_cpu(fmec->ec), 0); | |
641 | } | |
642 | ||
643 | /* read EC values from used list */ | |
644 | for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) { | |
645 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
646 | fm_pos += sizeof(*fmec); | |
647 | if (fm_pos >= fm_size) | |
648 | goto fail_bad; | |
649 | ||
650 | add_aeb(ai, &used, be32_to_cpu(fmec->pnum), | |
651 | be32_to_cpu(fmec->ec), 0); | |
652 | } | |
653 | ||
654 | /* read EC values from scrub list */ | |
655 | for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) { | |
656 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
657 | fm_pos += sizeof(*fmec); | |
658 | if (fm_pos >= fm_size) | |
659 | goto fail_bad; | |
660 | ||
661 | add_aeb(ai, &used, be32_to_cpu(fmec->pnum), | |
662 | be32_to_cpu(fmec->ec), 1); | |
663 | } | |
664 | ||
665 | /* read EC values from erase list */ | |
666 | for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) { | |
667 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
668 | fm_pos += sizeof(*fmec); | |
669 | if (fm_pos >= fm_size) | |
670 | goto fail_bad; | |
671 | ||
672 | add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum), | |
673 | be32_to_cpu(fmec->ec), 1); | |
674 | } | |
675 | ||
676 | ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); | |
677 | ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count); | |
678 | ||
679 | /* Iterate over all volumes and read their EBA table */ | |
680 | for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) { | |
681 | fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); | |
682 | fm_pos += sizeof(*fmvhdr); | |
683 | if (fm_pos >= fm_size) | |
684 | goto fail_bad; | |
685 | ||
686 | if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) { | |
32608703 | 687 | ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
688 | be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC); |
689 | goto fail_bad; | |
690 | } | |
691 | ||
692 | av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id), | |
693 | be32_to_cpu(fmvhdr->used_ebs), | |
694 | be32_to_cpu(fmvhdr->data_pad), | |
695 | fmvhdr->vol_type, | |
696 | be32_to_cpu(fmvhdr->last_eb_bytes)); | |
697 | ||
698 | if (!av) | |
699 | goto fail_bad; | |
700 | ||
701 | ai->vols_found++; | |
702 | if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id)) | |
703 | ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id); | |
704 | ||
705 | fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos); | |
706 | fm_pos += sizeof(*fm_eba); | |
707 | fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs)); | |
708 | if (fm_pos >= fm_size) | |
709 | goto fail_bad; | |
710 | ||
711 | if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) { | |
32608703 | 712 | ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
713 | be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC); |
714 | goto fail_bad; | |
715 | } | |
716 | ||
717 | for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) { | |
718 | int pnum = be32_to_cpu(fm_eba->pnum[j]); | |
719 | ||
720 | if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0) | |
721 | continue; | |
722 | ||
723 | aeb = NULL; | |
724 | list_for_each_entry(tmp_aeb, &used, u.list) { | |
584d4623 | 725 | if (tmp_aeb->pnum == pnum) { |
dbb7d2a8 | 726 | aeb = tmp_aeb; |
584d4623 BP |
727 | break; |
728 | } | |
dbb7d2a8 RW |
729 | } |
730 | ||
731 | /* This can happen if a PEB is already in an EBA known | |
732 | * by this fastmap but the PEB itself is not in the used | |
733 | * list. | |
734 | * In this case the PEB can be within the fastmap pool | |
735 | * or while writing the fastmap it was in the protection | |
736 | * queue. | |
737 | */ | |
738 | if (!aeb) { | |
739 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, | |
740 | GFP_KERNEL); | |
741 | if (!aeb) { | |
742 | ret = -ENOMEM; | |
743 | ||
744 | goto fail; | |
745 | } | |
746 | ||
747 | aeb->lnum = j; | |
748 | aeb->pnum = be32_to_cpu(fm_eba->pnum[j]); | |
749 | aeb->ec = -1; | |
750 | aeb->scrub = aeb->copy_flag = aeb->sqnum = 0; | |
751 | list_add_tail(&aeb->u.list, &eba_orphans); | |
752 | continue; | |
753 | } | |
754 | ||
755 | aeb->lnum = j; | |
756 | ||
757 | if (av->highest_lnum <= aeb->lnum) | |
758 | av->highest_lnum = aeb->lnum; | |
759 | ||
760 | assign_aeb_to_av(ai, aeb, av); | |
761 | ||
762 | dbg_bld("inserting PEB:%i (LEB %i) to vol %i", | |
763 | aeb->pnum, aeb->lnum, av->vol_id); | |
764 | } | |
765 | ||
766 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | |
767 | if (!ech) { | |
768 | ret = -ENOMEM; | |
769 | goto fail; | |
770 | } | |
771 | ||
772 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans, | |
773 | u.list) { | |
774 | int err; | |
775 | ||
776 | if (ubi_io_is_bad(ubi, tmp_aeb->pnum)) { | |
32608703 | 777 | ubi_err(ubi, "bad PEB in fastmap EBA orphan list"); |
dbb7d2a8 RW |
778 | ret = UBI_BAD_FASTMAP; |
779 | kfree(ech); | |
780 | goto fail; | |
781 | } | |
782 | ||
783 | err = ubi_io_read_ec_hdr(ubi, tmp_aeb->pnum, ech, 0); | |
784 | if (err && err != UBI_IO_BITFLIPS) { | |
32608703 TB |
785 | ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", |
786 | tmp_aeb->pnum, err); | |
dbb7d2a8 RW |
787 | ret = err > 0 ? UBI_BAD_FASTMAP : err; |
788 | kfree(ech); | |
789 | ||
790 | goto fail; | |
791 | } else if (err == UBI_IO_BITFLIPS) | |
792 | tmp_aeb->scrub = 1; | |
793 | ||
794 | tmp_aeb->ec = be64_to_cpu(ech->ec); | |
795 | assign_aeb_to_av(ai, tmp_aeb, av); | |
796 | } | |
797 | ||
798 | kfree(ech); | |
799 | } | |
800 | ||
801 | ret = scan_pool(ubi, ai, fmpl1->pebs, pool_size, &max_sqnum, | |
802 | &eba_orphans, &free); | |
803 | if (ret) | |
804 | goto fail; | |
805 | ||
806 | ret = scan_pool(ubi, ai, fmpl2->pebs, wl_pool_size, &max_sqnum, | |
807 | &eba_orphans, &free); | |
808 | if (ret) | |
809 | goto fail; | |
810 | ||
811 | if (max_sqnum > ai->max_sqnum) | |
812 | ai->max_sqnum = max_sqnum; | |
813 | ||
6a059abd WY |
814 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) |
815 | list_move_tail(&tmp_aeb->u.list, &ai->free); | |
dbb7d2a8 | 816 | |
a83832a7 RW |
817 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) |
818 | list_move_tail(&tmp_aeb->u.list, &ai->erase); | |
819 | ||
ae0d1469 RW |
820 | ubi_assert(list_empty(&eba_orphans)); |
821 | ubi_assert(list_empty(&free)); | |
822 | ||
dbb7d2a8 RW |
823 | /* |
824 | * If fastmap is leaking PEBs (must not happen), raise a | |
825 | * fat warning and fall back to scanning mode. | |
826 | * We do this here because in ubi_wl_init() it's too late | |
827 | * and we cannot fall back to scanning. | |
828 | */ | |
829 | if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - | |
830 | ai->bad_peb_count - fm->used_blocks)) | |
831 | goto fail_bad; | |
832 | ||
833 | return 0; | |
834 | ||
835 | fail_bad: | |
836 | ret = UBI_BAD_FASTMAP; | |
837 | fail: | |
fe24c6e5 | 838 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) { |
fe24c6e5 | 839 | list_del(&tmp_aeb->u.list); |
5547fec7 | 840 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
fe24c6e5 RW |
841 | } |
842 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &eba_orphans, u.list) { | |
fe24c6e5 | 843 | list_del(&tmp_aeb->u.list); |
5547fec7 | 844 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
fe24c6e5 RW |
845 | } |
846 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) { | |
fe24c6e5 | 847 | list_del(&tmp_aeb->u.list); |
5547fec7 | 848 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
fe24c6e5 RW |
849 | } |
850 | ||
dbb7d2a8 RW |
851 | return ret; |
852 | } | |
853 | ||
854 | /** | |
855 | * ubi_scan_fastmap - scan the fastmap. | |
856 | * @ubi: UBI device object | |
857 | * @ai: UBI attach info to be filled | |
858 | * @fm_anchor: The fastmap starts at this PEB | |
859 | * | |
860 | * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found, | |
861 | * UBI_BAD_FASTMAP if one was found but is not usable. | |
862 | * < 0 indicates an internal error. | |
863 | */ | |
864 | int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, | |
865 | int fm_anchor) | |
866 | { | |
867 | struct ubi_fm_sb *fmsb, *fmsb2; | |
868 | struct ubi_vid_hdr *vh; | |
869 | struct ubi_ec_hdr *ech; | |
870 | struct ubi_fastmap_layout *fm; | |
871 | int i, used_blocks, pnum, ret = 0; | |
872 | size_t fm_size; | |
873 | __be32 crc, tmp_crc; | |
874 | unsigned long long sqnum = 0; | |
875 | ||
876 | mutex_lock(&ubi->fm_mutex); | |
877 | memset(ubi->fm_buf, 0, ubi->fm_size); | |
878 | ||
879 | fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL); | |
880 | if (!fmsb) { | |
881 | ret = -ENOMEM; | |
882 | goto out; | |
883 | } | |
884 | ||
885 | fm = kzalloc(sizeof(*fm), GFP_KERNEL); | |
886 | if (!fm) { | |
887 | ret = -ENOMEM; | |
888 | kfree(fmsb); | |
889 | goto out; | |
890 | } | |
891 | ||
892 | ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb)); | |
893 | if (ret && ret != UBI_IO_BITFLIPS) | |
894 | goto free_fm_sb; | |
895 | else if (ret == UBI_IO_BITFLIPS) | |
896 | fm->to_be_tortured[0] = 1; | |
897 | ||
898 | if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) { | |
32608703 | 899 | ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
900 | be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC); |
901 | ret = UBI_BAD_FASTMAP; | |
902 | goto free_fm_sb; | |
903 | } | |
904 | ||
905 | if (fmsb->version != UBI_FM_FMT_VERSION) { | |
32608703 | 906 | ubi_err(ubi, "bad fastmap version: %i, expected: %i", |
dbb7d2a8 RW |
907 | fmsb->version, UBI_FM_FMT_VERSION); |
908 | ret = UBI_BAD_FASTMAP; | |
909 | goto free_fm_sb; | |
910 | } | |
911 | ||
912 | used_blocks = be32_to_cpu(fmsb->used_blocks); | |
913 | if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) { | |
32608703 TB |
914 | ubi_err(ubi, "number of fastmap blocks is invalid: %i", |
915 | used_blocks); | |
dbb7d2a8 RW |
916 | ret = UBI_BAD_FASTMAP; |
917 | goto free_fm_sb; | |
918 | } | |
919 | ||
920 | fm_size = ubi->leb_size * used_blocks; | |
921 | if (fm_size != ubi->fm_size) { | |
32608703 TB |
922 | ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", |
923 | fm_size, ubi->fm_size); | |
dbb7d2a8 RW |
924 | ret = UBI_BAD_FASTMAP; |
925 | goto free_fm_sb; | |
926 | } | |
927 | ||
928 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | |
929 | if (!ech) { | |
930 | ret = -ENOMEM; | |
931 | goto free_fm_sb; | |
932 | } | |
933 | ||
934 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); | |
935 | if (!vh) { | |
936 | ret = -ENOMEM; | |
937 | goto free_hdr; | |
938 | } | |
939 | ||
940 | for (i = 0; i < used_blocks; i++) { | |
c22301ad RG |
941 | int image_seq; |
942 | ||
dbb7d2a8 RW |
943 | pnum = be32_to_cpu(fmsb->block_loc[i]); |
944 | ||
945 | if (ubi_io_is_bad(ubi, pnum)) { | |
946 | ret = UBI_BAD_FASTMAP; | |
947 | goto free_hdr; | |
948 | } | |
949 | ||
950 | ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); | |
951 | if (ret && ret != UBI_IO_BITFLIPS) { | |
32608703 | 952 | ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", |
dbb7d2a8 RW |
953 | i, pnum); |
954 | if (ret > 0) | |
955 | ret = UBI_BAD_FASTMAP; | |
956 | goto free_hdr; | |
957 | } else if (ret == UBI_IO_BITFLIPS) | |
958 | fm->to_be_tortured[i] = 1; | |
959 | ||
c22301ad | 960 | image_seq = be32_to_cpu(ech->image_seq); |
dbb7d2a8 | 961 | if (!ubi->image_seq) |
c22301ad | 962 | ubi->image_seq = image_seq; |
dbb7d2a8 | 963 | |
c22301ad RG |
964 | /* |
965 | * Older UBI implementations have image_seq set to zero, so | |
966 | * we shouldn't fail if image_seq == 0. | |
967 | */ | |
968 | if (image_seq && (image_seq != ubi->image_seq)) { | |
32608703 | 969 | ubi_err(ubi, "wrong image seq:%d instead of %d", |
c22301ad | 970 | be32_to_cpu(ech->image_seq), ubi->image_seq); |
dbb7d2a8 RW |
971 | ret = UBI_BAD_FASTMAP; |
972 | goto free_hdr; | |
973 | } | |
974 | ||
975 | ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); | |
976 | if (ret && ret != UBI_IO_BITFLIPS) { | |
32608703 | 977 | ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", |
dbb7d2a8 RW |
978 | i, pnum); |
979 | goto free_hdr; | |
980 | } | |
981 | ||
982 | if (i == 0) { | |
983 | if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) { | |
32608703 | 984 | ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
985 | be32_to_cpu(vh->vol_id), |
986 | UBI_FM_SB_VOLUME_ID); | |
987 | ret = UBI_BAD_FASTMAP; | |
988 | goto free_hdr; | |
989 | } | |
990 | } else { | |
991 | if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) { | |
32608703 | 992 | ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", |
dbb7d2a8 RW |
993 | be32_to_cpu(vh->vol_id), |
994 | UBI_FM_DATA_VOLUME_ID); | |
995 | ret = UBI_BAD_FASTMAP; | |
996 | goto free_hdr; | |
997 | } | |
998 | } | |
999 | ||
1000 | if (sqnum < be64_to_cpu(vh->sqnum)) | |
1001 | sqnum = be64_to_cpu(vh->sqnum); | |
1002 | ||
1003 | ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum, | |
1004 | ubi->leb_start, ubi->leb_size); | |
1005 | if (ret && ret != UBI_IO_BITFLIPS) { | |
32608703 | 1006 | ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " |
dbb7d2a8 RW |
1007 | "err: %i)", i, pnum, ret); |
1008 | goto free_hdr; | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | kfree(fmsb); | |
1013 | fmsb = NULL; | |
1014 | ||
1015 | fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); | |
1016 | tmp_crc = be32_to_cpu(fmsb2->data_crc); | |
1017 | fmsb2->data_crc = 0; | |
1018 | crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); | |
1019 | if (crc != tmp_crc) { | |
32608703 TB |
1020 | ubi_err(ubi, "fastmap data CRC is invalid"); |
1021 | ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", | |
1022 | tmp_crc, crc); | |
dbb7d2a8 RW |
1023 | ret = UBI_BAD_FASTMAP; |
1024 | goto free_hdr; | |
1025 | } | |
1026 | ||
1027 | fmsb2->sqnum = sqnum; | |
1028 | ||
1029 | fm->used_blocks = used_blocks; | |
1030 | ||
1031 | ret = ubi_attach_fastmap(ubi, ai, fm); | |
1032 | if (ret) { | |
1033 | if (ret > 0) | |
1034 | ret = UBI_BAD_FASTMAP; | |
1035 | goto free_hdr; | |
1036 | } | |
1037 | ||
1038 | for (i = 0; i < used_blocks; i++) { | |
1039 | struct ubi_wl_entry *e; | |
1040 | ||
1041 | e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); | |
1042 | if (!e) { | |
1043 | while (i--) | |
1044 | kfree(fm->e[i]); | |
1045 | ||
1046 | ret = -ENOMEM; | |
1047 | goto free_hdr; | |
1048 | } | |
1049 | ||
1050 | e->pnum = be32_to_cpu(fmsb2->block_loc[i]); | |
1051 | e->ec = be32_to_cpu(fmsb2->block_ec[i]); | |
1052 | fm->e[i] = e; | |
1053 | } | |
1054 | ||
1055 | ubi->fm = fm; | |
1056 | ubi->fm_pool.max_size = ubi->fm->max_pool_size; | |
1057 | ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; | |
32608703 TB |
1058 | ubi_msg(ubi, "attached by fastmap"); |
1059 | ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); | |
1060 | ubi_msg(ubi, "fastmap WL pool size: %d", | |
1061 | ubi->fm_wl_pool.max_size); | |
dbb7d2a8 RW |
1062 | ubi->fm_disabled = 0; |
1063 | ||
1064 | ubi_free_vid_hdr(ubi, vh); | |
1065 | kfree(ech); | |
1066 | out: | |
1067 | mutex_unlock(&ubi->fm_mutex); | |
1068 | if (ret == UBI_BAD_FASTMAP) | |
32608703 | 1069 | ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); |
dbb7d2a8 RW |
1070 | return ret; |
1071 | ||
1072 | free_hdr: | |
1073 | ubi_free_vid_hdr(ubi, vh); | |
1074 | kfree(ech); | |
1075 | free_fm_sb: | |
1076 | kfree(fmsb); | |
1077 | kfree(fm); | |
1078 | goto out; | |
1079 | } | |
1080 | ||
1081 | /** | |
1082 | * ubi_write_fastmap - writes a fastmap. | |
1083 | * @ubi: UBI device object | |
1084 | * @new_fm: the to be written fastmap | |
1085 | * | |
1086 | * Returns 0 on success, < 0 indicates an internal error. | |
1087 | */ | |
1088 | static int ubi_write_fastmap(struct ubi_device *ubi, | |
1089 | struct ubi_fastmap_layout *new_fm) | |
1090 | { | |
1091 | size_t fm_pos = 0; | |
1092 | void *fm_raw; | |
1093 | struct ubi_fm_sb *fmsb; | |
1094 | struct ubi_fm_hdr *fmh; | |
1095 | struct ubi_fm_scan_pool *fmpl1, *fmpl2; | |
1096 | struct ubi_fm_ec *fec; | |
1097 | struct ubi_fm_volhdr *fvh; | |
1098 | struct ubi_fm_eba *feba; | |
1099 | struct rb_node *node; | |
1100 | struct ubi_wl_entry *wl_e; | |
1101 | struct ubi_volume *vol; | |
1102 | struct ubi_vid_hdr *avhdr, *dvhdr; | |
1103 | struct ubi_work *ubi_wrk; | |
1104 | int ret, i, j, free_peb_count, used_peb_count, vol_count; | |
1105 | int scrub_peb_count, erase_peb_count; | |
1106 | ||
1107 | fm_raw = ubi->fm_buf; | |
1108 | memset(ubi->fm_buf, 0, ubi->fm_size); | |
1109 | ||
1110 | avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); | |
1111 | if (!avhdr) { | |
1112 | ret = -ENOMEM; | |
1113 | goto out; | |
1114 | } | |
1115 | ||
1116 | dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID); | |
1117 | if (!dvhdr) { | |
1118 | ret = -ENOMEM; | |
1119 | goto out_kfree; | |
1120 | } | |
1121 | ||
1122 | spin_lock(&ubi->volumes_lock); | |
1123 | spin_lock(&ubi->wl_lock); | |
1124 | ||
1125 | fmsb = (struct ubi_fm_sb *)fm_raw; | |
1126 | fm_pos += sizeof(*fmsb); | |
1127 | ubi_assert(fm_pos <= ubi->fm_size); | |
1128 | ||
1129 | fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos); | |
1130 | fm_pos += sizeof(*fmh); | |
1131 | ubi_assert(fm_pos <= ubi->fm_size); | |
1132 | ||
1133 | fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC); | |
1134 | fmsb->version = UBI_FM_FMT_VERSION; | |
1135 | fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks); | |
1136 | /* the max sqnum will be filled in while *reading* the fastmap */ | |
1137 | fmsb->sqnum = 0; | |
1138 | ||
1139 | fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC); | |
1140 | free_peb_count = 0; | |
1141 | used_peb_count = 0; | |
1142 | scrub_peb_count = 0; | |
1143 | erase_peb_count = 0; | |
1144 | vol_count = 0; | |
1145 | ||
1146 | fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); | |
1147 | fm_pos += sizeof(*fmpl1); | |
1148 | fmpl1->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); | |
1149 | fmpl1->size = cpu_to_be16(ubi->fm_pool.size); | |
1150 | fmpl1->max_size = cpu_to_be16(ubi->fm_pool.max_size); | |
1151 | ||
1152 | for (i = 0; i < ubi->fm_pool.size; i++) | |
1153 | fmpl1->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); | |
1154 | ||
1155 | fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); | |
1156 | fm_pos += sizeof(*fmpl2); | |
1157 | fmpl2->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); | |
1158 | fmpl2->size = cpu_to_be16(ubi->fm_wl_pool.size); | |
1159 | fmpl2->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); | |
1160 | ||
1161 | for (i = 0; i < ubi->fm_wl_pool.size; i++) | |
1162 | fmpl2->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); | |
1163 | ||
1164 | for (node = rb_first(&ubi->free); node; node = rb_next(node)) { | |
1165 | wl_e = rb_entry(node, struct ubi_wl_entry, u.rb); | |
1166 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
1167 | ||
1168 | fec->pnum = cpu_to_be32(wl_e->pnum); | |
1169 | fec->ec = cpu_to_be32(wl_e->ec); | |
1170 | ||
1171 | free_peb_count++; | |
1172 | fm_pos += sizeof(*fec); | |
1173 | ubi_assert(fm_pos <= ubi->fm_size); | |
1174 | } | |
1175 | fmh->free_peb_count = cpu_to_be32(free_peb_count); | |
1176 | ||
1177 | for (node = rb_first(&ubi->used); node; node = rb_next(node)) { | |
1178 | wl_e = rb_entry(node, struct ubi_wl_entry, u.rb); | |
1179 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
1180 | ||
1181 | fec->pnum = cpu_to_be32(wl_e->pnum); | |
1182 | fec->ec = cpu_to_be32(wl_e->ec); | |
1183 | ||
1184 | used_peb_count++; | |
1185 | fm_pos += sizeof(*fec); | |
1186 | ubi_assert(fm_pos <= ubi->fm_size); | |
1187 | } | |
4f5e3b6f RW |
1188 | |
1189 | for (i = 0; i < UBI_PROT_QUEUE_LEN; i++) { | |
1190 | list_for_each_entry(wl_e, &ubi->pq[i], u.list) { | |
1191 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
1192 | ||
1193 | fec->pnum = cpu_to_be32(wl_e->pnum); | |
1194 | fec->ec = cpu_to_be32(wl_e->ec); | |
1195 | ||
1196 | used_peb_count++; | |
1197 | fm_pos += sizeof(*fec); | |
1198 | ubi_assert(fm_pos <= ubi->fm_size); | |
1199 | } | |
1200 | } | |
dbb7d2a8 RW |
1201 | fmh->used_peb_count = cpu_to_be32(used_peb_count); |
1202 | ||
1203 | for (node = rb_first(&ubi->scrub); node; node = rb_next(node)) { | |
1204 | wl_e = rb_entry(node, struct ubi_wl_entry, u.rb); | |
1205 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
1206 | ||
1207 | fec->pnum = cpu_to_be32(wl_e->pnum); | |
1208 | fec->ec = cpu_to_be32(wl_e->ec); | |
1209 | ||
1210 | scrub_peb_count++; | |
1211 | fm_pos += sizeof(*fec); | |
1212 | ubi_assert(fm_pos <= ubi->fm_size); | |
1213 | } | |
1214 | fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count); | |
1215 | ||
1216 | ||
1217 | list_for_each_entry(ubi_wrk, &ubi->works, list) { | |
1218 | if (ubi_is_erase_work(ubi_wrk)) { | |
1219 | wl_e = ubi_wrk->e; | |
1220 | ubi_assert(wl_e); | |
1221 | ||
1222 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); | |
1223 | ||
1224 | fec->pnum = cpu_to_be32(wl_e->pnum); | |
1225 | fec->ec = cpu_to_be32(wl_e->ec); | |
1226 | ||
1227 | erase_peb_count++; | |
1228 | fm_pos += sizeof(*fec); | |
1229 | ubi_assert(fm_pos <= ubi->fm_size); | |
1230 | } | |
1231 | } | |
1232 | fmh->erase_peb_count = cpu_to_be32(erase_peb_count); | |
1233 | ||
1234 | for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) { | |
1235 | vol = ubi->volumes[i]; | |
1236 | ||
1237 | if (!vol) | |
1238 | continue; | |
1239 | ||
1240 | vol_count++; | |
1241 | ||
1242 | fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); | |
1243 | fm_pos += sizeof(*fvh); | |
1244 | ubi_assert(fm_pos <= ubi->fm_size); | |
1245 | ||
1246 | fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC); | |
1247 | fvh->vol_id = cpu_to_be32(vol->vol_id); | |
1248 | fvh->vol_type = vol->vol_type; | |
1249 | fvh->used_ebs = cpu_to_be32(vol->used_ebs); | |
1250 | fvh->data_pad = cpu_to_be32(vol->data_pad); | |
1251 | fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes); | |
1252 | ||
1253 | ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME || | |
1254 | vol->vol_type == UBI_STATIC_VOLUME); | |
1255 | ||
1256 | feba = (struct ubi_fm_eba *)(fm_raw + fm_pos); | |
1257 | fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs); | |
1258 | ubi_assert(fm_pos <= ubi->fm_size); | |
1259 | ||
1260 | for (j = 0; j < vol->reserved_pebs; j++) | |
1261 | feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]); | |
1262 | ||
1263 | feba->reserved_pebs = cpu_to_be32(j); | |
1264 | feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC); | |
1265 | } | |
1266 | fmh->vol_count = cpu_to_be32(vol_count); | |
1267 | fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); | |
1268 | ||
1269 | avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); | |
1270 | avhdr->lnum = 0; | |
1271 | ||
1272 | spin_unlock(&ubi->wl_lock); | |
1273 | spin_unlock(&ubi->volumes_lock); | |
1274 | ||
1275 | dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); | |
1276 | ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); | |
1277 | if (ret) { | |
32608703 | 1278 | ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); |
dbb7d2a8 RW |
1279 | goto out_kfree; |
1280 | } | |
1281 | ||
1282 | for (i = 0; i < new_fm->used_blocks; i++) { | |
1283 | fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum); | |
1284 | fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec); | |
1285 | } | |
1286 | ||
1287 | fmsb->data_crc = 0; | |
1288 | fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw, | |
1289 | ubi->fm_size)); | |
1290 | ||
1291 | for (i = 1; i < new_fm->used_blocks; i++) { | |
1292 | dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); | |
1293 | dvhdr->lnum = cpu_to_be32(i); | |
1294 | dbg_bld("writing fastmap data to PEB %i sqnum %llu", | |
1295 | new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); | |
1296 | ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); | |
1297 | if (ret) { | |
32608703 | 1298 | ubi_err(ubi, "unable to write vid_hdr to PEB %i!", |
dbb7d2a8 RW |
1299 | new_fm->e[i]->pnum); |
1300 | goto out_kfree; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | for (i = 0; i < new_fm->used_blocks; i++) { | |
1305 | ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size), | |
1306 | new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size); | |
1307 | if (ret) { | |
32608703 | 1308 | ubi_err(ubi, "unable to write fastmap to PEB %i!", |
dbb7d2a8 RW |
1309 | new_fm->e[i]->pnum); |
1310 | goto out_kfree; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | ubi_assert(new_fm); | |
1315 | ubi->fm = new_fm; | |
1316 | ||
1317 | dbg_bld("fastmap written!"); | |
1318 | ||
1319 | out_kfree: | |
1320 | ubi_free_vid_hdr(ubi, avhdr); | |
1321 | ubi_free_vid_hdr(ubi, dvhdr); | |
1322 | out: | |
1323 | return ret; | |
1324 | } | |
1325 | ||
1326 | /** | |
1327 | * erase_block - Manually erase a PEB. | |
1328 | * @ubi: UBI device object | |
1329 | * @pnum: PEB to be erased | |
1330 | * | |
1331 | * Returns the new EC value on success, < 0 indicates an internal error. | |
1332 | */ | |
1333 | static int erase_block(struct ubi_device *ubi, int pnum) | |
1334 | { | |
1335 | int ret; | |
1336 | struct ubi_ec_hdr *ec_hdr; | |
1337 | long long ec; | |
1338 | ||
1339 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); | |
1340 | if (!ec_hdr) | |
1341 | return -ENOMEM; | |
1342 | ||
1343 | ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); | |
1344 | if (ret < 0) | |
1345 | goto out; | |
1346 | else if (ret && ret != UBI_IO_BITFLIPS) { | |
1347 | ret = -EINVAL; | |
1348 | goto out; | |
1349 | } | |
1350 | ||
1351 | ret = ubi_io_sync_erase(ubi, pnum, 0); | |
1352 | if (ret < 0) | |
1353 | goto out; | |
1354 | ||
1355 | ec = be64_to_cpu(ec_hdr->ec); | |
1356 | ec += ret; | |
1357 | if (ec > UBI_MAX_ERASECOUNTER) { | |
1358 | ret = -EINVAL; | |
1359 | goto out; | |
1360 | } | |
1361 | ||
1362 | ec_hdr->ec = cpu_to_be64(ec); | |
1363 | ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); | |
1364 | if (ret < 0) | |
1365 | goto out; | |
1366 | ||
1367 | ret = ec; | |
1368 | out: | |
1369 | kfree(ec_hdr); | |
1370 | return ret; | |
1371 | } | |
1372 | ||
1373 | /** | |
1374 | * invalidate_fastmap - destroys a fastmap. | |
1375 | * @ubi: UBI device object | |
1376 | * @fm: the fastmap to be destroyed | |
1377 | * | |
1378 | * Returns 0 on success, < 0 indicates an internal error. | |
1379 | */ | |
1380 | static int invalidate_fastmap(struct ubi_device *ubi, | |
1381 | struct ubi_fastmap_layout *fm) | |
1382 | { | |
8930fa50 | 1383 | int ret; |
dbb7d2a8 RW |
1384 | struct ubi_vid_hdr *vh; |
1385 | ||
1386 | ret = erase_block(ubi, fm->e[0]->pnum); | |
1387 | if (ret < 0) | |
1388 | return ret; | |
1389 | ||
1390 | vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); | |
1391 | if (!vh) | |
1392 | return -ENOMEM; | |
1393 | ||
1394 | /* deleting the current fastmap SB is not enough, an old SB may exist, | |
1395 | * so create a (corrupted) SB such that fastmap will find it and fall | |
1396 | * back to scanning mode in any case */ | |
1397 | vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); | |
1398 | ret = ubi_io_write_vid_hdr(ubi, fm->e[0]->pnum, vh); | |
1399 | ||
dbb7d2a8 RW |
1400 | return ret; |
1401 | } | |
1402 | ||
1403 | /** | |
1404 | * ubi_update_fastmap - will be called by UBI if a volume changes or | |
1405 | * a fastmap pool becomes full. | |
1406 | * @ubi: UBI device object | |
1407 | * | |
1408 | * Returns 0 on success, < 0 indicates an internal error. | |
1409 | */ | |
1410 | int ubi_update_fastmap(struct ubi_device *ubi) | |
1411 | { | |
1412 | int ret, i; | |
1413 | struct ubi_fastmap_layout *new_fm, *old_fm; | |
1414 | struct ubi_wl_entry *tmp_e; | |
1415 | ||
1416 | mutex_lock(&ubi->fm_mutex); | |
1417 | ||
1418 | ubi_refill_pools(ubi); | |
1419 | ||
1420 | if (ubi->ro_mode || ubi->fm_disabled) { | |
1421 | mutex_unlock(&ubi->fm_mutex); | |
1422 | return 0; | |
1423 | } | |
1424 | ||
1425 | ret = ubi_ensure_anchor_pebs(ubi); | |
1426 | if (ret) { | |
1427 | mutex_unlock(&ubi->fm_mutex); | |
1428 | return ret; | |
1429 | } | |
1430 | ||
1431 | new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL); | |
1432 | if (!new_fm) { | |
1433 | mutex_unlock(&ubi->fm_mutex); | |
1434 | return -ENOMEM; | |
1435 | } | |
1436 | ||
1437 | new_fm->used_blocks = ubi->fm_size / ubi->leb_size; | |
dbb7d2a8 RW |
1438 | old_fm = ubi->fm; |
1439 | ubi->fm = NULL; | |
1440 | ||
1441 | if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) { | |
32608703 | 1442 | ubi_err(ubi, "fastmap too large"); |
dbb7d2a8 RW |
1443 | ret = -ENOSPC; |
1444 | goto err; | |
1445 | } | |
1446 | ||
1447 | for (i = 1; i < new_fm->used_blocks; i++) { | |
1448 | spin_lock(&ubi->wl_lock); | |
1449 | tmp_e = ubi_wl_get_fm_peb(ubi, 0); | |
1450 | spin_unlock(&ubi->wl_lock); | |
1451 | ||
1452 | if (!tmp_e && !old_fm) { | |
1453 | int j; | |
32608703 | 1454 | ubi_err(ubi, "could not get any free erase block"); |
dbb7d2a8 RW |
1455 | |
1456 | for (j = 1; j < i; j++) | |
1457 | ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); | |
1458 | ||
1459 | ret = -ENOSPC; | |
1460 | goto err; | |
1461 | } else if (!tmp_e && old_fm) { | |
1462 | ret = erase_block(ubi, old_fm->e[i]->pnum); | |
1463 | if (ret < 0) { | |
1464 | int j; | |
1465 | ||
1466 | for (j = 1; j < i; j++) | |
1467 | ubi_wl_put_fm_peb(ubi, new_fm->e[j], | |
1468 | j, 0); | |
1469 | ||
32608703 | 1470 | ubi_err(ubi, "could not erase old fastmap PEB"); |
dbb7d2a8 RW |
1471 | goto err; |
1472 | } | |
c4ca6be9 | 1473 | new_fm->e[i] = old_fm->e[i]; |
dbb7d2a8 | 1474 | } else { |
c4ca6be9 | 1475 | new_fm->e[i] = tmp_e; |
dbb7d2a8 RW |
1476 | |
1477 | if (old_fm) | |
1478 | ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, | |
1479 | old_fm->to_be_tortured[i]); | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | spin_lock(&ubi->wl_lock); | |
1484 | tmp_e = ubi_wl_get_fm_peb(ubi, 1); | |
1485 | spin_unlock(&ubi->wl_lock); | |
1486 | ||
1487 | if (old_fm) { | |
1488 | /* no fresh anchor PEB was found, reuse the old one */ | |
1489 | if (!tmp_e) { | |
1490 | ret = erase_block(ubi, old_fm->e[0]->pnum); | |
1491 | if (ret < 0) { | |
1492 | int i; | |
32608703 | 1493 | ubi_err(ubi, "could not erase old anchor PEB"); |
dbb7d2a8 RW |
1494 | |
1495 | for (i = 1; i < new_fm->used_blocks; i++) | |
1496 | ubi_wl_put_fm_peb(ubi, new_fm->e[i], | |
1497 | i, 0); | |
1498 | goto err; | |
1499 | } | |
c4ca6be9 | 1500 | new_fm->e[0] = old_fm->e[0]; |
dbb7d2a8 RW |
1501 | new_fm->e[0]->ec = ret; |
1502 | } else { | |
1503 | /* we've got a new anchor PEB, return the old one */ | |
1504 | ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, | |
1505 | old_fm->to_be_tortured[0]); | |
c4ca6be9 | 1506 | new_fm->e[0] = tmp_e; |
dbb7d2a8 RW |
1507 | } |
1508 | } else { | |
1509 | if (!tmp_e) { | |
1510 | int i; | |
32608703 | 1511 | ubi_err(ubi, "could not find any anchor PEB"); |
dbb7d2a8 RW |
1512 | |
1513 | for (i = 1; i < new_fm->used_blocks; i++) | |
1514 | ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); | |
1515 | ||
1516 | ret = -ENOSPC; | |
1517 | goto err; | |
1518 | } | |
c4ca6be9 | 1519 | new_fm->e[0] = tmp_e; |
dbb7d2a8 RW |
1520 | } |
1521 | ||
1522 | down_write(&ubi->work_sem); | |
1523 | down_write(&ubi->fm_sem); | |
1524 | ret = ubi_write_fastmap(ubi, new_fm); | |
1525 | up_write(&ubi->fm_sem); | |
1526 | up_write(&ubi->work_sem); | |
1527 | ||
1528 | if (ret) | |
1529 | goto err; | |
1530 | ||
1531 | out_unlock: | |
1532 | mutex_unlock(&ubi->fm_mutex); | |
1533 | kfree(old_fm); | |
1534 | return ret; | |
1535 | ||
1536 | err: | |
1537 | kfree(new_fm); | |
1538 | ||
32608703 | 1539 | ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); |
dbb7d2a8 RW |
1540 | |
1541 | ret = 0; | |
1542 | if (old_fm) { | |
1543 | ret = invalidate_fastmap(ubi, old_fm); | |
1544 | if (ret < 0) | |
32608703 | 1545 | ubi_err(ubi, "Unable to invalidiate current fastmap!"); |
dbb7d2a8 RW |
1546 | else if (ret) |
1547 | ret = 0; | |
1548 | } | |
1549 | goto out_unlock; | |
1550 | } |