gc.c 27 KB

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