journal.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * fs/logfs/journal.c - journal handling 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/slab.h>
  10. static void logfs_calc_free(struct super_block *sb)
  11. {
  12. struct logfs_super *super = logfs_super(sb);
  13. u64 reserve, no_segs = super->s_no_segs;
  14. s64 free;
  15. int i;
  16. /* superblock segments */
  17. no_segs -= 2;
  18. super->s_no_journal_segs = 0;
  19. /* journal */
  20. journal_for_each(i)
  21. if (super->s_journal_seg[i]) {
  22. no_segs--;
  23. super->s_no_journal_segs++;
  24. }
  25. /* open segments plus one extra per level for GC */
  26. no_segs -= 2 * super->s_total_levels;
  27. free = no_segs * (super->s_segsize - LOGFS_SEGMENT_RESERVE);
  28. free -= super->s_used_bytes;
  29. /* just a bit extra */
  30. free -= super->s_total_levels * 4096;
  31. /* Bad blocks are 'paid' for with speed reserve - the filesystem
  32. * simply gets slower as bad blocks accumulate. Until the bad blocks
  33. * exceed the speed reserve - then the filesystem gets smaller.
  34. */
  35. reserve = super->s_bad_segments + super->s_bad_seg_reserve;
  36. reserve *= super->s_segsize - LOGFS_SEGMENT_RESERVE;
  37. reserve = max(reserve, super->s_speed_reserve);
  38. free -= reserve;
  39. if (free < 0)
  40. free = 0;
  41. super->s_free_bytes = free;
  42. }
  43. static void reserve_sb_and_journal(struct super_block *sb)
  44. {
  45. struct logfs_super *super = logfs_super(sb);
  46. struct btree_head32 *head = &super->s_reserved_segments;
  47. int i, err;
  48. err = btree_insert32(head, seg_no(sb, super->s_sb_ofs[0]), (void *)1,
  49. GFP_KERNEL);
  50. BUG_ON(err);
  51. err = btree_insert32(head, seg_no(sb, super->s_sb_ofs[1]), (void *)1,
  52. GFP_KERNEL);
  53. BUG_ON(err);
  54. journal_for_each(i) {
  55. if (!super->s_journal_seg[i])
  56. continue;
  57. err = btree_insert32(head, super->s_journal_seg[i], (void *)1,
  58. GFP_KERNEL);
  59. BUG_ON(err);
  60. }
  61. }
  62. static void read_dynsb(struct super_block *sb,
  63. struct logfs_je_dynsb *dynsb)
  64. {
  65. struct logfs_super *super = logfs_super(sb);
  66. super->s_gec = be64_to_cpu(dynsb->ds_gec);
  67. super->s_sweeper = be64_to_cpu(dynsb->ds_sweeper);
  68. super->s_victim_ino = be64_to_cpu(dynsb->ds_victim_ino);
  69. super->s_rename_dir = be64_to_cpu(dynsb->ds_rename_dir);
  70. super->s_rename_pos = be64_to_cpu(dynsb->ds_rename_pos);
  71. super->s_used_bytes = be64_to_cpu(dynsb->ds_used_bytes);
  72. super->s_generation = be32_to_cpu(dynsb->ds_generation);
  73. }
  74. static void read_anchor(struct super_block *sb,
  75. struct logfs_je_anchor *da)
  76. {
  77. struct logfs_super *super = logfs_super(sb);
  78. struct inode *inode = super->s_master_inode;
  79. struct logfs_inode *li = logfs_inode(inode);
  80. int i;
  81. super->s_last_ino = be64_to_cpu(da->da_last_ino);
  82. li->li_flags = 0;
  83. li->li_height = da->da_height;
  84. i_size_write(inode, be64_to_cpu(da->da_size));
  85. li->li_used_bytes = be64_to_cpu(da->da_used_bytes);
  86. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  87. li->li_data[i] = be64_to_cpu(da->da_data[i]);
  88. }
  89. static void read_erasecount(struct super_block *sb,
  90. struct logfs_je_journal_ec *ec)
  91. {
  92. struct logfs_super *super = logfs_super(sb);
  93. int i;
  94. journal_for_each(i)
  95. super->s_journal_ec[i] = be32_to_cpu(ec->ec[i]);
  96. }
  97. static int read_area(struct super_block *sb, struct logfs_je_area *a)
  98. {
  99. struct logfs_super *super = logfs_super(sb);
  100. struct logfs_area *area = super->s_area[a->gc_level];
  101. u64 ofs;
  102. u32 writemask = ~(super->s_writesize - 1);
  103. if (a->gc_level >= LOGFS_NO_AREAS)
  104. return -EIO;
  105. if (a->vim != VIM_DEFAULT)
  106. return -EIO; /* TODO: close area and continue */
  107. area->a_used_bytes = be32_to_cpu(a->used_bytes);
  108. area->a_written_bytes = area->a_used_bytes & writemask;
  109. area->a_segno = be32_to_cpu(a->segno);
  110. if (area->a_segno)
  111. area->a_is_open = 1;
  112. ofs = dev_ofs(sb, area->a_segno, area->a_written_bytes);
  113. if (super->s_writesize > 1)
  114. return logfs_buf_recover(area, ofs, a + 1, super->s_writesize);
  115. else
  116. return logfs_buf_recover(area, ofs, NULL, 0);
  117. }
  118. static void *unpack(void *from, void *to)
  119. {
  120. struct logfs_journal_header *jh = from;
  121. void *data = from + sizeof(struct logfs_journal_header);
  122. int err;
  123. size_t inlen, outlen;
  124. inlen = be16_to_cpu(jh->h_len);
  125. outlen = be16_to_cpu(jh->h_datalen);
  126. if (jh->h_compr == COMPR_NONE)
  127. memcpy(to, data, inlen);
  128. else {
  129. err = logfs_uncompress(data, to, inlen, outlen);
  130. BUG_ON(err);
  131. }
  132. return to;
  133. }
  134. static int __read_je_header(struct super_block *sb, u64 ofs,
  135. struct logfs_journal_header *jh)
  136. {
  137. struct logfs_super *super = logfs_super(sb);
  138. size_t bufsize = max_t(size_t, sb->s_blocksize, super->s_writesize)
  139. + MAX_JOURNAL_HEADER;
  140. u16 type, len, datalen;
  141. int err;
  142. /* read header only */
  143. err = wbuf_read(sb, ofs, sizeof(*jh), jh);
  144. if (err)
  145. return err;
  146. type = be16_to_cpu(jh->h_type);
  147. len = be16_to_cpu(jh->h_len);
  148. datalen = be16_to_cpu(jh->h_datalen);
  149. if (len > sb->s_blocksize)
  150. return -EIO;
  151. if ((type < JE_FIRST) || (type > JE_LAST))
  152. return -EIO;
  153. if (datalen > bufsize)
  154. return -EIO;
  155. return 0;
  156. }
  157. static int __read_je_payload(struct super_block *sb, u64 ofs,
  158. struct logfs_journal_header *jh)
  159. {
  160. u16 len;
  161. int err;
  162. len = be16_to_cpu(jh->h_len);
  163. err = wbuf_read(sb, ofs + sizeof(*jh), len, jh + 1);
  164. if (err)
  165. return err;
  166. if (jh->h_crc != logfs_crc32(jh, len + sizeof(*jh), 4)) {
  167. /* Old code was confused. It forgot about the header length
  168. * and stopped calculating the crc 16 bytes before the end
  169. * of data - ick!
  170. * FIXME: Remove this hack once the old code is fixed.
  171. */
  172. if (jh->h_crc == logfs_crc32(jh, len, 4))
  173. WARN_ON_ONCE(1);
  174. else
  175. return -EIO;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * jh needs to be large enough to hold the complete entry, not just the header
  181. */
  182. static int __read_je(struct super_block *sb, u64 ofs,
  183. struct logfs_journal_header *jh)
  184. {
  185. int err;
  186. err = __read_je_header(sb, ofs, jh);
  187. if (err)
  188. return err;
  189. return __read_je_payload(sb, ofs, jh);
  190. }
  191. static int read_je(struct super_block *sb, u64 ofs)
  192. {
  193. struct logfs_super *super = logfs_super(sb);
  194. struct logfs_journal_header *jh = super->s_compressed_je;
  195. void *scratch = super->s_je;
  196. u16 type, datalen;
  197. int err;
  198. err = __read_je(sb, ofs, jh);
  199. if (err)
  200. return err;
  201. type = be16_to_cpu(jh->h_type);
  202. datalen = be16_to_cpu(jh->h_datalen);
  203. switch (type) {
  204. case JE_DYNSB:
  205. read_dynsb(sb, unpack(jh, scratch));
  206. break;
  207. case JE_ANCHOR:
  208. read_anchor(sb, unpack(jh, scratch));
  209. break;
  210. case JE_ERASECOUNT:
  211. read_erasecount(sb, unpack(jh, scratch));
  212. break;
  213. case JE_AREA:
  214. err = read_area(sb, unpack(jh, scratch));
  215. break;
  216. case JE_OBJ_ALIAS:
  217. err = logfs_load_object_aliases(sb, unpack(jh, scratch),
  218. datalen);
  219. break;
  220. default:
  221. WARN_ON_ONCE(1);
  222. return -EIO;
  223. }
  224. return err;
  225. }
  226. static int logfs_read_segment(struct super_block *sb, u32 segno)
  227. {
  228. struct logfs_super *super = logfs_super(sb);
  229. struct logfs_journal_header *jh = super->s_compressed_je;
  230. u64 ofs, seg_ofs = dev_ofs(sb, segno, 0);
  231. u32 h_ofs, last_ofs = 0;
  232. u16 len, datalen, last_len = 0;
  233. int i, err;
  234. /* search for most recent commit */
  235. for (h_ofs = 0; h_ofs < super->s_segsize; h_ofs += sizeof(*jh)) {
  236. ofs = seg_ofs + h_ofs;
  237. err = __read_je_header(sb, ofs, jh);
  238. if (err)
  239. continue;
  240. if (jh->h_type != cpu_to_be16(JE_COMMIT))
  241. continue;
  242. err = __read_je_payload(sb, ofs, jh);
  243. if (err)
  244. continue;
  245. len = be16_to_cpu(jh->h_len);
  246. datalen = be16_to_cpu(jh->h_datalen);
  247. if ((datalen > sizeof(super->s_je_array)) ||
  248. (datalen % sizeof(__be64)))
  249. continue;
  250. last_ofs = h_ofs;
  251. last_len = datalen;
  252. h_ofs += ALIGN(len, sizeof(*jh)) - sizeof(*jh);
  253. }
  254. /* read commit */
  255. if (last_ofs == 0)
  256. return -ENOENT;
  257. ofs = seg_ofs + last_ofs;
  258. log_journal("Read commit from %llx\n", ofs);
  259. err = __read_je(sb, ofs, jh);
  260. BUG_ON(err); /* We should have caught it in the scan loop already */
  261. if (err)
  262. return err;
  263. /* uncompress */
  264. unpack(jh, super->s_je_array);
  265. super->s_no_je = last_len / sizeof(__be64);
  266. /* iterate over array */
  267. for (i = 0; i < super->s_no_je; i++) {
  268. err = read_je(sb, be64_to_cpu(super->s_je_array[i]));
  269. if (err)
  270. return err;
  271. }
  272. super->s_journal_area->a_segno = segno;
  273. return 0;
  274. }
  275. static u64 read_gec(struct super_block *sb, u32 segno)
  276. {
  277. struct logfs_segment_header sh;
  278. __be32 crc;
  279. int err;
  280. if (!segno)
  281. return 0;
  282. err = wbuf_read(sb, dev_ofs(sb, segno, 0), sizeof(sh), &sh);
  283. if (err)
  284. return 0;
  285. crc = logfs_crc32(&sh, sizeof(sh), 4);
  286. if (crc != sh.crc) {
  287. WARN_ON(sh.gec != cpu_to_be64(0xffffffffffffffffull));
  288. /* Most likely it was just erased */
  289. return 0;
  290. }
  291. return be64_to_cpu(sh.gec);
  292. }
  293. static int logfs_read_journal(struct super_block *sb)
  294. {
  295. struct logfs_super *super = logfs_super(sb);
  296. u64 gec[LOGFS_JOURNAL_SEGS], max;
  297. u32 segno;
  298. int i, max_i;
  299. max = 0;
  300. max_i = -1;
  301. journal_for_each(i) {
  302. segno = super->s_journal_seg[i];
  303. gec[i] = read_gec(sb, super->s_journal_seg[i]);
  304. if (gec[i] > max) {
  305. max = gec[i];
  306. max_i = i;
  307. }
  308. }
  309. if (max_i == -1)
  310. return -EIO;
  311. /* FIXME: Try older segments in case of error */
  312. return logfs_read_segment(sb, super->s_journal_seg[max_i]);
  313. }
  314. /*
  315. * First search the current segment (outer loop), then pick the next segment
  316. * in the array, skipping any zero entries (inner loop).
  317. */
  318. static void journal_get_free_segment(struct logfs_area *area)
  319. {
  320. struct logfs_super *super = logfs_super(area->a_sb);
  321. int i;
  322. journal_for_each(i) {
  323. if (area->a_segno != super->s_journal_seg[i])
  324. continue;
  325. do {
  326. i++;
  327. if (i == LOGFS_JOURNAL_SEGS)
  328. i = 0;
  329. } while (!super->s_journal_seg[i]);
  330. area->a_segno = super->s_journal_seg[i];
  331. area->a_erase_count = ++(super->s_journal_ec[i]);
  332. log_journal("Journal now at %x (ec %x)\n", area->a_segno,
  333. area->a_erase_count);
  334. return;
  335. }
  336. BUG();
  337. }
  338. static void journal_get_erase_count(struct logfs_area *area)
  339. {
  340. /* erase count is stored globally and incremented in
  341. * journal_get_free_segment() - nothing to do here */
  342. }
  343. static int journal_erase_segment(struct logfs_area *area)
  344. {
  345. struct super_block *sb = area->a_sb;
  346. union {
  347. struct logfs_segment_header sh;
  348. unsigned char c[ALIGN(sizeof(struct logfs_segment_header), 16)];
  349. } u;
  350. u64 ofs;
  351. int err;
  352. err = logfs_erase_segment(sb, area->a_segno, 1);
  353. if (err)
  354. return err;
  355. memset(&u, 0, sizeof(u));
  356. u.sh.pad = 0;
  357. u.sh.type = SEG_JOURNAL;
  358. u.sh.level = 0;
  359. u.sh.segno = cpu_to_be32(area->a_segno);
  360. u.sh.ec = cpu_to_be32(area->a_erase_count);
  361. u.sh.gec = cpu_to_be64(logfs_super(sb)->s_gec);
  362. u.sh.crc = logfs_crc32(&u.sh, sizeof(u.sh), 4);
  363. /* This causes a bug in segment.c. Not yet. */
  364. //logfs_set_segment_erased(sb, area->a_segno, area->a_erase_count, 0);
  365. ofs = dev_ofs(sb, area->a_segno, 0);
  366. area->a_used_bytes = sizeof(u);
  367. logfs_buf_write(area, ofs, &u, sizeof(u));
  368. return 0;
  369. }
  370. static size_t __logfs_write_header(struct logfs_super *super,
  371. struct logfs_journal_header *jh, size_t len, size_t datalen,
  372. u16 type, u8 compr)
  373. {
  374. jh->h_len = cpu_to_be16(len);
  375. jh->h_type = cpu_to_be16(type);
  376. jh->h_datalen = cpu_to_be16(datalen);
  377. jh->h_compr = compr;
  378. jh->h_pad[0] = 'H';
  379. jh->h_pad[1] = 'E';
  380. jh->h_pad[2] = 'A';
  381. jh->h_pad[3] = 'D';
  382. jh->h_pad[4] = 'R';
  383. jh->h_crc = logfs_crc32(jh, len + sizeof(*jh), 4);
  384. return ALIGN(len, 16) + sizeof(*jh);
  385. }
  386. static size_t logfs_write_header(struct logfs_super *super,
  387. struct logfs_journal_header *jh, size_t datalen, u16 type)
  388. {
  389. size_t len = datalen;
  390. return __logfs_write_header(super, jh, len, datalen, type, COMPR_NONE);
  391. }
  392. static inline size_t logfs_journal_erasecount_size(struct logfs_super *super)
  393. {
  394. return LOGFS_JOURNAL_SEGS * sizeof(__be32);
  395. }
  396. static void *logfs_write_erasecount(struct super_block *sb, void *_ec,
  397. u16 *type, size_t *len)
  398. {
  399. struct logfs_super *super = logfs_super(sb);
  400. struct logfs_je_journal_ec *ec = _ec;
  401. int i;
  402. journal_for_each(i)
  403. ec->ec[i] = cpu_to_be32(super->s_journal_ec[i]);
  404. *type = JE_ERASECOUNT;
  405. *len = logfs_journal_erasecount_size(super);
  406. return ec;
  407. }
  408. static void account_shadow(void *_shadow, unsigned long _sb, u64 ignore,
  409. size_t ignore2)
  410. {
  411. struct logfs_shadow *shadow = _shadow;
  412. struct super_block *sb = (void *)_sb;
  413. struct logfs_super *super = logfs_super(sb);
  414. /* consume new space */
  415. super->s_free_bytes -= shadow->new_len;
  416. super->s_used_bytes += shadow->new_len;
  417. super->s_dirty_used_bytes -= shadow->new_len;
  418. /* free up old space */
  419. super->s_free_bytes += shadow->old_len;
  420. super->s_used_bytes -= shadow->old_len;
  421. super->s_dirty_free_bytes -= shadow->old_len;
  422. logfs_set_segment_used(sb, shadow->old_ofs, -shadow->old_len);
  423. logfs_set_segment_used(sb, shadow->new_ofs, shadow->new_len);
  424. log_journal("account_shadow(%llx, %llx, %x) %llx->%llx %x->%x\n",
  425. shadow->ino, shadow->bix, shadow->gc_level,
  426. shadow->old_ofs, shadow->new_ofs,
  427. shadow->old_len, shadow->new_len);
  428. mempool_free(shadow, super->s_shadow_pool);
  429. }
  430. static void account_shadows(struct super_block *sb)
  431. {
  432. struct logfs_super *super = logfs_super(sb);
  433. struct inode *inode = super->s_master_inode;
  434. struct logfs_inode *li = logfs_inode(inode);
  435. struct shadow_tree *tree = &super->s_shadow_tree;
  436. btree_grim_visitor64(&tree->new, (unsigned long)sb, account_shadow);
  437. btree_grim_visitor64(&tree->old, (unsigned long)sb, account_shadow);
  438. btree_grim_visitor32(&tree->segment_map, 0, NULL);
  439. tree->no_shadowed_segments = 0;
  440. if (li->li_block) {
  441. /*
  442. * We never actually use the structure, when attached to the
  443. * master inode. But it is easier to always free it here than
  444. * to have checks in several places elsewhere when allocating
  445. * it.
  446. */
  447. li->li_block->ops->free_block(sb, li->li_block);
  448. }
  449. BUG_ON((s64)li->li_used_bytes < 0);
  450. }
  451. static void *__logfs_write_anchor(struct super_block *sb, void *_da,
  452. u16 *type, size_t *len)
  453. {
  454. struct logfs_super *super = logfs_super(sb);
  455. struct logfs_je_anchor *da = _da;
  456. struct inode *inode = super->s_master_inode;
  457. struct logfs_inode *li = logfs_inode(inode);
  458. int i;
  459. da->da_height = li->li_height;
  460. da->da_last_ino = cpu_to_be64(super->s_last_ino);
  461. da->da_size = cpu_to_be64(i_size_read(inode));
  462. da->da_used_bytes = cpu_to_be64(li->li_used_bytes);
  463. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  464. da->da_data[i] = cpu_to_be64(li->li_data[i]);
  465. *type = JE_ANCHOR;
  466. *len = sizeof(*da);
  467. return da;
  468. }
  469. static void *logfs_write_dynsb(struct super_block *sb, void *_dynsb,
  470. u16 *type, size_t *len)
  471. {
  472. struct logfs_super *super = logfs_super(sb);
  473. struct logfs_je_dynsb *dynsb = _dynsb;
  474. dynsb->ds_gec = cpu_to_be64(super->s_gec);
  475. dynsb->ds_sweeper = cpu_to_be64(super->s_sweeper);
  476. dynsb->ds_victim_ino = cpu_to_be64(super->s_victim_ino);
  477. dynsb->ds_rename_dir = cpu_to_be64(super->s_rename_dir);
  478. dynsb->ds_rename_pos = cpu_to_be64(super->s_rename_pos);
  479. dynsb->ds_used_bytes = cpu_to_be64(super->s_used_bytes);
  480. dynsb->ds_generation = cpu_to_be32(super->s_generation);
  481. *type = JE_DYNSB;
  482. *len = sizeof(*dynsb);
  483. return dynsb;
  484. }
  485. static void write_wbuf(struct super_block *sb, struct logfs_area *area,
  486. void *wbuf)
  487. {
  488. struct logfs_super *super = logfs_super(sb);
  489. struct address_space *mapping = super->s_mapping_inode->i_mapping;
  490. u64 ofs;
  491. pgoff_t index;
  492. int page_ofs;
  493. struct page *page;
  494. ofs = dev_ofs(sb, area->a_segno,
  495. area->a_used_bytes & ~(super->s_writesize - 1));
  496. index = ofs >> PAGE_SHIFT;
  497. page_ofs = ofs & (PAGE_SIZE - 1);
  498. page = find_lock_page(mapping, index);
  499. BUG_ON(!page);
  500. memcpy(wbuf, page_address(page) + page_ofs, super->s_writesize);
  501. unlock_page(page);
  502. }
  503. static void *logfs_write_area(struct super_block *sb, void *_a,
  504. u16 *type, size_t *len)
  505. {
  506. struct logfs_super *super = logfs_super(sb);
  507. struct logfs_area *area = super->s_area[super->s_sum_index];
  508. struct logfs_je_area *a = _a;
  509. a->vim = VIM_DEFAULT;
  510. a->gc_level = super->s_sum_index;
  511. a->used_bytes = cpu_to_be32(area->a_used_bytes);
  512. a->segno = cpu_to_be32(area->a_segno);
  513. if (super->s_writesize > 1)
  514. write_wbuf(sb, area, a + 1);
  515. *type = JE_AREA;
  516. *len = sizeof(*a) + super->s_writesize;
  517. return a;
  518. }
  519. static void *logfs_write_commit(struct super_block *sb, void *h,
  520. u16 *type, size_t *len)
  521. {
  522. struct logfs_super *super = logfs_super(sb);
  523. *type = JE_COMMIT;
  524. *len = super->s_no_je * sizeof(__be64);
  525. return super->s_je_array;
  526. }
  527. static size_t __logfs_write_je(struct super_block *sb, void *buf, u16 type,
  528. size_t len)
  529. {
  530. struct logfs_super *super = logfs_super(sb);
  531. void *header = super->s_compressed_je;
  532. void *data = header + sizeof(struct logfs_journal_header);
  533. ssize_t compr_len, pad_len;
  534. u8 compr = COMPR_ZLIB;
  535. if (len == 0)
  536. return logfs_write_header(super, header, 0, type);
  537. BUG_ON(len > sb->s_blocksize);
  538. compr_len = logfs_compress(buf, data, len, sb->s_blocksize);
  539. if (compr_len < 0 || type == JE_ANCHOR) {
  540. memcpy(data, buf, len);
  541. compr_len = len;
  542. compr = COMPR_NONE;
  543. }
  544. pad_len = ALIGN(compr_len, 16);
  545. memset(data + compr_len, 0, pad_len - compr_len);
  546. return __logfs_write_header(super, header, compr_len, len, type, compr);
  547. }
  548. static s64 logfs_get_free_bytes(struct logfs_area *area, size_t *bytes,
  549. int must_pad)
  550. {
  551. u32 writesize = logfs_super(area->a_sb)->s_writesize;
  552. s32 ofs;
  553. int ret;
  554. ret = logfs_open_area(area, *bytes);
  555. if (ret)
  556. return -EAGAIN;
  557. ofs = area->a_used_bytes;
  558. area->a_used_bytes += *bytes;
  559. if (must_pad) {
  560. area->a_used_bytes = ALIGN(area->a_used_bytes, writesize);
  561. *bytes = area->a_used_bytes - ofs;
  562. }
  563. return dev_ofs(area->a_sb, area->a_segno, ofs);
  564. }
  565. static int logfs_write_je_buf(struct super_block *sb, void *buf, u16 type,
  566. size_t buf_len)
  567. {
  568. struct logfs_super *super = logfs_super(sb);
  569. struct logfs_area *area = super->s_journal_area;
  570. struct logfs_journal_header *jh = super->s_compressed_je;
  571. size_t len;
  572. int must_pad = 0;
  573. s64 ofs;
  574. len = __logfs_write_je(sb, buf, type, buf_len);
  575. if (jh->h_type == cpu_to_be16(JE_COMMIT))
  576. must_pad = 1;
  577. ofs = logfs_get_free_bytes(area, &len, must_pad);
  578. if (ofs < 0)
  579. return ofs;
  580. logfs_buf_write(area, ofs, super->s_compressed_je, len);
  581. BUG_ON(super->s_no_je >= MAX_JOURNAL_ENTRIES);
  582. super->s_je_array[super->s_no_je++] = cpu_to_be64(ofs);
  583. return 0;
  584. }
  585. static int logfs_write_je(struct super_block *sb,
  586. void* (*write)(struct super_block *sb, void *scratch,
  587. u16 *type, size_t *len))
  588. {
  589. void *buf;
  590. size_t len;
  591. u16 type;
  592. buf = write(sb, logfs_super(sb)->s_je, &type, &len);
  593. return logfs_write_je_buf(sb, buf, type, len);
  594. }
  595. int write_alias_journal(struct super_block *sb, u64 ino, u64 bix,
  596. level_t level, int child_no, __be64 val)
  597. {
  598. struct logfs_super *super = logfs_super(sb);
  599. struct logfs_obj_alias *oa = super->s_je;
  600. int err = 0, fill = super->s_je_fill;
  601. log_aliases("logfs_write_obj_aliases #%x(%llx, %llx, %x, %x) %llx\n",
  602. fill, ino, bix, level, child_no, be64_to_cpu(val));
  603. oa[fill].ino = cpu_to_be64(ino);
  604. oa[fill].bix = cpu_to_be64(bix);
  605. oa[fill].val = val;
  606. oa[fill].level = (__force u8)level;
  607. oa[fill].child_no = cpu_to_be16(child_no);
  608. fill++;
  609. if (fill >= sb->s_blocksize / sizeof(*oa)) {
  610. err = logfs_write_je_buf(sb, oa, JE_OBJ_ALIAS, sb->s_blocksize);
  611. fill = 0;
  612. }
  613. super->s_je_fill = fill;
  614. return err;
  615. }
  616. static int logfs_write_obj_aliases(struct super_block *sb)
  617. {
  618. struct logfs_super *super = logfs_super(sb);
  619. int err;
  620. log_journal("logfs_write_obj_aliases: %d aliases to write\n",
  621. super->s_no_object_aliases);
  622. super->s_je_fill = 0;
  623. err = logfs_write_obj_aliases_pagecache(sb);
  624. if (err)
  625. return err;
  626. if (super->s_je_fill)
  627. err = logfs_write_je_buf(sb, super->s_je, JE_OBJ_ALIAS,
  628. super->s_je_fill
  629. * sizeof(struct logfs_obj_alias));
  630. return err;
  631. }
  632. /*
  633. * Write all journal entries. The goto logic ensures that all journal entries
  634. * are written whenever a new segment is used. It is ugly and potentially a
  635. * bit wasteful, but robustness is more important. With this we can *always*
  636. * erase all journal segments except the one containing the most recent commit.
  637. */
  638. void logfs_write_anchor(struct super_block *sb)
  639. {
  640. struct logfs_super *super = logfs_super(sb);
  641. struct logfs_area *area = super->s_journal_area;
  642. int i, err;
  643. if (!(super->s_flags & LOGFS_SB_FLAG_DIRTY))
  644. return;
  645. super->s_flags &= ~LOGFS_SB_FLAG_DIRTY;
  646. BUG_ON(super->s_flags & LOGFS_SB_FLAG_SHUTDOWN);
  647. mutex_lock(&super->s_journal_mutex);
  648. /* Do this first or suffer corruption */
  649. logfs_sync_segments(sb);
  650. account_shadows(sb);
  651. again:
  652. super->s_no_je = 0;
  653. for_each_area(i) {
  654. if (!super->s_area[i]->a_is_open)
  655. continue;
  656. super->s_sum_index = i;
  657. err = logfs_write_je(sb, logfs_write_area);
  658. if (err)
  659. goto again;
  660. }
  661. err = logfs_write_obj_aliases(sb);
  662. if (err)
  663. goto again;
  664. err = logfs_write_je(sb, logfs_write_erasecount);
  665. if (err)
  666. goto again;
  667. err = logfs_write_je(sb, __logfs_write_anchor);
  668. if (err)
  669. goto again;
  670. err = logfs_write_je(sb, logfs_write_dynsb);
  671. if (err)
  672. goto again;
  673. /*
  674. * Order is imperative. First we sync all writes, including the
  675. * non-committed journal writes. Then we write the final commit and
  676. * sync the current journal segment.
  677. * There is a theoretical bug here. Syncing the journal segment will
  678. * write a number of journal entries and the final commit. All these
  679. * are written in a single operation. If the device layer writes the
  680. * data back-to-front, the commit will precede the other journal
  681. * entries, leaving a race window.
  682. * Two fixes are possible. Preferred is to fix the device layer to
  683. * ensure writes happen front-to-back. Alternatively we can insert
  684. * another logfs_sync_area() super->s_devops->sync() combo before
  685. * writing the commit.
  686. */
  687. /*
  688. * On another subject, super->s_devops->sync is usually not necessary.
  689. * Unless called from sys_sync or friends, a barrier would suffice.
  690. */
  691. super->s_devops->sync(sb);
  692. err = logfs_write_je(sb, logfs_write_commit);
  693. if (err)
  694. goto again;
  695. log_journal("Write commit to %llx\n",
  696. be64_to_cpu(super->s_je_array[super->s_no_je - 1]));
  697. logfs_sync_area(area);
  698. BUG_ON(area->a_used_bytes != area->a_written_bytes);
  699. super->s_devops->sync(sb);
  700. mutex_unlock(&super->s_journal_mutex);
  701. return;
  702. }
  703. void do_logfs_journal_wl_pass(struct super_block *sb)
  704. {
  705. struct logfs_super *super = logfs_super(sb);
  706. struct logfs_area *area = super->s_journal_area;
  707. struct btree_head32 *head = &super->s_reserved_segments;
  708. u32 segno, ec;
  709. int i, err;
  710. log_journal("Journal requires wear-leveling.\n");
  711. /* Drop old segments */
  712. journal_for_each(i)
  713. if (super->s_journal_seg[i]) {
  714. btree_remove32(head, super->s_journal_seg[i]);
  715. logfs_set_segment_unreserved(sb,
  716. super->s_journal_seg[i],
  717. super->s_journal_ec[i]);
  718. super->s_journal_seg[i] = 0;
  719. super->s_journal_ec[i] = 0;
  720. }
  721. /* Get new segments */
  722. for (i = 0; i < super->s_no_journal_segs; i++) {
  723. segno = get_best_cand(sb, &super->s_reserve_list, &ec);
  724. super->s_journal_seg[i] = segno;
  725. super->s_journal_ec[i] = ec;
  726. logfs_set_segment_reserved(sb, segno);
  727. err = btree_insert32(head, segno, (void *)1, GFP_NOFS);
  728. BUG_ON(err); /* mempool should prevent this */
  729. err = logfs_erase_segment(sb, segno, 1);
  730. BUG_ON(err); /* FIXME: remount-ro would be nicer */
  731. }
  732. /* Manually move journal_area */
  733. freeseg(sb, area->a_segno);
  734. area->a_segno = super->s_journal_seg[0];
  735. area->a_is_open = 0;
  736. area->a_used_bytes = 0;
  737. /* Write journal */
  738. logfs_write_anchor(sb);
  739. /* Write superblocks */
  740. err = logfs_write_sb(sb);
  741. BUG_ON(err);
  742. }
  743. static const struct logfs_area_ops journal_area_ops = {
  744. .get_free_segment = journal_get_free_segment,
  745. .get_erase_count = journal_get_erase_count,
  746. .erase_segment = journal_erase_segment,
  747. };
  748. int logfs_init_journal(struct super_block *sb)
  749. {
  750. struct logfs_super *super = logfs_super(sb);
  751. size_t bufsize = max_t(size_t, sb->s_blocksize, super->s_writesize)
  752. + MAX_JOURNAL_HEADER;
  753. int ret = -ENOMEM;
  754. mutex_init(&super->s_journal_mutex);
  755. btree_init_mempool32(&super->s_reserved_segments, super->s_btree_pool);
  756. super->s_je = kzalloc(bufsize, GFP_KERNEL);
  757. if (!super->s_je)
  758. return ret;
  759. super->s_compressed_je = kzalloc(bufsize, GFP_KERNEL);
  760. if (!super->s_compressed_je)
  761. return ret;
  762. super->s_master_inode = logfs_new_meta_inode(sb, LOGFS_INO_MASTER);
  763. if (IS_ERR(super->s_master_inode))
  764. return PTR_ERR(super->s_master_inode);
  765. ret = logfs_read_journal(sb);
  766. if (ret)
  767. return -EIO;
  768. reserve_sb_and_journal(sb);
  769. logfs_calc_free(sb);
  770. super->s_journal_area->a_ops = &journal_area_ops;
  771. return 0;
  772. }
  773. void logfs_cleanup_journal(struct super_block *sb)
  774. {
  775. struct logfs_super *super = logfs_super(sb);
  776. btree_grim_visitor32(&super->s_reserved_segments, 0, NULL);
  777. kfree(super->s_compressed_je);
  778. kfree(super->s_je);
  779. }