Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 19 | */ |
| 20 | |
| 21 | #ifndef __UBI_SCAN_H__ |
| 22 | #define __UBI_SCAN_H__ |
| 23 | |
| 24 | /* The erase counter value for this physical eraseblock is unknown */ |
| 25 | #define UBI_SCAN_UNKNOWN_EC (-1) |
| 26 | |
| 27 | /** |
| 28 | * struct ubi_scan_leb - scanning information about a physical eraseblock. |
| 29 | * @ec: erase counter (%UBI_SCAN_UNKNOWN_EC if it is unknown) |
| 30 | * @pnum: physical eraseblock number |
| 31 | * @lnum: logical eraseblock number |
| 32 | * @scrub: if this physical eraseblock needs scrubbing |
Artem Bityutskiy | fb22b59 | 2010-10-19 22:00:11 +0300 | [diff] [blame] | 33 | * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 34 | * @sqnum: sequence number |
| 35 | * @u: unions RB-tree or @list links |
| 36 | * @u.rb: link in the per-volume RB-tree of &struct ubi_scan_leb objects |
| 37 | * @u.list: link in one of the eraseblock lists |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 38 | * |
| 39 | * One object of this type is allocated for each physical eraseblock during |
| 40 | * scanning. |
| 41 | */ |
| 42 | struct ubi_scan_leb { |
| 43 | int ec; |
| 44 | int pnum; |
| 45 | int lnum; |
Artem Bityutskiy | fb22b59 | 2010-10-19 22:00:11 +0300 | [diff] [blame] | 46 | unsigned int scrub:1; |
| 47 | unsigned int copy_flag:1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 48 | unsigned long long sqnum; |
| 49 | union { |
| 50 | struct rb_node rb; |
| 51 | struct list_head list; |
| 52 | } u; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * struct ubi_scan_volume - scanning information about a volume. |
| 57 | * @vol_id: volume ID |
| 58 | * @highest_lnum: highest logical eraseblock number in this volume |
| 59 | * @leb_count: number of logical eraseblocks in this volume |
| 60 | * @vol_type: volume type |
| 61 | * @used_ebs: number of used logical eraseblocks in this volume (only for |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 62 | * static volumes) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 63 | * @last_data_size: amount of data in the last logical eraseblock of this |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 64 | * volume (always equivalent to the usable logical eraseblock |
| 65 | * size in case of dynamic volumes) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 66 | * @data_pad: how many bytes at the end of logical eraseblocks of this volume |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 67 | * are not used (due to volume alignment) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 68 | * @compat: compatibility flags of this volume |
| 69 | * @rb: link in the volume RB-tree |
| 70 | * @root: root of the RB-tree containing all the eraseblock belonging to this |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 71 | * volume (&struct ubi_scan_leb objects) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 72 | * |
| 73 | * One object of this type is allocated for each volume during scanning. |
| 74 | */ |
| 75 | struct ubi_scan_volume { |
| 76 | int vol_id; |
| 77 | int highest_lnum; |
| 78 | int leb_count; |
| 79 | int vol_type; |
| 80 | int used_ebs; |
| 81 | int last_data_size; |
| 82 | int data_pad; |
| 83 | int compat; |
| 84 | struct rb_node rb; |
| 85 | struct rb_root root; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * struct ubi_scan_info - UBI scanning information. |
| 90 | * @volumes: root of the volume RB-tree |
| 91 | * @corr: list of corrupted physical eraseblocks |
| 92 | * @free: list of free physical eraseblocks |
| 93 | * @erase: list of physical eraseblocks which have to be erased |
| 94 | * @alien: list of physical eraseblocks which should not be used by UBI (e.g., |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 95 | * those belonging to "preserve"-compatible internal volumes) |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 96 | * @corr_peb_count: count of PEBs in the @corr list |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 97 | * @empty_peb_count: count of PEBs which are presumably empty (contain only |
| 98 | * 0xFF bytes) |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 99 | * @alien_peb_count: count of PEBs in the @alien list |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 100 | * @bad_peb_count: count of bad physical eraseblocks |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 101 | * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked |
| 102 | * as bad yet, but which look like bad |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 103 | * @vols_found: number of volumes found during scanning |
| 104 | * @highest_vol_id: highest volume ID |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 105 | * @is_empty: flag indicating whether the MTD device is empty or not |
| 106 | * @min_ec: lowest erase counter value |
| 107 | * @max_ec: highest erase counter value |
| 108 | * @max_sqnum: highest sequence number value |
| 109 | * @mean_ec: mean erase counter value |
| 110 | * @ec_sum: a temporary variable used when calculating @mean_ec |
| 111 | * @ec_count: a temporary variable used when calculating @mean_ec |
Artem Bityutskiy | 6c1e875 | 2010-10-31 17:54:14 +0200 | [diff] [blame] | 112 | * @scan_leb_slab: slab cache for &struct ubi_scan_leb objects |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 113 | * |
| 114 | * This data structure contains the result of scanning and may be used by other |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 115 | * UBI sub-systems to build final UBI data structures, further error-recovery |
| 116 | * and so on. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 117 | */ |
| 118 | struct ubi_scan_info { |
| 119 | struct rb_root volumes; |
| 120 | struct list_head corr; |
| 121 | struct list_head free; |
| 122 | struct list_head erase; |
| 123 | struct list_head alien; |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 124 | int corr_peb_count; |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 125 | int empty_peb_count; |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 126 | int alien_peb_count; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 127 | int bad_peb_count; |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 128 | int maybe_bad_peb_count; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 129 | int vols_found; |
| 130 | int highest_vol_id; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 131 | int is_empty; |
| 132 | int min_ec; |
| 133 | int max_ec; |
| 134 | unsigned long long max_sqnum; |
| 135 | int mean_ec; |
Artem Bityutskiy | 4bc1dca | 2008-04-19 20:44:31 +0300 | [diff] [blame] | 136 | uint64_t ec_sum; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 137 | int ec_count; |
Artem Bityutskiy | 6c1e875 | 2010-10-31 17:54:14 +0200 | [diff] [blame] | 138 | struct kmem_cache *scan_leb_slab; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | struct ubi_device; |
| 142 | struct ubi_vid_hdr; |
| 143 | |
| 144 | /* |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 145 | * ubi_scan_move_to_list - move a PEB from the volume tree to a list. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 146 | * |
| 147 | * @sv: volume scanning information |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 148 | * @seb: scanning eraseblock information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 149 | * @list: the list to move to |
| 150 | */ |
| 151 | static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv, |
| 152 | struct ubi_scan_leb *seb, |
| 153 | struct list_head *list) |
| 154 | { |
| 155 | rb_erase(&seb->u.rb, &sv->root); |
| 156 | list_add_tail(&seb->u.list, list); |
| 157 | } |
| 158 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 159 | int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 160 | int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, |
| 161 | int bitflips); |
| 162 | struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si, |
| 163 | int vol_id); |
| 164 | struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, |
| 165 | int lnum); |
| 166 | void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv); |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 167 | struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 168 | struct ubi_scan_info *si); |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 169 | int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si, |
| 170 | int pnum, int ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 171 | struct ubi_scan_info *ubi_scan(struct ubi_device *ubi); |
| 172 | void ubi_scan_destroy_si(struct ubi_scan_info *si); |
| 173 | |
| 174 | #endif /* !__UBI_SCAN_H__ */ |