gc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. * joinup - bring data nodes for an inode together.
  89. * @c: UBIFS file-system description object
  90. * @sleb: describes scanned LEB
  91. * @inum: inode number
  92. * @blk: block number
  93. * @data: list to which to add data nodes
  94. *
  95. * This function looks at the first few nodes in the scanned LEB @sleb and adds
  96. * them to @data if they are data nodes from @inum and have a larger block
  97. * number than @blk. This function returns %0 on success and a negative error
  98. * code on failure.
  99. */
  100. static int joinup(struct ubifs_info *c, struct ubifs_scan_leb *sleb, ino_t inum,
  101. unsigned int blk, struct list_head *data)
  102. {
  103. int err, cnt = 6, lnum = sleb->lnum, offs;
  104. struct ubifs_scan_node *snod, *tmp;
  105. union ubifs_key *key;
  106. list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
  107. key = &snod->key;
  108. if (key_inum(c, key) == inum &&
  109. key_type(c, key) == UBIFS_DATA_KEY &&
  110. key_block(c, key) > blk) {
  111. offs = snod->offs;
  112. err = ubifs_tnc_has_node(c, key, 0, lnum, offs, 0);
  113. if (err < 0)
  114. return err;
  115. list_del(&snod->list);
  116. if (err) {
  117. list_add_tail(&snod->list, data);
  118. blk = key_block(c, key);
  119. } else
  120. kfree(snod);
  121. cnt = 6;
  122. } else if (--cnt == 0)
  123. break;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * move_nodes - move nodes.
  129. * @c: UBIFS file-system description object
  130. * @sleb: describes nodes to move
  131. *
  132. * This function moves valid nodes from data LEB described by @sleb to the GC
  133. * journal head. The obsolete nodes are dropped.
  134. *
  135. * When moving nodes we have to deal with classical bin-packing problem: the
  136. * space in the current GC journal head LEB and in @c->gc_lnum are the "bins",
  137. * where the nodes in the @sleb->nodes list are the elements which should be
  138. * fit optimally to the bins. This function uses the "first fit decreasing"
  139. * strategy, although it does not really sort the nodes but just split them on
  140. * 3 classes - large, medium, and small, so they are roughly sorted.
  141. *
  142. * This function returns zero in case of success, %-EAGAIN if commit is
  143. * required, and other negative error codes in case of other failures.
  144. */
  145. static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
  146. {
  147. struct ubifs_scan_node *snod, *tmp;
  148. struct list_head data, large, medium, small;
  149. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  150. int avail, err, min = INT_MAX;
  151. unsigned int blk = 0;
  152. ino_t inum = 0;
  153. INIT_LIST_HEAD(&data);
  154. INIT_LIST_HEAD(&large);
  155. INIT_LIST_HEAD(&medium);
  156. INIT_LIST_HEAD(&small);
  157. while (!list_empty(&sleb->nodes)) {
  158. struct list_head *lst = sleb->nodes.next;
  159. snod = list_entry(lst, struct ubifs_scan_node, list);
  160. ubifs_assert(snod->type != UBIFS_IDX_NODE);
  161. ubifs_assert(snod->type != UBIFS_REF_NODE);
  162. ubifs_assert(snod->type != UBIFS_CS_NODE);
  163. err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
  164. snod->offs, 0);
  165. if (err < 0)
  166. goto out;
  167. list_del(lst);
  168. if (!err) {
  169. /* The node is obsolete, remove it from the list */
  170. kfree(snod);
  171. continue;
  172. }
  173. /*
  174. * Sort the list of nodes so that data nodes go first, large
  175. * nodes go second, and small nodes go last.
  176. */
  177. if (key_type(c, &snod->key) == UBIFS_DATA_KEY) {
  178. if (inum != key_inum(c, &snod->key)) {
  179. if (inum) {
  180. /*
  181. * Try to move data nodes from the same
  182. * inode together.
  183. */
  184. err = joinup(c, sleb, inum, blk, &data);
  185. if (err)
  186. goto out;
  187. }
  188. inum = key_inum(c, &snod->key);
  189. blk = key_block(c, &snod->key);
  190. }
  191. list_add_tail(lst, &data);
  192. } else if (snod->len > MEDIUM_NODE_WM)
  193. list_add_tail(lst, &large);
  194. else if (snod->len > SMALL_NODE_WM)
  195. list_add_tail(lst, &medium);
  196. else
  197. list_add_tail(lst, &small);
  198. /* And find the smallest node */
  199. if (snod->len < min)
  200. min = snod->len;
  201. }
  202. /*
  203. * Join the tree lists so that we'd have one roughly sorted list
  204. * ('large' will be the head of the joined list).
  205. */
  206. list_splice(&data, &large);
  207. list_splice(&medium, large.prev);
  208. list_splice(&small, large.prev);
  209. if (wbuf->lnum == -1) {
  210. /*
  211. * The GC journal head is not set, because it is the first GC
  212. * invocation since mount.
  213. */
  214. err = switch_gc_head(c);
  215. if (err)
  216. goto out;
  217. }
  218. /* Write nodes to their new location. Use the first-fit strategy */
  219. while (1) {
  220. avail = c->leb_size - wbuf->offs - wbuf->used;
  221. list_for_each_entry_safe(snod, tmp, &large, list) {
  222. int new_lnum, new_offs;
  223. if (avail < min)
  224. break;
  225. if (snod->len > avail)
  226. /* This node does not fit */
  227. continue;
  228. cond_resched();
  229. new_lnum = wbuf->lnum;
  230. new_offs = wbuf->offs + wbuf->used;
  231. err = ubifs_wbuf_write_nolock(wbuf, snod->node,
  232. snod->len);
  233. if (err)
  234. goto out;
  235. err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
  236. snod->offs, new_lnum, new_offs,
  237. snod->len);
  238. if (err)
  239. goto out;
  240. avail = c->leb_size - wbuf->offs - wbuf->used;
  241. list_del(&snod->list);
  242. kfree(snod);
  243. }
  244. if (list_empty(&large))
  245. break;
  246. /*
  247. * Waste the rest of the space in the LEB and switch to the
  248. * next LEB.
  249. */
  250. err = switch_gc_head(c);
  251. if (err)
  252. goto out;
  253. }
  254. return 0;
  255. out:
  256. list_for_each_entry_safe(snod, tmp, &large, list) {
  257. list_del(&snod->list);
  258. kfree(snod);
  259. }
  260. return err;
  261. }
  262. /**
  263. * gc_sync_wbufs - sync write-buffers for GC.
  264. * @c: UBIFS file-system description object
  265. *
  266. * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
  267. * be in a write-buffer instead. That is, a node could be written to a
  268. * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
  269. * erased before the write-buffer is sync'd and then there is an unclean
  270. * unmount, then an existing node is lost. To avoid this, we sync all
  271. * write-buffers.
  272. *
  273. * This function returns %0 on success or a negative error code on failure.
  274. */
  275. static int gc_sync_wbufs(struct ubifs_info *c)
  276. {
  277. int err, i;
  278. for (i = 0; i < c->jhead_cnt; i++) {
  279. if (i == GCHD)
  280. continue;
  281. err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  282. if (err)
  283. return err;
  284. }
  285. return 0;
  286. }
  287. /**
  288. * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
  289. * @c: UBIFS file-system description object
  290. * @lp: describes the LEB to garbage collect
  291. *
  292. * This function garbage-collects an LEB and returns one of the @LEB_FREED,
  293. * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
  294. * required, and other negative error codes in case of failures.
  295. */
  296. int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
  297. {
  298. struct ubifs_scan_leb *sleb;
  299. struct ubifs_scan_node *snod;
  300. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  301. int err = 0, lnum = lp->lnum;
  302. ubifs_assert(c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
  303. c->need_recovery);
  304. ubifs_assert(c->gc_lnum != lnum);
  305. ubifs_assert(wbuf->lnum != lnum);
  306. /*
  307. * We scan the entire LEB even though we only really need to scan up to
  308. * (c->leb_size - lp->free).
  309. */
  310. sleb = ubifs_scan(c, lnum, 0, c->sbuf);
  311. if (IS_ERR(sleb))
  312. return PTR_ERR(sleb);
  313. ubifs_assert(!list_empty(&sleb->nodes));
  314. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  315. if (snod->type == UBIFS_IDX_NODE) {
  316. struct ubifs_gced_idx_leb *idx_gc;
  317. dbg_gc("indexing LEB %d (free %d, dirty %d)",
  318. lnum, lp->free, lp->dirty);
  319. list_for_each_entry(snod, &sleb->nodes, list) {
  320. struct ubifs_idx_node *idx = snod->node;
  321. int level = le16_to_cpu(idx->level);
  322. ubifs_assert(snod->type == UBIFS_IDX_NODE);
  323. key_read(c, ubifs_idx_key(c, idx), &snod->key);
  324. err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
  325. snod->offs);
  326. if (err)
  327. goto out;
  328. }
  329. idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
  330. if (!idx_gc) {
  331. err = -ENOMEM;
  332. goto out;
  333. }
  334. idx_gc->lnum = lnum;
  335. idx_gc->unmap = 0;
  336. list_add(&idx_gc->list, &c->idx_gc);
  337. /*
  338. * Don't release the LEB until after the next commit, because
  339. * it may contain date which is needed for recovery. So
  340. * although we freed this LEB, it will become usable only after
  341. * the commit.
  342. */
  343. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
  344. LPROPS_INDEX, 1);
  345. if (err)
  346. goto out;
  347. err = LEB_FREED_IDX;
  348. } else {
  349. dbg_gc("data LEB %d (free %d, dirty %d)",
  350. lnum, lp->free, lp->dirty);
  351. err = move_nodes(c, sleb);
  352. if (err)
  353. goto out_inc_seq;
  354. err = gc_sync_wbufs(c);
  355. if (err)
  356. goto out_inc_seq;
  357. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
  358. if (err)
  359. goto out_inc_seq;
  360. /* Allow for races with TNC */
  361. c->gced_lnum = lnum;
  362. smp_wmb();
  363. c->gc_seq += 1;
  364. smp_wmb();
  365. if (c->gc_lnum == -1) {
  366. c->gc_lnum = lnum;
  367. err = LEB_RETAINED;
  368. } else {
  369. err = ubifs_wbuf_sync_nolock(wbuf);
  370. if (err)
  371. goto out;
  372. err = ubifs_leb_unmap(c, lnum);
  373. if (err)
  374. goto out;
  375. err = LEB_FREED;
  376. }
  377. }
  378. out:
  379. ubifs_scan_destroy(sleb);
  380. return err;
  381. out_inc_seq:
  382. /* We may have moved at least some nodes so allow for races with TNC */
  383. c->gced_lnum = lnum;
  384. smp_wmb();
  385. c->gc_seq += 1;
  386. smp_wmb();
  387. goto out;
  388. }
  389. /**
  390. * ubifs_garbage_collect - UBIFS garbage collector.
  391. * @c: UBIFS file-system description object
  392. * @anyway: do GC even if there are free LEBs
  393. *
  394. * This function does out-of-place garbage collection. The return codes are:
  395. * o positive LEB number if the LEB has been freed and may be used;
  396. * o %-EAGAIN if the caller has to run commit;
  397. * o %-ENOSPC if GC failed to make any progress;
  398. * o other negative error codes in case of other errors.
  399. *
  400. * Garbage collector writes data to the journal when GC'ing data LEBs, and just
  401. * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
  402. * commit may be required. But commit cannot be run from inside GC, because the
  403. * caller might be holding the commit lock, so %-EAGAIN is returned instead;
  404. * And this error code means that the caller has to run commit, and re-run GC
  405. * if there is still no free space.
  406. *
  407. * There are many reasons why this function may return %-EAGAIN:
  408. * o the log is full and there is no space to write an LEB reference for
  409. * @c->gc_lnum;
  410. * o the journal is too large and exceeds size limitations;
  411. * o GC moved indexing LEBs, but they can be used only after the commit;
  412. * o the shrinker fails to find clean znodes to free and requests the commit;
  413. * o etc.
  414. *
  415. * Note, if the file-system is close to be full, this function may return
  416. * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
  417. * the function. E.g., this happens if the limits on the journal size are too
  418. * tough and GC writes too much to the journal before an LEB is freed. This
  419. * might also mean that the journal is too large, and the TNC becomes to big,
  420. * so that the shrinker is constantly called, finds not clean znodes to free,
  421. * and requests commit. Well, this may also happen if the journal is all right,
  422. * but another kernel process consumes too much memory. Anyway, infinite
  423. * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
  424. */
  425. int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
  426. {
  427. int i, err, ret, min_space = c->dead_wm;
  428. struct ubifs_lprops lp;
  429. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  430. ubifs_assert_cmt_locked(c);
  431. if (ubifs_gc_should_commit(c))
  432. return -EAGAIN;
  433. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  434. if (c->ro_media) {
  435. ret = -EROFS;
  436. goto out_unlock;
  437. }
  438. /* We expect the write-buffer to be empty on entry */
  439. ubifs_assert(!wbuf->used);
  440. for (i = 0; ; i++) {
  441. int space_before = c->leb_size - wbuf->offs - wbuf->used;
  442. int space_after;
  443. cond_resched();
  444. /* Give the commit an opportunity to run */
  445. if (ubifs_gc_should_commit(c)) {
  446. ret = -EAGAIN;
  447. break;
  448. }
  449. if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
  450. /*
  451. * We've done enough iterations. Indexing LEBs were
  452. * moved and will be available after the commit.
  453. */
  454. dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
  455. ubifs_commit_required(c);
  456. ret = -EAGAIN;
  457. break;
  458. }
  459. if (i > HARD_LEBS_LIMIT) {
  460. /*
  461. * We've moved too many LEBs and have not made
  462. * progress, give up.
  463. */
  464. dbg_gc("hard limit, -ENOSPC");
  465. ret = -ENOSPC;
  466. break;
  467. }
  468. /*
  469. * Empty and freeable LEBs can turn up while we waited for
  470. * the wbuf lock, or while we have been running GC. In that
  471. * case, we should just return one of those instead of
  472. * continuing to GC dirty LEBs. Hence we request
  473. * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
  474. */
  475. ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
  476. if (ret) {
  477. if (ret == -ENOSPC)
  478. dbg_gc("no more dirty LEBs");
  479. break;
  480. }
  481. dbg_gc("found LEB %d: free %d, dirty %d, sum %d "
  482. "(min. space %d)", lp.lnum, lp.free, lp.dirty,
  483. lp.free + lp.dirty, min_space);
  484. if (lp.free + lp.dirty == c->leb_size) {
  485. /* An empty LEB was returned */
  486. dbg_gc("LEB %d is free, return it", lp.lnum);
  487. /*
  488. * ubifs_find_dirty_leb() doesn't return freeable index
  489. * LEBs.
  490. */
  491. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  492. if (lp.free != c->leb_size) {
  493. /*
  494. * Write buffers must be sync'd before
  495. * unmapping freeable LEBs, because one of them
  496. * may contain data which obsoletes something
  497. * in 'lp.pnum'.
  498. */
  499. ret = gc_sync_wbufs(c);
  500. if (ret)
  501. goto out;
  502. ret = ubifs_change_one_lp(c, lp.lnum,
  503. c->leb_size, 0, 0, 0,
  504. 0);
  505. if (ret)
  506. goto out;
  507. }
  508. ret = ubifs_leb_unmap(c, lp.lnum);
  509. if (ret)
  510. goto out;
  511. ret = lp.lnum;
  512. break;
  513. }
  514. space_before = c->leb_size - wbuf->offs - wbuf->used;
  515. if (wbuf->lnum == -1)
  516. space_before = 0;
  517. ret = ubifs_garbage_collect_leb(c, &lp);
  518. if (ret < 0) {
  519. if (ret == -EAGAIN || ret == -ENOSPC) {
  520. /*
  521. * These codes are not errors, so we have to
  522. * return the LEB to lprops. But if the
  523. * 'ubifs_return_leb()' function fails, its
  524. * failure code is propagated to the caller
  525. * instead of the original '-EAGAIN' or
  526. * '-ENOSPC'.
  527. */
  528. err = ubifs_return_leb(c, lp.lnum);
  529. if (err)
  530. ret = err;
  531. break;
  532. }
  533. goto out;
  534. }
  535. if (ret == LEB_FREED) {
  536. /* An LEB has been freed and is ready for use */
  537. dbg_gc("LEB %d freed, return", lp.lnum);
  538. ret = lp.lnum;
  539. break;
  540. }
  541. if (ret == LEB_FREED_IDX) {
  542. /*
  543. * This was an indexing LEB and it cannot be
  544. * immediately used. And instead of requesting the
  545. * commit straight away, we try to garbage collect some
  546. * more.
  547. */
  548. dbg_gc("indexing LEB %d freed, continue", lp.lnum);
  549. continue;
  550. }
  551. ubifs_assert(ret == LEB_RETAINED);
  552. space_after = c->leb_size - wbuf->offs - wbuf->used;
  553. dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
  554. space_after - space_before);
  555. if (space_after > space_before) {
  556. /* GC makes progress, keep working */
  557. min_space >>= 1;
  558. if (min_space < c->dead_wm)
  559. min_space = c->dead_wm;
  560. continue;
  561. }
  562. dbg_gc("did not make progress");
  563. /*
  564. * GC moved an LEB bud have not done any progress. This means
  565. * that the previous GC head LEB contained too few free space
  566. * and the LEB which was GC'ed contained only large nodes which
  567. * did not fit that space.
  568. *
  569. * We can do 2 things:
  570. * 1. pick another LEB in a hope it'll contain a small node
  571. * which will fit the space we have at the end of current GC
  572. * head LEB, but there is no guarantee, so we try this out
  573. * unless we have already been working for too long;
  574. * 2. request an LEB with more dirty space, which will force
  575. * 'ubifs_find_dirty_leb()' to start scanning the lprops
  576. * table, instead of just picking one from the heap
  577. * (previously it already picked the dirtiest LEB).
  578. */
  579. if (i < SOFT_LEBS_LIMIT) {
  580. dbg_gc("try again");
  581. continue;
  582. }
  583. min_space <<= 1;
  584. if (min_space > c->dark_wm)
  585. min_space = c->dark_wm;
  586. dbg_gc("set min. space to %d", min_space);
  587. }
  588. if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
  589. dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
  590. ubifs_commit_required(c);
  591. ret = -EAGAIN;
  592. }
  593. err = ubifs_wbuf_sync_nolock(wbuf);
  594. if (!err)
  595. err = ubifs_leb_unmap(c, c->gc_lnum);
  596. if (err) {
  597. ret = err;
  598. goto out;
  599. }
  600. out_unlock:
  601. mutex_unlock(&wbuf->io_mutex);
  602. return ret;
  603. out:
  604. ubifs_assert(ret < 0);
  605. ubifs_assert(ret != -ENOSPC && ret != -EAGAIN);
  606. ubifs_ro_mode(c, ret);
  607. ubifs_wbuf_sync_nolock(wbuf);
  608. mutex_unlock(&wbuf->io_mutex);
  609. ubifs_return_leb(c, lp.lnum);
  610. return ret;
  611. }
  612. /**
  613. * ubifs_gc_start_commit - garbage collection at start of commit.
  614. * @c: UBIFS file-system description object
  615. *
  616. * If a LEB has only dirty and free space, then we may safely unmap it and make
  617. * it free. Note, we cannot do this with indexing LEBs because dirty space may
  618. * correspond index nodes that are required for recovery. In that case, the
  619. * LEB cannot be unmapped until after the next commit.
  620. *
  621. * This function returns %0 upon success and a negative error code upon failure.
  622. */
  623. int ubifs_gc_start_commit(struct ubifs_info *c)
  624. {
  625. struct ubifs_gced_idx_leb *idx_gc;
  626. const struct ubifs_lprops *lp;
  627. int err = 0, flags;
  628. ubifs_get_lprops(c);
  629. /*
  630. * Unmap (non-index) freeable LEBs. Note that recovery requires that all
  631. * wbufs are sync'd before this, which is done in 'do_commit()'.
  632. */
  633. while (1) {
  634. lp = ubifs_fast_find_freeable(c);
  635. if (IS_ERR(lp)) {
  636. err = PTR_ERR(lp);
  637. goto out;
  638. }
  639. if (!lp)
  640. break;
  641. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  642. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  643. err = ubifs_leb_unmap(c, lp->lnum);
  644. if (err)
  645. goto out;
  646. lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
  647. if (IS_ERR(lp)) {
  648. err = PTR_ERR(lp);
  649. goto out;
  650. }
  651. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  652. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  653. }
  654. /* Mark GC'd index LEBs OK to unmap after this commit finishes */
  655. list_for_each_entry(idx_gc, &c->idx_gc, list)
  656. idx_gc->unmap = 1;
  657. /* Record index freeable LEBs for unmapping after commit */
  658. while (1) {
  659. lp = ubifs_fast_find_frdi_idx(c);
  660. if (IS_ERR(lp)) {
  661. err = PTR_ERR(lp);
  662. goto out;
  663. }
  664. if (!lp)
  665. break;
  666. idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
  667. if (!idx_gc) {
  668. err = -ENOMEM;
  669. goto out;
  670. }
  671. ubifs_assert(!(lp->flags & LPROPS_TAKEN));
  672. ubifs_assert(lp->flags & LPROPS_INDEX);
  673. /* Don't release the LEB until after the next commit */
  674. flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
  675. lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
  676. if (IS_ERR(lp)) {
  677. err = PTR_ERR(lp);
  678. kfree(idx_gc);
  679. goto out;
  680. }
  681. ubifs_assert(lp->flags & LPROPS_TAKEN);
  682. ubifs_assert(!(lp->flags & LPROPS_INDEX));
  683. idx_gc->lnum = lp->lnum;
  684. idx_gc->unmap = 1;
  685. list_add(&idx_gc->list, &c->idx_gc);
  686. }
  687. out:
  688. ubifs_release_lprops(c);
  689. return err;
  690. }
  691. /**
  692. * ubifs_gc_end_commit - garbage collection at end of commit.
  693. * @c: UBIFS file-system description object
  694. *
  695. * This function completes out-of-place garbage collection of index LEBs.
  696. */
  697. int ubifs_gc_end_commit(struct ubifs_info *c)
  698. {
  699. struct ubifs_gced_idx_leb *idx_gc, *tmp;
  700. struct ubifs_wbuf *wbuf;
  701. int err = 0;
  702. wbuf = &c->jheads[GCHD].wbuf;
  703. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  704. list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
  705. if (idx_gc->unmap) {
  706. dbg_gc("LEB %d", idx_gc->lnum);
  707. err = ubifs_leb_unmap(c, idx_gc->lnum);
  708. if (err)
  709. goto out;
  710. err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
  711. LPROPS_NC, 0, LPROPS_TAKEN, -1);
  712. if (err)
  713. goto out;
  714. list_del(&idx_gc->list);
  715. kfree(idx_gc);
  716. }
  717. out:
  718. mutex_unlock(&wbuf->io_mutex);
  719. return err;
  720. }
  721. /**
  722. * ubifs_destroy_idx_gc - destroy idx_gc list.
  723. * @c: UBIFS file-system description object
  724. *
  725. * This function destroys the idx_gc list. It is called when unmounting or
  726. * remounting read-only so locks are not needed.
  727. */
  728. void ubifs_destroy_idx_gc(struct ubifs_info *c)
  729. {
  730. while (!list_empty(&c->idx_gc)) {
  731. struct ubifs_gced_idx_leb *idx_gc;
  732. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
  733. list);
  734. c->idx_gc_cnt -= 1;
  735. list_del(&idx_gc->list);
  736. kfree(idx_gc);
  737. }
  738. }
  739. /**
  740. * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
  741. * @c: UBIFS file-system description object
  742. *
  743. * Called during start commit so locks are not needed.
  744. */
  745. int ubifs_get_idx_gc_leb(struct ubifs_info *c)
  746. {
  747. struct ubifs_gced_idx_leb *idx_gc;
  748. int lnum;
  749. if (list_empty(&c->idx_gc))
  750. return -ENOSPC;
  751. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
  752. lnum = idx_gc->lnum;
  753. /* c->idx_gc_cnt is updated by the caller when lprops are updated */
  754. list_del(&idx_gc->list);
  755. kfree(idx_gc);
  756. return lnum;
  757. }