lprops.c 36 KB

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