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