lprops.c 35 KB

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