gc.c 22 KB

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