aboutsummaryrefslogtreecommitdiffstats
path: root/fs/logfs/segment.c
diff options
context:
space:
mode:
authorJoern Engel <joern@logfs.org>2009-11-20 14:13:39 -0500
committerJoern Engel <joern@logfs.org>2009-11-20 14:13:39 -0500
commit5db53f3e80dee2d9dff5e534f9e9fe1db17c9936 (patch)
tree066f2873eeb7eb86466f6389e45892d957db3de2 /fs/logfs/segment.c
parent66b00a7c93ec782d118d2c03bd599cfd041e80a1 (diff)
[LogFS] add new flash file system
This is a new flash file system. See Documentation/filesystems/logfs.txt Signed-off-by: Joern Engel <joern@logfs.org>
Diffstat (limited to 'fs/logfs/segment.c')
-rw-r--r--fs/logfs/segment.c924
1 files changed, 924 insertions, 0 deletions
diff --git a/fs/logfs/segment.c b/fs/logfs/segment.c
new file mode 100644
index 000000000000..5f58b74516ca
--- /dev/null
+++ b/fs/logfs/segment.c
@@ -0,0 +1,924 @@
1/*
2 * fs/logfs/segment.c - Handling the Object Store
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 * Object store or ostore makes up the complete device with exception of
9 * the superblock and journal areas. Apart from its own metadata it stores
10 * three kinds of objects: inodes, dentries and blocks, both data and indirect.
11 */
12#include "logfs.h"
13
14static int logfs_mark_segment_bad(struct super_block *sb, u32 segno)
15{
16 struct logfs_super *super = logfs_super(sb);
17 struct btree_head32 *head = &super->s_reserved_segments;
18 int err;
19
20 err = btree_insert32(head, segno, (void *)1, GFP_NOFS);
21 if (err)
22 return err;
23 logfs_super(sb)->s_bad_segments++;
24 /* FIXME: write to journal */
25 return 0;
26}
27
28int logfs_erase_segment(struct super_block *sb, u32 segno)
29{
30 struct logfs_super *super = logfs_super(sb);
31
32 super->s_gec++;
33
34 return super->s_devops->erase(sb, (u64)segno << super->s_segshift,
35 super->s_segsize);
36}
37
38static s64 logfs_get_free_bytes(struct logfs_area *area, size_t bytes)
39{
40 s32 ofs;
41
42 logfs_open_area(area, bytes);
43
44 ofs = area->a_used_bytes;
45 area->a_used_bytes += bytes;
46 BUG_ON(area->a_used_bytes >= logfs_super(area->a_sb)->s_segsize);
47
48 return dev_ofs(area->a_sb, area->a_segno, ofs);
49}
50
51static struct page *get_mapping_page(struct super_block *sb, pgoff_t index,
52 int use_filler)
53{
54 struct logfs_super *super = logfs_super(sb);
55 struct address_space *mapping = super->s_mapping_inode->i_mapping;
56 filler_t *filler = super->s_devops->readpage;
57 struct page *page;
58
59 BUG_ON(mapping_gfp_mask(mapping) & __GFP_FS);
60 if (use_filler)
61 page = read_cache_page(mapping, index, filler, sb);
62 else {
63 page = find_or_create_page(mapping, index, GFP_NOFS);
64 unlock_page(page);
65 }
66 return page;
67}
68
69void __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len,
70 int use_filler)
71{
72 pgoff_t index = ofs >> PAGE_SHIFT;
73 struct page *page;
74 long offset = ofs & (PAGE_SIZE-1);
75 long copylen;
76
77 /* Only logfs_wbuf_recover may use len==0 */
78 BUG_ON(!len && !use_filler);
79 do {
80 copylen = min((ulong)len, PAGE_SIZE - offset);
81
82 page = get_mapping_page(area->a_sb, index, use_filler);
83 SetPageUptodate(page);
84 BUG_ON(!page); /* FIXME: reserve a pool */
85 memcpy(page_address(page) + offset, buf, copylen);
86 SetPagePrivate(page);
87 page_cache_release(page);
88
89 buf += copylen;
90 len -= copylen;
91 offset = 0;
92 index++;
93 } while (len);
94}
95
96/*
97 * bdev_writeseg will write full pages. Memset the tail to prevent data leaks.
98 */
99static void pad_wbuf(struct logfs_area *area, int final)
100{
101 struct super_block *sb = area->a_sb;
102 struct logfs_super *super = logfs_super(sb);
103 struct page *page;
104 u64 ofs = dev_ofs(sb, area->a_segno, area->a_used_bytes);
105 pgoff_t index = ofs >> PAGE_SHIFT;
106 long offset = ofs & (PAGE_SIZE-1);
107 u32 len = PAGE_SIZE - offset;
108
109 if (len == PAGE_SIZE) {
110 /* The math in this function can surely use some love */
111 len = 0;
112 }
113 if (len) {
114 BUG_ON(area->a_used_bytes >= super->s_segsize);
115
116 page = get_mapping_page(area->a_sb, index, 0);
117 BUG_ON(!page); /* FIXME: reserve a pool */
118 memset(page_address(page) + offset, 0xff, len);
119 SetPagePrivate(page);
120 page_cache_release(page);
121 }
122
123 if (!final)
124 return;
125
126 area->a_used_bytes += len;
127 for ( ; area->a_used_bytes < super->s_segsize;
128 area->a_used_bytes += PAGE_SIZE) {
129 /* Memset another page */
130 index++;
131 page = get_mapping_page(area->a_sb, index, 0);
132 BUG_ON(!page); /* FIXME: reserve a pool */
133 memset(page_address(page), 0xff, PAGE_SIZE);
134 SetPagePrivate(page);
135 page_cache_release(page);
136 }
137}
138
139/*
140 * We have to be careful with the alias tree. Since lookup is done by bix,
141 * it needs to be normalized, so 14, 15, 16, etc. all match when dealing with
142 * indirect blocks. So always use it through accessor functions.
143 */
144static void *alias_tree_lookup(struct super_block *sb, u64 ino, u64 bix,
145 level_t level)
146{
147 struct btree_head128 *head = &logfs_super(sb)->s_object_alias_tree;
148 pgoff_t index = logfs_pack_index(bix, level);
149
150 return btree_lookup128(head, ino, index);
151}
152
153static int alias_tree_insert(struct super_block *sb, u64 ino, u64 bix,
154 level_t level, void *val)
155{
156 struct btree_head128 *head = &logfs_super(sb)->s_object_alias_tree;
157 pgoff_t index = logfs_pack_index(bix, level);
158
159 return btree_insert128(head, ino, index, val, GFP_NOFS);
160}
161
162static int btree_write_alias(struct super_block *sb, struct logfs_block *block,
163 write_alias_t *write_one_alias)
164{
165 struct object_alias_item *item;
166 int err;
167
168 list_for_each_entry(item, &block->item_list, list) {
169 err = write_alias_journal(sb, block->ino, block->bix,
170 block->level, item->child_no, item->val);
171 if (err)
172 return err;
173 }
174 return 0;
175}
176
177static gc_level_t btree_block_level(struct logfs_block *block)
178{
179 return expand_level(block->ino, block->level);
180}
181
182static struct logfs_block_ops btree_block_ops = {
183 .write_block = btree_write_block,
184 .block_level = btree_block_level,
185 .free_block = __free_block,
186 .write_alias = btree_write_alias,
187};
188
189int logfs_load_object_aliases(struct super_block *sb,
190 struct logfs_obj_alias *oa, int count)
191{
192 struct logfs_super *super = logfs_super(sb);
193 struct logfs_block *block;
194 struct object_alias_item *item;
195 u64 ino, bix;
196 level_t level;
197 int i, err;
198
199 super->s_flags |= LOGFS_SB_FLAG_OBJ_ALIAS;
200 count /= sizeof(*oa);
201 for (i = 0; i < count; i++) {
202 item = mempool_alloc(super->s_alias_pool, GFP_NOFS);
203 if (!item)
204 return -ENOMEM;
205 memset(item, 0, sizeof(*item));
206
207 super->s_no_object_aliases++;
208 item->val = oa[i].val;
209 item->child_no = be16_to_cpu(oa[i].child_no);
210
211 ino = be64_to_cpu(oa[i].ino);
212 bix = be64_to_cpu(oa[i].bix);
213 level = LEVEL(oa[i].level);
214
215 log_aliases("logfs_load_object_aliases(%llx, %llx, %x, %x) %llx\n",
216 ino, bix, level, item->child_no,
217 be64_to_cpu(item->val));
218 block = alias_tree_lookup(sb, ino, bix, level);
219 if (!block) {
220 block = __alloc_block(sb, ino, bix, level);
221 block->ops = &btree_block_ops;
222 err = alias_tree_insert(sb, ino, bix, level, block);
223 BUG_ON(err); /* mempool empty */
224 }
225 if (test_and_set_bit(item->child_no, block->alias_map)) {
226 printk(KERN_ERR"LogFS: Alias collision detected\n");
227 return -EIO;
228 }
229 list_move_tail(&block->alias_list, &super->s_object_alias);
230 list_add(&item->list, &block->item_list);
231 }
232 return 0;
233}
234
235static void kill_alias(void *_block, unsigned long ignore0,
236 u64 ignore1, u64 ignore2, size_t ignore3)
237{
238 struct logfs_block *block = _block;
239 struct super_block *sb = block->sb;
240 struct logfs_super *super = logfs_super(sb);
241 struct object_alias_item *item;
242
243 while (!list_empty(&block->item_list)) {
244 item = list_entry(block->item_list.next, typeof(*item), list);
245 list_del(&item->list);
246 mempool_free(item, super->s_alias_pool);
247 }
248 block->ops->free_block(sb, block);
249}
250
251static int obj_type(struct inode *inode, level_t level)
252{
253 if (level == 0) {
254 if (S_ISDIR(inode->i_mode))
255 return OBJ_DENTRY;
256 if (inode->i_ino == LOGFS_INO_MASTER)
257 return OBJ_INODE;
258 }
259 return OBJ_BLOCK;
260}
261
262static int obj_len(struct super_block *sb, int obj_type)
263{
264 switch (obj_type) {
265 case OBJ_DENTRY:
266 return sizeof(struct logfs_disk_dentry);
267 case OBJ_INODE:
268 return sizeof(struct logfs_disk_inode);
269 case OBJ_BLOCK:
270 return sb->s_blocksize;
271 default:
272 BUG();
273 }
274}
275
276static int __logfs_segment_write(struct inode *inode, void *buf,
277 struct logfs_shadow *shadow, int type, int len, int compr)
278{
279 struct logfs_area *area;
280 struct super_block *sb = inode->i_sb;
281 s64 ofs;
282 struct logfs_object_header h;
283 int acc_len;
284
285 if (shadow->gc_level == 0)
286 acc_len = len;
287 else
288 acc_len = obj_len(sb, type);
289
290 area = get_area(sb, shadow->gc_level);
291 ofs = logfs_get_free_bytes(area, len + LOGFS_OBJECT_HEADERSIZE);
292 LOGFS_BUG_ON(ofs <= 0, sb);
293 /*
294 * Order is important. logfs_get_free_bytes(), by modifying the
295 * segment file, may modify the content of the very page we're about
296 * to write now. Which is fine, as long as the calculated crc and
297 * written data still match. So do the modifications _before_
298 * calculating the crc.
299 */
300
301 h.len = cpu_to_be16(len);
302 h.type = type;
303 h.compr = compr;
304 h.ino = cpu_to_be64(inode->i_ino);
305 h.bix = cpu_to_be64(shadow->bix);
306 h.crc = logfs_crc32(&h, sizeof(h) - 4, 4);
307 h.data_crc = logfs_crc32(buf, len, 0);
308
309 logfs_buf_write(area, ofs, &h, sizeof(h));
310 logfs_buf_write(area, ofs + LOGFS_OBJECT_HEADERSIZE, buf, len);
311
312 shadow->new_ofs = ofs;
313 shadow->new_len = acc_len + LOGFS_OBJECT_HEADERSIZE;
314
315 return 0;
316}
317
318static s64 logfs_segment_write_compress(struct inode *inode, void *buf,
319 struct logfs_shadow *shadow, int type, int len)
320{
321 struct super_block *sb = inode->i_sb;
322 void *compressor_buf = logfs_super(sb)->s_compressed_je;
323 ssize_t compr_len;
324 int ret;
325
326 mutex_lock(&logfs_super(sb)->s_journal_mutex);
327 compr_len = logfs_compress(buf, compressor_buf, len, len);
328
329 if (compr_len >= 0) {
330 ret = __logfs_segment_write(inode, compressor_buf, shadow,
331 type, compr_len, COMPR_ZLIB);
332 } else {
333 ret = __logfs_segment_write(inode, buf, shadow, type, len,
334 COMPR_NONE);
335 }
336 mutex_unlock(&logfs_super(sb)->s_journal_mutex);
337 return ret;
338}
339
340/**
341 * logfs_segment_write - write data block to object store
342 * @inode: inode containing data
343 *
344 * Returns an errno or zero.
345 */
346int logfs_segment_write(struct inode *inode, struct page *page,
347 struct logfs_shadow *shadow)
348{
349 struct super_block *sb = inode->i_sb;
350 struct logfs_super *super = logfs_super(sb);
351 int do_compress, type, len;
352 int ret;
353 void *buf;
354
355 BUG_ON(logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN);
356 do_compress = logfs_inode(inode)->li_flags & LOGFS_IF_COMPRESSED;
357 if (shadow->gc_level != 0) {
358 /* temporarily disable compression for indirect blocks */
359 do_compress = 0;
360 }
361
362 type = obj_type(inode, shrink_level(shadow->gc_level));
363 len = obj_len(sb, type);
364 buf = kmap(page);
365 if (do_compress)
366 ret = logfs_segment_write_compress(inode, buf, shadow, type,
367 len);
368 else
369 ret = __logfs_segment_write(inode, buf, shadow, type, len,
370 COMPR_NONE);
371 kunmap(page);
372
373 log_segment("logfs_segment_write(%llx, %llx, %x) %llx->%llx %x->%x\n",
374 shadow->ino, shadow->bix, shadow->gc_level,
375 shadow->old_ofs, shadow->new_ofs,
376 shadow->old_len, shadow->new_len);
377 /* this BUG_ON did catch a locking bug. useful */
378 BUG_ON(!(shadow->new_ofs & (super->s_segsize - 1)));
379 return ret;
380}
381
382int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf)
383{
384 pgoff_t index = ofs >> PAGE_SHIFT;
385 struct page *page;
386 long offset = ofs & (PAGE_SIZE-1);
387 long copylen;
388
389 while (len) {
390 copylen = min((ulong)len, PAGE_SIZE - offset);
391
392 page = get_mapping_page(sb, index, 1);
393 if (IS_ERR(page))
394 return PTR_ERR(page);
395 memcpy(buf, page_address(page) + offset, copylen);
396 page_cache_release(page);
397
398 buf += copylen;
399 len -= copylen;
400 offset = 0;
401 index++;
402 }
403 return 0;
404}
405
406/*
407 * The "position" of indirect blocks is ambiguous. It can be the position
408 * of any data block somewhere behind this indirect block. So we need to
409 * normalize the positions through logfs_block_mask() before comparing.
410 */
411static int check_pos(struct super_block *sb, u64 pos1, u64 pos2, level_t level)
412{
413 return (pos1 & logfs_block_mask(sb, level)) !=
414 (pos2 & logfs_block_mask(sb, level));
415}
416
417#if 0
418static int read_seg_header(struct super_block *sb, u64 ofs,
419 struct logfs_segment_header *sh)
420{
421 __be32 crc;
422 int err;
423
424 err = wbuf_read(sb, ofs, sizeof(*sh), sh);
425 if (err)
426 return err;
427 crc = logfs_crc32(sh, sizeof(*sh), 4);
428 if (crc != sh->crc) {
429 printk(KERN_ERR"LOGFS: header crc error at %llx: expected %x, "
430 "got %x\n", ofs, be32_to_cpu(sh->crc),
431 be32_to_cpu(crc));
432 return -EIO;
433 }
434 return 0;
435}
436#endif
437
438static int read_obj_header(struct super_block *sb, u64 ofs,
439 struct logfs_object_header *oh)
440{
441 __be32 crc;
442 int err;
443
444 err = wbuf_read(sb, ofs, sizeof(*oh), oh);
445 if (err)
446 return err;
447 crc = logfs_crc32(oh, sizeof(*oh) - 4, 4);
448 if (crc != oh->crc) {
449 printk(KERN_ERR"LOGFS: header crc error at %llx: expected %x, "
450 "got %x\n", ofs, be32_to_cpu(oh->crc),
451 be32_to_cpu(crc));
452 return -EIO;
453 }
454 return 0;
455}
456
457static void move_btree_to_page(struct inode *inode, struct page *page,
458 __be64 *data)
459{
460 struct super_block *sb = inode->i_sb;
461 struct logfs_super *super = logfs_super(sb);
462 struct btree_head128 *head = &super->s_object_alias_tree;
463 struct logfs_block *block;
464 struct object_alias_item *item, *next;
465
466 if (!(super->s_flags & LOGFS_SB_FLAG_OBJ_ALIAS))
467 return;
468
469 block = btree_remove128(head, inode->i_ino, page->index);
470 if (!block)
471 return;
472
473 log_blockmove("move_btree_to_page(%llx, %llx, %x)\n",
474 block->ino, block->bix, block->level);
475 list_for_each_entry_safe(item, next, &block->item_list, list) {
476 data[item->child_no] = item->val;
477 list_del(&item->list);
478 mempool_free(item, super->s_alias_pool);
479 }
480 block->page = page;
481 SetPagePrivate(page);
482 page->private = (unsigned long)block;
483 block->ops = &indirect_block_ops;
484 initialize_block_counters(page, block, data, 0);
485}
486
487/*
488 * This silences a false, yet annoying gcc warning. I hate it when my editor
489 * jumps into bitops.h each time I recompile this file.
490 * TODO: Complain to gcc folks about this and upgrade compiler.
491 */
492static unsigned long fnb(const unsigned long *addr,
493 unsigned long size, unsigned long offset)
494{
495 return find_next_bit(addr, size, offset);
496}
497
498void move_page_to_btree(struct page *page)
499{
500 struct logfs_block *block = logfs_block(page);
501 struct super_block *sb = block->sb;
502 struct logfs_super *super = logfs_super(sb);
503 struct object_alias_item *item;
504 unsigned long pos;
505 __be64 *child;
506 int err;
507
508 if (super->s_flags & LOGFS_SB_FLAG_SHUTDOWN) {
509 block->ops->free_block(sb, block);
510 return;
511 }
512 log_blockmove("move_page_to_btree(%llx, %llx, %x)\n",
513 block->ino, block->bix, block->level);
514 super->s_flags |= LOGFS_SB_FLAG_OBJ_ALIAS;
515
516 for (pos = 0; ; pos++) {
517 pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
518 if (pos >= LOGFS_BLOCK_FACTOR)
519 break;
520
521 item = mempool_alloc(super->s_alias_pool, GFP_NOFS);
522 BUG_ON(!item); /* mempool empty */
523 memset(item, 0, sizeof(*item));
524
525 child = kmap_atomic(page, KM_USER0);
526 item->val = child[pos];
527 kunmap_atomic(child, KM_USER0);
528 item->child_no = pos;
529 list_add(&item->list, &block->item_list);
530 }
531 block->page = NULL;
532 ClearPagePrivate(page);
533 page->private = 0;
534 block->ops = &btree_block_ops;
535 err = alias_tree_insert(block->sb, block->ino, block->bix, block->level,
536 block);
537 BUG_ON(err); /* mempool empty */
538 ClearPageUptodate(page);
539}
540
541static int __logfs_segment_read(struct inode *inode, void *buf,
542 u64 ofs, u64 bix, level_t level)
543{
544 struct super_block *sb = inode->i_sb;
545 void *compressor_buf = logfs_super(sb)->s_compressed_je;
546 struct logfs_object_header oh;
547 __be32 crc;
548 u16 len;
549 int err, block_len;
550
551 block_len = obj_len(sb, obj_type(inode, level));
552 err = read_obj_header(sb, ofs, &oh);
553 if (err)
554 goto out_err;
555
556 err = -EIO;
557 if (be64_to_cpu(oh.ino) != inode->i_ino
558 || check_pos(sb, be64_to_cpu(oh.bix), bix, level)) {
559 printk(KERN_ERR"LOGFS: (ino, bix) don't match at %llx: "
560 "expected (%lx, %llx), got (%llx, %llx)\n",
561 ofs, inode->i_ino, bix,
562 be64_to_cpu(oh.ino), be64_to_cpu(oh.bix));
563 goto out_err;
564 }
565
566 len = be16_to_cpu(oh.len);
567
568 switch (oh.compr) {
569 case COMPR_NONE:
570 err = wbuf_read(sb, ofs + LOGFS_OBJECT_HEADERSIZE, len, buf);
571 if (err)
572 goto out_err;
573 crc = logfs_crc32(buf, len, 0);
574 if (crc != oh.data_crc) {
575 printk(KERN_ERR"LOGFS: uncompressed data crc error at "
576 "%llx: expected %x, got %x\n", ofs,
577 be32_to_cpu(oh.data_crc),
578 be32_to_cpu(crc));
579 goto out_err;
580 }
581 break;
582 case COMPR_ZLIB:
583 mutex_lock(&logfs_super(sb)->s_journal_mutex);
584 err = wbuf_read(sb, ofs + LOGFS_OBJECT_HEADERSIZE, len,
585 compressor_buf);
586 if (err) {
587 mutex_unlock(&logfs_super(sb)->s_journal_mutex);
588 goto out_err;
589 }
590 crc = logfs_crc32(compressor_buf, len, 0);
591 if (crc != oh.data_crc) {
592 printk(KERN_ERR"LOGFS: compressed data crc error at "
593 "%llx: expected %x, got %x\n", ofs,
594 be32_to_cpu(oh.data_crc),
595 be32_to_cpu(crc));
596 mutex_unlock(&logfs_super(sb)->s_journal_mutex);
597 goto out_err;
598 }
599 err = logfs_uncompress(compressor_buf, buf, len, block_len);
600 mutex_unlock(&logfs_super(sb)->s_journal_mutex);
601 if (err) {
602 printk(KERN_ERR"LOGFS: uncompress error at %llx\n", ofs);
603 goto out_err;
604 }
605 break;
606 default:
607 LOGFS_BUG(sb);
608 err = -EIO;
609 goto out_err;
610 }
611 return 0;
612
613out_err:
614 logfs_set_ro(sb);
615 printk(KERN_ERR"LOGFS: device is read-only now\n");
616 LOGFS_BUG(sb);
617 return err;
618}
619
620/**
621 * logfs_segment_read - read data block from object store
622 * @inode: inode containing data
623 * @buf: data buffer
624 * @ofs: physical data offset
625 * @bix: block index
626 * @level: block level
627 *
628 * Returns 0 on success or a negative errno.
629 */
630int logfs_segment_read(struct inode *inode, struct page *page,
631 u64 ofs, u64 bix, level_t level)
632{
633 int err;
634 void *buf;
635
636 if (PageUptodate(page))
637 return 0;
638
639 ofs &= ~LOGFS_FULLY_POPULATED;
640
641 buf = kmap(page);
642 err = __logfs_segment_read(inode, buf, ofs, bix, level);
643 if (!err) {
644 move_btree_to_page(inode, page, buf);
645 SetPageUptodate(page);
646 }
647 kunmap(page);
648 log_segment("logfs_segment_read(%lx, %llx, %x) %llx (%d)\n",
649 inode->i_ino, bix, level, ofs, err);
650 return err;
651}
652
653int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow)
654{
655 struct super_block *sb = inode->i_sb;
656 struct logfs_object_header h;
657 u16 len;
658 int err;
659
660 BUG_ON(logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN);
661 BUG_ON(shadow->old_ofs & LOGFS_FULLY_POPULATED);
662 if (!shadow->old_ofs)
663 return 0;
664
665 log_segment("logfs_segment_delete(%llx, %llx, %x) %llx->%llx %x->%x\n",
666 shadow->ino, shadow->bix, shadow->gc_level,
667 shadow->old_ofs, shadow->new_ofs,
668 shadow->old_len, shadow->new_len);
669 err = read_obj_header(sb, shadow->old_ofs, &h);
670 LOGFS_BUG_ON(err, sb);
671 LOGFS_BUG_ON(be64_to_cpu(h.ino) != inode->i_ino, sb);
672 LOGFS_BUG_ON(check_pos(sb, shadow->bix, be64_to_cpu(h.bix),
673 shrink_level(shadow->gc_level)), sb);
674
675 if (shadow->gc_level == 0)
676 len = be16_to_cpu(h.len);
677 else
678 len = obj_len(sb, h.type);
679 shadow->old_len = len + sizeof(h);
680 return 0;
681}
682
683static void freeseg(struct super_block *sb, u32 segno)
684{
685 struct logfs_super *super = logfs_super(sb);
686 struct address_space *mapping = super->s_mapping_inode->i_mapping;
687 struct page *page;
688 u64 ofs, start, end;
689
690 start = dev_ofs(sb, segno, 0);
691 end = dev_ofs(sb, segno + 1, 0);
692 for (ofs = start; ofs < end; ofs += PAGE_SIZE) {
693 page = find_get_page(mapping, ofs >> PAGE_SHIFT);
694 if (!page)
695 continue;
696 ClearPagePrivate(page);
697 page_cache_release(page);
698 }
699}
700
701int logfs_open_area(struct logfs_area *area, size_t bytes)
702{
703 struct super_block *sb = area->a_sb;
704 struct logfs_super *super = logfs_super(sb);
705 int err, closed = 0;
706
707 if (area->a_is_open && area->a_used_bytes + bytes <= super->s_segsize)
708 return 0;
709
710 if (area->a_is_open) {
711 u64 ofs = dev_ofs(sb, area->a_segno, area->a_written_bytes);
712 u32 len = super->s_segsize - area->a_written_bytes;
713
714 log_gc("logfs_close_area(%x)\n", area->a_segno);
715 pad_wbuf(area, 1);
716 super->s_devops->writeseg(area->a_sb, ofs, len);
717 freeseg(sb, area->a_segno);
718 closed = 1;
719 }
720
721 area->a_used_bytes = 0;
722 area->a_written_bytes = 0;
723again:
724 area->a_ops->get_free_segment(area);
725 area->a_ops->get_erase_count(area);
726
727 log_gc("logfs_open_area(%x, %x)\n", area->a_segno, area->a_level);
728 err = area->a_ops->erase_segment(area);
729 if (err) {
730 printk(KERN_WARNING "LogFS: Error erasing segment %x\n",
731 area->a_segno);
732 logfs_mark_segment_bad(sb, area->a_segno);
733 goto again;
734 }
735 area->a_is_open = 1;
736 return closed;
737}
738
739void logfs_sync_area(struct logfs_area *area)
740{
741 struct super_block *sb = area->a_sb;
742 struct logfs_super *super = logfs_super(sb);
743 u64 ofs = dev_ofs(sb, area->a_segno, area->a_written_bytes);
744 u32 len = (area->a_used_bytes - area->a_written_bytes);
745
746 if (super->s_writesize)
747 len &= ~(super->s_writesize - 1);
748 if (len == 0)
749 return;
750 pad_wbuf(area, 0);
751 super->s_devops->writeseg(sb, ofs, len);
752 area->a_written_bytes += len;
753}
754
755void logfs_sync_segments(struct super_block *sb)
756{
757 struct logfs_super *super = logfs_super(sb);
758 int i;
759
760 for_each_area(i)
761 logfs_sync_area(super->s_area[i]);
762}
763
764/*
765 * Pick a free segment to be used for this area. Effectively takes a
766 * candidate from the free list (not really a candidate anymore).
767 */
768static void ostore_get_free_segment(struct logfs_area *area)
769{
770 struct super_block *sb = area->a_sb;
771 struct logfs_super *super = logfs_super(sb);
772
773 if (super->s_free_list.count == 0) {
774 printk(KERN_ERR"LOGFS: ran out of free segments\n");
775 LOGFS_BUG(sb);
776 }
777
778 area->a_segno = get_best_cand(sb, &super->s_free_list, NULL);
779}
780
781static void ostore_get_erase_count(struct logfs_area *area)
782{
783 struct logfs_segment_entry se;
784 u32 ec_level;
785
786 logfs_get_segment_entry(area->a_sb, area->a_segno, &se);
787 BUG_ON(se.ec_level == cpu_to_be32(BADSEG) ||
788 se.valid == cpu_to_be32(RESERVED));
789
790 ec_level = be32_to_cpu(se.ec_level);
791 area->a_erase_count = (ec_level >> 4) + 1;
792}
793
794static int ostore_erase_segment(struct logfs_area *area)
795{
796 struct super_block *sb = area->a_sb;
797 struct logfs_segment_header sh;
798 u64 ofs;
799 int err;
800
801 err = logfs_erase_segment(sb, area->a_segno);
802 if (err)
803 return err;
804
805 sh.pad = 0;
806 sh.type = SEG_OSTORE;
807 sh.level = (__force u8)area->a_level;
808 sh.segno = cpu_to_be32(area->a_segno);
809 sh.ec = cpu_to_be32(area->a_erase_count);
810 sh.gec = cpu_to_be64(logfs_super(sb)->s_gec);
811 sh.crc = logfs_crc32(&sh, sizeof(sh), 4);
812
813 logfs_set_segment_erased(sb, area->a_segno, area->a_erase_count,
814 area->a_level);
815
816 ofs = dev_ofs(sb, area->a_segno, 0);
817 area->a_used_bytes = sizeof(sh);
818 logfs_buf_write(area, ofs, &sh, sizeof(sh));
819 return 0;
820}
821
822static const struct logfs_area_ops ostore_area_ops = {
823 .get_free_segment = ostore_get_free_segment,
824 .get_erase_count = ostore_get_erase_count,
825 .erase_segment = ostore_erase_segment,
826};
827
828static void free_area(struct logfs_area *area)
829{
830 if (area)
831 freeseg(area->a_sb, area->a_segno);
832 kfree(area);
833}
834
835static struct logfs_area *alloc_area(struct super_block *sb)
836{
837 struct logfs_area *area;
838
839 area = kzalloc(sizeof(*area), GFP_KERNEL);
840 if (!area)
841 return NULL;
842
843 area->a_sb = sb;
844 return area;
845}
846
847static void map_invalidatepage(struct page *page, unsigned long l)
848{
849 BUG();
850}
851
852static int map_releasepage(struct page *page, gfp_t g)
853{
854 /* Don't release these pages */
855 return 0;
856}
857
858static const struct address_space_operations mapping_aops = {
859 .invalidatepage = map_invalidatepage,
860 .releasepage = map_releasepage,
861 .set_page_dirty = __set_page_dirty_nobuffers,
862};
863
864int logfs_init_mapping(struct super_block *sb)
865{
866 struct logfs_super *super = logfs_super(sb);
867 struct address_space *mapping;
868 struct inode *inode;
869
870 inode = logfs_new_meta_inode(sb, LOGFS_INO_MAPPING);
871 if (IS_ERR(inode))
872 return PTR_ERR(inode);
873 super->s_mapping_inode = inode;
874 mapping = inode->i_mapping;
875 mapping->a_ops = &mapping_aops;
876 /* Would it be possible to use __GFP_HIGHMEM as well? */
877 mapping_set_gfp_mask(mapping, GFP_NOFS);
878 return 0;
879}
880
881int logfs_init_areas(struct super_block *sb)
882{
883 struct logfs_super *super = logfs_super(sb);
884 int i = -1;
885
886 super->s_alias_pool = mempool_create_kmalloc_pool(600,
887 sizeof(struct object_alias_item));
888 if (!super->s_alias_pool)
889 return -ENOMEM;
890
891 super->s_journal_area = alloc_area(sb);
892 if (!super->s_journal_area)
893 goto err;
894
895 for_each_area(i) {
896 super->s_area[i] = alloc_area(sb);
897 if (!super->s_area[i])
898 goto err;
899 super->s_area[i]->a_level = GC_LEVEL(i);
900 super->s_area[i]->a_ops = &ostore_area_ops;
901 }
902 btree_init_mempool128(&super->s_object_alias_tree,
903 super->s_btree_pool);
904 return 0;
905
906err:
907 for (i--; i >= 0; i--)
908 free_area(super->s_area[i]);
909 free_area(super->s_journal_area);
910 mempool_destroy(super->s_alias_pool);
911 return -ENOMEM;
912}
913
914void logfs_cleanup_areas(struct super_block *sb)
915{
916 struct logfs_super *super = logfs_super(sb);
917 int i;
918
919 btree_grim_visitor128(&super->s_object_alias_tree, 0, kill_alias);
920 for_each_area(i)
921 free_area(super->s_area[i]);
922 free_area(super->s_journal_area);
923 destroy_meta_inode(super->s_mapping_inode);
924}