lprops.c 36 KB

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