gc.c 28 KB

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