find.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file contains functions for finding LEBs for various purposes e.g.
  24. * garbage collection. In general, lprops category heaps and lists are used
  25. * for fast access, falling back on scanning the LPT as a last resort.
  26. */
  27. #include <linux/sort.h>
  28. #include "ubifs.h"
  29. /**
  30. * struct scan_data - data provided to scan callback functions
  31. * @min_space: minimum number of bytes for which to scan
  32. * @pick_free: whether it is OK to scan for empty LEBs
  33. * @lnum: LEB number found is returned here
  34. * @exclude_index: whether to exclude index LEBs
  35. */
  36. struct scan_data {
  37. int min_space;
  38. int pick_free;
  39. int lnum;
  40. int exclude_index;
  41. };
  42. /**
  43. * valuable - determine whether LEB properties are valuable.
  44. * @c: the UBIFS file-system description object
  45. * @lprops: LEB properties
  46. *
  47. * This function return %1 if the LEB properties should be added to the LEB
  48. * properties tree in memory. Otherwise %0 is returned.
  49. */
  50. static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
  51. {
  52. int n, cat = lprops->flags & LPROPS_CAT_MASK;
  53. struct ubifs_lpt_heap *heap;
  54. switch (cat) {
  55. case LPROPS_DIRTY:
  56. case LPROPS_DIRTY_IDX:
  57. case LPROPS_FREE:
  58. heap = &c->lpt_heap[cat - 1];
  59. if (heap->cnt < heap->max_cnt)
  60. return 1;
  61. if (lprops->free + lprops->dirty >= c->dark_wm)
  62. return 1;
  63. return 0;
  64. case LPROPS_EMPTY:
  65. n = c->lst.empty_lebs + c->freeable_cnt -
  66. c->lst.taken_empty_lebs;
  67. if (n < c->lsave_cnt)
  68. return 1;
  69. return 0;
  70. case LPROPS_FREEABLE:
  71. return 1;
  72. case LPROPS_FRDI_IDX:
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * scan_for_dirty_cb - dirty space scan callback.
  79. * @c: the UBIFS file-system description object
  80. * @lprops: LEB properties to scan
  81. * @in_tree: whether the LEB properties are in main memory
  82. * @data: information passed to and from the caller of the scan
  83. *
  84. * This function returns a code that indicates whether the scan should continue
  85. * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
  86. * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
  87. * (%LPT_SCAN_STOP).
  88. */
  89. static int scan_for_dirty_cb(struct ubifs_info *c,
  90. const struct ubifs_lprops *lprops, int in_tree,
  91. struct scan_data *data)
  92. {
  93. int ret = LPT_SCAN_CONTINUE;
  94. /* Exclude LEBs that are currently in use */
  95. if (lprops->flags & LPROPS_TAKEN)
  96. return LPT_SCAN_CONTINUE;
  97. /* Determine whether to add these LEB properties to the tree */
  98. if (!in_tree && valuable(c, lprops))
  99. ret |= LPT_SCAN_ADD;
  100. /* Exclude LEBs with too little space */
  101. if (lprops->free + lprops->dirty < data->min_space)
  102. return ret;
  103. /* If specified, exclude index LEBs */
  104. if (data->exclude_index && lprops->flags & LPROPS_INDEX)
  105. return ret;
  106. /* If specified, exclude empty or freeable LEBs */
  107. if (lprops->free + lprops->dirty == c->leb_size) {
  108. if (!data->pick_free)
  109. return ret;
  110. /* Exclude LEBs with too little dirty space (unless it is empty) */
  111. } else if (lprops->dirty < c->dead_wm)
  112. return ret;
  113. /* Finally we found space */
  114. data->lnum = lprops->lnum;
  115. return LPT_SCAN_ADD | LPT_SCAN_STOP;
  116. }
  117. /**
  118. * scan_for_dirty - find a data LEB with free space.
  119. * @c: the UBIFS file-system description object
  120. * @min_space: minimum amount free plus dirty space the returned LEB has to
  121. * have
  122. * @pick_free: if it is OK to return a free or freeable LEB
  123. * @exclude_index: whether to exclude index LEBs
  124. *
  125. * This function returns a pointer to the LEB properties found or a negative
  126. * error code.
  127. */
  128. static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
  129. int min_space, int pick_free,
  130. int exclude_index)
  131. {
  132. const struct ubifs_lprops *lprops;
  133. struct ubifs_lpt_heap *heap;
  134. struct scan_data data;
  135. int err, i;
  136. /* There may be an LEB with enough dirty space on the free heap */
  137. heap = &c->lpt_heap[LPROPS_FREE - 1];
  138. for (i = 0; i < heap->cnt; i++) {
  139. lprops = heap->arr[i];
  140. if (lprops->free + lprops->dirty < min_space)
  141. continue;
  142. if (lprops->dirty < c->dead_wm)
  143. continue;
  144. return lprops;
  145. }
  146. /*
  147. * A LEB may have fallen off of the bottom of the dirty heap, and ended
  148. * up as uncategorized even though it has enough dirty space for us now,
  149. * so check the uncategorized list. N.B. neither empty nor freeable LEBs
  150. * can end up as uncategorized because they are kept on lists not
  151. * finite-sized heaps.
  152. */
  153. list_for_each_entry(lprops, &c->uncat_list, list) {
  154. if (lprops->flags & LPROPS_TAKEN)
  155. continue;
  156. if (lprops->free + lprops->dirty < min_space)
  157. continue;
  158. if (exclude_index && (lprops->flags & LPROPS_INDEX))
  159. continue;
  160. if (lprops->dirty < c->dead_wm)
  161. continue;
  162. return lprops;
  163. }
  164. /* We have looked everywhere in main memory, now scan the flash */
  165. if (c->pnodes_have >= c->pnode_cnt)
  166. /* All pnodes are in memory, so skip scan */
  167. return ERR_PTR(-ENOSPC);
  168. data.min_space = min_space;
  169. data.pick_free = pick_free;
  170. data.lnum = -1;
  171. data.exclude_index = exclude_index;
  172. err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
  173. (ubifs_lpt_scan_callback)scan_for_dirty_cb,
  174. &data);
  175. if (err)
  176. return ERR_PTR(err);
  177. ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
  178. c->lscan_lnum = data.lnum;
  179. lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
  180. if (IS_ERR(lprops))
  181. return lprops;
  182. ubifs_assert(lprops->lnum == data.lnum);
  183. ubifs_assert(lprops->free + lprops->dirty >= min_space);
  184. ubifs_assert(lprops->dirty >= c->dead_wm ||
  185. (pick_free &&
  186. lprops->free + lprops->dirty == c->leb_size));
  187. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  188. ubifs_assert(!exclude_index || !(lprops->flags & LPROPS_INDEX));
  189. return lprops;
  190. }
  191. /**
  192. * ubifs_find_dirty_leb - find a dirty LEB for the Garbage Collector.
  193. * @c: the UBIFS file-system description object
  194. * @ret_lp: LEB properties are returned here on exit
  195. * @min_space: minimum amount free plus dirty space the returned LEB has to
  196. * have
  197. * @pick_free: controls whether it is OK to pick empty or index LEBs
  198. *
  199. * This function tries to find a dirty logical eraseblock which has at least
  200. * @min_space free and dirty space. It prefers to take an LEB from the dirty or
  201. * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
  202. * or do not have an LEB which satisfies the @min_space criteria.
  203. *
  204. * Note:
  205. * o LEBs which have less than dead watermark of dirty space are never picked
  206. * by this function;
  207. *
  208. * Returns zero and the LEB properties of
  209. * found dirty LEB in case of success, %-ENOSPC if no dirty LEB was found and a
  210. * negative error code in case of other failures. The returned LEB is marked as
  211. * "taken".
  212. *
  213. * The additional @pick_free argument controls if this function has to return a
  214. * free or freeable LEB if one is present. For example, GC must to set it to %1,
  215. * when called from the journal space reservation function, because the
  216. * appearance of free space may coincide with the loss of enough dirty space
  217. * for GC to succeed anyway.
  218. *
  219. * In contrast, if the Garbage Collector is called from budgeting, it should
  220. * just make free space, not return LEBs which are already free or freeable.
  221. *
  222. * In addition @pick_free is set to %2 by the recovery process in order to
  223. * recover gc_lnum in which case an index LEB must not be returned.
  224. */
  225. int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
  226. int min_space, int pick_free)
  227. {
  228. int err = 0, sum, exclude_index = pick_free == 2 ? 1 : 0;
  229. const struct ubifs_lprops *lp = NULL, *idx_lp = NULL;
  230. struct ubifs_lpt_heap *heap, *idx_heap;
  231. ubifs_get_lprops(c);
  232. if (pick_free) {
  233. int lebs, rsvd_idx_lebs = 0;
  234. spin_lock(&c->space_lock);
  235. lebs = c->lst.empty_lebs;
  236. lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
  237. /*
  238. * Note, the index may consume more LEBs than have been reserved
  239. * for it. It is OK because it might be consolidated by GC.
  240. * But if the index takes fewer LEBs than it is reserved for it,
  241. * this function must avoid picking those reserved LEBs.
  242. */
  243. if (c->min_idx_lebs >= c->lst.idx_lebs) {
  244. rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
  245. exclude_index = 1;
  246. }
  247. spin_unlock(&c->space_lock);
  248. /* Check if there are enough free LEBs for the index */
  249. if (rsvd_idx_lebs < lebs) {
  250. /* OK, try to find an empty LEB */
  251. lp = ubifs_fast_find_empty(c);
  252. if (lp)
  253. goto found;
  254. /* Or a freeable LEB */
  255. lp = ubifs_fast_find_freeable(c);
  256. if (lp)
  257. goto found;
  258. } else
  259. /*
  260. * We cannot pick free/freeable LEBs in the below code.
  261. */
  262. pick_free = 0;
  263. } else {
  264. spin_lock(&c->space_lock);
  265. exclude_index = (c->min_idx_lebs >= c->lst.idx_lebs);
  266. spin_unlock(&c->space_lock);
  267. }
  268. /* Look on the dirty and dirty index heaps */
  269. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  270. idx_heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  271. if (idx_heap->cnt && !exclude_index) {
  272. idx_lp = idx_heap->arr[0];
  273. sum = idx_lp->free + idx_lp->dirty;
  274. /*
  275. * Since we reserve thrice as much space for the index than it
  276. * actually takes, it does not make sense to pick indexing LEBs
  277. * with less than, say, half LEB of dirty space. May be half is
  278. * not the optimal boundary - this should be tested and
  279. * checked. This boundary should determine how much we use
  280. * in-the-gaps to consolidate the index comparing to how much
  281. * we use garbage collector to consolidate it. The "half"
  282. * criteria just feels to be fine.
  283. */
  284. if (sum < min_space || sum < c->half_leb_size)
  285. idx_lp = NULL;
  286. }
  287. if (heap->cnt) {
  288. lp = heap->arr[0];
  289. if (lp->dirty + lp->free < min_space)
  290. lp = NULL;
  291. }
  292. /* Pick the LEB with most space */
  293. if (idx_lp && lp) {
  294. if (idx_lp->free + idx_lp->dirty >= lp->free + lp->dirty)
  295. lp = idx_lp;
  296. } else if (idx_lp && !lp)
  297. lp = idx_lp;
  298. if (lp) {
  299. ubifs_assert(lp->dirty >= c->dead_wm);
  300. goto found;
  301. }
  302. /* Did not find a dirty LEB on the dirty heaps, have to scan */
  303. dbg_find("scanning LPT for a dirty LEB");
  304. lp = scan_for_dirty(c, min_space, pick_free, exclude_index);
  305. if (IS_ERR(lp)) {
  306. err = PTR_ERR(lp);
  307. goto out;
  308. }
  309. ubifs_assert(lp->dirty >= c->dead_wm ||
  310. (pick_free && lp->free + lp->dirty == c->leb_size));
  311. found:
  312. dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
  313. lp->lnum, lp->free, lp->dirty, lp->flags);
  314. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  315. lp->flags | LPROPS_TAKEN, 0);
  316. if (IS_ERR(lp)) {
  317. err = PTR_ERR(lp);
  318. goto out;
  319. }
  320. memcpy(ret_lp, lp, sizeof(struct ubifs_lprops));
  321. out:
  322. ubifs_release_lprops(c);
  323. return err;
  324. }
  325. /**
  326. * scan_for_free_cb - free space scan callback.
  327. * @c: the UBIFS file-system description object
  328. * @lprops: LEB properties to scan
  329. * @in_tree: whether the LEB properties are in main memory
  330. * @data: information passed to and from the caller of the scan
  331. *
  332. * This function returns a code that indicates whether the scan should continue
  333. * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
  334. * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
  335. * (%LPT_SCAN_STOP).
  336. */
  337. static int scan_for_free_cb(struct ubifs_info *c,
  338. const struct ubifs_lprops *lprops, int in_tree,
  339. struct scan_data *data)
  340. {
  341. int ret = LPT_SCAN_CONTINUE;
  342. /* Exclude LEBs that are currently in use */
  343. if (lprops->flags & LPROPS_TAKEN)
  344. return LPT_SCAN_CONTINUE;
  345. /* Determine whether to add these LEB properties to the tree */
  346. if (!in_tree && valuable(c, lprops))
  347. ret |= LPT_SCAN_ADD;
  348. /* Exclude index LEBs */
  349. if (lprops->flags & LPROPS_INDEX)
  350. return ret;
  351. /* Exclude LEBs with too little space */
  352. if (lprops->free < data->min_space)
  353. return ret;
  354. /* If specified, exclude empty LEBs */
  355. if (!data->pick_free && lprops->free == c->leb_size)
  356. return ret;
  357. /*
  358. * LEBs that have only free and dirty space must not be allocated
  359. * because they may have been unmapped already or they may have data
  360. * that is obsolete only because of nodes that are still sitting in a
  361. * wbuf.
  362. */
  363. if (lprops->free + lprops->dirty == c->leb_size && lprops->dirty > 0)
  364. return ret;
  365. /* Finally we found space */
  366. data->lnum = lprops->lnum;
  367. return LPT_SCAN_ADD | LPT_SCAN_STOP;
  368. }
  369. /**
  370. * do_find_free_space - find a data LEB with free space.
  371. * @c: the UBIFS file-system description object
  372. * @min_space: minimum amount of free space required
  373. * @pick_free: whether it is OK to scan for empty LEBs
  374. * @squeeze: whether to try to find space in a non-empty LEB first
  375. *
  376. * This function returns a pointer to the LEB properties found or a negative
  377. * error code.
  378. */
  379. static
  380. const struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
  381. int min_space, int pick_free,
  382. int squeeze)
  383. {
  384. const struct ubifs_lprops *lprops;
  385. struct ubifs_lpt_heap *heap;
  386. struct scan_data data;
  387. int err, i;
  388. if (squeeze) {
  389. lprops = ubifs_fast_find_free(c);
  390. if (lprops && lprops->free >= min_space)
  391. return lprops;
  392. }
  393. if (pick_free) {
  394. lprops = ubifs_fast_find_empty(c);
  395. if (lprops)
  396. return lprops;
  397. }
  398. if (!squeeze) {
  399. lprops = ubifs_fast_find_free(c);
  400. if (lprops && lprops->free >= min_space)
  401. return lprops;
  402. }
  403. /* There may be an LEB with enough free space on the dirty heap */
  404. heap = &c->lpt_heap[LPROPS_DIRTY - 1];
  405. for (i = 0; i < heap->cnt; i++) {
  406. lprops = heap->arr[i];
  407. if (lprops->free >= min_space)
  408. return lprops;
  409. }
  410. /*
  411. * A LEB may have fallen off of the bottom of the free heap, and ended
  412. * up as uncategorized even though it has enough free space for us now,
  413. * so check the uncategorized list. N.B. neither empty nor freeable LEBs
  414. * can end up as uncategorized because they are kept on lists not
  415. * finite-sized heaps.
  416. */
  417. list_for_each_entry(lprops, &c->uncat_list, list) {
  418. if (lprops->flags & LPROPS_TAKEN)
  419. continue;
  420. if (lprops->flags & LPROPS_INDEX)
  421. continue;
  422. if (lprops->free >= min_space)
  423. return lprops;
  424. }
  425. /* We have looked everywhere in main memory, now scan the flash */
  426. if (c->pnodes_have >= c->pnode_cnt)
  427. /* All pnodes are in memory, so skip scan */
  428. return ERR_PTR(-ENOSPC);
  429. data.min_space = min_space;
  430. data.pick_free = pick_free;
  431. data.lnum = -1;
  432. err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
  433. (ubifs_lpt_scan_callback)scan_for_free_cb,
  434. &data);
  435. if (err)
  436. return ERR_PTR(err);
  437. ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
  438. c->lscan_lnum = data.lnum;
  439. lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
  440. if (IS_ERR(lprops))
  441. return lprops;
  442. ubifs_assert(lprops->lnum == data.lnum);
  443. ubifs_assert(lprops->free >= min_space);
  444. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  445. ubifs_assert(!(lprops->flags & LPROPS_INDEX));
  446. return lprops;
  447. }
  448. /**
  449. * ubifs_find_free_space - find a data LEB with free space.
  450. * @c: the UBIFS file-system description object
  451. * @min_space: minimum amount of required free space
  452. * @free: contains amount of free space in the LEB on exit
  453. * @squeeze: whether to try to find space in a non-empty LEB first
  454. *
  455. * This function looks for an LEB with at least @min_space bytes of free space.
  456. * It tries to find an empty LEB if possible. If no empty LEBs are available,
  457. * this function searches for a non-empty data LEB. The returned LEB is marked
  458. * as "taken".
  459. *
  460. * This function returns found LEB number in case of success, %-ENOSPC if it
  461. * failed to find a LEB with @min_space bytes of free space and other a negative
  462. * error codes in case of failure.
  463. */
  464. int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
  465. int squeeze)
  466. {
  467. const struct ubifs_lprops *lprops;
  468. int lebs, rsvd_idx_lebs, pick_free = 0, err, lnum, flags;
  469. dbg_find("min_space %d", min_space);
  470. ubifs_get_lprops(c);
  471. /* Check if there are enough empty LEBs for commit */
  472. spin_lock(&c->space_lock);
  473. if (c->min_idx_lebs > c->lst.idx_lebs)
  474. rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
  475. else
  476. rsvd_idx_lebs = 0;
  477. lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
  478. c->lst.taken_empty_lebs;
  479. ubifs_assert(lebs + c->lst.idx_lebs >= c->min_idx_lebs);
  480. if (rsvd_idx_lebs < lebs)
  481. /*
  482. * OK to allocate an empty LEB, but we still don't want to go
  483. * looking for one if there aren't any.
  484. */
  485. if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
  486. pick_free = 1;
  487. /*
  488. * Because we release the space lock, we must account
  489. * for this allocation here. After the LEB properties
  490. * flags have been updated, we subtract one. Note, the
  491. * result of this is that lprops also decreases
  492. * @taken_empty_lebs in 'ubifs_change_lp()', so it is
  493. * off by one for a short period of time which may
  494. * introduce a small disturbance to budgeting
  495. * calculations, but this is harmless because at the
  496. * worst case this would make the budgeting subsystem
  497. * be more pessimistic than needed.
  498. *
  499. * Fundamentally, this is about serialization of the
  500. * budgeting and lprops subsystems. We could make the
  501. * @space_lock a mutex and avoid dropping it before
  502. * calling 'ubifs_change_lp()', but mutex is more
  503. * heavy-weight, and we want budgeting to be as fast as
  504. * possible.
  505. */
  506. c->lst.taken_empty_lebs += 1;
  507. }
  508. spin_unlock(&c->space_lock);
  509. lprops = do_find_free_space(c, min_space, pick_free, squeeze);
  510. if (IS_ERR(lprops)) {
  511. err = PTR_ERR(lprops);
  512. goto out;
  513. }
  514. lnum = lprops->lnum;
  515. flags = lprops->flags | LPROPS_TAKEN;
  516. lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC, flags, 0);
  517. if (IS_ERR(lprops)) {
  518. err = PTR_ERR(lprops);
  519. goto out;
  520. }
  521. if (pick_free) {
  522. spin_lock(&c->space_lock);
  523. c->lst.taken_empty_lebs -= 1;
  524. spin_unlock(&c->space_lock);
  525. }
  526. *free = lprops->free;
  527. ubifs_release_lprops(c);
  528. if (*free == c->leb_size) {
  529. /*
  530. * Ensure that empty LEBs have been unmapped. They may not have
  531. * been, for example, because of an unclean unmount. Also
  532. * LEBs that were freeable LEBs (free + dirty == leb_size) will
  533. * not have been unmapped.
  534. */
  535. err = ubifs_leb_unmap(c, lnum);
  536. if (err)
  537. return err;
  538. }
  539. dbg_find("found LEB %d, free %d", lnum, *free);
  540. ubifs_assert(*free >= min_space);
  541. return lnum;
  542. out:
  543. if (pick_free) {
  544. spin_lock(&c->space_lock);
  545. c->lst.taken_empty_lebs -= 1;
  546. spin_unlock(&c->space_lock);
  547. }
  548. ubifs_release_lprops(c);
  549. return err;
  550. }
  551. /**
  552. * scan_for_idx_cb - callback used by the scan for a free LEB for the index.
  553. * @c: the UBIFS file-system description object
  554. * @lprops: LEB properties to scan
  555. * @in_tree: whether the LEB properties are in main memory
  556. * @data: information passed to and from the caller of the scan
  557. *
  558. * This function returns a code that indicates whether the scan should continue
  559. * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
  560. * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
  561. * (%LPT_SCAN_STOP).
  562. */
  563. static int scan_for_idx_cb(struct ubifs_info *c,
  564. const struct ubifs_lprops *lprops, int in_tree,
  565. struct scan_data *data)
  566. {
  567. int ret = LPT_SCAN_CONTINUE;
  568. /* Exclude LEBs that are currently in use */
  569. if (lprops->flags & LPROPS_TAKEN)
  570. return LPT_SCAN_CONTINUE;
  571. /* Determine whether to add these LEB properties to the tree */
  572. if (!in_tree && valuable(c, lprops))
  573. ret |= LPT_SCAN_ADD;
  574. /* Exclude index LEBS */
  575. if (lprops->flags & LPROPS_INDEX)
  576. return ret;
  577. /* Exclude LEBs that cannot be made empty */
  578. if (lprops->free + lprops->dirty != c->leb_size)
  579. return ret;
  580. /*
  581. * We are allocating for the index so it is safe to allocate LEBs with
  582. * only free and dirty space, because write buffers are sync'd at commit
  583. * start.
  584. */
  585. data->lnum = lprops->lnum;
  586. return LPT_SCAN_ADD | LPT_SCAN_STOP;
  587. }
  588. /**
  589. * scan_for_leb_for_idx - scan for a free LEB for the index.
  590. * @c: the UBIFS file-system description object
  591. */
  592. static const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
  593. {
  594. struct ubifs_lprops *lprops;
  595. struct scan_data data;
  596. int err;
  597. data.lnum = -1;
  598. err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
  599. (ubifs_lpt_scan_callback)scan_for_idx_cb,
  600. &data);
  601. if (err)
  602. return ERR_PTR(err);
  603. ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
  604. c->lscan_lnum = data.lnum;
  605. lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
  606. if (IS_ERR(lprops))
  607. return lprops;
  608. ubifs_assert(lprops->lnum == data.lnum);
  609. ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
  610. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  611. ubifs_assert(!(lprops->flags & LPROPS_INDEX));
  612. return lprops;
  613. }
  614. /**
  615. * ubifs_find_free_leb_for_idx - find a free LEB for the index.
  616. * @c: the UBIFS file-system description object
  617. *
  618. * This function looks for a free LEB and returns that LEB number. The returned
  619. * LEB is marked as "taken", "index".
  620. *
  621. * Only empty LEBs are allocated. This is for two reasons. First, the commit
  622. * calculates the number of LEBs to allocate based on the assumption that they
  623. * will be empty. Secondly, free space at the end of an index LEB is not
  624. * guaranteed to be empty because it may have been used by the in-the-gaps
  625. * method prior to an unclean unmount.
  626. *
  627. * If no LEB is found %-ENOSPC is returned. For other failures another negative
  628. * error code is returned.
  629. */
  630. int ubifs_find_free_leb_for_idx(struct ubifs_info *c)
  631. {
  632. const struct ubifs_lprops *lprops;
  633. int lnum = -1, err, flags;
  634. ubifs_get_lprops(c);
  635. lprops = ubifs_fast_find_empty(c);
  636. if (!lprops) {
  637. lprops = ubifs_fast_find_freeable(c);
  638. if (!lprops) {
  639. ubifs_assert(c->freeable_cnt == 0);
  640. if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
  641. lprops = scan_for_leb_for_idx(c);
  642. if (IS_ERR(lprops)) {
  643. err = PTR_ERR(lprops);
  644. goto out;
  645. }
  646. }
  647. }
  648. }
  649. if (!lprops) {
  650. err = -ENOSPC;
  651. goto out;
  652. }
  653. lnum = lprops->lnum;
  654. dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
  655. lnum, lprops->free, lprops->dirty, lprops->flags);
  656. flags = lprops->flags | LPROPS_TAKEN | LPROPS_INDEX;
  657. lprops = ubifs_change_lp(c, lprops, c->leb_size, 0, flags, 0);
  658. if (IS_ERR(lprops)) {
  659. err = PTR_ERR(lprops);
  660. goto out;
  661. }
  662. ubifs_release_lprops(c);
  663. /*
  664. * Ensure that empty LEBs have been unmapped. They may not have been,
  665. * for example, because of an unclean unmount. Also LEBs that were
  666. * freeable LEBs (free + dirty == leb_size) will not have been unmapped.
  667. */
  668. err = ubifs_leb_unmap(c, lnum);
  669. if (err) {
  670. ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  671. LPROPS_TAKEN | LPROPS_INDEX, 0);
  672. return err;
  673. }
  674. return lnum;
  675. out:
  676. ubifs_release_lprops(c);
  677. return err;
  678. }
  679. static int cmp_dirty_idx(const struct ubifs_lprops **a,
  680. const struct ubifs_lprops **b)
  681. {
  682. const struct ubifs_lprops *lpa = *a;
  683. const struct ubifs_lprops *lpb = *b;
  684. return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
  685. }
  686. static void swap_dirty_idx(struct ubifs_lprops **a, struct ubifs_lprops **b,
  687. int size)
  688. {
  689. struct ubifs_lprops *t = *a;
  690. *a = *b;
  691. *b = t;
  692. }
  693. /**
  694. * ubifs_save_dirty_idx_lnums - save an array of the most dirty index LEB nos.
  695. * @c: the UBIFS file-system description object
  696. *
  697. * This function is called each commit to create an array of LEB numbers of
  698. * dirty index LEBs sorted in order of dirty and free space. This is used by
  699. * the in-the-gaps method of TNC commit.
  700. */
  701. int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
  702. {
  703. int i;
  704. ubifs_get_lprops(c);
  705. /* Copy the LPROPS_DIRTY_IDX heap */
  706. c->dirty_idx.cnt = c->lpt_heap[LPROPS_DIRTY_IDX - 1].cnt;
  707. memcpy(c->dirty_idx.arr, c->lpt_heap[LPROPS_DIRTY_IDX - 1].arr,
  708. sizeof(void *) * c->dirty_idx.cnt);
  709. /* Sort it so that the dirtiest is now at the end */
  710. sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
  711. (int (*)(const void *, const void *))cmp_dirty_idx,
  712. (void (*)(void *, void *, int))swap_dirty_idx);
  713. dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
  714. if (c->dirty_idx.cnt)
  715. dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
  716. c->dirty_idx.arr[c->dirty_idx.cnt - 1]->lnum,
  717. c->dirty_idx.arr[c->dirty_idx.cnt - 1]->dirty,
  718. c->dirty_idx.arr[c->dirty_idx.cnt - 1]->free);
  719. /* Replace the lprops pointers with LEB numbers */
  720. for (i = 0; i < c->dirty_idx.cnt; i++)
  721. c->dirty_idx.arr[i] = (void *)(size_t)c->dirty_idx.arr[i]->lnum;
  722. ubifs_release_lprops(c);
  723. return 0;
  724. }
  725. /**
  726. * scan_dirty_idx_cb - callback used by the scan for a dirty index LEB.
  727. * @c: the UBIFS file-system description object
  728. * @lprops: LEB properties to scan
  729. * @in_tree: whether the LEB properties are in main memory
  730. * @data: information passed to and from the caller of the scan
  731. *
  732. * This function returns a code that indicates whether the scan should continue
  733. * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
  734. * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
  735. * (%LPT_SCAN_STOP).
  736. */
  737. static int scan_dirty_idx_cb(struct ubifs_info *c,
  738. const struct ubifs_lprops *lprops, int in_tree,
  739. struct scan_data *data)
  740. {
  741. int ret = LPT_SCAN_CONTINUE;
  742. /* Exclude LEBs that are currently in use */
  743. if (lprops->flags & LPROPS_TAKEN)
  744. return LPT_SCAN_CONTINUE;
  745. /* Determine whether to add these LEB properties to the tree */
  746. if (!in_tree && valuable(c, lprops))
  747. ret |= LPT_SCAN_ADD;
  748. /* Exclude non-index LEBs */
  749. if (!(lprops->flags & LPROPS_INDEX))
  750. return ret;
  751. /* Exclude LEBs with too little space */
  752. if (lprops->free + lprops->dirty < c->min_idx_node_sz)
  753. return ret;
  754. /* Finally we found space */
  755. data->lnum = lprops->lnum;
  756. return LPT_SCAN_ADD | LPT_SCAN_STOP;
  757. }
  758. /**
  759. * find_dirty_idx_leb - find a dirty index LEB.
  760. * @c: the UBIFS file-system description object
  761. *
  762. * This function returns LEB number upon success and a negative error code upon
  763. * failure. In particular, -ENOSPC is returned if a dirty index LEB is not
  764. * found.
  765. *
  766. * Note that this function scans the entire LPT but it is called very rarely.
  767. */
  768. static int find_dirty_idx_leb(struct ubifs_info *c)
  769. {
  770. const struct ubifs_lprops *lprops;
  771. struct ubifs_lpt_heap *heap;
  772. struct scan_data data;
  773. int err, i, ret;
  774. /* Check all structures in memory first */
  775. data.lnum = -1;
  776. heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
  777. for (i = 0; i < heap->cnt; i++) {
  778. lprops = heap->arr[i];
  779. ret = scan_dirty_idx_cb(c, lprops, 1, &data);
  780. if (ret & LPT_SCAN_STOP)
  781. goto found;
  782. }
  783. list_for_each_entry(lprops, &c->frdi_idx_list, list) {
  784. ret = scan_dirty_idx_cb(c, lprops, 1, &data);
  785. if (ret & LPT_SCAN_STOP)
  786. goto found;
  787. }
  788. list_for_each_entry(lprops, &c->uncat_list, list) {
  789. ret = scan_dirty_idx_cb(c, lprops, 1, &data);
  790. if (ret & LPT_SCAN_STOP)
  791. goto found;
  792. }
  793. if (c->pnodes_have >= c->pnode_cnt)
  794. /* All pnodes are in memory, so skip scan */
  795. return -ENOSPC;
  796. err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
  797. (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
  798. &data);
  799. if (err)
  800. return err;
  801. found:
  802. ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
  803. c->lscan_lnum = data.lnum;
  804. lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
  805. if (IS_ERR(lprops))
  806. return PTR_ERR(lprops);
  807. ubifs_assert(lprops->lnum == data.lnum);
  808. ubifs_assert(lprops->free + lprops->dirty >= c->min_idx_node_sz);
  809. ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
  810. ubifs_assert((lprops->flags & LPROPS_INDEX));
  811. dbg_find("found dirty LEB %d, free %d, dirty %d, flags %#x",
  812. lprops->lnum, lprops->free, lprops->dirty, lprops->flags);
  813. lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC,
  814. lprops->flags | LPROPS_TAKEN, 0);
  815. if (IS_ERR(lprops))
  816. return PTR_ERR(lprops);
  817. return lprops->lnum;
  818. }
  819. /**
  820. * get_idx_gc_leb - try to get a LEB number from trivial GC.
  821. * @c: the UBIFS file-system description object
  822. */
  823. static int get_idx_gc_leb(struct ubifs_info *c)
  824. {
  825. const struct ubifs_lprops *lp;
  826. int err, lnum;
  827. err = ubifs_get_idx_gc_leb(c);
  828. if (err < 0)
  829. return err;
  830. lnum = err;
  831. /*
  832. * The LEB was due to be unmapped after the commit but
  833. * it is needed now for this commit.
  834. */
  835. lp = ubifs_lpt_lookup_dirty(c, lnum);
  836. if (unlikely(IS_ERR(lp)))
  837. return PTR_ERR(lp);
  838. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  839. lp->flags | LPROPS_INDEX, -1);
  840. if (unlikely(IS_ERR(lp)))
  841. return PTR_ERR(lp);
  842. dbg_find("LEB %d, dirty %d and free %d flags %#x",
  843. lp->lnum, lp->dirty, lp->free, lp->flags);
  844. return lnum;
  845. }
  846. /**
  847. * find_dirtiest_idx_leb - find dirtiest index LEB from dirtiest array.
  848. * @c: the UBIFS file-system description object
  849. */
  850. static int find_dirtiest_idx_leb(struct ubifs_info *c)
  851. {
  852. const struct ubifs_lprops *lp;
  853. int lnum;
  854. while (1) {
  855. if (!c->dirty_idx.cnt)
  856. return -ENOSPC;
  857. /* The lprops pointers were replaced by LEB numbers */
  858. lnum = (size_t)c->dirty_idx.arr[--c->dirty_idx.cnt];
  859. lp = ubifs_lpt_lookup(c, lnum);
  860. if (IS_ERR(lp))
  861. return PTR_ERR(lp);
  862. if ((lp->flags & LPROPS_TAKEN) || !(lp->flags & LPROPS_INDEX))
  863. continue;
  864. lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
  865. lp->flags | LPROPS_TAKEN, 0);
  866. if (IS_ERR(lp))
  867. return PTR_ERR(lp);
  868. break;
  869. }
  870. dbg_find("LEB %d, dirty %d and free %d flags %#x", lp->lnum, lp->dirty,
  871. lp->free, lp->flags);
  872. ubifs_assert(lp->flags | LPROPS_TAKEN);
  873. ubifs_assert(lp->flags | LPROPS_INDEX);
  874. return lnum;
  875. }
  876. /**
  877. * ubifs_find_dirty_idx_leb - try to find dirtiest index LEB as at last commit.
  878. * @c: the UBIFS file-system description object
  879. *
  880. * This function attempts to find an untaken index LEB with the most free and
  881. * dirty space that can be used without overwriting index nodes that were in the
  882. * last index committed.
  883. */
  884. int ubifs_find_dirty_idx_leb(struct ubifs_info *c)
  885. {
  886. int err;
  887. ubifs_get_lprops(c);
  888. /*
  889. * We made an array of the dirtiest index LEB numbers as at the start of
  890. * last commit. Try that array first.
  891. */
  892. err = find_dirtiest_idx_leb(c);
  893. /* Next try scanning the entire LPT */
  894. if (err == -ENOSPC)
  895. err = find_dirty_idx_leb(c);
  896. /* Finally take any index LEBs awaiting trivial GC */
  897. if (err == -ENOSPC)
  898. err = get_idx_gc_leb(c);
  899. ubifs_release_lprops(c);
  900. return err;
  901. }