gc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. if (c->gc_lnum == -1) {
  302. c->gc_lnum = lnum;
  303. err = LEB_RETAINED;
  304. } else {
  305. err = ubifs_wbuf_sync_nolock(wbuf);
  306. if (err)
  307. goto out;
  308. err = ubifs_leb_unmap(c, lnum);
  309. if (err)
  310. goto out;
  311. err = LEB_FREED;
  312. }
  313. }
  314. out:
  315. ubifs_scan_destroy(sleb);
  316. return err;
  317. }
  318. /**
  319. * ubifs_garbage_collect - UBIFS garbage collector.
  320. * @c: UBIFS file-system description object
  321. * @anyway: do GC even if there are free LEBs
  322. *
  323. * This function does out-of-place garbage collection. The return codes are:
  324. * o positive LEB number if the LEB has been freed and may be used;
  325. * o %-EAGAIN if the caller has to run commit;
  326. * o %-ENOSPC if GC failed to make any progress;
  327. * o other negative error codes in case of other errors.
  328. *
  329. * Garbage collector writes data to the journal when GC'ing data LEBs, and just
  330. * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
  331. * commit may be required. But commit cannot be run from inside GC, because the
  332. * caller might be holding the commit lock, so %-EAGAIN is returned instead;
  333. * And this error code means that the caller has to run commit, and re-run GC
  334. * if there is still no free space.
  335. *
  336. * There are many reasons why this function may return %-EAGAIN:
  337. * o the log is full and there is no space to write an LEB reference for
  338. * @c->gc_lnum;
  339. * o the journal is too large and exceeds size limitations;
  340. * o GC moved indexing LEBs, but they can be used only after the commit;
  341. * o the shrinker fails to find clean znodes to free and requests the commit;
  342. * o etc.
  343. *
  344. * Note, if the file-system is close to be full, this function may return
  345. * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
  346. * the function. E.g., this happens if the limits on the journal size are too
  347. * tough and GC writes too much to the journal before an LEB is freed. This
  348. * might also mean that the journal is too large, and the TNC becomes to big,
  349. * so that the shrinker is constantly called, finds not clean znodes to free,
  350. * and requests commit. Well, this may also happen if the journal is all right,
  351. * but another kernel process consumes too much memory. Anyway, infinite
  352. * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
  353. */
  354. int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
  355. {
  356. int i, err, ret, min_space = c->dead_wm;
  357. struct ubifs_lprops lp;
  358. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  359. ubifs_assert_cmt_locked(c);
  360. if (ubifs_gc_should_commit(c))
  361. return -EAGAIN;
  362. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  363. if (c->ro_media) {
  364. ret = -EROFS;
  365. goto out_unlock;
  366. }
  367. /* We expect the write-buffer to be empty on entry */
  368. ubifs_assert(!wbuf->used);
  369. for (i = 0; ; i++) {
  370. int space_before = c->leb_size - wbuf->offs - wbuf->used;
  371. int space_after;
  372. cond_resched();
  373. /* Give the commit an opportunity to run */
  374. if (ubifs_gc_should_commit(c)) {
  375. ret = -EAGAIN;
  376. break;
  377. }
  378. if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
  379. /*
  380. * We've done enough iterations. Indexing LEBs were
  381. * moved and will be available after the commit.
  382. */
  383. dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
  384. ubifs_commit_required(c);
  385. ret = -EAGAIN;
  386. break;
  387. }
  388. if (i > HARD_LEBS_LIMIT) {
  389. /*
  390. * We've moved too many LEBs and have not made
  391. * progress, give up.
  392. */
  393. dbg_gc("hard limit, -ENOSPC");
  394. ret = -ENOSPC;
  395. break;
  396. }
  397. /*
  398. * Empty and freeable LEBs can turn up while we waited for
  399. * the wbuf lock, or while we have been running GC. In that
  400. * case, we should just return one of those instead of
  401. * continuing to GC dirty LEBs. Hence we request
  402. * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
  403. */
  404. ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
  405. if (ret) {
  406. if (ret == -ENOSPC)
  407. dbg_gc("no more dirty LEBs");
  408. break;
  409. }
  410. dbg_gc("found LEB %d: free %d, dirty %d, sum %d "
  411. "(min. space %d)", lp.lnum, lp.free, lp.dirty,
  412. lp.free + lp.dirty, min_space);
  413. if (lp.free + lp.dirty == c->leb_size) {
  414. /* An empty LEB was returned */
  415. dbg_gc("LEB %d is free, return it", lp.lnum);
  416. /*
  417. * ubifs_find_dirty_leb() doesn't return freeable index
  418. * LEBs.
  419. */
  420. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  421. if (lp.free != c->leb_size) {
  422. /*
  423. * Write buffers must be sync'd before
  424. * unmapping freeable LEBs, because one of them
  425. * may contain data which obsoletes something
  426. * in 'lp.pnum'.
  427. */
  428. ret = gc_sync_wbufs(c);
  429. if (ret)
  430. goto out;
  431. ret = ubifs_change_one_lp(c, lp.lnum,
  432. c->leb_size, 0, 0, 0,
  433. 0);
  434. if (ret)
  435. goto out;
  436. }
  437. ret = ubifs_leb_unmap(c, lp.lnum);
  438. if (ret)
  439. goto out;
  440. ret = lp.lnum;
  441. break;
  442. }
  443. space_before = c->leb_size - wbuf->offs - wbuf->used;
  444. if (wbuf->lnum == -1)
  445. space_before = 0;
  446. ret = ubifs_garbage_collect_leb(c, &lp);
  447. if (ret < 0) {
  448. if (ret == -EAGAIN || ret == -ENOSPC) {
  449. /*
  450. * These codes are not errors, so we have to
  451. * return the LEB to lprops. But if the
  452. * 'ubifs_return_leb()' function fails, its
  453. * failure code is propagated to the caller
  454. * instead of the original '-EAGAIN' or
  455. * '-ENOSPC'.
  456. */
  457. err = ubifs_return_leb(c, lp.lnum);
  458. if (err)
  459. ret = err;
  460. break;
  461. }
  462. goto out;
  463. }
  464. if (ret == LEB_FREED) {
  465. /* An LEB has been freed and is ready for use */
  466. dbg_gc("LEB %d freed, return", lp.lnum);
  467. ret = lp.lnum;
  468. break;
  469. }
  470. if (ret == LEB_FREED_IDX) {
  471. /*
  472. * This was an indexing LEB and it cannot be
  473. * immediately used. And instead of requesting the
  474. * commit straight away, we try to garbage collect some
  475. * more.
  476. */
  477. dbg_gc("indexing LEB %d freed, continue", lp.lnum);
  478. continue;
  479. }
  480. ubifs_assert(ret == LEB_RETAINED);
  481. space_after = c->leb_size - wbuf->offs - wbuf->used;
  482. dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
  483. space_after - space_before);
  484. if (space_after > space_before) {
  485. /* GC makes progress, keep working */
  486. min_space >>= 1;
  487. if (min_space < c->dead_wm)
  488. min_space = c->dead_wm;
  489. continue;
  490. }
  491. dbg_gc("did not make progress");
  492. /*
  493. * GC moved an LEB bud have not done any progress. This means
  494. * that the previous GC head LEB contained too few free space
  495. * and the LEB which was GC'ed contained only large nodes which
  496. * did not fit that space.
  497. *
  498. * We can do 2 things:
  499. * 1. pick another LEB in a hope it'll contain a small node
  500. * which will fit the space we have at the end of current GC
  501. * head LEB, but there is no guarantee, so we try this out
  502. * unless we have already been working for too long;
  503. * 2. request an LEB with more dirty space, which will force
  504. * 'ubifs_find_dirty_leb()' to start scanning the lprops
  505. * table, instead of just picking one from the heap
  506. * (previously it already picked the dirtiest LEB).
  507. */
  508. if (i < SOFT_LEBS_LIMIT) {
  509. dbg_gc("try again");
  510. continue;
  511. }
  512. min_space <<= 1;
  513. if (min_space > c->dark_wm)
  514. min_space = c->dark_wm;
  515. dbg_gc("set min. space to %d", min_space);
  516. }
  517. if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
  518. dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
  519. ubifs_commit_required(c);
  520. ret = -EAGAIN;
  521. }
  522. err = ubifs_wbuf_sync_nolock(wbuf);
  523. if (!err)
  524. err = ubifs_leb_unmap(c, c->gc_lnum);
  525. if (err) {
  526. ret = err;
  527. goto out;
  528. }
  529. out_unlock:
  530. mutex_unlock(&wbuf->io_mutex);
  531. return ret;
  532. out:
  533. ubifs_assert(ret < 0);
  534. ubifs_assert(ret != -ENOSPC && ret != -EAGAIN);
  535. ubifs_ro_mode(c, ret);
  536. ubifs_wbuf_sync_nolock(wbuf);
  537. mutex_unlock(&wbuf->io_mutex);
  538. ubifs_return_leb(c, lp.lnum);
  539. return ret;
  540. }
  541. /**
  542. * ubifs_gc_start_commit - garbage collection at start of commit.
  543. * @c: UBIFS file-system description object
  544. *
  545. * If a LEB has only dirty and free space, then we may safely unmap it and make
  546. * it free. Note, we cannot do this with indexing LEBs because dirty space may
  547. * correspond index nodes that are required for recovery. In that case, the
  548. * LEB cannot be unmapped until after the next commit.
  549. *
  550. * This function returns %0 upon success and a negative error code upon failure.
  551. */
  552. int ubifs_gc_start_commit(struct ubifs_info *c)
  553. {
  554. struct ubifs_gced_idx_leb *idx_gc;
  555. const struct ubifs_lprops *lp;
  556. int err = 0, flags;
  557. ubifs_get_lprops(c);
  558. /*
  559. * Unmap (non-index) freeable LEBs. Note that recovery requires that all
  560. * wbufs are sync'd before this, which is done in 'do_commit()'.
  561. */
  562. while (1) {
  563. lp = ubifs_fast_find_freeable(c);
  564. if (unlikely(IS_ERR(lp))) {
  565. err = PTR_ERR(lp);
  566. goto out;
  567. }
  568. if (!lp)
  569. break;
  570. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  571. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  572. err = ubifs_leb_unmap(c, lp->lnum);
  573. if (err)
  574. goto out;
  575. lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
  576. if (unlikely(IS_ERR(lp))) {
  577. err = PTR_ERR(lp);
  578. goto out;
  579. }
  580. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  581. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  582. }
  583. /* Mark GC'd index LEBs OK to unmap after this commit finishes */
  584. list_for_each_entry(idx_gc, &c->idx_gc, list)
  585. idx_gc->unmap = 1;
  586. /* Record index freeable LEBs for unmapping after commit */
  587. while (1) {
  588. lp = ubifs_fast_find_frdi_idx(c);
  589. if (unlikely(IS_ERR(lp))) {
  590. err = PTR_ERR(lp);
  591. goto out;
  592. }
  593. if (!lp)
  594. break;
  595. idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
  596. if (!idx_gc) {
  597. err = -ENOMEM;
  598. goto out;
  599. }
  600. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  601. ubifs_assert(lp->flags & LPROPS_INDEX);
  602. /* Don't release the LEB until after the next commit */
  603. flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
  604. lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
  605. if (unlikely(IS_ERR(lp))) {
  606. err = PTR_ERR(lp);
  607. kfree(idx_gc);
  608. goto out;
  609. }
  610. ubifs_assert(lp->flags & LPROPS_TAKEN);
  611. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  612. idx_gc->lnum = lp->lnum;
  613. idx_gc->unmap = 1;
  614. list_add(&idx_gc->list, &c->idx_gc);
  615. }
  616. out:
  617. ubifs_release_lprops(c);
  618. return err;
  619. }
  620. /**
  621. * ubifs_gc_end_commit - garbage collection at end of commit.
  622. * @c: UBIFS file-system description object
  623. *
  624. * This function completes out-of-place garbage collection of index LEBs.
  625. */
  626. int ubifs_gc_end_commit(struct ubifs_info *c)
  627. {
  628. struct ubifs_gced_idx_leb *idx_gc, *tmp;
  629. struct ubifs_wbuf *wbuf;
  630. int err = 0;
  631. wbuf = &c->jheads[GCHD].wbuf;
  632. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  633. list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
  634. if (idx_gc->unmap) {
  635. dbg_gc("LEB %d", idx_gc->lnum);
  636. err = ubifs_leb_unmap(c, idx_gc->lnum);
  637. if (err)
  638. goto out;
  639. err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
  640. LPROPS_NC, 0, LPROPS_TAKEN, -1);
  641. if (err)
  642. goto out;
  643. list_del(&idx_gc->list);
  644. kfree(idx_gc);
  645. }
  646. out:
  647. mutex_unlock(&wbuf->io_mutex);
  648. return err;
  649. }
  650. /**
  651. * ubifs_destroy_idx_gc - destroy idx_gc list.
  652. * @c: UBIFS file-system description object
  653. *
  654. * This function destroys the idx_gc list. It is called when unmounting or
  655. * remounting read-only so locks are not needed.
  656. */
  657. void ubifs_destroy_idx_gc(struct ubifs_info *c)
  658. {
  659. while (!list_empty(&c->idx_gc)) {
  660. struct ubifs_gced_idx_leb *idx_gc;
  661. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
  662. list);
  663. c->idx_gc_cnt -= 1;
  664. list_del(&idx_gc->list);
  665. kfree(idx_gc);
  666. }
  667. }
  668. /**
  669. * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
  670. * @c: UBIFS file-system description object
  671. *
  672. * Called during start commit so locks are not needed.
  673. */
  674. int ubifs_get_idx_gc_leb(struct ubifs_info *c)
  675. {
  676. struct ubifs_gced_idx_leb *idx_gc;
  677. int lnum;
  678. if (list_empty(&c->idx_gc))
  679. return -ENOSPC;
  680. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
  681. lnum = idx_gc->lnum;
  682. /* c->idx_gc_cnt is updated by the caller when lprops are updated */
  683. list_del(&idx_gc->list);
  684. kfree(idx_gc);
  685. return lnum;
  686. }