gc.c 25 KB

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