gc.c 28 KB

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