xref: /linux/fs/ubifs/lprops.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
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 	}
128 
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
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  */
256 static 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  */
275 void 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  */
315 static 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  */
349 void 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  */
381 void 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  */
404 int 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  */
444 static 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  * calc_dark - calculate LEB dark space size.
465  * @c: the UBIFS file-system description object
466  * @spc: amount of free and dirty space in the LEB
467  *
468  * This function calculates amount of dark space in an LEB which has @spc bytes
469  * of free and dirty space. Returns the calculations result.
470  *
471  * Dark space is the space which is not always usable - it depends on which
472  * nodes are written in which order. E.g., if an LEB has only 512 free bytes,
473  * it is dark space, because it cannot fit a large data node. So UBIFS cannot
474  * count on this LEB and treat these 512 bytes as usable because it is not true
475  * if, for example, only big chunks of uncompressible data will be written to
476  * the FS.
477  */
478 static int calc_dark(struct ubifs_info *c, int spc)
479 {
480 	ubifs_assert(!(spc & 7));
481 
482 	if (spc < c->dark_wm)
483 		return spc;
484 
485 	/*
486 	 * If we have slightly more space then the dark space watermark, we can
487 	 * anyway safely assume it we'll be able to write a node of the
488 	 * smallest size there.
489 	 */
490 	if (spc - c->dark_wm < MIN_WRITE_SZ)
491 		return spc - MIN_WRITE_SZ;
492 
493 	return c->dark_wm;
494 }
495 
496 /**
497  * is_lprops_dirty - determine if LEB properties are dirty.
498  * @c: the UBIFS file-system description object
499  * @lprops: LEB properties to test
500  */
501 static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
502 {
503 	struct ubifs_pnode *pnode;
504 	int pos;
505 
506 	pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
507 	pnode = (struct ubifs_pnode *)container_of(lprops - pos,
508 						   struct ubifs_pnode,
509 						   lprops[0]);
510 	return !test_bit(COW_ZNODE, &pnode->flags) &&
511 	       test_bit(DIRTY_CNODE, &pnode->flags);
512 }
513 
514 /**
515  * ubifs_change_lp - change LEB properties.
516  * @c: the UBIFS file-system description object
517  * @lp: LEB properties to change
518  * @free: new free space amount
519  * @dirty: new dirty space amount
520  * @flags: new flags
521  * @idx_gc_cnt: change to the count of idx_gc list
522  *
523  * This function changes LEB properties (@free, @dirty or @flag). However, the
524  * property which has the %LPROPS_NC value is not changed. Returns a pointer to
525  * the updated LEB properties on success and a negative error code on failure.
526  *
527  * Note, the LEB properties may have had to be copied (due to COW) and
528  * consequently the pointer returned may not be the same as the pointer
529  * passed.
530  */
531 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
532 					   const struct ubifs_lprops *lp,
533 					   int free, int dirty, int flags,
534 					   int idx_gc_cnt)
535 {
536 	/*
537 	 * This is the only function that is allowed to change lprops, so we
538 	 * discard the const qualifier.
539 	 */
540 	struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
541 
542 	dbg_lp("LEB %d, free %d, dirty %d, flags %d",
543 	       lprops->lnum, free, dirty, flags);
544 
545 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
546 	ubifs_assert(c->lst.empty_lebs >= 0 &&
547 		     c->lst.empty_lebs <= c->main_lebs);
548 	ubifs_assert(c->freeable_cnt >= 0);
549 	ubifs_assert(c->freeable_cnt <= c->main_lebs);
550 	ubifs_assert(c->lst.taken_empty_lebs >= 0);
551 	ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
552 	ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
553 	ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
554 	ubifs_assert(!(c->lst.total_used & 7));
555 	ubifs_assert(free == LPROPS_NC || free >= 0);
556 	ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
557 
558 	if (!is_lprops_dirty(c, lprops)) {
559 		lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
560 		if (IS_ERR(lprops))
561 			return lprops;
562 	} else
563 		ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
564 
565 	ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
566 
567 	spin_lock(&c->space_lock);
568 	if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
569 		c->lst.taken_empty_lebs -= 1;
570 
571 	if (!(lprops->flags & LPROPS_INDEX)) {
572 		int old_spc;
573 
574 		old_spc = lprops->free + lprops->dirty;
575 		if (old_spc < c->dead_wm)
576 			c->lst.total_dead -= old_spc;
577 		else
578 			c->lst.total_dark -= calc_dark(c, old_spc);
579 
580 		c->lst.total_used -= c->leb_size - old_spc;
581 	}
582 
583 	if (free != LPROPS_NC) {
584 		free = ALIGN(free, 8);
585 		c->lst.total_free += free - lprops->free;
586 
587 		/* Increase or decrease empty LEBs counter if needed */
588 		if (free == c->leb_size) {
589 			if (lprops->free != c->leb_size)
590 				c->lst.empty_lebs += 1;
591 		} else if (lprops->free == c->leb_size)
592 			c->lst.empty_lebs -= 1;
593 		lprops->free = free;
594 	}
595 
596 	if (dirty != LPROPS_NC) {
597 		dirty = ALIGN(dirty, 8);
598 		c->lst.total_dirty += dirty - lprops->dirty;
599 		lprops->dirty = dirty;
600 	}
601 
602 	if (flags != LPROPS_NC) {
603 		/* Take care about indexing LEBs counter if needed */
604 		if ((lprops->flags & LPROPS_INDEX)) {
605 			if (!(flags & LPROPS_INDEX))
606 				c->lst.idx_lebs -= 1;
607 		} else if (flags & LPROPS_INDEX)
608 			c->lst.idx_lebs += 1;
609 		lprops->flags = flags;
610 	}
611 
612 	if (!(lprops->flags & LPROPS_INDEX)) {
613 		int new_spc;
614 
615 		new_spc = lprops->free + lprops->dirty;
616 		if (new_spc < c->dead_wm)
617 			c->lst.total_dead += new_spc;
618 		else
619 			c->lst.total_dark += calc_dark(c, new_spc);
620 
621 		c->lst.total_used += c->leb_size - new_spc;
622 	}
623 
624 	if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
625 		c->lst.taken_empty_lebs += 1;
626 
627 	change_category(c, lprops);
628 	c->idx_gc_cnt += idx_gc_cnt;
629 	spin_unlock(&c->space_lock);
630 	return lprops;
631 }
632 
633 /**
634  * ubifs_get_lp_stats - get lprops statistics.
635  * @c: UBIFS file-system description object
636  * @st: return statistics
637  */
638 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
639 {
640 	spin_lock(&c->space_lock);
641 	memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
642 	spin_unlock(&c->space_lock);
643 }
644 
645 /**
646  * ubifs_change_one_lp - change LEB properties.
647  * @c: the UBIFS file-system description object
648  * @lnum: LEB to change properties for
649  * @free: amount of free space
650  * @dirty: amount of dirty space
651  * @flags_set: flags to set
652  * @flags_clean: flags to clean
653  * @idx_gc_cnt: change to the count of idx_gc list
654  *
655  * This function changes properties of LEB @lnum. It is a helper wrapper over
656  * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
657  * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
658  * a negative error code in case of failure.
659  */
660 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
661 			int flags_set, int flags_clean, int idx_gc_cnt)
662 {
663 	int err = 0, flags;
664 	const struct ubifs_lprops *lp;
665 
666 	ubifs_get_lprops(c);
667 
668 	lp = ubifs_lpt_lookup_dirty(c, lnum);
669 	if (IS_ERR(lp)) {
670 		err = PTR_ERR(lp);
671 		goto out;
672 	}
673 
674 	flags = (lp->flags | flags_set) & ~flags_clean;
675 	lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
676 	if (IS_ERR(lp))
677 		err = PTR_ERR(lp);
678 
679 out:
680 	ubifs_release_lprops(c);
681 	if (err)
682 		ubifs_err("cannot change properties of LEB %d, error %d",
683 			  lnum, err);
684 	return err;
685 }
686 
687 /**
688  * ubifs_update_one_lp - update LEB properties.
689  * @c: the UBIFS file-system description object
690  * @lnum: LEB to change properties for
691  * @free: amount of free space
692  * @dirty: amount of dirty space to add
693  * @flags_set: flags to set
694  * @flags_clean: flags to clean
695  *
696  * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
697  * current dirty space, not substitutes it.
698  */
699 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
700 			int flags_set, int flags_clean)
701 {
702 	int err = 0, flags;
703 	const struct ubifs_lprops *lp;
704 
705 	ubifs_get_lprops(c);
706 
707 	lp = ubifs_lpt_lookup_dirty(c, lnum);
708 	if (IS_ERR(lp)) {
709 		err = PTR_ERR(lp);
710 		goto out;
711 	}
712 
713 	flags = (lp->flags | flags_set) & ~flags_clean;
714 	lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
715 	if (IS_ERR(lp))
716 		err = PTR_ERR(lp);
717 
718 out:
719 	ubifs_release_lprops(c);
720 	if (err)
721 		ubifs_err("cannot update properties of LEB %d, error %d",
722 			  lnum, err);
723 	return err;
724 }
725 
726 /**
727  * ubifs_read_one_lp - read LEB properties.
728  * @c: the UBIFS file-system description object
729  * @lnum: LEB to read properties for
730  * @lp: where to store read properties
731  *
732  * This helper function reads properties of a LEB @lnum and stores them in @lp.
733  * Returns zero in case of success and a negative error code in case of
734  * failure.
735  */
736 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
737 {
738 	int err = 0;
739 	const struct ubifs_lprops *lpp;
740 
741 	ubifs_get_lprops(c);
742 
743 	lpp = ubifs_lpt_lookup(c, lnum);
744 	if (IS_ERR(lpp)) {
745 		err = PTR_ERR(lpp);
746 		ubifs_err("cannot read properties of LEB %d, error %d",
747 			  lnum, err);
748 		goto out;
749 	}
750 
751 	memcpy(lp, lpp, sizeof(struct ubifs_lprops));
752 
753 out:
754 	ubifs_release_lprops(c);
755 	return err;
756 }
757 
758 /**
759  * ubifs_fast_find_free - try to find a LEB with free space quickly.
760  * @c: the UBIFS file-system description object
761  *
762  * This function returns LEB properties for a LEB with free space or %NULL if
763  * the function is unable to find a LEB quickly.
764  */
765 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
766 {
767 	struct ubifs_lprops *lprops;
768 	struct ubifs_lpt_heap *heap;
769 
770 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
771 
772 	heap = &c->lpt_heap[LPROPS_FREE - 1];
773 	if (heap->cnt == 0)
774 		return NULL;
775 
776 	lprops = heap->arr[0];
777 	ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
778 	ubifs_assert(!(lprops->flags & LPROPS_INDEX));
779 	return lprops;
780 }
781 
782 /**
783  * ubifs_fast_find_empty - try to find an empty LEB quickly.
784  * @c: the UBIFS file-system description object
785  *
786  * This function returns LEB properties for an empty LEB or %NULL if the
787  * function is unable to find an empty LEB quickly.
788  */
789 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
790 {
791 	struct ubifs_lprops *lprops;
792 
793 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
794 
795 	if (list_empty(&c->empty_list))
796 		return NULL;
797 
798 	lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
799 	ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
800 	ubifs_assert(!(lprops->flags & LPROPS_INDEX));
801 	ubifs_assert(lprops->free == c->leb_size);
802 	return lprops;
803 }
804 
805 /**
806  * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
807  * @c: the UBIFS file-system description object
808  *
809  * This function returns LEB properties for a freeable LEB or %NULL if the
810  * function is unable to find a freeable LEB quickly.
811  */
812 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
813 {
814 	struct ubifs_lprops *lprops;
815 
816 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
817 
818 	if (list_empty(&c->freeable_list))
819 		return NULL;
820 
821 	lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
822 	ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
823 	ubifs_assert(!(lprops->flags & LPROPS_INDEX));
824 	ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
825 	ubifs_assert(c->freeable_cnt > 0);
826 	return lprops;
827 }
828 
829 /**
830  * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
831  * @c: the UBIFS file-system description object
832  *
833  * This function returns LEB properties for a freeable index LEB or %NULL if the
834  * function is unable to find a freeable index LEB quickly.
835  */
836 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
837 {
838 	struct ubifs_lprops *lprops;
839 
840 	ubifs_assert(mutex_is_locked(&c->lp_mutex));
841 
842 	if (list_empty(&c->frdi_idx_list))
843 		return NULL;
844 
845 	lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
846 	ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
847 	ubifs_assert((lprops->flags & LPROPS_INDEX));
848 	ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
849 	return lprops;
850 }
851 
852 #ifdef CONFIG_UBIFS_FS_DEBUG
853 
854 /**
855  * dbg_check_cats - check category heaps and lists.
856  * @c: UBIFS file-system description object
857  *
858  * This function returns %0 on success and a negative error code on failure.
859  */
860 int dbg_check_cats(struct ubifs_info *c)
861 {
862 	struct ubifs_lprops *lprops;
863 	struct list_head *pos;
864 	int i, cat;
865 
866 	if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
867 		return 0;
868 
869 	list_for_each_entry(lprops, &c->empty_list, list) {
870 		if (lprops->free != c->leb_size) {
871 			ubifs_err("non-empty LEB %d on empty list "
872 				  "(free %d dirty %d flags %d)", lprops->lnum,
873 				  lprops->free, lprops->dirty, lprops->flags);
874 			return -EINVAL;
875 		}
876 		if (lprops->flags & LPROPS_TAKEN) {
877 			ubifs_err("taken LEB %d on empty list "
878 				  "(free %d dirty %d flags %d)", lprops->lnum,
879 				  lprops->free, lprops->dirty, lprops->flags);
880 			return -EINVAL;
881 		}
882 	}
883 
884 	i = 0;
885 	list_for_each_entry(lprops, &c->freeable_list, list) {
886 		if (lprops->free + lprops->dirty != c->leb_size) {
887 			ubifs_err("non-freeable LEB %d on freeable list "
888 				  "(free %d dirty %d flags %d)", lprops->lnum,
889 				  lprops->free, lprops->dirty, lprops->flags);
890 			return -EINVAL;
891 		}
892 		if (lprops->flags & LPROPS_TAKEN) {
893 			ubifs_err("taken LEB %d on freeable list "
894 				  "(free %d dirty %d flags %d)", lprops->lnum,
895 				  lprops->free, lprops->dirty, lprops->flags);
896 			return -EINVAL;
897 		}
898 		i += 1;
899 	}
900 	if (i != c->freeable_cnt) {
901 		ubifs_err("freeable list count %d expected %d", i,
902 			  c->freeable_cnt);
903 		return -EINVAL;
904 	}
905 
906 	i = 0;
907 	list_for_each(pos, &c->idx_gc)
908 		i += 1;
909 	if (i != c->idx_gc_cnt) {
910 		ubifs_err("idx_gc list count %d expected %d", i,
911 			  c->idx_gc_cnt);
912 		return -EINVAL;
913 	}
914 
915 	list_for_each_entry(lprops, &c->frdi_idx_list, list) {
916 		if (lprops->free + lprops->dirty != c->leb_size) {
917 			ubifs_err("non-freeable LEB %d on frdi_idx list "
918 				  "(free %d dirty %d flags %d)", lprops->lnum,
919 				  lprops->free, lprops->dirty, lprops->flags);
920 			return -EINVAL;
921 		}
922 		if (lprops->flags & LPROPS_TAKEN) {
923 			ubifs_err("taken LEB %d on frdi_idx list "
924 				  "(free %d dirty %d flags %d)", lprops->lnum,
925 				  lprops->free, lprops->dirty, lprops->flags);
926 			return -EINVAL;
927 		}
928 		if (!(lprops->flags & LPROPS_INDEX)) {
929 			ubifs_err("non-index LEB %d on frdi_idx list "
930 				  "(free %d dirty %d flags %d)", lprops->lnum,
931 				  lprops->free, lprops->dirty, lprops->flags);
932 			return -EINVAL;
933 		}
934 	}
935 
936 	for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
937 		struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
938 
939 		for (i = 0; i < heap->cnt; i++) {
940 			lprops = heap->arr[i];
941 			if (!lprops) {
942 				ubifs_err("null ptr in LPT heap cat %d", cat);
943 				return -EINVAL;
944 			}
945 			if (lprops->hpos != i) {
946 				ubifs_err("bad ptr in LPT heap cat %d", cat);
947 				return -EINVAL;
948 			}
949 			if (lprops->flags & LPROPS_TAKEN) {
950 				ubifs_err("taken LEB in LPT heap cat %d", cat);
951 				return -EINVAL;
952 			}
953 		}
954 	}
955 
956 	return 0;
957 }
958 
959 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
960 		    int add_pos)
961 {
962 	int i = 0, j, err = 0;
963 
964 	if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
965 		return;
966 
967 	for (i = 0; i < heap->cnt; i++) {
968 		struct ubifs_lprops *lprops = heap->arr[i];
969 		struct ubifs_lprops *lp;
970 
971 		if (i != add_pos)
972 			if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
973 				err = 1;
974 				goto out;
975 			}
976 		if (lprops->hpos != i) {
977 			err = 2;
978 			goto out;
979 		}
980 		lp = ubifs_lpt_lookup(c, lprops->lnum);
981 		if (IS_ERR(lp)) {
982 			err = 3;
983 			goto out;
984 		}
985 		if (lprops != lp) {
986 			dbg_msg("lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
987 				(size_t)lprops, (size_t)lp, lprops->lnum,
988 				lp->lnum);
989 			err = 4;
990 			goto out;
991 		}
992 		for (j = 0; j < i; j++) {
993 			lp = heap->arr[j];
994 			if (lp == lprops) {
995 				err = 5;
996 				goto out;
997 			}
998 			if (lp->lnum == lprops->lnum) {
999 				err = 6;
1000 				goto out;
1001 			}
1002 		}
1003 	}
1004 out:
1005 	if (err) {
1006 		dbg_msg("failed cat %d hpos %d err %d", cat, i, err);
1007 		dbg_dump_stack();
1008 		dbg_dump_heap(c, heap, cat);
1009 	}
1010 }
1011 
1012 /**
1013  * struct scan_check_data - data provided to scan callback function.
1014  * @lst: LEB properties statistics
1015  * @err: error code
1016  */
1017 struct scan_check_data {
1018 	struct ubifs_lp_stats lst;
1019 	int err;
1020 };
1021 
1022 /**
1023  * scan_check_cb - scan callback.
1024  * @c: the UBIFS file-system description object
1025  * @lp: LEB properties to scan
1026  * @in_tree: whether the LEB properties are in main memory
1027  * @data: information passed to and from the caller of the scan
1028  *
1029  * This function returns a code that indicates whether the scan should continue
1030  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1031  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1032  * (%LPT_SCAN_STOP).
1033  */
1034 static int scan_check_cb(struct ubifs_info *c,
1035 			 const struct ubifs_lprops *lp, int in_tree,
1036 			 struct scan_check_data *data)
1037 {
1038 	struct ubifs_scan_leb *sleb;
1039 	struct ubifs_scan_node *snod;
1040 	struct ubifs_lp_stats *lst = &data->lst;
1041 	int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty;
1042 
1043 	cat = lp->flags & LPROPS_CAT_MASK;
1044 	if (cat != LPROPS_UNCAT) {
1045 		cat = ubifs_categorize_lprops(c, lp);
1046 		if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1047 			ubifs_err("bad LEB category %d expected %d",
1048 				  (lp->flags & LPROPS_CAT_MASK), cat);
1049 			goto out;
1050 		}
1051 	}
1052 
1053 	/* Check lp is on its category list (if it has one) */
1054 	if (in_tree) {
1055 		struct list_head *list = NULL;
1056 
1057 		switch (cat) {
1058 		case LPROPS_EMPTY:
1059 			list = &c->empty_list;
1060 			break;
1061 		case LPROPS_FREEABLE:
1062 			list = &c->freeable_list;
1063 			break;
1064 		case LPROPS_FRDI_IDX:
1065 			list = &c->frdi_idx_list;
1066 			break;
1067 		case LPROPS_UNCAT:
1068 			list = &c->uncat_list;
1069 			break;
1070 		}
1071 		if (list) {
1072 			struct ubifs_lprops *lprops;
1073 			int found = 0;
1074 
1075 			list_for_each_entry(lprops, list, list) {
1076 				if (lprops == lp) {
1077 					found = 1;
1078 					break;
1079 				}
1080 			}
1081 			if (!found) {
1082 				ubifs_err("bad LPT list (category %d)", cat);
1083 				goto out;
1084 			}
1085 		}
1086 	}
1087 
1088 	/* Check lp is on its category heap (if it has one) */
1089 	if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1090 		struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1091 
1092 		if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1093 		    lp != heap->arr[lp->hpos]) {
1094 			ubifs_err("bad LPT heap (category %d)", cat);
1095 			goto out;
1096 		}
1097 	}
1098 
1099 	sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
1100 	if (IS_ERR(sleb)) {
1101 		/*
1102 		 * After an unclean unmount, empty and freeable LEBs
1103 		 * may contain garbage.
1104 		 */
1105 		if (lp->free == c->leb_size) {
1106 			ubifs_err("scan errors were in empty LEB "
1107 				  "- continuing checking");
1108 			lst->empty_lebs += 1;
1109 			lst->total_free += c->leb_size;
1110 			lst->total_dark += calc_dark(c, c->leb_size);
1111 			return LPT_SCAN_CONTINUE;
1112 		}
1113 
1114 		if (lp->free + lp->dirty == c->leb_size &&
1115 		    !(lp->flags & LPROPS_INDEX)) {
1116 			ubifs_err("scan errors were in freeable LEB "
1117 				  "- continuing checking");
1118 			lst->total_free  += lp->free;
1119 			lst->total_dirty += lp->dirty;
1120 			lst->total_dark  +=  calc_dark(c, c->leb_size);
1121 			return LPT_SCAN_CONTINUE;
1122 		}
1123 		data->err = PTR_ERR(sleb);
1124 		return LPT_SCAN_STOP;
1125 	}
1126 
1127 	is_idx = -1;
1128 	list_for_each_entry(snod, &sleb->nodes, list) {
1129 		int found, level = 0;
1130 
1131 		cond_resched();
1132 
1133 		if (is_idx == -1)
1134 			is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1135 
1136 		if (is_idx && snod->type != UBIFS_IDX_NODE) {
1137 			ubifs_err("indexing node in data LEB %d:%d",
1138 				  lnum, snod->offs);
1139 			goto out_destroy;
1140 		}
1141 
1142 		if (snod->type == UBIFS_IDX_NODE) {
1143 			struct ubifs_idx_node *idx = snod->node;
1144 
1145 			key_read(c, ubifs_idx_key(c, idx), &snod->key);
1146 			level = le16_to_cpu(idx->level);
1147 		}
1148 
1149 		found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1150 					   snod->offs, is_idx);
1151 		if (found) {
1152 			if (found < 0)
1153 				goto out_destroy;
1154 			used += ALIGN(snod->len, 8);
1155 		}
1156 	}
1157 
1158 	free = c->leb_size - sleb->endpt;
1159 	dirty = sleb->endpt - used;
1160 
1161 	if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1162 	    dirty < 0) {
1163 		ubifs_err("bad calculated accounting for LEB %d: "
1164 			  "free %d, dirty %d", lnum, free, dirty);
1165 		goto out_destroy;
1166 	}
1167 
1168 	if (lp->free + lp->dirty == c->leb_size &&
1169 	    free + dirty == c->leb_size)
1170 		if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1171 		    (!is_idx && free == c->leb_size) ||
1172 		    lp->free == c->leb_size) {
1173 			/*
1174 			 * Empty or freeable LEBs could contain index
1175 			 * nodes from an uncompleted commit due to an
1176 			 * unclean unmount. Or they could be empty for
1177 			 * the same reason. Or it may simply not have been
1178 			 * unmapped.
1179 			 */
1180 			free = lp->free;
1181 			dirty = lp->dirty;
1182 			is_idx = 0;
1183 		    }
1184 
1185 	if (is_idx && lp->free + lp->dirty == free + dirty &&
1186 	    lnum != c->ihead_lnum) {
1187 		/*
1188 		 * After an unclean unmount, an index LEB could have a different
1189 		 * amount of free space than the value recorded by lprops. That
1190 		 * is because the in-the-gaps method may use free space or
1191 		 * create free space (as a side-effect of using ubi_leb_change
1192 		 * and not writing the whole LEB). The incorrect free space
1193 		 * value is not a problem because the index is only ever
1194 		 * allocated empty LEBs, so there will never be an attempt to
1195 		 * write to the free space at the end of an index LEB - except
1196 		 * by the in-the-gaps method for which it is not a problem.
1197 		 */
1198 		free = lp->free;
1199 		dirty = lp->dirty;
1200 	}
1201 
1202 	if (lp->free != free || lp->dirty != dirty)
1203 		goto out_print;
1204 
1205 	if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1206 		if (free == c->leb_size)
1207 			/* Free but not unmapped LEB, it's fine */
1208 			is_idx = 0;
1209 		else {
1210 			ubifs_err("indexing node without indexing "
1211 				  "flag");
1212 			goto out_print;
1213 		}
1214 	}
1215 
1216 	if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1217 		ubifs_err("data node with indexing flag");
1218 		goto out_print;
1219 	}
1220 
1221 	if (free == c->leb_size)
1222 		lst->empty_lebs += 1;
1223 
1224 	if (is_idx)
1225 		lst->idx_lebs += 1;
1226 
1227 	if (!(lp->flags & LPROPS_INDEX))
1228 		lst->total_used += c->leb_size - free - dirty;
1229 	lst->total_free += free;
1230 	lst->total_dirty += dirty;
1231 
1232 	if (!(lp->flags & LPROPS_INDEX)) {
1233 		int spc = free + dirty;
1234 
1235 		if (spc < c->dead_wm)
1236 			lst->total_dead += spc;
1237 		else
1238 			lst->total_dark += calc_dark(c, spc);
1239 	}
1240 
1241 	ubifs_scan_destroy(sleb);
1242 	return LPT_SCAN_CONTINUE;
1243 
1244 out_print:
1245 	ubifs_err("bad accounting of LEB %d: free %d, dirty %d flags %#x, "
1246 		  "should be free %d, dirty %d",
1247 		  lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1248 	dbg_dump_leb(c, lnum);
1249 out_destroy:
1250 	ubifs_scan_destroy(sleb);
1251 out:
1252 	data->err = -EINVAL;
1253 	return LPT_SCAN_STOP;
1254 }
1255 
1256 /**
1257  * dbg_check_lprops - check all LEB properties.
1258  * @c: UBIFS file-system description object
1259  *
1260  * This function checks all LEB properties and makes sure they are all correct.
1261  * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1262  * and other negative error codes in case of other errors. This function is
1263  * called while the file system is locked (because of commit start), so no
1264  * additional locking is required. Note that locking the LPT mutex would cause
1265  * a circular lock dependency with the TNC mutex.
1266  */
1267 int dbg_check_lprops(struct ubifs_info *c)
1268 {
1269 	int i, err;
1270 	struct scan_check_data data;
1271 	struct ubifs_lp_stats *lst = &data.lst;
1272 
1273 	if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1274 		return 0;
1275 
1276 	/*
1277 	 * As we are going to scan the media, the write buffers have to be
1278 	 * synchronized.
1279 	 */
1280 	for (i = 0; i < c->jhead_cnt; i++) {
1281 		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1282 		if (err)
1283 			return err;
1284 	}
1285 
1286 	memset(lst, 0, sizeof(struct ubifs_lp_stats));
1287 
1288 	data.err = 0;
1289 	err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1290 				    (ubifs_lpt_scan_callback)scan_check_cb,
1291 				    &data);
1292 	if (err && err != -ENOSPC)
1293 		goto out;
1294 	if (data.err) {
1295 		err = data.err;
1296 		goto out;
1297 	}
1298 
1299 	if (lst->empty_lebs != c->lst.empty_lebs ||
1300 	    lst->idx_lebs != c->lst.idx_lebs ||
1301 	    lst->total_free != c->lst.total_free ||
1302 	    lst->total_dirty != c->lst.total_dirty ||
1303 	    lst->total_used != c->lst.total_used) {
1304 		ubifs_err("bad overall accounting");
1305 		ubifs_err("calculated: empty_lebs %d, idx_lebs %d, "
1306 			  "total_free %lld, total_dirty %lld, total_used %lld",
1307 			  lst->empty_lebs, lst->idx_lebs, lst->total_free,
1308 			  lst->total_dirty, lst->total_used);
1309 		ubifs_err("read from lprops: empty_lebs %d, idx_lebs %d, "
1310 			  "total_free %lld, total_dirty %lld, total_used %lld",
1311 			  c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1312 			  c->lst.total_dirty, c->lst.total_used);
1313 		err = -EINVAL;
1314 		goto out;
1315 	}
1316 
1317 	if (lst->total_dead != c->lst.total_dead ||
1318 	    lst->total_dark != c->lst.total_dark) {
1319 		ubifs_err("bad dead/dark space accounting");
1320 		ubifs_err("calculated: total_dead %lld, total_dark %lld",
1321 			  lst->total_dead, lst->total_dark);
1322 		ubifs_err("read from lprops: total_dead %lld, total_dark %lld",
1323 			  c->lst.total_dead, c->lst.total_dark);
1324 		err = -EINVAL;
1325 		goto out;
1326 	}
1327 
1328 	err = dbg_check_cats(c);
1329 out:
1330 	return err;
1331 }
1332 
1333 #endif /* CONFIG_UBIFS_FS_DEBUG */
1334