Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of UBIFS. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: Adrian Hunter |
| 20 | * Artem Bityutskiy (Битюцкий Артём) |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This file implements the functions that access LEB properties and their |
| 25 | * categories. LEBs are categorized based on the needs of UBIFS, and the |
| 26 | * categories are stored as either heaps or lists to provide a fast way of |
| 27 | * finding a LEB in a particular category. For example, UBIFS may need to find |
| 28 | * an empty LEB for the journal, or a very dirty LEB for garbage collection. |
| 29 | */ |
| 30 | |
| 31 | #include "ubifs.h" |
| 32 | |
| 33 | /** |
| 34 | * get_heap_comp_val - get the LEB properties value for heap comparisons. |
| 35 | * @lprops: LEB properties |
| 36 | * @cat: LEB category |
| 37 | */ |
| 38 | static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat) |
| 39 | { |
| 40 | switch (cat) { |
| 41 | case LPROPS_FREE: |
| 42 | return lprops->free; |
| 43 | case LPROPS_DIRTY_IDX: |
| 44 | return lprops->free + lprops->dirty; |
| 45 | default: |
| 46 | return lprops->dirty; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * move_up_lpt_heap - move a new heap entry up as far as possible. |
| 52 | * @c: UBIFS file-system description object |
| 53 | * @heap: LEB category heap |
| 54 | * @lprops: LEB properties to move |
| 55 | * @cat: LEB category |
| 56 | * |
| 57 | * New entries to a heap are added at the bottom and then moved up until the |
| 58 | * parent's value is greater. In the case of LPT's category heaps, the value |
| 59 | * is either the amount of free space or the amount of dirty space, depending |
| 60 | * on the category. |
| 61 | */ |
| 62 | static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, |
| 63 | struct ubifs_lprops *lprops, int cat) |
| 64 | { |
| 65 | int val1, val2, hpos; |
| 66 | |
| 67 | hpos = lprops->hpos; |
| 68 | if (!hpos) |
| 69 | return; /* Already top of the heap */ |
| 70 | val1 = get_heap_comp_val(lprops, cat); |
| 71 | /* Compare to parent and, if greater, move up the heap */ |
| 72 | do { |
| 73 | int ppos = (hpos - 1) / 2; |
| 74 | |
| 75 | val2 = get_heap_comp_val(heap->arr[ppos], cat); |
| 76 | if (val2 >= val1) |
| 77 | return; |
| 78 | /* Greater than parent so move up */ |
| 79 | heap->arr[ppos]->hpos = hpos; |
| 80 | heap->arr[hpos] = heap->arr[ppos]; |
| 81 | heap->arr[ppos] = lprops; |
| 82 | lprops->hpos = ppos; |
| 83 | hpos = ppos; |
| 84 | } while (hpos); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * adjust_lpt_heap - move a changed heap entry up or down the heap. |
| 89 | * @c: UBIFS file-system description object |
| 90 | * @heap: LEB category heap |
| 91 | * @lprops: LEB properties to move |
| 92 | * @hpos: heap position of @lprops |
| 93 | * @cat: LEB category |
| 94 | * |
| 95 | * Changed entries in a heap are moved up or down until the parent's value is |
| 96 | * greater. In the case of LPT's category heaps, the value is either the amount |
| 97 | * of free space or the amount of dirty space, depending on the category. |
| 98 | */ |
| 99 | static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, |
| 100 | struct ubifs_lprops *lprops, int hpos, int cat) |
| 101 | { |
| 102 | int val1, val2, val3, cpos; |
| 103 | |
| 104 | val1 = get_heap_comp_val(lprops, cat); |
| 105 | /* Compare to parent and, if greater than parent, move up the heap */ |
| 106 | if (hpos) { |
| 107 | int ppos = (hpos - 1) / 2; |
| 108 | |
| 109 | val2 = get_heap_comp_val(heap->arr[ppos], cat); |
| 110 | if (val1 > val2) { |
| 111 | /* Greater than parent so move up */ |
| 112 | while (1) { |
| 113 | heap->arr[ppos]->hpos = hpos; |
| 114 | heap->arr[hpos] = heap->arr[ppos]; |
| 115 | heap->arr[ppos] = lprops; |
| 116 | lprops->hpos = ppos; |
| 117 | hpos = ppos; |
| 118 | if (!hpos) |
| 119 | return; |
| 120 | ppos = (hpos - 1) / 2; |
| 121 | val2 = get_heap_comp_val(heap->arr[ppos], cat); |
| 122 | if (val1 <= val2) |
| 123 | return; |
| 124 | /* Still greater than parent so keep going */ |
| 125 | } |
| 126 | } |
| 127 | } |
Artem Bityutskiy | 948cfb2 | 2008-08-20 11:56:33 +0300 | [diff] [blame] | 128 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 129 | /* Not greater than parent, so compare to children */ |
| 130 | while (1) { |
| 131 | /* Compare to left child */ |
| 132 | cpos = hpos * 2 + 1; |
| 133 | if (cpos >= heap->cnt) |
| 134 | return; |
| 135 | val2 = get_heap_comp_val(heap->arr[cpos], cat); |
| 136 | if (val1 < val2) { |
| 137 | /* Less than left child, so promote biggest child */ |
| 138 | if (cpos + 1 < heap->cnt) { |
| 139 | val3 = get_heap_comp_val(heap->arr[cpos + 1], |
| 140 | cat); |
| 141 | if (val3 > val2) |
| 142 | cpos += 1; /* Right child is bigger */ |
| 143 | } |
| 144 | heap->arr[cpos]->hpos = hpos; |
| 145 | heap->arr[hpos] = heap->arr[cpos]; |
| 146 | heap->arr[cpos] = lprops; |
| 147 | lprops->hpos = cpos; |
| 148 | hpos = cpos; |
| 149 | continue; |
| 150 | } |
| 151 | /* Compare to right child */ |
| 152 | cpos += 1; |
| 153 | if (cpos >= heap->cnt) |
| 154 | return; |
| 155 | val3 = get_heap_comp_val(heap->arr[cpos], cat); |
| 156 | if (val1 < val3) { |
| 157 | /* Less than right child, so promote right child */ |
| 158 | heap->arr[cpos]->hpos = hpos; |
| 159 | heap->arr[hpos] = heap->arr[cpos]; |
| 160 | heap->arr[cpos] = lprops; |
| 161 | lprops->hpos = cpos; |
| 162 | hpos = cpos; |
| 163 | continue; |
| 164 | } |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * add_to_lpt_heap - add LEB properties to a LEB category heap. |
| 171 | * @c: UBIFS file-system description object |
| 172 | * @lprops: LEB properties to add |
| 173 | * @cat: LEB category |
| 174 | * |
| 175 | * This function returns %1 if @lprops is added to the heap for LEB category |
| 176 | * @cat, otherwise %0 is returned because the heap is full. |
| 177 | */ |
| 178 | static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops, |
| 179 | int cat) |
| 180 | { |
| 181 | struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1]; |
| 182 | |
| 183 | if (heap->cnt >= heap->max_cnt) { |
| 184 | const int b = LPT_HEAP_SZ / 2 - 1; |
| 185 | int cpos, val1, val2; |
| 186 | |
| 187 | /* Compare to some other LEB on the bottom of heap */ |
| 188 | /* Pick a position kind of randomly */ |
| 189 | cpos = (((size_t)lprops >> 4) & b) + b; |
| 190 | ubifs_assert(cpos >= b); |
| 191 | ubifs_assert(cpos < LPT_HEAP_SZ); |
| 192 | ubifs_assert(cpos < heap->cnt); |
| 193 | |
| 194 | val1 = get_heap_comp_val(lprops, cat); |
| 195 | val2 = get_heap_comp_val(heap->arr[cpos], cat); |
| 196 | if (val1 > val2) { |
| 197 | struct ubifs_lprops *lp; |
| 198 | |
| 199 | lp = heap->arr[cpos]; |
| 200 | lp->flags &= ~LPROPS_CAT_MASK; |
| 201 | lp->flags |= LPROPS_UNCAT; |
| 202 | list_add(&lp->list, &c->uncat_list); |
| 203 | lprops->hpos = cpos; |
| 204 | heap->arr[cpos] = lprops; |
| 205 | move_up_lpt_heap(c, heap, lprops, cat); |
| 206 | dbg_check_heap(c, heap, cat, lprops->hpos); |
| 207 | return 1; /* Added to heap */ |
| 208 | } |
| 209 | dbg_check_heap(c, heap, cat, -1); |
| 210 | return 0; /* Not added to heap */ |
| 211 | } else { |
| 212 | lprops->hpos = heap->cnt++; |
| 213 | heap->arr[lprops->hpos] = lprops; |
| 214 | move_up_lpt_heap(c, heap, lprops, cat); |
| 215 | dbg_check_heap(c, heap, cat, lprops->hpos); |
| 216 | return 1; /* Added to heap */ |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * remove_from_lpt_heap - remove LEB properties from a LEB category heap. |
| 222 | * @c: UBIFS file-system description object |
| 223 | * @lprops: LEB properties to remove |
| 224 | * @cat: LEB category |
| 225 | */ |
| 226 | static void remove_from_lpt_heap(struct ubifs_info *c, |
| 227 | struct ubifs_lprops *lprops, int cat) |
| 228 | { |
| 229 | struct ubifs_lpt_heap *heap; |
| 230 | int hpos = lprops->hpos; |
| 231 | |
| 232 | heap = &c->lpt_heap[cat - 1]; |
| 233 | ubifs_assert(hpos >= 0 && hpos < heap->cnt); |
| 234 | ubifs_assert(heap->arr[hpos] == lprops); |
| 235 | heap->cnt -= 1; |
| 236 | if (hpos < heap->cnt) { |
| 237 | heap->arr[hpos] = heap->arr[heap->cnt]; |
| 238 | heap->arr[hpos]->hpos = hpos; |
| 239 | adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat); |
| 240 | } |
| 241 | dbg_check_heap(c, heap, cat, -1); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * lpt_heap_replace - replace lprops in a category heap. |
| 246 | * @c: UBIFS file-system description object |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 247 | * @new_lprops: LEB properties with which to replace |
| 248 | * @cat: LEB category |
| 249 | * |
| 250 | * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode) |
| 251 | * and the lprops that the pnode contains. When that happens, references in |
| 252 | * the category heaps to those lprops must be updated to point to the new |
| 253 | * lprops. This function does that. |
| 254 | */ |
| 255 | static void lpt_heap_replace(struct ubifs_info *c, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 256 | struct ubifs_lprops *new_lprops, int cat) |
| 257 | { |
| 258 | struct ubifs_lpt_heap *heap; |
| 259 | int hpos = new_lprops->hpos; |
| 260 | |
| 261 | heap = &c->lpt_heap[cat - 1]; |
| 262 | heap->arr[hpos] = new_lprops; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * ubifs_add_to_cat - add LEB properties to a category list or heap. |
| 267 | * @c: UBIFS file-system description object |
| 268 | * @lprops: LEB properties to add |
| 269 | * @cat: LEB category to which to add |
| 270 | * |
| 271 | * LEB properties are categorized to enable fast find operations. |
| 272 | */ |
| 273 | void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops, |
| 274 | int cat) |
| 275 | { |
| 276 | switch (cat) { |
| 277 | case LPROPS_DIRTY: |
| 278 | case LPROPS_DIRTY_IDX: |
| 279 | case LPROPS_FREE: |
| 280 | if (add_to_lpt_heap(c, lprops, cat)) |
| 281 | break; |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 282 | /* No more room on heap so make it un-categorized */ |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 283 | cat = LPROPS_UNCAT; |
| 284 | /* Fall through */ |
| 285 | case LPROPS_UNCAT: |
| 286 | list_add(&lprops->list, &c->uncat_list); |
| 287 | break; |
| 288 | case LPROPS_EMPTY: |
| 289 | list_add(&lprops->list, &c->empty_list); |
| 290 | break; |
| 291 | case LPROPS_FREEABLE: |
| 292 | list_add(&lprops->list, &c->freeable_list); |
| 293 | c->freeable_cnt += 1; |
| 294 | break; |
| 295 | case LPROPS_FRDI_IDX: |
| 296 | list_add(&lprops->list, &c->frdi_idx_list); |
| 297 | break; |
| 298 | default: |
| 299 | ubifs_assert(0); |
| 300 | } |
Artem Bityutskiy | 98a1eeb | 2012-10-10 10:55:28 +0300 | [diff] [blame] | 301 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 302 | lprops->flags &= ~LPROPS_CAT_MASK; |
| 303 | lprops->flags |= cat; |
Artem Bityutskiy | 98a1eeb | 2012-10-10 10:55:28 +0300 | [diff] [blame] | 304 | c->in_a_category_cnt += 1; |
| 305 | ubifs_assert(c->in_a_category_cnt <= c->main_lebs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
| 309 | * ubifs_remove_from_cat - remove LEB properties from a category list or heap. |
| 310 | * @c: UBIFS file-system description object |
| 311 | * @lprops: LEB properties to remove |
| 312 | * @cat: LEB category from which to remove |
| 313 | * |
| 314 | * LEB properties are categorized to enable fast find operations. |
| 315 | */ |
| 316 | static void ubifs_remove_from_cat(struct ubifs_info *c, |
| 317 | struct ubifs_lprops *lprops, int cat) |
| 318 | { |
| 319 | switch (cat) { |
| 320 | case LPROPS_DIRTY: |
| 321 | case LPROPS_DIRTY_IDX: |
| 322 | case LPROPS_FREE: |
| 323 | remove_from_lpt_heap(c, lprops, cat); |
| 324 | break; |
| 325 | case LPROPS_FREEABLE: |
| 326 | c->freeable_cnt -= 1; |
| 327 | ubifs_assert(c->freeable_cnt >= 0); |
| 328 | /* Fall through */ |
| 329 | case LPROPS_UNCAT: |
| 330 | case LPROPS_EMPTY: |
| 331 | case LPROPS_FRDI_IDX: |
| 332 | ubifs_assert(!list_empty(&lprops->list)); |
| 333 | list_del(&lprops->list); |
| 334 | break; |
| 335 | default: |
| 336 | ubifs_assert(0); |
| 337 | } |
Artem Bityutskiy | 98a1eeb | 2012-10-10 10:55:28 +0300 | [diff] [blame] | 338 | |
| 339 | c->in_a_category_cnt -= 1; |
| 340 | ubifs_assert(c->in_a_category_cnt >= 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
| 344 | * ubifs_replace_cat - replace lprops in a category list or heap. |
| 345 | * @c: UBIFS file-system description object |
| 346 | * @old_lprops: LEB properties to replace |
| 347 | * @new_lprops: LEB properties with which to replace |
| 348 | * |
| 349 | * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode) |
| 350 | * and the lprops that the pnode contains. When that happens, references in |
| 351 | * category lists and heaps must be replaced. This function does that. |
| 352 | */ |
| 353 | void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops, |
| 354 | struct ubifs_lprops *new_lprops) |
| 355 | { |
| 356 | int cat; |
| 357 | |
| 358 | cat = new_lprops->flags & LPROPS_CAT_MASK; |
| 359 | switch (cat) { |
| 360 | case LPROPS_DIRTY: |
| 361 | case LPROPS_DIRTY_IDX: |
| 362 | case LPROPS_FREE: |
Jiang Biao | 4fb1cd8 | 2018-02-28 10:22:37 +0800 | [diff] [blame] | 363 | lpt_heap_replace(c, new_lprops, cat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 364 | break; |
| 365 | case LPROPS_UNCAT: |
| 366 | case LPROPS_EMPTY: |
| 367 | case LPROPS_FREEABLE: |
| 368 | case LPROPS_FRDI_IDX: |
| 369 | list_replace(&old_lprops->list, &new_lprops->list); |
| 370 | break; |
| 371 | default: |
| 372 | ubifs_assert(0); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * ubifs_ensure_cat - ensure LEB properties are categorized. |
| 378 | * @c: UBIFS file-system description object |
| 379 | * @lprops: LEB properties |
| 380 | * |
| 381 | * A LEB may have fallen off of the bottom of a heap, and ended up as |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 382 | * un-categorized even though it has enough space for us now. If that is the |
| 383 | * case this function will put the LEB back onto a heap. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 384 | */ |
| 385 | void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops) |
| 386 | { |
| 387 | int cat = lprops->flags & LPROPS_CAT_MASK; |
| 388 | |
| 389 | if (cat != LPROPS_UNCAT) |
| 390 | return; |
| 391 | cat = ubifs_categorize_lprops(c, lprops); |
| 392 | if (cat == LPROPS_UNCAT) |
| 393 | return; |
| 394 | ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT); |
| 395 | ubifs_add_to_cat(c, lprops, cat); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * ubifs_categorize_lprops - categorize LEB properties. |
| 400 | * @c: UBIFS file-system description object |
| 401 | * @lprops: LEB properties to categorize |
| 402 | * |
| 403 | * LEB properties are categorized to enable fast find operations. This function |
| 404 | * returns the LEB category to which the LEB properties belong. Note however |
| 405 | * that if the LEB category is stored as a heap and the heap is full, the |
| 406 | * LEB properties may have their category changed to %LPROPS_UNCAT. |
| 407 | */ |
| 408 | int ubifs_categorize_lprops(const struct ubifs_info *c, |
| 409 | const struct ubifs_lprops *lprops) |
| 410 | { |
| 411 | if (lprops->flags & LPROPS_TAKEN) |
| 412 | return LPROPS_UNCAT; |
| 413 | |
| 414 | if (lprops->free == c->leb_size) { |
| 415 | ubifs_assert(!(lprops->flags & LPROPS_INDEX)); |
| 416 | return LPROPS_EMPTY; |
| 417 | } |
| 418 | |
| 419 | if (lprops->free + lprops->dirty == c->leb_size) { |
| 420 | if (lprops->flags & LPROPS_INDEX) |
| 421 | return LPROPS_FRDI_IDX; |
| 422 | else |
| 423 | return LPROPS_FREEABLE; |
| 424 | } |
| 425 | |
| 426 | if (lprops->flags & LPROPS_INDEX) { |
| 427 | if (lprops->dirty + lprops->free >= c->min_idx_node_sz) |
| 428 | return LPROPS_DIRTY_IDX; |
| 429 | } else { |
| 430 | if (lprops->dirty >= c->dead_wm && |
| 431 | lprops->dirty > lprops->free) |
| 432 | return LPROPS_DIRTY; |
| 433 | if (lprops->free > 0) |
| 434 | return LPROPS_FREE; |
| 435 | } |
| 436 | |
| 437 | return LPROPS_UNCAT; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * change_category - change LEB properties category. |
| 442 | * @c: UBIFS file-system description object |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 443 | * @lprops: LEB properties to re-categorize |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 444 | * |
| 445 | * LEB properties are categorized to enable fast find operations. When the LEB |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 446 | * properties change they must be re-categorized. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 447 | */ |
| 448 | static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops) |
| 449 | { |
| 450 | int old_cat = lprops->flags & LPROPS_CAT_MASK; |
| 451 | int new_cat = ubifs_categorize_lprops(c, lprops); |
| 452 | |
| 453 | if (old_cat == new_cat) { |
Dan Carpenter | 273946a | 2012-04-01 18:57:59 +0300 | [diff] [blame] | 454 | struct ubifs_lpt_heap *heap; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 455 | |
| 456 | /* lprops on a heap now must be moved up or down */ |
| 457 | if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT) |
| 458 | return; /* Not on a heap */ |
| 459 | heap = &c->lpt_heap[new_cat - 1]; |
| 460 | adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat); |
| 461 | } else { |
| 462 | ubifs_remove_from_cat(c, lprops, old_cat); |
| 463 | ubifs_add_to_cat(c, lprops, new_cat); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 468 | * ubifs_calc_dark - calculate LEB dark space size. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 469 | * @c: the UBIFS file-system description object |
| 470 | * @spc: amount of free and dirty space in the LEB |
| 471 | * |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 472 | * This function calculates and returns amount of dark space in an LEB which |
| 473 | * has @spc bytes of free and dirty space. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 474 | * |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 475 | * UBIFS is trying to account the space which might not be usable, and this |
| 476 | * space is called "dark space". For example, if an LEB has only %512 free |
| 477 | * bytes, it is dark space, because it cannot fit a large data node. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 478 | */ |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 479 | int ubifs_calc_dark(const struct ubifs_info *c, int spc) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 480 | { |
| 481 | ubifs_assert(!(spc & 7)); |
| 482 | |
| 483 | if (spc < c->dark_wm) |
| 484 | return spc; |
| 485 | |
| 486 | /* |
| 487 | * If we have slightly more space then the dark space watermark, we can |
| 488 | * anyway safely assume it we'll be able to write a node of the |
| 489 | * smallest size there. |
| 490 | */ |
| 491 | if (spc - c->dark_wm < MIN_WRITE_SZ) |
| 492 | return spc - MIN_WRITE_SZ; |
| 493 | |
| 494 | return c->dark_wm; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * is_lprops_dirty - determine if LEB properties are dirty. |
| 499 | * @c: the UBIFS file-system description object |
| 500 | * @lprops: LEB properties to test |
| 501 | */ |
| 502 | static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops) |
| 503 | { |
| 504 | struct ubifs_pnode *pnode; |
| 505 | int pos; |
| 506 | |
| 507 | pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1); |
| 508 | pnode = (struct ubifs_pnode *)container_of(lprops - pos, |
| 509 | struct ubifs_pnode, |
| 510 | lprops[0]); |
Artem Bityutskiy | 3766244 | 2011-05-30 14:51:20 +0300 | [diff] [blame] | 511 | return !test_bit(COW_CNODE, &pnode->flags) && |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 512 | test_bit(DIRTY_CNODE, &pnode->flags); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * ubifs_change_lp - change LEB properties. |
| 517 | * @c: the UBIFS file-system description object |
| 518 | * @lp: LEB properties to change |
| 519 | * @free: new free space amount |
| 520 | * @dirty: new dirty space amount |
| 521 | * @flags: new flags |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 522 | * @idx_gc_cnt: change to the count of @idx_gc list |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 523 | * |
Artem Bityutskiy | d3cf502 | 2008-12-16 17:52:35 +0200 | [diff] [blame] | 524 | * This function changes LEB properties (@free, @dirty or @flag). However, the |
| 525 | * property which has the %LPROPS_NC value is not changed. Returns a pointer to |
| 526 | * the updated LEB properties on success and a negative error code on failure. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 527 | * |
Artem Bityutskiy | d3cf502 | 2008-12-16 17:52:35 +0200 | [diff] [blame] | 528 | * Note, the LEB properties may have had to be copied (due to COW) and |
| 529 | * consequently the pointer returned may not be the same as the pointer |
| 530 | * passed. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 531 | */ |
| 532 | const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c, |
| 533 | const struct ubifs_lprops *lp, |
| 534 | int free, int dirty, int flags, |
| 535 | int idx_gc_cnt) |
| 536 | { |
| 537 | /* |
| 538 | * This is the only function that is allowed to change lprops, so we |
Artem Bityutskiy | 055da1b | 2009-09-15 17:09:24 +0300 | [diff] [blame] | 539 | * discard the "const" qualifier. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 540 | */ |
| 541 | struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp; |
| 542 | |
| 543 | dbg_lp("LEB %d, free %d, dirty %d, flags %d", |
| 544 | lprops->lnum, free, dirty, flags); |
| 545 | |
| 546 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 547 | ubifs_assert(c->lst.empty_lebs >= 0 && |
| 548 | c->lst.empty_lebs <= c->main_lebs); |
| 549 | ubifs_assert(c->freeable_cnt >= 0); |
| 550 | ubifs_assert(c->freeable_cnt <= c->main_lebs); |
| 551 | ubifs_assert(c->lst.taken_empty_lebs >= 0); |
| 552 | ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs); |
| 553 | ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7)); |
| 554 | ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7)); |
| 555 | ubifs_assert(!(c->lst.total_used & 7)); |
| 556 | ubifs_assert(free == LPROPS_NC || free >= 0); |
| 557 | ubifs_assert(dirty == LPROPS_NC || dirty >= 0); |
| 558 | |
| 559 | if (!is_lprops_dirty(c, lprops)) { |
| 560 | lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum); |
| 561 | if (IS_ERR(lprops)) |
| 562 | return lprops; |
| 563 | } else |
| 564 | ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum)); |
| 565 | |
| 566 | ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7)); |
| 567 | |
| 568 | spin_lock(&c->space_lock); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 569 | if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size) |
| 570 | c->lst.taken_empty_lebs -= 1; |
| 571 | |
| 572 | if (!(lprops->flags & LPROPS_INDEX)) { |
| 573 | int old_spc; |
| 574 | |
| 575 | old_spc = lprops->free + lprops->dirty; |
| 576 | if (old_spc < c->dead_wm) |
| 577 | c->lst.total_dead -= old_spc; |
| 578 | else |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 579 | c->lst.total_dark -= ubifs_calc_dark(c, old_spc); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 580 | |
| 581 | c->lst.total_used -= c->leb_size - old_spc; |
| 582 | } |
| 583 | |
| 584 | if (free != LPROPS_NC) { |
| 585 | free = ALIGN(free, 8); |
| 586 | c->lst.total_free += free - lprops->free; |
| 587 | |
| 588 | /* Increase or decrease empty LEBs counter if needed */ |
| 589 | if (free == c->leb_size) { |
| 590 | if (lprops->free != c->leb_size) |
| 591 | c->lst.empty_lebs += 1; |
| 592 | } else if (lprops->free == c->leb_size) |
| 593 | c->lst.empty_lebs -= 1; |
| 594 | lprops->free = free; |
| 595 | } |
| 596 | |
| 597 | if (dirty != LPROPS_NC) { |
| 598 | dirty = ALIGN(dirty, 8); |
| 599 | c->lst.total_dirty += dirty - lprops->dirty; |
| 600 | lprops->dirty = dirty; |
| 601 | } |
| 602 | |
| 603 | if (flags != LPROPS_NC) { |
| 604 | /* Take care about indexing LEBs counter if needed */ |
| 605 | if ((lprops->flags & LPROPS_INDEX)) { |
| 606 | if (!(flags & LPROPS_INDEX)) |
| 607 | c->lst.idx_lebs -= 1; |
| 608 | } else if (flags & LPROPS_INDEX) |
| 609 | c->lst.idx_lebs += 1; |
| 610 | lprops->flags = flags; |
| 611 | } |
| 612 | |
| 613 | if (!(lprops->flags & LPROPS_INDEX)) { |
| 614 | int new_spc; |
| 615 | |
| 616 | new_spc = lprops->free + lprops->dirty; |
| 617 | if (new_spc < c->dead_wm) |
| 618 | c->lst.total_dead += new_spc; |
| 619 | else |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 620 | c->lst.total_dark += ubifs_calc_dark(c, new_spc); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 621 | |
| 622 | c->lst.total_used += c->leb_size - new_spc; |
| 623 | } |
| 624 | |
| 625 | if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size) |
| 626 | c->lst.taken_empty_lebs += 1; |
| 627 | |
| 628 | change_category(c, lprops); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 629 | c->idx_gc_cnt += idx_gc_cnt; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 630 | spin_unlock(&c->space_lock); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 631 | return lprops; |
| 632 | } |
| 633 | |
| 634 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 635 | * ubifs_get_lp_stats - get lprops statistics. |
| 636 | * @c: UBIFS file-system description object |
Julia Lawall | ec037df | 2016-10-01 22:53:11 +0200 | [diff] [blame] | 637 | * @lst: return statistics |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 638 | */ |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 639 | void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 640 | { |
| 641 | spin_lock(&c->space_lock); |
Artem Bityutskiy | 84abf97 | 2009-01-23 14:54:59 +0200 | [diff] [blame] | 642 | memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 643 | spin_unlock(&c->space_lock); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * ubifs_change_one_lp - change LEB properties. |
| 648 | * @c: the UBIFS file-system description object |
| 649 | * @lnum: LEB to change properties for |
| 650 | * @free: amount of free space |
| 651 | * @dirty: amount of dirty space |
| 652 | * @flags_set: flags to set |
| 653 | * @flags_clean: flags to clean |
| 654 | * @idx_gc_cnt: change to the count of idx_gc list |
| 655 | * |
| 656 | * This function changes properties of LEB @lnum. It is a helper wrapper over |
| 657 | * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the |
| 658 | * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and |
| 659 | * a negative error code in case of failure. |
| 660 | */ |
| 661 | int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty, |
| 662 | int flags_set, int flags_clean, int idx_gc_cnt) |
| 663 | { |
| 664 | int err = 0, flags; |
| 665 | const struct ubifs_lprops *lp; |
| 666 | |
| 667 | ubifs_get_lprops(c); |
| 668 | |
| 669 | lp = ubifs_lpt_lookup_dirty(c, lnum); |
| 670 | if (IS_ERR(lp)) { |
| 671 | err = PTR_ERR(lp); |
| 672 | goto out; |
| 673 | } |
| 674 | |
| 675 | flags = (lp->flags | flags_set) & ~flags_clean; |
| 676 | lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt); |
| 677 | if (IS_ERR(lp)) |
| 678 | err = PTR_ERR(lp); |
| 679 | |
| 680 | out: |
| 681 | ubifs_release_lprops(c); |
Artem Bityutskiy | e4d9b6c | 2009-01-23 14:17:36 +0200 | [diff] [blame] | 682 | if (err) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 683 | ubifs_err(c, "cannot change properties of LEB %d, error %d", |
Artem Bityutskiy | e4d9b6c | 2009-01-23 14:17:36 +0200 | [diff] [blame] | 684 | lnum, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 685 | return err; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * ubifs_update_one_lp - update LEB properties. |
| 690 | * @c: the UBIFS file-system description object |
| 691 | * @lnum: LEB to change properties for |
| 692 | * @free: amount of free space |
| 693 | * @dirty: amount of dirty space to add |
| 694 | * @flags_set: flags to set |
| 695 | * @flags_clean: flags to clean |
| 696 | * |
| 697 | * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to |
| 698 | * current dirty space, not substitutes it. |
| 699 | */ |
| 700 | int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty, |
| 701 | int flags_set, int flags_clean) |
| 702 | { |
| 703 | int err = 0, flags; |
| 704 | const struct ubifs_lprops *lp; |
| 705 | |
| 706 | ubifs_get_lprops(c); |
| 707 | |
| 708 | lp = ubifs_lpt_lookup_dirty(c, lnum); |
| 709 | if (IS_ERR(lp)) { |
| 710 | err = PTR_ERR(lp); |
| 711 | goto out; |
| 712 | } |
| 713 | |
| 714 | flags = (lp->flags | flags_set) & ~flags_clean; |
| 715 | lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0); |
| 716 | if (IS_ERR(lp)) |
| 717 | err = PTR_ERR(lp); |
| 718 | |
| 719 | out: |
| 720 | ubifs_release_lprops(c); |
Artem Bityutskiy | e4d9b6c | 2009-01-23 14:17:36 +0200 | [diff] [blame] | 721 | if (err) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 722 | ubifs_err(c, "cannot update properties of LEB %d, error %d", |
Artem Bityutskiy | e4d9b6c | 2009-01-23 14:17:36 +0200 | [diff] [blame] | 723 | lnum, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 724 | return err; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * ubifs_read_one_lp - read LEB properties. |
| 729 | * @c: the UBIFS file-system description object |
| 730 | * @lnum: LEB to read properties for |
| 731 | * @lp: where to store read properties |
| 732 | * |
| 733 | * This helper function reads properties of a LEB @lnum and stores them in @lp. |
| 734 | * Returns zero in case of success and a negative error code in case of |
| 735 | * failure. |
| 736 | */ |
| 737 | int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp) |
| 738 | { |
| 739 | int err = 0; |
| 740 | const struct ubifs_lprops *lpp; |
| 741 | |
| 742 | ubifs_get_lprops(c); |
| 743 | |
| 744 | lpp = ubifs_lpt_lookup(c, lnum); |
| 745 | if (IS_ERR(lpp)) { |
| 746 | err = PTR_ERR(lpp); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 747 | ubifs_err(c, "cannot read properties of LEB %d, error %d", |
Artem Bityutskiy | e4d9b6c | 2009-01-23 14:17:36 +0200 | [diff] [blame] | 748 | lnum, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 749 | goto out; |
| 750 | } |
| 751 | |
| 752 | memcpy(lp, lpp, sizeof(struct ubifs_lprops)); |
| 753 | |
| 754 | out: |
| 755 | ubifs_release_lprops(c); |
| 756 | return err; |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * ubifs_fast_find_free - try to find a LEB with free space quickly. |
| 761 | * @c: the UBIFS file-system description object |
| 762 | * |
| 763 | * This function returns LEB properties for a LEB with free space or %NULL if |
| 764 | * the function is unable to find a LEB quickly. |
| 765 | */ |
| 766 | const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c) |
| 767 | { |
| 768 | struct ubifs_lprops *lprops; |
| 769 | struct ubifs_lpt_heap *heap; |
| 770 | |
| 771 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 772 | |
| 773 | heap = &c->lpt_heap[LPROPS_FREE - 1]; |
| 774 | if (heap->cnt == 0) |
| 775 | return NULL; |
| 776 | |
| 777 | lprops = heap->arr[0]; |
| 778 | ubifs_assert(!(lprops->flags & LPROPS_TAKEN)); |
| 779 | ubifs_assert(!(lprops->flags & LPROPS_INDEX)); |
| 780 | return lprops; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * ubifs_fast_find_empty - try to find an empty LEB quickly. |
| 785 | * @c: the UBIFS file-system description object |
| 786 | * |
| 787 | * This function returns LEB properties for an empty LEB or %NULL if the |
| 788 | * function is unable to find an empty LEB quickly. |
| 789 | */ |
| 790 | const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c) |
| 791 | { |
| 792 | struct ubifs_lprops *lprops; |
| 793 | |
| 794 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 795 | |
| 796 | if (list_empty(&c->empty_list)) |
| 797 | return NULL; |
| 798 | |
| 799 | lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list); |
| 800 | ubifs_assert(!(lprops->flags & LPROPS_TAKEN)); |
| 801 | ubifs_assert(!(lprops->flags & LPROPS_INDEX)); |
| 802 | ubifs_assert(lprops->free == c->leb_size); |
| 803 | return lprops; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * ubifs_fast_find_freeable - try to find a freeable LEB quickly. |
| 808 | * @c: the UBIFS file-system description object |
| 809 | * |
| 810 | * This function returns LEB properties for a freeable LEB or %NULL if the |
| 811 | * function is unable to find a freeable LEB quickly. |
| 812 | */ |
| 813 | const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c) |
| 814 | { |
| 815 | struct ubifs_lprops *lprops; |
| 816 | |
| 817 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 818 | |
| 819 | if (list_empty(&c->freeable_list)) |
| 820 | return NULL; |
| 821 | |
| 822 | lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list); |
| 823 | ubifs_assert(!(lprops->flags & LPROPS_TAKEN)); |
| 824 | ubifs_assert(!(lprops->flags & LPROPS_INDEX)); |
| 825 | ubifs_assert(lprops->free + lprops->dirty == c->leb_size); |
| 826 | ubifs_assert(c->freeable_cnt > 0); |
| 827 | return lprops; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly. |
| 832 | * @c: the UBIFS file-system description object |
| 833 | * |
| 834 | * This function returns LEB properties for a freeable index LEB or %NULL if the |
| 835 | * function is unable to find a freeable index LEB quickly. |
| 836 | */ |
| 837 | const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c) |
| 838 | { |
| 839 | struct ubifs_lprops *lprops; |
| 840 | |
| 841 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 842 | |
| 843 | if (list_empty(&c->frdi_idx_list)) |
| 844 | return NULL; |
| 845 | |
| 846 | lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list); |
| 847 | ubifs_assert(!(lprops->flags & LPROPS_TAKEN)); |
| 848 | ubifs_assert((lprops->flags & LPROPS_INDEX)); |
| 849 | ubifs_assert(lprops->free + lprops->dirty == c->leb_size); |
| 850 | return lprops; |
| 851 | } |
| 852 | |
Artem Bityutskiy | f70b7e5 | 2012-05-16 19:53:46 +0300 | [diff] [blame] | 853 | /* |
| 854 | * Everything below is related to debugging. |
| 855 | */ |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 856 | |
| 857 | /** |
| 858 | * dbg_check_cats - check category heaps and lists. |
| 859 | * @c: UBIFS file-system description object |
| 860 | * |
| 861 | * This function returns %0 on success and a negative error code on failure. |
| 862 | */ |
| 863 | int dbg_check_cats(struct ubifs_info *c) |
| 864 | { |
| 865 | struct ubifs_lprops *lprops; |
| 866 | struct list_head *pos; |
| 867 | int i, cat; |
| 868 | |
Artem Bityutskiy | 2b1844a | 2011-06-03 08:31:29 +0300 | [diff] [blame] | 869 | if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 870 | return 0; |
| 871 | |
| 872 | list_for_each_entry(lprops, &c->empty_list, list) { |
| 873 | if (lprops->free != c->leb_size) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 874 | ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 875 | lprops->lnum, lprops->free, lprops->dirty, |
| 876 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 877 | return -EINVAL; |
| 878 | } |
| 879 | if (lprops->flags & LPROPS_TAKEN) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 880 | ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 881 | lprops->lnum, lprops->free, lprops->dirty, |
| 882 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 883 | return -EINVAL; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | i = 0; |
| 888 | list_for_each_entry(lprops, &c->freeable_list, list) { |
| 889 | if (lprops->free + lprops->dirty != c->leb_size) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 890 | ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 891 | lprops->lnum, lprops->free, lprops->dirty, |
| 892 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 893 | return -EINVAL; |
| 894 | } |
| 895 | if (lprops->flags & LPROPS_TAKEN) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 896 | ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 897 | lprops->lnum, lprops->free, lprops->dirty, |
| 898 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 899 | return -EINVAL; |
| 900 | } |
| 901 | i += 1; |
| 902 | } |
| 903 | if (i != c->freeable_cnt) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 904 | ubifs_err(c, "freeable list count %d expected %d", i, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 905 | c->freeable_cnt); |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | i = 0; |
| 910 | list_for_each(pos, &c->idx_gc) |
| 911 | i += 1; |
| 912 | if (i != c->idx_gc_cnt) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 913 | ubifs_err(c, "idx_gc list count %d expected %d", i, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 914 | c->idx_gc_cnt); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | |
| 918 | list_for_each_entry(lprops, &c->frdi_idx_list, list) { |
| 919 | if (lprops->free + lprops->dirty != c->leb_size) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 920 | ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 921 | lprops->lnum, lprops->free, lprops->dirty, |
| 922 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 923 | return -EINVAL; |
| 924 | } |
| 925 | if (lprops->flags & LPROPS_TAKEN) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 926 | ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 927 | lprops->lnum, lprops->free, lprops->dirty, |
| 928 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 929 | return -EINVAL; |
| 930 | } |
| 931 | if (!(lprops->flags & LPROPS_INDEX)) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 932 | ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 933 | lprops->lnum, lprops->free, lprops->dirty, |
| 934 | lprops->flags); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 935 | return -EINVAL; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) { |
| 940 | struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1]; |
| 941 | |
| 942 | for (i = 0; i < heap->cnt; i++) { |
| 943 | lprops = heap->arr[i]; |
| 944 | if (!lprops) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 945 | ubifs_err(c, "null ptr in LPT heap cat %d", cat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 946 | return -EINVAL; |
| 947 | } |
| 948 | if (lprops->hpos != i) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 949 | ubifs_err(c, "bad ptr in LPT heap cat %d", cat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 950 | return -EINVAL; |
| 951 | } |
| 952 | if (lprops->flags & LPROPS_TAKEN) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 953 | ubifs_err(c, "taken LEB in LPT heap cat %d", cat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 954 | return -EINVAL; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat, |
| 963 | int add_pos) |
| 964 | { |
| 965 | int i = 0, j, err = 0; |
| 966 | |
Artem Bityutskiy | 2b1844a | 2011-06-03 08:31:29 +0300 | [diff] [blame] | 967 | if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 968 | return; |
| 969 | |
| 970 | for (i = 0; i < heap->cnt; i++) { |
| 971 | struct ubifs_lprops *lprops = heap->arr[i]; |
| 972 | struct ubifs_lprops *lp; |
| 973 | |
| 974 | if (i != add_pos) |
| 975 | if ((lprops->flags & LPROPS_CAT_MASK) != cat) { |
| 976 | err = 1; |
| 977 | goto out; |
| 978 | } |
| 979 | if (lprops->hpos != i) { |
| 980 | err = 2; |
| 981 | goto out; |
| 982 | } |
| 983 | lp = ubifs_lpt_lookup(c, lprops->lnum); |
| 984 | if (IS_ERR(lp)) { |
| 985 | err = 3; |
| 986 | goto out; |
| 987 | } |
| 988 | if (lprops != lp) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 989 | ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d", |
Artem Bityutskiy | 3668b70 | 2012-08-27 16:56:58 +0300 | [diff] [blame] | 990 | (size_t)lprops, (size_t)lp, lprops->lnum, |
| 991 | lp->lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 992 | err = 4; |
| 993 | goto out; |
| 994 | } |
| 995 | for (j = 0; j < i; j++) { |
| 996 | lp = heap->arr[j]; |
| 997 | if (lp == lprops) { |
| 998 | err = 5; |
| 999 | goto out; |
| 1000 | } |
| 1001 | if (lp->lnum == lprops->lnum) { |
| 1002 | err = 6; |
| 1003 | goto out; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | out: |
| 1008 | if (err) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1009 | ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err); |
Artem Bityutskiy | 7c46d0a | 2012-05-16 19:04:54 +0300 | [diff] [blame] | 1010 | dump_stack(); |
Artem Bityutskiy | edf6be2 | 2012-05-16 19:15:56 +0300 | [diff] [blame] | 1011 | ubifs_dump_heap(c, heap, cat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1016 | * scan_check_cb - scan callback. |
| 1017 | * @c: the UBIFS file-system description object |
| 1018 | * @lp: LEB properties to scan |
| 1019 | * @in_tree: whether the LEB properties are in main memory |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1020 | * @lst: lprops statistics to update |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1021 | * |
| 1022 | * This function returns a code that indicates whether the scan should continue |
| 1023 | * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree |
| 1024 | * in main memory (%LPT_SCAN_ADD), or whether the scan should stop |
| 1025 | * (%LPT_SCAN_STOP). |
| 1026 | */ |
| 1027 | static int scan_check_cb(struct ubifs_info *c, |
| 1028 | const struct ubifs_lprops *lp, int in_tree, |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1029 | struct ubifs_lp_stats *lst) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1030 | { |
| 1031 | struct ubifs_scan_leb *sleb; |
| 1032 | struct ubifs_scan_node *snod; |
Artem Bityutskiy | cd5f748 | 2011-03-11 15:50:37 +0200 | [diff] [blame] | 1033 | int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret; |
| 1034 | void *buf = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1035 | |
| 1036 | cat = lp->flags & LPROPS_CAT_MASK; |
| 1037 | if (cat != LPROPS_UNCAT) { |
| 1038 | cat = ubifs_categorize_lprops(c, lp); |
| 1039 | if (cat != (lp->flags & LPROPS_CAT_MASK)) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1040 | ubifs_err(c, "bad LEB category %d expected %d", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1041 | (lp->flags & LPROPS_CAT_MASK), cat); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1042 | return -EINVAL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /* Check lp is on its category list (if it has one) */ |
| 1047 | if (in_tree) { |
| 1048 | struct list_head *list = NULL; |
| 1049 | |
| 1050 | switch (cat) { |
| 1051 | case LPROPS_EMPTY: |
| 1052 | list = &c->empty_list; |
| 1053 | break; |
| 1054 | case LPROPS_FREEABLE: |
| 1055 | list = &c->freeable_list; |
| 1056 | break; |
| 1057 | case LPROPS_FRDI_IDX: |
| 1058 | list = &c->frdi_idx_list; |
| 1059 | break; |
| 1060 | case LPROPS_UNCAT: |
| 1061 | list = &c->uncat_list; |
| 1062 | break; |
| 1063 | } |
| 1064 | if (list) { |
| 1065 | struct ubifs_lprops *lprops; |
| 1066 | int found = 0; |
| 1067 | |
| 1068 | list_for_each_entry(lprops, list, list) { |
| 1069 | if (lprops == lp) { |
| 1070 | found = 1; |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | if (!found) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1075 | ubifs_err(c, "bad LPT list (category %d)", cat); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1076 | return -EINVAL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* Check lp is on its category heap (if it has one) */ |
| 1082 | if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) { |
| 1083 | struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1]; |
| 1084 | |
| 1085 | if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) || |
| 1086 | lp != heap->arr[lp->hpos]) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1087 | ubifs_err(c, "bad LPT heap (category %d)", cat); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1088 | return -EINVAL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
Artem Bityutskiy | 8ca5175 | 2011-05-06 20:41:48 +0300 | [diff] [blame] | 1092 | /* |
| 1093 | * After an unclean unmount, empty and freeable LEBs |
| 1094 | * may contain garbage - do not scan them. |
| 1095 | */ |
| 1096 | if (lp->free == c->leb_size) { |
| 1097 | lst->empty_lebs += 1; |
| 1098 | lst->total_free += c->leb_size; |
| 1099 | lst->total_dark += ubifs_calc_dark(c, c->leb_size); |
| 1100 | return LPT_SCAN_CONTINUE; |
| 1101 | } |
| 1102 | if (lp->free + lp->dirty == c->leb_size && |
| 1103 | !(lp->flags & LPROPS_INDEX)) { |
| 1104 | lst->total_free += lp->free; |
| 1105 | lst->total_dirty += lp->dirty; |
| 1106 | lst->total_dark += ubifs_calc_dark(c, c->leb_size); |
| 1107 | return LPT_SCAN_CONTINUE; |
| 1108 | } |
| 1109 | |
Richard Weinberger | eef1981 | 2018-06-12 20:49:45 +0200 | [diff] [blame] | 1110 | buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); |
| 1111 | if (!buf) |
| 1112 | return -ENOMEM; |
| 1113 | |
Artem Bityutskiy | cd5f748 | 2011-03-11 15:50:37 +0200 | [diff] [blame] | 1114 | sleb = ubifs_scan(c, lnum, 0, buf, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1115 | if (IS_ERR(sleb)) { |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1116 | ret = PTR_ERR(sleb); |
Artem Bityutskiy | 1234603 | 2011-05-06 21:23:25 +0300 | [diff] [blame] | 1117 | if (ret == -EUCLEAN) { |
Artem Bityutskiy | edf6be2 | 2012-05-16 19:15:56 +0300 | [diff] [blame] | 1118 | ubifs_dump_lprops(c); |
| 1119 | ubifs_dump_budg(c, &c->bi); |
Artem Bityutskiy | 1234603 | 2011-05-06 21:23:25 +0300 | [diff] [blame] | 1120 | } |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1121 | goto out; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | is_idx = -1; |
| 1125 | list_for_each_entry(snod, &sleb->nodes, list) { |
| 1126 | int found, level = 0; |
| 1127 | |
| 1128 | cond_resched(); |
| 1129 | |
| 1130 | if (is_idx == -1) |
| 1131 | is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0; |
| 1132 | |
| 1133 | if (is_idx && snod->type != UBIFS_IDX_NODE) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1134 | ubifs_err(c, "indexing node in data LEB %d:%d", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1135 | lnum, snod->offs); |
| 1136 | goto out_destroy; |
| 1137 | } |
| 1138 | |
| 1139 | if (snod->type == UBIFS_IDX_NODE) { |
| 1140 | struct ubifs_idx_node *idx = snod->node; |
| 1141 | |
| 1142 | key_read(c, ubifs_idx_key(c, idx), &snod->key); |
| 1143 | level = le16_to_cpu(idx->level); |
| 1144 | } |
| 1145 | |
| 1146 | found = ubifs_tnc_has_node(c, &snod->key, level, lnum, |
| 1147 | snod->offs, is_idx); |
| 1148 | if (found) { |
| 1149 | if (found < 0) |
| 1150 | goto out_destroy; |
| 1151 | used += ALIGN(snod->len, 8); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | free = c->leb_size - sleb->endpt; |
| 1156 | dirty = sleb->endpt - used; |
| 1157 | |
| 1158 | if (free > c->leb_size || free < 0 || dirty > c->leb_size || |
| 1159 | dirty < 0) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1160 | ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 1161 | lnum, free, dirty); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1162 | goto out_destroy; |
| 1163 | } |
| 1164 | |
| 1165 | if (lp->free + lp->dirty == c->leb_size && |
| 1166 | free + dirty == c->leb_size) |
| 1167 | if ((is_idx && !(lp->flags & LPROPS_INDEX)) || |
| 1168 | (!is_idx && free == c->leb_size) || |
| 1169 | lp->free == c->leb_size) { |
| 1170 | /* |
| 1171 | * Empty or freeable LEBs could contain index |
| 1172 | * nodes from an uncompleted commit due to an |
| 1173 | * unclean unmount. Or they could be empty for |
| 1174 | * the same reason. Or it may simply not have been |
| 1175 | * unmapped. |
| 1176 | */ |
| 1177 | free = lp->free; |
| 1178 | dirty = lp->dirty; |
| 1179 | is_idx = 0; |
| 1180 | } |
| 1181 | |
| 1182 | if (is_idx && lp->free + lp->dirty == free + dirty && |
| 1183 | lnum != c->ihead_lnum) { |
| 1184 | /* |
| 1185 | * After an unclean unmount, an index LEB could have a different |
| 1186 | * amount of free space than the value recorded by lprops. That |
| 1187 | * is because the in-the-gaps method may use free space or |
| 1188 | * create free space (as a side-effect of using ubi_leb_change |
| 1189 | * and not writing the whole LEB). The incorrect free space |
| 1190 | * value is not a problem because the index is only ever |
| 1191 | * allocated empty LEBs, so there will never be an attempt to |
| 1192 | * write to the free space at the end of an index LEB - except |
| 1193 | * by the in-the-gaps method for which it is not a problem. |
| 1194 | */ |
| 1195 | free = lp->free; |
| 1196 | dirty = lp->dirty; |
| 1197 | } |
| 1198 | |
| 1199 | if (lp->free != free || lp->dirty != dirty) |
| 1200 | goto out_print; |
| 1201 | |
| 1202 | if (is_idx && !(lp->flags & LPROPS_INDEX)) { |
| 1203 | if (free == c->leb_size) |
| 1204 | /* Free but not unmapped LEB, it's fine */ |
| 1205 | is_idx = 0; |
| 1206 | else { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1207 | ubifs_err(c, "indexing node without indexing flag"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1208 | goto out_print; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | if (!is_idx && (lp->flags & LPROPS_INDEX)) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1213 | ubifs_err(c, "data node with indexing flag"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1214 | goto out_print; |
| 1215 | } |
| 1216 | |
| 1217 | if (free == c->leb_size) |
| 1218 | lst->empty_lebs += 1; |
| 1219 | |
| 1220 | if (is_idx) |
| 1221 | lst->idx_lebs += 1; |
| 1222 | |
| 1223 | if (!(lp->flags & LPROPS_INDEX)) |
| 1224 | lst->total_used += c->leb_size - free - dirty; |
| 1225 | lst->total_free += free; |
| 1226 | lst->total_dirty += dirty; |
| 1227 | |
| 1228 | if (!(lp->flags & LPROPS_INDEX)) { |
| 1229 | int spc = free + dirty; |
| 1230 | |
| 1231 | if (spc < c->dead_wm) |
| 1232 | lst->total_dead += spc; |
| 1233 | else |
Artem Bityutskiy | be9e62a | 2008-12-28 10:17:23 +0200 | [diff] [blame] | 1234 | lst->total_dark += ubifs_calc_dark(c, spc); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | ubifs_scan_destroy(sleb); |
Artem Bityutskiy | cd5f748 | 2011-03-11 15:50:37 +0200 | [diff] [blame] | 1238 | vfree(buf); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1239 | return LPT_SCAN_CONTINUE; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1240 | |
| 1241 | out_print: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1242 | ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1243 | lnum, lp->free, lp->dirty, lp->flags, free, dirty); |
Artem Bityutskiy | edf6be2 | 2012-05-16 19:15:56 +0300 | [diff] [blame] | 1244 | ubifs_dump_leb(c, lnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1245 | out_destroy: |
| 1246 | ubifs_scan_destroy(sleb); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1247 | ret = -EINVAL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1248 | out: |
Artem Bityutskiy | cd5f748 | 2011-03-11 15:50:37 +0200 | [diff] [blame] | 1249 | vfree(buf); |
Artem Bityutskiy | dcc50c8e | 2011-05-06 20:47:14 +0300 | [diff] [blame] | 1250 | return ret; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * dbg_check_lprops - check all LEB properties. |
| 1255 | * @c: UBIFS file-system description object |
| 1256 | * |
| 1257 | * This function checks all LEB properties and makes sure they are all correct. |
| 1258 | * It returns zero if everything is fine, %-EINVAL if there is an inconsistency |
| 1259 | * and other negative error codes in case of other errors. This function is |
| 1260 | * called while the file system is locked (because of commit start), so no |
| 1261 | * additional locking is required. Note that locking the LPT mutex would cause |
| 1262 | * a circular lock dependency with the TNC mutex. |
| 1263 | */ |
| 1264 | int dbg_check_lprops(struct ubifs_info *c) |
| 1265 | { |
| 1266 | int i, err; |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1267 | struct ubifs_lp_stats lst; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1268 | |
Artem Bityutskiy | 2b1844a | 2011-06-03 08:31:29 +0300 | [diff] [blame] | 1269 | if (!dbg_is_chk_lprops(c)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1270 | return 0; |
| 1271 | |
| 1272 | /* |
| 1273 | * As we are going to scan the media, the write buffers have to be |
| 1274 | * synchronized. |
| 1275 | */ |
| 1276 | for (i = 0; i < c->jhead_cnt; i++) { |
| 1277 | err = ubifs_wbuf_sync(&c->jheads[i].wbuf); |
| 1278 | if (err) |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1282 | memset(&lst, 0, sizeof(struct ubifs_lp_stats)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1283 | err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1, |
| 1284 | (ubifs_lpt_scan_callback)scan_check_cb, |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1285 | &lst); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1286 | if (err && err != -ENOSPC) |
| 1287 | goto out; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1288 | |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1289 | if (lst.empty_lebs != c->lst.empty_lebs || |
| 1290 | lst.idx_lebs != c->lst.idx_lebs || |
| 1291 | lst.total_free != c->lst.total_free || |
| 1292 | lst.total_dirty != c->lst.total_dirty || |
| 1293 | lst.total_used != c->lst.total_used) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1294 | ubifs_err(c, "bad overall accounting"); |
| 1295 | ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld", |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1296 | lst.empty_lebs, lst.idx_lebs, lst.total_free, |
| 1297 | lst.total_dirty, lst.total_used); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1298 | ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1299 | c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free, |
| 1300 | c->lst.total_dirty, c->lst.total_used); |
| 1301 | err = -EINVAL; |
| 1302 | goto out; |
| 1303 | } |
| 1304 | |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1305 | if (lst.total_dead != c->lst.total_dead || |
| 1306 | lst.total_dark != c->lst.total_dark) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1307 | ubifs_err(c, "bad dead/dark space accounting"); |
| 1308 | ubifs_err(c, "calculated: total_dead %lld, total_dark %lld", |
Artem Bityutskiy | 34bdc3e | 2011-05-06 20:58:08 +0300 | [diff] [blame] | 1309 | lst.total_dead, lst.total_dark); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1310 | ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1311 | c->lst.total_dead, c->lst.total_dark); |
| 1312 | err = -EINVAL; |
| 1313 | goto out; |
| 1314 | } |
| 1315 | |
| 1316 | err = dbg_check_cats(c); |
| 1317 | out: |
| 1318 | return err; |
| 1319 | } |