gc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements garbage collection. The procedure for garbage collection
  24. * is different depending on whether a LEB as an index LEB (contains index
  25. * nodes) or not. For non-index LEBs, garbage collection finds a LEB which
  26. * contains a lot of dirty space (obsolete nodes), and copies the non-obsolete
  27. * nodes to the journal, at which point the garbage-collected LEB is free to be
  28. * reused. For index LEBs, garbage collection marks the non-obsolete index nodes
  29. * dirty in the TNC, and after the next commit, the garbage-collected LEB is
  30. * to be reused. Garbage collection will cause the number of dirty index nodes
  31. * to grow, however sufficient space is reserved for the index to ensure the
  32. * commit will never run out of space.
  33. */
  34. #include <linux/pagemap.h>
  35. #include "ubifs.h"
  36. /*
  37. * GC tries to optimize the way it fit nodes to available space, and it sorts
  38. * nodes a little. The below constants are watermarks which define "large",
  39. * "medium", and "small" nodes.
  40. */
  41. #define MEDIUM_NODE_WM (UBIFS_BLOCK_SIZE / 4)
  42. #define SMALL_NODE_WM UBIFS_MAX_DENT_NODE_SZ
  43. /*
  44. * GC may need to move more then one LEB to make progress. The below constants
  45. * define "soft" and "hard" limits on the number of LEBs the garbage collector
  46. * may move.
  47. */
  48. #define SOFT_LEBS_LIMIT 4
  49. #define HARD_LEBS_LIMIT 32
  50. /**
  51. * switch_gc_head - switch the garbage collection journal head.
  52. * @c: UBIFS file-system description object
  53. * @buf: buffer to write
  54. * @len: length of the buffer to write
  55. * @lnum: LEB number written is returned here
  56. * @offs: offset written is returned here
  57. *
  58. * This function switch the GC head to the next LEB which is reserved in
  59. * @c->gc_lnum. Returns %0 in case of success, %-EAGAIN if commit is required,
  60. * and other negative error code in case of failures.
  61. */
  62. static int switch_gc_head(struct ubifs_info *c)
  63. {
  64. int err, gc_lnum = c->gc_lnum;
  65. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  66. ubifs_assert(gc_lnum != -1);
  67. dbg_gc("switch GC head from LEB %d:%d to LEB %d (waste %d bytes)",
  68. wbuf->lnum, wbuf->offs + wbuf->used, gc_lnum,
  69. c->leb_size - wbuf->offs - wbuf->used);
  70. err = ubifs_wbuf_sync_nolock(wbuf);
  71. if (err)
  72. return err;
  73. /*
  74. * The GC write-buffer was synchronized, we may safely unmap
  75. * 'c->gc_lnum'.
  76. */
  77. err = ubifs_leb_unmap(c, gc_lnum);
  78. if (err)
  79. return err;
  80. err = ubifs_add_bud_to_log(c, GCHD, gc_lnum, 0);
  81. if (err)
  82. return err;
  83. c->gc_lnum = -1;
  84. err = ubifs_wbuf_seek_nolock(wbuf, gc_lnum, 0, UBI_LONGTERM);
  85. return err;
  86. }
  87. /**
  88. * move_nodes - move nodes.
  89. * @c: UBIFS file-system description object
  90. * @sleb: describes nodes to move
  91. *
  92. * This function moves valid nodes from data LEB described by @sleb to the GC
  93. * journal head. The obsolete nodes are dropped.
  94. *
  95. * When moving nodes we have to deal with classical bin-packing problem: the
  96. * space in the current GC journal head LEB and in @c->gc_lnum are the "bins",
  97. * where the nodes in the @sleb->nodes list are the elements which should be
  98. * fit optimally to the bins. This function uses the "first fit decreasing"
  99. * strategy, although it does not really sort the nodes but just split them on
  100. * 3 classes - large, medium, and small, so they are roughly sorted.
  101. *
  102. * This function returns zero in case of success, %-EAGAIN if commit is
  103. * required, and other negative error codes in case of other failures.
  104. */
  105. static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
  106. {
  107. struct ubifs_scan_node *snod, *tmp;
  108. struct list_head large, medium, small;
  109. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  110. int avail, err, min = INT_MAX;
  111. INIT_LIST_HEAD(&large);
  112. INIT_LIST_HEAD(&medium);
  113. INIT_LIST_HEAD(&small);
  114. list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
  115. struct list_head *lst;
  116. ubifs_assert(snod->type != UBIFS_IDX_NODE);
  117. ubifs_assert(snod->type != UBIFS_REF_NODE);
  118. ubifs_assert(snod->type != UBIFS_CS_NODE);
  119. err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
  120. snod->offs, 0);
  121. if (err < 0)
  122. goto out;
  123. lst = &snod->list;
  124. list_del(lst);
  125. if (!err) {
  126. /* The node is obsolete, remove it from the list */
  127. kfree(snod);
  128. continue;
  129. }
  130. /*
  131. * Sort the list of nodes so that large nodes go first, and
  132. * small nodes go last.
  133. */
  134. if (snod->len > MEDIUM_NODE_WM)
  135. list_add(lst, &large);
  136. else if (snod->len > SMALL_NODE_WM)
  137. list_add(lst, &medium);
  138. else
  139. list_add(lst, &small);
  140. /* And find the smallest node */
  141. if (snod->len < min)
  142. min = snod->len;
  143. }
  144. /*
  145. * Join the tree lists so that we'd have one roughly sorted list
  146. * ('large' will be the head of the joined list).
  147. */
  148. list_splice(&medium, large.prev);
  149. list_splice(&small, large.prev);
  150. if (wbuf->lnum == -1) {
  151. /*
  152. * The GC journal head is not set, because it is the first GC
  153. * invocation since mount.
  154. */
  155. err = switch_gc_head(c);
  156. if (err)
  157. goto out;
  158. }
  159. /* Write nodes to their new location. Use the first-fit strategy */
  160. while (1) {
  161. avail = c->leb_size - wbuf->offs - wbuf->used;
  162. list_for_each_entry_safe(snod, tmp, &large, list) {
  163. int new_lnum, new_offs;
  164. if (avail < min)
  165. break;
  166. if (snod->len > avail)
  167. /* This node does not fit */
  168. continue;
  169. cond_resched();
  170. new_lnum = wbuf->lnum;
  171. new_offs = wbuf->offs + wbuf->used;
  172. err = ubifs_wbuf_write_nolock(wbuf, snod->node,
  173. snod->len);
  174. if (err)
  175. goto out;
  176. err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
  177. snod->offs, new_lnum, new_offs,
  178. snod->len);
  179. if (err)
  180. goto out;
  181. avail = c->leb_size - wbuf->offs - wbuf->used;
  182. list_del(&snod->list);
  183. kfree(snod);
  184. }
  185. if (list_empty(&large))
  186. break;
  187. /*
  188. * Waste the rest of the space in the LEB and switch to the
  189. * next LEB.
  190. */
  191. err = switch_gc_head(c);
  192. if (err)
  193. goto out;
  194. }
  195. return 0;
  196. out:
  197. list_for_each_entry_safe(snod, tmp, &large, list) {
  198. list_del(&snod->list);
  199. kfree(snod);
  200. }
  201. return err;
  202. }
  203. /**
  204. * gc_sync_wbufs - sync write-buffers for GC.
  205. * @c: UBIFS file-system description object
  206. *
  207. * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
  208. * be in a write-buffer instead. That is, a node could be written to a
  209. * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
  210. * erased before the write-buffer is sync'd and then there is an unclean
  211. * unmount, then an existing node is lost. To avoid this, we sync all
  212. * write-buffers.
  213. *
  214. * This function returns %0 on success or a negative error code on failure.
  215. */
  216. static int gc_sync_wbufs(struct ubifs_info *c)
  217. {
  218. int err, i;
  219. for (i = 0; i < c->jhead_cnt; i++) {
  220. if (i == GCHD)
  221. continue;
  222. err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  223. if (err)
  224. return err;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
  230. * @c: UBIFS file-system description object
  231. * @lp: describes the LEB to garbage collect
  232. *
  233. * This function garbage-collects an LEB and returns one of the @LEB_FREED,
  234. * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
  235. * required, and other negative error codes in case of failures.
  236. */
  237. int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
  238. {
  239. struct ubifs_scan_leb *sleb;
  240. struct ubifs_scan_node *snod;
  241. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  242. int err = 0, lnum = lp->lnum;
  243. ubifs_assert(c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
  244. c->need_recovery);
  245. ubifs_assert(c->gc_lnum != lnum);
  246. ubifs_assert(wbuf->lnum != lnum);
  247. /*
  248. * We scan the entire LEB even though we only really need to scan up to
  249. * (c->leb_size - lp->free).
  250. */
  251. sleb = ubifs_scan(c, lnum, 0, c->sbuf);
  252. if (IS_ERR(sleb))
  253. return PTR_ERR(sleb);
  254. ubifs_assert(!list_empty(&sleb->nodes));
  255. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  256. if (snod->type == UBIFS_IDX_NODE) {
  257. struct ubifs_gced_idx_leb *idx_gc;
  258. dbg_gc("indexing LEB %d (free %d, dirty %d)",
  259. lnum, lp->free, lp->dirty);
  260. list_for_each_entry(snod, &sleb->nodes, list) {
  261. struct ubifs_idx_node *idx = snod->node;
  262. int level = le16_to_cpu(idx->level);
  263. ubifs_assert(snod->type == UBIFS_IDX_NODE);
  264. key_read(c, ubifs_idx_key(c, idx), &snod->key);
  265. err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
  266. snod->offs);
  267. if (err)
  268. goto out;
  269. }
  270. idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
  271. if (!idx_gc) {
  272. err = -ENOMEM;
  273. goto out;
  274. }
  275. idx_gc->lnum = lnum;
  276. idx_gc->unmap = 0;
  277. list_add(&idx_gc->list, &c->idx_gc);
  278. /*
  279. * Don't release the LEB until after the next commit, because
  280. * it may contain date which is needed for recovery. So
  281. * although we freed this LEB, it will become usable only after
  282. * the commit.
  283. */
  284. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
  285. LPROPS_INDEX, 1);
  286. if (err)
  287. goto out;
  288. err = LEB_FREED_IDX;
  289. } else {
  290. dbg_gc("data LEB %d (free %d, dirty %d)",
  291. lnum, lp->free, lp->dirty);
  292. err = move_nodes(c, sleb);
  293. if (err)
  294. goto out;
  295. err = gc_sync_wbufs(c);
  296. if (err)
  297. goto out;
  298. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
  299. if (err)
  300. goto out;
  301. /* Allow for races with TNC */
  302. c->gced_lnum = lnum;
  303. smp_wmb();
  304. c->gc_seq += 1;
  305. smp_wmb();
  306. if (c->gc_lnum == -1) {
  307. c->gc_lnum = lnum;
  308. err = LEB_RETAINED;
  309. } else {
  310. err = ubifs_wbuf_sync_nolock(wbuf);
  311. if (err)
  312. goto out;
  313. err = ubifs_leb_unmap(c, lnum);
  314. if (err)
  315. goto out;
  316. err = LEB_FREED;
  317. }
  318. }
  319. out:
  320. ubifs_scan_destroy(sleb);
  321. return err;
  322. }
  323. /**
  324. * ubifs_garbage_collect - UBIFS garbage collector.
  325. * @c: UBIFS file-system description object
  326. * @anyway: do GC even if there are free LEBs
  327. *
  328. * This function does out-of-place garbage collection. The return codes are:
  329. * o positive LEB number if the LEB has been freed and may be used;
  330. * o %-EAGAIN if the caller has to run commit;
  331. * o %-ENOSPC if GC failed to make any progress;
  332. * o other negative error codes in case of other errors.
  333. *
  334. * Garbage collector writes data to the journal when GC'ing data LEBs, and just
  335. * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
  336. * commit may be required. But commit cannot be run from inside GC, because the
  337. * caller might be holding the commit lock, so %-EAGAIN is returned instead;
  338. * And this error code means that the caller has to run commit, and re-run GC
  339. * if there is still no free space.
  340. *
  341. * There are many reasons why this function may return %-EAGAIN:
  342. * o the log is full and there is no space to write an LEB reference for
  343. * @c->gc_lnum;
  344. * o the journal is too large and exceeds size limitations;
  345. * o GC moved indexing LEBs, but they can be used only after the commit;
  346. * o the shrinker fails to find clean znodes to free and requests the commit;
  347. * o etc.
  348. *
  349. * Note, if the file-system is close to be full, this function may return
  350. * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
  351. * the function. E.g., this happens if the limits on the journal size are too
  352. * tough and GC writes too much to the journal before an LEB is freed. This
  353. * might also mean that the journal is too large, and the TNC becomes to big,
  354. * so that the shrinker is constantly called, finds not clean znodes to free,
  355. * and requests commit. Well, this may also happen if the journal is all right,
  356. * but another kernel process consumes too much memory. Anyway, infinite
  357. * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
  358. */
  359. int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
  360. {
  361. int i, err, ret, min_space = c->dead_wm;
  362. struct ubifs_lprops lp;
  363. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  364. ubifs_assert_cmt_locked(c);
  365. if (ubifs_gc_should_commit(c))
  366. return -EAGAIN;
  367. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  368. if (c->ro_media) {
  369. ret = -EROFS;
  370. goto out_unlock;
  371. }
  372. /* We expect the write-buffer to be empty on entry */
  373. ubifs_assert(!wbuf->used);
  374. for (i = 0; ; i++) {
  375. int space_before = c->leb_size - wbuf->offs - wbuf->used;
  376. int space_after;
  377. cond_resched();
  378. /* Give the commit an opportunity to run */
  379. if (ubifs_gc_should_commit(c)) {
  380. ret = -EAGAIN;
  381. break;
  382. }
  383. if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
  384. /*
  385. * We've done enough iterations. Indexing LEBs were
  386. * moved and will be available after the commit.
  387. */
  388. dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
  389. ubifs_commit_required(c);
  390. ret = -EAGAIN;
  391. break;
  392. }
  393. if (i > HARD_LEBS_LIMIT) {
  394. /*
  395. * We've moved too many LEBs and have not made
  396. * progress, give up.
  397. */
  398. dbg_gc("hard limit, -ENOSPC");
  399. ret = -ENOSPC;
  400. break;
  401. }
  402. /*
  403. * Empty and freeable LEBs can turn up while we waited for
  404. * the wbuf lock, or while we have been running GC. In that
  405. * case, we should just return one of those instead of
  406. * continuing to GC dirty LEBs. Hence we request
  407. * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
  408. */
  409. ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
  410. if (ret) {
  411. if (ret == -ENOSPC)
  412. dbg_gc("no more dirty LEBs");
  413. break;
  414. }
  415. dbg_gc("found LEB %d: free %d, dirty %d, sum %d "
  416. "(min. space %d)", lp.lnum, lp.free, lp.dirty,
  417. lp.free + lp.dirty, min_space);
  418. if (lp.free + lp.dirty == c->leb_size) {
  419. /* An empty LEB was returned */
  420. dbg_gc("LEB %d is free, return it", lp.lnum);
  421. /*
  422. * ubifs_find_dirty_leb() doesn't return freeable index
  423. * LEBs.
  424. */
  425. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  426. if (lp.free != c->leb_size) {
  427. /*
  428. * Write buffers must be sync'd before
  429. * unmapping freeable LEBs, because one of them
  430. * may contain data which obsoletes something
  431. * in 'lp.pnum'.
  432. */
  433. ret = gc_sync_wbufs(c);
  434. if (ret)
  435. goto out;
  436. ret = ubifs_change_one_lp(c, lp.lnum,
  437. c->leb_size, 0, 0, 0,
  438. 0);
  439. if (ret)
  440. goto out;
  441. }
  442. ret = ubifs_leb_unmap(c, lp.lnum);
  443. if (ret)
  444. goto out;
  445. ret = lp.lnum;
  446. break;
  447. }
  448. space_before = c->leb_size - wbuf->offs - wbuf->used;
  449. if (wbuf->lnum == -1)
  450. space_before = 0;
  451. ret = ubifs_garbage_collect_leb(c, &lp);
  452. if (ret < 0) {
  453. if (ret == -EAGAIN || ret == -ENOSPC) {
  454. /*
  455. * These codes are not errors, so we have to
  456. * return the LEB to lprops. But if the
  457. * 'ubifs_return_leb()' function fails, its
  458. * failure code is propagated to the caller
  459. * instead of the original '-EAGAIN' or
  460. * '-ENOSPC'.
  461. */
  462. err = ubifs_return_leb(c, lp.lnum);
  463. if (err)
  464. ret = err;
  465. break;
  466. }
  467. goto out;
  468. }
  469. if (ret == LEB_FREED) {
  470. /* An LEB has been freed and is ready for use */
  471. dbg_gc("LEB %d freed, return", lp.lnum);
  472. ret = lp.lnum;
  473. break;
  474. }
  475. if (ret == LEB_FREED_IDX) {
  476. /*
  477. * This was an indexing LEB and it cannot be
  478. * immediately used. And instead of requesting the
  479. * commit straight away, we try to garbage collect some
  480. * more.
  481. */
  482. dbg_gc("indexing LEB %d freed, continue", lp.lnum);
  483. continue;
  484. }
  485. ubifs_assert(ret == LEB_RETAINED);
  486. space_after = c->leb_size - wbuf->offs - wbuf->used;
  487. dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
  488. space_after - space_before);
  489. if (space_after > space_before) {
  490. /* GC makes progress, keep working */
  491. min_space >>= 1;
  492. if (min_space < c->dead_wm)
  493. min_space = c->dead_wm;
  494. continue;
  495. }
  496. dbg_gc("did not make progress");
  497. /*
  498. * GC moved an LEB bud have not done any progress. This means
  499. * that the previous GC head LEB contained too few free space
  500. * and the LEB which was GC'ed contained only large nodes which
  501. * did not fit that space.
  502. *
  503. * We can do 2 things:
  504. * 1. pick another LEB in a hope it'll contain a small node
  505. * which will fit the space we have at the end of current GC
  506. * head LEB, but there is no guarantee, so we try this out
  507. * unless we have already been working for too long;
  508. * 2. request an LEB with more dirty space, which will force
  509. * 'ubifs_find_dirty_leb()' to start scanning the lprops
  510. * table, instead of just picking one from the heap
  511. * (previously it already picked the dirtiest LEB).
  512. */
  513. if (i < SOFT_LEBS_LIMIT) {
  514. dbg_gc("try again");
  515. continue;
  516. }
  517. min_space <<= 1;
  518. if (min_space > c->dark_wm)
  519. min_space = c->dark_wm;
  520. dbg_gc("set min. space to %d", min_space);
  521. }
  522. if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
  523. dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
  524. ubifs_commit_required(c);
  525. ret = -EAGAIN;
  526. }
  527. err = ubifs_wbuf_sync_nolock(wbuf);
  528. if (!err)
  529. err = ubifs_leb_unmap(c, c->gc_lnum);
  530. if (err) {
  531. ret = err;
  532. goto out;
  533. }
  534. out_unlock:
  535. mutex_unlock(&wbuf->io_mutex);
  536. return ret;
  537. out:
  538. ubifs_assert(ret < 0);
  539. ubifs_assert(ret != -ENOSPC && ret != -EAGAIN);
  540. ubifs_ro_mode(c, ret);
  541. ubifs_wbuf_sync_nolock(wbuf);
  542. mutex_unlock(&wbuf->io_mutex);
  543. ubifs_return_leb(c, lp.lnum);
  544. return ret;
  545. }
  546. /**
  547. * ubifs_gc_start_commit - garbage collection at start of commit.
  548. * @c: UBIFS file-system description object
  549. *
  550. * If a LEB has only dirty and free space, then we may safely unmap it and make
  551. * it free. Note, we cannot do this with indexing LEBs because dirty space may
  552. * correspond index nodes that are required for recovery. In that case, the
  553. * LEB cannot be unmapped until after the next commit.
  554. *
  555. * This function returns %0 upon success and a negative error code upon failure.
  556. */
  557. int ubifs_gc_start_commit(struct ubifs_info *c)
  558. {
  559. struct ubifs_gced_idx_leb *idx_gc;
  560. const struct ubifs_lprops *lp;
  561. int err = 0, flags;
  562. ubifs_get_lprops(c);
  563. /*
  564. * Unmap (non-index) freeable LEBs. Note that recovery requires that all
  565. * wbufs are sync'd before this, which is done in 'do_commit()'.
  566. */
  567. while (1) {
  568. lp = ubifs_fast_find_freeable(c);
  569. if (unlikely(IS_ERR(lp))) {
  570. err = PTR_ERR(lp);
  571. goto out;
  572. }
  573. if (!lp)
  574. break;
  575. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  576. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  577. err = ubifs_leb_unmap(c, lp->lnum);
  578. if (err)
  579. goto out;
  580. lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
  581. if (unlikely(IS_ERR(lp))) {
  582. err = PTR_ERR(lp);
  583. goto out;
  584. }
  585. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  586. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  587. }
  588. /* Mark GC'd index LEBs OK to unmap after this commit finishes */
  589. list_for_each_entry(idx_gc, &c->idx_gc, list)
  590. idx_gc->unmap = 1;
  591. /* Record index freeable LEBs for unmapping after commit */
  592. while (1) {
  593. lp = ubifs_fast_find_frdi_idx(c);
  594. if (unlikely(IS_ERR(lp))) {
  595. err = PTR_ERR(lp);
  596. goto out;
  597. }
  598. if (!lp)
  599. break;
  600. idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
  601. if (!idx_gc) {
  602. err = -ENOMEM;
  603. goto out;
  604. }
  605. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  606. ubifs_assert(lp->flags & LPROPS_INDEX);
  607. /* Don't release the LEB until after the next commit */
  608. flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
  609. lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
  610. if (unlikely(IS_ERR(lp))) {
  611. err = PTR_ERR(lp);
  612. kfree(idx_gc);
  613. goto out;
  614. }
  615. ubifs_assert(lp->flags & LPROPS_TAKEN);
  616. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  617. idx_gc->lnum = lp->lnum;
  618. idx_gc->unmap = 1;
  619. list_add(&idx_gc->list, &c->idx_gc);
  620. }
  621. out:
  622. ubifs_release_lprops(c);
  623. return err;
  624. }
  625. /**
  626. * ubifs_gc_end_commit - garbage collection at end of commit.
  627. * @c: UBIFS file-system description object
  628. *
  629. * This function completes out-of-place garbage collection of index LEBs.
  630. */
  631. int ubifs_gc_end_commit(struct ubifs_info *c)
  632. {
  633. struct ubifs_gced_idx_leb *idx_gc, *tmp;
  634. struct ubifs_wbuf *wbuf;
  635. int err = 0;
  636. wbuf = &c->jheads[GCHD].wbuf;
  637. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  638. list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
  639. if (idx_gc->unmap) {
  640. dbg_gc("LEB %d", idx_gc->lnum);
  641. err = ubifs_leb_unmap(c, idx_gc->lnum);
  642. if (err)
  643. goto out;
  644. err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
  645. LPROPS_NC, 0, LPROPS_TAKEN, -1);
  646. if (err)
  647. goto out;
  648. list_del(&idx_gc->list);
  649. kfree(idx_gc);
  650. }
  651. out:
  652. mutex_unlock(&wbuf->io_mutex);
  653. return err;
  654. }
  655. /**
  656. * ubifs_destroy_idx_gc - destroy idx_gc list.
  657. * @c: UBIFS file-system description object
  658. *
  659. * This function destroys the idx_gc list. It is called when unmounting or
  660. * remounting read-only so locks are not needed.
  661. */
  662. void ubifs_destroy_idx_gc(struct ubifs_info *c)
  663. {
  664. while (!list_empty(&c->idx_gc)) {
  665. struct ubifs_gced_idx_leb *idx_gc;
  666. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
  667. list);
  668. c->idx_gc_cnt -= 1;
  669. list_del(&idx_gc->list);
  670. kfree(idx_gc);
  671. }
  672. }
  673. /**
  674. * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
  675. * @c: UBIFS file-system description object
  676. *
  677. * Called during start commit so locks are not needed.
  678. */
  679. int ubifs_get_idx_gc_leb(struct ubifs_info *c)
  680. {
  681. struct ubifs_gced_idx_leb *idx_gc;
  682. int lnum;
  683. if (list_empty(&c->idx_gc))
  684. return -ENOSPC;
  685. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
  686. lnum = idx_gc->lnum;
  687. /* c->idx_gc_cnt is updated by the caller when lprops are updated */
  688. list_del(&idx_gc->list);
  689. kfree(idx_gc);
  690. return lnum;
  691. }