Blame view

fs/ubifs/lprops.c 35.4 KB
2b27bdcc2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2
3
4
5
6
  /*
   * This file is part of UBIFS.
   *
   * Copyright (C) 2006-2008 Nokia Corporation.
   *
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
   * Authors: Adrian Hunter
   *          Artem Bityutskiy (Битюцкий Артём)
   */
  
  /*
   * This file implements the functions that access LEB properties and their
   * categories. LEBs are categorized based on the needs of UBIFS, and the
   * categories are stored as either heaps or lists to provide a fast way of
   * finding a LEB in a particular category. For example, UBIFS may need to find
   * an empty LEB for the journal, or a very dirty LEB for garbage collection.
   */
  
  #include "ubifs.h"
  
  /**
   * get_heap_comp_val - get the LEB properties value for heap comparisons.
   * @lprops: LEB properties
   * @cat: LEB category
   */
  static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
  {
  	switch (cat) {
  	case LPROPS_FREE:
  		return lprops->free;
  	case LPROPS_DIRTY_IDX:
  		return lprops->free + lprops->dirty;
  	default:
  		return lprops->dirty;
  	}
  }
  
  /**
   * move_up_lpt_heap - move a new heap entry up as far as possible.
   * @c: UBIFS file-system description object
   * @heap: LEB category heap
   * @lprops: LEB properties to move
   * @cat: LEB category
   *
   * New entries to a heap are added at the bottom and then moved up until the
   * parent's value is greater.  In the case of LPT's category heaps, the value
   * is either the amount of free space or the amount of dirty space, depending
   * on the category.
   */
  static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
  			     struct ubifs_lprops *lprops, int cat)
  {
  	int val1, val2, hpos;
  
  	hpos = lprops->hpos;
  	if (!hpos)
  		return; /* Already top of the heap */
  	val1 = get_heap_comp_val(lprops, cat);
  	/* Compare to parent and, if greater, move up the heap */
  	do {
  		int ppos = (hpos - 1) / 2;
  
  		val2 = get_heap_comp_val(heap->arr[ppos], cat);
  		if (val2 >= val1)
  			return;
  		/* Greater than parent so move up */
  		heap->arr[ppos]->hpos = hpos;
  		heap->arr[hpos] = heap->arr[ppos];
  		heap->arr[ppos] = lprops;
  		lprops->hpos = ppos;
  		hpos = ppos;
  	} while (hpos);
  }
  
  /**
   * adjust_lpt_heap - move a changed heap entry up or down the heap.
   * @c: UBIFS file-system description object
   * @heap: LEB category heap
   * @lprops: LEB properties to move
   * @hpos: heap position of @lprops
   * @cat: LEB category
   *
   * Changed entries in a heap are moved up or down until the parent's value is
   * greater.  In the case of LPT's category heaps, the value is either the amount
   * of free space or the amount of dirty space, depending on the category.
   */
  static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
  			    struct ubifs_lprops *lprops, int hpos, int cat)
  {
  	int val1, val2, val3, cpos;
  
  	val1 = get_heap_comp_val(lprops, cat);
  	/* Compare to parent and, if greater than parent, move up the heap */
  	if (hpos) {
  		int ppos = (hpos - 1) / 2;
  
  		val2 = get_heap_comp_val(heap->arr[ppos], cat);
  		if (val1 > val2) {
  			/* Greater than parent so move up */
  			while (1) {
  				heap->arr[ppos]->hpos = hpos;
  				heap->arr[hpos] = heap->arr[ppos];
  				heap->arr[ppos] = lprops;
  				lprops->hpos = ppos;
  				hpos = ppos;
  				if (!hpos)
  					return;
  				ppos = (hpos - 1) / 2;
  				val2 = get_heap_comp_val(heap->arr[ppos], cat);
  				if (val1 <= val2)
  					return;
  				/* Still greater than parent so keep going */
  			}
  		}
  	}
948cfb219   Artem Bityutskiy   UBIFS: add a prin...
116

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  	/* Not greater than parent, so compare to children */
  	while (1) {
  		/* Compare to left child */
  		cpos = hpos * 2 + 1;
  		if (cpos >= heap->cnt)
  			return;
  		val2 = get_heap_comp_val(heap->arr[cpos], cat);
  		if (val1 < val2) {
  			/* Less than left child, so promote biggest child */
  			if (cpos + 1 < heap->cnt) {
  				val3 = get_heap_comp_val(heap->arr[cpos + 1],
  							 cat);
  				if (val3 > val2)
  					cpos += 1; /* Right child is bigger */
  			}
  			heap->arr[cpos]->hpos = hpos;
  			heap->arr[hpos] = heap->arr[cpos];
  			heap->arr[cpos] = lprops;
  			lprops->hpos = cpos;
  			hpos = cpos;
  			continue;
  		}
  		/* Compare to right child */
  		cpos += 1;
  		if (cpos >= heap->cnt)
  			return;
  		val3 = get_heap_comp_val(heap->arr[cpos], cat);
  		if (val1 < val3) {
  			/* Less than right child, so promote right child */
  			heap->arr[cpos]->hpos = hpos;
  			heap->arr[hpos] = heap->arr[cpos];
  			heap->arr[cpos] = lprops;
  			lprops->hpos = cpos;
  			hpos = cpos;
  			continue;
  		}
  		return;
  	}
  }
  
  /**
   * add_to_lpt_heap - add LEB properties to a LEB category heap.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties to add
   * @cat: LEB category
   *
   * This function returns %1 if @lprops is added to the heap for LEB category
   * @cat, otherwise %0 is returned because the heap is full.
   */
  static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
  			   int cat)
  {
  	struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
  
  	if (heap->cnt >= heap->max_cnt) {
  		const int b = LPT_HEAP_SZ / 2 - 1;
  		int cpos, val1, val2;
  
  		/* Compare to some other LEB on the bottom of heap */
  		/* Pick a position kind of randomly */
  		cpos = (((size_t)lprops >> 4) & b) + b;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
178
179
180
  		ubifs_assert(c, cpos >= b);
  		ubifs_assert(c, cpos < LPT_HEAP_SZ);
  		ubifs_assert(c, cpos < heap->cnt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  
  		val1 = get_heap_comp_val(lprops, cat);
  		val2 = get_heap_comp_val(heap->arr[cpos], cat);
  		if (val1 > val2) {
  			struct ubifs_lprops *lp;
  
  			lp = heap->arr[cpos];
  			lp->flags &= ~LPROPS_CAT_MASK;
  			lp->flags |= LPROPS_UNCAT;
  			list_add(&lp->list, &c->uncat_list);
  			lprops->hpos = cpos;
  			heap->arr[cpos] = lprops;
  			move_up_lpt_heap(c, heap, lprops, cat);
  			dbg_check_heap(c, heap, cat, lprops->hpos);
  			return 1; /* Added to heap */
  		}
  		dbg_check_heap(c, heap, cat, -1);
  		return 0; /* Not added to heap */
  	} else {
  		lprops->hpos = heap->cnt++;
  		heap->arr[lprops->hpos] = lprops;
  		move_up_lpt_heap(c, heap, lprops, cat);
  		dbg_check_heap(c, heap, cat, lprops->hpos);
  		return 1; /* Added to heap */
  	}
  }
  
  /**
   * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties to remove
   * @cat: LEB category
   */
  static void remove_from_lpt_heap(struct ubifs_info *c,
  				 struct ubifs_lprops *lprops, int cat)
  {
  	struct ubifs_lpt_heap *heap;
  	int hpos = lprops->hpos;
  
  	heap = &c->lpt_heap[cat - 1];
6eb61d587   Richard Weinberger   ubifs: Pass struc...
221
222
  	ubifs_assert(c, hpos >= 0 && hpos < heap->cnt);
  	ubifs_assert(c, heap->arr[hpos] == lprops);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
223
224
225
226
227
228
229
230
231
232
233
234
  	heap->cnt -= 1;
  	if (hpos < heap->cnt) {
  		heap->arr[hpos] = heap->arr[heap->cnt];
  		heap->arr[hpos]->hpos = hpos;
  		adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
  	}
  	dbg_check_heap(c, heap, cat, -1);
  }
  
  /**
   * lpt_heap_replace - replace lprops in a category heap.
   * @c: UBIFS file-system description object
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
235
236
237
238
239
240
241
242
243
   * @new_lprops: LEB properties with which to replace
   * @cat: LEB category
   *
   * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
   * and the lprops that the pnode contains.  When that happens, references in
   * the category heaps to those lprops must be updated to point to the new
   * lprops.  This function does that.
   */
  static void lpt_heap_replace(struct ubifs_info *c,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  			     struct ubifs_lprops *new_lprops, int cat)
  {
  	struct ubifs_lpt_heap *heap;
  	int hpos = new_lprops->hpos;
  
  	heap = &c->lpt_heap[cat - 1];
  	heap->arr[hpos] = new_lprops;
  }
  
  /**
   * ubifs_add_to_cat - add LEB properties to a category list or heap.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties to add
   * @cat: LEB category to which to add
   *
   * LEB properties are categorized to enable fast find operations.
   */
  void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
  		      int cat)
  {
  	switch (cat) {
  	case LPROPS_DIRTY:
  	case LPROPS_DIRTY_IDX:
  	case LPROPS_FREE:
  		if (add_to_lpt_heap(c, lprops, cat))
  			break;
055da1b70   Artem Bityutskiy   UBIFS: various mi...
270
  		/* No more room on heap so make it un-categorized */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
271
  		cat = LPROPS_UNCAT;
df561f668   Gustavo A. R. Silva   treewide: Use fal...
272
  		fallthrough;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  	case LPROPS_UNCAT:
  		list_add(&lprops->list, &c->uncat_list);
  		break;
  	case LPROPS_EMPTY:
  		list_add(&lprops->list, &c->empty_list);
  		break;
  	case LPROPS_FREEABLE:
  		list_add(&lprops->list, &c->freeable_list);
  		c->freeable_cnt += 1;
  		break;
  	case LPROPS_FRDI_IDX:
  		list_add(&lprops->list, &c->frdi_idx_list);
  		break;
  	default:
6eb61d587   Richard Weinberger   ubifs: Pass struc...
287
  		ubifs_assert(c, 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
288
  	}
98a1eebda   Artem Bityutskiy   UBIFS: introduce ...
289

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
290
291
  	lprops->flags &= ~LPROPS_CAT_MASK;
  	lprops->flags |= cat;
98a1eebda   Artem Bityutskiy   UBIFS: introduce ...
292
  	c->in_a_category_cnt += 1;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
293
  	ubifs_assert(c, c->in_a_category_cnt <= c->main_lebs);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  }
  
  /**
   * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties to remove
   * @cat: LEB category from which to remove
   *
   * LEB properties are categorized to enable fast find operations.
   */
  static void ubifs_remove_from_cat(struct ubifs_info *c,
  				  struct ubifs_lprops *lprops, int cat)
  {
  	switch (cat) {
  	case LPROPS_DIRTY:
  	case LPROPS_DIRTY_IDX:
  	case LPROPS_FREE:
  		remove_from_lpt_heap(c, lprops, cat);
  		break;
  	case LPROPS_FREEABLE:
  		c->freeable_cnt -= 1;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
315
  		ubifs_assert(c, c->freeable_cnt >= 0);
df561f668   Gustavo A. R. Silva   treewide: Use fal...
316
  		fallthrough;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
317
318
319
  	case LPROPS_UNCAT:
  	case LPROPS_EMPTY:
  	case LPROPS_FRDI_IDX:
6eb61d587   Richard Weinberger   ubifs: Pass struc...
320
  		ubifs_assert(c, !list_empty(&lprops->list));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
321
322
323
  		list_del(&lprops->list);
  		break;
  	default:
6eb61d587   Richard Weinberger   ubifs: Pass struc...
324
  		ubifs_assert(c, 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
325
  	}
98a1eebda   Artem Bityutskiy   UBIFS: introduce ...
326
327
  
  	c->in_a_category_cnt -= 1;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
328
  	ubifs_assert(c, c->in_a_category_cnt >= 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  }
  
  /**
   * ubifs_replace_cat - replace lprops in a category list or heap.
   * @c: UBIFS file-system description object
   * @old_lprops: LEB properties to replace
   * @new_lprops: LEB properties with which to replace
   *
   * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
   * and the lprops that the pnode contains. When that happens, references in
   * category lists and heaps must be replaced. This function does that.
   */
  void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
  		       struct ubifs_lprops *new_lprops)
  {
  	int cat;
  
  	cat = new_lprops->flags & LPROPS_CAT_MASK;
  	switch (cat) {
  	case LPROPS_DIRTY:
  	case LPROPS_DIRTY_IDX:
  	case LPROPS_FREE:
4fb1cd823   Jiang Biao   ubifs: Remove use...
351
  		lpt_heap_replace(c, new_lprops, cat);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
352
353
354
355
356
357
358
359
  		break;
  	case LPROPS_UNCAT:
  	case LPROPS_EMPTY:
  	case LPROPS_FREEABLE:
  	case LPROPS_FRDI_IDX:
  		list_replace(&old_lprops->list, &new_lprops->list);
  		break;
  	default:
6eb61d587   Richard Weinberger   ubifs: Pass struc...
360
  		ubifs_assert(c, 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
361
362
363
364
365
366
367
368
369
  	}
  }
  
  /**
   * ubifs_ensure_cat - ensure LEB properties are categorized.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties
   *
   * A LEB may have fallen off of the bottom of a heap, and ended up as
055da1b70   Artem Bityutskiy   UBIFS: various mi...
370
371
   * un-categorized even though it has enough space for us now. If that is the
   * case this function will put the LEB back onto a heap.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
   */
  void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
  {
  	int cat = lprops->flags & LPROPS_CAT_MASK;
  
  	if (cat != LPROPS_UNCAT)
  		return;
  	cat = ubifs_categorize_lprops(c, lprops);
  	if (cat == LPROPS_UNCAT)
  		return;
  	ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
  	ubifs_add_to_cat(c, lprops, cat);
  }
  
  /**
   * ubifs_categorize_lprops - categorize LEB properties.
   * @c: UBIFS file-system description object
   * @lprops: LEB properties to categorize
   *
   * LEB properties are categorized to enable fast find operations. This function
   * returns the LEB category to which the LEB properties belong. Note however
   * that if the LEB category is stored as a heap and the heap is full, the
   * LEB properties may have their category changed to %LPROPS_UNCAT.
   */
  int ubifs_categorize_lprops(const struct ubifs_info *c,
  			    const struct ubifs_lprops *lprops)
  {
  	if (lprops->flags & LPROPS_TAKEN)
  		return LPROPS_UNCAT;
  
  	if (lprops->free == c->leb_size) {
6eb61d587   Richard Weinberger   ubifs: Pass struc...
403
  		ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  		return LPROPS_EMPTY;
  	}
  
  	if (lprops->free + lprops->dirty == c->leb_size) {
  		if (lprops->flags & LPROPS_INDEX)
  			return LPROPS_FRDI_IDX;
  		else
  			return LPROPS_FREEABLE;
  	}
  
  	if (lprops->flags & LPROPS_INDEX) {
  		if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
  			return LPROPS_DIRTY_IDX;
  	} else {
  		if (lprops->dirty >= c->dead_wm &&
  		    lprops->dirty > lprops->free)
  			return LPROPS_DIRTY;
  		if (lprops->free > 0)
  			return LPROPS_FREE;
  	}
  
  	return LPROPS_UNCAT;
  }
  
  /**
   * change_category - change LEB properties category.
   * @c: UBIFS file-system description object
055da1b70   Artem Bityutskiy   UBIFS: various mi...
431
   * @lprops: LEB properties to re-categorize
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
432
433
   *
   * LEB properties are categorized to enable fast find operations. When the LEB
055da1b70   Artem Bityutskiy   UBIFS: various mi...
434
   * properties change they must be re-categorized.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
435
436
437
438
439
440
441
   */
  static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
  {
  	int old_cat = lprops->flags & LPROPS_CAT_MASK;
  	int new_cat = ubifs_categorize_lprops(c, lprops);
  
  	if (old_cat == new_cat) {
273946a5c   Dan Carpenter   UBIFS: remove dou...
442
  		struct ubifs_lpt_heap *heap;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
443
444
445
446
447
448
449
450
451
452
453
454
455
  
  		/* lprops on a heap now must be moved up or down */
  		if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
  			return; /* Not on a heap */
  		heap = &c->lpt_heap[new_cat - 1];
  		adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
  	} else {
  		ubifs_remove_from_cat(c, lprops, old_cat);
  		ubifs_add_to_cat(c, lprops, new_cat);
  	}
  }
  
  /**
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
456
   * ubifs_calc_dark - calculate LEB dark space size.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
457
458
459
   * @c: the UBIFS file-system description object
   * @spc: amount of free and dirty space in the LEB
   *
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
460
461
   * This function calculates and returns amount of dark space in an LEB which
   * has @spc bytes of free and dirty space.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
462
   *
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
463
464
465
   * UBIFS is trying to account the space which might not be usable, and this
   * space is called "dark space". For example, if an LEB has only %512 free
   * bytes, it is dark space, because it cannot fit a large data node.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
466
   */
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
467
  int ubifs_calc_dark(const struct ubifs_info *c, int spc)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
468
  {
6eb61d587   Richard Weinberger   ubifs: Pass struc...
469
  	ubifs_assert(c, !(spc & 7));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  
  	if (spc < c->dark_wm)
  		return spc;
  
  	/*
  	 * If we have slightly more space then the dark space watermark, we can
  	 * anyway safely assume it we'll be able to write a node of the
  	 * smallest size there.
  	 */
  	if (spc - c->dark_wm < MIN_WRITE_SZ)
  		return spc - MIN_WRITE_SZ;
  
  	return c->dark_wm;
  }
  
  /**
   * is_lprops_dirty - determine if LEB properties are dirty.
   * @c: the UBIFS file-system description object
   * @lprops: LEB properties to test
   */
  static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
  {
  	struct ubifs_pnode *pnode;
  	int pos;
  
  	pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
  	pnode = (struct ubifs_pnode *)container_of(lprops - pos,
  						   struct ubifs_pnode,
  						   lprops[0]);
376624476   Artem Bityutskiy   UBIFS: use correc...
499
  	return !test_bit(COW_CNODE, &pnode->flags) &&
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
500
501
502
503
504
505
506
507
508
509
  	       test_bit(DIRTY_CNODE, &pnode->flags);
  }
  
  /**
   * ubifs_change_lp - change LEB properties.
   * @c: the UBIFS file-system description object
   * @lp: LEB properties to change
   * @free: new free space amount
   * @dirty: new dirty space amount
   * @flags: new flags
055da1b70   Artem Bityutskiy   UBIFS: various mi...
510
   * @idx_gc_cnt: change to the count of @idx_gc list
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
511
   *
d3cf502b6   Artem Bityutskiy   UBIFS: various co...
512
513
514
   * This function changes LEB properties (@free, @dirty or @flag). However, the
   * property which has the %LPROPS_NC value is not changed. Returns a pointer to
   * the updated LEB properties on success and a negative error code on failure.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
515
   *
d3cf502b6   Artem Bityutskiy   UBIFS: various co...
516
517
518
   * Note, the LEB properties may have had to be copied (due to COW) and
   * consequently the pointer returned may not be the same as the pointer
   * passed.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
519
520
521
522
523
524
525
526
   */
  const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
  					   const struct ubifs_lprops *lp,
  					   int free, int dirty, int flags,
  					   int idx_gc_cnt)
  {
  	/*
  	 * This is the only function that is allowed to change lprops, so we
055da1b70   Artem Bityutskiy   UBIFS: various mi...
527
  	 * discard the "const" qualifier.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
528
529
530
531
532
  	 */
  	struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
  
  	dbg_lp("LEB %d, free %d, dirty %d, flags %d",
  	       lprops->lnum, free, dirty, flags);
6eb61d587   Richard Weinberger   ubifs: Pass struc...
533
534
  	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
  	ubifs_assert(c, c->lst.empty_lebs >= 0 &&
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
535
  		     c->lst.empty_lebs <= c->main_lebs);
6eb61d587   Richard Weinberger   ubifs: Pass struc...
536
537
538
539
540
541
542
543
544
  	ubifs_assert(c, c->freeable_cnt >= 0);
  	ubifs_assert(c, c->freeable_cnt <= c->main_lebs);
  	ubifs_assert(c, c->lst.taken_empty_lebs >= 0);
  	ubifs_assert(c, c->lst.taken_empty_lebs <= c->lst.empty_lebs);
  	ubifs_assert(c, !(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
  	ubifs_assert(c, !(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
  	ubifs_assert(c, !(c->lst.total_used & 7));
  	ubifs_assert(c, free == LPROPS_NC || free >= 0);
  	ubifs_assert(c, dirty == LPROPS_NC || dirty >= 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
545
546
547
548
549
550
  
  	if (!is_lprops_dirty(c, lprops)) {
  		lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
  		if (IS_ERR(lprops))
  			return lprops;
  	} else
6eb61d587   Richard Weinberger   ubifs: Pass struc...
551
  		ubifs_assert(c, lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
552

6eb61d587   Richard Weinberger   ubifs: Pass struc...
553
  	ubifs_assert(c, !(lprops->free & 7) && !(lprops->dirty & 7));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
554
555
  
  	spin_lock(&c->space_lock);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
556
557
558
559
560
561
562
563
564
565
  	if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
  		c->lst.taken_empty_lebs -= 1;
  
  	if (!(lprops->flags & LPROPS_INDEX)) {
  		int old_spc;
  
  		old_spc = lprops->free + lprops->dirty;
  		if (old_spc < c->dead_wm)
  			c->lst.total_dead -= old_spc;
  		else
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
566
  			c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  
  		c->lst.total_used -= c->leb_size - old_spc;
  	}
  
  	if (free != LPROPS_NC) {
  		free = ALIGN(free, 8);
  		c->lst.total_free += free - lprops->free;
  
  		/* Increase or decrease empty LEBs counter if needed */
  		if (free == c->leb_size) {
  			if (lprops->free != c->leb_size)
  				c->lst.empty_lebs += 1;
  		} else if (lprops->free == c->leb_size)
  			c->lst.empty_lebs -= 1;
  		lprops->free = free;
  	}
  
  	if (dirty != LPROPS_NC) {
  		dirty = ALIGN(dirty, 8);
  		c->lst.total_dirty += dirty - lprops->dirty;
  		lprops->dirty = dirty;
  	}
  
  	if (flags != LPROPS_NC) {
  		/* Take care about indexing LEBs counter if needed */
  		if ((lprops->flags & LPROPS_INDEX)) {
  			if (!(flags & LPROPS_INDEX))
  				c->lst.idx_lebs -= 1;
  		} else if (flags & LPROPS_INDEX)
  			c->lst.idx_lebs += 1;
  		lprops->flags = flags;
  	}
  
  	if (!(lprops->flags & LPROPS_INDEX)) {
  		int new_spc;
  
  		new_spc = lprops->free + lprops->dirty;
  		if (new_spc < c->dead_wm)
  			c->lst.total_dead += new_spc;
  		else
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
607
  			c->lst.total_dark += ubifs_calc_dark(c, new_spc);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
608
609
610
611
612
613
614
615
  
  		c->lst.total_used += c->leb_size - new_spc;
  	}
  
  	if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
  		c->lst.taken_empty_lebs += 1;
  
  	change_category(c, lprops);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
616
  	c->idx_gc_cnt += idx_gc_cnt;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
617
  	spin_unlock(&c->space_lock);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
618
619
  	return lprops;
  }
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
620
621
622
  /**
   * ubifs_get_lp_stats - get lprops statistics.
   * @c: UBIFS file-system description object
ec037dfcc   Julia Lawall   UBIFS: improve fu...
623
   * @lst: return statistics
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
624
   */
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
625
  void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
626
627
  {
  	spin_lock(&c->space_lock);
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
628
  	memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
  	spin_unlock(&c->space_lock);
  }
  
  /**
   * ubifs_change_one_lp - change LEB properties.
   * @c: the UBIFS file-system description object
   * @lnum: LEB to change properties for
   * @free: amount of free space
   * @dirty: amount of dirty space
   * @flags_set: flags to set
   * @flags_clean: flags to clean
   * @idx_gc_cnt: change to the count of idx_gc list
   *
   * This function changes properties of LEB @lnum. It is a helper wrapper over
   * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
   * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
   * a negative error code in case of failure.
   */
  int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
  			int flags_set, int flags_clean, int idx_gc_cnt)
  {
  	int err = 0, flags;
  	const struct ubifs_lprops *lp;
  
  	ubifs_get_lprops(c);
  
  	lp = ubifs_lpt_lookup_dirty(c, lnum);
  	if (IS_ERR(lp)) {
  		err = PTR_ERR(lp);
  		goto out;
  	}
  
  	flags = (lp->flags | flags_set) & ~flags_clean;
  	lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
  	if (IS_ERR(lp))
  		err = PTR_ERR(lp);
  
  out:
  	ubifs_release_lprops(c);
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
668
  	if (err)
235c362bd   Sheng Yong   UBIFS: extend deb...
669
  		ubifs_err(c, "cannot change properties of LEB %d, error %d",
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
670
  			  lnum, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
  	return err;
  }
  
  /**
   * ubifs_update_one_lp - update LEB properties.
   * @c: the UBIFS file-system description object
   * @lnum: LEB to change properties for
   * @free: amount of free space
   * @dirty: amount of dirty space to add
   * @flags_set: flags to set
   * @flags_clean: flags to clean
   *
   * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
   * current dirty space, not substitutes it.
   */
  int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
  			int flags_set, int flags_clean)
  {
  	int err = 0, flags;
  	const struct ubifs_lprops *lp;
  
  	ubifs_get_lprops(c);
  
  	lp = ubifs_lpt_lookup_dirty(c, lnum);
  	if (IS_ERR(lp)) {
  		err = PTR_ERR(lp);
  		goto out;
  	}
  
  	flags = (lp->flags | flags_set) & ~flags_clean;
  	lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
  	if (IS_ERR(lp))
  		err = PTR_ERR(lp);
  
  out:
  	ubifs_release_lprops(c);
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
707
  	if (err)
235c362bd   Sheng Yong   UBIFS: extend deb...
708
  		ubifs_err(c, "cannot update properties of LEB %d, error %d",
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
709
  			  lnum, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  	return err;
  }
  
  /**
   * ubifs_read_one_lp - read LEB properties.
   * @c: the UBIFS file-system description object
   * @lnum: LEB to read properties for
   * @lp: where to store read properties
   *
   * This helper function reads properties of a LEB @lnum and stores them in @lp.
   * Returns zero in case of success and a negative error code in case of
   * failure.
   */
  int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
  {
  	int err = 0;
  	const struct ubifs_lprops *lpp;
  
  	ubifs_get_lprops(c);
  
  	lpp = ubifs_lpt_lookup(c, lnum);
  	if (IS_ERR(lpp)) {
  		err = PTR_ERR(lpp);
235c362bd   Sheng Yong   UBIFS: extend deb...
733
  		ubifs_err(c, "cannot read properties of LEB %d, error %d",
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
734
  			  lnum, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  		goto out;
  	}
  
  	memcpy(lp, lpp, sizeof(struct ubifs_lprops));
  
  out:
  	ubifs_release_lprops(c);
  	return err;
  }
  
  /**
   * ubifs_fast_find_free - try to find a LEB with free space quickly.
   * @c: the UBIFS file-system description object
   *
   * This function returns LEB properties for a LEB with free space or %NULL if
   * the function is unable to find a LEB quickly.
   */
  const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
  {
  	struct ubifs_lprops *lprops;
  	struct ubifs_lpt_heap *heap;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
756
  	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
757
758
759
760
761
762
  
  	heap = &c->lpt_heap[LPROPS_FREE - 1];
  	if (heap->cnt == 0)
  		return NULL;
  
  	lprops = heap->arr[0];
6eb61d587   Richard Weinberger   ubifs: Pass struc...
763
764
  	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
  	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
765
766
767
768
769
770
771
772
773
774
775
776
777
  	return lprops;
  }
  
  /**
   * ubifs_fast_find_empty - try to find an empty LEB quickly.
   * @c: the UBIFS file-system description object
   *
   * This function returns LEB properties for an empty LEB or %NULL if the
   * function is unable to find an empty LEB quickly.
   */
  const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
  {
  	struct ubifs_lprops *lprops;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
778
  	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
779
780
781
782
783
  
  	if (list_empty(&c->empty_list))
  		return NULL;
  
  	lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
6eb61d587   Richard Weinberger   ubifs: Pass struc...
784
785
786
  	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
  	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
  	ubifs_assert(c, lprops->free == c->leb_size);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
787
788
789
790
791
792
793
794
795
796
797
798
799
  	return lprops;
  }
  
  /**
   * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
   * @c: the UBIFS file-system description object
   *
   * This function returns LEB properties for a freeable LEB or %NULL if the
   * function is unable to find a freeable LEB quickly.
   */
  const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
  {
  	struct ubifs_lprops *lprops;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
800
  	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
801
802
803
804
805
  
  	if (list_empty(&c->freeable_list))
  		return NULL;
  
  	lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
6eb61d587   Richard Weinberger   ubifs: Pass struc...
806
807
808
809
  	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
  	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
  	ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
  	ubifs_assert(c, c->freeable_cnt > 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
810
811
812
813
814
815
816
817
818
819
820
821
822
  	return lprops;
  }
  
  /**
   * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
   * @c: the UBIFS file-system description object
   *
   * This function returns LEB properties for a freeable index LEB or %NULL if the
   * function is unable to find a freeable index LEB quickly.
   */
  const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
  {
  	struct ubifs_lprops *lprops;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
823
  	ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
824
825
826
827
828
  
  	if (list_empty(&c->frdi_idx_list))
  		return NULL;
  
  	lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
6eb61d587   Richard Weinberger   ubifs: Pass struc...
829
830
831
  	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
  	ubifs_assert(c, (lprops->flags & LPROPS_INDEX));
  	ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
832
833
  	return lprops;
  }
f70b7e52a   Artem Bityutskiy   UBIFS: remove Kco...
834
835
836
  /*
   * Everything below is related to debugging.
   */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
837
838
839
840
841
842
843
844
845
846
847
848
  
  /**
   * dbg_check_cats - check category heaps and lists.
   * @c: UBIFS file-system description object
   *
   * This function returns %0 on success and a negative error code on failure.
   */
  int dbg_check_cats(struct ubifs_info *c)
  {
  	struct ubifs_lprops *lprops;
  	struct list_head *pos;
  	int i, cat;
2b1844a8c   Artem Bityutskiy   UBIFS: introduce ...
849
  	if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
850
851
852
853
  		return 0;
  
  	list_for_each_entry(lprops, &c->empty_list, list) {
  		if (lprops->free != c->leb_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
854
  			ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
855
856
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
857
858
859
  			return -EINVAL;
  		}
  		if (lprops->flags & LPROPS_TAKEN) {
235c362bd   Sheng Yong   UBIFS: extend deb...
860
  			ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
861
862
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
863
864
865
866
867
868
869
  			return -EINVAL;
  		}
  	}
  
  	i = 0;
  	list_for_each_entry(lprops, &c->freeable_list, list) {
  		if (lprops->free + lprops->dirty != c->leb_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
870
  			ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
871
872
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
873
874
875
  			return -EINVAL;
  		}
  		if (lprops->flags & LPROPS_TAKEN) {
235c362bd   Sheng Yong   UBIFS: extend deb...
876
  			ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
877
878
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
879
880
881
882
883
  			return -EINVAL;
  		}
  		i += 1;
  	}
  	if (i != c->freeable_cnt) {
235c362bd   Sheng Yong   UBIFS: extend deb...
884
  		ubifs_err(c, "freeable list count %d expected %d", i,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
885
886
887
888
889
890
891
892
  			  c->freeable_cnt);
  		return -EINVAL;
  	}
  
  	i = 0;
  	list_for_each(pos, &c->idx_gc)
  		i += 1;
  	if (i != c->idx_gc_cnt) {
235c362bd   Sheng Yong   UBIFS: extend deb...
893
  		ubifs_err(c, "idx_gc list count %d expected %d", i,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
894
895
896
897
898
899
  			  c->idx_gc_cnt);
  		return -EINVAL;
  	}
  
  	list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  		if (lprops->free + lprops->dirty != c->leb_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
900
  			ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
901
902
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
903
904
905
  			return -EINVAL;
  		}
  		if (lprops->flags & LPROPS_TAKEN) {
235c362bd   Sheng Yong   UBIFS: extend deb...
906
  			ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
907
908
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
909
910
911
  			return -EINVAL;
  		}
  		if (!(lprops->flags & LPROPS_INDEX)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
912
  			ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
913
914
  				  lprops->lnum, lprops->free, lprops->dirty,
  				  lprops->flags);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
915
916
917
918
919
920
921
922
923
924
  			return -EINVAL;
  		}
  	}
  
  	for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
  		struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
  
  		for (i = 0; i < heap->cnt; i++) {
  			lprops = heap->arr[i];
  			if (!lprops) {
235c362bd   Sheng Yong   UBIFS: extend deb...
925
  				ubifs_err(c, "null ptr in LPT heap cat %d", cat);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
926
927
928
  				return -EINVAL;
  			}
  			if (lprops->hpos != i) {
235c362bd   Sheng Yong   UBIFS: extend deb...
929
  				ubifs_err(c, "bad ptr in LPT heap cat %d", cat);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
930
931
932
  				return -EINVAL;
  			}
  			if (lprops->flags & LPROPS_TAKEN) {
235c362bd   Sheng Yong   UBIFS: extend deb...
933
  				ubifs_err(c, "taken LEB in LPT heap cat %d", cat);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
934
935
936
937
938
939
940
941
942
943
944
945
  				return -EINVAL;
  			}
  		}
  	}
  
  	return 0;
  }
  
  void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
  		    int add_pos)
  {
  	int i = 0, j, err = 0;
2b1844a8c   Artem Bityutskiy   UBIFS: introduce ...
946
  	if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  		return;
  
  	for (i = 0; i < heap->cnt; i++) {
  		struct ubifs_lprops *lprops = heap->arr[i];
  		struct ubifs_lprops *lp;
  
  		if (i != add_pos)
  			if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
  				err = 1;
  				goto out;
  			}
  		if (lprops->hpos != i) {
  			err = 2;
  			goto out;
  		}
  		lp = ubifs_lpt_lookup(c, lprops->lnum);
  		if (IS_ERR(lp)) {
  			err = 3;
  			goto out;
  		}
  		if (lprops != lp) {
235c362bd   Sheng Yong   UBIFS: extend deb...
968
  			ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
3668b70fc   Artem Bityutskiy   UBIFS: print less
969
970
  				  (size_t)lprops, (size_t)lp, lprops->lnum,
  				  lp->lnum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  			err = 4;
  			goto out;
  		}
  		for (j = 0; j < i; j++) {
  			lp = heap->arr[j];
  			if (lp == lprops) {
  				err = 5;
  				goto out;
  			}
  			if (lp->lnum == lprops->lnum) {
  				err = 6;
  				goto out;
  			}
  		}
  	}
  out:
  	if (err) {
235c362bd   Sheng Yong   UBIFS: extend deb...
988
  		ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err);
7c46d0ae2   Artem Bityutskiy   UBIFS: get rid of...
989
  		dump_stack();
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
990
  		ubifs_dump_heap(c, heap, cat);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
991
992
993
994
  	}
  }
  
  /**
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
995
996
997
998
   * scan_check_cb - scan callback.
   * @c: the UBIFS file-system description object
   * @lp: LEB properties to scan
   * @in_tree: whether the LEB properties are in main memory
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
999
   * @lst: lprops statistics to update
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1000
1001
1002
1003
1004
1005
1006
1007
   *
   * This function returns a code that indicates whether the scan should continue
   * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
   * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
   * (%LPT_SCAN_STOP).
   */
  static int scan_check_cb(struct ubifs_info *c,
  			 const struct ubifs_lprops *lp, int in_tree,
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1008
  			 struct ubifs_lp_stats *lst)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1009
1010
1011
  {
  	struct ubifs_scan_leb *sleb;
  	struct ubifs_scan_node *snod;
cd5f7485b   Artem Bityutskiy   UBIFS: allocate s...
1012
1013
  	int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret;
  	void *buf = NULL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1014
1015
1016
1017
1018
  
  	cat = lp->flags & LPROPS_CAT_MASK;
  	if (cat != LPROPS_UNCAT) {
  		cat = ubifs_categorize_lprops(c, lp);
  		if (cat != (lp->flags & LPROPS_CAT_MASK)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1019
  			ubifs_err(c, "bad LEB category %d expected %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1020
  				  (lp->flags & LPROPS_CAT_MASK), cat);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1021
  			return -EINVAL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  		}
  	}
  
  	/* Check lp is on its category list (if it has one) */
  	if (in_tree) {
  		struct list_head *list = NULL;
  
  		switch (cat) {
  		case LPROPS_EMPTY:
  			list = &c->empty_list;
  			break;
  		case LPROPS_FREEABLE:
  			list = &c->freeable_list;
  			break;
  		case LPROPS_FRDI_IDX:
  			list = &c->frdi_idx_list;
  			break;
  		case LPROPS_UNCAT:
  			list = &c->uncat_list;
  			break;
  		}
  		if (list) {
  			struct ubifs_lprops *lprops;
  			int found = 0;
  
  			list_for_each_entry(lprops, list, list) {
  				if (lprops == lp) {
  					found = 1;
  					break;
  				}
  			}
  			if (!found) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1054
  				ubifs_err(c, "bad LPT list (category %d)", cat);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1055
  				return -EINVAL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
  			}
  		}
  	}
  
  	/* Check lp is on its category heap (if it has one) */
  	if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
  		struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
  
  		if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
  		    lp != heap->arr[lp->hpos]) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1066
  			ubifs_err(c, "bad LPT heap (category %d)", cat);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1067
  			return -EINVAL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1068
1069
  		}
  	}
8ca5175b0   Artem Bityutskiy   UBIFS: improve de...
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
  	/*
  	 * After an unclean unmount, empty and freeable LEBs
  	 * may contain garbage - do not scan them.
  	 */
  	if (lp->free == c->leb_size) {
  		lst->empty_lebs += 1;
  		lst->total_free += c->leb_size;
  		lst->total_dark += ubifs_calc_dark(c, c->leb_size);
  		return LPT_SCAN_CONTINUE;
  	}
  	if (lp->free + lp->dirty == c->leb_size &&
  	    !(lp->flags & LPROPS_INDEX)) {
  		lst->total_free  += lp->free;
  		lst->total_dirty += lp->dirty;
  		lst->total_dark  +=  ubifs_calc_dark(c, c->leb_size);
  		return LPT_SCAN_CONTINUE;
  	}
88dca4ca5   Christoph Hellwig   mm: remove the pg...
1087
  	buf = __vmalloc(c->leb_size, GFP_NOFS);
eef19816a   Richard Weinberger   ubifs: Fix memory...
1088
1089
  	if (!buf)
  		return -ENOMEM;
cd5f7485b   Artem Bityutskiy   UBIFS: allocate s...
1090
  	sleb = ubifs_scan(c, lnum, 0, buf, 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1091
  	if (IS_ERR(sleb)) {
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1092
  		ret = PTR_ERR(sleb);
12346037a   Artem Bityutskiy   UBIFS: dump more ...
1093
  		if (ret == -EUCLEAN) {
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
1094
1095
  			ubifs_dump_lprops(c);
  			ubifs_dump_budg(c, &c->bi);
12346037a   Artem Bityutskiy   UBIFS: dump more ...
1096
  		}
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1097
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  	}
  
  	is_idx = -1;
  	list_for_each_entry(snod, &sleb->nodes, list) {
  		int found, level = 0;
  
  		cond_resched();
  
  		if (is_idx == -1)
  			is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
  
  		if (is_idx && snod->type != UBIFS_IDX_NODE) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1110
  			ubifs_err(c, "indexing node in data LEB %d:%d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
  				  lnum, snod->offs);
  			goto out_destroy;
  		}
  
  		if (snod->type == UBIFS_IDX_NODE) {
  			struct ubifs_idx_node *idx = snod->node;
  
  			key_read(c, ubifs_idx_key(c, idx), &snod->key);
  			level = le16_to_cpu(idx->level);
  		}
  
  		found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
  					   snod->offs, is_idx);
  		if (found) {
  			if (found < 0)
  				goto out_destroy;
  			used += ALIGN(snod->len, 8);
  		}
  	}
  
  	free = c->leb_size - sleb->endpt;
  	dirty = sleb->endpt - used;
  
  	if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
  	    dirty < 0) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1136
  		ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
1137
  			  lnum, free, dirty);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
  		goto out_destroy;
  	}
  
  	if (lp->free + lp->dirty == c->leb_size &&
  	    free + dirty == c->leb_size)
  		if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
  		    (!is_idx && free == c->leb_size) ||
  		    lp->free == c->leb_size) {
  			/*
  			 * Empty or freeable LEBs could contain index
  			 * nodes from an uncompleted commit due to an
  			 * unclean unmount. Or they could be empty for
  			 * the same reason. Or it may simply not have been
  			 * unmapped.
  			 */
  			free = lp->free;
  			dirty = lp->dirty;
  			is_idx = 0;
  		    }
  
  	if (is_idx && lp->free + lp->dirty == free + dirty &&
  	    lnum != c->ihead_lnum) {
  		/*
  		 * After an unclean unmount, an index LEB could have a different
  		 * amount of free space than the value recorded by lprops. That
  		 * is because the in-the-gaps method may use free space or
  		 * create free space (as a side-effect of using ubi_leb_change
  		 * and not writing the whole LEB). The incorrect free space
  		 * value is not a problem because the index is only ever
  		 * allocated empty LEBs, so there will never be an attempt to
  		 * write to the free space at the end of an index LEB - except
  		 * by the in-the-gaps method for which it is not a problem.
  		 */
  		free = lp->free;
  		dirty = lp->dirty;
  	}
  
  	if (lp->free != free || lp->dirty != dirty)
  		goto out_print;
  
  	if (is_idx && !(lp->flags & LPROPS_INDEX)) {
  		if (free == c->leb_size)
  			/* Free but not unmapped LEB, it's fine */
  			is_idx = 0;
  		else {
235c362bd   Sheng Yong   UBIFS: extend deb...
1183
  			ubifs_err(c, "indexing node without indexing flag");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1184
1185
1186
1187
1188
  			goto out_print;
  		}
  	}
  
  	if (!is_idx && (lp->flags & LPROPS_INDEX)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1189
  		ubifs_err(c, "data node with indexing flag");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
  		goto out_print;
  	}
  
  	if (free == c->leb_size)
  		lst->empty_lebs += 1;
  
  	if (is_idx)
  		lst->idx_lebs += 1;
  
  	if (!(lp->flags & LPROPS_INDEX))
  		lst->total_used += c->leb_size - free - dirty;
  	lst->total_free += free;
  	lst->total_dirty += dirty;
  
  	if (!(lp->flags & LPROPS_INDEX)) {
  		int spc = free + dirty;
  
  		if (spc < c->dead_wm)
  			lst->total_dead += spc;
  		else
be9e62a73   Artem Bityutskiy   UBIFS: improve lp...
1210
  			lst->total_dark += ubifs_calc_dark(c, spc);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1211
1212
1213
  	}
  
  	ubifs_scan_destroy(sleb);
cd5f7485b   Artem Bityutskiy   UBIFS: allocate s...
1214
  	vfree(buf);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1215
  	return LPT_SCAN_CONTINUE;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1216
1217
  
  out_print:
235c362bd   Sheng Yong   UBIFS: extend deb...
1218
  	ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1219
  		  lnum, lp->free, lp->dirty, lp->flags, free, dirty);
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
1220
  	ubifs_dump_leb(c, lnum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1221
1222
  out_destroy:
  	ubifs_scan_destroy(sleb);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1223
  	ret = -EINVAL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1224
  out:
cd5f7485b   Artem Bityutskiy   UBIFS: allocate s...
1225
  	vfree(buf);
dcc50c8ee   Artem Bityutskiy   UBIFS: simplify e...
1226
  	return ret;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
  }
  
  /**
   * dbg_check_lprops - check all LEB properties.
   * @c: UBIFS file-system description object
   *
   * This function checks all LEB properties and makes sure they are all correct.
   * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
   * and other negative error codes in case of other errors. This function is
   * called while the file system is locked (because of commit start), so no
   * additional locking is required. Note that locking the LPT mutex would cause
   * a circular lock dependency with the TNC mutex.
   */
  int dbg_check_lprops(struct ubifs_info *c)
  {
  	int i, err;
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1243
  	struct ubifs_lp_stats lst;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1244

2b1844a8c   Artem Bityutskiy   UBIFS: introduce ...
1245
  	if (!dbg_is_chk_lprops(c))
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
  		return 0;
  
  	/*
  	 * As we are going to scan the media, the write buffers have to be
  	 * synchronized.
  	 */
  	for (i = 0; i < c->jhead_cnt; i++) {
  		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  		if (err)
  			return err;
  	}
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1257
  	memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1258
1259
  	err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
  				    (ubifs_lpt_scan_callback)scan_check_cb,
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1260
  				    &lst);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1261
1262
  	if (err && err != -ENOSPC)
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1263

34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1264
1265
1266
1267
1268
  	if (lst.empty_lebs != c->lst.empty_lebs ||
  	    lst.idx_lebs != c->lst.idx_lebs ||
  	    lst.total_free != c->lst.total_free ||
  	    lst.total_dirty != c->lst.total_dirty ||
  	    lst.total_used != c->lst.total_used) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1269
1270
  		ubifs_err(c, "bad overall accounting");
  		ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1271
1272
  			  lst.empty_lebs, lst.idx_lebs, lst.total_free,
  			  lst.total_dirty, lst.total_used);
235c362bd   Sheng Yong   UBIFS: extend deb...
1273
  		ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1274
1275
1276
1277
1278
  			  c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
  			  c->lst.total_dirty, c->lst.total_used);
  		err = -EINVAL;
  		goto out;
  	}
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1279
1280
  	if (lst.total_dead != c->lst.total_dead ||
  	    lst.total_dark != c->lst.total_dark) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1281
1282
  		ubifs_err(c, "bad dead/dark space accounting");
  		ubifs_err(c, "calculated: total_dead %lld, total_dark %lld",
34bdc3e25   Artem Bityutskiy   UBIFS: simplify l...
1283
  			  lst.total_dead, lst.total_dark);
235c362bd   Sheng Yong   UBIFS: extend deb...
1284
  		ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1285
1286
1287
1288
1289
1290
1291
1292
1293
  			  c->lst.total_dead, c->lst.total_dark);
  		err = -EINVAL;
  		goto out;
  	}
  
  	err = dbg_check_cats(c);
  out:
  	return err;
  }