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