find.c 30 KB

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