lprops.c 35 KB

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