gc.c 27 KB

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