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