lprops.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 uncategorized */
  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. * uncategorized even though it has enough space for us now. If that is the case
  355. * 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 recategorize
  409. *
  410. * LEB properties are categorized to enable fast find operations. When the LEB
  411. * properties change they must be recategorized.
  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. * 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 amount of dark space in an LEB which has @spc bytes
  435. * of free and dirty space. Returns the calculations result.
  436. *
  437. * Dark space is the space which is not always usable - it depends on which
  438. * nodes are written in which order. E.g., if an LEB has only 512 free bytes,
  439. * it is dark space, because it cannot fit a large data node. So UBIFS cannot
  440. * count on this LEB and treat these 512 bytes as usable because it is not true
  441. * if, for example, only big chunks of uncompressible data will be written to
  442. * the FS.
  443. */
  444. static int calc_dark(struct ubifs_info *c, int spc)
  445. {
  446. ubifs_assert(!(spc & 7));
  447. if (spc < c->dark_wm)
  448. return spc;
  449. /*
  450. * If we have slightly more space then the dark space watermark, we can
  451. * anyway safely assume it we'll be able to write a node of the
  452. * smallest size there.
  453. */
  454. if (spc - c->dark_wm < MIN_WRITE_SZ)
  455. return spc - MIN_WRITE_SZ;
  456. return c->dark_wm;
  457. }
  458. /**
  459. * is_lprops_dirty - determine if LEB properties are dirty.
  460. * @c: the UBIFS file-system description object
  461. * @lprops: LEB properties to test
  462. */
  463. static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
  464. {
  465. struct ubifs_pnode *pnode;
  466. int pos;
  467. pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
  468. pnode = (struct ubifs_pnode *)container_of(lprops - pos,
  469. struct ubifs_pnode,
  470. lprops[0]);
  471. return !test_bit(COW_ZNODE, &pnode->flags) &&
  472. test_bit(DIRTY_CNODE, &pnode->flags);
  473. }
  474. /**
  475. * ubifs_change_lp - change LEB properties.
  476. * @c: the UBIFS file-system description object
  477. * @lp: LEB properties to change
  478. * @free: new free space amount
  479. * @dirty: new dirty space amount
  480. * @flags: new flags
  481. * @idx_gc_cnt: change to the count of idx_gc list
  482. *
  483. * This function changes LEB properties (@free, @dirty or @flag). However, the
  484. * property which has the %LPROPS_NC value is not changed. Returns a pointer to
  485. * the updated LEB properties on success and a negative error code on failure.
  486. *
  487. * Note, the LEB properties may have had to be copied (due to COW) and
  488. * consequently the pointer returned may not be the same as the pointer
  489. * passed.
  490. */
  491. const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
  492. const struct ubifs_lprops *lp,
  493. int free, int dirty, int flags,
  494. int idx_gc_cnt)
  495. {
  496. /*
  497. * This is the only function that is allowed to change lprops, so we
  498. * discard the const qualifier.
  499. */
  500. struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
  501. dbg_lp("LEB %d, free %d, dirty %d, flags %d",
  502. lprops->lnum, free, dirty, flags);
  503. ubifs_assert(mutex_is_locked(&c->lp_mutex));
  504. ubifs_assert(c->lst.empty_lebs >= 0 &&
  505. c->lst.empty_lebs <= c->main_lebs);
  506. ubifs_assert(c->freeable_cnt >= 0);
  507. ubifs_assert(c->freeable_cnt <= c->main_lebs);
  508. ubifs_assert(c->lst.taken_empty_lebs >= 0);
  509. ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
  510. ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
  511. ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
  512. ubifs_assert(!(c->lst.total_used & 7));
  513. ubifs_assert(free == LPROPS_NC || free >= 0);
  514. ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
  515. if (!is_lprops_dirty(c, lprops)) {
  516. lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
  517. if (IS_ERR(lprops))
  518. return lprops;
  519. } else
  520. ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
  521. ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
  522. spin_lock(&c->space_lock);
  523. if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
  524. c->lst.taken_empty_lebs -= 1;
  525. if (!(lprops->flags & LPROPS_INDEX)) {
  526. int old_spc;
  527. old_spc = lprops->free + lprops->dirty;
  528. if (old_spc < c->dead_wm)
  529. c->lst.total_dead -= old_spc;
  530. else
  531. c->lst.total_dark -= calc_dark(c, old_spc);
  532. c->lst.total_used -= c->leb_size - old_spc;
  533. }
  534. if (free != LPROPS_NC) {
  535. free = ALIGN(free, 8);
  536. c->lst.total_free += free - lprops->free;
  537. /* Increase or decrease empty LEBs counter if needed */
  538. if (free == c->leb_size) {
  539. if (lprops->free != c->leb_size)
  540. c->lst.empty_lebs += 1;
  541. } else if (lprops->free == c->leb_size)
  542. c->lst.empty_lebs -= 1;
  543. lprops->free = free;
  544. }
  545. if (dirty != LPROPS_NC) {
  546. dirty = ALIGN(dirty, 8);
  547. c->lst.total_dirty += dirty - lprops->dirty;
  548. lprops->dirty = dirty;
  549. }
  550. if (flags != LPROPS_NC) {
  551. /* Take care about indexing LEBs counter if needed */
  552. if ((lprops->flags & LPROPS_INDEX)) {
  553. if (!(flags & LPROPS_INDEX))
  554. c->lst.idx_lebs -= 1;
  555. } else if (flags & LPROPS_INDEX)
  556. c->lst.idx_lebs += 1;
  557. lprops->flags = flags;
  558. }
  559. if (!(lprops->flags & LPROPS_INDEX)) {
  560. int new_spc;
  561. new_spc = lprops->free + lprops->dirty;
  562. if (new_spc < c->dead_wm)
  563. c->lst.total_dead += new_spc;
  564. else
  565. c->lst.total_dark += calc_dark(c, new_spc);
  566. c->lst.total_used += c->leb_size - new_spc;
  567. }
  568. if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
  569. c->lst.taken_empty_lebs += 1;
  570. change_category(c, lprops);
  571. c->idx_gc_cnt += idx_gc_cnt;
  572. spin_unlock(&c->space_lock);
  573. return lprops;
  574. }
  575. /**
  576. * ubifs_get_lp_stats - get lprops statistics.
  577. * @c: UBIFS file-system description object
  578. * @st: return statistics
  579. */
  580. void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
  581. {
  582. spin_lock(&c->space_lock);
  583. memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
  584. spin_unlock(&c->space_lock);
  585. }
  586. /**
  587. * ubifs_change_one_lp - change LEB properties.
  588. * @c: the UBIFS file-system description object
  589. * @lnum: LEB to change properties for
  590. * @free: amount of free space
  591. * @dirty: amount of dirty space
  592. * @flags_set: flags to set
  593. * @flags_clean: flags to clean
  594. * @idx_gc_cnt: change to the count of idx_gc list
  595. *
  596. * This function changes properties of LEB @lnum. It is a helper wrapper over
  597. * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
  598. * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
  599. * a negative error code in case of failure.
  600. */
  601. int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
  602. int flags_set, int flags_clean, int idx_gc_cnt)
  603. {
  604. int err = 0, flags;
  605. const struct ubifs_lprops *lp;
  606. ubifs_get_lprops(c);
  607. lp = ubifs_lpt_lookup_dirty(c, lnum);
  608. if (IS_ERR(lp)) {
  609. err = PTR_ERR(lp);
  610. goto out;
  611. }
  612. flags = (lp->flags | flags_set) & ~flags_clean;
  613. lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
  614. if (IS_ERR(lp))
  615. err = PTR_ERR(lp);
  616. out:
  617. ubifs_release_lprops(c);
  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. return err;
  650. }
  651. /**
  652. * ubifs_read_one_lp - read LEB properties.
  653. * @c: the UBIFS file-system description object
  654. * @lnum: LEB to read properties for
  655. * @lp: where to store read properties
  656. *
  657. * This helper function reads properties of a LEB @lnum and stores them in @lp.
  658. * Returns zero in case of success and a negative error code in case of
  659. * failure.
  660. */
  661. int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
  662. {
  663. int err = 0;
  664. const struct ubifs_lprops *lpp;
  665. ubifs_get_lprops(c);
  666. lpp = ubifs_lpt_lookup(c, lnum);
  667. if (IS_ERR(lpp)) {
  668. err = PTR_ERR(lpp);
  669. goto out;
  670. }
  671. memcpy(lp, lpp, sizeof(struct ubifs_lprops));
  672. out:
  673. ubifs_release_lprops(c);
  674. return err;
  675. }
  676. /**
  677. * ubifs_fast_find_free - try to find a LEB with free space quickly.
  678. * @c: the UBIFS file-system description object
  679. *
  680. * This function returns LEB properties for a LEB with free space or %NULL if
  681. * the function is unable to find a LEB quickly.
  682. */
  683. const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
  684. {
  685. struct ubifs_lprops *lprops;
  686. struct ubifs_lpt_heap *heap;
  687. ubifs_assert(mutex_is_locked(&c->lp_mutex));
  688. heap = &c->lpt_heap[LPROPS_FREE - 1];
  689. if (heap->cnt == 0)
  690. return NULL;
  691. lprops = heap->arr[0];
  692. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  693. ubifs_assert(!(lprops->flags & LPROPS_INDEX));
  694. return lprops;
  695. }
  696. /**
  697. * ubifs_fast_find_empty - try to find an empty LEB quickly.
  698. * @c: the UBIFS file-system description object
  699. *
  700. * This function returns LEB properties for an empty LEB or %NULL if the
  701. * function is unable to find an empty LEB quickly.
  702. */
  703. const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
  704. {
  705. struct ubifs_lprops *lprops;
  706. ubifs_assert(mutex_is_locked(&c->lp_mutex));
  707. if (list_empty(&c->empty_list))
  708. return NULL;
  709. lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
  710. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  711. ubifs_assert(!(lprops->flags & LPROPS_INDEX));
  712. ubifs_assert(lprops->free == c->leb_size);
  713. return lprops;
  714. }
  715. /**
  716. * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
  717. * @c: the UBIFS file-system description object
  718. *
  719. * This function returns LEB properties for a freeable LEB or %NULL if the
  720. * function is unable to find a freeable LEB quickly.
  721. */
  722. const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
  723. {
  724. struct ubifs_lprops *lprops;
  725. ubifs_assert(mutex_is_locked(&c->lp_mutex));
  726. if (list_empty(&c->freeable_list))
  727. return NULL;
  728. lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
  729. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  730. ubifs_assert(!(lprops->flags & LPROPS_INDEX));
  731. ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
  732. ubifs_assert(c->freeable_cnt > 0);
  733. return lprops;
  734. }
  735. /**
  736. * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
  737. * @c: the UBIFS file-system description object
  738. *
  739. * This function returns LEB properties for a freeable index LEB or %NULL if the
  740. * function is unable to find a freeable index LEB quickly.
  741. */
  742. const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
  743. {
  744. struct ubifs_lprops *lprops;
  745. ubifs_assert(mutex_is_locked(&c->lp_mutex));
  746. if (list_empty(&c->frdi_idx_list))
  747. return NULL;
  748. lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
  749. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  750. ubifs_assert((lprops->flags & LPROPS_INDEX));
  751. ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
  752. return lprops;
  753. }