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