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