find.c 29 KB

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