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