blob: 3659b887743b2c27da2a740eb39c6f97a980d6d3 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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 */
38static 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 */
62static 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 */
99static 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 Bityutskiy948cfb22008-08-20 11:56:33 +0300128
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300129 /* 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 */
178static 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 */
226static 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
247 * @old_lprops: LEB properties to replace
248 * @new_lprops: LEB properties with which to replace
249 * @cat: LEB category
250 *
251 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
252 * and the lprops that the pnode contains. When that happens, references in
253 * the category heaps to those lprops must be updated to point to the new
254 * lprops. This function does that.
255 */
256static void lpt_heap_replace(struct ubifs_info *c,
257 struct ubifs_lprops *old_lprops,
258 struct ubifs_lprops *new_lprops, int cat)
259{
260 struct ubifs_lpt_heap *heap;
261 int hpos = new_lprops->hpos;
262
263 heap = &c->lpt_heap[cat - 1];
264 heap->arr[hpos] = new_lprops;
265}
266
267/**
268 * ubifs_add_to_cat - add LEB properties to a category list or heap.
269 * @c: UBIFS file-system description object
270 * @lprops: LEB properties to add
271 * @cat: LEB category to which to add
272 *
273 * LEB properties are categorized to enable fast find operations.
274 */
275void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
276 int cat)
277{
278 switch (cat) {
279 case LPROPS_DIRTY:
280 case LPROPS_DIRTY_IDX:
281 case LPROPS_FREE:
282 if (add_to_lpt_heap(c, lprops, cat))
283 break;
284 /* No more room on heap so make it uncategorized */
285 cat = LPROPS_UNCAT;
286 /* Fall through */
287 case LPROPS_UNCAT:
288 list_add(&lprops->list, &c->uncat_list);
289 break;
290 case LPROPS_EMPTY:
291 list_add(&lprops->list, &c->empty_list);
292 break;
293 case LPROPS_FREEABLE:
294 list_add(&lprops->list, &c->freeable_list);
295 c->freeable_cnt += 1;
296 break;
297 case LPROPS_FRDI_IDX:
298 list_add(&lprops->list, &c->frdi_idx_list);
299 break;
300 default:
301 ubifs_assert(0);
302 }
303 lprops->flags &= ~LPROPS_CAT_MASK;
304 lprops->flags |= cat;
305}
306
307/**
308 * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
309 * @c: UBIFS file-system description object
310 * @lprops: LEB properties to remove
311 * @cat: LEB category from which to remove
312 *
313 * LEB properties are categorized to enable fast find operations.
314 */
315static void ubifs_remove_from_cat(struct ubifs_info *c,
316 struct ubifs_lprops *lprops, int cat)
317{
318 switch (cat) {
319 case LPROPS_DIRTY:
320 case LPROPS_DIRTY_IDX:
321 case LPROPS_FREE:
322 remove_from_lpt_heap(c, lprops, cat);
323 break;
324 case LPROPS_FREEABLE:
325 c->freeable_cnt -= 1;
326 ubifs_assert(c->freeable_cnt >= 0);
327 /* Fall through */
328 case LPROPS_UNCAT:
329 case LPROPS_EMPTY:
330 case LPROPS_FRDI_IDX:
331 ubifs_assert(!list_empty(&lprops->list));
332 list_del(&lprops->list);
333 break;
334 default:
335 ubifs_assert(0);
336 }
337}
338
339/**
340 * ubifs_replace_cat - replace lprops in a category list or heap.
341 * @c: UBIFS file-system description object
342 * @old_lprops: LEB properties to replace
343 * @new_lprops: LEB properties with which to replace
344 *
345 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
346 * and the lprops that the pnode contains. When that happens, references in
347 * category lists and heaps must be replaced. This function does that.
348 */
349void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
350 struct ubifs_lprops *new_lprops)
351{
352 int cat;
353
354 cat = new_lprops->flags & LPROPS_CAT_MASK;
355 switch (cat) {
356 case LPROPS_DIRTY:
357 case LPROPS_DIRTY_IDX:
358 case LPROPS_FREE:
359 lpt_heap_replace(c, old_lprops, new_lprops, cat);
360 break;
361 case LPROPS_UNCAT:
362 case LPROPS_EMPTY:
363 case LPROPS_FREEABLE:
364 case LPROPS_FRDI_IDX:
365 list_replace(&old_lprops->list, &new_lprops->list);
366 break;
367 default:
368 ubifs_assert(0);
369 }
370}
371
372/**
373 * ubifs_ensure_cat - ensure LEB properties are categorized.
374 * @c: UBIFS file-system description object
375 * @lprops: LEB properties
376 *
377 * A LEB may have fallen off of the bottom of a heap, and ended up as
378 * uncategorized even though it has enough space for us now. If that is the case
379 * this function will put the LEB back onto a heap.
380 */
381void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
382{
383 int cat = lprops->flags & LPROPS_CAT_MASK;
384
385 if (cat != LPROPS_UNCAT)
386 return;
387 cat = ubifs_categorize_lprops(c, lprops);
388 if (cat == LPROPS_UNCAT)
389 return;
390 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
391 ubifs_add_to_cat(c, lprops, cat);
392}
393
394/**
395 * ubifs_categorize_lprops - categorize LEB properties.
396 * @c: UBIFS file-system description object
397 * @lprops: LEB properties to categorize
398 *
399 * LEB properties are categorized to enable fast find operations. This function
400 * returns the LEB category to which the LEB properties belong. Note however
401 * that if the LEB category is stored as a heap and the heap is full, the
402 * LEB properties may have their category changed to %LPROPS_UNCAT.
403 */
404int ubifs_categorize_lprops(const struct ubifs_info *c,
405 const struct ubifs_lprops *lprops)
406{
407 if (lprops->flags & LPROPS_TAKEN)
408 return LPROPS_UNCAT;
409
410 if (lprops->free == c->leb_size) {
411 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
412 return LPROPS_EMPTY;
413 }
414
415 if (lprops->free + lprops->dirty == c->leb_size) {
416 if (lprops->flags & LPROPS_INDEX)
417 return LPROPS_FRDI_IDX;
418 else
419 return LPROPS_FREEABLE;
420 }
421
422 if (lprops->flags & LPROPS_INDEX) {
423 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
424 return LPROPS_DIRTY_IDX;
425 } else {
426 if (lprops->dirty >= c->dead_wm &&
427 lprops->dirty > lprops->free)
428 return LPROPS_DIRTY;
429 if (lprops->free > 0)
430 return LPROPS_FREE;
431 }
432
433 return LPROPS_UNCAT;
434}
435
436/**
437 * change_category - change LEB properties category.
438 * @c: UBIFS file-system description object
439 * @lprops: LEB properties to recategorize
440 *
441 * LEB properties are categorized to enable fast find operations. When the LEB
442 * properties change they must be recategorized.
443 */
444static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
445{
446 int old_cat = lprops->flags & LPROPS_CAT_MASK;
447 int new_cat = ubifs_categorize_lprops(c, lprops);
448
449 if (old_cat == new_cat) {
450 struct ubifs_lpt_heap *heap = &c->lpt_heap[new_cat - 1];
451
452 /* lprops on a heap now must be moved up or down */
453 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
454 return; /* Not on a heap */
455 heap = &c->lpt_heap[new_cat - 1];
456 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
457 } else {
458 ubifs_remove_from_cat(c, lprops, old_cat);
459 ubifs_add_to_cat(c, lprops, new_cat);
460 }
461}
462
463/**
464 * ubifs_get_lprops - get reference to LEB properties.
465 * @c: the UBIFS file-system description object
466 *
467 * This function locks lprops. Lprops have to be unlocked by
468 * 'ubifs_release_lprops()'.
469 */
470void ubifs_get_lprops(struct ubifs_info *c)
471{
472 mutex_lock(&c->lp_mutex);
473}
474
475/**
476 * calc_dark - calculate LEB dark space size.
477 * @c: the UBIFS file-system description object
478 * @spc: amount of free and dirty space in the LEB
479 *
480 * This function calculates amount of dark space in an LEB which has @spc bytes
481 * of free and dirty space. Returns the calculations result.
482 *
483 * Dark space is the space which is not always usable - it depends on which
484 * nodes are written in which order. E.g., if an LEB has only 512 free bytes,
485 * it is dark space, because it cannot fit a large data node. So UBIFS cannot
486 * count on this LEB and treat these 512 bytes as usable because it is not true
487 * if, for example, only big chunks of uncompressible data will be written to
488 * the FS.
489 */
490static int calc_dark(struct ubifs_info *c, int spc)
491{
492 ubifs_assert(!(spc & 7));
493
494 if (spc < c->dark_wm)
495 return spc;
496
497 /*
498 * If we have slightly more space then the dark space watermark, we can
499 * anyway safely assume it we'll be able to write a node of the
500 * smallest size there.
501 */
502 if (spc - c->dark_wm < MIN_WRITE_SZ)
503 return spc - MIN_WRITE_SZ;
504
505 return c->dark_wm;
506}
507
508/**
509 * is_lprops_dirty - determine if LEB properties are dirty.
510 * @c: the UBIFS file-system description object
511 * @lprops: LEB properties to test
512 */
513static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
514{
515 struct ubifs_pnode *pnode;
516 int pos;
517
518 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
519 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
520 struct ubifs_pnode,
521 lprops[0]);
522 return !test_bit(COW_ZNODE, &pnode->flags) &&
523 test_bit(DIRTY_CNODE, &pnode->flags);
524}
525
526/**
527 * ubifs_change_lp - change LEB properties.
528 * @c: the UBIFS file-system description object
529 * @lp: LEB properties to change
530 * @free: new free space amount
531 * @dirty: new dirty space amount
532 * @flags: new flags
533 * @idx_gc_cnt: change to the count of idx_gc list
534 *
535 * This function changes LEB properties. This function does not change a LEB
536 * property (@free, @dirty or @flag) if the value passed is %LPROPS_NC.
537 *
538 * This function returns a pointer to the updated LEB properties on success
539 * and a negative error code on failure. N.B. the LEB properties may have had to
540 * be copied (due to COW) and consequently the pointer returned may not be the
541 * same as the pointer passed.
542 */
543const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
544 const struct ubifs_lprops *lp,
545 int free, int dirty, int flags,
546 int idx_gc_cnt)
547{
548 /*
549 * This is the only function that is allowed to change lprops, so we
550 * discard the const qualifier.
551 */
552 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
553
554 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
555 lprops->lnum, free, dirty, flags);
556
557 ubifs_assert(mutex_is_locked(&c->lp_mutex));
558 ubifs_assert(c->lst.empty_lebs >= 0 &&
559 c->lst.empty_lebs <= c->main_lebs);
560 ubifs_assert(c->freeable_cnt >= 0);
561 ubifs_assert(c->freeable_cnt <= c->main_lebs);
562 ubifs_assert(c->lst.taken_empty_lebs >= 0);
563 ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
564 ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
565 ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
566 ubifs_assert(!(c->lst.total_used & 7));
567 ubifs_assert(free == LPROPS_NC || free >= 0);
568 ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
569
570 if (!is_lprops_dirty(c, lprops)) {
571 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
572 if (IS_ERR(lprops))
573 return lprops;
574 } else
575 ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
576
577 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
578
579 spin_lock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300580 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
581 c->lst.taken_empty_lebs -= 1;
582
583 if (!(lprops->flags & LPROPS_INDEX)) {
584 int old_spc;
585
586 old_spc = lprops->free + lprops->dirty;
587 if (old_spc < c->dead_wm)
588 c->lst.total_dead -= old_spc;
589 else
590 c->lst.total_dark -= calc_dark(c, old_spc);
591
592 c->lst.total_used -= c->leb_size - old_spc;
593 }
594
595 if (free != LPROPS_NC) {
596 free = ALIGN(free, 8);
597 c->lst.total_free += free - lprops->free;
598
599 /* Increase or decrease empty LEBs counter if needed */
600 if (free == c->leb_size) {
601 if (lprops->free != c->leb_size)
602 c->lst.empty_lebs += 1;
603 } else if (lprops->free == c->leb_size)
604 c->lst.empty_lebs -= 1;
605 lprops->free = free;
606 }
607
608 if (dirty != LPROPS_NC) {
609 dirty = ALIGN(dirty, 8);
610 c->lst.total_dirty += dirty - lprops->dirty;
611 lprops->dirty = dirty;
612 }
613
614 if (flags != LPROPS_NC) {
615 /* Take care about indexing LEBs counter if needed */
616 if ((lprops->flags & LPROPS_INDEX)) {
617 if (!(flags & LPROPS_INDEX))
618 c->lst.idx_lebs -= 1;
619 } else if (flags & LPROPS_INDEX)
620 c->lst.idx_lebs += 1;
621 lprops->flags = flags;
622 }
623
624 if (!(lprops->flags & LPROPS_INDEX)) {
625 int new_spc;
626
627 new_spc = lprops->free + lprops->dirty;
628 if (new_spc < c->dead_wm)
629 c->lst.total_dead += new_spc;
630 else
631 c->lst.total_dark += calc_dark(c, new_spc);
632
633 c->lst.total_used += c->leb_size - new_spc;
634 }
635
636 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
637 c->lst.taken_empty_lebs += 1;
638
639 change_category(c, lprops);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300640 c->idx_gc_cnt += idx_gc_cnt;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300641 spin_unlock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300642 return lprops;
643}
644
645/**
646 * ubifs_release_lprops - release lprops lock.
647 * @c: the UBIFS file-system description object
648 *
649 * This function has to be called after each 'ubifs_get_lprops()' call to
650 * unlock lprops.
651 */
652void ubifs_release_lprops(struct ubifs_info *c)
653{
654 ubifs_assert(mutex_is_locked(&c->lp_mutex));
655 ubifs_assert(c->lst.empty_lebs >= 0 &&
656 c->lst.empty_lebs <= c->main_lebs);
657
658 mutex_unlock(&c->lp_mutex);
659}
660
661/**
662 * ubifs_get_lp_stats - get lprops statistics.
663 * @c: UBIFS file-system description object
664 * @st: return statistics
665 */
666void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *st)
667{
668 spin_lock(&c->space_lock);
669 memcpy(st, &c->lst, sizeof(struct ubifs_lp_stats));
670 spin_unlock(&c->space_lock);
671}
672
673/**
674 * ubifs_change_one_lp - change LEB properties.
675 * @c: the UBIFS file-system description object
676 * @lnum: LEB to change properties for
677 * @free: amount of free space
678 * @dirty: amount of dirty space
679 * @flags_set: flags to set
680 * @flags_clean: flags to clean
681 * @idx_gc_cnt: change to the count of idx_gc list
682 *
683 * This function changes properties of LEB @lnum. It is a helper wrapper over
684 * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
685 * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
686 * a negative error code in case of failure.
687 */
688int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
689 int flags_set, int flags_clean, int idx_gc_cnt)
690{
691 int err = 0, flags;
692 const struct ubifs_lprops *lp;
693
694 ubifs_get_lprops(c);
695
696 lp = ubifs_lpt_lookup_dirty(c, lnum);
697 if (IS_ERR(lp)) {
698 err = PTR_ERR(lp);
699 goto out;
700 }
701
702 flags = (lp->flags | flags_set) & ~flags_clean;
703 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
704 if (IS_ERR(lp))
705 err = PTR_ERR(lp);
706
707out:
708 ubifs_release_lprops(c);
709 return err;
710}
711
712/**
713 * ubifs_update_one_lp - update LEB properties.
714 * @c: the UBIFS file-system description object
715 * @lnum: LEB to change properties for
716 * @free: amount of free space
717 * @dirty: amount of dirty space to add
718 * @flags_set: flags to set
719 * @flags_clean: flags to clean
720 *
721 * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
722 * current dirty space, not substitutes it.
723 */
724int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
725 int flags_set, int flags_clean)
726{
727 int err = 0, flags;
728 const struct ubifs_lprops *lp;
729
730 ubifs_get_lprops(c);
731
732 lp = ubifs_lpt_lookup_dirty(c, lnum);
733 if (IS_ERR(lp)) {
734 err = PTR_ERR(lp);
735 goto out;
736 }
737
738 flags = (lp->flags | flags_set) & ~flags_clean;
739 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
740 if (IS_ERR(lp))
741 err = PTR_ERR(lp);
742
743out:
744 ubifs_release_lprops(c);
745 return err;
746}
747
748/**
749 * ubifs_read_one_lp - read LEB properties.
750 * @c: the UBIFS file-system description object
751 * @lnum: LEB to read properties for
752 * @lp: where to store read properties
753 *
754 * This helper function reads properties of a LEB @lnum and stores them in @lp.
755 * Returns zero in case of success and a negative error code in case of
756 * failure.
757 */
758int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
759{
760 int err = 0;
761 const struct ubifs_lprops *lpp;
762
763 ubifs_get_lprops(c);
764
765 lpp = ubifs_lpt_lookup(c, lnum);
766 if (IS_ERR(lpp)) {
767 err = PTR_ERR(lpp);
768 goto out;
769 }
770
771 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
772
773out:
774 ubifs_release_lprops(c);
775 return err;
776}
777
778/**
779 * ubifs_fast_find_free - try to find a LEB with free space quickly.
780 * @c: the UBIFS file-system description object
781 *
782 * This function returns LEB properties for a LEB with free space or %NULL if
783 * the function is unable to find a LEB quickly.
784 */
785const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
786{
787 struct ubifs_lprops *lprops;
788 struct ubifs_lpt_heap *heap;
789
790 ubifs_assert(mutex_is_locked(&c->lp_mutex));
791
792 heap = &c->lpt_heap[LPROPS_FREE - 1];
793 if (heap->cnt == 0)
794 return NULL;
795
796 lprops = heap->arr[0];
797 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
798 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
799 return lprops;
800}
801
802/**
803 * ubifs_fast_find_empty - try to find an empty LEB quickly.
804 * @c: the UBIFS file-system description object
805 *
806 * This function returns LEB properties for an empty LEB or %NULL if the
807 * function is unable to find an empty LEB quickly.
808 */
809const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
810{
811 struct ubifs_lprops *lprops;
812
813 ubifs_assert(mutex_is_locked(&c->lp_mutex));
814
815 if (list_empty(&c->empty_list))
816 return NULL;
817
818 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
819 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
820 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
821 ubifs_assert(lprops->free == c->leb_size);
822 return lprops;
823}
824
825/**
826 * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
827 * @c: the UBIFS file-system description object
828 *
829 * This function returns LEB properties for a freeable LEB or %NULL if the
830 * function is unable to find a freeable LEB quickly.
831 */
832const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
833{
834 struct ubifs_lprops *lprops;
835
836 ubifs_assert(mutex_is_locked(&c->lp_mutex));
837
838 if (list_empty(&c->freeable_list))
839 return NULL;
840
841 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
842 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
843 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
844 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
845 ubifs_assert(c->freeable_cnt > 0);
846 return lprops;
847}
848
849/**
850 * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
851 * @c: the UBIFS file-system description object
852 *
853 * This function returns LEB properties for a freeable index LEB or %NULL if the
854 * function is unable to find a freeable index LEB quickly.
855 */
856const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
857{
858 struct ubifs_lprops *lprops;
859
860 ubifs_assert(mutex_is_locked(&c->lp_mutex));
861
862 if (list_empty(&c->frdi_idx_list))
863 return NULL;
864
865 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
866 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
867 ubifs_assert((lprops->flags & LPROPS_INDEX));
868 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
869 return lprops;
870}
871
872#ifdef CONFIG_UBIFS_FS_DEBUG
873
874/**
875 * dbg_check_cats - check category heaps and lists.
876 * @c: UBIFS file-system description object
877 *
878 * This function returns %0 on success and a negative error code on failure.
879 */
880int dbg_check_cats(struct ubifs_info *c)
881{
882 struct ubifs_lprops *lprops;
883 struct list_head *pos;
884 int i, cat;
885
886 if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
887 return 0;
888
889 list_for_each_entry(lprops, &c->empty_list, list) {
890 if (lprops->free != c->leb_size) {
891 ubifs_err("non-empty LEB %d on empty list "
892 "(free %d dirty %d flags %d)", lprops->lnum,
893 lprops->free, lprops->dirty, lprops->flags);
894 return -EINVAL;
895 }
896 if (lprops->flags & LPROPS_TAKEN) {
897 ubifs_err("taken LEB %d on empty list "
898 "(free %d dirty %d flags %d)", lprops->lnum,
899 lprops->free, lprops->dirty, lprops->flags);
900 return -EINVAL;
901 }
902 }
903
904 i = 0;
905 list_for_each_entry(lprops, &c->freeable_list, list) {
906 if (lprops->free + lprops->dirty != c->leb_size) {
907 ubifs_err("non-freeable LEB %d on freeable list "
908 "(free %d dirty %d flags %d)", lprops->lnum,
909 lprops->free, lprops->dirty, lprops->flags);
910 return -EINVAL;
911 }
912 if (lprops->flags & LPROPS_TAKEN) {
913 ubifs_err("taken LEB %d on freeable list "
914 "(free %d dirty %d flags %d)", lprops->lnum,
915 lprops->free, lprops->dirty, lprops->flags);
916 return -EINVAL;
917 }
918 i += 1;
919 }
920 if (i != c->freeable_cnt) {
921 ubifs_err("freeable list count %d expected %d", i,
922 c->freeable_cnt);
923 return -EINVAL;
924 }
925
926 i = 0;
927 list_for_each(pos, &c->idx_gc)
928 i += 1;
929 if (i != c->idx_gc_cnt) {
930 ubifs_err("idx_gc list count %d expected %d", i,
931 c->idx_gc_cnt);
932 return -EINVAL;
933 }
934
935 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
936 if (lprops->free + lprops->dirty != c->leb_size) {
937 ubifs_err("non-freeable LEB %d on frdi_idx list "
938 "(free %d dirty %d flags %d)", lprops->lnum,
939 lprops->free, lprops->dirty, lprops->flags);
940 return -EINVAL;
941 }
942 if (lprops->flags & LPROPS_TAKEN) {
943 ubifs_err("taken LEB %d on frdi_idx list "
944 "(free %d dirty %d flags %d)", lprops->lnum,
945 lprops->free, lprops->dirty, lprops->flags);
946 return -EINVAL;
947 }
948 if (!(lprops->flags & LPROPS_INDEX)) {
949 ubifs_err("non-index LEB %d on frdi_idx list "
950 "(free %d dirty %d flags %d)", lprops->lnum,
951 lprops->free, lprops->dirty, lprops->flags);
952 return -EINVAL;
953 }
954 }
955
956 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
957 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
958
959 for (i = 0; i < heap->cnt; i++) {
960 lprops = heap->arr[i];
961 if (!lprops) {
962 ubifs_err("null ptr in LPT heap cat %d", cat);
963 return -EINVAL;
964 }
965 if (lprops->hpos != i) {
966 ubifs_err("bad ptr in LPT heap cat %d", cat);
967 return -EINVAL;
968 }
969 if (lprops->flags & LPROPS_TAKEN) {
970 ubifs_err("taken LEB in LPT heap cat %d", cat);
971 return -EINVAL;
972 }
973 }
974 }
975
976 return 0;
977}
978
979void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
980 int add_pos)
981{
982 int i = 0, j, err = 0;
983
984 if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
985 return;
986
987 for (i = 0; i < heap->cnt; i++) {
988 struct ubifs_lprops *lprops = heap->arr[i];
989 struct ubifs_lprops *lp;
990
991 if (i != add_pos)
992 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
993 err = 1;
994 goto out;
995 }
996 if (lprops->hpos != i) {
997 err = 2;
998 goto out;
999 }
1000 lp = ubifs_lpt_lookup(c, lprops->lnum);
1001 if (IS_ERR(lp)) {
1002 err = 3;
1003 goto out;
1004 }
1005 if (lprops != lp) {
1006 dbg_msg("lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
1007 (size_t)lprops, (size_t)lp, lprops->lnum,
1008 lp->lnum);
1009 err = 4;
1010 goto out;
1011 }
1012 for (j = 0; j < i; j++) {
1013 lp = heap->arr[j];
1014 if (lp == lprops) {
1015 err = 5;
1016 goto out;
1017 }
1018 if (lp->lnum == lprops->lnum) {
1019 err = 6;
1020 goto out;
1021 }
1022 }
1023 }
1024out:
1025 if (err) {
1026 dbg_msg("failed cat %d hpos %d err %d", cat, i, err);
1027 dbg_dump_stack();
1028 dbg_dump_heap(c, heap, cat);
1029 }
1030}
1031
1032/**
1033 * struct scan_check_data - data provided to scan callback function.
1034 * @lst: LEB properties statistics
1035 * @err: error code
1036 */
1037struct scan_check_data {
1038 struct ubifs_lp_stats lst;
1039 int err;
1040};
1041
1042/**
1043 * scan_check_cb - scan callback.
1044 * @c: the UBIFS file-system description object
1045 * @lp: LEB properties to scan
1046 * @in_tree: whether the LEB properties are in main memory
1047 * @data: information passed to and from the caller of the scan
1048 *
1049 * This function returns a code that indicates whether the scan should continue
1050 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1051 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1052 * (%LPT_SCAN_STOP).
1053 */
1054static int scan_check_cb(struct ubifs_info *c,
1055 const struct ubifs_lprops *lp, int in_tree,
1056 struct scan_check_data *data)
1057{
1058 struct ubifs_scan_leb *sleb;
1059 struct ubifs_scan_node *snod;
1060 struct ubifs_lp_stats *lst = &data->lst;
1061 int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty;
1062
1063 cat = lp->flags & LPROPS_CAT_MASK;
1064 if (cat != LPROPS_UNCAT) {
1065 cat = ubifs_categorize_lprops(c, lp);
1066 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1067 ubifs_err("bad LEB category %d expected %d",
1068 (lp->flags & LPROPS_CAT_MASK), cat);
1069 goto out;
1070 }
1071 }
1072
1073 /* Check lp is on its category list (if it has one) */
1074 if (in_tree) {
1075 struct list_head *list = NULL;
1076
1077 switch (cat) {
1078 case LPROPS_EMPTY:
1079 list = &c->empty_list;
1080 break;
1081 case LPROPS_FREEABLE:
1082 list = &c->freeable_list;
1083 break;
1084 case LPROPS_FRDI_IDX:
1085 list = &c->frdi_idx_list;
1086 break;
1087 case LPROPS_UNCAT:
1088 list = &c->uncat_list;
1089 break;
1090 }
1091 if (list) {
1092 struct ubifs_lprops *lprops;
1093 int found = 0;
1094
1095 list_for_each_entry(lprops, list, list) {
1096 if (lprops == lp) {
1097 found = 1;
1098 break;
1099 }
1100 }
1101 if (!found) {
1102 ubifs_err("bad LPT list (category %d)", cat);
1103 goto out;
1104 }
1105 }
1106 }
1107
1108 /* Check lp is on its category heap (if it has one) */
1109 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1110 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1111
1112 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1113 lp != heap->arr[lp->hpos]) {
1114 ubifs_err("bad LPT heap (category %d)", cat);
1115 goto out;
1116 }
1117 }
1118
1119 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
1120 if (IS_ERR(sleb)) {
1121 /*
1122 * After an unclean unmount, empty and freeable LEBs
1123 * may contain garbage.
1124 */
1125 if (lp->free == c->leb_size) {
1126 ubifs_err("scan errors were in empty LEB "
1127 "- continuing checking");
1128 lst->empty_lebs += 1;
1129 lst->total_free += c->leb_size;
1130 lst->total_dark += calc_dark(c, c->leb_size);
1131 return LPT_SCAN_CONTINUE;
1132 }
1133
1134 if (lp->free + lp->dirty == c->leb_size &&
1135 !(lp->flags & LPROPS_INDEX)) {
1136 ubifs_err("scan errors were in freeable LEB "
1137 "- continuing checking");
1138 lst->total_free += lp->free;
1139 lst->total_dirty += lp->dirty;
1140 lst->total_dark += calc_dark(c, c->leb_size);
1141 return LPT_SCAN_CONTINUE;
1142 }
1143 data->err = PTR_ERR(sleb);
1144 return LPT_SCAN_STOP;
1145 }
1146
1147 is_idx = -1;
1148 list_for_each_entry(snod, &sleb->nodes, list) {
1149 int found, level = 0;
1150
1151 cond_resched();
1152
1153 if (is_idx == -1)
1154 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1155
1156 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1157 ubifs_err("indexing node in data LEB %d:%d",
1158 lnum, snod->offs);
1159 goto out_destroy;
1160 }
1161
1162 if (snod->type == UBIFS_IDX_NODE) {
1163 struct ubifs_idx_node *idx = snod->node;
1164
1165 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1166 level = le16_to_cpu(idx->level);
1167 }
1168
1169 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1170 snod->offs, is_idx);
1171 if (found) {
1172 if (found < 0)
1173 goto out_destroy;
1174 used += ALIGN(snod->len, 8);
1175 }
1176 }
1177
1178 free = c->leb_size - sleb->endpt;
1179 dirty = sleb->endpt - used;
1180
1181 if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1182 dirty < 0) {
1183 ubifs_err("bad calculated accounting for LEB %d: "
1184 "free %d, dirty %d", lnum, free, dirty);
1185 goto out_destroy;
1186 }
1187
1188 if (lp->free + lp->dirty == c->leb_size &&
1189 free + dirty == c->leb_size)
1190 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1191 (!is_idx && free == c->leb_size) ||
1192 lp->free == c->leb_size) {
1193 /*
1194 * Empty or freeable LEBs could contain index
1195 * nodes from an uncompleted commit due to an
1196 * unclean unmount. Or they could be empty for
1197 * the same reason. Or it may simply not have been
1198 * unmapped.
1199 */
1200 free = lp->free;
1201 dirty = lp->dirty;
1202 is_idx = 0;
1203 }
1204
1205 if (is_idx && lp->free + lp->dirty == free + dirty &&
1206 lnum != c->ihead_lnum) {
1207 /*
1208 * After an unclean unmount, an index LEB could have a different
1209 * amount of free space than the value recorded by lprops. That
1210 * is because the in-the-gaps method may use free space or
1211 * create free space (as a side-effect of using ubi_leb_change
1212 * and not writing the whole LEB). The incorrect free space
1213 * value is not a problem because the index is only ever
1214 * allocated empty LEBs, so there will never be an attempt to
1215 * write to the free space at the end of an index LEB - except
1216 * by the in-the-gaps method for which it is not a problem.
1217 */
1218 free = lp->free;
1219 dirty = lp->dirty;
1220 }
1221
1222 if (lp->free != free || lp->dirty != dirty)
1223 goto out_print;
1224
1225 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1226 if (free == c->leb_size)
1227 /* Free but not unmapped LEB, it's fine */
1228 is_idx = 0;
1229 else {
1230 ubifs_err("indexing node without indexing "
1231 "flag");
1232 goto out_print;
1233 }
1234 }
1235
1236 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1237 ubifs_err("data node with indexing flag");
1238 goto out_print;
1239 }
1240
1241 if (free == c->leb_size)
1242 lst->empty_lebs += 1;
1243
1244 if (is_idx)
1245 lst->idx_lebs += 1;
1246
1247 if (!(lp->flags & LPROPS_INDEX))
1248 lst->total_used += c->leb_size - free - dirty;
1249 lst->total_free += free;
1250 lst->total_dirty += dirty;
1251
1252 if (!(lp->flags & LPROPS_INDEX)) {
1253 int spc = free + dirty;
1254
1255 if (spc < c->dead_wm)
1256 lst->total_dead += spc;
1257 else
1258 lst->total_dark += calc_dark(c, spc);
1259 }
1260
1261 ubifs_scan_destroy(sleb);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001262 return LPT_SCAN_CONTINUE;
1263
1264out_print:
1265 ubifs_err("bad accounting of LEB %d: free %d, dirty %d flags %#x, "
1266 "should be free %d, dirty %d",
1267 lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1268 dbg_dump_leb(c, lnum);
1269out_destroy:
1270 ubifs_scan_destroy(sleb);
1271out:
1272 data->err = -EINVAL;
1273 return LPT_SCAN_STOP;
1274}
1275
1276/**
1277 * dbg_check_lprops - check all LEB properties.
1278 * @c: UBIFS file-system description object
1279 *
1280 * This function checks all LEB properties and makes sure they are all correct.
1281 * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1282 * and other negative error codes in case of other errors. This function is
1283 * called while the file system is locked (because of commit start), so no
1284 * additional locking is required. Note that locking the LPT mutex would cause
1285 * a circular lock dependency with the TNC mutex.
1286 */
1287int dbg_check_lprops(struct ubifs_info *c)
1288{
1289 int i, err;
1290 struct scan_check_data data;
1291 struct ubifs_lp_stats *lst = &data.lst;
1292
1293 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1294 return 0;
1295
1296 /*
1297 * As we are going to scan the media, the write buffers have to be
1298 * synchronized.
1299 */
1300 for (i = 0; i < c->jhead_cnt; i++) {
1301 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1302 if (err)
1303 return err;
1304 }
1305
1306 memset(lst, 0, sizeof(struct ubifs_lp_stats));
1307
1308 data.err = 0;
1309 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1310 (ubifs_lpt_scan_callback)scan_check_cb,
1311 &data);
1312 if (err && err != -ENOSPC)
1313 goto out;
1314 if (data.err) {
1315 err = data.err;
1316 goto out;
1317 }
1318
1319 if (lst->empty_lebs != c->lst.empty_lebs ||
1320 lst->idx_lebs != c->lst.idx_lebs ||
1321 lst->total_free != c->lst.total_free ||
1322 lst->total_dirty != c->lst.total_dirty ||
1323 lst->total_used != c->lst.total_used) {
1324 ubifs_err("bad overall accounting");
1325 ubifs_err("calculated: empty_lebs %d, idx_lebs %d, "
1326 "total_free %lld, total_dirty %lld, total_used %lld",
1327 lst->empty_lebs, lst->idx_lebs, lst->total_free,
1328 lst->total_dirty, lst->total_used);
1329 ubifs_err("read from lprops: empty_lebs %d, idx_lebs %d, "
1330 "total_free %lld, total_dirty %lld, total_used %lld",
1331 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1332 c->lst.total_dirty, c->lst.total_used);
1333 err = -EINVAL;
1334 goto out;
1335 }
1336
1337 if (lst->total_dead != c->lst.total_dead ||
1338 lst->total_dark != c->lst.total_dark) {
1339 ubifs_err("bad dead/dark space accounting");
1340 ubifs_err("calculated: total_dead %lld, total_dark %lld",
1341 lst->total_dead, lst->total_dark);
1342 ubifs_err("read from lprops: total_dead %lld, total_dark %lld",
1343 c->lst.total_dead, c->lst.total_dark);
1344 err = -EINVAL;
1345 goto out;
1346 }
1347
1348 err = dbg_check_cats(c);
1349out:
1350 return err;
1351}
1352
1353#endif /* CONFIG_UBIFS_FS_DEBUG */