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