gc.c 27 KB

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