gc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file implements garbage collection. The procedure for garbage collection
  12. * is different depending on whether a LEB as an index LEB (contains index
  13. * nodes) or not. For non-index LEBs, garbage collection finds a LEB which
  14. * contains a lot of dirty space (obsolete nodes), and copies the non-obsolete
  15. * nodes to the journal, at which point the garbage-collected LEB is free to be
  16. * reused. For index LEBs, garbage collection marks the non-obsolete index nodes
  17. * dirty in the TNC, and after the next commit, the garbage-collected LEB is
  18. * to be reused. Garbage collection will cause the number of dirty index nodes
  19. * to grow, however sufficient space is reserved for the index to ensure the
  20. * commit will never run out of space.
  21. *
  22. * Notes about dead watermark. At current UBIFS implementation we assume that
  23. * LEBs which have less than @c->dead_wm bytes of free + dirty space are full
  24. * and not worth garbage-collecting. The dead watermark is one min. I/O unit
  25. * size, or min. UBIFS node size, depending on what is greater. Indeed, UBIFS
  26. * Garbage Collector has to synchronize the GC head's write buffer before
  27. * returning, so this is about wasting one min. I/O unit. However, UBIFS GC can
  28. * actually reclaim even very small pieces of dirty space by garbage collecting
  29. * enough dirty LEBs, but we do not bother doing this at this implementation.
  30. *
  31. * Notes about dark watermark. The results of GC work depends on how big are
  32. * the UBIFS nodes GC deals with. Large nodes make GC waste more space. Indeed,
  33. * if GC move data from LEB A to LEB B and nodes in LEB A are large, GC would
  34. * have to waste large pieces of free space at the end of LEB B, because nodes
  35. * from LEB A would not fit. And the worst situation is when all nodes are of
  36. * maximum size. So dark watermark is the amount of free + dirty space in LEB
  37. * which are guaranteed to be reclaimable. If LEB has less space, the GC might
  38. * be unable to reclaim it. So, LEBs with free + dirty greater than dark
  39. * watermark are "good" LEBs from GC's point of view. The other LEBs are not so
  40. * good, and GC takes extra care when moving them.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/list_sort.h>
  45. #include "ubifs.h"
  46. /*
  47. * GC may need to move more than one LEB to make progress. The below constants
  48. * define "soft" and "hard" limits on the number of LEBs the garbage collector
  49. * may move.
  50. */
  51. #define SOFT_LEBS_LIMIT 4
  52. #define HARD_LEBS_LIMIT 32
  53. /**
  54. * switch_gc_head - switch the garbage collection journal head.
  55. * @c: UBIFS file-system description object
  56. *
  57. * This function switch the GC head to the next LEB which is reserved in
  58. * @c->gc_lnum. Returns %0 in case of success, %-EAGAIN if commit is required,
  59. * and other negative error code in case of failures.
  60. */
  61. static int switch_gc_head(struct ubifs_info *c)
  62. {
  63. int err, gc_lnum = c->gc_lnum;
  64. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  65. ubifs_assert(c, gc_lnum != -1);
  66. dbg_gc("switch GC head from LEB %d:%d to LEB %d (waste %d bytes)",
  67. wbuf->lnum, wbuf->offs + wbuf->used, gc_lnum,
  68. c->leb_size - wbuf->offs - wbuf->used);
  69. err = ubifs_wbuf_sync_nolock(wbuf);
  70. if (err)
  71. return err;
  72. /*
  73. * The GC write-buffer was synchronized, we may safely unmap
  74. * 'c->gc_lnum'.
  75. */
  76. err = ubifs_leb_unmap(c, gc_lnum);
  77. if (err)
  78. return err;
  79. err = ubifs_add_bud_to_log(c, GCHD, gc_lnum, 0);
  80. if (err)
  81. return err;
  82. c->gc_lnum = -1;
  83. err = ubifs_wbuf_seek_nolock(wbuf, gc_lnum, 0);
  84. return err;
  85. }
  86. /**
  87. * data_nodes_cmp - compare 2 data nodes.
  88. * @priv: UBIFS file-system description object
  89. * @a: first data node
  90. * @b: second data node
  91. *
  92. * This function compares data nodes @a and @b. Returns %1 if @a has greater
  93. * inode or block number, and %-1 otherwise.
  94. */
  95. static int data_nodes_cmp(void *priv, const struct list_head *a,
  96. const struct list_head *b)
  97. {
  98. ino_t inuma, inumb;
  99. struct ubifs_info *c = priv;
  100. struct ubifs_scan_node *sa, *sb;
  101. cond_resched();
  102. if (a == b)
  103. return 0;
  104. sa = list_entry(a, struct ubifs_scan_node, list);
  105. sb = list_entry(b, struct ubifs_scan_node, list);
  106. ubifs_assert(c, key_type(c, &sa->key) == UBIFS_DATA_KEY);
  107. ubifs_assert(c, key_type(c, &sb->key) == UBIFS_DATA_KEY);
  108. ubifs_assert(c, sa->type == UBIFS_DATA_NODE);
  109. ubifs_assert(c, sb->type == UBIFS_DATA_NODE);
  110. inuma = key_inum(c, &sa->key);
  111. inumb = key_inum(c, &sb->key);
  112. if (inuma == inumb) {
  113. unsigned int blka = key_block(c, &sa->key);
  114. unsigned int blkb = key_block(c, &sb->key);
  115. if (blka <= blkb)
  116. return -1;
  117. } else if (inuma <= inumb)
  118. return -1;
  119. return 1;
  120. }
  121. /*
  122. * nondata_nodes_cmp - compare 2 non-data nodes.
  123. * @priv: UBIFS file-system description object
  124. * @a: first node
  125. * @a: second node
  126. *
  127. * This function compares nodes @a and @b. It makes sure that inode nodes go
  128. * first and sorted by length in descending order. Directory entry nodes go
  129. * after inode nodes and are sorted in ascending hash valuer order.
  130. */
  131. static int nondata_nodes_cmp(void *priv, const struct list_head *a,
  132. const struct list_head *b)
  133. {
  134. ino_t inuma, inumb;
  135. struct ubifs_info *c = priv;
  136. struct ubifs_scan_node *sa, *sb;
  137. cond_resched();
  138. if (a == b)
  139. return 0;
  140. sa = list_entry(a, struct ubifs_scan_node, list);
  141. sb = list_entry(b, struct ubifs_scan_node, list);
  142. ubifs_assert(c, key_type(c, &sa->key) != UBIFS_DATA_KEY &&
  143. key_type(c, &sb->key) != UBIFS_DATA_KEY);
  144. ubifs_assert(c, sa->type != UBIFS_DATA_NODE &&
  145. sb->type != UBIFS_DATA_NODE);
  146. /* Inodes go before directory entries */
  147. if (sa->type == UBIFS_INO_NODE) {
  148. if (sb->type == UBIFS_INO_NODE)
  149. return sb->len - sa->len;
  150. return -1;
  151. }
  152. if (sb->type == UBIFS_INO_NODE)
  153. return 1;
  154. ubifs_assert(c, key_type(c, &sa->key) == UBIFS_DENT_KEY ||
  155. key_type(c, &sa->key) == UBIFS_XENT_KEY);
  156. ubifs_assert(c, key_type(c, &sb->key) == UBIFS_DENT_KEY ||
  157. key_type(c, &sb->key) == UBIFS_XENT_KEY);
  158. ubifs_assert(c, sa->type == UBIFS_DENT_NODE ||
  159. sa->type == UBIFS_XENT_NODE);
  160. ubifs_assert(c, sb->type == UBIFS_DENT_NODE ||
  161. sb->type == UBIFS_XENT_NODE);
  162. inuma = key_inum(c, &sa->key);
  163. inumb = key_inum(c, &sb->key);
  164. if (inuma == inumb) {
  165. uint32_t hasha = key_hash(c, &sa->key);
  166. uint32_t hashb = key_hash(c, &sb->key);
  167. if (hasha <= hashb)
  168. return -1;
  169. } else if (inuma <= inumb)
  170. return -1;
  171. return 1;
  172. }
  173. /**
  174. * sort_nodes - sort nodes for GC.
  175. * @c: UBIFS file-system description object
  176. * @sleb: describes nodes to sort and contains the result on exit
  177. * @nondata: contains non-data nodes on exit
  178. * @min: minimum node size is returned here
  179. *
  180. * This function sorts the list of inodes to garbage collect. First of all, it
  181. * kills obsolete nodes and separates data and non-data nodes to the
  182. * @sleb->nodes and @nondata lists correspondingly.
  183. *
  184. * Data nodes are then sorted in block number order - this is important for
  185. * bulk-read; data nodes with lower inode number go before data nodes with
  186. * higher inode number, and data nodes with lower block number go before data
  187. * nodes with higher block number;
  188. *
  189. * Non-data nodes are sorted as follows.
  190. * o First go inode nodes - they are sorted in descending length order.
  191. * o Then go directory entry nodes - they are sorted in hash order, which
  192. * should supposedly optimize 'readdir()'. Direntry nodes with lower parent
  193. * inode number go before direntry nodes with higher parent inode number,
  194. * and direntry nodes with lower name hash values go before direntry nodes
  195. * with higher name hash values.
  196. *
  197. * This function returns zero in case of success and a negative error code in
  198. * case of failure.
  199. */
  200. static int sort_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  201. struct list_head *nondata, int *min)
  202. {
  203. int err;
  204. struct ubifs_scan_node *snod, *tmp;
  205. *min = INT_MAX;
  206. /* Separate data nodes and non-data nodes */
  207. list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
  208. ubifs_assert(c, snod->type == UBIFS_INO_NODE ||
  209. snod->type == UBIFS_DATA_NODE ||
  210. snod->type == UBIFS_DENT_NODE ||
  211. snod->type == UBIFS_XENT_NODE ||
  212. snod->type == UBIFS_TRUN_NODE ||
  213. snod->type == UBIFS_AUTH_NODE);
  214. if (snod->type != UBIFS_INO_NODE &&
  215. snod->type != UBIFS_DATA_NODE &&
  216. snod->type != UBIFS_DENT_NODE &&
  217. snod->type != UBIFS_XENT_NODE) {
  218. /* Probably truncation node, zap it */
  219. list_del(&snod->list);
  220. kfree(snod);
  221. continue;
  222. }
  223. ubifs_assert(c, key_type(c, &snod->key) == UBIFS_DATA_KEY ||
  224. key_type(c, &snod->key) == UBIFS_INO_KEY ||
  225. key_type(c, &snod->key) == UBIFS_DENT_KEY ||
  226. key_type(c, &snod->key) == UBIFS_XENT_KEY);
  227. err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
  228. snod->offs, 0);
  229. if (err < 0)
  230. return err;
  231. if (!err) {
  232. /* The node is obsolete, remove it from the list */
  233. list_del(&snod->list);
  234. kfree(snod);
  235. continue;
  236. }
  237. if (snod->len < *min)
  238. *min = snod->len;
  239. if (key_type(c, &snod->key) != UBIFS_DATA_KEY)
  240. list_move_tail(&snod->list, nondata);
  241. }
  242. /* Sort data and non-data nodes */
  243. list_sort(c, &sleb->nodes, &data_nodes_cmp);
  244. list_sort(c, nondata, &nondata_nodes_cmp);
  245. err = dbg_check_data_nodes_order(c, &sleb->nodes);
  246. if (err)
  247. return err;
  248. err = dbg_check_nondata_nodes_order(c, nondata);
  249. if (err)
  250. return err;
  251. return 0;
  252. }
  253. /**
  254. * move_node - move a node.
  255. * @c: UBIFS file-system description object
  256. * @sleb: describes the LEB to move nodes from
  257. * @snod: the mode to move
  258. * @wbuf: write-buffer to move node to
  259. *
  260. * This function moves node @snod to @wbuf, changes TNC correspondingly, and
  261. * destroys @snod. Returns zero in case of success and a negative error code in
  262. * case of failure.
  263. */
  264. static int move_node(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  265. struct ubifs_scan_node *snod, struct ubifs_wbuf *wbuf)
  266. {
  267. int err, new_lnum = wbuf->lnum, new_offs = wbuf->offs + wbuf->used;
  268. cond_resched();
  269. err = ubifs_wbuf_write_nolock(wbuf, snod->node, snod->len);
  270. if (err)
  271. return err;
  272. err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
  273. snod->offs, new_lnum, new_offs,
  274. snod->len);
  275. list_del(&snod->list);
  276. kfree(snod);
  277. return err;
  278. }
  279. /**
  280. * move_nodes - move nodes.
  281. * @c: UBIFS file-system description object
  282. * @sleb: describes the LEB to move nodes from
  283. *
  284. * This function moves valid nodes from data LEB described by @sleb to the GC
  285. * journal head. This function returns zero in case of success, %-EAGAIN if
  286. * commit is required, and other negative error codes in case of other
  287. * failures.
  288. */
  289. static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
  290. {
  291. int err, min;
  292. LIST_HEAD(nondata);
  293. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  294. if (wbuf->lnum == -1) {
  295. /*
  296. * The GC journal head is not set, because it is the first GC
  297. * invocation since mount.
  298. */
  299. err = switch_gc_head(c);
  300. if (err)
  301. return err;
  302. }
  303. err = sort_nodes(c, sleb, &nondata, &min);
  304. if (err)
  305. goto out;
  306. /* Write nodes to their new location. Use the first-fit strategy */
  307. while (1) {
  308. int avail, moved = 0;
  309. struct ubifs_scan_node *snod, *tmp;
  310. /* Move data nodes */
  311. list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
  312. avail = c->leb_size - wbuf->offs - wbuf->used -
  313. ubifs_auth_node_sz(c);
  314. if (snod->len > avail)
  315. /*
  316. * Do not skip data nodes in order to optimize
  317. * bulk-read.
  318. */
  319. break;
  320. err = ubifs_shash_update(c, c->jheads[GCHD].log_hash,
  321. snod->node, snod->len);
  322. if (err)
  323. goto out;
  324. err = move_node(c, sleb, snod, wbuf);
  325. if (err)
  326. goto out;
  327. moved = 1;
  328. }
  329. /* Move non-data nodes */
  330. list_for_each_entry_safe(snod, tmp, &nondata, list) {
  331. avail = c->leb_size - wbuf->offs - wbuf->used -
  332. ubifs_auth_node_sz(c);
  333. if (avail < min)
  334. break;
  335. if (snod->len > avail) {
  336. /*
  337. * Keep going only if this is an inode with
  338. * some data. Otherwise stop and switch the GC
  339. * head. IOW, we assume that data-less inode
  340. * nodes and direntry nodes are roughly of the
  341. * same size.
  342. */
  343. if (key_type(c, &snod->key) == UBIFS_DENT_KEY ||
  344. snod->len == UBIFS_INO_NODE_SZ)
  345. break;
  346. continue;
  347. }
  348. err = ubifs_shash_update(c, c->jheads[GCHD].log_hash,
  349. snod->node, snod->len);
  350. if (err)
  351. goto out;
  352. err = move_node(c, sleb, snod, wbuf);
  353. if (err)
  354. goto out;
  355. moved = 1;
  356. }
  357. if (ubifs_authenticated(c) && moved) {
  358. struct ubifs_auth_node *auth;
  359. auth = kmalloc(ubifs_auth_node_sz(c), GFP_NOFS);
  360. if (!auth) {
  361. err = -ENOMEM;
  362. goto out;
  363. }
  364. err = ubifs_prepare_auth_node(c, auth,
  365. c->jheads[GCHD].log_hash);
  366. if (err) {
  367. kfree(auth);
  368. goto out;
  369. }
  370. err = ubifs_wbuf_write_nolock(wbuf, auth,
  371. ubifs_auth_node_sz(c));
  372. if (err) {
  373. kfree(auth);
  374. goto out;
  375. }
  376. ubifs_add_dirt(c, wbuf->lnum, ubifs_auth_node_sz(c));
  377. }
  378. if (list_empty(&sleb->nodes) && list_empty(&nondata))
  379. break;
  380. /*
  381. * Waste the rest of the space in the LEB and switch to the
  382. * next LEB.
  383. */
  384. err = switch_gc_head(c);
  385. if (err)
  386. goto out;
  387. }
  388. return 0;
  389. out:
  390. list_splice_tail(&nondata, &sleb->nodes);
  391. return err;
  392. }
  393. /**
  394. * gc_sync_wbufs - sync write-buffers for GC.
  395. * @c: UBIFS file-system description object
  396. *
  397. * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
  398. * be in a write-buffer instead. That is, a node could be written to a
  399. * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
  400. * erased before the write-buffer is sync'd and then there is an unclean
  401. * unmount, then an existing node is lost. To avoid this, we sync all
  402. * write-buffers.
  403. *
  404. * This function returns %0 on success or a negative error code on failure.
  405. */
  406. static int gc_sync_wbufs(struct ubifs_info *c)
  407. {
  408. int err, i;
  409. for (i = 0; i < c->jhead_cnt; i++) {
  410. if (i == GCHD)
  411. continue;
  412. err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  413. if (err)
  414. return err;
  415. }
  416. return 0;
  417. }
  418. /**
  419. * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
  420. * @c: UBIFS file-system description object
  421. * @lp: describes the LEB to garbage collect
  422. *
  423. * This function garbage-collects an LEB and returns one of the @LEB_FREED,
  424. * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
  425. * required, and other negative error codes in case of failures.
  426. */
  427. int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
  428. {
  429. struct ubifs_scan_leb *sleb;
  430. struct ubifs_scan_node *snod;
  431. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  432. int err = 0, lnum = lp->lnum;
  433. ubifs_assert(c, c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
  434. c->need_recovery);
  435. ubifs_assert(c, c->gc_lnum != lnum);
  436. ubifs_assert(c, wbuf->lnum != lnum);
  437. if (lp->free + lp->dirty == c->leb_size) {
  438. /* Special case - a free LEB */
  439. dbg_gc("LEB %d is free, return it", lp->lnum);
  440. ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
  441. if (lp->free != c->leb_size) {
  442. /*
  443. * Write buffers must be sync'd before unmapping
  444. * freeable LEBs, because one of them may contain data
  445. * which obsoletes something in 'lp->lnum'.
  446. */
  447. err = gc_sync_wbufs(c);
  448. if (err)
  449. return err;
  450. err = ubifs_change_one_lp(c, lp->lnum, c->leb_size,
  451. 0, 0, 0, 0);
  452. if (err)
  453. return err;
  454. }
  455. err = ubifs_leb_unmap(c, lp->lnum);
  456. if (err)
  457. return err;
  458. if (c->gc_lnum == -1) {
  459. c->gc_lnum = lnum;
  460. return LEB_RETAINED;
  461. }
  462. return LEB_FREED;
  463. }
  464. /*
  465. * We scan the entire LEB even though we only really need to scan up to
  466. * (c->leb_size - lp->free).
  467. */
  468. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
  469. if (IS_ERR(sleb))
  470. return PTR_ERR(sleb);
  471. ubifs_assert(c, !list_empty(&sleb->nodes));
  472. snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
  473. if (snod->type == UBIFS_IDX_NODE) {
  474. struct ubifs_gced_idx_leb *idx_gc;
  475. dbg_gc("indexing LEB %d (free %d, dirty %d)",
  476. lnum, lp->free, lp->dirty);
  477. list_for_each_entry(snod, &sleb->nodes, list) {
  478. struct ubifs_idx_node *idx = snod->node;
  479. int level = le16_to_cpu(idx->level);
  480. ubifs_assert(c, snod->type == UBIFS_IDX_NODE);
  481. key_read(c, ubifs_idx_key(c, idx), &snod->key);
  482. err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
  483. snod->offs);
  484. if (err)
  485. goto out;
  486. }
  487. idx_gc = kmalloc_obj(struct ubifs_gced_idx_leb, GFP_NOFS);
  488. if (!idx_gc) {
  489. err = -ENOMEM;
  490. goto out;
  491. }
  492. idx_gc->lnum = lnum;
  493. idx_gc->unmap = 0;
  494. list_add(&idx_gc->list, &c->idx_gc);
  495. /*
  496. * Don't release the LEB until after the next commit, because
  497. * it may contain data which is needed for recovery. So
  498. * although we freed this LEB, it will become usable only after
  499. * the commit.
  500. */
  501. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
  502. LPROPS_INDEX, 1);
  503. if (err)
  504. goto out;
  505. err = LEB_FREED_IDX;
  506. } else {
  507. dbg_gc("data LEB %d (free %d, dirty %d)",
  508. lnum, lp->free, lp->dirty);
  509. err = move_nodes(c, sleb);
  510. if (err)
  511. goto out_inc_seq;
  512. err = gc_sync_wbufs(c);
  513. if (err)
  514. goto out_inc_seq;
  515. err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
  516. if (err)
  517. goto out_inc_seq;
  518. /* Allow for races with TNC */
  519. c->gced_lnum = lnum;
  520. smp_wmb();
  521. c->gc_seq += 1;
  522. smp_wmb();
  523. if (c->gc_lnum == -1) {
  524. c->gc_lnum = lnum;
  525. err = LEB_RETAINED;
  526. } else {
  527. err = ubifs_wbuf_sync_nolock(wbuf);
  528. if (err)
  529. goto out;
  530. err = ubifs_leb_unmap(c, lnum);
  531. if (err)
  532. goto out;
  533. err = LEB_FREED;
  534. }
  535. }
  536. out:
  537. ubifs_scan_destroy(sleb);
  538. return err;
  539. out_inc_seq:
  540. /* We may have moved at least some nodes so allow for races with TNC */
  541. c->gced_lnum = lnum;
  542. smp_wmb();
  543. c->gc_seq += 1;
  544. smp_wmb();
  545. goto out;
  546. }
  547. /**
  548. * ubifs_garbage_collect - UBIFS garbage collector.
  549. * @c: UBIFS file-system description object
  550. * @anyway: do GC even if there are free LEBs
  551. *
  552. * This function does out-of-place garbage collection. The return codes are:
  553. * o positive LEB number if the LEB has been freed and may be used;
  554. * o %-EAGAIN if the caller has to run commit;
  555. * o %-ENOSPC if GC failed to make any progress;
  556. * o other negative error codes in case of other errors.
  557. *
  558. * Garbage collector writes data to the journal when GC'ing data LEBs, and just
  559. * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
  560. * commit may be required. But commit cannot be run from inside GC, because the
  561. * caller might be holding the commit lock, so %-EAGAIN is returned instead;
  562. * And this error code means that the caller has to run commit, and re-run GC
  563. * if there is still no free space.
  564. *
  565. * There are many reasons why this function may return %-EAGAIN:
  566. * o the log is full and there is no space to write an LEB reference for
  567. * @c->gc_lnum;
  568. * o the journal is too large and exceeds size limitations;
  569. * o GC moved indexing LEBs, but they can be used only after the commit;
  570. * o the shrinker fails to find clean znodes to free and requests the commit;
  571. * o etc.
  572. *
  573. * Note, if the file-system is close to be full, this function may return
  574. * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
  575. * the function. E.g., this happens if the limits on the journal size are too
  576. * tough and GC writes too much to the journal before an LEB is freed. This
  577. * might also mean that the journal is too large, and the TNC becomes to big,
  578. * so that the shrinker is constantly called, finds not clean znodes to free,
  579. * and requests commit. Well, this may also happen if the journal is all right,
  580. * but another kernel process consumes too much memory. Anyway, infinite
  581. * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
  582. */
  583. int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
  584. {
  585. int i, err, ret, min_space = c->dead_wm;
  586. struct ubifs_lprops lp;
  587. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  588. ubifs_assert_cmt_locked(c);
  589. ubifs_assert(c, !c->ro_media && !c->ro_mount);
  590. if (ubifs_gc_should_commit(c))
  591. return -EAGAIN;
  592. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  593. if (c->ro_error) {
  594. ret = -EROFS;
  595. goto out_unlock;
  596. }
  597. /* We expect the write-buffer to be empty on entry */
  598. ubifs_assert(c, !wbuf->used);
  599. for (i = 0; ; i++) {
  600. int space_before, space_after;
  601. /* Maybe continue after find and break before find */
  602. lp.lnum = -1;
  603. cond_resched();
  604. /* Give the commit an opportunity to run */
  605. if (ubifs_gc_should_commit(c)) {
  606. ret = -EAGAIN;
  607. break;
  608. }
  609. if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
  610. /*
  611. * We've done enough iterations. Indexing LEBs were
  612. * moved and will be available after the commit.
  613. */
  614. dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
  615. ubifs_commit_required(c);
  616. ret = -EAGAIN;
  617. break;
  618. }
  619. if (i > HARD_LEBS_LIMIT) {
  620. /*
  621. * We've moved too many LEBs and have not made
  622. * progress, give up.
  623. */
  624. dbg_gc("hard limit, -ENOSPC");
  625. ret = -ENOSPC;
  626. break;
  627. }
  628. /*
  629. * Empty and freeable LEBs can turn up while we waited for
  630. * the wbuf lock, or while we have been running GC. In that
  631. * case, we should just return one of those instead of
  632. * continuing to GC dirty LEBs. Hence we request
  633. * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
  634. */
  635. ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
  636. if (ret) {
  637. if (ret == -ENOSPC)
  638. dbg_gc("no more dirty LEBs");
  639. break;
  640. }
  641. dbg_gc("found LEB %d: free %d, dirty %d, sum %d (min. space %d)",
  642. lp.lnum, lp.free, lp.dirty, lp.free + lp.dirty,
  643. min_space);
  644. space_before = c->leb_size - wbuf->offs - wbuf->used;
  645. if (wbuf->lnum == -1)
  646. space_before = 0;
  647. ret = ubifs_garbage_collect_leb(c, &lp);
  648. if (ret < 0) {
  649. if (ret == -EAGAIN) {
  650. /*
  651. * This is not error, so we have to return the
  652. * LEB to lprops. But if 'ubifs_return_leb()'
  653. * fails, its failure code is propagated to the
  654. * caller instead of the original '-EAGAIN'.
  655. */
  656. err = ubifs_return_leb(c, lp.lnum);
  657. if (err) {
  658. ret = err;
  659. /*
  660. * An LEB may always be "taken",
  661. * so setting ubifs to read-only,
  662. * and then executing sync wbuf will
  663. * return -EROFS and enter the "out"
  664. * error branch.
  665. */
  666. ubifs_ro_mode(c, ret);
  667. }
  668. /* Maybe double return LEB if goto out */
  669. lp.lnum = -1;
  670. break;
  671. }
  672. goto out;
  673. }
  674. if (ret == LEB_FREED) {
  675. /* An LEB has been freed and is ready for use */
  676. dbg_gc("LEB %d freed, return", lp.lnum);
  677. ret = lp.lnum;
  678. break;
  679. }
  680. if (ret == LEB_FREED_IDX) {
  681. /*
  682. * This was an indexing LEB and it cannot be
  683. * immediately used. And instead of requesting the
  684. * commit straight away, we try to garbage collect some
  685. * more.
  686. */
  687. dbg_gc("indexing LEB %d freed, continue", lp.lnum);
  688. continue;
  689. }
  690. ubifs_assert(c, ret == LEB_RETAINED);
  691. space_after = c->leb_size - wbuf->offs - wbuf->used;
  692. dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
  693. space_after - space_before);
  694. if (space_after > space_before) {
  695. /* GC makes progress, keep working */
  696. min_space >>= 1;
  697. if (min_space < c->dead_wm)
  698. min_space = c->dead_wm;
  699. continue;
  700. }
  701. dbg_gc("did not make progress");
  702. /*
  703. * GC moved an LEB bud have not done any progress. This means
  704. * that the previous GC head LEB contained too few free space
  705. * and the LEB which was GC'ed contained only large nodes which
  706. * did not fit that space.
  707. *
  708. * We can do 2 things:
  709. * 1. pick another LEB in a hope it'll contain a small node
  710. * which will fit the space we have at the end of current GC
  711. * head LEB, but there is no guarantee, so we try this out
  712. * unless we have already been working for too long;
  713. * 2. request an LEB with more dirty space, which will force
  714. * 'ubifs_find_dirty_leb()' to start scanning the lprops
  715. * table, instead of just picking one from the heap
  716. * (previously it already picked the dirtiest LEB).
  717. */
  718. if (i < SOFT_LEBS_LIMIT) {
  719. dbg_gc("try again");
  720. continue;
  721. }
  722. min_space <<= 1;
  723. if (min_space > c->dark_wm)
  724. min_space = c->dark_wm;
  725. dbg_gc("set min. space to %d", min_space);
  726. }
  727. if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
  728. dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
  729. ubifs_commit_required(c);
  730. ret = -EAGAIN;
  731. }
  732. err = ubifs_wbuf_sync_nolock(wbuf);
  733. if (!err)
  734. err = ubifs_leb_unmap(c, c->gc_lnum);
  735. if (err) {
  736. ret = err;
  737. goto out;
  738. }
  739. out_unlock:
  740. mutex_unlock(&wbuf->io_mutex);
  741. return ret;
  742. out:
  743. ubifs_assert(c, ret < 0);
  744. ubifs_assert(c, ret != -ENOSPC && ret != -EAGAIN);
  745. ubifs_wbuf_sync_nolock(wbuf);
  746. ubifs_ro_mode(c, ret);
  747. mutex_unlock(&wbuf->io_mutex);
  748. if (lp.lnum != -1)
  749. ubifs_return_leb(c, lp.lnum);
  750. return ret;
  751. }
  752. /**
  753. * ubifs_gc_start_commit - garbage collection at start of commit.
  754. * @c: UBIFS file-system description object
  755. *
  756. * If a LEB has only dirty and free space, then we may safely unmap it and make
  757. * it free. Note, we cannot do this with indexing LEBs because dirty space may
  758. * correspond index nodes that are required for recovery. In that case, the
  759. * LEB cannot be unmapped until after the next commit.
  760. *
  761. * This function returns %0 upon success and a negative error code upon failure.
  762. */
  763. int ubifs_gc_start_commit(struct ubifs_info *c)
  764. {
  765. struct ubifs_gced_idx_leb *idx_gc;
  766. const struct ubifs_lprops *lp;
  767. int err = 0, flags;
  768. ubifs_get_lprops(c);
  769. /*
  770. * Unmap (non-index) freeable LEBs. Note that recovery requires that all
  771. * wbufs are sync'd before this, which is done in 'do_commit()'.
  772. */
  773. while (1) {
  774. lp = ubifs_fast_find_freeable(c);
  775. if (!lp)
  776. break;
  777. ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
  778. ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
  779. err = ubifs_leb_unmap(c, lp->lnum);
  780. if (err)
  781. goto out;
  782. lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
  783. if (IS_ERR(lp)) {
  784. err = PTR_ERR(lp);
  785. goto out;
  786. }
  787. ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
  788. ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
  789. }
  790. /* Mark GC'd index LEBs OK to unmap after this commit finishes */
  791. list_for_each_entry(idx_gc, &c->idx_gc, list)
  792. idx_gc->unmap = 1;
  793. /* Record index freeable LEBs for unmapping after commit */
  794. while (1) {
  795. lp = ubifs_fast_find_frdi_idx(c);
  796. if (IS_ERR(lp)) {
  797. err = PTR_ERR(lp);
  798. goto out;
  799. }
  800. if (!lp)
  801. break;
  802. idx_gc = kmalloc_obj(struct ubifs_gced_idx_leb, GFP_NOFS);
  803. if (!idx_gc) {
  804. err = -ENOMEM;
  805. goto out;
  806. }
  807. ubifs_assert(c, !(lp->flags & LPROPS_TAKEN));
  808. ubifs_assert(c, lp->flags & LPROPS_INDEX);
  809. /* Don't release the LEB until after the next commit */
  810. flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
  811. lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
  812. if (IS_ERR(lp)) {
  813. err = PTR_ERR(lp);
  814. kfree(idx_gc);
  815. goto out;
  816. }
  817. ubifs_assert(c, lp->flags & LPROPS_TAKEN);
  818. ubifs_assert(c, !(lp->flags & LPROPS_INDEX));
  819. idx_gc->lnum = lp->lnum;
  820. idx_gc->unmap = 1;
  821. list_add(&idx_gc->list, &c->idx_gc);
  822. }
  823. out:
  824. ubifs_release_lprops(c);
  825. return err;
  826. }
  827. /**
  828. * ubifs_gc_end_commit - garbage collection at end of commit.
  829. * @c: UBIFS file-system description object
  830. *
  831. * This function completes out-of-place garbage collection of index LEBs.
  832. */
  833. int ubifs_gc_end_commit(struct ubifs_info *c)
  834. {
  835. struct ubifs_gced_idx_leb *idx_gc, *tmp;
  836. struct ubifs_wbuf *wbuf;
  837. int err = 0;
  838. wbuf = &c->jheads[GCHD].wbuf;
  839. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  840. list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
  841. if (idx_gc->unmap) {
  842. dbg_gc("LEB %d", idx_gc->lnum);
  843. err = ubifs_leb_unmap(c, idx_gc->lnum);
  844. if (err)
  845. goto out;
  846. err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
  847. LPROPS_NC, 0, LPROPS_TAKEN, -1);
  848. if (err)
  849. goto out;
  850. list_del(&idx_gc->list);
  851. kfree(idx_gc);
  852. }
  853. out:
  854. mutex_unlock(&wbuf->io_mutex);
  855. return err;
  856. }
  857. /**
  858. * ubifs_destroy_idx_gc - destroy idx_gc list.
  859. * @c: UBIFS file-system description object
  860. *
  861. * This function destroys the @c->idx_gc list. It is called when unmounting
  862. * so locks are not needed. Returns zero in case of success and a negative
  863. * error code in case of failure.
  864. */
  865. void ubifs_destroy_idx_gc(struct ubifs_info *c)
  866. {
  867. while (!list_empty(&c->idx_gc)) {
  868. struct ubifs_gced_idx_leb *idx_gc;
  869. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
  870. list);
  871. c->idx_gc_cnt -= 1;
  872. list_del(&idx_gc->list);
  873. kfree(idx_gc);
  874. }
  875. }
  876. /**
  877. * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
  878. * @c: UBIFS file-system description object
  879. *
  880. * Called during start commit so locks are not needed.
  881. */
  882. int ubifs_get_idx_gc_leb(struct ubifs_info *c)
  883. {
  884. struct ubifs_gced_idx_leb *idx_gc;
  885. int lnum;
  886. if (list_empty(&c->idx_gc))
  887. return -ENOSPC;
  888. idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
  889. lnum = idx_gc->lnum;
  890. /* c->idx_gc_cnt is updated by the caller when lprops are updated */
  891. list_del(&idx_gc->list);
  892. kfree(idx_gc);
  893. return lnum;
  894. }