gc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * fs/logfs/gc.c - garbage collection code
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. */
  8. #include "logfs.h"
  9. #include <linux/sched.h>
  10. /*
  11. * Wear leveling needs to kick in when the difference between low erase
  12. * counts and high erase counts gets too big. A good value for "too big"
  13. * may be somewhat below 10% of maximum erase count for the device.
  14. * Why not 397, to pick a nice round number with no specific meaning? :)
  15. *
  16. * WL_RATELIMIT is the minimum time between two wear level events. A huge
  17. * number of segments may fulfil the requirements for wear leveling at the
  18. * same time. If that happens we don't want to cause a latency from hell,
  19. * but just gently pick one segment every so often and minimize overhead.
  20. */
  21. #define WL_DELTA 397
  22. #define WL_RATELIMIT 100
  23. #define MAX_OBJ_ALIASES 2600
  24. #define SCAN_RATIO 512 /* number of scanned segments per gc'd segment */
  25. #define LIST_SIZE 64 /* base size of candidate lists */
  26. #define SCAN_ROUNDS 128 /* maximum number of complete medium scans */
  27. #define SCAN_ROUNDS_HIGH 4 /* maximum number of higher-level scans */
  28. static int no_free_segments(struct super_block *sb)
  29. {
  30. struct logfs_super *super = logfs_super(sb);
  31. return super->s_free_list.count;
  32. }
  33. /* journal has distance -1, top-most ifile layer distance 0 */
  34. static u8 root_distance(struct super_block *sb, gc_level_t __gc_level)
  35. {
  36. struct logfs_super *super = logfs_super(sb);
  37. u8 gc_level = (__force u8)__gc_level;
  38. switch (gc_level) {
  39. case 0: /* fall through */
  40. case 1: /* fall through */
  41. case 2: /* fall through */
  42. case 3:
  43. /* file data or indirect blocks */
  44. return super->s_ifile_levels + super->s_iblock_levels - gc_level;
  45. case 6: /* fall through */
  46. case 7: /* fall through */
  47. case 8: /* fall through */
  48. case 9:
  49. /* inode file data or indirect blocks */
  50. return super->s_ifile_levels - (gc_level - 6);
  51. default:
  52. printk(KERN_ERR"LOGFS: segment of unknown level %x found\n",
  53. gc_level);
  54. WARN_ON(1);
  55. return super->s_ifile_levels + super->s_iblock_levels;
  56. }
  57. }
  58. static int segment_is_reserved(struct super_block *sb, u32 segno)
  59. {
  60. struct logfs_super *super = logfs_super(sb);
  61. struct logfs_area *area;
  62. void *reserved;
  63. int i;
  64. /* Some segments are reserved. Just pretend they were all valid */
  65. reserved = btree_lookup32(&super->s_reserved_segments, segno);
  66. if (reserved)
  67. return 1;
  68. /* Currently open segments */
  69. for_each_area(i) {
  70. area = super->s_area[i];
  71. if (area->a_is_open && area->a_segno == segno)
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static void logfs_mark_segment_bad(struct super_block *sb, u32 segno)
  77. {
  78. BUG();
  79. }
  80. /*
  81. * Returns the bytes consumed by valid objects in this segment. Object headers
  82. * are counted, the segment header is not.
  83. */
  84. static u32 logfs_valid_bytes(struct super_block *sb, u32 segno, u32 *ec,
  85. gc_level_t *gc_level)
  86. {
  87. struct logfs_segment_entry se;
  88. u32 ec_level;
  89. logfs_get_segment_entry(sb, segno, &se);
  90. if (se.ec_level == cpu_to_be32(BADSEG) ||
  91. se.valid == cpu_to_be32(RESERVED))
  92. return RESERVED;
  93. ec_level = be32_to_cpu(se.ec_level);
  94. *ec = ec_level >> 4;
  95. *gc_level = GC_LEVEL(ec_level & 0xf);
  96. return be32_to_cpu(se.valid);
  97. }
  98. static void logfs_cleanse_block(struct super_block *sb, u64 ofs, u64 ino,
  99. u64 bix, gc_level_t gc_level)
  100. {
  101. struct inode *inode;
  102. int err, cookie;
  103. inode = logfs_safe_iget(sb, ino, &cookie);
  104. err = logfs_rewrite_block(inode, bix, ofs, gc_level, 0);
  105. BUG_ON(err);
  106. logfs_safe_iput(inode, cookie);
  107. }
  108. static u32 logfs_gc_segment(struct super_block *sb, u32 segno, u8 dist)
  109. {
  110. struct logfs_super *super = logfs_super(sb);
  111. struct logfs_segment_header sh;
  112. struct logfs_object_header oh;
  113. u64 ofs, ino, bix;
  114. u32 seg_ofs, logical_segno, cleaned = 0;
  115. int err, len, valid;
  116. gc_level_t gc_level;
  117. LOGFS_BUG_ON(segment_is_reserved(sb, segno), sb);
  118. btree_insert32(&super->s_reserved_segments, segno, (void *)1, GFP_NOFS);
  119. err = wbuf_read(sb, dev_ofs(sb, segno, 0), sizeof(sh), &sh);
  120. BUG_ON(err);
  121. gc_level = GC_LEVEL(sh.level);
  122. logical_segno = be32_to_cpu(sh.segno);
  123. if (sh.crc != logfs_crc32(&sh, sizeof(sh), 4)) {
  124. logfs_mark_segment_bad(sb, segno);
  125. cleaned = -1;
  126. goto out;
  127. }
  128. for (seg_ofs = LOGFS_SEGMENT_HEADERSIZE;
  129. seg_ofs + sizeof(oh) < super->s_segsize; ) {
  130. ofs = dev_ofs(sb, logical_segno, seg_ofs);
  131. err = wbuf_read(sb, dev_ofs(sb, segno, seg_ofs), sizeof(oh),
  132. &oh);
  133. BUG_ON(err);
  134. if (!memchr_inv(&oh, 0xff, sizeof(oh)))
  135. break;
  136. if (oh.crc != logfs_crc32(&oh, sizeof(oh) - 4, 4)) {
  137. logfs_mark_segment_bad(sb, segno);
  138. cleaned = super->s_segsize - 1;
  139. goto out;
  140. }
  141. ino = be64_to_cpu(oh.ino);
  142. bix = be64_to_cpu(oh.bix);
  143. len = sizeof(oh) + be16_to_cpu(oh.len);
  144. valid = logfs_is_valid_block(sb, ofs, ino, bix, gc_level);
  145. if (valid == 1) {
  146. logfs_cleanse_block(sb, ofs, ino, bix, gc_level);
  147. cleaned += len;
  148. } else if (valid == 2) {
  149. /* Will be invalid upon journal commit */
  150. cleaned += len;
  151. }
  152. seg_ofs += len;
  153. }
  154. out:
  155. btree_remove32(&super->s_reserved_segments, segno);
  156. return cleaned;
  157. }
  158. static struct gc_candidate *add_list(struct gc_candidate *cand,
  159. struct candidate_list *list)
  160. {
  161. struct rb_node **p = &list->rb_tree.rb_node;
  162. struct rb_node *parent = NULL;
  163. struct gc_candidate *cur;
  164. int comp;
  165. cand->list = list;
  166. while (*p) {
  167. parent = *p;
  168. cur = rb_entry(parent, struct gc_candidate, rb_node);
  169. if (list->sort_by_ec)
  170. comp = cand->erase_count < cur->erase_count;
  171. else
  172. comp = cand->valid < cur->valid;
  173. if (comp)
  174. p = &parent->rb_left;
  175. else
  176. p = &parent->rb_right;
  177. }
  178. rb_link_node(&cand->rb_node, parent, p);
  179. rb_insert_color(&cand->rb_node, &list->rb_tree);
  180. if (list->count <= list->maxcount) {
  181. list->count++;
  182. return NULL;
  183. }
  184. cand = rb_entry(rb_last(&list->rb_tree), struct gc_candidate, rb_node);
  185. rb_erase(&cand->rb_node, &list->rb_tree);
  186. cand->list = NULL;
  187. return cand;
  188. }
  189. static void remove_from_list(struct gc_candidate *cand)
  190. {
  191. struct candidate_list *list = cand->list;
  192. rb_erase(&cand->rb_node, &list->rb_tree);
  193. list->count--;
  194. }
  195. static void free_candidate(struct super_block *sb, struct gc_candidate *cand)
  196. {
  197. struct logfs_super *super = logfs_super(sb);
  198. btree_remove32(&super->s_cand_tree, cand->segno);
  199. kfree(cand);
  200. }
  201. u32 get_best_cand(struct super_block *sb, struct candidate_list *list, u32 *ec)
  202. {
  203. struct gc_candidate *cand;
  204. u32 segno;
  205. BUG_ON(list->count == 0);
  206. cand = rb_entry(rb_first(&list->rb_tree), struct gc_candidate, rb_node);
  207. remove_from_list(cand);
  208. segno = cand->segno;
  209. if (ec)
  210. *ec = cand->erase_count;
  211. free_candidate(sb, cand);
  212. return segno;
  213. }
  214. /*
  215. * We have several lists to manage segments with. The reserve_list is used to
  216. * deal with bad blocks. We try to keep the best (lowest ec) segments on this
  217. * list.
  218. * The free_list contains free segments for normal usage. It usually gets the
  219. * second pick after the reserve_list. But when the free_list is running short
  220. * it is more important to keep the free_list full than to keep a reserve.
  221. *
  222. * Segments that are not free are put onto a per-level low_list. If we have
  223. * to run garbage collection, we pick a candidate from there. All segments on
  224. * those lists should have at least some free space so GC will make progress.
  225. *
  226. * And last we have the ec_list, which is used to pick segments for wear
  227. * leveling.
  228. *
  229. * If all appropriate lists are full, we simply free the candidate and forget
  230. * about that segment for a while. We have better candidates for each purpose.
  231. */
  232. static void __add_candidate(struct super_block *sb, struct gc_candidate *cand)
  233. {
  234. struct logfs_super *super = logfs_super(sb);
  235. u32 full = super->s_segsize - LOGFS_SEGMENT_RESERVE;
  236. if (cand->valid == 0) {
  237. /* 100% free segments */
  238. log_gc_noisy("add reserve segment %x (ec %x) at %llx\n",
  239. cand->segno, cand->erase_count,
  240. dev_ofs(sb, cand->segno, 0));
  241. cand = add_list(cand, &super->s_reserve_list);
  242. if (cand) {
  243. log_gc_noisy("add free segment %x (ec %x) at %llx\n",
  244. cand->segno, cand->erase_count,
  245. dev_ofs(sb, cand->segno, 0));
  246. cand = add_list(cand, &super->s_free_list);
  247. }
  248. } else {
  249. /* good candidates for Garbage Collection */
  250. if (cand->valid < full)
  251. cand = add_list(cand, &super->s_low_list[cand->dist]);
  252. /* good candidates for wear leveling,
  253. * segments that were recently written get ignored */
  254. if (cand)
  255. cand = add_list(cand, &super->s_ec_list);
  256. }
  257. if (cand)
  258. free_candidate(sb, cand);
  259. }
  260. static int add_candidate(struct super_block *sb, u32 segno, u32 valid, u32 ec,
  261. u8 dist)
  262. {
  263. struct logfs_super *super = logfs_super(sb);
  264. struct gc_candidate *cand;
  265. cand = kmalloc(sizeof(*cand), GFP_NOFS);
  266. if (!cand)
  267. return -ENOMEM;
  268. cand->segno = segno;
  269. cand->valid = valid;
  270. cand->erase_count = ec;
  271. cand->dist = dist;
  272. btree_insert32(&super->s_cand_tree, segno, cand, GFP_NOFS);
  273. __add_candidate(sb, cand);
  274. return 0;
  275. }
  276. static void remove_segment_from_lists(struct super_block *sb, u32 segno)
  277. {
  278. struct logfs_super *super = logfs_super(sb);
  279. struct gc_candidate *cand;
  280. cand = btree_lookup32(&super->s_cand_tree, segno);
  281. if (cand) {
  282. remove_from_list(cand);
  283. free_candidate(sb, cand);
  284. }
  285. }
  286. static void scan_segment(struct super_block *sb, u32 segno)
  287. {
  288. u32 valid, ec = 0;
  289. gc_level_t gc_level = 0;
  290. u8 dist;
  291. if (segment_is_reserved(sb, segno))
  292. return;
  293. remove_segment_from_lists(sb, segno);
  294. valid = logfs_valid_bytes(sb, segno, &ec, &gc_level);
  295. if (valid == RESERVED)
  296. return;
  297. dist = root_distance(sb, gc_level);
  298. add_candidate(sb, segno, valid, ec, dist);
  299. }
  300. static struct gc_candidate *first_in_list(struct candidate_list *list)
  301. {
  302. if (list->count == 0)
  303. return NULL;
  304. return rb_entry(rb_first(&list->rb_tree), struct gc_candidate, rb_node);
  305. }
  306. /*
  307. * Find the best segment for garbage collection. Main criterion is
  308. * the segment requiring the least effort to clean. Secondary
  309. * criterion is to GC on the lowest level available.
  310. *
  311. * So we search the least effort segment on the lowest level first,
  312. * then move up and pick another segment iff is requires significantly
  313. * less effort. Hence the LOGFS_MAX_OBJECTSIZE in the comparison.
  314. */
  315. static struct gc_candidate *get_candidate(struct super_block *sb)
  316. {
  317. struct logfs_super *super = logfs_super(sb);
  318. int i, max_dist;
  319. struct gc_candidate *cand = NULL, *this;
  320. max_dist = min(no_free_segments(sb), LOGFS_NO_AREAS);
  321. for (i = max_dist; i >= 0; i--) {
  322. this = first_in_list(&super->s_low_list[i]);
  323. if (!this)
  324. continue;
  325. if (!cand)
  326. cand = this;
  327. if (this->valid + LOGFS_MAX_OBJECTSIZE <= cand->valid)
  328. cand = this;
  329. }
  330. return cand;
  331. }
  332. static int __logfs_gc_once(struct super_block *sb, struct gc_candidate *cand)
  333. {
  334. struct logfs_super *super = logfs_super(sb);
  335. gc_level_t gc_level;
  336. u32 cleaned, valid, segno, ec;
  337. u8 dist;
  338. if (!cand) {
  339. log_gc("GC attempted, but no candidate found\n");
  340. return 0;
  341. }
  342. segno = cand->segno;
  343. dist = cand->dist;
  344. valid = logfs_valid_bytes(sb, segno, &ec, &gc_level);
  345. free_candidate(sb, cand);
  346. log_gc("GC segment #%02x at %llx, %x required, %x free, %x valid, %llx free\n",
  347. segno, (u64)segno << super->s_segshift,
  348. dist, no_free_segments(sb), valid,
  349. super->s_free_bytes);
  350. cleaned = logfs_gc_segment(sb, segno, dist);
  351. log_gc("GC segment #%02x complete - now %x valid\n", segno,
  352. valid - cleaned);
  353. BUG_ON(cleaned != valid);
  354. return 1;
  355. }
  356. static int logfs_gc_once(struct super_block *sb)
  357. {
  358. struct gc_candidate *cand;
  359. cand = get_candidate(sb);
  360. if (cand)
  361. remove_from_list(cand);
  362. return __logfs_gc_once(sb, cand);
  363. }
  364. /* returns 1 if a wrap occurs, 0 otherwise */
  365. static int logfs_scan_some(struct super_block *sb)
  366. {
  367. struct logfs_super *super = logfs_super(sb);
  368. u32 segno;
  369. int i, ret = 0;
  370. segno = super->s_sweeper;
  371. for (i = SCAN_RATIO; i > 0; i--) {
  372. segno++;
  373. if (segno >= super->s_no_segs) {
  374. segno = 0;
  375. ret = 1;
  376. /* Break out of the loop. We want to read a single
  377. * block from the segment size on next invocation if
  378. * SCAN_RATIO is set to match block size
  379. */
  380. break;
  381. }
  382. scan_segment(sb, segno);
  383. }
  384. super->s_sweeper = segno;
  385. return ret;
  386. }
  387. /*
  388. * In principle, this function should loop forever, looking for GC candidates
  389. * and moving data. LogFS is designed in such a way that this loop is
  390. * guaranteed to terminate.
  391. *
  392. * Limiting the loop to some iterations serves purely to catch cases when
  393. * these guarantees have failed. An actual endless loop is an obvious bug
  394. * and should be reported as such.
  395. */
  396. static void __logfs_gc_pass(struct super_block *sb, int target)
  397. {
  398. struct logfs_super *super = logfs_super(sb);
  399. struct logfs_block *block;
  400. int round, progress, last_progress = 0;
  401. if (no_free_segments(sb) >= target &&
  402. super->s_no_object_aliases < MAX_OBJ_ALIASES)
  403. return;
  404. log_gc("__logfs_gc_pass(%x)\n", target);
  405. for (round = 0; round < SCAN_ROUNDS; ) {
  406. if (no_free_segments(sb) >= target)
  407. goto write_alias;
  408. /* Sync in-memory state with on-medium state in case they
  409. * diverged */
  410. logfs_write_anchor(sb);
  411. round += logfs_scan_some(sb);
  412. if (no_free_segments(sb) >= target)
  413. goto write_alias;
  414. progress = logfs_gc_once(sb);
  415. if (progress)
  416. last_progress = round;
  417. else if (round - last_progress > 2)
  418. break;
  419. continue;
  420. /*
  421. * The goto logic is nasty, I just don't know a better way to
  422. * code it. GC is supposed to ensure two things:
  423. * 1. Enough free segments are available.
  424. * 2. The number of aliases is bounded.
  425. * When 1. is achieved, we take a look at 2. and write back
  426. * some alias-containing blocks, if necessary. However, after
  427. * each such write we need to go back to 1., as writes can
  428. * consume free segments.
  429. */
  430. write_alias:
  431. if (super->s_no_object_aliases < MAX_OBJ_ALIASES)
  432. return;
  433. if (list_empty(&super->s_object_alias)) {
  434. /* All aliases are still in btree */
  435. return;
  436. }
  437. log_gc("Write back one alias\n");
  438. block = list_entry(super->s_object_alias.next,
  439. struct logfs_block, alias_list);
  440. block->ops->write_block(block);
  441. /*
  442. * To round off the nasty goto logic, we reset round here. It
  443. * is a safety-net for GC not making any progress and limited
  444. * to something reasonably small. If incremented it for every
  445. * single alias, the loop could terminate rather quickly.
  446. */
  447. round = 0;
  448. }
  449. LOGFS_BUG(sb);
  450. }
  451. static int wl_ratelimit(struct super_block *sb, u64 *next_event)
  452. {
  453. struct logfs_super *super = logfs_super(sb);
  454. if (*next_event < super->s_gec) {
  455. *next_event = super->s_gec + WL_RATELIMIT;
  456. return 0;
  457. }
  458. return 1;
  459. }
  460. static void logfs_wl_pass(struct super_block *sb)
  461. {
  462. struct logfs_super *super = logfs_super(sb);
  463. struct gc_candidate *wl_cand, *free_cand;
  464. if (wl_ratelimit(sb, &super->s_wl_gec_ostore))
  465. return;
  466. wl_cand = first_in_list(&super->s_ec_list);
  467. if (!wl_cand)
  468. return;
  469. free_cand = first_in_list(&super->s_free_list);
  470. if (!free_cand)
  471. return;
  472. if (wl_cand->erase_count < free_cand->erase_count + WL_DELTA) {
  473. remove_from_list(wl_cand);
  474. __logfs_gc_once(sb, wl_cand);
  475. }
  476. }
  477. /*
  478. * The journal needs wear leveling as well. But moving the journal is an
  479. * expensive operation so we try to avoid it as much as possible. And if we
  480. * have to do it, we move the whole journal, not individual segments.
  481. *
  482. * Ratelimiting is not strictly necessary here, it mainly serves to avoid the
  483. * calculations. First we check whether moving the journal would be a
  484. * significant improvement. That means that a) the current journal segments
  485. * have more wear than the future journal segments and b) the current journal
  486. * segments have more wear than normal ostore segments.
  487. * Rationale for b) is that we don't have to move the journal if it is aging
  488. * less than the ostore, even if the reserve segments age even less (they are
  489. * excluded from wear leveling, after all).
  490. * Next we check that the superblocks have less wear than the journal. Since
  491. * moving the journal requires writing the superblocks, we have to protect the
  492. * superblocks even more than the journal.
  493. *
  494. * Also we double the acceptable wear difference, compared to ostore wear
  495. * leveling. Journal data is read and rewritten rapidly, comparatively. So
  496. * soft errors have much less time to accumulate and we allow the journal to
  497. * be a bit worse than the ostore.
  498. */
  499. static void logfs_journal_wl_pass(struct super_block *sb)
  500. {
  501. struct logfs_super *super = logfs_super(sb);
  502. struct gc_candidate *cand;
  503. u32 min_journal_ec = -1, max_reserve_ec = 0;
  504. int i;
  505. if (wl_ratelimit(sb, &super->s_wl_gec_journal))
  506. return;
  507. if (super->s_reserve_list.count < super->s_no_journal_segs) {
  508. /* Reserve is not full enough to move complete journal */
  509. return;
  510. }
  511. journal_for_each(i)
  512. if (super->s_journal_seg[i])
  513. min_journal_ec = min(min_journal_ec,
  514. super->s_journal_ec[i]);
  515. cand = rb_entry(rb_first(&super->s_free_list.rb_tree),
  516. struct gc_candidate, rb_node);
  517. max_reserve_ec = cand->erase_count;
  518. for (i = 0; i < 2; i++) {
  519. struct logfs_segment_entry se;
  520. u32 segno = seg_no(sb, super->s_sb_ofs[i]);
  521. u32 ec;
  522. logfs_get_segment_entry(sb, segno, &se);
  523. ec = be32_to_cpu(se.ec_level) >> 4;
  524. max_reserve_ec = max(max_reserve_ec, ec);
  525. }
  526. if (min_journal_ec > max_reserve_ec + 2 * WL_DELTA) {
  527. do_logfs_journal_wl_pass(sb);
  528. }
  529. }
  530. void logfs_gc_pass(struct super_block *sb)
  531. {
  532. struct logfs_super *super = logfs_super(sb);
  533. //BUG_ON(mutex_trylock(&logfs_super(sb)->s_w_mutex));
  534. /* Write journal before free space is getting saturated with dirty
  535. * objects.
  536. */
  537. if (super->s_dirty_used_bytes + super->s_dirty_free_bytes
  538. + LOGFS_MAX_OBJECTSIZE >= super->s_free_bytes)
  539. logfs_write_anchor(sb);
  540. __logfs_gc_pass(sb, super->s_total_levels);
  541. logfs_wl_pass(sb);
  542. logfs_journal_wl_pass(sb);
  543. }
  544. static int check_area(struct super_block *sb, int i)
  545. {
  546. struct logfs_super *super = logfs_super(sb);
  547. struct logfs_area *area = super->s_area[i];
  548. struct logfs_object_header oh;
  549. u32 segno = area->a_segno;
  550. u32 ofs = area->a_used_bytes;
  551. __be32 crc;
  552. int err;
  553. if (!area->a_is_open)
  554. return 0;
  555. for (ofs = area->a_used_bytes;
  556. ofs <= super->s_segsize - sizeof(oh);
  557. ofs += (u32)be16_to_cpu(oh.len) + sizeof(oh)) {
  558. err = wbuf_read(sb, dev_ofs(sb, segno, ofs), sizeof(oh), &oh);
  559. if (err)
  560. return err;
  561. if (!memchr_inv(&oh, 0xff, sizeof(oh)))
  562. break;
  563. crc = logfs_crc32(&oh, sizeof(oh) - 4, 4);
  564. if (crc != oh.crc) {
  565. printk(KERN_INFO "interrupted header at %llx\n",
  566. dev_ofs(sb, segno, ofs));
  567. return 0;
  568. }
  569. }
  570. if (ofs != area->a_used_bytes) {
  571. printk(KERN_INFO "%x bytes unaccounted data found at %llx\n",
  572. ofs - area->a_used_bytes,
  573. dev_ofs(sb, segno, area->a_used_bytes));
  574. area->a_used_bytes = ofs;
  575. }
  576. return 0;
  577. }
  578. int logfs_check_areas(struct super_block *sb)
  579. {
  580. int i, err;
  581. for_each_area(i) {
  582. err = check_area(sb, i);
  583. if (err)
  584. return err;
  585. }
  586. return 0;
  587. }
  588. static void logfs_init_candlist(struct candidate_list *list, int maxcount,
  589. int sort_by_ec)
  590. {
  591. list->count = 0;
  592. list->maxcount = maxcount;
  593. list->sort_by_ec = sort_by_ec;
  594. list->rb_tree = RB_ROOT;
  595. }
  596. int logfs_init_gc(struct super_block *sb)
  597. {
  598. struct logfs_super *super = logfs_super(sb);
  599. int i;
  600. btree_init_mempool32(&super->s_cand_tree, super->s_btree_pool);
  601. logfs_init_candlist(&super->s_free_list, LIST_SIZE + SCAN_RATIO, 1);
  602. logfs_init_candlist(&super->s_reserve_list,
  603. super->s_bad_seg_reserve, 1);
  604. for_each_area(i)
  605. logfs_init_candlist(&super->s_low_list[i], LIST_SIZE, 0);
  606. logfs_init_candlist(&super->s_ec_list, LIST_SIZE, 1);
  607. return 0;
  608. }
  609. static void logfs_cleanup_list(struct super_block *sb,
  610. struct candidate_list *list)
  611. {
  612. struct gc_candidate *cand;
  613. while (list->count) {
  614. cand = rb_entry(list->rb_tree.rb_node, struct gc_candidate,
  615. rb_node);
  616. remove_from_list(cand);
  617. free_candidate(sb, cand);
  618. }
  619. BUG_ON(list->rb_tree.rb_node);
  620. }
  621. void logfs_cleanup_gc(struct super_block *sb)
  622. {
  623. struct logfs_super *super = logfs_super(sb);
  624. int i;
  625. if (!super->s_free_list.count)
  626. return;
  627. /*
  628. * FIXME: The btree may still contain a single empty node. So we
  629. * call the grim visitor to clean up that mess. Btree code should
  630. * do it for us, really.
  631. */
  632. btree_grim_visitor32(&super->s_cand_tree, 0, NULL);
  633. logfs_cleanup_list(sb, &super->s_free_list);
  634. logfs_cleanup_list(sb, &super->s_reserve_list);
  635. for_each_area(i)
  636. logfs_cleanup_list(sb, &super->s_low_list[i]);
  637. logfs_cleanup_list(sb, &super->s_ec_list);
  638. }