lprops.c 35 KB

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