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